scanparamservice.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /**
  2. * @file
  3. * @author chipsea
  4. * @brief
  5. * @version 0.1
  6. * @date 2020-11-30
  7. * @copyright Copyright (c) 2020, CHIPSEA Co., Ltd.
  8. * @note
  9. */
  10. /*********************************************************************
  11. * INCLUDES
  12. */
  13. #include "bcomdef.h"
  14. #include "OSAL.h"
  15. #include "att.h"
  16. #include "gatt.h"
  17. #include "gatt_uuid.h"
  18. #include "gattservapp.h"
  19. #include "gatt_profile_uuid.h"
  20. #include "gapbondmgr.h"
  21. #include "linkdb.h"
  22. #include "scanparamservice.h"
  23. /*********************************************************************
  24. * MACROS
  25. */
  26. /*********************************************************************
  27. * CONSTANTS
  28. */
  29. /*********************************************************************
  30. * TYPEDEFS
  31. */
  32. /*********************************************************************
  33. * GLOBAL VARIABLES
  34. */
  35. // Scan parameters service
  36. CONST uint8 scanParamServUUID[ATT_BT_UUID_SIZE] =
  37. {
  38. LO_UINT16(SCAN_PARAM_SERV_UUID), HI_UINT16(SCAN_PARAM_SERV_UUID)
  39. };
  40. // Scan interval window characteristic
  41. CONST uint8 scanIntervalWindowUUID[ATT_BT_UUID_SIZE] =
  42. {
  43. LO_UINT16(SCAN_INTERVAL_WINDOW_UUID), HI_UINT16(SCAN_INTERVAL_WINDOW_UUID)
  44. };
  45. // Scan parameter refresh characteristic
  46. CONST uint8 scanParamRefreshUUID[ATT_BT_UUID_SIZE] =
  47. {
  48. LO_UINT16(SCAN_REFRESH_UUID), HI_UINT16(SCAN_REFRESH_UUID)
  49. };
  50. /*********************************************************************
  51. * EXTERNAL VARIABLES
  52. */
  53. /*********************************************************************
  54. * EXTERNAL FUNCTIONS
  55. */
  56. /*********************************************************************
  57. * LOCAL VARIABLES
  58. */
  59. // Application callback
  60. static scanParamServiceCB_t scanParamServiceCB;
  61. /*********************************************************************
  62. * Profile Attributes - variables
  63. */
  64. // Scan Parameters Service attribute
  65. static CONST gattAttrType_t scanParamService = { ATT_BT_UUID_SIZE, scanParamServUUID };
  66. // Scan Interval Window characteristic
  67. static uint8 scanIntervalWindowProps = GATT_PROP_WRITE_NO_RSP;
  68. static uint8 scanIntervalWindow[SCAN_INTERVAL_WINDOW_CHAR_LEN];
  69. // Scan Parameter Refresh characteristic
  70. static uint8 scanParamRefreshProps = GATT_PROP_NOTIFY;
  71. static uint8 scanParamRefresh[SCAN_PARAM_REFRESH_LEN];
  72. static gattCharCfg_t scanParamRefreshClientCharCfg[GATT_MAX_NUM_CONN];
  73. /*********************************************************************
  74. * Profile Attributes - Table
  75. */
  76. static gattAttribute_t scanParamAttrTbl[] =
  77. {
  78. // Scan Parameters Service attribute
  79. {
  80. { ATT_BT_UUID_SIZE, primaryServiceUUID }, /* type */
  81. GATT_PERMIT_READ, /* permissions */
  82. 0, /* handle */
  83. (uint8 *)&scanParamService /* pValue */
  84. },
  85. // Scan Interval Window declaration
  86. {
  87. { ATT_BT_UUID_SIZE, characterUUID },
  88. GATT_PERMIT_READ,
  89. 0,
  90. &scanIntervalWindowProps
  91. },
  92. // Scan Interval Window characteristic
  93. {
  94. { ATT_BT_UUID_SIZE, scanIntervalWindowUUID },
  95. GATT_PERMIT_ENCRYPT_WRITE,
  96. 0,
  97. scanIntervalWindow
  98. },
  99. // Scan Parameter Refresh declaration
  100. {
  101. { ATT_BT_UUID_SIZE, characterUUID },
  102. GATT_PERMIT_READ,
  103. 0,
  104. &scanParamRefreshProps
  105. },
  106. // Scan Parameter Refresh characteristic
  107. {
  108. { ATT_BT_UUID_SIZE, scanParamRefreshUUID },
  109. 0,
  110. 0,
  111. scanParamRefresh
  112. },
  113. // Scan Parameter Refresh characteristic client characteristic configuration
  114. {
  115. { ATT_BT_UUID_SIZE, clientCharCfgUUID },
  116. GATT_PERMIT_READ | GATT_PERMIT_ENCRYPT_WRITE,
  117. 0,
  118. (uint8 *) &scanParamRefreshClientCharCfg
  119. }
  120. };
  121. // Attribute index enumeration-- these indexes match array elements above
  122. enum
  123. {
  124. SCAN_PARAM_SERVICE_IDX, // Scan Parameters Service
  125. SCAN_PARAM_INTERVAL_DECL_IDX, // Scan Interval Window declaration
  126. SCAN_PARAM_INTERVAL_IDX, // Scan Interval Window characteristic
  127. SCAN_PARAM_REFRESH_DECL_IDX, // Scan Parameter Refresh declaration
  128. SCAN_PARAM_REFRESH_IDX, // Scan Parameter Refresh characteristic
  129. SCAN_PARAM_REFRESH_CCCD_IDX // Scan Parameter Refresh characteristic client characteristic configuration
  130. };
  131. /*********************************************************************
  132. * LOCAL FUNCTIONS
  133. */
  134. static bStatus_t scanParamWriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  135. uint8 *pValue, uint16 len, uint16 offset );
  136. static uint8 scanParamReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  137. uint8 *pValue, uint16 *pLen, uint16 offset, uint8 maxLen );
  138. /*********************************************************************
  139. * PROFILE CALLBACKS
  140. */
  141. // Service Callbacks
  142. CONST gattServiceCBs_t scanParamCBs =
  143. {
  144. scanParamReadAttrCB, // Read callback function pointer
  145. scanParamWriteAttrCB, // Write callback function pointer
  146. NULL // Authorization callback function pointer
  147. };
  148. /*********************************************************************
  149. * PUBLIC FUNCTIONS
  150. */
  151. /*********************************************************************
  152. * @fn ScanParam_AddService
  153. *
  154. * @brief Initializes the Battery Service by registering
  155. * GATT attributes with the GATT server.
  156. *
  157. * @return Success or Failure
  158. */
  159. bStatus_t ScanParam_AddService( void )
  160. {
  161. uint8 status = SUCCESS;
  162. // Initialize Client Characteristic Configuration attributes
  163. GATTServApp_InitCharCfg( INVALID_CONNHANDLE, scanParamRefreshClientCharCfg );
  164. // Register GATT attribute list and CBs with GATT Server App
  165. status = GATTServApp_RegisterService( scanParamAttrTbl, GATT_NUM_ATTRS( scanParamAttrTbl ),
  166. &scanParamCBs );
  167. return ( status );
  168. }
  169. /*********************************************************************
  170. * @fn ScanParam_Register
  171. *
  172. * @brief Register a callback function with the Battery Service.
  173. *
  174. * @param pfnServiceCB - Callback function.
  175. *
  176. * @return None.
  177. */
  178. extern void ScanParam_Register( scanParamServiceCB_t pfnServiceCB )
  179. {
  180. scanParamServiceCB = pfnServiceCB;
  181. }
  182. /*********************************************************************
  183. * @fn ScanParam_SetParameter
  184. *
  185. * @brief Set a Battery Service parameter.
  186. *
  187. * @param param - Profile parameter ID
  188. * @param len - length of data to right
  189. * @param value - pointer to data to write. This is dependent on
  190. * the parameter ID and WILL be cast to the appropriate
  191. * data type (example: data type of uint16 will be cast to
  192. * uint16 pointer).
  193. *
  194. * @return bStatus_t
  195. */
  196. bStatus_t ScanParam_SetParameter( uint8 param, uint8 len, void *value )
  197. {
  198. bStatus_t ret = SUCCESS;
  199. switch ( param )
  200. {
  201. default:
  202. ret = INVALIDPARAMETER;
  203. break;
  204. }
  205. return ( ret );
  206. }
  207. /*********************************************************************
  208. * @fn ScanParam_GetParameter
  209. *
  210. * @brief Get a Battery Service parameter.
  211. *
  212. * @param param - Profile parameter ID
  213. * @param value - pointer to data to get. This is dependent on
  214. * the parameter ID and WILL be cast to the appropriate
  215. * data type (example: data type of uint16 will be cast to
  216. * uint16 pointer).
  217. *
  218. * @return bStatus_t
  219. */
  220. bStatus_t ScanParam_GetParameter( uint8 param, void *value )
  221. {
  222. bStatus_t ret = SUCCESS;
  223. switch ( param )
  224. {
  225. case SCAN_PARAM_PARAM_INTERVAL:
  226. *((uint16*)value) = BUILD_UINT16(scanIntervalWindow[0],
  227. scanIntervalWindow[1]);
  228. break;
  229. case SCAN_PARAM_PARAM_WINDOW:
  230. *((uint16*)value) = BUILD_UINT16(scanIntervalWindow[2],
  231. scanIntervalWindow[3]);
  232. break;
  233. default:
  234. ret = INVALIDPARAMETER;
  235. break;
  236. }
  237. return ( ret );
  238. }
  239. /*********************************************************************
  240. * @fn ScanParam_RefreshNotify
  241. *
  242. * @brief Notify the peer to refresh the scan parameters.
  243. *
  244. * @param connHandle - connection handle
  245. *
  246. * @return None
  247. */
  248. void ScanParam_RefreshNotify( uint16 connHandle )
  249. {
  250. attHandleValueNoti_t noti;
  251. uint16 value;
  252. value = GATTServApp_ReadCharCfg( connHandle, scanParamRefreshClientCharCfg );
  253. if ( value & GATT_CLIENT_CFG_NOTIFY )
  254. {
  255. // send notification
  256. noti.handle = scanParamAttrTbl[SCAN_PARAM_REFRESH_CCCD_IDX].handle;
  257. noti.len = SCAN_PARAM_REFRESH_LEN;
  258. noti.value[0] = SCAN_PARAM_REFRESH_REQ;
  259. GATT_Notification( connHandle, &noti, FALSE );
  260. }
  261. }
  262. /*********************************************************************
  263. * @fn scanParamReadAttrCB
  264. *
  265. * @brief GATT read callback.
  266. *
  267. * @param connHandle - connection message was received on
  268. * @param pAttr - pointer to attribute
  269. * @param pValue - pointer to data to be read
  270. * @param pLen - length of data to be read
  271. * @param offset - offset of the first octet to be read
  272. * @param maxLen - maximum length of data to be read
  273. *
  274. * @return Success or Failure
  275. */
  276. static uint8 scanParamReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  277. uint8 *pValue, uint16 *pLen, uint16 offset, uint8 maxLen )
  278. {
  279. bStatus_t status = SUCCESS;
  280. return ( status );
  281. }
  282. /*********************************************************************
  283. * @fn scanParamWriteAttrCB
  284. *
  285. * @brief Validate attribute data prior to a write operation
  286. *
  287. * @param connHandle - connection message was received on
  288. * @param pAttr - pointer to attribute
  289. * @param pValue - pointer to data to be written
  290. * @param len - length of data
  291. * @param offset - offset of the first octet to be written
  292. *
  293. * @return Success or Failure
  294. */
  295. static bStatus_t scanParamWriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  296. uint8 *pValue, uint16 len, uint16 offset )
  297. {
  298. bStatus_t status = SUCCESS;
  299. // Make sure it's not a blob operation (no attributes in the profile are long)
  300. if ( offset > 0 )
  301. {
  302. return ( ATT_ERR_ATTR_NOT_LONG );
  303. }
  304. uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
  305. // Only one writeable attribute
  306. if ( uuid == SCAN_INTERVAL_WINDOW_UUID )
  307. {
  308. if ( len == SCAN_INTERVAL_WINDOW_CHAR_LEN )
  309. {
  310. uint16 interval = BUILD_UINT16( pValue[0], pValue[1] );
  311. uint16 window = BUILD_UINT16( pValue[0], pValue[1] );
  312. // Validate values
  313. if ( window <= interval )
  314. {
  315. osal_memcpy( pAttr->pValue, pValue, len );
  316. (*scanParamServiceCB)( SCAN_INTERVAL_WINDOW_SET );
  317. }
  318. else
  319. {
  320. status = ATT_ERR_INVALID_VALUE;
  321. }
  322. }
  323. else
  324. {
  325. status = ATT_ERR_INVALID_VALUE_SIZE;
  326. }
  327. }
  328. else if ( uuid == GATT_CLIENT_CHAR_CFG_UUID )
  329. {
  330. status = GATTServApp_ProcessCCCWriteReq( connHandle, pAttr, pValue, len,
  331. offset, GATT_CLIENT_CFG_NOTIFY );
  332. }
  333. else
  334. {
  335. status = ATT_ERR_ATTR_NOT_FOUND;
  336. }
  337. return ( status );
  338. }
  339. /*********************************************************************
  340. * @fn ScanParam_HandleConnStatusCB
  341. *
  342. * @brief Service link status change handler function.
  343. *
  344. * @param connHandle - connection handle
  345. * @param changeType - type of change
  346. *
  347. * @return none
  348. */
  349. void ScanParam_HandleConnStatusCB( uint16 connHandle, uint8 changeType )
  350. {
  351. // Make sure this is not loopback connection
  352. if ( connHandle != LOOPBACK_CONNHANDLE )
  353. {
  354. // Reset Client Char Config if connection has dropped
  355. if ( ( changeType == LINKDB_STATUS_UPDATE_REMOVED ) ||
  356. ( ( changeType == LINKDB_STATUS_UPDATE_STATEFLAGS ) &&
  357. ( !linkDB_Up( connHandle ) ) ) )
  358. {
  359. GATTServApp_InitCharCfg( connHandle, scanParamRefreshClientCharCfg );
  360. }
  361. }
  362. }