simplekeys.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. Filename: simplekey.c
  30. Revised:
  31. Revision:
  32. Description: Simple Keys Profile
  33. **************************************************************************************************/
  34. /*********************************************************************
  35. * INCLUDES
  36. */
  37. #include "bcomdef.h"
  38. #include "OSAL.h"
  39. #include "linkdb.h"
  40. #include "att.h"
  41. #include "gatt.h"
  42. #include "gatt_uuid.h"
  43. #include "gattservapp.h"
  44. #include "gapbondmgr.h"
  45. #include "simplekeys.h"
  46. /*********************************************************************
  47. * MACROS
  48. */
  49. /*********************************************************************
  50. * CONSTANTS
  51. */
  52. #define SERVAPP_NUM_ATTR_SUPPORTED 5
  53. /*********************************************************************
  54. * TYPEDEFS
  55. */
  56. /*********************************************************************
  57. * GLOBAL VARIABLES
  58. */
  59. // SK Service UUID: 0x1800
  60. CONST uint8 skServUUID[ATT_BT_UUID_SIZE] =
  61. {
  62. LO_UINT16(SK_SERV_UUID), HI_UINT16(SK_SERV_UUID)
  63. };
  64. // Key Pressed UUID: 0x1801
  65. CONST uint8 keyPressedUUID[ATT_BT_UUID_SIZE] =
  66. {
  67. LO_UINT16(SK_KEYPRESSED_UUID), HI_UINT16(SK_KEYPRESSED_UUID)
  68. };
  69. /*********************************************************************
  70. * EXTERNAL VARIABLES
  71. */
  72. /*********************************************************************
  73. * EXTERNAL FUNCTIONS
  74. */
  75. /*********************************************************************
  76. * LOCAL VARIABLES
  77. */
  78. /*********************************************************************
  79. * Profile Attributes - variables
  80. */
  81. // SK Service attribute
  82. static CONST gattAttrType_t skService = { ATT_BT_UUID_SIZE, skServUUID };
  83. // Keys Pressed Characteristic Properties
  84. static uint8 skCharProps = GATT_PROP_NOTIFY;
  85. // Key Pressed State Characteristic
  86. static uint8 skKeyPressed = 0;
  87. // Key Pressed Characteristic Configs
  88. static gattCharCfg_t skConfig[GATT_MAX_NUM_CONN];
  89. // Key Pressed Characteristic User Description
  90. static uint8 skCharUserDesp[16] = "Key Press State\0";
  91. /*********************************************************************
  92. * Profile Attributes - Table
  93. */
  94. static gattAttribute_t simplekeysAttrTbl[SERVAPP_NUM_ATTR_SUPPORTED] =
  95. {
  96. // Simple Keys Service
  97. {
  98. { ATT_BT_UUID_SIZE, primaryServiceUUID }, /* type */
  99. GATT_PERMIT_READ, /* permissions */
  100. 0, /* handle */
  101. (uint8 *)&skService /* pValue */
  102. },
  103. // Characteristic Declaration for Keys
  104. {
  105. { ATT_BT_UUID_SIZE, characterUUID },
  106. GATT_PERMIT_READ,
  107. 0,
  108. &skCharProps
  109. },
  110. // Characteristic Value- Key Pressed
  111. {
  112. { ATT_BT_UUID_SIZE, keyPressedUUID },
  113. 0,
  114. 0,
  115. &skKeyPressed
  116. },
  117. // Characteristic configuration
  118. {
  119. { ATT_BT_UUID_SIZE, clientCharCfgUUID },
  120. GATT_PERMIT_READ | GATT_PERMIT_WRITE,
  121. 0,
  122. (uint8 *)skConfig
  123. },
  124. // Characteristic User Description
  125. {
  126. { ATT_BT_UUID_SIZE, charUserDescUUID },
  127. GATT_PERMIT_READ,
  128. 0,
  129. skCharUserDesp
  130. },
  131. };
  132. /*********************************************************************
  133. * LOCAL FUNCTIONS
  134. */
  135. static uint8 sk_ReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  136. uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen );
  137. static bStatus_t sk_WriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  138. uint8 *pValue, uint8 len, uint16 offset );
  139. static void sk_HandleConnStatusCB( uint16 connHandle, uint8 changeType );
  140. /*********************************************************************
  141. * PROFILE CALLBACKS
  142. */
  143. // SK Service Callbacks
  144. CONST gattServiceCBs_t skCBs =
  145. {
  146. sk_ReadAttrCB, // Read callback function pointer
  147. sk_WriteAttrCB, // Write callback function pointer
  148. NULL // Authorization callback function pointer
  149. };
  150. /*********************************************************************
  151. * PUBLIC FUNCTIONS
  152. */
  153. /*********************************************************************
  154. * @fn SK_AddService
  155. *
  156. * @brief Initializes the Simple Key service by registering
  157. * GATT attributes with the GATT server.
  158. *
  159. * @param services - services to add. This is a bit map and can
  160. * contain more than one service.
  161. *
  162. * @return Success or Failure
  163. */
  164. bStatus_t SK_AddService( uint32 services )
  165. {
  166. uint8 status = SUCCESS;
  167. // Initialize Client Characteristic Configuration attributes
  168. GATTServApp_InitCharCfg( INVALID_CONNHANDLE, skConfig );
  169. // Register with Link DB to receive link status change callback
  170. VOID linkDB_Register( sk_HandleConnStatusCB );
  171. if ( services & SK_SERVICE )
  172. {
  173. // Register GATT attribute list and CBs with GATT Server App
  174. status = GATTServApp_RegisterService( simplekeysAttrTbl,
  175. GATT_NUM_ATTRS( simplekeysAttrTbl ),
  176. &skCBs );
  177. }
  178. return ( status );
  179. }
  180. /*********************************************************************
  181. * @fn SK_SetParameter
  182. *
  183. * @brief Set a Simple Key Profile parameter.
  184. *
  185. * @param param - Profile parameter ID
  186. * @param len - length of data to right
  187. * @param pValue - pointer to data to write. This is dependent on
  188. * the parameter ID and WILL be cast to the appropriate
  189. * data type (example: data type of uint16 will be cast to
  190. * uint16 pointer).
  191. *
  192. * @return bStatus_t
  193. */
  194. bStatus_t SK_SetParameter( uint8 param, uint8 len, void *pValue )
  195. {
  196. bStatus_t ret = SUCCESS;
  197. switch ( param )
  198. {
  199. case SK_KEY_ATTR:
  200. if ( len == sizeof ( uint8 ) )
  201. {
  202. skKeyPressed = *((uint8*)pValue);
  203. // See if Notification/Indication has been enabled
  204. GATTServApp_ProcessCharCfg( skConfig, &skKeyPressed, FALSE,
  205. simplekeysAttrTbl, GATT_NUM_ATTRS( simplekeysAttrTbl ),
  206. INVALID_TASK_ID );
  207. }
  208. else
  209. {
  210. ret = bleInvalidRange;
  211. }
  212. break;
  213. default:
  214. ret = INVALIDPARAMETER;
  215. break;
  216. }
  217. return ( ret );
  218. }
  219. /*********************************************************************
  220. * @fn SK_GetParameter
  221. *
  222. * @brief Get a Simple Key Profile parameter.
  223. *
  224. * @param param - Profile parameter ID
  225. * @param pValue - pointer to data to put. This is dependent on
  226. * the parameter ID and WILL be cast to the appropriate
  227. * data type (example: data type of uint16 will be cast to
  228. * uint16 pointer).
  229. *
  230. * @return bStatus_t
  231. */
  232. bStatus_t SK_GetParameter( uint8 param, void *pValue )
  233. {
  234. bStatus_t ret = SUCCESS;
  235. switch ( param )
  236. {
  237. case SK_KEY_ATTR:
  238. *((uint8*)pValue) = skKeyPressed;
  239. break;
  240. default:
  241. ret = INVALIDPARAMETER;
  242. break;
  243. }
  244. return ( ret );
  245. }
  246. /*********************************************************************
  247. * @fn sk_ReadAttrCB
  248. *
  249. * @brief Read an attribute.
  250. *
  251. * @param connHandle - connection message was received on
  252. * @param pAttr - pointer to attribute
  253. * @param pValue - pointer to data to be read
  254. * @param pLen - length of data to be read
  255. * @param offset - offset of the first octet to be read
  256. * @param maxLen - maximum length of data to be read
  257. *
  258. * @return Success or Failure
  259. */
  260. static uint8 sk_ReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  261. uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen )
  262. {
  263. bStatus_t status = SUCCESS;
  264. // Make sure it's not a blob operation (no attributes in the profile are long
  265. if ( offset > 0 )
  266. {
  267. return ( ATT_ERR_ATTR_NOT_LONG );
  268. }
  269. if ( pAttr->type.len == ATT_BT_UUID_SIZE )
  270. {
  271. // 16-bit UUID
  272. uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
  273. switch ( uuid )
  274. {
  275. // No need for "GATT_SERVICE_UUID" or "GATT_CLIENT_CHAR_CFG_UUID" cases;
  276. // gattserverapp handles this type for reads
  277. // simple keys characteristic does not have read permissions, but because it
  278. // can be sent as a notification, it must be included here
  279. case SK_KEYPRESSED_UUID:
  280. *pLen = 1;
  281. pValue[0] = *pAttr->pValue;
  282. break;
  283. default:
  284. // Should never get here!
  285. *pLen = 0;
  286. status = ATT_ERR_ATTR_NOT_FOUND;
  287. break;
  288. }
  289. }
  290. else
  291. {
  292. // 128-bit UUID
  293. *pLen = 0;
  294. status = ATT_ERR_INVALID_HANDLE;
  295. }
  296. return ( status );
  297. }
  298. /*********************************************************************
  299. * @fn sk_WriteAttrCB
  300. *
  301. * @brief Validate attribute data prior to a write operation
  302. *
  303. * @param connHandle - connection message was received on
  304. * @param pAttr - pointer to attribute
  305. * @param pValue - pointer to data to be written
  306. * @param len - length of data
  307. * @param offset - offset of the first octet to be written
  308. *
  309. * @return Success or Failure
  310. */
  311. static bStatus_t sk_WriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  312. uint8 *pValue, uint8 len, uint16 offset )
  313. {
  314. bStatus_t status = SUCCESS;
  315. if ( pAttr->type.len == ATT_BT_UUID_SIZE )
  316. {
  317. // 16-bit UUID
  318. uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
  319. switch ( uuid )
  320. {
  321. case GATT_CLIENT_CHAR_CFG_UUID:
  322. status = GATTServApp_ProcessCCCWriteReq( connHandle, pAttr, pValue, len,
  323. offset, GATT_CLIENT_CFG_NOTIFY );
  324. break;
  325. default:
  326. // Should never get here!
  327. status = ATT_ERR_ATTR_NOT_FOUND;
  328. break;
  329. }
  330. }
  331. else
  332. {
  333. // 128-bit UUID
  334. status = ATT_ERR_INVALID_HANDLE;
  335. }
  336. return ( status );
  337. }
  338. /*********************************************************************
  339. * @fn sk_HandleConnStatusCB
  340. *
  341. * @brief Simple Keys Profile link status change handler function.
  342. *
  343. * @param connHandle - connection handle
  344. * @param changeType - type of change
  345. *
  346. * @return none
  347. */
  348. static void sk_HandleConnStatusCB( uint16 connHandle, uint8 changeType )
  349. {
  350. // Make sure this is not loopback connection
  351. if ( connHandle != LOOPBACK_CONNHANDLE )
  352. {
  353. // Reset Client Char Config if connection has dropped
  354. if ( ( changeType == LINKDB_STATUS_UPDATE_REMOVED ) ||
  355. ( ( changeType == LINKDB_STATUS_UPDATE_STATEFLAGS ) &&
  356. ( !linkDB_Up( connHandle ) ) ) )
  357. {
  358. GATTServApp_InitCharCfg( connHandle, skConfig );
  359. }
  360. }
  361. }
  362. /*********************************************************************
  363. *********************************************************************/