Implement command parser up to second parameter
The command parser has been implemented for all options other than the config option. See #1
This commit is contained in:
parent
06d5c3c464
commit
45bc800397
@ -1,5 +1,131 @@
|
|||||||
#include "command-handler.h"
|
#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);
|
||||||
|
|||||||
@ -12,9 +12,12 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
// Enum of all valid command types
|
// Enum of all valid command types
|
||||||
typedef enum {
|
typedef enum {
|
||||||
WRITE_CHANGE, // Change the configuration
|
WRITE_CONFIG, // Change the configuration
|
||||||
READ_CONFIG, // Read, and print the current configuration
|
READ_CONFIG, // Read, and print the current configuration
|
||||||
READ_VOLTAGE, // Read, and print a voltage
|
READ_VOLTAGE, // Read, and print a voltage
|
||||||
READ_TERMPERATURE, // Read, and print the temperature
|
READ_TERMPERATURE, // Read, and print the temperature
|
||||||
@ -26,9 +29,10 @@ typedef enum {
|
|||||||
|
|
||||||
// Enum of all valid voltage sources
|
// Enum of all valid voltage sources
|
||||||
typedef enum {
|
typedef enum {
|
||||||
SRC_INTERNAL, // Internal volage
|
SRC_INTERNAL, // Internal volage
|
||||||
SRC_EXTRNAL, // External voltage
|
SRC_EXTRNAL, // External voltage
|
||||||
SRC_NONE // No voltage source selected
|
SRC_THERMISTOR, // Thermistor voltage
|
||||||
|
SRC_NONE // No voltage source selected
|
||||||
} src_voltage_t;
|
} src_voltage_t;
|
||||||
|
|
||||||
// Enum of all valid config options
|
// Enum of all valid config options
|
||||||
@ -48,12 +52,14 @@ typedef enum {
|
|||||||
// Struct with command context
|
// Struct with command context
|
||||||
typedef struct {
|
typedef struct {
|
||||||
command_t command; // The command to execute
|
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
|
config_option_t conf; // The configuration option to cange
|
||||||
|
// TODO: Add config value field for writing
|
||||||
} command_context_t;
|
} command_context_t;
|
||||||
|
|
||||||
// Parses the input string and outputs one of the valid commands
|
// 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
|
// Routes the provided command to the appropriate function to handle it
|
||||||
void route_command(command_context_t command);
|
void route_command(command_context_t command);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user