- adaptation to CMSIS OS2 done

This commit is contained in:
Wiesner András 2024-04-17 09:32:00 +02:00
parent 0a6d007c73
commit 3b5fac3ad8
9 changed files with 76 additions and 35 deletions

View File

@ -16,14 +16,14 @@ BlockingFifo *eth_bfifo_new(uint32_t size) {
memset(fifo, 0, sizeof(BlockingFifo));
fifo->size = size;
fifo->nonEmpty = ETHLIB_OS_SEM_CREATE(1);
ETHLIB_OS_SEM_WAIT(&fifo->nonEmpty); // FIFO is empty
ETHLIB_OS_SEM_WAIT(fifo->nonEmpty); // FIFO is empty
fifo->notFull = ETHLIB_OS_SEM_CREATE(1);
return fifo;
}
void eth_bfifo_destroy(BlockingFifo * bfifo) {
ETHLIB_OS_SEM_DESTROY(&bfifo->nonEmpty);
ETHLIB_OS_SEM_DESTROY(&bfifo->notFull);
ETHLIB_OS_SEM_DESTROY(bfifo->nonEmpty);
ETHLIB_OS_SEM_DESTROY(bfifo->notFull);
bfifo->size = 0;
dynmem_free(bfifo);
}
@ -45,7 +45,7 @@ uint32_t eth_bfifo_push_try(BlockingFifo * bfifo, const uint8_t * data, uint32_t
}
uint32_t eth_bfifo_push(BlockingFifo * bfifo, const uint8_t * data, uint32_t size) {
ETHLIB_OS_SEM_WAIT(&bfifo->notFull); // take not full semaphore
ETHLIB_OS_SEM_WAIT(bfifo->notFull); // take not full semaphore
// calculate copy size, limit if required
uint32_t freeArea = eth_bfifo_get_free(bfifo);
@ -70,10 +70,10 @@ uint32_t eth_bfifo_push(BlockingFifo * bfifo, const uint8_t * data, uint32_t siz
freeArea = eth_bfifo_get_free(bfifo);
if (freeArea > 0) { // FIFO is not full
ETHLIB_OS_SEM_POST(&bfifo->notFull);
ETHLIB_OS_SEM_POST(bfifo->notFull);
}
if (freeArea != bfifo->size) { // FIFO is not empty
ETHLIB_OS_SEM_POST(&bfifo->nonEmpty);
ETHLIB_OS_SEM_POST(bfifo->nonEmpty);
}
return copySize;
@ -95,7 +95,7 @@ uint32_t eth_bfifo_peek(BlockingFifo * bfifo, uint8_t * dst, uint32_t size) {
}
uint32_t eth_bfifo_pop(BlockingFifo *bfifo, uint8_t *dst, uint32_t size) {
ETHLIB_OS_SEM_WAIT(&bfifo->nonEmpty);
ETHLIB_OS_SEM_WAIT(bfifo->nonEmpty);
// if destination is given, then also perform a copy
if (dst != NULL) {

View File

@ -17,14 +17,14 @@ void dynmem_init() {
}
void *dynmem_alloc_(uint32_t size) {
ETHLIB_OS_MTX_LOCK(&dynmem_mtx);
ETHLIB_OS_MTX_LOCK(dynmem_mtx);
void * p = mp_alloc(E.mp, size);
ETHLIB_OS_MTX_UNLOCK(&dynmem_mtx);
ETHLIB_OS_MTX_UNLOCK(dynmem_mtx);
return p;
}
void dynmem_free_(const void *ptr) {
ETHLIB_OS_MTX_LOCK(&dynmem_mtx);
ETHLIB_OS_MTX_LOCK(dynmem_mtx);
mp_free(E.mp, (const uint8_t *) ptr);
ETHLIB_OS_MTX_UNLOCK(&dynmem_mtx);
ETHLIB_OS_MTX_UNLOCK(dynmem_mtx);
}

View File

@ -44,13 +44,13 @@ EthInterface *ethintf_new(EthIODef *io) {
EthInterface *ethIntf = (EthInterface *)dynmem_alloc(sizeof(EthInterface));
ASSERT_NULL(ethIntf);
memset(&ethIntf->sieve.layer0, 0, sizeof(PcktSieveLayer));
ethIntf->up = false; // the interface is down for now
ethIntf->sieve.intf = ethIntf;
ethIntf->sieve.layer0.connBReportFn = ethernet_print_report;
ethIntf->ioDef = io;
ethIntf->ip = 0;
ethIntf->arpc = arpc_new(ethIntf, ETHLIB_ARPCACHE_SIZE);
ASSERT_NULL(ethIntf->arpc);
ethintf_register(ethIntf);
ethIntf->txQ = mq_create(ETHLIB_DEF_MQ_SIZE);
ethIntf->rxQ = mq_create(ETHLIB_DEF_MQ_SIZE);
@ -58,7 +58,7 @@ EthInterface *ethintf_new(EthIODef *io) {
ethIntf->eventQ = ETHLIB_OS_QUEUE_CREATE(ETHLIB_DEF_MQ_SIZE, uint32_t);
// ETHLIB_OS_SEM_CREATE(&ethIntf->eventSem, ETHLIB_DEF_MQ_SIZE);
ETHLIB_OS_THREAD_CREATE(task_ethintf, "eth", ethIntf, osPriorityHigh, 512);
ETHLIB_OS_THREAD_CREATE(task_ethintf, "eth", ethIntf, osPriorityHigh, 2048);
ethIntf->ipra = ipra_new(ethIntf);
@ -70,6 +70,8 @@ EthInterface *ethintf_new(EthIODef *io) {
ethIntf->linkState = false;
ethintf_register(ethIntf);
return ethIntf;
}
@ -104,7 +106,7 @@ static ThreadReturnType task_ethintf(ThreadParamType param) {
}
}
MSG("Link state: %d\n", ls);
MSG("ETH LINK: %s%s\n", (ls ? (ANSI_COLOR_BGREEN "UP") : (ANSI_COLOR_BRED "DOWN")), ANSI_COLOR_RESET);
} break;
case ETH_IIE_TRANSMIT_NOTIFY: {
intf->ioDef->llTxTrigger(intf->ioDef, intf->txQ);
@ -130,6 +132,10 @@ void ethinf_transmit(EthInterface *intf, const RawPckt *rawPckt) {
}
void ethinf_push_notification(EthInterface *intf, uint16_t event_code, uint16_t event_data) {
if (!intf->up) { // disabled interfaces cannot receive notifications
return;
}
uint32_t cpd = (event_data << 16) | event_code; // create event compound
ETHLIB_OS_QUEUE_PUSH(intf->eventQ, &cpd);
}
@ -170,3 +176,13 @@ void ethinf_set_link_state(EthInterface *intf, bool ls) {
bool ethinf_get_link_state(EthInterface *intf) {
return intf->linkState;
}
void ethinf_up(EthInterface *intf) {
intf->up = true;
// TODO: check PHY link state
}
void ethinf_down(EthInterface *intf) {
intf->up = false;
}

View File

@ -82,6 +82,7 @@ typedef struct EthInterface_ {
IPv4Assembler * ipra; ///< IPv4 reassembler
EthIntfEventCbTable evtCbTable; ///< Event callback table
bool linkState; ///< Interface link state
bool up; ///< Is the interface up?
} EthInterface;
/**
@ -154,4 +155,16 @@ void ethinf_set_link_state(EthInterface * intf, bool ls);
*/
bool ethinf_get_link_state(EthInterface * intf);
/**
* Enable interface.
* @param intf pointer to Ethernet interface
*/
void ethinf_up(EthInterface *intf);
/**
* Disable interface.
* @param intf pointer to Ethernet interface
*/
void ethinf_down(EthInterface *intf);
#endif //ETHERLIB_ETH_INTERFACE_H

View File

@ -78,6 +78,7 @@ void ethlib_init() {
E.pcktReg = packreg_new(); // create new packet registry
register_packet_parsers(); // register packet parsers
E.initialized = true; // from now on, the EtherLib is ready!
//E.ethIntf = ethintf_new(); // create new Ethernet interface
}
@ -100,3 +101,7 @@ EthInterface *get_default_interface() {
EthInterface * get_interface_by_address(ip4_addr addr) {
return get_default_interface();
}
bool is_etherlib_initialized() {
return E.initialized;
}

View File

@ -17,6 +17,7 @@ typedef struct {
PcktRegistry * pcktReg; ///< Packet registry
EthInterface * ethIntf; ///< Array of Ethernet interfaces
CbdTable * cbdt; ///< Connection block descriptor table
bool initialized; ///< Indicates if EtherLib is initialized
} EthState;
extern EthState gEthState;
@ -48,4 +49,10 @@ EthInterface * get_default_interface();
*/
EthInterface * get_interface_by_address(ip4_addr addr);
/**
* Query if EtherLib was initialized.
* @return EtherLib was initialized
*/
bool is_etherlib_initialized();
#endif //ETHERLIB_GLOBAL_STATE_H

View File

@ -63,8 +63,8 @@ static inline TcpState *tcps_create() {
static inline void tcps_release_client_related_objects(TcpState *tcps) {
tcpw_destroy(tcps->txWin);
ETHLIB_OS_SEM_DESTROY(&tcps->txBufNotFull);
ETHLIB_OS_SEM_DESTROY(&tcps->txInProgress);
ETHLIB_OS_SEM_DESTROY(tcps->txBufNotFull);
ETHLIB_OS_SEM_DESTROY(tcps->txInProgress);
eth_bfifo_destroy(tcps->rxFifo);
}
@ -284,7 +284,7 @@ int tcp_receive_segment_cb(const Pckt *pckt, PcktSieveLayerTag tag) {
tcpw_acknowledge(tcps->txWin, tcpProps->AcknowledgementNumber);
// release the transmit semaphore
ETHLIB_OS_SEM_POST(&tcps->txBufNotFull);
ETHLIB_OS_SEM_POST(tcps->txBufNotFull);
// store last TX sequence number transmitted in ACK
tcps->lastTxSeqNumAcked = tcps->txSeqNum;
@ -297,8 +297,8 @@ int tcp_receive_segment_cb(const Pckt *pckt, PcktSieveLayerTag tag) {
case TCP_STATE_LAST_ACK: /* last ACK received */
if (tcpProps->Flags & TCP_FLAG_ACK) {
tcps->connState = TCP_STATE_CLOSED;
ETHLIB_OS_SEM_POST(&tcps->txBufNotFull);
ETHLIB_OS_SEM_WAIT(&tcps->txInProgress);
ETHLIB_OS_SEM_POST(tcps->txBufNotFull);
ETHLIB_OS_SEM_WAIT(tcps->txInProgress);
ret = SIEVE_LAYER_REMOVE_THIS; // if peer closed the connection, remove sieve layer
}
break;
@ -538,11 +538,11 @@ uint32_t tcp_send(cbd d, const uint8_t *data, uint32_t size) {
uint32_t sizeLeft = size;
// indicate that transmission is in progress
ETHLIB_OS_SEM_WAIT(&tcps->txInProgress);
ETHLIB_OS_SEM_WAIT(tcps->txInProgress);
// acquire resources
while ((sizeLeft > 0) && (tcps->connState == TCP_STATE_ESTAB)) {
ETHLIB_OS_SEM_WAIT(&tcps->txBufNotFull);
ETHLIB_OS_SEM_WAIT(tcps->txBufNotFull);
// TODO: lehet, hogy ide kell egy mutex....
// TODO: lezáráskor jelezni kell, hogy még egy blokkoló hívásban vagyunk
@ -567,12 +567,12 @@ uint32_t tcp_send(cbd d, const uint8_t *data, uint32_t size) {
// release semaphore if there's free space remaining TODO: ide szinte biztos, hogy kell mutex
freeSize = tcpw_get_max_window_size(tcps->txWin);
if (freeSize > 0) {
ETHLIB_OS_SEM_POST(&tcps->txBufNotFull);
ETHLIB_OS_SEM_POST(tcps->txBufNotFull);
}
}
}
ETHLIB_OS_SEM_POST(&tcps->txInProgress);
ETHLIB_OS_SEM_POST(tcps->txInProgress);
return size - sizeLeft;
}

View File

@ -251,7 +251,7 @@ static void dhcp_retry_discover(struct Timer_ *timer, AlarmUserData user) {
}
static void dhcp_process(DhcpState *s, DhcpProps *props, DhcpOption *opts) {
ETHLIB_OS_MTX_LOCK(&s->procMtx); // LOCK!
ETHLIB_OS_MTX_LOCK(s->procMtx); // LOCK!
switch (s->state) {
case DHCP_INIT: {
@ -302,10 +302,10 @@ static void dhcp_process(DhcpState *s, DhcpProps *props, DhcpOption *opts) {
AlarmUserData params = {0};
s->renewAlarmId = timer_sched_rel(E.tmr, ((int64_t)dhcpLeaseTime_s) * 1000000, NULL, params);
MSG("DHCP done!\n");
MSG("IP: ");
MSG(ANSI_COLOR_BGREEN "\nDHCP done!\n" ANSI_COLOR_RESET);
MSG("IP: " ANSI_COLOR_BYELLOW);
PRINT_IPv4(s->intf->ip);
MSG("\nRouter: ");
MSG(ANSI_COLOR_RESET "\nRouter: ");
PRINT_IPv4(s->intf->router);
MSG("\nNetmask: ");
PRINT_IPv4(s->intf->netmask);
@ -324,7 +324,7 @@ static void dhcp_process(DhcpState *s, DhcpProps *props, DhcpOption *opts) {
break;
}
ETHLIB_OS_MTX_UNLOCK(&s->procMtx); // RELEASE!
ETHLIB_OS_MTX_UNLOCK(s->procMtx); // RELEASE!
}
static int dhcp_resp_cb(const Pckt *pckt, PcktSieveLayerTag tag) {
@ -366,7 +366,7 @@ void dhcp_stop(DhcpState *s) {
ethinf_trigger_event(s->intf, ETH_EVT_IP_CHANGE);
}
ETHLIB_OS_MTX_UNLOCK(&s->procMtx);
ETHLIB_OS_MTX_UNLOCK(s->procMtx);
}
void dhcp_initiate(EthInterface *intf) {

12
timer.c
View File

@ -93,7 +93,7 @@ uint32_t timer_sched(Timer *tmr, const TimePoint *t, TimerAlarmCb cb, AlarmUserD
return TIMER_SCHED_FAILED;
}
ETHLIB_OS_MTX_LOCK(&(tmr->tabMtx));
ETHLIB_OS_MTX_LOCK((tmr->tabMtx));
// if we can schedule get the first unused block
AlarmAssignment *slot = timer_get_alarm_by_id(tmr, 0);
@ -116,7 +116,7 @@ uint32_t timer_sched(Timer *tmr, const TimePoint *t, TimerAlarmCb cb, AlarmUserD
//timer_report(tmr);
ETHLIB_OS_MTX_UNLOCK(&(tmr->tabMtx));
ETHLIB_OS_MTX_UNLOCK((tmr->tabMtx));
return slot->id;
}
@ -132,7 +132,7 @@ void timer_unsched(Timer *tmr, uint32_t id) {
return;
}
ETHLIB_OS_MTX_LOCK(&(tmr->tabMtx));
ETHLIB_OS_MTX_LOCK((tmr->tabMtx));
if (tmr->nSched > 0) {
AlarmAssignment *alarm = timer_get_alarm_by_id(tmr, id);
@ -143,7 +143,7 @@ void timer_unsched(Timer *tmr, uint32_t id) {
}
}
ETHLIB_OS_MTX_UNLOCK(&(tmr->tabMtx));
ETHLIB_OS_MTX_UNLOCK((tmr->tabMtx));
}
void timer_set_time(Timer *tmr, const TimePoint *t) {
@ -169,7 +169,7 @@ void timer_tick(Timer *tmr, int64_t us) {
if ((tmr->nSched > 0) && (tmr->nextAlarm != NULL)) {
int64_t t_alarm = time_to_us(&(tmr->nextAlarm->time));
while (((t_alarm - t_us) <= 0) && (tmr->nSched > 0)) {
if (ETHLIB_OS_MTX_LOCK(&(tmr->tabMtx)) != 0) {
if (ETHLIB_OS_MTX_LOCK((tmr->tabMtx)) != 0) {
return; // break from the loop, since we cannot lock the mutex
}
@ -187,7 +187,7 @@ void timer_tick(Timer *tmr, int64_t us) {
// update nearest alarm
timer_update_nearest_alarm(tmr);
ETHLIB_OS_MTX_UNLOCK(&(tmr->tabMtx));
ETHLIB_OS_MTX_UNLOCK((tmr->tabMtx));
// invoke callback
if (alarm.cb != NULL) {