1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef NATIVE_NET_CONN_ADAPTER_H 16 #define NATIVE_NET_CONN_ADAPTER_H 17 18 #include <map> 19 #include <mutex> 20 21 #include "http_proxy.h" 22 #include "net_all_capabilities.h" 23 #include "net_conn_callback_stub.h" 24 #include "net_connection_type.h" 25 #include "net_handle.h" 26 #include "net_link_info.h" 27 #include "refbase.h" 28 29 namespace OHOS::NetManagerStandard { 30 31 int32_t Conv2NetHandle(NetHandle &netHandleObj, NetConn_NetHandle *netHandle); 32 33 int32_t Conv2NetHandleObj(NetConn_NetHandle *netHandle, NetHandle &netHandleObj); 34 35 int32_t Conv2NetHandleList(const std::list<sptr<NetHandle>> &netHandleObjList, NetConn_NetHandleList *netHandleList); 36 37 int32_t Conv2NetLinkInfo(NetLinkInfo &infoObj, NetConn_ConnectionProperties *prop); 38 39 int32_t Conv2NetAllCapabilities(NetAllCapabilities &netAllCapsObj, NetConn_NetCapabilities *netAllCaps); 40 41 int32_t ConvFromNetAllCapabilities(NetAllCapabilities &netAllCapsObj, NetConn_NetCapabilities *netAllCaps); 42 43 int32_t Conv2HttpProxy(const HttpProxy &httpProxyObj, NetConn_HttpProxy *httpProxy); 44 45 void ConvertNetConn2HttpProxy(const NetConn_HttpProxy &netConn, HttpProxy &httpProxyObj); 46 47 class NetConnCallbackStubAdapter : public NetConnCallbackStub { 48 public: 49 NetConnCallbackStubAdapter(NetConn_NetConnCallback *callback); 50 51 int32_t NetAvailable(sptr<NetHandle> &netHandle) override; 52 int32_t NetCapabilitiesChange(sptr<NetHandle> &netHandle, const sptr<NetAllCapabilities> &netAllCap) override; 53 int32_t NetConnectionPropertiesChange(sptr<NetHandle> &netHandle, const sptr<NetLinkInfo> &info) override; 54 int32_t NetLost(sptr<NetHandle> &netHandle) override; 55 int32_t NetUnavailable() override; 56 int32_t NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked) override; 57 58 private: 59 NetConn_NetConnCallback callback_{}; 60 }; 61 62 class NetConnCallbackManager { 63 public: 64 static NetConnCallbackManager &GetInstance(); 65 int32_t RegisterNetConnCallback(NetConn_NetSpecifier *specifier, NetConn_NetConnCallback *netConnCallback, 66 const uint32_t &timeout, uint32_t *callbackId); 67 int32_t UnregisterNetConnCallback(uint32_t callbackId); 68 69 private: 70 NetConnCallbackManager() = default; 71 std::mutex callbackMapMutex_; 72 std::map<uint32_t, sptr<INetConnCallback>> callbackMap_; 73 uint32_t index_{0}; 74 }; 75 76 } // namespace OHOS::NetManagerStandard 77 #endif /* NATIVE_NET_CONN_ADAPTER_H */