scanparamservice.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. /*********************************************************************
  29. * INCLUDES
  30. */
  31. #include "bcomdef.h"
  32. #include "OSAL.h"
  33. #include "att.h"
  34. #include "gatt.h"
  35. #include "gatt_uuid.h"
  36. #include "gattservapp.h"
  37. #include "gatt_profile_uuid.h"
  38. #include "gapbondmgr.h"
  39. #include "linkdb.h"
  40. #include "scanparamservice.h"
  41. /*********************************************************************
  42. * MACROS
  43. */
  44. /*********************************************************************
  45. * CONSTANTS
  46. */
  47. /*********************************************************************
  48. * TYPEDEFS
  49. */
  50. /*********************************************************************
  51. * GLOBAL VARIABLES
  52. */
  53. // Scan parameters service
  54. CONST uint8 scanParamServUUID[ATT_BT_UUID_SIZE] =
  55. {
  56. LO_UINT16(SCAN_PARAM_SERV_UUID), HI_UINT16(SCAN_PARAM_SERV_UUID)
  57. };
  58. // Scan interval window characteristic
  59. CONST uint8 scanIntervalWindowUUID[ATT_BT_UUID_SIZE] =
  60. {
  61. LO_UINT16(SCAN_INTERVAL_WINDOW_UUID), HI_UINT16(SCAN_INTERVAL_WINDOW_UUID)
  62. };
  63. // Scan parameter refresh characteristic
  64. CONST uint8 scanParamRefreshUUID[ATT_BT_UUID_SIZE] =
  65. {
  66. LO_UINT16(SCAN_REFRESH_UUID), HI_UINT16(SCAN_REFRESH_UUID)
  67. };
  68. /*********************************************************************
  69. * EXTERNAL VARIABLES
  70. */
  71. /*********************************************************************
  72. * EXTERNAL FUNCTIONS
  73. */
  74. /*********************************************************************
  75. * LOCAL VARIABLES
  76. */
  77. // Application callback
  78. static scanParamServiceCB_t scanParamServiceCB;
  79. /*********************************************************************
  80. * Profile Attributes - variables
  81. */
  82. // Scan Parameters Service attribute
  83. static CONST gattAttrType_t scanParamService = { ATT_BT_UUID_SIZE, scanParamServUUID };
  84. // Scan Interval Window characteristic
  85. static uint8 scanIntervalWindowProps = GATT_PROP_WRITE_NO_RSP;
  86. static uint8 scanIntervalWindow[SCAN_INTERVAL_WINDOW_CHAR_LEN];
  87. // Scan Parameter Refresh characteristic
  88. static uint8 scanParamRefreshProps = GATT_PROP_NOTIFY;
  89. static uint8 scanParamRefresh[SCAN_PARAM_REFRESH_LEN];
  90. static gattCharCfg_t scanParamRefreshClientCharCfg[GATT_MAX_NUM_CONN];
  91. /*********************************************************************
  92. * Profile Attributes - Table
  93. */
  94. static gattAttribute_t scanParamAttrTbl[] =
  95. {
  96. // Scan Parameters Service attribute
  97. {
  98. { ATT_BT_UUID_SIZE, primaryServiceUUID }, /* type */
  99. GATT_PERMIT_READ, /* permissions */
  100. 0, /* handle */
  101. (uint8 *)&scanParamService /* pValue */
  102. },
  103. // Scan Interval Window declaration
  104. {
  105. { ATT_BT_UUID_SIZE, characterUUID },
  106. GATT_PERMIT_READ,
  107. 0,
  108. &scanIntervalWindowProps
  109. },
  110. // Scan Interval Window characteristic
  111. {
  112. { ATT_BT_UUID_SIZE, scanIntervalWindowUUID },
  113. GATT_PERMIT_ENCRYPT_WRITE,
  114. 0,
  115. scanIntervalWindow
  116. },
  117. // Scan Parameter Refresh declaration
  118. {
  119. { ATT_BT_UUID_SIZE, characterUUID },
  120. GATT_PERMIT_READ,
  121. 0,
  122. &scanParamRefreshProps
  123. },
  124. // Scan Parameter Refresh characteristic
  125. {
  126. { ATT_BT_UUID_SIZE, scanParamRefreshUUID },
  127. 0,
  128. 0,
  129. scanParamRefresh
  130. },
  131. // Scan Parameter Refresh characteristic client characteristic configuration
  132. {
  133. { ATT_BT_UUID_SIZE, clientCharCfgUUID },
  134. GATT_PERMIT_READ | GATT_PERMIT_ENCRYPT_WRITE,
  135. 0,
  136. (uint8 *) &scanParamRefreshClientCharCfg
  137. }
  138. };
  139. // Attribute index enumeration-- these indexes match array elements above
  140. enum
  141. {
  142. SCAN_PARAM_SERVICE_IDX, // Scan Parameters Service
  143. SCAN_PARAM_INTERVAL_DECL_IDX, // Scan Interval Window declaration
  144. SCAN_PARAM_INTERVAL_IDX, // Scan Interval Window characteristic
  145. SCAN_PARAM_REFRESH_DECL_IDX, // Scan Parameter Refresh declaration
  146. SCAN_PARAM_REFRESH_IDX, // Scan Parameter Refresh characteristic
  147. SCAN_PARAM_REFRESH_CCCD_IDX // Scan Parameter Refresh characteristic client characteristic configuration
  148. };
  149. /*********************************************************************
  150. * LOCAL FUNCTIONS
  151. */
  152. static bStatus_t scanParamWriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  153. uint8 *pValue, uint8 len, uint16 offset );
  154. static uint8 scanParamReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  155. uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen );
  156. /*********************************************************************
  157. * PROFILE CALLBACKS
  158. */
  159. // Service Callbacks
  160. CONST gattServiceCBs_t scanParamCBs =
  161. {
  162. scanParamReadAttrCB, // Read callback function pointer
  163. scanParamWriteAttrCB, // Write callback function pointer
  164. NULL // Authorization callback function pointer
  165. };
  166. /*********************************************************************
  167. * PUBLIC FUNCTIONS
  168. */
  169. /*********************************************************************
  170. * @fn ScanParam_AddService
  171. *
  172. * @brief Initializes the Battery Service by registering
  173. * GATT attributes with the GATT server.
  174. *
  175. * @return Success or Failure
  176. */
  177. bStatus_t ScanParam_AddService( void )
  178. {
  179. uint8 status = SUCCESS;
  180. // Initialize Client Characteristic Configuration attributes
  181. GATTServApp_InitCharCfg( INVALID_CONNHANDLE, scanParamRefreshClientCharCfg );
  182. // Register GATT attribute list and CBs with GATT Server App
  183. status = GATTServApp_RegisterService( scanParamAttrTbl, GATT_NUM_ATTRS( scanParamAttrTbl ),
  184. &scanParamCBs );
  185. return ( status );
  186. }
  187. /*********************************************************************
  188. * @fn ScanParam_Register
  189. *
  190. * @brief Register a callback function with the Battery Service.
  191. *
  192. * @param pfnServiceCB - Callback function.
  193. *
  194. * @return None.
  195. */
  196. extern void ScanParam_Register( scanParamServiceCB_t pfnServiceCB )
  197. {
  198. scanParamServiceCB = pfnServiceCB;
  199. }
  200. /*********************************************************************
  201. * @fn ScanParam_SetParameter
  202. *
  203. * @brief Set a Battery Service parameter.
  204. *
  205. * @param param - Profile parameter ID
  206. * @param len - length of data to right
  207. * @param value - pointer to data to write. This is dependent on
  208. * the parameter ID and WILL be cast to the appropriate
  209. * data type (example: data type of uint16 will be cast to
  210. * uint16 pointer).
  211. *
  212. * @return bStatus_t
  213. */
  214. bStatus_t ScanParam_SetParameter( uint8 param, uint8 len, void *value )
  215. {
  216. bStatus_t ret = SUCCESS;
  217. switch ( param )
  218. {
  219. default:
  220. ret = INVALIDPARAMETER;
  221. break;
  222. }
  223. return ( ret );
  224. }
  225. /*********************************************************************
  226. * @fn ScanParam_GetParameter
  227. *
  228. * @brief Get a Battery Service parameter.
  229. *
  230. * @param param - Profile parameter ID
  231. * @param value - pointer to data to get. This is dependent on
  232. * the parameter ID and WILL be cast to the appropriate
  233. * data type (example: data type of uint16 will be cast to
  234. * uint16 pointer).
  235. *
  236. * @return bStatus_t
  237. */
  238. bStatus_t ScanParam_GetParameter( uint8 param, void *value )
  239. {
  240. bStatus_t ret = SUCCESS;
  241. switch ( param )
  242. {
  243. case SCAN_PARAM_PARAM_INTERVAL:
  244. *((uint16*)value) = BUILD_UINT16(scanIntervalWindow[0],
  245. scanIntervalWindow[1]);
  246. break;
  247. case SCAN_PARAM_PARAM_WINDOW:
  248. *((uint16*)value) = BUILD_UINT16(scanIntervalWindow[2],
  249. scanIntervalWindow[3]);
  250. break;
  251. default:
  252. ret = INVALIDPARAMETER;
  253. break;
  254. }
  255. return ( ret );
  256. }
  257. /*********************************************************************
  258. * @fn ScanParam_RefreshNotify
  259. *
  260. * @brief Notify the peer to refresh the scan parameters.
  261. *
  262. * @param connHandle - connection handle
  263. *
  264. * @return None
  265. */
  266. void ScanParam_RefreshNotify( uint16 connHandle )
  267. {
  268. attHandleValueNoti_t noti;
  269. uint16 value;
  270. value = GATTServApp_ReadCharCfg( connHandle, scanParamRefreshClientCharCfg );
  271. if ( value & GATT_CLIENT_CFG_NOTIFY )
  272. {
  273. // send notification
  274. noti.handle = scanParamAttrTbl[SCAN_PARAM_REFRESH_CCCD_IDX].handle;
  275. noti.len = SCAN_PARAM_REFRESH_LEN;
  276. noti.value[0] = SCAN_PARAM_REFRESH_REQ;
  277. GATT_Notification( connHandle, &noti, FALSE );
  278. }
  279. }
  280. /*********************************************************************
  281. * @fn scanParamReadAttrCB
  282. *
  283. * @brief GATT read callback.
  284. *
  285. * @param connHandle - connection message was received on
  286. * @param pAttr - pointer to attribute
  287. * @param pValue - pointer to data to be read
  288. * @param pLen - length of data to be read
  289. * @param offset - offset of the first octet to be read
  290. * @param maxLen - maximum length of data to be read
  291. *
  292. * @return Success or Failure
  293. */
  294. static uint8 scanParamReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  295. uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen )
  296. {
  297. bStatus_t status = SUCCESS;
  298. return ( status );
  299. }
  300. /*********************************************************************
  301. * @fn scanParamWriteAttrCB
  302. *
  303. * @brief Validate attribute data prior to a write operation
  304. *
  305. * @param connHandle - connection message was received on
  306. * @param pAttr - pointer to attribute
  307. * @param pValue - pointer to data to be written
  308. * @param len - length of data
  309. * @param offset - offset of the first octet to be written
  310. *
  311. * @return Success or Failure
  312. */
  313. static bStatus_t scanParamWriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  314. uint8 *pValue, uint8 len, uint16 offset )
  315. {
  316. bStatus_t status = SUCCESS;
  317. // Make sure it's not a blob operation (no attributes in the profile are long)
  318. if ( offset > 0 )
  319. {
  320. return ( ATT_ERR_ATTR_NOT_LONG );
  321. }
  322. uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
  323. // Only one writeable attribute
  324. if ( uuid == SCAN_INTERVAL_WINDOW_UUID )
  325. {
  326. if ( len == SCAN_INTERVAL_WINDOW_CHAR_LEN )
  327. {
  328. uint16 interval = BUILD_UINT16( pValue[0], pValue[1] );
  329. uint16 window = BUILD_UINT16( pValue[0], pValue[1] );
  330. // Validate values
  331. if ( window <= interval )
  332. {
  333. osal_memcpy( pAttr->pValue, pValue, len );
  334. (*scanParamServiceCB)( SCAN_INTERVAL_WINDOW_SET );
  335. }
  336. else
  337. {
  338. status = ATT_ERR_INVALID_VALUE;
  339. }
  340. }
  341. else
  342. {
  343. status = ATT_ERR_INVALID_VALUE_SIZE;
  344. }
  345. }
  346. else if ( uuid == GATT_CLIENT_CHAR_CFG_UUID )
  347. {
  348. status = GATTServApp_ProcessCCCWriteReq( connHandle, pAttr, pValue, len,
  349. offset, GATT_CLIENT_CFG_NOTIFY );
  350. }
  351. else
  352. {
  353. status = ATT_ERR_ATTR_NOT_FOUND;
  354. }
  355. return ( status );
  356. }
  357. /*********************************************************************
  358. * @fn ScanParam_HandleConnStatusCB
  359. *
  360. * @brief Service link status change handler function.
  361. *
  362. * @param connHandle - connection handle
  363. * @param changeType - type of change
  364. *
  365. * @return none
  366. */
  367. void ScanParam_HandleConnStatusCB( uint16 connHandle, uint8 changeType )
  368. {
  369. // Make sure this is not loopback connection
  370. if ( connHandle != LOOPBACK_CONNHANDLE )
  371. {
  372. // Reset Client Char Config if connection has dropped
  373. if ( ( changeType == LINKDB_STATUS_UPDATE_REMOVED ) ||
  374. ( ( changeType == LINKDB_STATUS_UPDATE_STATEFLAGS ) &&
  375. ( !linkDB_Up( connHandle ) ) ) )
  376. {
  377. GATTServApp_InitCharCfg( connHandle, scanParamRefreshClientCharCfg );
  378. }
  379. }
  380. }