2024-03-06 12:20:10 +00:00
|
|
|
#include "command-handler.h"
|
|
|
|
|
|
2024-03-06 12:51:52 +00:00
|
|
|
command_context_t parse_command(uint8_t *command, uint8_t command_len) {
|
|
|
|
|
// Create the context
|
|
|
|
|
command_context_t context;
|
2024-03-06 12:20:10 +00:00
|
|
|
|
2024-03-06 12:51:52 +00:00
|
|
|
///////////////////////
|
|
|
|
|
// 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);
|