Ejemplo n.º 1
0
Tuple * dict_read_begin_from_buffer(DictionaryIterator *iter,
                                    const uint8_t * const buffer,
                                    const uint16_t size) {

    if (dict_write_begin(iter, (uint8_t* const)buffer, size) != DICT_OK)
        return NULL;

    return iter->cursor;
}
Ejemplo n.º 2
0
static void inbox_received_callback(DictionaryIterator *iterator, void *context) {
  dict_write_begin(&items_iter, items_buffer, ITEMS_BUFFER_SIZE);
  Tuple *t = dict_read_first(iterator);
  while(t != NULL) {
    dict_write_cstring(&items_iter, (int) t->key, (char*) t->value->cstring);
    t = dict_read_next(iterator);
  }
  int items_size = dict_write_end(&items_iter);
  load_menu();
}
Ejemplo n.º 3
0
void pbl_capture_send() {
	struct GContext *gctx = app_get_current_graphics_context();
	unsigned char *ptr = (unsigned char *)(*gctx->ptr);
	int pbl_capture_sentLen = 0;

	DictionaryResult ret;
	DictionaryIterator imgChunk;
	uint8_t buff[MAX_OUT_MESSAGE_SIZE];

	int len = 0;

	for (int y=0; y<168; y++) {
		for (int x=0; x<18; x++) {
			http_capture_frameBuffer[len++] = *ptr++;
		}
		ptr++; ptr++;
	}

	app_comm_set_sniff_interval(SNIFF_INTERVAL_REDUCED);

	while (pbl_capture_sentLen < IMAGE_SIZE)
	{
		DictionaryResult res = dict_write_begin(&imgChunk, buff, MAX_OUT_MESSAGE_SIZE);
		if (res != DICT_OK)
		{
			return;
		}
		uint8_t msg =  mMT_SendImage;
		ret = dict_write_int(&imgChunk, KEY_COMMAND, &msg, 1, false);
		if (DICT_OK != ret)
		{
			return;
		}

		uint16_t pos = pbl_capture_sentLen;
		ret = dict_write_int(&imgChunk, KEY_IMG_SIZE, &pos, 2, false);
		if (DICT_OK != ret)
		{
			return;
		}

		ret = dict_write_data(&imgChunk, KEY_IMG_START, http_capture_frameBuffer + pbl_capture_sentLen, BODY_LEN);
		if (DICT_OK != ret)
		{
			return;
		}
		pbl_capture_sentLen += BODY_LEN;

		dict_write_end(&imgChunk);

		mq_post(&imgChunk);
	}
}
Ejemplo n.º 4
0
void app_sync_init(struct AppSync *s,
                   uint8_t *buffer,
                   const uint16_t buffer_size,
                   const Tuplet * const keys_and_initial_values,
                   const uint8_t count,
                   AppSyncTupleChangedCallback tuple_changed_callback,
                   AppSyncErrorCallback error_callback,
                   void *context) {

    uint32_t final_buffer_size = buffer_size;
    if (dict_serialize_tuplets_to_buffer(keys_and_initial_values, count, buffer, &final_buffer_size) != DICT_OK)
        return;

    s->buffer = buffer;
    s->buffer_size = buffer_size;

    if (dict_write_begin(&s->current_iter, buffer, final_buffer_size) != DICT_OK)
        return;

    s->callback.context = context;
    s->callback.error = error_callback;
    s->callback.value_changed = tuple_changed_callback;

    // insert into list of regiested AppSync
    struct AppSyncList* pNode = (struct AppSyncList*) malloc(sizeof(struct AppSyncList));
    pthread_mutex_lock(&g_mxAppSyncList);
    pNode->s = s;
    pNode->next = g_pAppSyncList;
    g_pAppSyncList = pNode;
    pthread_mutex_unlock(&g_mxAppSyncList);

    // send to client
    DictionaryIterator* iter;
    app_message_outbox_begin(&iter);
    for (int i = 0; i < count; ++i) {
        dict_write_tuplet(iter, &(keys_and_initial_values[i]));
    }
    dict_write_end(iter);
    app_message_outbox_send();

    // notify
    Tuple* p = dict_read_first(&s->current_iter);
    while (p) {
        tuple_changed_callback(p->key, p, NULL, context);

        p = dict_read_next(&s->current_iter);
    }
}
Ejemplo n.º 5
0
void send_byte_array(){
  static const uint8_t data[] = {0};
  const uint32_t size = dict_calc_buffer_size(1, sizeof(data)+1);

  // Stack-allocated buffer in which to create the Dictionary:
  uint8_t buffer[size];  
  DictionaryIterator *iter;
  app_message_outbox_begin(&iter);
  // Begin:
  dict_write_begin(iter, buffer, sizeof(buffer));
  // Write the Data:
  dict_write_data(iter, XVALUE_KEY, data, sizeof(data));
  const uint32_t final_size = dict_write_end(iter);
  app_message_outbox_send();
  APP_LOG(APP_LOG_LEVEL_DEBUG, "**WATCH**: A message has been sent from pebble to the phone");

}
Ejemplo n.º 6
0
void activate_gesture(int16_t gesture_type) {
	vibes_short_pulse();
    app_message_register_outbox_sent(on_send_success);
    app_message_register_outbox_failed(on_send_failed);
    app_message_open(64, 64);
    DictionaryIterator *iter;
    if (!check_appmessage_result(app_message_outbox_begin(&iter))) return;
    static uint8_t outbox_buffer[50];
    switch (dict_write_begin(iter, outbox_buffer, 50)) {
        case DICT_OK: break;
        case DICT_INVALID_ARGS: APP_LOG(APP_LOG_LEVEL_ERROR, "send_alert_to_phone: DICT_INVALID_ARGS"); return;
        case DICT_INTERNAL_INCONSISTENCY: APP_LOG(APP_LOG_LEVEL_ERROR, "send_alert_to_phone: DICT_INTERNAL_INCONSISTENCY"); return;
        case DICT_MALLOC_FAILED: APP_LOG(APP_LOG_LEVEL_ERROR, "send_alert_to_phone: DICT_MALLOC_FAILED"); return;
        case DICT_NOT_ENOUGH_STORAGE: APP_LOG(APP_LOG_LEVEL_ERROR, "send_alert_to_phone: DICT_NOT_ENOUGH_STORAGE"); return;
    }
    dict_write_uint16(iter, 0x0, gesture_type);
    if (dict_write_end(iter) == 0) { APP_LOG(APP_LOG_LEVEL_ERROR, "send_alert_to_phone: ERROR ENDING DICTIONARY"); return; }
    check_appmessage_result(app_message_outbox_send());
}
Ejemplo n.º 7
0
DictionaryResult dict_serialize_tuplets_to_buffer_with_iter(DictionaryIterator *iter,
                                                            const Tuplet * const tuplets,
                                                            const uint8_t tuplets_count,
                                                            uint8_t *buffer,
                                                            uint32_t *size_in_out) {

    const uint32_t size = dict_calc_buffer_size_from_tuplets(tuplets, tuplets_count);
    if (size > *size_in_out)
        return DICT_NOT_ENOUGH_STORAGE;
    *size_in_out = size;

    DictionaryResult rval = dict_write_begin(iter, buffer, size);
    if (rval != DICT_OK)
        return rval;

    for (int i = 0; i < tuplets_count; ++i) {
        rval = dict_write_tuplet(iter, &(tuplets[i]));
        if (rval != DICT_OK)
            return rval;
    }

    return DICT_OK;
}
Ejemplo n.º 8
0
DictionaryResult dict_serialize_tuplets(DictionarySerializeCallback callback,
                                        void *context,
										const Tuplet * const tuplets,
                                        const uint8_t tuplets_count){

    const uint32_t size = dict_calc_buffer_size_from_tuplets(tuplets, tuplets_count);
    uint8_t* buffer = (uint8_t*) malloc(size);

    DictionaryIterator iter;
    DictionaryResult rval = dict_write_begin(&iter, buffer, size);
    if (rval != DICT_OK)
        return rval;

    for (int i = 0; i < tuplets_count; ++i) {
        rval = dict_write_tuplet(&iter, &(tuplets[i]));
        if (rval != DICT_OK)
            return rval;
    }

    callback(buffer, size, context);

    return DICT_OK;
}
Ejemplo n.º 9
0
DictionaryResult dict_merge(DictionaryIterator *dest,
                            uint32_t *dest_max_size_in_out,
                            DictionaryIterator *source,
                            const bool update_existing_keys_only,
                            const DictionaryKeyUpdatedCallback key_callback,
                            void *context) {

    // figure out size of new dictionary

    uint32_t new_size = 0;

    Tuple* tuple = dict_read_first(dest);
    while (tuple) {
        Tuple* sourceTuple = dict_find(source, tuple->key);
        if (sourceTuple)
            new_size = 7 + sourceTuple->length;
        else
            new_size = 7 + tuple->length;

        tuple = dict_read_next(dest);
    }

    if (!update_existing_keys_only) {
        tuple = dict_read_first(source);
        while (tuple) {
            Tuple* destTuple = dict_find(dest, tuple->key);
            if (!destTuple)
                new_size = 7 + destTuple->length;

            tuple = dict_read_next(source);
        }
    }

    if (new_size > *dest_max_size_in_out)
        return DICT_NOT_ENOUGH_STORAGE;

    // allocate temporary dictionary to hold previous dest
    const uint16_t old_size = dest->end - (void*)dest->dictionary;
    uint8_t* temp_buffer = (uint8_t*) malloc(old_size);
    memcpy(temp_buffer, dest->dictionary, old_size);
    DictionaryIterator temp_iter;
    tuple = dict_read_begin_from_buffer(&temp_iter, temp_buffer, old_size);
    if (!tuple)
        return DICT_INTERNAL_INCONSISTENCY;

    // re-init dest
    DictionaryResult rval = dict_write_begin(dest, (uint8_t*)dest->dictionary, *dest_max_size_in_out);
    if (rval != DICT_OK)
        return rval;

    // merge existing
    while (tuple) {
        Tuple* sourceTuple = dict_find(source, tuple->key);
        if (sourceTuple) {
            Tuple* newTuple = dest->cursor;
            rval = dict_write_data_helper(dest, sourceTuple->key, sourceTuple->type, sourceTuple->value, sourceTuple->length);
            if (rval != DICT_OK)
                return rval;

            if (key_callback)
                key_callback(tuple->key, newTuple, tuple, context);
        }
        else {
            rval = dict_write_data_helper(dest, tuple->key, tuple->type, tuple->value, tuple->length);
            if (rval != DICT_OK)
                return rval;
        }

        tuple = dict_read_next(&temp_iter);
    }

    // merge new
    if (!update_existing_keys_only) {
        tuple = dict_read_first(source);
        while (tuple) {
            Tuple* destTuple = dict_find(&temp_iter, tuple->key);
            if (!destTuple) {
                rval = dict_write_data_helper(dest, tuple->key, tuple->type, tuple->value, tuple->length);
                if (rval != DICT_OK)
                    return rval;
            }

            tuple = dict_read_next(source);
        }
    }

    free(temp_buffer);
    *dest_max_size_in_out = dict_write_end(dest);

    return DICT_OK;
}
Ejemplo n.º 10
0
void locale_init(void) {
  //hard-coded for testing 
  //const char* locale_str = "de";

  // Detect system locale
  const char* locale_str = i18n_get_system_locale();
  ResHandle locale_handle = NULL;
  int locale_size = 0;

  if (strncmp(locale_str, "fr", 2) == 0) {
    locale_handle = resource_get_handle(RESOURCE_ID_LOCALE_FRENCH);
    locale_size = resource_size(locale_handle);
  } else if (strncmp(locale_str, "es", 2) == 0) {
    locale_handle = resource_get_handle(RESOURCE_ID_LOCALE_SPANISH);
    locale_size = resource_size(locale_handle);
  } else if (strncmp(locale_str, "de", 2) == 0) {
    locale_handle = resource_get_handle(RESOURCE_ID_LOCALE_GERMAN);
    locale_size = resource_size(locale_handle);
  }

  // Fallback to English for unlocalized languages (0 byte files)
  if (locale_size == 0) {
    locale_handle = resource_get_handle(RESOURCE_ID_LOCALE_ENGLISH);
    locale_size = resource_size(locale_handle);
  }

  int resource_offset = 0;
  int locale_entries = 0;
  resource_offset += resource_load_byte_range(locale_handle, resource_offset, 
      (uint8_t*)&locale_entries, sizeof(locale_entries));

  struct locale {
    int32_t hashval;
    int32_t strlen;
  } locale_info;

  int dict_buffer_size = locale_size + 7 * locale_entries; //7 byte header per item
  char *dict_buffer = malloc(dict_buffer_size);
  dict_write_begin(&s_locale_dict, (uint8_t*)dict_buffer, dict_buffer_size);

  for (int i = 0; i < locale_entries; i++) {
    resource_offset += resource_load_byte_range(locale_handle, resource_offset, 
        (uint8_t*)&locale_info, sizeof(struct locale));

    struct Tuplet tupl = {
      .type = TUPLE_CSTRING, 
      .key = locale_info.hashval, 
      .cstring.length = locale_info.strlen};

    tupl.cstring.data = malloc(tupl.cstring.length);

    resource_offset += resource_load_byte_range(locale_handle, 
        resource_offset, (uint8_t*)tupl.cstring.data, tupl.cstring.length);

    dict_write_tuplet(&s_locale_dict, &tupl);
  }

  dict_write_end(&s_locale_dict);
}

char *locale_str(int hashval) { 
  Tuple *tupl = dict_find(&s_locale_dict, hashval);

  if (tupl && tupl->value->cstring) {
    return tupl->value->cstring;
  }
  return "\7"; //return blank character
}
Ejemplo n.º 11
0
AppMessageResult app_message_outbox_begin__patch(DictionaryIterator** iter) {
  static char massive_buffer;
  dict_write_begin(*iter, &massive_buffer, 1);
  return APP_MSG_OK;
}