Select one of the symbols to view example projects that use it.
 
Outline
#include "common.h"
#include "gap.h"
#include "gatt_svc.h"
#include "heart_rate.h"
#include "led.h"
on_stack_reset(int)
on_stack_sync()
nimble_host_config_init()
nimble_host_task(void *)
heart_rate_task(void *)
app_main()
Files
loading (2/4)...
SourceVuESP-IDF Framework and ExamplesNimBLE_Security samplemain/main.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 *//* ... */ /* Includes */ #include "common.h" #include "gap.h" #include "gatt_svc.h" #include "heart_rate.h" #include "led.h"5 includes /* Library function declarations */ void ble_store_config_init(void); /* Private function declarations */ static void nimble_host_config_init(void); static void nimble_host_task(void *param); /* Private functions */ /* * Stack event callback functions * - on_stack_reset is called when host resets BLE stack due to errors * - on_stack_sync is called when host has synced with controller *//* ... */ static void on_stack_reset(int reason) { /* On reset, print reset reason to console */ ESP_LOGI(TAG, "nimble stack reset, reset reason: %d", reason); }{ ... } static void on_stack_sync(void) { /* On stack sync, do advertising initialization */ adv_init(); }{ ... } static void nimble_host_config_init(void) { /* Set host callbacks */ ble_hs_cfg.reset_cb = on_stack_reset; ble_hs_cfg.sync_cb = on_stack_sync; ble_hs_cfg.gatts_register_cb = gatt_svr_register_cb; ble_hs_cfg.store_status_cb = ble_store_util_status_rr; /* Security manager configuration */ ble_hs_cfg.sm_io_cap = BLE_HS_IO_DISPLAY_ONLY; ble_hs_cfg.sm_bonding = 1; ble_hs_cfg.sm_mitm = 1; ble_hs_cfg.sm_our_key_dist |= BLE_SM_PAIR_KEY_DIST_ENC | BLE_SM_PAIR_KEY_DIST_ID; ble_hs_cfg.sm_their_key_dist |= BLE_SM_PAIR_KEY_DIST_ENC | BLE_SM_PAIR_KEY_DIST_ID; /* Store host configuration */ ble_store_config_init(); }{ ... } static void nimble_host_task(void *param) { /* Task entry log */ ESP_LOGI(TAG, "nimble host task has been started!"); /* This function won't return until nimble_port_stop() is executed */ nimble_port_run(); /* Clean up at exit */ vTaskDelete(NULL); }{ ... } static void heart_rate_task(void *param) { /* Task entry log */ ESP_LOGI(TAG, "heart rate task has been started!"); /* Loop forever */ while (1) { /* Update heart rate value every 1 second */ update_heart_rate(); ESP_LOGI(TAG, "heart rate updated to %d", get_heart_rate()); /* Send heart rate indication if enabled */ send_heart_rate_indication(); /* Sleep */ vTaskDelay(HEART_RATE_TASK_PERIOD); }{...} /* Clean up at exit */ vTaskDelete(NULL); }{ ... } void app_main(void) { /* Local variables */ int rc; uint32_t seed = esp_random(); esp_err_t ret; /* LED initialization */ led_init(); /* Random generator initialization */ srand(seed); /* * NVS flash initialization * Dependency of BLE stack to store configurations *//* ... */ ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); }{...} if (ret != ESP_OK) { ESP_LOGE(TAG, "failed to initialize nvs flash, error code: %d ", ret); return; }{...} /* NimBLE stack initialization */ ret = nimble_port_init(); if (ret != ESP_OK) { ESP_LOGE(TAG, "failed to initialize nimble stack, error code: %d ", ret); return; }{...} /* GAP service initialization */ rc = gap_init(); if (rc != 0) { ESP_LOGE(TAG, "failed to initialize GAP service, error code: %d", rc); return; }{...} /* GATT server initialization */ rc = gatt_svc_init(); if (rc != 0) { ESP_LOGE(TAG, "failed to initialize GATT server, error code: %d", rc); return; }{...} /* NimBLE host configuration initialization */ nimble_host_config_init(); /* Start NimBLE host task thread and return */ xTaskCreate(nimble_host_task, "NimBLE Host", 4*1024, NULL, 5, NULL); xTaskCreate(heart_rate_task, "Heart Rate", 4*1024, NULL, 5, NULL); return; }{ ... }
Details