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