Skip to content

Commit

Permalink
uri context attribute: add it, so the app doesn't need to keep track
Browse files Browse the repository at this point in the history
Add a "uri" context attribute so the application doesn't need to keep
track of where things were started from.

Signed-off-by: Robin Getz <[email protected]>
  • Loading branch information
rgetz committed May 20, 2020
1 parent 5c12c77 commit 2754f3e
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 50 deletions.
4 changes: 4 additions & 0 deletions local.c
Original file line number Diff line number Diff line change
Expand Up @@ -2063,6 +2063,10 @@ struct iio_context * local_create_context(void)
if (ret < 0)
goto err_context_destroy;

ret = iio_context_add_attr(ctx, "uri", "local:");
if (ret < 0)
goto err_context_destroy;

ret = iio_context_init(ctx);
if (ret < 0)
goto err_context_destroy;
Expand Down
28 changes: 25 additions & 3 deletions network.c
Original file line number Diff line number Diff line change
Expand Up @@ -1293,9 +1293,9 @@ struct iio_context * network_create_context(const char *host)
struct addrinfo hints, *res;
struct iio_context *ctx;
struct iio_context_pdata *pdata;
size_t i, len;
size_t i, len, uri_len;
int fd, ret;
char *description;
char *description, *uri;
#ifdef _WIN32
WSADATA wsaData;

Expand Down Expand Up @@ -1399,10 +1399,21 @@ struct iio_context * network_create_context(const char *host)
len = INET_ADDRSTRLEN + 1;
#endif

uri_len = len;
if (host && host[0])
uri_len = strnlen(host, MAXHOSTNAMELEN);
uri_len += sizeof ("ip:");

uri = malloc(uri_len);
if (!uri) {
ret = -ENOMEM;
goto err_network_shutdown;
}

description = malloc(len);
if (!description) {
ret = -ENOMEM;
goto err_network_shutdown;
goto err_free_uri;
}

description[0] = '\0';
Expand Down Expand Up @@ -1441,6 +1452,15 @@ struct iio_context * network_create_context(const char *host)
if (ret < 0)
goto err_free_description;

if (host && host[0])
iio_snprintf(uri, uri_len, "ip:%s", host);
else
iio_snprintf(uri, uri_len, "ip:%s\n", description);

ret = iio_context_add_attr(ctx, "uri", uri);
if (ret < 0)
goto err_free_description;

