void initializeTime(int GMT_zone) { GMT_hour_adding = GMT_zone; while(epoch<epochtime) { vTaskDelay(50); //Remember to enable the SNTP Client in the wizard. epoch=SNTPGetUTCSeconds(); } epoch=SNTPGetUTCSeconds(); now=(time_t)epoch; ts=localtime(&now); vTaskDelay(20); ts->tm_hour = (ts->tm_hour + GMT_hour_adding); // Correct if overflowed hour 0-24 format if(ts->tm_hour > 24) { ts->tm_hour = ts->tm_hour - 24; } else if(ts->tm_hour < 0) { ts->tm_hour = ts->tm_hour +24; } sprintf (epochString, "\nReceived date/time is: %s \r\n", asctime (ts) ); UARTWrite(1,epochString); RTCCSet(ts); //Set time };
void tempo_hum(void){ char buffer[SIZE]; time_t timestamp; timestamp = SNTPGetUTCSeconds() + (unsigned long)UTC_CORRECAO; printf("%u\n", (unsigned int)timestamp); strftime(buffer, SIZE, "%a %e %b %Y %r %Z %n", gmtime(×tamp)); printf("\r\n%s", buffer); }
void tempo_lcd(char *tlcd){ time_t timestamp; struct tm *dif; timestamp = SNTPGetUTCSeconds() + (unsigned long)UTC_CORRECAO; if(timestamp > 10000){ dif = gmtime(×tamp); sprintf(tlcd, "%02d:%02d %02d/%02d/%02d", dif->tm_hour, dif->tm_min, dif->tm_mday, dif->tm_mon+1, dif->tm_year-100); } else { sprintf(tlcd, "--:-- --/--/--"); } }
void ClockInit() //Initializes RTCC based on SNTP and also configures RTCC to raise an alarm every second { time_t now; struct tm *ts; DWORD epoch=0; DWORD epochtime=0xA2C2A; t_RTCC mytime, myalarm; if(profile.SNTPEnable) { UARTWrite(1,"waiting..."); while(epoch<epochtime) { vTaskDelay(50); epoch=SNTPGetUTCSeconds();//Get SNTP time } UARTWrite(1, "done!\r\n"); } else { epoch=profile.Time+19800; } //Get SNTP time //epoch = SNTPGetUTCSeconds(); //+ 19800; //Converting GMT to Indian time//manoj now = (time_t)epoch; ts = gmtime(&now); //Time Library related conversion for GMT format //ts = localtime(&now); //Time Library related conversion //Manoj //Set the RTC according to SNTP time mytime.year = ts->tm_year; //mytime is internal structure for Flyport defined for RTC mytime.month = ts->tm_mon+1; mytime.dweek = ts->tm_wday; mytime.day = ts->tm_mday; mytime.hour = ts->tm_hour; mytime.min = ts->tm_min; mytime.sec = ts->tm_sec; RTCCWrite (&mytime); myalarm = mytime; //Configuring RTC to raise an alarm every second (This will be used for sampling the sensors and transmitting the data) myalarm.sec = myalarm.sec + 1; RTCCSetAlarm(&myalarm, REPEAT_INFINITE, EVERY_SEC); RTCCRunAlarm(1); }
BYTE RunServer(BYTE bConnectionID, BYTE* pbBlob, int* pbBlobLen) { BYTE res = 0; BYTE i = 0; BYTE j = 0; BYTE Command = 0; BOOL NeedAnswer = FALSE; BYTE Answers = 0; BYTE bAttributeLen = 0; BYTE pbMem[MEM_BUFFER_LEN]; ST_ATTRIBUTE Data[MAX_ATTRIBUTE]; BYTE bMemPos = 0; BYTE bBlobPos = 0; ST_ATTRIBUTE Answer[] = { {STA_FLAG, sizeof(BYTE),&Answers } }; ST_ATTRIBUTE SendData[] = { {0, 0, 0 } }; if(*pbBlobLen == 0) return STR_OK; if(*pbBlobLen < 0) { Connection[bConnectionID].Mode = STS_NO_CONNECT; return STR_NEED_DISCONNECT; } memset(pbMem,0,sizeof(pbMem)); memset(Data,0,sizeof(Data)); res = ParseBlob(pbBlob, *pbBlobLen, Data, &bAttributeLen, pbMem, sizeof(pbMem), &bMemPos); if(res!=STR_OK) return res; i = FindParam(Data, bAttributeLen, STA_COMMAND); if(i==255) return STR_COMMAND_UNKNOWN; Command = *((BYTE*)Data[i].pValue); switch(Connection[bConnectionID].Mode){ case STS_NO_CONNECT: if(Command == STC_REQEST_CONNECT) { Answers = STF_AUTH_NEEDED; Connection[bConnectionID].Mode = STS_AUTH_REQ; } else { Answers = STF_DECLINED; } NeedAnswer = TRUE; break; case STS_AUTH_REQ: if(Command == STC_REQEST_AUTH) { BYTE login = FindParam(Data, bAttributeLen, STA_LOGIN); BYTE password = FindParam(Data, bAttributeLen, STA_PASSWORD); if((login == 255)||(password == 255)) { Answers = STF_COMMAND_INCOMPLETE; Connection[bConnectionID].Mode = STS_NO_CONNECT; } else { //strcmp(); if(1){ // authority check Answers = STF_ACCEPTED; Connection[bConnectionID].Mode = STS_CONNECTED; } else { Answers = STF_DECLINED; Connection[bConnectionID].Mode = STS_NO_CONNECT; } } } else { Answers = STF_DECLINED; Connection[bConnectionID].Mode = STS_NO_CONNECT; } NeedAnswer = TRUE; break; case STS_CONNECTED: LED1_IO = 1; switch(Command){ case STC_REQEST_DATA: for(j = 0; j < bAttributeLen; j++){ switch (Data[j].type){ case STA_COMMAND: case STA_FLAG: case STA_LOGIN: case STA_PASSWORD: break; case STA_NETWORK_NAME: SendData[0].type = STA_NETWORK_NAME; SendData[0].ulValueLen = sizeof(AppConfig.NetBIOSName); SendData[0].pValue = AppConfig.NetBIOSName; res = FormBlob(SendData, 1, pbBlob, MAX_BUFFER_LEN, &bBlobPos); if(res!=STR_OK){ Answers = STF_DATA_ERROR; i = bAttributeLen; // interrupt process *pbBlobLen = 0; bBlobPos = 0; }else { Answers = STF_DATA_READY; *pbBlobLen = bBlobPos; } break; case STA_TIME_SNTP: { static DWORD dwTime; static BYTE vTime[11]; dwTime = SNTPGetUTCSeconds(); ultoa(dwTime, vTime); SendData[0].type = STA_TIME_SNTP; SendData[0].ulValueLen = sizeof(vTime); SendData[0].pValue = vTime; res = FormBlob(SendData, 1, pbBlob, MAX_BUFFER_LEN, &bBlobPos); if(res!=STR_OK){ Answers = STF_DATA_ERROR; i = bAttributeLen; // interrupt process *pbBlobLen = 0; bBlobPos = 0; }else { Answers = STF_DATA_READY; *pbBlobLen = bBlobPos; } } break; default: Answers = STF_DATA_TYPE_UNKNOWN; i = bAttributeLen; // interrupt process *pbBlobLen = 0; bBlobPos = 0; }; } NeedAnswer = TRUE; res = STR_OK; break; case STC_SEND_DATA: res = STR_OK; break; default : Answers = STF_COMMAND_UNKNOWN; } break; default: res = STR_FUNCTION_FAILED; } if(NeedAnswer) { res = FormBlob(Answer, 1, pbBlob, MAX_BUFFER_LEN, &bBlobPos); *pbBlobLen = bBlobPos; if(res!=STR_OK) return res; if(Connection[bConnectionID].Mode == STS_NO_CONNECT) { res = STR_NEED_DISCONNECT; } else { res = STR_NEED_ANSWER; } } return res; }
/********************************************************************* * Function: void TelnetTask(void) * * PreCondition: Stack is initialized() * * Input: None * * Output: None * * Side Effects: None * * Overview: None * * Note: None ********************************************************************/ void TelnetTask(void) { BYTE i; BYTE vTelnetSession; WORD w, w2; TCP_SOCKET MySocket; enum { SM_HOME = 0, SM_PRINT_LOGIN, SM_GET_LOGIN, SM_GET_PASSWORD, SM_GET_PASSWORD_BAD_LOGIN, SM_AUTHENTICATED, SM_REFRESH_VALUES } TelnetState; static TCP_SOCKET hTelnetSockets[MAX_TELNET_CONNECTIONS]; static BYTE vTelnetStates[MAX_TELNET_CONNECTIONS]; static BOOL bInitialized = FALSE; // Perform one time initialization on power up if(!bInitialized) { for(vTelnetSession = 0; vTelnetSession < MAX_TELNET_CONNECTIONS; vTelnetSession++) { hTelnetSockets[vTelnetSession] = INVALID_SOCKET; vTelnetStates[vTelnetSession] = SM_HOME; } bInitialized = TRUE; } // Loop through each telnet session and process state changes and TX/RX data for(vTelnetSession = 0; vTelnetSession < MAX_TELNET_CONNECTIONS; vTelnetSession++) { // Load up static state information for this session MySocket = hTelnetSockets[vTelnetSession]; TelnetState = vTelnetStates[vTelnetSession]; // Reset our state if the remote client disconnected from us if(MySocket != INVALID_SOCKET) { if(TCPWasReset(MySocket)) TelnetState = SM_PRINT_LOGIN; } // Handle session state switch(TelnetState) { case SM_HOME: // Connect a socket to the remote TCP server MySocket = TCPOpen(0, TCP_OPEN_SERVER, TELNET_PORT, TCP_PURPOSE_TELNET); // Abort operation if no TCP socket of type TCP_PURPOSE_TELNET is available // If this ever happens, you need to go add one to TCPIPConfig.h if(MySocket == INVALID_SOCKET) break; TelnetState++; break; case SM_PRINT_LOGIN: // Make certain the socket can be written to if(TCPIsPutReady(MySocket) < strlenpgm((ROM char*)strTitle)) break; // Place the application protocol data into the transmit buffer. TCPPutROMString(MySocket, strTitle); // Send the packet TCPFlush(MySocket); TelnetState++; case SM_GET_LOGIN: // Make sure we can put the password prompt if(TCPIsPutReady(MySocket) < strlenpgm((ROM char*)strPassword)) break; // See if the user pressed return w = TCPFind(MySocket, '\n', 0, FALSE); if(w == 0xFFFFu) { if(TCPGetRxFIFOFree(MySocket) == 0u) { TCPPutROMString(MySocket, (ROM BYTE*)"\r\nToo much data.\r\n"); TCPDisconnect(MySocket); } break; } // Search for the username -- case insensitive w2 = TCPFindROMArray(MySocket, (ROM BYTE*)TELNET_USERNAME, sizeof(TELNET_USERNAME)-1, 0, TRUE); if((w2 != 0u) || !((sizeof(TELNET_USERNAME)-1 == w) || (sizeof(TELNET_USERNAME) == w))) { // Did not find the username, but let's pretend we did so we don't leak the user name validity TelnetState = SM_GET_PASSWORD_BAD_LOGIN; } else { TelnetState = SM_GET_PASSWORD; } // Username verified, throw this line of data away TCPGetArray(MySocket, NULL, w + 1); // Print the password prompt TCPPutROMString(MySocket, strPassword); TCPFlush(MySocket); break; case SM_GET_PASSWORD: case SM_GET_PASSWORD_BAD_LOGIN: // Make sure we can put the authenticated prompt if(TCPIsPutReady(MySocket) < strlenpgm((ROM char*)strAuthenticated)) break; // See if the user pressed return w = TCPFind(MySocket, '\n', 0, FALSE); if(w == 0xFFFFu) { if(TCPGetRxFIFOFree(MySocket) == 0u) { TCPPutROMString(MySocket, (ROM BYTE*)"Too much data.\r\n"); TCPDisconnect(MySocket); } break; } // Search for the password -- case sensitive w2 = TCPFindROMArray(MySocket, (ROM BYTE*)TELNET_PASSWORD, sizeof(TELNET_PASSWORD)-1, 0, FALSE); if((w2 != 3u) || !((sizeof(TELNET_PASSWORD)-1 == w-3) || (sizeof(TELNET_PASSWORD) == w-3)) || (TelnetState == SM_GET_PASSWORD_BAD_LOGIN)) { // Did not find the password TelnetState = SM_PRINT_LOGIN; TCPPutROMString(MySocket, strAccessDenied); TCPDisconnect(MySocket); break; } // Password verified, throw this line of data away TCPGetArray(MySocket, NULL, w + 1); // Print the authenticated prompt TCPPutROMString(MySocket, strAuthenticated); TelnetState = SM_AUTHENTICATED; // No break case SM_AUTHENTICATED: if(TCPIsPutReady(MySocket) < strlenpgm((ROM char*)strDisplay) + 4) break; TCPPutROMString(MySocket, strDisplay); TelnetState++; // All future characters will be bold TCPPutROMString(MySocket, (ROM BYTE*)"\x1b[1m"); case SM_REFRESH_VALUES: if(TCPIsPutReady(MySocket) >= 78u) { //[10;1] //"SNTP Time: (disabled)\r\n" //"Analog: 1023\r\n" //"Buttons: 3 2 1 0\r\n" //"LEDs: 7 6 5 4 3 2 1 0\r\n" // Write current UTC seconds from SNTP module, if it is enable // and has changed. Note that conversion from a DWORD to an // ASCII string can take a lot of CPU power, so we only print // this if the value has changed. #if defined(STACK_USE_SNTP_CLIENT) { static DWORD dwTime; BYTE vTime[11]; if(dwTime != SNTPGetUTCSeconds()) { // Position cursor at Line 10, Col 15 TCPPutROMString(MySocket, (ROM BYTE*)"\x1b[10;15f"); dwTime = SNTPGetUTCSeconds(); ultoa(dwTime, vTime); TCPPutROMArray(MySocket, (ROM BYTE*)strSpaces, 10-strlen((char*)vTime)); TCPPutString(MySocket, vTime); } } #endif // Position cursor at Line 11, Col 21 TCPPutROMString(MySocket, (ROM BYTE*)"\x1b[11;21f"); // Put analog value with space padding on right side for 4 characters TCPPutROMArray(MySocket, (ROM BYTE*)strSpaces, 4-strlen((char*)AN0String)); TCPPutString(MySocket, AN0String); // Put Buttons TCPPutROMString(MySocket, (ROM BYTE*)"\x1b[12;18f"); TCPPut(MySocket, BUTTON3_IO ? '1':'0'); TCPPut(MySocket, ' '); TCPPut(MySocket, BUTTON2_IO ? '1':'0'); TCPPut(MySocket, ' '); TCPPut(MySocket, BUTTON1_IO ? '1':'0'); TCPPut(MySocket, ' '); TCPPut(MySocket, BUTTON0_IO ? '1':'0'); // Put LEDs TCPPutROMString(MySocket, (ROM BYTE*)"\x1b[13;10f"); TCPPut(MySocket, LED1_IO ? '1':'0'); TCPPut(MySocket, ' '); TCPPut(MySocket, LED0_IO ? '1':'0'); // Put cursor at beginning of next line TCPPutROMString(MySocket, (ROM BYTE*)"\x1b[14;1f"); // Send the data out immediately TCPFlush(MySocket); } if(TCPIsGetReady(MySocket)) { TCPGet(MySocket, &i); switch(i) { case '\r': case 'q': case 'Q': if(TCPIsPutReady(MySocket) >= strlenpgm((ROM char*)strGoodBye)) TCPPutROMString(MySocket, strGoodBye); TCPDisconnect(MySocket); TelnetState = SM_PRINT_LOGIN; break; } } break; } // Save session state back into the static array hTelnetSockets[vTelnetSession] = MySocket; vTelnetStates[vTelnetSession] = TelnetState; } }