From ffd0ba64ec7670ec6cc2fb094a924c9575192444 Mon Sep 17 00:00:00 2001 From: Keegan Date: Wed, 26 Oct 2016 20:16:30 -0700 Subject: [PATCH] [WIP] - Move callbacks registration to init method --- include/clay.h | 4 +--- src/c/clay.c | 30 +++++++++++++----------------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/include/clay.h b/include/clay.h index 7317ee6..211ce5b 100644 --- a/include/clay.h +++ b/include/clay.h @@ -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); diff --git a/src/c/clay.c b/src/c/clay.c index 8b583cc..c070a67 100644 --- a/src/c/clay.c +++ b/src/c/clay.c @@ -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);