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 /** 17 * @addtogroup Bluetooth 18 * @{ 19 * 20 * @brief Defines a bluetooth system that provides basic blurtooth connection and profile functions, 21 * including A2DP, AVRCP, BLE, GATT, HFP, MAP, PBAP, and SPP, etc. 22 * 23 */ 24 25 /** 26 * @file socket_service.h 27 * 28 * @brief Declares SPP service functions, including basic and observer functions. 29 * 30 */ 31 32 #ifndef SOCKET_SERVICE_H 33 #define SOCKET_SERVICE_H 34 35 #include <mutex> 36 #include <string> 37 #include <vector> 38 #include "bt_uuid.h" 39 #include "context.h" 40 #include "interface_profile_socket.h" 41 #include "profile_service_manager.h" 42 #include "socket.h" 43 44 namespace OHOS { 45 namespace bluetooth { 46 /** 47 * @brief This Socket class provides a set of methods that is called by Framework API, and manage 48 * the socket object and the message of other thread. 49 * 50 */ 51 class SocketService : public IProfileSocket, public utility::Context { 52 public: 53 /** 54 * @brief Constructor. 55 * 56 */ 57 SocketService(); 58 59 /** 60 * @brief Destructor. 61 * 62 */ 63 ~SocketService(); 64 65 utility::Context *GetContext() override; 66 67 /** 68 * @brief Starts a socket service. 69 * 70 * @details Switch to the thread of the Socket service in this function. 71 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 72 */ 73 void Enable() override; 74 75 /** 76 * @brief Shut down the Socket target service. 77 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 78 */ 79 void Disable() override; 80 81 /** 82 * @brief Connects to a peer bluetooth device. 83 * 84 * @param device Remote device address.operation 85 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 86 */ 87 int Connect(const RawAddress &device) override; 88 89 /** 90 * @brief Disconnects from a peer bluetooth device. 91 * 92 * @param device Remote device address. 93 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 94 */ 95 int Disconnect(const RawAddress &device) override; 96 97 /** 98 * @brief Get the connected devices list. 99 * @return @c std::list<RawAddress> : the connected devices list. 100 */ 101 std::list<RawAddress> GetConnectDevices() override; 102 103 /** 104 * @brief Get the Connect State object. 105 * @return int 106 */ 107 int GetConnectState() override; 108 109 /** 110 * @brief Get the maximum number of connected devices. 111 * @return @c int : connected devices number. 112 */ 113 int GetMaxConnectNum() override; 114 115 /** 116 * @brief process the message of the socket . 117 * @param @msg: the message of the socket. 118 */ 119 void ProcessMessage(const utility::Message &msg); 120 121 /** 122 * @brief The client initiates the connection. 123 * @details The client queries the SDP and finds the channel to be connected through UUID. 124 * Client sets security level to GAP. 125 * @param addr addr 126 * @param uuid server record uuid to search scn. 127 * @param securityFlag require the connection to be encrypted and authenticated. 128 * @param type socket type. 129 * @return the upper socket fd that generated by the socketpair . 130 * @since 6 131 */ 132 int Connect(const std::string &addr, const Uuid &uuid, int securityFlag, int type) override; 133 134 /** 135 * @brief The server listen and accept the connection. 136 * @details The server registers service records to SDP with service name, uuid and server channel 137 * number that assigned by rfcomm. Server sets security level to GAP. 138 * @param name server service name. 139 * @param uuid server uuid. 140 * @param securityFlag require the connection to be encrypted and authenticated. 141 * @param type socket type. 142 * @return the upper socket fd that generated by the socketpair. 143 * @since 6 144 */ 145 int Listen(const std::string &name, const Uuid &uuid, int securityFlag, int type) override; 146 147 /** 148 * @brief shutdown for adapter manager 149 * @return int 150 */ 151 void ShutDownInternal(); 152 153 private: 154 // service status 155 bool isShutdown_ {false}; 156 // client push status 157 bool isClientPush_ {false}; 158 // all socket object 159 std::vector<std::unique_ptr<Socket>> clientSockets_ {}; 160 std::vector<std::unique_ptr<Socket>> serverSockets_ {}; 161 std::vector<Socket *> acceptSockets_ {}; 162 std::mutex mutex_ {}; 163 164 /** 165 * @brief Enables the Socket service. 166 * 167 */ 168 void EnableNative(void); 169 170 /** 171 * @brief Disable the Socket service. 172 * 173 */ 174 void DisableNative(void); 175 176 /** 177 * @brief cleanup server socket object. 178 * 179 */ 180 void CleanupServerSocket(); 181 182 /** 183 * @brief delete socket object. 184 * 185 */ 186 void CleanupClientSocket(Socket *socket); 187 }; 188 } // namespace bluetooth 189 } // namespace OHOS 190 #endif // SOCKET_SERVICE_H 191