83 lines
2.7 KiB
C

#ifndef CLASS_EEM
#define CLASS_EEM
#include <stdint.h>
#include "../usb_callback_event.h"
#include <blocking_io/blocking_fifo.h>
#include <etherlib/etherlib.h>
#define USB_EEM_FIFO_SIZE (8192)
typedef enum {
EEM_ECHO = 0, // Echo
EEM_ECHO_RESPONSE, // Echo response
EEM_SUSPEND_HINT, // Suspend hint
EEM_RESPONSE_HINT, // Response hint
EEM_RESPONSE_COMPLETE_HINT, // Response complete hint
EEM_TICKLE, // Tickle
} USB_EemCmd;
typedef enum {
EEM_IDLE = 0, // IDLE state, a new transfer may be initiated
EEM_TRANSFER_IN_PROGRESS // transfer is in progress, new transfers have to wait
} USB_EemFsmState;
typedef enum {
EEM_EVT_NONE = 0, // no event
EEM_EVT_NETBOUND_PCKT_RECEIVED, // a USB packet has been received for a netbound Ethernet frame
// EEM_EVT_HOSTBOUND_FRAME_IN_QUEUE, // a new hostbound frame is in the queue
EEM_EVT_HOSTBOUND_TRANSFER_DONE, // the previous hostbound transfer has completed
} USB_EemEvent;
/**
* Ethernet frame prepended with EEM header
*/
typedef struct {
uint16_t header; ///< EEM header (CRC present?, Frame Length)
uint8_t payload[]; ///< Actual payload
} USB_EemFrame;
typedef struct {
EthInterface * intf; // interface for packet interception and injection
uint8_t data_ep; // bulk data in and out endpoint pair
osMessageQueueId_t eventQueue; // event queue
// BFifo hostbFifo; // Hostbound FIFO
BFifo netbFifo; // Network bound FIFO
USB_EemFsmState netbState; // state of host bound operations
uint16_t netbFrameSize; // size of network bound packet
uint16_t netbSizeLeft; // number of bytes expected from the host sending into current network bound packet
bool netbAutoArm; // signal, that the netbound endpoint should be autoarmed or not
USB_EemFsmState hostbState; // state of network bound operations
osMessageQueueId_t hostbFrameQ; // queue for hostbound message pointers
USB_EemFrame * hostbFrame; // hostbound frame
uint16_t hostbFrameLength; // hostbound frame length
uint16_t hostbFrameIndex; // read index in hostbound frame
bool echoRespQueued; // indicates if an EchoResponse was queued
bool initialized; // EEM is initialized
osThreadId_t th; // EEM thread
} USB_EemState;
/**
* Initialize USB EEM module.
* @param data_ep data endpoint number
*/
void usb_eem_init(uint8_t data_ep);
/**
* Callback function for data reception and transmission.
* @param cbevt pointer to callback event structure
* @return function returns how to proceed with packet processing
*/
int usb_eem_process_and_return(USB_CallbackEvent * cbevt);
/**
* Set Ethernet interface.
* @param pointer to Ethernet interface
*/
void usb_eem_set_intf(EthInterface * intf);
#endif /* CLASS_EEM */