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