static int network_open(const struct iio_device *dev, size_t samples_count, bool cyclic) { struct iio_context_pdata *pdata = dev->ctx->pdata; char buf[1024], *ptr; size_t i; int ret, fd; if (dev->pdata->fd >= 0) return -EBUSY; fd = create_socket(pdata->addrinfo); if (fd < 0) return fd; snprintf(buf, sizeof(buf), "OPEN %s %lu ", dev->id, (unsigned long) samples_count); ptr = buf + strlen(buf); for (i = dev->words; i > 0; i--) { snprintf(ptr, (ptr - buf) + i * 8, "%08x", dev->mask[i - 1]); ptr += 8; } strcpy(ptr, cyclic ? " CYCLIC\r\n" : "\r\n"); network_lock_dev(dev->pdata); ret = (int) exec_command(buf, fd); network_unlock_dev(dev->pdata); if (ret < 0) { close(fd); return ret; } dev->pdata->is_tx = iio_device_is_tx(dev); dev->pdata->is_cyclic = cyclic; dev->pdata->fd = fd; dev->pdata->wait_for_err_code = false; return 0; }
static int network_open(const struct iio_device *dev, size_t samples_count, bool cyclic) { struct iio_context_pdata *pdata = dev->ctx->pdata; struct iio_device_pdata *ppdata = dev->pdata; int fd, ret = -EBUSY; iio_mutex_lock(ppdata->lock); if (ppdata->fd >= 0) goto out_mutex_unlock; ret = create_socket(pdata->addrinfo); if (ret < 0) goto out_mutex_unlock; fd = ret; ret = iiod_client_open_unlocked(pdata->iiod_client, fd, dev, samples_count, cyclic); if (ret < 0) { close(fd); goto out_mutex_unlock; } ppdata->is_tx = iio_device_is_tx(dev); ppdata->is_cyclic = cyclic; ppdata->fd = fd; ppdata->wait_for_err_code = false; #ifdef WITH_NETWORK_GET_BUFFER ppdata->mmap_len = samples_count * iio_device_get_sample_size(dev); #endif out_mutex_unlock: iio_mutex_unlock(ppdata->lock); return ret; }
struct iio_buffer * iio_device_create_buffer(const struct iio_device *dev, size_t samples_count, bool cyclic) { int ret = -EINVAL; struct iio_buffer *buf; unsigned int sample_size = iio_device_get_sample_size(dev); if (!sample_size) goto err_set_errno; buf = malloc(sizeof(*buf)); if (!buf) { ret = -ENOMEM; goto err_set_errno; } buf->dev_sample_size = sample_size; buf->length = sample_size * samples_count; buf->dev = dev; buf->mask = calloc(dev->words, sizeof(*buf->mask)); if (!buf->mask) { ret = -ENOMEM; goto err_free_buf; } /* Set the default channel mask to the one used by the device. * While input buffers will erase this as soon as the refill function * is used, it is useful for output buffers, as it permits * iio_buffer_foreach_sample to be used. */ memcpy(buf->mask, dev->mask, dev->words * sizeof(*buf->mask)); ret = iio_device_open(dev, samples_count, cyclic); if (ret < 0) goto err_free_mask; buf->dev_is_high_speed = device_is_high_speed(dev); if (buf->dev_is_high_speed) { /* Dequeue the first buffer, so that buf->buffer is correctly * initialized */ buf->buffer = NULL; if (iio_device_is_tx(dev)) { ret = dev->ctx->ops->get_buffer(dev, &buf->buffer, buf->length, buf->mask, dev->words); if (ret < 0) goto err_close_device; } } else { buf->buffer = malloc(buf->length); if (!buf->buffer) { ret = -ENOMEM; goto err_close_device; } } buf->sample_size = iio_device_get_sample_size_mask(dev, buf->mask, dev->words); buf->data_length = buf->length; return buf; err_close_device: iio_device_close(dev); err_free_mask: free(buf->mask); err_free_buf: free(buf); err_set_errno: errno = -ret; return NULL; }