Ejemplo n.º 1
0
mqtt_data_t json_deserialize_command(jsmntok_t *tokens, char* payload, mqtt_data_t data_t)
{
	char* temp_name = json_token_tostr(payload, &tokens[9]);
	char* name = BS_MEM_ALLOC(strlen(temp_name) + 1);
	memcpy(name, temp_name, strlen(temp_name) + 1);

	//data_t->ticketId = uid;

	int max = (&tokens[11])->size;
	printf("Command: %s (%d params)\r\n", name, max / 2);
	int index = 0;

	char** keys = BS_MEM_ALLOC(max / 2);
	int key_index = 0;
	for(index = 0; index < max; index+=2, key_index++)
	{
		char* temp_pname = json_token_tostr(payload, &tokens[12 + index]);
		keys[key_index] = BS_MEM_ALLOC(strlen(temp_pname) + 1);
		memcpy(keys[key_index], temp_pname, strlen(temp_pname) + 1);

		char* temp = json_token_tostr(payload, &tokens[12 + index + 1]);
		char* param_value = BS_MEM_ALLOC(strlen(temp) + 1);
		memcpy(param_value, temp, strlen(temp) + 1);

		printf("%s: %s\r\n", keys[key_index], param_value);
		//x.fieldname = param_name;

		bsd_data_t x;

		if(strcmp(param_value, "true") == 0)
		{
			printf("param bool @ true\r\n");
			x.kind = BSD_BOOL;
			x.content.boolean = true;
		}
		else if(strcmp(param_value, "false") == 0)
		{
			printf("param bool @ false\r\n");
			x.kind = BSD_BOOL;
			x.content.boolean = false;
		}
		else
		{
			// Not supported
//    			x.content.string.length = strlen(param_value);
//    			x.content.string.data = param_value;
		}

		data_t.values[key_index] = BS_MEM_ALLOC(sizeof(x));
		memcpy(data_t.values[key_index], &x, sizeof(x));
	}
	data_t.path = name;
	data_t.nbofvalues = max / 2;
	data_t.keys = keys;
	return data_t;
}
Ejemplo n.º 2
0
int sdb_untrim( sdb_table_t *tbl) {
    if( tbl->bss_ctx) return SDB_EBADSTATE; else {
        bss_ctx_t *bss = BS_MEM_ALLOC( sizeof( *bss));
        if( ! bss) return SDB_EMEM;
        bss_init( bss, sdb_bss_writer, tbl);
        bss->supports_chunks = 0; // Stagedb can't currently handle chunked data
        tbl->bss_ctx = bss;
        return SDB_EOK;
    }
}
Ejemplo n.º 3
0
int sdb_setconstable(  sdb_table_t *src,  sdb_table_t *dst) {
    struct sdb_consolidation_t *cons;
    if( (src->state == SDB_ST_BROKEN) || (src->state == SDB_ST_UNCONFIGURED)) return SDB_EBADSTATE;
    if( src->consolidation) return SDB_EINVALID;
    cons = BS_MEM_ALLOC( sizeof( * cons) + dst->ncolumns * sizeof( * cons->dst_columns));
    if( ! cons) return SDB_EMEM;
    cons->dst = dst;
    cons->dst_columns  = (void*) (cons+1); // TODO: use the struct hack [http://c2.com/cgi/wiki?StructHack]?
    cons->conf_col     = 0;
    src->consolidation = cons;
    return SDB_EOK;
}
Ejemplo n.º 4
0
/* Release all resources reserved by the table.
 * If there is a serialization in progress, it is canceled. */
