• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "parcel.h"
22 #include "singleton.h"
23 
24 #include "i_networkshare_service.h"
25 #include "i_netshare_result_callback.h"
26 #include "i_sharing_event_callback.h"
27 
28 namespace OHOS {
29 namespace NetManagerStandard {
30 class NetworkShareClient {
31     DECLARE_DELAYED_SINGLETON(NetworkShareClient)
32 
33 public:
34     /**
35      * check if the sharing is supported
36      *
37      * @param supported NETWORKSHARE_IS_SUPPORTED(1) if supported, other is NETWORKSHARE_IS_UNSUPPORTED(0)
38      * @return NETMANAGER_EXT_SUCCESS(2200000) if process normal, others is error
39      */
40     int32_t IsSharingSupported(int32_t &supported);
41 
42     /**
43      * get the sharing running state, WiFi, Bluetooth, USB, as long as one of them is shared, it will return true
44      *
45      * @param sharingStatus NETWORKSHARE_IS_SHARING(1) if sharing running, others is NETWORKSHARE_IS_UNSHARING(0)
46      * @return NETMANAGER_EXT_SUCCESS(2200000) if process normal, others is error
47      */
48     int32_t IsSharing(int32_t &sharingStatus);
49 
50     /**
51      * start network by type
52      *
53      * @param type network sharing type, including Wifi, Bluetooth, USB
54      * @return NETMANAGER_EXT_SUCCESS(2200000) if process normal, others is error
55      */
56     int32_t StartSharing(const SharingIfaceType &type);
57 
58     /**
59      * stop network by type
60      *
61      * @param type network sharing type, including Wifi, Bluetooth, USB
62      * @return NETMANAGER_EXT_SUCCESS(2200000) if process normal, others is error
63      */
64     int32_t StopSharing(const SharingIfaceType &type);
65 
66     /**
67      * register the sharing state callback
68      *
69      * @param callback if this fuction return NETMANAGER_EXT_SUCCESS(2200000), this callback will be called by service
70      * @return NETMANAGER_EXT_SUCCESS(2200000) if process normal, others is error
71      */
72     int32_t RegisterSharingEvent(sptr<ISharingEventCallback> callback);
73 
74     /**
75      * unregister the sharing state callback
76      *
77      * @param callback if this fuction return NETMANAGER_EXT_SUCCESS(2200000), this callback will not be called by
78      * service
79      * @return NETMANAGER_EXT_SUCCESS(2200000) if process normal, others is error
80      */
81     int32_t UnregisterSharingEvent(sptr<ISharingEventCallback> callback);
82 
83     /**
84      * get the regexs data of the type.
85      * like these "usb\d" "wlan\d" "bt-pan"
86      *
87      * @param type the network sharing type, including Wifi, Bluetooth, USB
88      * @return regexs vector
89      */
90     int32_t GetSharableRegexs(const SharingIfaceType &type, std::vector<std::string> &ifaceRegexs);
91 
92     /**
93      * get sharing state by type
94      *
95      * @param type the network sharing type, including Wifi, Bluetooth, USB
96      * @param state the network sharing state, includes services, can services, errors
97      * @return Return NETMANAGER_EXT_SUCCESS(2200000) if process normal, others is error
98      */
99     int32_t GetSharingState(const SharingIfaceType &type, SharingIfaceState &state);
100 
101     /**
102      * get interface name by sharing state
103      * like these "usb0" "wlan0" "bt-pan"
104      *
105      * @param state the network sharing state, includes services, can services, errors
106      * @param ifaces interface name vector
107      * @return Return NETMANAGER_EXT_SUCCESS(2200000) if process normal, others is error
108      */
109     int32_t GetSharingIfaces(const SharingIfaceState &state, std::vector<std::string> &ifaces);
110 
111     /**
112      * Obtains the number of downlink data bytes of the sharing network interfaces.
113      *
114      * @param bytes network traffic data unit is KB
115      * @return Return NETMANAGER_EXT_SUCCESS(2200000) if process normal, others is error
116      */
117     int32_t GetStatsRxBytes(int32_t &bytes);
118 
119     /**
120      * Obtains the number of uplink data bytes of the sharing network interfaces.
121      *
122      * @param bytes network traffic data unit is KB
123      * @return Return NETMANAGER_EXT_SUCCESS(2200000) if process normal, others is error
124      */
125     int32_t GetStatsTxBytes(int32_t &bytes);
126 
127     /**
128      * Obtains the number of total data bytes of the sharing network interfaces.
129      *
130      * @param bytes network traffic data unit is KB
131      * @return Return NETMANAGER_EXT_SUCCESS(2200000) if process normal, others is error
132      */
133     int32_t GetStatsTotalBytes(int32_t &bytes);
134 
135 private:
136     class NetshareDeathRecipient : public IRemoteObject::DeathRecipient {
137     public:
NetshareDeathRecipient(NetworkShareClient & client)138         explicit NetshareDeathRecipient(NetworkShareClient &client) : client_(client) {}
139         ~NetshareDeathRecipient() override = default;
OnRemoteDied(const wptr<IRemoteObject> & remote)140         void OnRemoteDied(const wptr<IRemoteObject> &remote) override
141         {
142             client_.OnRemoteDied(remote);
143         }
144 
145     private:
146         NetworkShareClient &client_;
147     };
148 
149 private:
150     sptr<INetworkShareService> GetProxy();
151     void OnRemoteDied(const wptr<IRemoteObject> &remote);
152 
153 private:
154     std::mutex mutex_;
155     sptr<INetworkShareService> networkShareService_;
156     sptr<IRemoteObject::DeathRecipient> deathRecipient_;
157 };
158 } // namespace NetManagerStandard
159 } // namespace OHOS
160 #endif // NETWORKSHARE_CLIENT_H
161