unsigned int sw_location(char* hardwareId, float lat, float lon, float ele, int64_t eventDate, uint8_t* buffer, size_t length, char* originator) { pb_ostream_t stream = pb_ostream_from_buffer(buffer, length); SiteWhere_Header header = { }; header.command = SiteWhere_Command_DEVICELOCATION; if (originator != NULL) { header.has_originator = true; strcpy(header.originator, originator); } if (!pb_encode_delimited(&stream, SiteWhere_Header_fields, &header)) { return 0; } SiteWhere_DeviceLocation location = { }; strcpy(location.hardwareId, hardwareId); location.latitude = float_to_double(lat); location.longitude = float_to_double(lon); location.elevation = float_to_double(ele); location.has_elevation = true; if (eventDate != NULL) { location.has_eventDate = true; location.eventDate = eventDate; } if (!pb_encode_delimited(&stream, SiteWhere_DeviceLocation_fields, &location)) { return 0; } return stream.bytes_written; }
unsigned int sw_measurement(char* hardwareId, char* name, float value, int64_t eventDate, uint8_t* buffer, size_t length, char* originator) { pb_ostream_t stream = pb_ostream_from_buffer(buffer, length); SiteWhere_Header header = { }; header.command = SiteWhere_Command_DEVICEMEASUREMENT; if (originator != NULL) { header.has_originator = true; strcpy(header.originator, originator); } if (!pb_encode_delimited(&stream, SiteWhere_Header_fields, &header)) { return 0; } SiteWhere_DeviceMeasurements measurements = { }; strcpy(measurements.hardwareId, hardwareId); SiteWhere_Measurement measurement = { }; strcpy(measurement.measurementId, name); measurement.measurementValue = float_to_double(value); measurements.measurement[0] = measurement; measurements.measurement_count = 1; if (eventDate != NULL) { measurements.has_eventDate = true; measurements.eventDate = eventDate; } if (!pb_encode_delimited(&stream, SiteWhere_DeviceMeasurements_fields, &measurements)) { return 0; } return stream.bytes_written; }
unsigned int openiot_location(char *hardwareId, char *lat, char *lon, char *ele, int64_t eventDate, uint8_t *buffer, size_t length, char *originator) { pb_ostream_t stream = pb_ostream_from_buffer(buffer, length); OpenIoT_Header header = {}; header.command = OpenIoT_Command_DEVICELOCATION; if (originator != NULL) { header.has_originator = true; strcpy(header.originator, originator); } if (!pb_encode_delimited(&stream, OpenIoT_Header_fields, &header)) { return 0; } OpenIoT_DeviceLocation location = {}; strcpy(location.hardwareId, hardwareId); strcpy(location.latitude, lat); strcpy(location.longitude, lon); strcpy(location.elevation, ele); location.has_elevation = true; if (eventDate != 0) { location.has_eventDate = true; location.eventDate = eventDate; } if (!pb_encode_delimited(&stream, OpenIoT_DeviceLocation_fields, &location)) { return 0; } return stream.bytes_written; }
void send_debug_telemetry() { #ifdef SEND_TELEMETRY shedBoat_Telemetry telemetryMessage = shedBoat_Telemetry_init_zero; telemetryMessage.status = shedBoat_Telemetry_Status_STATIONARY; telemetryMessage.has_debug = true; telemetryMessage.debug.has_bearing_compensation = true; telemetryMessage.debug.bearing_compensation = bearingCompensation * 1000; telemetryMessage.debug.has_speed_over_ground_compensation = true; telemetryMessage.debug.speed_over_ground_compensation = speedOverGroundCompensation * 1000; telemetryMessage.debug.has_motor_1_throttle_compensation = true; telemetryMessage.debug.motor_1_throttle_compensation = leftThrottle * 1000; telemetryMessage.debug.has_motor_2_throttle_compensation = true; telemetryMessage.debug.motor_2_throttle_compensation = rightThrottle * 1000; uint8_t buffer[100]; pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); bool success = pb_encode(&stream, shedBoat_Telemetry_fields, &telemetryMessage); if(success) { send_xbee_packet(buffer, stream.bytes_written); } else { error("Failed to encode Proto Buffer"); DEBUG_OUTPUT("Failed to encode Proto Buffer /r/n"); } #endif }
TEST(test_encoder, test_encode_object_none) { uint8_t data[1]; pb_ostream_t stream = pb_ostream_from_buffer(data, sizeof(data)); krpc_TestService_TestClass_t value = 0; ASSERT_EQ(KRPC_OK, krpc_encode_object(&stream, value)); ASSERT_EQ("00", hexlify(data, stream.bytes_written)); }
unsigned int openiot_alert(char *hardwareId, char *alertType, char *alertMessage, int64_t eventDate, uint8_t *buffer, size_t length, char *originator) { pb_ostream_t stream = pb_ostream_from_buffer(buffer, length); OpenIoT_Header header = {}; header.command = OpenIoT_Command_DEVICEALERT; if (originator != NULL) { header.has_originator = true; strcpy(header.originator, originator); } if (!pb_encode_delimited(&stream, OpenIoT_Header_fields, &header)) { return 0; } OpenIoT_DeviceAlert alert = {}; strcpy(alert.hardwareId, hardwareId); strcpy(alert.alertType, alertType); strcpy(alert.alertMessage, alertMessage); if (eventDate != 0) { alert.has_eventDate = true; alert.eventDate = eventDate; } if (!pb_encode_delimited(&stream, OpenIoT_DeviceAlert_fields, &alert)) { return 0; } return stream.bytes_written; }
void sendSensorValue(MotorMsg_Data_Param_Id dataId, MotorMsg_Unit unit, double value) { pb_ostream_t stream = pb_ostream_from_buffer(gTxFrameBuffer, FRAME_BUFFER_MAX_LEN); static MotorMsg_Data_Param param = MotorMsg_Data_Param_init_zero; param.id = dataId; param.unit = unit; param.value = value; static MotorMsg_Data motorData = MotorMsg_Data_init_zero; motorData.action = MotorMsg_Data_Action_RESULT; motorData.params.funcs.encode = &motordataEncoderCallback; motorData.params.arg = ¶m; static MotorMsg motorMsg = MotorMsg_init_zero; memcpy(motorMsg.uuid, UUID, sizeof(motorMsg.uuid)); motorMsg.which_content = MotorMsg_motor_data_tag; motorMsg.content.motor_data = motorData; pb_encode(&stream, MotorMsg_fields, &motorMsg); //pb_encode_tag_for_field(stream, field); //pb_encode_submessage(&stream, MotorMsg_MotorData_fields, &motorData); usbTransmitFrame((BufferStoragePtrType) gTxFrameBuffer, stream.bytes_written); }
/** * encode the gateway message and write the encoded buf to GATEWAY_MSG_PATH * @param ssmsg */ void gwmsg_write(SSMsg *ssmsg) { uint8_t buffer[1024]; int msg_len; FILE *fd; int status; pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); status=pb_encode(&stream, SSMsg_fields, ssmsg); msg_len=stream.bytes_written; if (!status) { log_printf(LOG_ERROR,"GateWay Msg Encoding failed: %s\n", PB_GET_ERROR(&stream)); return ; } fd=fopen(GATEWAY_MSG_PATH,"wb"); if(fd==NULL) { log_printf(LOG_ERROR,"Can not open %s error!\n",GATEWAY_MSG_PATH); } fwrite(buffer,msg_len,1,fd); fclose(fd); }
void MotorController::sendRequest() { /* This is the buffer where we will store the request message. */ uint8_t requestbuffer[256]; /* allocate space for the request message to the server */ RequestMessage request = RequestMessage_init_zero; /* Create a stream that will write to our buffer. */ pb_ostream_t ostream = pb_ostream_from_buffer(requestbuffer, sizeof(requestbuffer)); /* indicate that speed fields will contain values */ request.has_speed_l = true; request.has_speed_r = true; /* fill in the message fields */ request.speed_l = static_cast<float>(current_motor_command_.left_velocity); request.speed_r = static_cast<float>(current_motor_command_.right_velocity); /* encode the protobuffer */ bool status = pb_encode(&ostream, RequestMessage_fields, &request); size_t message_length = ostream.bytes_written; /* check for any errors.. */ if (!status) { ROS_ERROR_STREAM("Encoding failed: " << PB_GET_ERROR(&ostream)); ros::shutdown(); } /* Send the message strapped to a pigeon's leg! */ (*sock_).sendMessage(reinterpret_cast<char*>(requestbuffer), message_length); }
unsigned int openiot_acknowledge(char *hardwareId, char *message, uint8_t *buffer, size_t length, char *originator) { pb_ostream_t stream = pb_ostream_from_buffer(buffer, length); OpenIoT_Header header = {}; header.command = OpenIoT_Command_ACKNOWLEDGE; if (originator != NULL) { header.has_originator = true; strcpy(header.originator, originator); } if (!pb_encode_delimited(&stream, OpenIoT_Header_fields, &header)) { return 0; } OpenIoT_Acknowledge ack = {}; strcpy(ack.hardwareId, hardwareId); if (message != NULL) { ack.has_message = true; strcpy(ack.message, message); } if (!pb_encode_delimited(&stream, OpenIoT_Acknowledge_fields, &ack)) { return 0; } return stream.bytes_written; }
void cdc_rx_notify(uint8_t port) { l("cdc_rx_notify [%d]", port); uint8_t b = udi_cdc_getc(); if (b != 0x08) { l("Protocol desync"); } l("First byte ok"); uint32_t offset = 0; do { buffer[offset++] = b; b = udi_cdc_getc(); l("-> 0x%02x", b); } while (b & 0x80); buffer[offset++] = b; // Now we have enough to know the size l("Length read, decoding..."); l("... 0x%02x 0x%02x", buffer[0], buffer[1]); pb_istream_t istream = pb_istream_from_buffer(buffer + 1, USB_BUFFER_SIZE); l("istream bytes_left before %d", istream.bytes_left); uint64_t len = 0; pb_decode_varint(&istream, &len); l("message_length %d", (uint32_t)len); l("offset %d", offset); udi_cdc_read_buf(buffer + offset, len); l("decode message"); istream = pb_istream_from_buffer(buffer + offset, len); DonglePiRequest request = {0}; request.config.i2c.funcs.decode = handle_i2c_config_cb; request.config.uart.funcs.decode = handle_uart_config_cb; request.config.spi.funcs.decode = handle_spi_config_cb; request.config.gpio.pins.funcs.decode = handle_gpio_pin_config_cb; request.data.i2c.writes.funcs.decode = handle_i2c_write_cb; if (!pb_decode(&istream, DonglePiRequest_fields, &request)) { l("failed to decode the packet, wait for more data"); return; } l("Request #%d received", request.message_nb); if (request.has_data && request.data.has_gpio) { handle_gpio_write(request.data.gpio); } pb_ostream_t ostream = pb_ostream_from_buffer(buffer, USB_BUFFER_SIZE); DonglePiResponse response = {}; response.message_nb = request.message_nb; l("Create response for #%d", response.message_nb); handle_gpio_read(&response); pb_encode_delimited(&ostream, DonglePiResponse_fields, &response); l("Write response nb_bytes = %d", ostream.bytes_written); uint32_t wrote = udi_cdc_write_buf(buffer, ostream.bytes_written); l("Done. wrote %d bytes", wrote); }
TEST(test_encoder, test_encode_unicode_string) { char in[4]; unhexlify((uint8_t*)in, "e284a2"); in[3] = '\0'; uint8_t data[4]; pb_ostream_t stream = pb_ostream_from_buffer(data, sizeof(data)); ASSERT_EQ(KRPC_OK, krpc_encode_string(&stream, in)); ASSERT_EQ("03e284a2", hexlify(data, stream.bytes_written)); }
int pc_pb_encode(uint8_t *buf, size_t len, size_t *written, const json_t *gprotos, const json_t *protos, json_t *msg) { pb_ostream_t stream = pb_ostream_from_buffer(buf, len); if (!pb_encode(&stream, gprotos, protos, msg)) { fprintf(stderr, "pb_encode error\n"); return 0; } *written = stream.bytes_written; return 1; }
int pc_pb_encode(uint8_t *buf, size_t len, size_t *written, const pc_JSON *gprotos, const pc_JSON *protos, const pc_JSON *msg) { /* TODO: const */ pb_ostream_t stream = pb_ostream_from_buffer(buf, len); if (!pb_encode(&stream, gprotos, protos, (pc_JSON*)msg)) { return 0; } *written = stream.bytes_written; return 1; }
TEST(test_encoder, test_encode_procedure_message) { uint8_t data[256]; krpc_call_t call; krpc_argument_t arguments[0]; ASSERT_EQ(KRPC_OK, krpc_call(&call, 99, 42, 0, arguments)); pb_ostream_t stream = pb_ostream_from_buffer(data, sizeof(data)); ASSERT_EQ(KRPC_OK, krpc_encode_message_ProcedureCall(&stream, &call.message)); std::string expected = "0a0012002063282a"; ASSERT_EQ(expected, hexlify(data, stream.bytes_written)); }
void gps_satellite_telemetry() { DEBUG_OUTPUT("Time: %02d:%02d:%02d\r\n", NMEA::getHour(), NMEA::getMinute(), NMEA::getSecond()); DEBUG_OUTPUT("Satellites: %d\r\n", NMEA::getSatellites()); DEBUG_OUTPUT("Latitude: %0.5f\r\n", NMEA::getLatitude()); DEBUG_OUTPUT("Longitude: %0.5f\r\n", NMEA::getLongitude()); DEBUG_OUTPUT("Altitude: %0.2fm\r\n", NMEA::getAltitude()); DEBUG_OUTPUT("Speed: %0.2fkm/h\r\n", NMEA::getSpeed()); DEBUG_OUTPUT("GPS Bearing (Track made good): %0.2f degrees\r\n", NMEA::getBearing()); DEBUG_OUTPUT("Compass Bearing: %03.0f\r\n", bearing); DEBUG_OUTPUT("Heading Delta to Waypoint: %03.0f\r\n", heading_delta(180.0, bearing)); DEBUG_OUTPUT("Distance to Next Nav %06.2fm\r\n", distance_to_current_nav(degToRad((double)NMEA::getLatitude()), degToRad((double)NMEA::getLongitude())) ); #ifdef SEND_TELEMETRY shedBoat_Telemetry telemetryMessage = shedBoat_Telemetry_init_zero; if(autopilotEngaged) { telemetryMessage.status = shedBoat_Telemetry_Status_UNDERWAY_AUTOPILOT; } else { telemetryMessage.status = shedBoat_Telemetry_Status_UNDERWAY_MANUAL; } telemetryMessage.has_location = true; telemetryMessage.location.has_latitude = true; telemetryMessage.location.latitude = NMEA::getLatitude(); telemetryMessage.location.has_longitude = true; telemetryMessage.location.longitude = NMEA::getLongitude(); telemetryMessage.location.has_number_of_satellites_visible = true; telemetryMessage.location.number_of_satellites_visible = NMEA::getSatellites(); telemetryMessage.location.has_true_heading = true; telemetryMessage.location.true_heading = heading * 1000; telemetryMessage.location.has_true_bearing = true; telemetryMessage.location.true_bearing = bearing * 1000; telemetryMessage.location.has_speed_over_ground = true; telemetryMessage.location.speed_over_ground = NMEA::getSpeed() * 1000; telemetryMessage.location.has_utc_seconds = true; telemetryMessage.location.utc_seconds = NMEA::getSecond(); telemetryMessage.location.fix_quality = (_shedBoat_Location_Quality)NMEA::getFixQuality(); telemetryMessage.location.has_distance_to_waypoint = true; telemetryMessage.location.distance_to_waypoint = (int32_t)distance_to_current_nav(degToRad((double)NMEA::getLatitude()), degToRad((double)NMEA::getLongitude())); telemetryMessage.location.has_waypoint_number = true; telemetryMessage.location.waypoint_number = get_nav_num(); uint8_t buffer[100]; pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); bool success = pb_encode(&stream, shedBoat_Telemetry_fields, &telemetryMessage); if(success) { send_xbee_packet(buffer, stream.bytes_written); } else { error("Failed to encode Proto Buffer"); } #endif }
TEST(test_encoder, test_encode_procedure_message_with_args) { uint8_t data[256]; int32_t x = 42; double y = 3.14159; krpc_call_t call; krpc_argument_t arguments[2]; ASSERT_EQ(KRPC_OK, krpc_call(&call, 99, 42, 2, arguments)); ASSERT_EQ(KRPC_OK, krpc_add_argument(&call, 0, &krpc_encode_callback_int32, &x)); ASSERT_EQ(KRPC_OK, krpc_add_argument(&call, 1, &krpc_encode_callback_double, &y)); pb_ostream_t stream = pb_ostream_from_buffer(data, sizeof(data)); ASSERT_EQ(KRPC_OK, krpc_encode_message_ProcedureCall(&stream, &call.message)); std::string expected = "0a0012001a031201541a0c080112086e861bf0f92109402063282a"; ASSERT_EQ(expected, hexlify(data, stream.bytes_written)); }
int main() { int status = 0; uint8_t buf[256]; SignedMsg msg1; UnsignedMsg msg2; pb_ostream_t s; { COMMENT("Test negative value of signed enum"); /* Negative value should take up the maximum size */ msg1.value = SignedEnum_SE_MIN; s = pb_ostream_from_buffer(buf, sizeof(buf)); TEST(pb_encode(&s, SignedMsg_fields, &msg1)); TEST(s.bytes_written == SignedMsg_size); COMMENT("Test positive value of signed enum"); /* Positive value should be smaller */ msg1.value = SignedEnum_SE_MAX; s = pb_ostream_from_buffer(buf, sizeof(buf)); TEST(pb_encode(&s, SignedMsg_fields, &msg1)); TEST(s.bytes_written < SignedMsg_size); } { COMMENT("Test positive value of unsigned enum"); /* This should take up the maximum size */ msg2.value = UnsignedEnum_UE_MAX; s = pb_ostream_from_buffer(buf, sizeof(buf)); TEST(pb_encode(&s, UnsignedMsg_fields, &msg2)); TEST(s.bytes_written == UnsignedMsg_size); } return status; }
ENetPacket *NetEncode(const GameEventType e, const void *data) { uint8_t buffer[1024]; pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof buffer); const pb_field_t *fields = GameEventGetEntry(e).Fields; const bool status = (data && fields) ? pb_encode(&stream, fields, data) : true; CASSERT(status, "Failed to encode pb"); int msgId = (int)e; ENetPacket *packet = enet_packet_create( &msgId, NET_MSG_SIZE + stream.bytes_written, ENET_PACKET_FLAG_RELIABLE); memcpy(packet->data + NET_MSG_SIZE, buffer, stream.bytes_written); return packet; }
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: %s (1|2|3)\n", argv[0]); return 1; } uint8_t buffer[512]; pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); bool status = false; int msgtype = atoi(argv[1]); if (msgtype == 1) { /* Send message of type 1 */ MsgType1 msg = {42}; status = encode_unionmessage(&stream, MsgType1_fields, &msg); } else if (msgtype == 2) { /* Send message of type 2 */ MsgType2 msg = {true}; status = encode_unionmessage(&stream, MsgType2_fields, &msg); } else if (msgtype == 3) { /* Send message of type 3 */ MsgType3 msg = {3, 1415}; status = encode_unionmessage(&stream, MsgType3_fields, &msg); } else { fprintf(stderr, "Unknown message type: %d\n", msgtype); return 2; } if (!status) { fprintf(stderr, "Encoding failed!\n"); return 3; } else { fwrite(buffer, 1, stream.bytes_written, stdout); return 0; /* Success */ } }
// This function assumes the TraceContext is valid. size_t encode_trace_context(google_trace_TraceContext *ctxt, uint8_t *buffer, const size_t buf_size) { // Create a stream that will write to our buffer. pb_ostream_t stream = pb_ostream_from_buffer(buffer, buf_size); // encode message bool status = pb_encode(&stream, google_trace_TraceContext_fields, ctxt); if (!status) { gpr_log(GPR_DEBUG, "TraceContext encoding failed: %s", PB_GET_ERROR(&stream)); return 0; } return stream.bytes_written; }
grpc_slice grpc_grpclb_request_encode(const grpc_grpclb_request *request) { size_t encoded_length; pb_ostream_t sizestream; pb_ostream_t outputstream; grpc_slice slice; memset(&sizestream, 0, sizeof(pb_ostream_t)); pb_encode(&sizestream, grpc_lb_v1_LoadBalanceRequest_fields, request); encoded_length = sizestream.bytes_written; slice = grpc_slice_malloc(encoded_length); outputstream = pb_ostream_from_buffer(GRPC_SLICE_START_PTR(slice), encoded_length); GPR_ASSERT(pb_encode(&outputstream, grpc_lb_v1_LoadBalanceRequest_fields, request) != 0); return slice; }
unsigned int openiot_measurement_multi(char *hardwareId, char **measurementTokens, int64_t eventDate, uint8_t *buffer, size_t length, char *originator) { pb_ostream_t stream = pb_ostream_from_buffer(buffer, length); OpenIoT_Header header = {}; header.command = OpenIoT_Command_DEVICEMEASUREMENT; if (originator != NULL) { header.has_originator = true; strcpy(header.originator, originator); } if (!pb_encode_delimited(&stream, OpenIoT_Header_fields, &header)) { return 0; } OpenIoT_DeviceMeasurements measurements = {}; strcpy(measurements.hardwareId, hardwareId); measurements.measurement_count = 0; if (measurementTokens) { int i; for (i = 0; *(measurementTokens + i); i++) { OpenIoT_Measurement measurement = {}; char *key = strtok(*(measurementTokens + i), ":"); //float value = atof(strtok(NULL, ":")); char *value = strtok(NULL, ":"); //printf("value is %lf\n", value); strcpy(measurement.measurementId, key); //measurement.measurementValue = float_to_double(value); //measurement.measurementValue = value; strcpy(measurement.measurementValue, value); measurements.measurement[i] = measurement; measurements.measurement_count++; free(*(measurementTokens + i)); } free(measurementTokens); } if (eventDate != 0) { measurements.has_eventDate = true; measurements.eventDate = eventDate; } if (!pb_encode_delimited(&stream, OpenIoT_DeviceMeasurements_fields, &measurements)) { return 0; } return stream.bytes_written; }
// encode message struct in msg according to 'fields', and send off via rb // return 0 on success or < 0 on failure; see rtmsg_errors_t // static int npb_send_msg(const void *msg, const pb_field_t *fields, ringbuffer_t *rb, const hal_funct_args_t *fa) { void *buffer; int retval; size_t size; // determine size pb_ostream_t sstream = PB_OSTREAM_SIZING; if (!pb_encode(&sstream, fields, msg)) { rtapi_print_msg(RTAPI_MSG_ERR, "%s: sizing pb_encode(): %s written=%zu\n", fa_funct_name(fa), PB_GET_ERROR(&sstream), sstream.bytes_written); return NBP_SIZE_FAIL; } size = sstream.bytes_written; // preallocate memory in ringbuffer if ((retval = record_write_begin(rb, (void **)&buffer, size)) != 0) { rtapi_print_msg(RTAPI_MSG_ERR, "%s: record_write_begin(%zu) failed: %d\n", fa_funct_name(fa), size, retval); return RB_RESERVE_FAIL; } // zero-copy encode directly into ringbuffer pb_ostream_t rstream = pb_ostream_from_buffer(buffer, size); if (!pb_encode(&rstream, fields, msg)) { rtapi_print_msg(RTAPI_MSG_ERR, "%s: pb_encode failed: %s, size=%zu written=%zu\n", fa_funct_name(fa), PB_GET_ERROR(&rstream), size, rstream.bytes_written); return NBP_ENCODE_FAIL; } // send it off if ((retval = record_write_end(rb, buffer, rstream.bytes_written))) { rtapi_print_msg(RTAPI_MSG_ERR, "%s: record_write_end(%zu) failed: %d\n", fa_funct_name(fa), size, retval); return RB_WRITE_FAIL; } return 0; }
int main() { TestResults results = {}; AllTypes message = {}; pb_istream_t istream = pb_istream_from_buffer((uint8_t*)input_data, sizeof(input_data)); results.success = pb_decode(&istream, AllTypes_fields, &message); results.success &= (message.end == 1099); results.has_stack_usage = true; results.stack_usage = platform_stack_usage(); uint8_t buf[16]; pb_ostream_t ostream = pb_ostream_from_buffer(buf, sizeof(buf)); pb_encode(&ostream, TestResults_fields, &results); platform_write(buf, ostream.bytes_written); return 0; }
void send_printer_status() { uint8_t out[16]; uint8_t type; pb_ostream_t stream = pb_ostream_from_buffer(out, sizeof(out)); bool status; PrinterStatus record; type = PRINTER_STATUS; pb_write(&stream, &type, 1); record.cardInserted = 1; record.keyInserted = get_interlock_state(); record.overrideSwitch = get_interlock_state(); record.laserOn = g_laser_gating; record.laserPowerFeedback = 42; status = pb_encode(&stream, PrinterStatus_fields, &record); if (status) { serialio_write(out, stream.bytes_written); } }
void send_system_telemetry() { #ifdef SEND_TELEMETRY shedBoat_Telemetry telemetryMessage = shedBoat_Telemetry_init_zero; telemetryMessage.status = shedBoat_Telemetry_Status_STATIONARY; telemetryMessage.motor_count = 2; telemetryMessage.motor[0].motor_number = 1; telemetryMessage.motor[0].has_is_alive = true; telemetryMessage.motor[0].is_alive = leftMotor.isAlive(); telemetryMessage.motor[0].has_rpm = true; telemetryMessage.motor[0].rpm = leftMotor.rpm(); telemetryMessage.motor[0].has_temperature = true; telemetryMessage.motor[0].temperature = leftMotor.temperature(); telemetryMessage.motor[0].has_voltage = true; telemetryMessage.motor[0].voltage = leftMotor.voltage(); telemetryMessage.motor[1].motor_number = 2; telemetryMessage.motor[1].has_is_alive = true; telemetryMessage.motor[1].is_alive = rightMotor.isAlive(); telemetryMessage.motor[1].has_rpm = true; telemetryMessage.motor[1].rpm = rightMotor.rpm(); telemetryMessage.motor[1].has_temperature = true; telemetryMessage.motor[1].temperature = rightMotor.temperature(); telemetryMessage.motor[1].has_voltage = true; telemetryMessage.motor[1].voltage = rightMotor.voltage(); telemetryMessage.has_battery = false; uint8_t buffer[100]; pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); bool success = pb_encode(&stream, shedBoat_Telemetry_fields, &telemetryMessage); if(success) { send_xbee_packet(buffer, stream.bytes_written); } else { error("Failed to encode Proto Buffer"); DEBUG_OUTPUT("Failed to encode Proto Buffer /r/n"); } #endif }
unsigned int sw_register(char* hardwareId, char* specificationToken, uint8_t* buffer, size_t length, char* originator) { pb_ostream_t stream = pb_ostream_from_buffer(buffer, length); SiteWhere_Header header = { }; header.command = SiteWhere_Command_REGISTER; if (originator != NULL) { header.has_originator = true; strcpy(header.originator, originator); } if (!pb_encode_delimited(&stream, SiteWhere_Header_fields, &header)) { return 0; } SiteWhere_RegisterDevice registerDevice = { }; strcpy(registerDevice.hardwareId, hardwareId); strcpy(registerDevice.specificationToken, specificationToken); if (!pb_encode_delimited(&stream, SiteWhere_RegisterDevice_fields, ®isterDevice)) { return 0; } return stream.bytes_written; }
static THD_FUNCTION(can_rx, p) { event_listener_t el; CANRxFrame rxmsg; (void)p; chRegSetThreadName("receiver"); chEvtRegister(&CAND1.rxfull_event, &el, 0); while (true) { if (chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(100)) == 0) continue; while (canReceive(&CAND1, CAN_ANY_MAILBOX, &rxmsg, TIME_IMMEDIATE) == MSG_OK) { /* Process message.*/ oca_led_toggle(oca_led_act); CanMessage UsbMessage = CanMessage_init_default; UsbMessage.DLC = rxmsg.DLC; UsbMessage.RTR = rxmsg.DLC; UsbMessage.IDE = rxmsg.IDE; UsbMessage.ID = rxmsg.EID; UsbMessage.Data1 = rxmsg.data32[0]; UsbMessage.Data1 = rxmsg.data32[1]; uint8_t buffer[30]; pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); pb_encode(&stream, CanMessage_fields, &UsbMessage); /*if(serusbcfg.usbp->state == USB_ACTIVE) { if(SDU1.state == SDU_READY) chnWrite(&SDU1, buffer, stream.bytes_written); }*/ } } chEvtUnregister(&CAND1.rxfull_event, &el); }
int main(int argc, char *argv[]) { uint8_t buffer[256]; pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); Dictionary dict = Dictionary_init_zero; if (argc <= 1) { fprintf(stderr, "Usage: %s \"{'foobar': 1234, ...}\"\n", argv[0]); return 1; } dict.dictItem.funcs.encode = encode_dictionary; dict.dictItem.arg = argv[1]; if (!pb_encode(&stream, Dictionary_fields, &dict)) { fprintf(stderr, "Encoding error: %s\n", PB_GET_ERROR(&stream)); return 1; } fwrite(buffer, 1, stream.bytes_written, stdout); return 0; }