blob: bb6991e18b05d5fce751b10e87ed201305c26818 [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
Lars-Peter Clausen6a6c8662016-02-12 15:35:52 +0100202static ssize_t network_recv(int fd, void *data, size_t len, int flags)
203{
204 ssize_t ret;
205 int err;
206
207 while (1) {
208 ret = recv(fd, data, (int) len, flags);
209 if (ret == 0)
210 return -EPIPE;
211 else if (ret > 0)
212 break;
213
214 err = network_get_error();
215 if (err != -EINTR)
216 return (ssize_t) err;
217 }
218 return ret;
219}
220
221static ssize_t network_send(int fd, const void *data, size_t len, int flags)
222{
223 ssize_t ret;
224 int err;
225
226 while (1) {
227 ret = send(fd, data, (int) len, flags);
228 if (ret == 0)
229 return -EPIPE;
230 else if (ret > 0)
231 break;
232
233 err = network_get_error();
234 if (err != -EINTR)
235 return (ssize_t) err;
236 }
237
238 return ret;
239}
240
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100241static ssize_t write_all(const void *src, size_t len, int fd)
242{
Paul Cercueil6e7f79e2014-04-04 12:27:24 +0200243 uintptr_t ptr = (uintptr_t) src;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100244 while (len) {
Lars-Peter Clausen6a6c8662016-02-12 15:35:52 +0100245 ssize_t ret = network_send(fd, (const void *) ptr, len, 0);
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100246 ptr += ret;
247 len -= ret;
248 }
Paul Cercueil4012cff2015-05-11 10:47:40 +0200249 return (ssize_t)(ptr - (uintptr_t) src);
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100250}
251
Paul Cercueilbb76bf92014-03-11 10:20:18 +0100252static ssize_t write_command(const char *cmd, int fd)
253{
254 ssize_t ret;
255
256 DEBUG("Writing command: %s\n", cmd);
257 ret = write_all(cmd, strlen(cmd), fd);
258 if (ret < 0) {
259 char buf[1024];
Paul Cercueil53fc4852015-05-22 10:57:53 +0200260 iio_strerror(-ret, buf, sizeof(buf));
Paul Cercueilbb76bf92014-03-11 10:20:18 +0100261 ERROR("Unable to send command: %s\n", buf);
262 }
263 return ret;
264}
265
Paul Cercueil4970ac32015-02-24 10:59:00 +0100266#ifndef _WIN32
Lars-Peter Clausena2cbc812016-02-11 14:49:27 +0100267
268/* Use it if available */
269#ifndef SOCK_CLOSEXEC
270#define SOCK_CLOSEXEC 0
271#endif
272
Paul Cercueil4970ac32015-02-24 10:59:00 +0100273/* The purpose of this function is to provide a version of connect()
274 * that does not ignore timeouts... */
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100275static int do_connect(const struct addrinfo *addrinfo,
276 struct timeval *timeout)
Paul Cercueil4970ac32015-02-24 10:59:00 +0100277{
278 int ret, error;
279 socklen_t len;
280 fd_set set;
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100281 int fd;
282
Lars-Peter Clausena2cbc812016-02-11 14:49:27 +0100283 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype | SOCK_CLOEXEC, 0);
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100284 if (fd < 0)
285 return -errno;
Paul Cercueil4970ac32015-02-24 10:59:00 +0100286
287 FD_ZERO(&set);
288 FD_SET(fd, &set);
289
290 ret = set_blocking_mode(fd, false);
Lars-Peter Clausenebbd9072016-02-11 15:15:08 +0100291 if (ret < 0) {
292 close(fd);
Paul Cercueil4970ac32015-02-24 10:59:00 +0100293 return ret;
Lars-Peter Clausenebbd9072016-02-11 15:15:08 +0100294 }
Paul Cercueil4970ac32015-02-24 10:59:00 +0100295
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100296 ret = connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen);
Paul Cercueil4970ac32015-02-24 10:59:00 +0100297 if (ret < 0 && errno != EINPROGRESS) {
298 ret = -errno;
299 goto end;
300 }
301
302 ret = select(fd + 1, &set, &set, NULL, timeout);
303 if (ret < 0) {
304 ret = -errno;
305 goto end;
306 }
307 if (ret == 0) {
308 ret = -ETIMEDOUT;
309 goto end;
310 }
311
312 /* Verify that we don't have an error */
313 len = sizeof(error);
314 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len);
315 if(ret < 0) {
316 ret = -errno;
317 goto end;
318 }
319 if (error) {
320 ret = -error;
321 goto end;
322 }
323
324end:
325 /* Restore blocking mode */
326 set_blocking_mode(fd, true);
Lars-Peter Clausenebbd9072016-02-11 15:15:08 +0100327 if (ret < 0) {
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100328 close(fd);
Lars-Peter Clausenebbd9072016-02-11 15:15:08 +0100329 return ret;
330 }
331
332 return fd;
Paul Cercueil4970ac32015-02-24 10:59:00 +0100333}
334
335static int set_socket_timeout(int fd, unsigned int timeout)
336{
337 struct timeval tv;
338
339 tv.tv_sec = timeout / 1000;
340 tv.tv_usec = (timeout % 1000) * 1000;
341 if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0 ||
342 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,
343 &tv, sizeof(tv)) < 0)
344 return -errno;
345 else
346 return 0;
347}
348#else
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100349
Lars-Peter Clausena2cbc812016-02-11 14:49:27 +0100350/* Use it if available */
351#ifndef WSA_FLAG_NO_HANDLE_INHERIT
352#define WSA_FLAG_NO_HANDLE_INHERIT 0
353#endif
354
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100355static int do_connect(const struct addrinfo *addrinfo,
356 struct timeval *timeout)
357{
358 int ret;
359 SOCKET s;
360
Lars-Peter Clausena2cbc812016-02-11 14:49:27 +0100361 s = WSASocket(addrinfo->ai_family, addrinfo->ai_socktype, 0, NULL, 0,
362 WSA_FLAG_NO_HANDLE_INHERIT);
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100363 if (s == INVALID_SOCKET)
364 return -WSAGetLastError();
365
366 ret = connect(s, addrinfo->ai_addr, (int) addrinfo->ai_addrlen);
367 if (ret == SOCKET_ERROR) {
368 close(s);
369 return -WSAGetLastError();
370 }
371
372 return (int) s;
373}
374
Paul Cercueil4970ac32015-02-24 10:59:00 +0100375static int set_socket_timeout(int fd, unsigned int timeout)
376{
377 if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO,
378 (const char *) &timeout, sizeof(timeout)) < 0 ||
379 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,
380 (const char *) &timeout, sizeof(timeout)) < 0)
Paul Cercueil135b3612015-06-30 14:10:14 +0200381 return -WSAGetLastError();
Paul Cercueil4970ac32015-02-24 10:59:00 +0100382 else
383 return 0;
384}
385#endif /* !_WIN32 */
386
387static int create_socket(const struct addrinfo *addrinfo)
388{
389 struct timeval timeout;
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100390 int fd, yes = 1;
Paul Cercueil4970ac32015-02-24 10:59:00 +0100391
392 timeout.tv_sec = DEFAULT_TIMEOUT_MS / 1000;
393 timeout.tv_usec = (DEFAULT_TIMEOUT_MS % 1000) * 1000;
394
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100395 fd = do_connect(addrinfo, &timeout);
396 if (fd < 0)
397 return fd;
Paul Cercueil4970ac32015-02-24 10:59:00 +0100398
399 set_socket_timeout(fd, DEFAULT_TIMEOUT_MS);
400 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
401 (const char *) &yes, sizeof(yes));
402 return fd;
403}
404
Paul Cercueil92f15c22015-04-20 11:36:51 +0200405static int network_open(const struct iio_device *dev,
406 size_t samples_count, bool cyclic)
Paul Cercueilba059762014-03-14 11:02:02 +0100407{
Paul Cercueil439e1a22015-02-24 13:50:51 +0100408 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueil80dc1082015-12-01 18:20:31 +0100409 struct iio_device_pdata *ppdata = dev->pdata;
410 int fd, ret = -EBUSY;
Paul Cercueilba059762014-03-14 11:02:02 +0100411
Paul Cercueil80dc1082015-12-01 18:20:31 +0100412 iio_mutex_lock(ppdata->lock);
413 if (ppdata->fd >= 0)
414 goto out_mutex_unlock;
Paul Cercueil439e1a22015-02-24 13:50:51 +0100415
Paul Cercueil80dc1082015-12-01 18:20:31 +0100416 ret = create_socket(pdata->addrinfo);
417 if (ret < 0)
418 goto out_mutex_unlock;
Paul Cercueilba059762014-03-14 11:02:02 +0100419
Paul Cercueil80dc1082015-12-01 18:20:31 +0100420 fd = ret;
Paul Cercueilba059762014-03-14 11:02:02 +0100421
Paul Cercueil80dc1082015-12-01 18:20:31 +0100422 ret = iiod_client_open_unlocked(pdata->iiod_client, fd,
423 dev, samples_count, cyclic);
Paul Cercueil44ae11c2014-03-17 09:56:29 +0100424 if (ret < 0) {
Paul Cercueil439e1a22015-02-24 13:50:51 +0100425 close(fd);
Paul Cercueil80dc1082015-12-01 18:20:31 +0100426 goto out_mutex_unlock;
Paul Cercueil44ae11c2014-03-17 09:56:29 +0100427 }
Paul Cercueil439e1a22015-02-24 13:50:51 +0100428
Paul Cercueil80dc1082015-12-01 18:20:31 +0100429 ppdata->is_tx = iio_device_is_tx(dev);
430 ppdata->is_cyclic = cyclic;
431 ppdata->fd = fd;
432 ppdata->wait_for_err_code = false;
Paul Cercueild957b982015-08-05 14:39:01 +0200433#ifdef WITH_NETWORK_GET_BUFFER
Paul Cercueil80dc1082015-12-01 18:20:31 +0100434 ppdata->mmap_len = samples_count * iio_device_get_sample_size(dev);
Paul Cercueild957b982015-08-05 14:39:01 +0200435#endif
Paul Cercueil80dc1082015-12-01 18:20:31 +0100436
437out_mutex_unlock:
438 iio_mutex_unlock(ppdata->lock);
439 return ret;
Paul Cercueilba059762014-03-14 11:02:02 +0100440}
441
Lars-Peter Clausen90e75a92016-02-22 11:34:34 +0100442static int network_close(const struct iio_device *dev)
443{
444 struct iio_device_pdata *pdata = dev->pdata;
445 int ret = -EBADF;
446
447 iio_mutex_lock(pdata->lock);
448
449 if (pdata->fd >= 0) {
450 ret = iiod_client_close_unlocked(dev->ctx->pdata->iiod_client,
451 pdata->fd, dev);
452
453 write_command("\r\nEXIT\r\n", pdata->fd);
454
455 close(pdata->fd);
456 pdata->fd = -1;
457 }
458
459#ifdef WITH_NETWORK_GET_BUFFER
460 if (pdata->memfd >= 0)
461 close(pdata->memfd);
462 pdata->memfd = -1;
463
464 if (pdata->mmap_addr) {
465 munmap(pdata->mmap_addr, pdata->mmap_len);
466 pdata->mmap_addr = NULL;
467 }
468#endif
469
470 iio_mutex_unlock(pdata->lock);
471 return ret;
472}
473
474static ssize_t network_read(const struct iio_device *dev, void *dst, size_t len,
475 uint32_t *mask, size_t words)
476{
477 struct iio_device_pdata *pdata = dev->pdata;
478 ssize_t ret;
479
480 iio_mutex_lock(pdata->lock);
481 ret = iiod_client_read_unlocked(dev->ctx->pdata->iiod_client,
482 pdata->fd, dev, dst, len, mask, words);
483 iio_mutex_unlock(pdata->lock);
484
485 return ret;
486}
487
488static ssize_t network_write(const struct iio_device *dev,
489 const void *src, size_t len)
490{
491 struct iio_device_pdata *pdata = dev->pdata;
492 ssize_t ret;
493
494 iio_mutex_lock(pdata->lock);
495 ret = iiod_client_write_unlocked(dev->ctx->pdata->iiod_client,
496 pdata->fd, dev, src, len);
497 iio_mutex_unlock(pdata->lock);
498
499 return ret;
500}
501
502#ifdef WITH_NETWORK_GET_BUFFER
503
504static ssize_t read_all(void *dst, size_t len, int fd)
505{
506 uintptr_t ptr = (uintptr_t) dst;
507 while (len) {
508 ssize_t ret = network_recv(fd, (void *) ptr, len, 0);
509 if (ret < 0)
510 return ret;
511 ptr += ret;
512 len -= ret;
513 }
514 return (ssize_t)(ptr - (uintptr_t) dst);
515}
516
517static int read_integer(int fd, long *val)
518{
519 unsigned int i;
520 char buf[1024], *ptr;
521 ssize_t ret;
522 bool found = false;
523
524 for (i = 0; i < sizeof(buf) - 1; i++) {
525 ret = read_all(buf + i, 1, fd);
526 if (ret < 0)
527 return (int) ret;
528
529 /* Skip the eventual first few carriage returns.
530 * Also stop when a dot is found (for parsing floats) */
531 if (buf[i] != '\n' && buf[i] != '.')
532 found = true;
533 else if (found)
534 break;
535 }
536
537 buf[i] = '\0';
538 ret = (ssize_t) strtol(buf, &ptr, 10);
539 if (ptr == buf)
540 return -EINVAL;
541 *val = (long) ret;
542 return 0;
543}
544
545static ssize_t network_read_mask(int fd, uint32_t *mask, size_t words)
546{
547 long read_len;
548 ssize_t ret;
549
550 ret = read_integer(fd, &read_len);
551 if (ret < 0)
552 return ret;
553
554 if (read_len > 0 && mask) {
555 size_t i;
556 char buf[9];
557
558 buf[8] = '\0';
559 DEBUG("Reading mask\n");
560
561 for (i = words; i > 0; i--) {
562 ret = read_all(buf, 8, fd);
563 if (ret < 0)
564 return ret;
565
566 sscanf(buf, "%08x", &mask[i - 1]);
567 DEBUG("mask[%i] = 0x%x\n", i - 1, mask[i - 1]);
568 }
569 }
570
571 if (read_len > 0) {
572 char c;
573 ssize_t nb = read_all(&c, 1, fd);
574 if (nb > 0 && c != '\n')
575 read_len = -EIO;
576 }
577
578 return (ssize_t) read_len;
579}
580
Paul Cercueil8f7f6c42015-02-24 15:00:35 +0100581static ssize_t read_error_code(int fd)
582{
583 /*
584 * The server returns two integer codes.
585 * The first one is returned right after the WRITEBUF command is issued,
586 * and corresponds to the error code returned when the server attempted
587 * to open the device.
588 * If zero, a second error code is returned, that corresponds (if positive)
589 * to the number of bytes written.
590 *
591 * To speed up things, we delay error reporting. We just send out the
592 * data without reading the error code that the server gives us, because
593 * the answer will take too much time. If an error occured, it will be
594 * reported by the next call to iio_buffer_push().
595 */
596
597 unsigned int i;
598 long resp = 0;
599
600 for (i = 0; i < 2; i++) {
601 ssize_t ret = read_integer(fd, &resp);
602 if (ret < 0)
603 return ret;
604 if (resp < 0)
605 return (ssize_t) resp;
606 }
607
Paul Cercueil7d7e5d32015-03-05 10:51:41 +0100608 return (ssize_t) resp;
Paul Cercueil8f7f6c42015-02-24 15:00:35 +0100609}
610
Paul Cercueil3c0da372015-03-05 11:36:39 +0100611static ssize_t write_rwbuf_command(const struct iio_device *dev,
Lars-Peter Clausen2e380822016-02-11 20:36:13 +0100612 const char *cmd)
Paul Cercueil3c0da372015-03-05 11:36:39 +0100613{
614 struct iio_device_pdata *pdata = dev->pdata;
615 int fd = pdata->fd;
616
617 if (pdata->wait_for_err_code) {
618 ssize_t ret = read_error_code(fd);
619
620 pdata->wait_for_err_code = false;
621 if (ret < 0)
622 return ret;
623 }
624
Lars-Peter Clausen78067fc2016-02-11 12:48:01 +0100625 return write_command(cmd, fd);
Paul Cercueil3c0da372015-03-05 11:36:39 +0100626}
627
Paul Cercueil7d7e5d32015-03-05 10:51:41 +0100628static ssize_t network_do_splice(int fd_out, int fd_in, size_t len)
Paul Cercueil09a43482015-03-04 15:50:27 +0100629{
630 int pipefd[2];
Paul Cercueil04065c22015-03-05 14:08:16 +0100631 ssize_t ret, read_len = len;
Paul Cercueil09a43482015-03-04 15:50:27 +0100632
Lars-Peter Clausena2cbc812016-02-11 14:49:27 +0100633 ret = (ssize_t) pipe2(pipefd, O_CLOEXEC);
Paul Cercueil09a43482015-03-04 15:50:27 +0100634 if (ret < 0)
635 return -errno;
636
637 do {
Paul Cercueila107c6d2015-03-05 13:54:10 +0100638 /*
639 * SPLICE_F_NONBLOCK is just here to avoid a deadlock when
640 * splicing from a socket. As the socket is not in
641 * non-blocking mode, it should never return -EAGAIN.
642 * TODO(pcercuei): Find why it locks...
643 * */
644 ret = splice(fd_in, NULL, pipefd[1], NULL, len,
645 SPLICE_F_MOVE | SPLICE_F_NONBLOCK);
646 if (!ret)
647 ret = -EIO;
Paul Cercueil09a43482015-03-04 15:50:27 +0100648 if (ret < 0)
649 goto err_close_pipe;
650
Paul Cercueila107c6d2015-03-05 13:54:10 +0100651 ret = splice(pipefd[0], NULL, fd_out, NULL, ret,
652 SPLICE_F_MOVE | SPLICE_F_NONBLOCK);
653 if (!ret)
654 ret = -EIO;
Paul Cercueil09a43482015-03-04 15:50:27 +0100655 if (ret < 0)
656 goto err_close_pipe;
657
658 len -= ret;
659 } while (len);
660
661err_close_pipe:
662 close(pipefd[0]);
663 close(pipefd[1]);
Paul Cercueil04065c22015-03-05 14:08:16 +0100664 return ret < 0 ? ret : read_len;
Paul Cercueil09a43482015-03-04 15:50:27 +0100665}
666
667static ssize_t network_get_buffer(const struct iio_device *dev,
Paul Cercueil76ca8842015-03-05 11:16:16 +0100668 void **addr_ptr, size_t bytes_used,
669 uint32_t *mask, size_t words)
Paul Cercueil09a43482015-03-04 15:50:27 +0100670{
671 struct iio_device_pdata *pdata = dev->pdata;
Paul Cercueil04065c22015-03-05 14:08:16 +0100672 ssize_t ret, read = 0;
Paul Cercueilca9d3382015-05-04 14:30:09 +0200673 int memfd;
Paul Cercueil09a43482015-03-04 15:50:27 +0100674
Paul Cercueil04065c22015-03-05 14:08:16 +0100675 if (pdata->is_cyclic)
Paul Cercueil09a43482015-03-04 15:50:27 +0100676 return -ENOSYS;
Paul Cercueilca9d3382015-05-04 14:30:09 +0200677
678 /* We check early that the temporary file can be created, so that we can
679 * return -ENOSYS in case it fails, which will indicate that the
680 * high-speed interface is not available.
681 *
682 * O_TMPFILE -> Linux 3.11.
683 * TODO: use memfd_create (Linux 3.17) */
Lars-Peter Clausena2cbc812016-02-11 14:49:27 +0100684 memfd = open(P_tmpdir, O_RDWR | O_TMPFILE | O_EXCL | O_CLOEXEC, S_IRWXU);
Paul Cercueilca9d3382015-05-04 14:30:09 +0200685 if (memfd < 0)
686 return -ENOSYS;
687
688 if (!addr_ptr || words != (dev->nb_channels + 31) / 32) {
689 close(memfd);
Paul Cercueil09a43482015-03-04 15:50:27 +0100690 return -EINVAL;
Paul Cercueilca9d3382015-05-04 14:30:09 +0200691 }
Paul Cercueil09a43482015-03-04 15:50:27 +0100692
Paul Cercueile6e5a092015-03-04 16:33:26 +0100693 if (pdata->mmap_addr)
694 munmap(pdata->mmap_addr, pdata->mmap_len);
Paul Cercueil09a43482015-03-04 15:50:27 +0100695
Paul Cercueile6e5a092015-03-04 16:33:26 +0100696 if (pdata->mmap_addr && pdata->is_tx) {
Paul Cercueil09a43482015-03-04 15:50:27 +0100697 char buf[1024];
698 snprintf(buf, sizeof(buf), "WRITEBUF %s %lu\r\n",
Paul Cercueild957b982015-08-05 14:39:01 +0200699 dev->id, (unsigned long) bytes_used);
Paul Cercueil09a43482015-03-04 15:50:27 +0100700
Paul Cercueil388dcd62015-11-27 15:15:48 +0100701 iio_mutex_lock(pdata->lock);
Paul Cercueil09a43482015-03-04 15:50:27 +0100702
Lars-Peter Clausen78067fc2016-02-11 12:48:01 +0100703 ret = write_rwbuf_command(dev, buf);
Paul Cercueil09a43482015-03-04 15:50:27 +0100704 if (ret < 0)
Paul Cercueilca9d3382015-05-04 14:30:09 +0200705 goto err_close_memfd;
Paul Cercueil09a43482015-03-04 15:50:27 +0100706
Paul Cercueild957b982015-08-05 14:39:01 +0200707 ret = network_do_splice(pdata->fd, pdata->memfd, bytes_used);
Paul Cercueil09a43482015-03-04 15:50:27 +0100708 if (ret < 0)
Paul Cercueilca9d3382015-05-04 14:30:09 +0200709 goto err_close_memfd;
Paul Cercueil09a43482015-03-04 15:50:27 +0100710
711 pdata->wait_for_err_code = true;
Paul Cercueil388dcd62015-11-27 15:15:48 +0100712 iio_mutex_unlock(pdata->lock);
Paul Cercueil09a43482015-03-04 15:50:27 +0100713 }
714
715 if (pdata->memfd >= 0)
716 close(pdata->memfd);
717
Paul Cercueilca9d3382015-05-04 14:30:09 +0200718 pdata->memfd = memfd;
Paul Cercueil09a43482015-03-04 15:50:27 +0100719
Paul Cercueilef32d582015-03-05 11:18:35 +0100720 ret = (ssize_t) ftruncate(pdata->memfd, pdata->mmap_len);
721 if (ret < 0) {
722 ret = -errno;
723 ERROR("Unable to truncate temp file: %zi\n", -ret);
724 return ret;
725 }
Paul Cercueil09a43482015-03-04 15:50:27 +0100726
Paul Cercueil04065c22015-03-05 14:08:16 +0100727 if (!pdata->is_tx) {
728 char buf[1024];
729 size_t len = pdata->mmap_len;
730
731 snprintf(buf, sizeof(buf), "READBUF %s %lu\r\n",
732 dev->id, (unsigned long) len);
733
Paul Cercueil388dcd62015-11-27 15:15:48 +0100734 iio_mutex_lock(pdata->lock);
Lars-Peter Clausen78067fc2016-02-11 12:48:01 +0100735 ret = write_rwbuf_command(dev, buf);
Paul Cercueil04065c22015-03-05 14:08:16 +0100736 if (ret < 0)
737 goto err_unlock;
738
739 do {
740 ret = network_read_mask(pdata->fd, mask, words);
741 if (!ret)
742 break;
743 if (ret < 0)
744 goto err_unlock;
745
746 mask = NULL; /* We read the mask only once */
747
748 ret = network_do_splice(pdata->memfd, pdata->fd, ret);
749 if (ret < 0)
750 goto err_unlock;
751
752 read += ret;
753 len -= ret;
754 } while (len);
755
Paul Cercueil388dcd62015-11-27 15:15:48 +0100756 iio_mutex_unlock(pdata->lock);
Paul Cercueil04065c22015-03-05 14:08:16 +0100757 }
Paul Cercueil09a43482015-03-04 15:50:27 +0100758
Paul Cercueile6e5a092015-03-04 16:33:26 +0100759 pdata->mmap_addr = mmap(NULL, pdata->mmap_len,
Paul Cercueil09a43482015-03-04 15:50:27 +0100760 PROT_READ | PROT_WRITE, MAP_SHARED, pdata->memfd, 0);
Paul Cercueile6e5a092015-03-04 16:33:26 +0100761 if (pdata->mmap_addr == MAP_FAILED) {
762 pdata->mmap_addr = NULL;
Paul Cercueil09a43482015-03-04 15:50:27 +0100763 ret = -errno;
Paul Cercueil7d7e5d32015-03-05 10:51:41 +0100764 ERROR("Unable to mmap: %zi\n", -ret);
Paul Cercueil09a43482015-03-04 15:50:27 +0100765 return ret;
766 }
767
Paul Cercueile6e5a092015-03-04 16:33:26 +0100768 *addr_ptr = pdata->mmap_addr;
Paul Cercueil7c3f5d22016-02-18 11:49:04 +0100769 return read ? read : (ssize_t) bytes_used;
Paul Cercueil09a43482015-03-04 15:50:27 +0100770
Paul Cercueilca9d3382015-05-04 14:30:09 +0200771err_close_memfd:
772 close(memfd);
Paul Cercueil09a43482015-03-04 15:50:27 +0100773err_unlock:
Paul Cercueil388dcd62015-11-27 15:15:48 +0100774 iio_mutex_unlock(pdata->lock);
Paul Cercueil09a43482015-03-04 15:50:27 +0100775 return ret;
776}
777#endif
778
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100779static ssize_t network_read_dev_attr(const struct iio_device *dev,
Paul Cercueil50c762a2014-04-14 15:55:43 +0200780 const char *attr, char *dst, size_t len, bool is_debug)
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100781{
Paul Cercueil2e28d502015-11-30 18:46:49 +0100782 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueil5b577762014-06-03 15:31:42 +0200783
Paul Cercueil2e28d502015-11-30 18:46:49 +0100784 return iiod_client_read_attr(pdata->iiod_client, pdata->fd,
785 dev, NULL, attr, dst, len, is_debug);
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100786}
787
Paul Cercueil07897d32014-03-06 12:46:08 +0100788static ssize_t network_write_dev_attr(const struct iio_device *dev,
Paul Cercueilcecda352014-05-06 18:14:29 +0200789 const char *attr, const char *src, size_t len, bool is_debug)
Paul Cercueil07897d32014-03-06 12:46:08 +0100790{
Paul Cercueil2e28d502015-11-30 18:46:49 +0100791 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueil5b577762014-06-03 15:31:42 +0200792
Paul Cercueil2e28d502015-11-30 18:46:49 +0100793 return iiod_client_write_attr(pdata->iiod_client, pdata->fd,
794 dev, NULL, attr, src, len, is_debug);
Paul Cercueil07897d32014-03-06 12:46:08 +0100795}
796
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100797static ssize_t network_read_chn_attr(const struct iio_channel *chn,
798 const char *attr, char *dst, size_t len)
799{
Paul Cercueil2e28d502015-11-30 18:46:49 +0100800 struct iio_context_pdata *pdata = chn->dev->ctx->pdata;
Paul Cercueil5b577762014-06-03 15:31:42 +0200801
Paul Cercueil2e28d502015-11-30 18:46:49 +0100802 return iiod_client_read_attr(pdata->iiod_client, pdata->fd,
803 chn->dev, chn, attr, dst, len, false);
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100804}
805
Paul Cercueil07897d32014-03-06 12:46:08 +0100806static ssize_t network_write_chn_attr(const struct iio_channel *chn,
Paul Cercueilcecda352014-05-06 18:14:29 +0200807 const char *attr, const char *src, size_t len)
Paul Cercueil07897d32014-03-06 12:46:08 +0100808{
Paul Cercueil2e28d502015-11-30 18:46:49 +0100809 struct iio_context_pdata *pdata = chn->dev->ctx->pdata;
Paul Cercueil5b577762014-06-03 15:31:42 +0200810
Paul Cercueil2e28d502015-11-30 18:46:49 +0100811 return iiod_client_write_attr(pdata->iiod_client, pdata->fd,
812 chn->dev, chn, attr, src, len, false);
Paul Cercueil07897d32014-03-06 12:46:08 +0100813}
814
Paul Cercueildcab40c2014-03-11 10:59:14 +0100815static int network_get_trigger(const struct iio_device *dev,
816 const struct iio_device **trigger)
817{
818 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueildcab40c2014-03-11 10:59:14 +0100819
Paul Cercueilcae6c2a2015-11-30 16:37:19 +0100820 return iiod_client_get_trigger(pdata->iiod_client,
821 pdata->fd, dev, trigger);
Paul Cercueildcab40c2014-03-11 10:59:14 +0100822}
823
824static int network_set_trigger(const struct iio_device *dev,
825 const struct iio_device *trigger)
826{
Paul Cercueila7b2aae2015-12-04 16:02:00 +0100827 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueil1494b862014-05-05 12:49:07 +0200828
Paul Cercueilcae6c2a2015-11-30 16:37:19 +0100829 return iiod_client_set_trigger(pdata->iiod_client,
830 pdata->fd, dev, trigger);
Paul Cercueildcab40c2014-03-11 10:59:14 +0100831}
832
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +0100833static void network_shutdown(struct iio_context *ctx)
834{
835 struct iio_context_pdata *pdata = ctx->pdata;
Paul Cercueil44ae11c2014-03-17 09:56:29 +0100836 unsigned int i;
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +0100837
Paul Cercueil388dcd62015-11-27 15:15:48 +0100838 iio_mutex_lock(pdata->lock);
Paul Cercueilbb76bf92014-03-11 10:20:18 +0100839 write_command("\r\nEXIT\r\n", pdata->fd);
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +0100840 close(pdata->fd);
Paul Cercueil388dcd62015-11-27 15:15:48 +0100841 iio_mutex_unlock(pdata->lock);
Paul Cercueil1494b862014-05-05 12:49:07 +0200842
Paul Cercueil439e1a22015-02-24 13:50:51 +0100843 for (i = 0; i < ctx->nb_devices; i++) {
844 struct iio_device *dev = ctx->devices[i];
Paul Cercueilc5b00752015-02-24 14:29:53 +0100845 struct iio_device_pdata *dpdata = dev->pdata;
Paul Cercueil439e1a22015-02-24 13:50:51 +0100846
Paul Cercueilc5b00752015-02-24 14:29:53 +0100847 if (dpdata) {
Paul Cercueilaa0db142015-03-04 16:25:24 +0100848 network_close(dev);
Paul Cercueil388dcd62015-11-27 15:15:48 +0100849 iio_mutex_destroy(dpdata->lock);
Paul Cercueilc5b00752015-02-24 14:29:53 +0100850 free(dpdata);
Paul Cercueil439e1a22015-02-24 13:50:51 +0100851 }
852 }
853
Paul Cercueil157d43c2015-11-30 15:05:06 +0100854 iiod_client_destroy(pdata->iiod_client);
Paul Cercueil388dcd62015-11-27 15:15:48 +0100855 iio_mutex_destroy(pdata->lock);
Paul Cercueil2dd2c132015-02-24 10:41:01 +0100856 freeaddrinfo(pdata->addrinfo);
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +0100857 free(pdata);
858}
859
Paul Cercueil6691a3f2014-05-02 12:32:01 +0200860static int network_get_version(const struct iio_context *ctx,
Paul Cercueil9de9e9d2014-05-20 13:18:19 +0200861 unsigned int *major, unsigned int *minor, char git_tag[8])
Paul Cercueil6691a3f2014-05-02 12:32:01 +0200862{
Paul Cercueil2e209f92015-11-30 15:05:38 +0100863 return iiod_client_get_version(ctx->pdata->iiod_client, ctx->pdata->fd,
864 major, minor, git_tag);
Paul Cercueil6691a3f2014-05-02 12:32:01 +0200865}
866
Paul Cercueil8a266f12014-06-10 16:06:31 +0200867static unsigned int calculate_remote_timeout(unsigned int timeout)
868{
869 /* XXX(pcercuei): We currently hardcode timeout / 2 for the backend used
870 * by the remote. Is there something better to do here? */
871 return timeout / 2;
872}
873
Paul Cercueilbca3dbc2014-06-11 12:00:21 +0200874static int network_set_timeout(struct iio_context *ctx, unsigned int timeout)
875{
Paul Cercueilf996ae12015-12-03 12:29:13 +0100876 struct iio_context_pdata *pdata = ctx->pdata;
877 int ret, fd = pdata->fd;
878
879 ret = set_socket_timeout(fd, timeout);
Paul Cercueilbca3dbc2014-06-11 12:00:21 +0200880 if (!ret) {
881 timeout = calculate_remote_timeout(timeout);
Paul Cercueilf996ae12015-12-03 12:29:13 +0100882 ret = iiod_client_set_timeout(pdata->iiod_client, fd, timeout);
Paul Cercueilbca3dbc2014-06-11 12:00:21 +0200883 }
884 if (ret < 0) {
885 char buf[1024];
Paul Cercueil53fc4852015-05-22 10:57:53 +0200886 iio_strerror(-ret, buf, sizeof(buf));
Paul Cercueil8a266f12014-06-10 16:06:31 +0200887 WARNING("Unable to set R/W timeout: %s\n", buf);
888 } else {
Paul Cercueil8a266f12014-06-10 16:06:31 +0200889 ctx->rw_timeout_ms = timeout;
890 }
891 return ret;
892}
893
Paul Cercueil61157f92015-11-19 17:48:22 +0100894static int network_set_kernel_buffers_count(const struct iio_device *dev,
895 unsigned int nb_blocks)
896{
Paul Cercueila9810a82015-11-30 16:55:07 +0100897 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueil61157f92015-11-19 17:48:22 +0100898
Paul Cercueila9810a82015-11-30 16:55:07 +0100899 return iiod_client_set_kernel_buffers_count(pdata->iiod_client,
900 pdata->fd, dev, nb_blocks);
Paul Cercueil61157f92015-11-19 17:48:22 +0100901}
902
Paul Cercueil12d41832014-10-28 14:35:53 +0100903static struct iio_context * network_clone(const struct iio_context *ctx)
904{
Paul Cercueil7ef45ce2015-03-16 14:36:16 +0100905 if (ctx->description) {
906 char *ptr = strchr(ctx->description, ' ');
907 if (ptr) {
908#ifdef HAVE_IPV6
909 char buf[INET6_ADDRSTRLEN + IF_NAMESIZE + 2];
910#else
911 char buf[INET_ADDRSTRLEN + 1];
912#endif
913 strncpy(buf, ctx->description, sizeof(buf) - 1);
914 buf[ptr - ctx->description] = '\0';
915 return iio_create_network_context(buf);
916 }
917 }
918
Paul Cercueild3d56ee2015-01-08 16:36:31 +0100919 return iio_create_network_context(ctx->description);
Paul Cercueil12d41832014-10-28 14:35:53 +0100920}
921
Lars-Peter Clausen09a59d72016-02-03 15:27:04 +0100922static const struct iio_backend_ops network_ops = {
Paul Cercueil12d41832014-10-28 14:35:53 +0100923 .clone = network_clone,
Paul Cercueilba059762014-03-14 11:02:02 +0100924 .open = network_open,
925 .close = network_close,
Paul Cercueil9945bc82014-03-05 14:07:29 +0100926 .read = network_read,
Paul Cercueil2725f702014-05-02 11:02:16 +0200927 .write = network_write,
Paul Cercueil90189672015-03-16 11:41:53 +0100928#ifdef WITH_NETWORK_GET_BUFFER
Paul Cercueil09a43482015-03-04 15:50:27 +0100929 .get_buffer = network_get_buffer,
930#endif
Paul Cercueil3e94a5b2014-03-04 13:00:41 +0100931 .read_device_attr = network_read_dev_attr,
932 .write_device_attr = network_write_dev_attr,
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100933 .read_channel_attr = network_read_chn_attr,
Paul Cercueil07897d32014-03-06 12:46:08 +0100934 .write_channel_attr = network_write_chn_attr,
Paul Cercueildcab40c2014-03-11 10:59:14 +0100935 .get_trigger = network_get_trigger,
936 .set_trigger = network_set_trigger,
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +0100937 .shutdown = network_shutdown,
Paul Cercueil6691a3f2014-05-02 12:32:01 +0200938 .get_version = network_get_version,
Paul Cercueil8a266f12014-06-10 16:06:31 +0200939 .set_timeout = network_set_timeout,
Paul Cercueil61157f92015-11-19 17:48:22 +0100940 .set_kernel_buffers_count = network_set_kernel_buffers_count,
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100941};
942
Paul Cercueil157d43c2015-11-30 15:05:06 +0100943static ssize_t network_write_data(struct iio_context_pdata *pdata,
944 int desc, const char *src, size_t len)
945{
Lars-Peter Clausen6a6c8662016-02-12 15:35:52 +0100946 return network_send(desc, src, len, 0);
Paul Cercueil157d43c2015-11-30 15:05:06 +0100947}
948
949static ssize_t network_read_data(struct iio_context_pdata *pdata,
950 int desc, char *dst, size_t len)
951{
Lars-Peter Clausen6a6c8662016-02-12 15:35:52 +0100952 return network_recv(desc, dst, len, 0);
Paul Cercueil157d43c2015-11-30 15:05:06 +0100953}
954
955static ssize_t network_read_line(struct iio_context_pdata *pdata,
956 int desc, char *dst, size_t len)
957{
958 size_t i;
Paul Cercueilce02dc92016-01-07 11:46:38 +0100959#ifdef __linux__
960 ssize_t ret;
961
Lars-Peter Clausen6a6c8662016-02-12 15:35:52 +0100962 ret = network_recv(desc, dst, len, MSG_PEEK);
Paul Cercueilce02dc92016-01-07 11:46:38 +0100963 if (ret < 0)
Lars-Peter Clausen6a6c8662016-02-12 15:35:52 +0100964 return ret;
Paul Cercueilce02dc92016-01-07 11:46:38 +0100965
966 /* Lookup for the trailing \n */
967 for (i = 0; i < (size_t) ret && dst[i] != '\n'; i++);
968
969 /* No \n found? Just garbage data */
970 if (i == (size_t) ret)
971 return -EIO;
972
973 /* Advance the read offset to the byte following the \n */
Lars-Peter Clausen6a6c8662016-02-12 15:35:52 +0100974 return network_recv(desc, dst, i + 1, MSG_TRUNC);
Paul Cercueilce02dc92016-01-07 11:46:38 +0100975#else
Paul Cercueil157d43c2015-11-30 15:05:06 +0100976 bool found = false;
977
978 for (i = 0; i < len - 1; i++) {
979 ssize_t ret = network_read_data(pdata, desc, dst + i, 1);
980
981 if (ret < 0)
982 return ret;
983
984 if (dst[i] != '\n')
985 found = true;
986 else if (found)
987 break;
988 }
989
990 dst[i] = '\0';
991 return (ssize_t) i;
Paul Cercueilce02dc92016-01-07 11:46:38 +0100992#endif
Paul Cercueil157d43c2015-11-30 15:05:06 +0100993}
994
Lars-Peter Clausen09a59d72016-02-03 15:27:04 +0100995static const struct iiod_client_ops network_iiod_client_ops = {
Paul Cercueil157d43c2015-11-30 15:05:06 +0100996 .write = network_write_data,
997 .read = network_read_data,
998 .read_line = network_read_line,
999};
1000
Paul Cercueil63e52182014-12-11 12:52:48 +01001001struct iio_context * network_create_context(const char *host)
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001002{
Paul Cercueil2e38bbe2014-03-19 15:27:15 +01001003 struct addrinfo hints, *res;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001004 struct iio_context *ctx;
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001005 struct iio_context_pdata *pdata;
Paul Cercueil0e3c04d2015-05-13 17:37:11 +02001006 size_t i, len;
Paul Cercueil4970ac32015-02-24 10:59:00 +01001007 int fd, ret;
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001008 char *description;
Paul Cercueil1fef1a52014-04-07 16:31:15 +02001009#ifdef _WIN32
1010 WSADATA wsaData;
1011
1012 ret = WSAStartup(MAKEWORD(2, 0), &wsaData);
1013 if (ret < 0) {
1014 ERROR("WSAStartup failed with error %i\n", ret);
Paul Cercueilcc575532015-03-16 17:15:24 +01001015 errno = -ret;
Paul Cercueil1fef1a52014-04-07 16:31:15 +02001016 return NULL;
1017 }
1018#endif
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001019
Paul Cercueil2e38bbe2014-03-19 15:27:15 +01001020 memset(&hints, 0, sizeof(hints));
1021 hints.ai_family = AF_UNSPEC;
1022 hints.ai_socktype = SOCK_STREAM;
Paul Cercueilab114932014-05-19 13:03:17 +02001023
1024#ifdef HAVE_AVAHI
1025 if (!host) {
1026 char addr_str[AVAHI_ADDRESS_STR_MAX];
1027 char port_str[6];
1028 AvahiAddress address;
Paul Cercueil0f729d32014-06-05 17:38:54 +02001029 uint16_t port = IIOD_PORT;
Paul Cercueilab114932014-05-19 13:03:17 +02001030
1031 memset(&address, 0, sizeof(address));
1032
1033 ret = discover_host(&address, &port);
1034 if (ret < 0) {
Paul Cercueilcc575532015-03-16 17:15:24 +01001035 DEBUG("Unable to find host: %s\n", strerror(-ret));
1036 errno = -ret;
Paul Cercueilab114932014-05-19 13:03:17 +02001037 return NULL;
1038 }
1039
1040 avahi_address_snprint(addr_str, sizeof(addr_str), &address);
1041 snprintf(port_str, sizeof(port_str), "%hu", port);
1042 ret = getaddrinfo(addr_str, port_str, &hints, &res);
1043 } else
1044#endif
1045 {
1046 ret = getaddrinfo(host, IIOD_PORT_STR, &hints, &res);
1047 }
1048
Paul Cercueil2e38bbe2014-03-19 15:27:15 +01001049 if (ret) {
Paul Cercueilab114932014-05-19 13:03:17 +02001050 ERROR("Unable to find host: %s\n", gai_strerror(ret));
Paul Cercueild9eb4cb2015-03-23 14:30:20 +01001051#ifndef _WIN32
Paul Cercueilcc575532015-03-16 17:15:24 +01001052 if (ret != EAI_SYSTEM)
1053 errno = ret;
Paul Cercueild9eb4cb2015-03-23 14:30:20 +01001054#endif
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001055 return NULL;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001056 }
1057
Paul Cercueil4970ac32015-02-24 10:59:00 +01001058 fd = create_socket(res);
Paul Cercueilcc575532015-03-16 17:15:24 +01001059 if (fd < 0) {
1060 errno = fd;
Paul Cercueil2dd2c132015-02-24 10:41:01 +01001061 goto err_free_addrinfo;
Paul Cercueilcc575532015-03-16 17:15:24 +01001062 }
Paul Cercueilbca3dbc2014-06-11 12:00:21 +02001063
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001064 pdata = calloc(1, sizeof(*pdata));
1065 if (!pdata) {
Paul Cercueilcc575532015-03-16 17:15:24 +01001066 errno = ENOMEM;
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001067 goto err_close_socket;
1068 }
1069
1070 pdata->fd = fd;
Paul Cercueil2dd2c132015-02-24 10:41:01 +01001071 pdata->addrinfo = res;
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001072
Paul Cercueil157d43c2015-11-30 15:05:06 +01001073 pdata->lock = iio_mutex_create();
1074 if (!pdata->lock) {
1075 errno = ENOMEM;
1076 goto err_free_pdata;
1077 }
1078
1079 pdata->iiod_client = iiod_client_new(pdata, pdata->lock,
1080 &network_iiod_client_ops);
1081 if (!pdata->iiod_client)
1082 goto err_destroy_mutex;
1083
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001084 DEBUG("Creating context...\n");
Paul Cercueil94726b12015-12-01 11:15:54 +01001085 ctx = iiod_client_create_context(pdata->iiod_client, fd);
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001086 if (!ctx)
Paul Cercueil157d43c2015-11-30 15:05:06 +01001087 goto err_destroy_iiod_client;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001088
Paul Cercueil2057fd32014-10-28 14:44:19 +01001089 /* Override the name and low-level functions of the XML context
1090 * with those corresponding to the network context */
1091 ctx->name = "network";
1092 ctx->ops = &network_ops;
1093 ctx->pdata = pdata;
1094
Paul Cercueil06c479d2015-01-08 16:14:57 +01001095#ifdef HAVE_IPV6
Paul Cercueiled15e492015-01-12 15:52:28 +01001096 len = INET6_ADDRSTRLEN + IF_NAMESIZE + 2;
Paul Cercueil06c479d2015-01-08 16:14:57 +01001097#else
1098 len = INET_ADDRSTRLEN + 1;
1099#endif
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001100
1101 description = malloc(len);
1102 if (!description) {
Paul Cercueilcc575532015-03-16 17:15:24 +01001103 ret = -ENOMEM;
Paul Cercueil06c479d2015-01-08 16:14:57 +01001104 goto err_network_shutdown;
1105 }
1106
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001107 description[0] = '\0';
Paul Cercueil06c479d2015-01-08 16:14:57 +01001108
1109#ifdef HAVE_IPV6
1110 if (res->ai_family == AF_INET6) {
1111 struct sockaddr_in6 *in = (struct sockaddr_in6 *) res->ai_addr;
Paul Cercueiled15e492015-01-12 15:52:28 +01001112 char *ptr;
Paul Cercueil06c479d2015-01-08 16:14:57 +01001113 inet_ntop(AF_INET6, &in->sin6_addr,
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001114 description, INET6_ADDRSTRLEN);
Paul Cercueiled15e492015-01-12 15:52:28 +01001115
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001116 ptr = if_indextoname(in->sin6_scope_id, description +
1117 strlen(description) + 1);
Paul Cercueiled15e492015-01-12 15:52:28 +01001118 if (!ptr) {
Paul Cercueilcc575532015-03-16 17:15:24 +01001119 ret = -errno;
Paul Cercueiled15e492015-01-12 15:52:28 +01001120 ERROR("Unable to lookup interface of IPv6 address\n");
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001121 goto err_free_description;
Paul Cercueiled15e492015-01-12 15:52:28 +01001122 }
1123
1124 *(ptr - 1) = '%';
Paul Cercueil06c479d2015-01-08 16:14:57 +01001125 }
1126#endif
1127 if (res->ai_family == AF_INET) {
1128 struct sockaddr_in *in = (struct sockaddr_in *) res->ai_addr;
Paul Cercueile7a31692015-07-15 10:52:47 +02001129#if (!_WIN32 || _WIN32_WINNT >= 0x600)
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001130 inet_ntop(AF_INET, &in->sin_addr, description, INET_ADDRSTRLEN);
Paul Cercueile7a31692015-07-15 10:52:47 +02001131#else
1132 char *tmp = inet_ntoa(in->sin_addr);
1133 strncpy(description, tmp, len);
1134#endif
Paul Cercueil06c479d2015-01-08 16:14:57 +01001135 }
1136
Paul Cercueil44ae11c2014-03-17 09:56:29 +01001137 for (i = 0; i < ctx->nb_devices; i++) {
1138 struct iio_device *dev = ctx->devices[i];
Paul Cercueilff778232014-03-24 14:23:08 +01001139
Paul Cercueil439e1a22015-02-24 13:50:51 +01001140 dev->pdata = calloc(1, sizeof(*dev->pdata));
1141 if (!dev->pdata) {
Paul Cercueilcc575532015-03-16 17:15:24 +01001142 ret = -ENOMEM;
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001143 goto err_free_description;
Paul Cercueil439e1a22015-02-24 13:50:51 +01001144 }
1145
1146 dev->pdata->fd = -1;
Paul Cercueil90189672015-03-16 11:41:53 +01001147#ifdef WITH_NETWORK_GET_BUFFER
Paul Cercueil09a43482015-03-04 15:50:27 +01001148 dev->pdata->memfd = -1;
Paul Cercueile6e5a092015-03-04 16:33:26 +01001149#endif
Paul Cercueilc5b00752015-02-24 14:29:53 +01001150
Paul Cercueil388dcd62015-11-27 15:15:48 +01001151 dev->pdata->lock = iio_mutex_create();
1152 if (!dev->pdata->lock) {
1153 ret = -ENOMEM;
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001154 goto err_free_description;
Paul Cercueil388dcd62015-11-27 15:15:48 +01001155 }
Paul Cercueil44ae11c2014-03-17 09:56:29 +01001156 }
1157
Paul Cercueilfd387472015-08-05 10:34:19 +02001158 ret = iio_context_init(ctx);
1159 if (ret < 0)
1160 goto err_free_description;
Paul Cercueil4c6729d2014-04-04 17:24:41 +02001161
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001162 if (ctx->description) {
Paul Cercueil0e3c04d2015-05-13 17:37:11 +02001163 size_t desc_len = strlen(description);
1164 size_t new_size = desc_len + strlen(ctx->description) + 2;
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001165 char *ptr, *new_description = realloc(description, new_size);
Paul Cercueilcc575532015-03-16 17:15:24 +01001166 if (!new_description) {
1167 ret = -ENOMEM;
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001168 goto err_free_description;
Paul Cercueilcc575532015-03-16 17:15:24 +01001169 }
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001170
1171 ptr = strrchr(new_description, '\0');
Paul Cercueil0e3c04d2015-05-13 17:37:11 +02001172 snprintf(ptr, new_size - desc_len, " %s", ctx->description);
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001173 free(ctx->description);
1174
1175 ctx->description = new_description;
1176 } else {
1177 ctx->description = description;
1178 }
1179
Paul Cercueilf996ae12015-12-03 12:29:13 +01001180 iiod_client_set_timeout(pdata->iiod_client, fd,
1181 calculate_remote_timeout(DEFAULT_TIMEOUT_MS));
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001182 return ctx;
1183
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001184err_free_description:
1185 free(description);
Paul Cercueil44ae11c2014-03-17 09:56:29 +01001186err_network_shutdown:
Paul Cercueil44ae11c2014-03-17 09:56:29 +01001187 iio_context_destroy(ctx);
Paul Cercueilcc575532015-03-16 17:15:24 +01001188 errno = -ret;
Paul Cercueil2057fd32014-10-28 14:44:19 +01001189 return NULL;
Paul Cercueil157d43c2015-11-30 15:05:06 +01001190
1191err_destroy_iiod_client:
1192 iiod_client_destroy(pdata->iiod_client);
1193err_destroy_mutex:
1194 iio_mutex_destroy(pdata->lock);
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001195err_free_pdata:
1196 free(pdata);
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001197err_close_socket:
1198 close(fd);
Paul Cercueil2dd2c132015-02-24 10:41:01 +01001199err_free_addrinfo:
1200 freeaddrinfo(res);
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001201 return NULL;
1202}