annotate network/network.c @ 107:56d09a0969b5 avr-http

Import uIP and the PPP implementation from https://code.google.com/p/avrusbmodem/
author Matt Johnston <matt@ucc.asn.au>
date Fri, 07 Sep 2012 23:53:53 +0800
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
107
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 #define INCLUDE_FROM_NETWORK_C
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 #include "network.h"
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 static bool Escape = false;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 static uint8_t PacketState = PACKET_STATE_NULL;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 static uint16_t PacketProtocol = 0;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 static uint16_t PacketLength = 0;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 static uint16_t CurrentChecksum, OneBackChecksum, TwoBackChecksum;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 // Framed packet looks like:
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 // Framing: 0x7e
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 // Address: 0xff
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 // Control: 0x03
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 // Protocol: 0xnn 0xnn
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 // Information: 0xnn 0xnn 0xnn 0xnn ...
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 // Checksum: 0xnn 0xnn
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 // Framing: 0x7e
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 //
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 // If Address-and-Control-Field-Compression is switched on then the remote end is allowed to leave out the 0xff 0x03 in the header
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 // If Protocol-Field-Compression is switched on and the protocol < 0x00ff then the remote end is allowed to leave out the initial 0x00 in the protocol field.
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 // We can check for Protocol-Field-Compression by seeing if the first byte of the protocol is odd (the first byte of a full two-byte protocol value must be even).
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 uint16_t network_read(void)
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 RingBuff_Elements_t c;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 while (Modem_ReceiveBuffer.Elements)
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 c = Buffer_GetElement(&Modem_ReceiveBuffer);
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 switch (PacketState)
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 case PACKET_STATE_NULL:
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 if (c == 0x7e) // New Packet
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 PacketState = PACKET_STATE_INHEADER; // We're now in the header
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 PacketLength = 0;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 CurrentChecksum = 0xffff; // Start new checksum
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 break;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 case PACKET_STATE_INHEADER:
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 if (c == 0x7d) // Escaped character. Set flag and process next time around
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 Escape = true;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 continue;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 else if (Escape) // Escaped character. Process now.
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 Escape = false;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 c = (c ^ 0x20);
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 if (PacketLength == 1 && c != 0xff) // Should be 0xff. If not then Address-and-Control-Field-Compression is switched on
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 PacketLength += 2; // Adjust the length as if the 0xff 0x03 was there
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 if (PacketLength == 3)
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 if (c & 1) // Should be even. If it's odd then Protocol-Field-Compression is switched on
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 PacketProtocol = 0x00; // Add in the initial 0x00
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 PacketLength++; // Adjust the length as if the 0x00 was there
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 else
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 PacketProtocol = c * 256; // Store the MSB of the protocol
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 if (PacketLength == 4) // End of header
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 PacketProtocol |= c; // Store the LSB of the protocol
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 PacketLength = -1; // This will be incremented to 0 at the end of this loop
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 Escape = false;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 PacketState = PACKET_STATE_INBODY; // We're now in the body
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 CurrentChecksum = CALC_CRC16(CurrentChecksum, c); // Calculate checksum
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 break;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 case PACKET_STATE_INBODY:
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 if (c == 0x7e) // End of packet
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 uip_len = PacketLength - 2; // Strip off the checksum and framing
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 Escape = false;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 PacketState = PACKET_STATE_NULL; // Back to waiting for a header
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 Debug_Print("\r\nReceive ");
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 DumpPacket();
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 if (~TwoBackChecksum == (*(uip_buf + PacketLength - 1) * 256 + *(uip_buf + PacketLength - 2)))
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 return PacketProtocol;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 else
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 Debug_Print("Bad CRC\r\n");
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 else
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 if (c == 0x7d) // Escaped character. Set flag and process next time around
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 Escape = true;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 continue;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 else if (Escape) // Escaped character. Process now.
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 Escape = false;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 c = (c ^ 0x20);
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 *(uip_buf + PacketLength) = c; // Store the character in the buffer
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 TwoBackChecksum = OneBackChecksum; // Keep a rolling count of the last 3 checksums
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 OneBackChecksum = CurrentChecksum; // Eventually we need to see if the checksum is valid
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 CurrentChecksum = CALC_CRC16(CurrentChecksum, c); // and we need the one two back (as the current one includes the checksum itself)
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 break;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 PacketLength++; // Increment the length of the received packet
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 return 0; // No data or packet not complete yet
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 void network_send(uint16_t protocol)
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 unsigned int checksum = 0xffff;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 PacketProtocol = protocol;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 PacketState = PACKET_STATE_NULL;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 Debug_Print("\r\nSend ");
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133 DumpPacket();
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135 // Send out the packet with HDLC-like framing - see http://tools.ietf.org/html/rfc1662
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 // Start with the framing flag
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138 Buffer_StoreElement(&Modem_SendBuffer, 0x7e);
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140 // Address
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 Buffer_StoreElement(&Modem_SendBuffer, 0xff);
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142 checksum = CALC_CRC16(checksum, 0xff);
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 // Control
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 Buffer_StoreElement(&Modem_SendBuffer, 0x03);
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 checksum = CALC_CRC16(checksum, 0x03);
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148 // Protocol
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 Buffer_StoreElement(&Modem_SendBuffer, protocol / 256);
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150 checksum = CALC_CRC16(checksum, protocol / 256);
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152 Buffer_StoreElement(&Modem_SendBuffer, protocol & 255);
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153 checksum = CALC_CRC16(checksum, protocol & 255);
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155 // Add the information, escaping it as necessary
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 for (uint16_t i = 0; i < uip_len; i++)
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158 if (*(uip_buf + i) < 0x20 || *(uip_buf + i) == 0x7d || *(uip_buf + i) == 0x7e)
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160 Buffer_StoreElement(&Modem_SendBuffer, 0x7d);
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161 Buffer_StoreElement(&Modem_SendBuffer, *(uip_buf + i) ^ 0x20);
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 else
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 Buffer_StoreElement(&Modem_SendBuffer, *(uip_buf + i));
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
167
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
168 checksum = CALC_CRC16(checksum, *(uip_buf + i));
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170 if (Modem_SendBuffer.Elements == BUFF_STATICSIZE) // Periodically flush the buffer to the modem
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
171 USBManagement_SendReceivePipes();
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174 // Add the checksum to the end of the packet, escaping it as necessary
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175 checksum = ~checksum;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177 if ((checksum & 255) < 0x20 || (checksum & 255) == 0x7d || (checksum & 255) == 0x7e)
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179 Buffer_StoreElement(&Modem_SendBuffer, 0x7d);
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 Buffer_StoreElement(&Modem_SendBuffer, (checksum & 255) ^ 0x20);
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182 else
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
184 Buffer_StoreElement(&Modem_SendBuffer, checksum & 255); // Insert checksum MSB
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
186
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187 if ((checksum / 256) < 0x20 || (checksum / 256) == 0x7d || (checksum / 256) == 0x7e)
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189 Buffer_StoreElement(&Modem_SendBuffer, 0x7d);
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
190 Buffer_StoreElement(&Modem_SendBuffer, (checksum / 256) ^ 0x20);
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192 else
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
193 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
194 Buffer_StoreElement(&Modem_SendBuffer, checksum / 256); // Insert checksum LSB
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
195 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
196
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
197 Buffer_StoreElement(&Modem_SendBuffer, 0x7e); // Framing
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
198
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
199 uip_len = 0;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
200
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
201 USBManagement_SendReceivePipes(); // Flush the rest of the buffer
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
202 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
204 void network_init(void)
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
205 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
206 //Initialise the device
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
207 PacketState = PACKET_STATE_NULL;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
208 Escape = false;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
209 uip_len = 0;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
210 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
211
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
212 void DumpPacket(void)
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
213 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
214 int i, j;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
216 if (!DebugModeEnabled)
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
217 return;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
218
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
219 Debug_Print("(Protocol = 0x");
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
220 Debug_PrintHex(PacketProtocol / 256); Debug_PrintHex(PacketProtocol & 255);
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
221 Debug_Print("):\r\n");
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
223 for (i = 0; i < uip_len; i += 16)
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
224 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
225 Debug_PrintHex(i / 256); Debug_PrintHex(i & 255); Debug_Print(": ");
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
226
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
227 for (j = 0; j < 16; j++)
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
228 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
229 if ((i + j) >= uip_len)
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
230 break;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
231
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
232 Debug_PrintHex(*(uip_buf + i + j));
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
233 Debug_PrintChar(' ');
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
234 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
235
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
236 Debug_Print("\r\n ");
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
237
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
238 for (j = 0; j < 16; j++)
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
239 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
240 if ((i + j) >= uip_len)
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
241 break;
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
242
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
243 if (*(uip_buf + i + j) >= 0x20 && *(uip_buf + i + j) <= 0x7e)
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
244 {
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
245 Debug_PrintChar(' ');
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
246 Debug_PrintChar(*(uip_buf + i + j));
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
247 Debug_PrintChar(' ');
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
248 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
249 else
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
250 Debug_Print(" . ");
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
251
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
252 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
253
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
254 Debug_Print("\r\n");
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
255 }
56d09a0969b5 Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
256 }