mirror of
https://github.com/pebble-dev/clay.git
synced 2026-09-03 15:47:43 -04:00
[WIP] - Basic SimpleAppMessage integration for ints.
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
#include "clay.h"
|
||||
#include "hash.h"
|
||||
|
||||
#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));
|
||||
|
||||
switch (type) {
|
||||
case SimpleDictDataType_Raw:
|
||||
persist_write_data(persist_key, data, data_size);
|
||||
return true;
|
||||
case SimpleDictDataType_Bool: {
|
||||
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);
|
||||
persist_write_int(persist_key, value);
|
||||
return true;
|
||||
}
|
||||
case SimpleDictDataType_String: {
|
||||
const char *value = data;
|
||||
persist_write_string(persist_key, value);
|
||||
return true;
|
||||
}
|
||||
case SimpleDictDataTypeCount:
|
||||
break;
|
||||
}
|
||||
APP_LOG(APP_LOG_LEVEL_ERROR, "Unexpected type %d", type);
|
||||
return false;
|
||||
}
|
||||
|
||||
static void prv_simple_app_message_received_callback(const SimpleDict *message, void *context) {
|
||||
if (!message) {
|
||||
return;
|
||||
}
|
||||
|
||||
APP_LOG(APP_LOG_LEVEL_INFO, "CLAY: Received SimpleAppMessage");
|
||||
simple_dict_foreach(message, prv_store_settings, NULL);
|
||||
s_callbacks.settings_updated(context);
|
||||
}
|
||||
|
||||
bool clay_register_callbacks(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) {
|
||||
.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);
|
||||
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);
|
||||
return;
|
||||
}
|
||||
|
||||
simple_app_message_open();
|
||||
}
|
||||
|
||||
bool clay_get_int(const char *key, int *value_out) {
|
||||
uint32_t persist_key = hash((uint8_t *)key, strlen(key));
|
||||
|
||||
if (persist_exists(persist_key)) {
|
||||
int value = persist_read_int(persist_key);
|
||||
APP_LOG(APP_LOG_LEVEL_INFO, "CLAY: reading %d - %d from storage", (int)persist_key, value);
|
||||
memcpy(value_out, &value, sizeof(&value));
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
#include "hash.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// Based on DJB2 Hash
|
||||
uint32_t hash(const uint8_t *bytes, const uint32_t length) {
|
||||
uint32_t hash = 5381;
|
||||
|
||||
if (length == 0) {
|
||||
return hash;
|
||||
}
|
||||
|
||||
uint8_t c;
|
||||
const uint8_t *last_byte = bytes + length;
|
||||
while (bytes != last_byte) {
|
||||
c = *bytes;
|
||||
hash = ((hash << 5) + hash) + c;
|
||||
bytes++;
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
uint32_t hash(const uint8_t *bytes, const uint32_t length);
|
||||
@@ -133,3 +133,11 @@ module.exports.includesCapability = function(activeWatchInfo, capabilities) {
|
||||
|
||||
return result.indexOf(false) === -1;
|
||||
};
|
||||
|
||||
module.exports.log = function(message) {
|
||||
console.log('Clay: ' + JSON.stringify(message));
|
||||
};
|
||||
|
||||
module.exports.throw = function(error) {
|
||||
throw new Error('Clay: '+ error)
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user