/[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 102 by hedin, Fri May 25 16:00:14 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;  char netlevel = 0;
16    
# Line 83  typedef enum _NetworkState Line 85  typedef enum _NetworkState
85          TcpReadyToSend,          TcpReadyToSend,
86          TcpSend,          TcpSend,
87          HelloDone,          HelloDone,
88          StregkodeDone,          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 99  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 115  void network_worker(void) Line 126  void network_worker(void)
126      StackTask();          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)  void network_wait(unsigned char n)
142  {  {
143          int i;          int i;
144          for (i=0; i<n; i++)          for (i=0; i<n; i++)
145          {          {
146                  network_worker();                  network_worker();
147                  DelayMs(1);                  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;
   
 while( !DHCPIsBound() && isBound == 0 )  
         network_worker();  
178    
179  //      while( state != DhcpIsBound ){          while( !DHCPIsBound() && isBound == 0 )
180                  if ( DHCPIsBound() && isBound == 0 ){                  network_worker();
                         state = DhcpIsBound;  
                         isBound = 1;  
 //              }  
181    
182            if ( DHCPIsBound() && isBound == 0 ){
183                    state = DhcpIsBound;
184                    isBound = 1;
185          }          }
186            
187          while( state != HelloDone )          while( state != HelloDone )
188          {          {
189                  switch (state)                  switch (state)
# Line 180  while( !DHCPIsBound() && isBound == 0 ) Line 223  while( !DHCPIsBound() && isBound == 0 )
223                                  TCPFlush(tsock1);                                  TCPFlush(tsock1);
224                                  //TCPDiscard( tsock1 );                                  //TCPDiscard( tsock1 );
225                                  state = HelloDone;                                  state = HelloDone;
                                 network_wait(5);  
226                                  break;                                  break;
227                  }                  }
228                  network_worker();                  network_worker();
229          }          }
230          return 0;          return network_read_ack();
231  }  }
232    
233  char network_send_scan_frame( unsigned char stregkode[], unsigned char buflen )  //Request ID 1
234    char network_send_scan_frame( unsigned char antal, unsigned char buflen )
235  {  {
236          while( state != StregkodeDone )          char i;
237            char ack;
238    
239            while( state != StregkodeSendDone ) //Sender vare-request-info pakke
240          {          {
241                  switch (state)                  switch (state)
242                  {                  {
243                          case HelloDone:                          case TcpReadyToSend:
244                                  if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )                                  if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
245                                  state = TcpSend;                                  state = TcpSend;
246                          case TcpSend:                          case TcpSend:
247                                  TCPPut( tsock1, 0x01 );                                  TCPPut( tsock1, 0x01 );
248                                    TCPPut( tsock1, antal );
249                                    TCPPut( tsock1, buflen );
250                                  for (i = 0; i < buflen; i++)                                  for (i = 0; i < buflen; i++)
251                                  {                                  {
252                                          TCPPut(tsock1, stregkode[i] );                                          TCPPut(tsock1, global_network_char[i] );
253                                  }                                  }
254                                  TCPFlush(tsock1);                                  TCPFlush(tsock1);
255                                  state = StregkodeDone;                                  state = StregkodeSendDone;
256                                    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                                    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;                                  break;
287                  }                  }
288                  network_worker();                  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            while (state != TotalRequestedDone)
317            {
318                    if (TCPIsGetReady(tsock1))
319                    {
320                            TCPGet(tsock1, &ack);
321                            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;          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);
376                            state = ChangeRequestedDone;
377                            
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            char ack;
423          while( state != SockClosed)          while( state != SockClosed)
424          {          {
425                  switch (state)                  switch (state)
426                  {                  {
427                          case TcpReadyToSend:                          case TcpReadyToSend:
428                                  if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )                                  if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
429                                  state = TcpSend;                                          state = TcpSend;
430                                  break;                                  break;
431                                                                    
432                          case TcpSend:                          case TcpSend:
# Line 229  char network_send_goodbye(void) Line 436  char network_send_goodbye(void)
436                                  break;                                  break;
437                                                    
438                          case SockClosing:                          case SockClosing:
439                                    ack = network_read_ack();
440                                  TCPDisconnect( tsock1 );                                  TCPDisconnect( tsock1 );
441                                  state = SockClosed;                                  state = SockClosed;
442                                  break;                                  break;
443                  }                  }
444                  network_worker();                  network_worker();
445          }          }
446          if( TCPIsConnected(tsock1) )          if( TCPIsConnected(tsock1))
447                  return 3;                               // Didn't close the socket properly;                  return 3;                               // Didn't close the socket properly;
448          else          else
449                  return 0;                  return ack;
450                    
451  }        }      
452  void network_wait_for_dhcp()  void network_wait_for_dhcp()
# Line 255  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;
     char a = 1;  
466    
467                    
468      TICK tick;      TICK tick;
# Line 330  void main(void) Line 537  void main(void)
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    

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

  ViewVC Help
Powered by ViewVC 1.1.20