/[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 98 by hedin, Tue May 22 14:45:39 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)
# Line 106  void network_init(void) Line 120  void network_init(void)
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 gotArp = 0;                // if gotArp is 0, the embedded haven't talked to the server since boot.          char gotArp = 0;                // if gotArp is 0, the embedded haven't talked to the server since boot.
177  static char isBound = 0;          static char isBound = 0;
178    
179            while( !DHCPIsBound() && isBound == 0 )
180                    network_worker();
181    
182          if ( DHCPIsBound() && isBound == 0 ){          if ( DHCPIsBound() && isBound == 0 ){
183                  state = DhcpIsBound;                  state = DhcpIsBound;
184                  isBound = 1;                  isBound = 1;
185          }          }
186          switch (state)          
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 = ArpIsResolved;                                          state = ArpIsTxReady;
194                          return 0;                                  else
                   
                 case ArpIsTxReady:  
                         ARPResolve(&rnode.IPAddr);  
                         gotArp = 1;  
                         state = ArpIsResolved;  
                         return 0;  
                                           
                 case ArpIsResolved:  
                         if (ARPIsResolved(&rnode.IPAddr, &rnode.MACAddr) ){  
                                 state = SockOpening;  
                                 return 0;  
                                 }  
                                 else{  
195                                          state = ArpIsResolved;                                          state = ArpIsResolved;
196                                          return 2;                                                                       // no arp resolved                                  break;
197                            
198                            case ArpIsTxReady:
199                                    ARPResolve(&rnode.IPAddr);
200                                    gotArp = 1;
201                                    state = ArpIsResolved;
202                                    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 SockOpening:                          case SockOpen:
216                          tsock1 = TCPConnect (&rnode, 3000);                                  if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
217                          state = SockOpen;                                          state = TcpSend;
218                          return 0;                                  break;
           
                 case SockOpen:  
                         if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )  
                         state = TcpSend;  
                         return 0;  
                   
                 case TcpSend:  
                         TCPPut( tsock1, 0x00 );  
                         TCPPut( tsock1, termid );  
                         TCPFlush(tsock1);  
                         TCPDiscard( tsock1 );  
219                                                    
220                            case TcpSend:
221                                    TCPPut( tsock1, 0x00 );
222                                    TCPPut( tsock1, termid );
223                                    TCPFlush(tsock1);
224                                    //TCPDiscard( tsock1 );
225                                    state = HelloDone;
226                                    break;
227                    }
228                    network_worker();
229            }
230            return network_read_ack();
231    }
232    
233                          state = TcpReadyToSend;  //Request ID 1
234                          return 0;  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                                    TCPFlush(tsock1);
255                                    state = StregkodeSendDone;
256                                    break;
257                    }
258                    network_worker();
259          }          }
260          return 1;          
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                                    network_read_amount();
280                                    TCPGet( tsock1, &global_network_charlen);
281                                    TCPGetArray( tsock1, global_network_char, global_network_charlen);
282                                    TCPDiscard(tsock1);
283                                    
284                                    state = StregkodeRecieveDone;                          
285                                            
286                                    break;
287                    }
288                    network_worker();
289            }
290            state = TcpReadyToSend;
291            return 0;
292  }  }
293    
294  char network_send_scan_frame( unsigned char stregkode, unsigned char buflen )  //Request ID 2
295    char network_send_calc_total(void)
296  {  {
297          switch (state)          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            while (state != TotalRequestedDone)
317          {          {
318                          TCPPut( tsock1, 0x01 );                  if (TCPIsGetReady(tsock1))
319                          for (i = buflen; i >= 0; i--)                  {
320                          {                          TCPGet(tsock1, &ack);
321                                  TCPPut(tsock1, stregkode );                          if (ack == 0)
322                            {
323                                    TCPDiscard(tsock1);
324                                    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 1;                          
378                    }
379                    network_worker();
380          }          }
381            state = TcpReadyToSend;
382            return 0;
383    }
384    
385    //request 4+5
386    char network_send_cancel_generic(char id)
387    {
388            while(state != CancelDone)
389            {
390                    switch (state)
391                    {
392                            case TcpReadyToSend:
393                                    if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
394                                            state = TcpSend;
395                                    break;
396                            case TcpSend:
397                                    TCPPut(tsock1, id);
398                                    TCPFlush(tsock1);
399                                    state = CancelDone;
400    
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)  char network_send_goodbye(void)
421  {  {
422          switch (state)          char ack;
423            while( state != SockClosed)
424          {          {
425                  case TcpReadyToSend:                  switch (state)
426                          if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )                  {
427                          state = TcpSend;                          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 TcpSend:                          case SockClosing:
439                          TCPPut( tsock1, 0x06 );                                  ack = network_read_ack();
440                          TCPFlush( tsock1 );                                  TCPDisconnect( tsock1 );
441                          state = SockClosing;                                  state = SockClosed;
442                          return 0;                                  break;
443                                    }
444                  case SockClosing:                  network_worker();
                         TCPDisconnect( tsock1 );  
                         state = SockClosed;  
                         return 1;  
445          }          }
446          return 3;                               // Didn't close the socket properly;          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)
# Line 206  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 279  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( 0x01 );          
541  //              a = network_send_scan_frame( stregkode, antal );                      network_send_hello( 0x01 );
542                  a = network_send_goodbye();                      
543                        strncpy(global_network_char, "12345678", 8);
544                            network_send_scan_frame( 1, 8 ); //1x vare, stregkodelængde=8
545                            
546                            strncpy(global_network_char, "87654444", 8);
547                            network_send_scan_frame( 1, 8 ); //1x vare, stregkodelængde=8
548                            
549                            network_send_calc_total();
550                            
551                            network_send_cash_payed(50000);
552                            
553                            network_send_cancel_last();
554                            
555                            network_send_cancel_all();
556                                                    
557                            network_send_goodbye();
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.98  
changed lines
  Added in v.105

  ViewVC Help
Powered by ViewVC 1.1.20