main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "esp_log.h"
  7. #include "nvs_flash.h"
  8. #include "freertos/FreeRTOSConfig.h"
  9. /* BLE */
  10. #include "nimble/nimble_port.h"
  11. #include "nimble/nimble_port_freertos.h"
  12. #include "host/ble_hs.h"
  13. #include "host/util/util.h"
  14. #include "console/console.h"
  15. #include "services/gap/ble_svc_gap.h"
  16. #include "gatts_sens.h"
  17. #include "../src/ble_hs_hci_priv.h"
  18. #define NOTIFY_THROUGHPUT_PAYLOAD 500
  19. #define MIN_REQUIRED_MBUF 2 /* Assuming payload of 500Bytes and each mbuf can take 292Bytes. */
  20. #define PREFERRED_MTU_VALUE 512
  21. #define LL_PACKET_TIME 2120
  22. #define LL_PACKET_LENGTH 251
  23. #define MTU_DEF 512
  24. static const char *tag = "bleprph_throughput";
  25. static const char *device_name = "nimble_prph";
  26. static SemaphoreHandle_t notify_sem;
  27. static bool notify_state;
  28. static int notify_test_time = 60;
  29. static uint16_t conn_handle;
  30. /* Dummy variable */
  31. static uint8_t dummy;
  32. static uint8_t gatts_addr_type;
  33. static int gatts_gap_event(struct ble_gap_event *event, void *arg);
  34. /**
  35. * Utility function to log an array of bytes.
  36. */
  37. void
  38. print_bytes(const uint8_t *bytes, int len)
  39. {
  40. int i;
  41. for (i = 0; i < len; i++) {
  42. ESP_LOGI(tag, "%s0x%02x", i != 0 ? ":" : "", bytes[i]);
  43. }
  44. }
  45. void
  46. print_addr(const void *addr)
  47. {
  48. const uint8_t *u8p;
  49. u8p = addr;
  50. ESP_LOGI(tag, "%02x:%02x:%02x:%02x:%02x:%02x",
  51. u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]);
  52. }
  53. static void
  54. bleprph_print_conn_desc(struct ble_gap_conn_desc *desc)
  55. {
  56. ESP_LOGI(tag, "handle=%d our_ota_addr_type=%d our_ota_addr=",
  57. desc->conn_handle, desc->our_ota_addr.type);
  58. print_addr(desc->our_ota_addr.val);
  59. ESP_LOGI(tag, " our_id_addr_type=%d our_id_addr=",
  60. desc->our_id_addr.type);
  61. print_addr(desc->our_id_addr.val);
  62. ESP_LOGI(tag, " peer_ota_addr_type=%d peer_ota_addr=",
  63. desc->peer_ota_addr.type);
  64. print_addr(desc->peer_ota_addr.val);
  65. ESP_LOGI(tag, " peer_id_addr_type=%d peer_id_addr=",
  66. desc->peer_id_addr.type);
  67. print_addr(desc->peer_id_addr.val);
  68. ESP_LOGI(tag, " conn_itvl=%d conn_latency=%d supervision_timeout=%d "
  69. "encrypted=%d authenticated=%d bonded=%d",
  70. desc->conn_itvl, desc->conn_latency,
  71. desc->supervision_timeout,
  72. desc->sec_state.encrypted,
  73. desc->sec_state.authenticated,
  74. desc->sec_state.bonded);
  75. }
  76. /*
  77. * Enables advertising with parameters:
  78. * o General discoverable mode
  79. * o Undirected connectable mode
  80. */
  81. static void
  82. gatts_advertise(void)
  83. {
  84. struct ble_gap_adv_params adv_params;
  85. struct ble_hs_adv_fields fields;
  86. int rc;
  87. /*
  88. * Set the advertisement data included in our advertisements:
  89. * o Flags (indicates advertisement type and other general info)
  90. * o Advertising tx power
  91. * o Device name
  92. */
  93. memset(&fields, 0, sizeof(fields));
  94. /*
  95. * Advertise two flags:
  96. * o Discoverability in forthcoming advertisement (general)
  97. * o BLE-only (BR/EDR unsupported)
  98. */
  99. fields.flags = BLE_HS_ADV_F_DISC_GEN |
  100. BLE_HS_ADV_F_BREDR_UNSUP;
  101. /*
  102. * Indicate that the TX power level field should be included; have the
  103. * stack fill this value automatically. This is done by assigning the
  104. * special value BLE_HS_ADV_TX_PWR_LVL_AUTO.
  105. */
  106. fields.tx_pwr_lvl_is_present = 1;
  107. fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
  108. fields.name = (uint8_t *)device_name;
  109. fields.name_len = strlen(device_name);
  110. fields.name_is_complete = 1;
  111. rc = ble_gap_adv_set_fields(&fields);
  112. if (rc != 0) {
  113. ESP_LOGE(tag, "Error setting advertisement data; rc=%d", rc);
  114. return;
  115. }
  116. /* Begin advertising */
  117. memset(&adv_params, 0, sizeof(adv_params));
  118. adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
  119. adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
  120. rc = ble_gap_adv_start(gatts_addr_type, NULL, BLE_HS_FOREVER,
  121. &adv_params, gatts_gap_event, NULL);
  122. if (rc != 0) {
  123. ESP_LOGE(tag, "Error enabling advertisement; rc=%d", rc);
  124. return;
  125. }
  126. }
  127. /* This function sends notifications to the client */
  128. static void
  129. notify_task(void *arg)
  130. {
  131. static uint8_t payload[NOTIFY_THROUGHPUT_PAYLOAD] = {0};/* Data payload */
  132. int rc, notify_count = 0;
  133. int64_t start_time, end_time, notify_time = 0;
  134. struct os_mbuf *om;
  135. payload[0] = dummy; /* storing dummy data */
  136. payload[1] = rand();
  137. payload[99] = rand();
  138. while (!notify_state) {
  139. vTaskDelay(1000 / portTICK_PERIOD_MS);
  140. }
  141. while (1) {
  142. switch (notify_test_time) {
  143. case 0:
  144. vTaskDelay(1000 / portTICK_PERIOD_MS);
  145. break;
  146. default:
  147. start_time = esp_timer_get_time();
  148. if (!notify_state) {
  149. vTaskDelay(1000 / portTICK_PERIOD_MS);
  150. break;
  151. }
  152. while (notify_time < (notify_test_time * 1000)) {
  153. /* We are anyway using counting semaphore for sending
  154. * notifications. So hopefully not much waiting period will be
  155. * introduced before sending a new notification. Revisit this
  156. * counter if need to do away with semaphore waiting. XXX */
  157. xSemaphoreTake(notify_sem, portMAX_DELAY);
  158. if (dummy == 200) {
  159. dummy = 0;
  160. }
  161. dummy++;
  162. /* Check if the MBUFs are available */
  163. if (os_msys_num_free() >= MIN_REQUIRED_MBUF) {
  164. do {
  165. om = ble_hs_mbuf_from_flat(payload, sizeof(payload));
  166. if (om == NULL) {
  167. /* Memory not available for mbuf */
  168. ESP_LOGE(tag, "No MBUFs available from pool, retry..");
  169. vTaskDelay(100 / portTICK_PERIOD_MS);
  170. }
  171. } while (om == NULL);
  172. rc = ble_gatts_notify_custom(conn_handle, notify_handle, om);
  173. if (rc != 0) {
  174. ESP_LOGE(tag, "Error while sending notification; rc = %d", rc);
  175. notify_count -= 1;
  176. xSemaphoreGive(notify_sem);
  177. /* Most probably error is because we ran out of mbufs (rc = 6),
  178. * increase the mbuf count/size from menuconfig. Though
  179. * inserting delay is not good solution let us keep it
  180. * simple for time being so that the mbufs get freed up
  181. * (?), of course assumption is we ran out of mbufs */
  182. vTaskDelay(10 / portTICK_PERIOD_MS);
  183. }
  184. } else {
  185. ESP_LOGE(tag, "Not enough OS_MBUFs available; reduce notify count ");
  186. xSemaphoreGive(notify_sem);
  187. notify_count -= 1;
  188. vTaskDelay(10 / portTICK_PERIOD_MS);
  189. }
  190. end_time = esp_timer_get_time();
  191. notify_time = (end_time - start_time) / 1000 ;
  192. notify_count += 1;
  193. }
  194. printf("\n*********************************\n");
  195. ESP_LOGI(tag, "Notify throughput = %d bps, count = %d",
  196. (notify_count * NOTIFY_THROUGHPUT_PAYLOAD * 8) / notify_test_time, notify_count);
  197. printf("\n*********************************\n");
  198. ESP_LOGI(tag, " Notification test complete for stipulated time of %d sec", notify_test_time);
  199. notify_test_time = 0;
  200. notify_count = 0;
  201. break;
  202. }
  203. vTaskDelay(3000 / portTICK_PERIOD_MS);
  204. }
  205. }
  206. static int
  207. gatts_gap_event(struct ble_gap_event *event, void *arg)
  208. {
  209. struct ble_gap_conn_desc desc;
  210. int rc;
  211. switch (event->type) {
  212. case BLE_GAP_EVENT_CONNECT:
  213. /* A new connection was established or a connection attempt failed */
  214. ESP_LOGI(tag, "connection %s; status = %d ",
  215. event->connect.status == 0 ? "established" : "failed",
  216. event->connect.status);
  217. rc = ble_att_set_preferred_mtu(PREFERRED_MTU_VALUE);
  218. if (rc != 0) {
  219. ESP_LOGE(tag, "Failed to set preferred MTU; rc = %d", rc);
  220. }
  221. if (event->connect.status != 0) {
  222. /* Connection failed; resume advertising */
  223. gatts_advertise();
  224. }
  225. rc = ble_hs_hci_util_set_data_len(event->connect.conn_handle,
  226. LL_PACKET_LENGTH,
  227. LL_PACKET_TIME);
  228. if (rc != 0) {
  229. ESP_LOGE(tag, "Set packet length failed");
  230. }
  231. conn_handle = event->connect.conn_handle;
  232. break;
  233. case BLE_GAP_EVENT_DISCONNECT:
  234. ESP_LOGI(tag, "disconnect; reason = %d", event->disconnect.reason);
  235. /* Connection terminated; resume advertising */
  236. gatts_advertise();
  237. break;
  238. case BLE_GAP_EVENT_CONN_UPDATE:
  239. /* The central has updated the connection parameters. */
  240. ESP_LOGI(tag, "connection updated; status=%d ",
  241. event->conn_update.status);
  242. rc = ble_gap_conn_find(event->conn_update.conn_handle, &desc);
  243. assert(rc == 0);
  244. bleprph_print_conn_desc(&desc);
  245. return 0;
  246. case BLE_GAP_EVENT_ADV_COMPLETE:
  247. ESP_LOGI(tag, "adv complete ");
  248. gatts_advertise();
  249. break;
  250. case BLE_GAP_EVENT_SUBSCRIBE:
  251. ESP_LOGI(tag, "subscribe event; cur_notify=%d; value handle; "
  252. "val_handle = %d",
  253. event->subscribe.cur_notify, event->subscribe.attr_handle);
  254. if (event->subscribe.attr_handle == notify_handle) {
  255. notify_state = event->subscribe.cur_notify;
  256. if (arg != NULL) {
  257. ESP_LOGI(tag, "notify test time = %d", *(int *)arg);
  258. notify_test_time = *((int *)arg);
  259. }
  260. xSemaphoreGive(notify_sem);
  261. } else if (event->subscribe.attr_handle != notify_handle) {
  262. notify_state = event->subscribe.cur_notify;
  263. }
  264. break;
  265. case BLE_GAP_EVENT_NOTIFY_TX:
  266. ESP_LOGD(tag, "BLE_GAP_EVENT_NOTIFY_TX success !!");
  267. if ((event->notify_tx.status == 0) ||
  268. (event->notify_tx.status == BLE_HS_EDONE)) {
  269. /* Send new notification i.e. give Semaphore. By definition,
  270. * sending new notifications should not be based on successful
  271. * notifications sent, but let us adopt this method to avoid too
  272. * many `BLE_HS_ENOMEM` errors because of continuous transfer of
  273. * notifications.XXX */
  274. xSemaphoreGive(notify_sem);
  275. } else {
  276. ESP_LOGE(tag, "BLE_GAP_EVENT_NOTIFY_TX notify tx status = %d", event->notify_tx.status);
  277. }
  278. break;
  279. case BLE_GAP_EVENT_MTU:
  280. ESP_LOGI(tag, "mtu update event; conn_handle = %d mtu = %d ",
  281. event->mtu.conn_handle,
  282. event->mtu.value);
  283. break;
  284. }
  285. return 0;
  286. }
  287. static void
  288. gatts_on_sync(void)
  289. {
  290. int rc;
  291. uint8_t addr_val[6] = {0};
  292. rc = ble_hs_id_infer_auto(0, &gatts_addr_type);
  293. assert(rc == 0);
  294. rc = ble_hs_id_copy_addr(gatts_addr_type, addr_val, NULL);
  295. assert(rc == 0);
  296. ESP_LOGI(tag, "Device Address: ");
  297. print_addr(addr_val);
  298. /* Begin advertising */
  299. gatts_advertise();
  300. }
  301. static void
  302. gatts_on_reset(int reason)
  303. {
  304. ESP_LOGE(tag, "Resetting state; reason=%d", reason);
  305. }
  306. void gatts_host_task(void *param)
  307. {
  308. ESP_LOGI(tag, "BLE Host Task Started");
  309. /* Create a counting semaphore for Notification. Can be used to track
  310. * successful notification txmission. Optimistically take some big number
  311. * for counting Semaphore */
  312. notify_sem = xSemaphoreCreateCounting(100, 0);
  313. /* This function will return only when nimble_port_stop() is executed */
  314. nimble_port_run();
  315. vSemaphoreDelete(notify_sem);
  316. nimble_port_freertos_deinit();
  317. }
  318. void app_main(void)
  319. {
  320. int rc;
  321. /* Initialize NVS — it is used to store PHY calibration data */
  322. esp_err_t ret = nvs_flash_init();
  323. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  324. ESP_ERROR_CHECK(nvs_flash_erase());
  325. ret = nvs_flash_init();
  326. }
  327. ESP_ERROR_CHECK(ret);
  328. ret = nimble_port_init();
  329. if (ret != ESP_OK) {
  330. ESP_LOGE(tag, "Failed to init nimble %d ", ret);
  331. return;
  332. }
  333. /* Initialize the NimBLE host configuration */
  334. ble_hs_cfg.sync_cb = gatts_on_sync;
  335. ble_hs_cfg.reset_cb = gatts_on_reset;
  336. ble_hs_cfg.gatts_register_cb = gatt_svr_register_cb,
  337. ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
  338. /* Initialize Notify Task */
  339. xTaskCreate(notify_task, "notify_task", 4096, NULL, 10, NULL);
  340. rc = gatt_svr_init();
  341. assert(rc == 0);
  342. /* Set the default device name */
  343. rc = ble_svc_gap_device_name_set(device_name);
  344. assert(rc == 0);
  345. /* Start the task */
  346. nimble_port_freertos_init(gatts_host_task);
  347. }