1 #include <VP_Os/vp_os_malloc.h>
2 #include <VP_Os/vp_os_assert.h>
4 #include <VP_Com/vp_com.h>
5 #include <VP_Com/vp_com_error.h>
7 #include <VP_Com/vp_com_wifi.h>
13 //#include <VP_Com/vp_com_serial.h>
14 #include <VP_Com/vp_com_socket.h>
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_config_t* config, vp_com_connection_t* connection);
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_wait_connections)(vp_com_connection_t** c, vp_com_socket_t* server, vp_com_socket_t* client, int queueLength);
25 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);
26 typedef C_RESULT (*VP_COM_x_close)(vp_com_socket_t* socket);
28 #define VP_COM_INIT vp_com->init
29 #define VP_COM_SHUTDOWN vp_com->shutdown
30 #define VP_COM_NETWORKADAPTERLOOKUP vp_com->network_adapter_lookup
31 #define VP_COM_INQUIRE vp_com->inquire
32 #define VP_COM_LOCAL_CONFIG vp_com->local_config
33 #define VP_COM_CONNECT vp_com->connect
34 #define VP_COM_DISCONNECT vp_com->disconnect
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 C_RESULT vp_com_init(vp_com_t* vp_com)
41 C_RESULT res = VP_COM_NOTSUPPORTED;
43 /* VP_OS_ASSERT( vp_com != NULL ); */
45 /* if(!vp_com->initialized) */
47 /* vp_os_mutex_init(&vp_com->mutex); */
48 /* vp_com->initialized ++; */
51 /* vp_os_mutex_lock(&vp_com->mutex); */
53 /* if(vp_com->ref_count > 0) */
55 /* vp_com->ref_count ++; */
56 /* res = VP_COM_OK; */
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->wait_connections = (VP_COM_x_wait_connections) vp_com_wf_wait_connections; */
73 /* vp_com->open = (VP_COM_x_open) vp_com_wf_open; */
74 /* vp_com->close = (VP_COM_x_close) vp_com_wf_close; */
76 /* #endif // > USE_WIFI */
78 /* #endif // > NO_COM */
81 /* res = VP_COM_INIT(); */
83 /* if(res == VP_COM_OK) */
85 /* vp_os_install_error_handler(VP_COM_SDK_SIGNATURE, vp_com_formatMessage); */
86 /* vp_com->ref_count ++; */
90 /* vp_os_mutex_unlock(&vp_com->mutex); */
95 C_RESULT vp_com_shutdown(vp_com_t* vp_com)
97 /* VP_OS_ASSERT( vp_com != NULL ); */
99 /* vp_os_mutex_lock(&vp_com->mutex); */
101 /* if(vp_com->ref_count > 0) */
103 /* vp_com->ref_count--; */
104 /* if(vp_com->ref_count == 0) */
106 /* vp_os_mutex_unlock(&vp_com->mutex); */
107 /* vp_os_mutex_destroy(&vp_com->mutex); */
109 /* return VP_COM_SHUTDOWN(); */
113 /* vp_os_mutex_unlock(&vp_com->mutex); */
118 C_RESULT vp_com_network_adapter_lookup(vp_com_t* vp_com, vp_com_network_adapter_lookup_t callback)
122 /* VP_OS_ASSERT( vp_com != NULL ); */
124 /* vp_os_mutex_lock(&vp_com->mutex); */
126 /* res = VP_COM_NETWORKADAPTERLOOKUP(callback); */
128 /* vp_os_mutex_unlock(&vp_com->mutex); */
133 C_RESULT vp_com_inquire(vp_com_t* vp_com, const char* deviceName, vp_com_inquiry_t callback, uint32_t timeout)
137 /* VP_OS_ASSERT( vp_com != NULL ); */
139 /* vp_os_mutex_lock(&vp_com->mutex); */
141 /* res = VP_COM_INQUIRE(deviceName, callback, timeout); */
143 /* vp_os_mutex_unlock(&vp_com->mutex); */
148 C_RESULT vp_com_local_config(vp_com_t* vp_com, vp_com_config_t* config)
152 /* VP_OS_ASSERT( vp_com != NULL ); */
154 /* if( vp_com->config != config ) */
157 /* vp_os_mutex_lock(&vp_com->mutex); */
159 /* res = VP_COM_LOCAL_CONFIG(config); */
161 /* if( SUCCEED( res ) ) */
162 /* vp_com->config = config; */
164 /* vp_os_mutex_unlock(&vp_com->mutex); */
171 C_RESULT vp_com_connect(vp_com_t* vp_com, vp_com_connection_t* connection, uint32_t numAttempts)
173 C_RESULT res = VP_COM_OK;
174 /* bool_t already_connected; */
176 /* VP_OS_ASSERT( vp_com != NULL ); */
178 /* if(vp_com->config != NULL) */
180 /* vp_os_mutex_lock(&vp_com->mutex); */
182 /* already_connected = vp_com->connection && vp_com->connection->is_up == 1; */
184 /* // TODO voir pour ajouter un test sur l'adresse ethernet de la connection */
185 /* if( already_connected && vp_com->connection != connection ) */
187 /* already_connected = false; */
188 /* vp_com_disconnect(vp_com); */
191 /* if( !already_connected ) */
193 /* res = VP_COM_CONNECT(vp_com->config, connection); */
195 /* if( SUCCEED( res ) ) */
197 /* vp_com->connection = connection; */
198 /* vp_com->connection->is_up = 1; */
202 /* vp_os_mutex_unlock(&vp_com->mutex); */
208 C_RESULT vp_com_disconnect(vp_com_t* vp_com)
210 C_RESULT res = VP_COM_ERROR;
212 /* VP_OS_ASSERT( vp_com != NULL ); */
214 /* if(vp_com->config != NULL) */
216 /* vp_os_mutex_lock(&vp_com->mutex); */
218 /* res = VP_COM_DISCONNECT(vp_com->config, vp_com->connection); */
220 /* if( SUCCEED( res ) ) */
221 /* vp_com->connection->is_up = 0; */
224 /* vp_os_mutex_unlock(&vp_com->mutex); */
230 C_RESULT vp_com_wait_connections(vp_com_t* vp_com, vp_com_socket_t* server, vp_com_socket_t* client, int32_t queueLength)
232 /* VP_OS_ASSERT( vp_com != NULL ); */
234 /* return VP_COM_WAITCONNECTIONS( &vp_com->connection, server, client, queueLength); */
239 C_RESULT vp_com_open(vp_com_t* vp_com, vp_com_socket_t* socket, Read* read, Write* write)
241 /* VP_OS_ASSERT( vp_com != NULL ); */
243 /* return VP_COM_OPEN(vp_com->config, vp_com->connection, socket, read, write); */
248 C_RESULT vp_com_close(vp_com_t* vp_com, vp_com_socket_t* socket)
250 /* return VP_COM_CLOSE( socket ); */
255 C_RESULT vp_com_sockopt(vp_com_t* vp_com, vp_com_socket_t* socket, VP_COM_SOCKET_OPTIONS options)
257 C_RESULT res = VP_COM_ERROR;
259 /* #ifdef CYGPKG_NET */
260 /* switch( socket->protocol ) */
262 /* case VP_COM_TCP: */
263 /* res = vp_com_sockopt_ip(vp_com, socket, options); */
266 /* case VP_COM_SERIAL: */
267 /* res = VP_COM_OK; */
273 /* #endif // -> CYGPKG_NET */
278 // Convert a char to an hexidecimal value between 0x0 and 0xF
279 static char ctohex(char c)
281 if(c >= '0' && c <= '9')
283 if(c >= 'a' && c <= 'f')
284 return c - 'a' + 0xa;
285 if(c >= 'A' && c <= 'F')
286 return c - 'A' + 0xa;
291 // Convert an hexidecimal value to char
292 static char hextoc(char h)
300 C_RESULT vp_com_str_to_address(const char* address, bdaddr_t* addr)
308 addr->b[i] = ctohex(*address++) << 4;
309 addr->b[i] |= ctohex(*address++);
317 C_RESULT vp_com_address_to_str(const bdaddr_t* addr, char* address)
322 char c1 = addr->b[i] && 0xF0;
323 char c2 = addr->b[i] && 0xF0;
325 *address++ = hextoc(c1);
326 *address++ = hextoc(c2);
327 *address++ = hextoc(':');
333 C_RESULT vp_com_copy_address(const bdaddr_t* from, bdaddr_t* to)
335 vp_os_memcpy(to,from,sizeof(bdaddr_t));
340 C_RESULT vp_com_cmp_address(const bdaddr_t* bd1, const bdaddr_t* bd2)
344 for( i = 0; i < BDADDR_SIZE && ( bd1->b[i] == bd2->b[i] ); i++ );
346 return ( i < BDADDR_SIZE ) ? VP_COM_ERROR : VP_COM_OK;