// something has timed out, handle this static void dhcp_timeout(struct dhcp_state *state) { DEBUGF(DHCP_DEBUG, ("dhcp_timeout()\n")); if ((state->state == DHCP_BACKING_OFF) || (state->state == DHCP_SELECTING)) { DEBUGF(DHCP_DEBUG, ("dhcp_timeout(): restarting discovery\n")); dhcp_discover(state); } else if (state->state == DHCP_REQUESTING) { DEBUGF(DHCP_DEBUG, ("dhcp_timeout(): REQUESTING, DHCP request timed out\n")); if (state->tries <= 5) { dhcp_select(state); } else { DEBUGF(DHCP_DEBUG, ("dhcp_timeout(): REQUESTING, releasing, restarting\n")); dhcp_release(state); dhcp_discover(state); } } else if (state->state == DHCP_CHECKING) { DEBUGF(DHCP_DEBUG, ("dhcp_timeout(): CHECKING, ARP request timed out\n")); if (state->tries <= 1) { dhcp_check(state); } // no ARP replies on the offered address, // looks like the IP address is indeed free else { dhcp_bind(state); } } else if (state->state == DHCP_RENEWING) { DEBUGF(DHCP_DEBUG, ("dhcp_timeout(): RENEWING, DHCP request timed out\n")); dhcp_renew(state); } else if (state->state == DHCP_REBINDING) { DEBUGF(DHCP_DEBUG, ("dhcp_timeout(): REBINDING, DHCP request timed out\n")); if (state->tries <= 8) { dhcp_rebind(state); } else { DEBUGF(DHCP_DEBUG, ("dhcp_timeout(): REBINDING, release, restart\n")); dhcp_release(state); dhcp_discover(state); } } }
__task void tcp_task (void) { /* Main Thread of the TcpNet. This task should have */ /* the lowest priority because it is always READY. */ dhcp_tout = DHCP_TOUT; while (1) { main_TcpNet(); dhcp_check (); os_tsk_pass(); } }
int main (void) { /* Main Thread of the TcpNet */ init (); LEDrun = __TRUE; dhcp_tout = DHCP_TOUT; while (1) { timer_poll (); main_TcpNet (); dhcp_check (); blink_led (); } }
/***************************************************************************** ** Function name: U16 tcp_task (void) ** ** Descriptions: envia dados para o servidor TCP *****************************************************************************/ void tcp_task(void) { static U32 tcp_timer = 0u; U8 buf_udp[100],i; /****************** instantâneo ********************************/ #if USE_TCP_SERVER /*Comunicação TCP SERVER será usada?*/ U8 ip[4]; #if USE_PCK_SERV static U32 cnt_tcp_serv = 0u; if(tcp_state.bit.dataToSend) { tcp_state.bit.dataToSend = __FALSE; cnt_tcp_serv = 0; //if(tcp_buf[0] == 0) //sprintf((char*)tcp_buf,"%02X.%02X.%02X.%02X.%02X.%02X\r\n\0",own_hw_adr[0],own_hw_adr[1],own_hw_adr[2],own_hw_adr[3],own_hw_adr[4],own_hw_adr[5]); //tcpSend((char*)tcp_buf, strlen((const char*)tcp_buf), tcp_server_soc, CMD_SEND_TO_SERVER); //memset(tcp_buf,0,sizeof(tcp_buf)); } #endif #endif /****************** a cada 100ms *******************************/ if(!tick) return; ++tcp_timer; dhcp_check(); /****************** a cada 1s **********************************/ if((tcp_timer%TASK_1S)) return; #if USE_TCP_CLIENT if(!(tcp_timer%TASK_5S)) { /*Pacote UDP de identificação*/ sprintf((char*)buf_udp,"%s %u.%u.%u.%u %02X.%02X.%02X.%02X.%02X.%02X %s %s %u.%u.%u.%u %u.%u.%u.%u\r\n\0",lhost_name,MY_IP[0],MY_IP[1],MY_IP[2],MY_IP[3], own_hw_adr[0],own_hw_adr[1],own_hw_adr[2],own_hw_adr[3],own_hw_adr[4],own_hw_adr[5], cfg.tcp.port_serv_rem, cfg.tcp.ip_serv_rem, MY_MASK[0], MY_MASK[1], MY_MASK[2], MY_MASK[3], MY_GATEWAY[0], MY_GATEWAY[1], MY_GATEWAY[2], MY_GATEWAY[3]); if(!ftp_state.bit.UpdateTcp) udpSendBroadcast(buf_udp); //udpSendUnicast(buf_udp,cfg.tcp.ip_app); } if(!(tcp_timer%TASK_30S)) /*A cada 30s*/ { /*Mantém o link ativo*/ //if(tcp_get_state (tcp_client_soc[0]) == TCP_STATE_CONNECT) //{ sprintf(buf_tx,"LOK OK\r\n\0"); for(i=0;i<MAX_NUM_SOC;i++) tcpSend (buf_tx, strlen(buf_tx), tcp_client_soc[i], CMD_SEND_TO_CLIENT); //tcpSend (buf_tx, strlen(buf_tx), tcp_client_soc[0], CMD_SEND_TO_CLIENT); //} } #endif #if USE_TCP_SERVER && USE_PCK_SERV /*Comunicação TCP SERVER será usada?*/ if(!(++cnt_tcp_serv % atoi(cfg.tcp.interval_packet_serv))) tcp_state.bit.dataToSend = __TRUE; #endif #if USE_TCP_SERVER /*Comunicação TCP SERVER será usada?*/ /*Analisa estado de comunicação com o servidor externo*/ if(tcp_get_state (tcp_server_soc) != TCP_STATE_CONNECT) { tcp_state.bit.serverConnected = __FALSE; inet_aton((U8*)cfg.tcp.ip_serv_rem,ip); tcp_connect(tcp_server_soc,ip,atoi(cfg.tcp.port_serv_rem),atoi(cfg.tcp.port_serv_loc)); }else tcp_state.bit.serverConnected = __TRUE; #endif /*Houve alteração das cenas via FTP?*/ if(ftp_state.bit.UpdateScene) { printf ("[Scene config via FTP]\r\r"); fflush(stdout); ftp_state.bit.UpdateScene = __FALSE; init_scene(0xFF); } if(ftp_state.bit.UpdateFileCfg) { printf ("[CFG.CFG configurado via FTP]\r\r"); fflush(stdout); ftp_state.bit.UpdateFileCfg = __FALSE; read_file_cfg(); } /*Houve alteração das configurações TCP via FTP?*/ if(ftp_state.bit.UpdateTcp) { printf ("[TCP Config via FTP..Reiniciando]\r\r"); fflush(stdout); #if USE_TCP_CLIENT for(i=0;i<MAX_NUM_SOC;i++) tcp_close (tcp_client_soc[i]); #endif #if USE_TCP_SERVER tcp_close (tcp_server_soc); #endif LPC_WDT->WDTC = 0x003FFFF; /*0.5s*/ wdt_feed(); while(1); } }
/** * Match incoming DHCP messages against a DHCP client, and trigger its state machine */ static void dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16_t port) { struct dhcp_state *state = (struct dhcp_state *)arg; struct dhcp_msg *reply_msg = (struct dhcp_msg *)p->payload; DEBUGF(DHCP_DEBUG, ("dhcp_recv()\n")); DEBUGF(DHCP_DEBUG, ("pbuf->len = %u\n", p->len)); DEBUGF(DHCP_DEBUG, ("pbuf->tot_len = %u\n", p->tot_len)); state->p = p; if (reply_msg->op == DHCP_BOOTREPLY) { DEBUGF(DHCP_DEBUG, ("state->netif->hwaddr = %02x:%02x:%02x:%02x:%02x:%02x\n", state->netif->hwaddr[0], state->netif->hwaddr[1], state->netif->hwaddr[2], state->netif->hwaddr[3], state->netif->hwaddr[4], state->netif->hwaddr[5])); // TODO: Add multi network interface support, look up the targetted // interface here. if ((state->netif->hwaddr[0] == reply_msg->chaddr[0]) && (state->netif->hwaddr[1] == reply_msg->chaddr[1]) && (state->netif->hwaddr[2] == reply_msg->chaddr[2]) && (state->netif->hwaddr[3] == reply_msg->chaddr[3]) && (state->netif->hwaddr[4] == reply_msg->chaddr[4]) && (state->netif->hwaddr[5] == reply_msg->chaddr[5])) { // check if the transaction ID matches if (get_reply_xid(reply_msg) == state->xid) { // option fields could be unfold? if (dhcp_unfold_reply(state) == ERR_OK) { u8_t *options_ptr = NULL; DEBUGF(DHCP_DEBUG, ("searching DHCP_OPTION_MESSAGE_TYPE\n")); options_ptr = dhcp_get_option_ptr(state, DHCP_OPTION_MESSAGE_TYPE); if (options_ptr != NULL) { u8_t msg_type = dhcp_get_option_byte(options_ptr + 2); if (msg_type == DHCP_ACK) { DEBUGF(DHCP_DEBUG, ("DHCP_ACK received\n")); if (state->state == DHCP_REQUESTING) { dhcp_handle_ack(state); state->request_timeout = 0; #if DHCP_DOES_ARP_CHECK dhcp_check(state); #else dhcp_bind(state); #endif } else if ((state->state == DHCP_REBOOTING) || (state->state == DHCP_REBINDING) ||(state->state == DHCP_RENEWING)) { state->request_timeout = 0; dhcp_bind(state); } } // received a DHCP_NAK in appropriate state? else if ((msg_type == DHCP_NAK) && ((state->state == DHCP_REBOOTING) || (state->state == DHCP_REQUESTING) || (state->state == DHCP_REBINDING) || (state->state == DHCP_RENEWING ))) { DEBUGF(DHCP_DEBUG, ("DHCP_NAK received\n")); state->request_timeout = 0; dhcp_handle_nak(state); } // received a DHCP_OFFER in DHCP_SELECTING state? else if ((msg_type == DHCP_OFFER) && (state->state == DHCP_SELECTING)) { DEBUGF(DHCP_DEBUG, ("DHCP_OFFER received in DHCP_SELECTING state\n")); state->request_timeout = 0; dhcp_handle_offer(state); } } else { DEBUGF(DHCP_DEBUG, ("DHCP_OPTION_MESSAGE_TYPE option not found\n")); } dhcp_free_reply(state); } } else { DEBUGF(DHCP_DEBUG, ("reply_msg->xid=%x does not match with state->xid=%x\n", get_reply_xid(reply_msg), state->xid)); } } else { DEBUGF(DHCP_DEBUG, ("hardware address did not match\n")); DEBUGF(DHCP_DEBUG, ("reply_msg->chaddr = %02x:%02x:%02x:%02x:%02x:%02x\n", reply_msg->chaddr[0], reply_msg->chaddr[1], reply_msg->chaddr[2], reply_msg->chaddr[3], reply_msg->chaddr[4], reply_msg->chaddr[5])); } } else { DEBUGF(DHCP_DEBUG, ("not a DHCP reply message, but type %u\n", reply_msg->op)); } pbuf_free(p); }