1 /* 2 * Copyright (c) 2022 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 16 #ifndef NETWORKSHARE_CLIENT_H 17 #define NETWORKSHARE_CLIENT_H 18 19 #include <string> 20 21 #include "i_netshare_result_callback.h" 22 #include "i_networkshare_service.h" 23 #include "i_sharing_event_callback.h" 24 #include "parcel.h" 25 #include "singleton.h" 26 #include "system_ability_load_callback_stub.h" 27 28 namespace OHOS { 29 namespace NetManagerStandard { 30 class NetworkShareLoadCallback : public SystemAbilityLoadCallbackStub { 31 public: 32 void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject) override; 33 void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; 34 bool IsFailed(); 35 const sptr<IRemoteObject> &GetRemoteObject() const; 36 37 private: 38 bool loadSAFailed_ = false; 39 sptr<IRemoteObject> remoteObject_ = nullptr; 40 }; 41 class NetworkShareClient { 42 DECLARE_DELAYED_SINGLETON(NetworkShareClient) 43 44 public: 45 /** 46 * check if the sharing is supported 47 * 48 * @param supported NETWORKSHARE_IS_SUPPORTED(1) if supported, other is NETWORKSHARE_IS_UNSUPPORTED(0) 49 * @return NETMANAGER_EXT_SUCCESS if process normal, others is error 50 * @permission ohos.permission.CONNECTIVITY_INTERNAL 51 * @systemapi Hide this for inner system use. 52 */ 53 int32_t IsSharingSupported(int32_t &supported); 54 55 /** 56 * get the sharing running state, WiFi, Bluetooth, USB, as long as one of them is shared, it will return true 57 * 58 * @param sharingStatus NETWORKSHARE_IS_SHARING(1) if sharing running, others is NETWORKSHARE_IS_UNSHARING(0) 59 * @return NETMANAGER_EXT_SUCCESS if process normal, others is error 60 * @permission ohos.permission.CONNECTIVITY_INTERNAL 61 * @systemapi Hide this for inner system use. 62 */ 63 int32_t IsSharing(int32_t &sharingStatus); 64 65 /** 66 * start network by type 67 * 68 * @param type network sharing type, including Wifi, Bluetooth, USB 69 * @return NETMANAGER_EXT_SUCCESS if process normal, others is error 70 * @permission ohos.permission.CONNECTIVITY_INTERNAL 71 * @systemapi Hide this for inner system use. 72 */ 73 int32_t StartSharing(const SharingIfaceType &type); 74 75 /** 76 * stop network by type 77 * 78 * @param type network sharing type, including Wifi, Bluetooth, USB 79 * @return NETMANAGER_EXT_SUCCESS if process normal, others is error 80 * @permission ohos.permission.CONNECTIVITY_INTERNAL 81 * @systemapi Hide this for inner system use. 82 */ 83 int32_t StopSharing(const SharingIfaceType &type); 84 85 /** 86 * register the sharing state callback 87 * 88 * @param callback if this fuction return NETMANAGER_EXT_SUCCESS, this callback will be called by service 89 * @return NETMANAGER_EXT_SUCCESS if process normal, others is error 90 * @permission ohos.permission.CONNECTIVITY_INTERNAL 91 * @systemapi Hide this for inner system use. 92 */ 93 int32_t RegisterSharingEvent(sptr<ISharingEventCallback> callback); 94 95 /** 96 * unregister the sharing state callback 97 * 98 * @param callback if this fuction return NETMANAGER_EXT_SUCCESS, this callback will not be called by 99 * service 100 * @return NETMANAGER_EXT_SUCCESS if process normal, others is error 101 * @permission ohos.permission.CONNECTIVITY_INTERNAL 102 * @systemapi Hide this for inner system use. 103 */ 104 int32_t UnregisterSharingEvent(sptr<ISharingEventCallback> callback); 105 106 /** 107 * get the regexs data of the type. 108 * like these "usb\d" "wlan\d" "bt-pan" 109 * 110 * @param type the network sharing type, including Wifi, Bluetooth, USB 111 * @param ifaceRegexs get list of interface sharable regex 112 * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error 113 * @permission ohos.permission.CONNECTIVITY_INTERNAL 114 * @systemapi Hide this for inner system use. 115 */ 116 int32_t GetSharableRegexs(const SharingIfaceType &type, std::vector<std::string> &ifaceRegexs); 117 118 /** 119 * get sharing state by type 120 * 121 * @param type the network sharing type, including Wifi, Bluetooth, USB 122 * @param state the network sharing state, includes services, can services, errors 123 * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error 124 * @permission ohos.permission.CONNECTIVITY_INTERNAL 125 * @systemapi Hide this for inner system use. 126 */ 127 int32_t GetSharingState(const SharingIfaceType &type, SharingIfaceState &state); 128 129 /** 130 * get interface name by sharing state, like these "usb0" "wlan0" "bt-pan" 131 * 132 * @param state the network sharing state, includes services, can services, errors 133 * @param ifaces interface name vector 134 * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error 135 * @permission ohos.permission.CONNECTIVITY_INTERNAL 136 * @systemapi Hide this for inner system use. 137 */ 138 int32_t GetSharingIfaces(const SharingIfaceState &state, std::vector<std::string> &ifaces); 139 140 /** 141 * Obtains the number of downlink data bytes of the sharing network interfaces. 142 * 143 * @param bytes network traffic data unit is KB 144 * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error 145 * @permission ohos.permission.CONNECTIVITY_INTERNAL 146 * @systemapi Hide this for inner system use. 147 */ 148 int32_t GetStatsRxBytes(int32_t &bytes); 149 150 /** 151 * Obtains the number of uplink data bytes of the sharing network interfaces. 152 * 153 * @param bytes network traffic data unit is KB 154 * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error 155 * @permission ohos.permission.CONNECTIVITY_INTERNAL 156 * @systemapi Hide this for inner system use. 157 */ 158 int32_t GetStatsTxBytes(int32_t &bytes); 159 160 /** 161 * Obtains the number of total data bytes of the sharing network interfaces. 162 * 163 * @param bytes network traffic data unit is KB 164 * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error 165 * @permission ohos.permission.CONNECTIVITY_INTERNAL 166 * @systemapi Hide this for inner system use. 167 */ 168 int32_t GetStatsTotalBytes(int32_t &bytes); 169 170 private: 171 void RestartNetTetheringManagerSysAbility(); 172 173 private: 174 class NetshareDeathRecipient : public IRemoteObject::DeathRecipient { 175 public: NetshareDeathRecipient(NetworkShareClient & client)176 explicit NetshareDeathRecipient(NetworkShareClient &client) : client_(client) {} 177 ~NetshareDeathRecipient() override = default; OnRemoteDied(const wptr<IRemoteObject> & remote)178 void OnRemoteDied(const wptr<IRemoteObject> &remote) override 179 { 180 client_.OnRemoteDied(remote); 181 } 182 183 private: 184 NetworkShareClient &client_; 185 }; 186 187 private: 188 sptr<INetworkShareService> GetProxy(); 189 void OnRemoteDied(const wptr<IRemoteObject> &remote); 190 191 private: 192 std::mutex mutex_; 193 sptr<INetworkShareService> networkShareService_; 194 sptr<IRemoteObject::DeathRecipient> deathRecipient_; 195 }; 196 } // namespace NetManagerStandard 197 } // namespace OHOS 198 #endif // NETWORKSHARE_CLIENT_H 199