Skip to content

Commit

Permalink
added configurable device name
Browse files Browse the repository at this point in the history
  • Loading branch information
dakhnod committed Oct 3, 2022
1 parent 7d6c393 commit 685f2d7
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 22 deletions.
60 changes: 54 additions & 6 deletions src/ble/sensor_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,29 @@ uint16_t advertising_interval = APP_ADV_INTERVAL_FAST;

void ble_init() {
ble_stack_init();
gap_params_init();

uint8_t device_name[LENGTH_DEVICE_NAME];
uint32_t device_name_length;

storage_read_device_name(device_name, &device_name_length);

NRF_LOG_INFO("device name length: %d\n", device_name_length);

if(device_name_length == 0){
// set default device name
gap_params_init(
(uint8_t*) DEVICE_NAME,
strlen(DEVICE_NAME)
);
}else {
gap_params_init(
device_name,
device_name_length
);
}
conn_params_init();
services_init();
advertising_init();


}

void ble_handle_input_change(uint32_t index, gpio_config_input_digital_t *config)
Expand All @@ -32,6 +49,31 @@ void ble_handle_input_change(uint32_t index, gpio_config_input_digital_t *config
ble_gpio_asm_handle_input_change(index, config);
}

void ble_handle_device_name_write(ble_gatts_evt_write_t *write_evt){
uint16_t len = write_evt->len;
uint8_t *data = write_evt->data;

storage_store_device_name(data, len);
}

void ble_on_write(ble_evt_t *p_ble_evt) {
ble_gatts_evt_write_t *write_evt = &p_ble_evt
->evt
.gatts_evt
.params
.write;

uint16_t handle = write_evt->handle;
uint16_t uuid = write_evt->uuid.uuid;

UNUSED_PARAMETER(handle);

if (uuid == BLE_UUID_GAP_CHARACTERISTIC_DEVICE_NAME) {
ble_handle_device_name_write(write_evt);
return;
}
}

