ble_hids.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  1. /**
  2. * Copyright (c) 2012 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /* Attention!
  41. * To maintain compliance with Nordic Semiconductor ASA's Bluetooth profile
  42. * qualification listings, this section of source code must not be modified.
  43. */
  44. #include "sdk_common.h"
  45. #if NRF_MODULE_ENABLED(BLE_HIDS)
  46. #include "ble_hids.h"
  47. #include <string.h>
  48. #include "app_error.h"
  49. #include "ble_srv_common.h"
  50. // Protocol Mode values
  51. #define PROTOCOL_MODE_BOOT 0x00 /**< Boot Protocol Mode. */
  52. #define PROTOCOL_MODE_REPORT 0x01 /**< Report Protocol Mode. */
  53. // HID Control Point values
  54. #define HIDS_CONTROL_POINT_SUSPEND 0 /**< Suspend command. */
  55. #define HIDS_CONTROL_POINT_EXIT_SUSPEND 1 /**< Exit Suspend command. */
  56. #define DEFAULT_PROTOCOL_MODE PROTOCOL_MODE_REPORT /**< Default value for the Protocol Mode characteristic. */
  57. #define INITIAL_VALUE_HID_CONTROL_POINT HIDS_CONTROL_POINT_SUSPEND /**< Initial value for the HID Control Point characteristic. */
  58. #define ENCODED_HID_INFORMATION_LEN 4 /**< Maximum size of an encoded HID Information characteristic. */
  59. /**@brief Handles errors for HID service module.
  60. *
  61. * @param[in] _p_hids HID Service structure.
  62. * @param[in] _err_code Maximum number of HID hosts connected at a time.
  63. *
  64. */
  65. #define BLE_HIDS_ERROR_HANDLE(_p_hids, _err_code) \
  66. do \
  67. { \
  68. if (((_err_code) != NRF_SUCCESS) && ((_p_hids)->error_handler != NULL)) \
  69. { \
  70. (_p_hids)->error_handler((_err_code)); \
  71. } \
  72. } while (0)
  73. /**@brief Function for calculating the link context size for the HID service.
  74. *
  75. * @param[in] p_hids HID Service structure.
  76. *
  77. * @return Link context size.
  78. */
  79. static size_t ble_hids_client_context_size_calc(ble_hids_t * p_hids)
  80. {
  81. size_t client_size = sizeof(ble_hids_client_context_t) + (BOOT_KB_INPUT_REPORT_MAX_SIZE) +
  82. (BOOT_KB_OUTPUT_REPORT_MAX_SIZE) + (BOOT_MOUSE_INPUT_REPORT_MAX_SIZE);
  83. if (p_hids->p_inp_rep_init_array != NULL)
  84. {
  85. for (uint32_t i = 0; i < p_hids->inp_rep_count; i++)
  86. {
  87. client_size += p_hids->p_inp_rep_init_array[i].max_len;
  88. }
  89. }
  90. if (p_hids->p_outp_rep_init_array != NULL)
  91. {
  92. for (uint32_t i = 0; i < p_hids->outp_rep_count; i++)
  93. {
  94. client_size += p_hids->p_outp_rep_init_array[i].max_len;
  95. }
  96. }
  97. if (p_hids->p_feature_rep_init_array != NULL)
  98. {
  99. for (uint32_t i = 0; i < p_hids->feature_rep_count; i++)
  100. {
  101. client_size += p_hids->p_feature_rep_init_array[i].max_len;
  102. }
  103. }
  104. return client_size;
  105. }
  106. /**@brief Function for making a HID Service characteristic id.
  107. *
  108. * @param[in] uuid UUID of characteristic.
  109. * @param[in] rep_type Type of report.
  110. * @param[in] rep_index Index of the characteristic.
  111. *
  112. * @return HID Service characteristic id structure.
  113. */
  114. static ble_hids_char_id_t make_char_id(uint16_t uuid, uint8_t rep_type, uint8_t rep_index)
  115. {
  116. ble_hids_char_id_t char_id = {0};
  117. char_id.uuid = uuid;
  118. char_id.rep_type = rep_type;
  119. char_id.rep_index = rep_index;
  120. return char_id;
  121. }
  122. /**@brief Function for handling the Connect event.
  123. *
  124. * @param[in] p_hids HID Service structure.
  125. * @param[in] p_ble_evt Event received from the BLE stack.
  126. */
  127. static void on_connect(ble_hids_t * p_hids, ble_evt_t const * p_ble_evt)
  128. {
  129. uint32_t err_code;
  130. ble_gatts_value_t gatts_value;
  131. ble_hids_client_context_t * p_client = NULL;
  132. err_code = blcm_link_ctx_get(p_hids->p_link_ctx_storage,
  133. p_ble_evt->evt.gap_evt.conn_handle,
  134. (void *) &p_client);
  135. BLE_HIDS_ERROR_HANDLE(p_hids, err_code);
  136. if (p_client == NULL)
  137. {
  138. return;
  139. }
  140. memset(p_client, 0, ble_hids_client_context_size_calc(p_hids));
  141. if (p_hids->protocol_mode_handles.value_handle)
  142. {
  143. // Set Protocol Mode characteristic value to default value
  144. p_client->protocol_mode = DEFAULT_PROTOCOL_MODE;
  145. // Initialize value struct.
  146. memset(&gatts_value, 0, sizeof(gatts_value));
  147. gatts_value.len = sizeof(uint8_t);
  148. gatts_value.offset = 0;
  149. gatts_value.p_value = &p_client->protocol_mode;
  150. err_code = sd_ble_gatts_value_set(p_ble_evt->evt.gap_evt.conn_handle,
  151. p_hids->protocol_mode_handles.value_handle,
  152. &gatts_value);
  153. BLE_HIDS_ERROR_HANDLE(p_hids, err_code);
  154. }
  155. }
  156. /**@brief Function for handling write events to the HID Control Point value.
  157. *
  158. * @param[in] p_hids HID Service structure.
  159. * @param[in] p_evt_write Write event received from the BLE stack.
  160. */
  161. static void on_control_point_write(ble_hids_t * p_hids, ble_evt_t const * p_ble_evt)
  162. {
  163. ret_code_t err_code;
  164. ble_hids_client_context_t * p_host;
  165. ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
  166. err_code = blcm_link_ctx_get(p_hids->p_link_ctx_storage,
  167. p_ble_evt->evt.gatts_evt.conn_handle,
  168. (void *) &p_host);
  169. BLE_HIDS_ERROR_HANDLE(p_hids, err_code);
  170. if (p_host == NULL)
  171. {
  172. return;
  173. }
  174. if (p_evt_write->len == 1)
  175. {
  176. ble_hids_evt_t evt;
  177. switch (p_evt_write->data[0])
  178. {
  179. case HIDS_CONTROL_POINT_SUSPEND:
  180. evt.evt_type = BLE_HIDS_EVT_HOST_SUSP;
  181. break;
  182. case HIDS_CONTROL_POINT_EXIT_SUSPEND:
  183. evt.evt_type = BLE_HIDS_EVT_HOST_EXIT_SUSP;
  184. break;
  185. default:
  186. // Illegal Control Point value, ignore
  187. return;
  188. }
  189. // Store the new Control Point value for the host
  190. p_host->ctrl_pt = p_evt_write->data[0];
  191. // HID Control Point written, propagate event to application
  192. if (p_hids->evt_handler != NULL)
  193. {
  194. evt.p_ble_evt = p_ble_evt;
  195. p_hids->evt_handler(p_hids, &evt);
  196. }
  197. }
  198. }
  199. /**@brief Function for handling write events to the Protocol Mode value.
  200. *
  201. * @param[in] p_hids HID Service structure.
  202. * @param[in] p_ble_evt BLE event received from the BLE stack.
  203. */
  204. static void on_protocol_mode_write(ble_hids_t * p_hids, ble_evt_t const * p_ble_evt)
  205. {
  206. ret_code_t err_code;
  207. ble_hids_client_context_t * p_host;
  208. ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
  209. err_code = blcm_link_ctx_get(p_hids->p_link_ctx_storage,
  210. p_ble_evt->evt.gatts_evt.conn_handle,
  211. (void *) &p_host);
  212. BLE_HIDS_ERROR_HANDLE(p_hids, err_code);
  213. if (p_host == NULL)
  214. {
  215. return;
  216. }
  217. if (p_evt_write->len == 1)
  218. {
  219. ble_hids_evt_t evt;
  220. switch (p_evt_write->data[0])
  221. {
  222. case PROTOCOL_MODE_BOOT:
  223. evt.evt_type = BLE_HIDS_EVT_BOOT_MODE_ENTERED;
  224. break;
  225. case PROTOCOL_MODE_REPORT:
  226. evt.evt_type = BLE_HIDS_EVT_REPORT_MODE_ENTERED;
  227. break;
  228. default:
  229. // Illegal Protocol Mode value, ignore
  230. return;
  231. }
  232. // Store Protocol Mode of the host
  233. p_host->protocol_mode = p_evt_write->data[0];
  234. // HID Protocol Mode written, propagate event to application
  235. if (p_hids->evt_handler != NULL)
  236. {
  237. evt.p_ble_evt = p_ble_evt;
  238. p_hids->evt_handler(p_hids, &evt);
  239. }
  240. }
  241. }
  242. /**@brief Function for handling authorize read events to the Protocol Mode value.
  243. *
  244. * @param[in] p_hids HID Service structure.
  245. * @param[in] p_ble_evt BLE event received from the BLE stack.
  246. */
  247. void on_protocol_mode_read_auth(ble_hids_t * p_hids, ble_evt_t const * p_ble_evt)
  248. {
  249. ret_code_t err_code;
  250. ble_gatts_rw_authorize_reply_params_t auth_read_params;
  251. ble_hids_client_context_t * p_host;
  252. ble_gatts_evt_rw_authorize_request_t const * p_read_auth =
  253. &p_ble_evt->evt.gatts_evt.params.authorize_request;
  254. err_code = blcm_link_ctx_get(p_hids->p_link_ctx_storage,
  255. p_ble_evt->evt.gatts_evt.conn_handle,
  256. (void *) &p_host);
  257. BLE_HIDS_ERROR_HANDLE(p_hids, err_code);
  258. // Update GATTS table with this host's Protocol Mode value and authorize Read
  259. if (p_host != NULL)
  260. {
  261. memset(&auth_read_params, 0, sizeof(auth_read_params));
  262. auth_read_params.type = BLE_GATTS_AUTHORIZE_TYPE_READ;
  263. auth_read_params.params.read.gatt_status = BLE_GATT_STATUS_SUCCESS;
  264. auth_read_params.params.read.offset = p_read_auth->request.read.offset;
  265. auth_read_params.params.read.len = sizeof(uint8_t);
  266. auth_read_params.params.read.p_data = &p_host->protocol_mode;
  267. auth_read_params.params.read.update = 1;
  268. err_code = sd_ble_gatts_rw_authorize_reply(p_ble_evt->evt.gap_evt.conn_handle,
  269. &auth_read_params);
  270. BLE_HIDS_ERROR_HANDLE(p_hids, err_code);
  271. }
  272. }
  273. /**@brief Function for handling write events to a report CCCD.
  274. *
  275. * @param[in] p_hids HID Service structure.
  276. * @param[in] p_char_id Id of report characteristic.
  277. * @param[in] p_ble_evt BLE event received from the BLE stack.
  278. */
  279. static void on_report_cccd_write(ble_hids_t * p_hids,
  280. ble_hids_char_id_t * p_char_id,
  281. ble_evt_t const * p_ble_evt)
  282. {
  283. ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
  284. if (p_evt_write->len == 2)
  285. {
  286. // CCCD written, update notification state
  287. if (p_hids->evt_handler != NULL)
  288. {
  289. ble_hids_evt_t evt;
  290. if (ble_srv_is_notification_enabled(p_evt_write->data))
  291. {
  292. evt.evt_type = BLE_HIDS_EVT_NOTIF_ENABLED;
  293. }
  294. else
  295. {
  296. evt.evt_type = BLE_HIDS_EVT_NOTIF_DISABLED;
  297. }
  298. evt.params.notification.char_id = *p_char_id;
  299. evt.p_ble_evt = p_ble_evt;
  300. p_hids->evt_handler(p_hids, &evt);
  301. }
  302. }
  303. }
  304. /**@brief Function for handling write events to a report value.
  305. *
  306. * @param[in] p_hids HID Service structure.
  307. * @param[in] p_ble_evt Pointer to BLE event structure.
  308. * @param[in] p_char_id Id of report characteristic.
  309. * @param[in] rep_offset Offset to the affected HID report data.
  310. * @param[in] rep_max_len Maximum HID report length.
  311. */
  312. static void on_report_value_write(ble_hids_t * p_hids,
  313. ble_evt_t const * p_ble_evt,
  314. ble_hids_char_id_t * p_char_id,
  315. uint16_t rep_offset,
  316. uint16_t rep_max_len)
  317. {
  318. // Update host's Output Report data
  319. ret_code_t err_code;
  320. uint8_t * p_report;
  321. ble_hids_client_context_t * p_host;
  322. ble_gatts_evt_write_t const * p_write = &p_ble_evt->evt.gatts_evt.params.write;
  323. err_code = blcm_link_ctx_get(p_hids->p_link_ctx_storage,
  324. p_ble_evt->evt.gatts_evt.conn_handle,
  325. (void *) &p_host);
  326. BLE_HIDS_ERROR_HANDLE(p_hids, err_code);
  327. // Store the written values in host's report data
  328. if ((p_host != NULL) && (p_write->len + p_write->offset <= rep_max_len))
  329. {
  330. p_report = (uint8_t *) p_host + rep_offset;
  331. memcpy(p_report, p_write->data, p_write->len);
  332. }
  333. else
  334. {
  335. return;
  336. }
  337. // Notify the applicartion
  338. if (p_hids->evt_handler != NULL)
  339. {
  340. ble_hids_evt_t evt;
  341. evt.evt_type = BLE_HIDS_EVT_REP_CHAR_WRITE;
  342. evt.params.char_write.char_id = *p_char_id;
  343. evt.params.char_write.offset = p_ble_evt->evt.gatts_evt.params.write.offset;
  344. evt.params.char_write.len = p_ble_evt->evt.gatts_evt.params.write.len;
  345. evt.params.char_write.data = p_ble_evt->evt.gatts_evt.params.write.data;
  346. evt.p_ble_evt = p_ble_evt;
  347. p_hids->evt_handler(p_hids, &evt);
  348. }
  349. }
  350. /**@brief Handle authorize read events to a report value.
  351. *
  352. * @param[in] p_hids HID Service structure.
  353. * @param[in] p_char_id Id of report characteristic.
  354. * @param[in] p_ble_evt Pointer to BLE event structure.
  355. * @param[in] rep_offset Report data offset.
  356. * @param[in] rep_max_len Report maximum length.
  357. */
  358. static void on_report_value_read_auth(ble_hids_t * p_hids,
  359. ble_hids_char_id_t * p_char_id,
  360. ble_evt_t const * p_ble_evt,
  361. uint16_t rep_offset,
  362. uint16_t rep_max_len)
  363. {
  364. ble_gatts_rw_authorize_reply_params_t auth_read_params;
  365. ble_hids_client_context_t * p_host;
  366. uint8_t * p_report;
  367. uint16_t read_offset;
  368. uint32_t err_code = NRF_SUCCESS;
  369. err_code = blcm_link_ctx_get(p_hids->p_link_ctx_storage,
  370. p_ble_evt->evt.gatts_evt.conn_handle,
  371. (void *) &p_host);
  372. BLE_HIDS_ERROR_HANDLE(p_hids, err_code);
  373. // Update Report GATTS table with host's current report data
  374. if (p_host != NULL)
  375. {
  376. read_offset = p_ble_evt->evt.gatts_evt.params.authorize_request.request.read.offset;
  377. p_report = (uint8_t *) p_host + rep_offset;
  378. memset(&auth_read_params, 0, sizeof(auth_read_params));
  379. auth_read_params.type = BLE_GATTS_AUTHORIZE_TYPE_READ;
  380. auth_read_params.params.read.gatt_status = BLE_GATT_STATUS_SUCCESS;
  381. auth_read_params.params.read.offset = read_offset;
  382. auth_read_params.params.read.len = rep_max_len - read_offset;
  383. auth_read_params.params.read.p_data = p_report + read_offset;
  384. auth_read_params.params.read.update = 1;
  385. err_code = sd_ble_gatts_rw_authorize_reply(p_ble_evt->evt.gap_evt.conn_handle,
  386. &auth_read_params);
  387. BLE_HIDS_ERROR_HANDLE(p_hids, err_code);
  388. }
  389. else
  390. {
  391. return;
  392. }
  393. if (p_hids->evt_handler != NULL)
  394. {
  395. ble_hids_evt_t evt;
  396. evt.evt_type = BLE_HIDS_EVT_REPORT_READ;
  397. evt.params.char_auth_read.char_id = *p_char_id;
  398. evt.p_ble_evt = p_ble_evt;
  399. p_hids->evt_handler(p_hids, &evt);
  400. }
  401. }
  402. /**@brief Function for finding the Characteristic Id of a characteristic corresponding to a CCCD handle.
  403. *
  404. * @param[in] p_hids HID Service structure.
  405. * @param[in] handle Handle to search for.
  406. * @param[out] p_char_id Id of report characteristic.
  407. *
  408. * @return TRUE if CCCD handle was found, FALSE otherwise.
  409. */
  410. static bool inp_rep_cccd_identify(ble_hids_t * p_hids,
  411. uint16_t handle,
  412. ble_hids_char_id_t * p_char_id)
  413. {
  414. uint8_t i;
  415. for (i = 0; i < p_hids->inp_rep_count; i++)
  416. {
  417. if (handle == p_hids->inp_rep_array[i].char_handles.cccd_handle)
  418. {
  419. *p_char_id = make_char_id(BLE_UUID_REPORT_CHAR, BLE_HIDS_REP_TYPE_INPUT, i);
  420. return true;
  421. }
  422. }
  423. return false;
  424. }
  425. /**@brief Function for finding the Characteristic Id of a characteristic corresponding to a value handle.
  426. *
  427. * @param[in] p_hids HID Service structure.
  428. * @param[in] handle Handle to search for.
  429. * @param[out] p_char_id Id of report characteristic.
  430. * @param[out] p_context_offset Pointer to offset to the report data within the host data.
  431. * @param[out] p_rep_max_len Pointer to maximum report length.
  432. *
  433. * @return TRUE if value handle was found, FALSE otherwise.
  434. */
  435. static bool rep_value_identify(ble_hids_t * p_hids,
  436. uint16_t handle,
  437. ble_hids_char_id_t * p_char_id,
  438. uint16_t * p_context_offset,
  439. uint16_t * p_rep_max_len)
  440. {
  441. uint8_t i;
  442. /* Initialize the offset with size of non-report-related data */
  443. *p_context_offset = sizeof(ble_hids_client_context_t) + BOOT_KB_INPUT_REPORT_MAX_SIZE +
  444. BOOT_KB_OUTPUT_REPORT_MAX_SIZE + BOOT_MOUSE_INPUT_REPORT_MAX_SIZE;
  445. if (p_hids->p_inp_rep_init_array != NULL)
  446. {
  447. for (i = 0; i < p_hids->inp_rep_count; i++)
  448. {
  449. if (handle == p_hids->inp_rep_array[i].char_handles.value_handle)
  450. {
  451. *p_char_id = make_char_id(BLE_UUID_REPORT_CHAR, BLE_HIDS_REP_TYPE_INPUT, i);
  452. *p_rep_max_len = p_hids->p_inp_rep_init_array[i].max_len;
  453. return true;
  454. }
  455. *p_context_offset += p_hids->p_inp_rep_init_array[i].max_len;
  456. }
  457. }
  458. if (p_hids->p_outp_rep_init_array != NULL)
  459. {
  460. for (i = 0; i < p_hids->outp_rep_count; i++)
  461. {
  462. if (handle == p_hids->outp_rep_array[i].char_handles.value_handle)
  463. {
  464. *p_char_id = make_char_id(BLE_UUID_REPORT_CHAR, BLE_HIDS_REP_TYPE_OUTPUT, i);
  465. *p_rep_max_len = p_hids->p_outp_rep_init_array[i].max_len;
  466. return true;
  467. }
  468. *p_context_offset += p_hids->p_outp_rep_init_array[i].max_len;
  469. }
  470. }
  471. if (p_hids->p_feature_rep_init_array != NULL)
  472. {
  473. for (i = 0; i < p_hids->feature_rep_count; i++)
  474. {
  475. if (handle == p_hids->feature_rep_array[i].char_handles.value_handle)
  476. {
  477. *p_char_id = make_char_id(BLE_UUID_REPORT_CHAR, BLE_HIDS_REP_TYPE_FEATURE, i);
  478. *p_rep_max_len = p_hids->p_feature_rep_init_array[i].max_len;
  479. return true;
  480. }
  481. *p_context_offset += p_hids->p_feature_rep_init_array[i].max_len;
  482. }
  483. }
  484. return false;
  485. }
  486. /**@brief Function for handling the Write event.
  487. *
  488. * @param[in] p_hids HID Service structure.
  489. * @param[in] p_ble_evt Event received from the BLE stack.
  490. */
  491. static void on_write(ble_hids_t * p_hids, ble_evt_t const * p_ble_evt)
  492. {
  493. ble_hids_char_id_t char_id;
  494. ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
  495. uint16_t rep_data_offset = sizeof(ble_hids_client_context_t);
  496. uint16_t max_rep_len = 0;
  497. if (p_evt_write->handle == p_hids->hid_control_point_handles.value_handle)
  498. {
  499. on_control_point_write(p_hids, p_ble_evt);
  500. }
  501. else if (p_evt_write->handle == p_hids->protocol_mode_handles.value_handle)
  502. {
  503. on_protocol_mode_write(p_hids, p_ble_evt);
  504. }
  505. else if (p_evt_write->handle == p_hids->boot_kb_inp_rep_handles.cccd_handle)
  506. {
  507. char_id = make_char_id(BLE_UUID_BOOT_KEYBOARD_INPUT_REPORT_CHAR, 0, 0);
  508. on_report_cccd_write(p_hids, &char_id, p_ble_evt);
  509. }
  510. else if (p_evt_write->handle == p_hids->boot_kb_inp_rep_handles.value_handle)
  511. {
  512. char_id = make_char_id(BLE_UUID_BOOT_KEYBOARD_INPUT_REPORT_CHAR, 0, 0);
  513. max_rep_len = BOOT_KB_INPUT_REPORT_MAX_SIZE;
  514. on_report_value_write(p_hids, p_ble_evt, &char_id, rep_data_offset, max_rep_len);
  515. }
  516. else if (p_evt_write->handle == p_hids->boot_kb_outp_rep_handles.value_handle)
  517. {
  518. char_id = make_char_id(BLE_UUID_BOOT_KEYBOARD_OUTPUT_REPORT_CHAR, 0, 0);
  519. rep_data_offset += BOOT_KB_INPUT_REPORT_MAX_SIZE;
  520. max_rep_len = BOOT_KB_OUTPUT_REPORT_MAX_SIZE;
  521. on_report_value_write(p_hids, p_ble_evt, &char_id, rep_data_offset, max_rep_len);
  522. }
  523. else if (p_evt_write->handle == p_hids->boot_mouse_inp_rep_handles.cccd_handle)
  524. {
  525. char_id = make_char_id(BLE_UUID_BOOT_MOUSE_INPUT_REPORT_CHAR, 0, 0);
  526. on_report_cccd_write(p_hids, &char_id, p_ble_evt);
  527. }
  528. else if (p_evt_write->handle == p_hids->boot_mouse_inp_rep_handles.value_handle)
  529. {
  530. char_id = make_char_id(BLE_UUID_BOOT_MOUSE_INPUT_REPORT_CHAR, 0, 0);
  531. rep_data_offset += BOOT_KB_INPUT_REPORT_MAX_SIZE + BOOT_KB_OUTPUT_REPORT_MAX_SIZE;
  532. max_rep_len = BOOT_MOUSE_INPUT_REPORT_MAX_SIZE;
  533. on_report_value_write(p_hids, p_ble_evt, &char_id, rep_data_offset, max_rep_len);
  534. }
  535. else if (inp_rep_cccd_identify(p_hids, p_evt_write->handle, &char_id))
  536. {
  537. on_report_cccd_write(p_hids, &char_id, p_ble_evt);
  538. }
  539. else if (rep_value_identify(p_hids,
  540. p_evt_write->handle,
  541. &char_id,
  542. &rep_data_offset,
  543. &max_rep_len))
  544. {
  545. on_report_value_write(p_hids, p_ble_evt, &char_id, rep_data_offset, max_rep_len);
  546. }
  547. else
  548. {
  549. // No implementation needed.
  550. }
  551. }
  552. /**@brief Read/write authorize request event handler.
  553. *
  554. * @param[in] p_hids HID Service structure.
  555. * @param[in] p_ble_evt Event received from the BLE stack.
  556. */
  557. static void on_rw_authorize_request(ble_hids_t * p_hids, ble_evt_t const * p_ble_evt)
  558. {
  559. ble_gatts_evt_rw_authorize_request_t const * evt_rw_auth =
  560. &p_ble_evt->evt.gatts_evt.params.authorize_request;
  561. ble_hids_char_id_t char_id;
  562. uint16_t rep_data_offset = sizeof(ble_hids_client_context_t);
  563. uint16_t max_rep_len = 0;
  564. if (evt_rw_auth->type != BLE_GATTS_AUTHORIZE_TYPE_READ)
  565. {
  566. // Unexpected operation
  567. return;
  568. }
  569. /* Update SD GATTS values of appropriate host before SD sends the Read Response */
  570. if (evt_rw_auth->request.read.handle == p_hids->protocol_mode_handles.value_handle)
  571. {
  572. on_protocol_mode_read_auth(p_hids, p_ble_evt);
  573. }
  574. else if (evt_rw_auth->request.read.handle == p_hids->boot_kb_inp_rep_handles.value_handle)
  575. {
  576. char_id = make_char_id(BLE_UUID_BOOT_KEYBOARD_INPUT_REPORT_CHAR, 0, 0);
  577. max_rep_len = BOOT_KB_INPUT_REPORT_MAX_SIZE;
  578. on_report_value_read_auth(p_hids, &char_id, p_ble_evt, rep_data_offset, max_rep_len);
  579. }
  580. else if (evt_rw_auth->request.read.handle == p_hids->boot_kb_outp_rep_handles.value_handle)
  581. {
  582. char_id = make_char_id(BLE_UUID_BOOT_KEYBOARD_OUTPUT_REPORT_CHAR, 0, 0);
  583. rep_data_offset += BOOT_KB_INPUT_REPORT_MAX_SIZE;
  584. max_rep_len = BOOT_KB_OUTPUT_REPORT_MAX_SIZE;
  585. on_report_value_read_auth(p_hids, &char_id, p_ble_evt, rep_data_offset, max_rep_len);
  586. }
  587. else if (evt_rw_auth->request.read.handle == p_hids->boot_mouse_inp_rep_handles.value_handle)
  588. {
  589. char_id = make_char_id(BLE_UUID_BOOT_MOUSE_INPUT_REPORT_CHAR, 0, 0);
  590. rep_data_offset += BOOT_KB_INPUT_REPORT_MAX_SIZE + BOOT_KB_OUTPUT_REPORT_MAX_SIZE;
  591. max_rep_len = BOOT_MOUSE_INPUT_REPORT_MAX_SIZE;
  592. on_report_value_read_auth(p_hids, &char_id, p_ble_evt, rep_data_offset, max_rep_len);
  593. }
  594. else if (rep_value_identify(p_hids,
  595. evt_rw_auth->request.read.handle,
  596. &char_id,
  597. &rep_data_offset,
  598. &max_rep_len))
  599. {
  600. on_report_value_read_auth(p_hids, &char_id, p_ble_evt, rep_data_offset, max_rep_len);
  601. }
  602. else
  603. {
  604. // Do nothing.
  605. }
  606. }
  607. void ble_hids_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  608. {
  609. ble_hids_t * p_hids = (ble_hids_t *) p_context;
  610. switch (p_ble_evt->header.evt_id)
  611. {
  612. case BLE_GAP_EVT_CONNECTED:
  613. on_connect(p_hids, p_ble_evt);
  614. break;
  615. case BLE_GATTS_EVT_WRITE:
  616. on_write(p_hids, p_ble_evt);
  617. break;
  618. case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
  619. on_rw_authorize_request(p_hids, p_ble_evt);
  620. break;
  621. default:
  622. // No implementation needed.
  623. break;
  624. }
  625. }
  626. /**@brief Function for adding Protocol Mode characteristics.
  627. *
  628. * @param[in] p_hids HID Service structure.
  629. * @param[in] read_access Security requirement for reading characteristic value.
  630. * @param[in] write_access Security requirement for writing characteristic value.
  631. *
  632. * @return NRF_SUCCESS on success, otherwise an error code.
  633. */
  634. static uint32_t protocol_mode_char_add(ble_hids_t * p_hids,
  635. security_req_t read_access,
  636. security_req_t write_access)
  637. {
  638. ble_add_char_params_t add_char_params;
  639. uint8_t initial_protocol_mode;
  640. initial_protocol_mode = DEFAULT_PROTOCOL_MODE;
  641. memset(&add_char_params, 0, sizeof(add_char_params));
  642. add_char_params.uuid = BLE_UUID_PROTOCOL_MODE_CHAR;
  643. add_char_params.max_len = sizeof(uint8_t);
  644. add_char_params.init_len = sizeof(uint8_t);
  645. add_char_params.p_init_value = &initial_protocol_mode;
  646. add_char_params.char_props.read = 1;
  647. add_char_params.char_props.write_wo_resp = 1;
  648. add_char_params.read_access = read_access;
  649. add_char_params.write_access = write_access;
  650. add_char_params.is_defered_read = true;
  651. return characteristic_add(p_hids->service_handle,
  652. &add_char_params,
  653. &p_hids->protocol_mode_handles);
  654. }
  655. /**@brief Function for adding report characteristics.
  656. *
  657. * @param[in] p_hids HID Service structure.
  658. * @param[in] p_properties Report characteristic properties.
  659. * @param[in] max_len Maximum length of report value.
  660. * @param[in] p_rep_ref Report Reference descriptor.
  661. * @param[in] p_char_sec Characteristic security settings.
  662. * @param[out] p_rep_char Handles of new characteristic.
  663. *
  664. * @return NRF_SUCCESS on success, otherwise an error code.
  665. */
  666. static uint32_t rep_char_add(ble_hids_t * p_hids,
  667. ble_gatt_char_props_t * p_properties,
  668. uint16_t max_len,
  669. ble_srv_report_ref_t const * p_rep_ref,
  670. ble_hids_char_sec_t const * const p_char_sec,
  671. ble_hids_rep_char_t * p_rep_char)
  672. {
  673. uint32_t err_code;
  674. ble_add_char_params_t add_char_params;
  675. ble_add_descr_params_t add_descr_params;
  676. uint8_t encoded_rep_ref[BLE_SRV_ENCODED_REPORT_REF_LEN];
  677. memset(&add_char_params, 0, sizeof(add_char_params));
  678. add_char_params.uuid = BLE_UUID_REPORT_CHAR;
  679. add_char_params.max_len = max_len;
  680. add_char_params.char_props = *p_properties;
  681. add_char_params.read_access = p_char_sec->rd;
  682. add_char_params.write_access = p_char_sec->wr;
  683. add_char_params.cccd_write_access = p_char_sec->cccd_wr;
  684. add_char_params.is_var_len = true;
  685. add_char_params.is_defered_read = true;
  686. err_code = characteristic_add(p_hids->service_handle,
  687. &add_char_params,
  688. &p_rep_char->char_handles);
  689. if (err_code != NRF_SUCCESS)
  690. {
  691. return err_code;
  692. }
  693. // Add Report Reference descriptor
  694. memset(&add_descr_params, 0, sizeof(add_descr_params));
  695. add_descr_params.uuid = BLE_UUID_REPORT_REF_DESCR;
  696. add_descr_params.read_access = p_char_sec->rd;
  697. add_descr_params.write_access = p_char_sec->wr;
  698. add_descr_params.init_len = ble_srv_report_ref_encode(encoded_rep_ref, p_rep_ref);
  699. add_descr_params.max_len = add_descr_params.init_len;
  700. add_descr_params.p_value = encoded_rep_ref;
  701. return descriptor_add(p_rep_char->char_handles.value_handle,
  702. &add_descr_params,
  703. &p_rep_char->ref_handle);
  704. }
  705. /**@brief Function for adding Report Map characteristics.
  706. *
  707. * @param[in] p_hids HID Service structure.
  708. * @param[in] p_hids_init Service initialization structure.
  709. *
  710. * @return NRF_SUCCESS on success, otherwise an error code.
  711. */
  712. static uint32_t rep_map_char_add(ble_hids_t * p_hids, const ble_hids_init_t * p_hids_init)
  713. {
  714. uint32_t err_code;
  715. ble_add_char_params_t add_char_params;
  716. ble_add_descr_params_t add_descr_params;
  717. // Add Report Map characteristic
  718. memset(&add_char_params, 0, sizeof(add_char_params));
  719. add_char_params.uuid = BLE_UUID_REPORT_MAP_CHAR;
  720. add_char_params.max_len = p_hids_init->rep_map.data_len;
  721. add_char_params.init_len = p_hids_init->rep_map.data_len;
  722. add_char_params.p_init_value = p_hids_init->rep_map.p_data;
  723. add_char_params.char_props.read = 1;
  724. add_char_params.read_access = p_hids_init->rep_map.rd_sec;
  725. add_char_params.is_var_len = true;
  726. err_code = characteristic_add(p_hids->service_handle,
  727. &add_char_params,
  728. &p_hids->rep_map_handles);
  729. if (err_code != NRF_SUCCESS)
  730. {
  731. return err_code;
  732. }
  733. if (p_hids_init->rep_map.ext_rep_ref_num != 0 && p_hids_init->rep_map.p_ext_rep_ref == NULL)
  734. {
  735. return NRF_ERROR_INVALID_PARAM;
  736. }
  737. for (int i = 0; i < p_hids_init->rep_map.ext_rep_ref_num; ++i)
  738. {
  739. uint8_t encoded_rep_ref[sizeof(ble_uuid128_t)];
  740. uint8_t encoded_rep_ref_len;
  741. // Add External Report Reference descriptor
  742. err_code = sd_ble_uuid_encode(&p_hids_init->rep_map.p_ext_rep_ref[i],
  743. &encoded_rep_ref_len,
  744. encoded_rep_ref);
  745. if (err_code != NRF_SUCCESS)
  746. {
  747. return err_code;
  748. }
  749. memset(&add_descr_params, 0, sizeof(add_descr_params));
  750. add_descr_params.uuid = BLE_UUID_EXTERNAL_REPORT_REF_DESCR;
  751. add_descr_params.read_access = p_hids_init->rep_map.rd_sec;
  752. add_descr_params.init_len = encoded_rep_ref_len;
  753. add_descr_params.max_len = add_descr_params.init_len;
  754. add_descr_params.p_value = encoded_rep_ref;
  755. err_code = descriptor_add(p_hids->rep_map_handles.value_handle,
  756. &add_descr_params,
  757. &p_hids->rep_map_ext_rep_ref_handle);
  758. if (err_code != NRF_SUCCESS)
  759. {
  760. return err_code;
  761. }
  762. }
  763. return NRF_SUCCESS;
  764. }
  765. /**@brief Function for adding Input Report characteristics.
  766. *
  767. * @param[in] p_hids HID Service structure.
  768. * @param[in] uuid UUID of report characteristic to be added.
  769. * @param[in] max_data_len Maximum length of report value.
  770. * @param[in] p_char_sec Characteristic security settings.
  771. * @param[out] p_char_handles Handles of new characteristic.
  772. *
  773. * @return NRF_SUCCESS on success, otherwise an error code.
  774. */
  775. static uint32_t boot_inp_rep_char_add(ble_hids_t * p_hids,
  776. uint16_t uuid,
  777. uint16_t max_data_len,
  778. ble_hids_char_sec_t const * const p_char_sec,
  779. ble_gatts_char_handles_t * p_char_handles)
  780. {
  781. ble_add_char_params_t add_char_params;
  782. memset(&add_char_params, 0, sizeof(add_char_params));
  783. add_char_params.uuid = uuid;
  784. add_char_params.max_len = max_data_len;
  785. add_char_params.char_props.read = 1;
  786. add_char_params.char_props.write = (p_char_sec->wr != SEC_NO_ACCESS) ? 1 : 0;
  787. add_char_params.char_props.notify = 1;
  788. add_char_params.read_access = p_char_sec->rd;
  789. add_char_params.write_access = p_char_sec->wr;
  790. add_char_params.cccd_write_access = p_char_sec->cccd_wr;
  791. add_char_params.is_defered_read = true;
  792. return characteristic_add(p_hids->service_handle,
  793. &add_char_params,
  794. p_char_handles);
  795. }
  796. /**@brief Function for adding Boot Keyboard Output Report characteristics.
  797. *
  798. * @param[in] p_hids HID Service structure.
  799. * @param[in] p_hids_init Service initialization structure.
  800. *
  801. * @return NRF_SUCCESS on success, otherwise an error code.
  802. */
  803. static uint32_t boot_kb_outp_rep_char_add(ble_hids_t * p_hids, const ble_hids_init_t * p_hids_init)
  804. {
  805. ble_add_char_params_t add_char_params;
  806. memset(&add_char_params, 0, sizeof(add_char_params));
  807. add_char_params.uuid = BLE_UUID_BOOT_KEYBOARD_OUTPUT_REPORT_CHAR;
  808. add_char_params.max_len = BOOT_KB_OUTPUT_REPORT_MAX_SIZE;
  809. add_char_params.char_props.read = 1;
  810. add_char_params.char_props.write = 1;
  811. add_char_params.char_props.write_wo_resp = 1;
  812. add_char_params.read_access = p_hids_init->boot_kb_outp_rep_sec.rd;
  813. add_char_params.write_access = p_hids_init->boot_kb_outp_rep_sec.wr;
  814. add_char_params.is_defered_read = true;
  815. return characteristic_add(p_hids->service_handle,
  816. &add_char_params,
  817. &p_hids->boot_kb_outp_rep_handles);
  818. }
  819. /**@brief Function for encoding a HID Information characteristic value.
  820. *
  821. * @param[out] p_encoded_buffer Buffer where the encoded data will be written.
  822. * @param[in] p_hid_information Measurement to be encoded.
  823. *
  824. * @return Size of encoded data.
  825. */
  826. static uint8_t encode_hid_information(uint8_t * p_encoded_buffer,
  827. const ble_hids_hid_information_t * p_hid_information)
  828. {
  829. uint8_t len = uint16_encode(p_hid_information->bcd_hid, p_encoded_buffer);
  830. p_encoded_buffer[len++] = p_hid_information->b_country_code;
  831. p_encoded_buffer[len++] = p_hid_information->flags;
  832. APP_ERROR_CHECK_BOOL(len == ENCODED_HID_INFORMATION_LEN);
  833. return len;
  834. }
  835. /**@brief Function for adding HID Information characteristics.
  836. *
  837. * @param[in] p_hids HID Service structure.
  838. * @param[in] p_hids_init Service initialization structure.
  839. *
  840. * @return NRF_SUCCESS on success, otherwise an error code.
  841. */
  842. static uint32_t hid_information_char_add(ble_hids_t * p_hids, const ble_hids_init_t * p_hids_init)
  843. {
  844. uint8_t encoded_hid_information[ENCODED_HID_INFORMATION_LEN];
  845. uint8_t hid_info_len;
  846. ble_add_char_params_t add_char_params;
  847. hid_info_len = encode_hid_information(encoded_hid_information, &p_hids_init->hid_information);
  848. memset(&add_char_params, 0, sizeof(add_char_params));
  849. add_char_params.uuid = BLE_UUID_HID_INFORMATION_CHAR;
  850. add_char_params.max_len = hid_info_len;
  851. add_char_params.char_props.read = 1;
  852. add_char_params.read_access = p_hids_init->hid_information.rd_sec;
  853. add_char_params.init_len = hid_info_len;
  854. add_char_params.p_init_value = encoded_hid_information;
  855. return characteristic_add(p_hids->service_handle,
  856. &add_char_params,
  857. &p_hids->hid_information_handles);
  858. }
  859. /**@brief Function for adding HID Control Point characteristics.
  860. *
  861. * @param[in] p_hids HID Service structure.
  862. * @param[in] write_access Security requirement for writing characteristic value.
  863. *
  864. * @return NRF_SUCCESS on success, otherwise an error code.
  865. */
  866. static uint32_t hid_control_point_char_add(ble_hids_t * p_hids, security_req_t write_access)
  867. {
  868. uint8_t initial_hid_control_point;
  869. ble_add_char_params_t add_char_params;
  870. initial_hid_control_point = INITIAL_VALUE_HID_CONTROL_POINT;
  871. memset(&add_char_params, 0, sizeof(add_char_params));
  872. add_char_params.uuid = BLE_UUID_HID_CONTROL_POINT_CHAR;
  873. add_char_params.max_len = sizeof(uint8_t);
  874. add_char_params.char_props.write_wo_resp = 1;
  875. add_char_params.write_access = write_access;
  876. add_char_params.init_len = sizeof(uint8_t);
  877. add_char_params.p_init_value = &initial_hid_control_point;
  878. return characteristic_add(p_hids->service_handle,
  879. &add_char_params,
  880. &p_hids->hid_control_point_handles);
  881. }
  882. /**@brief Function for adding input report characteristics.
  883. *
  884. * @param[in] p_hids HID Service structure.
  885. * @param[in] p_hids_init Service initialization structure.
  886. *
  887. * @return NRF_SUCCESS on success, otherwise an error code.
  888. */
  889. static uint32_t inp_rep_characteristics_add(ble_hids_t * p_hids,
  890. const ble_hids_init_t * p_hids_init)
  891. {
  892. if ((p_hids_init->inp_rep_count != 0) && (p_hids_init->p_inp_rep_array != NULL))
  893. {
  894. uint8_t i;
  895. for (i = 0; i < p_hids_init->inp_rep_count; i++)
  896. {
  897. uint32_t err_code;
  898. ble_hids_inp_rep_init_t const * p_rep_init = &p_hids_init->p_inp_rep_array[i];
  899. ble_gatt_char_props_t properties;
  900. memset(&properties, 0, sizeof(properties));
  901. properties.read = true;
  902. properties.write = (p_rep_init->sec.wr != SEC_NO_ACCESS) ? 1 : 0;
  903. properties.notify = true;
  904. err_code = rep_char_add(p_hids,
  905. &properties,
  906. p_rep_init->max_len,
  907. &p_rep_init->rep_ref,
  908. &p_rep_init->sec,
  909. &p_hids->inp_rep_array[i]);
  910. if (err_code != NRF_SUCCESS)
  911. {
  912. return err_code;
  913. }
  914. }
  915. }
  916. return NRF_SUCCESS;
  917. }
  918. /**@brief Function for adding output report characteristics.
  919. *
  920. * @param[in] p_hids HID Service structure.
  921. * @param[in] p_hids_init Service initialization structure.
  922. *
  923. * @return NRF_SUCCESS on success, otherwise an error code.
  924. */
  925. static uint32_t outp_rep_characteristics_add(ble_hids_t * p_hids,
  926. const ble_hids_init_t * p_hids_init)
  927. {
  928. if ((p_hids_init->outp_rep_count != 0) && (p_hids_init->p_outp_rep_array != NULL))
  929. {
  930. uint8_t i;
  931. for (i = 0; i < p_hids_init->outp_rep_count; i++)
  932. {
  933. uint32_t err_code;
  934. ble_hids_outp_rep_init_t const * p_rep_init = &p_hids_init->p_outp_rep_array[i];
  935. ble_gatt_char_props_t properties;
  936. memset(&properties, 0, sizeof(properties));
  937. properties.read = true;
  938. properties.write = true;
  939. properties.write_wo_resp = true;
  940. err_code = rep_char_add(p_hids,
  941. &properties,
  942. p_rep_init->max_len,
  943. &p_rep_init->rep_ref,
  944. &p_rep_init->sec,
  945. &p_hids->outp_rep_array[i]);
  946. if (err_code != NRF_SUCCESS)
  947. {
  948. return err_code;
  949. }
  950. }
  951. }
  952. return NRF_SUCCESS;
  953. }
  954. /**@brief Function for adding feature report characteristics.
  955. *
  956. * @param[in] p_hids HID Service structure.
  957. * @param[in] p_hids_init Service initialization structure.
  958. *
  959. * @return NRF_SUCCESS on success, otherwise an error code.
  960. */
  961. static uint32_t feature_rep_characteristics_add(ble_hids_t * p_hids,
  962. const ble_hids_init_t * p_hids_init)
  963. {
  964. if ((p_hids_init->feature_rep_count != 0) && (p_hids_init->p_feature_rep_array != NULL))
  965. {
  966. uint8_t i;
  967. for (i = 0; i < p_hids_init->feature_rep_count; i++)
  968. {
  969. uint32_t err_code;
  970. ble_hids_feature_rep_init_t const * p_rep_init = &p_hids_init->p_feature_rep_array[i];
  971. ble_gatt_char_props_t properties;
  972. memset(&properties, 0, sizeof(properties));
  973. properties.read = true;
  974. properties.write = true;
  975. err_code = rep_char_add(p_hids,
  976. &properties,
  977. p_rep_init->max_len,
  978. &p_rep_init->rep_ref,
  979. &p_rep_init->sec,
  980. &p_hids->feature_rep_array[i]);
  981. if (err_code != NRF_SUCCESS)
  982. {
  983. return err_code;
  984. }
  985. }
  986. }
  987. return NRF_SUCCESS;
  988. }
  989. /**@brief Function for adding included services.
  990. *
  991. * @param[in] p_hids HID Service structure.
  992. * @param[in] p_hids_init Service initialization structure.
  993. *
  994. * @return NRF_SUCCESS on success, otherwise an error code.
  995. */
  996. static uint32_t includes_add(ble_hids_t * p_hids, const ble_hids_init_t * p_hids_init)
  997. {
  998. uint32_t err_code;
  999. uint8_t i;
  1000. uint16_t unused_include_handle;
  1001. for (i = 0; i < p_hids_init->included_services_count; i++)
  1002. {
  1003. err_code = sd_ble_gatts_include_add(p_hids->service_handle,
  1004. p_hids_init->p_included_services_array[i],
  1005. &unused_include_handle);
  1006. if (err_code != NRF_SUCCESS)
  1007. {
  1008. return err_code;
  1009. }
  1010. }
  1011. return NRF_SUCCESS;
  1012. }
  1013. uint32_t ble_hids_init(ble_hids_t * p_hids, const ble_hids_init_t * p_hids_init)
  1014. {
  1015. VERIFY_PARAM_NOT_NULL(p_hids);
  1016. VERIFY_PARAM_NOT_NULL(p_hids_init);
  1017. uint32_t err_code;
  1018. ble_uuid_t ble_uuid;
  1019. if ((p_hids_init->inp_rep_count > BLE_HIDS_MAX_INPUT_REP) ||
  1020. (p_hids_init->outp_rep_count > BLE_HIDS_MAX_OUTPUT_REP) ||
  1021. (p_hids_init->feature_rep_count > BLE_HIDS_MAX_FEATURE_REP)
  1022. )
  1023. {
  1024. return NRF_ERROR_INVALID_PARAM;
  1025. }
  1026. // Initialize service structure.
  1027. p_hids->evt_handler = p_hids_init->evt_handler;
  1028. p_hids->error_handler = p_hids_init->error_handler;
  1029. p_hids->inp_rep_count = p_hids_init->inp_rep_count;
  1030. p_hids->outp_rep_count = p_hids_init->outp_rep_count;
  1031. p_hids->feature_rep_count = p_hids_init->feature_rep_count;
  1032. // Add service.
  1033. BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_HUMAN_INTERFACE_DEVICE_SERVICE);
  1034. err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
  1035. &ble_uuid,
  1036. &p_hids->service_handle);
  1037. if (err_code != NRF_SUCCESS)
  1038. {
  1039. return err_code;
  1040. }
  1041. // Add includes.
  1042. err_code = includes_add(p_hids, p_hids_init);
  1043. if (err_code != NRF_SUCCESS)
  1044. {
  1045. return err_code;
  1046. }
  1047. if (p_hids_init->is_kb || p_hids_init->is_mouse)
  1048. {
  1049. // Add Protocol Mode characteristic.
  1050. err_code = protocol_mode_char_add(p_hids,
  1051. p_hids_init->protocol_mode_rd_sec,
  1052. p_hids_init->protocol_mode_wr_sec);
  1053. if (err_code != NRF_SUCCESS)
  1054. {
  1055. return err_code;
  1056. }
  1057. }
  1058. // Add Input Report characteristics (if any).
  1059. err_code = inp_rep_characteristics_add(p_hids, p_hids_init);
  1060. if (err_code != NRF_SUCCESS)
  1061. {
  1062. return err_code;
  1063. }
  1064. // Add Output Report characteristics (if any).
  1065. err_code = outp_rep_characteristics_add(p_hids, p_hids_init);
  1066. if (err_code != NRF_SUCCESS)
  1067. {
  1068. return err_code;
  1069. }
  1070. // Add Feature Report characteristic (if any).
  1071. err_code = feature_rep_characteristics_add(p_hids, p_hids_init);
  1072. if (err_code != NRF_SUCCESS)
  1073. {
  1074. return err_code;
  1075. }
  1076. // Add Report Map characteristic.
  1077. err_code = rep_map_char_add(p_hids, p_hids_init);
  1078. if (err_code != NRF_SUCCESS)
  1079. {
  1080. return err_code;
  1081. }
  1082. if (p_hids_init->is_kb)
  1083. {
  1084. // Add Boot Keyboard Input Report characteristic.
  1085. err_code = boot_inp_rep_char_add(p_hids,
  1086. BLE_UUID_BOOT_KEYBOARD_INPUT_REPORT_CHAR,
  1087. BOOT_KB_INPUT_REPORT_MAX_SIZE,
  1088. &p_hids_init->boot_kb_inp_rep_sec,
  1089. &p_hids->boot_kb_inp_rep_handles);
  1090. if (err_code != NRF_SUCCESS)
  1091. {
  1092. return err_code;
  1093. }
  1094. // Add Boot Keyboard Output Report characteristic.
  1095. err_code = boot_kb_outp_rep_char_add(p_hids, p_hids_init);
  1096. if (err_code != NRF_SUCCESS)
  1097. {
  1098. return err_code;
  1099. }
  1100. }
  1101. if (p_hids_init->is_mouse)
  1102. {
  1103. // Add Boot Mouse Input Report characteristic.
  1104. err_code = boot_inp_rep_char_add(p_hids,
  1105. BLE_UUID_BOOT_MOUSE_INPUT_REPORT_CHAR,
  1106. BOOT_MOUSE_INPUT_REPORT_MAX_SIZE,
  1107. &p_hids_init->boot_mouse_inp_rep_sec,
  1108. &p_hids->boot_mouse_inp_rep_handles);
  1109. if (err_code != NRF_SUCCESS)
  1110. {
  1111. return err_code;
  1112. }
  1113. }
  1114. // Add HID Information characteristic.
  1115. err_code = hid_information_char_add(p_hids, p_hids_init);
  1116. if (err_code != NRF_SUCCESS)
  1117. {
  1118. return err_code;
  1119. }
  1120. // Add HID Control Point characteristic.
  1121. err_code = hid_control_point_char_add(p_hids, p_hids_init->ctrl_point_wr_sec);
  1122. if (err_code != NRF_SUCCESS)
  1123. {
  1124. return err_code;
  1125. }
  1126. // Set pointers to user-defined report arrays
  1127. p_hids->p_inp_rep_init_array = p_hids_init->p_inp_rep_array;
  1128. p_hids->p_outp_rep_init_array = p_hids_init->p_outp_rep_array;
  1129. p_hids->p_feature_rep_init_array = p_hids_init->p_feature_rep_array;
  1130. return err_code;
  1131. }
  1132. uint32_t ble_hids_inp_rep_send(ble_hids_t * p_hids,
  1133. uint8_t rep_index,
  1134. uint16_t len,
  1135. uint8_t * p_data,
  1136. uint16_t conn_handle)
  1137. {
  1138. VERIFY_PARAM_NOT_NULL(p_hids);
  1139. VERIFY_PARAM_NOT_NULL(p_data);
  1140. uint32_t err_code;
  1141. if (rep_index < p_hids->inp_rep_count)
  1142. {
  1143. ble_hids_rep_char_t * p_rep_char = &p_hids->inp_rep_array[rep_index];
  1144. if (conn_handle != BLE_CONN_HANDLE_INVALID)
  1145. {
  1146. ble_gatts_hvx_params_t hvx_params;
  1147. uint8_t index = 0;
  1148. uint16_t hvx_len = len;
  1149. uint8_t * p_host_rep_data;
  1150. err_code = blcm_link_ctx_get(p_hids->p_link_ctx_storage,
  1151. conn_handle,
  1152. (void *) &p_host_rep_data);
  1153. VERIFY_SUCCESS(err_code);
  1154. p_host_rep_data += sizeof(ble_hids_client_context_t) + BOOT_KB_INPUT_REPORT_MAX_SIZE +
  1155. BOOT_KB_OUTPUT_REPORT_MAX_SIZE + BOOT_MOUSE_INPUT_REPORT_MAX_SIZE;
  1156. // Store the new report data in host's context
  1157. while (index < rep_index)
  1158. {
  1159. p_host_rep_data += p_hids->p_inp_rep_init_array[index].max_len;
  1160. ++index;
  1161. }
  1162. if (len <= p_hids->p_inp_rep_init_array[rep_index].max_len)
  1163. {
  1164. memcpy(p_host_rep_data, p_data, len);
  1165. }
  1166. else
  1167. {
  1168. return NRF_ERROR_DATA_SIZE;
  1169. }
  1170. // Notify host
  1171. memset(&hvx_params, 0, sizeof(hvx_params));
  1172. hvx_params.handle = p_rep_char->char_handles.value_handle;
  1173. hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
  1174. hvx_params.offset = 0;
  1175. hvx_params.p_len = &hvx_len;
  1176. hvx_params.p_data = p_data;
  1177. err_code = sd_ble_gatts_hvx(conn_handle, &hvx_params);
  1178. if ((err_code == NRF_SUCCESS) && (*hvx_params.p_len != len))
  1179. {
  1180. err_code = NRF_ERROR_DATA_SIZE;
  1181. }
  1182. }
  1183. else
  1184. {
  1185. err_code = NRF_ERROR_INVALID_STATE;
  1186. }
  1187. }
  1188. else
  1189. {
  1190. err_code = NRF_ERROR_INVALID_PARAM;
  1191. }
  1192. return err_code;
  1193. }
  1194. uint32_t ble_hids_boot_kb_inp_rep_send(ble_hids_t * p_hids,
  1195. uint16_t len,
  1196. uint8_t * p_data,
  1197. uint16_t conn_handle)
  1198. {
  1199. VERIFY_PARAM_NOT_NULL(p_hids);
  1200. VERIFY_PARAM_NOT_NULL(p_data);
  1201. uint32_t err_code;
  1202. if (conn_handle != BLE_CONN_HANDLE_INVALID)
  1203. {
  1204. ble_gatts_hvx_params_t hvx_params;
  1205. uint16_t hvx_len = len;
  1206. uint8_t * p_host_rep_data;
  1207. err_code = blcm_link_ctx_get(p_hids->p_link_ctx_storage,
  1208. conn_handle,
  1209. (void *) &p_host_rep_data);
  1210. VERIFY_SUCCESS(err_code);
  1211. p_host_rep_data += sizeof(ble_hids_client_context_t);
  1212. // Store the new value in the host's context
  1213. if (len <= BOOT_KB_INPUT_REPORT_MAX_SIZE)
  1214. {
  1215. memcpy(p_host_rep_data, p_data, len);
  1216. }
  1217. // Notify host
  1218. memset(&hvx_params, 0, sizeof(hvx_params));
  1219. hvx_params.handle = p_hids->boot_kb_inp_rep_handles.value_handle;
  1220. hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
  1221. hvx_params.offset = 0;
  1222. hvx_params.p_len = &hvx_len;
  1223. hvx_params.p_data = p_data;
  1224. err_code = sd_ble_gatts_hvx(conn_handle, &hvx_params);
  1225. if ((err_code == NRF_SUCCESS) && (*hvx_params.p_len != len))
  1226. {
  1227. err_code = NRF_ERROR_DATA_SIZE;
  1228. }
  1229. }
  1230. else
  1231. {
  1232. err_code = NRF_ERROR_INVALID_STATE;
  1233. }
  1234. return err_code;
  1235. }
  1236. uint32_t ble_hids_boot_mouse_inp_rep_send(ble_hids_t * p_hids,
  1237. uint8_t buttons,
  1238. int8_t x_delta,
  1239. int8_t y_delta,
  1240. uint16_t optional_data_len,
  1241. uint8_t * p_optional_data,
  1242. uint16_t conn_handle)
  1243. {
  1244. VERIFY_PARAM_NOT_NULL(p_hids);
  1245. uint32_t err_code;
  1246. if (conn_handle != BLE_CONN_HANDLE_INVALID)
  1247. {
  1248. uint16_t hvx_len = BOOT_MOUSE_INPUT_REPORT_MIN_SIZE + optional_data_len;
  1249. if (hvx_len <= BOOT_MOUSE_INPUT_REPORT_MAX_SIZE)
  1250. {
  1251. uint8_t buffer[BOOT_MOUSE_INPUT_REPORT_MAX_SIZE];
  1252. ble_gatts_hvx_params_t hvx_params;
  1253. uint8_t * p_host_rep_data;
  1254. err_code = blcm_link_ctx_get(p_hids->p_link_ctx_storage,
  1255. conn_handle,
  1256. (void *) &p_host_rep_data);
  1257. VERIFY_SUCCESS(err_code);
  1258. p_host_rep_data += sizeof(ble_hids_client_context_t) + BOOT_KB_INPUT_REPORT_MAX_SIZE +
  1259. BOOT_KB_OUTPUT_REPORT_MAX_SIZE;
  1260. APP_ERROR_CHECK_BOOL(BOOT_MOUSE_INPUT_REPORT_MIN_SIZE == 3);
  1261. // Build buffer
  1262. buffer[0] = buttons;
  1263. buffer[1] = (uint8_t)x_delta;
  1264. buffer[2] = (uint8_t)y_delta;
  1265. if (optional_data_len > 0)
  1266. {
  1267. memcpy(&buffer[3], p_optional_data, optional_data_len);
  1268. }
  1269. // Store the new value in the host's context
  1270. memcpy(p_host_rep_data, buffer, hvx_len);
  1271. // Pass buffer to stack
  1272. memset(&hvx_params, 0, sizeof(hvx_params));
  1273. hvx_params.handle = p_hids->boot_mouse_inp_rep_handles.value_handle;
  1274. hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
  1275. hvx_params.offset = 0;
  1276. hvx_params.p_len = &hvx_len;
  1277. hvx_params.p_data = buffer;
  1278. err_code = sd_ble_gatts_hvx(conn_handle, &hvx_params);
  1279. if ((err_code == NRF_SUCCESS) &&
  1280. (*hvx_params.p_len != BOOT_MOUSE_INPUT_REPORT_MIN_SIZE + optional_data_len)
  1281. )
  1282. {
  1283. err_code = NRF_ERROR_DATA_SIZE;
  1284. }
  1285. }
  1286. else
  1287. {
  1288. err_code = NRF_ERROR_DATA_SIZE;
  1289. }
  1290. }
  1291. else
  1292. {
  1293. err_code = NRF_ERROR_INVALID_STATE;
  1294. }
  1295. return err_code;
  1296. }
  1297. uint32_t ble_hids_outp_rep_get(ble_hids_t * p_hids,
  1298. uint8_t rep_index,
  1299. uint16_t len,
  1300. uint8_t offset,
  1301. uint16_t conn_handle,
  1302. uint8_t * p_outp_rep)
  1303. {
  1304. VERIFY_PARAM_NOT_NULL(p_hids);
  1305. VERIFY_PARAM_NOT_NULL(p_outp_rep);
  1306. ret_code_t err_code;
  1307. uint8_t * p_rep_data;
  1308. uint8_t index;
  1309. if (rep_index >= p_hids->outp_rep_count)
  1310. {
  1311. return NRF_ERROR_INVALID_PARAM;
  1312. }
  1313. err_code = blcm_link_ctx_get(p_hids->p_link_ctx_storage,
  1314. conn_handle,
  1315. (void *) &p_rep_data);
  1316. VERIFY_SUCCESS(err_code);
  1317. p_rep_data += sizeof(ble_hids_client_context_t) + BOOT_KB_INPUT_REPORT_MAX_SIZE +
  1318. BOOT_KB_OUTPUT_REPORT_MAX_SIZE + BOOT_MOUSE_INPUT_REPORT_MAX_SIZE;
  1319. for (index = 0; index < p_hids->inp_rep_count; index++)
  1320. {
  1321. p_rep_data += p_hids->p_inp_rep_init_array[index].max_len;
  1322. }
  1323. for (index = 0; index < rep_index; index++)
  1324. {
  1325. p_rep_data += p_hids->p_outp_rep_init_array[index].max_len;
  1326. }
  1327. // Copy the requested output report data
  1328. if (len + offset <= p_hids->p_outp_rep_init_array[rep_index].max_len)
  1329. {
  1330. memcpy(p_outp_rep, p_rep_data + offset, len);
  1331. }
  1332. else
  1333. {
  1334. return NRF_ERROR_INVALID_LENGTH;
  1335. }
  1336. return NRF_SUCCESS;
  1337. }
  1338. /**
  1339. @}
  1340. */
  1341. #endif // NRF_MODULE_ENABLED(BLE_HIDS)