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 DATA_CONNECTION_MANAGER_H
17#define DATA_CONNECTION_MANAGER_H
18
19#include <tel_ril_data_parcel.h>
20#include <map>
21#include <memory>
22
23#include "data_connection_monitor.h"
24#include "state_machine.h"
25
26namespace OHOS {
27namespace Telephony {
28class CellularDataStateMachine;
29class DataConnectionManager : public StateMachine, public RefBase {
30public:
31    explicit DataConnectionManager(int32_t slotId);
32    ~DataConnectionManager();
33    void Init();
34    void AddConnectionStateMachine(const std::shared_ptr<CellularDataStateMachine> &stateMachine);
35    void RemoveConnectionStateMachine(const std::shared_ptr<CellularDataStateMachine> &stateMachine);
36    void AddActiveConnectionByCid(const std::shared_ptr<CellularDataStateMachine> &stateMachine);
37    std::shared_ptr<CellularDataStateMachine> GetActiveConnectionByCid(int32_t cid);
38    bool isNoActiveConnection();
39    std::map<int32_t, std::shared_ptr<CellularDataStateMachine>> GetActiveConnection();
40    bool IsBandwidthSourceModem() const;
41    void RemoveActiveConnectionByCid(int32_t cid);
42    void StartStallDetectionTimer();
43    void StopStallDetectionTimer();
44    void RegisterRadioObserver();
45    void UnRegisterRadioObserver() const;
46    void BeginNetStatistics();
47    void EndNetStatistics();
48    int32_t GetDataFlowType();
49    void SetDataFlowType(CellDataFlowType dataFlowType);
50    int32_t GetSlotId() const;
51    std::vector<std::shared_ptr<CellularDataStateMachine>> GetAllConnectionMachine();
52    void GetDefaultBandWidthsConfig();
53    void GetDefaultTcpBufferConfig();
54    LinkBandwidthInfo GetBandwidthsByRadioTech(const int32_t radioTech);
55    std::string GetTcpBufferByRadioTech(const int32_t radioTech);
56    void UpdateCallState(int32_t state);
57    int32_t GetDataRecoveryState();
58    void IsNeedDoRecovery(bool needDoRecovery) const;
59    void HandleScreenStateChanged(bool isScreenOn) const;
60
61private:
62    void UpdateBandWidthsUseLte();
63
64private:
65    std::shared_ptr<DataConnectionMonitor> connectionMonitor_;
66    std::vector<std::shared_ptr<CellularDataStateMachine>> stateMachines_;
67    std::map<int32_t, std::shared_ptr<CellularDataStateMachine>> cidActiveConnectionMap_;
68    std::mutex stateMachineMutex_;
69    std::mutex activeConnectionMutex_;
70    std::mutex tcpBufferConfigMutex_;
71    std::mutex bandwidthConfigMutex_;
72    sptr<State> ccmDefaultState_;
73    const int32_t slotId_;
74    std::map<std::string, LinkBandwidthInfo> bandwidthConfigMap_;
75    std::map<std::string, std::string> tcpBufferConfigMap_;
76    bool bandwidthSourceModem_ = true;
77    bool uplinkUseLte_ = false;
78};
79
80class CcmDefaultState : public State {
81public:
82    CcmDefaultState(DataConnectionManager &connectManager, std::string &&name)
83        : State(std::move(name)), connectManager_(connectManager)
84    {}
85    virtual ~CcmDefaultState() = default;
86    virtual void StateBegin();
87    virtual void StateEnd();
88    virtual bool StateProcess(const AppExecFwk::InnerEvent::Pointer &event);
89
90protected:
91    void RadioDataCallListChanged(const AppExecFwk::InnerEvent::Pointer &event);
92    void RadioLinkCapabilityChanged(const AppExecFwk::InnerEvent::Pointer &event);
93    void UpdateNetworkInfo(const AppExecFwk::InnerEvent::Pointer &event);
94
95private:
96    DataConnectionManager &connectManager_;
97};
98} // namespace Telephony
99} // namespace OHOS
100#endif // DATA_CONNECTION_MANAGER_H
101