int sdb_reset( sdb_table_t *tbl) {
    int i;
    if( tbl->state == SDB_ST_SERIALIZING) sdb_serialize_cancel( tbl);
    if( tbl->state != SDB_ST_READING) return SDB_EBADSTATE;
    // TODO must be able to reset an unconfigured table

    switch( tbl->storage_kind) {
    case SDB_SK_RAM: {
        struct sdb_chunk_t *p, *q;
        struct sdb_ram_storage_t *ram = & tbl->storage.ram;
        p = ram->first_chunk;
        while( p) {
            q=p->next; BS_MEM_FREE( p); p=q;
        }
        p = BS_MEM_ALLOC( sizeof( struct sdb_chunk_t) + SDB_MIN_CHUNK_SIZE - SDB_CHUNK_SIZE);
        if( ! p) { tbl->state = SDB_ST_BROKEN; return SDB_EMEM; }
        p->next = NULL;
        ram->first_chunk = p;
        ram->last_chunk = p;
        ram->last_chunk_ptr =  & ram->last_chunk;
        ram->last_chunk_size = SDB_MIN_CHUNK_SIZE;
        break;
    }
#ifdef SDB_FILE_SUPPORT
    case SDB_SK_FILE:
        // identifier/filename is stored as the 1st conf string
        tbl->storage.file = freopen( tbl->conf_strings, "w+", tbl->storage.file); // erases content
        if( ! tbl->storage.file) return SDB_EBADFILE;
        break;
#endif
    }
    tbl->nwrittenbytes     = 0;
    tbl->nwrittenobjects   = 0;

    // reset data analysis
    for( i=0; i<tbl->ncolumns; i++) {
        sdb_column_t *c = tbl->columns + i;
        if( SDB_SM_SMALLEST == SDB_SM_CONTAINER(c->serialization_method)) {
            c->data_analysis.delta_sum = 0;
            c->data_analysis.all_integer = 1;
            c->data_analysis.all_numeric = 1;
        }
    }

    if( tbl->bss_ctx) bss_reset( tbl->bss_ctx);
    return SDB_EOK;
}
Ejemplo n.º 5
0
static int decodeClass( bsd_ctx_t *ctx, bsd_data_t *x, const uint8_t *buffer, int length) {
  int nread, i, named, fieldnread;
  size_t nfields, blocksize, namesize;
  bs_classid_t classid;
  const char *name;
  const uint8_t *fieldbuffer;
  void *endofclass;
  bs_class_t *classdef;

  CHECK_LENGTH( 1);
  switch( buffer[0]) {
  case 0x71: named = 1; break;
  case 0x72: named = 0; name = NULL; namesize = 0; break;
  default: return 0;
  }

  // decoding is done in two passes: 1st to compute the size and allocate right amount of memory,
  // second one to make the actual data copy.
  blocksize = sizeof( bs_class_t);

  CHECK_SUBDECODE( bsd_uis( ctx, x, buffer + nread, length - nread ), BSD_INT);
  classid = x->content.i;
  if( named) {
    CHECK_SUBDECODE( bsd_uis( ctx, x, buffer + nread, length - nread ), BSD_STRING);
    name = x->content.string.data;
    namesize = x->content.string.length + 1; // add null terminator
  }
  CHECK_SUBDECODE( bsd_uis( ctx, x, buffer + nread, length - nread), BSD_INT);
  nfields = x->content.i;

  // allocate class structure
  blocksize = sizeof( bs_class_t) + namesize + nfields * sizeof( struct bs_field_t);

  // 1st pass
  fieldbuffer = buffer;
  fieldnread = nread;
  for( i=0; i<nfields; i++) {
    if( named) {
      CHECK_SUBDECODE( bsd_uis( ctx, x, buffer + nread, length - nread ), BSD_STRING);
      blocksize += x->content.length + 1;
    }
    CHECK_LENGTH( nread + 1);
  }

  // memory allocation
  classdef = BS_MEM_ALLOC( blocksize);
  if( NULL == classdef) return bsd_error( x, BSD_EMEMORY);
  classdef->classid = classid;
  classdef->nfields = nfields;
  classdef->mode = BS_CLASS_MANAGED;
  endofclass = ((void *)classdef) + sizeof( bs_class_t);
  classdef->classname = NULL;
  if( named) {
    memcpy( endofclass, name, namesize-1);
    ((char *)endofclass)[namesize-1] = '\0';
    classdef->classname = endofclass;
    endofclass += namesize;
  }
  classdef->fields = endofclass;
  endofclass += nfields * sizeof( struct bs_field_t);

  // 2nd pass (no possible parsing errors since processing is the same as 1st pass)
  buffer = fieldbuffer;
  nread = fieldnread;
  for( i=0; i<classdef->nfields; i++) {
    if( named) {
      CHECK_SUBDECODE( bsd_uis( ctx, x, buffer + nread, length - nread ), BSD_STRING);
      memcpy( endofclass, x->content.string.data, x->content.string.length);
      ((char *)endofclass)[x->content.string.length] = '\0';
      classdef->fields[i].name = endofclass;
      endofclass += x->content.string.length + 1;
    } else {
      classdef->fields[i].name = NULL;
    }
    CHECK_LENGTH( nread + 1);
    classdef->fields[i].ctxid = buffer[nread - 1];
  }

  bsd_addClass( ctx, classdef);
  x->kind = BSD_CLASSDEF;
  x->content.classdef = classdef;
  return nread;
}
Ejemplo n.º 6
0
/*
 * Initialize a table structure, return SDB_EOK or SDB_EMEM.
 * The result table must still have its columns configured with
 * calls to sdb_column() before it can accept data.
 *
 * This form is primarily useful when the column number,
 * names and serialization methods are not known at compile-time.
 * In simple cases, the sdb_init() interface is lighter and more readable.
 *
 * Initialize the table passed as 1st parameter as an unconfigured table.
 * next steps: configure its columns with sdb_column(), write data in it,
 * periodically serialize or consolidate it. */
