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
29namespace OHOS {
30namespace Telephony {
31class CellularDataClient : public DelayedRefSingleton<CellularDataClient> {
32    DECLARE_DELAYED_REF_SINGLETON(CellularDataClient);
33
34public:
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    int32_t EstablishAllApnsIfConnectable(int32_t slotId);
212
213    /**
214     * @brief Release cellular data connection.
215     *
216     * @param slotId Card slot identification.
217     * @return 1 set success, 0 set fail
218     */
219    int32_t ReleaseCellularDataConnection(int32_t slotId);
220
221    /**
222     * @brief Get cellular data supplierId.
223     *
224     * @param slotId Card slot identification.
225     * @param capability Net capability.
226     * @param supplierId Cellular supplier id.
227     * @return 0 get success, others get fail.
228     */
229    int32_t GetCellularDataSupplierId(int32_t slotId, uint64_t capability, uint32_t &supplierId);
230
231    /**
232     * @brief Correct net supplier available is false.
233     *
234     * @param slotId Card slot identification.
235     * @return 0 set success, others set fail.
236     */
237    int32_t CorrectNetSupplierNoAvailable(int32_t slotId);
238
239    /**
240     * @brief Get supplier register state.
241     *
242     * @param supplierId Cellular supplier id.
243     * @param regState Supplier register state.
244     * @return 0 set success, others set fail.
245     */
246    int32_t GetSupplierRegisterState(uint32_t supplierId, int32_t &regState);
247
248private:
249    class CellularDataDeathRecipient : public IRemoteObject::DeathRecipient {
250    public:
251        explicit CellularDataDeathRecipient(CellularDataClient &client) : client_(client) {}
252        ~CellularDataDeathRecipient() override = default;
253        void OnRemoteDied(const wptr<IRemoteObject> &remote) override
254        {
255            client_.OnRemoteDied(remote);
256        }
257
258    private:
259        CellularDataClient &client_;
260    };
261
262    void OnRemoteDied(const wptr<IRemoteObject> &remote);
263    void RegisterSimAccountCallback();
264    void UnregisterSimAccountCallback();
265    bool IsValidSlotId(int32_t slotId);
266    bool IsCellularDataSysAbilityExist(sptr<IRemoteObject> &object);
267
268private:
269    std::mutex mutexProxy_;
270    sptr<ICellularDataManager> proxy_ { nullptr };
271    sptr<IRemoteObject::DeathRecipient> deathRecipient_ { nullptr };
272    sptr<SimAccountCallback> callback_ { nullptr };
273    static int32_t defaultCellularDataSlotId_;
274    static int32_t defaultCellularDataSimId_;
275    bool registerStatus_ = false;
276};
277} // namespace Telephony
278} // namespace OHOS
279#endif // CELLULAR_DATA_CLIENT_H
280