]> sigrok.org Git - libsigrok.git/blob - hardware/common/serial.c
serial: Improved docs.
[libsigrok.git] / hardware / common / serial.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
5  * Copyright (C) 2010-2012 Uwe Hermann <uwe@hermann-uwe.de>
6  * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <string.h>
23 #include <stdlib.h>
24 #include <glib.h>
25 #include <libserialport.h>
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
28
29 #define LOG_PREFIX "serial"
30
31 /**
32  * Open the specified serial port.
33  *
34  * @param serial Previously initialized serial port structure.
35  * @param flags Flags to use when opening the serial port. Possible flags
36  *              include SERIAL_RDWR, SERIAL_RDONLY, SERIAL_NONBLOCK.
37  *
38  * If the serial structure contains a serialcomm string, it will be
39  * passed to serial_set_paramstr() after the port is opened.
40  *
41  * @return SR_OK on success, SR_ERR on failure.
42  */
43 SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags)
44 {
45         int ret;
46         char *error;
47         int sp_flags = 0;
48
49         if (!serial) {
50                 sr_dbg("Invalid serial port.");
51                 return SR_ERR;
52         }
53
54         sr_spew("Opening serial port '%s' (flags %d).", serial->port, flags);
55
56         sp_get_port_by_name(serial->port, &serial->data);
57
58         if (flags & SERIAL_RDWR)
59                 sp_flags = (SP_MODE_READ | SP_MODE_WRITE);
60         else if (flags & SERIAL_RDONLY)
61                 sp_flags = SP_MODE_READ;
62
63         serial->nonblocking = (flags & SERIAL_NONBLOCK) ? 1 : 0;
64
65         ret = sp_open(serial->data, sp_flags);
66
67         switch (ret) {
68         case SP_ERR_ARG:
69                 sr_err("Attempt to open serial port with invalid parameters.");
70                 return SR_ERR_ARG;
71         case SP_ERR_FAIL:
72                 error = sp_last_error_message();
73                 sr_err("Error opening port (%d): %s.",
74                         sp_last_error_code(), error);
75                 sp_free_error_message(error);
76                 return SR_ERR;
77         }
78
79         if (serial->serialcomm)
80                 return serial_set_paramstr(serial, serial->serialcomm);
81         else
82                 return SR_OK;
83 }
84
85 /**
86  * Close the specified serial port.
87  *
88  * @param serial Previously initialized serial port structure.
89  *
90  * @return SR_OK on success, SR_ERR on failure.
91  */
92 SR_PRIV int serial_close(struct sr_serial_dev_inst *serial)
93 {
94         int ret;
95         char *error;
96
97         if (!serial) {
98                 sr_dbg("Invalid serial port.");
99                 return SR_ERR;
100         }
101
102         if (!serial->data) {
103                 sr_dbg("Cannot close unopened serial port %s.", serial->port);
104                 return SR_ERR;
105         }
106
107         sr_spew("Closing serial port %s.", serial->port);
108
109         ret = sp_close(serial->data);
110
111         switch (ret) {
112         case SP_ERR_ARG:
113                 sr_err("Attempt to close an invalid serial port.");
114                 return SR_ERR_ARG;
115         case SP_ERR_FAIL:
116                 error = sp_last_error_message();
117                 sr_err("Error closing port (%d): %s.",
118                         sp_last_error_code(), error);
119                 sp_free_error_message(error);
120                 return SR_ERR;
121         }
122
123         sp_free_port(serial->data);
124         serial->data = NULL;
125
126         return SR_OK;
127 }
128
129 /**
130  * Flush serial port buffers.
131  *
132  * @param serial Previously initialized serial port structure.
133  *
134  * @return SR_OK on success, SR_ERR on failure.
135  */
136 SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial)
137 {
138         int ret;
139         char *error;
140
141         if (!serial) {
142                 sr_dbg("Invalid serial port.");
143                 return SR_ERR;
144         }
145
146         if (!serial->data) {
147                 sr_dbg("Cannot flush unopened serial port %s.", serial->port);
148                 return SR_ERR;
149         }
150
151         sr_spew("Flushing serial port %s.", serial->port);
152
153         ret = sp_flush(serial->data, SP_BUF_BOTH);
154
155         switch (ret) {
156         case SP_ERR_ARG:
157                 sr_err("Attempt to flush an invalid serial port.");
158                 return SR_ERR_ARG;
159         case SP_ERR_FAIL:
160                 error = sp_last_error_message();
161                 sr_err("Error flushing port (%d): %s.",
162                         sp_last_error_code(), error);
163                 sp_free_error_message(error);
164                 return SR_ERR;
165         }
166
167         return SR_OK;
168 }
169
170 static int _serial_write(struct sr_serial_dev_inst *serial,
171                 const void *buf, size_t count, int nonblocking)
172 {
173         ssize_t ret;
174         char *error;
175
176         if (!serial) {
177                 sr_dbg("Invalid serial port.");
178                 return SR_ERR;
179         }
180
181         if (!serial->data) {
182                 sr_dbg("Cannot use unopened serial port %s.", serial->port);
183                 return SR_ERR;
184         }
185
186         if (nonblocking)
187                 ret = sp_nonblocking_write(serial->data, buf, count);
188         else
189                 ret = sp_blocking_write(serial->data, buf, count, 0);
190
191         switch (ret) {
192         case SP_ERR_ARG:
193                 sr_err("Attempted serial port write with invalid arguments.");
194                 return SR_ERR_ARG;
195         case SP_ERR_FAIL:
196                 error = sp_last_error_message();
197                 sr_err("Write error (%d): %s.", sp_last_error_code(), error);
198                 sp_free_error_message(error);
199                 return SR_ERR;
200         }
201
202         sr_spew("Wrote %d/%d bytes.", ret, count);
203
204         return ret;
205 }
206
207 /**
208  * Write a number of bytes to the specified serial port.
209  *
210  * @param serial Previously initialized serial port structure.
211  * @param buf Buffer containing the bytes to write.
212  * @param count Number of bytes to write.
213  *
214  * @return The number of bytes written, or a negative error code upon failure.
215  */
216 SR_PRIV int serial_write(struct sr_serial_dev_inst *serial,
217                 const void *buf, size_t count)
218 {
219         return _serial_write(serial, buf, count, serial->nonblocking);
220 }
221
222 SR_PRIV int serial_write_blocking(struct sr_serial_dev_inst *serial,
223                 const void *buf, size_t count)
224 {
225         return _serial_write(serial, buf, count, 0);
226 }
227
228 SR_PRIV int serial_write_nonblocking(struct sr_serial_dev_inst *serial,
229                 const void *buf, size_t count)
230 {
231         return _serial_write(serial, buf, count, 1);
232 }
233
234 static int _serial_read(struct sr_serial_dev_inst *serial, void *buf,
235                 size_t count, int nonblocking)
236 {
237         ssize_t ret;
238         char *error;
239
240         if (!serial) {
241                 sr_dbg("Invalid serial port.");
242                 return SR_ERR;
243         }
244
245         if (!serial->data) {
246                 sr_dbg("Cannot use unopened serial port %s.", serial->port);
247                 return SR_ERR;
248         }
249
250         if (nonblocking)
251                 ret = sp_nonblocking_read(serial->data, buf, count);
252         else
253                 ret = sp_blocking_read(serial->data, buf, count, 0);
254
255         switch (ret) {
256         case SP_ERR_ARG:
257                 sr_err("Attempted serial port read with invalid arguments.");
258                 return SR_ERR_ARG;
259         case SP_ERR_FAIL:
260                 error = sp_last_error_message();
261                 sr_err("Read error (%d): %s.", sp_last_error_code(), error);
262                 sp_free_error_message(error);
263                 return SR_ERR;
264         }
265
266         if (ret > 0)
267                 sr_spew("Read %d/%d bytes.", ret, count);
268
269         return ret;
270 }
271
272 /**
273  * Read a number of bytes from the specified serial port.
274  *
275  * @param serial Previously initialized serial port structure.
276  * @param buf Buffer where to store the bytes that are read.
277  * @param count The number of bytes to read.
278  *
279  * @return The number of bytes read, or a negative error code upon failure.
280  */
281 SR_PRIV int serial_read(struct sr_serial_dev_inst *serial, void *buf,
282                 size_t count)
283 {
284         return _serial_read(serial, buf, count, serial->nonblocking);
285 }
286
287 SR_PRIV int serial_read_blocking(struct sr_serial_dev_inst *serial, void *buf,
288                 size_t count)
289 {
290         return _serial_read(serial, buf, count, 0);
291 }
292
293 SR_PRIV int serial_read_nonblocking(struct sr_serial_dev_inst *serial, void *buf,
294                 size_t count)
295 {
296         return _serial_read(serial, buf, count, 1);
297 }
298
299 /**
300  * Set serial parameters for the specified serial port.
301  *
302  * @param serial Previously initialized serial port structure.
303  * @param[in] baudrate The baudrate to set.
304  * @param[in] bits The number of data bits to use (5, 6, 7 or 8).
305  * @param[in] parity The parity setting to use (0 = none, 1 = even, 2 = odd).
306  * @param[in] stopbits The number of stop bits to use (1 or 2).
307  * @param[in] flowcontrol The flow control settings to use (0 = none,
308  *                      1 = RTS/CTS, 2 = XON/XOFF).
309  * @param[in] rts Status of RTS line (0 or 1; required by some interfaces).
310  * @param[in] dtr Status of DTR line (0 or 1; required by some interfaces).
311  *
312  * @retval SR_OK Success
313  * @retval SR_ERR Failure.
314  */
315 SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate,
316                               int bits, int parity, int stopbits,
317                               int flowcontrol, int rts, int dtr)
318 {
319         int ret;
320         char *error;
321         struct sp_port_config *config;
322
323         if (!serial) {
324                 sr_dbg("Invalid serial port.");
325                 return SR_ERR;
326         }
327
328         if (!serial->data) {
329                 sr_dbg("Cannot configure unopened serial port %s.", serial->port);
330                 return SR_ERR;
331         }
332
333         sr_spew("Setting serial parameters on port %s.", serial->port);
334
335         sp_new_config(&config);
336         sp_set_config_baudrate(config, baudrate);
337         sp_set_config_bits(config, bits);
338         switch (parity) {
339         case 0:
340                 sp_set_config_parity(config, SP_PARITY_NONE);
341                 break;
342         case 1:
343                 sp_set_config_parity(config, SP_PARITY_EVEN);
344                 break;
345         case 2:
346                 sp_set_config_parity(config, SP_PARITY_ODD);
347                 break;
348         default:
349                 return SR_ERR_ARG;
350         }
351         sp_set_config_stopbits(config, stopbits);
352         sp_set_config_rts(config, flowcontrol == 1 ? SP_RTS_FLOW_CONTROL : rts);
353         sp_set_config_cts(config, flowcontrol == 1 ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE);
354         sp_set_config_dtr(config, dtr);
355         sp_set_config_dsr(config, SP_DSR_IGNORE);
356         sp_set_config_xon_xoff(config, flowcontrol == 2 ? SP_XONXOFF_INOUT : SP_XONXOFF_DISABLED);
357
358         ret = sp_set_config(serial->data, config);
359         sp_free_config(config);
360
361         switch (ret) {
362         case SP_ERR_ARG:
363                 sr_err("Invalid arguments for setting serial port parameters.");
364                 return SR_ERR_ARG;
365         case SP_ERR_FAIL:
366                 error = sp_last_error_message();
367                 sr_err("Error setting serial port parameters (%d): %s.",
368                         sp_last_error_code(), error);
369                 sp_free_error_message(error);
370                 return SR_ERR;
371         }
372
373         return SR_OK;
374 }
375
376 /**
377  * Set serial parameters for the specified serial port from parameter string.
378  *
379  * @param serial Previously initialized serial port structure.
380  * @param[in] paramstr A serial communication parameters string of the form
381  * "<baudrate>/<bits><parity><stopbits>{/<option>}".\n
382  *  Examples: "9600/8n1", "600/7o2/dtr=1/rts=0" or "460800/8n1/flow=2".\n
383  * \<baudrate\>=integer Baud rate.\n
384  * \<bits\>=5|6|7|8 Number of data bits.\n
385  * \<parity\>=n|e|o None, even, odd.\n
386  * \<stopbits\>=1|2 One or two stop bits.\n
387  * Options:\n
388  * dtr=0|1 Set DTR off resp. on.\n
389  * flow=0|1|2 Flow control. 0 for none, 1 for RTS/CTS, 2 for XON/XOFF.\n
390  * rts=0|1 Set RTS off resp. on.\n
391  * Please note that values and combinations of these parameters must be
392  * supported by the concrete serial interface hardware and the drivers for it.
393  * @retval SR_OK Success.
394  * @retval SR_ERR Failure.
395  */
396 SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial,
397                 const char *paramstr)
398 {
399 #define SERIAL_COMM_SPEC "^(\\d+)/([5678])([neo])([12])(.*)$"
400
401         GRegex *reg;
402         GMatchInfo *match;
403         int speed, databits, parity, stopbits, flow, rts, dtr, i;
404         char *mstr, **opts, **kv;
405
406         speed = databits = parity = stopbits = flow = 0;
407         rts = dtr = -1;
408         sr_spew("Parsing parameters from \"%s\".", paramstr);
409         reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL);
410         if (g_regex_match(reg, paramstr, 0, &match)) {
411                 if ((mstr = g_match_info_fetch(match, 1)))
412                         speed = strtoul(mstr, NULL, 10);
413                 g_free(mstr);
414                 if ((mstr = g_match_info_fetch(match, 2)))
415                         databits = strtoul(mstr, NULL, 10);
416                 g_free(mstr);
417                 if ((mstr = g_match_info_fetch(match, 3))) {
418                         switch (mstr[0]) {
419                         case 'n':
420                                 parity = SERIAL_PARITY_NONE;
421                                 break;
422                         case 'e':
423                                 parity = SERIAL_PARITY_EVEN;
424                                 break;
425                         case 'o':
426                                 parity = SERIAL_PARITY_ODD;
427                                 break;
428                         }
429                 }
430                 g_free(mstr);
431                 if ((mstr = g_match_info_fetch(match, 4)))
432                         stopbits = strtoul(mstr, NULL, 10);
433                 g_free(mstr);
434                 if ((mstr = g_match_info_fetch(match, 5)) && mstr[0] != '\0') {
435                         if (mstr[0] != '/') {
436                                 sr_dbg("missing separator before extra options");
437                                 speed = 0;
438                         } else {
439                                 /* A set of "key=value" options separated by / */
440                                 opts = g_strsplit(mstr + 1, "/", 0);
441                                 for (i = 0; opts[i]; i++) {
442                                         kv = g_strsplit(opts[i], "=", 2);
443                                         if (!strncmp(kv[0], "rts", 3)) {
444                                                 if (kv[1][0] == '1')
445                                                         rts = 1;
446                                                 else if (kv[1][0] == '0')
447                                                         rts = 0;
448                                                 else {
449                                                         sr_dbg("invalid value for rts: %c", kv[1][0]);
450                                                         speed = 0;
451                                                 }
452                                         } else if (!strncmp(kv[0], "dtr", 3)) {
453                                                 if (kv[1][0] == '1')
454                                                         dtr = 1;
455                                                 else if (kv[1][0] == '0')
456                                                         dtr = 0;
457                                                 else {
458                                                         sr_dbg("invalid value for dtr: %c", kv[1][0]);
459                                                         speed = 0;
460                                                 }
461                                         } else if (!strncmp(kv[0], "flow", 4)) {
462                                                 if (kv[1][0] == '0')
463                                                         flow = 0;
464                                                 else if (kv[1][0] == '1')
465                                                         flow = 1;
466                                                 else if (kv[1][0] == '2')
467                                                         flow = 2;
468                                                 else {
469                                                         sr_dbg("invalid value for flow: %c", kv[1][0]);
470                                                         speed = 0;
471                                                 }
472                                         }
473                                         g_strfreev(kv);
474                                 }
475                                 g_strfreev(opts);
476                         }
477                 }
478                 g_free(mstr);
479         }
480         g_match_info_unref(match);
481         g_regex_unref(reg);
482
483         if (speed) {
484                 return serial_set_params(serial, speed, databits, parity,
485                                          stopbits, flow, rts, dtr);
486         } else {
487                 sr_dbg("Could not infer speed from parameter string.");
488                 return SR_ERR_ARG;
489         }
490 }
491
492 /**
493  * Read a line from the specified serial port.
494  *
495  * @param serial Previously initialized serial port structure.
496  * @param buf Buffer where to store the bytes that are read.
497  * @param buflen Size of the buffer.
498  * @param[in] timeout_ms How long to wait for a line to come in.
499  *
500  * Reading stops when CR of LR is found, which is stripped from the buffer.
501  *
502  * @retval SR_OK Success.
503  * @retval SR_ERR Failure.
504  */
505 SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf,
506                 int *buflen, gint64 timeout_ms)
507 {
508         gint64 start;
509         int maxlen, len;
510
511         if (!serial) {
512                 sr_dbg("Invalid serial port.");
513                 return SR_ERR;
514         }
515
516         if (!serial->data) {
517                 sr_dbg("Cannot use unopened serial port %s.", serial->port);
518                 return -1;
519         }
520
521         timeout_ms *= 1000;
522         start = g_get_monotonic_time();
523
524         maxlen = *buflen;
525         *buflen = len = 0;
526         while(1) {
527                 len = maxlen - *buflen - 1;
528                 if (len < 1)
529                         break;
530                 len = serial_read(serial, *buf + *buflen, 1);
531                 if (len > 0) {
532                         *buflen += len;
533                         *(*buf + *buflen) = '\0';
534                         if (*buflen > 0 && (*(*buf + *buflen - 1) == '\r'
535                                         || *(*buf + *buflen - 1) == '\n')) {
536                                 /* Strip CR/LF and terminate. */
537                                 *(*buf + --*buflen) = '\0';
538                                 break;
539                         }
540                 }
541                 if (g_get_monotonic_time() - start > timeout_ms)
542                         /* Timeout */
543                         break;
544                 if (len < 1)
545                         g_usleep(2000);
546         }
547         if (*buflen)
548                 sr_dbg("Received %d: '%s'.", *buflen, *buf);
549
550         return SR_OK;
551 }
552
553 /**
554  * Try to find a valid packet in a serial data stream.
555  *
556  * @param serial Previously initialized serial port structure.
557  * @param buf Buffer containing the bytes to write.
558  * @param buflen Size of the buffer.
559  * @param[in] packet_size Size, in bytes, of a valid packet.
560  * @param is_valid Callback that assesses whether the packet is valid or not.
561  * @param[in] timeout_ms The timeout after which, if no packet is detected, to
562  *                   abort scanning.
563  * @param[in] baudrate The baudrate of the serial port. This parameter is not
564  *                 critical, but it helps fine tune the serial port polling
565  *                 delay.
566  *
567  * @retval SR_OK Valid packet was found within the given timeout
568  * @retval SR_ERR Failure.
569  */
570 SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
571                                  uint8_t *buf, size_t *buflen,
572                                  size_t packet_size, packet_valid_t is_valid,
573                                  uint64_t timeout_ms, int baudrate)
574 {
575         uint64_t start, time, byte_delay_us;
576         size_t ibuf, i, maxlen;
577         int len;
578
579         maxlen = *buflen;
580
581         sr_dbg("Detecting packets on %s (timeout = %" PRIu64
582                "ms, baudrate = %d).", serial->port, timeout_ms, baudrate);
583
584         if (maxlen < (packet_size / 2) ) {
585                 sr_err("Buffer size must be at least twice the packet size.");
586                 return SR_ERR;
587         }
588
589         /* Assume 8n1 transmission. That is 10 bits for every byte. */
590         byte_delay_us = 10 * (1000000 / baudrate);
591         start = g_get_monotonic_time();
592
593         i = ibuf = len = 0;
594         while (ibuf < maxlen) {
595                 len = serial_read(serial, &buf[ibuf], 1);
596                 if (len > 0) {
597                         ibuf += len;
598                 } else if (len == 0) {
599                         /* No logging, already done in serial_read(). */
600                 } else {
601                         /* Error reading byte, but continuing anyway. */
602                 }
603
604                 time = g_get_monotonic_time() - start;
605                 time /= 1000;
606
607                 if ((ibuf - i) >= packet_size) {
608                         /* We have at least a packet's worth of data. */
609                         if (is_valid(&buf[i])) {
610                                 sr_spew("Found valid %d-byte packet after "
611                                         "%" PRIu64 "ms.", (ibuf - i), time);
612                                 *buflen = ibuf;
613                                 return SR_OK;
614                         } else {
615                                 sr_spew("Got %d bytes, but not a valid "
616                                         "packet.", (ibuf - i));
617                         }
618                         /* Not a valid packet. Continue searching. */
619                         i++;
620                 }
621                 if (time >= timeout_ms) {
622                         /* Timeout */
623                         sr_dbg("Detection timed out after %dms.", time);
624                         break;
625                 }
626                 if (len < 1)
627                         g_usleep(byte_delay_us);
628         }
629
630         *buflen = ibuf;
631
632         sr_err("Didn't find a valid packet (read %d bytes).", *buflen);
633
634         return SR_ERR;
635 }
636
637 /**
638  * Extract the serial device and options from the options linked list.
639  *
640  * @param options List of options passed from the command line.
641  * @param serial_device Pointer where to store the exctracted serial device.
642  * @param serial_options Pointer where to store the optional extracted serial
643  * options.
644  *
645  * @return SR_OK if a serial_device is found, SR_ERR if no device is found. The
646  * returned string should not be freed by the caller.
647  */
648 SR_PRIV int sr_serial_extract_options(GSList *options, const char **serial_device,
649                                       const char **serial_options)
650 {
651         GSList *l;
652         struct sr_config *src;
653
654         *serial_device = NULL;
655
656         for (l = options; l; l = l->next) {
657                 src = l->data;
658                 switch (src->key) {
659                 case SR_CONF_CONN:
660                         *serial_device = g_variant_get_string(src->data, NULL);
661                         sr_dbg("Parsed serial device: %s", *serial_device);
662                         break;
663
664                 case SR_CONF_SERIALCOMM:
665                         *serial_options = g_variant_get_string(src->data, NULL);
666                         sr_dbg("Parsed serial options: %s", *serial_options);
667                         break;
668                 }
669         }
670
671         if (!*serial_device) {
672                 sr_dbg("No serial device specified");
673                 return SR_ERR;
674         }
675
676         return SR_OK;
677 }
678
679 #ifdef _WIN32
680 typedef HANDLE event_handle;
681 #else
682 typedef int event_handle;
683 #endif
684
685 SR_PRIV int serial_source_add(struct sr_serial_dev_inst *serial, int events,
686                 int timeout, sr_receive_data_callback_t cb, void *cb_data)
687 {
688         enum sp_event mask = 0;
689         unsigned int i;
690
691         if (sp_new_event_set(&serial->event_set) != SP_OK)
692                 return SR_ERR;
693
694         if (events & G_IO_IN)
695                 mask |= SP_EVENT_RX_READY;
696         if (events & G_IO_OUT)
697                 mask |= SP_EVENT_TX_READY;
698         if (events & G_IO_ERR)
699                 mask |= SP_EVENT_ERROR;
700
701         if (sp_add_port_events(serial->event_set, serial->data, mask) != SP_OK) {
702                 sp_free_event_set(serial->event_set);
703                 return SR_ERR;
704         }
705
706         serial->pollfds = (GPollFD *) g_malloc0(sizeof(GPollFD) * serial->event_set->count);
707
708         for (i = 0; i < serial->event_set->count; i++) {
709
710                 serial->pollfds[i].fd = ((event_handle *) serial->event_set->handles)[i];
711
712                 mask = serial->event_set->masks[i];
713
714                 if (mask & SP_EVENT_RX_READY)
715                         serial->pollfds[i].events |= G_IO_IN;
716                 if (mask & SP_EVENT_TX_READY)
717                         serial->pollfds[i].events |= G_IO_OUT;
718                 if (mask & SP_EVENT_ERROR)
719                         serial->pollfds[i].events |= G_IO_ERR;
720
721                 if (sr_session_source_add_pollfd(&serial->pollfds[i],
722                                 timeout, cb, cb_data) != SR_OK)
723                         return SR_ERR;
724         }
725
726         return SR_OK;
727 }
728
729 SR_PRIV int serial_source_remove(struct sr_serial_dev_inst *serial)
730 {
731         unsigned int i;
732
733         for (i = 0; i < serial->event_set->count; i++)
734                 if (sr_session_source_remove_pollfd(&serial->pollfds[i]) != SR_OK)
735                         return SR_ERR;
736
737         g_free(serial->pollfds);
738         sp_free_event_set(serial->event_set);
739
740         serial->pollfds = NULL;
741         serial->event_set = NULL;
742
743         return SR_OK;
744 }