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

Annotation of /trunk/PIC/Demo trimmet/network.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 106 - (hide annotations) (download)
Mon May 28 08:26:29 2007 UTC (16 years, 11 months ago) by hedin
File MIME type: text/plain
File size: 6902 byte(s)
Refactored network module to seperate files.

Done some cleanup in the main module

(Done by Torben)
1 hedin 106
2     #include "stacktsk.h"
3     #include "dhcp.h"
4     #include "tick.h"
5     #include "delay.h"
6     #include "udp.h"
7     #include "arp.h"
8     #include "arptsk.h"
9     #include "tcp.h"
10     #include "network.h"
11    
12    
13    
14    
15     /*****************************
16     GLOBAL vars
17     ****************************/
18     NODE_INFO rnode;
19     NetworkState state;
20     TCP_SOCKET tsock1;
21    
22    
23     unsigned long global_network_amount;
24     unsigned char global_network_char[16];
25     unsigned char global_network_charlen;
26    
27    
28     /*****************************
29     network funktions
30     ****************************/
31    
32    
33     void network_init(void)
34     {
35     state = off;
36     }
37    
38    
39     void network_worker(void)
40     {
41     TickGet();
42     StackTask();
43     }
44    
45     void network_parse_amount(unsigned char buf[])
46     {
47     global_network_amount = buf[0]<<24 + buf[1]<<16 + buf[2]<<8 + buf[3];
48     }
49    
50     void network_read_amount(void)
51     {
52     unsigned char buf[4];
53     TCPGetArray(tsock1, buf,4);
54     network_parse_amount(buf);
55     }
56    
57     void network_wait(unsigned char n)
58     {
59     int i;
60     for (i=0; i<n; i++)
61     {
62     network_worker();
63     Delay10us(1);
64     }
65     }
66    
67     char network_read_ack(void)
68     {
69     char ack;
70     while(state != Acknowledged)
71     {
72     if (TCPIsGetReady(tsock1))
73     {
74     TCPGet(tsock1, &ack);
75     TCPDiscard(tsock1);
76     state = Acknowledged;
77     }
78     network_worker();
79     }
80    
81     state = TcpReadyToSend;
82    
83     if (ack == 1)
84     return 0;
85     else
86     return 4;
87     }
88    
89     //Request ID 0
90     char network_send_hello( unsigned char termid )
91     {
92     char gotArp = 0; // if gotArp is 0, the embedded haven't talked to the server since boot.
93     static char isBound = 0;
94    
95     while( !DHCPIsBound() && isBound == 0 )
96     network_worker();
97    
98     if ( DHCPIsBound() && isBound == 0 ){
99     state = DhcpIsBound;
100     isBound = 1;
101     }
102    
103     while( state != HelloDone )
104     {
105     switch (state)
106     {
107     case DhcpIsBound:
108     if ( ARPIsTxReady() && gotArp == 0 )
109     state = ArpIsTxReady;
110     else
111     state = ArpIsResolved;
112     break;
113    
114     case ArpIsTxReady:
115     ARPResolve(&rnode.IPAddr);
116     gotArp = 1;
117     state = ArpIsResolved;
118     break;
119    
120     case ArpIsResolved:
121     if ( ARPIsResolved(&rnode.IPAddr, &rnode.MACAddr) ){
122     state = SockOpening;
123     }
124     break;
125    
126     case SockOpening:
127     tsock1 = TCPConnect (&rnode, 3000);
128     state = SockOpen;
129     break;
130    
131     case SockOpen:
132     if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
133     state = TcpSend;
134     break;
135    
136     case TcpSend:
137     TCPPut( tsock1, 0x00 );
138     TCPPut( tsock1, termid );
139     TCPFlush(tsock1);
140     //TCPDiscard( tsock1 );
141     state = HelloDone;
142     break;
143     }
144     network_worker();
145     }
146     return network_read_ack();
147     }
148    
149     //Request ID 1
150     char network_send_scan_frame( unsigned char antal, unsigned char buflen )
151     {
152     char i;
153     char ack;
154    
155     while( state != StregkodeSendDone ) //Sender vare-request-info pakke
156     {
157     switch (state)
158     {
159     case TcpReadyToSend:
160     if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
161     state = TcpSend;
162     case TcpSend:
163     TCPPut( tsock1, 0x01 );
164     TCPPut( tsock1, antal );
165     TCPPut( tsock1, buflen );
166     for (i = 0; i < buflen; i++)
167     {
168     TCPPut(tsock1, global_network_char[i] );
169     }
170     TCPFlush(tsock1);
171     state = StregkodeSendDone;
172     break;
173     }
174     network_worker();
175     }
176    
177     while( state != StregkodeRecieveDone ) //venter på svar og parser denne
178     {
179     switch( state )
180     {
181     case StregkodeSendDone:
182     if( TCPIsGetReady(tsock1) )
183     state = ReadyToRecieve;
184     break;
185     case ReadyToRecieve:
186     TCPGet( tsock1, &ack );
187    
188     if (ack == 0)
189     {
190     state = TcpReadyToSend;
191     TCPDiscard(tsock1);
192     return 4;
193     }
194    
195     network_read_amount();
196     TCPGet( tsock1, &global_network_charlen);
197     TCPGetArray( tsock1, global_network_char, global_network_charlen);
198     TCPDiscard(tsock1);
199    
200     state = StregkodeRecieveDone;
201    
202     break;
203     }
204     network_worker();
205     }
206     state = TcpReadyToSend;
207     return 0;
208     }
209    
210     //Request ID 2
211     char network_send_calc_total(void)
212     {
213     unsigned char ack;
214     while(state != TotalRequested)
215     {
216     switch(state)
217     {
218     case TcpReadyToSend:
219     if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
220     state = TcpSend;
221     break;
222    
223     case TcpSend:
224     TCPPut(tsock1, 0x02);
225     TCPFlush(tsock1);
226     state = TotalRequested;
227     break;
228     }
229     network_worker();
230     }
231    
232     while (state != TotalRequestedDone)
233     {
234     if (TCPIsGetReady(tsock1))
235     {
236     TCPGet(tsock1, &ack);
237     if (ack == 0)
238     {
239     TCPDiscard(tsock1);
240     state = TcpReadyToSend;
241     return 4;
242     }
243     network_read_amount();
244     TCPDiscard(tsock1);
245     state = TotalRequestedDone;
246     }
247     network_worker();
248     }
249    
250     state = TcpReadyToSend;
251     return 0;
252     }
253    
254     //Request ID 3
255     char network_send_cash_payed(unsigned long amount)
256     {
257     unsigned char ack;
258     while (state != ChangeRequested)
259     {
260     switch(state)
261     {
262     case TcpReadyToSend:
263     if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
264     state = TcpSend;
265     break;
266     case TcpSend:
267     TCPPut(tsock1, 0x03);
268     TCPPut(tsock1, amount>>24);
269     TCPPut(tsock1, amount>>16);
270     TCPPut(tsock1, amount>>8);
271     TCPPut(tsock1, amount);
272     TCPFlush(tsock1);
273     state = ChangeRequested;
274     break;
275     }
276     network_worker();
277     }
278    
279     while (state != ChangeRequestedDone)
280     {
281     if (TCPIsGetReady(tsock1))
282     {
283     TCPGet(tsock1, &ack);
284     if (ack == 0)
285     {
286     TCPFlush(tsock1);
287     state = TcpReadyToSend;
288     return 4;
289     }
290     network_read_amount();
291     TCPFlush(tsock1);
292     state = ChangeRequestedDone;
293    
294     }
295     network_worker();
296     }
297     state = TcpReadyToSend;
298     return 0;
299     }
300    
301     //request 4+5
302     char network_send_cancel_generic(char id)
303     {
304     while(state != CancelDone)
305     {
306     switch (state)
307     {
308     case TcpReadyToSend:
309     if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
310     state = TcpSend;
311     break;
312     case TcpSend:
313     TCPPut(tsock1, id);
314     TCPFlush(tsock1);
315     state = CancelDone;
316    
317     }
318     network_worker();
319     }
320     return network_read_ack();
321     }
322    
323     //Request ID 4
324     char network_send_cancel_last(void)
325     {
326     return network_send_cancel_generic(0x04);
327     }
328    
329     //Request ID 5
330     char network_send_cancel_all(void)
331     {
332     return network_send_cancel_generic(0x05);
333     }
334    
335     //Request ID 6
336     char network_send_goodbye(void)
337     {
338     char ack;
339     while( state != SockClosed)
340     {
341     switch (state)
342     {
343     case TcpReadyToSend:
344     if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
345     state = TcpSend;
346     break;
347    
348     case TcpSend:
349     TCPPut( tsock1, 0x06 );
350     TCPFlush( tsock1 );
351     state = SockClosing;
352     break;
353    
354     case SockClosing:
355     ack = network_read_ack();
356     TCPDisconnect( tsock1 );
357     state = SockClosed;
358     break;
359     }
360     network_worker();
361     }
362     if( TCPIsConnected(tsock1))
363     return 3; // Didn't close the socket properly;
364     else
365     return ack;
366    
367     }
368     void network_wait_for_dhcp()
369     {
370     while (!DHCPIsBound())
371     network_worker();
372     }
373    

  ViewVC Help
Powered by ViewVC 1.1.20