1 #include <VP_Os/vp_os_malloc.h>
2 #include <VP_Os/vp_os_signal.h>
3 #include <VP_Os/vp_os_assert.h>
4 #include <VP_Os/vp_os_print.h>
5 #include <VP_Com/vp_com.h>
6 #include <VP_Com/vp_com_socket.h>
7 #include <VP_Com/vp_com_error.h>
8 #include <VP_Api/vp_api_error.h>
10 #include "vp_com_bluetooth.h"
11 #include "vp_com_wifi.h"
12 #include "vp_com_serial.h"
13 #include "vp_com_wired.h"
15 typedef C_RESULT (*VP_COM_x_init)(void);
16 typedef C_RESULT (*VP_COM_x_shutdown)(void);
17 typedef C_RESULT (*VP_COM_x_network_adapter_lookup)(vp_com_network_adapter_lookup_t callback);
18 typedef C_RESULT (*VP_COM_x_inquire)(const char* deviceName, vp_com_inquiry_t callback, uint32_t timeout);
19 typedef C_RESULT (*VP_COM_x_local_config)(vp_com_config_t* config);
20 typedef C_RESULT (*VP_COM_x_connect)(vp_com_t* vp_com, vp_com_connection_t* connection, int32_t numAttempts);
21 typedef C_RESULT (*VP_COM_x_disconnect)(vp_com_config_t* config, vp_com_connection_t* connection);
22 typedef C_RESULT (*VP_COM_x_get_rssi)(vp_com_config_t* cfg, int32_t* rssi);
23 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);
24 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);
25 typedef C_RESULT (*VP_COM_x_close)(vp_com_socket_t* socket);
27 #define VP_COM_INIT vp_com->init
28 #define VP_COM_SHUTDOWN vp_com->shutdown
29 #define VP_COM_NETWORKADAPTERLOOKUP vp_com->network_adapter_lookup
30 #define VP_COM_INQUIRE vp_com->inquire
31 #define VP_COM_LOCAL_CONFIG vp_com->local_config
32 #define VP_COM_CONNECT vp_com->connect
33 #define VP_COM_DISCONNECT vp_com->disconnect
34 #define VP_COM_GET_RSSI vp_com->get_rssi
35 #define VP_COM_WAITCONNECTIONS vp_com->wait_connections
36 #define VP_COM_OPEN vp_com->open
37 #define VP_COM_CLOSE vp_com->close
39 char vp_com_pin_code[VP_COM_NAME_MAXSIZE];
41 C_RESULT vp_com_init(vp_com_t* vp_com)
43 C_RESULT res = VP_COM_NOTSUPPORTED;
45 VP_OS_ASSERT( vp_com != NULL );
47 if(!vp_com->initialized)
49 vp_os_mutex_init(&vp_com->mutex);
50 vp_com->initialized ++;
53 vp_os_mutex_lock(&vp_com->mutex);
55 if(vp_com->ref_count > 0)
63 if(vp_com->type == VP_COM_WIFI)
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;
80 if(vp_com->type == VP_COM_SERIAL)
82 vp_com->init = (VP_COM_x_init) vp_com_serial_init;
83 vp_com->shutdown = (VP_COM_x_shutdown) vp_com_serial_shutdown;
84 vp_com->network_adapter_lookup = (VP_COM_x_network_adapter_lookup) vp_com_serial_network_adapter_lookup;
85 vp_com->local_config = (VP_COM_x_local_config) vp_com_serial_local_config;
86 vp_com->inquire = (VP_COM_x_inquire) vp_com_serial_inquire;
87 vp_com->connect = (VP_COM_x_connect) vp_com_serial_connect;
88 vp_com->disconnect = (VP_COM_x_disconnect) vp_com_serial_disconnect;
89 vp_com->get_rssi = NULL;
90 vp_com->wait_connections = (VP_COM_x_wait_connections) vp_com_serial_wait_connections;
91 vp_com->open = (VP_COM_x_open) vp_com_serial_open;
92 vp_com->close = (VP_COM_x_close) vp_com_serial_close;
97 if( vp_com->type == VP_COM_WIRED )
99 vp_com->init = (VP_COM_x_init) vp_com_wired_init;
100 vp_com->shutdown = (VP_COM_x_shutdown) vp_com_wired_shutdown;
101 vp_com->network_adapter_lookup = (VP_COM_x_network_adapter_lookup) vp_com_wired_network_adapter_lookup;
102 vp_com->local_config = (VP_COM_x_local_config) vp_com_wired_local_config;
103 vp_com->inquire = (VP_COM_x_inquire) vp_com_wired_inquire;
104 vp_com->connect = (VP_COM_x_connect) vp_com_wired_connect;
105 vp_com->disconnect = (VP_COM_x_disconnect) vp_com_wired_disconnect;
106 vp_com->get_rssi = NULL;
107 vp_com->wait_connections = (VP_COM_x_wait_connections) vp_com_wired_wait_connections;
108 vp_com->open = (VP_COM_x_open) vp_com_wired_open;
109 vp_com->close = (VP_COM_x_close) vp_com_wired_close;
118 vp_os_install_error_handler( VP_COM_SDK_SIGNATURE, vp_com_formatMessage );
119 vp_com->ref_count ++;
123 vp_os_mutex_unlock( &vp_com->mutex );
128 C_RESULT vp_com_shutdown(vp_com_t* vp_com)
130 VP_OS_ASSERT( vp_com != NULL );
132 vp_os_mutex_lock( &vp_com->mutex );
134 if(vp_com->ref_count > 0)
137 if(vp_com->ref_count == 0)
139 vp_os_mutex_unlock( &vp_com->mutex );
140 vp_os_mutex_destroy( &vp_com->mutex );
142 return VP_COM_SHUTDOWN();
146 vp_os_mutex_unlock(&vp_com->mutex);
151 // Convert a char to an hexidecimal value between 0x0 and 0xF
152 static char ctohex(char c)
154 if(c >= '0' && c <= '9')
156 if(c >= 'a' && c <= 'f')
157 return c - 'a' + 0xa;
158 if(c >= 'A' && c <= 'F')
159 return c - 'A' + 0xa;
164 // Convert an hexidecimal value to char
165 static char hextoc(char h)
170 return ((h - 0xa) + 'a');
173 C_RESULT vp_com_str_to_address(const char* address, bdaddr_t* addr)
181 addr->b[i] = ctohex(*address++) << 4;
182 addr->b[i] |= ctohex(*address++);
190 C_RESULT vp_com_address_to_str(const bdaddr_t* addr, char* address)
193 char c1 = addr->b[i] & 0xF0;
194 char c2 = addr->b[i] & 0x0F;
196 *address++ = hextoc(c1 >> 4);
197 *address++ = hextoc(c2);
201 c1 = addr->b[i] & 0xF0;
202 c2 = addr->b[i] & 0x0F;
205 *address++ = hextoc(c1 >> 4);
206 *address++ = hextoc(c2);
212 C_RESULT vp_com_copy_address(const bdaddr_t* from,bdaddr_t* to)
214 vp_os_memcpy( to, from, sizeof( bdaddr_t ) );
219 C_RESULT vp_com_cmp_address(const bdaddr_t* bd1, const bdaddr_t* bd2)
223 for( i = 0; i < BDADDR_SIZE && ( bd1->b[i] == bd2->b[i] ); i++ );
225 return ( i < BDADDR_SIZE ) ? VP_COM_ERROR : VP_COM_OK;
228 C_RESULT vp_com_network_adapter_lookup(vp_com_t* vp_com, vp_com_network_adapter_lookup_t callback)
230 return VP_COM_NETWORKADAPTERLOOKUP( callback );
233 C_RESULT vp_com_local_config(vp_com_t* vp_com, vp_com_config_t* config)
237 VP_OS_ASSERT( vp_com != NULL );
239 if( vp_com->config != config )
242 res = VP_COM_LOCAL_CONFIG(config);
245 vp_com->config = config;
252 C_RESULT vp_com_inquire(vp_com_t* vp_com, const char* deviceName, vp_com_inquiry_t callback, uint32_t timeout)
254 VP_OS_ASSERT( vp_com != NULL );
256 return VP_COM_INQUIRE( deviceName, callback, timeout );
259 C_RESULT vp_com_connect(vp_com_t* vp_com, vp_com_connection_t* connection, uint32_t numAttempts)
261 C_RESULT res = VP_COM_OK;
262 bool_t already_connected;
264 VP_OS_ASSERT( vp_com != NULL );
266 if(vp_com->config != NULL)
268 vp_os_mutex_lock(&vp_com->mutex);
270 already_connected = vp_com->connection && vp_com->connection->is_up == 1;
272 // TODO voir pour ajouter un test sur l'adresse ethernet de la connection
273 if( already_connected && vp_com->connection != connection )
275 already_connected = FALSE;
276 vp_com_disconnect(vp_com);
279 if( !already_connected )
281 res = VP_COM_CONNECT(vp_com, connection, numAttempts);
285 vp_com->connection = connection;
286 vp_com->connection->is_up = 1;
290 vp_os_mutex_unlock(&vp_com->mutex);
296 C_RESULT vp_com_disconnect(vp_com_t* vp_com)
298 C_RESULT res = VP_COM_ERROR;
300 VP_OS_ASSERT( vp_com != NULL );
302 if(vp_com->config != NULL && vp_com->connection != NULL)
304 vp_os_mutex_lock(&vp_com->mutex);
306 res = VP_COM_DISCONNECT(vp_com->config, vp_com->connection);
309 vp_com->connection->is_up = 0;
312 vp_os_mutex_unlock(&vp_com->mutex);
318 C_RESULT vp_com_get_rssi(vp_com_t* vp_com, int32_t* rssi)
322 if( vp_com != NULL && vp_com->config != NULL && vp_com->get_rssi != NULL )
323 res = VP_COM_GET_RSSI( vp_com->config, rssi );
333 C_RESULT vp_com_wait_connections(vp_com_t* vp_com, vp_com_socket_t* server, vp_com_socket_t* client, int32_t queueLength)
335 VP_OS_ASSERT( vp_com != NULL );
337 return VP_COM_WAITCONNECTIONS( &vp_com->connection, server, client, queueLength );
340 C_RESULT vp_com_open(vp_com_t* vp_com, vp_com_socket_t* socket,Read* read,Write* write)
342 VP_OS_ASSERT( vp_com != NULL );
344 if( vp_com->ref_count > 0 )
345 return VP_COM_OPEN(vp_com->config, vp_com->connection, socket, read, write);
347 PRINT("[VP_COM] Trying to open a socket with a non initialized vp_com object\n");
352 C_RESULT vp_com_close(vp_com_t* vp_com, vp_com_socket_t* socket)
354 VP_OS_ASSERT( vp_com != NULL );
356 if( vp_com->ref_count > 0 )
357 return VP_COM_CLOSE( socket );
359 PRINT("[VP_COM] Trying to close a socket with a non initialized vp_com object\n");
364 C_RESULT vp_com_sockopt(vp_com_t* vp_com, vp_com_socket_t* socket, VP_COM_SOCKET_OPTIONS options)
368 switch( socket->protocol )
372 res = vp_com_sockopt_ip(vp_com, socket, options);