[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; ClayUpdatedCallback settings_updated;
} ClayCallbacks; } 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); 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_int(const char *key, int *value_out);
bool clay_get_string(const char *key, char *value_out, size_t n); 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") #define SIMPLE_APP_MESSAGE_NAMESPACE ("CLAY")
static ClayCallbacks s_callbacks; static ClayCallbacks s_callbacks;
static void *s_context;
static bool prv_store_settings(const char *key, SimpleDictDataType type, const void *data, static bool prv_store_settings(
size_t data_size, void *context) { const char *key, SimpleDictDataType type, const void *data, size_t data_size, void *context) {
uint32_t persist_key = hash((uint8_t *) key, strlen(key));
uint32_t persist_key = hash((uint8_t *)key, strlen(key));
switch (type) { switch (type) {
case SimpleDictDataType_Raw: case SimpleDictDataType_Raw:
persist_write_data(persist_key, data, data_size); persist_write_data(persist_key, data, data_size);
return true; return true;
case SimpleDictDataType_Bool: { case SimpleDictDataType_Bool: {
const bool value = *((bool *)data); const bool value = *((bool *) data);
persist_write_bool(persist_key, value); persist_write_bool(persist_key, value);
return true; return true;
} }
case SimpleDictDataType_Int: { case SimpleDictDataType_Int: {
const int value = *((int *)data); const int value = *((int *) data);
APP_LOG(APP_LOG_LEVEL_INFO, "CLAY: writing %d - %d to storage", (int)persist_key, value); APP_LOG(APP_LOG_LEVEL_INFO, "CLAY: writing %d - %d to storage",
(int) persist_key, value);
persist_write_int(persist_key, value); persist_write_int(persist_key, value);
return true; return true;
} }
@@ -48,23 +47,20 @@ static void prv_simple_app_message_received_callback(const SimpleDict *message,
s_callbacks.settings_updated(context); 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_callbacks = *callbacks;
s_context = context;
return true;
}
void clay_init(uint32_t inbox_size) { const SimpleAppMessageCallbacks simple_app_message_callbacks = {
const SimpleAppMessageCallbacks simple_app_message_callbacks = (SimpleAppMessageCallbacks) {
.message_received = prv_simple_app_message_received_callback, .message_received = prv_simple_app_message_received_callback,
}; };
const bool register_success = simple_app_message_register_callbacks(SIMPLE_APP_MESSAGE_NAMESPACE, const bool register_success = simple_app_message_register_callbacks(
&simple_app_message_callbacks, SIMPLE_APP_MESSAGE_NAMESPACE, &simple_app_message_callbacks, context);
s_context);
if (!register_success) { if (!register_success) {
APP_LOG(APP_LOG_LEVEL_ERROR, "Failed to register callbacks for namespace %s", SIMPLE_APP_MESSAGE_NAMESPACE); APP_LOG(APP_LOG_LEVEL_ERROR, "Failed to register callbacks for namespace %s", SIMPLE_APP_MESSAGE_NAMESPACE);
return; return;
} }
const bool request_inbox_size_success = simple_app_message_request_inbox_size(inbox_size); const bool request_inbox_size_success = simple_app_message_request_inbox_size(inbox_size);
if (!request_inbox_size_success) { if (!request_inbox_size_success) {
APP_LOG(APP_LOG_LEVEL_ERROR, "Failed to request inbox size of %d", (int)inbox_size); APP_LOG(APP_LOG_LEVEL_ERROR, "Failed to request inbox size of %d", (int)inbox_size);