time_t NTPTimeOut(uint8_t * NTPServer, uint32_t timeout) { uint32_t tickStart; time_t currentTime; int8_t ret; uint8_t dnsIP[4] = {0,}; sockPool *sockP = sockPoolAlloc(); ret = DNSTimeOut(NTPServer,dnsIP,NETINFO_DNS_TIMEOUT); if(ret < 0)//dns timeout; { sockPoolFree(sockP); return ret; } SNTP_init(sockP->sockNo,dnsIP,40,sockP->netBuf); tickStart = HAL_GetTick(); do{ ret = SNTP_run(¤tTime); if(ret == 1) { sockPoolFree(sockP); return currentTime; } }while((HAL_GetTick() - tickStart) < timeout); sockPoolFree(sockP); return -1; }
/* Defined by the application code, but called by FreeRTOS+UDP when the network connects/disconnects (if ipconfigUSE_NETWORK_EVENT_HOOK is set to 1 in FreeRTOSIPConfig.h). */ void vApplicationIPNetworkEventHook(eIPCallbackEvent_t eNetworkEvent) { uint32_t ulIPAddress, ulNetMask, ulGatewayAddress, ulDNSServerAddress; static BaseType_t xTasksAlreadyCreated = pdFALSE; int8_t cBuffer[16]; char bufor[100]; /* Check this was a network up event, as opposed to a network down event. */ if (eNetworkEvent == eNetworkUp) { /* Create the tasks that use the IP stack if they have not already been created. */ if (xTasksAlreadyCreated == pdFALSE) { //if(xTaskCreate(vUDPTransmitCANFramesTask,"Tx CAN/UDP",UDPD_CAN_TX_SIZE,NULL,UDP_CAN_TX_PRIO,NULL)==pdPASS) print_console("Creating UDP/CAN Tx task -- > OK\r\n"); // else print_console("Creating UDP/CAN Tx -- > failed\r\n"); SNTP_init(); //daj znac taskowi do obslugi http/ftp,ze moze utworzyc sockety #if ipconfigUSE_HTTP==1 || ipconfigUSE_FTP==1 xTaskNotifyGive(xServerWorkTaskHandle); #endif xTasksAlreadyCreated = pdTRUE; } /* The network is up and configured. Print out the configuration, which may have been obtained from a DHCP server. */ FreeRTOS_GetAddressConfiguration(&ulIPAddress, &ulNetMask, &ulGatewayAddress, &ulDNSServerAddress); print_console("\r\n-----------------------\r\n\r\n"); /* Convert the IP address to a string then print it out. */ FreeRTOS_inet_ntoa(ulIPAddress, cBuffer); sprintf(bufor, "IP Address: %s\r\n", cBuffer); print_console(bufor); gdb.sys.ipaddress = ulIPAddress; /* Convert the net mask to a string then print it out. */ FreeRTOS_inet_ntoa(ulNetMask, cBuffer); sprintf(bufor, "Subnet Mask: %s\r\n", cBuffer); print_console(bufor); gdb.sys.maskaddress = ulNetMask; /* Convert the IP address of the gateway to a string then print it out. */ FreeRTOS_inet_ntoa(ulGatewayAddress, cBuffer); sprintf(bufor, "Gateway IP Address: %s\r\n", cBuffer); print_console(bufor); gdb.sys.gwaddress = ulGatewayAddress; /* Convert the IP address of the DNS server to a string then print it out. */ FreeRTOS_inet_ntoa(ulDNSServerAddress, cBuffer); sprintf(bufor, "DNS server IP Address: %s\r\n", cBuffer); print_console(bufor); gdb.sys.dnsaddress = ulDNSServerAddress; } }