diff --git a/prosjekt.X/command-handler.c b/prosjekt.X/command-handler.c index 91e537c..75d91d1 100644 --- a/prosjekt.X/command-handler.c +++ b/prosjekt.X/command-handler.c @@ -1,5 +1,131 @@ #include "command-handler.h" -command_t parse_command(char *command_str); +command_context_t parse_command(uint8_t *command, uint8_t command_len) { + // Create the context + command_context_t context; -void route_command(command_t command); + /////////////////////// + // Command selection // + /////////////////////// + + // Validate that we have a command + if (command_len < 1) { + context.command = UNKNOWN_COMMAND; + return context; + } + + // Figure out which command to run + switch (command[0]) { + 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 context; + } + + // 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 context; + } + + // 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 context; + } + + // 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 context; + } + + // Store the parameter + param = command[2]; + + // TODO: Handle the config parameters + + // Return the context + return context; +} + +void route_command(command_context_t command); diff --git a/prosjekt.X/command-handler.h b/prosjekt.X/command-handler.h index 7d8c304..d59305a 100644 --- a/prosjekt.X/command-handler.h +++ b/prosjekt.X/command-handler.h @@ -12,9 +12,12 @@ extern "C" { #endif +#include +#include + // Enum of all valid command types typedef enum { - WRITE_CHANGE, // Change the configuration + WRITE_CONFIG, // Change the configuration READ_CONFIG, // Read, and print the current configuration READ_VOLTAGE, // Read, and print a voltage READ_TERMPERATURE, // Read, and print the temperature @@ -26,9 +29,10 @@ typedef enum { // Enum of all valid voltage sources typedef enum { - SRC_INTERNAL, // Internal volage - SRC_EXTRNAL, // External voltage - SRC_NONE // No voltage source selected + SRC_INTERNAL, // Internal volage + SRC_EXTRNAL, // External voltage + SRC_THERMISTOR, // Thermistor voltage + SRC_NONE // No voltage source selected } src_voltage_t; // Enum of all valid config options @@ -48,12 +52,14 @@ typedef enum { // Struct with command context typedef struct { command_t command; // The command to execute - src_voltage_t src_voltage; // The selected voltage source + src_voltage_t src_voltage; // The selected voltage source + fans_t fan; // The selected fan config_option_t conf; // The configuration option to cange + // TODO: Add config value field for writing } command_context_t; // Parses the input string and outputs one of the valid commands -command_context_t parse_command(char *command_str); +command_context_t parse_command(uint8_t *command, uint8_t command_len); // Routes the provided command to the appropriate function to handle it void route_command(command_context_t command);