ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / VP_SDK / VP_Com / win32 / vp_com.c
1 //#include <bluetooth/bluetooth.h>
2
3 #include <VP_Os/vp_os_print.h>
4
5 #include <VP_Os/vp_os_malloc.h>
6 #include <VP_Os/vp_os_assert.h>
7
8 #include <VP_Com/vp_com.h>
9 #include <VP_Com/vp_com_error.h>
10
11 #ifndef _WIN32
12 #include <VP_Com/vp_com_serial.h>
13 #endif
14
15 #include <VP_Com/vp_com_wifi.h>
16
17 typedef C_RESULT (*VP_COM_x_init)(void);
18 typedef C_RESULT (*VP_COM_x_shutdown)(void);
19 typedef C_RESULT (*VP_COM_x_network_adapter_lookup)(vp_com_network_adapter_lookup_t callback);
20 typedef C_RESULT (*VP_COM_x_inquire)(const char* deviceName, vp_com_inquiry_t callback, uint32_t timeout);
21 typedef C_RESULT (*VP_COM_x_local_config)(vp_com_config_t* config);
22 typedef C_RESULT (*VP_COM_x_connect)(vp_com_t* vp_com, vp_com_connection_t* connection, int32_t numAttempts);
23 typedef C_RESULT (*VP_COM_x_disconnect)(vp_com_config_t* config, vp_com_connection_t* connection);
24 typedef C_RESULT (*VP_COM_x_get_rssi)(vp_com_config_t* cfg, int32_t* rssi);
25 typedef C_RESULT (*VP_COM_x_wait_connections)(vp_com_connection_t** c, vp_com_socket_t* server, vp_com_socket_t* client, int queueLength);
26 typedef C_RESULT (*VP_COM_x_open)(vp_com_config_t* config, vp_com_connection_t* connection, vp_com_socket_t* socket, Read* read, Write* write);
27 typedef C_RESULT (*VP_COM_x_close)(vp_com_socket_t* socket);
28
29 #define VP_COM_INIT                  vp_com->init
30 #define VP_COM_SHUTDOWN              vp_com->shutdown
31 #define VP_COM_NETWORKADAPTERLOOKUP  vp_com->network_adapter_lookup
32 #define VP_COM_INQUIRE               vp_com->inquire
33 #define VP_COM_LOCAL_CONFIG          vp_com->local_config
34 #define VP_COM_CONNECT               vp_com->connect
35 #define VP_COM_DISCONNECT            vp_com->disconnect
36 #define VP_COM_GET_RSSI              vp_com->get_rssi
37 #define VP_COM_WAITCONNECTIONS       vp_com->wait_connections
38 #define VP_COM_OPEN                  vp_com->open
39 #define VP_COM_CLOSE                 vp_com->close
40
41 C_RESULT vp_com_init(vp_com_t* vp_com)
42 {
43   C_RESULT res = VP_COM_NOTSUPPORTED;
44
45   VP_OS_ASSERT( vp_com != NULL );
46
47   if(!vp_com->initialized)
48   {
49     vp_os_mutex_init(&vp_com->mutex);
50     vp_com->initialized ++;
51   }
52
53   vp_os_mutex_lock(&vp_com->mutex);
54
55   if(vp_com->ref_count > 0)
56   {
57     vp_com->ref_count ++;
58     res = VP_COM_OK;
59   }
60   else
61   {
62
63           if(vp_com->type == VP_COM_WIFI)
64     {
65       vp_com->init                    = (VP_COM_x_init) vp_com_wf_init;
66       vp_com->shutdown                = (VP_COM_x_shutdown) vp_com_wf_shutdown;
67       vp_com->network_adapter_lookup  = (VP_COM_x_network_adapter_lookup) vp_com_wf_network_adapter_lookup;
68       vp_com->local_config            = (VP_COM_x_local_config) vp_com_wf_local_config;
69       vp_com->inquire                 = (VP_COM_x_inquire) vp_com_wf_inquire;
70       vp_com->connect                 = (VP_COM_x_connect) vp_com_wf_connect;
71       vp_com->disconnect              = (VP_COM_x_disconnect) vp_com_wf_disconnect;
72       vp_com->get_rssi                = (VP_COM_x_get_rssi) vp_com_wf_get_rssi;
73       vp_com->wait_connections        = (VP_COM_x_wait_connections) vp_com_wf_wait_connections;
74       vp_com->open                    = (VP_COM_x_open) vp_com_wf_open;
75       vp_com->close                   = (VP_COM_x_close) vp_com_wf_close;
76     }
77
78
79 /*#ifndef NO_COM
80
81     if(vp_com->type == VP_COM_BLUETOOTH)
82     {
83       vp_com->init                    = (VP_COM_x_init) vp_com_bt_init;
84       vp_com->shutdown                = (VP_COM_x_shutdown) vp_com_bt_shutdown;
85       vp_com->network_adapter_lookup  = (VP_COM_x_network_adapter_lookup) vp_com_bt_network_adapter_lookup;
86       vp_com->local_config            = (VP_COM_x_local_config) vp_com_bt_local_config;
87       vp_com->inquire                 = (VP_COM_x_inquire) vp_com_bt_inquire;
88       vp_com->connect                 = (VP_COM_x_connect) vp_com_bt_connect;
89       vp_com->disconnect              = (VP_COM_x_disconnect) vp_com_bt_disconnect;
90       vp_com->get_rssi                = NULL;
91       vp_com->wait_connections        = (VP_COM_x_wait_connections) vp_com_bt_wait_connections;
92       vp_com->open                    = (VP_COM_x_open) vp_com_bt_open;
93       vp_com->close                   = (VP_COM_x_close) vp_com_bt_close;
94     }
95
96    
97
98 #endif // > NO_COM*/
99
100 #ifndef _WIN32
101         
102
103     if(vp_com->type == VP_COM_SERIAL)
104     {
105       vp_com->init                    = (VP_COM_x_init) vp_com_serial_init;
106       vp_com->shutdown                = (VP_COM_x_shutdown) vp_com_serial_shutdown;
107       vp_com->network_adapter_lookup  = (VP_COM_x_network_adapter_lookup) vp_com_serial_network_adapter_lookup;
108       vp_com->local_config            = (VP_COM_x_local_config) vp_com_serial_local_config;
109       vp_com->inquire                 = (VP_COM_x_inquire) vp_com_serial_inquire;
110       vp_com->connect                 = (VP_COM_x_connect) vp_com_serial_connect;
111       vp_com->disconnect              = (VP_COM_x_disconnect) vp_com_serial_disconnect;
112       vp_com->get_rssi                = NULL;
113       vp_com->wait_connections        = (VP_COM_x_wait_connections) vp_com_serial_wait_connections;
114       vp_com->open                    = (VP_COM_x_open) vp_com_serial_open;
115       vp_com->close                   = (VP_COM_x_close) vp_com_serial_close;
116     }
117 #endif
118
119 /*    if( vp_com->type == VP_COM_WIRED )
120     {
121       vp_com->init                    = (VP_COM_x_init) vp_com_wired_init;
122       vp_com->shutdown                = (VP_COM_x_shutdown) vp_com_wired_shutdown;
123       vp_com->network_adapter_lookup  = (VP_COM_x_network_adapter_lookup) vp_com_wired_network_adapter_lookup;
124       vp_com->local_config            = (VP_COM_x_local_config) vp_com_wired_local_config;
125       vp_com->inquire                 = (VP_COM_x_inquire) vp_com_wired_inquire;
126       vp_com->connect                 = (VP_COM_x_connect) vp_com_wired_connect;
127       vp_com->disconnect              = (VP_COM_x_disconnect) vp_com_wired_disconnect;
128       vp_com->get_rssi                = NULL;
129       vp_com->wait_connections        = (VP_COM_x_wait_connections) vp_com_wired_wait_connections;
130       vp_com->open                    = (VP_COM_x_open) vp_com_wired_open;
131       vp_com->close                   = (VP_COM_x_close) vp_com_wired_close;
132     }*/
133
134     if(VP_COM_INIT)
135       res = VP_COM_INIT();
136
137     if(res == VP_COM_OK)
138     {
139       vp_os_install_error_handler( VP_COM_SDK_SIGNATURE, vp_com_formatMessage );
140       vp_com->ref_count ++;
141     }
142   }
143
144   vp_os_mutex_unlock( &vp_com->mutex );
145
146   return res;
147 }
148
149 C_RESULT vp_com_shutdown(vp_com_t* vp_com)
150 {
151   VP_OS_ASSERT( vp_com != NULL );
152
153   vp_os_mutex_lock( &vp_com->mutex );
154
155   if(vp_com->ref_count > 0)
156   {
157     vp_com->ref_count--;
158     if(vp_com->ref_count == 0)
159     {
160       vp_os_mutex_unlock( &vp_com->mutex );
161       vp_os_mutex_destroy( &vp_com->mutex );
162
163       return VP_COM_SHUTDOWN();
164     }
165   }
166
167   vp_os_mutex_unlock(&vp_com->mutex);
168
169   return VP_COM_OK;
170 }
171
172 /*C_RESULT vp_com_str_to_address(const char* address, bdaddr_t* addr)
173 {
174   str2ba( address,addr );
175
176   return VP_COM_OK;
177 }
178
179 C_RESULT vp_com_address_to_str(const bdaddr_t* addr, char* address)
180 {
181   ba2str( addr, address );
182
183   return VP_COM_OK;
184 }
185
186 C_RESULT vp_com_copy_address(const bdaddr_t* from,bdaddr_t* to)
187 {
188   vp_os_memcpy( to, from, sizeof( bdaddr_t ) );
189
190   return VP_COM_OK;
191 }
192
193 C_RESULT vp_com_cmp_address(const bdaddr_t* bd1, const bdaddr_t* bd2)
194 {
195   int32_t i;
196
197   for( i = 0; i < BDADDR_SIZE && ( bd1->b[i] == bd2->b[i] ); i++ );
198
199   return ( i < BDADDR_SIZE ) ? VP_COM_ERROR : VP_COM_OK;
200 }
201 */
202 C_RESULT vp_com_network_adapter_lookup(vp_com_t* vp_com, vp_com_network_adapter_lookup_t callback)
203 {
204   return VP_COM_NETWORKADAPTERLOOKUP( callback );
205 }
206
207 C_RESULT vp_com_local_config(vp_com_t* vp_com, vp_com_config_t* config)
208 {
209   C_RESULT res = C_OK;
210
211   VP_OS_ASSERT( vp_com != NULL );
212
213   if( vp_com->config != config )
214   {
215
216     res = VP_COM_LOCAL_CONFIG(config);
217
218     if( VP_SUCCEEDED( res ) )
219       vp_com->config = config;
220
221   }
222
223   return res;
224 }
225
226 C_RESULT vp_com_inquire(vp_com_t* vp_com, const char* deviceName, vp_com_inquiry_t callback, uint32_t timeout)
227 {
228   VP_OS_ASSERT( vp_com != NULL );
229
230   return VP_COM_INQUIRE( deviceName, callback, timeout );
231 }
232
233 C_RESULT vp_com_connect(vp_com_t* vp_com, vp_com_connection_t* connection, uint32_t numAttempts)
234 {
235   C_RESULT res = VP_COM_OK;
236   bool_t already_connected;
237
238   VP_OS_ASSERT( vp_com != NULL );
239
240   if(vp_com->config != NULL)
241   {
242     vp_os_mutex_lock(&vp_com->mutex);
243
244     already_connected = vp_com->connection && vp_com->connection->is_up == 1;
245
246     // TODO voir pour ajouter un test sur l'adresse ethernet de la connection
247     if( already_connected && vp_com->connection != connection )
248     {
249       already_connected = FALSE;
250       vp_com_disconnect(vp_com);
251     }
252
253     if( !already_connected )
254     {
255       res = VP_COM_CONNECT(vp_com, connection, numAttempts);
256
257       if( VP_SUCCEEDED( res ) )
258       {
259         vp_com->connection = connection;
260         vp_com->connection->is_up = 1;
261       }
262     }
263
264     vp_os_mutex_unlock(&vp_com->mutex);
265   }
266
267   return res;
268 }
269
270 C_RESULT vp_com_disconnect(vp_com_t* vp_com)
271 {
272   C_RESULT res = VP_COM_ERROR;
273
274   VP_OS_ASSERT( vp_com != NULL );
275
276   if(vp_com->config != NULL && vp_com->connection != NULL)
277   {
278     vp_os_mutex_lock(&vp_com->mutex);
279
280     res = VP_COM_DISCONNECT(vp_com->config, vp_com->connection);
281
282     if( VP_SUCCEEDED( res ) )
283       vp_com->connection->is_up = 0;
284
285
286     vp_os_mutex_unlock(&vp_com->mutex);
287   }
288
289   return res;
290 }
291 /*
292 C_RESULT vp_com_get_rssi(vp_com_t* vp_com, int32_t* rssi)
293 {
294   C_RESULT res;
295
296   if( vp_com != NULL && vp_com->config != NULL && vp_com->get_rssi != NULL )
297     res = VP_COM_GET_RSSI( vp_com->config, rssi );
298   else
299   {
300     *rssi = 0;
301     res = C_FAIL;
302   }
303
304   return res;
305 }
306 */
307 C_RESULT vp_com_wait_connections(vp_com_t* vp_com, vp_com_socket_t* server, vp_com_socket_t* client, int32_t queueLength)
308 {
309   VP_OS_ASSERT( vp_com != NULL );
310
311   return VP_COM_WAITCONNECTIONS( &vp_com->connection, server, client, queueLength );
312 }
313
314 C_RESULT vp_com_open(vp_com_t* vp_com, vp_com_socket_t* socket, Read* read, Write* write)
315 {
316   VP_OS_ASSERT( vp_com != NULL );
317
318   return VP_COM_OPEN(vp_com->config, vp_com->connection, socket, read, write);
319 }
320
321 C_RESULT vp_com_close(vp_com_t* vp_com, vp_com_socket_t* socket)
322 {
323   return VP_COM_CLOSE( socket );
324 }
325
326 C_RESULT vp_com_sockopt(vp_com_t* vp_com, vp_com_socket_t* socket, VP_COM_SOCKET_OPTIONS options)
327 {
328   C_RESULT res;
329
330   switch( socket->protocol )
331   {
332 /*#ifndef NO_COM
333     case VP_COM_TCP:
334       res = vp_com_sockopt_ip(vp_com, socket, options);
335       break;
336 #endif*/
337     case VP_COM_SERIAL:
338       res = VP_COM_OK;
339       break;
340
341     default:
342       res = VP_COM_ERROR;
343       break;
344   }
345
346   return res;
347 }