Files
cls_master/Application/Tasks/UsbDataHandler.h
2024-02-06 01:54:06 +01:00

112 lines
3.1 KiB
C

#ifndef USBDATAHANDLER_H
#define USBDATAHANDLER_H
#include "stdbool.h"
#include "stdint.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @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;
#define MAX_PACKET_SIZE 512 - sizeof(UsbDataPacketHead) // Define your maximum packet size
/**
* @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);
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);
void DataClbk_ResponseDeviceList(void* msg, uint32_t length);
void DataClbk_RequestDeviceList(void* msg, uint32_t length);
#include "firmware.pb.h"
void USBDataResonse(void * msg, const pb_msgdesc_t *fields, UsbPackageType typeid);
#ifdef __cplusplus
}
#endif
#endif /* USBDATAHANDLER_H */