/[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 105 by hedin, Sun May 27 16:32:30 2007 UTC revision 106 by hedin, Mon May 28 08:26:29 2007 UTC
# Line 1  Line 1 
1  #define THIS_IS_STACK_APPLICATION  #define THIS_IS_STACK_APPLICATION
2    
3  #include "stacktsk.h"  #include "stacktsk.h"
 #include "dhcp.h"  
4  #include "tick.h"  #include "tick.h"
5  #include "delay.h"  #include "delay.h"
6  #include "udp.h"  #include "network.h"
 #include "arp.h"  
 #include "arptsk.h"  
 #include "tcp.h"  
7    
8  #include <string.h>  #include <string.h>
9    
 bit bIsDhcpUp = 0;  
 char netlevel = 0;  
10    
 // All TFTP command statuts display will be done on first line of LCD.  
 //#define TFTP_COMMAND_DISPLAY_LINE       0  
   
 // Result will be displayed at y = 15.  
 #define TFTP_COMMAND_RESULT_POSITION    15  
   
   
 /*#define STARTUP_MSG "G1_Build_0x00"  
   
 ROM char StartupMsg[] = STARTUP_MSG;  
   
 #if defined(STACK_USE_DHCP) || defined(STACK_USE_IP_GLEANING)  
 ROM char DHCPMsg[] = "DHCP/Gleaning...";  
 #endif  
   
 ROM char SetupMsg[] = "Board Setup...";  
   
 //                         1234567890123456  
 //ROM char blankLCDLine[] = "                ";  
 */  
11  /*  /*
12   * This is used by other stack elements.   * This is used by other stack elements.
13   * Main application must define this and initialize it with   * Main application must define this and initialize it with
# Line 71  static void InitAppConfig(void); Line 45  static void InitAppConfig(void);
45  static void InitializeBoard(void);  static void InitializeBoard(void);
46  static void SetConfig(void);  static void SetConfig(void);
47    
 /*****************************  
  Enummerations  
  ****************************/  
 typedef enum _NetworkState  
 {  
         off,  
         DhcpIsBound,  
         ArpIsTxReady,  
         ArpIsResolved,  
         SockOpening,  
         SockOpen,  
         TcpReadyToSend,  
         TcpSend,  
         HelloDone,  
         StregkodeSendDone,  
         ReadyToRecieve,  
         StregkodeRecieveDone,  
         TotalRequested,  
         TotalRequestedDone,  
         ChangeRequested,  
         ChangeRequestedDone,  
         Acknowledged,  
         CancelDone,  
         SockClosing,  
         SockClosed  
 } NetworkState;  
   
 /*****************************  
  GLOBAL INIT  
  ****************************/  
 NODE_INFO rnode;  
 NetworkState state;  
 TCP_SOCKET tsock1;  
   
   
 /*****************************  
  network funktions  
  ****************************/  
 unsigned long global_network_amount;  
 unsigned char global_network_char[16];  
 unsigned char global_network_charlen;  
   
   
 void network_init(void)  
 {  
         state = off;  
 }        
   
   
 void network_worker(void)  
 {      
         TickGet();  
     StackTask();      
 }        
   
 void network_parse_amount(unsigned char buf[])  
 {  
         global_network_amount = buf[0]<<24 + buf[1]<<16 + buf[2]<<8 + buf[3];  
 }        
   
 void network_read_amount(void)  
 {  
         unsigned char buf[4];  
         TCPGetArray(tsock1, buf,4);  
         network_parse_amount(buf);  
 }        
   
 void network_wait(unsigned char n)  
 {  
         int i;  
         for (i=0; i<n; i++)  
         {  
                 network_worker();  
                 Delay10us(1);  
         }  
 }        
   
 char network_read_ack(void)  
 {  
         char ack;  
         while(state != Acknowledged)  
         {  
                 if (TCPIsGetReady(tsock1))  
                 {  
                         TCPGet(tsock1, &ack);  
                         TCPDiscard(tsock1);  
                         state = Acknowledged;  
                 }  
                 network_worker();  
         }  
           
         state = TcpReadyToSend;  
           
         if (ack == 1)  
                 return 0;  
         else  
                 return 4;  
 }        
   
 //Request ID 0  
 char network_send_hello( unsigned char termid )  
 {  
         char gotArp = 0;                // if gotArp is 0, the embedded haven't talked to the server since boot.  
         static char isBound = 0;  
   
         while( !DHCPIsBound() && isBound == 0 )  
                 network_worker();  
   
         if ( DHCPIsBound() && isBound == 0 ){  
                 state = DhcpIsBound;  
                 isBound = 1;  
         }  
           
         while( state != HelloDone )  
         {  
                 switch (state)  
                 {  
                         case DhcpIsBound:  
                                 if ( ARPIsTxReady() && gotArp == 0 )  
                                         state = ArpIsTxReady;  
                                 else  
                                         state = ArpIsResolved;  
                                 break;  
                           
                         case ArpIsTxReady:  
                                 ARPResolve(&rnode.IPAddr);  
                                 gotArp = 1;  
                                 state = ArpIsResolved;  
                                 break;  
                                                   
                         case ArpIsResolved:  
                                 if ( ARPIsResolved(&rnode.IPAddr, &rnode.MACAddr) ){  
                                         state = SockOpening;  
                                 }  
                                 break;  
                           
                         case SockOpening:  
                                 tsock1 = TCPConnect (&rnode, 3000);  
                                 state = SockOpen;  
                                 break;  
                   
                         case SockOpen:  
                                 if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )  
                                         state = TcpSend;  
                                 break;  
                           
                         case TcpSend:  
                                 TCPPut( tsock1, 0x00 );  
                                 TCPPut( tsock1, termid );  
                                 TCPFlush(tsock1);  
                                 //TCPDiscard( tsock1 );  
                                 state = HelloDone;  
                                 break;  
                 }  
                 network_worker();  
         }  
         return network_read_ack();  
 }  
   
 //Request ID 1  
 char network_send_scan_frame( unsigned char antal, unsigned char buflen )  
 {  
         char i;  
         char ack;  
48    
         while( state != StregkodeSendDone ) //Sender vare-request-info pakke  
         {  
                 switch (state)  
                 {  
                         case TcpReadyToSend:  
                                 if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )  
                                 state = TcpSend;  
                         case TcpSend:  
                                 TCPPut( tsock1, 0x01 );  
                                 TCPPut( tsock1, antal );  
                                 TCPPut( tsock1, buflen );  
                                 for (i = 0; i < buflen; i++)  
                                 {  
                                         TCPPut(tsock1, global_network_char[i] );  
                                 }  
                                 TCPFlush(tsock1);  
                                 state = StregkodeSendDone;  
                                 break;  
                 }  
                 network_worker();  
         }  
           
         while( state != StregkodeRecieveDone ) //venter på svar og parser denne  
         {  
                 switch( state )  
                 {  
                         case StregkodeSendDone:  
                                 if( TCPIsGetReady(tsock1) )  
                                         state = ReadyToRecieve;  
                                 break;  
                         case ReadyToRecieve:  
                                 TCPGet( tsock1, &ack );  
                                   
                                 if (ack == 0)  
                                 {  
                                         state = TcpReadyToSend;  
                                         TCPDiscard(tsock1);  
                                         return 4;  
                                 }  
                                   
                                 network_read_amount();  
                                 TCPGet( tsock1, &global_network_charlen);  
                                 TCPGetArray( tsock1, global_network_char, global_network_charlen);  
                                 TCPDiscard(tsock1);  
                                   
                                 state = StregkodeRecieveDone;                            
                                           
                                 break;  
                 }  
                 network_worker();  
         }  
         state = TcpReadyToSend;  
         return 0;  
 }  
   
 //Request ID 2  
 char network_send_calc_total(void)  
 {  
         unsigned char ack;  
         while(state != TotalRequested)  
         {  
                 switch(state)  
                 {  
                         case TcpReadyToSend:  
                                 if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )  
                                         state = TcpSend;  
                                 break;  
                                   
                         case TcpSend:  
                                 TCPPut(tsock1, 0x02);  
                                 TCPFlush(tsock1);  
                                 state = TotalRequested;  
                                 break;  
                 }  
                 network_worker();  
         }  
           
         while (state != TotalRequestedDone)  
         {  
                 if (TCPIsGetReady(tsock1))  
                 {  
                         TCPGet(tsock1, &ack);  
                         if (ack == 0)  
                         {  
                                 TCPDiscard(tsock1);  
                                 state = TcpReadyToSend;  
                                 return 4;  
                         }  
                         network_read_amount();  
                         TCPDiscard(tsock1);  
                         state = TotalRequestedDone;  
                 }  
                 network_worker();  
         }  
           
         state = TcpReadyToSend;  
         return 0;  
 }        
   
 //Request ID 3  
 char network_send_cash_payed(unsigned long amount)  
 {  
         unsigned char ack;  
         while (state != ChangeRequested)  
         {  
                 switch(state)  
                 {  
                         case TcpReadyToSend:  
                                 if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )  
                                         state = TcpSend;  
                                 break;  
                         case TcpSend:  
                                 TCPPut(tsock1, 0x03);  
                                 TCPPut(tsock1, amount>>24);  
                                 TCPPut(tsock1, amount>>16);  
                                 TCPPut(tsock1, amount>>8);  
                                 TCPPut(tsock1, amount);  
                                 TCPFlush(tsock1);  
                                 state = ChangeRequested;          
                                 break;  
                 }  
                 network_worker();  
         }  
           
         while (state != ChangeRequestedDone)  
         {  
                 if (TCPIsGetReady(tsock1))  
                 {  
                         TCPGet(tsock1, &ack);  
                         if (ack == 0)  
                         {  
                                 TCPFlush(tsock1);  
                                 state = TcpReadyToSend;  
                                 return 4;  
                         }  
                         network_read_amount();  
                         TCPFlush(tsock1);  
                         state = ChangeRequestedDone;  
                           
                 }  
                 network_worker();  
         }  
         state = TcpReadyToSend;  
         return 0;  
 }  
   
 //request 4+5  
 char network_send_cancel_generic(char id)  
 {  
         while(state != CancelDone)  
         {  
                 switch (state)  
                 {  
                         case TcpReadyToSend:  
                                 if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )  
                                         state = TcpSend;  
                                 break;  
                         case TcpSend:  
                                 TCPPut(tsock1, id);  
                                 TCPFlush(tsock1);  
                                 state = CancelDone;  
   
                 }  
                 network_worker();  
         }  
         return network_read_ack();  
 }        
   
 //Request ID 4  
 char network_send_cancel_last(void)  
 {  
         return network_send_cancel_generic(0x04);        
 }  
   
 //Request ID 5  
 char network_send_cancel_all(void)  
 {  
         return network_send_cancel_generic(0x05);  
 }        
   
 //Request ID 6  
 char network_send_goodbye(void)  
 {  
         char ack;  
         while( state != SockClosed)  
         {  
                 switch (state)  
                 {  
                         case TcpReadyToSend:  
                                 if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )  
                                         state = TcpSend;  
                                 break;  
                                   
                         case TcpSend:  
                                 TCPPut( tsock1, 0x06 );  
                                 TCPFlush( tsock1 );  
                                 state = SockClosing;  
                                 break;  
                           
                         case SockClosing:  
                                 ack = network_read_ack();  
                                 TCPDisconnect( tsock1 );  
                                 state = SockClosed;  
                                 break;  
                 }  
                 network_worker();  
         }  
         if( TCPIsConnected(tsock1))  
                 return 3;                               // Didn't close the socket properly;  
         else  
                 return ack;  
           
 }        
 void network_wait_for_dhcp()  
 {  
         while (!DHCPIsBound())  
                 network_worker();  
 }        
