static void network_shutdown(struct iio_context *ctx) { struct iio_context_pdata *pdata = ctx->pdata; unsigned int i; network_lock(pdata); write_command("\r\nEXIT\r\n", pdata->fd); close(pdata->fd); network_unlock(pdata); for (i = 0; i < ctx->nb_devices; i++) { struct iio_device *dev = ctx->devices[i]; struct iio_device_pdata *dpdata = dev->pdata; if (dpdata) { network_close(dev); #if HAVE_PTHREAD pthread_mutex_destroy(&dpdata->lock); #endif free(dpdata); } } #if HAVE_PTHREAD pthread_mutex_destroy(&pdata->lock); #endif freeaddrinfo(pdata->addrinfo); free(pdata); }
static int set_remote_timeout(struct iio_context *ctx, unsigned int timeout) { char buf[1024]; int ret; snprintf(buf, sizeof(buf), "TIMEOUT %u\r\n", timeout); network_lock(ctx->pdata); ret = (int) exec_command(buf, ctx->pdata->fd); network_unlock(ctx->pdata); return ret; }
static int network_get_trigger(const struct iio_device *dev, const struct iio_device **trigger) { struct iio_context_pdata *pdata = dev->ctx->pdata; unsigned int i; char buf[1024]; ssize_t ret; long resp; snprintf(buf, sizeof(buf), "GETTRIG %s\r\n", dev->id); network_lock(dev->ctx->pdata); resp = exec_command(buf, pdata->fd); if (resp < 0) { network_unlock(pdata); return (int) resp; } else if (resp == 0) { *trigger = NULL; network_unlock(pdata); return 0; } else if ((unsigned long) resp > sizeof(buf)) { ERROR("Value returned by server is too large\n"); network_unlock(pdata); return -EIO; } ret = read_all(buf, resp, pdata->fd); network_unlock(pdata); if (ret < 0) { iio_strerror(-ret, buf, sizeof(buf)); ERROR("Unable to read response to GETTRIG: %s\n", buf); return ret; } if (buf[0] == '\0') { *trigger = NULL; return 0; } for (i = 0; i < dev->ctx->nb_devices; i++) { struct iio_device *cur = dev->ctx->devices[i]; if (iio_device_is_trigger(cur) && !strncmp(cur->name, buf, resp)) { *trigger = cur; return 0; } } return -ENXIO; }
static int network_set_trigger(const struct iio_device *dev, const struct iio_device *trigger) { int ret; char buf[1024]; if (trigger) snprintf(buf, sizeof(buf), "SETTRIG %s %s\r\n", dev->id, trigger->id); else snprintf(buf, sizeof(buf), "SETTRIG %s\r\n", dev->id); network_lock(dev->ctx->pdata); ret = (int) exec_command(buf, dev->ctx->pdata->fd); network_unlock(dev->ctx->pdata); return ret; }
static ssize_t network_read_attr_helper(const struct iio_device *dev, const struct iio_channel *chn, const char *attr, char *dst, size_t len, bool is_debug) { long read_len; ssize_t ret; char buf[1024]; struct iio_context_pdata *pdata = dev->ctx->pdata; int fd = pdata->fd; const char *id = dev->id; if (chn) snprintf(buf, sizeof(buf), "READ %s %s %s %s\r\n", id, chn->is_output ? "OUTPUT" : "INPUT", chn->id, attr ? attr : ""); else if (is_debug) snprintf(buf, sizeof(buf), "READ %s DEBUG %s\r\n", id, attr ? attr : ""); else snprintf(buf, sizeof(buf), "READ %s %s\r\n", id, attr ? attr : ""); network_lock(pdata); read_len = exec_command(buf, fd); if (read_len < 0) { network_unlock(pdata); return (ssize_t) read_len; } if ((unsigned long) read_len > len) { ERROR("Value returned by server is too large\n"); network_unlock(pdata); return -EIO; } ret = read_all(dst, read_len, fd); network_unlock(pdata); if (ret < 0) { iio_strerror(-ret, buf, sizeof(buf)); ERROR("Unable to read response to READ: %s\n", buf); return ret; } return read_len; }
static ssize_t network_write_attr_helper(const struct iio_device *dev, const struct iio_channel *chn, const char *attr, const char *src, size_t len, bool is_debug) { struct iio_context_pdata *pdata = dev->ctx->pdata; int fd; ssize_t ret; long resp; char buf[1024]; const char *id = dev->id; if (chn) snprintf(buf, sizeof(buf), "WRITE %s %s %s %s %lu\r\n", id, chn->is_output ? "OUTPUT" : "INPUT", chn->id, attr ? attr : "", (unsigned long) len); else if (is_debug) snprintf(buf, sizeof(buf), "WRITE %s DEBUG %s %lu\r\n", id, attr ? attr : "", (unsigned long) len); else snprintf(buf, sizeof(buf), "WRITE %s %s %lu\r\n", id, attr ? attr : "", (unsigned long) len); network_lock(pdata); fd = pdata->fd; ret = (ssize_t) write_command(buf, fd); if (ret < 0) goto err_unlock; ret = write_all(src, len, fd); if (ret < 0) goto err_unlock; ret = read_integer(fd, &resp); network_unlock(pdata); if (ret < 0) return ret; return (ssize_t) resp; err_unlock: network_unlock(pdata); return ret; }
MLUPluginFixture() : node(4), v(4), e(8), pid(4) { BlockWriteLock network_lock(network); // setup nodes node[0] = "0"; node[1] = "1"; node[2] = "2"; node[3] = "3"; for (int i = 0; i < 4; ++i) { network.add_node(node[i], v[i], network_lock); network.set_external(v[i], false, network_lock); network.add_autoroute_node(v[i], network_lock); } // setup links and routing int link[8][2] = { {0, 1}, {1, 0}, {1, 2}, {2, 1}, {2, 3}, {3, 2}, {3, 0}, {0, 3}}; double capacity[8] = {2, 4, 4, 4, 4, 4, 4, 4}; double weight[8] = {1, 1, 1, 1, 1, 1, 1, 1}; for (int i = 0; i < 8; ++i) { network.add_edge(node[ link[i][0] ], node[ link[i][1] ], e[i], network_lock); network.set_capacity(e[i], capacity[i], network_lock); network.set_weight(e[i], weight[i], network_lock); } network.update_autoroutes(network_lock); // setup traffic // 0 -> 1: 1 unit of traffic network.set_traffic(e[0], 1.2, network_lock); // setup PIDs pid[0] = NetLocation(1, 0); pid[1] = NetLocation(1, 1); pid[2] = NetLocation(1, 2); pid[3] = NetLocation(1, 3); for (int i = 0; i < 4; ++i) { pid_map[ pid[i] ].insert( v[i] ); } }
static int network_get_version(const struct iio_context *ctx, unsigned int *major, unsigned int *minor, char git_tag[8]) { struct iio_context_pdata *pdata = ctx->pdata; long maj, min; int ret; network_lock(pdata); ret = (int) write_command("VERSION\r\n", pdata->fd); if (ret < 0) goto err_unlock; ret = read_integer(pdata->fd, &maj); if (!ret) ret = read_integer(pdata->fd, &min); if (!ret) { char tag[8]; tag[7] = '\0'; ret = read_all(tag, sizeof(tag) - 1, pdata->fd); if (ret < 0) goto err_unlock; if (major) *major = (unsigned int) maj; if (minor) *minor = (unsigned int) min; if (git_tag) strncpy(git_tag, tag, 8); } ret = 0; err_unlock: network_unlock(pdata); return ret; }