main.c 8.0 KB

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