[WIP] - Move callbacks registration to init method

This commit is contained in:
Keegan
2016-10-26 20:16:30 -07:00
parent 74d6d0d21d
commit ffd0ba64ec
2 changed files with 14 additions and 20 deletions
+1 -3
View File
@@ -8,7 +8,7 @@ typedef struct ClayCallbacks {
ClayUpdatedCallback settings_updated;
} ClayCallbacks;
void clay_init(uint32_t inbox_size);
void clay_init(uint32_t inbox_size, const ClayCallbacks *callbacks, void *context);
void clay_remove(const char *key);
@@ -29,5 +29,3 @@ bool clay_get_data(const char *key, void *value_out, size_t n);
bool clay_get_int(const char *key, int *value_out);
bool clay_get_string(const char *key, char *value_out, size_t n);
bool clay_register_callbacks(const ClayCallbacks *callbacks, void *context);
+13 -17
View File
@@ -4,25 +4,24 @@
#define SIMPLE_APP_MESSAGE_NAMESPACE ("CLAY")
static ClayCallbacks s_callbacks;
static void *s_context;
static bool prv_store_settings(const char *key, SimpleDictDataType type, const void *data,
size_t data_size, void *context) {
uint32_t persist_key = hash((uint8_t *)key, strlen(key));
static bool prv_store_settings(
const char *key, SimpleDictDataType type, const void *data, size_t data_size, void *context) {
uint32_t persist_key = hash((uint8_t *) key, strlen(key));
switch (type) {
case SimpleDictDataType_Raw:
persist_write_data(persist_key, data, data_size);
return true;
case SimpleDictDataType_Bool: {
const bool value = *((bool *)data);
const bool value = *((bool *) data);
persist_write_bool(persist_key, value);
return true;
}
case SimpleDictDataType_Int: {
const int value = *((int *)data);
APP_LOG(APP_LOG_LEVEL_INFO, "CLAY: writing %d - %d to storage", (int)persist_key, value);
const int value = *((int *) data);
APP_LOG(APP_LOG_LEVEL_INFO, "CLAY: writing %d - %d to storage",
(int) persist_key, value);
persist_write_int(persist_key, value);
return true;
}
@@ -48,23 +47,20 @@ static void prv_simple_app_message_received_callback(const SimpleDict *message,
s_callbacks.settings_updated(context);
}
bool clay_register_callbacks(const ClayCallbacks *callbacks, void *context) {
void clay_init(uint32_t inbox_size, const ClayCallbacks *callbacks, void *context) {
s_callbacks = *callbacks;
s_context = context;
return true;
}
void clay_init(uint32_t inbox_size) {
const SimpleAppMessageCallbacks simple_app_message_callbacks = (SimpleAppMessageCallbacks) {
const SimpleAppMessageCallbacks simple_app_message_callbacks = {
.message_received = prv_simple_app_message_received_callback,
};
const bool register_success = simple_app_message_register_callbacks(SIMPLE_APP_MESSAGE_NAMESPACE,
&simple_app_message_callbacks,
s_context);
const bool register_success = simple_app_message_register_callbacks(
SIMPLE_APP_MESSAGE_NAMESPACE, &simple_app_message_callbacks, context);
if (!register_success) {
APP_LOG(APP_LOG_LEVEL_ERROR, "Failed to register callbacks for namespace %s", SIMPLE_APP_MESSAGE_NAMESPACE);
return;
}
const bool request_inbox_size_success = simple_app_message_request_inbox_size(inbox_size);
if (!request_inbox_size_success) {
APP_LOG(APP_LOG_LEVEL_ERROR, "Failed to request inbox size of %d", (int)inbox_size);