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 OHOS_BT_SPP_H 17 #define OHOS_BT_SPP_H 18 19 #include <stdbool.h> 20 #include "ohos_bt_def.h" 21 22 #ifdef __cplusplus 23 extern "C" { 24 #endif 25 26 #define BT_SPP_READ_SOCKET_CLOSED (0) 27 #define BT_SPP_READ_FAILED (-1) 28 #define BT_SPP_WRITE_FAILED (-1) 29 #define BT_SPP_INVALID_ID (-1) 30 31 typedef enum { 32 OHOS_SPP_SOCKET_RFCOMM = 0x0, 33 } BtSppSocketType; 34 35 typedef struct { 36 BtUuid uuid; 37 BtSppSocketType socketType; 38 bool isEncrypt; 39 } BtCreateSocketPara; 40 41 /** 42 * @brief Creates an server listening socket based on the service record. 43 * 44 * @param socketPara The parameters to create a server socket. 45 * @param name The service's name. 46 * @param len The length of the service's name. 47 * @return Returns a server ID, if create fail return {@link BT_SPP_INVALID_ID}. 48 */ 49 int SppServerCreate(BtCreateSocketPara *socketPara, const char *name, unsigned int len); 50 51 /** 52 * @brief Waits for a remote device to connect to this server socket. 53 * 54 * This method return a client ID indicates a client socket 55 * can be used to read data from and write data to remote device. 56 * 57 * @param serverId The relative ID used to identify the current server socket, obtain the value by calling 58 * {@link SppServerCreate}. 59 * @return Returns a client ID, if accept fail return {@link BT_SPP_INVALID_ID}. 60 */ 61 int SppServerAccept(int serverId); 62 63 /** 64 * @brief Disables an spp server socket and releases related resources. 65 * 66 * @param serverId The relative ID used to identify the current server socket, obtain the value by calling 67 * {@link SppServerCreate}. 68 * @return Returns the operation result status {@link BtStatus}. 69 */ 70 int SppServerClose(int serverId); 71 72 /** 73 * @brief Connects to a remote device over the socket. 74 * 75 * @param socketPara The param to create a client socket and connect to a remote device. 76 * @return Returns a client ID, if connect fail return {@link BT_SPP_INVALID_ID}. 77 */ 78 int SppConnect(BtCreateSocketPara *socketPara, const BdAddr *bdAddr); 79 80 /** 81 * @brief Disables a connection and releases related resources. 82 * 83 * @param clientId The relative ID used to identify the current client socket. 84 * @return Returns the operation result status {@link BtStatus}. 85 */ 86 int SppDisconnect(int clientId); 87 88 /** 89 * @brief Get the connection status of this socket. 90 * 91 * @param clientId The relative ID used to identify the current client socket. 92 * @return Returns true is connected or false is not connected. 93 */ 94 bool IsSppConnected(int clientId); 95 96 /** 97 * @brief Spp get remote device's address. 98 * 99 * @param clientId The relative ID used to identify the current client socket. 100 * @param remoteAddr Remote device's address, memory allocated by caller. 101 * @return Returns the operation result status {@link BtStatus}. 102 */ 103 int SppGetRemoteAddr(int clientId, BdAddr *remoteAddr); 104 105 /** 106 * @brief Read data from socket. 107 * 108 * @param clientId The relative ID used to identify the current client socket. 109 * @param buf Indicate the buffer which read in, memory allocated by caller. 110 * @param bufLen Indicate the buffer length. 111 * @return Returns the length greater than 0 as read the actual length. 112 * Returns {@link BT_SPP_READ_SOCKET_CLOSED} if the socket is closed. 113 * Returns {@link BT_SPP_READ_FAILED} if the operation failed. 114 */ 115 int SppRead(int clientId, char *buf, const unsigned int bufLen); 116 117 /** 118 * @brief Client write data to socket. 119 * 120 * @param clientId The relative ID used to identify the current client socket. 121 * @param data Indicate the data to be written. 122 * @param len Indicates the length of the data to be written. 123 * @return Returns the actual write length. 124 * Returns {@link BT_SPP_WRITE_FAILED} if the operation failed. 125 */ 126 int SppWrite(int clientId, const char *data, const unsigned int len); 127 128 /** 129 * @brief Adjust the socket send and recv buffer size, limit range is 4KB to 50KB 130 * 131 * @param clientId The relative ID used to identify the current client socket. 132 * @param bufferSize The buffer size want to set, unit is byte. 133 * @return Returns the operation result status {@link BtStatus}. 134 */ 135 int SppSetSocketBufferSize(int clientId, int bufferSize); 136 #ifdef __cplusplus 137 } 138 #endif 139 #endif