AudioGATTprofile.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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. Filename: AudioGATTprofile.c
  12. Revised:
  13. Revision:
  14. Description: This file contains the Simple GATT profile sample GATT service
  15. profile for use with the BLE sample application.
  16. **************************************************************************************************/
  17. /*********************************************************************
  18. * INCLUDES
  19. */
  20. #include "bcomdef.h"
  21. #include "OSAL.h"
  22. #include "linkdb.h"
  23. #include "att.h"
  24. #include "gatt.h"
  25. #include "gatt_uuid.h"
  26. #include "gattservapp.h"
  27. #include "gapbondmgr.h"
  28. #include "AudioGATTprofile.h"
  29. #include "log.h"
  30. //#include "common.h"
  31. #include "peripheral.h"
  32. #include "hidkbd.h"
  33. /*********************************************************************
  34. * MACROS
  35. */
  36. /*********************************************************************
  37. * CONSTANTS
  38. */
  39. #define SERVAPP_NUM_ATTR_SUPPORTED 8
  40. /*********************************************************************
  41. * TYPEDEFS
  42. */
  43. #define AUDIO_BASE_UUID_128( uuid ) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xB0, \
  44. 0x00, 0x40, 0x51, 0x04, LO_UINT16( uuid ), HI_UINT16( uuid ), 0x00, 0xF0
  45. /*********************************************************************
  46. * GLOBAL VARIABLES
  47. */
  48. uint8 char1Tx_length=0;
  49. uint8 char2Tx_length=0;
  50. // Audio GATT Profile Service UUID: 0xFF01
  51. CONST uint8 AudioProfileServUUID[ATT_UUID_SIZE] =
  52. {
  53. AUDIO_BASE_UUID_128(AUDIOPROFILE_SERV_UUID)
  54. };
  55. // Characteristic 1 UUID: 0xFFF1
  56. CONST uint8 AudioProfilechar1UUID[ATT_UUID_SIZE] =
  57. {
  58. AUDIO_BASE_UUID_128(AUDIOPROFILE_CHAR1_UUID)
  59. };
  60. // Characteristic 2 UUID: 0xFFF2
  61. CONST uint8 AudioProfilechar2UUID[ATT_UUID_SIZE] =
  62. {
  63. AUDIO_BASE_UUID_128(AUDIOPROFILE_CHAR2_UUID)
  64. };
  65. /*********************************************************************
  66. * EXTERNAL VARIABLES
  67. */
  68. /*********************************************************************
  69. * EXTERNAL FUNCTIONS
  70. */
  71. /*********************************************************************
  72. * LOCAL VARIABLES
  73. */
  74. static AudioProfileCBs_t *AudioProfile_AppCBs = NULL;
  75. /*********************************************************************
  76. * Profile Attributes - variables
  77. */
  78. // Simple Profile Service attribute
  79. static CONST gattAttrType_t AudioProfileService = { ATT_UUID_SIZE, AudioProfileServUUID };
  80. // Audio Profile Characteristic 1 Properties
  81. static uint8 AudioProfileChar1Props = GATT_PROP_NOTIFY|GATT_PROP_READ;
  82. // Characteristic 1 Value
  83. uint8 AudioProfileChar1[AUDIOPROFILE_CHAR1_LEN];
  84. // Audio Profile Characteristic 1 Configuration Each client has its own
  85. // instantiation of the Client Characteristic Configuration. Reads of the
  86. // Client Characteristic Configuration only shows the configuration for
  87. // that client and writes only affect the configuration of that client.
  88. static gattCharCfg_t AudioProfileChar1Config[GATT_MAX_NUM_CONN];
  89. // Audio Profile Characteristic 2 User Description
  90. //static uint8 AudioProfileChar1UserDesp[] = "RX CHAR\0";
  91. // Audio Profile Characteristic 1 Properties
  92. static uint8 AudioProfileChar2Props = GATT_PROP_NOTIFY|GATT_PROP_READ;
  93. // Characteristic 1 Value
  94. uint8 AudioProfileChar2[AUDIOPROFILE_CHAR2_LEN];
  95. // Audio Profile Characteristic 1 User Description
  96. //static uint8 AudioProfileChar2UserDesp[] = "TX CHAR\0";
  97. // Audio Profile Characteristic 1 Configuration Each client has its own
  98. // instantiation of the Client Characteristic Configuration. Reads of the
  99. // Client Characteristic Configuration only shows the configuration for
  100. // that client and writes only affect the configuration of that client.
  101. static gattCharCfg_t AudioProfileChar2Config[GATT_MAX_NUM_CONN];
  102. /*********************************************************************
  103. * Profile Attributes - Table
  104. */
  105. static gattAttribute_t AudioProfileAttrTbl[] =
  106. {
  107. // =========== Simple Profile Service
  108. {
  109. { ATT_BT_UUID_SIZE, primaryServiceUUID }, /* type */
  110. GATT_PERMIT_READ, /* permissions */
  111. 0, /* handle */
  112. (uint8 *)&AudioProfileService /* pValue */
  113. },
  114. // ----------------------------------------------------------------------
  115. // Characteristic 1 Declaration,
  116. {
  117. { ATT_BT_UUID_SIZE, characterUUID },
  118. GATT_PERMIT_READ,
  119. 0,
  120. &AudioProfileChar1Props
  121. },
  122. // Characteristic Value 1
  123. {
  124. { ATT_UUID_SIZE, AudioProfilechar1UUID },
  125. GATT_PERMIT_READ | GATT_PERMIT_WRITE,
  126. 0,
  127. (uint8 *)&AudioProfileChar1
  128. },
  129. // Characteristic 1 User Description, this field is optional
  130. // {
  131. // { ATT_BT_UUID_SIZE, charUserDescUUID },
  132. // GATT_PERMIT_READ,
  133. // 0,
  134. // AudioProfileChar1UserDesp
  135. // },
  136. // Characteristic 1 configuration
  137. {
  138. { ATT_BT_UUID_SIZE, clientCharCfgUUID },
  139. GATT_PERMIT_READ | GATT_PERMIT_WRITE,
  140. 0,
  141. (uint8 *)AudioProfileChar1Config
  142. },
  143. // ----------------------------------------------------------------------
  144. // Characteristic 2 Declaration,
  145. {
  146. { ATT_BT_UUID_SIZE, characterUUID },
  147. GATT_PERMIT_READ,
  148. 0,
  149. &AudioProfileChar2Props
  150. },
  151. // Characteristic Value 2
  152. {
  153. { ATT_UUID_SIZE, AudioProfilechar2UUID },
  154. GATT_PERMIT_READ,
  155. 0,
  156. AudioProfileChar2
  157. },
  158. // Characteristic 2 configuration
  159. {
  160. { ATT_BT_UUID_SIZE, clientCharCfgUUID },
  161. GATT_PERMIT_READ | GATT_PERMIT_WRITE,
  162. 0,
  163. (uint8 *)AudioProfileChar2Config
  164. },
  165. // Characteristic 2 User Description
  166. // {
  167. // { ATT_BT_UUID_SIZE, charUserDescUUID },
  168. // GATT_PERMIT_READ,
  169. // 0,
  170. // AudioProfileChar2UserDesp
  171. // },
  172. };
  173. /*********************************************************************
  174. * LOCAL FUNCTIONS
  175. */
  176. static uint8 AudioProfile_ReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  177. uint8 *pValue, uint16 *pLen, uint16 offset, uint8 maxLen );
  178. static bStatus_t AudioProfile_WriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  179. uint8 *pValue, uint16 len, uint16 offset );
  180. static void AudioProfile_HandleConnStatusCB( uint16 connHandle, uint8 changeType );
  181. /*********************************************************************
  182. * PROFILE CALLBACKS
  183. */
  184. // Audio Profile Service Callbacks
  185. CONST gattServiceCBs_t AudioProfileCBs =
  186. {
  187. AudioProfile_ReadAttrCB, // Read callback function pointer
  188. AudioProfile_WriteAttrCB, // Write callback function pointer
  189. NULL // Authorization callback function pointer
  190. };
  191. /*********************************************************************
  192. * PUBLIC FUNCTIONS
  193. */
  194. /*********************************************************************
  195. * @fn AudioProfile_AddService
  196. *
  197. * @brief Initializes the Simple Profile service by registering
  198. * GATT attributes with the GATT server.
  199. *
  200. * @param services - services to add. This is a bit map and can
  201. * contain more than one service.
  202. *
  203. * @return Success or Failure
  204. */
  205. bStatus_t AudioProfile_AddService( uint32 services )
  206. {
  207. uint8 status = SUCCESS;
  208. // Initialize Client Characteristic Configuration attributes
  209. GATTServApp_InitCharCfg( INVALID_CONNHANDLE, AudioProfileChar1Config );
  210. GATTServApp_InitCharCfg( INVALID_CONNHANDLE, AudioProfileChar2Config );
  211. // Register with Link DB to receive link status change callback
  212. VOID linkDB_Register(AudioProfile_HandleConnStatusCB );
  213. if ( services & AUDIOPROFILE_SERVICE )
  214. {
  215. // Register GATT attribute list and CBs with GATT Server App
  216. status = GATTServApp_RegisterService( AudioProfileAttrTbl,
  217. GATT_NUM_ATTRS( AudioProfileAttrTbl ),
  218. &AudioProfileCBs );
  219. }
  220. return ( status );
  221. }
  222. /*********************************************************************
  223. * @fn AudioProfile_RegisterAppCBs
  224. *
  225. * @brief Registers the application callback function. Only call
  226. * this function once.
  227. *
  228. * @param callbacks - pointer to application callbacks.
  229. *
  230. * @return SUCCESS or bleAlreadyInRequestedMode
  231. */
  232. bStatus_t AudioProfile_RegisterAppCBs( AudioProfileCBs_t *appCallbacks )
  233. {
  234. if ( appCallbacks )
  235. {
  236. AudioProfile_AppCBs = appCallbacks;
  237. return ( SUCCESS );
  238. }
  239. else
  240. {
  241. return ( bleAlreadyInRequestedMode );
  242. }
  243. }
  244. /*********************************************************************
  245. * @fn AudioProfile_SetParameter
  246. *
  247. * @brief Set a Simple Profile parameter.
  248. *
  249. * @param param - Profile parameter ID
  250. * @param len - length of data to right
  251. * @param value - pointer to data to write. This is dependent on
  252. * the parameter ID and WILL be cast to the appropriate
  253. * data type (example: data type of uint16 will be cast to
  254. * uint16 pointer).
  255. *
  256. * @return bStatus_t
  257. */
  258. bStatus_t AudioProfile_SetParameter( uint8 param, uint8 len, void *value )
  259. {
  260. bStatus_t ret = SUCCESS;
  261. switch ( param )
  262. {
  263. case AUDIOPROFILE_CHAR1:
  264. if ( len >0)
  265. {
  266. VOID osal_memcpy( AudioProfileChar1, value, len );
  267. char1Tx_length=len;
  268. ret=GATTServApp_ProcessCharCfg( AudioProfileChar1Config, AudioProfileChar1, FALSE,
  269. AudioProfileAttrTbl, GATT_NUM_ATTRS( AudioProfileAttrTbl ),
  270. INVALID_TASK_ID );
  271. if(ret!=SUCCESS)
  272. {
  273. // LOG("Notify_error:%d\n\r",ret);
  274. }
  275. }
  276. else
  277. {
  278. ret = bleInvalidRange;
  279. }
  280. break;
  281. case AUDIOPROFILE_CHAR2:
  282. if ( len >0)
  283. {
  284. VOID osal_memcpy( AudioProfileChar2, value, len );
  285. char2Tx_length=len;
  286. ret=GATTServApp_ProcessCharCfg( AudioProfileChar2Config, AudioProfileChar2, FALSE,
  287. AudioProfileAttrTbl, GATT_NUM_ATTRS( AudioProfileAttrTbl ),
  288. INVALID_TASK_ID );
  289. if(ret!=SUCCESS)
  290. {
  291. // LOG("Notify_error:%d\nr",ret);
  292. }
  293. }
  294. else
  295. {
  296. ret = bleInvalidRange;
  297. }
  298. break;
  299. default:
  300. ret = INVALIDPARAMETER;
  301. break;
  302. }
  303. return ( ret );
  304. }
  305. /*********************************************************************
  306. * @fn AudioProfile_GetParameter
  307. *
  308. * @brief Get a Audio Profile parameter.
  309. *
  310. * @param param - Profile parameter ID
  311. * @param value - pointer to data to put. This is dependent on
  312. * the parameter ID and WILL be cast to the appropriate
  313. * data type (example: data type of uint16 will be cast to
  314. * uint16 pointer).
  315. *
  316. * @return bStatus_t
  317. */
  318. bStatus_t AudioProfile_GetParameter( uint8 param, void *value )
  319. {
  320. bStatus_t ret = SUCCESS;
  321. switch ( param )
  322. {
  323. default:
  324. ret = INVALIDPARAMETER;
  325. break;
  326. }
  327. return ( ret );
  328. }
  329. /*********************************************************************
  330. * @fn AudioProfile_ReadAttrCB
  331. *
  332. * @brief Read an attribute.
  333. *
  334. * @param connHandle - connection message was received on
  335. * @param pAttr - pointer to attribute
  336. * @param pValue - pointer to data to be read
  337. * @param pLen - length of data to be read
  338. * @param offset - offset of the first octet to be read
  339. * @param maxLen - maximum length of data to be read
  340. *
  341. * @return Success or Failure
  342. */
  343. static uint8 AudioProfile_ReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  344. uint8 *pValue, uint16 *pLen, uint16 offset, uint8 maxLen )
  345. {
  346. bStatus_t status = SUCCESS;
  347. // If attribute permissions require authorization to read, return error
  348. if ( gattPermitAuthorRead( pAttr->permissions ) )
  349. {
  350. // Insufficient authorization
  351. return ( ATT_ERR_INSUFFICIENT_AUTHOR );
  352. }
  353. // Make sure it's not a blob operation (no attributes in the profile are long)
  354. if ( offset > 0 )
  355. {
  356. return ( ATT_ERR_ATTR_NOT_LONG );
  357. }
  358. if ( pAttr->type.len == ATT_BT_UUID_SIZE||pAttr->type.len == ATT_UUID_SIZE )
  359. {
  360. AudioProfile_Read(connHandle,pAttr,pValue,pLen,offset,maxLen);
  361. }
  362. return ( status );
  363. }
  364. /*********************************************************************
  365. * @fn AudioProfile_WriteAttrCB
  366. *
  367. * @brief Validate attribute data prior to a write operation
  368. *
  369. * @param connHandle - connection message was received on
  370. * @param pAttr - pointer to attribute
  371. * @param pValue - pointer to data to be written
  372. * @param len - length of data
  373. * @param offset - offset of the first octet to be written
  374. *
  375. * @return Success or Failure
  376. */
  377. static bStatus_t AudioProfile_WriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  378. uint8 *pValue, uint16 len, uint16 offset )
  379. {
  380. bStatus_t status = SUCCESS;
  381. uint8 notifyApp = 0xFF;
  382. // If attribute permissions require authorization to write, return error
  383. if ( gattPermitAuthorWrite( pAttr->permissions ) )
  384. {
  385. // Insufficient authorization
  386. return ( ATT_ERR_INSUFFICIENT_AUTHOR );
  387. }
  388. if ( pAttr->type.len == ATT_BT_UUID_SIZE )
  389. {
  390. // 16-bit UUID
  391. uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
  392. switch ( uuid )
  393. {
  394. case GATT_CLIENT_CHAR_CFG_UUID:
  395. status = GATTServApp_ProcessCCCWriteReq( connHandle, pAttr, pValue, len,
  396. offset, GATT_CLIENT_CFG_NOTIFY );
  397. if ( status == SUCCESS )
  398. {
  399. // uint16 charCfg = BUILD_UINT16( pValue[0], pValue[1] );
  400. if(pAttr->handle==AudioProfileAttrTbl[3].handle)
  401. {
  402. LOG("audio start cmd enable\n\r");
  403. }
  404. else if(pAttr->handle==AudioProfileAttrTbl[6].handle)
  405. {
  406. LOG("audio data transf enable\n\r");
  407. // if(charCfg==0x0001)
  408. //osal_start_timerEx(hidKbdTaskId, HID_KEY_TEST_EVT, 8000);
  409. }
  410. }
  411. break;
  412. default:
  413. // Should never get here! (characteristics 2 and 4 do not have write permissions)
  414. status = ATT_ERR_ATTR_NOT_FOUND;
  415. break;
  416. }
  417. }
  418. else
  419. {
  420. // 128-bit UUID
  421. status = ATT_ERR_INVALID_HANDLE;
  422. }
  423. // If a charactersitic value changed then callback function to notify application of change
  424. if ( (notifyApp != 0xFF ) && AudioProfile_AppCBs && AudioProfile_AppCBs->pfnAudioProfileChange )
  425. {
  426. AudioProfile_AppCBs->pfnAudioProfileChange( notifyApp );
  427. }
  428. return ( status );
  429. }
  430. /*********************************************************************
  431. * @fn AudioProfile_HandleConnStatusCB
  432. *
  433. * @brief Audio Profile link status change handler function.
  434. *
  435. * @param connHandle - connection handle
  436. * @param changeType - type of change
  437. *
  438. * @return none
  439. */
  440. static void AudioProfile_HandleConnStatusCB( uint16 connHandle, uint8 changeType )
  441. {
  442. // Make sure this is not loopback connection
  443. if ( connHandle != LOOPBACK_CONNHANDLE )
  444. {
  445. // Reset Client Char Config if connection has dropped
  446. if ( ( changeType == LINKDB_STATUS_UPDATE_REMOVED ) ||
  447. ( ( changeType == LINKDB_STATUS_UPDATE_STATEFLAGS ) &&
  448. ( !linkDB_Up( connHandle ) ) ) )
  449. {
  450. GATTServApp_InitCharCfg( connHandle, AudioProfileChar1Config );
  451. GATTServApp_InitCharCfg( connHandle, AudioProfileChar2Config );
  452. }
  453. }
  454. }
  455. bStatus_t AudioProfile_Notify( uint8 param, uint8 len, void *value )
  456. {
  457. bStatus_t ret = SUCCESS;
  458. switch ( param )
  459. {
  460. case AUDIOPROFILE_CHAR2:
  461. VOID osal_memcpy( AudioProfileChar2, value, len );
  462. GATTServApp_ProcessCharCfg( AudioProfileChar2Config, AudioProfileChar2, FALSE,
  463. AudioProfileAttrTbl, GATT_NUM_ATTRS( AudioProfileAttrTbl ),
  464. INVALID_TASK_ID );
  465. break;
  466. default:
  467. ret = INVALIDPARAMETER;
  468. break;
  469. }
  470. return ( ret );
  471. }
  472. bStatus_t AudioProfile_Write( uint16 connHandle, gattAttribute_t *pAttr,
  473. uint8 *pValue, uint8 len, uint16 offset )
  474. {
  475. bStatus_t status = SUCCESS;
  476. // 16-bit UUID
  477. uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
  478. switch(uuid){
  479. case AUDIOPROFILE_CHAR1_UUID:
  480. if ( offset != 0 )
  481. {
  482. status = ATT_ERR_ATTR_NOT_LONG;
  483. }
  484. if ( status == SUCCESS ){
  485. uint8 *pCurValue = (uint8 *)pAttr->pValue;
  486. VOID osal_memcpy( pCurValue, pValue, len );
  487. //rxdata_process(pCurValue,len);
  488. }
  489. break;
  490. case GATT_CLIENT_CHAR_CFG_UUID:
  491. status = GATTServApp_ProcessCCCWriteReq( connHandle, pAttr, pValue, len,
  492. offset, GATT_CLIENT_CFG_NOTIFY );
  493. break;
  494. default:
  495. status = ATT_ERR_ATTR_NOT_FOUND;
  496. break;
  497. }
  498. return status;
  499. }
  500. bStatus_t AudioProfile_Read( uint16 connHandle, gattAttribute_t *pAttr,
  501. uint8 *pValue, uint16 *pLen, uint16 offset, uint8 maxLen )
  502. {
  503. bStatus_t status = SUCCESS;
  504. if(pAttr->type.len == ATT_BT_UUID_SIZE)
  505. {
  506. // 16-bit UUID
  507. uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
  508. LOG("16 uuid:%X\n\r",uuid);
  509. switch ( uuid )
  510. {
  511. case AUDIOPROFILE_CHAR1_UUID:
  512. *pLen = char1Tx_length;
  513. VOID osal_memcpy( pValue, pAttr->pValue, *pLen );
  514. break;
  515. case AUDIOPROFILE_CHAR2_UUID:
  516. *pLen=char2Tx_length;
  517. VOID osal_memcpy( pValue, pAttr->pValue, *pLen );
  518. break;
  519. default:
  520. // Should never get here! (characteristics 3 and 4 do not have read permissions)
  521. *pLen = 0;
  522. status = ATT_ERR_ATTR_NOT_FOUND;
  523. LOG("uuid not find\n\r");
  524. break;
  525. }
  526. }
  527. else
  528. {
  529. uint16 uuid = BUILD_UINT16( pAttr->type.uuid[12], pAttr->type.uuid[13]);
  530. //LOG("128 uuid:%X\n\r",uuid);
  531. switch ( uuid )
  532. {
  533. case AUDIOPROFILE_CHAR1_UUID:
  534. *pLen = char1Tx_length;
  535. VOID osal_memcpy( pValue, pAttr->pValue, *pLen );
  536. break;
  537. case AUDIOPROFILE_CHAR2_UUID:
  538. *pLen=char2Tx_length;
  539. VOID osal_memcpy( pValue, pAttr->pValue, *pLen );
  540. break;
  541. default:
  542. // Should never get here! (characteristics 3 and 4 do not have read permissions)
  543. *pLen = 0;
  544. status = ATT_ERR_ATTR_NOT_FOUND;
  545. LOG("uuid not find\n\r");
  546. break;
  547. }
  548. }
  549. return status;
  550. }
  551. /*********************************************************************
  552. *********************************************************************/