void unit_message_serialize_request(UNUSED(void **state)) { msgpack_sbuffer sbuf; msgpack_packer pk; struct message_request request; array params; params.size = 1; params.obj = CALLOC(1, struct message_object); params.obj[0].type = OBJECT_TYPE_UINT; params.obj[0].data.uinteger = 1234; msgpack_sbuffer_init(&sbuf); /* positiv test */ msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); request.msgid = 1234; request.method = (string) {.str = "test method", .length = sizeof("test method") - 1}; request.params = params; assert_int_equal(0, message_serialize_request(&request, &pk)); msgpack_sbuffer_clear(&sbuf); /* no valid string */ msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); request.msgid = 1234; request.method = (string) STRING_INIT; request.params = params; assert_int_not_equal(0, message_serialize_request(&request, &pk)); msgpack_sbuffer_clear(&sbuf); free_params(request.params); params.size = 1; params.obj = CALLOC(1, struct message_object); params.obj[0].type = 1000; params.obj[0].data.uinteger = 1234; /* no valid params */ msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); request.msgid = 1234; request.method = (string) {.str = "test method", .length = sizeof("test method") - 1}; request.params = params; assert_int_not_equal(0, message_serialize_request(&request, &pk)); msgpack_sbuffer_clear(&sbuf); free_params(request.params); /* null check */ assert_int_not_equal(0, message_serialize_request(NULL, NULL)); msgpack_sbuffer_destroy(&sbuf); }
void unit_message_is_response(UNUSED(void **state)) { msgpack_sbuffer sbuf; msgpack_packer pk; msgpack_zone mempool; msgpack_object deserialized; msgpack_sbuffer_init(&sbuf); msgpack_zone_init(&mempool, 2048); /* positiv test */ msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); msgpack_pack_array(&pk, 4); msgpack_pack_uint8(&pk, 1); msgpack_pack_uint8(&pk, 0); msgpack_pack_uint8(&pk, 0); msgpack_pack_uint8(&pk, 0); msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized); assert_true(message_is_response(&deserialized)); msgpack_sbuffer_clear(&sbuf); /* wrong type test */ msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); msgpack_pack_array(&pk, 4); msgpack_pack_uint8(&pk, 0); msgpack_pack_uint8(&pk, 1); msgpack_pack_uint8(&pk, 1); msgpack_pack_uint8(&pk, 1); msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized); assert_false(message_is_response(&deserialized)); msgpack_sbuffer_clear(&sbuf); /* no array test */ msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); msgpack_pack_uint8(&pk, 1); msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized); assert_false(message_is_response(&deserialized)); msgpack_sbuffer_clear(&sbuf); /* NULL test */ assert_false(message_is_response(NULL)); msgpack_sbuffer_clear(&sbuf); msgpack_zone_destroy(&mempool); msgpack_sbuffer_destroy(&sbuf); }
static inline msgpack_packer *monitor_method_new(char *method, msgpack_sbuffer *sbuffer) { msgpack_packer *pk; msgpack_sbuffer_clear(sbuffer); pk = msgpack_packer_new(sbuffer, msgpack_sbuffer_write); monitor_method_add(method, pk); return pk; }
int filter_send_message(Filter * const filter) { ssize_t written = safe_write(filter->upipe_stdin.fd_write, filter->msgpack_sbuffer->data, filter->msgpack_sbuffer->size, -1); msgpack_sbuffer_clear(filter->msgpack_sbuffer); if (written != (ssize_t) 0) { return -1; } return 0; }
void pack_event(rbkit_event_header *event_header, msgpack_packer *packer) { msgpack_sbuffer *sbuf = packer->data; msgpack_sbuffer_clear(sbuf); switch (event_header->event_type) { case obj_created: pack_obj_created_event((rbkit_obj_created_event *)event_header, packer); break; case obj_destroyed: pack_obj_destroyed_event((rbkit_obj_destroyed_event *)event_header, packer); break; case gc_start: pack_event_header_only(event_header, packer); break; case gc_end_m: pack_event_header_only(event_header, packer); break; case gc_end_s: pack_event_header_only(event_header, packer); break; case object_space_dump: pack_object_space_dump_event((rbkit_object_space_dump_event *)event_header, packer); break; case gc_stats: pack_hash_event((rbkit_hash_event *)event_header, packer); break; case handshake: pack_hash_event((rbkit_hash_event *)event_header, packer); break; case cpu_sample: pack_cpu_sample_event((rbkit_cpu_sample_event *)event_header, packer); break; case event_collection: pack_event_collection_event((rbkit_event_collection_event *)event_header, packer); break; default: rb_raise(rb_eNotImpError, "Rbkit : Unpacking of event type '%u' not implemented", event_header->event_type); } }
void unit_message_deserialize_response(UNUSED(void **state)) { struct api_error error = ERROR_INIT; msgpack_sbuffer sbuf; msgpack_packer pk; msgpack_zone mempool; msgpack_object deserialized; struct message_response response; msgpack_sbuffer_init(&sbuf); msgpack_zone_init(&mempool, 2048); /* positiv test */ msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); msgpack_pack_array(&pk, 4); msgpack_pack_uint8(&pk, 1); msgpack_pack_uint32(&pk, 1234); msgpack_pack_nil(&pk); msgpack_pack_array(&pk, 1); msgpack_pack_uint8(&pk, 0); msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized); assert_int_equal(0, message_deserialize_response(&response, &deserialized, &error)); msgpack_sbuffer_clear(&sbuf); free_params(response.params); /* wrong type type */ msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); msgpack_pack_array(&pk, 4); msgpack_pack_nil(&pk); msgpack_pack_uint32(&pk, 1234); msgpack_pack_nil(&pk); msgpack_pack_array(&pk, 1); msgpack_pack_uint8(&pk, 0); msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized); assert_int_not_equal(0, message_deserialize_response(&response, &deserialized, &error)); msgpack_sbuffer_clear(&sbuf); /* wrong type */ msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); msgpack_pack_array(&pk, 4); msgpack_pack_uint8(&pk, 0); msgpack_pack_uint32(&pk, 1234); msgpack_pack_nil(&pk); msgpack_pack_array(&pk, 1); msgpack_pack_uint8(&pk, 0); msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized); assert_int_not_equal(0, message_deserialize_response(&response, &deserialized, &error)); msgpack_sbuffer_clear(&sbuf); /* wrong msgid */ msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); msgpack_pack_array(&pk, 4); msgpack_pack_uint8(&pk, 1); msgpack_pack_int(&pk, -1234); msgpack_pack_nil(&pk); msgpack_pack_array(&pk, 1); msgpack_pack_uint8(&pk, 0); msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized); assert_int_not_equal(0, message_deserialize_response(&response, &deserialized, &error)); msgpack_sbuffer_clear(&sbuf); /* wrong msgid value*/ msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); msgpack_pack_array(&pk, 4); msgpack_pack_uint8(&pk, 1); msgpack_pack_uint32(&pk, UINT32_MAX); msgpack_pack_nil(&pk); msgpack_pack_array(&pk, 1); msgpack_pack_uint8(&pk, 0); msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized); assert_int_not_equal(0, message_deserialize_response(&response, &deserialized, &error)); msgpack_sbuffer_clear(&sbuf); /* wrong nil */ msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); msgpack_pack_array(&pk, 4); msgpack_pack_uint8(&pk, 1); msgpack_pack_uint32(&pk, 1234); msgpack_pack_uint8(&pk, 1); msgpack_pack_array(&pk, 1); msgpack_pack_uint8(&pk, 0); msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized); assert_int_not_equal(0, message_deserialize_response(&response, &deserialized, &error)); msgpack_sbuffer_clear(&sbuf); /* wrong params */ msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); msgpack_pack_array(&pk, 4); msgpack_pack_uint8(&pk, 1); msgpack_pack_uint32(&pk, 1234); msgpack_pack_nil(&pk); msgpack_pack_nil(&pk); msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized); assert_int_not_equal(0, message_deserialize_response(&response, &deserialized, &error)); msgpack_sbuffer_clear(&sbuf); /* null input params */ assert_int_not_equal(0, message_deserialize_response(&response, &deserialized, NULL)); assert_int_not_equal(0, message_deserialize_response(&response, NULL, &error)); assert_int_not_equal(0, message_deserialize_response(NULL, &deserialized, &error)); msgpack_zone_destroy(&mempool); msgpack_sbuffer_destroy(&sbuf); }
int _main(void) { THROW_BEGIN() /* init scl and get sockets:: */ THROW_ON_ERR(scl_init("arduino")); void *rc_socket = scl_get_socket("rc_raw"); THROW_IF(rc_socket == NULL, -ENODEV); void *power_socket = scl_get_socket("power"); THROW_IF(power_socket == NULL, -ENODEV); /* allocate msgpack buffers: */ msgpack_sbuffer *msgpack_buf = msgpack_sbuffer_new(); THROW_IF(msgpack_buf == NULL, -ENOMEM); msgpack_packer *pk = msgpack_packer_new(msgpack_buf, msgpack_sbuffer_write); THROW_IF(pk == NULL, -ENOMEM); /* fetch parameters: */ char *dev_path; tsint_t dev_speed; opcd_params_init("exynos_quad.arduino_serial.", 0); opcd_param_t params[] = { {"path", &dev_path}, {"speed", &dev_speed}, OPCD_PARAMS_END }; opcd_params_apply("", params); /* opern serial port: */ serialport_t port; THROW_ON_ERR(serial_open(&port, dev_path, tsint_get(&dev_speed), O_RDONLY)); uint16_t channels_raw[PPM_CHAN_MAX]; uint32_t voltage_raw, current_raw; float channels[PPM_CHAN_MAX]; while (running) { int c = serial_read_char(&port); if (c < 0) msleep(1); /* parse channels: */ int status = ppm_parse_frame(channels_raw, (uint8_t)(c)); if (status) { int sig_valid = 0; int invalid_count = 0; FOR_N(i, PPM_CHAN_MAX) { uint16_t chan_in = channels_raw[i]; if (chan_in >= PPM_VALUE_INVALID) invalid_count++; if (chan_in > PPM_VALUE_MAX) chan_in = PPM_VALUE_MAX; if (chan_in < PPM_VALUE_MIN) chan_in = PPM_VALUE_MIN; channels[i] = (float)(chan_in - PPM_VALUE_MIN) / (PPM_VALUE_MAX - PPM_VALUE_MIN) * 2.f - 1.f; } sig_valid = (invalid_count < (PPM_CHAN_MAX / 3)); /* send channels: */ msgpack_sbuffer_clear(msgpack_buf); msgpack_pack_array(pk, 1 + PPM_CHAN_MAX); PACKI(sig_valid); /* index 0: valid */ PACKFV(channels, PPM_CHAN_MAX); /* index 1, .. : channels */ scl_copy_send_dynamic(rc_socket, msgpack_buf->data, msgpack_buf->size); } /* parse adc voltage/current: */ status = power_parse_frame(&voltage_raw, ¤t_raw, (uint8_t)(c)); if (status) { float voltage = (float)(voltage_raw) / 1000.0; float current = (float)(current_raw) / 1000.0; /* send voltage / current: */ msgpack_sbuffer_clear(msgpack_buf); msgpack_pack_array(pk, 2); PACKF(voltage); /* index 0 */ PACKF(current); /* index 1 */ scl_copy_send_dynamic(power_socket, msgpack_buf->data, msgpack_buf->size); } }
int main_i2c(void) { THROW_BEGIN(); void *gps_socket = scl_get_socket("gps"); THROW_IF(gps_socket == NULL, -EIO); int64_t hwm = 1; zmq_setsockopt(gps_socket, ZMQ_SNDHWM, &hwm, sizeof(hwm)); void *sats_socket = scl_get_socket("sats"); THROW_IF(sats_socket == NULL, -EIO); i2c_bus_t bus; i2c_dev_t device; uint8_t data_w[128]; uint8_t data_r[128]; if (i2c_bus_open(&bus, "/dev/i2c-1")) { syslog(LOG_CRIT, "could not open i2c device"); exit(EXIT_FAILURE); } i2c_dev_init(&device, &bus, I2C_GPS_ADDRESS); msgpack_sbuffer *msgpack_buf = msgpack_sbuffer_new(); THROW_IF(msgpack_buf == NULL, -ENOMEM); msgpack_packer *pk = msgpack_packer_new(msgpack_buf, msgpack_sbuffer_write); THROW_IF(pk == NULL, -ENOMEM); while (1) { msleep(200); data_w[0] = I2C_GPS_STATUS; i2c_xfer(&device, 1, data_w, 1, data_r); msgpack_sbuffer_clear(msgpack_buf); int status = data_r[0]; int fix = 0; if (status & I2C_GPS_STATUS_2DFIX) fix = 2; if (status & I2C_GPS_STATUS_3DFIX) fix = 3; if (fix == 2) msgpack_pack_array(pk, 7); /* 2d fix */ else if (fix == 3) msgpack_pack_array(pk, 9); /* 3d fix */ else msgpack_pack_array(pk, 1); /* no fix */ data_w[0] = I2C_GPS_TIME; i2c_xfer(&device, 1, data_w, 4, data_r); long time = (((long) data_r[3]) << 24) | (((long) data_r[2]) << 16) | (((long) data_r[1]) << 8) | (data_r[0]); time /= 1000; char *time_str = ctime(&time); size_t len = strlen(time_str); msgpack_pack_raw(pk, len); msgpack_pack_raw_body(pk, time_str, len); /* gps array index 0 */ if (fix == 2 || fix == 3) { data_w[0] = I2C_GPS_LOCATION; i2c_xfer(&device, 1, data_w, 8, data_r); PACKD(( (((long) data_r[3]) << 24) | (((long) data_r[2]) << 16) | (((long) data_r[1]) << 8) | (data_r[0])) / 10000000.0); /* latitude, gps array index 1 */ PACKD(( (((long) data_r[7]) << 24) | (((long) data_r[6]) << 16) | (((long) data_r[5]) << 8) | (data_r[4])) / 10000000.0); /* logitude, gps array index 2 */ PACKI((status & I2C_GPS_STATUS_NUMSATS) >> 4); /* gps array index 3 */ data_w[0] = I2C_GPS_GROUND_SPEED; i2c_xfer(&device, 1, data_w, 2, data_r); PACKF(((float)((data_r[1] << 8) | data_r[0])) / 100.0 * 1.94384); /* gps array index 4 */ data_w[0] = I2C_GPS_GROUND_SPEED; i2c_xfer(&device, 1, data_w, 2, data_r); PACKF((data_r[1] << 8) | data_r[0]); /* gps array index 5 */ PACKF(0 /* HDOP */); /* gps array index 6 */ } if (fix == 3) { data_w[0] = I2C_GPS_ALTITUDE; i2c_xfer(&device, 1, data_w, 2, data_r); PACKF(((data_r[1]) << 8) | data_r[0]); /* gps array index 7 */ PACKF(0 /* VDOP */); /* gps array index 8 */ } scl_copy_send_dynamic(gps_socket, msgpack_buf->data, msgpack_buf->size); }
void clear() { msgpack_sbuffer_clear(this); }
static inline void rbtrace__send_event(int nargs, const char *name, ...) { if (!rbtracer.attached_pid || !rbtracer.sbuf || !rbtracer.msgpacker || rbtracer.mqo_fd == -1) return; int n; msgpack_sbuffer_clear(rbtracer.sbuf); msgpack_packer *pk = rbtracer.msgpacker; msgpack_pack_array(pk, nargs+1); msgpack_pack_raw(pk, strlen(name)); msgpack_pack_raw_body(pk, name, strlen(name)); if (nargs > 0) { int type; int sint; uint32_t uint; uint64_t uint64; unsigned long ulong; char *str; va_list ap; va_start(ap, name); for (n=0; n<nargs; n++) { type = va_arg(ap, int); switch (type) { case 'b': // boolean if (va_arg(ap, int)) msgpack_pack_true(pk); else msgpack_pack_false(pk); break; case 'd': // signed int sint = va_arg(ap, int); msgpack_pack_int(pk, sint); break; case 'u': // unsigned int uint = va_arg(ap, uint32_t); msgpack_pack_uint32(pk, uint); break; case 'l': // unsigned long (VALUE/ID) ulong = va_arg(ap, unsigned long); msgpack_pack_unsigned_long(pk, ulong); break; case 't': // uint64 (timestamps) uint64 = va_arg(ap, uint64_t); msgpack_pack_uint64(pk, uint64); break; case 'n': // current timestamp msgpack_pack_uint64(pk, timeofday_usec()); break; case 's': // string str = va_arg(ap, char *); if (!str) str = (char *)""; msgpack_pack_raw(pk, strlen(str)); msgpack_pack_raw_body(pk, str, strlen(str)); break; default: fprintf(stderr, "unknown type (%d) passed to rbtrace__send_event for %s\n", (int)type, name); } }