Skip to content

Commit

Permalink
property: add readonly support and other minor changes in the API
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoquesada committed Jan 1, 2024
1 parent 62acdc6 commit d9949c2
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 39 deletions.
15 changes: 9 additions & 6 deletions src/components/bluepad32/arch/uni_property_esp32.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ static const char* STORAGE_NAMESPACE = "bp32";

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

void uni_property_set(uni_property_idx_t idx, uni_property_value_t value) {
void uni_property_set_with_property(const uni_property_t* p, 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);
loge("Cannot set invalid property\n");
return;
}

if (p->flags & UNI_PROPERTY_FLAG_READ_ONLY) {
loge("Cannot set READ_ONLY property: '%s'\n", p->name);
return;
}

Expand Down Expand Up @@ -62,16 +66,15 @@ void uni_property_set(uni_property_idx_t idx, uni_property_value_t value) {
nvs_close(nvs_handle);
}

uni_property_value_t uni_property_get(uni_property_idx_t idx) {
uni_property_value_t uni_property_get_with_property(const uni_property_t* p) {
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);
loge("Cannot get invalid property\n");
ret.u8 = 0;
return ret;
}
Expand Down
25 changes: 20 additions & 5 deletions src/components/bluepad32/arch/uni_property_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,34 @@
#include "uni_property.h"

#include "uni_common.h"
#include "uni_log.h"

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

