1 /*
2  * Copyright (C) 2021 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 CELLULAR_DATA_CLIENT_H
17 #define CELLULAR_DATA_CLIENT_H
18 
19 #include <cstdint>
20 #include <iremote_object.h>
21 #include <singleton.h>
22 
23 #include "data_sim_account_call_back.h"
24 #include "i_cellular_data_manager.h"
25 #include "sim_account_callback.h"
26 #include "apn_item.h"
27 #include "cellular_data_constant.h"
28 
29 namespace OHOS {
30 namespace Telephony {
31 class CellularDataClient : public DelayedRefSingleton<CellularDataClient> {
32     DECLARE_DELAYED_REF_SINGLETON(CellularDataClient);
33 
34 public:
35     /**
36      * @brief Whether cellular data service is connected.
37      *
38      * @return Return true on connected, false on not connected.
39      */
40     bool IsConnect();
41 
42     /**
43      * @brief Whether to enable cellular data user switch
44      *
45      * @param enable Enable or not.
46      * @return Return 84082688 invalid parameter, 0 data enable success, others enable fail.
47      */
48     int32_t EnableCellularData(bool enable);
49 
50     /**
51      * @brief Whether to enable intelligence switch
52      *
53      * @param enable Enable or not.
54      * @return Return 84082688 invalid parameter, 1 data enable success, 0 enable fail.
55      */
56     int32_t EnableIntelligenceSwitch(bool enable);
57 
58     /**
59      * @brief Whether the cellular data user switch is enabled
60      *
61      * @param dataEnabled Indicates the result of data enabled status.
62      * @return Returns error code.
63      */
64     int32_t IsCellularDataEnabled(bool &dataEnabled);
65 
66     /**
67      * @brief Cellular data connection status
68      *
69      * @return Returns data connection status defined in DataConnectionStatus.
70      */
71     int32_t GetCellularDataState();
72 
73     /**
74      * @brief Get the apn status based on slotId and apnType
75      *
76      * @return Returns apn status
77      */
78     int32_t GetApnState(int32_t slotId, const std::string &apnType);
79 
80     /**
81      * Get IntelligenceSwitch State
82      *
83      * @param switchState Returns IntelligenceSwitch State
84      * @return Returns 0 on success, others on failure.
85      */
86     int32_t GetIntelligenceSwitchState(bool &switchState);
87 
88     /**
89      * @brief Get recovery state
90      */
91     int32_t GetDataRecoveryState();
92 
93     /**
94      * @brief Whether roaming is allowed
95      *
96      * @param slotId Indicates card slot identification
97      * @param dataRoamingEnabled Indicates the result of data roaming enabled status.
98      * @return Returns error code.
99      */
100     int32_t IsCellularDataRoamingEnabled(int32_t slotId, bool &dataRoamingEnabled);
101 
102     /**
103      * @brief Whether roaming switches are allowed
104      *
105      * @param slotId card slot identification
106      * @param enable Whether roaming switches are allowed
107      * @return Returns 0 on failure, 1 on failure. 84082688 invalid parameter
108      */
109     int32_t EnableCellularDataRoaming(int32_t slotId, bool enable);
110 
111     /**
112      * @brief Get the slotId that uses the data traffic by default
113      *
114      * @return Returns the default settings data card, -1 error code
115      */
116     int32_t GetDefaultCellularDataSlotId();
117 
118     /**
119      * Get the simId that uses the data traffic by default
120      *
121      * @param simId Returns default settings data sim id
122      * @return Returns 0 on success, others on failure.
123      */
124     int32_t GetDefaultCellularDataSimId(int32_t &simId);
125 
126     /**
127      * @brief Set the slotId that uses the data traffic by default
128      *
129      * @param slotId card slot identification
130      * @return 0 set success, others set fail, 84082688 invalid parameter
131      */
132     int32_t SetDefaultCellularDataSlotId(int32_t slotId);
133 
134     /**
135      * @brief Get data packet type
136      *
137      * @return Returns cell data flow type defined in CellDataFlowType.
138      */
139     int32_t GetCellularDataFlowType();
140 
141     /**
142      * @brief Whether cellular data has internet capability.
143      *
144      * @param slotId Card slot identification.
145      * @param cid Context identification.
146      * @return Return 1 if has, 0 if hasn't.
147      */
148     int32_t HasInternetCapability(int32_t slotId, int32_t cid);
149 
150     /**
151      * @brief Clear cellular data connections.
152      *
153      * @param slotId Card slot identification.
154      * @return 1 set success, 0 set fail, 84082688 invalid parameter
155      */
156     int32_t ClearCellularDataConnections(int32_t slotId);
157 
158     int32_t ClearAllConnections(int32_t slotId, DisConnectionReason reason);
159 
160     int32_t HandleApnChanged(int32_t slotId);
161 
162     /**
163      * @brief Get cellular data proxy.
164      *
165      * @return Cellular data service.
166      */
167     sptr<ICellularDataManager> GetProxy();
168 
169     /**
170      * @brief Update the slotId that uses the data traffic by default
171      *
172      * @return Returns the default settings data card, -1 error code
173      */
174     int32_t UpdateDefaultCellularDataSlotId();
175 
176     /**
177      * @brief Get data connections apn attribute.
178      *
179      * @param slotId Card slot identification.
180      * @param apnType Indicates the APN attribute used by the data connection.
181      * @return 1 set success, 0 set fail
182      */
183     int32_t GetDataConnApnAttr(int32_t slotId, ApnItem::Attribute &apnAttr);
184 
185     /**
186      * @brief Get data connections ip type.
187      *
188      * @param slotId Card slot identification.
189      * @param ipType Indicates IP array after data connection.
190      * @return 1 set success, 0 set fail
191      */
192     int32_t GetDataConnIpType(int32_t slotId, std::string &ipType);
193 
194     /**
195      * @brief Whether do recovery is needed.
196      *
197      * @param slotId Card slot identification.
198      * @param needDoRecovery Whether do recovery is needed.
199      * @return 1 set success, 0 set fail
200      */
201     int32_t IsNeedDoRecovery(int32_t slotId, bool needDoRecovery);
202 
203     /**
204      * @brief Init CellularDataController instance.
205      *
206      * @param slotId Card slot identification.
207      * @return 1 set success, 0 set fail
208      */
209     int32_t InitCellularDataController(int32_t slotId);
210 
211 private:
212     class CellularDataDeathRecipient : public IRemoteObject::DeathRecipient {
213     public:
CellularDataDeathRecipient(CellularDataClient & client)214         explicit CellularDataDeathRecipient(CellularDataClient &client) : client_(client) {}
215         ~CellularDataDeathRecipient() override = default;
OnRemoteDied(const wptr<IRemoteObject> & remote)216         void OnRemoteDied(const wptr<IRemoteObject> &remote) override
217         {
218             client_.OnRemoteDied(remote);
219         }
220 
221     private:
222         CellularDataClient &client_;
223     };
224 
225     void OnRemoteDied(const wptr<IRemoteObject> &remote);
226     void RegisterSimAccountCallback();
227     void UnregisterSimAccountCallback();
228     bool IsValidSlotId(int32_t slotId);
229     bool IsCellularDataSysAbilityExist(sptr<IRemoteObject> &object);
230 
231 private:
232     std::mutex mutexProxy_;
233     sptr<ICellularDataManager> proxy_ { nullptr };
234     sptr<IRemoteObject::DeathRecipient> deathRecipient_ { nullptr };
235     sptr<SimAccountCallback> callback_ { nullptr };
236     static int32_t defaultCellularDataSlotId_;
237     static int32_t defaultCellularDataSimId_;
238     bool registerStatus_ = false;
239 };
240 } // namespace Telephony
241 } // namespace OHOS
242 #endif // CELLULAR_DATA_CLIENT_H
243