Skip to content

Commit

Permalink
properties: big refactor
Browse files Browse the repository at this point in the history
Move properties to a "property registry" where the default values and
type are stored there.

This make it easier to manipulate the properties (from the BLE service
or console), and less code.
  • Loading branch information
ricardoquesada committed Dec 31, 2023
1 parent 0d097a8 commit f9d7b62
Show file tree
Hide file tree
Showing 26 changed files with 732 additions and 698 deletions.
7 changes: 3 additions & 4 deletions examples/esp32/main/my_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,9 @@ static void my_platform_on_controller_data(uni_hid_device_t* d, uni_controller_t
}
}

static int32_t my_platform_get_property(uni_platform_property_t key) {
// Deprecated
ARG_UNUSED(key);
return 0;
static const uni_property_t* my_platform_get_property(uni_property_idx_t idx) {
ARG_UNUSED(idx);
return NULL;
}

static void my_platform_on_oob_event(uni_platform_oob_event_t event, void* data) {
Expand Down
7 changes: 3 additions & 4 deletions examples/linux/src/my_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,9 @@ static void pc_debug_on_controller_data(uni_hid_device_t* d, uni_controller_t* c
}
}

static int32_t pc_debug_get_property(uni_platform_property_t key) {
// Deprecated
ARG_UNUSED(key);
return 0;
static const uni_property_t* pc_debug_get_property(uni_property_idx_t idx) {
ARG_UNUSED(idx);
return NULL;
}

static void pc_debug_on_oob_event(uni_platform_oob_event_t event, void* data) {
Expand Down
6 changes: 3 additions & 3 deletions src/components/bluepad32/arch/uni_console_esp32.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ static int set_gap_security_level(int argc, char** argv) {
}

static void print_gap_periodic_inquiry(void) {
int max = uni_bt_get_gap_max_periodic_lenght();
int min = uni_bt_get_gap_min_periodic_lenght();
int len = uni_bt_get_gap_inquiry_lenght();
int max = uni_bt_get_gap_max_periodic_length();
int min = uni_bt_get_gap_min_periodic_length();
int len = uni_bt_get_gap_inquiry_length();
logi("GAP max periodic len: %d, min periodic len: %d, inquiry len: %d\n", max, min, len);
}

Expand Down
51 changes: 32 additions & 19 deletions src/components/bluepad32/arch/uni_property_esp32.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,82 +14,95 @@ static const char* STORAGE_NAMESPACE = "bp32";

// Uses NVS for storage. Used in all ESP32 Bluepad32 platforms.

void uni_property_set(const char* key, uni_property_type_t type, uni_property_value_t value) {
void uni_property_set(uni_property_idx_t idx, uni_property_value_t value) {
nvs_handle_t nvs_handle;
esp_err_t err;
uint32_t* float_alias;

const uni_property_t* p = uni_property_get_property_for_index(idx);
if (!p) {
loge("Could not find property %d\n", idx);
return;
}

err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &nvs_handle);
if (err != ESP_OK) {
loge("Could not open readwrite NVS storage, key: %s, err=%#x\n", key, err);
loge("Could not open readwrite NVS storage, key: %s, err=%#x\n", p->name, err);
return;
}

switch (type) {
switch (p->type) {
case UNI_PROPERTY_TYPE_U8:
err = nvs_set_u8(nvs_handle, key, value.u8);
err = nvs_set_u8(nvs_handle, p->name, value.u8);
break;
case UNI_PROPERTY_TYPE_U32:
err = nvs_set_u32(nvs_handle, key, value.u32);
err = nvs_set_u32(nvs_handle, p->name, value.u32);
break;
case UNI_PROPERTY_TYPE_FLOAT:
float_alias = (uint32_t*)&value.f32;
err = nvs_set_u32(nvs_handle, key, *float_alias);
err = nvs_set_u32(nvs_handle, p->name, *float_alias);
break;
case UNI_PROPERTY_TYPE_STRING:
err = nvs_set_str(nvs_handle, key, value.str);
err = nvs_set_str(nvs_handle, p->name, value.str);
break;
}

if (err != ESP_OK) {
loge("Could not store '%s' in NVS, err=%#x\n", key, err);
loge("Could not store '%s' in NVS, err=%#x\n", p->name, err);
goto out;
}

err = nvs_commit(nvs_handle);
if (err != ESP_OK) {
loge("Could not commit '%s' in NVS, err=%#x\n", key, err);
loge("Could not commit '%s' in NVS, err=%#x\n", p->name, err);
}

out:
nvs_close(nvs_handle);
}

uni_property_value_t uni_property_get(const char* key, uni_property_type_t type, uni_property_value_t def) {
uni_property_value_t uni_property_get(uni_property_idx_t idx) {
nvs_handle_t nvs_handle;
esp_err_t err;
uni_property_value_t ret;
size_t str_len;
static char str_ret[128];

const uni_property_t* p = uni_property_get_property_for_index(idx);
if (!p) {
loge("Could not find property %d\n", idx);
ret.u8 = 0;
return ret;
}

err = nvs_open(STORAGE_NAMESPACE, NVS_READONLY, &nvs_handle);
if (err != ESP_OK) {
// Might be valid if no bp32 keys were stored
logd("Could not open readonly NVS storage, key:'%s'\n", key);
return def;
logd("Could not open readonly NVS storage, key:'%s'\n", p->name);
return p->default_value;
}

switch (type) {
switch (p->type) {
case UNI_PROPERTY_TYPE_U8:
err = nvs_get_u8(nvs_handle, key, &ret.u8);
err = nvs_get_u8(nvs_handle, p->name, &ret.u8);
break;
case UNI_PROPERTY_TYPE_U32:
err = nvs_get_u32(nvs_handle, key, &ret.u32);
err = nvs_get_u32(nvs_handle, p->name, &ret.u32);
break;
case UNI_PROPERTY_TYPE_FLOAT:
err = nvs_get_u32(nvs_handle, key, (uint32_t*)&ret.f32);
err = nvs_get_u32(nvs_handle, p->name, (uint32_t*)&ret.f32);
break;
case UNI_PROPERTY_TYPE_STRING:
ret.str = str_ret;
memset(str_ret, 0, sizeof(str_ret));
err = nvs_get_str(nvs_handle, key, str_ret, &str_len);
err = nvs_get_str(nvs_handle, p->name, str_ret, &str_len);
break;
}

if (err != ESP_OK) {
// Might be valid if the key was not previously stored
logd("could not read property '%s' from NVS, err=%#x\n", key, err);
ret = def;
logd("could not read property '%s' from NVS, err=%#x\n", p->name, err);
ret = p->default_value;
/* falltrhough */
}

Expand Down
14 changes: 5 additions & 9 deletions src/components/bluepad32/arch/uni_property_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,16 @@
// Used only in non-ESP32 platforms.
// Short-term solution: just return the default value.

void uni_property_set(const char* key, uni_property_type_t type, uni_property_value_t value) {
ARG_UNUSED(key);
ARG_UNUSED(type);
void uni_property_set(uni_property_idx_t idx, uni_property_value_t value) {
ARG_UNUSED(idx);
ARG_UNUSED(value);
/* Nothing */
}

uni_property_value_t uni_property_get(const char* key, uni_property_type_t type, uni_property_value_t def) {
ARG_UNUSED(key);
ARG_UNUSED(type);

return def;
uni_property_value_t uni_property_get(uni_property_idx_t idx) {
return uni_property_get_property_for_index(idx)->default_value;
}

void uni_property_init(void) {
/* Nothing */
uni_property_init_debug();
}
23 changes: 15 additions & 8 deletions src/components/bluepad32/arch/uni_property_pico.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,33 @@

#include "uni_property.h"

#include <stddef.h>

#include "uni_common.h"

// TODO: Implement a memory cache.
// Used only in non-ESP32 platforms.
// Short-term solution: just return the default value.

void uni_property_set(const char* key, uni_property_type_t type, uni_property_value_t value) {
ARG_UNUSED(key);
ARG_UNUSED(type);
void uni_property_set(uni_property_idx_t idx, uni_property_value_t value) {
ARG_UNUSED(idx);
ARG_UNUSED(value);
/* Nothing */
}

uni_property_value_t uni_property_get(const char* key, uni_property_type_t type, uni_property_value_t def) {
ARG_UNUSED(key);
ARG_UNUSED(type);
uni_property_value_t uni_property_get(uni_property_idx_t idx) {
uni_property_value_t ret;
const uni_property_t *p;

p = uni_property_get_property_for_index(idx);
if (p == NULL) {
ret.u8 = 0;
return ret;
}

return def;
return p->default_value;
}

void uni_property_init(void) {
/* Nothing */
uni_property_init_debug();
}
36 changes: 11 additions & 25 deletions src/components/bluepad32/bt/uni_bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,69 +469,55 @@ void uni_bt_set_gap_security_level(int gap) {
uni_property_value_t val;

val.u32 = gap;
uni_property_set(UNI_PROPERTY_KEY_GAP_LEVEL, UNI_PROPERTY_TYPE_U32, val);
uni_property_set(UNI_PROPERTY_IDX_GAP_LEVEL, val);
}

int uni_bt_get_gap_security_level() {
uni_property_value_t val;
uni_property_value_t def;

// It seems that with gap_security_level(0) all controllers work except Nintendo Switch Pro controller.
#if CONFIG_BLUEPAD32_GAP_SECURITY
def.u32 = 2;
#else
def.u32 = 0;
#endif // CONFIG_BLUEPAD32_GAP_SECURITY

val = uni_property_get(UNI_PROPERTY_KEY_GAP_LEVEL, UNI_PROPERTY_TYPE_U32, def);
val = uni_property_get(UNI_PROPERTY_IDX_GAP_LEVEL);
return val.u32;
}

void uni_bt_set_gap_inquiry_length(int len) {
uni_property_value_t val;

val.u8 = len;
uni_property_set(UNI_PROPERTY_KEY_GAP_INQ_LEN, UNI_PROPERTY_TYPE_U8, val);
uni_property_set(UNI_PROPERTY_IDX_GAP_INQ_LEN, val);
}

int uni_bt_get_gap_inquiry_lenght(void) {
int uni_bt_get_gap_inquiry_length(void) {
uni_property_value_t val;
uni_property_value_t def;

def.u8 = UNI_BT_INQUIRY_LENGTH;
val = uni_property_get(UNI_PROPERTY_KEY_GAP_INQ_LEN, UNI_PROPERTY_TYPE_U8, def);
val = uni_property_get(UNI_PROPERTY_IDX_GAP_INQ_LEN);
return val.u8;
}

void uni_bt_set_gap_max_peridic_length(int len) {
uni_property_value_t val;

val.u8 = len;
uni_property_set(UNI_PROPERTY_KEY_GAP_MAX_PERIODIC_LEN, UNI_PROPERTY_TYPE_U8, val);
uni_property_set(UNI_PROPERTY_IDX_GAP_MAX_PERIODIC_LEN, val);
}

int uni_bt_get_gap_max_periodic_lenght(void) {
int uni_bt_get_gap_max_periodic_length(void) {
uni_property_value_t val;
uni_property_value_t def;

def.u8 = UNI_BT_MAX_PERIODIC_LENGTH;
val = uni_property_get(UNI_PROPERTY_KEY_GAP_MAX_PERIODIC_LEN, UNI_PROPERTY_TYPE_U8, def);
val = uni_property_get(UNI_PROPERTY_IDX_GAP_MAX_PERIODIC_LEN);
return val.u8;
}

void uni_bt_set_gap_min_peridic_length(int len) {
uni_property_value_t val;

val.u8 = len;
uni_property_set(UNI_PROPERTY_KEY_GAP_MIN_PERIODIC_LEN, UNI_PROPERTY_TYPE_U8, val);
uni_property_set(UNI_PROPERTY_IDX_GAP_MIN_PERIODIC_LEN, val);
}

int uni_bt_get_gap_min_periodic_lenght(void) {
int uni_bt_get_gap_min_periodic_length(void) {
uni_property_value_t val;
uni_property_value_t def;

def.u8 = UNI_BT_MIN_PERIODIC_LENGTH;
val = uni_property_get(UNI_PROPERTY_KEY_GAP_MIN_PERIODIC_LEN, UNI_PROPERTY_TYPE_U8, def);
val = uni_property_get(UNI_PROPERTY_IDX_GAP_MIN_PERIODIC_LEN);
return val.u8;
}

Expand Down
12 changes: 4 additions & 8 deletions src/components/bluepad32/bt/uni_bt_allowlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,18 @@ static void update_allowlist_to_property(void) {
}

val.str = str;
uni_property_set(UNI_PROPERTY_KEY_ALLOWLIST_LIST, UNI_PROPERTY_TYPE_STRING, val);
uni_property_set(UNI_PROPERTY_IDX_ALLOWLIST_LIST, val);
}

static void update_allowlist_from_property(void) {
// Parses the list from the property and stored it locally.
uni_property_value_t def;
uni_property_value_t val;
bd_addr_t addr;
int offset;
int len;

// Whether it is enabled.
def.str = NULL;
val = uni_property_get(UNI_PROPERTY_KEY_ALLOWLIST_LIST, UNI_PROPERTY_TYPE_STRING, def);
val = uni_property_get(UNI_PROPERTY_IDX_ALLOWLIST_LIST);

if (val.str == NULL)
return;
Expand Down Expand Up @@ -128,17 +126,15 @@ void uni_bt_allowlist_set_enabled(bool enabled) {
enforced = enabled;

val.u8 = enforced;
uni_property_set(UNI_PROPERTY_KEY_ALLOWLIST_ENABLED, UNI_PROPERTY_TYPE_U8, val);
uni_property_set(UNI_PROPERTY_IDX_ALLOWLIST_ENABLED, val);
}
}

void uni_bt_allowlist_init(void) {
uni_property_value_t def;
uni_property_value_t val;

// Whether it is enabled.
def.u8 = 0;
val = uni_property_get(UNI_PROPERTY_KEY_ALLOWLIST_ENABLED, UNI_PROPERTY_TYPE_U8, def);
val = uni_property_get(UNI_PROPERTY_IDX_ALLOWLIST_ENABLED);
enforced = val.u8;

// The list of allowed-list addresses.
Expand Down
8 changes: 4 additions & 4 deletions src/components/bluepad32/bt/uni_bt_bredr.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ static void inquiry_remote_name_timeout_callback(btstack_timer_source_t* ts) {
void uni_bt_bredr_scan_start(void) {
uint8_t status;

status = gap_inquiry_periodic_start(uni_bt_get_gap_inquiry_lenght(), uni_bt_get_gap_max_periodic_lenght(),
uni_bt_get_gap_min_periodic_lenght());
status = gap_inquiry_periodic_start(uni_bt_get_gap_inquiry_length(), uni_bt_get_gap_max_periodic_length(),
uni_bt_get_gap_min_periodic_length());
if (status)
loge("Failed to start period inquiry, error=0x%02x\n", status);
logi("BR/EDR scan -> 1\n");
Expand Down Expand Up @@ -177,8 +177,8 @@ void uni_bt_bredr_setup(void) {
hci_set_master_slave_policy(HCI_ROLE_MASTER);

logi("Gap security level: %d\n", security_level);
logi("Periodic Inquiry: max=%d, min=%d, len=%d\n", uni_bt_get_gap_max_periodic_lenght(),
uni_bt_get_gap_min_periodic_lenght(), uni_bt_get_gap_inquiry_lenght());
logi("Periodic Inquiry: max=%d, min=%d, len=%d\n", uni_bt_get_gap_max_periodic_length(),
uni_bt_get_gap_min_periodic_length(), uni_bt_get_gap_inquiry_length());
}

void uni_bt_bredr_set_enabled(bool enabled) {
Expand Down
Loading

0 comments on commit f9d7b62

Please sign in to comment.