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
16 #include "bluetooth_service.h"
17 #include "ohos_run.h"
18 #include "blegap.h"
19 #define GATTS_TAG "BLUETOOTH_SERVICE"
20
21 /**
22 * @brief Registers GATT server callbacks.
23 *
24 * @param func Indicates the pointer to the callbacks to register, as enumerated in {@link BtGattServerCallbacks}.
25 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the callbacks are registered;
26 * returns an error code defined in {@link BtStatus} otherwise.
27 * @since 6
28 */
BleGattsRegisterCallbacks(BtGattServerCallbacks func)29 BtError BleGattsRegisterCallbacks(BtGattServerCallbacks func)
30 {
31 esp_err_t ret;
32 ret = esp_ble_gatts_register_callback(func.gattsCallback);
33 if (ret) {
34 ESP_LOGE(GATTS_TAG, "gatts register error, error code = %x", ret);
35 return;
36 }
37 ret = esp_ble_gap_register_callback(func.gapCallback);
38 if (ret) {
39 ESP_LOGE(GATTS_TAG, "gap register error, error code = %x", ret);
40 return;
41 }
42 ret = esp_ble_gatts_app_register(func.profileAppId);
43 if (ret) {
44 ESP_LOGE(GATTS_TAG, "gatts app register error, error code = %x", ret);
45 return;
46 }
47 return ret;
48 }
49
50 /**
51 * @brief Starts a service.
52 *
53 * @param serverId Indicates the ID of the GATT server.
54 * @param srvcHandle Indicates the handle ID of the service.
55 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the service is started;
56 * returns an error code defined in {@link BtStatus} otherwise.
57 * @since 6
58 */
BleGattsStartService(int serverId,int srvcHandle)59 BtError BleGattsStartService(int serverId, int srvcHandle)
60 {
61 return esp_ble_gatts_start_service(srvcHandle);
62 }
63
64 /**
65 * @brief Stops a service.
66 *
67 * @param serverId Indicates the ID of the GATT server.
68 * @param srvcHandle Indicates the handle ID of the service.
69 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the service is stopped;
70 * returns an error code defined in {@link BtStatus} otherwise.
71 * @since 6
72 */
BleGattsStopService(int serverId,int srvcHandle)73 BtError BleGattsStopService(int serverId, int srvcHandle)
74 {
75 return esp_ble_gatts_stop_service(srvcHandle);
76 }
77
78 /**
79 * @brief Sends an indication or notification to the client.
80 *
81 * The <b>confirm</b> field in <b>param</b> determines whether to send an indication or a notification.
82 *
83 * @param serverId Indicates the ID of the GATT server.
84 * @param param Indicates the pointer to the sending parameters. For details, see {@link GattsSendIndParam}.
85 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the indication or notification is sent;
86 * returns an error code defined in {@link BtStatus} otherwise.
87 * @since 6
88 */
BleGattsSendIndication(GattsSendParam * param)89 BtError BleGattsSendIndication(GattsSendParam *param)
90 {
91 if (param == NULL && param->value == NULL) {
92 return BT_PARAMINPUT_ERROR;
93 }
94 return esp_ble_gatts_send_indicate(param->gattsIf, param->connId, param->attrHandle, param->valueLen, param->value,
95 param->needConfirm);
96 }
97
98 /**
99 * @brief This function is called to start advertising.
100 *
101 * @param advParams: pointer to User defined advParams data structure.
102
103 * @return
104 * - ESP_OK : success
105 * - other : failed
106 *
107 */
BleGattsStartAdvertising(BleAdvParams2 * advParams)108 BtError BleGattsStartAdvertising(BleAdvParams2 *advParams)
109 {
110 if (advParams == NULL) {
111 BT_DEBUG("BleGattsStartAdvertising param is NULL! \n");
112 return BT_PARAMINPUT_ERROR;
113 }
114 return esp_ble_gap_start_advertising(advParams);
115 }
116
117 /**
118 * @brief This function is called to send a response to a request.
119 *
120 * @param[in] gattsIf: GATT server access interface
121 * @param[in] connId - connection identifier.
122 * @param[in] transId - transfer id
123 * @param[in] status - response status
124 * @param[in] rsp - response data.
125 *
126 * @return
127 * - ESP_OK : success
128 * - other : failed
129 *
130 */
BleGattsSendResponse(GattInterfaceType gattsIf,uint16_t connId,uint32_t transId,GattStatus status,BleGattRsp * rsp)131 BtError BleGattsSendResponse(GattInterfaceType gattsIf, uint16_t connId, uint32_t transId, GattStatus status,
132 BleGattRsp *rsp)
133 {
134 return esp_ble_gatts_send_response(gattsIf, connId, transId, status, rsp);
135 }
136
137 /**
138 * @brief This function is called to override the BTA default ADV parameters.
139 *
140 * @param[in] advData: Pointer to User defined ADV data structure. This
141 * memory space can not be freed until callback of config_adv_data
142 * is received.
143 *
144 * @return
145 * - ESP_OK : success
146 * - other : failed
147 *
148 */
BleGapConfigAdvData(BleAdvData * advData)149 BtError BleGapConfigAdvData(BleAdvData *advData)
150 {
151 if (advData == NULL) {
152 BT_DEBUG("BleGapConfigAdvData param is NULL! \n");
153 return BT_PARAMINPUT_ERROR;
154 }
155 return esp_ble_gap_config_adv_data(advData);
156 }
157
158 /**
159 * @brief Create a service. When service creation is done, a callback
160 * event ESP_GATTS_CREATE_EVT is called to report status
161 * and service ID to the profile. The service ID obtained in
162 * the callback function needs to be used when adding included
163 * service and characteristics/descriptors into the service.
164 *
165 * @param[in] gattsIf: GATT server access interface
166 * @param[in] serviceId: service ID.
167 * @param[in] numHandle: number of handle requested for this service.
168 *
169 * @return
170 * - ESP_OK : success
171 * - other : failed
172 *
173 */
BleGattsCreateService(GattInterfaceType gattsIf,GattSrvcId * serviceId,uint16_t numHandle)174 BtError BleGattsCreateService(GattInterfaceType gattsIf, GattSrvcId *serviceId, uint16_t numHandle)
175 {
176 if (serviceId == NULL) {
177 BT_DEBUG("BleGattsCreateService param is NULL! \n");
178 return BT_PARAMINPUT_ERROR;
179 }
180 return esp_ble_gatts_create_service(gattsIf, serviceId, numHandle);
181 }
182
183 /**
184 * @brief Retrieve attribute value
185 *
186 * @param[in] attrHandle: Attribute handle.
187 * @param[out] length: pointer to the attribute value length
188 * @param[out] value: Pointer to attribute value payload, the value cannot be modified by user
189 *
190 * @return
191 * - ESP_GATT_OK : success
192 * - other : failed
193 *
194 */
BleGattsGetAttrValue(uint16_t attrHandle,uint16_t * length,const uint8_t ** value)195 BtError BleGattsGetAttrValue(uint16_t attrHandle, uint16_t *length, const uint8_t **value)
196 {
197 if ((length == NULL) || (value == NULL)) {
198 BT_DEBUG("BleGattsGetAttrValue param is NULL! \n");
199 return BT_PARAMINPUT_ERROR;
200 }
201 return esp_ble_gatts_get_attr_value(attrHandle, length, value);
202 }
203
204 /**
205 * @brief This function is called to add characteristic descriptor. When
206 * it's done, a callback event ESP_GATTS_ADD_DESCR_EVT is called
207 * to report the status and an ID number for this descriptor.
208 *
209 * @param[in] serviceHandle: service handle to which this characteristic descriptor is to
210 * be added.
211 * @param[in] perm: descriptor access permission.
212 * @param[in] descrUuid: descriptor UUID.
213 * @param[in] charDescrVal : Characteristic descriptor value
214 * @param[in] control : attribute response control byte
215 * @return
216 * - ESP_OK : success
217 * - other : failed
218 *
219 */
BleGattsAddCharDescr(uint16_t serviceHandle,BtUuid * descrUuid,uint16_t perm,BleAttrValue * charDescrVal,BleAttrControl * control)220 BtError BleGattsAddCharDescr(uint16_t serviceHandle, BtUuid *descrUuid, uint16_t perm, BleAttrValue *charDescrVal,
221 BleAttrControl *control)
222 {
223 if (descrUuid == NULL) {
224 BT_DEBUG("BleGattsAddCharDescr param is NULL! \n");
225 return BT_PARAMINPUT_ERROR;
226 }
227 return esp_ble_gatts_add_char_descr(serviceHandle, descrUuid, perm, charDescrVal, control);
228 }
229
230 /**
231 * @brief This function is called to add a characteristic into a service.
232 *
233 * @param[in] serviceHandle: service handle to which this included service is to
234 * be added.
235 * @param[in] character : Characteristic.
236 *
237 * @return
238 * - ESP_OK : success
239 * - other : failed
240 *
241 */
BleGattsAddChar(uint16_t serviceHandle,GattsChar * character)242 BtError BleGattsAddChar(uint16_t serviceHandle, GattsChar *character)
243 {
244 if ((character == NULL) || (character->charUuid == NULL) || (character->charVal == NULL)) {
245 BT_DEBUG("BleGattsAddChar param is NULL! \n");
246 return BT_PARAMINPUT_ERROR;
247 }
248 return esp_ble_gatts_add_char(serviceHandle, character->charUuid, character->perm, character->property,
249 character->charVal, character->control);
250 }
251
252 /**
253 * @brief Update connection parameters, can only be used when connection is up.
254 *
255 * @param[in] params - connection update parameters
256 *
257 * @return
258 * - ESP_OK : success
259 * - other : failed
260 *
261 */
BleGapUpdateConnParams(BleConnUpdateParams * params)262 BtError BleGapUpdateConnParams(BleConnUpdateParams *params)
263 {
264 if (params == NULL) {
265 BT_DEBUG("BleGapUpdateConnParams param is NULL! \n");
266 return BT_PARAMINPUT_ERROR;
267 }
268 return esp_ble_gap_update_conn_params(params);
269 }