void uni_property_set(uni_property_idx_t idx, uni_property_value_t value) {
ARG_UNUSED(idx);
void uni_property_set_with_property(const uni_property_t* p, uni_property_value_t value) {
ARG_UNUSED(value);
/* Nothing */

if (!p) {
loge("Invalid set property\n");
return;
}

if (p->flags & UNI_PROPERTY_FLAG_READ_ONLY)
return;
return;
}

uni_property_value_t uni_property_get(uni_property_idx_t idx) {
return uni_property_get_property_for_index(idx)->default_value;
uni_property_value_t uni_property_get_with_property(const uni_property_t* p) {
if (!p) {
uni_property_value_t ret;
loge("Invalid get property\n");
ret.u8 = 0;
return ret;
}

return p->default_value;
}

void uni_property_init(void) {
Expand Down
27 changes: 15 additions & 12 deletions src/components/bluepad32/arch/uni_property_pico.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,29 @@

#include "uni_property.h"

#include <stddef.h>

#include "uni_common.h"
#include "uni_log.h"

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

void uni_property_set(uni_property_idx_t idx, uni_property_value_t value) {
ARG_UNUSED(idx);
void uni_property_set_with_property(const uni_property_t* p, uni_property_value_t value) {
ARG_UNUSED(value);
/* Nothing */
}

uni_property_value_t uni_property_get(uni_property_idx_t idx) {
uni_property_value_t ret;
const uni_property_t* p;
if (!p) {
loge("Invalid set property\n");
return;
}

if (p->flags & UNI_PROPERTY_FLAG_READ_ONLY)
return;
return;
}

p = uni_property_get_property_for_index(idx);
if (p == NULL) {
uni_property_value_t uni_property_get_with_property(const uni_property_t* p) {
if (!p) {
uni_property_value_t ret;
loge("Invalid get property\n");
ret.u8 = 0;
return ret;
}
Expand Down
14 changes: 11 additions & 3 deletions src/components/bluepad32/include/uni_property.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <stdbool.h>
#include <stdint.h>

#include "uni_common.h"

// Bluepad32-global properties
// Keep them sorted
#define UNI_PROPERTY_NAME_ALLOWLIST_ENABLED "bp.bt.allow_en"
Expand Down Expand Up @@ -67,21 +69,27 @@ typedef union {
char* str;
} uni_property_value_t;

typedef enum {
UNI_PROPERTY_FLAG_READ_ONLY = BIT(0),
} uni_property_flag_t;

typedef struct {
uni_property_idx_t idx; // Used for debugging: idx must match order
const char* name;
uni_property_type_t type;
uni_property_value_t default_value;
uni_property_flag_t flags;
} uni_property_t;

const uni_property_t* uni_property_get_property_for_index(uni_property_idx_t idx);
void uni_property_set(uni_property_idx_t idx, uni_property_value_t value);
uni_property_value_t uni_property_get(uni_property_idx_t idx);
void uni_property_list_all(void);
void uni_property_init_debug(void);

// Interface
// Each arch needs to implement these functions:
void uni_property_init(void);
void uni_property_set(uni_property_idx_t idx, uni_property_value_t value);
uni_property_value_t uni_property_get(uni_property_idx_t idx);
void uni_property_set_with_property(const uni_property_t* p, uni_property_value_t value);
uni_property_value_t uni_property_get_with_property(const uni_property_t* p);

#endif // UNI_PROPERTY_H
7 changes: 4 additions & 3 deletions src/components/bluepad32/platform/uni_platform_unijoysticle.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,14 @@ static const uni_property_t properties[] = {
.default_value.u32 = UNI_BALANCE_BOARD_FIRE_THRESHOLD_DEFAULT},
{UNI_PROPERTY_IDX_UNI_C64_POT_MODE, UNI_PROPERTY_NAME_UNI_C64_POT_MODE, UNI_PROPERTY_TYPE_U8,
.default_value.u8 = UNI_PLATFORM_UNIJOYSTICLE_C64_POT_MODE_3BUTTONS},
{UNI_PROPERTY_IDX_UNI_MODEL, UNI_PROPERTY_NAME_UNI_MODEL, UNI_PROPERTY_TYPE_STRING, .default_value.str = "Unknown"},
{UNI_PROPERTY_IDX_UNI_MODEL, UNI_PROPERTY_NAME_UNI_MODEL, UNI_PROPERTY_TYPE_STRING, .default_value.str = "Unknown",
.flags = UNI_PROPERTY_FLAG_READ_ONLY},
{UNI_PROPERTY_IDX_UNI_MOUSE_EMULATION, UNI_PROPERTY_NAME_UNI_MOUSE_EMULATION, UNI_PROPERTY_TYPE_U8,
.default_value.u8 = UNI_PLATFORM_UNIJOYSTICLE_MOUSE_EMULATION_AUTO},
{UNI_PROPERTY_IDX_UNI_SERIAL_NUMBER, UNI_PROPERTY_NAME_UNI_SERIAL_NUMBER, UNI_PROPERTY_TYPE_U32,
.default_value.u32 = 0},
.default_value.u32 = 0, .flags = UNI_PROPERTY_FLAG_READ_ONLY},
{UNI_PROPERTY_IDX_UNI_VENDOR, UNI_PROPERTY_NAME_UNI_VENDOR, UNI_PROPERTY_TYPE_STRING,
.default_value.str = "Unknown"},
.default_value.str = "Unknown", .flags = UNI_PROPERTY_FLAG_READ_ONLY},
};
_Static_assert(ARRAY_SIZE(properties) == (UNI_PROPERTY_IDX_UNI_LAST - UNI_PROPERTY_IDX_LAST), "Invalid property size");

Expand Down
44 changes: 34 additions & 10 deletions src/components/bluepad32/uni_property.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ static const uni_property_t properties[] = {
{UNI_PROPERTY_IDX_GAP_MIN_PERIODIC_LEN, UNI_PROPERTY_NAME_GAP_MIN_PERIODIC_LEN, UNI_PROPERTY_TYPE_U8,
.default_value.u8 = UNI_BT_MIN_PERIODIC_LENGTH},
{UNI_PROPERTY_IDX_MOUSE_SCALE, UNI_PROPERTY_NAME_MOUSE_SCALE, UNI_PROPERTY_TYPE_FLOAT, .default_value.f32 = 1.0f},
{UNI_PROPERTY_IDX_VERSION, UNI_PROPERTY_NAME_VERSION, UNI_PROPERTY_TYPE_STRING, .default_value.str = UNI_VERSION},
{UNI_PROPERTY_IDX_VERSION, UNI_PROPERTY_NAME_VERSION, UNI_PROPERTY_TYPE_STRING, .default_value.str = UNI_VERSION,
.flags = UNI_PROPERTY_FLAG_READ_ONLY},
{UNI_PROPERTY_IDX_VIRTUAL_DEVICE_ENABLED, UNI_PROPERTY_NAME_VIRTUAL_DEVICE_ENABLED, UNI_PROPERTY_TYPE_BOOL,
#ifdef CONFIG_BLUEPAD32_ENABLE_VIRTUAL_DEVICE_BY_DEFAULT
.default_value.boolean = true
Expand All @@ -53,6 +54,20 @@ static const uni_property_t properties[] = {
};
_Static_assert(ARRAY_SIZE(properties) == UNI_PROPERTY_IDX_LAST, "Invalid properties size");

// Helpers
static const uni_property_t* get_property(uni_property_idx_t idx) {
if (idx >= UNI_PROPERTY_IDX_LAST) {
if (uni_get_platform()->get_property)
return uni_get_platform()->get_property(idx);
// Invalid
return NULL;
}

return &properties[idx];
}

// Public functions

void uni_property_init_debug(void) {
for (int i = 0; i < ARRAY_SIZE(properties); i++) {
const uni_property_t* p = &properties[i];
Expand All @@ -65,11 +80,11 @@ void uni_property_init_debug(void) {
void uni_property_list_all(void) {
logi("properties:\n");
for (int i = 0; i < UNI_PROPERTY_IDX_COUNT; i++) {
const uni_property_t* p = uni_property_get_property_for_index(i);
const uni_property_t* p = get_property(i);
if (!p)
// Means the property is not implemented, safe to break here.
break;
uni_property_value_t val = uni_property_get(i);
uni_property_value_t val = uni_property_get_with_property(p);
switch (p->type) {
case UNI_PROPERTY_TYPE_BOOL:
logi("%s = %s\n", p->name, val.boolean ? "true" : "false");
Expand Down Expand Up @@ -97,13 +112,22 @@ void uni_property_list_all(void) {
}
}

const uni_property_t* uni_property_get_property_for_index(uni_property_idx_t idx) {
if (idx >= UNI_PROPERTY_IDX_LAST) {
if (uni_get_platform()->get_property)
return uni_get_platform()->get_property(idx);
// Invalid
return NULL;
void uni_property_set(uni_property_idx_t idx, uni_property_value_t value) {
const uni_property_t* p = get_property(idx);
if (!p) {
loge("Could not find property %d\n", idx);
return;
}
uni_property_set_with_property(p, value);
}

return &properties[idx];
uni_property_value_t uni_property_get(uni_property_idx_t idx) {
const uni_property_t* p = get_property(idx);
if (!p) {
uni_property_value_t ret;
loge("Could not find property %d\n", idx);
ret.u8 = 0;
return ret;
}
return uni_property_get_with_property(p);
}

0 comments on commit d9949c2

Please sign in to comment.