void on_ble_evt(ble_evt_t *p_ble_evt) {
uint32_t err_code;

Expand All @@ -46,6 +88,11 @@ void on_ble_evt(ble_evt_t *p_ble_evt) {
connection_handle = BLE_CONN_HANDLE_INVALID;
break; // BLE_GAP_EVT_DISCONNECTED


case BLE_GATTS_EVT_WRITE:
ble_on_write(p_ble_evt);
break;

case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
// Pairing not supported
err_code = sd_ble_gap_sec_params_reply(connection_handle,
Expand Down Expand Up @@ -297,15 +344,16 @@ void ble_stack_init(void) {
* @details This function sets up all the necessary GAP (Generic Access Profile) parameters of the
* device including the device name, appearance, and the preferred connection parameters.
*/
void gap_params_init(void) {
void gap_params_init(uint8_t *device_name, uint32_t device_name_length) {
uint32_t err_code;
ble_gap_conn_sec_mode_t sec_mode;

BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);

err_code = sd_ble_gap_device_name_set(&sec_mode,
(const uint8_t *)DEVICE_NAME,
strlen(DEVICE_NAME));
device_name,
device_name_length
);
APP_ERROR_CHECK(err_code);

ble_gap_conn_params_t gap_conn_params;
Expand Down
2 changes: 1 addition & 1 deletion src/ble/sensor_ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void advertising_data_init(void);
void advertising_init(void);
void advertising_start(void);
void ble_stack_init(void);
void gap_params_init(void);
void gap_params_init(uint8_t *device_name, uint32_t device_name_length);
uint32_t bas_init(void);
uint32_t dfu_init(void);
void services_init(void);
Expand Down
1 change: 0 additions & 1 deletion src/ble/services/configuration/ble_configuration_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ void ble_configuration_authorize_connection_params_write(ble_gatts_evt_write_t *
}
}


authorize_params.params.write.gatt_status = status;

sd_ble_gatts_rw_authorize_reply(
Expand Down
12 changes: 6 additions & 6 deletions src/gpio/sensor_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,16 @@ void gpio_init(gpio_input_change_handler_t input_change_handler) {

for (int i = 0; i < gpio_output_digital_pin_count; i++) {
gpio_config_output_digital_t *config = gpio_output_configs + i;
NRF_LOG_INFO("pin output: %d\n", config->pin);
NRF_LOG_INFO("pin default state: %d\n", config->default_state);
NRF_LOG_INFO("pin invert: %d\n\n", config->invert);
NRF_LOG_DEBUG("pin output: %d\n", config->pin);
NRF_LOG_DEBUG("pin default state: %d\n", config->default_state);
NRF_LOG_DEBUG("pin invert: %d\n\n", config->invert);
}

for (int i = 0; i < gpio_input_digital_pin_count; i++) {
gpio_config_input_digital_t *config = gpio_input_configs + i;
NRF_LOG_INFO("pin input: %d\n", config->pin);
NRF_LOG_INFO("pin pull: %d\n", config->pull);
NRF_LOG_INFO("pin invert: %d\n", config->invert);
NRF_LOG_DEBUG("pin input: %d\n", config->pin);
NRF_LOG_DEBUG("pin pull: %d\n", config->pull);
NRF_LOG_DEBUG("pin invert: %d\n", config->invert);
}

gpio_input_change_handler = input_change_handler;
Expand Down
44 changes: 38 additions & 6 deletions src/storage/storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,39 @@ void storage_read_connection_params_configuration(uint8_t *buffer) {
storage_read(OFFSET_CONNECTION_PARAMS_CONFIGURATION, buffer, 10);
}

void storage_read_device_name(uint8_t *buffer, uint32_t *length_) {
storage_read(OFFSET_DEVICE_NAME, buffer, LENGTH_DEVICE_NAME);

uint32_t length;

for(length = 0; ; length++){
if(length >= LENGTH_DEVICE_NAME){
break;
}
if(buffer[length] == 0){
break;
}
if(buffer[length] == 0xFF){
break;
}
}

*length_ = length;
}

void storage_store(uint32_t offset, uint8_t *data, uint32_t length, uint8_t reboot) {
fs_ret_t ret_code;

uint32_t data_size_32 = CEIL_DIV(26, 4);
const uint32_t size = 46; // 16 bytes for pin configuration + 10 bytes for connection param configuration + 20 bytes for device name

// should should be done dynamically, but at compile-time
const uint32_t size_aligned = 48; // calculate 4-byte-alignet size

// this is prefered, but initializing storage_data with unknown length is illegal
// uint32_t data_size = data_size_32 * 4;;
const uint32_t data_size_32 = size_aligned / 4; // calculate size in 32-bit-words

// needs to be static for fs_store
static uint8_t storage_data[28]; // 16 pins for pin configuration + 10 pins for connection param configuration + 2 bytes for alignment
storage_read(0, storage_data, 26); // read whole storage
// we should use size_aligned as the size, but that isn't constant enough for the compiler...
static uint8_t storage_data[48];
storage_read(0, storage_data, size); // read whole storage

memcpy(storage_data + offset, data, length);

Expand Down Expand Up @@ -114,4 +136,14 @@ void storage_store_pin_configuration(uint8_t *data) {

void storage_store_connection_params_configuration(uint8_t *data) {
storage_store(OFFSET_CONNECTION_PARAMS_CONFIGURATION, data, 10, true);
}

void storage_store_device_name(uint8_t *name, int length) {
uint8_t name_buffer[LENGTH_DEVICE_NAME];
memcpy(name_buffer, name, MIN(length, LENGTH_DEVICE_NAME));
if(length < LENGTH_DEVICE_NAME){
name_buffer[length] = 0;
}

storage_store(OFFSET_DEVICE_NAME, name_buffer, LENGTH_DEVICE_NAME, true);
}
8 changes: 6 additions & 2 deletions src/storage/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@

#define OFFSET_PIN_CONFIGURATION 0x00
#define OFFSET_CONNECTION_PARAMS_CONFIGURATION 0x10
#define OFFSET_DEVICE_NAME 0x1A
#define LENGTH_DEVICE_NAME 20

void storage_init();
void fs_evt_handler(fs_evt_t const *const evt, fs_ret_t result);
void storage_on_sys_evt(uint32_t sys_evt);
void storage_read_pin_configuration(uint8_t *buffer);
void storage_store_pin_configuration(uint8_t *buffer);
void storage_store_device_name(uint8_t *name, int length);
void storage_store_connection_params_configuration(uint8_t *buffer);

void storage_read_connection_params_configuration(uint8_t *buffer);
void storage_store_connection_params_configuration(uint8_t *buffer);
void storage_read_device_name(uint8_t *buffer, uint32_t *length);
void storage_read_pin_configuration(uint8_t *buffer);

0 comments on commit 685f2d7

Please sign in to comment.