]> sigrok.org Git - libserialport.git/blobdiff - serialport.c
Replace sp_set_params with sp_set_config, which takes a struct.
[libserialport.git] / serialport.c
index 838f9c7bee2e76a79da2a8d91333002d92c9d427..3f6b35c8655564ac1fbf226a196f9fa6e1f70ae1 100644 (file)
@@ -116,6 +116,7 @@ static struct sp_port **sp_list_append(struct sp_port **list, const char *portna
 {
        void *tmp;
        unsigned int count;
+
        for (count = 0; list[count]; count++);
        if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2))))
                goto fail;
@@ -124,6 +125,7 @@ static struct sp_port **sp_list_append(struct sp_port **list, const char *portna
                goto fail;
        list[count + 1] = NULL;
        return list;
+
 fail:
        sp_free_port_list(list);
        return NULL;
@@ -135,7 +137,7 @@ int sp_list_ports(struct sp_port ***list_ptr)
        int ret = SP_OK;
 
        if (!(list = malloc(sizeof(struct sp_port **))))
-               return SP_ERR_MEM;;
+               return SP_ERR_MEM;
 
        list[0] = NULL;
 
@@ -343,6 +345,7 @@ out:
 void sp_free_port_list(struct sp_port **list)
 {
        unsigned int i;
+
        for (i = 0; list[i]; i++)
                sp_free_port(list[i]);
        free(list);
@@ -395,6 +398,7 @@ int sp_open(struct sp_port *port, int flags)
                return SP_ERR_FAIL;
 #else
        int flags_local = 0;
+
        /* Map 'flags' to the OS-specific settings. */
        if (flags & SP_MODE_RDWR)
                flags_local |= O_RDWR;
@@ -452,6 +456,7 @@ int sp_write(struct sp_port *port, const void *buf, size_t count)
 
 #ifdef _WIN32
        DWORD written = 0;
+
        /* Returns non-zero upon success, 0 upon failure. */
        if (WriteFile(port->hdl, buf, count, &written, NULL) == 0)
                return SP_ERR_FAIL;
@@ -459,10 +464,11 @@ int sp_write(struct sp_port *port, const void *buf, size_t count)
 #else
        /* Returns the number of bytes written, or -1 upon failure. */
        ssize_t written = write(port->fd, buf, count);
+
        if (written < 0)
                return SP_ERR_FAIL;
        else
-               return written;;
+               return written;
 #endif
 }
 
@@ -475,12 +481,14 @@ int sp_read(struct sp_port *port, void *buf, size_t count)
 
 #ifdef _WIN32
        DWORD bytes_read = 0;
+
        /* Returns non-zero upon success, 0 upon failure. */
        if (ReadFile(port->hdl, buf, count, &bytes_read, NULL) == 0)
                return SP_ERR_FAIL;
        return bytes_read;
 #else
        ssize_t bytes_read;
+
        /* Returns the number of bytes read, or -1 upon failure. */
        if ((bytes_read = read(port->fd, buf, count)) < 0)
                return SP_ERR_FAIL;
@@ -813,19 +821,18 @@ static int apply_config(struct sp_port *port, struct sp_port_data *data)
 
 #define TRY(x) do { int ret = x; if (ret != SP_OK) return ret; } while (0)
 
-int sp_set_params(struct sp_port *port, int baudrate, int bits, int parity,
-               int stopbits, int flowcontrol, int rts, int dtr)
+int sp_set_config(struct sp_port *port, struct sp_port_config *config)
 {
        struct sp_port_data data;
 
        TRY(start_config(port, &data));
-       TRY(set_baudrate(&data, baudrate));
-       TRY(set_bits(&data, bits));
-       TRY(set_parity(&data, parity));
-       TRY(set_stopbits(&data, stopbits));
-       TRY(set_flowcontrol(&data, flowcontrol));
-       TRY(set_rts(&data, rts));
-       TRY(set_dtr(&data, dtr));
+       TRY(set_baudrate(&data, config->baudrate));
+       TRY(set_bits(&data, config->bits));
+       TRY(set_parity(&data, config->parity));
+       TRY(set_stopbits(&data, config->stopbits));
+       TRY(set_flowcontrol(&data, config->flowcontrol));
+       TRY(set_rts(&data, config->rts));
+       TRY(set_dtr(&data, config->dtr));
        TRY(apply_config(port, &data));
 
        return SP_OK;