blebrr_gatt.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. 
  2. /**
  3. * \file blebrr_gatt.c
  4. *
  5. * This File contains the BLE Bearer interface for the GATT bearer over
  6. * Mindtree Mesh stack.
  7. */
  8. /*
  9. * Copyright (C) 2016. Mindtree Ltd.
  10. * All rights reserved.
  11. */
  12. /* ------------------------------- Header File Inclusion */
  13. #include "MS_brr_api.h"
  14. #include "MS_prov_api.h"
  15. #include "blebrr.h"
  16. /* --------------------------------------------- External Global Variables */
  17. /* --------------------------------------------- Global Definitions */
  18. /* GATT Proxy Segmentation and Reassembly related states */
  19. #define BLEBRR_GATT_SAR_COMPLETE_PKT 0x00
  20. #define BLEBRR_GATT_SAR_START_PKT 0x01
  21. #define BLEBRR_GATT_SAR_CONTINUE_PKT 0x02
  22. #define BLEBRR_GATT_SAR_END_PKT 0x03
  23. /* GATT Proxy Segmentation and Reassembly related state values */
  24. #define BLEBRR_GATT_SAR_INIT_STATE 0x00
  25. #define BLEBRR_GATT_SAR_TX_PROGRESS 0x01
  26. /* GATT PDU Size in octets */
  27. #define BLEBRR_GATT_PDU_SIZE 75
  28. /* --------------------------------------------- Macros */
  29. /* --------------------------------------------- Structures/Data Types */
  30. /* --------------------------------------------- Global Variables */
  31. /** GATT bearer Information */
  32. DECL_STATIC BRR_BEARER_INFO blebrr_gatt;
  33. /* TODO: Check if the MTU be global or part of specific context */
  34. /** GATT bearer specific identifiers */
  35. DECL_STATIC BRR_BEARER_CH_INFO blebrr_gatt_ch_info;
  36. /** GATT Current mode identifier - PROV or PROXY */
  37. DECL_STATIC UCHAR blebrr_gatt_mode = 0xFF;
  38. /* ------------------------------- Functions */
  39. /**
  40. * \brief
  41. *
  42. * \par Description
  43. *
  44. *
  45. * \param flag
  46. *
  47. * \return void
  48. */
  49. void blebrr_gatt_mode_set(UCHAR flag)
  50. {
  51. /**
  52. * The valid values for 'flag' are:
  53. * 0x00: BLEBRR_GATT_PROV_MODE GATT Provisioning Mode
  54. * 0x01: BLEBRR_GATT_PROXY_MODE GATT Proxy Mode
  55. * All other values are RFU.
  56. */
  57. blebrr_gatt_mode = ((flag == BLEBRR_GATT_PROV_MODE) || (flag == BLEBRR_GATT_PROXY_MODE))?
  58. flag: 0xFF;
  59. /* Notify GATT mode setting to blebrr pl */
  60. blebrr_set_gattmode_pl (blebrr_gatt_mode);
  61. }
  62. /**
  63. * \brief
  64. *
  65. * \par Description
  66. *
  67. * *
  68. * * \param void
  69. *
  70. * \return void
  71. */
  72. UCHAR blebrr_gatt_mode_get(void)
  73. {
  74. /**
  75. * The valid return values are:
  76. * 0x00: BLEBRR_GATT_PROV_MODE GATT Provisioning Mode
  77. * 0x01: BLEBRR_GATT_PROXY_MODE GATT Proxy Mode
  78. * All other values are RFU.
  79. */
  80. return blebrr_gatt_mode;
  81. }
  82. /**
  83. * \brief
  84. *
  85. * \par Description
  86. *
  87. *
  88. * \param handle
  89. * \param type
  90. * \param pdata
  91. * \param datalen
  92. *
  93. * \return void
  94. */
  95. DECL_STATIC API_RESULT blebrr_gatt_send
  96. (
  97. BRR_HANDLE * handle,
  98. UCHAR type,
  99. void * pdata,
  100. UINT16 datalen
  101. )
  102. {
  103. API_RESULT retval;
  104. MS_IGNORE_UNUSED_PARAM(type);
  105. retval = blebrr_gatt_send_pl
  106. (
  107. handle,
  108. pdata,
  109. datalen
  110. );
  111. // if (API_SUCCESS != retval)
  112. // {
  113. // BLEBRR_LOG("Error - 0x%04X\n", retval);
  114. // }
  115. return retval;
  116. }
  117. /**
  118. * \brief
  119. *
  120. * \par Description
  121. *
  122. *
  123. * \param handle
  124. * \param type
  125. * \param pdata
  126. * \param pdatalen
  127. *
  128. * \return void
  129. */
  130. API_RESULT blebrr_pl_recv_gattpacket
  131. (
  132. BRR_HANDLE * handle,
  133. UCHAR * pdata,
  134. UINT16 pdatalen
  135. )
  136. {
  137. if (NULL != blebrr_gatt.bearer_recv)
  138. {
  139. blebrr_gatt.bearer_recv
  140. (
  141. handle,
  142. pdata,
  143. pdatalen,
  144. NULL
  145. );
  146. }
  147. return API_SUCCESS;
  148. }
  149. /**
  150. * \brief
  151. *
  152. * \par Description
  153. *
  154. *
  155. * \param type
  156. * \param uuid
  157. * \param handle
  158. *
  159. * \return void
  160. */
  161. API_RESULT blebrr_pl_gatt_connection
  162. (
  163. BRR_HANDLE * handle,
  164. UCHAR role,
  165. UCHAR mode,
  166. UINT16 mtu
  167. )
  168. {
  169. MS_BUFFER buffer;
  170. API_RESULT retval;
  171. /* Initialize */
  172. retval = API_FAILURE;
  173. /* Populate the Bearer GATT Channel info */
  174. blebrr_gatt_ch_info.role = role;
  175. blebrr_gatt_ch_info.mtu = mtu;
  176. /* Add bearer to the mesh network */
  177. blebrr_gatt.bearer_send = blebrr_gatt_send;
  178. blebrr_gatt.binfo = &buffer;
  179. buffer.payload = (UCHAR*)&blebrr_gatt_ch_info;
  180. buffer.length = sizeof(blebrr_gatt_ch_info);
  181. retval = MS_brr_add_bearer(BRR_TYPE_GATT, &blebrr_gatt, handle);
  182. /* Check the PDU type received and Add bearer to Mesh stack */
  183. if (BLEBRR_SERVER_ROLE == role)
  184. {
  185. blebrr_pl_advertise_end();
  186. if (BLEBRR_GATT_PROXY_MODE == mode)
  187. {
  188. /* Start observing */
  189. blebrr_scan_enable();
  190. }
  191. }
  192. else if (BLEBRR_CLIENT_ROLE == role)
  193. {
  194. /* Do Nothing */
  195. /**
  196. * Currently, not enabling scan for Proxy Client.
  197. * Typically, Proxy Client supports only GATT Bearer.
  198. * Hence, not initiating 'SCAN' on Bearer UP event.
  199. */
  200. }
  201. return retval;
  202. }
  203. /**
  204. * \brief
  205. *
  206. * \par Description
  207. *
  208. *
  209. * \param type
  210. * \param handle
  211. *
  212. * \return void
  213. */
  214. API_RESULT blebrr_pl_gatt_disconnection
  215. (
  216. BRR_HANDLE * handle
  217. )
  218. {
  219. API_RESULT retval;
  220. retval = MS_brr_remove_bearer(BRR_TYPE_GATT, handle);
  221. return retval;
  222. }