rework of the UsbDataHandler

This commit is contained in:
2024-02-02 19:53:32 +01:00
parent 83830808a9
commit 766a3f021c
2 changed files with 251 additions and 99 deletions

View File

@@ -1,22 +1,144 @@
#ifndef USBDATAHANDLER_H
#define USBDATAHANDLER_H
#include "usbd_cdc_if.h"
#include "firmware.pb.h"
#include "stdbool.h"
#include "stdint.h"
#ifdef __cplusplus
extern "C" {
#endif
#define MAX_PACKET_SIZE 512 - 4 // Define your maximum packet size
void UsbDataHandler_Start();
/**
* @struct UsbDataPacketHead
* @brief This structure represents the header of a USB data packet.
*
* The header includes the length and type of the packet, as well as a check byte.
* The structure is packed to ensure that the memory layout is exactly as defined,
* without any padding bytes.
*/
typedef struct {
uint16_t length; // Length of the USB data packet
uint16_t type; // Type of the USB data packet(used to parse the message content).
uint8_t check; // Check byte of the USB data packet.
} __attribute__((packed)) UsbDataPacketHead;
USBD_StatusTypeDef UsbDataHandler_RxCallback(uint8_t* Buf, uint32_t Len);
void DataClbk_FirmwareStart(void * msg, uint32_t length);
void DataClbk_FirmwarePackage(void * msg, uint32_t length);
void DataClbk_FirmwarePackageAck(void * msg, uint32_t length);
void DataClbk_FirmwareDone(void * msg, uint32_t length);
/**
* @struct UsbDataPacket
* @brief This structure represents a USB data packet.
*
* The packet includes a header and a data array. The structure is packed to ensure
* that the memory layout is exactly as defined, without any padding bytes.
*
* @var UsbDataPacket::head
*
*
* @var UsbDataPacket::data
*
*/
typedef struct {
UsbDataPacketHead head; // Header of the USB data packet.
uint8_t data[MAX_PACKET_SIZE]; // Data of the USB data packet.
} __attribute__((packed)) UsbDataPacket;
/**
* @brief Calculate the chekcsum of the header of a USB data packet.
*
* @param p Pointer to the USB data packet.
* @return Sum of the header of the USB data packet.
*/
uint8_t UsbDataPacket_head_sum(const UsbDataPacket* p);
/**
* @brief Check the header of a USB data packet.
*
* @param p Pointer to the USB data packet.
* @return True if the header is valid, false otherwise.
*/
bool UsbDataPacket_head_check(const UsbDataPacket* p);
/**
* @brief This function starts the USB data handler.
*
* It initializes any necessary hardware or software resources
* and prepares the data handler to process USB data.
*
* @return void
*/
void UsbDataHandler_Start();
/**
* @brief This function runs the USB data handler.
*
* It processes any pending USB data and performs any necessary
* tasks related to USB data handling. It should be called
* periodically in the main loop of your program.
*
* @return void
*/
void UsbDataHandler_Runner();
/**
* @brief This function is a callback that is invoked when USB data is received.
*
* It processes the received data and performs any necessary tasks.
*
* @param Buf A pointer to the buffer that contains the received data.
* @param Len The length of the received data, in bytes.
*
* @return int Returns 0 on success, or a non-zero error code on failure.
*/
int UsbDataHandler_RxCallback(uint8_t* Buf, uint32_t Len);
/**
* @brief Callback function that is invoked when firmware update message is received.
*
* Other parts of the software can implement this function to handle the start of a firmware update.
*
* @param msg A pointer to the message related to the firmware start.
* @param length The length of the message, in bytes.
*
* @return void
*/
__attribute__((weak)) void DataClbk_FirmwareStart(void* msg, uint32_t length);
/**
* @brief Callback function that is invoked when a firmware package message is received.
*
* Other parts of the software can implement this function to handle each received firmware package.
*
* @param msg A pointer to the message related to the received firmware package.
* @param length The length of the message, in bytes.
*
* @return void
*/
__attribute__((weak)) void DataClbk_FirmwarePackage(void* msg, uint32_t length);
/**
* @brief Callback function that is invoked when a firmware package acknowledgment message is received.
*
* Other parts of the software can implement this function to handle the acknowledgment of a received firmware package.
*
* @param msg A pointer to the message related to the firmware package acknowledgment.
* @param length The length of the message, in bytes.
*
* @return void
*/
__attribute__((weak)) void DataClbk_FirmwarePackageAck(void* msg, uint32_t length);
/**
* @brief Callback function that is invoked when firmware update done message.
*
* Other parts of the software can implement this function to handle the completion of a firmware update.
*
* @param msg A pointer to the message related to the completion of the firmware update.
* @param length The length of the message, in bytes.
*
* @return void
*/
__attribute__((weak)) void DataClbk_FirmwareDone(void* msg, uint32_t length);
#ifdef __cplusplus
}