Skip to content

Commit 1bb6072

Browse files
committed
familiarization demo code
part 1
1 parent 3ffa6a7 commit 1bb6072

File tree

10 files changed

+154
-1
lines changed

10 files changed

+154
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build/
2+
sdkconfig
3+
sdkconfig.old

.vscode/c_cpp_properties.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Linux",
5+
"includePath": [
6+
"${workspaceFolder}/**",
7+
"${workspaceFolder}/build/config/**",
8+
"${env:HOME}/esp/esp-idf/**"
9+
],
10+
"defines": [],
11+
"compilerPath": "/usr/bin/gcc",
12+
"cStandard": "gnu17",
13+
"cppStandard": "gnu++17",
14+
"intelliSenseMode": "linux-gcc-x64"
15+
}
16+
],
17+
"version": 4
18+
}

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"idf.adapterTargetName": "esp32",
3+
"idf.port": "/dev/ttyUSB0",
4+
"idf.flashType": "UART"
5+
}

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# For more information about build system see
2+
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
3+
# The following five lines of boilerplate have to be in your project's
4+
# CMakeLists in this exact order for cmake to work correctly
5+
cmake_minimum_required(VERSION 3.5)
6+
7+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
8+
project(SimLinkSoftware)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# Software
1+
# Main ESP32 Code

links

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
github.com/apache/mynewt-nimble/blob/master/apps/bleprph/src/main.c
2+
github.com/apache/mynewt-nimble/blob/master/apps/blehr/src/main.c
3+
github.com/espressif/esp-idf/blob/master/examples/bluetooth/nimble/blehr/main/main.c
4+
github.com/espressif/esp-idf/blob/master/examples/bluetooth/nimble/bleprph/main/main.c
5+
mynewt.apache.org/latest/network/ble_hs/ble_att.html
6+
docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/bluetooth/nimble.index.html
7+
https://iotespresso.com/esp32-nimble-how-to-send-notifications/

main/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
set(srcs "main.cpp"
2+
"functions.cpp")
3+
4+
idf_component_register(SRCS "${srcs}"
5+
INCLUDE_DIRS ".")
6+
7+
#hier muss kein required oder ähnliches rein, weil main alles required

main/functions.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "main.hpp"
2+
3+
void bleprph_host_task(void *param){
4+
ESP_LOGI(tag, "BLE Host Task Started");
5+
/*This function will return only when nimble_port_stop() is executed*/
6+
nimble_port_run();
7+
8+
nimble_port_freertos_deinit();
9+
}
10+
11+
static void bleprhp_on_reset(int reason){
12+
ESP_LOGE(tag, "Resetting state; reason=%d\n",reason);
13+
}
14+
15+
static void bleprhp_on_sync(void){
16+
//save return code
17+
int rc;
18+
19+
/*use privacy*/
20+
rc = ble_hs_id_infer_auto(0, &bleprhp_addr_type);
21+
assert(rc == 0);
22+
23+
//TODO
24+
//bleprhp_advertise();
25+
}

main/main.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "main.hpp"
2+
3+
extern "C" void app_main(void)
4+
{
5+
//returncodes von nimble functionen zwischenspeichern --> geben viele informationen über den status der funktionen
6+
//mynewt.apache.org/latest/network/ble_hs/ble_hs_return_codes.html
7+
int rc;
8+
9+
/*Initialize NVS - it is used to store PHY calibration data*/
10+
//Ablauf ist in esp idf beschrieben
11+
//docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/bluetooth/nimble/index.html
12+
esp_err_t ret = nvs_flash_init();
13+
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND){
14+
ESP_ERROR_CHECK(nvs_flash_erase());
15+
ret = nvs_flash_init();
16+
}
17+
ESP_ERROR_CHECK(ret);
18+
19+
nimble_port_init();
20+
/*Initialize the NimBLE host configuration.*/
21+
ble_hs_cfg.sync_cb = bleprhp_on_sync;
22+
ble_hs_cfg.reset_cb = bleprhp_on_reset;
23+
/*Security Manager local input output capabilities*/
24+
ble_hs_cfg.sm_io_cap = BLE_SM_IO_CAP_DISP_YES_NO;
25+
/*security Manager bond flag
26+
if set proper flag in Pairing request/response will be set. This results in storing keys distributed during bonding.*/
27+
ble_hs_cfg.sm_bonding = 1;
28+
/*security manager mitm flag
29+
if set proper flag in pairing request/response will be set. This results in requiring man-in-the-middle protection when pairing.*/
30+
ble_hs_cfg.sm_mitm = 1;
31+
/*Security Manager secure connections flag
32+
if set proper flag in pairing request/response will be set. this results in using LE Secure Connections for pairing if also supported by remote device. Fallback to legacy pairing if not supported by remote.*/
33+
ble_hs_cfg.sm_sc = 1;
34+
/*Security Manager Local Key Distribution Mask*/
35+
ble_hs_cfg.sm_our_key_dist = 1;
36+
/*Security Manager Remote Key Distribution Mask*/
37+
ble_hs_cfg.sm_their_key_dist = 1;
38+
39+
//TODO
40+
//rc = gatt_svr_init();
41+
assert(rc == 0);
42+
43+
/*Set the default device name. */
44+
rc = ble_svc_gap_device_name_set(CONFIG_BT_NIMBLE_SVC_GAP_DEVICE_NAME);
45+
assert(rc == 0);
46+
47+
nimble_port_freertos_init(bleprph_host_task);
48+
}

main/main.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef H_MAIN
2+
#define H_MAIN
3+
4+
#include <cstdlib>
5+
#include <stdio.h>
6+
#include "freertos/FreeRTOS.h"
7+
#include "freertos/task.h"
8+
#include "sdkconfig.h"
9+
#include "esp_log.h"
10+
11+
//esp32
12+
#include "nvs_flash.h"
13+
#include "nimble/nimble_port.h"
14+
#include "nimble/nimble_port_freertos.h"
15+
#include "host/ble_hs.h"
16+
#include "services/gap/ble_svc_gap.h"
17+
18+
extern "C" {
19+
20+
static const char *tag = "SimLinkModule_BLE";
21+
22+
static uint8_t bleprhp_addr_type;
23+
24+
//start nimble in a task
25+
void bleprph_host_task(void *param);
26+
//This callback is executed when the host resets itself and the controller
27+
static void bleprhp_on_reset(int reason);
28+
//This callback is executed when the host and controller become synced. This happens at startup and after a reset
29+
static void bleprhp_on_sync(void);
30+
}
31+
32+
#endif

0 commit comments

Comments
 (0)