void in_received_handler( DictionaryIterator *received, void *context) { // incoming message received APP_LOG(APP_LOG_LEVEL_DEBUG, "In Received"); uint32_t size = dict_size(received); uint8_t buffer[size]; Tuple *tuple = dict_read_begin_from_buffer(received, buffer, size); char buf[500]; while(tuple) { char tmp[500]; tuple = dict_read_next(received); snprintf(tmp, 500, "%s\ntype: %d data:%s", buf, tuple->type, tuple->value->cstring); snprintf(buf, 500, "%s", tmp); } tuple = dict_read_first(received); while (tuple) { char name[256]; char value[256]; for(int i = 0; i < tuple->length; i++) { name[i] = tuple->value->cstring[i]; } tuple = dict_read_next(received); if(tuple) { for(int i = 0; i < tuple->length; i++) { value[i] = tuple->value->cstring[i]; } put_entry(name, value); } else { break; } } }
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; }