Skip to content
This repository was archived by the owner on Sep 20, 2022. It is now read-only.

Commit a71d184

Browse files
committed
add configurator application
1 parent 02efd7c commit a71d184

File tree

7 files changed

+872
-0
lines changed

7 files changed

+872
-0
lines changed

configurator/Makefile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# name of your application
2+
APPLICATION = configurator
3+
4+
# If no BOARD is found in the environment, use this default:
5+
BOARD ?= nrf52dk
6+
7+
# So far, NimBLE only works on nRF52 based platforms
8+
BOARD_WHITELIST := nrf52dk nrf52840dk
9+
10+
# This has to be the absolute path to the RIOT base directory:
11+
RIOTBASE ?= $(CURDIR)/../RIOT
12+
13+
# Include NimBLE
14+
USEPKG += nimble
15+
16+
USEMODULE += nimble_svc_gap
17+
USEMODULE += nimble_svc_gatt
18+
19+
FEATURES_REQUIRED += periph_flashpage
20+
21+
# Comment this out to disable code in RIOT that does safety checking
22+
# which is not needed in a production environment but helps in the
23+
# development process:
24+
DEVELHELP ?= 1
25+
26+
CFLAGS += -DDEBUG_ASSERT_VERBOSE
27+
28+
# Change this to 0 show compiler invocation lines by default:
29+
QUIET ?= 1
30+
31+
include $(RIOTBASE)/Makefile.include

