// // Created by epagris on 2022.10.20.. // #include "eth_interface.h" #include "dynmem.h" #include "utils.h" #include "etherlib_options.h" #include "etherlib/prefab/conn_blocks/ethernet_info.h" static int ethintf_llrecv(EthIODef * io, const RawPckt * pckt) { ethinf_receive((EthInterface *) io->tag, pckt); return 0; } static void ethintf_register(EthInterface * intf) { intf->ioDef->tag = intf; intf->ioDef->llRxDone = ethintf_llrecv; } // interface processing thread static void * ethinf_proc_thread(void * param); EthInterface *ethintf_new(EthIODef * io) { EthInterface * ethIntf = (EthInterface *)dynmem_alloc(sizeof(EthInterface)); ASSERT_NULL(ethIntf); memset(ðIntf->sieve.layer0, 0, sizeof(PcktSieveLayer)); 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); ETHLIB_OS_SEM_CREATE(ðIntf->rxSem); ETHLIB_OS_THREAD_DEFINE(ethinf_proc_thread, ipp, 10, 4096, ethIntf); ETHLIB_OS_THREAD_CREATE(ipp, ethIntf); ethIntf->ipra = ipra_new(); return ethIntf; } static void * ethinf_proc_thread(void * param) { EthInterface * intf = (EthInterface *) param; while (true) { ETHLIB_OS_SEM_WAIT(&intf->rxSem); if (mq_avail(intf->rxQ) > 0) { RawPckt rawPckt = mq_top(intf->rxQ); mq_pop(intf->rxQ); packsieve_input(&intf->sieve, &rawPckt); } } return NULL; } void ethinf_receive(EthInterface *intf, const RawPckt *rawPckt) { bool pushOK = mq_push(intf->rxQ, rawPckt); if (pushOK) { ETHLIB_OS_SEM_POST(&intf->rxSem); } else { ERROR("Input queue full, packet dropped!\n"); } } void ethinf_transmit(EthInterface *intf, const RawPckt *rawPckt) { mq_push(intf->txQ, rawPckt); // push packet onto the message queue intf->ioDef->llTxTrigger(intf->ioDef, intf->txQ); }