• 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 PAN_SERVICE_H
17 #define PAN_SERVICE_H
18 
19 #include <memory.h>
20 #include <list>
21 #include <mutex>
22 #include <vector>
23 #include <cmath>
24 #include <cstring>
25 #include "log.h"
26 #include "packet.h"
27 #include "securec.h"
28 #include "adapter_config.h"
29 #include "base_observer_list.h"
30 #include "class_creator.h"
31 #include "context.h"
32 #include "interface_profile_pan.h"
33 
34 
35 #include "profile_config.h"
36 #include "profile_service_manager.h"
37 #include "raw_address.h"
38 #include "pan_bnep.h"
39 #include "pan_message.h"
40 #include "pan_sdp.h"
41 #include "base_def.h"
42 #include "pan_statemachine.h"
43 
44 namespace OHOS {
45 namespace bluetooth {
46 class PanService : public IProfilePan, public utility::Context {
47 public:
48     /**
49      * @brief Get the instance of the HfpHfService object.
50      *
51      * @return Returns the instance of the HfpHfService object.
52      */
53     static PanService *GetService();
54     /**
55      * @brief Construct a new Pan Service object
56      *
57      */
58     PanService();
59     /**
60      * @brief Destroy the Pan Service object
61      *
62      */
63     virtual ~PanService();
64     utility::Context *GetContext() override;
65     void Enable(void) override;
66     void Disable(void) override;
67     int Connect(const RawAddress &device) override;
68     std::list<RawAddress> GetConnectDevices() override;
69     int GetConnectState(void) override;
70     int GetMaxConnectNum(void) override;
71     int Disconnect(const RawAddress &device) override;
72     std::vector<RawAddress> GetDevicesByStates(std::vector<int> states) override;
73     int GetDeviceState(const RawAddress &device) override;
74     void RegisterObserver(IPanObserver &PanObserver) override;
75     void DeregisterObserver(IPanObserver &PanObserver) override;
76     bool SetTethering(bool enable)override;
77     bool IsTetheringOn()override;
78     void NotifyStateChanged(const RawAddress &device, int state);
79     void ShutDownDone(bool isAllDisconnected);
80     void ProcessEvent(const PanMessage &event);
81 
82     /**
83      * @brief Send the event of the Pan role.
84      *
85      * @param event The event of the Pan role.
86      */
87     void PostEvent(const PanMessage &event);
88     void RemoveStateMachine(const std::string &device);
89 
90     static std::string GetLocalAddress();
91 
92     std::string PanFindDeviceByLcid(uint16_t lcid);
93     void OpenNetwork();
94     void CloseNetwork(std::string device);
95     void WriteNetworkData(std::string address, EthernetHeader head, uint8_t *data, int len);
96     int ReceiveRemoteBusy(bool isBusy);
97     int PanSendData(EthernetHeader head, uint8_t *data, int len);
98 
99     static void ReverseAddress(uint8_t *oldAddr, uint8_t *newAddr);
100 
101 private:
102     /**
103      * @brief Service startup.
104      *
105      */
106     void StartUp();
107 
108     /**
109      * @brief Service shutdown.
110      *
111      */
112     void ShutDown();
113     /**
114      * @brief Get the max connection devices number.
115      *
116      * @return Returns the max connection devices number.
117      */
118     int GetMaxConnectionsDeviceNum() const;
119     int GetConnectionsDeviceNum() const;
120     bool IsConnected(const std::string &address) const;
121     void ProcessConnectEvent(const PanMessage &event);
122     void ProcessDefaultEvent(const PanMessage &event) const;
123     void ProcessRemoveStateMachine(const std::string &address);
124     void PanSendData(std::string address, EthernetHeader head, uint8_t *data, int len);
125     //  service status
126     bool isStarted_ {false};
127     //  service status
128     bool isShuttingDown_ {false};
129     // the mutex variable
130     std::recursive_mutex mutex_ {};
131     // The maximum default number of connection devices
132     static const int PAN_MAX_DEFAULT_CONNECTIONS_NUMR = 6;
133     // the maximum number of connection devices.
134     int maxConnectionsNum_ {PAN_MAX_DEFAULT_CONNECTIONS_NUMR};
135 
136     BaseObserverList<IPanObserver> panObservers_ {};
137     // the map of the device and sate machine
138     std::map<const std::string, std::unique_ptr<PanStateMachine>> stateMachines_ {};
139     std::unique_ptr<PanSdp> panSdp_ {nullptr};
140     std::unique_ptr<PanNetwork> panNetwork_ {nullptr};
141     bool isTetheringOn_ {false};
142     // const state map
143     const std::map<const int, const int> stateMap_ = {
144         {PAN_STATE_DISCONNECTED, static_cast<int>(BTConnectState::DISCONNECTED)},
145         {PAN_STATE_CONNECTING, static_cast<int>(BTConnectState::CONNECTING)},
146         {PAN_STATE_DISCONNECTING, static_cast<int>(BTConnectState::DISCONNECTING)},
147         {PAN_STATE_CONNECTED, static_cast<int>(BTConnectState::CONNECTED)}
148     };
149 };
150 }  // namespace bluetooth
151 }  // namespace OHOS
152 #endif  // PAN_SERVICE_H
153