1 /* 2 * Copyright (c) 2024 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 WEARABLE_DISTRIBUTED_NET_SERVICE_H 17 #define WEARABLE_DISTRIBUTED_NET_SERVICE_H 18 19 #include <cstdint> 20 #include "common_event_manager.h" 21 #include "common_event_subscriber.h" 22 #include "common_event_support.h" 23 #include "iservice_registry.h" 24 #include "system_ability.h" 25 #include "system_ability_definition.h" 26 #include "wearable_distributed_net_stub.h" 27 #include "wearable_distributed_net_management.h" 28 29 namespace OHOS { 30 namespace NetManagerStandard { 31 namespace { 32 using ChargeState = OHOS::PowerMgr::BatteryChargeState; 33 using Event = EventFwk::CommonEventSupport; 34 } 35 class WearableDistributedNetService : public SystemAbility, 36 public WearableDistributedNetStub { 37 DECLARE_SYSTEM_ABILITY(WearableDistributedNetService) 38 39 public: 40 WearableDistributedNetService(int32_t saId, bool runOnCreate = true); 41 ~WearableDistributedNetService(); 42 void OnStart() override; 43 void OnStop() override; 44 int32_t SetupWearableDistributedNet(int32_t tcpPortId, int32_t udpPortId, bool isMetered) override; 45 int32_t EnableWearableDistributedNet(bool enableFlag) override; 46 int32_t TearDownWearableDistributedNet() override; 47 int32_t UpdateMeteredStatus(const bool isMetered) override; 48 49 private: 50 class ReceiveMessage : public OHOS::EventFwk::CommonEventSubscriber { 51 public: ReceiveMessage(const EventFwk::CommonEventSubscribeInfo & subscriberInfo,WearableDistributedNetService & WearableDistributedNetService)52 ReceiveMessage(const EventFwk::CommonEventSubscribeInfo &subscriberInfo, 53 WearableDistributedNetService &WearableDistributedNetService) 54 : EventFwk::CommonEventSubscriber(subscriberInfo), 55 WearableDistributedNetService_(WearableDistributedNetService) {}; 56 57 void OnReceiveEvent(const EventFwk::CommonEventData &eventData) override; 58 59 private: 60 WearableDistributedNetService &WearableDistributedNetService_; 61 }; 62 63 enum ServiceRunningState { 64 STATE_STOPPED = 0, 65 STATE_RUNNING, 66 }; 67 68 bool Init(); 69 void UpdateNetScore(const bool isCharging); 70 bool SubscribeCommonEvent(); 71 72 private: 73 ServiceRunningState state_ = ServiceRunningState::STATE_STOPPED; 74 bool registerToService_ = false; 75 std::shared_ptr<ReceiveMessage> subscriber_ = nullptr; 76 }; 77 } // namespace NetManagerStandard 78 } // namespace OHOS 79 #endif // WEARABLE_DISTRIBUTED_NET_SERVICE_H 80