-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathALLUTIL.c
311 lines (281 loc) · 7.53 KB
/
ALLUTIL.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
###############################################################################
Include Part
###############################################################################
*/
#include "reg51.h"
#include "allutil.h"
#include "serial.h"
#include "socket.h"
#
/*
Description : Convert 32bit Address into Dotted Decimal Format
Argument : addr - Pointer variable to store converted value(INPUT)
addr_str - Character string to store Decimal Dotted Notation Format.(INPUT,OUTPUT)
Return Value :
Note :
*/
void inet_ntoa(u_char* addr,char* addr_str)
{
char i ;
int d;
for(i = 0; i < 4; i++)
{
d = *(addr+i);
d = d & 0x00FF;
/* Convert to decimal number */
*addr_str++ = D2C(d/100);
*addr_str++ = D2C(( d / 10 )%10);
*addr_str++ = D2C(D2C(d%10));
*addr_str++ = '.';
}
*(--addr_str) = 0;
}
/*
Description : Get Source IP Address of W3100A.
Argument : addr - Pointer to store Source IP Address(32bit Address)(INPUT, OUTPUT)
Return Value :
Note :
*/
void GetIPAddress(u_char* addr)
{
char i ;
for(i = 0; i < 4; i++)
addr[i] = *(SRC_IP_PTR+i);
}
/*
Description : Get Source IP Address of W3100A.
Argument : addr - Pointer to store Gateway IP Address(32bit Address)(INPUT, OUTPUT)
Return Value :
Note :
*/
void GetGWAddress(u_char* addr)
{
char i ;
for(i = 0; i < 4; i++)
addr[i] = *(GATEWAY_PTR+i);
}
/*
Description : Get Source Subnet mask of W3100A.
Argument : addr - Pointer to store Subnet Mask(32bit Address)(INPUT, OUTPUT)
Return Value :
Note :
*/
void GetSubMask(u_char* addr)
{
char i ;
for(i = 0; i < 4; i++)
addr[i] = *(SUBNET_MASK_PTR+i);
}
/*
Description : To be input Dotted Notation string from RS232C and convert 32-bit or 48bit Decimal Address
Argument : addr - Pointer variable to be stored Converted value (INPUT, OUTPUT)
base - binary,decimal,hexa-decimal (INPUT)
len - number of Dot Character string to be input(INPUT)
Return Value : Length of source string
Note : If converting has finished, then '1'.Else if user cancel the input or input wrong valued, then '-1'.
*/
char GetDotNotationAddr(u_char* addr, u_int base, u_int len)
{
u_char xdata str[9]; // Consider the case of binary numberr
u_char i,c;
u_char j = 0;
if(base == 0x10) i = 2; // max number
else if (base == 10) i = 3;
else i = 8;
while(1)
{
c = GetByte(); // Read 1 Character
switch(c)
{
case 0x0D : // If New line
str[j++] = '\0';
*addr++ = (u_char) ATOI(str,base); // Convert to Decimal and Store
len--;
PutStringLn("");
if( len <= 0 ) return 1;
else return -1;
case '.' : // If Dot
str[j++] = '\0';
*addr++ = (u_char) ATOI(str,base);
len--;
PutByte('.');
j = 0;
break;
case 0x1B:
return -1; // Cancel
case 0x08:
if(j !=0)
{
PutByte(0x08);
PutByte(' ');
PutByte(0x08);
j--;
}
break;
default:
if( C2D(c) != c && j < i) // If Value to be input is not character and not above avaiable max input number.
{
PutByte(c); // echo.
str[j++] = c;
}
break;
}
}
}
/*
********************************************************************************
* Description: Read established network information(G/W, IP, S/N, Mac) of W3100A and Output that through Serial.
* Arguments :
* Returns :
* Note : Mac Address is output into format of Dotted HexaDecimal.Others are output into format of Dotted Decimal Format.
********************************************************************************
*/
void GetNetConfig()
{
char xdata str[16];
u_char xdata addr[6];
u_char i;
PutStringLn("\r\n====================================");
PutStringLn(" Net Config Information");
PutStringLn("====================================");
PutString("MAC ADDRESS : ");
for(i = 0; i < 5; i++) // HexaDecimal
{
PutHTOA(*(SRC_HA_PTR+i));
PutByte('.');
}
PutHTOA(*(SRC_HA_PTR+i));
GetSubMask(addr);
inet_ntoa(addr,str); // Dotted Decimal Format convert
PutString("\r\nSUBNET MASK : "); PutStringLn(str);
GetGWAddress(addr);
inet_ntoa(addr,str);
PutString("G/W IP ADDRESS : ");PutStringLn(str);
GetIPAddress(addr);
inet_ntoa(addr,str);
PutString("LOCAL IP ADDRESS : "); PutStringLn(str);
PutStringLn("====================================");
}
/*
Description : Convert Hex(0-F) to Character
Argument : c - Hex( 0x00'~0x0F)to convert into character (INPUT)
Note :
*/
UCHAR D2C(char c)
{
if( c >= 0 && c <= 9)
return '0' + c;
if( c >= 10 && c <= 15)
return 'A' + c - 10;
return c;
}
/*
Description : Convert Character into Hex
Argument : c - character( '0'~'F') to convert into Hex(INPUT)
Note :
*/
char C2D(UCHAR c)
{
if( c >= '0' && c <= '9')
return c - '0';
if( c >= 'a' && c <= 'f')
return 10 + c -'a';
if( c >= 'A' && c <= 'F')
return 10 + c -'A';
return c;
}
/*
Description : Convert Character string into integer number.
Argument : str - Source string (INPUT)
base - Base of value; must be in the range 2 ~ 16
Return Value : Value of Number to be converted
Note :
*/
UINT ATOI(UCHAR* str,char base)
{
UINT num = 0;
while (*str !=0)
num = num * base + C2D(*str++);
return num;
}
/*
Description : Convert Character string into decimal integer number.
Argument : str - Source string (INPUT)
Return Value : Value of decimal number Number to be converted
Note :
*/
UINT CTOI(UCHAR* str)
//UINT CTOI(UCHAR str)
{
UINT num = 0;
// while(*str != 0)
while(*str >= '0' && *str <= '9')
{
num = num * 10 + (unsigned int)((*str) - '0');
str++;
}
// num = num * 10 + (unsigned int)((str) - '0');
return num;
}
/*
Description : Find beginning of character in string
Argument : src - source string (INPUT)
tag - Character to find in source string (INPUT)
Note :
*/
UCHAR * FindFirstStr(UCHAR *src,UCHAR * tag)
{
UINT i;
UCHAR * tsrc = src;
FIND:
if((tsrc = FindFirst(tsrc,tag[0])) !=0)
{
for(i=1; tag[i] != '\0' ; i++)
{
if(*(tsrc+i) == '\0') return 0;
if( *(tag + i) != *(tsrc+ i) )
{
tsrc++;
goto FIND;
}
}
return tsrc;
}
return 0;
}
/*
Description : Find location of character in string
Argument : src - source string (INPUT)
c - Character to find in source string (INPUT)
Note :
*/
UCHAR * FindFirst(UCHAR * src,char c)
{
while(*src != '\0')
{
if(*src == c) return src;
src++;
}
return 0;
}
/*
********************************************************************************
* Output destination IP address of appropriate channel
*
* Description : Output destination IP address of appropriate channel
* Arguments : s - Channel number which try to get destination IP Address
* addr - Buffer address to store destination IP address
* Returns : None
* Note : API Function
* Output format is written in Hexadecimal.
********************************************************************************
*/
/*char* GetDestAddr(SOCKET s,u_char* addr)
{
addr[0] = *(DST_IP_PTR(s));
addr[1] = *(DST_IP_PTR(s)+1);
addr[2] = *(DST_IP_PTR(s)+2);
addr[3] = *(DST_IP_PTR(s)+3);
return addr;
}*/