38 changed files with 506 additions and 104 deletions
@ -0,0 +1,64 @@ |
|||
/*
|
|||
* command_interpreter.c |
|||
* |
|||
* Created on: Jul 10, 2021 |
|||
* Author: Andreas Berthoud |
|||
*/ |
|||
|
|||
#include <stdio.h> |
|||
#include "usbd_cdc_if.h" |
|||
#include "commands.h" |
|||
#include "ble_cmd_mbx.h" |
|||
|
|||
enum command_target_t { |
|||
client, |
|||
server, |
|||
}; |
|||
|
|||
// #define DEBUG_COMMADS
|
|||
|
|||
void usb_receive(uint8_t *buf, uint32_t *len) { |
|||
|
|||
if (*len < 5) { |
|||
log_error("usb_receive", "received command which cannot be interpreted", 0); |
|||
return; |
|||
} |
|||
uint16_t length = buf[1] << 8 | buf[2]; |
|||
uint16_t command_total_length = length + 5; |
|||
uint8_t command_target = buf[3]; |
|||
uint8_t stop_byte = 0x1; |
|||
|
|||
if (*len >= command_total_length - 1) { |
|||
stop_byte = buf[command_total_length - 1]; |
|||
} |
|||
|
|||
if (stop_byte != 0xFF) { |
|||
log_error("usb_receive", "received command has invalid stop byte: 0x%x", 1, stop_byte); |
|||
return; |
|||
} |
|||
|
|||
if (command_target == server) { |
|||
#ifdef DEBUG_COMMADS |
|||
uint8_t buffer[512]; |
|||
buffer[0] = (uint8_t)('0'); |
|||
buffer[1] = (uint8_t)('x'); |
|||
uint8_t i; |
|||
for (i = 0; i<command_total_length; i++) { |
|||
snprintf((char*)(buffer + i + 2), 512-i-2, "%x", buf[i]); |
|||
} |
|||
buffer[i + 2] = (uint8_t)('\0'); |
|||
log_debug("usb_receive", "raw command: %s", 1, buffer); |
|||
#endif /* DEBUG_COMMADS */ |
|||
push_ble_command(buf, command_total_length); |
|||
return; |
|||
} |
|||
|
|||
uint8_t command_id = buf[0]; |
|||
uint8_t * payload_ptr = buf + 4; |
|||
if (command_target == client) { |
|||
handle_received_usb_command(command_id, payload_ptr, length); |
|||
} else { |
|||
log_error("usb_receive", "unknown command target: 0x%x", 1, command_target); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
/*
|
|||
* ble_cmd_mbx.cpp |
|||
* |
|||
* Created on: Jul 23, 2021 |
|||
* Author: Andreas Berthoud |
|||
*/ |
|||
|
|||
#include <queue> |
|||
#include "string.h" |
|||
#include "ble_cmd_mbx.h" |
|||
#include "stm32_seq.h" |
|||
#include "app_conf.h" |
|||
|
|||
|
|||
std::queue<raw_command_t> raw_ble_command_queue; |
|||
|
|||
void push_ble_command(uint8_t * payload_ptr, uint16_t length) { |
|||
raw_command_t command = { |
|||
.size = length, |
|||
}; |
|||
|
|||
memcpy(command.payload, payload_ptr, length); |
|||
raw_ble_command_queue.push(command); |
|||
UTIL_SEQ_SetTask(1<<CFG_TASK_SEND_COMMAND_ID, CFG_SCH_PRIO_0); |
|||
return; |
|||
} |
|||
|
|||
uint8_t get_number_of_ble_commands_in_mailbox() { |
|||
return raw_ble_command_queue.size(); |
|||
} |
|||
|
|||
raw_command_t pop_ble_command() { |
|||
raw_command_t command = raw_ble_command_queue.front(); |
|||
raw_ble_command_queue.pop(); |
|||
return command; |
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
/*
|
|||
* ble_cmd_mbx.h |
|||
* |
|||
* Created on: Jul 23, 2021 |
|||
* Author: Andreas Berthoud |
|||
*/ |
|||
|
|||
#ifndef BLE_CMD_MBX_H_ |
|||
#define BLE_CMD_MBX_H_ |
|||
|
|||
#include "cmd_mbx.h" |
|||
|
|||
|
|||
#ifdef __cplusplus |
|||
extern "C" { |
|||
#endif |
|||
|
|||
void push_ble_command(uint8_t * payload_ptr, uint16_t length); |
|||
|
|||
uint8_t get_number_of_ble_commands_in_mailbox(); |
|||
|
|||
raw_command_t pop_ble_command(); |
|||
|
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
|||
|
|||
#endif /* BLE_CMD_MBX_H_ */ |
|||
@ -0,0 +1,28 @@ |
|||
/*
|
|||
* cmd_mbx.h |
|||
* |
|||
* Created on: Jul 27, 2021 |
|||
* Author: Andreas Berthoud |
|||
*/ |
|||
|
|||
#ifndef CMD_MBX_H_ |
|||
#define CMD_MBX_H_ |
|||
|
|||
#include <stdio.h> |
|||
|
|||
|
|||
#ifdef __cplusplus |
|||
extern "C" { |
|||
#endif |
|||
|
|||
typedef struct { |
|||
uint8_t payload[247]; |
|||
uint16_t size; |
|||
} raw_command_t; |
|||
|
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
|||
|
|||
|
|||
#endif /* CMD_MBX_H_ */ |
|||
@ -0,0 +1,33 @@ |
|||
/*
|
|||
* usb_cmd_mbx.cpp |
|||
* |
|||
* Created on: Jul 23, 2021 |
|||
* Author: Andreas Berthoud |
|||
*/ |
|||
|
|||
#include <queue> |
|||
#include "string.h" |
|||
#include "usb_cmd_mbx.h" |
|||
|
|||
|
|||
std::queue<raw_command_t> raw_usb_command_queue; |
|||
|
|||
void push_usb_command(uint8_t * payload_ptr, uint16_t length) { |
|||
raw_command_t command = { |
|||
.size = length, |
|||
}; |
|||
|
|||
memcpy(command.payload, payload_ptr, length); |
|||
raw_usb_command_queue.push(command); |
|||
return; |
|||
} |
|||
|
|||
uint8_t get_number_of_usb_commands_in_mailbox() { |
|||
return raw_usb_command_queue.size(); |
|||
} |
|||
|
|||
raw_command_t pop_usb_command() { |
|||
raw_command_t command = raw_usb_command_queue.front(); |
|||
raw_usb_command_queue.pop(); |
|||
return command; |
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
/*
|
|||
* ble_cmd_mbx.h |
|||
* |
|||
* Created on: Jul 23, 2021 |
|||
* Author: Andreas Berthoud |
|||
*/ |
|||
|
|||
#ifndef BLE_CMD_MBX_H_ |
|||
#define BLE_CMD_MBX_H_ |
|||
|
|||
#include "cmd_mbx.h" |
|||
|
|||
|
|||
#ifdef __cplusplus |
|||
extern "C" { |
|||
#endif |
|||
|
|||
void push_usb_command(uint8_t * payload_ptr, uint16_t length); |
|||
|
|||
uint8_t get_number_of_usb_commands_in_mailbox(); |
|||
|
|||
raw_command_t pop_usb_command(); |
|||
|
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
|||
|
|||
#endif /* BLE_CMD_MBX_H_ */ |
|||
Loading…
Reference in new issue