main.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. #include "esp_log.h"
  20. #include "nvs_flash.h"
  21. #include "freertos/FreeRTOSConfig.h"
  22. /* BLE */
  23. #include "esp_nimble_hci.h"
  24. #include "nimble/nimble_port.h"
  25. #include "nimble/nimble_port_freertos.h"
  26. #include "host/ble_hs.h"
  27. #include "host/util/util.h"
  28. #include "console/console.h"
  29. #include "services/gap/ble_svc_gap.h"
  30. #include "blehr_sens.h"
  31. static const char *tag = "NimBLE_BLE_HeartRate";
  32. static xTimerHandle blehr_tx_timer;
  33. static bool notify_state;
  34. static uint16_t conn_handle;
  35. static const char *device_name = "blehr_sensor_1.0";
  36. static int blehr_gap_event(struct ble_gap_event *event, void *arg);
  37. static uint8_t blehr_addr_type;
  38. /* Variable to simulate heart beats */
  39. static uint8_t heartrate = 90;
  40. /**
  41. * Utility function to log an array of bytes.
  42. */
  43. void
  44. print_bytes(const uint8_t *bytes, int len)
  45. {
  46. int i;
  47. for (i = 0; i < len; i++) {
  48. MODLOG_DFLT(INFO, "%s0x%02x", i != 0 ? ":" : "", bytes[i]);
  49. }
  50. }
  51. void
  52. print_addr(const void *addr)
  53. {
  54. const uint8_t *u8p;
  55. u8p = addr;
  56. MODLOG_DFLT(INFO, "%02x:%02x:%02x:%02x:%02x:%02x",
  57. u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]);
  58. }
  59. /*
  60. * Enables advertising with parameters:
  61. * o General discoverable mode
  62. * o Undirected connectable mode
  63. */
  64. static void
  65. blehr_advertise(void)
  66. {
  67. struct ble_gap_adv_params adv_params;
  68. struct ble_hs_adv_fields fields;
  69. int rc;
  70. /*
  71. * Set the advertisement data included in our advertisements:
  72. * o Flags (indicates advertisement type and other general info)
  73. * o Advertising tx power
  74. * o Device name
  75. */
  76. memset(&fields, 0, sizeof(fields));
  77. /*
  78. * Advertise two flags:
  79. * o Discoverability in forthcoming advertisement (general)
  80. * o BLE-only (BR/EDR unsupported)
  81. */
  82. fields.flags = BLE_HS_ADV_F_DISC_GEN |
  83. BLE_HS_ADV_F_BREDR_UNSUP;
  84. /*
  85. * Indicate that the TX power level field should be included; have the
  86. * stack fill this value automatically. This is done by assigning the
  87. * special value BLE_HS_ADV_TX_PWR_LVL_AUTO.
  88. */
  89. fields.tx_pwr_lvl_is_present = 1;
  90. fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
  91. fields.name = (uint8_t *)device_name;
  92. fields.name_len = strlen(device_name);
  93. fields.name_is_complete = 1;
  94. rc = ble_gap_adv_set_fields(&fields);
  95. if (rc != 0) {
  96. MODLOG_DFLT(ERROR, "error setting advertisement data; rc=%d\n", rc);
  97. return;
  98. }
  99. /* Begin advertising */
  100. memset(&adv_params, 0, sizeof(adv_params));
  101. adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
  102. adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
  103. rc = ble_gap_adv_start(blehr_addr_type, NULL, BLE_HS_FOREVER,
  104. &adv_params, blehr_gap_event, NULL);
  105. if (rc != 0) {
  106. MODLOG_DFLT(ERROR, "error enabling advertisement; rc=%d\n", rc);
  107. return;
  108. }
  109. }
  110. static void
  111. blehr_tx_hrate_stop(void)
  112. {
  113. xTimerStop( blehr_tx_timer, 1000 / portTICK_PERIOD_MS );
  114. }
  115. /* Reset heart rate measurement */
  116. static void
  117. blehr_tx_hrate_reset(void)
  118. {
  119. int rc;
  120. if (xTimerReset(blehr_tx_timer, 1000 / portTICK_PERIOD_MS ) == pdPASS) {
  121. rc = 0;
  122. } else {
  123. rc = 1;
  124. }
  125. assert(rc == 0);
  126. }
  127. /* This function simulates heart beat and notifies it to the client */
  128. static void
  129. blehr_tx_hrate(xTimerHandle ev)
  130. {
  131. static uint8_t hrm[2];
  132. int rc;
  133. struct os_mbuf *om;
  134. if (!notify_state) {
  135. blehr_tx_hrate_stop();
  136. heartrate = 90;
  137. return;
  138. }
  139. hrm[0] = 0x06; /* contact of a sensor */
  140. hrm[1] = heartrate; /* storing dummy data */
  141. /* Simulation of heart beats */
  142. heartrate++;
  143. if (heartrate == 160) {
  144. heartrate = 90;
  145. }
  146. om = ble_hs_mbuf_from_flat(hrm, sizeof(hrm));
  147. rc = ble_gattc_notify_custom(conn_handle, hrs_hrm_handle, om);
  148. assert(rc == 0);
  149. blehr_tx_hrate_reset();
  150. }
  151. static int
  152. blehr_gap_event(struct ble_gap_event *event, void *arg)
  153. {
  154. switch (event->type) {
  155. case BLE_GAP_EVENT_CONNECT:
  156. /* A new connection was established or a connection attempt failed */
  157. MODLOG_DFLT(INFO, "connection %s; status=%d\n",
  158. event->connect.status == 0 ? "established" : "failed",
  159. event->connect.status);
  160. if (event->connect.status != 0) {
  161. /* Connection failed; resume advertising */
  162. blehr_advertise();
  163. }
  164. conn_handle = event->connect.conn_handle;
  165. break;
  166. case BLE_GAP_EVENT_DISCONNECT:
  167. MODLOG_DFLT(INFO, "disconnect; reason=%d\n", event->disconnect.reason);
  168. /* Connection terminated; resume advertising */
  169. blehr_advertise();
  170. break;
  171. case BLE_GAP_EVENT_ADV_COMPLETE:
  172. MODLOG_DFLT(INFO, "adv complete\n");
  173. blehr_advertise();
  174. break;
  175. case BLE_GAP_EVENT_SUBSCRIBE:
  176. MODLOG_DFLT(INFO, "subscribe event; cur_notify=%d\n value handle; "
  177. "val_handle=%d\n",
  178. event->subscribe.cur_notify, hrs_hrm_handle);
  179. if (event->subscribe.attr_handle == hrs_hrm_handle) {
  180. notify_state = event->subscribe.cur_notify;
  181. blehr_tx_hrate_reset();
  182. } else if (event->subscribe.attr_handle != hrs_hrm_handle) {
  183. notify_state = event->subscribe.cur_notify;
  184. blehr_tx_hrate_stop();
  185. }
  186. ESP_LOGI("BLE_GAP_SUBSCRIBE_EVENT", "conn_handle from subscribe=%d", conn_handle);
  187. break;
  188. case BLE_GAP_EVENT_MTU:
  189. MODLOG_DFLT(INFO, "mtu update event; conn_handle=%d mtu=%d\n",
  190. event->mtu.conn_handle,
  191. event->mtu.value);
  192. break;
  193. }
  194. return 0;
  195. }
  196. static void
  197. blehr_on_sync(void)
  198. {
  199. int rc;
  200. rc = ble_hs_id_infer_auto(0, &blehr_addr_type);
  201. assert(rc == 0);
  202. uint8_t addr_val[6] = {0};
  203. rc = ble_hs_id_copy_addr(blehr_addr_type, addr_val, NULL);
  204. MODLOG_DFLT(INFO, "Device Address: ");
  205. print_addr(addr_val);
  206. MODLOG_DFLT(INFO, "\n");
  207. /* Begin advertising */
  208. blehr_advertise();
  209. }
  210. static void
  211. blehr_on_reset(int reason)
  212. {
  213. MODLOG_DFLT(ERROR, "Resetting state; reason=%d\n", reason);
  214. }
  215. void blehr_host_task(void *param)
  216. {
  217. ESP_LOGI(tag, "BLE Host Task Started");
  218. /* This function will return only when nimble_port_stop() is executed */
  219. nimble_port_run();
  220. nimble_port_freertos_deinit();
  221. }
  222. void app_main(void)
  223. {
  224. int rc;
  225. /* Initialize NVS — it is used to store PHY calibration data */
  226. esp_err_t ret = nvs_flash_init();
  227. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  228. ESP_ERROR_CHECK(nvs_flash_erase());
  229. ret = nvs_flash_init();
  230. }
  231. ESP_ERROR_CHECK(ret);
  232. ESP_ERROR_CHECK(esp_nimble_hci_and_controller_init());
  233. nimble_port_init();
  234. /* Initialize the NimBLE host configuration */
  235. ble_hs_cfg.sync_cb = blehr_on_sync;
  236. ble_hs_cfg.reset_cb = blehr_on_reset;
  237. /* name, period/time, auto reload, timer ID, callback */
  238. blehr_tx_timer = xTimerCreate("blehr_tx_timer", pdMS_TO_TICKS(1000), pdTRUE, (void *)0, blehr_tx_hrate);
  239. rc = gatt_svr_init();
  240. assert(rc == 0);
  241. /* Set the default device name */
  242. rc = ble_svc_gap_device_name_set(device_name);
  243. assert(rc == 0);
  244. /* Start the task */
  245. nimble_port_freertos_init(blehr_host_task);
  246. }