blob: c4f8963fa8e39fb65c1d78d60100fe95c800782d [file] [log] [blame]
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001/*
2 * libiio - Library for interfacing industrial I/O (IIO) devices
3 *
Paul Cercueil135b3612015-06-30 14:10:14 +02004 * Copyright (C) 2014-2015 Analog Devices, Inc.
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01005 * Author: Paul Cercueil <paul.cercueil@analog.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * */
18
Paul Cercueilc7bad0f2014-03-03 17:06:48 +010019#include "iio-private.h"
Paul Cercueil388dcd62015-11-27 15:15:48 +010020#include "iio-lock.h"
Paul Cercueil157d43c2015-11-30 15:05:06 +010021#include "iiod-client.h"
Paul Cercueil9aabe892014-09-02 12:33:46 +020022
Paul Cercueilc7bad0f2014-03-03 17:06:48 +010023#include <errno.h>
Paul Cercueila7d445b2014-11-11 16:00:15 +010024#include <fcntl.h>
Paul Cercueile60c3ed2014-03-04 11:23:56 +010025#include <stdbool.h>
Paul Cercueilc7bad0f2014-03-03 17:06:48 +010026#include <string.h>
27#include <sys/types.h>
Paul Cercueilab114932014-05-19 13:03:17 +020028#include <time.h>
Paul Cercueil1fef1a52014-04-07 16:31:15 +020029
30#ifdef _WIN32
31#include <winsock2.h>
32#include <ws2tcpip.h>
33#define close(s) closesocket(s)
34
35/* winsock2.h defines ERROR, we don't want that */
36#undef ERROR
37
38#else /* _WIN32 */
Paul Cercueil06c479d2015-01-08 16:14:57 +010039#include <arpa/inet.h>
Paul Cercueil1fef1a52014-04-07 16:31:15 +020040#include <netdb.h>
Paul Cercueil0c3ce452015-02-05 16:37:42 +010041#include <netinet/in.h>
Paul Cercueil0b584e12015-01-28 11:47:03 +010042#include <netinet/tcp.h>
Paul Cercueiled15e492015-01-12 15:52:28 +010043#include <net/if.h>
Paul Cercueil09a43482015-03-04 15:50:27 +010044#include <sys/mman.h>
Paul Cercueil83628b32014-11-24 11:26:21 +010045#include <sys/select.h>
Paul Cercueilc7bad0f2014-03-03 17:06:48 +010046#include <sys/socket.h>
47#include <unistd.h>
Paul Cercueil1fef1a52014-04-07 16:31:15 +020048#endif /* _WIN32 */
49
Paul Cercueilab114932014-05-19 13:03:17 +020050#ifdef HAVE_AVAHI
51#include <avahi-client/client.h>
52#include <avahi-common/error.h>
53#include <avahi-client/lookup.h>
54#include <avahi-common/simple-watch.h>
55#endif
56
Paul Cercueil1fef1a52014-04-07 16:31:15 +020057#include "debug.h"
Paul Cercueilc7bad0f2014-03-03 17:06:48 +010058
Paul Cercueil8a266f12014-06-10 16:06:31 +020059#define DEFAULT_TIMEOUT_MS 5000
60
Paul Cercueil2e38bbe2014-03-19 15:27:15 +010061#define _STRINGIFY(x) #x
62#define STRINGIFY(x) _STRINGIFY(x)
63
Paul Cercueilc7bad0f2014-03-03 17:06:48 +010064#define IIOD_PORT 30431
Paul Cercueil2e38bbe2014-03-19 15:27:15 +010065#define IIOD_PORT_STR STRINGIFY(IIOD_PORT)
Paul Cercueilc7bad0f2014-03-03 17:06:48 +010066
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +010067struct iio_context_pdata {
68 int fd;
Paul Cercueil2dd2c132015-02-24 10:41:01 +010069 struct addrinfo *addrinfo;
Paul Cercueil388dcd62015-11-27 15:15:48 +010070 struct iio_mutex *lock;
Paul Cercueil157d43c2015-11-30 15:05:06 +010071 struct iiod_client *iiod_client;
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +010072};
73
Paul Cercueil439e1a22015-02-24 13:50:51 +010074struct iio_device_pdata {
75 int fd;
Paul Cercueil90189672015-03-16 11:41:53 +010076#ifdef WITH_NETWORK_GET_BUFFER
Paul Cercueil09a43482015-03-04 15:50:27 +010077 int memfd;
Paul Cercueile6e5a092015-03-04 16:33:26 +010078 void *mmap_addr;
Paul Cercueil09a43482015-03-04 15:50:27 +010079 size_t mmap_len;
Paul Cercueile6e5a092015-03-04 16:33:26 +010080#endif
Paul Cercueil09a43482015-03-04 15:50:27 +010081 bool wait_for_err_code, is_cyclic, is_tx;
Paul Cercueil388dcd62015-11-27 15:15:48 +010082 struct iio_mutex *lock;
Paul Cercueil439e1a22015-02-24 13:50:51 +010083};
84
Lars-Peter Clausendd04e6a2016-02-10 14:56:36 +010085#ifdef _WIN32
86static int network_get_error(void)
87{
88 return -WSAGetLastError();
89}
90#else
91static int network_get_error(void)
92{
93 return -errno;
94}
95#endif
96
Paul Cercueilab114932014-05-19 13:03:17 +020097#ifdef HAVE_AVAHI
98struct avahi_discovery_data {
99 AvahiSimplePoll *poll;
100 AvahiAddress *address;
101 uint16_t *port;
102 bool found, resolved;
103};
104
105static void __avahi_resolver_cb(AvahiServiceResolver *resolver,
106 __notused AvahiIfIndex iface, __notused AvahiProtocol proto,
107 __notused AvahiResolverEvent event, __notused const char *name,
108 __notused const char *type, __notused const char *domain,
109 __notused const char *host_name, const AvahiAddress *address,
110 uint16_t port, __notused AvahiStringList *txt,
111 __notused AvahiLookupResultFlags flags, void *d)
112{
113 struct avahi_discovery_data *ddata = (struct avahi_discovery_data *) d;
114
115 memcpy(ddata->address, address, sizeof(*address));
116 *ddata->port = port;
117 ddata->resolved = true;
118 avahi_service_resolver_free(resolver);
119}
120
121static void __avahi_browser_cb(AvahiServiceBrowser *browser,
122 AvahiIfIndex iface, AvahiProtocol proto,
123 AvahiBrowserEvent event, const char *name,
124 const char *type, const char *domain,
125 __notused AvahiLookupResultFlags flags, void *d)
126{
127 struct avahi_discovery_data *ddata = (struct avahi_discovery_data *) d;
128 struct AvahiClient *client = avahi_service_browser_get_client(browser);
129
130 switch (event) {
131 default:
132 case AVAHI_BROWSER_NEW:
133 ddata->found = !!avahi_service_resolver_new(client, iface,
134 proto, name, type, domain,
135 AVAHI_PROTO_UNSPEC, 0,
136 __avahi_resolver_cb, d);
137 break;
138 case AVAHI_BROWSER_ALL_FOR_NOW:
139 if (ddata->found) {
140 while (!ddata->resolved) {
141 struct timespec ts;
142 ts.tv_sec = 0;
143 ts.tv_nsec = 4000000;
144 nanosleep(&ts, NULL);
145 }
146 }
147 case AVAHI_BROWSER_FAILURE: /* fall-through */
148 avahi_simple_poll_quit(ddata->poll);
149 case AVAHI_BROWSER_CACHE_EXHAUSTED: /* fall-through */
150 break;
151 }
152}
153
154static int discover_host(AvahiAddress *addr, uint16_t *port)
155{
156 struct avahi_discovery_data ddata;
157 int ret = 0;
158 AvahiClient *client;
159 AvahiServiceBrowser *browser;
160 AvahiSimplePoll *poll = avahi_simple_poll_new();
161 if (!poll)
162 return -ENOMEM;
163
164 client = avahi_client_new(avahi_simple_poll_get(poll),
165 0, NULL, NULL, &ret);
166 if (!client) {
167 ERROR("Unable to start ZeroConf client :%s\n",
168 avahi_strerror(ret));
169 goto err_free_poll;
170 }
171
172 memset(&ddata, 0, sizeof(ddata));
173 ddata.poll = poll;
174 ddata.address = addr;
175 ddata.port = port;
176
177 browser = avahi_service_browser_new(client,
178 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
179 "_iio._tcp", NULL, 0, __avahi_browser_cb, &ddata);
180 if (!browser) {
181 ret = avahi_client_errno(client);
182 ERROR("Unable to create ZeroConf browser: %s\n",
183 avahi_strerror(ret));
184 goto err_free_client;
185 }
186
187 DEBUG("Trying to discover host\n");
188 avahi_simple_poll_loop(poll);
189
190 if (!ddata.found)
191 ret = ENXIO;
192
193 avahi_service_browser_free(browser);
194err_free_client:
195 avahi_client_free(client);
196err_free_poll:
197 avahi_simple_poll_free(poll);
198 return -ret; /* we want a negative error code */
199}
200#endif /* HAVE_AVAHI */
201
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100202static ssize_t write_all(const void *src, size_t len, int fd)
203{
Paul Cercueil6e7f79e2014-04-04 12:27:24 +0200204 uintptr_t ptr = (uintptr_t) src;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100205 while (len) {
Paul Cercueil4012cff2015-05-11 10:47:40 +0200206 ssize_t ret = send(fd, (const void *) ptr, (int) len, 0);
Lars-Peter Clausencb7b8bf2014-12-05 16:08:52 +0100207 if (ret < 0) {
Lars-Peter Clausendd04e6a2016-02-10 14:56:36 +0100208 int err = network_get_error();
209 if (err == -EINTR)
Lars-Peter Clausencb7b8bf2014-12-05 16:08:52 +0100210 continue;
Lars-Peter Clausendd04e6a2016-02-10 14:56:36 +0100211 return (ssize_t) err;
Lars-Peter Clausencb7b8bf2014-12-05 16:08:52 +0100212 }
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100213 ptr += ret;
214 len -= ret;
215 }
Paul Cercueil4012cff2015-05-11 10:47:40 +0200216 return (ssize_t)(ptr - (uintptr_t) src);
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100217}
218
219static ssize_t read_all(void *dst, size_t len, int fd)
220{
Paul Cercueil6e7f79e2014-04-04 12:27:24 +0200221 uintptr_t ptr = (uintptr_t) dst;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100222 while (len) {
Paul Cercueil4012cff2015-05-11 10:47:40 +0200223 ssize_t ret = recv(fd, (void *) ptr, (int) len, 0);
Lars-Peter Clausencb7b8bf2014-12-05 16:08:52 +0100224 if (ret < 0) {
Lars-Peter Clausendd04e6a2016-02-10 14:56:36 +0100225 int err = network_get_error();
226 if (err == -EINTR)
Lars-Peter Clausencb7b8bf2014-12-05 16:08:52 +0100227 continue;
Lars-Peter Clausendd04e6a2016-02-10 14:56:36 +0100228 return (ssize_t) err;
Lars-Peter Clausencb7b8bf2014-12-05 16:08:52 +0100229 }
Paul Cercueil43eb7e82014-11-13 12:46:59 +0100230 if (ret == 0)
231 return -EPIPE;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100232 ptr += ret;
233 len -= ret;
234 }
Paul Cercueil4012cff2015-05-11 10:47:40 +0200235 return (ssize_t)(ptr - (uintptr_t) dst);
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100236}
237
Paul Cercueile60c3ed2014-03-04 11:23:56 +0100238static int read_integer(int fd, long *val)
239{
240 unsigned int i;
241 char buf[1024], *ptr;
242 ssize_t ret;
243 bool found = false;
244
245 for (i = 0; i < sizeof(buf) - 1; i++) {
246 ret = read_all(buf + i, 1, fd);
247 if (ret < 0)
248 return (int) ret;
249
Paul Cercueil6691a3f2014-05-02 12:32:01 +0200250 /* Skip the eventual first few carriage returns.
251 * Also stop when a dot is found (for parsing floats) */
252 if (buf[i] != '\n' && buf[i] != '.')
Paul Cercueile60c3ed2014-03-04 11:23:56 +0100253 found = true;
254 else if (found)
255 break;
256 }
257
258 buf[i] = '\0';
259 ret = (ssize_t) strtol(buf, &ptr, 10);
260 if (ptr == buf)
261 return -EINVAL;
262 *val = (long) ret;
263 return 0;
264}
265
Paul Cercueilbb76bf92014-03-11 10:20:18 +0100266static ssize_t write_command(const char *cmd, int fd)
267{
268 ssize_t ret;
269
270 DEBUG("Writing command: %s\n", cmd);
271 ret = write_all(cmd, strlen(cmd), fd);
272 if (ret < 0) {
273 char buf[1024];
Paul Cercueil53fc4852015-05-22 10:57:53 +0200274 iio_strerror(-ret, buf, sizeof(buf));
Paul Cercueilbb76bf92014-03-11 10:20:18 +0100275 ERROR("Unable to send command: %s\n", buf);
276 }
277 return ret;
278}
279
Paul Cercueil4970ac32015-02-24 10:59:00 +0100280#ifndef _WIN32
Paul Cercueil4970ac32015-02-24 10:59:00 +0100281/* The purpose of this function is to provide a version of connect()
282 * that does not ignore timeouts... */
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100283static int do_connect(const struct addrinfo *addrinfo,
284 struct timeval *timeout)
Paul Cercueil4970ac32015-02-24 10:59:00 +0100285{
286 int ret, error;
287 socklen_t len;
288 fd_set set;
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100289 int fd;
290
291 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype, 0);
292 if (fd < 0)
293 return -errno;
Paul Cercueil4970ac32015-02-24 10:59:00 +0100294
295 FD_ZERO(&set);
296 FD_SET(fd, &set);
297
298 ret = set_blocking_mode(fd, false);
Lars-Peter Clausenebbd9072016-02-11 15:15:08 +0100299 if (ret < 0) {
300 close(fd);
Paul Cercueil4970ac32015-02-24 10:59:00 +0100301 return ret;
Lars-Peter Clausenebbd9072016-02-11 15:15:08 +0100302 }
Paul Cercueil4970ac32015-02-24 10:59:00 +0100303
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100304 ret = connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen);
Paul Cercueil4970ac32015-02-24 10:59:00 +0100305 if (ret < 0 && errno != EINPROGRESS) {
306 ret = -errno;
307 goto end;
308 }
309
310 ret = select(fd + 1, &set, &set, NULL, timeout);
311 if (ret < 0) {
312 ret = -errno;
313 goto end;
314 }
315 if (ret == 0) {
316 ret = -ETIMEDOUT;
317 goto end;
318 }
319
320 /* Verify that we don't have an error */
321 len = sizeof(error);
322 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len);
323 if(ret < 0) {
324 ret = -errno;
325 goto end;
326 }
327 if (error) {
328 ret = -error;
329 goto end;
330 }
331
332end:
333 /* Restore blocking mode */
334 set_blocking_mode(fd, true);
Lars-Peter Clausenebbd9072016-02-11 15:15:08 +0100335 if (ret < 0) {
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100336 close(fd);
Lars-Peter Clausenebbd9072016-02-11 15:15:08 +0100337 return ret;
338 }
339
340 return fd;
Paul Cercueil4970ac32015-02-24 10:59:00 +0100341}
342
343static int set_socket_timeout(int fd, unsigned int timeout)
344{
345 struct timeval tv;
346
347 tv.tv_sec = timeout / 1000;
348 tv.tv_usec = (timeout % 1000) * 1000;
349 if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0 ||
350 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,
351 &tv, sizeof(tv)) < 0)
352 return -errno;
353 else
354 return 0;
355}
356#else
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100357
358static int do_connect(const struct addrinfo *addrinfo,
359 struct timeval *timeout)
360{
361 int ret;
362 SOCKET s;
363
364 s = socket(addrinfo->ai_family, addrinfo->ai_socktype, 0);
365 if (s == INVALID_SOCKET)
366 return -WSAGetLastError();
367
368 ret = connect(s, addrinfo->ai_addr, (int) addrinfo->ai_addrlen);
369 if (ret == SOCKET_ERROR) {
370 close(s);
371 return -WSAGetLastError();
372 }
373
374 return (int) s;
375}
376
Paul Cercueil4970ac32015-02-24 10:59:00 +0100377static int set_socket_timeout(int fd, unsigned int timeout)
378{
379 if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO,
380 (const char *) &timeout, sizeof(timeout)) < 0 ||
381 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,
382 (const char *) &timeout, sizeof(timeout)) < 0)
Paul Cercueil135b3612015-06-30 14:10:14 +0200383 return -WSAGetLastError();
Paul Cercueil4970ac32015-02-24 10:59:00 +0100384 else
385 return 0;
386}
387#endif /* !_WIN32 */
388
389static int create_socket(const struct addrinfo *addrinfo)
390{
391 struct timeval timeout;
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100392 int fd, yes = 1;
Paul Cercueil4970ac32015-02-24 10:59:00 +0100393
394 timeout.tv_sec = DEFAULT_TIMEOUT_MS / 1000;
395 timeout.tv_usec = (DEFAULT_TIMEOUT_MS % 1000) * 1000;
396
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100397 fd = do_connect(addrinfo, &timeout);
398 if (fd < 0)
399 return fd;
Paul Cercueil4970ac32015-02-24 10:59:00 +0100400
401 set_socket_timeout(fd, DEFAULT_TIMEOUT_MS);
402 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
403 (const char *) &yes, sizeof(yes));
404 return fd;
405}
406
Paul Cercueil92f15c22015-04-20 11:36:51 +0200407static int network_open(const struct iio_device *dev,
408 size_t samples_count, bool cyclic)
Paul Cercueilba059762014-03-14 11:02:02 +0100409{
Paul Cercueil439e1a22015-02-24 13:50:51 +0100410 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueil80dc1082015-12-01 18:20:31 +0100411 struct iio_device_pdata *ppdata = dev->pdata;
412 int fd, ret = -EBUSY;
Paul Cercueilba059762014-03-14 11:02:02 +0100413
Paul Cercueil80dc1082015-12-01 18:20:31 +0100414 iio_mutex_lock(ppdata->lock);
415 if (ppdata->fd >= 0)
416 goto out_mutex_unlock;
Paul Cercueil439e1a22015-02-24 13:50:51 +0100417
Paul Cercueil80dc1082015-12-01 18:20:31 +0100418 ret = create_socket(pdata->addrinfo);
419 if (ret < 0)
420 goto out_mutex_unlock;
Paul Cercueilba059762014-03-14 11:02:02 +0100421
Paul Cercueil80dc1082015-12-01 18:20:31 +0100422 fd = ret;
Paul Cercueilba059762014-03-14 11:02:02 +0100423
Paul Cercueil80dc1082015-12-01 18:20:31 +0100424 ret = iiod_client_open_unlocked(pdata->iiod_client, fd,
425 dev, samples_count, cyclic);
Paul Cercueil44ae11c2014-03-17 09:56:29 +0100426 if (ret < 0) {
Paul Cercueil439e1a22015-02-24 13:50:51 +0100427 close(fd);
Paul Cercueil80dc1082015-12-01 18:20:31 +0100428 goto out_mutex_unlock;
Paul Cercueil44ae11c2014-03-17 09:56:29 +0100429 }
Paul Cercueil439e1a22015-02-24 13:50:51 +0100430
Paul Cercueil80dc1082015-12-01 18:20:31 +0100431 ppdata->is_tx = iio_device_is_tx(dev);
432 ppdata->is_cyclic = cyclic;
433 ppdata->fd = fd;
434 ppdata->wait_for_err_code = false;
Paul Cercueild957b982015-08-05 14:39:01 +0200435#ifdef WITH_NETWORK_GET_BUFFER
Paul Cercueil80dc1082015-12-01 18:20:31 +0100436 ppdata->mmap_len = samples_count * iio_device_get_sample_size(dev);
Paul Cercueild957b982015-08-05 14:39:01 +0200437#endif
Paul Cercueil80dc1082015-12-01 18:20:31 +0100438
439out_mutex_unlock:
440 iio_mutex_unlock(ppdata->lock);
441 return ret;
Paul Cercueilba059762014-03-14 11:02:02 +0100442}
443
Paul Cercueil8f7f6c42015-02-24 15:00:35 +0100444static ssize_t read_error_code(int fd)
445{
446 /*
447 * The server returns two integer codes.
448 * The first one is returned right after the WRITEBUF command is issued,
449 * and corresponds to the error code returned when the server attempted
450 * to open the device.
451 * If zero, a second error code is returned, that corresponds (if positive)
452 * to the number of bytes written.
453 *
454 * To speed up things, we delay error reporting. We just send out the
455 * data without reading the error code that the server gives us, because
456 * the answer will take too much time. If an error occured, it will be
457 * reported by the next call to iio_buffer_push().
458 */
459
460 unsigned int i;
461 long resp = 0;
462
463 for (i = 0; i < 2; i++) {
464 ssize_t ret = read_integer(fd, &resp);
465 if (ret < 0)
466 return ret;
467 if (resp < 0)
468 return (ssize_t) resp;
469 }
470
Paul Cercueil7d7e5d32015-03-05 10:51:41 +0100471 return (ssize_t) resp;
Paul Cercueil8f7f6c42015-02-24 15:00:35 +0100472}
473
Paul Cercueil3c0da372015-03-05 11:36:39 +0100474static ssize_t write_rwbuf_command(const struct iio_device *dev,
Lars-Peter Clausen2e380822016-02-11 20:36:13 +0100475 const char *cmd)
Paul Cercueil3c0da372015-03-05 11:36:39 +0100476{
477 struct iio_device_pdata *pdata = dev->pdata;
478 int fd = pdata->fd;
479
480 if (pdata->wait_for_err_code) {
481 ssize_t ret = read_error_code(fd);
482
483 pdata->wait_for_err_code = false;
484 if (ret < 0)
485 return ret;
486 }
487
Lars-Peter Clausen78067fc2016-02-11 12:48:01 +0100488 return write_command(cmd, fd);
Paul Cercueil3c0da372015-03-05 11:36:39 +0100489}
490
Paul Cercueilba059762014-03-14 11:02:02 +0100491static int network_close(const struct iio_device *dev)
492{
Paul Cercueil439e1a22015-02-24 13:50:51 +0100493 struct iio_device_pdata *pdata = dev->pdata;
Paul Cercueild5d84612015-06-08 21:24:15 +0200494 int ret = -EBADF;
Paul Cercueil80dc1082015-12-01 18:20:31 +0100495
496 iio_mutex_lock(pdata->lock);
Paul Cercueil439e1a22015-02-24 13:50:51 +0100497
Paul Cercueila34596e2015-03-05 14:33:46 +0100498 if (pdata->fd >= 0) {
Paul Cercueil80dc1082015-12-01 18:20:31 +0100499 ret = iiod_client_close_unlocked(dev->ctx->pdata->iiod_client,
500 pdata->fd, dev);
Paul Cercueil1494b862014-05-05 12:49:07 +0200501
Paul Cercueila34596e2015-03-05 14:33:46 +0100502 write_command("\r\nEXIT\r\n", pdata->fd);
Paul Cercueilaa0db142015-03-04 16:25:24 +0100503
Paul Cercueila34596e2015-03-05 14:33:46 +0100504 close(pdata->fd);
505 pdata->fd = -1;
Paul Cercueila34596e2015-03-05 14:33:46 +0100506 }
Paul Cercueile6e5a092015-03-04 16:33:26 +0100507
Paul Cercueil90189672015-03-16 11:41:53 +0100508#ifdef WITH_NETWORK_GET_BUFFER
Paul Cercueile6e5a092015-03-04 16:33:26 +0100509 if (pdata->memfd >= 0)
510 close(pdata->memfd);
511 pdata->memfd = -1;
512
513 if (pdata->mmap_addr) {
514 munmap(pdata->mmap_addr, pdata->mmap_len);
515 pdata->mmap_addr = NULL;
516 }
517#endif
Paul Cercueil80dc1082015-12-01 18:20:31 +0100518
519 iio_mutex_unlock(pdata->lock);
Paul Cercueil1494b862014-05-05 12:49:07 +0200520 return ret;
Paul Cercueilba059762014-03-14 11:02:02 +0100521}
522
Paul Cercueil7d7e5d32015-03-05 10:51:41 +0100523static ssize_t network_read_mask(int fd, uint32_t *mask, size_t words)
524{
525 long read_len;
526 ssize_t ret;
527
528 ret = read_integer(fd, &read_len);
529 if (ret < 0)
530 return ret;
531
532 if (read_len > 0 && mask) {
Paul Cercueil4012cff2015-05-11 10:47:40 +0200533 size_t i;
Paul Cercueil7d7e5d32015-03-05 10:51:41 +0100534 char buf[9];
535
536 buf[8] = '\0';
537 DEBUG("Reading mask\n");
538
539 for (i = words; i > 0; i--) {
540 ret = read_all(buf, 8, fd);
541 if (ret < 0)
542 return ret;
543
544 sscanf(buf, "%08x", &mask[i - 1]);
545 DEBUG("mask[%i] = 0x%x\n", i - 1, mask[i - 1]);
546 }
547 }
548
549 if (read_len > 0) {
550 char c;
551 ssize_t nb = read_all(&c, 1, fd);
552 if (nb > 0 && c != '\n')
553 read_len = -EIO;
554 }
555
556 return (ssize_t) read_len;
557}
558
Paul Cercueil45c575d2014-03-20 15:14:01 +0100559static ssize_t network_read(const struct iio_device *dev, void *dst, size_t len,
560 uint32_t *mask, size_t words)
Paul Cercueilf9286452014-03-18 14:32:17 +0100561{
Paul Cercueilc5b00752015-02-24 14:29:53 +0100562 struct iio_device_pdata *pdata = dev->pdata;
Paul Cercueil72793642015-12-02 11:57:10 +0100563 ssize_t ret;
Paul Cercueil1494b862014-05-05 12:49:07 +0200564
Paul Cercueil388dcd62015-11-27 15:15:48 +0100565 iio_mutex_lock(pdata->lock);
Paul Cercueil72793642015-12-02 11:57:10 +0100566 ret = iiod_client_read_unlocked(dev->ctx->pdata->iiod_client,
567 pdata->fd, dev, dst, len, mask, words);
Paul Cercueil388dcd62015-11-27 15:15:48 +0100568 iio_mutex_unlock(pdata->lock);
Paul Cercueil72793642015-12-02 11:57:10 +0100569
570 return ret;
Paul Cercueil9945bc82014-03-05 14:07:29 +0100571}
572
Paul Cercueila62f84e2015-02-24 14:16:08 +0100573static ssize_t network_write(const struct iio_device *dev,
574 const void *src, size_t len)
Paul Cercueil2725f702014-05-02 11:02:16 +0200575{
Paul Cercueilc5b00752015-02-24 14:29:53 +0100576 struct iio_device_pdata *pdata = dev->pdata;
Paul Cercueil2725f702014-05-02 11:02:16 +0200577 ssize_t ret;
Paul Cercueil2725f702014-05-02 11:02:16 +0200578
Paul Cercueil388dcd62015-11-27 15:15:48 +0100579 iio_mutex_lock(pdata->lock);
Paul Cercueilf19568a2015-12-02 17:57:51 +0100580 ret = iiod_client_write_unlocked(dev->ctx->pdata->iiod_client,
581 pdata->fd, dev, src, len);
Paul Cercueil388dcd62015-11-27 15:15:48 +0100582 iio_mutex_unlock(pdata->lock);
Paul Cercueil1494b862014-05-05 12:49:07 +0200583
Paul Cercueil1494b862014-05-05 12:49:07 +0200584 return ret;
Paul Cercueil2725f702014-05-02 11:02:16 +0200585}
586
Paul Cercueil90189672015-03-16 11:41:53 +0100587#ifdef WITH_NETWORK_GET_BUFFER
Paul Cercueil7d7e5d32015-03-05 10:51:41 +0100588static ssize_t network_do_splice(int fd_out, int fd_in, size_t len)
Paul Cercueil09a43482015-03-04 15:50:27 +0100589{
590 int pipefd[2];
Paul Cercueil04065c22015-03-05 14:08:16 +0100591 ssize_t ret, read_len = len;
Paul Cercueil09a43482015-03-04 15:50:27 +0100592
593 ret = (ssize_t) pipe(pipefd);
594 if (ret < 0)
595 return -errno;
596
597 do {
Paul Cercueila107c6d2015-03-05 13:54:10 +0100598 /*
599 * SPLICE_F_NONBLOCK is just here to avoid a deadlock when
600 * splicing from a socket. As the socket is not in
601 * non-blocking mode, it should never return -EAGAIN.
602 * TODO(pcercuei): Find why it locks...
603 * */
604 ret = splice(fd_in, NULL, pipefd[1], NULL, len,
605 SPLICE_F_MOVE | SPLICE_F_NONBLOCK);
606 if (!ret)
607 ret = -EIO;
Paul Cercueil09a43482015-03-04 15:50:27 +0100608 if (ret < 0)
609 goto err_close_pipe;
610
Paul Cercueila107c6d2015-03-05 13:54:10 +0100611 ret = splice(pipefd[0], NULL, fd_out, NULL, ret,
612 SPLICE_F_MOVE | SPLICE_F_NONBLOCK);
613 if (!ret)
614 ret = -EIO;
Paul Cercueil09a43482015-03-04 15:50:27 +0100615 if (ret < 0)
616 goto err_close_pipe;
617
618 len -= ret;
619 } while (len);
620
621err_close_pipe:
622 close(pipefd[0]);
623 close(pipefd[1]);
Paul Cercueil04065c22015-03-05 14:08:16 +0100624 return ret < 0 ? ret : read_len;
Paul Cercueil09a43482015-03-04 15:50:27 +0100625}
626
627static ssize_t network_get_buffer(const struct iio_device *dev,
Paul Cercueil76ca8842015-03-05 11:16:16 +0100628 void **addr_ptr, size_t bytes_used,
629 uint32_t *mask, size_t words)
Paul Cercueil09a43482015-03-04 15:50:27 +0100630{
631 struct iio_device_pdata *pdata = dev->pdata;
Paul Cercueil04065c22015-03-05 14:08:16 +0100632 ssize_t ret, read = 0;
Paul Cercueilca9d3382015-05-04 14:30:09 +0200633 int memfd;
Paul Cercueil09a43482015-03-04 15:50:27 +0100634 bool tx;
Paul Cercueil09a43482015-03-04 15:50:27 +0100635
Paul Cercueil04065c22015-03-05 14:08:16 +0100636 if (pdata->is_cyclic)
Paul Cercueil09a43482015-03-04 15:50:27 +0100637 return -ENOSYS;
Paul Cercueilca9d3382015-05-04 14:30:09 +0200638
639 /* We check early that the temporary file can be created, so that we can
640 * return -ENOSYS in case it fails, which will indicate that the
641 * high-speed interface is not available.
642 *
643 * O_TMPFILE -> Linux 3.11.
644 * TODO: use memfd_create (Linux 3.17) */
645 memfd = open(P_tmpdir, O_RDWR | O_TMPFILE | O_EXCL, S_IRWXU);
646 if (memfd < 0)
647 return -ENOSYS;
648
649 if (!addr_ptr || words != (dev->nb_channels + 31) / 32) {
650 close(memfd);
Paul Cercueil09a43482015-03-04 15:50:27 +0100651 return -EINVAL;
Paul Cercueilca9d3382015-05-04 14:30:09 +0200652 }
Paul Cercueil09a43482015-03-04 15:50:27 +0100653
Paul Cercueile6e5a092015-03-04 16:33:26 +0100654 if (pdata->mmap_addr)
655 munmap(pdata->mmap_addr, pdata->mmap_len);
Paul Cercueil09a43482015-03-04 15:50:27 +0100656
Paul Cercueile6e5a092015-03-04 16:33:26 +0100657 if (pdata->mmap_addr && pdata->is_tx) {
Paul Cercueil09a43482015-03-04 15:50:27 +0100658 char buf[1024];
659 snprintf(buf, sizeof(buf), "WRITEBUF %s %lu\r\n",
Paul Cercueild957b982015-08-05 14:39:01 +0200660 dev->id, (unsigned long) bytes_used);
Paul Cercueil09a43482015-03-04 15:50:27 +0100661
Paul Cercueil388dcd62015-11-27 15:15:48 +0100662 iio_mutex_lock(pdata->lock);
Paul Cercueil09a43482015-03-04 15:50:27 +0100663
Lars-Peter Clausen78067fc2016-02-11 12:48:01 +0100664 ret = write_rwbuf_command(dev, buf);
Paul Cercueil09a43482015-03-04 15:50:27 +0100665 if (ret < 0)
Paul Cercueilca9d3382015-05-04 14:30:09 +0200666 goto err_close_memfd;
Paul Cercueil09a43482015-03-04 15:50:27 +0100667
Paul Cercueild957b982015-08-05 14:39:01 +0200668 ret = network_do_splice(pdata->fd, pdata->memfd, bytes_used);
Paul Cercueil09a43482015-03-04 15:50:27 +0100669 if (ret < 0)
Paul Cercueilca9d3382015-05-04 14:30:09 +0200670 goto err_close_memfd;
Paul Cercueil09a43482015-03-04 15:50:27 +0100671
672 pdata->wait_for_err_code = true;
Paul Cercueil388dcd62015-11-27 15:15:48 +0100673 iio_mutex_unlock(pdata->lock);
Paul Cercueil09a43482015-03-04 15:50:27 +0100674 }
675
676 if (pdata->memfd >= 0)
677 close(pdata->memfd);
678
Paul Cercueilca9d3382015-05-04 14:30:09 +0200679 pdata->memfd = memfd;
Paul Cercueil09a43482015-03-04 15:50:27 +0100680
Paul Cercueilef32d582015-03-05 11:18:35 +0100681 ret = (ssize_t) ftruncate(pdata->memfd, pdata->mmap_len);
682 if (ret < 0) {
683 ret = -errno;
684 ERROR("Unable to truncate temp file: %zi\n", -ret);
685 return ret;
686 }
Paul Cercueil09a43482015-03-04 15:50:27 +0100687
Paul Cercueil04065c22015-03-05 14:08:16 +0100688 if (!pdata->is_tx) {
689 char buf[1024];
690 size_t len = pdata->mmap_len;
691
692 snprintf(buf, sizeof(buf), "READBUF %s %lu\r\n",
693 dev->id, (unsigned long) len);
694
Paul Cercueil388dcd62015-11-27 15:15:48 +0100695 iio_mutex_lock(pdata->lock);
Lars-Peter Clausen78067fc2016-02-11 12:48:01 +0100696 ret = write_rwbuf_command(dev, buf);
Paul Cercueil04065c22015-03-05 14:08:16 +0100697 if (ret < 0)
698 goto err_unlock;
699
700 do {
701 ret = network_read_mask(pdata->fd, mask, words);
702 if (!ret)
703 break;
704 if (ret < 0)
705 goto err_unlock;
706
707 mask = NULL; /* We read the mask only once */
708
709 ret = network_do_splice(pdata->memfd, pdata->fd, ret);
710 if (ret < 0)
711 goto err_unlock;
712
713 read += ret;
714 len -= ret;
715 } while (len);
716
Paul Cercueil388dcd62015-11-27 15:15:48 +0100717 iio_mutex_unlock(pdata->lock);
Paul Cercueil04065c22015-03-05 14:08:16 +0100718 }
Paul Cercueil09a43482015-03-04 15:50:27 +0100719
Paul Cercueile6e5a092015-03-04 16:33:26 +0100720 pdata->mmap_addr = mmap(NULL, pdata->mmap_len,
Paul Cercueil09a43482015-03-04 15:50:27 +0100721 PROT_READ | PROT_WRITE, MAP_SHARED, pdata->memfd, 0);
Paul Cercueile6e5a092015-03-04 16:33:26 +0100722 if (pdata->mmap_addr == MAP_FAILED) {
723 pdata->mmap_addr = NULL;
Paul Cercueil09a43482015-03-04 15:50:27 +0100724 ret = -errno;
Paul Cercueil7d7e5d32015-03-05 10:51:41 +0100725 ERROR("Unable to mmap: %zi\n", -ret);
Paul Cercueil09a43482015-03-04 15:50:27 +0100726 return ret;
727 }
728
Paul Cercueile6e5a092015-03-04 16:33:26 +0100729 *addr_ptr = pdata->mmap_addr;
Paul Cercueil04065c22015-03-05 14:08:16 +0100730 return read ? read : bytes_used;
Paul Cercueil09a43482015-03-04 15:50:27 +0100731
Paul Cercueilca9d3382015-05-04 14:30:09 +0200732err_close_memfd:
733 close(memfd);
Paul Cercueil09a43482015-03-04 15:50:27 +0100734err_unlock:
Paul Cercueil388dcd62015-11-27 15:15:48 +0100735 iio_mutex_unlock(pdata->lock);
Paul Cercueil09a43482015-03-04 15:50:27 +0100736 return ret;
737}
738#endif
739
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100740static ssize_t network_read_dev_attr(const struct iio_device *dev,
Paul Cercueil50c762a2014-04-14 15:55:43 +0200741 const char *attr, char *dst, size_t len, bool is_debug)
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100742{
Paul Cercueil2e28d502015-11-30 18:46:49 +0100743 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueil5b577762014-06-03 15:31:42 +0200744
Paul Cercueil2e28d502015-11-30 18:46:49 +0100745 return iiod_client_read_attr(pdata->iiod_client, pdata->fd,
746 dev, NULL, attr, dst, len, is_debug);
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100747}
748
Paul Cercueil07897d32014-03-06 12:46:08 +0100749static ssize_t network_write_dev_attr(const struct iio_device *dev,
Paul Cercueilcecda352014-05-06 18:14:29 +0200750 const char *attr, const char *src, size_t len, bool is_debug)
Paul Cercueil07897d32014-03-06 12:46:08 +0100751{
Paul Cercueil2e28d502015-11-30 18:46:49 +0100752 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueil5b577762014-06-03 15:31:42 +0200753
Paul Cercueil2e28d502015-11-30 18:46:49 +0100754 return iiod_client_write_attr(pdata->iiod_client, pdata->fd,
755 dev, NULL, attr, src, len, is_debug);
Paul Cercueil07897d32014-03-06 12:46:08 +0100756}
757
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100758static ssize_t network_read_chn_attr(const struct iio_channel *chn,
759 const char *attr, char *dst, size_t len)
760{
Paul Cercueil2e28d502015-11-30 18:46:49 +0100761 struct iio_context_pdata *pdata = chn->dev->ctx->pdata;
Paul Cercueil5b577762014-06-03 15:31:42 +0200762
Paul Cercueil2e28d502015-11-30 18:46:49 +0100763 return iiod_client_read_attr(pdata->iiod_client, pdata->fd,
764 chn->dev, chn, attr, dst, len, false);
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100765}
766
Paul Cercueil07897d32014-03-06 12:46:08 +0100767static ssize_t network_write_chn_attr(const struct iio_channel *chn,
Paul Cercueilcecda352014-05-06 18:14:29 +0200768 const char *attr, const char *src, size_t len)
Paul Cercueil07897d32014-03-06 12:46:08 +0100769{
Paul Cercueil2e28d502015-11-30 18:46:49 +0100770 struct iio_context_pdata *pdata = chn->dev->ctx->pdata;
Paul Cercueil5b577762014-06-03 15:31:42 +0200771
Paul Cercueil2e28d502015-11-30 18:46:49 +0100772 return iiod_client_write_attr(pdata->iiod_client, pdata->fd,
773 chn->dev, chn, attr, src, len, false);
Paul Cercueil07897d32014-03-06 12:46:08 +0100774}
775
Paul Cercueildcab40c2014-03-11 10:59:14 +0100776static int network_get_trigger(const struct iio_device *dev,
777 const struct iio_device **trigger)
778{
779 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueildcab40c2014-03-11 10:59:14 +0100780
Paul Cercueilcae6c2a2015-11-30 16:37:19 +0100781 return iiod_client_get_trigger(pdata->iiod_client,
782 pdata->fd, dev, trigger);
Paul Cercueildcab40c2014-03-11 10:59:14 +0100783}
784
785static int network_set_trigger(const struct iio_device *dev,
786 const struct iio_device *trigger)
787{
Paul Cercueila7b2aae2015-12-04 16:02:00 +0100788 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueil1494b862014-05-05 12:49:07 +0200789
Paul Cercueilcae6c2a2015-11-30 16:37:19 +0100790 return iiod_client_set_trigger(pdata->iiod_client,
791 pdata->fd, dev, trigger);
Paul Cercueildcab40c2014-03-11 10:59:14 +0100792}
793
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +0100794static void network_shutdown(struct iio_context *ctx)
795{
796 struct iio_context_pdata *pdata = ctx->pdata;
Paul Cercueil44ae11c2014-03-17 09:56:29 +0100797 unsigned int i;
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +0100798
Paul Cercueil388dcd62015-11-27 15:15:48 +0100799 iio_mutex_lock(pdata->lock);
Paul Cercueilbb76bf92014-03-11 10:20:18 +0100800 write_command("\r\nEXIT\r\n", pdata->fd);
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +0100801 close(pdata->fd);
Paul Cercueil388dcd62015-11-27 15:15:48 +0100802 iio_mutex_unlock(pdata->lock);
Paul Cercueil1494b862014-05-05 12:49:07 +0200803
Paul Cercueil439e1a22015-02-24 13:50:51 +0100804 for (i = 0; i < ctx->nb_devices; i++) {
805 struct iio_device *dev = ctx->devices[i];
Paul Cercueilc5b00752015-02-24 14:29:53 +0100806 struct iio_device_pdata *dpdata = dev->pdata;
Paul Cercueil439e1a22015-02-24 13:50:51 +0100807
Paul Cercueilc5b00752015-02-24 14:29:53 +0100808 if (dpdata) {
Paul Cercueilaa0db142015-03-04 16:25:24 +0100809 network_close(dev);
Paul Cercueil388dcd62015-11-27 15:15:48 +0100810 iio_mutex_destroy(dpdata->lock);
Paul Cercueilc5b00752015-02-24 14:29:53 +0100811 free(dpdata);
Paul Cercueil439e1a22015-02-24 13:50:51 +0100812 }
813 }
814
Paul Cercueil157d43c2015-11-30 15:05:06 +0100815 iiod_client_destroy(pdata->iiod_client);
Paul Cercueil388dcd62015-11-27 15:15:48 +0100816 iio_mutex_destroy(pdata->lock);
Paul Cercueil2dd2c132015-02-24 10:41:01 +0100817 freeaddrinfo(pdata->addrinfo);
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +0100818 free(pdata);
819}
820
Paul Cercueil6691a3f2014-05-02 12:32:01 +0200821static int network_get_version(const struct iio_context *ctx,
Paul Cercueil9de9e9d2014-05-20 13:18:19 +0200822 unsigned int *major, unsigned int *minor, char git_tag[8])
Paul Cercueil6691a3f2014-05-02 12:32:01 +0200823{
Paul Cercueil2e209f92015-11-30 15:05:38 +0100824 return iiod_client_get_version(ctx->pdata->iiod_client, ctx->pdata->fd,
825 major, minor, git_tag);
Paul Cercueil6691a3f2014-05-02 12:32:01 +0200826}
827
Paul Cercueil8a266f12014-06-10 16:06:31 +0200828static unsigned int calculate_remote_timeout(unsigned int timeout)
829{
830 /* XXX(pcercuei): We currently hardcode timeout / 2 for the backend used
831 * by the remote. Is there something better to do here? */
832 return timeout / 2;
833}
834
Paul Cercueilbca3dbc2014-06-11 12:00:21 +0200835static int network_set_timeout(struct iio_context *ctx, unsigned int timeout)
836{
Paul Cercueilf996ae12015-12-03 12:29:13 +0100837 struct iio_context_pdata *pdata = ctx->pdata;
838 int ret, fd = pdata->fd;
839
840 ret = set_socket_timeout(fd, timeout);
Paul Cercueilbca3dbc2014-06-11 12:00:21 +0200841 if (!ret) {
842 timeout = calculate_remote_timeout(timeout);
Paul Cercueilf996ae12015-12-03 12:29:13 +0100843 ret = iiod_client_set_timeout(pdata->iiod_client, fd, timeout);
Paul Cercueilbca3dbc2014-06-11 12:00:21 +0200844 }
845 if (ret < 0) {
846 char buf[1024];
Paul Cercueil53fc4852015-05-22 10:57:53 +0200847 iio_strerror(-ret, buf, sizeof(buf));
Paul Cercueil8a266f12014-06-10 16:06:31 +0200848 WARNING("Unable to set R/W timeout: %s\n", buf);
849 } else {
Paul Cercueil8a266f12014-06-10 16:06:31 +0200850 ctx->rw_timeout_ms = timeout;
851 }
852 return ret;
853}
854
Paul Cercueil61157f92015-11-19 17:48:22 +0100855static int network_set_kernel_buffers_count(const struct iio_device *dev,
856 unsigned int nb_blocks)
857{
Paul Cercueila9810a82015-11-30 16:55:07 +0100858 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueil61157f92015-11-19 17:48:22 +0100859
Paul Cercueila9810a82015-11-30 16:55:07 +0100860 return iiod_client_set_kernel_buffers_count(pdata->iiod_client,
861 pdata->fd, dev, nb_blocks);
Paul Cercueil61157f92015-11-19 17:48:22 +0100862}
863
Paul Cercueil12d41832014-10-28 14:35:53 +0100864static struct iio_context * network_clone(const struct iio_context *ctx)
865{
Paul Cercueil7ef45ce2015-03-16 14:36:16 +0100866 if (ctx->description) {
867 char *ptr = strchr(ctx->description, ' ');
868 if (ptr) {
869#ifdef HAVE_IPV6
870 char buf[INET6_ADDRSTRLEN + IF_NAMESIZE + 2];
871#else
872 char buf[INET_ADDRSTRLEN + 1];
873#endif
874 strncpy(buf, ctx->description, sizeof(buf) - 1);
875 buf[ptr - ctx->description] = '\0';
876 return iio_create_network_context(buf);
877 }
878 }
879
Paul Cercueild3d56ee2015-01-08 16:36:31 +0100880 return iio_create_network_context(ctx->description);
Paul Cercueil12d41832014-10-28 14:35:53 +0100881}
882
Lars-Peter Clausen09a59d72016-02-03 15:27:04 +0100883static const struct iio_backend_ops network_ops = {
Paul Cercueil12d41832014-10-28 14:35:53 +0100884 .clone = network_clone,
Paul Cercueilba059762014-03-14 11:02:02 +0100885 .open = network_open,
886 .close = network_close,
Paul Cercueil9945bc82014-03-05 14:07:29 +0100887 .read = network_read,
Paul Cercueil2725f702014-05-02 11:02:16 +0200888 .write = network_write,
Paul Cercueil90189672015-03-16 11:41:53 +0100889#ifdef WITH_NETWORK_GET_BUFFER
Paul Cercueil09a43482015-03-04 15:50:27 +0100890 .get_buffer = network_get_buffer,
891#endif
Paul Cercueil3e94a5b2014-03-04 13:00:41 +0100892 .read_device_attr = network_read_dev_attr,
893 .write_device_attr = network_write_dev_attr,
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100894 .read_channel_attr = network_read_chn_attr,
Paul Cercueil07897d32014-03-06 12:46:08 +0100895 .write_channel_attr = network_write_chn_attr,
Paul Cercueildcab40c2014-03-11 10:59:14 +0100896 .get_trigger = network_get_trigger,
897 .set_trigger = network_set_trigger,
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +0100898 .shutdown = network_shutdown,
Paul Cercueil6691a3f2014-05-02 12:32:01 +0200899 .get_version = network_get_version,
Paul Cercueil8a266f12014-06-10 16:06:31 +0200900 .set_timeout = network_set_timeout,
Paul Cercueil61157f92015-11-19 17:48:22 +0100901 .set_kernel_buffers_count = network_set_kernel_buffers_count,
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100902};
903
Paul Cercueil157d43c2015-11-30 15:05:06 +0100904static ssize_t network_write_data(struct iio_context_pdata *pdata,
905 int desc, const char *src, size_t len)
906{
907 ssize_t ret;
908
909 ret = send(desc, src, (int) len, 0);
910 if (ret < 0) {
Lars-Peter Clausendd04e6a2016-02-10 14:56:36 +0100911 return (ssize_t) network_get_error();
Paul Cercueil157d43c2015-11-30 15:05:06 +0100912 } else if (ret == 0) {
913 return -EPIPE;
914 } else {
915 return ret;
916 }
917}
918
919static ssize_t network_read_data(struct iio_context_pdata *pdata,
920 int desc, char *dst, size_t len)
921{
922 ssize_t ret;
923
924 ret = recv(desc, dst, (int) len, 0);
925 if (ret < 0) {
Lars-Peter Clausendd04e6a2016-02-10 14:56:36 +0100926 return (ssize_t) network_get_error();
Paul Cercueil157d43c2015-11-30 15:05:06 +0100927 } else {
928 return ret;
929 }
930}
931
932static ssize_t network_read_line(struct iio_context_pdata *pdata,
933 int desc, char *dst, size_t len)
934{
935 size_t i;
Paul Cercueilce02dc92016-01-07 11:46:38 +0100936#ifdef __linux__
937 ssize_t ret;
938
939 /* First read from the socket without advancing the read offset */
940 ret = recv(desc, dst, len, MSG_PEEK);
941 if (ret < 0)
942 return ret;
943
944 /* Lookup for the trailing \n */
945 for (i = 0; i < (size_t) ret && dst[i] != '\n'; i++);
946
947 /* No \n found? Just garbage data */
948 if (i == (size_t) ret)
949 return -EIO;
950
951 /* Advance the read offset to the byte following the \n */
952 return recv(desc, dst, i + 1, MSG_TRUNC);
953#else
Paul Cercueil157d43c2015-11-30 15:05:06 +0100954 bool found = false;
955
956 for (i = 0; i < len - 1; i++) {
957 ssize_t ret = network_read_data(pdata, desc, dst + i, 1);
958
959 if (ret < 0)
960 return ret;
961
962 if (dst[i] != '\n')
963 found = true;
964 else if (found)
965 break;
966 }
967
968 dst[i] = '\0';
969 return (ssize_t) i;
Paul Cercueilce02dc92016-01-07 11:46:38 +0100970#endif
Paul Cercueil157d43c2015-11-30 15:05:06 +0100971}
972
Lars-Peter Clausen09a59d72016-02-03 15:27:04 +0100973static const struct iiod_client_ops network_iiod_client_ops = {
Paul Cercueil157d43c2015-11-30 15:05:06 +0100974 .write = network_write_data,
975 .read = network_read_data,
976 .read_line = network_read_line,
977};
978
Paul Cercueil63e52182014-12-11 12:52:48 +0100979struct iio_context * network_create_context(const char *host)
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100980{
Paul Cercueil2e38bbe2014-03-19 15:27:15 +0100981 struct addrinfo hints, *res;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100982 struct iio_context *ctx;
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +0100983 struct iio_context_pdata *pdata;
Paul Cercueil0e3c04d2015-05-13 17:37:11 +0200984 size_t i, len;
Paul Cercueil4970ac32015-02-24 10:59:00 +0100985 int fd, ret;
Paul Cercueilb03c9ef2015-03-16 14:07:58 +0100986 char *description;
Paul Cercueil1fef1a52014-04-07 16:31:15 +0200987#ifdef _WIN32
988 WSADATA wsaData;
989
990 ret = WSAStartup(MAKEWORD(2, 0), &wsaData);
991 if (ret < 0) {
992 ERROR("WSAStartup failed with error %i\n", ret);
Paul Cercueilcc575532015-03-16 17:15:24 +0100993 errno = -ret;
Paul Cercueil1fef1a52014-04-07 16:31:15 +0200994 return NULL;
995 }
996#endif
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100997
Paul Cercueil2e38bbe2014-03-19 15:27:15 +0100998 memset(&hints, 0, sizeof(hints));
999 hints.ai_family = AF_UNSPEC;
1000 hints.ai_socktype = SOCK_STREAM;
Paul Cercueilab114932014-05-19 13:03:17 +02001001
1002#ifdef HAVE_AVAHI
1003 if (!host) {
1004 char addr_str[AVAHI_ADDRESS_STR_MAX];
1005 char port_str[6];
1006 AvahiAddress address;
Paul Cercueil0f729d32014-06-05 17:38:54 +02001007 uint16_t port = IIOD_PORT;
Paul Cercueilab114932014-05-19 13:03:17 +02001008
1009 memset(&address, 0, sizeof(address));
1010
1011 ret = discover_host(&address, &port);
1012 if (ret < 0) {
Paul Cercueilcc575532015-03-16 17:15:24 +01001013 DEBUG("Unable to find host: %s\n", strerror(-ret));
1014 errno = -ret;
Paul Cercueilab114932014-05-19 13:03:17 +02001015 return NULL;
1016 }
1017
1018 avahi_address_snprint(addr_str, sizeof(addr_str), &address);
1019 snprintf(port_str, sizeof(port_str), "%hu", port);
1020 ret = getaddrinfo(addr_str, port_str, &hints, &res);
1021 } else
1022#endif
1023 {
1024 ret = getaddrinfo(host, IIOD_PORT_STR, &hints, &res);
1025 }
1026
Paul Cercueil2e38bbe2014-03-19 15:27:15 +01001027 if (ret) {
Paul Cercueilab114932014-05-19 13:03:17 +02001028 ERROR("Unable to find host: %s\n", gai_strerror(ret));
Paul Cercueild9eb4cb2015-03-23 14:30:20 +01001029#ifndef _WIN32
Paul Cercueilcc575532015-03-16 17:15:24 +01001030 if (ret != EAI_SYSTEM)
1031 errno = ret;
Paul Cercueild9eb4cb2015-03-23 14:30:20 +01001032#endif
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001033 return NULL;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001034 }
1035
Paul Cercueil4970ac32015-02-24 10:59:00 +01001036 fd = create_socket(res);
Paul Cercueilcc575532015-03-16 17:15:24 +01001037 if (fd < 0) {
1038 errno = fd;
Paul Cercueil2dd2c132015-02-24 10:41:01 +01001039 goto err_free_addrinfo;
Paul Cercueilcc575532015-03-16 17:15:24 +01001040 }
Paul Cercueilbca3dbc2014-06-11 12:00:21 +02001041
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001042 pdata = calloc(1, sizeof(*pdata));
1043 if (!pdata) {
Paul Cercueilcc575532015-03-16 17:15:24 +01001044 errno = ENOMEM;
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001045 goto err_close_socket;
1046 }
1047
1048 pdata->fd = fd;
Paul Cercueil2dd2c132015-02-24 10:41:01 +01001049 pdata->addrinfo = res;
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001050
Paul Cercueil157d43c2015-11-30 15:05:06 +01001051 pdata->lock = iio_mutex_create();
1052 if (!pdata->lock) {
1053 errno = ENOMEM;
1054 goto err_free_pdata;
1055 }
1056
1057 pdata->iiod_client = iiod_client_new(pdata, pdata->lock,
1058 &network_iiod_client_ops);
1059 if (!pdata->iiod_client)
1060 goto err_destroy_mutex;
1061
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001062 DEBUG("Creating context...\n");
Paul Cercueil94726b12015-12-01 11:15:54 +01001063 ctx = iiod_client_create_context(pdata->iiod_client, fd);
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001064 if (!ctx)
Paul Cercueil157d43c2015-11-30 15:05:06 +01001065 goto err_destroy_iiod_client;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001066
Paul Cercueil2057fd32014-10-28 14:44:19 +01001067 /* Override the name and low-level functions of the XML context
1068 * with those corresponding to the network context */
1069 ctx->name = "network";
1070 ctx->ops = &network_ops;
1071 ctx->pdata = pdata;
1072
Paul Cercueil06c479d2015-01-08 16:14:57 +01001073#ifdef HAVE_IPV6
Paul Cercueiled15e492015-01-12 15:52:28 +01001074 len = INET6_ADDRSTRLEN + IF_NAMESIZE + 2;
Paul Cercueil06c479d2015-01-08 16:14:57 +01001075#else
1076 len = INET_ADDRSTRLEN + 1;
1077#endif
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001078
1079 description = malloc(len);
1080 if (!description) {
Paul Cercueilcc575532015-03-16 17:15:24 +01001081 ret = -ENOMEM;
Paul Cercueil06c479d2015-01-08 16:14:57 +01001082 goto err_network_shutdown;
1083 }
1084
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001085 description[0] = '\0';
Paul Cercueil06c479d2015-01-08 16:14:57 +01001086
1087#ifdef HAVE_IPV6
1088 if (res->ai_family == AF_INET6) {
1089 struct sockaddr_in6 *in = (struct sockaddr_in6 *) res->ai_addr;
Paul Cercueiled15e492015-01-12 15:52:28 +01001090 char *ptr;
Paul Cercueil06c479d2015-01-08 16:14:57 +01001091 inet_ntop(AF_INET6, &in->sin6_addr,
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001092 description, INET6_ADDRSTRLEN);
Paul Cercueiled15e492015-01-12 15:52:28 +01001093
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001094 ptr = if_indextoname(in->sin6_scope_id, description +
1095 strlen(description) + 1);
Paul Cercueiled15e492015-01-12 15:52:28 +01001096 if (!ptr) {
Paul Cercueilcc575532015-03-16 17:15:24 +01001097 ret = -errno;
Paul Cercueiled15e492015-01-12 15:52:28 +01001098 ERROR("Unable to lookup interface of IPv6 address\n");
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001099 goto err_free_description;
Paul Cercueiled15e492015-01-12 15:52:28 +01001100 }
1101
1102 *(ptr - 1) = '%';
Paul Cercueil06c479d2015-01-08 16:14:57 +01001103 }
1104#endif
1105 if (res->ai_family == AF_INET) {
1106 struct sockaddr_in *in = (struct sockaddr_in *) res->ai_addr;
Paul Cercueile7a31692015-07-15 10:52:47 +02001107#if (!_WIN32 || _WIN32_WINNT >= 0x600)
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001108 inet_ntop(AF_INET, &in->sin_addr, description, INET_ADDRSTRLEN);
Paul Cercueile7a31692015-07-15 10:52:47 +02001109#else
1110 char *tmp = inet_ntoa(in->sin_addr);
1111 strncpy(description, tmp, len);
1112#endif
Paul Cercueil06c479d2015-01-08 16:14:57 +01001113 }
1114
Paul Cercueil44ae11c2014-03-17 09:56:29 +01001115 for (i = 0; i < ctx->nb_devices; i++) {
1116 struct iio_device *dev = ctx->devices[i];
Paul Cercueilff778232014-03-24 14:23:08 +01001117
Paul Cercueil439e1a22015-02-24 13:50:51 +01001118 dev->pdata = calloc(1, sizeof(*dev->pdata));
1119 if (!dev->pdata) {
Paul Cercueilcc575532015-03-16 17:15:24 +01001120 ret = -ENOMEM;
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001121 goto err_free_description;
Paul Cercueil439e1a22015-02-24 13:50:51 +01001122 }
1123
1124 dev->pdata->fd = -1;
Paul Cercueil90189672015-03-16 11:41:53 +01001125#ifdef WITH_NETWORK_GET_BUFFER
Paul Cercueil09a43482015-03-04 15:50:27 +01001126 dev->pdata->memfd = -1;
Paul Cercueile6e5a092015-03-04 16:33:26 +01001127#endif
Paul Cercueilc5b00752015-02-24 14:29:53 +01001128
Paul Cercueil388dcd62015-11-27 15:15:48 +01001129 dev->pdata->lock = iio_mutex_create();
1130 if (!dev->pdata->lock) {
1131 ret = -ENOMEM;
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001132 goto err_free_description;
Paul Cercueil388dcd62015-11-27 15:15:48 +01001133 }
Paul Cercueil44ae11c2014-03-17 09:56:29 +01001134 }
1135
Paul Cercueilfd387472015-08-05 10:34:19 +02001136 ret = iio_context_init(ctx);
1137 if (ret < 0)
1138 goto err_free_description;
Paul Cercueil4c6729d2014-04-04 17:24:41 +02001139
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001140 if (ctx->description) {
Paul Cercueil0e3c04d2015-05-13 17:37:11 +02001141 size_t desc_len = strlen(description);
1142 size_t new_size = desc_len + strlen(ctx->description) + 2;
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001143 char *ptr, *new_description = realloc(description, new_size);
Paul Cercueilcc575532015-03-16 17:15:24 +01001144 if (!new_description) {
1145 ret = -ENOMEM;
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001146 goto err_free_description;
Paul Cercueilcc575532015-03-16 17:15:24 +01001147 }
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001148
1149 ptr = strrchr(new_description, '\0');
Paul Cercueil0e3c04d2015-05-13 17:37:11 +02001150 snprintf(ptr, new_size - desc_len, " %s", ctx->description);
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001151 free(ctx->description);
1152
1153 ctx->description = new_description;
1154 } else {
1155 ctx->description = description;
1156 }
1157
Paul Cercueilf996ae12015-12-03 12:29:13 +01001158 iiod_client_set_timeout(pdata->iiod_client, fd,
1159 calculate_remote_timeout(DEFAULT_TIMEOUT_MS));
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001160 return ctx;
1161
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001162err_free_description:
1163 free(description);
Paul Cercueil44ae11c2014-03-17 09:56:29 +01001164err_network_shutdown:
Paul Cercueil44ae11c2014-03-17 09:56:29 +01001165 iio_context_destroy(ctx);
Paul Cercueilcc575532015-03-16 17:15:24 +01001166 errno = -ret;
Paul Cercueil2057fd32014-10-28 14:44:19 +01001167 return NULL;
Paul Cercueil157d43c2015-11-30 15:05:06 +01001168
1169err_destroy_iiod_client:
1170 iiod_client_destroy(pdata->iiod_client);
1171err_destroy_mutex:
1172 iio_mutex_destroy(pdata->lock);
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001173err_free_pdata:
1174 free(pdata);
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001175err_close_socket:
1176 close(fd);
Paul Cercueil2dd2c132015-02-24 10:41:01 +01001177err_free_addrinfo:
1178 freeaddrinfo(res);
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001179 return NULL;
1180}