simpleGATTprofile_ota.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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: simpleGATTprofile_ota.c
  30. Revised:
  31. Revision:
  32. Description: This file contains the Simple GATT profile sample GATT service
  33. profile for use with the BLE sample application.
  34. **************************************************************************************************/
  35. /*********************************************************************
  36. * INCLUDES
  37. */
  38. #include "bcomdef.h"
  39. #include "OSAL.h"
  40. #include "linkdb.h"
  41. #include "att.h"
  42. #include "gatt.h"
  43. #include "gatt_uuid.h"
  44. #include "gattservapp.h"
  45. #include "gapbondmgr.h"
  46. #include "simpleGATTprofile_ota.h"
  47. /*********************************************************************
  48. * MACROS
  49. */
  50. /*********************************************************************
  51. * CONSTANTS
  52. */
  53. #define SERVAPP_NUM_ATTR_SUPPORTED 21
  54. /*********************************************************************
  55. * TYPEDEFS
  56. */
  57. /*********************************************************************
  58. * GLOBAL VARIABLES
  59. */
  60. // Simple GATT Profile Service UUID: 0xFFF0
  61. CONST uint8 simpleProfileServUUID[ATT_BT_UUID_SIZE] =
  62. {
  63. LO_UINT16(SIMPLEPROFILE_SERV_UUID), HI_UINT16(SIMPLEPROFILE_SERV_UUID)
  64. };
  65. // Characteristic 1 UUID: 0xFFF1
  66. CONST uint8 simpleProfilechar1UUID[ATT_BT_UUID_SIZE] =
  67. {
  68. LO_UINT16(SIMPLEPROFILE_CHAR1_UUID), HI_UINT16(SIMPLEPROFILE_CHAR1_UUID)
  69. };
  70. // Characteristic 2 UUID: 0xFFF2
  71. CONST uint8 simpleProfilechar2UUID[ATT_BT_UUID_SIZE] =
  72. {
  73. LO_UINT16(SIMPLEPROFILE_CHAR2_UUID), HI_UINT16(SIMPLEPROFILE_CHAR2_UUID)
  74. };
  75. // Characteristic 3 UUID: 0xFFF3
  76. CONST uint8 simpleProfilechar3UUID[ATT_BT_UUID_SIZE] =
  77. {
  78. LO_UINT16(SIMPLEPROFILE_CHAR3_UUID), HI_UINT16(SIMPLEPROFILE_CHAR3_UUID)
  79. };
  80. // Characteristic 4 UUID: 0xFFF4
  81. CONST uint8 simpleProfilechar4UUID[ATT_BT_UUID_SIZE] =
  82. {
  83. LO_UINT16(SIMPLEPROFILE_CHAR4_UUID), HI_UINT16(SIMPLEPROFILE_CHAR4_UUID)
  84. };
  85. // Characteristic 5 UUID: 0xFFF5
  86. CONST uint8 simpleProfilechar5UUID[ATT_BT_UUID_SIZE] =
  87. {
  88. LO_UINT16(SIMPLEPROFILE_CHAR5_UUID), HI_UINT16(SIMPLEPROFILE_CHAR5_UUID)
  89. };
  90. // Characteristic 6 UUID: 0xFFF6
  91. CONST uint8 simpleProfilechar6UUID[ATT_BT_UUID_SIZE] =
  92. {
  93. LO_UINT16(SIMPLEPROFILE_CHAR6_UUID), HI_UINT16(SIMPLEPROFILE_CHAR6_UUID)
  94. };
  95. /*********************************************************************
  96. * EXTERNAL VARIABLES
  97. */
  98. /*********************************************************************
  99. * EXTERNAL FUNCTIONS
  100. */
  101. /*********************************************************************
  102. * LOCAL VARIABLES
  103. */
  104. static simpleProfileCBs_t *simpleProfile_AppCBs = NULL;
  105. /*********************************************************************
  106. * Profile Attributes - variables
  107. */
  108. // Simple Profile Service attribute
  109. static CONST gattAttrType_t simpleProfileService = { ATT_BT_UUID_SIZE, simpleProfileServUUID };
  110. // Simple Profile Characteristic 1 Properties
  111. static uint8 simpleProfileChar1Props = GATT_PROP_READ | GATT_PROP_WRITE;
  112. // Characteristic 1 Value
  113. static uint8 simpleProfileChar1[IBEACON_UUID_LEN];// = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
  114. // Simple Profile Characteristic 1 User Description
  115. static uint8 simpleProfileChar1UserDesp[] = "UUID\0";
  116. // Simple Profile Characteristic 2 Properties
  117. static uint8 simpleProfileChar2Props = GATT_PROP_READ | GATT_PROP_WRITE;
  118. // Characteristic 2 Value
  119. static uint16 simpleProfileChar2 = 0;
  120. // Simple Profile Characteristic 2 User Description
  121. static uint8 simpleProfileChar2UserDesp[] = "Major\0";
  122. // Simple Profile Characteristic 3 Properties
  123. static uint8 simpleProfileChar3Props = GATT_PROP_READ | GATT_PROP_WRITE;
  124. // Characteristic 3 Value
  125. static uint16 simpleProfileChar3 = 0;
  126. // Simple Profile Characteristic 3 User Description
  127. static uint8 simpleProfileChar3UserDesp[] = "Minor\0";
  128. // Simple Profile Characteristic 4 Properties
  129. static uint8 simpleProfileChar4Props = GATT_PROP_READ | GATT_PROP_WRITE;
  130. // Characteristic 4 Value
  131. static uint8 simpleProfileChar4 = 0;
  132. // Simple Profile Characteristic 4 Configuration Each client has its own
  133. // instantiation of the Client Characteristic Configuration. Reads of the
  134. // Client Characteristic Configuration only shows the configuration for
  135. // that client and writes only affect the configuration of that client.
  136. static gattCharCfg_t simpleProfileChar4Config[GATT_MAX_NUM_CONN];
  137. // Simple Profile Characteristic 4 User Description
  138. static uint8 simpleProfileChar4UserDesp[] = "Power\0";
  139. // Simple Profile Characteristic 5 Properties
  140. static uint8 simpleProfileChar5Props = GATT_PROP_READ | GATT_PROP_WRITE; // to change to write only, HZF
  141. // Characteristic 5 Value
  142. static uint8 simpleProfileChar5 = 0;
  143. // Simple Profile Characteristic 5 User Description
  144. static uint8 simpleProfileChar5UserDesp[] = "Reset\0";
  145. // Simple Profile Characteristic 6 Properties
  146. static uint8 simpleProfileChar6Props = GATT_PROP_READ | GATT_PROP_NOTIFY;
  147. // Characteristic 6 Value
  148. static uint8 simpleProfileChar6[20];
  149. // Simple Profile Characteristic 6 User Description
  150. static uint8 simpleProfileChar6UserDesp[] = "NOTIFY\0";
  151. // Simple Profile Characteristic 1 Configuration Each client has its own
  152. // instantiation of the Client Characteristic Configuration. Reads of the
  153. // Client Characteristic Configuration only shows the configuration for
  154. // that client and writes only affect the configuration of that client.
  155. static gattCharCfg_t simpleProfileChar6Config[GATT_MAX_NUM_CONN];
  156. /*********************************************************************
  157. * Profile Attributes - Table
  158. */
  159. static gattAttribute_t simpleProfileAttrTbl[SERVAPP_NUM_ATTR_SUPPORTED] =
  160. {
  161. // Simple Profile Service
  162. {
  163. { ATT_BT_UUID_SIZE, primaryServiceUUID }, /* type */
  164. GATT_PERMIT_READ, /* permissions */
  165. 0, /* handle */
  166. (uint8 *)&simpleProfileService /* pValue */
  167. },
  168. // Characteristic 1 Declaration
  169. {
  170. { ATT_BT_UUID_SIZE, characterUUID },
  171. GATT_PERMIT_READ,
  172. 0,
  173. &simpleProfileChar1Props
  174. },
  175. // Characteristic Value 1
  176. {
  177. { ATT_BT_UUID_SIZE, simpleProfilechar1UUID },
  178. GATT_PERMIT_READ | GATT_PERMIT_WRITE,
  179. 0,
  180. &simpleProfileChar1[0]
  181. },
  182. // Characteristic 1 User Description
  183. {
  184. { ATT_BT_UUID_SIZE, charUserDescUUID },
  185. GATT_PERMIT_READ,
  186. 0,
  187. simpleProfileChar1UserDesp
  188. },
  189. // Characteristic 2 Declaration
  190. {
  191. { ATT_BT_UUID_SIZE, characterUUID },
  192. GATT_PERMIT_READ,
  193. 0,
  194. &simpleProfileChar2Props
  195. },
  196. // Characteristic Value 2
  197. {
  198. { ATT_BT_UUID_SIZE, simpleProfilechar2UUID },
  199. GATT_PERMIT_READ | GATT_PERMIT_WRITE,
  200. 0,
  201. (uint8 *)&simpleProfileChar2
  202. },
  203. // Characteristic 2 User Description
  204. {
  205. { ATT_BT_UUID_SIZE, charUserDescUUID },
  206. GATT_PERMIT_READ,
  207. 0,
  208. simpleProfileChar2UserDesp
  209. },
  210. // Characteristic 3 Declaration
  211. {
  212. { ATT_BT_UUID_SIZE, characterUUID },
  213. GATT_PERMIT_READ,
  214. 0,
  215. &simpleProfileChar3Props
  216. },
  217. // Characteristic Value 3
  218. {
  219. { ATT_BT_UUID_SIZE, simpleProfilechar3UUID },
  220. GATT_PERMIT_READ | GATT_PERMIT_WRITE,
  221. 0,
  222. (uint8 *)&simpleProfileChar3
  223. },
  224. // Characteristic 3 User Description
  225. {
  226. { ATT_BT_UUID_SIZE, charUserDescUUID },
  227. GATT_PERMIT_READ,
  228. 0,
  229. simpleProfileChar3UserDesp
  230. },
  231. // Characteristic 4 Declaration
  232. {
  233. { ATT_BT_UUID_SIZE, characterUUID },
  234. GATT_PERMIT_READ,
  235. 0,
  236. &simpleProfileChar4Props
  237. },
  238. // Characteristic Value 4
  239. {
  240. { ATT_BT_UUID_SIZE, simpleProfilechar4UUID },
  241. GATT_PERMIT_READ | GATT_PERMIT_WRITE,
  242. 0,
  243. (uint8 *)&simpleProfileChar4
  244. },
  245. // Characteristic 4 User Description
  246. {
  247. { ATT_BT_UUID_SIZE, charUserDescUUID },
  248. GATT_PERMIT_READ,
  249. 0,
  250. simpleProfileChar4UserDesp
  251. },
  252. // Characteristic 5 Declaration
  253. {
  254. { ATT_BT_UUID_SIZE, characterUUID },
  255. GATT_PERMIT_READ,
  256. 0,
  257. &simpleProfileChar5Props
  258. },
  259. // Characteristic Value 5
  260. {
  261. { ATT_BT_UUID_SIZE, simpleProfilechar5UUID },
  262. GATT_PERMIT_READ | GATT_PERMIT_WRITE,
  263. 0,
  264. &simpleProfileChar5
  265. },
  266. // Characteristic 5 User Description
  267. {
  268. { ATT_BT_UUID_SIZE, charUserDescUUID },
  269. GATT_PERMIT_READ,
  270. 0,
  271. simpleProfileChar5UserDesp
  272. },
  273. // ----------------------------------------------------------------------
  274. // Characteristic 6 Declaration, NOTify
  275. {
  276. { ATT_BT_UUID_SIZE, characterUUID },
  277. GATT_PERMIT_READ,
  278. 0,
  279. &simpleProfileChar6Props
  280. },
  281. // Characteristic Value 6
  282. {
  283. { ATT_BT_UUID_SIZE, simpleProfilechar6UUID },
  284. GATT_PERMIT_READ,
  285. 0,
  286. (uint8 *)&simpleProfileChar6
  287. },
  288. // Characteristic 6 configuration
  289. {
  290. { ATT_BT_UUID_SIZE, clientCharCfgUUID },
  291. GATT_PERMIT_READ | GATT_PERMIT_WRITE,
  292. 0,
  293. (uint8 *)simpleProfileChar6Config
  294. },
  295. // Characteristic 6 User Description
  296. {
  297. { ATT_BT_UUID_SIZE, charUserDescUUID },
  298. GATT_PERMIT_READ,
  299. 0,
  300. simpleProfileChar6UserDesp
  301. },
  302. };
  303. /*********************************************************************
  304. * LOCAL FUNCTIONS
  305. */
  306. static uint8 simpleProfile_ReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  307. uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen );
  308. static bStatus_t simpleProfile_WriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  309. uint8 *pValue, uint8 len, uint16 offset );
  310. static void simpleProfile_HandleConnStatusCB( uint16 connHandle, uint8 changeType );
  311. /*********************************************************************
  312. * PROFILE CALLBACKS
  313. */
  314. // Simple Profile Service Callbacks
  315. CONST gattServiceCBs_t simpleProfileCBs =
  316. {
  317. simpleProfile_ReadAttrCB, // Read callback function pointer
  318. simpleProfile_WriteAttrCB, // Write callback function pointer
  319. NULL // Authorization callback function pointer
  320. };
  321. /*********************************************************************
  322. * PUBLIC FUNCTIONS
  323. */
  324. /*********************************************************************
  325. * @fn SimpleProfile_AddService
  326. *
  327. * @brief Initializes the Simple Profile service by registering
  328. * GATT attributes with the GATT server.
  329. *
  330. * @param services - services to add. This is a bit map and can
  331. * contain more than one service.
  332. *
  333. * @return Success or Failure
  334. */
  335. bStatus_t SimpleProfile_AddService( uint32 services )
  336. {
  337. uint8 status = SUCCESS;
  338. // Initialize Client Characteristic Configuration attributes
  339. GATTServApp_InitCharCfg( INVALID_CONNHANDLE, simpleProfileChar6Config );
  340. // Register with Link DB to receive link status change callback
  341. VOID linkDB_Register( simpleProfile_HandleConnStatusCB );
  342. if ( services & SIMPLEPROFILE_SERVICE )
  343. {
  344. // Register GATT attribute list and CBs with GATT Server App
  345. status = GATTServApp_RegisterService( simpleProfileAttrTbl,
  346. GATT_NUM_ATTRS( simpleProfileAttrTbl ),
  347. &simpleProfileCBs );
  348. }
  349. return ( status );
  350. }
  351. /*********************************************************************
  352. * @fn SimpleProfile_RegisterAppCBs
  353. *
  354. * @brief Registers the application callback function. Only call
  355. * this function once.
  356. *
  357. * @param callbacks - pointer to application callbacks.
  358. *
  359. * @return SUCCESS or bleAlreadyInRequestedMode
  360. */
  361. bStatus_t SimpleProfile_RegisterAppCBs( simpleProfileCBs_t *appCallbacks )
  362. {
  363. if ( appCallbacks )
  364. {
  365. simpleProfile_AppCBs = appCallbacks;
  366. return ( SUCCESS );
  367. }
  368. else
  369. {
  370. return ( bleAlreadyInRequestedMode );
  371. }
  372. }
  373. /*********************************************************************
  374. * @fn SimpleProfile_SetParameter
  375. *
  376. * @brief Set a Simple Profile parameter.
  377. *
  378. * @param param - Profile parameter ID
  379. * @param len - length of data to right
  380. * @param value - pointer to data to write. This is dependent on
  381. * the parameter ID and WILL be cast to the appropriate
  382. * data type (example: data type of uint16 will be cast to
  383. * uint16 pointer).
  384. *
  385. * @return bStatus_t
  386. */
  387. bStatus_t SimpleProfile_SetParameter( uint8 param, uint8 len, void *value )
  388. {
  389. bStatus_t ret = SUCCESS;
  390. switch ( param )
  391. {
  392. case SIMPLEPROFILE_CHAR1:
  393. if ( len <= IBEACON_UUID_LEN )
  394. {
  395. osal_memcpy(simpleProfileChar1, value, len);
  396. }
  397. else
  398. {
  399. ret = bleInvalidRange;
  400. }
  401. break;
  402. case SIMPLEPROFILE_CHAR2:
  403. if ( len == sizeof ( uint16 ) )
  404. {
  405. simpleProfileChar2 = (*(uint8 *)value) << 8 | *((uint8 *)value + 1);
  406. }
  407. else
  408. {
  409. ret = bleInvalidRange;
  410. }
  411. break;
  412. case SIMPLEPROFILE_CHAR3:
  413. if ( len == sizeof ( uint16 ) )
  414. {
  415. simpleProfileChar3 = (*(uint8 *)value) << 8 | *((uint8 *)value + 1);
  416. }
  417. else
  418. {
  419. ret = bleInvalidRange;
  420. }
  421. break;
  422. case SIMPLEPROFILE_CHAR4:
  423. if ( len == sizeof ( uint8 ) )
  424. {
  425. simpleProfileChar4 = *((uint8*)value);
  426. // See if Notification has been enabled
  427. //GATTServApp_ProcessCharCfg( simpleProfileChar4Config, &simpleProfileChar4, FALSE,
  428. // simpleProfileAttrTbl, GATT_NUM_ATTRS( simpleProfileAttrTbl ),
  429. // INVALID_TASK_ID );
  430. }
  431. else
  432. {
  433. ret = bleInvalidRange;
  434. }
  435. break;
  436. case SIMPLEPROFILE_CHAR5:
  437. if ( len == sizeof ( uint8 ) )
  438. {
  439. simpleProfileChar5 = *((uint8*)value);
  440. }
  441. else
  442. {
  443. ret = bleInvalidRange;
  444. }
  445. break;
  446. default:
  447. ret = INVALIDPARAMETER;
  448. break;
  449. }
  450. return ( ret );
  451. }
  452. /*********************************************************************
  453. * @fn SimpleProfile_GetParameter
  454. *
  455. * @brief Get a Simple Profile parameter.
  456. *
  457. * @param param - Profile parameter ID
  458. * @param value - pointer to data to put. This is dependent on
  459. * the parameter ID and WILL be cast to the appropriate
  460. * data type (example: data type of uint16 will be cast to
  461. * uint16 pointer).
  462. *
  463. * @return bStatus_t
  464. */
  465. bStatus_t SimpleProfile_GetParameter( uint8 param, void *value )
  466. {
  467. bStatus_t ret = SUCCESS;
  468. switch ( param )
  469. {
  470. case SIMPLEPROFILE_CHAR1:
  471. VOID osal_memcpy( value, simpleProfileChar1, IBEACON_UUID_LEN );
  472. break;
  473. case SIMPLEPROFILE_CHAR2:
  474. *((uint16*)value) = simpleProfileChar2;
  475. break;
  476. case SIMPLEPROFILE_CHAR3:
  477. *((uint16*)value) = simpleProfileChar3;
  478. break;
  479. case SIMPLEPROFILE_CHAR4:
  480. *((uint8*)value) = simpleProfileChar4;
  481. //*((uint16*)value) = simpleProfileChar4;
  482. break;
  483. case SIMPLEPROFILE_CHAR5:
  484. *((uint8*)value) = simpleProfileChar5;
  485. break;
  486. default:
  487. ret = INVALIDPARAMETER;
  488. break;
  489. }
  490. return ( ret );
  491. }
  492. /*********************************************************************
  493. * @fn simpleProfile_ReadAttrCB
  494. *
  495. * @brief Read an attribute.
  496. *
  497. * @param connHandle - connection message was received on
  498. * @param pAttr - pointer to attribute
  499. * @param pValue - pointer to data to be read
  500. * @param pLen - length of data to be read
  501. * @param offset - offset of the first octet to be read
  502. * @param maxLen - maximum length of data to be read
  503. *
  504. * @return Success or Failure
  505. */
  506. static uint8 simpleProfile_ReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  507. uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen )
  508. {
  509. bStatus_t status = SUCCESS;
  510. // If attribute permissions require authorization to read, return error
  511. if ( gattPermitAuthorRead( pAttr->permissions ) )
  512. {
  513. // Insufficient authorization
  514. return ( ATT_ERR_INSUFFICIENT_AUTHOR );
  515. }
  516. // Make sure it's not a blob operation (no attributes in the profile are long)
  517. if ( offset > 0 )
  518. {
  519. return ( ATT_ERR_ATTR_NOT_LONG );
  520. }
  521. if ( pAttr->type.len == ATT_BT_UUID_SIZE )
  522. {
  523. // 16-bit UUID
  524. uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
  525. switch ( uuid )
  526. {
  527. // No need for "GATT_SERVICE_UUID" or "GATT_CLIENT_CHAR_CFG_UUID" cases;
  528. // gattserverapp handles those reads
  529. case SIMPLEPROFILE_CHAR1_UUID:
  530. *pLen = IBEACON_UUID_LEN;
  531. VOID osal_memcpy( pValue, pAttr->pValue, IBEACON_UUID_LEN );
  532. break;
  533. case SIMPLEPROFILE_CHAR2_UUID:
  534. case SIMPLEPROFILE_CHAR3_UUID:
  535. //case SIMPLEPROFILE_CHAR4_UUID:
  536. *pLen = 2;
  537. VOID osal_memcpy( pValue, pAttr->pValue, *pLen );
  538. break;
  539. case SIMPLEPROFILE_CHAR4_UUID:
  540. case SIMPLEPROFILE_CHAR5_UUID:
  541. *pLen = 1;
  542. pValue[0] = *pAttr->pValue;
  543. break;
  544. case SIMPLEPROFILE_CHAR6_UUID:
  545. *pLen = sizeof(simpleProfileChar6);
  546. VOID osal_memcpy( pValue, pAttr->pValue, *pLen );
  547. break;
  548. default:
  549. // Should never get here! (characteristics 3 and 4 do not have read permissions)
  550. *pLen = 0;
  551. status = ATT_ERR_ATTR_NOT_FOUND;
  552. break;
  553. }
  554. }
  555. else
  556. {
  557. // 128-bit UUID
  558. *pLen = 0;
  559. status = ATT_ERR_INVALID_HANDLE;
  560. }
  561. return ( status );
  562. }
  563. /*********************************************************************
  564. * @fn simpleProfile_WriteAttrCB
  565. *
  566. * @brief Validate attribute data prior to a write operation
  567. *
  568. * @param connHandle - connection message was received on
  569. * @param pAttr - pointer to attribute
  570. * @param pValue - pointer to data to be written
  571. * @param len - length of data
  572. * @param offset - offset of the first octet to be written
  573. *
  574. * @return Success or Failure
  575. */
  576. // TODO: test this function
  577. static bStatus_t simpleProfile_WriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  578. uint8 *pValue, uint8 len, uint16 offset )
  579. {
  580. bStatus_t status = SUCCESS;
  581. uint8 notifyApp = 0xFF;
  582. // If attribute permissions require authorization to write, return error
  583. if ( gattPermitAuthorWrite( pAttr->permissions ) )
  584. {
  585. // Insufficient authorization
  586. return ( ATT_ERR_INSUFFICIENT_AUTHOR );
  587. }
  588. if ( pAttr->type.len == ATT_BT_UUID_SIZE )
  589. {
  590. // 16-bit UUID
  591. uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
  592. switch ( uuid )
  593. {
  594. case SIMPLEPROFILE_CHAR1_UUID:
  595. //Validate the value
  596. // Make sure it's not a blob oper
  597. if ( offset == 0 )
  598. {
  599. if ( len != IBEACON_UUID_LEN )
  600. {
  601. status = ATT_ERR_INVALID_VALUE_SIZE;
  602. }
  603. }
  604. else
  605. {
  606. status = ATT_ERR_ATTR_NOT_LONG;
  607. }
  608. //Write the value
  609. if ( status == SUCCESS )
  610. {
  611. uint8 *pCurValue = (uint8 *)pAttr->pValue;
  612. VOID osal_memcpy( pCurValue, pValue, IBEACON_UUID_LEN );
  613. notifyApp = SIMPLEPROFILE_CHAR1;
  614. }
  615. break;
  616. case SIMPLEPROFILE_CHAR2_UUID:
  617. case SIMPLEPROFILE_CHAR3_UUID:
  618. //Validate the value
  619. // Make sure it's not a blob oper
  620. if ( offset == 0 )
  621. {
  622. if ( len != 2 )
  623. {
  624. status = ATT_ERR_INVALID_VALUE_SIZE;
  625. }
  626. }
  627. else
  628. {
  629. status = ATT_ERR_ATTR_NOT_LONG;
  630. }
  631. //Write the value
  632. if ( status == SUCCESS )
  633. {
  634. uint8 *pCurValue = (uint8 *)pAttr->pValue;
  635. *pCurValue = pValue[0];
  636. *(pCurValue + 1 ) = pValue[1];
  637. if( uuid == SIMPLEPROFILE_CHAR2_UUID)
  638. {
  639. notifyApp = SIMPLEPROFILE_CHAR2;
  640. }
  641. else
  642. {
  643. notifyApp = SIMPLEPROFILE_CHAR3;
  644. }
  645. }
  646. break;
  647. case SIMPLEPROFILE_CHAR4_UUID:
  648. case SIMPLEPROFILE_CHAR5_UUID:
  649. //Validate the value
  650. // Make sure it's not a blob oper
  651. if ( offset == 0 )
  652. {
  653. if ( len != 1 )
  654. {
  655. status = ATT_ERR_INVALID_VALUE_SIZE;
  656. }
  657. }
  658. else
  659. {
  660. status = ATT_ERR_ATTR_NOT_LONG;
  661. }
  662. //Write the value
  663. if ( status == SUCCESS )
  664. {
  665. uint8 *pCurValue = (uint8 *)pAttr->pValue;
  666. *pCurValue = pValue[0];
  667. if( uuid == SIMPLEPROFILE_CHAR4_UUID )
  668. {
  669. notifyApp = SIMPLEPROFILE_CHAR4;
  670. }
  671. else
  672. {
  673. notifyApp = SIMPLEPROFILE_CHAR5;
  674. }
  675. }
  676. break;
  677. case GATT_CLIENT_CHAR_CFG_UUID:
  678. status = GATTServApp_ProcessCCCWriteReq( connHandle, pAttr, pValue, len,
  679. offset, GATT_CLIENT_CFG_NOTIFY );
  680. break;
  681. default:
  682. // Should never get here! (characteristics 2 and 4 do not have write permissions)
  683. status = ATT_ERR_ATTR_NOT_FOUND;
  684. break;
  685. }
  686. }
  687. else
  688. {
  689. // 128-bit UUID
  690. status = ATT_ERR_INVALID_HANDLE;
  691. }
  692. // If a charactersitic value changed then callback function to notify application of change
  693. if ( (notifyApp != 0xFF ) && simpleProfile_AppCBs && simpleProfile_AppCBs->pfnSimpleProfileChange )
  694. {
  695. simpleProfile_AppCBs->pfnSimpleProfileChange( notifyApp );
  696. }
  697. return ( status );
  698. }
  699. /*********************************************************************
  700. * @fn simpleProfile_HandleConnStatusCB
  701. *
  702. * @brief Simple Profile link status change handler function.
  703. *
  704. * @param connHandle - connection handle
  705. * @param changeType - type of change
  706. *
  707. * @return none
  708. */
  709. static void simpleProfile_HandleConnStatusCB( uint16 connHandle, uint8 changeType )
  710. {
  711. // Make sure this is not loopback connection
  712. if ( connHandle != LOOPBACK_CONNHANDLE )
  713. {
  714. // Reset Client Char Config if connection has dropped
  715. if ( ( changeType == LINKDB_STATUS_UPDATE_REMOVED ) ||
  716. ( ( changeType == LINKDB_STATUS_UPDATE_STATEFLAGS ) &&
  717. ( !linkDB_Up( connHandle ) ) ) )
  718. {
  719. GATTServApp_InitCharCfg( connHandle, simpleProfileChar4Config );
  720. }
  721. }
  722. }
  723. bStatus_t simpleProfile_Notify( uint8 param, uint8 len, void *value )
  724. {
  725. bStatus_t ret = SUCCESS;
  726. uint16 notfEnable;
  727. switch ( param )
  728. {
  729. case SIMPLEPROFILE_CHAR6:
  730. notfEnable = GATTServApp_ReadCharCfg( 0, simpleProfileChar6Config );
  731. // If notifications enabled
  732. if ( notfEnable & GATT_CLIENT_CFG_NOTIFY )
  733. {
  734. VOID osal_memcpy( simpleProfileChar6, value, len );
  735. ret=GATTServApp_ProcessCharCfg( simpleProfileChar6Config, simpleProfileChar6, FALSE,
  736. simpleProfileAttrTbl, GATT_NUM_ATTRS( simpleProfileAttrTbl ),
  737. INVALID_TASK_ID );
  738. }
  739. else
  740. {
  741. ret = bleNotReady;
  742. }
  743. break;
  744. default:
  745. ret = INVALIDPARAMETER;
  746. break;
  747. }
  748. return ( ret );
  749. }
  750. /*********************************************************************
  751. *********************************************************************/