static void dhcp_async_cb(uint8 sock, uint8 item, int32 ret) // for async mode { switch(item) { case WATCH_SOCK_UDP_SEND: if(dhcp_alarm) alarm_set(10, dhcp_alarm_cb, 0); dhcp_run_tick = wizpf_get_systick(); if(ret == RET_OK) { DBG("DHCP Discovery Sent Async"); if(di.state == DHCP_STATE_INIT) SET_STATE(DHCP_STATE_SEARCHING); else if(di.state == DHCP_STATE_SELECTING) SET_STATE(DHCP_STATE_REQUESTING); else DBGCRTCA(TRUE, "wrong state(%d)", di.state); } else { DBGA("WATCH_SOCK_UDP_SEND fail - ret(%d)", ret); } break; case WATCH_SOCK_TCP_SEND: case WATCH_SOCK_CONN_TRY: case WATCH_SOCK_CLS_TRY: case WATCH_SOCK_CONN_EVT: case WATCH_SOCK_CLS_EVT: case WATCH_SOCK_RECV: DBGCRTC(TRUE, "DHCP does not use TCP"); default: DBGCRTCA(TRUE, "wrong item(0x%x)", item); } }
void initTable() { startTime = wizpf_get_systick(); snmpData[6].u.intval = 5; snmpData[7].u.intval = 0; snmpData[8].u.intval = 0; }
int32 main(void) { #define TCP_LISTEN_PORT 5000 #define UDP_LISTEN_PORT 5000 int8 ret, root; uint32 tick = 0; ret = platform_init(); if(ret != RET_OK) { goto FAIL_TRAP; } ret = network_init(SOCK_DHCP, NULL, NULL); if(ret != RET_OK) { ERRA("network_init fail - ret(%d)", ret); goto FAIL_TRAP; } printf("\r\n-----------------------------------\r\n"); printf("SMTP Client using W5200\r\n"); printf("-----------------------------------\r\n\r\n"); Delay_tick(2000); menu_init(); root = menu_add("Network setting", 0, NULL); menu_add("Show", root, mn_show_network); menu_add("Static Set", root, mn_set_network); menu_add("Loopback", 0, mn_loopback); menu_add("LED Test", 0, mn_set_led); root = menu_add("App Test", 0, NULL); menu_add("DNS", root, mn_dns); menu_add("BASE64", root, mn_base64); menu_add("eMail", root, mn_email); menu_print_tree(); dhcp_alarm_start(NULL); while(1) { alarm_run(); menu_run(); if(lb_tcp) loopback_tcps(7, (uint16)TCP_LISTEN_PORT); if(lb_udp) loopback_udp(7, (uint16)UDP_LISTEN_PORT); if(wizpf_tick_elapse(tick) > 1000) { // running check wizpf_led_set(WIZ_LED3, VAL_TOG); tick = wizpf_get_systick(); } } FAIL_TRAP: wizpf_led_trap(1); return 0; }
int32 wizpf_tick_elapse(uint32 tick) // + Elapsed time, - Remaining time { uint32 cur = wizpf_get_systick(); return cur - tick; }
static void dhcp_run(void) { static bool udp_open_fail = FALSE; if(di.state == DHCP_STATE_INIT && di.action != DHCP_ACT_START) { DBG("wrong attempt"); return; } else if(GetUDPSocketStatus(di.sock) == SOCKSTAT_CLOSED) { if(udp_open_fail == TRUE && !IS_TIME_PASSED(dhcp_run_tick, DHCP_RETRY_DELAY)) goto RET_ALARM; ClsNetInfo(NI_IP_ADDR); ClsNetInfo(NI_SN_MASK); ClsNetInfo(NI_GW_ADDR); ClsNetInfo(NI_DNS_ADDR); if(UDPOpen(di.sock, DHCP_CLIENT_PORT) == RET_OK) { if(dhcp_async) sockwatch_open(di.sock, dhcp_async_cb); udp_open_fail = FALSE; dhcp_run_tick = wizpf_get_systick(); dhcp_run_cnt = 0; } else { ERR("UDPOpen fail"); udp_open_fail = TRUE; dhcp_run_tick = wizpf_get_systick(); goto RET_ALARM; } } switch(di.state) { case DHCP_STATE_INIT: if(dhcp_run_cnt==0 && !IS_TIME_PASSED(dhcp_run_tick, DHCP_OPEN_DELAY)) goto RET_ALARM; if(dhcp_run_cnt < DHCP_SEND_RETRY_COUNT) { dhcp_run_cnt++; if(send_discover() == RET_OK) { // Discover ok if(dhcp_async) { DBG("DHCP Discovery Send Async"); sockwatch_set(di.sock, WATCH_SOCK_UDP_SEND); return; // alarm set is not needed } else { DBG("DHCP Discovery Sent"); SET_STATE(DHCP_STATE_SEARCHING); dhcp_run_tick = wizpf_get_systick(); } } else { ERRA("DHCP Discovery SEND fail - (%d)times", dhcp_run_cnt); dhcp_run_tick = wizpf_get_systick(); } } else { ERRA("DHCP Discovery SEND fail - (%d)times", dhcp_run_cnt); dhcp_run_cnt = 0; UDPClose(di.sock); if(dhcp_async) sockwatch_close(di.sock); dhcp_fail(); return; // alarm set is not needed } break; case DHCP_STATE_SEARCHING: if(!IS_TIME_PASSED(dhcp_run_tick, DHCP_RETRY_DELAY)) { int8 ret = recv_handler(); if(ret == DHCP_MSG_OFFER) { SET_STATE(DHCP_STATE_SELECTING); dhcp_run_tick = wizpf_get_systick(); dhcp_run_cnt = 0; } else if(ret != RET_NOK) DBGCRTCA(TRUE, "recv wrong packet(%d)", ret); } else { ERRA("DHCP Offer RECV fail - for (%d)msec", DHCP_RETRY_DELAY); SET_STATE(DHCP_STATE_INIT); dhcp_run_tick = wizpf_get_systick(); } break; case DHCP_STATE_SELECTING: if(dhcp_run_cnt < DHCP_SEND_RETRY_COUNT) { dhcp_run_cnt++; if(send_request() == RET_OK) { // Request ok if(dhcp_async) { DBG("DHCP Request Send Async"); sockwatch_set(di.sock, WATCH_SOCK_UDP_SEND); return; // alarm set is not needed } else { DBG("DHCP Request Sent"); SET_STATE(DHCP_STATE_REQUESTING); dhcp_run_tick = wizpf_get_systick(); } } else { ERRA("DHCP Request SEND fail - (%d)times", dhcp_run_cnt); dhcp_run_tick = wizpf_get_systick(); } } else { ERRA("DHCP Request SEND fail - (%d)times", dhcp_run_cnt); dhcp_run_cnt = 0; UDPClose(di.sock); if(dhcp_async) sockwatch_close(di.sock); dhcp_fail(); return; // alarm set is not needed } break; case DHCP_STATE_REQUESTING: if(!IS_TIME_PASSED(dhcp_run_tick, DHCP_RETRY_DELAY)) { int8 ret = recv_handler(); if(ret == DHCP_MSG_ACK) { // Recv ACK LOG("DHCP Success"); SET_STATE(DHCP_STATE_IP_CHECK); dhcp_run_tick = wizpf_get_systick(); dhcp_run_cnt = 0; } else if(ret == DHCP_MSG_NAK) { // Recv NAK if(di.action == DHCP_ACT_START) { SET_STATE(DHCP_STATE_INIT); dhcp_run_tick = wizpf_get_systick(); } else { SET_STATE(DHCP_STATE_BOUND); } dhcp_run_cnt = 0; } else if(ret != RET_NOK) DBGCRTCA(TRUE, "recv wrong packet(%d)", ret); } else { ERRA("DHCP ACK RECV fail - for (%d)msec", DHCP_RETRY_DELAY); if(di.action == DHCP_ACT_START) { SET_STATE(DHCP_STATE_INIT); dhcp_run_tick = wizpf_get_systick(); } else { SET_STATE(DHCP_STATE_BOUND); } } break; case DHCP_STATE_IP_CHECK: //if(send_checker() == RET_OK) { SET_STATE(DHCP_STATE_BOUND); SetNetInfo(&workinfo); if(di.ip_update) di.ip_update(); LOGA("DHCP ok - New IP (%d.%d.%d.%d)", workinfo.ip[0], workinfo.ip[1], workinfo.ip[2], workinfo.ip[3]); //} else { // SET_STATE(DHCP_STATE_INIT); // ERR("IP Addr conflicted - IP(%d.%d.%d.%d)", workinfo.ip[0], workinfo.ip[1], workinfo.ip[2], workinfo.ip[3]); // send_rel_dec(DHCP_MSG_DECLINE); // if(di.ip_conflict) (*di.ip_conflict)(); //} break; case DHCP_STATE_BOUND: di.action = DHCP_ACT_NONE; UDPClose(di.sock); if(dhcp_async) sockwatch_close(di.sock); return; // alarm set is not needed case DHCP_STATE_FAILED: return; // alarm set is not needed default: ERRA("wrong state(%d)", di.state); return; // alarm set is not needed } RET_ALARM: if(dhcp_alarm) alarm_set(10, dhcp_alarm_cb, 0); }
void currentUptime(void *ptr, uint8 *len) { time_t curTime = wizpf_get_systick(); *(uint32 *)ptr = (uint32)(curTime - startTime) / 10; *len = 4; }
int32 main(void) { #define TCP_LISTEN_PORT 5000 #define UDP_LISTEN_PORT 5000 int8 ret, root; uint32 dhcp_renew, dhcp_rebind, dhcp_time; uint32 dhcp_tick, led_tick; ret = platform_init(); if(ret != RET_OK) { goto FAIL_TRAP; } ret = network_init(SOCK_DHCP, NULL, NULL); if(ret != RET_OK) { ERRA("network_init fail - ret(%d)", ret); goto FAIL_TRAP; } printf("\r\n-----------------------------------\r\n"); printf("SMTP Client using W5200\r\n"); printf("-----------------------------------\r\n\r\n"); Delay_tick(2000); do { ret = dhcp_manual(DHCP_ACT_START, NULL, &dhcp_renew, &dhcp_rebind); } while(ret != RET_OK); dhcp_renew = wizpf_tick_conv(FALSE, dhcp_renew); dhcp_rebind = wizpf_tick_conv(FALSE, dhcp_rebind); dhcp_time = dhcp_renew; menu_init(); root = menu_add("Network setting", 0, NULL); menu_add("Show", root, mn_show_network); menu_add("Static Set", root, mn_set_network); menu_add("Loopback", 0, mn_loopback); menu_add("LED Test", 0, mn_set_led); root = menu_add("App Test", 0, NULL); menu_add("DNS", root, mn_dns); menu_add("BASE64", root, mn_base64); menu_add("eMail", root, mn_email); menu_print_tree(); dhcp_tick = led_tick = wizpf_get_systick(); while(1) { if(wizpf_tick_elapse(dhcp_tick) > dhcp_time) { if(dhcp_time==dhcp_renew) DBG("start renew"); else DBG("start rebind"); ret = dhcp_manual(dhcp_time==dhcp_renew? DHCP_ACT_RENEW: DHCP_ACT_REBIND, NULL, &dhcp_renew, &dhcp_rebind); dhcp_tick = wizpf_get_systick(); if(ret == RET_OK) { // renew success dhcp_renew = wizpf_tick_conv(FALSE, dhcp_renew); dhcp_rebind = wizpf_tick_conv(FALSE, dhcp_rebind); dhcp_time = dhcp_renew; } else { if(dhcp_time == dhcp_renew) dhcp_time = dhcp_rebind; // renew fail, so try rebind else dhcp_time = 60000; // retry after 1 min } } menu_run(); if(lb_tcp) loopback_tcps(7, (uint16)TCP_LISTEN_PORT); if(lb_udp) loopback_udp(7, (uint16)UDP_LISTEN_PORT); if(wizpf_tick_elapse(led_tick) > 1000) { wizpf_led_set(WIZ_LED3, VAL_TOG); led_tick = wizpf_get_systick(); } } FAIL_TRAP: wizpf_led_trap(1); return 0; }