X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=serialport.c;h=7796fce2573f37fa066bbc36e92c6e4868d9ab0b;hb=c85d0a28de3bd50ff4237cbd44db28a9f449944f;hp=f95afd0cd0af8d537e6576433654ad0107ac3b87;hpb=c6754b4517d7c9764a887ee3078a3693bfa5c571;p=libserialport.git diff --git a/serialport.c b/serialport.c index f95afd0..7796fce 100644 --- a/serialport.c +++ b/serialport.c @@ -46,7 +46,7 @@ #include "libserialport.h" -struct sp_port_data { +struct port_data { #ifdef _WIN32 DCB dcb; #else @@ -91,11 +91,15 @@ const struct std_baudrate std_baudrates[] = { #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) #define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates) -/* Helper functions for configuring ports. */ -static int get_config(struct sp_port *port, struct sp_port_data *data, struct sp_port_config *config); -static int set_config(struct sp_port *port, struct sp_port_data *data, struct sp_port_config *config); +/* Helper functions. */ +static enum sp_return validate_port(struct sp_port *port); +static struct sp_port **list_append(struct sp_port **list, const char *portname); +static enum sp_return get_config(struct sp_port *port, struct port_data *data, + struct sp_port_config *config); +static enum sp_return set_config(struct sp_port *port, struct port_data *data, + const struct sp_port_config *config); -int sp_get_port_by_name(const char *portname, struct sp_port **port_ptr) +enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr) { struct sp_port *port; int len; @@ -121,12 +125,18 @@ int sp_get_port_by_name(const char *portname, struct sp_port **port_ptr) memcpy(port->name, portname, len); +#ifdef _WIN32 + port->hdl = INVALID_HANDLE_VALUE; +#else + port->fd = -1; +#endif + *port_ptr = port; return SP_OK; } -int sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr) +enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr) { if (!copy_ptr) return SP_ERR_ARG; @@ -150,7 +160,7 @@ void sp_free_port(struct sp_port *port) free(port); } -static struct sp_port **sp_list_append(struct sp_port **list, const char *portname) +static struct sp_port **list_append(struct sp_port **list, const char *portname) { void *tmp; unsigned int count; @@ -169,7 +179,7 @@ fail: return NULL; } -int sp_list_ports(struct sp_port ***list_ptr) +enum sp_return sp_list_ports(struct sp_port ***list_ptr) { struct sp_port **list; int ret = SP_OK; @@ -234,7 +244,7 @@ int sp_list_ports(struct sp_port ***list_ptr) #else strcpy(name, data); #endif - if (type == REG_SZ && !(list = sp_list_append(list, name))) + if (type == REG_SZ && !(list = list_append(list, name))) { ret = SP_ERR_MEM; goto out; @@ -292,7 +302,7 @@ out_done: result = CFStringGetCString(cf_path, path, PATH_MAX, kCFStringEncodingASCII); CFRelease(cf_path); - if (result && !(list = sp_list_append(list, path))) + if (result && !(list = list_append(list, path))) { ret = SP_ERR_MEM; IOObjectRelease(port); @@ -351,7 +361,7 @@ out_done: if (serial_info.type == PORT_UNKNOWN) goto skip; } - list = sp_list_append(list, name); + list = list_append(list, name); skip: udev_device_unref(ud_dev); if (!list) @@ -389,7 +399,7 @@ void sp_free_port_list(struct sp_port **list) free(list); } -static int sp_validate_port(struct sp_port *port) +static enum sp_return validate_port(struct sp_port *port) { if (port == NULL) return 0; @@ -403,9 +413,9 @@ static int sp_validate_port(struct sp_port *port) return 1; } -#define CHECK_PORT() do { if (!sp_validate_port(port)) return SP_ERR_ARG; } while (0) +#define CHECK_PORT() do { if (!validate_port(port)) return SP_ERR_ARG; } while (0) -int sp_open(struct sp_port *port, int flags) +enum sp_return sp_open(struct sp_port *port, enum sp_mode flags) { if (!port) return SP_ERR_ARG; @@ -436,7 +446,7 @@ int sp_open(struct sp_port *port, int flags) return SP_ERR_FAIL; #else int flags_local = 0; - struct sp_port_data data; + struct port_data data; struct sp_port_config config; int ret; @@ -483,7 +493,7 @@ int sp_open(struct sp_port *port, int flags) return SP_OK; } -int sp_close(struct sp_port *port) +enum sp_return sp_close(struct sp_port *port) { CHECK_PORT(); @@ -491,16 +501,18 @@ int sp_close(struct sp_port *port) /* Returns non-zero upon success, 0 upon failure. */ if (CloseHandle(port->hdl) == 0) return SP_ERR_FAIL; + port->hdl = INVALID_HANDLE_VALUE; #else /* Returns 0 upon success, -1 upon failure. */ if (close(port->fd) == -1) return SP_ERR_FAIL; + port->fd = -1; #endif return SP_OK; } -int sp_flush(struct sp_port *port) +enum sp_return sp_flush(struct sp_port *port) { CHECK_PORT(); @@ -516,7 +528,7 @@ int sp_flush(struct sp_port *port) return SP_OK; } -int sp_write(struct sp_port *port, const void *buf, size_t count) +enum sp_return sp_write(struct sp_port *port, const void *buf, size_t count) { CHECK_PORT(); @@ -541,7 +553,7 @@ int sp_write(struct sp_port *port, const void *buf, size_t count) #endif } -int sp_read(struct sp_port *port, void *buf, size_t count) +enum sp_return sp_read(struct sp_port *port, void *buf, size_t count) { CHECK_PORT(); @@ -565,7 +577,8 @@ int sp_read(struct sp_port *port, void *buf, size_t count) #endif } -static int get_config(struct sp_port *port, struct sp_port_data *data, struct sp_port_config *config) +static enum sp_return get_config(struct sp_port *port, struct port_data *data, + struct sp_port_config *config) { unsigned int i; @@ -716,7 +729,8 @@ static int get_config(struct sp_port *port, struct sp_port_data *data, struct sp return SP_OK; } -static int set_config(struct sp_port *port, struct sp_port_data *data, struct sp_port_config *config) +static enum sp_return set_config(struct sp_port *port, struct port_data *data, + const struct sp_port_config *config) { unsigned int i; @@ -995,41 +1009,49 @@ static int set_config(struct sp_port *port, struct sp_port_data *data, struct sp #define TRY(x) do { int ret = x; if (ret != SP_OK) return ret; } while (0) -int sp_set_config(struct sp_port *port, struct sp_port_config *config) +enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config) { - struct sp_port_data data; + struct port_data data; struct sp_port_config prev_config; + CHECK_PORT(); + + if (!config) + return SP_ERR_ARG; + TRY(get_config(port, &data, &prev_config)); TRY(set_config(port, &data, config)); return SP_OK; } -#define CREATE_SETTER(x) int sp_set_##x(struct sp_port *port, int x) { \ - struct sp_port_data data; \ +#define CREATE_SETTER(x, type) int sp_set_##x(struct sp_port *port, type x) { \ + struct port_data data; \ struct sp_port_config config; \ + CHECK_PORT(); \ TRY(get_config(port, &data, &config)); \ config.x = x; \ TRY(set_config(port, &data, &config)); \ return SP_OK; \ } -CREATE_SETTER(baudrate) -CREATE_SETTER(bits) -CREATE_SETTER(parity) -CREATE_SETTER(stopbits) -CREATE_SETTER(rts) -CREATE_SETTER(cts) -CREATE_SETTER(dtr) -CREATE_SETTER(dsr) -CREATE_SETTER(xon_xoff) - -int sp_set_flowcontrol(struct sp_port *port, int flowcontrol) +CREATE_SETTER(baudrate, int) +CREATE_SETTER(bits, int) +CREATE_SETTER(parity, enum sp_parity) +CREATE_SETTER(stopbits, int) +CREATE_SETTER(rts, enum sp_rts) +CREATE_SETTER(cts, enum sp_cts) +CREATE_SETTER(dtr, enum sp_dtr) +CREATE_SETTER(dsr, enum sp_dsr) +CREATE_SETTER(xon_xoff, enum sp_xonxoff) + +enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol) { - struct sp_port_data data; + struct port_data data; struct sp_port_config config; + CHECK_PORT(); + TRY(get_config(port, &data, &config)); if (flowcontrol == SP_FLOWCONTROL_XONXOFF)