• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 TearDownWearableDistributedNet() override;
46     int32_t UpdateMeteredStatus(const bool isMetered) override;
47 
48 private:
49     class ReceiveMessage : public OHOS::EventFwk::CommonEventSubscriber {
50     public:
ReceiveMessage(const EventFwk::CommonEventSubscribeInfo & subscriberInfo,WearableDistributedNetService & WearableDistributedNetService)51         ReceiveMessage(const EventFwk::CommonEventSubscribeInfo &subscriberInfo,
52             WearableDistributedNetService &WearableDistributedNetService)
53             : EventFwk::CommonEventSubscriber(subscriberInfo),
54               WearableDistributedNetService_(WearableDistributedNetService) {};
55 
56         void OnReceiveEvent(const EventFwk::CommonEventData &eventData) override;
57 
58     private:
59         WearableDistributedNetService &WearableDistributedNetService_;
60     };
61 
62     enum ServiceRunningState {
63         STATE_STOPPED = 0,
64         STATE_RUNNING,
65     };
66 
67     bool Init();
68     void UpdateNetScore(const bool isCharging);
69     bool SubscribeCommonEvent();
70 
71 private:
72     ServiceRunningState state_ = ServiceRunningState::STATE_STOPPED;
73     bool registerToService_ = false;
74     std::shared_ptr<ReceiveMessage> subscriber_ = nullptr;
75 };
76 } // namespace NetManagerStandard
77 } // namespace OHOS
78 #endif // WEARABLE_DISTRIBUTED_NET_SERVICE_H
79