/[H8]/trunk/PIC/Demo trimmet/TFTPcDemo.c
ViewVC logotype

Diff of /trunk/PIC/Demo trimmet/TFTPcDemo.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 96 by hedin, Tue May 22 06:41:06 2007 UTC revision 105 by hedin, Sun May 27 16:32:30 2007 UTC
# Line 9  Line 9 
9  #include "arptsk.h"  #include "arptsk.h"
10  #include "tcp.h"  #include "tcp.h"
11    
12    #include <string.h>
13    
14  bit bIsDhcpUp = 0;  bit bIsDhcpUp = 0;
15    char netlevel = 0;
16    
17  // All TFTP command statuts display will be done on first line of LCD.  // All TFTP command statuts display will be done on first line of LCD.
18  //#define TFTP_COMMAND_DISPLAY_LINE       0  //#define TFTP_COMMAND_DISPLAY_LINE       0
# Line 18  bit bIsDhcpUp = 0; Line 21  bit bIsDhcpUp = 0;
21  #define TFTP_COMMAND_RESULT_POSITION    15  #define TFTP_COMMAND_RESULT_POSITION    15
22    
23    
24  #define STARTUP_MSG "G1_Build_0x00"  /*#define STARTUP_MSG "G1_Build_0x00"
25    
26  ROM char StartupMsg[] = STARTUP_MSG;  ROM char StartupMsg[] = STARTUP_MSG;
27    
# Line 30  ROM char SetupMsg[] = "Board Setup..."; Line 33  ROM char SetupMsg[] = "Board Setup...";
33    
34  //                         1234567890123456  //                         1234567890123456
35  //ROM char blankLCDLine[] = "                ";  //ROM char blankLCDLine[] = "                ";
36    */
37  /*  /*
38   * This is used by other stack elements.   * This is used by other stack elements.
39   * Main application must define this and initialize it with   * Main application must define this and initialize it with
# Line 81  typedef enum _NetworkState Line 84  typedef enum _NetworkState
84          SockOpen,          SockOpen,
85          TcpReadyToSend,          TcpReadyToSend,
86          TcpSend,          TcpSend,
87            HelloDone,
88            StregkodeSendDone,
89            ReadyToRecieve,
90            StregkodeRecieveDone,
91            TotalRequested,
92            TotalRequestedDone,
93            ChangeRequested,
94            ChangeRequestedDone,
95            Acknowledged,
96            CancelDone,
97          SockClosing,          SockClosing,
98          SockClosed          SockClosed
99  } NetworkState;  } NetworkState;
# Line 96  TCP_SOCKET tsock1; Line 109  TCP_SOCKET tsock1;
109  /*****************************  /*****************************
110   network funktions   network funktions
111   ****************************/   ****************************/
112  int net_INT = 13, i;  unsigned long global_network_amount;
113  char *net_CHAR = "1234567890123";  unsigned char global_network_char[16];
114    unsigned char global_network_charlen;
115    
116    
117  void network_init(void)  void network_init(void)
118  {  {
119          state = off;          state = off;
120  }        }      
121    
122    
123    void network_worker(void)
124    {    
125            TickGet();
126        StackTask();    
127    }      
128    
129    void network_parse_amount(unsigned char buf[])
130    {
131            global_network_amount = buf[0]<<24 + buf[1]<<16 + buf[2]<<8 + buf[3];
132    }      
133    
134    void network_read_amount(void)
135    {
136            unsigned char buf[4];
137            TCPGetArray(tsock1, buf,4);
138            network_parse_amount(buf);
139    }      
140    
141    void network_wait(unsigned char n)
142    {
143            int i;
144            for (i=0; i<n; i++)
145            {
146                    network_worker();
147                    Delay10us(1);
148            }
149    }      
150    
151    char network_read_ack(void)
152    {
153            char ack;
154            while(state != Acknowledged)
155            {
156                    if (TCPIsGetReady(tsock1))
157                    {
158                            TCPGet(tsock1, &ack);
159                            TCPDiscard(tsock1);
160                            state = Acknowledged;
161                    }
162                    network_worker();
163            }
164            
165            state = TcpReadyToSend;
166            
167            if (ack == 1)
168                    return 0;
169            else
170                    return 4;
171    }      
172    
173    //Request ID 0
174  char network_send_hello( unsigned char termid )  char network_send_hello( unsigned char termid )
175  {  {
176  char error = 0;          char gotArp = 0;                // if gotArp is 0, the embedded haven't talked to the server since boot.
177  char gotArp = 0;          static char isBound = 0;
178  static char isBound = 0;  
179            while( !DHCPIsBound() && isBound == 0 )
180  if ( DHCPIsBound() && isBound == 0 ){                  network_worker();
         state = DhcpIsBound;  
         isBound = 1;  
 }  
181    
182          switch (state)          if ( DHCPIsBound() && isBound == 0 ){
183                    state = DhcpIsBound;
184                    isBound = 1;
185            }
186            
187            while( state != HelloDone )
188          {          {
189                  case DhcpIsBound:                  switch (state)
190                          if ( ARPIsTxReady() && gotArp == 0 )                  {
191                                  state = ArpIsTxReady;                          case DhcpIsBound:
192                          else                                  if ( ARPIsTxReady() && gotArp == 0 )
193                                            state = ArpIsTxReady;
194                                    else
195                                            state = ArpIsResolved;
196                                    break;
197                            
198                            case ArpIsTxReady:
199                                    ARPResolve(&rnode.IPAddr);
200                                    gotArp = 1;
201                                  state = ArpIsResolved;                                  state = ArpIsResolved;
202                          return 0;                                  break;
203                                                    
204                            case ArpIsResolved:
205                                    if ( ARPIsResolved(&rnode.IPAddr, &rnode.MACAddr) ){
206                                            state = SockOpening;
207                                    }
208                                    break;
209                            
210                            case SockOpening:
211                                    tsock1 = TCPConnect (&rnode, 3000);
212                                    state = SockOpen;
213                                    break;
214                                    
215                  case ArpIsTxReady:                          case SockOpen:
216                          ARPResolve(&rnode.IPAddr);                                  if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
217                          gotArp = 1;                                          state = TcpSend;
218                          state = ArpIsResolved;                                  break;
219                          return 0;                          
220                                                                    case TcpSend:
221                  case ArpIsResolved:                                  TCPPut( tsock1, 0x00 );
222                          if (ARPIsResolved(&rnode.IPAddr, &rnode.MACAddr) ){                                  TCPPut( tsock1, termid );
223                                  state = SockOpening;                                  TCPFlush(tsock1);
224                                  return 0;                                  //TCPDiscard( tsock1 );
225                                    state = HelloDone;
226                                    break;
227                    }
228                    network_worker();
229            }
230            return network_read_ack();
231    }
232    
233    //Request ID 1
234    char network_send_scan_frame( unsigned char antal, unsigned char buflen )
235    {
236            char i;
237            char ack;
238    
239            while( state != StregkodeSendDone ) //Sender vare-request-info pakke
240            {
241                    switch (state)
242                    {
243                            case TcpReadyToSend:
244                                    if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
245                                    state = TcpSend;
246                            case TcpSend:
247                                    TCPPut( tsock1, 0x01 );
248                                    TCPPut( tsock1, antal );
249                                    TCPPut( tsock1, buflen );
250                                    for (i = 0; i < buflen; i++)
251                                    {
252                                            TCPPut(tsock1, global_network_char[i] );
253                                  }                                  }
254                                  else{                                  TCPFlush(tsock1);
255                                          state = ArpIsResolved;                                  state = StregkodeSendDone;
256                                          return 2;                                                                       // no arp resolved                                  break;
257                    }
258                    network_worker();
259            }
260            
261            while( state != StregkodeRecieveDone ) //venter på svar og parser denne
262            {
263                    switch( state )
264                    {
265                            case StregkodeSendDone:
266                                    if( TCPIsGetReady(tsock1) )
267                                            state = ReadyToRecieve;
268                                    break;
269                            case ReadyToRecieve:
270                                    TCPGet( tsock1, &ack );
271                                    
272                                    if (ack == 0)
273                                    {
274                                            state = TcpReadyToSend;
275                                            TCPDiscard(tsock1);
276                                            return 4;
277                                  }                                  }
278                                                    
279                  case SockOpening:                                  network_read_amount();
280                          tsock1 = TCPConnect (&rnode, 3000);                                  TCPGet( tsock1, &global_network_charlen);
281                          state = SockOpen;                                  TCPGetArray( tsock1, global_network_char, global_network_charlen);
282                          return 0;                                  TCPDiscard(tsock1);
283                                    
284                                    state = StregkodeRecieveDone;                          
285                                            
286                                    break;
287                    }
288                    network_worker();
289            }
290            state = TcpReadyToSend;
291            return 0;
292    }
293    
294    //Request ID 2
295    char network_send_calc_total(void)
296    {
297            unsigned char ack;
298            while(state != TotalRequested)
299            {
300                    switch(state)
301                    {
302                            case TcpReadyToSend:
303                                    if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
304                                            state = TcpSend;
305                                    break;
306                                    
307                            case TcpSend:
308                                    TCPPut(tsock1, 0x02);
309                                    TCPFlush(tsock1);
310                                    state = TotalRequested;
311                                    break;
312                    }
313                    network_worker();
314            }
315                    
316                  case SockOpen:          while (state != TotalRequestedDone)
317                          if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )          {
318                          state = TcpSend;                  if (TCPIsGetReady(tsock1))
319                          return 0;                  {
320                                            TCPGet(tsock1, &ack);
321                  case TcpSend:                          if (ack == 0)
322                          for (i = net_INT; i >= 0; i--)                          {
323                          {                                  TCPDiscard(tsock1);
324                                  TCPPut(tsock1, termid );                                  state = TcpReadyToSend;
325                                    return 4;
326                          }                          }
327                            network_read_amount();
328                            TCPDiscard(tsock1);
329                            state = TotalRequestedDone;
330                    }
331                    network_worker();
332            }
333            
334            state = TcpReadyToSend;
335            return 0;
336    }      
337    
338    //Request ID 3
339    char network_send_cash_payed(unsigned long amount)
340    {
341            unsigned char ack;
342            while (state != ChangeRequested)
343            {
344                    switch(state)
345                    {
346                            case TcpReadyToSend:
347                                    if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
348                                            state = TcpSend;
349                                    break;
350                            case TcpSend:
351                                    TCPPut(tsock1, 0x03);
352                                    TCPPut(tsock1, amount>>24);
353                                    TCPPut(tsock1, amount>>16);
354                                    TCPPut(tsock1, amount>>8);
355                                    TCPPut(tsock1, amount);
356                                    TCPFlush(tsock1);
357                                    state = ChangeRequested;        
358                                    break;
359                    }
360                    network_worker();
361            }
362            
363            while (state != ChangeRequestedDone)
364            {
365                    if (TCPIsGetReady(tsock1))
366                    {
367                            TCPGet(tsock1, &ack);
368                            if (ack == 0)
369                            {
370                                    TCPFlush(tsock1);
371                                    state = TcpReadyToSend;
372                                    return 4;
373                            }
374                            network_read_amount();
375                          TCPFlush(tsock1);                          TCPFlush(tsock1);
376                          state = TcpReadyToSend;                          state = ChangeRequestedDone;
377                          return 0;                          
378                    }
379                    network_worker();
380          }          }
381          return 1;          state = TcpReadyToSend;
382            return 0;
383  }  }
384  /*  
385  void network_send_hello( unsigned char termid )  //request 4+5
386    char network_send_cancel_generic(char id)
387  {  {
388             if (DHCPIsBound())          while(state != CancelDone)
389          {          {
390                  RA3 = 0;                  switch (state)
391                  if (sock_open == 0)                  {
392                  {                          case TcpReadyToSend:
393                          if (ARPIsTxReady())                                  if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
394                          {                                          state = TcpSend;
395                                  ARPResolve( &rnode.IPAddr);                                  break;
396                                  is_resolved = 1;                          case TcpSend:
397                              }                                  TCPPut(tsock1, id);
398                                  else                                  TCPFlush(tsock1);
399                                  {                                  state = CancelDone;
400                                          if (ARPIsResolved( &rnode.IPAddr, &rnode.MACAddr))  
                                         //if (MyArp( &rnode))  
                                         {  
                                                 usock1=UDPOpen(2000,&rnode,3000); // socket to send UDP  
                                                 sock_open=1;  
                                         }  
                                 }        
                         }  
                 }  
                   
                 if (UDPIsPutReady(usock1) && datagrams_to_send>0)  
                 {  
                         UDPPut( rnode.MACAddr.v[0] );  
                         UDPPut( rnode.MACAddr.v[1] );  
                         UDPPut( rnode.MACAddr.v[2] );  
                         UDPPut( rnode.MACAddr.v[3] );  
                         UDPPut( rnode.MACAddr.v[4] );  
                         UDPPut( rnode.MACAddr.v[5] );  
                         UDPPut('D');  
                         UDPPut('a');  
                         UDPPut('w');  
                         UDPPut(0);  
                         UDPFlush();  
                         datagrams_to_send--;  
                 }  
401                  }                  }
402                    network_worker();
403            }
404            return network_read_ack();
405    }      
406    
407    //Request ID 4
408    char network_send_cancel_last(void)
409    {
410            return network_send_cancel_generic(0x04);      
411  }  }
412  */  
413    //Request ID 5
414    char network_send_cancel_all(void)
415    {
416            return network_send_cancel_generic(0x05);
417    }      
418    
419    //Request ID 6
420    char network_send_goodbye(void)
421    {
422            char ack;
423            while( state != SockClosed)
424            {
425                    switch (state)
426                    {
427                            case TcpReadyToSend:
428                                    if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
429                                            state = TcpSend;
430                                    break;
431                                    
432                            case TcpSend:
433                                    TCPPut( tsock1, 0x06 );
434                                    TCPFlush( tsock1 );
435                                    state = SockClosing;
436                                    break;
437                            
438                            case SockClosing:
439                                    ack = network_read_ack();
440                                    TCPDisconnect( tsock1 );
441                                    state = SockClosed;
442                                    break;
443                    }
444                    network_worker();
445            }
446            if( TCPIsConnected(tsock1))
447                    return 3;                               // Didn't close the socket properly;
448            else
449                    return ack;
450            
451    }      
452    void network_wait_for_dhcp()
453    {
454            while (!DHCPIsBound())
455                    network_worker();
456    }      
457    
458  void main(void)  void main(void)
459  {  {
# Line 213  void main(void) Line 463  void main(void)
463      char sock_state = 0;      char sock_state = 0;
464      char is_resolved = 0;      char is_resolved = 0;
465      char has_arp = 0;      char has_arp = 0;
466      char a;  
467                    
468      TICK tick;      TICK tick;
469      TICK lastTick;      TICK lastTick;
# Line 264  void main(void) Line 514  void main(void)
514    
515      while(1)      while(1)
516      {      {
517                unsigned char antal = 8;
518                unsigned char stregkode[] = "12345678";
519          /*          /*
520           * Blink SYSTEM LED every second.           * Blink SYSTEM LED every second.
521           */           */
# Line 284  void main(void) Line 536  void main(void)
536           * For DHCP information, display how many times we have renewed the IP           * For DHCP information, display how many times we have renewed the IP
537           * configuration since last reset.           * configuration since last reset.
538           */           */
539            
540          a = network_send_hello( 1 );          
541     /*     if (DHCPIsBound())                      network_send_hello( 0x01 );
542          {                      
543                  RA3 =0;                      strncpy(global_network_char, "12345678", 8);
544                  if (has_arp == 0)                          network_send_scan_frame( 1, 8 ); //1x vare, stregkodelængde=8
545                  {                          
546                          if (ARPIsTxReady())                          strncpy(global_network_char, "87654444", 8);
547                          {                          network_send_scan_frame( 1, 8 ); //1x vare, stregkodelængde=8
548                                  if (is_resolved == 0)                          
549                                  {                          network_send_calc_total();
550                                          ARPResolve( &rnode.IPAddr);                          
551                                          is_resolved = 1;                          network_send_cash_payed(50000);
552                                  }                          
553                                  else                          network_send_cancel_last();
554                                  {                          
555                                          if (ARPIsResolved( &rnode.IPAddr, &rnode.MACAddr))                          network_send_cancel_all();
556                                          {                                                  
557                                                          has_arp = 1;                          network_send_goodbye();
                                         }  
                                 }  
           
                         }  
                 }  
                   
                 if (has_arp == 1)  
                 {  
                         if (sock_state == 0)  
                         {  
                         usock1=TCPConnect(&rnode,3000);  
                                 sock_state = 1;  
                         }  
                 /*      if (sock_state ==1 && TCPIsConnected(usock1) && TCPIsPutReady(usock1))  
                         {  
                                 TCPPut(usock1,'D');  
                                 TCPPut(usock1,'a');  
                                 TCPPut(usock1,'w');  
                                 TCPPut(usock1,0);  
                         TCPFlush(usock1);  
                                 sock_state = 2;  
                         }  
                   
                         if (sock_state == 2 && TCPIsPutReady(usock1))  
                     {  
                                 TCPDisconnect(usock1);  
                                 lastTick = TickGet();  
                                 sock_state =3;  
                         }  
                         tick = TickGet();  
                         diffTicks = TickGetDiff(tick, lastTick);  
                           
                         if ( sock_state == 3 && diffTicks > ((TICK)TICK_SECOND * (TICK)2) )  
                                 sock_state = 0;  
                                   
                     }  
         } */  
558      }      }
559  }  }
560    
561  #if defined(MCHP_C18)  void interrupt HighISR(void)
     #pragma interrupt HighISR save=section(".tmpdata")  
     void HighISR(void)  
 #elif defined(HITECH_C18)  
     #if defined(STACK_USE_SLIP)  
         extern void MACISR(void);  
     #endif  
     void interrupt HighISR(void)  
 #endif  
562  {  {
563      TickUpdate();      TickUpdate();
 /*  
 #if defined(STACK_USE_SLIP)  
     MACISR();  
 #endif  
 */  
564  }  }
565    
566  #if defined(MCHP_C18)  #if defined(MCHP_C18)

Legend:
Removed from v.96  
changed lines
  Added in v.105

  ViewVC Help
Powered by ViewVC 1.1.20