49    
50  void main(void)  void main(void)
51  {  {
52      // Tick to blink SYSTEM led.      // Tick to blink SYSTEM led.
53      static TICK t = 0;      static TICK t = 0;
     TCP_SOCKET usock1;  
     char sock_state = 0;  
     char is_resolved = 0;  
     char has_arp = 0;  
54    
55                    
56      TICK tick;      TICK tick;
# Line 514  void main(void) Line 102  void main(void)
102    
103      while(1)      while(1)
104      {      {
             unsigned char antal = 8;  
             unsigned char stregkode[] = "12345678";  
105          /*          /*
106           * Blink SYSTEM LED every second.           * Blink SYSTEM LED every second.
107           */           */
# Line 563  void interrupt HighISR(void) Line 149  void interrupt HighISR(void)
149      TickUpdate();      TickUpdate();
150  }  }
151    
152  #if defined(MCHP_C18)  
 #pragma code highVector=0x08  
 void HighVector (void)  
 {  
     _asm goto HighISR _endasm  
 }  
 #pragma code /* return to default code section */  
 #endif  
153    
154  /*********************************************************************  /*********************************************************************
155   * Function:        void InitializeBoard(void)   * Function:        void InitializeBoard(void)

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

  ViewVC Help
Powered by ViewVC 1.1.20