static int sdb_initwithoutcolumns( sdb_table_t *tbl, const char *id, const char *path, sdb_ncolumn_t ncolumns,
		enum sdb_storage_kind_t storage_kind) {
    int r;
#ifdef SDB_VERBOSE_PRINT
    printf( "Initialized %d bytes of sdb struct\n", sizeof( * tbl));
#endif

    if( 0 == ncolumns || SDB_NCOLUMN_INVALID <= ncolumns)
        return SDB_EINVALID;

    memset( tbl, 0, sizeof( *tbl));
    tbl->conf_string_idx   = 0;
    tbl->conf_strings      = NULL;

    /* The first string in `conf_strings` is the table id, the 2nd is the table path */
    r = new_conf_string( tbl, id);
    if( r<0) goto fail_id;
    r = new_conf_string( tbl, path);
    if( r<0) goto fail_path;

    /* Allocate dynamic arrays. */
    tbl->columns = BS_MEM_ALLOC( ncolumns * sizeof( tbl->columns[0]));
    if( ! tbl->columns) goto fail_columns;

    /* Other initializations. */
    tbl->state             = SDB_ST_UNCONFIGURED;
    tbl->ncolumns          = ncolumns;
    tbl->consolidation     = NULL;
    tbl->serialization_ctx = NULL;
    tbl->nwrittenbytes     = 0;
    tbl->nwrittenobjects   = 0;
    tbl->conf_col          = 0;
    tbl->maxwrittenobjects = 0;

    r = sdb_untrim( tbl);
    if( r<0) goto fail_untrim;

    switch(storage_kind) {
    case SDB_SK_RAM: {
        struct sdb_ram_storage_t *ram = & tbl->storage.ram;
        sdb_chunk_t *c = BS_MEM_ALLOC( sizeof( * c)+SDB_MIN_CHUNK_SIZE-SDB_CHUNK_SIZE);
        if( ! c) goto fail_chunk;
        c->next = NULL;
        tbl->storage_kind = SDB_SK_RAM;
        ram->first_chunk = c;
        ram->last_chunk = c;
        ram->last_chunk_ptr = & ram->last_chunk;
        ram->last_chunk_size = SDB_MIN_CHUNK_SIZE;
        break;
    }
#ifdef SDB_FLASH_SUPPORT
    case SDB_SK_FLASH: {
        tbl->storage_kind = SDB_SK_FLASH;
//#        error "flash storage not implemented"
        return -1;

        break;
    }
#endif
#ifdef SDB_FILE_SUPPORT
    case SDB_SK_FILE: {
        FILE *fd = fopen( id, "a+");
        if( ! fd) return SDB_EBADFILE;
        tbl->storage_kind = SDB_SK_FILE;
        tbl->storage.file = fd;

        // TODO: get in a state where config is retrieved and
        // column configuration through sdb_setcolumn() calls
        // can be skipped?
        break;
    }
#endif
    }

    /* Handle allocation failures. */
    if( 0) {
        fail_chunk:
        sdb_trim( tbl);
        fail_untrim:
        BS_MEM_FREE( tbl->columns);
        fail_columns:
        fail_path:
        BS_MEM_FREE( tbl->conf_strings);
        fail_id:
        memset( tbl, 0, sizeof( *tbl));
        tbl->state = SDB_ST_BROKEN;
        return SDB_EMEM;
    }

    return SDB_EOK;
}