Skip to content

Commit e4549fa

Browse files
committed
using a timer to set a config mode
1 parent ed4f01a commit e4549fa

3 files changed

Lines changed: 93 additions & 20 deletions

File tree

apps/firmware/src/ble_stack.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,27 @@ void advertising_init(int interval)
358358
ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
359359
}
360360

361+
/**
362+
* updateAdvertisementData will update the data to be advertised
363+
*/
364+
void updateAdvertisementData(uint8_t *data, uint8_t dlen) {
365+
uint32_t err_code;
366+
367+
setAdvertisementData(data, dlen);
368+
369+
err_code = sd_ble_gap_adv_stop(m_advertising.adv_handle);
370+
APP_ERROR_CHECK(err_code);
371+
372+
err_code = sd_ble_gap_adv_set_configure(&m_advertising.adv_handle,
373+
m_advertising.p_adv_data,
374+
NULL);
375+
APP_ERROR_CHECK(err_code);
376+
377+
err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
378+
APP_ERROR_CHECK(err_code);
379+
380+
return;
381+
}
361382

362383
/**
363384
* setAdvertisementData will set the data to be advertised

apps/firmware/src/ble_stack.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,31 @@ void init_ble();
2222
void setMacAddress(uint8_t *addr);
2323

2424
/**
25-
* startAdvertisingWithData will set the data to be advertised
25+
* setAdvertisementData will set the data to be advertised
2626
*
2727
* @param[in] data raw data to be advertised
2828
* @param[in] dlen length of the data
29-
* @param[in] interval advertising interval in milliseconds
3029
*/
3130
void setAdvertisementData(uint8_t *data, uint8_t dlen);
3231

3332
/**
34-
* Start advertising at the specified interval
33+
* updateAdvertisementData will update the data to be advertised
34+
*
35+
* @param[in] data raw data to be advertised
36+
* @param[in] dlen length of the data
37+
*/
38+
void updateAdvertisementData(uint8_t *data, uint8_t dlen);
39+
40+
/**
41+
* Start advertising
3542
*/
3643
void startAdvertisement();
3744

45+
/**
46+
* Initialize advertising with specified interval in msec
47+
*/
48+
void advertising_init(int interval);
49+
3850
/**
3951
* Function for the Power manager.
4052
*/
@@ -46,7 +58,6 @@ void power_manage(void);
4658
void peer_manager_init();
4759
void gap_params_init(void);
4860
void gatt_init(void);
49-
void advertising_init(int interval);
5061
void services_init(void);
5162
void conn_params_init(void);
5263
void ble_dfu_buttonless_evt_handler(ble_dfu_buttonless_evt_type_t event);

apps/firmware/src/main.c

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@
3636
*/
3737
#define ADVERTISING_INTERVAL 3000
3838

39+
/**
40+
* config mode timeout
41+
*
42+
* This setting will specify for how long the device will remain on "config mode" before start
43+
* advertising the location beacon. Actually you can config the device on "advertising mode" but
44+
* on "config mode" it will broadcast its name.
45+
*/
46+
#define CONFIG_MODE_TIMEOUT APP_TIMER_TICKS(60000L) // 60 seconds
47+
48+
// Define the "config mode" timer
49+
APP_TIMER_DEF(config_mode_timer);
50+
51+
/**
52+
* This is the default public key. You can either modify this variable or patch the binary firmware
53+
* once compiled, which is the preferred way since it can be done without recompilation.
54+
*/
3955
static char public_key[28] = "OFFLINEFINDINGPUBLICKEYHERE!";
4056

4157
//Test1
@@ -64,17 +80,43 @@ static void timers_init(void)
6480
uint32_t err_code = app_timer_init();
6581
APP_ERROR_CHECK(err_code);
6682

67-
// Create timers.
83+
err_code = app_timer_create(&config_mode_timer,
84+
APP_TIMER_MODE_SINGLE_SHOT,
85+
config_timer_handler);
86+
APP_ERROR_CHECK(err_code);
87+
88+
89+
}
90+
91+
/**@brief The handler of the config timer
92+
*
93+
* @details this function will be called when the timer timeouts
94+
*/
95+
static void config_timer_handler(void * p_context)
96+
{
97+
// Variable to hold the data to advertise
98+
uint8_t *ble_address;
99+
uint8_t *raw_data;
100+
uint8_t data_len;
101+
102+
// Set key to be advertised
103+
data_len = setAdvertisementKey(public_key, &ble_address, &raw_data);
68104

69-
/* YOUR_JOB: Create any timers to be used by the application.
70-
Below is an example of how to create a timer.
71-
For every new timer needed, increase the value of the macro APP_TIMER_MAX_TIMERS by
72-
one.
73-
uint32_t err_code;
74-
err_code = app_timer_create(&m_app_timer_id, APP_TIMER_MODE_REPEATED, timer_timeout_handler);
75-
APP_ERROR_CHECK(err_code); */
105+
// Update advertisement
106+
updateAdvertisementData(raw_data, data_len);
76107
}
77108

109+
/**@brief Function to start the timers
110+
*
111+
*/
112+
static void timers_start(void)
113+
{
114+
ret_code_t err_code;
115+
err_code = app_timer_start(config_mode_timer,
116+
CONFIG_MODE_TIMEOUT,
117+
NULL);
118+
APP_ERROR_CHECK(err_code);
119+
}
78120

79121
/**@brief Function for the Power manager.
80122
*/
@@ -100,10 +142,9 @@ int main(void) {
100142
// Variable to hold the data to advertise
101143
uint8_t *ble_address;
102144
uint8_t *raw_data;
103-
uint8_t data_len;
104145

105146
// Set key to be advertised
106-
data_len = setAdvertisementKey(public_key, &ble_address, &raw_data);
147+
setAdvertisementKey(public_key, &ble_address, &raw_data);
107148

108149
// Timers
109150
timers_init();
@@ -121,7 +162,11 @@ int main(void) {
121162

122163
advertising_init(ADVERTISING_INTERVAL);
123164

124-
// Set advertisement data
165+
// Enable services
166+
services_init();
167+
conn_params_init();
168+
169+
// Start timers if the device is configured
125170
if (public_key[0] == 'O' &&
126171
public_key[1] == 'F' &&
127172
public_key[2] == 'F' &&
@@ -131,13 +176,9 @@ int main(void) {
131176
public_key[6] == 'E') {
132177
// Leave unconfigured
133178
} else {
134-
setAdvertisementData(raw_data, data_len);
179+
timers_start();
135180
}
136181

137-
// Enable services
138-
services_init();
139-
conn_params_init();
140-
141182
// Start advertising
142183
startAdvertisement();
143184

0 commit comments

Comments
 (0)