-
Notifications
You must be signed in to change notification settings - Fork 0
/
re-test2.c
405 lines (342 loc) · 11.2 KB
/
re-test2.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
// to compile this code gcc -o Rtrg_gps Rtrg_gps.c libiw.so.29
/*
* UTM location tracking source code designed By ADEL ALI adelali3@lycos.com
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/time.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <netdb.h>
#include <asm/ioctl.h>
#include <math.h>
#include "rtsock.h"
#define SENDRATE 1000 // 2000 packets per second
struct packet { // packet contents for sending
char mac[20];
char essid[20];
int sl;
float dis;
};
struct packet p[15];
static int g_rtfd = -1;
int
DecodeIPAddress(char *s, struct sockaddr_in *sin)
{
int port;
char ip[1024];
struct hostent *he;
memset(sin, 0, sizeof(*sin));
sin->sin_family = AF_INET;
switch(sscanf(s, "%1023[^:]:%i", ip, &port)) {
case 0:
if(sscanf(s, ":%i", &port) == 1) {
sin->sin_port = htons(port);
}
break;
case 1:
port = 0;
case 2:
sin->sin_port = htons(port);
he = gethostbyname(ip);
if(he) {
memcpy(&sin->sin_addr.s_addr, he->h_addr_list[0], 4);
} else {
return -1;
}
break;
}
return 0;
}
char *
EncodeIPAddress(char *s, struct sockaddr_in *sin)
{
int x;
x = ntohl(sin->sin_addr.s_addr);
sprintf(s, "%d.%d.%d.%d:%d",
(x>>24)&0xFF, (x>>16)&0xFF, (x>>8)&0xFF, x&0xFF,
htons(sin->sin_port)
);
return s;
}
int
CreateUDPSocket()
{
int sock;
if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
return -1;
}
return sock;
}
void
DestroySocket(int sock)
{
if(sock < 0) {
return;
}
close(sock);
}
int
BindSocket(int sock, char *s)
{
struct sockaddr_in localAddr;
if(sock < 0) {
return sock;
}
if(DecodeIPAddress(s, &localAddr) < 0) {
fprintf(stderr, "cannot resolve %s\n", s);
return -1;
}
if(bind(sock, &localAddr, sizeof(localAddr)) < 0 ) {
perror("bind failed");
return -1;
}
return sock;
}
int
ConnectSocket(int sock, char *s)
{
struct sockaddr_in farAddr;
if(sock < 0) {
return sock;
}
if(DecodeIPAddress(s, &farAddr) < 0) {
fprintf(stderr, "cannot resolve %s\n", s);
return -1;
}
if(connect(sock, &farAddr, sizeof(farAddr)) < 0) {
perror("connect failed");
return -1;
}
return sock;
}
int
CreateRtSocket(int sock, int *params, int paramsLength)
{
int *ioctlParams, index, i;
if(sock < 0) {
return sock;
}
if(g_rtfd < 0) {
if((g_rtfd=open("/dev/rtsock0", O_RDWR)) < 0 ) {
perror("rtsock0 open");
return -1;
}
}
ioctlParams = calloc(paramsLength+3, sizeof(int));
ioctlParams[0] = sock; // fd of sock
ioctlParams[1] = 0; // operation
ioctlParams[2] = 0; // index of mysock in rt space
if(params) {
for(i=0; i<paramsLength; i++) {
ioctlParams[3+i] = params[i];
}
}
if(ioctl(g_rtfd, RTSOCK_IOCINTERFACE, ioctlParams) < 0) {
perror("ioctl(RTSOCK_IOCINTERFACE, insert)");
free(ioctlParams);
close(sock);
return -1;
}
index = ioctlParams[2]; // this parameter is return by ioctl
free(ioctlParams);
return index;
}
void
DestroyRtSocket(int sock, int index)
{
long ioctlParams[3];
if(sock < 0 || index < 0) {
return;
}
ioctlParams[0] = sock;
ioctlParams[1] = 2;
ioctlParams[2] = index;
if(ioctl(g_rtfd, RTSOCK_IOCINTERFACE, ioctlParams) < 0) {
perror("ioctl(RTSOCK_IOCINTERFACE, destroy)");
}
}
int
RedirectRtSocket(int sock)
{
long ioctlParams[1];
if(sock < 0) {
return sock;
}
ioctlParams[0] = sock;
if(ioctl(g_rtfd, RTSOCK_IOCREDIRECTFD, ioctlParams) < 0) {
perror("ioctl(RTSOCK_IOCINTERFACE, destroy)");
return -1;
}
return sock;
}
int
ResetRtSocket(int sock)
{
long ioctlParams[1];
if(sock < 0) {
return sock;
}
ioctlParams[0] = sock;
if(ioctl(g_rtfd, RTSOCK_IOCRESETFD, ioctlParams) < 0) {
perror("ioctl(RTSOCK_IOCINTERFACE, destroy)");
return -1;
}
return sock;
}
int
UpdateRtSocket(int sock, int index, int *params, int paramsLength)
{
int *ioctlParams, i;
if(sock < 0 || index < 0) {
return -1;
}
ioctlParams = calloc(paramsLength+3, sizeof(int));
ioctlParams[0] = sock; // fd of sock
ioctlParams[1] = 1; // operation
ioctlParams[2] = index; // index of mysock in rt space
if(params) {
for(i=0; i<paramsLength; i++) {
ioctlParams[3+i] = params[i];
}
}
if(ioctl(g_rtfd, RTSOCK_IOCINTERFACE, ioctlParams) < 0) {
perror("ioctl(RTSOCK_IOCINTERFACE, update)");
free(ioctlParams);
return -1;
}
free(ioctlParams);
return sock;
}
int
main(int argc, char *argv[])
{
// int sock, rtsock, count, params[2], fd,index_sp,mimo_flag=0,mimo_flag1=0,mimo_flag2=0, average[10];
int sock, rtsock, i,j, counta=0, countb=0, countc=0, count, params[2], fd,index_sp,mimo_flag=0,mimo_flag1=0,mimo_flag2=0, average[10];
int addrLen,k,sum=0,qsum=0,sum1=0,qsum1=0,sum2=0,qsum2=0, number=0, add=0;
FILE* f;
double alfa;
float a,b,c,Xmn,Ymn,Ymn1,Ymn2,Xinput,Yinput,Distance,dp1,dp2,tambah, averageda, averagedb, averagedc, averaged=0.0;
struct sockaddr_in addr;
char *s, s1[1024];
if((f=fopen("/root/ssf.c","r+"))==NULL)
{
printf("cannot open this file\n");exit(-1);
}
if(argc < 2) {
fprintf(stderr, "usage: Rtrg_gps [ru|rr|tu|tr] [<address>]\n");
exit(-1);
}
s = (argc < 3)? "" : argv[2];
if(strcmp(argv[1], "ru") == 0) {
if((sock = BindSocket(CreateUDPSocket(), s)) < 0) {
exit(-1);
}
addrLen = sizeof(addr);
if(getsockname(sock, &addr, &addrLen) < 0 ) {
close(sock);
return -1;
}
printf("Rtrg_gps will receive request at this address %s\n", EncodeIPAddress(s1, &addr));
for(;;){
recv(sock,&index_sp, sizeof(index_sp), 0);
//printf("I receive ur index \n");
for(i=0; i< index_sp;i++) {
recv(sock, &p[i], sizeof(p), 0);
if(strcmp((p[i].essid,"mimos")==0)&&counta<3)
// {a=p[i].dis;mimo_flag++;fprintf(f,"%s ESSID: %s Signal level: %d Distance: %f\n",p[i].mac,p[i].essid,p[i].sl,a);
{a=p[i].dis;tambah=p[i].sl;mimo_flag++;fprintf(f,"%s ESSID: %s Signal level: %d Distance: %f\n",p[i].mac,p[i].essid,tambah,a);
a=a+p[i].dis;
counta++;
// add += p[i].sl;
// averaged = (float)add / 10;
// n[i]=0;
// a+= n[i];
// printf(" entah macam mana %f\n", averaged);
}
else if(strcmp((p[i].essid,"mimos1")==0)&&countb<3)
{b=p[i].dis;mimo_flag1++;fprintf(f,"%s ESSID: %s Signal level: %d Distance: %f\n",p[i].mac,p[i].essid,p[i].sl,b);
b=b+p[i].dis;
countb++;
}
else if(strcmp((p[i].essid,"mimos2")==0)&&countc<3)
{c=p[i].dis;mimo_flag2++;fprintf(f,"%s ESSID: %s Signal level: %d Distance: %f\n",p[i].mac,p[i].essid,p[i].sl,c);
c=c+p[i].dis;
countc++;
}
printf("%s ESSID: %s Signal level: %d Distance: %f\n",p[i].mac,p[i].essid,p[i].sl,p[i].dis);
//add += p[i].sl;
//averaged = (float)add / i;
// n[i]=0;
// a+= n[i];
//printf(" %f\n", averaged);
}
}
add += tambah;
averaged = (float)add / 3;
printf(" entah macam mana %f\n", averaged);
//printf("enter referend point x,y : " );
//scanf ("%f,%f", &Xinput, &Yinput); // key in location of other point
//printf ("You enter %f, %f \n", Xinput, Yinput);
// calculate mobile node location
if(mimo_flag > 0 && mimo_flag1>0 && mimo_flag2 >0 ){
alfa=acos((a*a+100-b*b)/(20*a));
if( isnan(alfa)==0){
Xmn=a*cos(alfa);Ymn1=a*sin(alfa);Ymn2=-Ymn1;
Distance=sqrt(((Xmn-Xinput)*(Xmn-Xinput))+((Ymn-Yinput)*(Ymn-Yinput)));
dp1=sqrt(Xmn*Xmn + (10-Ymn1)*(10-Ymn1));
dp2=sqrt(Xmn*Xmn + (10-Ymn2)*(10-Ymn2));
if (fabsf(dp1-c) < fabsf(dp2-c))
Ymn=Ymn1;
else
Ymn=-Ymn2;
if ( Xmn < 0 )
Xmn = -Xmn;
else
Xmn = Xmn;
printf("alfa is %f, dp1 is %f,dp2 is %f. The location of mobile node is (%f,%f) \n",alfa,dp1,dp2,Xmn,Ymn);
fprintf(f,"The location of mobile node is (%f,%f)\n",Xmn,Ymn);
// {
// Ymn=Ymn1;
// Distance=sqrt((Xmn-Xinput)*(Xmn-Xinput)+(Ymn1-Yinput)*(Ymn1-Yinput)); //measure the distance
// }
// else
// {
// Ymn=Ymn2;
// Distance=sqrt((Xmn-Xinput)*(Xmn-Xinput)+(Ymn2-Yinput)*(Ymn2-Yinput)); //measure the distance
// }
//printf("alfa is %f, dp1 is %f,dp2 is %f. The location of mobile node is (%f,%f). The distance between mobile node (%f,%f) and (%f,%f) is %f \n",alfa,dp1,dp2,Xmn,Ymn, Xinput, Yinput, Xmn, Ymn, Distance);
//fprintf(f,"The location of mobile node is , and the nearest distance is \n");
//fclose(f);
//return (i=0);
}
else
printf("Please wait ...... \n");
} //end if
else
printf("Please wait ...... \n");
//} //end for loop
} //end infinate for loop
//}
}
//comment.. this coding has been add on the distance measurement by using signal strength instead of calculating the distance at the mobile node 20 April 09, 11:32 pm .............. Result = NO ERROR!
// comment putting the distance coding in this Rtrg_gps.c coding to combine the coding into one program.. Result = NO ERROR! 21 April 09, 4:11 am
//new 10hb mei.. midnite... try wat loop forever.. so just started the location server n it will forever receive signal from others
//success to make it forever's loop! and most important it's mean that location server automatically receive signal from other mobile node and it will only stop if the location server is 'exit'.. now.. try to see the file inside root's file and see the mobile node.. what happend if the location already been calculated.. is the mobile node still keep sending the data to location server or not.. if not.. changes it to forever..
//there is a problem occur in writing up the data into file.. try check in the 'tu' coding whether there is something to changes or what often this matterz.. erm.. so now.. it prove that the data will be forever loop.
//nex step is to check up the file at root.. need to build up the database structure.. not yet done the average part so do it fast! target this done by this Friday!
//wednesday.. filter the (negatif y axis)....... success!!!
//thursday.. success updating the file :------->
// "r" open text file for reading
// "w" create text file for writing; discard previous contents if any
// "a" append; open or create text file for writing at end of file
// "r+" open text file for update (ie., reading and writing)
// "w+" create text file for update; discard previous contents of any
// "a+" append; open or create text file for update, writing at end
// update mode permits reading and writing the same file; fflush or a file-positioning function must be called between a read and a write or vice versa. If the mode includes b after the initial letter, as in"rb" or "w+b", that indicates a binary file. Filenames are limited to FILENAME_MAX characters. At most FOPEN_MAX files may be open at once.
//friday.. success 1 quater n forever loop.. but Dr. Sharifah want the file update after 10 reading.. now i can do the update only when the location server is restarted by using "r+".. so need to modified this coding into 10 times reading..