bt_gatt.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  1. /****************************************************************************
  2. * wireless/bluetooth/bt_gatt.c
  3. * Generic Attribute Profile handling.
  4. *
  5. * Copyright (C) 2018 Gregory Nutt. All rights reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * Ported from the Intel/Zephyr arduino101_firmware_source-v1.tar package
  9. * where the code was released with a compatible 3-clause BSD license:
  10. *
  11. * Copyright (c) 2016, Intel Corporation
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are
  16. * met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. *
  21. * 2. Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in the
  23. * documentation and/or other materials provided with the distribution.
  24. *
  25. * 3. Neither the name of the copyright holder nor the names of its
  26. * contributors may be used to endorse or promote products derived from
  27. * this software without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  31. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  32. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  33. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  34. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS
  36. * ; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  37. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  38. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  39. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. ****************************************************************************/
  42. /****************************************************************************
  43. * Included Files
  44. ****************************************************************************/
  45. #include <nuttx/config.h>
  46. #include <stdbool.h>
  47. #include <string.h>
  48. #include <errno.h>
  49. #include <debug.h>
  50. #include <nuttx/wireless/bluetooth/bt_hci.h>
  51. #include <nuttx/wireless/bluetooth/bt_core.h>
  52. #include <nuttx/wireless/bluetooth/bt_buf.h>
  53. #include <nuttx/wireless/bluetooth/bt_uuid.h>
  54. #include <nuttx/wireless/bluetooth/bt_gatt.h>
  55. #include "bt_hcicore.h"
  56. #include "bt_conn.h"
  57. #include "bt_keys.h"
  58. #include "bt_l2cap.h"
  59. #include "bt_att.h"
  60. /****************************************************************************
  61. * Private Types
  62. ****************************************************************************/
  63. begin_packed_struct struct gatt_incl_s
  64. {
  65. uint16_t start_handle;
  66. uint16_t end_handle;
  67. union
  68. {
  69. uint16_t uuid16;
  70. uint8_t uuid[16];
  71. } u;
  72. } end_packed_struct;
  73. begin_packed_struct struct gatt_chrc_s
  74. {
  75. uint8_t properties;
  76. uint16_t value_handle;
  77. union
  78. {
  79. uint16_t uuid16;
  80. uint8_t uuid[16];
  81. } u;
  82. } end_packed_struct;
  83. struct notify_data_s
  84. {
  85. FAR const void *data;
  86. size_t len;
  87. uint8_t handle;
  88. };
  89. /****************************************************************************
  90. * Private Data
  91. ****************************************************************************/
  92. static FAR const struct bt_gatt_attr_s *g_db = NULL;
  93. static size_t g_attr_count = 0;
  94. /****************************************************************************
  95. * Public Functions
  96. ****************************************************************************/
  97. void bt_gatt_register(FAR const struct bt_gatt_attr_s *attrs, size_t count)
  98. {
  99. g_db = attrs;
  100. g_attr_count = count;
  101. }
  102. int bt_gatt_attr_read(FAR struct bt_conn_s *conn,
  103. FAR const struct bt_gatt_attr_s *attr, FAR void *buf,
  104. uint8_t buf_len, uint16_t offset,
  105. FAR const void *value, uint8_t value_len)
  106. {
  107. uint8_t maxlen;
  108. uint8_t len;
  109. if (offset > value_len)
  110. {
  111. return -EINVAL;
  112. }
  113. len = buf_len;
  114. maxlen = value_len - offset;
  115. if (len > maxlen)
  116. {
  117. len = maxlen;
  118. }
  119. wlinfo("handle 0x%04x offset %u length %u\n", attr->handle, offset, len);
  120. memcpy(buf, value + offset, len);
  121. return len;
  122. }
  123. int bt_gatt_attr_read_service(FAR struct bt_conn_s *conn,
  124. FAR const struct bt_gatt_attr_s *attr,
  125. FAR void *buf, uint8_t len, uint16_t offset)
  126. {
  127. FAR struct bt_uuid_s *uuid = attr->user_data;
  128. if (uuid->type == BT_UUID_16)
  129. {
  130. uint16_t uuid16 = BT_HOST2LE16(uuid->u.u16);
  131. return bt_gatt_attr_read(conn, attr, buf, len, offset, &uuid16,
  132. sizeof(uuid16));
  133. }
  134. return bt_gatt_attr_read(conn, attr, buf, len, offset, uuid->u.u128,
  135. sizeof(uuid->u.u128));
  136. }
  137. int bt_gatt_attr_read_include(FAR struct bt_conn_s *conn,
  138. FAR const struct bt_gatt_attr_s *attr,
  139. FAR void *buf, uint8_t len, uint16_t offset)
  140. {
  141. FAR struct bt_gatt_include_s *incl = attr->user_data;
  142. struct gatt_incl_s pdu;
  143. uint8_t value_len;
  144. pdu.start_handle = BT_HOST2LE16(incl->start_handle);
  145. pdu.end_handle = BT_HOST2LE16(incl->end_handle);
  146. value_len = sizeof(pdu.start_handle) + sizeof(pdu.end_handle);
  147. if (incl->uuid->type == BT_UUID_16)
  148. {
  149. pdu.u.uuid16 = BT_HOST2LE16(incl->uuid->u.u16);
  150. value_len += sizeof(pdu.u.uuid16);
  151. }
  152. else
  153. {
  154. memcpy(pdu.u.uuid, incl->uuid->u.u128, sizeof(incl->uuid->u.u128));
  155. value_len += sizeof(incl->uuid->u.u128);
  156. }
  157. return bt_gatt_attr_read(conn, attr, buf, len, offset, &pdu, value_len);
  158. }
  159. int bt_gatt_attr_read_chrc(FAR struct bt_conn_s *conn,
  160. FAR const struct bt_gatt_attr_s *attr,
  161. FAR void *buf, uint8_t len, uint16_t offset)
  162. {
  163. FAR struct bt_gatt_chrc_s *chrc = attr->user_data;
  164. struct gatt_chrc_s pdu;
  165. uint8_t value_len;
  166. pdu.properties = chrc->properties;
  167. pdu.value_handle = BT_HOST2LE16(chrc->value_handle);
  168. value_len = sizeof(pdu.properties) + sizeof(pdu.value_handle);
  169. if (chrc->uuid->type == BT_UUID_16)
  170. {
  171. pdu.u.uuid16 = BT_HOST2LE16(chrc->uuid->u.u16);
  172. value_len += sizeof(pdu.u.uuid16);
  173. }
  174. else
  175. {
  176. memcpy(pdu.u.uuid, chrc->uuid->u.u128, sizeof(chrc->uuid->u.u128));
  177. value_len += sizeof(chrc->uuid->u.u128);
  178. }
  179. return bt_gatt_attr_read(conn, attr, buf, len, offset, &pdu, value_len);
  180. }
  181. void bt_gatt_foreach_attr(uint16_t start_handle, uint16_t end_handle,
  182. bt_gatt_attr_func_t func, FAR void *user_data)
  183. {
  184. size_t i;
  185. for (i = 0; i < g_attr_count; i++)
  186. {
  187. FAR const struct bt_gatt_attr_s *attr = &g_db[i];
  188. /* Check if attribute handle is within range */
  189. if (attr->handle < start_handle || attr->handle > end_handle)
  190. {
  191. continue;
  192. }
  193. if (func(attr, user_data) == BT_GATT_ITER_STOP)
  194. {
  195. break;
  196. }
  197. }
  198. }
  199. int bt_gatt_attr_read_ccc(FAR struct bt_conn_s *conn,
  200. FAR const struct bt_gatt_attr_s *attr,
  201. FAR void *buf, uint8_t len, uint16_t offset)
  202. {
  203. FAR struct _bt_gatt_ccc_s *ccc = attr->user_data;
  204. uint16_t value;
  205. size_t i;
  206. for (i = 0; i < ccc->cfg_len; i++)
  207. {
  208. if (bt_addr_le_cmp(&ccc->cfg[i].peer, &conn->dst))
  209. {
  210. continue;
  211. }
  212. value = BT_HOST2LE16(ccc->cfg[i].value);
  213. break;
  214. }
  215. /* Default to disable if there is no cfg for the peer */
  216. if (i == ccc->cfg_len)
  217. {
  218. value = 0x0000;
  219. }
  220. return bt_gatt_attr_read(conn, attr, buf, len, offset, &value,
  221. sizeof(value));
  222. }
  223. static void gatt_ccc_changed(FAR struct _bt_gatt_ccc_s *ccc)
  224. {
  225. uint16_t value = 0x0000;
  226. int i;
  227. for (i = 0; i < ccc->cfg_len; i++)
  228. {
  229. if (ccc->cfg[i].value > value)
  230. {
  231. value = ccc->cfg[i].value;
  232. }
  233. }
  234. wlinfo("ccc %p value 0x%04x\n", ccc, value);
  235. if (value != ccc->value)
  236. {
  237. ccc->value = value;
  238. ccc->cfg_changed(value);
  239. }
  240. }
  241. int bt_gatt_attr_write_ccc(FAR struct bt_conn_s *conn,
  242. FAR const struct bt_gatt_attr_s *attr,
  243. FAR const void *buf, uint8_t len,
  244. uint16_t offset)
  245. {
  246. FAR struct _bt_gatt_ccc_s *ccc = attr->user_data;
  247. FAR const uint16_t *data = buf;
  248. bool bonded;
  249. size_t i;
  250. if (len != sizeof(*data) || offset)
  251. {
  252. return -EINVAL;
  253. }
  254. if (bt_keys_get_addr(&conn->dst))
  255. {
  256. bonded = true;
  257. }
  258. else
  259. {
  260. bonded = false;
  261. }
  262. for (i = 0; i < ccc->cfg_len; i++)
  263. {
  264. /* Check for existing configuration */
  265. if (!bt_addr_le_cmp(&ccc->cfg[i].peer, &conn->dst))
  266. {
  267. break;
  268. }
  269. }
  270. if (i == ccc->cfg_len)
  271. {
  272. for (i = 0; i < ccc->cfg_len; i++)
  273. {
  274. /* Check for unused configuration */
  275. if (!ccc->cfg[i].valid)
  276. {
  277. bt_addr_le_copy(&ccc->cfg[i].peer, &conn->dst);
  278. /* Only set valid if bonded */
  279. ccc->cfg[i].valid = bonded;
  280. break;
  281. }
  282. }
  283. if (i == ccc->cfg_len)
  284. {
  285. wlwarn("No space to store CCC cfg\n");
  286. return -ENOMEM;
  287. }
  288. }
  289. ccc->cfg[i].value = BT_LE162HOST(*data);
  290. wlinfo("handle 0x%04x value %u\n", attr->handle, ccc->cfg[i].value);
  291. /* Update cfg if don't match */
  292. if (ccc->cfg[i].value != ccc->value)
  293. {
  294. gatt_ccc_changed(ccc);
  295. }
  296. return len;
  297. }
  298. int bt_gatt_attr_read_cep(FAR struct bt_conn_s *conn,
  299. FAR const struct bt_gatt_attr_s *attr,
  300. FAR void *buf, uint8_t len, uint16_t offset)
  301. {
  302. FAR struct bt_gatt_cep_s *value = attr->user_data;
  303. uint16_t props = BT_HOST2LE16(value->properties);
  304. return bt_gatt_attr_read(conn, attr, buf, len, offset, &props,
  305. sizeof(props));
  306. }
  307. static uint8_t notify_cb(FAR const struct bt_gatt_attr_s *attr,
  308. FAR void *user_data)
  309. {
  310. FAR struct notify_data_s *data = user_data;
  311. const struct bt_uuid_s uuid =
  312. {
  313. BT_UUID_16,
  314. {
  315. BT_UUID_GATT_CCC
  316. }
  317. };
  318. const struct bt_uuid_s chrc =
  319. {
  320. BT_UUID_16,
  321. {
  322. BT_UUID_GATT_CHRC
  323. }
  324. };
  325. FAR struct _bt_gatt_ccc_s *ccc;
  326. size_t i;
  327. if (bt_uuid_cmp(attr->uuid, &uuid))
  328. {
  329. /* Stop if we reach the next characteristic */
  330. if (!bt_uuid_cmp(attr->uuid, &chrc))
  331. {
  332. return BT_GATT_ITER_STOP;
  333. }
  334. return BT_GATT_ITER_CONTINUE;
  335. }
  336. /* Check attribute user_data must be of type struct _bt_gatt_ccc_s */
  337. if (attr->write != bt_gatt_attr_write_ccc)
  338. {
  339. return BT_GATT_ITER_CONTINUE;
  340. }
  341. ccc = attr->user_data;
  342. /* Notify all peers configured */
  343. for (i = 0; i < ccc->cfg_len; i++)
  344. {
  345. FAR struct bt_conn_s *conn;
  346. FAR struct bt_buf_s *buf;
  347. FAR struct bt_att_notify_s *nfy;
  348. /* TODO: Handle indications */
  349. if (ccc->value != BT_GATT_CCC_NOTIFY)
  350. {
  351. continue;
  352. }
  353. conn = bt_conn_lookup_addr_le(&ccc->cfg[i].peer);
  354. if (!conn || conn->state != BT_CONN_CONNECTED)
  355. {
  356. continue;
  357. }
  358. buf = bt_att_create_pdu(conn, BT_ATT_OP_NOTIFY,
  359. sizeof(*nfy) + data->len);
  360. if (!buf)
  361. {
  362. wlwarn("No buffer available to send notification");
  363. bt_conn_release(conn);
  364. return BT_GATT_ITER_STOP;
  365. }
  366. wlinfo("conn %p handle 0x%04x\n", conn, data->handle);
  367. nfy = bt_buf_extend(buf, sizeof(*nfy));
  368. nfy->handle = BT_HOST2LE16(data->handle);
  369. bt_buf_extend(buf, data->len);
  370. memcpy(nfy->value, data->data, data->len);
  371. bt_l2cap_send(conn, BT_L2CAP_CID_ATT, buf);
  372. bt_conn_release(conn);
  373. }
  374. return BT_GATT_ITER_CONTINUE;
  375. }
  376. void bt_gatt_notify(uint16_t handle, FAR const void *data, size_t len)
  377. {
  378. struct notify_data_s nfy;
  379. nfy.handle = handle;
  380. nfy.data = data;
  381. nfy.len = len;
  382. bt_gatt_foreach_attr(handle, 0xffff, notify_cb, &nfy);
  383. }
  384. static uint8_t connected_cb(FAR const struct bt_gatt_attr_s *attr,
  385. FAR void *user_data)
  386. {
  387. FAR struct bt_conn_s *conn = user_data;
  388. FAR struct _bt_gatt_ccc_s *ccc;
  389. size_t i;
  390. /* Check attribute user_data must be of type struct _bt_gatt_ccc_s */
  391. if (attr->write != bt_gatt_attr_write_ccc)
  392. {
  393. return BT_GATT_ITER_CONTINUE;
  394. }
  395. ccc = attr->user_data;
  396. /* If already enabled skip */
  397. if (ccc->value)
  398. {
  399. return BT_GATT_ITER_CONTINUE;
  400. }
  401. for (i = 0; i < ccc->cfg_len; i++)
  402. {
  403. /* Ignore configuration for different peer */
  404. if (bt_addr_le_cmp(&conn->dst, &ccc->cfg[i].peer))
  405. {
  406. continue;
  407. }
  408. if (ccc->cfg[i].value)
  409. {
  410. gatt_ccc_changed(ccc);
  411. return BT_GATT_ITER_CONTINUE;
  412. }
  413. }
  414. return BT_GATT_ITER_CONTINUE;
  415. }
  416. void bt_gatt_connected(FAR struct bt_conn_s *conn)
  417. {
  418. wlinfo("conn %p\n", conn);
  419. bt_gatt_foreach_attr(0x0001, 0xffff, connected_cb, conn);
  420. }
  421. static uint8_t disconnected_cb(FAR const struct bt_gatt_attr_s *attr,
  422. FAR void *user_data)
  423. {
  424. FAR struct bt_conn_s *conn = user_data;
  425. FAR struct _bt_gatt_ccc_s *ccc;
  426. size_t i;
  427. /* Check attribute user_data must be of type struct _bt_gatt_ccc_s */
  428. if (attr->write != bt_gatt_attr_write_ccc)
  429. {
  430. return BT_GATT_ITER_CONTINUE;
  431. }
  432. ccc = attr->user_data;
  433. /* If already disabled skip */
  434. if (!ccc->value)
  435. {
  436. return BT_GATT_ITER_CONTINUE;
  437. }
  438. for (i = 0; i < ccc->cfg_len; i++)
  439. {
  440. /* Ignore configurations with disabled value */
  441. if (!ccc->cfg[i].value)
  442. {
  443. continue;
  444. }
  445. if (bt_addr_le_cmp(&conn->dst, &ccc->cfg[i].peer))
  446. {
  447. FAR struct bt_conn_s *tmp;
  448. /* Skip if there is another peer connected */
  449. tmp = bt_conn_lookup_addr_le(&ccc->cfg[i].peer);
  450. if (tmp && tmp->state == BT_CONN_CONNECTED)
  451. {
  452. bt_conn_release(tmp);
  453. return BT_GATT_ITER_CONTINUE;
  454. }
  455. }
  456. }
  457. /* Reset value while disconnected */
  458. memset(&ccc->value, 0, sizeof(ccc->value));
  459. ccc->cfg_changed(ccc->value);
  460. wlinfo("ccc %p reset\n", ccc);
  461. return BT_GATT_ITER_CONTINUE;
  462. }
  463. void bt_gatt_disconnected(FAR struct bt_conn_s *conn)
  464. {
  465. wlinfo("conn %p\n", conn);
  466. bt_gatt_foreach_attr(0x0001, 0xffff, disconnected_cb, conn);
  467. }
  468. static void gatt_mtu_rsp(FAR struct bt_conn_s *conn, uint8_t err,
  469. FAR const void *pdu, uint16_t length,
  470. FAR void *user_data)
  471. {
  472. bt_gatt_rsp_func_t func = user_data;
  473. func(conn, err);
  474. }
  475. static int gatt_send(FAR struct bt_conn_s *conn, FAR struct bt_buf_s *buf,
  476. bt_att_func_t func, FAR void *user_data,
  477. bt_att_destroy_t destroy)
  478. {
  479. int err;
  480. err = bt_att_send(conn, buf, func, user_data, destroy);
  481. if (err)
  482. {
  483. wlerr("ERROR: Error sending ATT PDU: %d\n", err);
  484. bt_buf_release(buf);
  485. }
  486. return err;
  487. }
  488. int bt_gatt_exchange_mtu(FAR struct bt_conn_s *conn,
  489. bt_gatt_rsp_func_t func)
  490. {
  491. FAR struct bt_att_exchange_mtu_req_s *req;
  492. FAR struct bt_buf_s *buf;
  493. uint16_t mtu;
  494. if (!conn || !func)
  495. {
  496. return -EINVAL;
  497. }
  498. buf = bt_att_create_pdu(conn, BT_ATT_OP_MTU_REQ, sizeof(*req));
  499. if (!buf)
  500. {
  501. return -ENOMEM;
  502. }
  503. /* Select MTU based on the amount of room we have in bt_buf_s including one
  504. * extra byte for ATT header.
  505. */
  506. mtu = bt_buf_tailroom(buf) + 1;
  507. wlinfo("Client MTU %u\n", mtu);
  508. req = bt_buf_extend(buf, sizeof(*req));
  509. req->mtu = BT_HOST2LE16(mtu);
  510. return gatt_send(conn, buf, gatt_mtu_rsp, func, NULL);
  511. }
  512. static void att_find_type_rsp(FAR struct bt_conn_s *conn, uint8_t err,
  513. FAR const void *pdu, uint16_t length,
  514. FAR void *user_data)
  515. {
  516. FAR const struct bt_att_find_type_rsp_s *rsp = pdu;
  517. FAR struct bt_gatt_discover_params_s *params = user_data;
  518. uint16_t end_handle = 0;
  519. uint16_t start_handle;
  520. uint8_t i;
  521. wlinfo("err 0x%02x\n", err);
  522. if (err)
  523. {
  524. goto done;
  525. }
  526. /* Parse attributes found */
  527. for (i = 0; length >= sizeof(rsp->list[i]);
  528. i++, length -= sizeof(rsp->list[i]))
  529. {
  530. FAR const struct bt_gatt_attr_s *attr;
  531. start_handle = BT_LE162HOST(rsp->list[i].start_handle);
  532. end_handle = BT_LE162HOST(rsp->list[i].end_handle);
  533. wlinfo("start_handle 0x%04x end_handle 0x%04x\n", start_handle,
  534. end_handle);
  535. attr =
  536. (&(struct bt_gatt_attr_s)BT_GATT_PRIMARY_SERVICE
  537. (start_handle, params->uuid));
  538. if (params->func(attr, params) == BT_GATT_ITER_STOP)
  539. {
  540. goto done;
  541. }
  542. }
  543. /* Stop if could not parse the whole PDU */
  544. if (length > 0)
  545. {
  546. goto done;
  547. }
  548. /* Stop if over the range or the requests */
  549. if (end_handle >= params->end_handle)
  550. {
  551. goto done;
  552. }
  553. /* Continue for the last found handle */
  554. params->start_handle = end_handle;
  555. if (!bt_gatt_discover(conn, params))
  556. {
  557. return;
  558. }
  559. done:
  560. if (params->destroy)
  561. {
  562. params->destroy(params);
  563. }
  564. }
  565. /****************************************************************************
  566. * Name: bt_gatt_discover
  567. *
  568. * Description:
  569. * This function implements the SIOCBTDISCOVER ioctl command for the
  570. * GATT discovery.
  571. *
  572. ****************************************************************************/
  573. int bt_gatt_discover(FAR struct bt_conn_s *conn,
  574. FAR struct bt_gatt_discover_params_s *params)
  575. {
  576. FAR struct bt_buf_s *buf;
  577. FAR struct bt_att_find_type_req_s *req;
  578. FAR uint16_t *value;
  579. if (!conn || !params->uuid || !params->func || !params->start_handle ||
  580. !params->end_handle)
  581. {
  582. return -EINVAL;
  583. }
  584. buf = bt_att_create_pdu(conn, BT_ATT_OP_FIND_TYPE_REQ, sizeof(*req));
  585. if (!buf)
  586. {
  587. return -ENOMEM;
  588. }
  589. req = bt_buf_extend(buf, sizeof(*req));
  590. req->start_handle = BT_HOST2LE16(params->start_handle);
  591. req->end_handle = BT_HOST2LE16(params->end_handle);
  592. req->type = BT_HOST2LE16(BT_UUID_GATT_PRIMARY);
  593. wlinfo("uuid 0x%04x start_handle 0x%04x end_handle 0x%04x\n",
  594. params->uuid->u.u16, params->start_handle, params->end_handle);
  595. switch (params->uuid->type)
  596. {
  597. case BT_UUID_16:
  598. value = bt_buf_extend(buf, sizeof(*value));
  599. *value = BT_HOST2LE16(params->uuid->u.u16);
  600. break;
  601. case BT_UUID_128:
  602. bt_buf_extend(buf, sizeof(params->uuid->u.u128));
  603. memcpy(req->value,
  604. params->uuid->u.u128,
  605. sizeof(params->uuid->u.u128));
  606. break;
  607. default:
  608. wlerr("ERROR: Unknown UUID type %u\n", params->uuid->type);
  609. bt_buf_release(buf);
  610. return -EINVAL;
  611. }
  612. return gatt_send(conn, buf, att_find_type_rsp, params, NULL);
  613. }
  614. static void att_read_type_rsp(FAR struct bt_conn_s *conn, uint8_t err,
  615. FAR const void *pdu, uint16_t length,
  616. FAR void *user_data)
  617. {
  618. FAR const struct bt_att_read_type_rsp_s *rsp = pdu;
  619. FAR struct bt_gatt_discover_params_s *params = user_data;
  620. struct bt_uuid_s uuid;
  621. struct bt_gatt_chrc_s value;
  622. uint16_t handle = 0;
  623. wlinfo("err 0x%02x\n", err);
  624. if (err)
  625. {
  626. goto done;
  627. }
  628. /* Data can be either in UUID16 or UUID128 */
  629. switch (rsp->len)
  630. {
  631. case 7: /* UUID16 */
  632. uuid.type = BT_UUID_16;
  633. break;
  634. case 21: /* UUID128 */
  635. uuid.type = BT_UUID_128;
  636. break;
  637. default:
  638. wlerr("ERROR: Invalid data len %u\n", rsp->len);
  639. goto done;
  640. }
  641. /* Parse characteristics found */
  642. for (length--, pdu = rsp->data; length >= rsp->len;
  643. length -= rsp->len, pdu += rsp->len)
  644. {
  645. FAR const struct bt_gatt_attr_s *attr;
  646. FAR const struct bt_att_data_s *data = pdu;
  647. FAR struct gatt_chrc_s *chrc = (FAR void *)data->value;
  648. handle = BT_LE162HOST(data->handle);
  649. /* Handle 0 is invalid */
  650. if (!handle)
  651. {
  652. goto done;
  653. }
  654. /* Convert characteristic data, bt_gatt_chrc and gatt_chrc_s have
  655. * different formats so the conversion have to be done field by field.
  656. */
  657. value.properties = chrc->properties;
  658. value.value_handle = BT_LE162HOST(chrc->value_handle);
  659. value.uuid = &uuid;
  660. switch (uuid.type)
  661. {
  662. case BT_UUID_16:
  663. uuid.u.u16 = BT_LE162HOST(chrc->u.uuid16);
  664. break;
  665. case BT_UUID_128:
  666. memcpy(uuid.u.u128, chrc->u.uuid, sizeof(chrc->u.uuid));
  667. break;
  668. }
  669. wlinfo("handle 0x%04x properties 0x%02x value_handle 0x%04x\n",
  670. handle, value.properties, value.value_handle);
  671. /* Skip if UUID is set but doesn't match */
  672. if (params->uuid && bt_uuid_cmp(&uuid, params->uuid))
  673. {
  674. continue;
  675. }
  676. attr = (&(struct bt_gatt_attr_s)BT_GATT_CHARACTERISTIC(handle,
  677. &value));
  678. if (params->func(attr, params) == BT_GATT_ITER_STOP)
  679. {
  680. goto done;
  681. }
  682. }
  683. /* Stop if could not parse the whole PDU */
  684. if (length > 0)
  685. {
  686. goto done;
  687. }
  688. /* Next characteristic shall be after current value handle */
  689. params->start_handle = handle;
  690. if (params->start_handle < UINT16_MAX)
  691. {
  692. params->start_handle++;
  693. }
  694. /* Stop if over the requested range */
  695. if (params->start_handle >= params->end_handle)
  696. {
  697. goto done;
  698. }
  699. /* Continue to the next range */
  700. if (!bt_gatt_discover_characteristic(conn, params))
  701. {
  702. return;
  703. }
  704. done:
  705. if (params->destroy)
  706. {
  707. params->destroy(params);
  708. }
  709. }
  710. /****************************************************************************
  711. * Name: bt_gatt_discover_characteristic
  712. *
  713. * Description:
  714. * This function implements the SIOCBTDISCOVER ioctl command for the
  715. * GATT discover characteristics type.
  716. *
  717. ****************************************************************************/
  718. int bt_gatt_discover_characteristic(FAR struct bt_conn_s *conn,
  719. FAR struct bt_gatt_discover_params_s
  720. *params)
  721. {
  722. FAR struct bt_buf_s *buf;
  723. FAR struct bt_att_read_type_req_s *req;
  724. FAR uint16_t *value;
  725. if (!conn || !params->func || !params->start_handle || !params->end_handle)
  726. {
  727. return -EINVAL;
  728. }
  729. buf = bt_att_create_pdu(conn, BT_ATT_OP_READ_TYPE_REQ, sizeof(*req));
  730. if (!buf)
  731. {
  732. return -ENOMEM;
  733. }
  734. req = bt_buf_extend(buf, sizeof(*req));
  735. req->start_handle = BT_HOST2LE16(params->start_handle);
  736. req->end_handle = BT_HOST2LE16(params->end_handle);
  737. value = bt_buf_extend(buf, sizeof(*value));
  738. *value = BT_HOST2LE16(BT_UUID_GATT_CHRC);
  739. wlinfo("start_handle 0x%04x end_handle 0x%04x\n", params->start_handle,
  740. params->end_handle);
  741. return gatt_send(conn, buf, att_read_type_rsp, params, NULL);
  742. }
  743. static void att_find_info_rsp(FAR struct bt_conn_s *conn, uint8_t err,
  744. FAR const void *pdu, uint16_t length,
  745. FAR void *user_data)
  746. {
  747. FAR const struct bt_att_find_info_rsp_s *rsp = pdu;
  748. FAR struct bt_gatt_discover_params_s *params = user_data;
  749. struct bt_uuid_s uuid;
  750. uint16_t handle = 0;
  751. uint8_t len;
  752. union
  753. {
  754. FAR const struct bt_att_info_16_s *i16;
  755. FAR const struct bt_att_info_128_s *i128;
  756. } info;
  757. wlinfo("err 0x%02x\n", err);
  758. if (err)
  759. {
  760. goto done;
  761. }
  762. /* Data can be either in UUID16 or UUID128 */
  763. switch (rsp->format)
  764. {
  765. case BT_ATT_INFO_16:
  766. uuid.type = BT_UUID_16;
  767. len = sizeof(info.i16);
  768. break;
  769. case BT_ATT_INFO_128:
  770. uuid.type = BT_UUID_128;
  771. len = sizeof(info.i128);
  772. break;
  773. default:
  774. wlerr("ERROR: Invalid format %u\n", rsp->format);
  775. goto done;
  776. }
  777. /* Parse descriptors found */
  778. for (length--, pdu = rsp->info; length >= len; length -= len, pdu += len)
  779. {
  780. FAR const struct bt_gatt_attr_s *attr;
  781. info.i16 = pdu;
  782. handle = BT_LE162HOST(info.i16->handle);
  783. switch (uuid.type)
  784. {
  785. case BT_UUID_16:
  786. uuid.u.u16 = BT_LE162HOST(info.i16->uuid);
  787. break;
  788. case BT_UUID_128:
  789. memcpy(uuid.u.u128, info.i128->uuid, sizeof(uuid.u.u128));
  790. break;
  791. }
  792. wlinfo("handle 0x%04x\n", handle);
  793. /* Skip if UUID is set but doesn't match */
  794. if (params->uuid && bt_uuid_cmp(&uuid, params->uuid))
  795. {
  796. continue;
  797. }
  798. attr =
  799. (&(struct bt_gatt_attr_s)BT_GATT_DESCRIPTOR
  800. (handle, &uuid, 0, NULL, NULL, NULL));
  801. if (params->func(attr, params) == BT_GATT_ITER_STOP)
  802. {
  803. goto done;
  804. }
  805. }
  806. /* Stop if could not parse the whole PDU */
  807. if (length > 0)
  808. {
  809. goto done;
  810. }
  811. /* Next characteristic shall be after current value handle */
  812. params->start_handle = handle;
  813. if (params->start_handle < UINT16_MAX)
  814. {
  815. params->start_handle++;
  816. }
  817. /* Stop if over the requested range */
  818. if (params->start_handle >= params->end_handle)
  819. {
  820. goto done;
  821. }
  822. /* Continue to the next range */
  823. if (!bt_gatt_discover_descriptor(conn, params))
  824. {
  825. return;
  826. }
  827. done:
  828. if (params->destroy)
  829. {
  830. params->destroy(params);
  831. }
  832. }
  833. /****************************************************************************
  834. * Name: bt_gatt_discover_descriptor
  835. *
  836. * Description:
  837. * This function implements the SIOCBTDISCOVER ioctl command for the
  838. * GATT discover descriptor type.
  839. *
  840. ****************************************************************************/
  841. int bt_gatt_discover_descriptor(FAR struct bt_conn_s *conn,
  842. FAR struct bt_gatt_discover_params_s *params)
  843. {
  844. FAR struct bt_buf_s *buf;
  845. FAR struct bt_att_find_info_req_s *req;
  846. if (!conn || !params->func || !params->start_handle || !params->end_handle)
  847. {
  848. return -EINVAL;
  849. }
  850. buf = bt_att_create_pdu(conn, BT_ATT_OP_FIND_INFO_REQ, sizeof(*req));
  851. if (!buf)
  852. {
  853. return -ENOMEM;
  854. }
  855. req = bt_buf_extend(buf, sizeof(*req));
  856. req->start_handle = BT_HOST2LE16(params->start_handle);
  857. req->end_handle = BT_HOST2LE16(params->end_handle);
  858. wlinfo("start_handle 0x%04x end_handle 0x%04x\n", params->start_handle,
  859. params->end_handle);
  860. return gatt_send(conn, buf, att_find_info_rsp, params, NULL);
  861. }
  862. static void att_read_rsp(FAR struct bt_conn_s *conn, uint8_t err,
  863. FAR const void *pdu, uint16_t length,
  864. FAR void *user_data)
  865. {
  866. bt_gatt_read_func_t func = user_data;
  867. wlinfo("err 0x%02x\n", err);
  868. if (err)
  869. {
  870. func(conn, err, NULL, 0);
  871. return;
  872. }
  873. func(conn, 0, pdu, length);
  874. }
  875. static int gatt_read_blob(FAR struct bt_conn_s *conn, uint16_t handle,
  876. uint16_t offset, bt_gatt_read_func_t func)
  877. {
  878. FAR struct bt_buf_s *buf;
  879. FAR struct bt_att_read_blob_req_s *req;
  880. buf = bt_att_create_pdu(conn, BT_ATT_OP_READ_BLOB_REQ, sizeof(*req));
  881. if (!buf)
  882. {
  883. return -ENOMEM;
  884. }
  885. req = bt_buf_extend(buf, sizeof(*req));
  886. req->handle = BT_HOST2LE16(handle);
  887. req->offset = BT_HOST2LE16(offset);
  888. wlinfo("handle 0x%04x offset 0x%04x\n", handle, offset);
  889. return gatt_send(conn, buf, att_read_rsp, func, NULL);
  890. }
  891. int bt_gatt_read(FAR struct bt_conn_s *conn, uint16_t handle,
  892. uint16_t offset, bt_gatt_read_func_t func)
  893. {
  894. FAR struct bt_buf_s *buf;
  895. FAR struct bt_att_read_req_s *req;
  896. if (!conn || !handle || !func)
  897. {
  898. return -EINVAL;
  899. }
  900. if (offset)
  901. {
  902. return gatt_read_blob(conn, handle, offset, func);
  903. }
  904. buf = bt_att_create_pdu(conn, BT_ATT_OP_READ_REQ, sizeof(*req));
  905. if (!buf)
  906. {
  907. return -ENOMEM;
  908. }
  909. req = bt_buf_extend(buf, sizeof(*req));
  910. req->handle = BT_HOST2LE16(handle);
  911. wlinfo("handle 0x%04x\n", handle);
  912. return gatt_send(conn, buf, att_read_rsp, func, NULL);
  913. }
  914. static void att_write_rsp(FAR struct bt_conn_s *conn, uint8_t err,
  915. FAR const void *pdu, uint16_t length,
  916. FAR void *user_data)
  917. {
  918. bt_gatt_rsp_func_t func = user_data;
  919. wlinfo("err 0x%02x\n", err);
  920. func(conn, err);
  921. }
  922. static int gatt_write_cmd(FAR struct bt_conn_s *conn, uint16_t handle,
  923. FAR const void *data, uint16_t length)
  924. {
  925. FAR struct bt_buf_s *buf;
  926. FAR struct bt_att_write_cmd_s *cmd;
  927. buf = bt_att_create_pdu(conn, BT_ATT_OP_WRITE_CMD, sizeof(*cmd) + length);
  928. if (!buf)
  929. {
  930. return -ENOMEM;
  931. }
  932. cmd = bt_buf_extend(buf, sizeof(*cmd));
  933. cmd->handle = BT_HOST2LE16(handle);
  934. memcpy(cmd->value, data, length);
  935. bt_buf_extend(buf, length);
  936. wlinfo("handle 0x%04x length %u\n", handle, length);
  937. return gatt_send(conn, buf, NULL, NULL, NULL);
  938. }
  939. int bt_gatt_write(FAR struct bt_conn_s *conn, uint16_t handle,
  940. FAR const void *data, uint16_t length,
  941. bt_gatt_rsp_func_t func)
  942. {
  943. FAR struct bt_buf_s *buf;
  944. FAR struct bt_att_write_req_s *req;
  945. if (!conn || !handle)
  946. {
  947. return -EINVAL;
  948. }
  949. if (!func)
  950. {
  951. return gatt_write_cmd(conn, handle, data, length);
  952. }
  953. buf = bt_att_create_pdu(conn, BT_ATT_OP_WRITE_REQ, sizeof(*req) + length);
  954. if (!buf)
  955. {
  956. return -ENOMEM;
  957. }
  958. req = bt_buf_extend(buf, sizeof(*req));
  959. req->handle = BT_HOST2LE16(handle);
  960. memcpy(req->value, data, length);
  961. bt_buf_extend(buf, length);
  962. wlinfo("handle 0x%04x length %u\n", handle, length);
  963. return gatt_send(conn, buf, att_write_rsp, func, NULL);
  964. }
  965. void bt_gatt_cancel(FAR struct bt_conn_s *conn)
  966. {
  967. bt_att_cancel(conn);
  968. }
  969. int bt_gatt_read_multiple(FAR struct bt_conn_s *conn,
  970. FAR const uint16_t *handles, size_t count,
  971. bt_gatt_read_func_t func)
  972. {
  973. FAR struct bt_buf_s *buf;
  974. uint8_t i;
  975. if (!conn && conn->state != BT_CONN_CONNECTED)
  976. {
  977. return -ENOTCONN;
  978. }
  979. if (!handles || count < 2 || !func)
  980. {
  981. return -EINVAL;
  982. }
  983. buf = bt_att_create_pdu(conn, BT_ATT_OP_READ_MULT_REQ,
  984. count * sizeof(*handles));
  985. if (!buf)
  986. {
  987. return -ENOMEM;
  988. }
  989. for (i = 0; i < count; i++)
  990. {
  991. bt_buf_put_le16(buf, handles[i]);
  992. }
  993. return gatt_send(conn, buf, att_read_rsp, func, NULL);
  994. }