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_SERVICE_H
17 #define CELLULAR_DATA_SERVICE_H
18 
19 #include "iremote_object.h"
20 #include "nocopyable.h"
21 #include "singleton.h"
22 #include "system_ability.h"
23 
24 #include "cellular_data_service_stub.h"
25 #include "cellular_data_constant.h"
26 #include "cellular_data_controller.h"
27 #include "traffic_management.h"
28 #include "apn_item.h"
29 
30 namespace OHOS {
31 namespace Telephony {
32 enum class ServiceRunningState {
33     STATE_NOT_START,
34     STATE_RUNNING,
35 };
36 
37 class CellularDataService : public SystemAbility, public CellularDataServiceStub {
38     DECLARE_DELAYED_REF_SINGLETON(CellularDataService)
39     DECLARE_SYSTEM_ABILITY(CellularDataService)
40 
41 public:
42     /**
43      * service Start
44      */
45     void OnStart() override;
46     /**
47      * service OnStop
48      */
49     void OnStop() override;
50     int32_t Dump(std::int32_t fd, const std::vector<std::u16string>& args) override;
51     std::string GetBeginTime();
52     std::string GetEndTime();
53     std::string GetCellularDataSlotIdDump();
54     std::string GetStateMachineCurrentStatusDump();
55     std::string GetFlowDataInfoDump();
56     int32_t IsCellularDataEnabled(bool &dataEnabled) override;
57     int32_t EnableCellularData(bool enable) override;
58     int32_t GetCellularDataState() override;
59     int32_t IsCellularDataRoamingEnabled(const int32_t slotId, bool &dataRoamingEnabled) override;
60     int32_t EnableCellularDataRoaming(const int32_t slotId, bool enable) override;
61     int32_t HandleApnChanged(const int32_t slotId) override;
62     int32_t GetDefaultCellularDataSlotId() override;
63     int32_t GetDefaultCellularDataSimId(int32_t &simId) override;
64     int32_t SetDefaultCellularDataSlotId(const int32_t slotId) override;
65     int32_t GetCellularDataFlowType() override;
66     void DispatchEvent(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event);
67     int32_t HasInternetCapability(const int32_t slotId, const int32_t cid) override;
68     int32_t ClearCellularDataConnections(const int32_t slotId) override;
69     int32_t ClearAllConnections(const int32_t slotId, DisConnectionReason reason) override;
70     int32_t ChangeConnectionForDsds(const int32_t slotId, bool enable);
71     int32_t StrategySwitch(int32_t slotId, bool enable);
72     int32_t RequestNet(const NetRequest &request);
73     int32_t ReleaseNet(const NetRequest &request);
74     int32_t GetServiceRunningState();
75     int64_t GetSpendTime();
76     int32_t GetApnState(int32_t slotId, const std::string &apnType) override;
77     int32_t GetDataRecoveryState() override;
78     int32_t RegisterSimAccountCallback(const sptr<SimAccountCallback> callback) override;
79     int32_t UnregisterSimAccountCallback() override;
80     int32_t GetDataConnApnAttr(int32_t slotId, ApnItem::Attribute &apnAttr) override;
81     int32_t GetDataConnIpType(int32_t slotId, std::string &ipType) override;
82     int32_t IsNeedDoRecovery(int32_t slotId, bool needDoRecovery) override;
83     int32_t EnableIntelligenceSwitch(bool enable) override;
84     int32_t InitCellularDataController(int32_t slotId) override;
85     int32_t GetIntelligenceSwitchState(bool &switchState) override;
86 
87 private:
88     bool Init();
89     void InitModule();
90     void UnRegisterAllNetSpecifier();
91     void AddNetSupplier(int32_t slotId, CellularDataNetAgent &netAgent, std::vector<uint64_t> &netCapabilities);
92     void ClearCellularDataControllers();
93     void AddCellularDataControllers(int32_t slotId, std::shared_ptr<CellularDataController> cellularDataController);
94     std::shared_ptr<CellularDataController> GetCellularDataController(int32_t slotId);
95 
96 private:
97     std::map<int32_t, std::shared_ptr<CellularDataController>> cellularDataControllers_;
98     bool registerToService_;
99     int64_t beginTime_ = 0L;
100     int64_t endTime_ = 0L;
101     ServiceRunningState state_;
102     std::mutex mapLock_;
103     bool isInitSuccess_ = false;
104 };
105 } // namespace Telephony
106 } // namespace OHOS
107 #endif // CELLULAR_DATA_SERVICE_H
108