Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 363 | johboh | 1 | #define THIS_IS_TIMESYNC |
| 2 | |||
| 3 | #include "..\Include\DNS.h" |
||
| 4 | #include "..\Include\Helpers.h" |
||
| 5 | #include "..\Include\TCP.h" |
||
| 6 | #include "..\Include\ARPTsk.h" |
||
| 7 | #include "..\Include\ARP.h" |
||
| 8 | #include "..\Include\UART.h" |
||
| 9 | #include "..\Include\StackTsk.h" |
||
| 10 | #include "..\Include\TimeSync.h" |
||
| 11 | |||
| 12 | #if defined(STACK_USE_TIMESYNC) |
||
| 13 | |||
| 14 | typedef enum _timeState |
||
| 15 | { |
||
| 16 | SM_START=0, |
||
| 17 | SM_ARP_RESOLVE, |
||
| 18 | SM_ARP_RESOLVED, |
||
| 19 | SM_CONNECT, |
||
| 20 | SM_CONNECT_WAIT, |
||
| 21 | SM_CONNECTED, |
||
| 22 | SM_RECEIVE, |
||
| 23 | SM_DISCONNECT, |
||
| 24 | SM_ABORT, |
||
| 25 | SM_DONE |
||
| 26 | |||
| 27 | } timeState; |
||
| 28 | |||
| 29 | timeState smTS = SM_START; |
||
| 30 | |||
| 31 | DWORD lastSync = 0; |
||
| 32 | DWORD timeNow = 0; |
||
| 33 | |||
| 34 | //#define TIMESYNC_DEBUG |
||
| 35 | |||
| 36 | void timeSync(void) |
||
| 37 | { |
||
| 38 | BYTE i; |
||
| 39 | signed char j; |
||
| 40 | static TICK Timer; |
||
| 41 | static TICK perodicTick = 0; |
||
| 42 | static TICK t = 0; |
||
| 43 | static TCP_SOCKET MySocket = INVALID_SOCKET; |
||
| 44 | static NODE_INFO Server; |
||
| 45 | static int arp_tries = 0; |
||
| 46 | static int tcp_tries = 0; |
||
| 47 | |||
| 48 | BYTE rcnt=0; |
||
| 49 | BYTE ncnt=0; |
||
| 50 | char foundData=0; |
||
| 51 | |||
| 52 | if ((tickGet()-t) >= TICK_1S ) |
||
| 53 | { |
||
| 54 | t = tickGet(); |
||
| 55 | timeNow++; |
||
| 56 | } |
||
| 57 | |||
| 58 | switch(smTS) |
||
| 59 | { |
||
| 60 | case SM_START: |
||
| 61 | #if defined(TIMESYNC_DEBUG) |
||
| 62 | putrsUART("Start!\r\n"); |
||
| 63 | #endif |
||
| 64 | // Set IP adress to connect to. |
||
| 65 | Server.IPAddr.v[0]=193; |
||
| 66 | Server.IPAddr.v[1]=11; |
||
| 67 | Server.IPAddr.v[2]=249; |
||
| 68 | Server.IPAddr.v[3]=54; |
||
| 69 | |||
| 70 | arp_tries = 0; |
||
| 71 | tcp_tries = 0; |
||
| 72 | |||
| 73 | smTS = SM_ARP_RESOLVE; |
||
| 74 | break; |
||
| 75 | |||
| 76 | case SM_ARP_RESOLVE: |
||
| 77 | #if defined(TIMESYNC_DEBUG) |
||
| 78 | putrsUART("Resolve..\r\n"); |
||
| 79 | #endif |
||
| 80 | // If ARP is redy.. |
||
| 81 | if (ARPIsTxReady()) |
||
| 82 | { |
||
| 83 | // Resolve the IP adress.. |
||
| 84 | ARPResolve(&Server.IPAddr); |
||
| 85 | arp_tries++; |
||
| 86 | Timer = tickGet(); |
||
| 87 | smTS = SM_ARP_RESOLVED; |
||
| 88 | } |
||
| 89 | break; |
||
| 90 | |||
| 91 | case SM_ARP_RESOLVED: |
||
| 92 | #if defined(TIMESYNC_DEBUG) |
||
| 93 | putrsUART("Resolved..\r\n"); |
||
| 94 | #endif |
||
| 95 | // If IP adress is resolved, go to next state |
||
| 96 | if (ARPIsResolved(&Server.IPAddr, &Server.MACAddr)) |
||
| 97 | { |
||
| 98 | smTS = SM_CONNECT; |
||
| 99 | } |
||
| 100 | // If not resolved and spent long time here, |
||
| 101 | // Go back to previous state and re-resolve. |
||
| 102 | else if (tickGet()-Timer > 1*TICK_1S) |
||
| 103 | { |
||
| 104 | smTS = SM_ARP_RESOLVE; |
||
| 105 | } |
||
| 106 | else if (arp_tries>=MAX_ARP_TRIES) |
||
| 107 | { |
||
| 108 | //Abort |
||
| 109 | smTS = SM_ABORT; |
||
| 110 | } |
||
| 111 | |||
| 112 | break; |
||
| 113 | |||
| 114 | case SM_CONNECT: |
||
| 115 | #if defined(TIMESYNC_DEBUG) |
||
| 116 | putrsUART("Connect..\r\n"); |
||
| 117 | #endif |
||
| 118 | // We have an sucessfull ARP, connect.. |
||
| 119 | MySocket = TCPConnect(&Server, ServerPort); |
||
| 120 | tcp_tries++; |
||
| 121 | |||
| 122 | if(MySocket == INVALID_SOCKET) |
||
| 123 | { |
||
| 124 | // Do something. |
||
| 125 | } |
||
| 126 | |||
| 127 | Timer = tickGet(); |
||
| 128 | smTS = SM_CONNECT_WAIT; |
||
| 129 | |||
| 130 | break; |
||
| 131 | |||
| 132 | case SM_CONNECT_WAIT: |
||
| 133 | #if defined(TIMESYNC_DEBUG) |
||
| 134 | putrsUART("Connect wait..\r\n"); |
||
| 135 | #endif |
||
| 136 | // Wait for connection.. |
||
| 137 | |||
| 138 | if (TCPIsConnected(MySocket)) |
||
| 139 | { |
||
| 140 | smTS = SM_CONNECTED; |
||
| 141 | } |
||
| 142 | // If not connected and spent long time here, |
||
| 143 | // Go back to previous state and re-connect. |
||
| 144 | else if (tickGet()-Timer > 5*TICK_1S) |
||
| 145 | { |
||
| 146 | TCPDisconnect(MySocket); |
||
| 147 | MySocket = INVALID_SOCKET; |
||
| 148 | smTS = SM_CONNECT; |
||
| 149 | } |
||
| 150 | else if (tcp_tries>=MAX_TCP_TRIES) |
||
| 151 | { |
||
| 152 | //Abort |
||
| 153 | TCPDisconnect(MySocket); |
||
| 154 | MySocket = INVALID_SOCKET; |
||
| 155 | smTS = SM_ABORT; |
||
| 156 | } |
||
| 157 | |||
| 158 | |||
| 159 | break; |
||
| 160 | |||
| 161 | case SM_CONNECTED: |
||
| 162 | #if defined(TIMESYNC_DEBUG) |
||
| 163 | putrsUART("Connected..\r\n"); |
||
| 164 | #endif |
||
| 165 | // Send data. |
||
| 166 | Timer = tickGet(); |
||
| 167 | |||
| 168 | if(TCPIsPutReady(MySocket)) |
||
| 169 | { |
||
| 170 | |||
| 171 | TCPPut(MySocket, 'G'); |
||
| 172 | TCPPut(MySocket, 'E'); |
||
| 173 | TCPPut(MySocket, 'T'); |
||
| 174 | TCPPut(MySocket, ' '); |
||
| 175 | TCPPut(MySocket, '/'); |
||
| 176 | TCPPut(MySocket, 't'); |
||
| 177 | TCPPut(MySocket, 'i'); |
||
| 178 | TCPPut(MySocket, 'm'); |
||
| 179 | TCPPut(MySocket, 'e'); |
||
| 180 | TCPPut(MySocket, '.'); |
||
| 181 | TCPPut(MySocket, 'p'); |
||
| 182 | TCPPut(MySocket, 'h'); |
||
| 183 | TCPPut(MySocket, 'p'); |
||
| 184 | TCPPut(MySocket, ' '); |
||
| 185 | TCPPut(MySocket, 'H'); |
||
| 186 | TCPPut(MySocket, 'T'); |
||
| 187 | TCPPut(MySocket, 'T'); |
||
| 188 | TCPPut(MySocket, 'P'); |
||
| 189 | TCPPut(MySocket, '/'); |
||
| 190 | TCPPut(MySocket, '1'); |
||
| 191 | TCPPut(MySocket, '.'); |
||
| 192 | TCPPut(MySocket, '0'); |
||
| 193 | TCPPut(MySocket, '\r'); |
||
| 194 | TCPPut(MySocket, '\n'); |
||
| 195 | TCPPut(MySocket, '\r'); |
||
| 196 | TCPPut(MySocket, '\n'); |
||
| 197 | |||
| 198 | // Send the packet |
||
| 199 | TCPFlush(MySocket); |
||
| 200 | |||
| 201 | smTS = SM_RECEIVE; |
||
| 202 | } |
||
| 203 | |||
| 204 | break; |
||
| 205 | |||
| 206 | case SM_RECEIVE: |
||
| 207 | #if defined(TIMESYNC_DEBUG) |
||
| 208 | putrsUART("Receive..\r\n"); |
||
| 209 | #endif |
||
| 210 | // Client disconnected. |
||
| 211 | if(!TCPIsConnected(MySocket)) |
||
| 212 | { |
||
| 213 | smTS = SM_ABORT; |
||
| 214 | break; |
||
| 215 | } |
||
| 216 | |||
| 217 | if(TCPIsGetReady(MySocket)) |
||
| 218 | { |
||
| 219 | while(TCPGet(MySocket, &i)) |
||
| 220 | { |
||
| 221 | if (i==CR) rcnt++; |
||
| 222 | else if(i==LF) ncnt++; |
||
| 223 | else |
||
| 224 | { |
||
| 225 | rcnt=0; |
||
| 226 | ncnt=0; |
||
| 227 | } |
||
| 228 | |||
| 229 | if (foundData==1) |
||
| 230 | { |
||
| 231 | if (j>=0) |
||
| 232 | { |
||
| 233 | timeNow=timeNow+(((DWORD)i)<<(8*j--)); |
||
| 234 | #if defined(TIMESYNC_DEBUG) |
||
| 235 | while(BusyUART()); WriteUART(i); |
||
| 236 | #endif |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | if(rcnt>1 && ncnt>1) {j=3; timeNow=0; foundData=1;} |
||
| 241 | |||
| 242 | |||
| 243 | } |
||
| 244 | smTS = SM_DISCONNECT; |
||
| 245 | } |
||
| 246 | |||
| 247 | break; |
||
| 248 | |||
| 249 | case SM_DISCONNECT: |
||
| 250 | #if defined(TIMESYNC_DEBUG) |
||
| 251 | putrsUART("\r\nDisconnect\r\n"); |
||
| 252 | #endif |
||
| 253 | foundData=0; |
||
| 254 | t = tickGet(); |
||
| 255 | lastSync = timeNow; |
||
| 256 | perodicTick=tickGet(); |
||
| 257 | TCPDisconnect(MySocket); |
||
| 258 | MySocket = INVALID_SOCKET; |
||
| 259 | smTS = SM_DONE; |
||
| 260 | break; |
||
| 261 | |||
| 262 | case SM_ABORT: |
||
| 263 | #if defined(TIMESYNC_DEBUG) |
||
| 264 | putrsUART("Abort...\r\n"); |
||
| 265 | #endif |
||
| 266 | smTS = SM_START; |
||
| 267 | break; |
||
| 268 | |||
| 269 | case SM_DONE: |
||
| 270 | if (tickGet()-perodicTick > SYNC_INTERVAL*TICK_1S) |
||
| 271 | { |
||
| 272 | #if defined(TIMESYNC_DEBUG) |
||
| 273 | putrsUART("GO!\r\n"); |
||
| 274 | #endif |
||
| 275 | smTS = SM_START; |
||
| 276 | } |
||
| 277 | break; |
||
| 278 | |||
| 279 | default: smTS = SM_START; break; |
||
| 280 | |||
| 281 | } |
||
| 282 | |||
| 283 | |||
| 284 | } |
||
| 285 | |||
| 286 | DWORD getTime(timeClock tc) |
||
| 287 | { |
||
| 288 | DWORD c; |
||
| 289 | |||
| 290 | if (tc==CURRENT) c = timeNow; |
||
| 291 | if (tc==LAST) c = lastSync; |
||
| 292 | |||
| 293 | if (lastSync>0) |
||
| 294 | return c; |
||
| 295 | return 0; |
||
| 296 | |||
| 297 | } |
||
| 298 | |||
| 299 | DWORD getTimeStr(timeClock tc,char *str) |
||
| 300 | { |
||
| 301 | DWORD c; |
||
| 302 | char i; |
||
| 303 | |||
| 304 | c = getTime(tc); |
||
| 305 | |||
| 306 | dwordToHex(c,str); |
||
| 307 | |||
| 308 | return c; |
||
| 309 | } |
||
| 310 | |||
| 311 | |||
| 312 | |||
| 313 | #endif |