#include "command-handler.h" // Initialize empty, global command context volatile command_context_t context = {UNKNOWN_COMMAND, SRC_NONE, FAN_NONE, CNF_NONE}; void parse_command(uint8_t command[], uint8_t command_len) { /////////////////////// // Command selection // /////////////////////// // Validate that we have a command if (command_len < 1) { context.command = UNKNOWN_COMMAND; } uint8_t foo = command[0]; // Figure out which command to run switch (foo) { case 0x11: // Read config context.command = READ_CONFIG; break; case 0x12: // Read voltage context.command = READ_VOLTAGE; break; case 0x13: // Read temperature context.command = READ_TERMPERATURE; break; case 0x14: // Read current fan speed context.command = READ_FAN_SPEED; break; case 0x15: // Read bulk fan speed context.command = READ_BULK_FAN_SPEED; break; case 0x21: // Write config context.command = WRITE_CONFIG; break; case 0x22: // Clear stored fan speed data context.command = CLEAR_BULK_FAN_SPEED; break; default: // Unrecognized command context.command = UNKNOWN_COMMAND; break; } /////////////////////////////// // First parameter selection // /////////////////////////////// // Check if the command does not require a parameter. If it does not, return. if (context.command == READ_TERMPERATURE) { return; } // Validate that we have a first parameter, else requirements for command are // not fulfilled. return unknown command. if (command_len < 2) { context.command = UNKNOWN_COMMAND; return; } // Store the parameter uint8_t param = command[1]; // Extract the parameter. Parameter is dependent on command switch (command[0]) { // Configuration parameters case 0x11: // Read config case 0x21: // Write config // TODO: Handle parameters for config break; // Voltage parameters case 0x12: // Read voltage if (param == 0x01) { context.src_voltage = SRC_INTERNAL; } else if (param == 0x02) { context.src_voltage = SRC_EXTRNAL; } else if (param == 0x03) { context.src_voltage = SRC_THERMISTOR; } else { context.src_voltage = SRC_NONE; } break; // Fan parameters case 0x14: // Read current fan speed case 0x15: // Read bulk fan speed case 0x22: // Clear stored fan speed data context.command = READ_BULK_FAN_SPEED; if (param == 0x01) { context.fan = FAN1; } else if (param == 0x02) { context.fan = FAN2; } else { context.fan = FAN_NONE; } break; // This should never be reached default: // Unrecognized command context.command = UNKNOWN_COMMAND; break; } //////////////////////////////// // Second parameter selection // //////////////////////////////// // Check if the command does not require a second parameter. If it does not, // return. Only config write requires a second parameter. if (context.command != WRITE_CONFIG) { return; } // Validate that we have a first parameter, else requirements for command are // not fulfilled. return unknown command. if (command_len < 3) { context.command = UNKNOWN_COMMAND; return; } // Store the parameter param = command[2]; // TODO: Handle the config parameters // exit return; } uint8_t route_command(int pos) { switch (context.command) { WRITE_CONFIG: return WRITE_CONFIG; break; READ_CONFIG: return READ_CONFIG; break; READ_VOLTAGE: return READ_VOLTAGE; break; READ_TERMPERATURE: return READ_TERMPERATURE; break; READ_FAN_SPEED: return READ_FAN_SPEED; break; CLEAR_BULK_FAN_SPEED: return CLEAR_BULK_FAN_SPEED; break; UNKNOWN_COMMAND: return 0xFF; break; } }