#include #include #define NELEMS(x) (sizeof(x) / sizeof((x)[0])) typedef enum { SINGLE_VALUE = 1, MULTI_VALUE, } argWitdhTypeDef; typedef enum { NONE, DECIMAL, FLOAT, HEX, BINARY, CHAR, STRING, } argFormatTypeDef; typedef void (*callback_t)(void *); typedef const struct { char parameter; char *description; argFormatTypeDef format; argWitdhTypeDef width; char **values; } argTypeDef; typedef const struct { char *action; char *description; argTypeDef *args; } actionTypeDef; typedef const struct { char *command; uint8_t action_size; actionTypeDef *action; char *description; callback_t callback; } cmdTypeDef; static cmdTypeDef led_cmd = { .command = "led", .description = "Operacje na diodach IR", .action_size = 2, .action = (actionTypeDef[]){ { .action = "set", .description = "Pozwala zmienić ustawienia diod IR", .args = (argTypeDef[]){ { .parameter = 'p', .description = "Moc [0-100].", .format = DECIMAL, .width = SINGLE_VALUE, }, { .parameter = 'n', .description = "Wybór diod(y) [Atop, Abot, Btop, Bbot, Ctop, Cbot, all]", .format = STRING, .width = MULTI_VALUE, .values = (char *[]){"Atop", "Abot", "Btop", "Bbot", "Ctop", "Cbot", "all"}, }, { .parameter = 'm', .format = STRING, .width = SINGLE_VALUE, .description = "Tryb działania [all_on, scan]", }, }, }, { .action = "get", .description = "Pozwla sprawdzić ustawienia diod IR", .args = (argTypeDef[]){ {.parameter = 'p', .description = "Moc [0-100].", .width = NONE}, { .parameter = 'n', .description = "Wybór diod(y) [Atop, Abot, Btop, Bbot, Ctop, Cbot]", .format = STRING, .width = MULTI_VALUE, .values = (char *[]){"Atop", "Abot", "Btop", "Bbot", "Ctop", "Cbot", "all"}, }, {.parameter = 'm', .description = "Tryb działania [all_on, scan]", .width = NONE}, }, }, }, }; static cmdTypeDef *commands[] = { &led_cmd, }; typedef struct { char command[8]; char action[8]; char arguments[128]; } MsgTemplete_TypeDef; void at_parse_msg(MsgTemplete_TypeDef *msg); void at_parse_action(cmdTypeDef *cmd, MsgTemplete_TypeDef *msg); void at_parse_arguments(actionTypeDef *action, MsgTemplete_TypeDef *msg); void at_wait_msg_complete(char *text) { MsgTemplete_TypeDef msg; uint8_t total_read = sscanf(text, "%7s %7s %127s", msg.command, msg.action, msg.arguments); if (total_read == 1) { // help } if (total_read >= 2) { at_parse_msg(&msg); } } enum { LED, CMD_MAX, }; void at_parse_msg(MsgTemplete_TypeDef *msg) { cmdTypeDef *cmd = NULL; for (uint8_t i = 0; i < NELEMS(commands); i++) { if (strcmp(msg->command, commands[i]->command) == 0) { cmd = commands[i]; break; } } if (cmd == NULL) { // error } at_parse_action(cmd, msg); } void at_parse_action(cmdTypeDef *cmd, MsgTemplete_TypeDef *msg) { actionTypeDef *action = NULL; for (uint8_t i = 0; i < cmd->action_size; i++) { if (strcmp(msg->action, cmd->action[i].action) == 0) { action = &cmd->action[i]; break; } } if (action == NULL) { // error } at_parse_arguments(action, msg); } void at_parse_arguments(actionTypeDef *action, MsgTemplete_TypeDef *msg) { }