uint_32 RTCS_socket ( uint_32 pf, /*[IN] specifies the protocol family */ uint_32 type, /*[IN] specifies the type of communication */ uint_32 protocol /*[IN] select a specific protocol */ ) { /* Body */ SOCKET_STRUCT_PTR socket_ptr; uint_32 error; RTCSLOG_API(RTCSLOG_TYPE_FNENTRY, RTCSLOG_FN_SOCKET, type); socket_ptr = SOCK_Get_sock_struct((RTCS_SOCKET_CALL_STRUCT_PTR)type, RTCS_task_getid()); if (socket_ptr == NULL) { RTCSLOG_API(RTCSLOG_TYPE_FNEXIT, RTCSLOG_FN_SOCKET, RTCS_SOCKET_ERROR); return RTCS_SOCKET_ERROR; } /* Endif */ if (socket_ptr->PROTOCOL->SOCK_SOCKET) { error = socket_ptr->PROTOCOL->SOCK_SOCKET((uint_32)socket_ptr); if (error) { SOCK_Free_sock_struct(socket_ptr); RTCSLOG_API(RTCSLOG_TYPE_FNEXIT, RTCSLOG_FN_SOCKET, RTCS_SOCKET_ERROR); return RTCS_SOCKET_ERROR; } /* Endif */ } /* Endif */ RTCSLOG_API(RTCSLOG_TYPE_FNEXIT, RTCSLOG_FN_SOCKET, (uint_32)socket_ptr); return (uint_32)socket_ptr; } /* Endbody */
uint32_t RTCS_transfersock ( uint32_t in_sock, /*[IN] specifies the handle of the existing socket */ _task_id new_owner ) { /* Body */ SOCKET_STRUCT_PTR socket_ptr = (SOCKET_STRUCT_PTR)in_sock; _rtcs_taskid old_owner_id; _rtcs_taskid new_owner_id; uint32_t out_sock = RTCS_SOCKET_ERROR; old_owner_id = RTCS_task_getid(); if (SOCK_Is_owner(socket_ptr, old_owner_id)) { new_owner_id = _task_get_td(new_owner); if (new_owner_id) { if (old_owner_id==new_owner_id) { out_sock=in_sock; } else if (SOCK_Add_owner(socket_ptr, new_owner_id)) { out_sock=in_sock; SOCK_Remove_owner(socket_ptr, old_owner_id); } else { RTCS_setsockerror(in_sock, RTCSERR_OUT_OF_MEMORY); } /* Endif */ } else { RTCS_setsockerror(in_sock, RTCSERR_INVALID_PARAMETER); } } else { RTCS_setsockerror(in_sock, RTCSERR_SOCK_NOT_OWNER); } /* Endif */ return out_sock; } /* Endbody */
void TFTPSRV_task ( void *dummy, void *creator ) { /* Body */ TFTPSRV_STATE_STRUCT_PTR tftpsrv_ptr; sockaddr_in laddr; uint32_t i, numtrans, timeout; uint32_t error; tftpsrv_ptr = RTCS_mem_alloc_zero(sizeof(TFTPSRV_STATE_STRUCT)); if (tftpsrv_ptr == NULL) { RTCS_task_exit(creator, RTCSERR_OUT_OF_MEMORY); } /* Endif */ TFTPSRV_task_id = RTCS_task_getid(); #ifdef __MQX__ /* Set up exit handler and context so that we can clean up if the TFTP Server is terminated */ _task_set_environment( _task_get_id(), (void *) tftpsrv_ptr ); _task_set_exit_handler( _task_get_id(), TFTPSRV_Exit_handler ); #endif tftpsrv_ptr->SRV_SOCK = socket(PF_INET, SOCK_DGRAM, 0); if (tftpsrv_ptr->SRV_SOCK == RTCS_SOCKET_ERROR) { RTCS_task_exit(creator, RTCSERR_OUT_OF_SOCKETS); } /* Endif */ laddr.sin_family = AF_INET; laddr.sin_port = IPPORT_TFTP; laddr.sin_addr.s_addr = INADDR_ANY; error = bind(tftpsrv_ptr->SRV_SOCK, (const sockaddr *)&laddr, sizeof(laddr)); if (error != RTCS_OK) { RTCS_task_exit(creator, error); } /* Endif */ RTCS_task_resume_creator(creator, RTCS_OK); for (;;) { timeout = TFTPSRV_service_timer(tftpsrv_ptr); numtrans = tftpsrv_ptr->NUM_TRANSACTIONS; error = RTCS_selectset(&tftpsrv_ptr->SRV_SOCK, numtrans+1, timeout); if ((error == RTCS_OK) || (error == RTCS_SOCKET_ERROR)) { continue; } /* Endif */ if (error == tftpsrv_ptr->SRV_SOCK) { /* New request, service it */ TFTPSRV_service_request(tftpsrv_ptr); } else { for (i = 0; i < numtrans; i++) { if (error == tftpsrv_ptr->SOCKETS[i]) { TFTPSRV_service_transaction(tftpsrv_ptr, tftpsrv_ptr->TRANS_PTRS[i]); break; } /* Endif */ } /* Endfor */ } /* Endif */ } /* Endfor */ } /* Endbody */
uint32_t RTCS_detachsock ( uint32_t sock /*[IN] specifies the handle of the existing socket */ ) { /* Body */ register SOCKET_STRUCT_PTR socket_ptr = (SOCKET_STRUCT_PTR)sock; if (!SOCK_Remove_owner(socket_ptr, RTCS_task_getid())) { return RTCSERR_SOCK_NOT_OWNER; } /* Endif */ return RTCS_OK; } /*EndBody*/
uint32_t RTCS_attachsock ( uint32_t sock /*[IN] specifies the handle of the existing socket */ ) { /* Body */ register SOCKET_STRUCT_PTR socket_ptr = (SOCKET_STRUCT_PTR)sock; if (!SOCK_Add_owner(socket_ptr, RTCS_task_getid())) { RTCS_setsockerror(sock, RTCSERR_OUT_OF_MEMORY); return RTCS_SOCKET_ERROR; } /* Endif */ return sock; } /* Endbody */
uint_32 RTCS_cmd_smartissue ( TCPIP_PARM_PTR parm_ptr, void (_CODE_PTR_ command)(pointer) ) { /* Body */ RTCS_DATA_PTR RTCS_data_ptr; RTCS_data_ptr = RTCS_get_data(); if (RTCS_task_getid() == RTCS_data_ptr->TCPIP_TASKID) { return RTCS_cmd_internal(parm_ptr, command); } else { return RTCS_cmd_issue(parm_ptr, command); } /* Endif */ } /* Endbody */
void PPP_rx_task ( pointer handle, /* [IN] - the PPP state structure */ pointer creator /* [IN] - the task create information */ ) { /* Body */ PPP_CFG_PTR ppp_ptr = handle; PCB_PTR pcb; boolean linkstate, linkauth; PPP_OPT opt; uint_16 protocol, len; void (_CODE_PTR_ callback)(PCB_PTR, pointer); pointer param; #if RTCS_PRINT_PPP_PACKETS /****/ uint_32 i; uchar_ptr a; /****/ #endif ppp_ptr->RX_TASKID = RTCS_task_getid(); RTCS_task_resume_creator(creator, PPP_OK); /* Wait for incoming packets */ for (;;) { /* new fix */ //check if app shutdown (STOP_RX == TRUE) if(ppp_ptr->STOP_RX) { break; } /***********/ /********************************************************** ** ** Wait for a frame ** */ /* Read a frame */ /*Lets use flag for send pointer to ppp_ptr->STOP_RX */ pcb = _iopcb_read(ppp_ptr->DEVICE, (uint_32)&(ppp_ptr->STOP_RX)); /* new fix */ if(NULL == pcb) { _time_delay(1); continue; } /***********/ #if RTCS_PRINT_PPP_PACKETS /* TBR */ /* This part print input packet */ a = pcb->FRAG[0].FRAGMENT; i=0; printf("receive 7E FF 03 "); while(i< (pcb->FRAG[0].LENGTH)+2) { if(*a < 0x10) { printf("0"); } printf("%X ",*a); i++; a++; } printf("7E\n"); /*******/ #endif len = pcb->FRAG[0].LENGTH; if (len < 2) { PCB_free(pcb); ppp_ptr->ST_RX_RUNT++; continue; } /* Endif */ protocol = ntohs(pcb->FRAG[0].FRAGMENT); /* Read the negotiated Receive options */ PPP_mutex_lock(&ppp_ptr->MUTEX); linkstate = ppp_ptr->LINK_STATE; linkauth = ppp_ptr->LINK_AUTH; opt = *ppp_ptr->RECV_OPTIONS; PPP_mutex_unlock(&ppp_ptr->MUTEX); /* Discard all non-LCP packets until the link is opened */ if (protocol != PPP_PROT_LCP) { if (!linkstate) { PCB_free(pcb); ppp_ptr->ST_RX_DISCARDED++; continue; } /* Endif */ /* and all non-AP packets until the link is authenticated */ if (protocol != opt.AP) { if (!linkauth) { PCB_free(pcb); ppp_ptr->ST_RX_DISCARDED++; continue; } /* Endif */ } /* Endif */ } /* Endif */ /* Decompress the packet if compression was negotiated */ if ((protocol == PPP_PROT_CP) && opt.CP) { pcb = opt.CP->CP_decomp(&ppp_ptr->CCP_STATE.RECV_DATA, pcb, ppp_ptr, &opt); protocol = ntohs(pcb->FRAG[0].FRAGMENT); } /* Endif */ /********************************************************** ** ** Forward the packet to higher-level protocol ** */ /* Find out where to send the packet */ ppp_ptr->ST_RX_RECEIVED++; /* ** We could put the known protocols in the PROT_CALLS ** list, but we don't because we always want them to ** work, even if someone tries to PPP_unregister() them. */ switch (protocol) { /* Got an LCP packet */ case PPP_PROT_LCP: LCP_input(pcb, &ppp_ptr->LCP_FSM); break; /* Got a CCP packet */ case PPP_PROT_CCP: CCP_input(pcb, &ppp_ptr->CCP_FSM); break; /* Got a PAP packet */ case PPP_PROT_PAP: PAP_input(pcb, ppp_ptr); break; /* Got a CHAP packet */ case PPP_PROT_CHAP: CHAP_input(pcb, ppp_ptr); break; default: callback = NULL; PPP_mutex_lock(&ppp_ptr->MUTEX); if (PPP_findprot(ppp_ptr, protocol)) { callback = ppp_ptr->PROT_CALLS->CALLBACK; param = ppp_ptr->PROT_CALLS->PARAM; } /* Endif */ PPP_mutex_unlock(&ppp_ptr->MUTEX); if (callback) { callback(pcb, param); } else { /* No callback found -- Send Protocol-Reject */ LCP_sendprotrej(pcb, &ppp_ptr->LCP_FSM); } /* Endif */ break; } /* Endswitch */ } /* Endfor */ /* We can start task again. (STOP_RX == FALSE) */ ppp_ptr->STOP_RX = FALSE; } /* Endbody */
void TCPIP_task ( void *dummy, void *creator ) { /* Body */ TCPIP_CFG_STRUCT TCPIP_cfg; RTCS_DATA_PTR RTCS_data_ptr; uint32_t i; TCPIP_MESSAGE_PTR tcpip_msg; uint32_t timeout = 1, timebefore, timeafter, timedelta; uint32_t status; /* Return status */ _queue_id tcpip_qid; RTCSLOG_FNE2(RTCSLOG_FN_TCPIP_task, creator); RTCS_data_ptr = RTCS_get_data(); RTCS_setcfg(TCPIP, &TCPIP_cfg); TCPIP_cfg.status = RTCS_OK; tcpip_qid = RTCS_msgq_open(TCPIP_QUEUE, 0); if (tcpip_qid == 0) { RTCS_task_exit(creator, RTCSERR_OPEN_QUEUE_FAILED); } /* Endif */ RTCS_data_ptr->TCPIP_TASKID = RTCS_task_getid(); /* ** Initialize the Time Service */ TCP_tick = TCPIP_fake_tick; TCPIP_Event_init(); timebefore = RTCS_time_get(); /* ** Allocate a block of PCBs */ status = RTCSPCB_init(); if (status != RTCS_OK) { RTCS_task_exit(creator, status); } /* Endif */ IF_FREE = NULL; /* ** Initialize the protocols */ #if RTCSCFG_ENABLE_IP4 /********************************************* * Initialize IPv4 **********************************************/ status = IP_init(); if (status) { RTCS_task_exit(creator, status); } #if RTCSCFG_ENABLE_ICMP status = ICMP_init(); if (status) { RTCS_task_exit(creator, status); } #endif /* RTCSCFG_ENABLE_ICMP */ ARP_init(); BOOT_init(); #endif /* RTCSCFG_ENABLE_IP4 */ #if RTCSCFG_ENABLE_IP6 /********************************************* * Initialize IPv6 **********************************************/ status = IP6_init(); if (status) { RTCS_task_exit(creator, status); } /* Init ICMP6. */ status = ICMP6_init(); //TBD Add it to RTCS6_protocol_table if (status) { RTCS_task_exit(creator, status); } #endif /* RTCSCFG_ENABLE_IP6*/ #if (RTCSCFG_ENABLE_IP_REASSEMBLY && RTCSCFG_ENABLE_IP4) || (RTCSCFG_ENABLE_IP6_REASSEMBLY && RTCSCFG_ENABLE_IP6) /* Initialize the reassembler */ status = IP_reasm_init(); if (status) { RTCS_task_exit(creator, status); } #endif /* Add loopback interface.*/ status = IPLOCAL_init (); if (status) { RTCS_task_exit(creator, status); }; for (i = 0; RTCS_protocol_table[i]; i++) { status = (*RTCS_protocol_table[i])(); if (status) { RTCS_task_exit(creator, status); } /* Endif */ } /* Endfor */ _RTCS_initialized= TRUE; RTCS_task_resume_creator(creator, RTCS_OK); while (1) { TCPIP_EVENT_PTR queue = TCPIP_Event_head; tcpip_msg = (TCPIP_MESSAGE_PTR)RTCS_msgq_receive(tcpip_qid, timeout, RTCS_data_ptr->TCPIP_msg_pool); if (tcpip_msg) { if (NULL != tcpip_msg->COMMAND) { tcpip_msg->COMMAND(tcpip_msg->DATA); } RTCS_msg_free(tcpip_msg); } timeout = TCP_tick(); timeafter = RTCS_time_get(); /* If head changed set time delta to zero to prevent immidiate event */ if (queue == TCPIP_Event_head) { timedelta = RTCS_timer_get_interval(timebefore, timeafter); } else { timedelta = 0; } timebefore = timeafter; timedelta = TCPIP_Event_time(timedelta); if (timedelta != 0) { if ((timedelta < timeout) || (timeout == 0)) { timeout = timedelta; } } } } /* Endbody */