1 // Header ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
4 * \brief Com Api for video sdk. Private declarations.
5 * \author Aurelien Morelle <aurelien.morelle@parrot.com>
10 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
12 // Include //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 #include <sys/types.h>
21 #include <VP_Com/vp_com.h>
22 #include <VP_Com/vp_com_error.h>
24 #include <VP_Os/vp_os_assert.h>
25 #include <VP_Os/vp_os_malloc.h>
26 #include <VP_Os/vp_os_signal.h>
27 #include <VP_Os/vp_os_print.h>
28 #include <VP_Os/vp_os_delay.h>
30 #include <VP_Api/vp_api_error.h>
32 #include "vp_com_serial.h"
34 // Static ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
36 static struct termios tio_save;
37 static vp_os_mutex_t wait_sync_mutex;
38 static vp_os_mutex_t write_sync_mutex;
40 // Forward declarations /////////////////////////////////////////////////////////////////////////////////////////////////////////
42 static C_RESULT vp_com_serial_wait_sync(vp_com_serial_config_t* config, vp_com_socket_t* socket);
43 static C_RESULT vp_com_serial_write_sync(vp_com_serial_config_t* config, vp_com_socket_t* socket);
45 static C_RESULT vp_com_serial_read (vp_com_socket_t* socket, int8_t* buffer, int32_t* size);
46 static C_RESULT vp_com_serial_write(vp_com_socket_t* socket, const int8_t* buffer, int32_t* size);
48 // Functions declaration ////////////////////////////////////////////////////////////////////////////////////////////////////////
50 C_RESULT vp_com_serial_init(void)
52 vp_os_mutex_init(&wait_sync_mutex);
53 vp_os_mutex_init(&write_sync_mutex);
58 C_RESULT vp_com_serial_shutdown()
64 C_RESULT vp_com_serial_local_config(vp_com_serial_config_t* config)
66 C_RESULT res = VP_COM_OK;
68 // Serial port must be opened before changing config
73 C_RESULT vp_com_serial_connect(vp_com_t* vp_com, vp_com_connection_t* connection, int32_t numAttempts)
75 C_RESULT res = VP_COM_OK;
77 res = (connection == NULL) ? VP_COM_PARAMERROR : VP_COM_OK;
80 connection->is_up = 1;
85 C_RESULT vp_com_serial_disconnect(vp_com_serial_config_t* config, vp_com_connection_t* connection)
87 C_RESULT res = (connection == NULL) ? VP_COM_PARAMERROR : VP_COM_OK;
90 connection->is_up = 0;
95 C_RESULT vp_com_serial_open(vp_com_serial_config_t* config, vp_com_connection_t* connection, vp_com_socket_t* socket, Read* read, Write* write)
100 VP_OS_ASSERT(config->blocking == 0 || config->blocking == 1);
102 if(config->blocking == 0)
103 socket->priv = (void *)open(&config->itfName[0], O_RDWR|O_NOCTTY|O_NONBLOCK);
105 socket->priv = (void *)open(&config->itfName[0], O_RDWR|O_NOCTTY);
107 if(((int)socket->priv) == -1)
109 PRINT("Unable to \"open\" serial device");
110 return (VP_COM_ERROR);
113 /* get current serial port settings */
114 if(tcgetattr((int)socket->priv, &tio) != 0)
116 PRINT("Serial device configuration failure (%s)", strerror(errno));
117 close((int)socket->priv);
118 return (VP_COM_ERROR);
125 /* set serial settings */
126 speed = (speed_t)config->initial_baudrate;
127 cfsetispeed(&tio, speed);
128 cfsetospeed(&tio, speed);
131 if(tcsetattr((int)socket->priv, TCSANOW, &tio) != 0)
133 PRINT("Serial device configuration failure (%s)", strerror(errno));
134 close((int)socket->priv);
135 return (VP_COM_ERROR);
138 if(socket->type == VP_COM_CLIENT)
140 if(FAILED(vp_com_serial_write_sync(config, socket)))
141 return (VP_COM_ERROR);
142 vp_os_delay(VP_COM_SYNC_DELAY);
144 else if(socket->type == VP_COM_SERVER)
146 if(FAILED(vp_com_serial_wait_sync(config, socket)))
147 return (VP_COM_ERROR);
148 vp_os_delay(VP_COM_SYNC_DELAY);
152 /* set serial settings */
153 speed = (speed_t)config->baudrate;
154 cfsetispeed(&tio, speed);
155 cfsetospeed(&tio, speed);
158 if(tcsetattr((int)socket->priv, TCSANOW, &tio) != 0)
160 PRINT("Serial device configuration failure (%s)", strerror(errno));
161 close((int)socket->priv);
162 return (VP_COM_ERROR);
165 if(read) *read = (Read) vp_com_serial_read;
166 if(write) *write = (Write) vp_com_serial_write;
171 C_RESULT vp_com_serial_close(vp_com_socket_t* socket)
173 if(tcsetattr((int)socket->priv, TCSANOW, &tio_save) != 0)
175 PRINT("Serial device configuration failure (%s)", strerror(errno));
176 close((int)socket->priv);
177 return (VP_COM_ERROR);
180 close((int)socket->priv);
185 C_RESULT vp_com_serial_wait_connections(vp_com_connection_t** c, vp_com_socket_t* server, vp_com_socket_t* client, int32_t queueLength)
187 vp_os_memcpy(client, server, sizeof(vp_com_socket_t));
191 C_RESULT vp_com_serial_network_adapter_lookup(vp_com_network_adapter_lookup_t callback)
197 C_RESULT vp_com_serial_inquire(const char* deviceName, vp_com_inquiry_t callback, uint32_t timeout)
203 // Static functions /////////////////////////////////////////////////////////////////////////////////////////////////////////////
205 static C_RESULT vp_com_serial_read (vp_com_socket_t* socket, int8_t* buffer, int32_t* size)
207 *size = read((int)socket->priv, buffer, *size);
209 if(*size == -1 && errno != EAGAIN)
219 for(i = 0 ; i < red ; i++)
221 PRINT("%02X", (buffer+(*size-remain))[i]);
223 PRINT(" (%d)\n", red);
230 static C_RESULT vp_com_serial_write(vp_com_socket_t* socket, const int8_t* buffer, int32_t* size)
232 uint32_t remain = *size, written = 0;
236 written = write((int)socket->priv, buffer+(*size-remain), remain);
238 if(written == -1 && errno != EAGAIN)
240 else if(written == -1)
249 static C_RESULT vp_com_serial_wait_sync(vp_com_serial_config_t* config, vp_com_socket_t* socket)
254 vp_os_mutex_lock(&wait_sync_mutex);
256 if(!config->sync_done)
261 if(-1 == read((int)socket->priv, &c, sizeof(int8_t)))
263 if(c == (VP_COM_SYNC_STRING)[nb])
272 while(nb != sizeof(VP_COM_SYNC_STRING));
274 config->sync_done = 1;
277 vp_os_mutex_unlock(&wait_sync_mutex);
282 static C_RESULT vp_com_serial_write_sync(vp_com_serial_config_t* config, vp_com_socket_t* socket)
287 vp_os_mutex_lock(&write_sync_mutex);
289 if(!config->sync_done)
291 for(i = 0 ; i < sizeof(VP_COM_SYNC_STRING) ; i++)
293 c = (VP_COM_SYNC_STRING)[i];
294 if(-1 == write((int)socket->priv, &c, sizeof(int8_t)))
301 config->sync_done = 1;
303 vp_os_mutex_unlock(&write_sync_mutex);
308 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////