main.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. /**
  2. * Copyright (c) 2014 - 2021, 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. /** @file
  41. *
  42. * @defgroup ble_sdk_app_hids_mouse_main main.c
  43. * @{
  44. * @ingroup ble_sdk_app_hids_mouse
  45. * @brief HID Mouse Sample Application main file.
  46. *
  47. * This file contains is the source code for a sample application using the HID, Battery and Device
  48. * Information Service for implementing a simple mouse functionality. This application uses the
  49. * @ref app_scheduler.
  50. *
  51. * Also it would accept pairing requests from any peer device. This implementation of the
  52. * application will not know whether a connected central is a known device or not.
  53. */
  54. #include <stdint.h>
  55. #include <string.h>
  56. #include "nordic_common.h"
  57. #include "nrf.h"
  58. #include "nrf_sdm.h"
  59. #include "app_error.h"
  60. #include "ble.h"
  61. #include "ble_err.h"
  62. #include "ble_hci.h"
  63. #include "ble_srv_common.h"
  64. #include "ble_advdata.h"
  65. #include "ble_hids.h"
  66. #include "ble_bas.h"
  67. #include "ble_dis.h"
  68. #include "ble_conn_params.h"
  69. #include "sensorsim.h"
  70. #include "bsp_btn_ble.h"
  71. #include "app_scheduler.h"
  72. #include "nrf_sdh.h"
  73. #include "nrf_sdh_soc.h"
  74. #include "nrf_sdh_ble.h"
  75. #include "app_timer.h"
  76. #include "peer_manager.h"
  77. #include "ble_advertising.h"
  78. #include "fds.h"
  79. #include "ble_conn_state.h"
  80. #include "nrf_ble_gatt.h"
  81. #include "nrf_ble_qwr.h"
  82. #include "nrf_pwr_mgmt.h"
  83. #include "peer_manager_handler.h"
  84. #include "nrf_log.h"
  85. #include "nrf_log_ctrl.h"
  86. #include "nrf_log_default_backends.h"
  87. #define DEVICE_NAME "nRF5_Mouse" /**< Name of device. Will be included in the advertising data. */
  88. #define MANUFACTURER_NAME "NordicSemiconductor" /**< Manufacturer. Will be passed to Device Information Service. */
  89. #define APP_BLE_OBSERVER_PRIO 3 /**< Application's BLE observer priority. You shouldn't need to modify this value. */
  90. #define APP_BLE_CONN_CFG_TAG 1 /**< A tag identifying the SoftDevice BLE configuration. */
  91. #define BATTERY_LEVEL_MEAS_INTERVAL APP_TIMER_TICKS(2000) /**< Battery level measurement interval (ticks). */
  92. #define MIN_BATTERY_LEVEL 81 /**< Minimum simulated battery level. */
  93. #define MAX_BATTERY_LEVEL 100 /**< Maximum simulated battery level. */
  94. #define BATTERY_LEVEL_INCREMENT 1 /**< Increment between each simulated battery level measurement. */
  95. #define PNP_ID_VENDOR_ID_SOURCE 0x02 /**< Vendor ID Source. */
  96. #define PNP_ID_VENDOR_ID 0x1915 /**< Vendor ID. */
  97. #define PNP_ID_PRODUCT_ID 0xEEEE /**< Product ID. */
  98. #define PNP_ID_PRODUCT_VERSION 0x0001 /**< Product Version. */
  99. /*lint -emacro(524, MIN_CONN_INTERVAL) // Loss of precision */
  100. #define MIN_CONN_INTERVAL MSEC_TO_UNITS(7.5, UNIT_1_25_MS) /**< Minimum connection interval (7.5 ms). */
  101. #define MAX_CONN_INTERVAL MSEC_TO_UNITS(15, UNIT_1_25_MS) /**< Maximum connection interval (15 ms). */
  102. #define SLAVE_LATENCY 20 /**< Slave latency. */
  103. #define CONN_SUP_TIMEOUT MSEC_TO_UNITS(3000, UNIT_10_MS) /**< Connection supervisory timeout (3000 ms). */
  104. #define FIRST_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(5000) /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (5 seconds). */
  105. #define NEXT_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(30000) /**< Time between each call to sd_ble_gap_conn_param_update after the first call (30 seconds). */
  106. #define MAX_CONN_PARAM_UPDATE_COUNT 3 /**< Number of attempts before giving up the connection parameter negotiation. */
  107. #define SEC_PARAM_BOND 1 /**< Perform bonding. */
  108. #define SEC_PARAM_MITM 0 /**< Man In The Middle protection not required. */
  109. #define SEC_PARAM_LESC 0 /**< LE Secure Connections not enabled. */
  110. #define SEC_PARAM_KEYPRESS 0 /**< Keypress notifications not enabled. */
  111. #define SEC_PARAM_IO_CAPABILITIES BLE_GAP_IO_CAPS_NONE /**< No I/O capabilities. */
  112. #define SEC_PARAM_OOB 0 /**< Out Of Band data not available. */
  113. #define SEC_PARAM_MIN_KEY_SIZE 7 /**< Minimum encryption key size. */
  114. #define SEC_PARAM_MAX_KEY_SIZE 16 /**< Maximum encryption key size. */
  115. #define SWIFT_PAIR_SUPPORTED 1 /**< Swift Pair feature is supported. */
  116. #if SWIFT_PAIR_SUPPORTED == 1
  117. #define MICROSOFT_VENDOR_ID 0x0006 /**< Microsoft Vendor ID.*/
  118. #define MICROSOFT_BEACON_ID 0x03 /**< Microsoft Beacon ID, used to indicate that Swift Pair feature is supported. */
  119. #define MICROSOFT_BEACON_SUB_SCENARIO 0x00 /**< Microsoft Beacon Sub Scenario, used to indicate how the peripheral will pair using Swift Pair feature. */
  120. #define RESERVED_RSSI_BYTE 0x80 /**< Reserved RSSI byte, used to maintain forwards and backwards compatibility. */
  121. #endif
  122. #define MOVEMENT_SPEED 5 /**< Number of pixels by which the cursor is moved each time a button is pushed. */
  123. #define INPUT_REPORT_COUNT 3 /**< Number of input reports in this application. */
  124. #define INPUT_REP_BUTTONS_LEN 3 /**< Length of Mouse Input Report containing button data. */
  125. #define INPUT_REP_MOVEMENT_LEN 3 /**< Length of Mouse Input Report containing movement data. */
  126. #define INPUT_REP_MEDIA_PLAYER_LEN 1 /**< Length of Mouse Input Report containing media player data. */
  127. #define INPUT_REP_BUTTONS_INDEX 0 /**< Index of Mouse Input Report containing button data. */
  128. #define INPUT_REP_MOVEMENT_INDEX 1 /**< Index of Mouse Input Report containing movement data. */
  129. #define INPUT_REP_MPLAYER_INDEX 2 /**< Index of Mouse Input Report containing media player data. */
  130. #define INPUT_REP_REF_BUTTONS_ID 1 /**< Id of reference to Mouse Input Report containing button data. */
  131. #define INPUT_REP_REF_MOVEMENT_ID 2 /**< Id of reference to Mouse Input Report containing movement data. */
  132. #define INPUT_REP_REF_MPLAYER_ID 3 /**< Id of reference to Mouse Input Report containing media player data. */
  133. #define BASE_USB_HID_SPEC_VERSION 0x0101 /**< Version number of base USB HID Specification implemented by this application. */
  134. #define SCHED_MAX_EVENT_DATA_SIZE APP_TIMER_SCHED_EVENT_DATA_SIZE /**< Maximum size of scheduler events. */
  135. #ifdef SVCALL_AS_NORMAL_FUNCTION
  136. #define SCHED_QUEUE_SIZE 20 /**< Maximum number of events in the scheduler queue. More is needed in case of Serialization. */
  137. #else
  138. #define SCHED_QUEUE_SIZE 10 /**< Maximum number of events in the scheduler queue. */
  139. #endif
  140. #define DEAD_BEEF 0xDEADBEEF /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */
  141. #define APP_ADV_FAST_INTERVAL 0x0028 /**< Fast advertising interval (in units of 0.625 ms. This value corresponds to 25 ms.). */
  142. #define APP_ADV_SLOW_INTERVAL 0x00A0 /**< Slow advertising interval (in units of 0.625 ms. This value corresponds to 100 ms.). */
  143. #define APP_ADV_FAST_DURATION 3000 /**< The advertising duration of fast advertising in units of 10 milliseconds. */
  144. #define APP_ADV_SLOW_DURATION 18000 /**< The advertising duration of slow advertising in units of 10 milliseconds. */
  145. APP_TIMER_DEF(m_battery_timer_id); /**< Battery timer. */
  146. BLE_BAS_DEF(m_bas); /**< Battery service instance. */
  147. BLE_HIDS_DEF(m_hids, /**< HID service instance. */
  148. NRF_SDH_BLE_TOTAL_LINK_COUNT,
  149. INPUT_REP_BUTTONS_LEN,
  150. INPUT_REP_MOVEMENT_LEN,
  151. INPUT_REP_MEDIA_PLAYER_LEN);
  152. NRF_BLE_GATT_DEF(m_gatt); /**< GATT module instance. */
  153. NRF_BLE_QWR_DEF(m_qwr); /**< Context for the Queued Write module.*/
  154. BLE_ADVERTISING_DEF(m_advertising); /**< Advertising module instance. */
  155. static bool m_in_boot_mode = false; /**< Current protocol mode. */
  156. static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; /**< Handle of the current connection. */
  157. static pm_peer_id_t m_peer_id; /**< Device reference handle to the current bonded central. */
  158. static sensorsim_cfg_t m_battery_sim_cfg; /**< Battery Level sensor simulator configuration. */
  159. static sensorsim_state_t m_battery_sim_state; /**< Battery Level sensor simulator state. */
  160. static ble_uuid_t m_adv_uuids[] = /**< Universally unique service identifiers. */
  161. {
  162. {BLE_UUID_HUMAN_INTERFACE_DEVICE_SERVICE, BLE_UUID_TYPE_BLE}
  163. };
  164. #if SWIFT_PAIR_SUPPORTED == 1
  165. static uint8_t m_sp_payload[] = /**< Payload of advertising data structure for Microsoft Swift Pair feature. */
  166. {
  167. MICROSOFT_BEACON_ID,
  168. MICROSOFT_BEACON_SUB_SCENARIO,
  169. RESERVED_RSSI_BYTE
  170. };
  171. static ble_advdata_manuf_data_t m_sp_manuf_advdata = /**< Advertising data structure for Microsoft Swift Pair feature. */
  172. {
  173. .company_identifier = MICROSOFT_VENDOR_ID,
  174. .data =
  175. {
  176. .size = sizeof(m_sp_payload),
  177. .p_data = &m_sp_payload[0]
  178. }
  179. };
  180. static ble_advdata_t m_sp_advdata;
  181. #endif
  182. static void on_hids_evt(ble_hids_t * p_hids, ble_hids_evt_t * p_evt);
  183. /**@brief Callback function for asserts in the SoftDevice.
  184. *
  185. * @details This function will be called in case of an assert in the SoftDevice.
  186. *
  187. * @warning This handler is an example only and does not fit a final product. You need to analyze
  188. * how your product is supposed to react in case of Assert.
  189. * @warning On assert from the SoftDevice, the system can only recover on reset.
  190. *
  191. * @param[in] line_num Line number of the failing ASSERT call.
  192. * @param[in] file_name File name of the failing ASSERT call.
  193. */
  194. void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
  195. {
  196. app_error_handler(DEAD_BEEF, line_num, p_file_name);
  197. }
  198. /**@brief Function for setting filtered whitelist.
  199. *
  200. * @param[in] skip Filter passed to @ref pm_peer_id_list.
  201. */
  202. static void whitelist_set(pm_peer_id_list_skip_t skip)
  203. {
  204. pm_peer_id_t peer_ids[BLE_GAP_WHITELIST_ADDR_MAX_COUNT];
  205. uint32_t peer_id_count = BLE_GAP_WHITELIST_ADDR_MAX_COUNT;
  206. ret_code_t err_code = pm_peer_id_list(peer_ids, &peer_id_count, PM_PEER_ID_INVALID, skip);
  207. APP_ERROR_CHECK(err_code);
  208. NRF_LOG_INFO("\tm_whitelist_peer_cnt %d, MAX_PEERS_WLIST %d",
  209. peer_id_count + 1,
  210. BLE_GAP_WHITELIST_ADDR_MAX_COUNT);
  211. err_code = pm_whitelist_set(peer_ids, peer_id_count);
  212. APP_ERROR_CHECK(err_code);
  213. }
  214. /**@brief Function for setting filtered device identities.
  215. *
  216. * @param[in] skip Filter passed to @ref pm_peer_id_list.
  217. */
  218. static void identities_set(pm_peer_id_list_skip_t skip)
  219. {
  220. pm_peer_id_t peer_ids[BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT];
  221. uint32_t peer_id_count = BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT;
  222. ret_code_t err_code = pm_peer_id_list(peer_ids, &peer_id_count, PM_PEER_ID_INVALID, skip);
  223. APP_ERROR_CHECK(err_code);
  224. err_code = pm_device_identities_list_set(peer_ids, peer_id_count);
  225. APP_ERROR_CHECK(err_code);
  226. }
  227. /**@brief Clear bond information from persistent storage.
  228. */
  229. static void delete_bonds(void)
  230. {
  231. ret_code_t err_code;
  232. NRF_LOG_INFO("Erase bonds!");
  233. err_code = pm_peers_delete();
  234. APP_ERROR_CHECK(err_code);
  235. }
  236. /**@brief Function for starting advertising.
  237. */
  238. static void advertising_start(bool erase_bonds)
  239. {
  240. if (erase_bonds == true)
  241. {
  242. delete_bonds();
  243. // Advertising is started by PM_EVT_PEERS_DELETE_SUCCEEDED event.
  244. }
  245. else
  246. {
  247. whitelist_set(PM_PEER_ID_LIST_SKIP_NO_ID_ADDR);
  248. ret_code_t ret = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
  249. APP_ERROR_CHECK(ret);
  250. }
  251. }
  252. /**@brief Function for handling Peer Manager events.
  253. *
  254. * @param[in] p_evt Peer Manager event.
  255. */
  256. static void pm_evt_handler(pm_evt_t const * p_evt)
  257. {
  258. pm_handler_on_pm_evt(p_evt);
  259. pm_handler_disconnect_on_sec_failure(p_evt);
  260. pm_handler_flash_clean(p_evt);
  261. switch (p_evt->evt_id)
  262. {
  263. case PM_EVT_CONN_SEC_SUCCEEDED:
  264. m_peer_id = p_evt->peer_id;
  265. break;
  266. case PM_EVT_PEERS_DELETE_SUCCEEDED:
  267. advertising_start(false);
  268. break;
  269. case PM_EVT_PEER_DATA_UPDATE_SUCCEEDED:
  270. if ( p_evt->params.peer_data_update_succeeded.flash_changed
  271. && (p_evt->params.peer_data_update_succeeded.data_id == PM_PEER_DATA_ID_BONDING))
  272. {
  273. NRF_LOG_INFO("New Bond, add the peer to the whitelist if possible");
  274. // Note: You should check on what kind of white list policy your application should use.
  275. whitelist_set(PM_PEER_ID_LIST_SKIP_NO_ID_ADDR);
  276. }
  277. break;
  278. default:
  279. break;
  280. }
  281. }
  282. /**@brief Function for handling Service errors.
  283. *
  284. * @details A pointer to this function will be passed to each service which may need to inform the
  285. * application about an error.
  286. *
  287. * @param[in] nrf_error Error code containing information about what went wrong.
  288. */
  289. static void service_error_handler(uint32_t nrf_error)
  290. {
  291. APP_ERROR_HANDLER(nrf_error);
  292. }
  293. /**@brief Function for handling advertising errors.
  294. *
  295. * @param[in] nrf_error Error code containing information about what went wrong.
  296. */
  297. static void ble_advertising_error_handler(uint32_t nrf_error)
  298. {
  299. APP_ERROR_HANDLER(nrf_error);
  300. }
  301. /**@brief Function for performing a battery measurement, and update the Battery Level characteristic in the Battery Service.
  302. */
  303. static void battery_level_update(void)
  304. {
  305. ret_code_t err_code;
  306. uint8_t battery_level;
  307. battery_level = (uint8_t)sensorsim_measure(&m_battery_sim_state, &m_battery_sim_cfg);
  308. err_code = ble_bas_battery_level_update(&m_bas, battery_level, BLE_CONN_HANDLE_ALL);
  309. if ((err_code != NRF_SUCCESS) &&
  310. (err_code != NRF_ERROR_BUSY) &&
  311. (err_code != NRF_ERROR_RESOURCES) &&
  312. (err_code != NRF_ERROR_FORBIDDEN) &&
  313. (err_code != NRF_ERROR_INVALID_STATE) &&
  314. (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
  315. )
  316. {
  317. APP_ERROR_HANDLER(err_code);
  318. }
  319. }
  320. /**@brief Function for handling the Battery measurement timer timeout.
  321. *
  322. * @details This function will be called each time the battery level measurement timer expires.
  323. *
  324. * @param[in] p_context Pointer used for passing some arbitrary information (context) from the
  325. * app_start_timer() call to the timeout handler.
  326. */
  327. static void battery_level_meas_timeout_handler(void * p_context)
  328. {
  329. UNUSED_PARAMETER(p_context);
  330. battery_level_update();
  331. }
  332. /**@brief Function for the Timer initialization.
  333. *
  334. * @details Initializes the timer module.
  335. */
  336. static void timers_init(void)
  337. {
  338. ret_code_t err_code;
  339. err_code = app_timer_init();
  340. APP_ERROR_CHECK(err_code);
  341. // Create battery timer.
  342. err_code = app_timer_create(&m_battery_timer_id,
  343. APP_TIMER_MODE_REPEATED,
  344. battery_level_meas_timeout_handler);
  345. APP_ERROR_CHECK(err_code);
  346. }
  347. /**@brief Function for the GAP initialization.
  348. *
  349. * @details This function sets up all the necessary GAP (Generic Access Profile) parameters of the
  350. * device including the device name, appearance, and the preferred connection parameters.
  351. */
  352. static void gap_params_init(void)
  353. {
  354. ret_code_t err_code;
  355. ble_gap_conn_params_t gap_conn_params;
  356. ble_gap_conn_sec_mode_t sec_mode;
  357. BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
  358. err_code = sd_ble_gap_device_name_set(&sec_mode,
  359. (const uint8_t *)DEVICE_NAME,
  360. strlen(DEVICE_NAME));
  361. APP_ERROR_CHECK(err_code);
  362. err_code = sd_ble_gap_appearance_set(BLE_APPEARANCE_HID_MOUSE);
  363. APP_ERROR_CHECK(err_code);
  364. memset(&gap_conn_params, 0, sizeof(gap_conn_params));
  365. gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
  366. gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
  367. gap_conn_params.slave_latency = SLAVE_LATENCY;
  368. gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT;
  369. err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
  370. APP_ERROR_CHECK(err_code);
  371. }
  372. /**@brief Function for initializing the GATT module.
  373. */
  374. static void gatt_init(void)
  375. {
  376. ret_code_t err_code = nrf_ble_gatt_init(&m_gatt, NULL);
  377. APP_ERROR_CHECK(err_code);
  378. }
  379. /**@brief Function for handling Queued Write Module errors.
  380. *
  381. * @details A pointer to this function will be passed to each service which may need to inform the
  382. * application about an error.
  383. *
  384. * @param[in] nrf_error Error code containing information about what went wrong.
  385. */
  386. static void nrf_qwr_error_handler(uint32_t nrf_error)
  387. {
  388. APP_ERROR_HANDLER(nrf_error);
  389. }
  390. /**@brief Function for initializing the Queued Write Module.
  391. */
  392. static void qwr_init(void)
  393. {
  394. ret_code_t err_code;
  395. nrf_ble_qwr_init_t qwr_init_obj = {0};
  396. qwr_init_obj.error_handler = nrf_qwr_error_handler;
  397. err_code = nrf_ble_qwr_init(&m_qwr, &qwr_init_obj);
  398. APP_ERROR_CHECK(err_code);
  399. }
  400. /**@brief Function for initializing Device Information Service.
  401. */
  402. static void dis_init(void)
  403. {
  404. ret_code_t err_code;
  405. ble_dis_init_t dis_init_obj;
  406. ble_dis_pnp_id_t pnp_id;
  407. pnp_id.vendor_id_source = PNP_ID_VENDOR_ID_SOURCE;
  408. pnp_id.vendor_id = PNP_ID_VENDOR_ID;
  409. pnp_id.product_id = PNP_ID_PRODUCT_ID;
  410. pnp_id.product_version = PNP_ID_PRODUCT_VERSION;
  411. memset(&dis_init_obj, 0, sizeof(dis_init_obj));
  412. ble_srv_ascii_to_utf8(&dis_init_obj.manufact_name_str, MANUFACTURER_NAME);
  413. dis_init_obj.p_pnp_id = &pnp_id;
  414. dis_init_obj.dis_char_rd_sec = SEC_JUST_WORKS;
  415. err_code = ble_dis_init(&dis_init_obj);
  416. APP_ERROR_CHECK(err_code);
  417. }
  418. /**@brief Function for initializing Battery Service.
  419. */
  420. static void bas_init(void)
  421. {
  422. ret_code_t err_code;
  423. ble_bas_init_t bas_init_obj;
  424. memset(&bas_init_obj, 0, sizeof(bas_init_obj));
  425. bas_init_obj.evt_handler = NULL;
  426. bas_init_obj.support_notification = true;
  427. bas_init_obj.p_report_ref = NULL;
  428. bas_init_obj.initial_batt_level = 100;
  429. bas_init_obj.bl_rd_sec = SEC_JUST_WORKS;
  430. bas_init_obj.bl_cccd_wr_sec = SEC_JUST_WORKS;
  431. bas_init_obj.bl_report_rd_sec = SEC_JUST_WORKS;
  432. err_code = ble_bas_init(&m_bas, &bas_init_obj);
  433. APP_ERROR_CHECK(err_code);
  434. }
  435. /**@brief Function for initializing HID Service.
  436. */
  437. static void hids_init(void)
  438. {
  439. ret_code_t err_code;
  440. ble_hids_init_t hids_init_obj;
  441. ble_hids_inp_rep_init_t * p_input_report;
  442. uint8_t hid_info_flags;
  443. static ble_hids_inp_rep_init_t inp_rep_array[INPUT_REPORT_COUNT];
  444. static uint8_t rep_map_data[] =
  445. {
  446. 0x05, 0x01, // Usage Page (Generic Desktop)
  447. 0x09, 0x02, // Usage (Mouse)
  448. 0xA1, 0x01, // Collection (Application)
  449. // Report ID 1: Mouse buttons + scroll/pan
  450. 0x85, 0x01, // Report Id 1
  451. 0x09, 0x01, // Usage (Pointer)
  452. 0xA1, 0x00, // Collection (Physical)
  453. 0x95, 0x05, // Report Count (3)
  454. 0x75, 0x01, // Report Size (1)
  455. 0x05, 0x09, // Usage Page (Buttons)
  456. 0x19, 0x01, // Usage Minimum (01)
  457. 0x29, 0x05, // Usage Maximum (05)
  458. 0x15, 0x00, // Logical Minimum (0)
  459. 0x25, 0x01, // Logical Maximum (1)
  460. 0x81, 0x02, // Input (Data, Variable, Absolute)
  461. 0x95, 0x01, // Report Count (1)
  462. 0x75, 0x03, // Report Size (3)
  463. 0x81, 0x01, // Input (Constant) for padding
  464. 0x75, 0x08, // Report Size (8)
  465. 0x95, 0x01, // Report Count (1)
  466. 0x05, 0x01, // Usage Page (Generic Desktop)
  467. 0x09, 0x38, // Usage (Wheel)
  468. 0x15, 0x81, // Logical Minimum (-127)
  469. 0x25, 0x7F, // Logical Maximum (127)
  470. 0x81, 0x06, // Input (Data, Variable, Relative)
  471. 0x05, 0x0C, // Usage Page (Consumer)
  472. 0x0A, 0x38, 0x02, // Usage (AC Pan)
  473. 0x95, 0x01, // Report Count (1)
  474. 0x81, 0x06, // Input (Data,Value,Relative,Bit Field)
  475. 0xC0, // End Collection (Physical)
  476. // Report ID 2: Mouse motion
  477. 0x85, 0x02, // Report Id 2
  478. 0x09, 0x01, // Usage (Pointer)
  479. 0xA1, 0x00, // Collection (Physical)
  480. 0x75, 0x0C, // Report Size (12)
  481. 0x95, 0x02, // Report Count (2)
  482. 0x05, 0x01, // Usage Page (Generic Desktop)
  483. 0x09, 0x30, // Usage (X)
  484. 0x09, 0x31, // Usage (Y)
  485. 0x16, 0x01, 0xF8, // Logical maximum (2047)
  486. 0x26, 0xFF, 0x07, // Logical minimum (-2047)
  487. 0x81, 0x06, // Input (Data, Variable, Relative)
  488. 0xC0, // End Collection (Physical)
  489. 0xC0, // End Collection (Application)
  490. // Report ID 3: Advanced buttons
  491. 0x05, 0x0C, // Usage Page (Consumer)
  492. 0x09, 0x01, // Usage (Consumer Control)
  493. 0xA1, 0x01, // Collection (Application)
  494. 0x85, 0x03, // Report Id (3)
  495. 0x15, 0x00, // Logical minimum (0)
  496. 0x25, 0x01, // Logical maximum (1)
  497. 0x75, 0x01, // Report Size (1)
  498. 0x95, 0x01, // Report Count (1)
  499. 0x09, 0xCD, // Usage (Play/Pause)
  500. 0x81, 0x06, // Input (Data,Value,Relative,Bit Field)
  501. 0x0A, 0x83, 0x01, // Usage (AL Consumer Control Configuration)
  502. 0x81, 0x06, // Input (Data,Value,Relative,Bit Field)
  503. 0x09, 0xB5, // Usage (Scan Next Track)
  504. 0x81, 0x06, // Input (Data,Value,Relative,Bit Field)
  505. 0x09, 0xB6, // Usage (Scan Previous Track)
  506. 0x81, 0x06, // Input (Data,Value,Relative,Bit Field)
  507. 0x09, 0xEA, // Usage (Volume Down)
  508. 0x81, 0x06, // Input (Data,Value,Relative,Bit Field)
  509. 0x09, 0xE9, // Usage (Volume Up)
  510. 0x81, 0x06, // Input (Data,Value,Relative,Bit Field)
  511. 0x0A, 0x25, 0x02, // Usage (AC Forward)
  512. 0x81, 0x06, // Input (Data,Value,Relative,Bit Field)
  513. 0x0A, 0x24, 0x02, // Usage (AC Back)
  514. 0x81, 0x06, // Input (Data,Value,Relative,Bit Field)
  515. 0xC0 // End Collection
  516. };
  517. memset(inp_rep_array, 0, sizeof(inp_rep_array));
  518. // Initialize HID Service.
  519. p_input_report = &inp_rep_array[INPUT_REP_BUTTONS_INDEX];
  520. p_input_report->max_len = INPUT_REP_BUTTONS_LEN;
  521. p_input_report->rep_ref.report_id = INPUT_REP_REF_BUTTONS_ID;
  522. p_input_report->rep_ref.report_type = BLE_HIDS_REP_TYPE_INPUT;
  523. p_input_report->sec.cccd_wr = SEC_JUST_WORKS;
  524. p_input_report->sec.wr = SEC_JUST_WORKS;
  525. p_input_report->sec.rd = SEC_JUST_WORKS;
  526. p_input_report = &inp_rep_array[INPUT_REP_MOVEMENT_INDEX];
  527. p_input_report->max_len = INPUT_REP_MOVEMENT_LEN;
  528. p_input_report->rep_ref.report_id = INPUT_REP_REF_MOVEMENT_ID;
  529. p_input_report->rep_ref.report_type = BLE_HIDS_REP_TYPE_INPUT;
  530. p_input_report->sec.cccd_wr = SEC_JUST_WORKS;
  531. p_input_report->sec.wr = SEC_JUST_WORKS;
  532. p_input_report->sec.rd = SEC_JUST_WORKS;
  533. p_input_report = &inp_rep_array[INPUT_REP_MPLAYER_INDEX];
  534. p_input_report->max_len = INPUT_REP_MEDIA_PLAYER_LEN;
  535. p_input_report->rep_ref.report_id = INPUT_REP_REF_MPLAYER_ID;
  536. p_input_report->rep_ref.report_type = BLE_HIDS_REP_TYPE_INPUT;
  537. p_input_report->sec.cccd_wr = SEC_JUST_WORKS;
  538. p_input_report->sec.wr = SEC_JUST_WORKS;
  539. p_input_report->sec.rd = SEC_JUST_WORKS;
  540. hid_info_flags = HID_INFO_FLAG_REMOTE_WAKE_MSK | HID_INFO_FLAG_NORMALLY_CONNECTABLE_MSK;
  541. memset(&hids_init_obj, 0, sizeof(hids_init_obj));
  542. hids_init_obj.evt_handler = on_hids_evt;
  543. hids_init_obj.error_handler = service_error_handler;
  544. hids_init_obj.is_kb = false;
  545. hids_init_obj.is_mouse = true;
  546. hids_init_obj.inp_rep_count = INPUT_REPORT_COUNT;
  547. hids_init_obj.p_inp_rep_array = inp_rep_array;
  548. hids_init_obj.outp_rep_count = 0;
  549. hids_init_obj.p_outp_rep_array = NULL;
  550. hids_init_obj.feature_rep_count = 0;
  551. hids_init_obj.p_feature_rep_array = NULL;
  552. hids_init_obj.rep_map.data_len = sizeof(rep_map_data);
  553. hids_init_obj.rep_map.p_data = rep_map_data;
  554. hids_init_obj.hid_information.bcd_hid = BASE_USB_HID_SPEC_VERSION;
  555. hids_init_obj.hid_information.b_country_code = 0;
  556. hids_init_obj.hid_information.flags = hid_info_flags;
  557. hids_init_obj.included_services_count = 0;
  558. hids_init_obj.p_included_services_array = NULL;
  559. hids_init_obj.rep_map.rd_sec = SEC_JUST_WORKS;
  560. hids_init_obj.hid_information.rd_sec = SEC_JUST_WORKS;
  561. hids_init_obj.boot_mouse_inp_rep_sec.cccd_wr = SEC_JUST_WORKS;
  562. hids_init_obj.boot_mouse_inp_rep_sec.wr = SEC_JUST_WORKS;
  563. hids_init_obj.boot_mouse_inp_rep_sec.rd = SEC_JUST_WORKS;
  564. hids_init_obj.protocol_mode_rd_sec = SEC_JUST_WORKS;
  565. hids_init_obj.protocol_mode_wr_sec = SEC_JUST_WORKS;
  566. hids_init_obj.ctrl_point_wr_sec = SEC_JUST_WORKS;
  567. err_code = ble_hids_init(&m_hids, &hids_init_obj);
  568. APP_ERROR_CHECK(err_code);
  569. }
  570. /**@brief Function for initializing services that will be used by the application.
  571. */
  572. static void services_init(void)
  573. {
  574. qwr_init();
  575. dis_init();
  576. bas_init();
  577. hids_init();
  578. }
  579. /**@brief Function for initializing the battery sensor simulator.
  580. */
  581. static void sensor_simulator_init(void)
  582. {
  583. m_battery_sim_cfg.min = MIN_BATTERY_LEVEL;
  584. m_battery_sim_cfg.max = MAX_BATTERY_LEVEL;
  585. m_battery_sim_cfg.incr = BATTERY_LEVEL_INCREMENT;
  586. m_battery_sim_cfg.start_at_max = true;
  587. sensorsim_init(&m_battery_sim_state, &m_battery_sim_cfg);
  588. }
  589. /**@brief Function for handling a Connection Parameters error.
  590. *
  591. * @param[in] nrf_error Error code containing information about what went wrong.
  592. */
  593. static void conn_params_error_handler(uint32_t nrf_error)
  594. {
  595. APP_ERROR_HANDLER(nrf_error);
  596. }
  597. /**@brief Function for initializing the Connection Parameters module.
  598. */
  599. static void conn_params_init(void)
  600. {
  601. ret_code_t err_code;
  602. ble_conn_params_init_t cp_init;
  603. memset(&cp_init, 0, sizeof(cp_init));
  604. cp_init.p_conn_params = NULL;
  605. cp_init.first_conn_params_update_delay = FIRST_CONN_PARAMS_UPDATE_DELAY;
  606. cp_init.next_conn_params_update_delay = NEXT_CONN_PARAMS_UPDATE_DELAY;
  607. cp_init.max_conn_params_update_count = MAX_CONN_PARAM_UPDATE_COUNT;
  608. cp_init.start_on_notify_cccd_handle = BLE_GATT_HANDLE_INVALID;
  609. cp_init.disconnect_on_fail = false;
  610. cp_init.evt_handler = NULL;
  611. cp_init.error_handler = conn_params_error_handler;
  612. err_code = ble_conn_params_init(&cp_init);
  613. APP_ERROR_CHECK(err_code);
  614. }
  615. /**@brief Function for starting timers.
  616. */
  617. static void timers_start(void)
  618. {
  619. ret_code_t err_code;
  620. err_code = app_timer_start(m_battery_timer_id, BATTERY_LEVEL_MEAS_INTERVAL, NULL);
  621. APP_ERROR_CHECK(err_code);
  622. }
  623. /**@brief Function for putting the chip into sleep mode.
  624. *
  625. * @note This function will not return.
  626. */
  627. static void sleep_mode_enter(void)
  628. {
  629. ret_code_t err_code;
  630. err_code = bsp_indication_set(BSP_INDICATE_IDLE);
  631. APP_ERROR_CHECK(err_code);
  632. // Prepare wakeup buttons.
  633. err_code = bsp_btn_ble_sleep_mode_prepare();
  634. APP_ERROR_CHECK(err_code);
  635. // Go to system-off mode (this function will not return; wakeup will cause a reset).
  636. err_code = sd_power_system_off();
  637. APP_ERROR_CHECK(err_code);
  638. }
  639. /**@brief Function for handling HID events.
  640. *
  641. * @details This function will be called for all HID events which are passed to the application.
  642. *
  643. * @param[in] p_hids HID service structure.
  644. * @param[in] p_evt Event received from the HID service.
  645. */
  646. static void on_hids_evt(ble_hids_t * p_hids, ble_hids_evt_t * p_evt)
  647. {
  648. switch (p_evt->evt_type)
  649. {
  650. case BLE_HIDS_EVT_BOOT_MODE_ENTERED:
  651. m_in_boot_mode = true;
  652. break;
  653. case BLE_HIDS_EVT_REPORT_MODE_ENTERED:
  654. m_in_boot_mode = false;
  655. break;
  656. case BLE_HIDS_EVT_NOTIF_ENABLED:
  657. break;
  658. default:
  659. // No implementation needed.
  660. break;
  661. }
  662. }
  663. /**@brief Function for handling advertising events.
  664. *
  665. * @details This function will be called for advertising events which are passed to the application.
  666. *
  667. * @param[in] ble_adv_evt Advertising event.
  668. */
  669. static void on_adv_evt(ble_adv_evt_t ble_adv_evt)
  670. {
  671. ret_code_t err_code;
  672. switch (ble_adv_evt)
  673. {
  674. case BLE_ADV_EVT_DIRECTED_HIGH_DUTY:
  675. NRF_LOG_INFO("Directed advertising.");
  676. err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING_DIRECTED);
  677. APP_ERROR_CHECK(err_code);
  678. break;
  679. case BLE_ADV_EVT_FAST:
  680. NRF_LOG_INFO("Fast advertising.");
  681. err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
  682. APP_ERROR_CHECK(err_code);
  683. break;
  684. case BLE_ADV_EVT_SLOW:
  685. NRF_LOG_INFO("Slow advertising.");
  686. #if SWIFT_PAIR_SUPPORTED == 1
  687. m_sp_advdata.p_manuf_specific_data = NULL;
  688. err_code = ble_advertising_advdata_update(&m_advertising, &m_sp_advdata, NULL);
  689. APP_ERROR_CHECK(err_code);
  690. #endif
  691. err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING_SLOW);
  692. APP_ERROR_CHECK(err_code);
  693. break;
  694. case BLE_ADV_EVT_FAST_WHITELIST:
  695. NRF_LOG_INFO("Fast advertising with whitelist.");
  696. err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING_WHITELIST);
  697. APP_ERROR_CHECK(err_code);
  698. break;
  699. case BLE_ADV_EVT_SLOW_WHITELIST:
  700. NRF_LOG_INFO("Slow advertising with whitelist.");
  701. err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING_WHITELIST);
  702. APP_ERROR_CHECK(err_code);
  703. err_code = ble_advertising_restart_without_whitelist(&m_advertising);
  704. APP_ERROR_CHECK(err_code);
  705. break;
  706. case BLE_ADV_EVT_IDLE:
  707. err_code = bsp_indication_set(BSP_INDICATE_IDLE);
  708. APP_ERROR_CHECK(err_code);
  709. sleep_mode_enter();
  710. break;
  711. case BLE_ADV_EVT_WHITELIST_REQUEST:
  712. {
  713. ble_gap_addr_t whitelist_addrs[BLE_GAP_WHITELIST_ADDR_MAX_COUNT];
  714. ble_gap_irk_t whitelist_irks[BLE_GAP_WHITELIST_ADDR_MAX_COUNT];
  715. uint32_t addr_cnt = BLE_GAP_WHITELIST_ADDR_MAX_COUNT;
  716. uint32_t irk_cnt = BLE_GAP_WHITELIST_ADDR_MAX_COUNT;
  717. err_code = pm_whitelist_get(whitelist_addrs, &addr_cnt,
  718. whitelist_irks, &irk_cnt);
  719. APP_ERROR_CHECK(err_code);
  720. NRF_LOG_DEBUG("pm_whitelist_get returns %d addr in whitelist and %d irk whitelist",
  721. addr_cnt,
  722. irk_cnt);
  723. // Set the correct identities list (no excluding peers with no Central Address Resolution).
  724. identities_set(PM_PEER_ID_LIST_SKIP_NO_IRK);
  725. // Apply the whitelist.
  726. err_code = ble_advertising_whitelist_reply(&m_advertising,
  727. whitelist_addrs,
  728. addr_cnt,
  729. whitelist_irks,
  730. irk_cnt);
  731. APP_ERROR_CHECK(err_code);
  732. }
  733. break;
  734. case BLE_ADV_EVT_PEER_ADDR_REQUEST:
  735. {
  736. pm_peer_data_bonding_t peer_bonding_data;
  737. // Only Give peer address if we have a handle to the bonded peer.
  738. if (m_peer_id != PM_PEER_ID_INVALID)
  739. {
  740. err_code = pm_peer_data_bonding_load(m_peer_id, &peer_bonding_data);
  741. if (err_code != NRF_ERROR_NOT_FOUND)
  742. {
  743. APP_ERROR_CHECK(err_code);
  744. // Manipulate identities to exclude peers with no Central Address Resolution.
  745. identities_set(PM_PEER_ID_LIST_SKIP_ALL);
  746. ble_gap_addr_t * p_peer_addr = &(peer_bonding_data.peer_ble_id.id_addr_info);
  747. err_code = ble_advertising_peer_addr_reply(&m_advertising, p_peer_addr);
  748. APP_ERROR_CHECK(err_code);
  749. }
  750. }
  751. break;
  752. }
  753. default:
  754. break;
  755. }
  756. }
  757. /**@brief Function for handling BLE events.
  758. *
  759. * @param[in] p_ble_evt Bluetooth stack event.
  760. * @param[in] p_context Unused.
  761. */
  762. static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
  763. {
  764. ret_code_t err_code;
  765. switch (p_ble_evt->header.evt_id)
  766. {
  767. case BLE_GAP_EVT_CONNECTED:
  768. NRF_LOG_INFO("Connected");
  769. err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
  770. APP_ERROR_CHECK(err_code);
  771. m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  772. err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle);
  773. APP_ERROR_CHECK(err_code);
  774. break;
  775. case BLE_GAP_EVT_DISCONNECTED:
  776. NRF_LOG_INFO("Disconnected");
  777. // LED indication will be changed when advertising starts.
  778. m_conn_handle = BLE_CONN_HANDLE_INVALID;
  779. break;
  780. case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
  781. {
  782. NRF_LOG_DEBUG("PHY update request.");
  783. ble_gap_phys_t const phys =
  784. {
  785. .rx_phys = BLE_GAP_PHY_AUTO,
  786. .tx_phys = BLE_GAP_PHY_AUTO,
  787. };
  788. err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
  789. APP_ERROR_CHECK(err_code);
  790. } break;
  791. case BLE_GATTC_EVT_TIMEOUT:
  792. // Disconnect on GATT Client timeout event.
  793. NRF_LOG_DEBUG("GATT Client Timeout.");
  794. err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
  795. BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  796. APP_ERROR_CHECK(err_code);
  797. break;
  798. case BLE_GATTS_EVT_TIMEOUT:
  799. // Disconnect on GATT Server timeout event.
  800. NRF_LOG_DEBUG("GATT Server Timeout.");
  801. err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
  802. BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  803. APP_ERROR_CHECK(err_code);
  804. break;
  805. default:
  806. // No implementation needed.
  807. break;
  808. }
  809. }
  810. /**@brief Function for initializing the BLE stack.
  811. *
  812. * @details Initializes the SoftDevice and the BLE event interrupt.
  813. */
  814. static void ble_stack_init(void)
  815. {
  816. ret_code_t err_code;
  817. err_code = nrf_sdh_enable_request();
  818. APP_ERROR_CHECK(err_code);
  819. // Configure the BLE stack using the default settings.
  820. // Fetch the start address of the application RAM.
  821. uint32_t ram_start = 0;
  822. err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
  823. APP_ERROR_CHECK(err_code);
  824. // Enable BLE stack.
  825. err_code = nrf_sdh_ble_enable(&ram_start);
  826. APP_ERROR_CHECK(err_code);
  827. // Register a handler for BLE events.
  828. NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
  829. }
  830. /**@brief Function for the Peer Manager initialization.
  831. */
  832. static void peer_manager_init(void)
  833. {
  834. ble_gap_sec_params_t sec_param;
  835. ret_code_t err_code;
  836. err_code = pm_init();
  837. APP_ERROR_CHECK(err_code);
  838. memset(&sec_param, 0, sizeof(ble_gap_sec_params_t));
  839. // Security parameters to be used for all security procedures.
  840. sec_param.bond = SEC_PARAM_BOND;
  841. sec_param.mitm = SEC_PARAM_MITM;
  842. sec_param.lesc = SEC_PARAM_LESC;
  843. sec_param.keypress = SEC_PARAM_KEYPRESS;
  844. sec_param.io_caps = SEC_PARAM_IO_CAPABILITIES;
  845. sec_param.oob = SEC_PARAM_OOB;
  846. sec_param.min_key_size = SEC_PARAM_MIN_KEY_SIZE;
  847. sec_param.max_key_size = SEC_PARAM_MAX_KEY_SIZE;
  848. sec_param.kdist_own.enc = 1;
  849. sec_param.kdist_own.id = 1;
  850. sec_param.kdist_peer.enc = 1;
  851. sec_param.kdist_peer.id = 1;
  852. err_code = pm_sec_params_set(&sec_param);
  853. APP_ERROR_CHECK(err_code);
  854. err_code = pm_register(pm_evt_handler);
  855. APP_ERROR_CHECK(err_code);
  856. }
  857. /**@brief Function for initializing the Advertising functionality.
  858. */
  859. static void advertising_init(void)
  860. {
  861. ret_code_t err_code;
  862. uint8_t adv_flags;
  863. ble_advertising_init_t init;
  864. memset(&init, 0, sizeof(init));
  865. adv_flags = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
  866. init.advdata.name_type = BLE_ADVDATA_FULL_NAME;
  867. init.advdata.include_appearance = true;
  868. init.advdata.flags = adv_flags;
  869. init.advdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
  870. init.advdata.uuids_complete.p_uuids = m_adv_uuids;
  871. #if SWIFT_PAIR_SUPPORTED == 1
  872. init.advdata.p_manuf_specific_data = &m_sp_manuf_advdata;
  873. memcpy(&m_sp_advdata, &init.advdata, sizeof(m_sp_advdata));
  874. #endif
  875. init.config.ble_adv_whitelist_enabled = true;
  876. init.config.ble_adv_directed_high_duty_enabled = true;
  877. init.config.ble_adv_directed_enabled = false;
  878. init.config.ble_adv_directed_interval = 0;
  879. init.config.ble_adv_directed_timeout = 0;
  880. init.config.ble_adv_fast_enabled = true;
  881. init.config.ble_adv_fast_interval = APP_ADV_FAST_INTERVAL;
  882. init.config.ble_adv_fast_timeout = APP_ADV_FAST_DURATION;
  883. init.config.ble_adv_slow_enabled = true;
  884. init.config.ble_adv_slow_interval = APP_ADV_SLOW_INTERVAL;
  885. init.config.ble_adv_slow_timeout = APP_ADV_SLOW_DURATION;
  886. init.evt_handler = on_adv_evt;
  887. init.error_handler = ble_advertising_error_handler;
  888. err_code = ble_advertising_init(&m_advertising, &init);
  889. APP_ERROR_CHECK(err_code);
  890. ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
  891. }
  892. /**@brief Function for the Event Scheduler initialization.
  893. */
  894. static void scheduler_init(void)
  895. {
  896. APP_SCHED_INIT(SCHED_MAX_EVENT_DATA_SIZE, SCHED_QUEUE_SIZE);
  897. }
  898. /**@brief Function for sending a Mouse Movement.
  899. *
  900. * @param[in] x_delta Horizontal movement.
  901. * @param[in] y_delta Vertical movement.
  902. */
  903. static void mouse_movement_send(int16_t x_delta, int16_t y_delta)
  904. {
  905. ret_code_t err_code;
  906. if (m_in_boot_mode)
  907. {
  908. x_delta = MIN(x_delta, 0x00ff);
  909. y_delta = MIN(y_delta, 0x00ff);
  910. err_code = ble_hids_boot_mouse_inp_rep_send(&m_hids,
  911. 0x00,
  912. (int8_t)x_delta,
  913. (int8_t)y_delta,
  914. 0,
  915. NULL,
  916. m_conn_handle);
  917. }
  918. else
  919. {
  920. uint8_t buffer[INPUT_REP_MOVEMENT_LEN];
  921. APP_ERROR_CHECK_BOOL(INPUT_REP_MOVEMENT_LEN == 3);
  922. x_delta = MIN(x_delta, 0x0fff);
  923. y_delta = MIN(y_delta, 0x0fff);
  924. buffer[0] = x_delta & 0x00ff;
  925. buffer[1] = ((y_delta & 0x000f) << 4) | ((x_delta & 0x0f00) >> 8);
  926. buffer[2] = (y_delta & 0x0ff0) >> 4;
  927. err_code = ble_hids_inp_rep_send(&m_hids,
  928. INPUT_REP_MOVEMENT_INDEX,
  929. INPUT_REP_MOVEMENT_LEN,
  930. buffer,
  931. m_conn_handle);
  932. }
  933. if ((err_code != NRF_SUCCESS) &&
  934. (err_code != NRF_ERROR_INVALID_STATE) &&
  935. (err_code != NRF_ERROR_RESOURCES) &&
  936. (err_code != NRF_ERROR_BUSY) &&
  937. (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
  938. )
  939. {
  940. APP_ERROR_HANDLER(err_code);
  941. }
  942. }
  943. /**@brief Function for handling events from the BSP module.
  944. *
  945. * @param[in] event Event generated by button press.
  946. */
  947. static void bsp_event_handler(bsp_event_t event)
  948. {
  949. ret_code_t err_code;
  950. switch (event)
  951. {
  952. case BSP_EVENT_SLEEP:
  953. sleep_mode_enter();
  954. break;
  955. case BSP_EVENT_DISCONNECT:
  956. err_code = sd_ble_gap_disconnect(m_conn_handle,
  957. BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  958. if (err_code != NRF_ERROR_INVALID_STATE)
  959. {
  960. APP_ERROR_CHECK(err_code);
  961. }
  962. break;
  963. case BSP_EVENT_WHITELIST_OFF:
  964. if (m_conn_handle == BLE_CONN_HANDLE_INVALID)
  965. {
  966. err_code = ble_advertising_restart_without_whitelist(&m_advertising);
  967. if (err_code != NRF_ERROR_INVALID_STATE)
  968. {
  969. APP_ERROR_CHECK(err_code);
  970. }
  971. }
  972. break;
  973. case BSP_EVENT_KEY_0:
  974. if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
  975. {
  976. mouse_movement_send(-MOVEMENT_SPEED, 0);
  977. }
  978. break;
  979. case BSP_EVENT_KEY_1:
  980. if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
  981. {
  982. mouse_movement_send(0, -MOVEMENT_SPEED);
  983. }
  984. break;
  985. case BSP_EVENT_KEY_2:
  986. if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
  987. {
  988. mouse_movement_send(MOVEMENT_SPEED, 0);
  989. }
  990. break;
  991. case BSP_EVENT_KEY_3:
  992. if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
  993. {
  994. mouse_movement_send(0, MOVEMENT_SPEED);
  995. }
  996. break;
  997. default:
  998. break;
  999. }
  1000. }
  1001. /**@brief Function for initializing buttons and leds.
  1002. *
  1003. * @param[out] p_erase_bonds Will be true if the clear bonding button was pressed to wake the application up.
  1004. */
  1005. static void buttons_leds_init(bool * p_erase_bonds)
  1006. {
  1007. ret_code_t err_code;
  1008. bsp_event_t startup_event;
  1009. err_code = bsp_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS, bsp_event_handler);
  1010. APP_ERROR_CHECK(err_code);
  1011. err_code = bsp_btn_ble_init(NULL, &startup_event);
  1012. APP_ERROR_CHECK(err_code);
  1013. *p_erase_bonds = (startup_event == BSP_EVENT_CLEAR_BONDING_DATA);
  1014. }
  1015. /**@brief Function for initializing the nrf log module.
  1016. */
  1017. static void log_init(void)
  1018. {
  1019. ret_code_t err_code = NRF_LOG_INIT(NULL);
  1020. APP_ERROR_CHECK(err_code);
  1021. NRF_LOG_DEFAULT_BACKENDS_INIT();
  1022. }
  1023. /**@brief Function for initializing power management.
  1024. */
  1025. static void power_management_init(void)
  1026. {
  1027. ret_code_t err_code;
  1028. err_code = nrf_pwr_mgmt_init();
  1029. APP_ERROR_CHECK(err_code);
  1030. }
  1031. /**@brief Function for handling the idle state (main loop).
  1032. *
  1033. * @details If there is no pending log operation, then sleep until next the next event occurs.
  1034. */
  1035. static void idle_state_handle(void)
  1036. {
  1037. app_sched_execute();
  1038. if (NRF_LOG_PROCESS() == false)
  1039. {
  1040. nrf_pwr_mgmt_run();
  1041. }
  1042. }
  1043. /**@brief Function for application main entry.
  1044. */
  1045. int main(void)
  1046. {
  1047. bool erase_bonds;
  1048. // Initialize.
  1049. log_init();
  1050. timers_init();
  1051. buttons_leds_init(&erase_bonds);
  1052. power_management_init();
  1053. ble_stack_init();
  1054. scheduler_init();
  1055. gap_params_init();
  1056. gatt_init();
  1057. advertising_init();
  1058. services_init();
  1059. sensor_simulator_init();
  1060. conn_params_init();
  1061. peer_manager_init();
  1062. // Start execution.
  1063. NRF_LOG_INFO("HID Mouse example started.");
  1064. timers_start();
  1065. advertising_start(erase_bonds);
  1066. // Enter main loop.
  1067. for (;;)
  1068. {
  1069. idle_state_handle();
  1070. }
  1071. }
  1072. /**
  1073. * @}
  1074. */