hidkbdservice.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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. *****************************************************************************/
  30. /*********************************************************************
  31. * INCLUDES
  32. */
  33. #include "bcomdef.h"
  34. #include "OSAL.h"
  35. #include "att.h"
  36. #include "gatt.h"
  37. #include "gatt_uuid.h"
  38. #include "gatt_profile_uuid.h"
  39. #include "linkdb.h"
  40. #include "gattservapp.h"
  41. #include "hidkbdservice.h"
  42. #include "peripheral.h"
  43. #include "hiddev.h"
  44. #include "battservice.h"
  45. /*********************************************************************
  46. * MACROS
  47. */
  48. /*********************************************************************
  49. * CONSTANTS
  50. */
  51. /*********************************************************************
  52. * TYPEDEFS
  53. */
  54. /*********************************************************************
  55. * GLOBAL VARIABLES
  56. */
  57. // HID service
  58. CONST uint8 hidServUUID[ATT_BT_UUID_SIZE] =
  59. {
  60. LO_UINT16(HID_SERV_UUID), HI_UINT16(HID_SERV_UUID)
  61. };
  62. // HID Boot Keyboard Input Report characteristic
  63. CONST uint8 hidBootKeyInputUUID[ATT_BT_UUID_SIZE] =
  64. {
  65. LO_UINT16(BOOT_KEY_INPUT_UUID), HI_UINT16(BOOT_KEY_INPUT_UUID)
  66. };
  67. // HID Boot Mouse Input Report characteristic
  68. CONST uint8 hidBootMouseInputUUID[ATT_BT_UUID_SIZE] =
  69. {
  70. LO_UINT16(BOOT_MOUSE_INPUT_UUID), HI_UINT16(BOOT_MOUSE_INPUT_UUID)
  71. };
  72. // HID Boot Keyboard Output Report characteristic
  73. CONST uint8 hidBootKeyOutputUUID[ATT_BT_UUID_SIZE] =
  74. {
  75. LO_UINT16(BOOT_KEY_OUTPUT_UUID), HI_UINT16(BOOT_KEY_OUTPUT_UUID)
  76. };
  77. // HID Information characteristic
  78. CONST uint8 hidInfoUUID[ATT_BT_UUID_SIZE] =
  79. {
  80. LO_UINT16(HID_INFORMATION_UUID), HI_UINT16(HID_INFORMATION_UUID)
  81. };
  82. // HID Report Map characteristic
  83. CONST uint8 hidReportMapUUID[ATT_BT_UUID_SIZE] =
  84. {
  85. LO_UINT16(REPORT_MAP_UUID), HI_UINT16(REPORT_MAP_UUID)
  86. };
  87. // HID Control Point characteristic
  88. CONST uint8 hidControlPointUUID[ATT_BT_UUID_SIZE] =
  89. {
  90. LO_UINT16(HID_CTRL_PT_UUID), HI_UINT16(HID_CTRL_PT_UUID)
  91. };
  92. // HID Report characteristic
  93. CONST uint8 hidReportUUID[ATT_BT_UUID_SIZE] =
  94. {
  95. LO_UINT16(REPORT_UUID), HI_UINT16(REPORT_UUID)
  96. };
  97. // HID Protocol Mode characteristic
  98. CONST uint8 hidProtocolModeUUID[ATT_BT_UUID_SIZE] =
  99. {
  100. LO_UINT16(PROTOCOL_MODE_UUID), HI_UINT16(PROTOCOL_MODE_UUID)
  101. };
  102. /*********************************************************************
  103. * EXTERNAL VARIABLES
  104. */
  105. /*********************************************************************
  106. * EXTERNAL FUNCTIONS
  107. */
  108. /*********************************************************************
  109. * LOCAL VARIABLES
  110. */
  111. // HID Information characteristic value
  112. static CONST uint8 hidInfo[HID_INFORMATION_LEN] =
  113. {
  114. LO_UINT16(0x0111), HI_UINT16(0x0111), // bcdHID (USB HID version)
  115. 0x00, // bCountryCode
  116. HID_KBD_FLAGS // Flags
  117. };
  118. // HID Report Map characteristic value
  119. // Keyboard report descriptor (using format for Boot interface descriptor)
  120. static CONST uint8 hidReportMap[] =
  121. {
  122. 0x05, 0x01, // Usage Pg (Generic Desktop)
  123. 0x09, 0x06, // Usage (Keyboard)
  124. 0xA1, 0x01, // Collection: (Application)
  125. //
  126. 0x05, 0x07, // Usage Pg (Key Codes)
  127. 0x19, 0xE0, // Usage Min (224)
  128. 0x29, 0xE7, // Usage Max (231)
  129. 0x15, 0x00, // Log Min (0)
  130. 0x25, 0x01, // Log Max (1)
  131. //
  132. // Modifier byte
  133. 0x75, 0x01, // Report Size (1)
  134. 0x95, 0x08, // Report Count (8)
  135. 0x81, 0x02, // Input: (Data, Variable, Absolute)
  136. //
  137. // Reserved byte
  138. 0x95, 0x01, // Report Count (1)
  139. 0x75, 0x08, // Report Size (8)
  140. 0x81, 0x01, // Input: (Constant)
  141. //
  142. // LED report
  143. 0x95, 0x05, // Report Count (5)
  144. 0x75, 0x01, // Report Size (1)
  145. 0x05, 0x08, // Usage Pg (LEDs)
  146. 0x19, 0x01, // Usage Min (1)
  147. 0x29, 0x05, // Usage Max (5)
  148. 0x91, 0x02, // Output: (Data, Variable, Absolute)
  149. //
  150. // LED report padding
  151. 0x95, 0x01, // Report Count (1)
  152. 0x75, 0x03, // Report Size (3)
  153. 0x91, 0x01, // Output: (Constant)
  154. //
  155. // Key arrays (6 bytes)
  156. 0x95, 0x06, // Report Count (6)
  157. 0x75, 0x08, // Report Size (8)
  158. 0x15, 0x00, // Log Min (0)
  159. 0x25, 0x65, // Log Max (101)
  160. 0x05, 0x07, // Usage Pg (Key Codes)
  161. 0x19, 0x00, // Usage Min (0)
  162. 0x29, 0x65, // Usage Max (101)
  163. 0x81, 0x00, // Input: (Data, Array)
  164. //
  165. 0xC0 // End Collection
  166. };
  167. // HID report map length
  168. uint8 hidReportMapLen = sizeof(hidReportMap);
  169. // HID report mapping table
  170. static hidRptMap_t hidRptMap[HID_NUM_REPORTS];
  171. /*********************************************************************
  172. * Profile Attributes - variables
  173. */
  174. // HID Service attribute
  175. static CONST gattAttrType_t hidService = { ATT_BT_UUID_SIZE, hidServUUID };
  176. // Include attribute (Battery service)
  177. static uint16 include = GATT_INVALID_HANDLE;
  178. // HID Information characteristic
  179. static uint8 hidInfoProps = GATT_PROP_READ;
  180. // HID Report Map characteristic
  181. static uint8 hidReportMapProps = GATT_PROP_READ;
  182. // HID External Report Reference Descriptor
  183. static uint8 hidExtReportRefDesc[ATT_BT_UUID_SIZE] =
  184. { LO_UINT16(BATT_LEVEL_UUID), HI_UINT16(BATT_LEVEL_UUID) };
  185. // HID Control Point characteristic
  186. static uint8 hidControlPointProps = GATT_PROP_WRITE_NO_RSP;
  187. static uint8 hidControlPoint;
  188. // HID Protocol Mode characteristic
  189. static uint8 hidProtocolModeProps = GATT_PROP_READ | GATT_PROP_WRITE_NO_RSP;
  190. uint8 hidProtocolMode = HID_PROTOCOL_MODE_REPORT;
  191. // HID Report characteristic, key input
  192. static uint8 hidReportKeyInProps = GATT_PROP_READ | GATT_PROP_NOTIFY;
  193. static uint8 hidReportKeyIn;
  194. static gattCharCfg_t hidReportKeyInClientCharCfg[GATT_MAX_NUM_CONN];
  195. // HID Report Reference characteristic descriptor, key input
  196. static uint8 hidReportRefKeyIn[HID_REPORT_REF_LEN] =
  197. { HID_RPT_ID_KEY_IN, HID_REPORT_TYPE_INPUT };
  198. // HID Report characteristic, LED output
  199. static uint8 hidReportLedOutProps = GATT_PROP_READ | GATT_PROP_WRITE | GATT_PROP_WRITE_NO_RSP;
  200. static uint8 hidReportLedOut;
  201. // HID Report Reference characteristic descriptor, LED output
  202. static uint8 hidReportRefLedOut[HID_REPORT_REF_LEN] =
  203. { HID_RPT_ID_LED_OUT, HID_REPORT_TYPE_OUTPUT };
  204. // HID Boot Keyboard Input Report
  205. static uint8 hidReportBootKeyInProps = GATT_PROP_READ | GATT_PROP_NOTIFY;
  206. static uint8 hidReportBootKeyIn;
  207. static gattCharCfg_t hidReportBootKeyInClientCharCfg[GATT_MAX_NUM_CONN];
  208. // HID Boot Keyboard Output Report
  209. static uint8 hidReportBootKeyOutProps = GATT_PROP_READ | GATT_PROP_WRITE | GATT_PROP_WRITE_NO_RSP;
  210. static uint8 hidReportBootKeyOut;
  211. // HID Boot Mouse Input Report
  212. static uint8 hidReportBootMouseInProps = GATT_PROP_READ | GATT_PROP_NOTIFY;
  213. static uint8 hidReportBootMouseIn;
  214. static gattCharCfg_t hidReportBootMouseInClientCharCfg[GATT_MAX_NUM_CONN];
  215. // Feature Report
  216. static uint8 hidReportFeatureProps = GATT_PROP_READ | GATT_PROP_WRITE;
  217. static uint8 hidReportFeature;
  218. // HID Report Reference characteristic descriptor, Feature
  219. static uint8 hidReportRefFeature[HID_REPORT_REF_LEN] =
  220. { HID_RPT_ID_FEATURE, HID_REPORT_TYPE_FEATURE };
  221. /*********************************************************************
  222. * Profile Attributes - Table
  223. */
  224. static gattAttribute_t hidAttrTbl[] =
  225. {
  226. // HID Service
  227. {
  228. { ATT_BT_UUID_SIZE, primaryServiceUUID }, /* type */
  229. GATT_PERMIT_READ, /* permissions */
  230. 0, /* handle */
  231. (uint8 *) &hidService /* pValue */
  232. },
  233. // Included service (battery)
  234. {
  235. { ATT_BT_UUID_SIZE, includeUUID },
  236. GATT_PERMIT_READ,
  237. 0,
  238. (uint8 *)&include
  239. },
  240. // HID Information characteristic declaration
  241. {
  242. { ATT_BT_UUID_SIZE, characterUUID },
  243. GATT_PERMIT_READ,
  244. 0,
  245. &hidInfoProps
  246. },
  247. // HID Information characteristic
  248. {
  249. { ATT_BT_UUID_SIZE, hidInfoUUID },
  250. GATT_PERMIT_ENCRYPT_READ,
  251. 0,
  252. (uint8 *) hidInfo
  253. },
  254. // HID Control Point characteristic declaration
  255. {
  256. { ATT_BT_UUID_SIZE, characterUUID },
  257. GATT_PERMIT_READ,
  258. 0,
  259. &hidControlPointProps
  260. },
  261. // HID Control Point characteristic
  262. {
  263. { ATT_BT_UUID_SIZE, hidControlPointUUID },
  264. GATT_PERMIT_ENCRYPT_WRITE,
  265. 0,
  266. &hidControlPoint
  267. },
  268. // HID Protocol Mode characteristic declaration
  269. {
  270. { ATT_BT_UUID_SIZE, characterUUID },
  271. GATT_PERMIT_READ,
  272. 0,
  273. &hidProtocolModeProps
  274. },
  275. // HID Protocol Mode characteristic
  276. {
  277. { ATT_BT_UUID_SIZE, hidProtocolModeUUID },
  278. GATT_PERMIT_ENCRYPT_READ | GATT_PERMIT_ENCRYPT_WRITE,
  279. 0,
  280. &hidProtocolMode
  281. },
  282. // HID Report Map characteristic declaration
  283. {
  284. { ATT_BT_UUID_SIZE, characterUUID },
  285. GATT_PERMIT_READ,
  286. 0,
  287. &hidReportMapProps
  288. },
  289. // HID Report Map characteristic
  290. {
  291. { ATT_BT_UUID_SIZE, hidReportMapUUID },
  292. GATT_PERMIT_ENCRYPT_READ,
  293. 0,
  294. (uint8 *) hidReportMap
  295. },
  296. // HID External Report Reference Descriptor
  297. {
  298. { ATT_BT_UUID_SIZE, extReportRefUUID },
  299. GATT_PERMIT_READ,
  300. 0,
  301. hidExtReportRefDesc
  302. },
  303. // HID Report characteristic, key input declaration
  304. {
  305. { ATT_BT_UUID_SIZE, characterUUID },
  306. GATT_PERMIT_READ,
  307. 0,
  308. &hidReportKeyInProps
  309. },
  310. // HID Report characteristic, key input
  311. {
  312. { ATT_BT_UUID_SIZE, hidReportUUID },
  313. GATT_PERMIT_ENCRYPT_READ,
  314. 0,
  315. &hidReportKeyIn
  316. },
  317. // HID Report characteristic client characteristic configuration
  318. {
  319. { ATT_BT_UUID_SIZE, clientCharCfgUUID },
  320. GATT_PERMIT_READ | GATT_PERMIT_ENCRYPT_WRITE,
  321. 0,
  322. (uint8 *) &hidReportKeyInClientCharCfg
  323. },
  324. // HID Report Reference characteristic descriptor, key input
  325. {
  326. { ATT_BT_UUID_SIZE, reportRefUUID },
  327. GATT_PERMIT_READ,
  328. 0,
  329. hidReportRefKeyIn
  330. },
  331. // HID Report characteristic, LED output declaration
  332. {
  333. { ATT_BT_UUID_SIZE, characterUUID },
  334. GATT_PERMIT_READ,
  335. 0,
  336. &hidReportLedOutProps
  337. },
  338. // HID Report characteristic, LED output
  339. {
  340. { ATT_BT_UUID_SIZE, hidReportUUID },
  341. GATT_PERMIT_ENCRYPT_READ | GATT_PERMIT_ENCRYPT_WRITE,
  342. 0,
  343. &hidReportLedOut
  344. },
  345. // HID Report Reference characteristic descriptor, LED output
  346. {
  347. { ATT_BT_UUID_SIZE, reportRefUUID },
  348. GATT_PERMIT_READ,
  349. 0,
  350. hidReportRefLedOut
  351. },
  352. // HID Boot Keyboard Input Report declaration
  353. {
  354. { ATT_BT_UUID_SIZE, characterUUID },
  355. GATT_PERMIT_READ,
  356. 0,
  357. &hidReportBootKeyInProps
  358. },
  359. // HID Boot Keyboard Input Report
  360. {
  361. { ATT_BT_UUID_SIZE, hidBootKeyInputUUID },
  362. GATT_PERMIT_ENCRYPT_READ,
  363. 0,
  364. &hidReportBootKeyIn
  365. },
  366. // HID Boot Keyboard Input Report characteristic client characteristic configuration
  367. {
  368. { ATT_BT_UUID_SIZE, clientCharCfgUUID },
  369. GATT_PERMIT_READ | GATT_PERMIT_ENCRYPT_WRITE,
  370. 0,
  371. (uint8 *) &hidReportBootKeyInClientCharCfg
  372. },
  373. // HID Boot Keyboard Output Report declaration
  374. {
  375. { ATT_BT_UUID_SIZE, characterUUID },
  376. GATT_PERMIT_READ,
  377. 0,
  378. &hidReportBootKeyOutProps
  379. },
  380. // HID Boot Keyboard Output Report
  381. {
  382. { ATT_BT_UUID_SIZE, hidBootKeyOutputUUID },
  383. GATT_PERMIT_ENCRYPT_READ | GATT_PERMIT_ENCRYPT_WRITE,
  384. 0,
  385. &hidReportBootKeyOut
  386. },
  387. // HID Boot Mouse Input Report declaration
  388. {
  389. { ATT_BT_UUID_SIZE, characterUUID },
  390. GATT_PERMIT_READ,
  391. 0,
  392. &hidReportBootMouseInProps
  393. },
  394. // HID Boot Mouse Input Report
  395. {
  396. { ATT_BT_UUID_SIZE, hidBootMouseInputUUID },
  397. GATT_PERMIT_ENCRYPT_READ,
  398. 0,
  399. &hidReportBootMouseIn
  400. },
  401. // HID Boot Mouse Input Report characteristic client characteristic configuration
  402. {
  403. { ATT_BT_UUID_SIZE, clientCharCfgUUID },
  404. GATT_PERMIT_READ | GATT_PERMIT_ENCRYPT_WRITE,
  405. 0,
  406. (uint8 *) &hidReportBootMouseInClientCharCfg
  407. },
  408. // Feature Report declaration
  409. {
  410. { ATT_BT_UUID_SIZE, characterUUID },
  411. GATT_PERMIT_READ,
  412. 0,
  413. &hidReportFeatureProps
  414. },
  415. // Feature Report
  416. {
  417. { ATT_BT_UUID_SIZE, hidReportUUID},
  418. GATT_PERMIT_ENCRYPT_READ | GATT_PERMIT_ENCRYPT_WRITE,
  419. 0,
  420. &hidReportFeature
  421. },
  422. // HID Report Reference characteristic descriptor, feature
  423. {
  424. { ATT_BT_UUID_SIZE, reportRefUUID },
  425. GATT_PERMIT_READ,
  426. 0,
  427. hidReportRefFeature
  428. },
  429. };
  430. // Attribute index enumeration-- these indexes match array elements above
  431. enum
  432. {
  433. HID_SERVICE_IDX, // HID Service
  434. HID_INCLUDED_SERVICE_IDX, // Included Service
  435. HID_INFO_DECL_IDX, // HID Information characteristic declaration
  436. HID_INFO_IDX, // HID Information characteristic
  437. HID_CONTROL_POINT_DECL_IDX, // HID Control Point characteristic declaration
  438. HID_CONTROL_POINT_IDX, // HID Control Point characteristic
  439. HID_PROTOCOL_MODE_DECL_IDX, // HID Protocol Mode characteristic declaration
  440. HID_PROTOCOL_MODE_IDX, // HID Protocol Mode characteristic
  441. HID_REPORT_MAP_DECL_IDX, // HID Report Map characteristic declaration
  442. HID_REPORT_MAP_IDX, // HID Report Map characteristic
  443. HID_EXT_REPORT_REF_DESC_IDX, // HID External Report Reference Descriptor
  444. HID_REPORT_KEY_IN_DECL_IDX, // HID Report characteristic, key input declaration
  445. HID_REPORT_KEY_IN_IDX, // HID Report characteristic, key input
  446. HID_REPORT_KEY_IN_CCCD_IDX, // HID Report characteristic client characteristic configuration
  447. HID_REPORT_REF_KEY_IN_IDX, // HID Report Reference characteristic descriptor, key input
  448. HID_REPORT_LED_OUT_DECL_IDX, // HID Report characteristic, LED output declaration
  449. HID_REPORT_LED_OUT_IDX, // HID Report characteristic, LED output
  450. HID_REPORT_REF_LED_OUT_IDX, // HID Report Reference characteristic descriptor, LED output
  451. HID_BOOT_KEY_IN_DECL_IDX, // HID Boot Keyboard Input Report declaration
  452. HID_BOOT_KEY_IN_IDX, // HID Boot Keyboard Input Report
  453. HID_BOOT_KEY_IN_CCCD_IDX, // HID Boot Keyboard Input Report characteristic client characteristic configuration
  454. HID_BOOT_KEY_OUT_DECL_IDX, // HID Boot Keyboard Output Report declaration
  455. HID_BOOT_KEY_OUT_IDX, // HID Boot Keyboard Output Report
  456. HID_BOOT_MOUSE_IN_DECL_IDX, // HID Boot Mouse Input Report declaration
  457. HID_BOOT_MOUSE_IN_IDX, // HID Boot Mouse Input Report
  458. HID_BOOT_MOUSE_IN_CCCD_IDX, // HID Boot Mouse Input Report characteristic client characteristic configuration
  459. HID_FEATURE_DECL_IDX, // Feature Report declaration
  460. HID_FEATURE_IDX, // Feature Report
  461. HID_REPORT_REF_FEATURE_IDX // HID Report Reference characteristic descriptor, feature
  462. };
  463. /*********************************************************************
  464. * LOCAL FUNCTIONS
  465. */
  466. /*********************************************************************
  467. * PROFILE CALLBACKS
  468. */
  469. // Service Callbacks
  470. CONST gattServiceCBs_t hidKbdCBs =
  471. {
  472. HidDev_ReadAttrCB, // Read callback function pointer
  473. HidDev_WriteAttrCB, // Write callback function pointer
  474. NULL // Authorization callback function pointer
  475. };
  476. /*********************************************************************
  477. * PUBLIC FUNCTIONS
  478. */
  479. /*********************************************************************
  480. * @fn HidKbd_AddService
  481. *
  482. * @brief Initializes the HID Service by registering
  483. * GATT attributes with the GATT server.
  484. *
  485. * @return Success or Failure
  486. */
  487. bStatus_t HidKbd_AddService( void )
  488. {
  489. uint8 status = SUCCESS;
  490. // Initialize Client Characteristic Configuration attributes
  491. GATTServApp_InitCharCfg( INVALID_CONNHANDLE, hidReportKeyInClientCharCfg );
  492. GATTServApp_InitCharCfg( INVALID_CONNHANDLE, hidReportBootKeyInClientCharCfg );
  493. GATTServApp_InitCharCfg( INVALID_CONNHANDLE, hidReportBootMouseInClientCharCfg );
  494. // Register GATT attribute list and CBs with GATT Server App
  495. status = GATTServApp_RegisterService( hidAttrTbl, GATT_NUM_ATTRS( hidAttrTbl ), &hidKbdCBs );
  496. // Set up included service
  497. Batt_GetParameter( BATT_PARAM_SERVICE_HANDLE,
  498. &GATT_INCLUDED_HANDLE( hidAttrTbl, HID_INCLUDED_SERVICE_IDX ) );
  499. // Construct map of reports to characteristic handles
  500. // Each report is uniquely identified via its ID and type
  501. // Key input report
  502. hidRptMap[0].id = hidReportRefKeyIn[0];
  503. hidRptMap[0].type = hidReportRefKeyIn[1];
  504. hidRptMap[0].handle = hidAttrTbl[HID_REPORT_KEY_IN_IDX].handle;
  505. hidRptMap[0].cccdHandle = hidAttrTbl[HID_REPORT_KEY_IN_CCCD_IDX].handle;
  506. hidRptMap[0].mode = HID_PROTOCOL_MODE_REPORT;
  507. // LED output report
  508. hidRptMap[1].id = hidReportRefLedOut[0];
  509. hidRptMap[1].type = hidReportRefLedOut[1];
  510. hidRptMap[1].handle = hidAttrTbl[HID_REPORT_LED_OUT_IDX].handle;
  511. hidRptMap[1].cccdHandle = 0;
  512. hidRptMap[1].mode = HID_PROTOCOL_MODE_REPORT;
  513. // Boot keyboard input report
  514. // Use same ID and type as key input report
  515. hidRptMap[2].id = hidReportRefKeyIn[0];
  516. hidRptMap[2].type = hidReportRefKeyIn[1];
  517. hidRptMap[2].handle = hidAttrTbl[HID_BOOT_KEY_IN_IDX].handle;
  518. hidRptMap[2].cccdHandle = hidAttrTbl[HID_BOOT_KEY_IN_CCCD_IDX].handle;
  519. hidRptMap[2].mode = HID_PROTOCOL_MODE_BOOT;
  520. // Boot keyboard output report
  521. // Use same ID and type as LED output report
  522. hidRptMap[3].id = hidReportRefLedOut[0];
  523. hidRptMap[3].type = hidReportRefLedOut[1];
  524. hidRptMap[3].handle = hidAttrTbl[HID_BOOT_KEY_OUT_IDX].handle;
  525. hidRptMap[3].cccdHandle = 0;
  526. hidRptMap[3].mode = HID_PROTOCOL_MODE_BOOT;
  527. // Boot mouse input report
  528. hidRptMap[4].id = HID_RPT_ID_MOUSE_IN;
  529. hidRptMap[4].type = HID_REPORT_TYPE_INPUT;
  530. hidRptMap[4].handle = hidAttrTbl[HID_BOOT_MOUSE_IN_IDX].handle;
  531. hidRptMap[4].cccdHandle = hidAttrTbl[HID_BOOT_MOUSE_IN_CCCD_IDX].handle;
  532. hidRptMap[4].mode = HID_PROTOCOL_MODE_BOOT;
  533. // Feature report
  534. hidRptMap[5].id = hidReportRefFeature[0];
  535. hidRptMap[5].type = hidReportRefFeature[1];
  536. hidRptMap[5].handle = hidAttrTbl[HID_FEATURE_IDX].handle;
  537. hidRptMap[5].cccdHandle = 0;
  538. hidRptMap[5].mode = HID_PROTOCOL_MODE_REPORT;
  539. // Battery level input report
  540. VOID Batt_GetParameter( BATT_PARAM_BATT_LEVEL_IN_REPORT, &(hidRptMap[6]) );
  541. // Setup report ID map
  542. HidDev_RegisterReports( HID_NUM_REPORTS, hidRptMap );
  543. return ( status );
  544. }
  545. /*********************************************************************
  546. * @fn HidKbd_SetParameter
  547. *
  548. * @brief Set a HID Kbd parameter.
  549. *
  550. * @param id - HID report ID.
  551. * @param type - HID report type.
  552. * @param uuid - attribute uuid.
  553. * @param len - length of data to right.
  554. * @param pValue - pointer to data to write. This is dependent on
  555. * the input parameters and WILL be cast to the appropriate
  556. * data type (example: data type of uint16 will be cast to
  557. * uint16 pointer).
  558. *
  559. * @return GATT status code.
  560. */
  561. uint8 HidKbd_SetParameter( uint8 id, uint8 type, uint16 uuid, uint8 len, void *pValue )
  562. {
  563. bStatus_t ret = SUCCESS;
  564. switch ( uuid )
  565. {
  566. case REPORT_UUID:
  567. if ( type == HID_REPORT_TYPE_OUTPUT )
  568. {
  569. if ( len == 1 )
  570. {
  571. hidReportLedOut = *((uint8 *)pValue);
  572. }
  573. else
  574. {
  575. ret = ATT_ERR_INVALID_VALUE_SIZE;
  576. }
  577. }
  578. else if ( type == HID_REPORT_TYPE_FEATURE )
  579. {
  580. if ( len == 1 )
  581. {
  582. hidReportFeature = *((uint8 *)pValue);
  583. }
  584. else
  585. {
  586. ret = ATT_ERR_INVALID_VALUE_SIZE;
  587. }
  588. }
  589. else
  590. {
  591. ret = ATT_ERR_ATTR_NOT_FOUND;
  592. }
  593. break;
  594. case BOOT_KEY_OUTPUT_UUID:
  595. if ( len == 1 )
  596. {
  597. hidReportBootKeyOut = *((uint8 *)pValue);
  598. }
  599. else
  600. {
  601. ret = ATT_ERR_INVALID_VALUE_SIZE;
  602. }
  603. break;
  604. default:
  605. // ignore the request
  606. break;
  607. }
  608. return ( ret );
  609. }
  610. /*********************************************************************
  611. * @fn HidKbd_GetParameter
  612. *
  613. * @brief Get a HID Kbd parameter.
  614. *
  615. * @param id - HID report ID.
  616. * @param type - HID report type.
  617. * @param uuid - attribute uuid.
  618. * @param pLen - length of data to be read
  619. * @param pValue - pointer to data to get. This is dependent on
  620. * the input parameters and WILL be cast to the appropriate
  621. * data type (example: data type of uint16 will be cast to
  622. * uint16 pointer).
  623. *
  624. * @return GATT status code.
  625. */
  626. uint8 HidKbd_GetParameter( uint8 id, uint8 type, uint16 uuid, uint8 *pLen, void *pValue )
  627. {
  628. switch ( uuid )
  629. {
  630. case REPORT_UUID:
  631. if ( type == HID_REPORT_TYPE_OUTPUT )
  632. {
  633. *((uint8 *)pValue) = hidReportLedOut;
  634. *pLen = 1;
  635. }
  636. else if ( type == HID_REPORT_TYPE_FEATURE )
  637. {
  638. *((uint8 *)pValue) = hidReportFeature;
  639. *pLen = 1;
  640. }
  641. else
  642. {
  643. *pLen = 0;
  644. }
  645. break;
  646. case BOOT_KEY_OUTPUT_UUID:
  647. *((uint8 *)pValue) = hidReportBootKeyOut;
  648. *pLen = 1;
  649. break;
  650. default:
  651. *pLen = 0;
  652. break;
  653. }
  654. return ( SUCCESS );
  655. }
  656. /*********************************************************************
  657. *********************************************************************/