configurator/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## About
2+
3+
This application provides a simple Bluetooth LE service (via RIOT's
4+
NimBLE port) for device configuration purposes. The included bluetooth
5+
characteristics are taken from an
6+
[application](https://github.com/HendrikVE/smarthome2/tree/master/ESP32/window_alert_riot) for
7+
a sensor node, which is placed in a specific room and transmits data to
8+
a MQTT-SN gateway.
9+
The sent values ​​are stored persistently in MCU's internal flash via
10+
RIOT's flashpage API.
11+
12+
There is an Android App specifically written for this application.
13+
Either build the app yourself
14+
[from source](https://github.com/HendrikVE/smarthome2/tree/master/AndroidApp/WindowAlarmConfig),
15+
download it from
16+
[here](https://play.google.com/store/apps/details?id=de.vanappsteer.windowalarmconfig)
17+
or use Nordics "nRF Connect"-App, available for
18+
[Android](https://play.google.com/store/apps/details?id=no.nordicsemi.android.mcp)
19+
and
20+
[iOS](https://itunes.apple.com/us/app/nrf-connect/id1054362403).
21+
22+
In the case you want to use Nordics app, you will need to write an empty string to the
23+
characteristic "890f7b6f-cecc-4e3e-ade2-5f2907867f4b" in the end of the configuration process
24+
to store the data permanently and to restart the device afterwards.
25+
26+
Currently the application is only running on *nrf52dk* and *nrf52840dk* boards.
27+
28+
## Clean install
29+
30+
Add `JLINK_PRE_FLASH=erase` to your make command to erase the flash memory.
31+
That way you avoid data left in the storage, especially on the flash page
32+
used for configuration.

configurator/ble_service.h

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
#ifndef BLE_SERVICE_H
2+
#define BLE_SERVICE_H
3+
4+
#include "host/ble_uuid.h"
5+
6+
#define GATT_DEVICE_INFO_UUID 0x180A
7+
#define GATT_MANUFACTURER_NAME_UUID 0x2A29
8+
#define GATT_MODEL_NUMBER_UUID 0x2A24
9+
10+
/* CONFIG SERVICE UUID */
11+
/* UUID = 2fa1dab8-3eef-40fc-8540-7fc496a10d75 */
12+
static const ble_uuid128_t gatt_svr_svc_cfg_uuid
13+
= BLE_UUID128_INIT(0x75, 0x0d, 0xa1, 0x96, 0xc4, 0x7f, 0x40, 0x85, 0xfc,
14+
0x40, 0xef, 0x3e, 0xb8, 0xda, 0xa1, 0x2f);
15+
16+
/* CONFIG DEVICE */
17+
/* UUID = d3491796-683b-4b9c-aafb-f51a35459d43 */
18+
static const ble_uuid128_t gatt_svr_chr_cfg_device_room_uuid
19+
= BLE_UUID128_INIT(0x43, 0x9d, 0x45, 0x35, 0x1a, 0xf5, 0xfb, 0xaa, 0x9c,
20+
0x4b, 0x3b, 0x68, 0x96, 0x17, 0x49, 0xd3);
21+
22+
/* UUID = 4745e11f-b403-4cfb-83bb-710d46897875 */
23+
static const ble_uuid128_t gatt_svr_chr_cfg_device_id_uuid
24+
= BLE_UUID128_INIT(0x75, 0x78, 0x89, 0x46, 0x0d, 0x71, 0xbb, 0x83, 0xfb,
25+
0x4c, 0x03, 0xb4, 0x1f, 0xe1, 0x45, 0x47);
26+
27+
/* CONFIG OTA */
28+
/* UUID = 2f44b103-444c-48f5-bf60-91b81dfa0a25 */
29+
static const ble_uuid128_t gatt_svr_chr_cfg_ota_host_uuid
30+
= BLE_UUID128_INIT(0x25, 0x0a, 0xfa, 0x1d, 0xb8, 0x91, 0x60, 0xbf, 0xf5,
31+
0x48, 0x4c, 0x44, 0x03, 0xb1, 0x44, 0x2f);
32+
33+
/* UUID = 4b95d245-db08-4c56-98f9-738faa8cfbb6 */
34+
static const ble_uuid128_t gatt_svr_chr_cfg_ota_filename_uuid
35+
= BLE_UUID128_INIT(0xb6, 0xfb, 0x8c, 0xaa, 0x8f, 0x73, 0xf9, 0x98, 0x56,
36+
0x4c, 0x08, 0xdb, 0x45, 0xd2, 0x95, 0x4b);
37+
38+
/* UUID = 1c93dce2-3796-4027-9f55-6d251c41dd34 */
39+
static const ble_uuid128_t gatt_svr_chr_cfg_ota_server_username_uuid
40+
= BLE_UUID128_INIT(0x34, 0xdd, 0x41, 0x1c, 0x25, 0x6d, 0x55, 0x9f, 0x27,
41+
0x40, 0x96, 0x37, 0xe2, 0xdc, 0x93, 0x1c);
42+
43+
/* UUID = 0e837309-5336-45a3-9b69-d0f7134f30ff */
44+
static const ble_uuid128_t gatt_svr_chr_cfg_ota_server_password_uuid
45+
= BLE_UUID128_INIT(0xff, 0x30, 0x4f, 0x13, 0xf7, 0xd0, 0x69, 0x9b, 0xa3,
46+
0x45, 0x36, 0x53, 0x09, 0x73, 0x83, 0x0e);
47+
48+
/* CONFIG WiFi */
49+
/* UUID = 8ca0bf1d-bb5d-4a66-9191-341fd805e288 */
50+
static const ble_uuid128_t gatt_svr_chr_cfg_wifi_ssid_uuid
51+
= BLE_UUID128_INIT(0x88, 0xe2, 0x05, 0xd8, 0x1f, 0x34, 0x91, 0x91, 0x66,
52+
0x4a, 0x5d, 0xbb, 0x1d, 0xbf, 0xa0, 0x8c);
53+
54+
/* UUID = fa41c195-ae99-422e-8f1f-0730702b3fc5 */
55+
static const ble_uuid128_t gatt_svr_chr_cfg_wifi_password_uuid
56+
= BLE_UUID128_INIT(0xc5, 0x3f, 0x2b, 0x70, 0x30, 0x07, 0x1f, 0x8f, 0x2e,
57+
0x42, 0x99, 0xae, 0x95, 0xc1, 0x41, 0xfa);
58+
59+
/* CONFIG MQTT */
60+
/* UUID = 69150609-18f8-4523-a41f-6d9a01d2e08d */
61+
static const ble_uuid128_t gatt_svr_chr_cfg_mqtt_user_uuid
62+
= BLE_UUID128_INIT(0x8d, 0xe0, 0xd2, 0x01, 0x9a, 0x6d, 0x1f, 0xa4, 0x23,
63+
0x45, 0xf8, 0x18, 0x09, 0x06, 0x15, 0x69);
64+
65+
/* UUID = 8bebec77-ea21-4c14-9d64-dbec1fd5267c */
66+
static const ble_uuid128_t gatt_svr_chr_cfg_mqtt_password_uuid
67+
= BLE_UUID128_INIT(0x7c, 0x26, 0xd5, 0x1f, 0xec, 0xdb, 0x64, 0x9d, 0x14,
68+
0x4c, 0x21, 0xea, 0x77, 0xec, 0xeb, 0x8b);
69+
70+
/* UUID = e3b150fb-90a2-4cd3-80c5-b1189e110ef1 */
71+
static const ble_uuid128_t gatt_svr_chr_cfg_mqtt_server_ip_uuid
72+
= BLE_UUID128_INIT(0xf1, 0x0e, 0x11, 0x9e, 0x18, 0xb1, 0xc5, 0x80, 0xd3,
73+
0x4c, 0xa2, 0x90, 0xfb, 0x50, 0xb1, 0xe3);
74+
75+
/* UUID = 4eeff953-0f5e-43ee-b1be-1783a8190b0d */
76+
static const ble_uuid128_t gatt_svr_chr_cfg_mqtt_server_port_uuid
77+
= BLE_UUID128_INIT(0x0d, 0x0b, 0x19, 0xa8, 0x83, 0x17, 0xbe, 0xb1, 0xee,
78+
0x43, 0x5e, 0x0f, 0x53, 0xf9, 0xef, 0x4e);
79+
80+
/* CONFIG SENSOR */
81+
/* UUID = 68011c92-854a-4f2c-a94c-5ee37dc607c3 */
82+
static const ble_uuid128_t gatt_svr_chr_cfg_sensor_poll_interval_ms_uuid
83+
= BLE_UUID128_INIT(0xc3, 0x07, 0xc6, 0x7d, 0xe3, 0x5e, 0x4c, 0xa9, 0x2c,
84+
0x4f, 0x4a, 0x85, 0x92, 0x1c, 0x01, 0x68);
85+
86+
/* RESTART */
87+
/* UUID = 890f7b6f-cecc-4e3e-ade2-5f2907867f4b */
88+
static const ble_uuid128_t gatt_svr_chr_cfg_restart_uuid
89+
= BLE_UUID128_INIT(0x4b, 0x7f, 0x86, 0x07, 0x29, 0x5f, 0xe2, 0xad, 0x3e,
90+
0x4e, 0xcc, 0xce, 0x6f, 0x7b, 0x0f, 0x89);
91+
92+
static int gatt_svr_chr_access_device_info_manufacturer(
93+
uint16_t conn_handle, uint16_t attr_handle,
94+
struct ble_gatt_access_ctxt *ctxt, void *arg);
95+
96+
static int gatt_svr_chr_access_device_info_model(
97+
uint16_t conn_handle, uint16_t attr_handle,
98+
struct ble_gatt_access_ctxt *ctxt, void *arg);
99+
100+
static int gatt_svr_chr_access_rw_demo(
101+
uint16_t conn_handle, uint16_t attr_handle,
102+
struct ble_gatt_access_ctxt *ctxt, void *arg);
103+
104+
/* define several bluetooth services for our device */
105+
static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
106+
/*
107+
* access_cb defines a callback for read and write access events on
108+
* given characteristics
109+
*/
110+
{
111+
/* Service: Device Information */
112+
.type = BLE_GATT_SVC_TYPE_PRIMARY,
113+
.uuid = BLE_UUID16_DECLARE(GATT_DEVICE_INFO_UUID),
114+
.characteristics = (struct ble_gatt_chr_def[]) { {
115+
/* Characteristic: * Manufacturer name */
116+
.uuid = BLE_UUID16_DECLARE(GATT_MANUFACTURER_NAME_UUID),
117+
.access_cb = gatt_svr_chr_access_device_info_manufacturer,
118+
.flags = BLE_GATT_CHR_F_READ,
119+
}, {
120+
/* Characteristic: Model number string */
121+
.uuid = BLE_UUID16_DECLARE(GATT_MODEL_NUMBER_UUID),
122+
.access_cb = gatt_svr_chr_access_device_info_model,
123+
.flags = BLE_GATT_CHR_F_READ,
124+
}, {
125+
0, /* No more characteristics in this service */
126+
}, }
127+
},
128+
{
129+
/* Service: Config */
130+
.type = BLE_GATT_SVC_TYPE_PRIMARY,
131+
.uuid = (ble_uuid_t*) &gatt_svr_svc_cfg_uuid.u,
132+
.characteristics = (struct ble_gatt_chr_def[]) { {
133+
/* Characteristic: Config device room */
134+
.uuid = (ble_uuid_t*) &gatt_svr_chr_cfg_device_room_uuid.u,
135+
.access_cb = gatt_svr_chr_access_rw_demo,
136+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
137+
},{
138+
/* Characteristic: Config device id */
139+
.uuid = (ble_uuid_t*) &gatt_svr_chr_cfg_device_id_uuid.u,
140+
.access_cb = gatt_svr_chr_access_rw_demo,
141+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
142+
},{
143+
/* Characteristic: Config ota host */
144+
.uuid = (ble_uuid_t*) &gatt_svr_chr_cfg_ota_host_uuid.u,
145+
.access_cb = gatt_svr_chr_access_rw_demo,
146+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
147+
},{
148+
/* Characteristic: Config ota filename */
149+
.uuid = (ble_uuid_t*) &gatt_svr_chr_cfg_ota_filename_uuid.u,
150+
.access_cb = gatt_svr_chr_access_rw_demo,
151+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
152+
},{
153+
/* Characteristic: Config ota server username */
154+
.uuid = (ble_uuid_t*) &gatt_svr_chr_cfg_ota_server_username_uuid.u,
155+
.access_cb = gatt_svr_chr_access_rw_demo,
156+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
157+
},{
158+
/* Characteristic: Config ota server password */
159+
.uuid = (ble_uuid_t*) &gatt_svr_chr_cfg_ota_server_password_uuid.u,
160+
.access_cb = gatt_svr_chr_access_rw_demo,
161+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
162+
},{
163+
/* Characteristic: Config wifi ssid */
164+
.uuid = (ble_uuid_t*) &gatt_svr_chr_cfg_wifi_ssid_uuid.u,
165+
.access_cb = gatt_svr_chr_access_rw_demo,
166+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
167+
},{
168+
/* Characteristic: Config wifi password */
169+
.uuid = (ble_uuid_t*) &gatt_svr_chr_cfg_wifi_password_uuid.u,
170+
.access_cb = gatt_svr_chr_access_rw_demo,
171+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
172+
},{
173+
/* Characteristic: Config mqtt user */
174+
.uuid = (ble_uuid_t*) &gatt_svr_chr_cfg_mqtt_user_uuid.u,
175+
.access_cb = gatt_svr_chr_access_rw_demo,
176+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
177+
},{
178+
/* Characteristic: Config mqtt password */
179+
.uuid = (ble_uuid_t*) &gatt_svr_chr_cfg_mqtt_password_uuid.u,
180+
.access_cb = gatt_svr_chr_access_rw_demo,
181+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
182+
},{
183+
/* Characteristic: Config mqtt server ip */
184+
.uuid = (ble_uuid_t*) &gatt_svr_chr_cfg_mqtt_server_ip_uuid.u,
185+
.access_cb = gatt_svr_chr_access_rw_demo,
186+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
187+
},{
188+
/* Characteristic: Config mqtt server port */
189+
.uuid = (ble_uuid_t*) &gatt_svr_chr_cfg_mqtt_server_port_uuid.u,
190+
.access_cb = gatt_svr_chr_access_rw_demo,
191+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
192+
},{
193+
/* Characteristic: Config sensor poll interval ms */
194+
.uuid = (ble_uuid_t*) &gatt_svr_chr_cfg_sensor_poll_interval_ms_uuid.u,
195+
.access_cb = gatt_svr_chr_access_rw_demo,
196+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
197+
},{
198+
/* Characteristic: Config save and restart */
199+
.uuid = (ble_uuid_t*) &gatt_svr_chr_cfg_restart_uuid.u,
200+
.access_cb = gatt_svr_chr_access_rw_demo,
201+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
202+
},{
203+
0, /* No more characteristics in this service */
204+
}, }
205+
},
206+
{
207+
0, /* No more services */
208+
},
209+
};
210+
211+
#endif /*BLE_SERVICE_H*/

configurator/config.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
#include "periph/flashpage.h"
5+
6+
#include "config.h"
7+
8+
static int read_page(int page, uint8_t buffer[]);
9+
static int write_page(int page, uint8_t buffer[]);
10+
11+
void config_read(union config_flashpage* config)
12+
{
13+
read_page(FLASHPAGE_NUM_CONFIG, config->page_mem);
14+
}
15+
16+
void config_store(union config_flashpage* config)
17+
{
18+
write_page(FLASHPAGE_NUM_CONFIG, config->page_mem);
19+
}
20+
21+
static int read_page(int page, uint8_t buffer[])
22+
{
23+
if ((page >= (int)FLASHPAGE_NUMOF) || (page < 0)) {
24+
printf("error: page %i is invalid\n", page);
25+
return 1;
26+
}
27+
28+
flashpage_read(page, buffer);
29+
printf("Read flash page %i into local page buffer\n", page);
30+
31+
return 0;
32+
}
33+
34+
static int write_page(int page, uint8_t buffer[])
35+
{
36+
if ((page >= (int)FLASHPAGE_NUMOF) || (page < 0)) {
37+
printf("error: page %i is invalid\n", page);
38+
return 1;
39+
}
40+
41+
if (flashpage_write_and_verify(page, buffer) != FLASHPAGE_OK) {
42+
printf("error: verification for page %i failed\n", page);
43+
return 1;
44+
}
45+
46+
printf("wrote local page buffer to flash page %i at addr %p\n",
47+
page, flashpage_addr(page));
48+
49+
return 0;
50+
}

configurator/config.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#ifndef CONFIG_H
2+
#define CONFIG_H
3+
4+
#include <assert.h>
5+
6+
#define ALIGNMENT_ATTR
7+
#define FLASHPAGE_NUM_CONFIG FLASHPAGE_NUMOF - 1
8+
9+
#define CONFIG_DEVICE_ROOM "CONFIG_DEVICE_ROOM"
10+
#define CONFIG_DEVICE_ID "CONFIG_DEVICE_ID"
11+
#define CONFIG_OTA_HOST "CONFIG_OTA_HOST"
12+
#define CONFIG_OTA_FILENAME "CONFIG_OTA_FILENAME"
13+
#define CONFIG_OTA_SERVER_USERNAME "CONFIG_OTA_SERVER_USERNAME"
14+
#define CONFIG_OTA_SERVER_PASSWORD "CONFIG_OTA_SERVER_PASSWORD"
15+
#define CONFIG_ESP_WIFI_SSID "CONFIG_ESP_WIFI_SSID"
16+
#define CONFIG_ESP_WIFI_PASSWORD "CONFIG_ESP_WIFI_PASSWORD"
17+
#define CONFIG_MQTT_USER "CONFIG_MQTT_USER"
18+
#define CONFIG_MQTT_PASSWORD "CONFIG_MQTT_PASSWORD"
19+
#define CONFIG_MQTT_SERVER_IP "CONFIG_MQTT_SERVER_IP"
20+
#define CONFIG_MQTT_SERVER_PORT "CONFIG_MQTT_SERVER_PORT"
21+
#define CONFIG_SENSOR_POLL_INTERVAL_MS "CONFIG_SENSOR_POLL_INTERVAL_MS"
22+
23+
struct config_values {
24+
uint8_t device_room[256];
25+
uint8_t device_id[256];
26+
uint8_t ota_host[256];
27+
uint8_t ota_filename[256];
28+
uint8_t ota_server_username[256];
29+
uint8_t ota_server_password[256];
30+
uint8_t wifi_ssid[256];
31+
uint8_t wifi_password[256];
32+
uint8_t mqtt_user[256];
33+
uint8_t mqtt_password[256];
34+
uint8_t mqtt_server_ip[256];
35+
uint8_t mqtt_server_port[32];
36+
uint8_t sensor_poll_interval_ms[32];
37+
};
38+
39+
static_assert(FLASHPAGE_SIZE >= 4096, "condition not met: FLASHPAGE_SIZE >= 4096");
40+
union config_flashpage {
41+
struct config_values config_values;
42+
uint8_t page_mem[FLASHPAGE_SIZE] ALIGNMENT_ATTR;
43+
};
44+
45+
void config_read(union config_flashpage* config);
46+
void config_store(union config_flashpage* config);
47+
48+
#endif /*CONFIG_H*/

0 commit comments

Comments
 (0)