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 "singleton.h"
20 #include "system_ability.h"
21 
22 #include "errorcode_convertor.h"
23 #include "networkshare_service_stub.h"
24 #include "networkshare_tracker.h"
25 
26 namespace OHOS {
27 namespace NetManagerStandard {
28 class NetworkShareService : public SystemAbility,
29                             public NetworkShareServiceStub,
30                             public std::enable_shared_from_this<NetworkShareService> {
31     DECLARE_DELAYED_SINGLETON(NetworkShareService)
32     DECLARE_SYSTEM_ABILITY(NetworkShareService)
33 
34     enum ServiceRunningState {
35         STATE_STOPPED = 0,
36         STATE_RUNNING,
37     };
38 
39 public:
40     /**
41      * service start
42      */
43     void OnStart() override;
44 
45     /**
46      * service stop
47      */
48     void OnStop() override;
49 
50     /**
51      * is surpport share network
52      */
53     int32_t IsNetworkSharingSupported(int32_t &supported) override;
54 
55     /**
56      * has shared network
57      */
58     int32_t IsSharing(int32_t &sharingStatus) override;
59 
60     /**
61      * start network by type
62      */
63     int32_t StartNetworkSharing(const SharingIfaceType &type) override;
64 
65     /**
66      * stop network by type
67      */
68     int32_t StopNetworkSharing(const SharingIfaceType &type) override;
69 
70     /**
71      * get sharable regex
72      */
73     int32_t GetSharableRegexs(SharingIfaceType type, std::vector<std::string> &ifaceRegexs) override;
74 
75     /**
76      * get sharing type
77      */
78     int32_t GetSharingState(SharingIfaceType type, SharingIfaceState &state) override;
79 
80     /**
81      * get sharing ifaces
82      */
83     int32_t GetNetSharingIfaces(const SharingIfaceState &state, std::vector<std::string> &ifaces) override;
84 
85     /**
86      * register callback
87      */
88     int32_t RegisterSharingEvent(sptr<ISharingEventCallback> callback) override;
89 
90     /**
91      * unregister callback
92      */
93     int32_t UnregisterSharingEvent(sptr<ISharingEventCallback> callback) override;
94 
95     /**
96      * get downlink data bytes
97      */
98     int32_t GetStatsRxBytes(int32_t &bytes) override;
99 
100     /**
101      * get uplink data bytes
102      */
103     int32_t GetStatsTxBytes(int32_t &bytes) override;
104 
105     /**
106      * get total data bytes
107      */
108     int32_t GetStatsTotalBytes(int32_t &bytes) override;
109 
110     /**
111      * dump function
112      */
113     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
114 
115 protected:
116     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
117     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
118 
119 private:
120     bool Init();
121     void GetDumpMessage(std::string &message);
122     void GetSharingType(const SharingIfaceType &type, const std::string &typeContent, std::string &sharingType);
123     void GetShareRegexsContent(const SharingIfaceType &type, std::string &shareRegexsContent);
124 
125     void OnNetSysRestart();
126     static void DisAllowNetworkShareEventCallback(const char *key, const char *value, void *context);
127 
128 private:
129     ServiceRunningState state_ = ServiceRunningState::STATE_STOPPED;
130     bool registerToService_ = false;
131 
132     bool hasSARemoved_ = false;
133 };
134 } // namespace NetManagerStandard
135 } // namespace OHOS
136 #endif // NETWORKSHARE_SERVICE_H
137