for (i = 0; i < ctx->nb_devices; i++) {
struct iio_device *dev = ctx->devices[i];

Expand Down Expand Up @@ -1487,6 +1507,8 @@ struct iio_context * network_create_context(const char *host)

err_free_description:
free(description);
err_free_uri:
free(uri);
err_network_shutdown:
iio_context_destroy(ctx);
errno = -ret;
Expand Down
152 changes: 105 additions & 47 deletions serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define DEFAULT_TIMEOUT_MS 1000

Expand All @@ -41,6 +42,55 @@ struct iio_device_pdata {
bool opened;
};

struct p_options {
char flag;
enum sp_parity parity;
};

struct f_options {
char flag;
enum sp_flowcontrol flowcontrol;
};

static const struct p_options parity_options[] = {
{'n', SP_PARITY_NONE},
{'o', SP_PARITY_ODD},
{'e', SP_PARITY_EVEN},
{'m', SP_PARITY_MARK},
{'s', SP_PARITY_SPACE},
{'\0', SP_PARITY_INVALID},
};

static const struct f_options flow_options[] = {
{'n', SP_FLOWCONTROL_NONE},
{'x', SP_FLOWCONTROL_XONXOFF},
{'r', SP_FLOWCONTROL_RTSCTS},
{'d', SP_FLOWCONTROL_DTRDSR},
{'\0', SP_FLOWCONTROL_NONE},
};

static char flow_char(enum sp_flowcontrol fc)
{
unsigned int i;

for (i = 0; flow_options[i].flag != '\0'; i++) {
if (fc == flow_options[i].flowcontrol)
return flow_options[i].flag;
}
return '\0';
}

static char parity_char(enum sp_parity pc)
{
unsigned int i;

for (i = 0; parity_options[i].flag != '\0'; i++) {
if (pc == parity_options[i].parity)
return parity_options[i].flag;
}
return '\0';
}

static inline int libserialport_to_errno(enum sp_return ret)
{
switch (ret) {
Expand Down Expand Up @@ -322,8 +372,16 @@ static struct iio_context * serial_create_context(const char *port_name,
struct iio_context *ctx;
char *name, *desc, *description;
size_t desc_len;
unsigned int i;
unsigned int i, uri_len;
int ret;
char *uri;

uri_len = sizeof("serial:,1000000,8n1n") + strnlen(port_name, PATH_MAX);
uri = malloc(uri_len);
if (!uri) {
errno = ENOMEM;
return NULL;
}

ret = libserialport_to_errno(sp_get_port_by_name(port_name, &port));
if (ret) {
Expand Down Expand Up @@ -397,9 +455,18 @@ static struct iio_context * serial_create_context(const char *port_name,
}
}

iio_snprintf(uri, uri_len, "serial:%s,%u,%u%c%u%c",
port_name, baud_rate, bits,
parity_char(parity), stop, flow_char(flow));
ret = iio_context_add_attr(ctx, "uri", uri);
if (ret < 0)
goto err_context_destroy;
free(uri);

return ctx;

err_context_destroy:
free(uri);
iio_context_destroy(ctx);
errno = -ret;
return NULL;
Expand All @@ -416,6 +483,7 @@ static struct iio_context * serial_create_context(const char *port_name,
sp_close(port);
err_free_port:
sp_free_port(port);
free(uri);
return NULL;
}

Expand All @@ -439,7 +507,8 @@ static int serial_parse_params(const char *params,
unsigned int *baud_rate, unsigned int *bits, unsigned int *stop,
enum sp_parity *parity, enum sp_flowcontrol *flow)
{
char *end;
char *end, ch;
unsigned int i;

/* Default settings */
*baud_rate = 115200;
Expand All @@ -452,12 +521,11 @@ static int serial_parse_params(const char *params,
return 0;

*baud_rate = strtoul(params, &end, 10);
if (params == end)
return -EINVAL;

/* 110 baud to 1,000,000 baud */
if (*baud_rate < 110 || *baud_rate > 1000001)
if (params == end || *baud_rate < 110 || *baud_rate > 1000001) {
IIO_ERROR("Invalid baud rate\n");
return -EINVAL;
}

if (*end == ',')
end++;
Expand All @@ -468,34 +536,25 @@ static int serial_parse_params(const char *params,
params = (const char *)(end);

*bits = strtoul(params, &end, 10);
if (params == end)
return -EINVAL;

if (*bits > 9 || *bits < 5)
if (params == end || *bits > 9 || *bits < 5) {
IIO_ERROR("Invalid number of bits\n");
return -EINVAL;
}

if (*end == ',')
end++;

switch (*end) {
case '\0':
if (*end == '\0')
return 0;
case 'n':
*parity = SP_PARITY_NONE;
break;
case 'o':
*parity = SP_PARITY_ODD;
break;
case 'e':
*parity = SP_PARITY_EVEN;
break;
case 'm':
*parity = SP_PARITY_MARK;
break;
case 's':
*parity = SP_PARITY_SPACE;
break;
default:
ch = tolower(*end);
for(i = 0; parity_options[i].flag != '\0'; i++) {
if (ch == parity_options[i].flag) {
*parity = parity_options[i].parity;
break;
}
}
if (parity_options[i].flag == '\0') {
IIO_ERROR("Invalid Parity character\n");
return -EINVAL;
}

Expand All @@ -513,36 +572,35 @@ static int serial_parse_params(const char *params,
return 0;
*stop = strtoul(params, &end, 10);

if (params == end)
return -EINVAL;

if (!*stop || *stop > 2)
if (params == end || !*stop || *stop > 2) {
IIO_ERROR("Invalid number of stop bits\n");
return -EINVAL;
}

if (*end == ',')
end++;

switch (*end) {
case '\0':
if (*end == '\0')
return 0;
case 'x':
*flow = SP_FLOWCONTROL_XONXOFF;
break;
case 'r':
*flow = SP_FLOWCONTROL_RTSCTS;
break;
case 'd':
*flow = SP_FLOWCONTROL_DTRDSR;
break;
default:
ch = tolower(*end);
for(i = 0; flow_options[i].flag != '\0'; i++) {
if (ch == flow_options[i].flag) {
*flow = flow_options[i].flowcontrol;
break;
}
}
if (flow_options[i].flag == '\0') {
IIO_ERROR("Invalid Flow Control character\n");
return -EINVAL;
}

/* We should have a '\0' after the flow character */
if (end[1])
if (end[1]) {
IIO_ERROR("Invalid characters after Flow Control flag\n");
return -EINVAL;
else
return 0;
}

return 0;
}

struct iio_context * serial_create_context_from_uri(const char *uri)
Expand Down
8 changes: 8 additions & 0 deletions usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ static int usb_populate_context_attrs(struct iio_context *ctx,
char buffer[64];
unsigned int i;
int ret;
char uri[sizeof("usb:127.255.255")];

struct {
const char *attr;
Expand All @@ -759,6 +760,13 @@ static int usb_populate_context_attrs(struct iio_context *ctx,
attrs[2].attr = "usb,serial";
attrs[2].idx = dev_desc.iSerialNumber;

iio_snprintf(uri, sizeof(uri), "usb:%d.%d.%u",
libusb_get_bus_number(dev), libusb_get_device_address(dev),
(uint8_t)ctx->pdata->interface);
ret = iio_context_add_attr(ctx, "uri", uri);
if (ret < 0)
return ret;

iio_snprintf(buffer, sizeof(buffer), "%04hx", dev_desc.idVendor);
ret = iio_context_add_attr(ctx, "usb,idVendor", buffer);
if (ret < 0)
Expand Down

0 comments on commit 2754f3e

Please sign in to comment.