4791e1b5c9fab5310a704f7a1c9f736cfaec4d6d
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / VP_SDK / Examples / linux / wifi.c
1 #include <Com/com.h>
2 #include <VP_Os/vp_os_print.h>
3 #include <VP_Os/vp_os_delay.h>
4 #include <VP_Api/vp_api_error.h>
5
6 #include <iwlib.h>
7 #include <netdb.h>
8 #include <sys/socket.h>
9
10 #include <sys/timeb.h>
11 #include <sys/time.h>
12 #include <sys/types.h>
13 #include <time.h>
14
15 #define DRONE_HOST "192.168.1.2"
16 #define DRONE_PORT 5555
17
18 #define TIME_TO_SEND 1024*10
19 #define SIZE_TO_SEND 1024
20
21 static com_socket_t     clt;
22 static Read             my_read;
23 static Write            my_write;
24
25 static int8_t buffer[SIZE_TO_SEND];
26 static int32_t size;
27
28 static uint32_t timeGetTime()
29 {
30   struct timeval tv; struct timezone tz;
31
32   gettimeofday(&tv,&tz);
33
34   return (uint32_t)( tv.tv_sec*1000 + tv.tv_usec/10000 );
35 }
36
37 int main(void)
38 {
39   com_config_t config;
40   vp_com_connection_t connection;
41
42   config.connection = VP_COM_WIFI;
43   config.localAdapterName = "rausb0";
44
45   connection.essid = "Drone";
46   connection.channel = 10;
47
48   if(FAILED(com_init(&config)))
49     PRINT("com_init failed\n");
50
51   if(FAILED(com_passKey("9F1C3EE11CBA230B27BF1C1B6F")))
52     PRINT("com_passKey failed\n");
53
54   if(FAILED(com_connect(&connection,1)))
55     PRINT("com_connect failed\n");
56
57   clt.socket      = VP_COM_CLIENT;
58   clt.port        = DRONE_PORT;
59   clt.serverHost  = DRONE_HOST;
60
61   if(SUCCEED(com_open(&clt,&my_read,&my_write)))
62   {
63     int32_t i = 0;
64     float st = timeGetTime();
65     float et = timeGetTime();
66     float db = 0.0f;
67     float d = 0.0f;
68
69     for(i=0; i < TIME_TO_SEND;i++)
70     {
71       int32_t received;
72
73       PRINT("\r reception n° %d... ",i);
74
75       received  = 0;
76       size      = SIZE_TO_SEND;
77
78       while(received != SIZE_TO_SEND)
79       {
80         my_read(&clt,buffer,&size);
81         received += size;
82         size = SIZE_TO_SEND - received;
83       }
84
85       PRINT("%d bytes           ",received);
86     }
87
88     et = timeGetTime();
89     d = (et - st) / 1000.0f;
90     if(d > 0)
91     {
92       float tx = SIZE_TO_SEND * TIME_TO_SEND / 1024.0f;
93       db = tx / d;
94     }
95     PRINT("\n---------------\n");
96     PRINT("Start Time : %f\n",st);
97     PRINT("End Time : %f\n",et);
98     PRINT("%d Kbytes sent in %f time\n",SIZE_TO_SEND * TIME_TO_SEND / 1024,d);
99     PRINT("Debit: %f\n",db);
100     PRINT("\n---------------\n");
101   }
102   else
103   {
104     PRINT("snif... pas connecte a la socket\n");
105   }
106
107   PRINT("Waiting for disconnection\n");
108   vp_delay(5000);
109
110   com_disconnect();
111   PRINT("Disconnected\n");
112
113   com_shutdown();
114
115   return 0;
116 }