/** Handle a system command */ void handleSystemCommand(byte* payload, unsigned int length) { Device_Header header; pb_istream_t stream = pb_istream_from_buffer(payload, length); // Read header to find what type of command follows. if (pb_decode_delimited(&stream, Device_Header_fields, &header)) { // Handle a registration acknowledgement. if (header.command == Device_Command_REGISTER_ACK) { Device_RegistrationAck ack; if (pb_decode_delimited(&stream, Device_RegistrationAck_fields, &ack)) { if (ack.state == Device_RegistrationAckState_NEW_REGISTRATION) { baseEvents_log("Registered new device."); registered = true; } else if (ack.state == Device_RegistrationAckState_ALREADY_REGISTERED) { baseEvents_log("Device was already registered."); registered = true; } else if (ack.state == Device_RegistrationAckState_REGISTRATION_ERROR) { baseEvents_log("Error registering device."); } } } } else { baseEvents_log("Unable to decode system command."); } }
void ennoSystemMessageHandler(char* topic, char* payload, int length) { Device_Header header; pb_istream_t stream = pb_istream_from_buffer(payload, length); if (pb_decode_delimited(&stream, Device_Header_fields, &header)) { if (header.command == Device_Command_REGISTER_ACK) { Device_RegistrationAck ack; if (pb_decode_delimited(&stream, Device_RegistrationAck_fields, &ack)) { if (ack.state == Device_RegistrationAckState_NEW_REGISTRATION) { printf("Registered new device.\n"); registered = true; } else if (ack.state == Device_RegistrationAckState_ALREADY_REGISTERED) { printf("Device has already registered.\n"); } else if (ack.state == Device_RegistrationAckState_REGISTRATION_ERROR) { printf("Error rigistering device.\n"); } } } } else { printf("Unable to decode system command.\n"); } }
void ennoCommandMessageHandler(char* topic, char* payload, int length){ ArduinoCustom__Header header; pb_istream_t stream = pb_istream_from_buffer(payload, length); if (pb_decode_delimited(&stream, ArduinoCustom__Header_fields, &header)) { printf("Decoded header for custom command.\n"); if (header.command == ArduinoCustom_Command_PING) { ArduinoCustom_ping ping; if (pb_decode_delimited(&stream, ArduinoCustom_ping_fields, &ping)) { handlePing(ping, header.originator); } } else if (header.command == ArduinoCustom_Command_TESTEVENTS) { ArduinoCustom_testEvents testEvents; if (pb_decode_delimited(&stream, ArduinoCustom_testEvents_fields, &testEvents)) { handleTestEvents(testEvents, header.originator); } } else if (header.command == ArduinoCustom_Command_SERIALPRINTLN) { ArduinoCustom_serialPrintln serialPrintln; if (pb_decode_delimited(&stream, ArduinoCustom_serialPrintln_fields, &serialPrintln)) { handleSerialPrintln(serialPrintln, header.originator); } } } }
/** Handle a command specific to the specification for this device */ void handleSpecificationCommand(byte* payload, unsigned int length) { ArduinoCustom__Header header; memset(buffer,0,300); ArduinoCustom_testData testEvents; pb_istream_t stream = pb_istream_from_buffer(payload, length); if (pb_decode_delimited(&stream, ArduinoCustom__Header_fields, &header)) { baseEvents_log("Decoded header for custom command."); if (header.command == ArduinoCustom_Command_RGB_LED) { if (pb_decode_delimited(&stream, ArduinoCustom_RGB_fields, &RGB_LED)) { baseEvents_log("Command: RGB_LED set(h=%d, s=%d, b=%d)", RGB_LED.rgbled_h, RGB_LED.rgbled_s, RGB_LED.rgbled_b); hsb2rgb_led_open(RGB_LED.rgbled_h, RGB_LED.rgbled_s, RGB_LED.rgbled_b); } } else if (header.command == ArduinoCustom_Command_DC_MOTOR) { ArduinoCustom_DC_MOTOR dc_motor; if (pb_decode_delimited(&stream, ArduinoCustom__Header_fields, &dc_motor)) { baseEvents_log("Command: DC_MOTOR set: %d", dc_motor.motor_sw); dc_motor_set(dc_motor.motor_sw); } } else if (header.command == ArduinoCustom_Command_PING) { ArduinoCustom_ping ping; if (pb_decode_delimited(&stream, ArduinoCustom_ping_fields, &ping)) { handlePing(ping, header.originator); } } else if (header.command == ArduinoCustom_Command_TESTEVENTS) { if (pb_decode_delimited(&stream, ArduinoCustom_testEvents_fields, &testEvents)) { handleTestEvents(testEvents, header.originator); } } else if (header.command == ArduinoCustom_Command_SERIALPRINTLN) { ArduinoCustom_serialPrintln serialPrintln; if (pb_decode_delimited(&stream, ArduinoCustom_serialPrintln_fields, &serialPrintln)) { handleSerialPrintln(serialPrintln,header.originator); } } else { baseEvents_log("Unknown command."); } } }