devinfoservice.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : devinfoservice.c
  3. * Author : WCH
  4. * Version : V1.0
  5. * Date : 2018/12/10
  6. * Description : Device information service
  7. *********************************************************************************
  8. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  9. * Attention: This software (modified or not) and binary are used for
  10. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  11. *******************************************************************************/
  12. /*********************************************************************
  13. * INCLUDES
  14. */
  15. #include <user_config.h>
  16. #include "devinfoservice.h"
  17. /*********************************************************************
  18. * MACROS
  19. */
  20. /*********************************************************************
  21. * CONSTANTS
  22. */
  23. /*********************************************************************
  24. * TYPEDEFS
  25. */
  26. /*********************************************************************
  27. * GLOBAL VARIABLES
  28. */
  29. // Device information service
  30. const uint8_t devInfoServUUID[ATT_BT_UUID_SIZE] = {
  31. LO_UINT16(DEVINFO_SERV_UUID), HI_UINT16(DEVINFO_SERV_UUID)};
  32. // System ID
  33. const uint8_t devInfoSystemIdUUID[ATT_BT_UUID_SIZE] = {
  34. LO_UINT16(SYSTEM_ID_UUID), HI_UINT16(SYSTEM_ID_UUID)};
  35. // Model Number String
  36. const uint8_t devInfoModelNumberUUID[ATT_BT_UUID_SIZE] = {
  37. LO_UINT16(MODEL_NUMBER_UUID), HI_UINT16(MODEL_NUMBER_UUID)};
  38. // Serial Number String
  39. const uint8_t devInfoSerialNumberUUID[ATT_BT_UUID_SIZE] = {
  40. LO_UINT16(SERIAL_NUMBER_UUID), HI_UINT16(SERIAL_NUMBER_UUID)};
  41. // Firmware Revision String
  42. const uint8_t devInfoFirmwareRevUUID[ATT_BT_UUID_SIZE] = {
  43. LO_UINT16(FIRMWARE_REV_UUID), HI_UINT16(FIRMWARE_REV_UUID)};
  44. // Hardware Revision String
  45. const uint8_t devInfoHardwareRevUUID[ATT_BT_UUID_SIZE] = {
  46. LO_UINT16(HARDWARE_REV_UUID), HI_UINT16(HARDWARE_REV_UUID)};
  47. // Software Revision String
  48. const uint8_t devInfoSoftwareRevUUID[ATT_BT_UUID_SIZE] = {
  49. LO_UINT16(SOFTWARE_REV_UUID), HI_UINT16(SOFTWARE_REV_UUID)};
  50. // Manufacturer Name String
  51. const uint8_t devInfoMfrNameUUID[ATT_BT_UUID_SIZE] = {
  52. LO_UINT16(MANUFACTURER_NAME_UUID), HI_UINT16(MANUFACTURER_NAME_UUID)};
  53. // IEEE 11073-20601 Regulatory Certification Data List
  54. const uint8_t devInfo11073CertUUID[ATT_BT_UUID_SIZE] = {
  55. LO_UINT16(IEEE_11073_CERT_DATA_UUID), HI_UINT16(IEEE_11073_CERT_DATA_UUID)};
  56. // PnP ID
  57. const uint8_t devInfoPnpIdUUID[ATT_BT_UUID_SIZE] = {
  58. LO_UINT16(PNP_ID_UUID), HI_UINT16(PNP_ID_UUID)};
  59. /*********************************************************************
  60. * EXTERNAL VARIABLES
  61. */
  62. /*********************************************************************
  63. * EXTERNAL FUNCTIONS
  64. */
  65. /*********************************************************************
  66. * LOCAL VARIABLES
  67. */
  68. /*********************************************************************
  69. * Profile Attributes - variables
  70. */
  71. // Device Information Service attribute
  72. static const gattAttrType_t devInfoService = {ATT_BT_UUID_SIZE, devInfoServUUID};
  73. // System ID characteristic
  74. static uint8_t devInfoSystemIdProps = GATT_PROP_READ;
  75. static uint8_t devInfoSystemId[DEVINFO_SYSTEM_ID_LEN] = {0, 0, 0, 0, 0, 0, 0, 0};
  76. // Model Number String characteristic
  77. static uint8_t devInfoModelNumberProps = GATT_PROP_READ;
  78. static const uint8_t devInfoModelNumber[] = "Model Number";
  79. // Serial Number String characteristic
  80. static uint8_t devInfoSerialNumberProps = GATT_PROP_READ;
  81. static const uint8_t devInfoSerialNumber[] = "Serial Number";
  82. // Firmware Revision String characteristic
  83. static uint8_t devInfoFirmwareRevProps = GATT_PROP_READ;
  84. static const uint8_t devInfoFirmwareRev[] = "Firmware Revision";
  85. // Hardware Revision String characteristic
  86. static uint8_t devInfoHardwareRevProps = GATT_PROP_READ;
  87. static const uint8_t devInfoHardwareRev[] = "Hardware Revision";
  88. // Software Revision String characteristic
  89. static uint8_t devInfoSoftwareRevProps = GATT_PROP_READ;
  90. static const uint8_t devInfoSoftwareRev[] = "Software Revision";
  91. // Manufacturer Name String characteristic
  92. static uint8_t devInfoMfrNameProps = GATT_PROP_READ;
  93. static const uint8_t devInfoMfrName[] = "Manufacturer Name";
  94. // IEEE 11073-20601 Regulatory Certification Data List characteristic
  95. static uint8_t devInfo11073CertProps = GATT_PROP_READ;
  96. static const uint8_t devInfo11073Cert[] = {
  97. DEVINFO_11073_BODY_EXP, // authoritative body type
  98. 0x00, // authoritative body structure type
  99. // authoritative body data follows below:
  100. 'e', 'x', 'p', 'e', 'r', 'i', 'm', 'e', 'n', 't', 'a', 'l'};
  101. // System ID characteristic
  102. static uint8_t devInfoPnpIdProps = GATT_PROP_READ;
  103. static uint8_t devInfoPnpId[DEVINFO_PNP_ID_LEN] = {
  104. 1, // Vendor ID source (1=Bluetooth SIG)
  105. LO_UINT16(0x07D7), HI_UINT16(0x07D7), // Vendor ID (WCH)
  106. LO_UINT16(0x0000), HI_UINT16(0x0000), // Product ID (vendor-specific)
  107. LO_UINT16(0x0110), HI_UINT16(0x0110) // Product version (JJ.M.N)
  108. };
  109. /*********************************************************************
  110. * Profile Attributes - Table
  111. */
  112. static gattAttribute_t devInfoAttrTbl[] = {
  113. // Device Information Service
  114. {
  115. {ATT_BT_UUID_SIZE, primaryServiceUUID}, /* type */
  116. GATT_PERMIT_READ, /* permissions */
  117. 0, /* handle */
  118. (uint8_t *)&devInfoService /* pValue */
  119. },
  120. // System ID Declaration
  121. {
  122. {ATT_BT_UUID_SIZE, characterUUID},
  123. GATT_PERMIT_READ,
  124. 0,
  125. &devInfoSystemIdProps},
  126. // System ID Value
  127. {
  128. {ATT_BT_UUID_SIZE, devInfoSystemIdUUID},
  129. GATT_PERMIT_READ,
  130. 0,
  131. (uint8_t *)devInfoSystemId},
  132. // Model Number String Declaration
  133. {
  134. {ATT_BT_UUID_SIZE, characterUUID},
  135. GATT_PERMIT_READ,
  136. 0,
  137. &devInfoModelNumberProps},
  138. // Model Number Value
  139. {
  140. {ATT_BT_UUID_SIZE, devInfoModelNumberUUID},
  141. GATT_PERMIT_READ,
  142. 0,
  143. (uint8_t *)devInfoModelNumber},
  144. // Serial Number String Declaration
  145. {
  146. {ATT_BT_UUID_SIZE, characterUUID},
  147. GATT_PERMIT_READ,
  148. 0,
  149. &devInfoSerialNumberProps},
  150. // Serial Number Value
  151. {
  152. {ATT_BT_UUID_SIZE, devInfoSerialNumberUUID},
  153. GATT_PERMIT_READ,
  154. 0,
  155. (uint8_t *)devInfoSerialNumber},
  156. // Firmware Revision String Declaration
  157. {
  158. {ATT_BT_UUID_SIZE, characterUUID},
  159. GATT_PERMIT_READ,
  160. 0,
  161. &devInfoFirmwareRevProps},
  162. // Firmware Revision Value
  163. {
  164. {ATT_BT_UUID_SIZE, devInfoFirmwareRevUUID},
  165. GATT_PERMIT_READ,
  166. 0,
  167. (uint8_t *)devInfoFirmwareRev},
  168. // Hardware Revision String Declaration
  169. {
  170. {ATT_BT_UUID_SIZE, characterUUID},
  171. GATT_PERMIT_READ,
  172. 0,
  173. &devInfoHardwareRevProps},
  174. // Hardware Revision Value
  175. {
  176. {ATT_BT_UUID_SIZE, devInfoHardwareRevUUID},
  177. GATT_PERMIT_READ,
  178. 0,
  179. (uint8_t *)devInfoHardwareRev},
  180. // Software Revision String Declaration
  181. {
  182. {ATT_BT_UUID_SIZE, characterUUID},
  183. GATT_PERMIT_READ,
  184. 0,
  185. &devInfoSoftwareRevProps},
  186. // Software Revision Value
  187. {
  188. {ATT_BT_UUID_SIZE, devInfoSoftwareRevUUID},
  189. GATT_PERMIT_READ,
  190. 0,
  191. (uint8_t *)devInfoSoftwareRev},
  192. // Manufacturer Name String Declaration
  193. {
  194. {ATT_BT_UUID_SIZE, characterUUID},
  195. GATT_PERMIT_READ,
  196. 0,
  197. &devInfoMfrNameProps},
  198. // Manufacturer Name Value
  199. {
  200. {ATT_BT_UUID_SIZE, devInfoMfrNameUUID},
  201. GATT_PERMIT_READ,
  202. 0,
  203. (uint8_t *)devInfoMfrName},
  204. // IEEE 11073-20601 Regulatory Certification Data List Declaration
  205. {
  206. {ATT_BT_UUID_SIZE, characterUUID},
  207. GATT_PERMIT_READ,
  208. 0,
  209. &devInfo11073CertProps},
  210. // IEEE 11073-20601 Regulatory Certification Data List Value
  211. {
  212. {ATT_BT_UUID_SIZE, devInfo11073CertUUID},
  213. GATT_PERMIT_READ,
  214. 0,
  215. (uint8_t *)devInfo11073Cert},
  216. // PnP ID Declaration
  217. {
  218. {ATT_BT_UUID_SIZE, characterUUID},
  219. GATT_PERMIT_READ,
  220. 0,
  221. &devInfoPnpIdProps},
  222. // PnP ID Value
  223. {
  224. {ATT_BT_UUID_SIZE, devInfoPnpIdUUID},
  225. GATT_PERMIT_READ,
  226. 0,
  227. (uint8_t *)devInfoPnpId}
  228. };
  229. /*********************************************************************
  230. * LOCAL FUNCTIONS
  231. */
  232. static bStatus_t devInfo_ReadAttrCB(uint16_t connHandle, gattAttribute_t *pAttr,
  233. uint8_t *pValue, uint16_t *pLen, uint16_t offset, uint16_t maxLen, uint8_t method);
  234. /*********************************************************************
  235. * PROFILE CALLBACKS
  236. */
  237. // Device Info Service Callbacks
  238. gattServiceCBs_t devInfoCBs = {
  239. devInfo_ReadAttrCB, // Read callback function pointer
  240. NULL, // Write callback function pointer
  241. NULL // Authorization callback function pointer
  242. };
  243. /*********************************************************************
  244. * NETWORK LAYER CALLBACKS
  245. */
  246. /*********************************************************************
  247. * PUBLIC FUNCTIONS
  248. */
  249. /*********************************************************************
  250. * @fn DevInfo_AddService
  251. *
  252. * @brief Initializes the Device Information service by registering
  253. * GATT attributes with the GATT server.
  254. *
  255. * @return Success or Failure
  256. */
  257. bStatus_t DevInfo_AddService(void)
  258. {
  259. // Register GATT attribute list and CBs with GATT Server App
  260. return GATTServApp_RegisterService(devInfoAttrTbl,
  261. GATT_NUM_ATTRS(devInfoAttrTbl),
  262. GATT_MAX_ENCRYPT_KEY_SIZE,
  263. &devInfoCBs);
  264. }
  265. /*********************************************************************
  266. * @fn DevInfo_SetParameter
  267. *
  268. * @brief Set a Device Information parameter.
  269. *
  270. * @param param - Profile parameter ID
  271. * @param len - length of data to write
  272. * @param value - pointer to data to write. This is dependent on
  273. * the parameter ID and WILL be cast to the appropriate
  274. * data type (example: data type of uint16_t will be cast to
  275. * uint16_t pointer).
  276. *
  277. * @return bStatus_t
  278. */
  279. bStatus_t DevInfo_SetParameter(uint8_t param, uint16_t len, void *value)
  280. {
  281. bStatus_t ret = SUCCESS;
  282. switch(param)
  283. {
  284. case DEVINFO_SYSTEM_ID:
  285. tmos_memcpy(devInfoSystemId, value, len);
  286. break;
  287. default:
  288. ret = INVALIDPARAMETER;
  289. break;
  290. }
  291. return (ret);
  292. }
  293. /*********************************************************************
  294. * @fn DevInfo_GetParameter
  295. *
  296. * @brief Get a Device Information parameter.
  297. *
  298. * @param param - Profile parameter ID
  299. * @param value - pointer to data to get. This is dependent on
  300. * the parameter ID and WILL be cast to the appropriate
  301. * data type (example: data type of uint16_t will be cast to
  302. * uint16_t pointer).
  303. *
  304. * @return bStatus_t
  305. */
  306. bStatus_t DevInfo_GetParameter(uint8_t param, void *value)
  307. {
  308. bStatus_t ret = SUCCESS;
  309. switch(param)
  310. {
  311. case DEVINFO_SYSTEM_ID:
  312. tmos_memcpy(value, devInfoSystemId, sizeof(devInfoSystemId));
  313. break;
  314. case DEVINFO_MODEL_NUMBER:
  315. tmos_memcpy(value, devInfoModelNumber, sizeof(devInfoModelNumber));
  316. break;
  317. case DEVINFO_SERIAL_NUMBER:
  318. tmos_memcpy(value, devInfoSerialNumber, sizeof(devInfoSerialNumber));
  319. break;
  320. case DEVINFO_FIRMWARE_REV:
  321. tmos_memcpy(value, devInfoFirmwareRev, sizeof(devInfoFirmwareRev));
  322. break;
  323. case DEVINFO_HARDWARE_REV:
  324. tmos_memcpy(value, devInfoHardwareRev, sizeof(devInfoHardwareRev));
  325. break;
  326. case DEVINFO_SOFTWARE_REV:
  327. tmos_memcpy(value, devInfoSoftwareRev, sizeof(devInfoSoftwareRev));
  328. break;
  329. case DEVINFO_MANUFACTURER_NAME:
  330. tmos_memcpy(value, devInfoMfrName, sizeof(devInfoMfrName));
  331. break;
  332. case DEVINFO_11073_CERT_DATA:
  333. tmos_memcpy(value, devInfo11073Cert, sizeof(devInfo11073Cert));
  334. break;
  335. case DEVINFO_PNP_ID:
  336. tmos_memcpy(value, devInfoPnpId, sizeof(devInfoPnpId));
  337. break;
  338. default:
  339. ret = INVALIDPARAMETER;
  340. break;
  341. }
  342. return (ret);
  343. }
  344. /*********************************************************************
  345. * @fn devInfo_ReadAttrCB
  346. *
  347. * @brief Read an attribute.
  348. *
  349. * @param connHandle - connection message was received on
  350. * @param pAttr - pointer to attribute
  351. * @param pValue - pointer to data to be read
  352. * @param pLen - length of data to be read
  353. * @param offset - offset of the first octet to be read
  354. * @param maxLen - maximum length of data to be read
  355. *
  356. * @return Success or Failure
  357. */
  358. static bStatus_t devInfo_ReadAttrCB(uint16_t connHandle, gattAttribute_t *pAttr,
  359. uint8_t *pValue, uint16_t *pLen, uint16_t offset, uint16_t maxLen, uint8_t method)
  360. {
  361. bStatus_t status = SUCCESS;
  362. uint16_t uuid = BUILD_UINT16(pAttr->type.uuid[0], pAttr->type.uuid[1]);
  363. switch(uuid)
  364. {
  365. case SYSTEM_ID_UUID:
  366. // verify offset
  367. if(offset >= sizeof(devInfoSystemId))
  368. {
  369. status = ATT_ERR_INVALID_OFFSET;
  370. }
  371. else
  372. {
  373. // determine read length
  374. *pLen = MIN(maxLen, (sizeof(devInfoSystemId) - offset));
  375. // copy data
  376. tmos_memcpy(pValue, &devInfoSystemId[offset], *pLen);
  377. }
  378. break;
  379. case MODEL_NUMBER_UUID:
  380. // verify offset
  381. if(offset >= (sizeof(devInfoModelNumber) - 1))
  382. {
  383. status = ATT_ERR_INVALID_OFFSET;
  384. }
  385. else
  386. {
  387. // determine read length (exclude null terminating character)
  388. *pLen = MIN(maxLen, ((sizeof(devInfoModelNumber) - 1) - offset));
  389. // copy data
  390. tmos_memcpy(pValue, &devInfoModelNumber[offset], *pLen);
  391. }
  392. break;
  393. case SERIAL_NUMBER_UUID:
  394. // verify offset
  395. if(offset >= (sizeof(devInfoSerialNumber) - 1))
  396. {
  397. status = ATT_ERR_INVALID_OFFSET;
  398. }
  399. else
  400. {
  401. // determine read length (exclude null terminating character)
  402. *pLen = MIN(maxLen, ((sizeof(devInfoSerialNumber) - 1) - offset));
  403. // copy data
  404. tmos_memcpy(pValue, &devInfoSerialNumber[offset], *pLen);
  405. }
  406. break;
  407. case FIRMWARE_REV_UUID:
  408. // verify offset
  409. if(offset >= (sizeof(devInfoFirmwareRev) - 1))
  410. {
  411. status = ATT_ERR_INVALID_OFFSET;
  412. }
  413. else
  414. {
  415. // determine read length (exclude null terminating character)
  416. *pLen = MIN(maxLen, ((sizeof(devInfoFirmwareRev) - 1) - offset));
  417. // copy data
  418. tmos_memcpy(pValue, &devInfoFirmwareRev[offset], *pLen);
  419. }
  420. break;
  421. case HARDWARE_REV_UUID:
  422. // verify offset
  423. if(offset >= (sizeof(devInfoHardwareRev) - 1))
  424. {
  425. status = ATT_ERR_INVALID_OFFSET;
  426. }
  427. else
  428. {
  429. // determine read length (exclude null terminating character)
  430. *pLen = MIN(maxLen, ((sizeof(devInfoHardwareRev) - 1) - offset));
  431. // copy data
  432. tmos_memcpy(pValue, &devInfoHardwareRev[offset], *pLen);
  433. }
  434. break;
  435. case SOFTWARE_REV_UUID:
  436. // verify offset
  437. if(offset >= (sizeof(devInfoSoftwareRev) - 1))
  438. {
  439. status = ATT_ERR_INVALID_OFFSET;
  440. }
  441. else
  442. {
  443. // determine read length (exclude null terminating character)
  444. *pLen = MIN(maxLen, ((sizeof(devInfoSoftwareRev) - 1) - offset));
  445. // copy data
  446. tmos_memcpy(pValue, &devInfoSoftwareRev[offset], *pLen);
  447. }
  448. break;
  449. case MANUFACTURER_NAME_UUID:
  450. // verify offset
  451. if(offset >= (sizeof(devInfoMfrName) - 1))
  452. {
  453. status = ATT_ERR_INVALID_OFFSET;
  454. }
  455. else
  456. {
  457. // determine read length (exclude null terminating character)
  458. *pLen = MIN(maxLen, ((sizeof(devInfoMfrName) - 1) - offset));
  459. // copy data
  460. tmos_memcpy(pValue, &devInfoMfrName[offset], *pLen);
  461. }
  462. break;
  463. case IEEE_11073_CERT_DATA_UUID:
  464. // verify offset
  465. if(offset >= sizeof(devInfo11073Cert))
  466. {
  467. status = ATT_ERR_INVALID_OFFSET;
  468. }
  469. else
  470. {
  471. // determine read length
  472. *pLen = MIN(maxLen, (sizeof(devInfo11073Cert) - offset));
  473. // copy data
  474. tmos_memcpy(pValue, &devInfo11073Cert[offset], *pLen);
  475. }
  476. break;
  477. case PNP_ID_UUID:
  478. // verify offset
  479. if(offset >= sizeof(devInfoPnpId))
  480. {
  481. status = ATT_ERR_INVALID_OFFSET;
  482. }
  483. else
  484. {
  485. // determine read length
  486. *pLen = MIN(maxLen, (sizeof(devInfoPnpId) - offset));
  487. // copy data
  488. tmos_memcpy(pValue, &devInfoPnpId[offset], *pLen);
  489. }
  490. break;
  491. default:
  492. *pLen = 0;
  493. status = ATT_ERR_ATTR_NOT_FOUND;
  494. break;
  495. }
  496. return (status);
  497. }
  498. /*********************************************************************
  499. *********************************************************************/