Skip to content

Commit 84be8ac

Browse files
committed
familiarization demo code
part 2
1 parent 1bb6072 commit 84be8ac

File tree

6 files changed

+116
-4
lines changed

6 files changed

+116
-4
lines changed

main/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
set(srcs "main.cpp"
2-
"functions.cpp")
2+
"common.cpp"
3+
"gap.cpp"
4+
"gatt.cpp")
35

46
idf_component_register(SRCS "${srcs}"
57
INCLUDE_DIRS ".")
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ static void bleprhp_on_sync(void){
2020
rc = ble_hs_id_infer_auto(0, &bleprhp_addr_type);
2121
assert(rc == 0);
2222

23-
//TODO
24-
//bleprhp_advertise();
23+
/*start advertising, when controller and host are in sync*/
24+
bleprhp_advertise();
2525
}

main/gap.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include "main.hpp"
2+
3+
static void bleprhp_advertise(void){
4+
struct ble_gap_adv_params adv_params;
5+
struct ble_hs_adv_fields fields;
6+
int rc;
7+
8+
/*
9+
set the advertisement data included in our advertisements:
10+
-flags (indicates advertisement type and other gernal info)
11+
-advertising tx power
12+
-device name
13+
*/
14+
memset(&fields, 0, sizeof(fields));
15+
16+
/*advertise two flags:
17+
-discoverability in forhtcoming advertisement (general)
18+
-ble-only (br/edr unsupported)
19+
*/
20+
fields.flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP;
21+
22+
/*inidcate that the tx power level field should be included; have the stack fill this value automatically. This is done ba assigning the special value BLE_HS_ADV_TX_PWR_LVL_AUTO.
23+
*/
24+
fields.tx_pwr_lvl_is_present = 1;
25+
fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
26+
27+
fields.name = (uint8_t *)CONFIG_BT_NIMBLE_SVC_GAP_DEVICE_NAME;
28+
fields.name_len = strlen(CONFIG_BT_NIMBLE_SVC_GAP_DEVICE_NAME);
29+
fields.name_is_complete = 1;
30+
31+
rc = ble_gap_adv_set_fields(&fields);
32+
if (rc != 0){
33+
ESP_LOGE(tag, "error setting advertisement data; rc=%d\n",rc);
34+
}
35+
36+
/*Begin advertising*/
37+
memset(&adv_params, 0, sizeof(adv_params));
38+
//TODO
39+
adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
40+
adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
41+
rc = ble_gap_adv_start(bleprhp_addr_type, NULL, BLE_HS_FOREVER, &adv_params, bleprhp_gap_event, NULL);
42+
if (rc != 0){
43+
ESP_LOGE(tag, "error enabling advertisement; rc=%d\n",rc);
44+
}
45+
}
46+
47+
static int bleprhp_gap_event(struct ble_gap_event *event, void *arg){
48+
switch(event->type){
49+
case BLE_GAP_EVENT_CONNECT:
50+
//A new connection was establied or a connection attempt failed
51+
ESP_LOGI(tag, "connection %s; status=%d\n",event->connect.status == 0 ? "established" : "failed",event->connect.status);
52+
53+
if(event->connect.status != 0){
54+
//Conection failed; resume advertising
55+
bleprhp_advertise();
56+
}
57+
conn_handle = event->connect.conn_handle;
58+
break;
59+
case BLE_GAP_EVENT_DISCONNECT:
60+
ESP_LOGI(tag, "disconnect; reason%d\n", event->disconnect.reason);
61+
//connection terminated; resume advertising
62+
bleprhp_advertise();
63+
break;
64+
case BLE_GAP_EVENT_ADV_COMPLETE:
65+
//TODO
66+
ESP_LOGI(tag, "adv complete\n");
67+
bleprhp_advertise();
68+
break;
69+
case BLE_GAP_EVENT_SUBSCRIBE:
70+
//TODO
71+
/*ESP_LOGI(tag, "subscribe event; cur_notify=%d\n value handle; val_handle=%d\n", event->subscribe.cur_notify, hrs_hrm_handle);
72+
if(event->subscribe.attr_handle == hrs_hrm_handle){
73+
notify_state = event->subscribe.cur_notify;
74+
blehr_tx_hrate_reset();
75+
} else if (event->subscribe.attr_handle != hrs_hrm_handle){
76+
notify_state = event->subscribe.cur_notify;
77+
blehr_tx_hrate_stop();
78+
}*/
79+
ESP_LOGI(tag, "conn_handle from subscribe=%d", conn_handle);
80+
break;
81+
case BLE_GAP_EVENT_MTU:
82+
ESP_LOGI(tag, "mtu update event; conn_handle=%d mtu=%d\n", event->mtu.conn_handle, event->mtu.value);
83+
break;
84+
}
85+
86+
return 0;
87+
}

main/gatt.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "main.hpp"
2+
3+
int gatt_svr_init(void){
4+
int rc;
5+
6+
ble_svc_gap_init();
7+
ble_svc_gatt_init();
8+
9+
//TODO
10+
rc = ble_gatts_count_cfg(gatt_svr_srvcs);
11+
12+
}

main/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ extern "C" void app_main(void)
77
int rc;
88

99
/*Initialize NVS - it is used to store PHY calibration data*/
10+
//NVS = Non-volatile storage library is designed to store key-balue pairs in flash
1011
//Ablauf ist in esp idf beschrieben
1112
//docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/bluetooth/nimble/index.html
1213
esp_err_t ret = nvs_flash_init();
@@ -37,7 +38,7 @@ extern "C" void app_main(void)
3738
ble_hs_cfg.sm_their_key_dist = 1;
3839

3940
//TODO
40-
//rc = gatt_svr_init();
41+
rc = gatt_svr_init();
4142
assert(rc == 0);
4243

4344
/*Set the default device name. */

main/main.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,29 @@
1414
#include "nimble/nimble_port_freertos.h"
1515
#include "host/ble_hs.h"
1616
#include "services/gap/ble_svc_gap.h"
17+
#include "services/gatt/ble_svc_gatt.h"
1718

1819
extern "C" {
1920

2021
static const char *tag = "SimLinkModule_BLE";
2122

2223
static uint8_t bleprhp_addr_type;
2324

25+
//eindeutiges handle was bei einem verbindungsaufbau einer verbindung zugeordnet wird
26+
static uint16_t conn_handle;
27+
2428
//start nimble in a task
2529
void bleprph_host_task(void *param);
2630
//This callback is executed when the host resets itself and the controller
2731
static void bleprhp_on_reset(int reason);
2832
//This callback is executed when the host and controller become synced. This happens at startup and after a reset
2933
static void bleprhp_on_sync(void);
34+
//enables advertising with parameters: general discoverable mode and unidrect connectable mode
35+
static void bleprhp_advertise(void);
36+
//The callback to associate with this advertising procedure. If advertising ends, the event is reported through this callback. If advertising results in a connection, the connection inherits this callback as its event-reporting mechanism.
37+
static int bleprhp_gap_event(struct ble_gap_event *event, void *arg);
38+
//init gatt server
39+
int gatt_svr_init(void);
3040
}
3141

3242
#endif

0 commit comments

Comments
 (0)