• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Hunan OpenValley Digital Industry Development 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 #ifndef __BLUETOOTH_SERVICE_H__
16 #define __BLUETOOTH_SERVICE_H__
17 #include "securec.h"
18 #include "blegap.h"
19 #include "esp_gatts_api.h"
20 
21 typedef esp_ble_gatts_cb_param_t BleGattsParam;
22 
23 // Gap回调参数联合体
24 typedef esp_ble_gap_cb_param_t BleGapParam;
25 // Gatt服务端回调参数联合体
26 typedef esp_ble_gatts_cb_param_t BleGattsCbParam;
27 // Ble扫描参数
28 typedef esp_ble_scan_params_t BleScanParams;
29 // 设置属性值类型
30 typedef esp_attr_value_t BleAttrValue;
31 // 广播数据结构体
32 typedef esp_ble_adv_data_t BleAdvData;
33 // 广播参数
34 typedef esp_ble_adv_params_t BleAdvParams2;
35 // GATT远程读请求响应类型
36 typedef esp_gatt_rsp_t BleGattRsp;
37 // Gatt服务ID
38 typedef esp_gatt_srvc_id_t GattSrvcId;
39 // 属性自动响应标志
40 typedef esp_attr_control_t BleAttrControl;
41 // 蓝牙版本号
42 #define BT_VERSION 1
43 
44 /// GATT服务回调函数事件
45 typedef enum {
46     OHOS_GATTS_REG_EVT = 0,             /*!< When register application id, the event comes */
47     OHOS_GATTS_READ_EVT = 1,            /*!< When gatt client request read operation, the event comes */
48     OHOS_GATTS_WRITE_EVT = 2,           /*!< When gatt client request write operation, the event comes */
49     OHOS_GATTS_EXEC_WRITE_EVT = 3,      /*!< When gatt client request execute write, the event comes */
50     OHOS_GATTS_MTU_EVT = 4,             /*!< When set mtu complete, the event comes */
51     OHOS_GATTS_CONF_EVT = 5,            /*!< When receive confirm, the event comes */
52     OHOS_GATTS_UNREG_EVT = 6,           /*!< When unregister application id, the event comes */
53     OHOS_GATTS_CREATE_EVT = 7,          /*!< When create service complete, the event comes */
54     OHOS_GATTS_ADD_INCL_SRVC_EVT = 8,   /*!< When add included service complete, the event comes */
55     OHOS_GATTS_ADD_CHAR_EVT = 9,        /*!< When add characteristic complete, the event comes */
56     OHOS_GATTS_ADD_CHAR_DESCR_EVT = 10, /*!< When add descriptor complete, the event comes */
57     OHOS_GATTS_DELETE_EVT = 11,         /*!< When delete service complete, the event comes */
58     OHOS_GATTS_START_EVT = 12,          /*!< When start service complete, the event comes */
59     OHOS_GATTS_STOP_EVT = 13,           /*!< When stop service complete, the event comes */
60     OHOS_GATTS_CONNECT_EVT = 14,        /*!< When gatt client connect, the event comes */
61     OHOS_GATTS_DISCONNECT_EVT = 15,     /*!< When gatt client disconnect, the event comes */
62     OHOS_GATTS_OPEN_EVT = 16,           /*!< When connect to peer, the event comes */
63     OHOS_GATTS_CANCEL_OPEN_EVT = 17,    /*!< When disconnect from peer, the event comes */
64     OHOS_GATTS_CLOSE_EVT = 18,          /*!< When gatt server close, the event comes */
65     OHOS_GATTS_LISTEN_EVT = 19,         /*!< When gatt listen to be connected the event comes */
66     OHOS_GATTS_CONGEST_EVT = 20,        /*!< When congest happen, the event comes */
67     /* following is extra event */
68     OHOS_GATTS_RESPONSE_EVT = 21,            /*!< When gatt send response complete, the event comes */
69     OHOS_GATTS_CREAT_ATTR_TAB_EVT = 22,      /*!< When gatt create table complete, the event comes */
70     OHOS_GATTS_SET_ATTR_VAL_EVT = 23,        /*!< When gatt set attr value complete, the event comes */
71     OHOS_GATTS_SEND_SERVICE_CHANGE_EVT = 24, /*!< When gatt send service change indication complete, the event comes */
72 } GattsBleCallbackEvent;
73 
74 /**
75  * @brief GATT服务端回调函数类型
76  * @param event : 事件类型
77  * @param gattc_if : GATT客户端访问接口,不同的gattc_if对应不同的profile
78  * @param param : 指向回调参数指针,当前是联合类型
79  */
80 typedef void (*GattsBleCallback)(GattsBleCallbackEvent event, GattInterfaceType gattsIf, BleGattsParam *param);
81 
82 typedef struct {
83     GapBleCallback gapCallback;     // gap回调函数
84     GattsBleCallback gattsCallback; // gatts回调函数
85     uint16_t profileAppId; // 应用程序标识(UUID),用于不同的应用程序,应用程序回调函数使用
86 } BtGattServerCallbacks;
87 
88 typedef struct {
89     GattInterfaceType gattsIf;  // GATT服务器访问接口
90     uint16_t connId;            // 连接id
91     uint16_t attrHandle;        // 属性句柄
92     uint16_t valueLen;          // 值的长度
93     uint8_t *value;             // 值
94     bool needConfirm;           // 是否需要确认
95 } GattsSendParam;
96 
97 typedef struct {
98     BtUuid *charUuid;           // Characteristic UUID.
99     uint16_t perm;              // Characteristic value declaration attribute permission.
100     uint8_t property;           // Characteristic Properties
101     BleAttrValue *charVal;      // Characteristic value
102     BleAttrControl *control;    // attribute response control byte
103 } GattsChar;
104 
105 /**
106  * @brief 发现请求远程设备上的GATT服务
107  *
108  * @param clientId 客户端ID
109  * @param connId  连接ID
110  * @param filterUuid  过滤服务UUID *
111  * @return BtResult 成功返回 BT_SUCCESS
112  */
113 BtError BleGattcSearchServices(int clientId, int connId, BtUuid *filterUuid);
114 
115 /**
116  * @brief 注册Gatt服务端回调
117  *
118  * @param func BtGattServerCallbacks结构体
119  * @return BtResult 成功返回 BT_SUCCESS
120  */
121 BtError BleGattsRegisterCallbacks(BtGattServerCallbacks func);
122 
123 /**
124  * @brief 启动Gatt服务
125  *
126  * @param serverId Gatt服务id
127  * @param srvcHandl Gatt服务句柄
128  * @return BtResult 成功返回 BT_SUCCESS
129  * @since 6
130  */
131 BtError BleGattsStartService(int serverId, int srvcHandle);
132 
133 /**
134  * @brief 停止Gatt服务
135  *
136  * @param serverId Gatt服务id
137  * @param srvcHandl Gatt服务句柄
138  * @return BtResult 成功返回 BT_SUCCESS
139  */
140 BtError BleGattsStopService(int serverId, int srvcHandle);
141 
142 /**
143  * @brief 发送指示或通知GATT客户端
144  *
145  * @param param: GATT服务器发送数据参数
146  * @return BtResult 成功返回 BT_SUCCESS
147  */
148 BtError BleGattsSendIndication(GattsSendParam* param);
149 
150 /**
151  * @brief 发送指示或通知GATT客户端
152  *
153  * @param advParams: 广播参数
154  * @return BtResult 成功返回 BT_SUCCESS
155  */
156 BtError BleGattsStartAdvertising(BleAdvParams2 *advParams);
157 
158 /**
159  * @brief 向请求发送响应函数.
160  *
161  * @param gattsIf: GATT server access interface
162  * @param connId - connection identifier.
163  * @param transId - transfer id
164  * @param status - response status
165  * @param rsp - response data.
166  * @return BtResult 成功返回 BT_SUCCESS
167  *
168  */
169 BtError BleGattsSendResponse(GattInterfaceType gattsIf, uint16_t connId, uint32_t transId, GattStatus status,
170                              BleGattRsp *rsp);
171 /**
172  * @brief 覆盖BTA默认的ADV参数
173  *
174  * @param[in]       advData: Pointer to User defined ADV data structure.
175  * @return BtResult 成功返回 BT_SUCCESS
176  *
177  */
178 BtError BleGapConfigAdvData(BleAdvData *advData);
179 
180 /**
181  * @brief 创建Gatt服务
182  *
183  * @param gattsIf: GATT server access interface
184  * @param serviceId: service ID.
185  * @param numHandle: number of handle requested for this service.
186  *
187  * @return BtResult 成功返回 BT_SUCCESS
188  *
189  */
190 BtError BleGattsCreateService(GattInterfaceType gattsIf, GattSrvcId *serviceId, uint16_t numHandle);
191 
192 /**
193  * @brief 获取属性值
194  *
195  * @param attrHandle: Attribute handle.
196  * @param length: pointer to the attribute value length
197  * @param value:  Pointer to attribute value payload, the value cannot be modified by user
198  * @return BtResult 成功返回 BT_SUCCESS
199  *
200  */
201 BtError BleGattsGetAttrValue(uint16_t attrHandle, uint16_t *length, const uint8_t **value);
202 
203 /**
204  * @brief 添加特征描述符
205  *
206  * @param serviceHandle: service handle to which this characteristic descriptor is to
207  *                              be added.
208  * @param perm: descriptor access permission.
209  * @param descrUuid: descriptor UUID.
210  * @param charDescrVal  : Characteristic descriptor value
211  * @param control : attribute response control byte
212  * @return BtResult 成功返回 BT_SUCCESS
213  *
214  */
215 BtError BleGattsAddCharDescr(uint16_t serviceHandle, BtUuid *descrUuid, uint16_t perm, BleAttrValue *charDescrVal,
216                              BleAttrControl *control);
217 
218 /**
219  * @brief 将特征添加到服务中
220  *
221  * @param serviceHandle: service handle to which this included service is to
222  *                  be added.
223  * @param character: 特征值参数
224  * @return BtResult 成功返回 BT_SUCCESS
225  *
226  */
227 BtError BleGattsAddChar(uint16_t serviceHandle, GattsChar* character);
228 #endif