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); } }
static int8 sock_put(uint8 sock) { DBGCRTCA(sock<ATC_SOCK_NUM_START||sock>ATC_SOCK_NUM_END, "wrong sock(%d)", sock); if(sockstat[sock] & SOCK_STAT_UDP) { udpip[sock][0] = udpip[sock][1] = udpip[sock][2] = 0; udpip[sock][3] = udpport[sock] = 0; } sockstat[sock] = SOCK_STAT_IDLE; sockport[sock] = 0; sockwatch_clr(sock, WATCH_SOCK_ALL_MASK); return RET_OK; }
static int8 sock_get(int8 initval, uint16 srcport) { int8 i; DBGCRTCA(initval<1||initval>3, "wrong init value(%d)", initval); for(i=ATC_SOCK_NUM_START; i<=ATC_SOCK_NUM_END; i++) { if(sockstat[i] == SOCK_STAT_IDLE) { sockstat[i] = initval; sockport[i] = srcport; //DBGA("Sock(%d) Assign stt(%d), port(%d)", i, sockstat[i], sockport[i]); return i; } } return RET_NOK; }
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 atc_async_cb(uint8 sock, uint8 item, int32 ret) { DBGCRTCA(sock<ATC_SOCK_NUM_START||sock>ATC_SOCK_NUM_END, "wrong sock(%d)", sock); switch(item) { case WATCH_SOCK_UDP_SEND: DBG("WATCH_SOCK_UDP_SEND"); sockbusy[sock] = VAL_FALSE; //DBGA("WATCH UDP Sent - sock(%d), item(%d)", sock, item); if(ret == RET_OK) { cmd_resp(RET_OK, sock); } else { DBGA("WATCH_SOCK_UDP_SEND fail - ret(%d)", ret); cmd_resp(RET_TIMEOUT, sock); } break; case WATCH_SOCK_TCP_SEND: DBG("WATCH_SOCK_TCP_SEND"); if(ret < RET_OK) { sockbusy[sock] = VAL_FALSE; DBGA("WATCH_SOCK_TCP_SEND fail - ret(%d)", ret); sock_put(sock); cmd_resp(RET_TIMEOUT, sock); } else { tcpleft[sock] -= ret; if(tcpleft[sock] > 0) { ret = TCPReSendNB(sock); if(ret != RET_OK) { if(ret == SOCKERR_BUSY) { alarm_set(WINDOWFULL_WAIT_TIME, atc_resend_alarm, sock); } else if(ret == SOCKERR_WINDOW_FULL) { sockbusy[sock] = VAL_FALSE; DBGA("WATCH_SOCK_TCP_SEND fail - ret(%d)", ret); cmd_resp(RET_TIMEOUT, sock); } else { sockbusy[sock] = VAL_FALSE; DBGA("WATCH_SOCK_TCP_SEND fail - ret(%d)", ret); sock_put(sock); cmd_resp(RET_TIMEOUT, sock); } } else sockwatch_set(sock, WATCH_SOCK_TCP_SEND); } else { sockbusy[sock] = VAL_FALSE; cmd_resp(RET_OK, sock); } } break; case WATCH_SOCK_CONN_TRY: DBG("WATCH_SOCK_CONN_TRY"); sockbusy[sock] = VAL_FALSE; if(ret == RET_OK) { BITSET(sockstat[sock], SOCK_STAT_CONNECTED); sockwatch_set(sock, WATCH_SOCK_CLS_EVT); sockwatch_set(sock, WATCH_SOCK_RECV); cmd_resp(RET_ASYNC, sock); } else { DBGA("WATCH_SOCK_CONN_EVT fail - ret(%d)", ret); sock_put(sock); cmd_resp(RET_TIMEOUT, sock); } break; case WATCH_SOCK_CLS_TRY: DBG("WATCH_SOCK_CLS_TRY"); sockbusy[sock] = VAL_FALSE; if(ret == RET_OK) { sock_put(sock); cmd_resp(RET_ASYNC, sock); } else { CRITICAL_ERRA("WATCH_SOCK_CONN_EVT fail - ret(%d)", ret); } break; case WATCH_SOCK_CONN_EVT: DBG("WATCH_SOCK_CONN_EVT"); if(ret == RET_OK) { BITSET(sockstat[sock], SOCK_STAT_CONNECTED); sockwatch_set(sock, WATCH_SOCK_CLS_EVT); sockwatch_set(sock, WATCH_SOCK_RECV); if(atci.poll != POLL_MODE_FULL) EVENT_RESP(sock, SOCKEVENT_CONN); else event_enqueue(sock, SOCKEVENT_CONN); } else { CRITICAL_ERRA("WATCH_SOCK_CONN_EVT fail - ret(%d)", ret); } break; case WATCH_SOCK_CLS_EVT: DBG("WATCH_SOCK_CLS_EVT"); sockbusy[sock] = VAL_FALSE; if(ret == RET_OK) { if(sockwatch_chk(sock, WATCH_SOCK_CLS_TRY) == RET_OK) cmd_resp(RET_OK, sock); sock_put(sock); if(atci.poll != POLL_MODE_FULL) EVENT_RESP(sock, SOCKEVENT_CLS); else event_enqueue(sock, SOCKEVENT_CLS); } else { CRITICAL_ERRA("WATCH_SOCK_CONN_EVT fail - ret(%d)", ret); } break; case WATCH_SOCK_RECV: DBG("WATCH_SOCK_RECV"); { int8 i; if(atci.poll != POLL_MODE_NONE) { recvnum++; if(recvord[sock] == 0) { for(i=ATC_SOCK_NUM_START; i<=ATC_SOCK_NUM_END; i++) if(recvord[i] != 0) recvord[i]++; } else { for(i=ATC_SOCK_NUM_START; i<=ATC_SOCK_NUM_END; i++) if(recvord[i] != 0 && recvord[i] < recvord[sock]) recvord[i]++; } recvord[sock] = 1; if(atci.poll != POLL_MODE_FULL) EVENT_RESP_SIZE(sock, SOCKEVENT_RECV, GetSocketRxRecvBufferSize(sock)); else event_enqueue(sock, SOCKEVENT_RECV); } else { act_nrecv(sock, WORK_BUF_SIZE); } } break; default: CRITICAL_ERRA("wrong item(0x%x)", item); } }