This repository has been archived by the owner on Mar 9, 2020. It is now read-only.
/
UDPserver.c
411 lines (357 loc) · 10.3 KB
/
UDPserver.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//=============================================================================
// Copyright (c) 2006 Realtek Semiconductor Corporation. All Rights Reserved.
//
// Title:
// UDPserver.c
// Desc:
// UDP server : accepts MP commands from the client
//=============================================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <linux/wireless.h>
#include <fcntl.h>
#ifndef WIN32
#define __PACK__ __attribute__ ((packed))
#else
#define __PACK__
#endif
#define MYPORT 9034 // the port users will be connecting to
#define BUFLEN 1024 // length of the buffer
#define MP_TX_PACKET 0x8B71
#define MP_BRX_PACKET 0x8B73
#define MP_QUERY_STATS 0x8B6D
#define RTL8190_IOCTL_WRITE_REG 0x89f3
#define RTL8190_IOCTL_READ_REG 0x89f4
#define MP_CONTIOUS_TX 0x8B66
#define MP_TXPWR_TRACK 0x8B6E
#define MP_QUERY_TSSI 0x8B6F
#define MP_QUERY_THER 0x8B77
#define FLASH_DEVICE_NAME ("/dev/mtd")
#define FLASH_DEVICE_NAME1 ("/dev/mtdblock1")
#define HW_SETTING_HEADER_TAG ((char *)"hs")
#define HW_SETTING_OFFSET 0x6000
#define DEFAULT_SETTING_OFFSET 0x8000
#define CURRENT_SETTING_OFFSET 0xc000
#if 1
#define TAG_LEN 2
#define SIGNATURE_LEN 4
#define HW_SETTING_VER 3 // hw setting version
/* Config file header */
typedef struct param_header {
unsigned char signature[SIGNATURE_LEN] __PACK__; // Tag + version
unsigned short len __PACK__;
} PARAM_HEADER_T, *PARAM_HEADER_Tp;
PARAM_HEADER_T hsHeader;
#endif
#if 0
/* Do checksum and verification for configuration data */
#ifndef WIN32
static inline unsigned char CHECKSUM(unsigned char *data, int len)
#else
__inline unsigned char CHECKSUM(unsigned char *data, int len)
#endif
{
int i;
unsigned char sum=0;
for (i=0; i<len; i++)
sum += data[i];
sum = ~sum + 1;
return sum;
}
#ifndef WIN32
static inline int CHECKSUM_OK(unsigned char *data, int len)
#else
__inline int CHECKSUM_OK(unsigned char *data, int len)
#endif
{
int i;
unsigned char sum=0;
for (i=0; i<len; i++)
sum += data[i];
if (sum == 0)
return 1;
else
return 0;
}
/////////////////////////////////////////////////////////////////////////////////
static int flash_read(char *buf, int offset, int len)
{
int fh;
int ok=1;
fh = open(FLASH_DEVICE_NAME, O_RDWR);
if ( fh == -1 )
return 0;
lseek(fh, offset, SEEK_SET);
if ( read(fh, buf, len) != len)
ok = 0;
close(fh);
return ok;
}
////////////////////////////////////////////////////////////////////////////////
static int flash_write(char *buf, int offset, int len)
{
int fh;
int ok=1;
fh = open(FLASH_DEVICE_NAME, O_RDWR);
if ( fh == -1 )
return 0;
lseek(fh, offset, SEEK_SET);
if ( write(fh, buf, len) != len)
ok = 0;
close(fh);
sync();
return ok;
}
int ReadSinguture(void)
{
int ver;
char *buff;
// Read hw setting
if ( flash_read((char *)&hsHeader, HW_SETTING_OFFSET, sizeof(hsHeader))==0 ) {
printf("Read hw setting header failed!\n");
return NULL;
}
if ( sscanf(&hsHeader.signature[TAG_LEN], "%02d", &ver) != 1)
ver = -1;
if ( memcmp(hsHeader.signature, HW_SETTING_HEADER_TAG, TAG_LEN) || // invalid signatur
(ver != HW_SETTING_VER) ) { // length is less than current
printf("Invalid hw setting signature or version number [sig=%c%c, ver=%d, len=%d]!\n", hsHeader.signature[0],
hsHeader.signature[1], ver, hsHeader.len);
return NULL;
}
//printf("hw setting signature or version number [sig=%c%c, ver=%d, len=%d]!\n", hsHeader.signature[0], hsHeader.signature[1], ver, hsHeader.len);
buff = calloc(1, hsHeader.len);
if ( buff == 0 ) {
printf("Allocate buffer failed!\n");
return NULL;
}
if ( flash_read(buff, HW_SETTING_OFFSET+sizeof(hsHeader), hsHeader.len)==0 ) {
printf("Read hw setting failed!\n");
free(buff);
return NULL;
}
if ( !CHECKSUM_OK(buff, hsHeader.len) ) {
printf("Invalid checksum of hw setting!\n");
free(buff);
return NULL;
}
//printf("CS=%x\n",buff[hsHeader.len-1]);
}
#endif
/*
* Wrapper to extract some Wireless Parameter out of the driver
*/
static inline int iw_get_ext(int skfd, /* Socket to the kernel */
char *ifname, /* Device name */
int request, /* WE ID */
struct iwreq *pwrq) /* Fixed part of the request */
{
strncpy(pwrq->ifr_name, ifname, IFNAMSIZ); /* Set device name */
return(ioctl(skfd, request, pwrq)); /* Do the request */
}
int MP_get_ext(char *ifname, char *buf, unsigned int ext_num)
{
int skfd;
struct iwreq wrq;
skfd = socket(AF_INET, SOCK_DGRAM, 0);
wrq.u.data.pointer = (caddr_t)buf;
wrq.u.data.length = strlen(buf);
if (iw_get_ext(skfd, ifname, ext_num, &wrq) < 0) {
printf("MP_get_ext failed\n");
return -1;
}
close(skfd);
return 0;
}
int main(void) {
int sockfd; // socket descriptors
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector¡¦s address information
int addr_len, numbytes;
FILE *fp;
char buf[BUFLEN], buf_tmp[BUFLEN],
pre_result[BUFLEN]; // buffer that stores message
static char cmdWrap[500];
static int rwHW=0;
static int ret_value=0;
// create a socket
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
// bind the socket with the address
if (bind(sockfd, (struct sockaddr *)&my_addr,
sizeof(struct sockaddr)) == -1) {
perror("bind");
close(sockfd);
exit(1);
}
addr_len = sizeof(struct sockaddr);
printf("MP AUTOMATION daemon (ver 1.2)\n");
//Self reading flash!!!
#if 0
if(!ReadSinguture())
{
printf("HW Settting Error!!\n");
}
#endif
// main loop : wait for the client
while (1) {
//receive the command from the client
memset(buf, 0, BUFLEN);
memset(cmdWrap, 0, 500);
rwHW = 0;
if ((numbytes = recvfrom(sockfd, buf, BUFLEN, 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1) {
fprintf(stderr,"Receive failed!!!\n");
close(sockfd);
exit(1);
}
//printf("received command (%s) from IP:%s\n", buf, inet_ntoa(their_addr.sin_addr));
if(!memcmp(buf, "orf", 3)){
strcat(buf, " > /tmp/MP.txt");
system(buf);
}
if(!memcmp(buf, "irf", 3)){
strcat(buf, " > /tmp/MP.txt");
system(buf);
}
if (!memcmp(buf, "ther", 4)) {
strcpy(buf, pre_result);
}
if (!memcmp(buf, "tssi", 4)) {
strcpy(buf, pre_result);
}
if (!memcmp(buf, "query", 5)) {
strcpy(buf, pre_result);
}
if(!memcmp(buf, "cat", 3)){
strcat(buf, " > /tmp/MP.txt");
system(buf);
}
#if 1
if (!memcmp(buf, "iwpriv wlan0 mp_tssi", 20)) {
strcpy(buf, pre_result);
MP_get_ext("wlan0", buf_tmp, MP_QUERY_TSSI);
strcpy(buf, buf_tmp);
printf("buf= %s\n",buf);
usleep(1000);
}
else if (!memcmp(buf, "iwpriv wlan0 mp_ther", 20)) {
strcpy(buf, pre_result);
MP_get_ext("wlan0", buf_tmp, MP_QUERY_THER);
strcpy(buf, buf_tmp);
printf("buf= %s\n",buf);
usleep(1000);
}
else if (!memcmp(buf, "iwpriv wlan0 mp_query", 21)) {
strcpy(buf, pre_result);
MP_get_ext("wlan0", buf_tmp, MP_QUERY_STATS);
strcpy(buf, buf_tmp);
usleep(1000);
printf("2b= %s\n",buf);
}
#endif
else {
if ( (!memcmp(buf, "flash read", 10)) ){
if ((fp = fopen("/tmp/MP.txt", "r")) == NULL)
fprintf(stderr, "opening MP.txt failed !\n");
if (fp) {
fgets(buf, BUFLEN, fp);
buf[BUFLEN-1] = '\0';
fclose(fp);
}
sprintf(pre_result, "data:%s", buf);
}
//ack to the client
else if (!memcmp(buf, "flash get", 9))
sprintf(pre_result, "%s > /tmp/MP.txt ok", buf);
else {
sprintf(pre_result, "%s ok", buf);;
}
if (!memcmp(buf, "iwpriv wlan0 mp_brx stop", 24)) {
strcpy(buf, "stop");
MP_get_ext("wlan0", buf, MP_BRX_PACKET);
}
else if (!memcmp(buf, "iwpriv wlan0 mp_tx", 18) && buf[18] == ' ') {
memcpy(buf_tmp, buf+19, strlen(buf)-19);
MP_get_ext("wlan0", buf_tmp, MP_TX_PACKET);
strcpy(buf, buf_tmp);
}
else if (!memcmp(buf, "iwpriv wlan0 mp_ctx", 19) && buf[19] == ' ') {
memcpy(buf_tmp, buf+20, strlen(buf)-20);
MP_get_ext("wlan0", buf_tmp, MP_CONTIOUS_TX);
strcpy(buf, buf_tmp);;
}
else if(!memcmp(buf, "iwpriv wlan0 read_reg", 21)){
strcat(buf, " > /tmp/MP.txt");
system(buf);
}
else if (!memcmp(buf, "probe", 5))
strcpy(buf, "ack");
else if (!memcmp(buf, "verify_flw", 10)) {
if ((fp = fopen("/tmp/MP.txt", "r")) == NULL)
fprintf(stderr, "opening MP.txt failed !\n");
if (fp) {
fgets(buf, BUFLEN, fp);
buf[BUFLEN-1] = '\0';
fclose(fp);
}
}
else {
#if 0
if (!memcmp(buf, "flash get", 9))
strcat(buf, " > /tmp/MP.txt");
#endif
if (!memcmp(buf, "flash get", 9)){
sprintf(cmdWrap, "flash gethw %s", buf+10);
rwHW = 1;
////strcat(buf, " > /tmp/MP.txt");
strcat(cmdWrap, " > /tmp/MP.txt");
}
if (!memcmp(buf, "flash set", 9)) {
sprintf(cmdWrap, "flash sethw %s", buf+10);
rwHW = 1;
//printf("1 sent command (%s) to IP:%s\n", pre_result, inet_ntoa(their_addr.sin_addr));
if ((numbytes = sendto(sockfd, pre_result, strlen(pre_result), 0,
(struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) {
fprintf(stderr, "send failed\n");
close(sockfd);
exit(1);
}
//printf("2 sent command (%s) to IP:%s\n", pre_result, inet_ntoa(their_addr.sin_addr));
}
if(rwHW == 1){
system(cmdWrap);
}else{
system(buf);
}
//delay
//open(/tmp/abc.txt)
}
strcpy(buf_tmp, pre_result);
strcpy(pre_result, buf);
strcpy(buf, buf_tmp);
}
if (memcmp(buf, "flash set", 9) != 0) {
//printf("1 sent command (%s) to IP:%s\n", buf, inet_ntoa(their_addr.sin_addr));
if ((numbytes = sendto(sockfd, buf, strlen(buf), 0,
(struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) {
fprintf(stderr, "send failed\n");
close(sockfd);
exit(1);
}
//printf("2 sent command (%s) to IP:%s\n", buf, inet_ntoa(their_addr.sin_addr));
}
}
return 0;
}