/***************************************************************************** Function: void SNTPClient(void) Summary: Periodically checks the current time from a pool of servers. Description: This function periodically checks a pool of time servers to obtain the current date/time. Precondition: UDP is initialized. Parameters: None Returns: None Remarks: This function requires once available UDP socket while processing, but frees that socket when the SNTP module is idle. ***************************************************************************/ void SNTPClient(void) { NTP_PACKET pkt; WORD w; // static NODE_INFO Server; static DWORD dwTimer; static UDP_SOCKET MySocket = INVALID_UDP_SOCKET; static enum { SM_HOME = 0, SM_UDP_IS_OPENED, //SM_NAME_RESOLVE, //SM_ARP_START_RESOLVE, //SM_ARP_RESOLVE, //SM_ARP_START_RESOLVE2, //SM_ARP_RESOLVE2, //SM_ARP_START_RESOLVE3, //SM_ARP_RESOLVE3, //SM_ARP_RESOLVE_FAIL, SM_UDP_SEND, SM_UDP_RECV, SM_SHORT_WAIT, SM_WAIT } SNTPState = SM_HOME; switch(SNTPState) { case SM_HOME: if(MySocket == INVALID_UDP_SOCKET) MySocket = UDPOpenEx((DWORD)(PTR_BASE)NTP_SERVER,UDP_OPEN_ROM_HOST,0,NTP_SERVER_PORT); SNTPState++; break; case SM_UDP_IS_OPENED: if(UDPIsOpened(MySocket) == TRUE) { SNTPState = SM_UDP_SEND; } /* else { UDPClose(MySocket); SNTPState = SM_HOME; MySocket = INVALID_UDP_SOCKET; } */ break; #if 0 // Obtain ownership of the DNS resolution module if(!DNSBeginUsage()) break; // Obtain the IP address associated with the server name DNSResolveROM((ROM BYTE*)NTP_SERVER, DNS_TYPE_A); dwTimer = TickGet(); SNTPState = SM_NAME_RESOLVE; break; case SM_NAME_RESOLVE: // Wait for DNS resolution to complete if(!DNSIsResolved(&Server.IPAddr)) { if((TickGet() - dwTimer) > (5 * TICK_SECOND)) { DNSEndUsage(); dwTimer = TickGetDiv64K(); SNTPState = SM_SHORT_WAIT; } break; } // Obtain DNS resolution result if(!DNSEndUsage()) { // No valid IP address was returned from the DNS // server. Quit and fail for a while if host is not valid. dwTimer = TickGetDiv64K(); SNTPState = SM_SHORT_WAIT; break; } SNTPState = SM_ARP_START_RESOLVE; // No need to break case SM_ARP_START_RESOLVE: case SM_ARP_START_RESOLVE2: case SM_ARP_START_RESOLVE3: // Obtain the MAC address associated with the server's IP address ARPResolve(&Server.IPAddr); dwTimer = TickGet(); SNTPState++; break; case SM_ARP_RESOLVE: case SM_ARP_RESOLVE2: case SM_ARP_RESOLVE3: // Wait for the MAC address to finish being obtained if(!ARPIsResolved(&Server.IPAddr, &Server.MACAddr)) { // Time out if too much time is spent in this state if(TickGet() - dwTimer > 1*TICK_SECOND) { // Retransmit ARP request by going to next SM_ARP_START_RESOLVE state or fail by going to SM_ARP_RESOLVE_FAIL state. SNTPState++; } break; } SNTPState = SM_UDP_SEND; break; case SM_ARP_RESOLVE_FAIL: // ARP failed after 3 tries, abort and wait for next time query dwTimer = TickGetDiv64K(); SNTPState = SM_SHORT_WAIT; break; #endif // case SM_UDP_IS_OPENED: case SM_UDP_SEND: // Open up the sending UDP socket //MySocket = UDPOpen(0, &Server, NTP_SERVER_PORT); #if 0 MySocket = UDPOpenEx(NTP_SERVER,UDP_OPEN_ROM_HOST,0,NTP_SERVER_PORT); if(MySocket == INVALID_UDP_SOCKET) break; #endif // Make certain the socket can be written to if(!UDPIsPutReady(MySocket)) { UDPClose(MySocket); SNTPState = SM_HOME; MySocket = INVALID_UDP_SOCKET; break; } // Transmit a time request packet memset(&pkt, 0, sizeof(pkt)); pkt.flags.versionNumber = 3; // NTP Version 3 pkt.flags.mode = 3; // NTP Client pkt.orig_ts_secs = swapl(NTP_EPOCH); UDPPutArray((BYTE*) &pkt, sizeof(pkt)); UDPFlush(); dwTimer = TickGet(); SNTPState = SM_UDP_RECV; break; case SM_UDP_RECV: // Look for a response time packet if(!UDPIsGetReady(MySocket)) { if((TickGet()) - dwTimer > NTP_REPLY_TIMEOUT) { // Abort the request and wait until the next timeout period UDPClose(MySocket); //dwTimer = TickGetDiv64K(); //SNTPState = SM_SHORT_WAIT; SNTPState = SM_HOME; MySocket = INVALID_UDP_SOCKET; break; } break; } // Get the response time packet w = UDPGetArray((BYTE*) &pkt, sizeof(pkt)); UDPClose(MySocket); dwTimer = TickGetDiv64K(); SNTPState = SM_WAIT; MySocket = INVALID_UDP_SOCKET; // Validate packet size if(w != sizeof(pkt)) { break; } // Set out local time to match the returned time dwLastUpdateTick = TickGet(); dwSNTPSeconds = swapl(pkt.tx_ts_secs) - NTP_EPOCH; // Do rounding. If the partial seconds is > 0.5 then add 1 to the seconds count. if(((BYTE*)&pkt.tx_ts_fraq)[0] & 0x80) dwSNTPSeconds++; #ifdef WIFI_NET_TEST wifi_net_test_print("SNTP: current time", dwSNTPSeconds); #endif break; case SM_SHORT_WAIT: // Attempt to requery the NTP server after a specified NTP_FAST_QUERY_INTERVAL time (ex: 8 seconds) has elapsed. if(TickGetDiv64K() - dwTimer > (NTP_FAST_QUERY_INTERVAL/65536ull)) { SNTPState = SM_HOME; MySocket = INVALID_UDP_SOCKET; } break; case SM_WAIT: // Requery the NTP server after a specified NTP_QUERY_INTERVAL time (ex: 10 minutes) has elapsed. if(TickGetDiv64K() - dwTimer > (NTP_QUERY_INTERVAL/65536ull)) { SNTPState = SM_HOME; MySocket = INVALID_UDP_SOCKET; } break; } }
/***************************************************************************** Function: void DNSClientTask(void) Summary: DNS client state machine Description: Process the DNS client state machine Precondition: DNSClientInit has been called. Parameters: None Return Values: None ***************************************************************************/ void DNSClientTask(void) { uint8_t i; TCPIP_UINT16_VAL w; DNS_HEADER DNSHeader; DNS_ANSWER_HEADER DNSAnswerHeader; switch(smDNS) { case DNS_IDLE: break; // nothing to do case DNS_START: smDNS = DNSRetry(DNS_START); stateStartTime = 0; // flag the first Open try break; case DNS_OPEN_SOCKET: DNSSocket = UDPOpenClient(IP_ADDRESS_TYPE_IPV4, DNS_CLIENT_PORT, (IP_MULTI_ADDRESS*)(DNSServers + vDNSServerIx)); if(DNSSocket == INVALID_UDP_SOCKET) { if(stateStartTime == 0) { stateStartTime = SYS_TICK_Get(); } else if(SYS_TICK_Get() - stateStartTime > (DNS_CLIENT_OPEN_TMO * SYS_TICK_TicksPerSecondGet())) { smDNS = DNS_FAIL_OPEN_TMO; } break; } // got a valid UDP socket UDPSocketSetNet(DNSSocket, pDNSNet); stateStartTime = SYS_TICK_Get(); smDNS = DNS_QUERY; // no break, start sending the query; case DNS_QUERY: if(!UDPIsOpened(DNSSocket) || (UDPIsTxPutReady(DNSSocket, 18 + strlen (DNSHostName)) < (18 + strlen (DNSHostName)))) { if(SYS_TICK_Get() - stateStartTime > (DNS_CLIENT_OPEN_TMO * SYS_TICK_TicksPerSecondGet())) { smDNS = DNS_FAIL_OPEN_TMO; } break; // wait some more } // Put DNS query here SentTransactionID.Val = (uint16_t)rand(); UDPPut(DNSSocket, SentTransactionID.v[1]);// User chosen transaction ID UDPPut(DNSSocket, SentTransactionID.v[0]); UDPPut(DNSSocket, 0x01); // Standard query with recursion UDPPut(DNSSocket, 0x00); UDPPut(DNSSocket, 0x00); // 0x0001 questions UDPPut(DNSSocket, 0x01); UDPPut(DNSSocket, 0x00); // 0x0000 answers UDPPut(DNSSocket, 0x00); UDPPut(DNSSocket, 0x00); // 0x0000 name server resource records UDPPut(DNSSocket, 0x00); UDPPut(DNSSocket, 0x00); // 0x0000 additional records UDPPut(DNSSocket, 0x00); // Put hostname string to resolve DNSPutString(DNSSocket, DNSHostName); UDPPut(DNSSocket, 0x00); // Type: DNS_TYPE_A A (host address) or DNS_TYPE_MX for mail exchange UDPPut(DNSSocket, RecordType); UDPPut(DNSSocket, 0x00); // Class: IN (Internet) UDPPut(DNSSocket, 0x01); UDPFlush(DNSSocket); stateStartTime = SYS_TICK_Get(); smDNS = DNS_GET_RESULT; break; case DNS_GET_RESULT: if(!UDPIsGetReady(DNSSocket)) { if(SYS_TICK_Get() - stateStartTime > (DNS_CLIENT_SERVER_TMO * SYS_TICK_TicksPerSecondGet())) { smDNS = DNS_FAIL_SERVER; } break; } // Retrieve the DNS header and de-big-endian it UDPGet(DNSSocket, &DNSHeader.TransactionID.v[1]); UDPGet(DNSSocket, &DNSHeader.TransactionID.v[0]); // Throw this packet away if it isn't in response to our last query if(DNSHeader.TransactionID.Val != SentTransactionID.Val) { UDPDiscard(DNSSocket); break; } UDPGet(DNSSocket, &DNSHeader.Flags.v[1]); UDPGet(DNSSocket, &DNSHeader.Flags.v[0]); UDPGet(DNSSocket, &DNSHeader.Questions.v[1]); UDPGet(DNSSocket, &DNSHeader.Questions.v[0]); UDPGet(DNSSocket, &DNSHeader.Answers.v[1]); UDPGet(DNSSocket, &DNSHeader.Answers.v[0]); UDPGet(DNSSocket, &DNSHeader.AuthoritativeRecords.v[1]); UDPGet(DNSSocket, &DNSHeader.AuthoritativeRecords.v[0]); UDPGet(DNSSocket, &DNSHeader.AdditionalRecords.v[1]); UDPGet(DNSSocket, &DNSHeader.AdditionalRecords.v[0]); // Remove all questions (queries) while(DNSHeader.Questions.Val--) { DNSDiscardName(DNSSocket); UDPGet(DNSSocket, &w.v[1]); // Question type UDPGet(DNSSocket, &w.v[0]); UDPGet(DNSSocket, &w.v[1]); // Question class UDPGet(DNSSocket, &w.v[0]); } // Scan through answers while(DNSHeader.Answers.Val--) { DNSDiscardName(DNSSocket); // Throw away response name UDPGet(DNSSocket, &DNSAnswerHeader.ResponseType.v[1]); // Response type UDPGet(DNSSocket, &DNSAnswerHeader.ResponseType.v[0]); UDPGet(DNSSocket, &DNSAnswerHeader.ResponseClass.v[1]); // Response class UDPGet(DNSSocket, &DNSAnswerHeader.ResponseClass.v[0]); UDPGet(DNSSocket, &DNSAnswerHeader.ResponseTTL.v[3]); // Time to live UDPGet(DNSSocket, &DNSAnswerHeader.ResponseTTL.v[2]); UDPGet(DNSSocket, &DNSAnswerHeader.ResponseTTL.v[1]); UDPGet(DNSSocket, &DNSAnswerHeader.ResponseTTL.v[0]); UDPGet(DNSSocket, &DNSAnswerHeader.ResponseLen.v[1]); // Response length UDPGet(DNSSocket, &DNSAnswerHeader.ResponseLen.v[0]); // Make sure that this is a 4 byte IP address, response type A or MX, class 1 // Check if this is Type A, MX, or AAAA if( DNSAnswerHeader.ResponseClass.Val == 0x0001u) // Internet class { if (DNSAnswerHeader.ResponseType.Val == 0x0001u && DNSAnswerHeader.ResponseLen.Val == 0x0004u) { Flags.bits.AddressValid = true; Flags.bits.AddressType = IP_ADDRESS_TYPE_IPV4; UDPGet(DNSSocket, &ResolvedAddress.ipv4Address.v[0]); UDPGet(DNSSocket, &ResolvedAddress.ipv4Address.v[1]); UDPGet(DNSSocket, &ResolvedAddress.ipv4Address.v[2]); UDPGet(DNSSocket, &ResolvedAddress.ipv4Address.v[3]); goto DoneSearchingRecords; } else if (DNSAnswerHeader.ResponseType.Val == 0x001Cu && DNSAnswerHeader.ResponseLen.Val == 0x0010u) { if (RecordType != DNS_TYPE_AAAA) { while(DNSAnswerHeader.ResponseLen.Val--) { UDPGet(DNSSocket, &i); } break; } Flags.bits.AddressValid = true; Flags.bits.AddressType = IP_ADDRESS_TYPE_IPV6; UDPGetArray (DNSSocket, (void *)&ResolvedAddress.ipv6Address, sizeof (IPV6_ADDR)); goto DoneSearchingRecords; } else { while(DNSAnswerHeader.ResponseLen.Val--) { UDPGet(DNSSocket, &i); } } } else { while(DNSAnswerHeader.ResponseLen.Val--) { UDPGet(DNSSocket, &i); } } } // Remove all Authoritative Records while(DNSHeader.AuthoritativeRecords.Val--) { DNSDiscardName(DNSSocket); // Throw away response name UDPGet(DNSSocket, &DNSAnswerHeader.ResponseType.v[1]); // Response type UDPGet(DNSSocket, &DNSAnswerHeader.ResponseType.v[0]); UDPGet(DNSSocket, &DNSAnswerHeader.ResponseClass.v[1]); // Response class UDPGet(DNSSocket, &DNSAnswerHeader.ResponseClass.v[0]); UDPGet(DNSSocket, &DNSAnswerHeader.ResponseTTL.v[3]); // Time to live UDPGet(DNSSocket, &DNSAnswerHeader.ResponseTTL.v[2]); UDPGet(DNSSocket, &DNSAnswerHeader.ResponseTTL.v[1]); UDPGet(DNSSocket, &DNSAnswerHeader.ResponseTTL.v[0]); UDPGet(DNSSocket, &DNSAnswerHeader.ResponseLen.v[1]); // Response length UDPGet(DNSSocket, &DNSAnswerHeader.ResponseLen.v[0]); // Make sure that this is a 4 byte IP address, response type A or MX, class 1 // Check if this is Type A if( DNSAnswerHeader.ResponseClass.Val == 0x0001u) // Internet class { if (DNSAnswerHeader.ResponseType.Val == 0x0001u && DNSAnswerHeader.ResponseLen.Val == 0x0004u) { Flags.bits.AddressValid = true; Flags.bits.AddressType = IP_ADDRESS_TYPE_IPV4; UDPGet(DNSSocket, &ResolvedAddress.ipv4Address.v[0]); UDPGet(DNSSocket, &ResolvedAddress.ipv4Address.v[1]); UDPGet(DNSSocket, &ResolvedAddress.ipv4Address.v[2]); UDPGet(DNSSocket, &ResolvedAddress.ipv4Address.v[3]); goto DoneSearchingRecords; } else if (DNSAnswerHeader.ResponseType.Val == 0x001Cu && DNSAnswerHeader.ResponseLen.Val == 0x0010u) { if (RecordType != DNS_TYPE_AAAA) { while(DNSAnswerHeader.ResponseLen.Val--) { UDPGet(DNSSocket, &i); } break; } Flags.bits.AddressValid = true; Flags.bits.AddressType = IP_ADDRESS_TYPE_IPV6; UDPGetArray (DNSSocket, (void *)&ResolvedAddress.ipv6Address, sizeof (IPV6_ADDR)); goto DoneSearchingRecords; } else { while(DNSAnswerHeader.ResponseLen.Val--) { UDPGet(DNSSocket, &i); } } } else { while(DNSAnswerHeader.ResponseLen.Val--) { UDPGet(DNSSocket, &i); } } } // Remove all Additional Records while(DNSHeader.AdditionalRecords.Val--) { DNSDiscardName(DNSSocket); // Throw away response name UDPGet(DNSSocket, &DNSAnswerHeader.ResponseType.v[1]); // Response type UDPGet(DNSSocket, &DNSAnswerHeader.ResponseType.v[0]); UDPGet(DNSSocket, &DNSAnswerHeader.ResponseClass.v[1]); // Response class UDPGet(DNSSocket, &DNSAnswerHeader.ResponseClass.v[0]); UDPGet(DNSSocket, &DNSAnswerHeader.ResponseTTL.v[3]); // Time to live UDPGet(DNSSocket, &DNSAnswerHeader.ResponseTTL.v[2]); UDPGet(DNSSocket, &DNSAnswerHeader.ResponseTTL.v[1]); UDPGet(DNSSocket, &DNSAnswerHeader.ResponseTTL.v[0]); UDPGet(DNSSocket, &DNSAnswerHeader.ResponseLen.v[1]); // Response length UDPGet(DNSSocket, &DNSAnswerHeader.ResponseLen.v[0]); // Make sure that this is a 4 byte IP address, response type A or MX, class 1 // Check if this is Type A if( DNSAnswerHeader.ResponseClass.Val == 0x0001u) // Internet class { if (DNSAnswerHeader.ResponseType.Val == 0x0001u && DNSAnswerHeader.ResponseLen.Val == 0x0004u) { Flags.bits.AddressValid = true; Flags.bits.AddressType = IP_ADDRESS_TYPE_IPV4; UDPGet(DNSSocket, &ResolvedAddress.ipv4Address.v[0]); UDPGet(DNSSocket, &ResolvedAddress.ipv4Address.v[1]); UDPGet(DNSSocket, &ResolvedAddress.ipv4Address.v[2]); UDPGet(DNSSocket, &ResolvedAddress.ipv4Address.v[3]); goto DoneSearchingRecords; } else if (DNSAnswerHeader.ResponseType.Val == 0x001Cu && DNSAnswerHeader.ResponseLen.Val == 0x0010u) { if (RecordType != DNS_TYPE_AAAA) { while(DNSAnswerHeader.ResponseLen.Val--) { UDPGet(DNSSocket, &i); } break; } Flags.bits.AddressValid = true; Flags.bits.AddressType = IP_ADDRESS_TYPE_IPV6; UDPGetArray (DNSSocket, (void *)&ResolvedAddress.ipv6Address, sizeof (IPV6_ADDR)); goto DoneSearchingRecords; } else { while(DNSAnswerHeader.ResponseLen.Val--) { UDPGet(DNSSocket, &i); } } } else { while(DNSAnswerHeader.ResponseLen.Val--) { UDPGet(DNSSocket, &i); } } } DoneSearchingRecords: UDPDiscard(DNSSocket); _DNSReleaseSocket(); if(Flags.bits.AddressValid) { smDNS = DNS_DONE; } else { smDNS = DNSRetry(DNS_FAIL_SERVER); } break; // done case DNS_FAIL_ARP: // see if there is other server we may try smDNS = DNSRetry(DNS_FAIL_ARP); break; case DNS_FAIL_SERVER: smDNS = DNSRetry(DNS_FAIL_SERVER); break; default: // DNS_DONE, DNS_FAIL_ARP_TMO, DNS_FAIL_OPEN_TMO, DNS_FAIL_SERVER_TMO // either done or some error state break; } #if DNS_CLIENT_VERSION_NO >= 2 dnsTickPending = 0; #endif // DNS_CLIENT_VERSION_NO >= 2 }
/***************************************************************************** Function: int sendto(SOCKET s, const char* buf, int len, int flags, const struct sockaddr* to, int tolen) Summary: This function used to send the data for both connection oriented and connection-less sockets. Description: The sendto function is used to send outgoing data on a socket. The destination address is given by to and tolen. Both Datagram and stream sockets are supported. Precondition: socket function should be called. Parameters: s - Socket descriptor returned from a previous call to socket. buf - application data buffer containing data to transmit. len - length of data in bytes. flags - message flags. Currently this field is not supported. to - Optional pointer to the the sockaddr structure containing the destination address. If NULL, the currently bound remote port and IP address are used as the destination. tolen - length of the sockaddr structure. Returns: On success, sendto returns number of bytes sent. In case of error returns SOCKET_ERROR Remarks: None. ***************************************************************************/ int sendto( SOCKET s, const char* buf, int len, int flags, const struct sockaddr* to, int tolen ) { struct BSDSocket *socket; int size = SOCKET_ERROR; NODE_INFO remoteInfo; // static DWORD startTick; // NOTE: startTick really should be a per socket BSDSocket structure member since other BSD calls can interfere with the ARP cycles WORD wRemotePort; struct sockaddr_in local; if( s >= BSD_SOCKET_COUNT ) return SOCKET_ERROR; socket = &BSDSocketArray[s]; if(socket->bsdState == SKT_CLOSED) return SOCKET_ERROR; if(socket->SocketType == SOCK_DGRAM) //UDP { // Decide the destination IP address and port remoteInfo.IPAddr.Val = socket->remoteIP; wRemotePort = socket->remotePort; if(to) { if((unsigned int)tolen != sizeof(struct sockaddr_in)) return SOCKET_ERROR; wRemotePort = ((struct sockaddr_in*)to)->sin_port; remoteInfo.IPAddr.Val = ((struct sockaddr_in*)to)->sin_addr.s_addr; // Implicitly bind the socket if it isn't already if(socket->bsdState == SKT_CREATED) { memset(&local, 0x00, sizeof(local)); if(bind(s, (struct sockaddr*)&local, sizeof(local)) == SOCKET_ERROR) return SOCKET_ERROR; } } if(UDPIsOpened((UDP_SOCKET)s) != TRUE) return SOCKET_ERROR; if(remoteInfo.IPAddr.Val == IP_ADDR_ANY) remoteInfo.IPAddr.Val = 0xFFFFFFFFu; #if 0 // Set the remote IP and MAC address if it is different from what we already have stored in the UDP socket if(UDPSocketInfo[socket->SocketID].remoteNode.IPAddr.Val != remoteInfo.IPAddr.Val) { if(ARPIsResolved(&remoteInfo.IPAddr, &remoteInfo.MACAddr)) { memcpy((void*)&UDPSocketInfo[socket->SocketID].remoteNode, (void*)&remoteInfo, sizeof(remoteInfo)); } else { if(TickGet() - startTick > 1*TICK_SECOND) { ARPResolve(&remoteInfo.IPAddr); startTick = TickGet(); } return SOCKET_ERROR; } } #endif // Select the UDP socket and see if we can write to it if(UDPIsPutReady(socket->SocketID)) { // Set the proper remote port UDPSocketInfo[socket->SocketID].remotePort = wRemotePort; // Write data and send UDP datagram size = UDPPutArray((BYTE*)buf, len); UDPFlush(); return size; } } else if(socket->SocketType == SOCK_STREAM) //TCP will only send to the already established socket. { if(socket->bsdState != SKT_EST) return SOCKET_ERROR; if(HandlePossibleTCPDisconnection(s)) return SOCKET_ERROR; // Handle special case were 0 return value is okay if(len == 0) return 0; // Write data to the socket. If one or more bytes were written, then // return this value. Otherwise, fail and return SOCKET_ERROR. size = TCPPutArray(socket->SocketID, (BYTE*)buf, len); if(size) return size; } return SOCKET_ERROR; }
// // Main application entry point. // int main(void) { #if defined(HOST_CM_TEST) DWORD t1 = 0; char st[80]; BOOL host_scan = FALSE; UINT16 scan_count = 0; #endif static IPV4_ADDR dwLastIP[sizeof (TCPIP_HOSTS_CONFIGURATION) / sizeof (*TCPIP_HOSTS_CONFIGURATION)]; int i, nNets; #if defined(SYS_USERIO_ENABLE) static SYS_TICK startTick = 0; int32_t LEDstate=SYS_USERIO_LED_DEASSERTED; #endif // defined(SYS_USERIO_ENABLE) TCPIP_NET_HANDLE netH; const char *netName=0; const char *netBiosName; #if defined (TCPIP_STACK_USE_ZEROCONF_MDNS_SD) char mDNSServiceName[] = "MyWebServiceNameX "; // base name of the service Must not exceed 16 bytes long // the last digit will be incremented by interface #endif // defined (TCPIP_STACK_USE_ZEROCONF_MDNS_SD) // perform system initialization if(!SYS_Initialize()) { return 0; } SYS_CONSOLE_MESSAGE("\r\n\n\n --- TCPIP Demo Starts! --- \r\n"); SYS_OUT_MESSAGE("TCPIPStack " TCPIP_STACK_VERSION " "" "); // Initialize the TCPIP stack if (!TCPIP_STACK_Init(TCPIP_HOSTS_CONFIGURATION, sizeof (TCPIP_HOSTS_CONFIGURATION) / sizeof (*TCPIP_HOSTS_CONFIGURATION), TCPIP_STACK_MODULE_CONFIG_TBL, sizeof (TCPIP_STACK_MODULE_CONFIG_TBL) / sizeof (*TCPIP_STACK_MODULE_CONFIG_TBL))) { return 0; } // Display the names associated with each interface // Perform mDNS registration if mDNS is enabled nNets = TCPIP_STACK_NetworksNo(); for(i = 0; i < nNets; i++) { netH = TCPIP_STACK_IxToNet(i); netName = TCPIP_STACK_NetName(netH); netBiosName = TCPIP_STACK_NetBIOSName(netH); #if defined(TCPIP_STACK_USE_NBNS) SYS_CONSOLE_PRINT(" Interface %s on host %s - NBNS enabled\r\n", netName, netBiosName); #else SYS_CONSOLE_PRINT(" Interface %s on host %s - NBNS disabled\r\n", netName, netBiosName); #endif // defined(TCPIP_STACK_USE_NBNS) #if defined (TCPIP_STACK_USE_ZEROCONF_MDNS_SD) mDNSServiceName[sizeof(mDNSServiceName) - 2] = '1' + i; mDNSServiceRegister( netH , mDNSServiceName // name of the service ,"_http._tcp.local" // type of the service ,80 // TCP or UDP port, at which this service is available ,((const BYTE *)"path=/index.htm") // TXT info ,1 // auto rename the service when if needed ,NULL // no callback function ,NULL); // no application context #endif //TCPIP_STACK_USE_ZEROCONF_MDNS_SD } #if defined (TCPIP_STACK_USE_IPV6) TCPIP_ICMPV6_RegisterCallback(ICMPv6Callback); #endif #if defined(TCPIP_STACK_USE_ICMP_CLIENT) ICMPRegisterCallback(PingProcessIPv4); #endif #if defined(TCPIP_STACK_USE_EVENT_NOTIFICATION) TCPIP_NET_HANDLE hWiFi = TCPIP_STACK_NetHandle("MRF24W"); if (hWiFi) { TCPIP_STACK_RegisterHandler(hWiFi, TCPIP_EV_RX_ALL | TCPIP_EV_TX_ALL | TCPIP_EV_RXTX_ERRORS, StackNotification, 0); } #endif // defined(TCPIP_STACK_USE_EVENT_NOTIFICATION) #if defined(WF_UPDATE_FIRMWARE_UART_24G) extern bool WF_FirmwareUpdate_Uart_24G(void); WF_FirmwareUpdate_Uart_24G(); #endif // Now that all items are initialized, begin the co-operative // multitasking loop. This infinite loop will continuously // execute all stack-related tasks, as well as your own // application's functions. Custom functions should be added // at the end of this loop. // Note that this is a "co-operative mult-tasking" mechanism // where every task performs its tasks (whether all in one shot // or part of it) and returns so that other tasks can do their // job. // If a task needs very long time to do its job, it must be broken // down into smaller pieces so that other tasks can have CPU time. while (1) { SYS_Tasks(); #if defined(SYS_USERIO_ENABLE) // Blink LED0 (right most one) every second. if (SYS_TICK_Get() - startTick >= SYS_TICK_TicksPerSecondGet() / 2ul) { startTick = SYS_TICK_Get(); LEDstate ^= SYS_USERIO_LED_ASSERTED; SYS_USERIO_SetLED(SYS_USERIO_LED_0, LEDstate); } #endif // defined(SYS_USERIO_ENABLE) // This task performs normal stack task including checking // for incoming packet, type of packet and calling // appropriate stack entity to process it. TCPIP_STACK_Task(); // Process application specific tasks here. // For this demo app, this will include the Generic TCP // client and servers, and the SNMP, Ping, and SNMP Trap // demos. Following that, we will process any IO from // the inputs on the board itself. // Any custom modules or processing you need to do should // go here. #if defined(TCPIP_STACK_USE_TCP) && defined(APP_USE_FTP_CLIENT_DEMO) FTPClient(); #endif #if defined(TCPIP_STACK_USE_TCP) && defined(APP_USE_GENERIC_TCP_CLIENT_DEMO) GenericTCPClient(); #endif #if defined(TCPIP_STACK_USE_TCP) && defined(APP_USE_GENERIC_TCP_SERVER_DEMO) GenericTCPServer(); #endif #if defined(TCPIP_STACK_USE_SMTP_CLIENT) && defined(APP_USE_SMTP_CLIENT_DEMO) SMTPDemo(); #endif #if (defined(TCPIP_STACK_USE_ICMP_CLIENT) || defined (TCPIP_STACK_USE_IPV6)) && defined(APP_USE_PING_DEMO) // use ping on the default interface PingDemoTask(); #endif #if defined(TCPIP_STACK_USE_SNMP_SERVER) && !defined(SNMP_TRAP_DISABLED) // User should use one of the following SNMP demo // This routine demonstrates V1 or V2 trap formats with one variable binding. //SNMPTrapDemo(); //This function sends the both SNMP trap version1 and 2 type of notifications #if defined(SNMP_STACK_USE_V2_TRAP) || defined(SNMP_V1_V2_TRAP_WITH_SNMPV3) //This routine provides V2 format notifications with multiple (3) variable bindings //User should modify this routine to send v2 trap format notifications with the required varbinds. SNMPV2TrapDemo(); //This function sends the SNMP trap version 2 type of notifications #endif /* SNMPSendTrap() is used to send trap notification to previously configured ip address if trap notification is enabled. There are different trap notification code. The current implementation sends trap for authentication failure (4). PreCondition: If application defined event occurs to send the trap. Declare a notification flag and update as the event occurs. Uncomment the below function if the application requires. if(notification flag is updated by the application as a predefined event occured) { SNMPSendTrap(); } */ #endif #if defined(TCPIP_STACK_USE_BERKELEY_API) && defined(APP_USE_BERKELEY_API_DEMO) BerkeleyTCPClientDemo(); BerkeleyTCPServerDemo(); BerkeleyUDPClientDemo(0); #endif // If the local IP address has changed (ex: due to DHCP lease change) // write the new IP address to the console display, UART, and Announce // service // We use the default interface for (i = 0; i < sizeof (TCPIP_HOSTS_CONFIGURATION) / sizeof (*TCPIP_HOSTS_CONFIGURATION); i++) { netH = TCPIP_STACK_NetHandle(TCPIP_HOSTS_CONFIGURATION[i].interface); if ((uint32_t) dwLastIP[i].Val != TCPIP_STACK_NetAddress(netH)) { dwLastIP[i].Val = TCPIP_STACK_NetAddress(netH); SYS_CONSOLE_PRINT("Interface Name is: %s\r\n", TCPIP_HOSTS_CONFIGURATION[i].interface); SYS_CONSOLE_MESSAGE("New IP Address is: "); DisplayIPValue(dwLastIP[i]); SYS_CONSOLE_MESSAGE("\r\n"); } } #if defined(TCPIP_STACK_USE_EVENT_NOTIFICATION) if (stackNotifyCnt) { stackNotifyCnt = 0; ProcessNotification(stackNotifyHandle); } #endif // defined(TCPIP_STACK_USE_EVENT_NOTIFICATION) #if defined(WF_UPDATE_FIRMWARE_TCPCLIENT_24G) void WF_FirmwareUpdate_TcpClient_24G(void); WF_FirmwareUpdate_TcpClient_24G(); #endif //defined(WF_UPDATE_FIRMWARE_TCPCLIENT_24G) #if defined(HOST_CM_TEST) switch (g_event) { case WF_EVENT_CONNECTION_PERMANENTLY_LOST: case WF_EVENT_CONNECTION_FAILED: g_event = 0xff; // clear current event // if host scan is active, it can be forced inactive by connection/re-connection process // so just reset host scan state to inactive. host_scan = FALSE; // host scan inactive SYS_CONSOLE_MESSAGE("Reconnecting....\r\n"); WF_Connect(); break; case WF_EVENT_CONNECTION_SUCCESSFUL: g_event = 0xff; // clear current event // if host scan is active, it can be forced inactive by connection/re-connection process // so just reset host scan state to inactive. host_scan = FALSE; // host scan inactive break; case WF_EVENT_SCAN_RESULTS_READY: g_event = 0xff; // clear current event host_scan = FALSE; // host scan inactive // Scan results are valid - OK to retrieve if (SCANCXT.numScanResults > 0) { SCAN_SET_DISPLAY(SCANCXT.scanState); SCANCXT.displayIdx = 0; while (IS_SCAN_STATE_DISPLAY(SCANCXT.scanState)) WFDisplayScanMgr(); } break; case WF_EVENT_CONNECTION_TEMPORARILY_LOST: // This event can happened when CM in module is enabled. g_event = 0xff; // clear current event // if host scan is active, it can be forced inactive by connection/re-connection process // so just reset host scan state to inactive. host_scan = FALSE; // host scan inactive break; default: //sprintf(st,"skip event = %d\r\n",g_event); //SYS_CONSOLE_MESSAGE(st); break; } if (g_DhcpSuccessful) { /* Send and Receive UDP packets */ if(UDPIsOpened(socket1)) { // UDP TX every 10 msec if(SYS_TICK_Get() - timeudp >= SYS_TICK_TicksPerSecondGet() / 100) { timeudp = SYS_TICK_Get(); tx_number++; LED0_IO ^= 1; sprintf(str,"rem=%12lu",tx_number); for(cntstr=16;cntstr<999;cntstr++) str[cntstr]=cntstr; str[999]=0; // Send tx_number (formatted in a string) if(UDPIsTxPutReady(socket1,1000)!=0) { UDPPutString(socket1,(BYTE *)str); UDPFlush(socket1); SYS_CONSOLE_MESSAGE("."); } } // UDP RX tx_number of remote board if(UDPIsGetReady(socket1)!=0) { LED1_IO ^= 1; UDPGetArray(socket1,(BYTE *)str,1000); str[16]=0; //sprintf((char*)LCDText,"%sloc=%12lu",str,tx_number); // Write on EXP16 LCD local and remote TX number //strcpypgm2ram(LCDText,str); //LCDUpdate(); SYS_CONSOLE_MESSAGE("Rx"); } } // Do host scan if((SYS_TICK_Get() - t1) >= SYS_TICK_TicksPerSecondGet() * 20) { t1 = SYS_TICK_Get(); if (!host_scan) // allow host scan if currently inactive { sprintf(st,"%d Scanning ..... event = %d\r\n",++scan_count, g_event); SYS_CONSOLE_MESSAGE(st); host_scan = TRUE; // host scan active WF_Scan(0xff); // scan on all channels } } } // DHCP status #endif //HOST_CM_TEST } }
/***************************************************************************** Function: CHAR TFTPGetUploadStatus(void) Summary: Returns the TFTP file upload status started by calling the TFTPUploadRAMFileToHost() or TFTPUploadFragmentedRAMFileToHost() functions. Description: Returns the TFTP file upload status started by calling the TFTPUploadRAMFileToHost() or TFTPUploadFragmentedRAMFileToHost() functions. Precondition: None Parameters: None Returns: A status code. Negative results are fatal errors. Positive results indicate the TFTP upload operation is still being processed. A zero result indicates successful file upload completion (TFTP API is now idle and available for further calls). Specific return values are as follows: 0 (TFTP_UPLOAD_COMPLETE): Upload completed successfully 1 (TFTP_UPLOAD_GET_DNS): Attempting to obtain DNS client module 2 (TFTP_UPLOAD_RESOLVE_HOST): Attempting to resolve TFTP hostname 3 (TFTP_UPLOAD_CONNECT): Attempting to ARP and contact the TFTP server 4 (TFTP_UPLOAD_SEND_FILENAME): Attempting to send the filename and receive acknowledgement. 5 (TFTP_UPLOAD_SEND_DATA): Attempting to send the file contents and receive acknowledgement. 6 (TFTP_UPLOAD_WAIT_FOR_CLOSURE): Attempting to send the final packet of file contents and receive acknowledgement. -1 (TFTP_UPLOAD_HOST_RESOLVE_TIMEOUT): Couldn't resolve hostname -2 (TFTP_UPLOAD_CONNECT_TIMEOUT): Couldn't finish ARP and reach server -3 (TFTP_UPLOAD_SERVER_ERROR): TFTP server returned an error (ex: access denial) or file upload failed due to a timeout (partial file may have been uploaded). Remarks: The DNS client module must be enabled to use this function. i.e. STACK_USE_DNS must be defined in TCPIPConfig.h. ***************************************************************************/ CHAR TFTPGetUploadStatus(void) { TFTP_RESULT result; IP_ADDR ipRemote; WORD w, w2; BYTE *vData; if(UDPIsOpened(_tftpSocket)== FALSE) { _tftpSocket = UDPOpenEx((DWORD)(ROM_PTR_BASE)vUploadRemoteHost, UDP_OPEN_ROM_HOST,TFTP_CLIENT_PORT, TFTP_SERVER_PORT); } switch(smUpload) { case TFTP_UPLOAD_GET_DNS: if(!DNSBeginUsage()) break; DNSResolveROM(vUploadRemoteHost, DNS_TYPE_A); smUpload = TFTP_UPLOAD_RESOLVE_HOST; break; case TFTP_UPLOAD_RESOLVE_HOST: if(!DNSIsResolved(&ipRemote)) break; DNSEndUsage(); if(ipRemote.Val == 0u) { smUpload = TFTP_UPLOAD_HOST_RESOLVE_TIMEOUT; break; } TFTPOpen(&ipRemote); smUpload = TFTP_UPLOAD_CONNECT; break; case TFTP_UPLOAD_CONNECT: switch(TFTPIsOpened()) { case TFTP_OK: TFTPOpenROMFile(vUploadFilename, TFTP_FILE_MODE_WRITE); smUpload = TFTP_UPLOAD_SEND_FILENAME; break; case TFTP_TIMEOUT: smUpload = TFTP_UPLOAD_CONNECT_TIMEOUT; break; default: break; } break; case TFTP_UPLOAD_SEND_FILENAME: result = TFTPIsFileOpened(); switch(result) { case TFTP_OK: smUpload = TFTP_UPLOAD_SEND_DATA; break; case TFTP_RETRY: TFTPOpenROMFile(vUploadFilename, TFTP_FILE_MODE_WRITE); break; case TFTP_TIMEOUT: smUpload = TFTP_UPLOAD_CONNECT_TIMEOUT; break; case TFTP_ERROR: smUpload = TFTP_UPLOAD_SERVER_ERROR; break; default: break; } if(result != TFTP_OK) break; // No break when TFTPIsFileOpened() returns TFTP_OK -- we need to immediately start sending data case TFTP_UPLOAD_SEND_DATA: switch(TFTPIsPutReady()) { case TFTP_OK: // Write blocksize bytes of data uploadChunkDescriptorForRetransmit = uploadChunkDescriptor; wUploadChunkOffsetForRetransmit = wUploadChunkOffset; vData = uploadChunkDescriptor->vDataPointer + wUploadChunkOffset; w = TFTP_BLOCK_SIZE; while(w) { w2 = uploadChunkDescriptor->wDataLength - wUploadChunkOffset; if(w2 > w) w2 = w; w -= w2; wUploadChunkOffset += w2; if(vData == NULL) { TFTPCloseFile(); smUpload = TFTP_UPLOAD_WAIT_FOR_CLOSURE; break; } while(w2--) { TFTPPut(*vData++); } if(wUploadChunkOffset == uploadChunkDescriptor->wDataLength) { uploadChunkDescriptor++; wUploadChunkOffset = 0; vData = uploadChunkDescriptor->vDataPointer; } } break; case TFTP_RETRY: uploadChunkDescriptor = uploadChunkDescriptorForRetransmit; wUploadChunkOffset = wUploadChunkOffsetForRetransmit; break; case TFTP_TIMEOUT: case TFTP_ERROR: smUpload = TFTP_UPLOAD_SERVER_ERROR; break; default: break; } break; case TFTP_UPLOAD_WAIT_FOR_CLOSURE: switch(TFTPIsFileClosed()) { case TFTP_OK: smUpload = TFTP_UPLOAD_COMPLETE; UDPClose(_tftpSocket); break; case TFTP_RETRY: uploadChunkDescriptor = uploadChunkDescriptorForRetransmit; wUploadChunkOffset = wUploadChunkOffsetForRetransmit; smUpload = TFTP_UPLOAD_SEND_DATA; break; case TFTP_TIMEOUT: case TFTP_ERROR: smUpload = TFTP_UPLOAD_SERVER_ERROR; break; default: break; } break; default: break; } return smUpload; }