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_SERVICE_H 17 #define NETWORKSHARE_SERVICE_H 18 19 #include <memory> 20 #include "common_event.h" 21 #include "common_event_data.h" 22 #include "common_event_manager.h" 23 #include "common_event_support.h" 24 #include "singleton.h" 25 #include "system_ability.h" 26 #include "errorcode_convertor.h" 27 #include "networkshare_service_stub.h" 28 #include "networkshare_tracker.h" 29 #include "ffrt.h" 30 31 namespace OHOS { 32 namespace NetManagerStandard { 33 class NetworkShareService : public SystemAbility, 34 public NetworkShareServiceStub, 35 public std::enable_shared_from_this<NetworkShareService> { 36 DECLARE_DELAYED_SINGLETON(NetworkShareService) 37 DECLARE_SYSTEM_ABILITY(NetworkShareService) 38 39 enum ServiceRunningState { 40 STATE_STOPPED = 0, 41 STATE_RUNNING, 42 }; 43 44 class CommonEventSubscriber : public OHOS::EventFwk::CommonEventSubscriber { 45 public: CommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)46 explicit CommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo) 47 : OHOS::EventFwk::CommonEventSubscriber(subscribeInfo) {}; 48 virtual void OnReceiveEvent(const EventFwk::CommonEventData &eventData) override; 49 }; 50 51 #ifdef SHARE_NOTIFICATION_ENABLE 52 class WifiShareNtfSubscriber : public OHOS::EventFwk::CommonEventSubscriber { 53 public: WifiShareNtfSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)54 explicit WifiShareNtfSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo) 55 : OHOS::EventFwk::CommonEventSubscriber(subscribeInfo) {}; 56 virtual void OnReceiveEvent(const EventFwk::CommonEventData &eventData) override; 57 }; 58 #endif 59 public: 60 /** 61 * service start 62 */ 63 void OnStart() override; 64 65 /** 66 * service stop 67 */ 68 void OnStop() override; 69 70 /** 71 * is surpport share network 72 */ 73 int32_t IsNetworkSharingSupported(int32_t &supported) override; 74 75 /** 76 * has shared network 77 */ 78 int32_t IsSharing(int32_t &sharingStatus) override; 79 80 /** 81 * start network by type 82 */ 83 int32_t StartNetworkSharing(const SharingIfaceType &type) override; 84 85 /** 86 * stop network by type 87 */ 88 int32_t StopNetworkSharing(const SharingIfaceType &type) override; 89 90 /** 91 * get sharable regex 92 */ 93 int32_t GetSharableRegexs(SharingIfaceType type, std::vector<std::string> &ifaceRegexs) override; 94 95 /** 96 * get sharing type 97 */ 98 int32_t GetSharingState(SharingIfaceType type, SharingIfaceState &state) override; 99 100 /** 101 * get sharing ifaces 102 */ 103 int32_t GetNetSharingIfaces(const SharingIfaceState &state, std::vector<std::string> &ifaces) override; 104 105 /** 106 * register callback 107 */ 108 int32_t RegisterSharingEvent(sptr<ISharingEventCallback> callback) override; 109 110 /** 111 * unregister callback 112 */ 113 int32_t UnregisterSharingEvent(sptr<ISharingEventCallback> callback) override; 114 115 /** 116 * get downlink data bytes 117 */ 118 int32_t GetStatsRxBytes(int32_t &bytes) override; 119 120 /** 121 * get uplink data bytes 122 */ 123 int32_t GetStatsTxBytes(int32_t &bytes) override; 124 125 /** 126 * get total data bytes 127 */ 128 int32_t GetStatsTotalBytes(int32_t &bytes) override; 129 130 /** 131 * dump function 132 */ 133 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 134 135 /** 136 * set sysctl prop 137 */ 138 int32_t SetConfigureForShare(bool enabled) override; 139 140 protected: 141 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 142 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 143 144 private: 145 bool Init(); 146 void GetDumpMessage(std::string &message); 147 void GetSharingType(const SharingIfaceType &type, const std::string &typeContent, std::string &sharingType); 148 void GetShareRegexsContent(const SharingIfaceType &type, std::string &shareRegexsContent); 149 150 void OnNetSysRestart(); 151 static void DisAllowNetworkShareEventCallback(const char *key, const char *value, void *context); 152 void SubscribeCommonEvent(); 153 #ifdef SHARE_NOTIFICATION_ENABLE 154 void SubscribeWifiShareNtfEvent(); 155 #endif 156 157 private: 158 ServiceRunningState state_ = ServiceRunningState::STATE_STOPPED; 159 bool registerToService_ = false; 160 std::shared_ptr<CommonEventSubscriber> commonEventSubscriber_ = nullptr; 161 #ifdef SHARE_NOTIFICATION_ENABLE 162 std::shared_ptr<WifiShareNtfSubscriber> wifiShareNtfSubscriber_ = nullptr; 163 #endif 164 bool hasSARemoved_ = false; 165 int32_t setConfigTimes_ = 0; 166 ffrt::mutex setConfigureMutex_; 167 ffrt::mutex openFileMutex_; 168 }; 169 } // namespace NetManagerStandard 170 } // namespace OHOS 171 #endif // NETWORKSHARE_SERVICE_H 172