blob: 7ba9979d9866d0b8620193d564835ce79549e877 [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);
299 if (ret < 0)
300 return ret;
301
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100302 ret = connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen);
Paul Cercueil4970ac32015-02-24 10:59:00 +0100303 if (ret < 0 && errno != EINPROGRESS) {
304 ret = -errno;
305 goto end;
306 }
307
308 ret = select(fd + 1, &set, &set, NULL, timeout);
309 if (ret < 0) {
310 ret = -errno;
311 goto end;
312 }
313 if (ret == 0) {
314 ret = -ETIMEDOUT;
315 goto end;
316 }
317
318 /* Verify that we don't have an error */
319 len = sizeof(error);
320 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len);
321 if(ret < 0) {
322 ret = -errno;
323 goto end;
324 }
325 if (error) {
326 ret = -error;
327 goto end;
328 }
329
330end:
331 /* Restore blocking mode */
332 set_blocking_mode(fd, true);
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100333 if (ret < 0)
334 close(fd);
Paul Cercueil4970ac32015-02-24 10:59:00 +0100335 return ret;
336}
337
338static int set_socket_timeout(int fd, unsigned int timeout)
339{
340 struct timeval tv;
341
342 tv.tv_sec = timeout / 1000;
343 tv.tv_usec = (timeout % 1000) * 1000;
344 if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0 ||
345 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,
346 &tv, sizeof(tv)) < 0)
347 return -errno;
348 else
349 return 0;
350}
351#else
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100352
353static int do_connect(const struct addrinfo *addrinfo,
354 struct timeval *timeout)
355{
356 int ret;
357 SOCKET s;
358
359 s = socket(addrinfo->ai_family, addrinfo->ai_socktype, 0);
360 if (s == INVALID_SOCKET)
361 return -WSAGetLastError();
362
363 ret = connect(s, addrinfo->ai_addr, (int) addrinfo->ai_addrlen);
364 if (ret == SOCKET_ERROR) {
365 close(s);
366 return -WSAGetLastError();
367 }
368
369 return (int) s;
370}
371
Paul Cercueil4970ac32015-02-24 10:59:00 +0100372static int set_socket_timeout(int fd, unsigned int timeout)
373{
374 if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO,
375 (const char *) &timeout, sizeof(timeout)) < 0 ||
376 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,
377 (const char *) &timeout, sizeof(timeout)) < 0)
Paul Cercueil135b3612015-06-30 14:10:14 +0200378 return -WSAGetLastError();
Paul Cercueil4970ac32015-02-24 10:59:00 +0100379 else
380 return 0;
381}
382#endif /* !_WIN32 */
383
384static int create_socket(const struct addrinfo *addrinfo)
385{
386 struct timeval timeout;
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100387 int fd, yes = 1;
Paul Cercueil4970ac32015-02-24 10:59:00 +0100388
389 timeout.tv_sec = DEFAULT_TIMEOUT_MS / 1000;
390 timeout.tv_usec = (DEFAULT_TIMEOUT_MS % 1000) * 1000;
391
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100392 fd = do_connect(addrinfo, &timeout);
393 if (fd < 0)
394 return fd;
Paul Cercueil4970ac32015-02-24 10:59:00 +0100395
396 set_socket_timeout(fd, DEFAULT_TIMEOUT_MS);
397 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
398 (const char *) &yes, sizeof(yes));
399 return fd;
400}
401
Paul Cercueil92f15c22015-04-20 11:36:51 +0200402static int network_open(const struct iio_device *dev,
403 size_t samples_count, bool cyclic)
Paul Cercueilba059762014-03-14 11:02:02 +0100404{
Paul Cercueil439e1a22015-02-24 13:50:51 +0100405 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueil80dc1082015-12-01 18:20:31 +0100406 struct iio_device_pdata *ppdata = dev->pdata;
407 int fd, ret = -EBUSY;
Paul Cercueilba059762014-03-14 11:02:02 +0100408
Paul Cercueil80dc1082015-12-01 18:20:31 +0100409 iio_mutex_lock(ppdata->lock);
410 if (ppdata->fd >= 0)
411 goto out_mutex_unlock;
Paul Cercueil439e1a22015-02-24 13:50:51 +0100412
Paul Cercueil80dc1082015-12-01 18:20:31 +0100413 ret = create_socket(pdata->addrinfo);
414 if (ret < 0)
415 goto out_mutex_unlock;
Paul Cercueilba059762014-03-14 11:02:02 +0100416
Paul Cercueil80dc1082015-12-01 18:20:31 +0100417 fd = ret;
Paul Cercueilba059762014-03-14 11:02:02 +0100418
Paul Cercueil80dc1082015-12-01 18:20:31 +0100419 ret = iiod_client_open_unlocked(pdata->iiod_client, fd,
420 dev, samples_count, cyclic);
Paul Cercueil44ae11c2014-03-17 09:56:29 +0100421 if (ret < 0) {
Paul Cercueil439e1a22015-02-24 13:50:51 +0100422 close(fd);
Paul Cercueil80dc1082015-12-01 18:20:31 +0100423 goto out_mutex_unlock;
Paul Cercueil44ae11c2014-03-17 09:56:29 +0100424 }
Paul Cercueil439e1a22015-02-24 13:50:51 +0100425
Paul Cercueil80dc1082015-12-01 18:20:31 +0100426 ppdata->is_tx = iio_device_is_tx(dev);
427 ppdata->is_cyclic = cyclic;
428 ppdata->fd = fd;
429 ppdata->wait_for_err_code = false;
Paul Cercueild957b982015-08-05 14:39:01 +0200430#ifdef WITH_NETWORK_GET_BUFFER
Paul Cercueil80dc1082015-12-01 18:20:31 +0100431 ppdata->mmap_len = samples_count * iio_device_get_sample_size(dev);
Paul Cercueild957b982015-08-05 14:39:01 +0200432#endif
Paul Cercueil80dc1082015-12-01 18:20:31 +0100433
434out_mutex_unlock:
435 iio_mutex_unlock(ppdata->lock);
436 return ret;
Paul Cercueilba059762014-03-14 11:02:02 +0100437}
438
Paul Cercueil8f7f6c42015-02-24 15:00:35 +0100439static ssize_t read_error_code(int fd)
440{
441 /*
442 * The server returns two integer codes.
443 * The first one is returned right after the WRITEBUF command is issued,
444 * and corresponds to the error code returned when the server attempted
445 * to open the device.
446 * If zero, a second error code is returned, that corresponds (if positive)
447 * to the number of bytes written.
448 *
449 * To speed up things, we delay error reporting. We just send out the
450 * data without reading the error code that the server gives us, because
451 * the answer will take too much time. If an error occured, it will be
452 * reported by the next call to iio_buffer_push().
453 */
454
455 unsigned int i;
456 long resp = 0;
457
458 for (i = 0; i < 2; i++) {
459 ssize_t ret = read_integer(fd, &resp);
460 if (ret < 0)
461 return ret;
462 if (resp < 0)
463 return (ssize_t) resp;
464 }
465
Paul Cercueil7d7e5d32015-03-05 10:51:41 +0100466 return (ssize_t) resp;
Paul Cercueil8f7f6c42015-02-24 15:00:35 +0100467}
468
Paul Cercueil3c0da372015-03-05 11:36:39 +0100469static ssize_t write_rwbuf_command(const struct iio_device *dev,
470 const char *cmd, bool do_exec)
471{
472 struct iio_device_pdata *pdata = dev->pdata;
473 int fd = pdata->fd;
474
475 if (pdata->wait_for_err_code) {
476 ssize_t ret = read_error_code(fd);
477
478 pdata->wait_for_err_code = false;
479 if (ret < 0)
480 return ret;
481 }
482
Lars-Peter Clausen78067fc2016-02-11 12:48:01 +0100483 return write_command(cmd, fd);
Paul Cercueil3c0da372015-03-05 11:36:39 +0100484}
485
Paul Cercueilba059762014-03-14 11:02:02 +0100486static int network_close(const struct iio_device *dev)
487{
Paul Cercueil439e1a22015-02-24 13:50:51 +0100488 struct iio_device_pdata *pdata = dev->pdata;
Paul Cercueild5d84612015-06-08 21:24:15 +0200489 int ret = -EBADF;
Paul Cercueil80dc1082015-12-01 18:20:31 +0100490
491 iio_mutex_lock(pdata->lock);
Paul Cercueil439e1a22015-02-24 13:50:51 +0100492
Paul Cercueila34596e2015-03-05 14:33:46 +0100493 if (pdata->fd >= 0) {
Paul Cercueil80dc1082015-12-01 18:20:31 +0100494 ret = iiod_client_close_unlocked(dev->ctx->pdata->iiod_client,
495 pdata->fd, dev);
Paul Cercueil1494b862014-05-05 12:49:07 +0200496
Paul Cercueila34596e2015-03-05 14:33:46 +0100497 write_command("\r\nEXIT\r\n", pdata->fd);
Paul Cercueilaa0db142015-03-04 16:25:24 +0100498
Paul Cercueila34596e2015-03-05 14:33:46 +0100499 close(pdata->fd);
500 pdata->fd = -1;
Paul Cercueila34596e2015-03-05 14:33:46 +0100501 }
Paul Cercueile6e5a092015-03-04 16:33:26 +0100502
Paul Cercueil90189672015-03-16 11:41:53 +0100503#ifdef WITH_NETWORK_GET_BUFFER
Paul Cercueile6e5a092015-03-04 16:33:26 +0100504 if (pdata->memfd >= 0)
505 close(pdata->memfd);
506 pdata->memfd = -1;
507
508 if (pdata->mmap_addr) {
509 munmap(pdata->mmap_addr, pdata->mmap_len);
510 pdata->mmap_addr = NULL;
511 }
512#endif
Paul Cercueil80dc1082015-12-01 18:20:31 +0100513
514 iio_mutex_unlock(pdata->lock);
Paul Cercueil1494b862014-05-05 12:49:07 +0200515 return ret;
Paul Cercueilba059762014-03-14 11:02:02 +0100516}
517
Paul Cercueil7d7e5d32015-03-05 10:51:41 +0100518static ssize_t network_read_mask(int fd, uint32_t *mask, size_t words)
519{
520 long read_len;
521 ssize_t ret;
522
523 ret = read_integer(fd, &read_len);
524 if (ret < 0)
525 return ret;
526
527 if (read_len > 0 && mask) {
Paul Cercueil4012cff2015-05-11 10:47:40 +0200528 size_t i;
Paul Cercueil7d7e5d32015-03-05 10:51:41 +0100529 char buf[9];
530
531 buf[8] = '\0';
532 DEBUG("Reading mask\n");
533
534 for (i = words; i > 0; i--) {
535 ret = read_all(buf, 8, fd);
536 if (ret < 0)
537 return ret;
538
539 sscanf(buf, "%08x", &mask[i - 1]);
540 DEBUG("mask[%i] = 0x%x\n", i - 1, mask[i - 1]);
541 }
542 }
543
544 if (read_len > 0) {
545 char c;
546 ssize_t nb = read_all(&c, 1, fd);
547 if (nb > 0 && c != '\n')
548 read_len = -EIO;
549 }
550
551 return (ssize_t) read_len;
552}
553
Paul Cercueil45c575d2014-03-20 15:14:01 +0100554static ssize_t network_read(const struct iio_device *dev, void *dst, size_t len,
555 uint32_t *mask, size_t words)
Paul Cercueilf9286452014-03-18 14:32:17 +0100556{
Paul Cercueilc5b00752015-02-24 14:29:53 +0100557 struct iio_device_pdata *pdata = dev->pdata;
Paul Cercueil72793642015-12-02 11:57:10 +0100558 ssize_t ret;
Paul Cercueil1494b862014-05-05 12:49:07 +0200559
Paul Cercueil388dcd62015-11-27 15:15:48 +0100560 iio_mutex_lock(pdata->lock);
Paul Cercueil72793642015-12-02 11:57:10 +0100561 ret = iiod_client_read_unlocked(dev->ctx->pdata->iiod_client,
562 pdata->fd, dev, dst, len, mask, words);
Paul Cercueil388dcd62015-11-27 15:15:48 +0100563 iio_mutex_unlock(pdata->lock);
Paul Cercueil72793642015-12-02 11:57:10 +0100564
565 return ret;
Paul Cercueil9945bc82014-03-05 14:07:29 +0100566}
567
Paul Cercueila62f84e2015-02-24 14:16:08 +0100568static ssize_t network_write(const struct iio_device *dev,
569 const void *src, size_t len)
Paul Cercueil2725f702014-05-02 11:02:16 +0200570{
Paul Cercueilc5b00752015-02-24 14:29:53 +0100571 struct iio_device_pdata *pdata = dev->pdata;
Paul Cercueil2725f702014-05-02 11:02:16 +0200572 ssize_t ret;
Paul Cercueil2725f702014-05-02 11:02:16 +0200573
Paul Cercueil388dcd62015-11-27 15:15:48 +0100574 iio_mutex_lock(pdata->lock);
Paul Cercueilf19568a2015-12-02 17:57:51 +0100575 ret = iiod_client_write_unlocked(dev->ctx->pdata->iiod_client,
576 pdata->fd, dev, src, len);
Paul Cercueil388dcd62015-11-27 15:15:48 +0100577 iio_mutex_unlock(pdata->lock);
Paul Cercueil1494b862014-05-05 12:49:07 +0200578
Paul Cercueil1494b862014-05-05 12:49:07 +0200579 return ret;
Paul Cercueil2725f702014-05-02 11:02:16 +0200580}
581
Paul Cercueil90189672015-03-16 11:41:53 +0100582#ifdef WITH_NETWORK_GET_BUFFER
Paul Cercueil7d7e5d32015-03-05 10:51:41 +0100583static ssize_t network_do_splice(int fd_out, int fd_in, size_t len)
Paul Cercueil09a43482015-03-04 15:50:27 +0100584{
585 int pipefd[2];
Paul Cercueil04065c22015-03-05 14:08:16 +0100586 ssize_t ret, read_len = len;
Paul Cercueil09a43482015-03-04 15:50:27 +0100587
588 ret = (ssize_t) pipe(pipefd);
589 if (ret < 0)
590 return -errno;
591
592 do {
Paul Cercueila107c6d2015-03-05 13:54:10 +0100593 /*
594 * SPLICE_F_NONBLOCK is just here to avoid a deadlock when
595 * splicing from a socket. As the socket is not in
596 * non-blocking mode, it should never return -EAGAIN.
597 * TODO(pcercuei): Find why it locks...
598 * */
599 ret = splice(fd_in, NULL, pipefd[1], NULL, len,
600 SPLICE_F_MOVE | SPLICE_F_NONBLOCK);
601 if (!ret)
602 ret = -EIO;
Paul Cercueil09a43482015-03-04 15:50:27 +0100603 if (ret < 0)
604 goto err_close_pipe;
605
Paul Cercueila107c6d2015-03-05 13:54:10 +0100606 ret = splice(pipefd[0], NULL, fd_out, NULL, ret,
607 SPLICE_F_MOVE | SPLICE_F_NONBLOCK);
608 if (!ret)
609 ret = -EIO;
Paul Cercueil09a43482015-03-04 15:50:27 +0100610 if (ret < 0)
611 goto err_close_pipe;
612
613 len -= ret;
614 } while (len);
615
616err_close_pipe:
617 close(pipefd[0]);
618 close(pipefd[1]);
Paul Cercueil04065c22015-03-05 14:08:16 +0100619 return ret < 0 ? ret : read_len;
Paul Cercueil09a43482015-03-04 15:50:27 +0100620}
621
622static ssize_t network_get_buffer(const struct iio_device *dev,
Paul Cercueil76ca8842015-03-05 11:16:16 +0100623 void **addr_ptr, size_t bytes_used,
624 uint32_t *mask, size_t words)
Paul Cercueil09a43482015-03-04 15:50:27 +0100625{
626 struct iio_device_pdata *pdata = dev->pdata;
Paul Cercueil04065c22015-03-05 14:08:16 +0100627 ssize_t ret, read = 0;
Paul Cercueilca9d3382015-05-04 14:30:09 +0200628 int memfd;
Paul Cercueil09a43482015-03-04 15:50:27 +0100629 bool tx;
Paul Cercueil09a43482015-03-04 15:50:27 +0100630
Paul Cercueil04065c22015-03-05 14:08:16 +0100631 if (pdata->is_cyclic)
Paul Cercueil09a43482015-03-04 15:50:27 +0100632 return -ENOSYS;
Paul Cercueilca9d3382015-05-04 14:30:09 +0200633
634 /* We check early that the temporary file can be created, so that we can
635 * return -ENOSYS in case it fails, which will indicate that the
636 * high-speed interface is not available.
637 *
638 * O_TMPFILE -> Linux 3.11.
639 * TODO: use memfd_create (Linux 3.17) */
640 memfd = open(P_tmpdir, O_RDWR | O_TMPFILE | O_EXCL, S_IRWXU);
641 if (memfd < 0)
642 return -ENOSYS;
643
644 if (!addr_ptr || words != (dev->nb_channels + 31) / 32) {
645 close(memfd);
Paul Cercueil09a43482015-03-04 15:50:27 +0100646 return -EINVAL;
Paul Cercueilca9d3382015-05-04 14:30:09 +0200647 }
Paul Cercueil09a43482015-03-04 15:50:27 +0100648
Paul Cercueile6e5a092015-03-04 16:33:26 +0100649 if (pdata->mmap_addr)
650 munmap(pdata->mmap_addr, pdata->mmap_len);
Paul Cercueil09a43482015-03-04 15:50:27 +0100651
Paul Cercueile6e5a092015-03-04 16:33:26 +0100652 if (pdata->mmap_addr && pdata->is_tx) {
Paul Cercueil09a43482015-03-04 15:50:27 +0100653 char buf[1024];
654 snprintf(buf, sizeof(buf), "WRITEBUF %s %lu\r\n",
Paul Cercueild957b982015-08-05 14:39:01 +0200655 dev->id, (unsigned long) bytes_used);
Paul Cercueil09a43482015-03-04 15:50:27 +0100656
Paul Cercueil388dcd62015-11-27 15:15:48 +0100657 iio_mutex_lock(pdata->lock);
Paul Cercueil09a43482015-03-04 15:50:27 +0100658
Lars-Peter Clausen78067fc2016-02-11 12:48:01 +0100659 ret = write_rwbuf_command(dev, buf);
Paul Cercueil09a43482015-03-04 15:50:27 +0100660 if (ret < 0)
Paul Cercueilca9d3382015-05-04 14:30:09 +0200661 goto err_close_memfd;
Paul Cercueil09a43482015-03-04 15:50:27 +0100662
Paul Cercueild957b982015-08-05 14:39:01 +0200663 ret = network_do_splice(pdata->fd, pdata->memfd, bytes_used);
Paul Cercueil09a43482015-03-04 15:50:27 +0100664 if (ret < 0)
Paul Cercueilca9d3382015-05-04 14:30:09 +0200665 goto err_close_memfd;
Paul Cercueil09a43482015-03-04 15:50:27 +0100666
667 pdata->wait_for_err_code = true;
Paul Cercueil388dcd62015-11-27 15:15:48 +0100668 iio_mutex_unlock(pdata->lock);
Paul Cercueil09a43482015-03-04 15:50:27 +0100669 }
670
671 if (pdata->memfd >= 0)
672 close(pdata->memfd);
673
Paul Cercueilca9d3382015-05-04 14:30:09 +0200674 pdata->memfd = memfd;
Paul Cercueil09a43482015-03-04 15:50:27 +0100675
Paul Cercueilef32d582015-03-05 11:18:35 +0100676 ret = (ssize_t) ftruncate(pdata->memfd, pdata->mmap_len);
677 if (ret < 0) {
678 ret = -errno;
679 ERROR("Unable to truncate temp file: %zi\n", -ret);
680 return ret;
681 }
Paul Cercueil09a43482015-03-04 15:50:27 +0100682
Paul Cercueil04065c22015-03-05 14:08:16 +0100683 if (!pdata->is_tx) {
684 char buf[1024];
685 size_t len = pdata->mmap_len;
686
687 snprintf(buf, sizeof(buf), "READBUF %s %lu\r\n",
688 dev->id, (unsigned long) len);
689
Paul Cercueil388dcd62015-11-27 15:15:48 +0100690 iio_mutex_lock(pdata->lock);
Lars-Peter Clausen78067fc2016-02-11 12:48:01 +0100691 ret = write_rwbuf_command(dev, buf);
Paul Cercueil04065c22015-03-05 14:08:16 +0100692 if (ret < 0)
693 goto err_unlock;
694
695 do {
696 ret = network_read_mask(pdata->fd, mask, words);
697 if (!ret)
698 break;
699 if (ret < 0)
700 goto err_unlock;
701
702 mask = NULL; /* We read the mask only once */
703
704 ret = network_do_splice(pdata->memfd, pdata->fd, ret);
705 if (ret < 0)
706 goto err_unlock;
707
708 read += ret;
709 len -= ret;
710 } while (len);
711
Paul Cercueil388dcd62015-11-27 15:15:48 +0100712 iio_mutex_unlock(pdata->lock);
Paul Cercueil04065c22015-03-05 14:08:16 +0100713 }
Paul Cercueil09a43482015-03-04 15:50:27 +0100714
Paul Cercueile6e5a092015-03-04 16:33:26 +0100715 pdata->mmap_addr = mmap(NULL, pdata->mmap_len,
Paul Cercueil09a43482015-03-04 15:50:27 +0100716 PROT_READ | PROT_WRITE, MAP_SHARED, pdata->memfd, 0);
Paul Cercueile6e5a092015-03-04 16:33:26 +0100717 if (pdata->mmap_addr == MAP_FAILED) {
718 pdata->mmap_addr = NULL;
Paul Cercueil09a43482015-03-04 15:50:27 +0100719 ret = -errno;
Paul Cercueil7d7e5d32015-03-05 10:51:41 +0100720 ERROR("Unable to mmap: %zi\n", -ret);
Paul Cercueil09a43482015-03-04 15:50:27 +0100721 return ret;
722 }
723
Paul Cercueile6e5a092015-03-04 16:33:26 +0100724 *addr_ptr = pdata->mmap_addr;
Paul Cercueil04065c22015-03-05 14:08:16 +0100725 return read ? read : bytes_used;
Paul Cercueil09a43482015-03-04 15:50:27 +0100726
Paul Cercueilca9d3382015-05-04 14:30:09 +0200727err_close_memfd:
728 close(memfd);
Paul Cercueil09a43482015-03-04 15:50:27 +0100729err_unlock:
Paul Cercueil388dcd62015-11-27 15:15:48 +0100730 iio_mutex_unlock(pdata->lock);
Paul Cercueil09a43482015-03-04 15:50:27 +0100731 return ret;
732}
733#endif
734
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100735static ssize_t network_read_dev_attr(const struct iio_device *dev,
Paul Cercueil50c762a2014-04-14 15:55:43 +0200736 const char *attr, char *dst, size_t len, bool is_debug)
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100737{
Paul Cercueil2e28d502015-11-30 18:46:49 +0100738 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueil5b577762014-06-03 15:31:42 +0200739
Paul Cercueil2e28d502015-11-30 18:46:49 +0100740 return iiod_client_read_attr(pdata->iiod_client, pdata->fd,
741 dev, NULL, attr, dst, len, is_debug);
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100742}
743
Paul Cercueil07897d32014-03-06 12:46:08 +0100744static ssize_t network_write_dev_attr(const struct iio_device *dev,
Paul Cercueilcecda352014-05-06 18:14:29 +0200745 const char *attr, const char *src, size_t len, bool is_debug)
Paul Cercueil07897d32014-03-06 12:46:08 +0100746{
Paul Cercueil2e28d502015-11-30 18:46:49 +0100747 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueil5b577762014-06-03 15:31:42 +0200748
Paul Cercueil2e28d502015-11-30 18:46:49 +0100749 return iiod_client_write_attr(pdata->iiod_client, pdata->fd,
750 dev, NULL, attr, src, len, is_debug);
Paul Cercueil07897d32014-03-06 12:46:08 +0100751}
752
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100753static ssize_t network_read_chn_attr(const struct iio_channel *chn,
754 const char *attr, char *dst, size_t len)
755{
Paul Cercueil2e28d502015-11-30 18:46:49 +0100756 struct iio_context_pdata *pdata = chn->dev->ctx->pdata;
Paul Cercueil5b577762014-06-03 15:31:42 +0200757
Paul Cercueil2e28d502015-11-30 18:46:49 +0100758 return iiod_client_read_attr(pdata->iiod_client, pdata->fd,
759 chn->dev, chn, attr, dst, len, false);
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100760}
761
Paul Cercueil07897d32014-03-06 12:46:08 +0100762static ssize_t network_write_chn_attr(const struct iio_channel *chn,
Paul Cercueilcecda352014-05-06 18:14:29 +0200763 const char *attr, const char *src, size_t len)
Paul Cercueil07897d32014-03-06 12:46:08 +0100764{
Paul Cercueil2e28d502015-11-30 18:46:49 +0100765 struct iio_context_pdata *pdata = chn->dev->ctx->pdata;
Paul Cercueil5b577762014-06-03 15:31:42 +0200766
Paul Cercueil2e28d502015-11-30 18:46:49 +0100767 return iiod_client_write_attr(pdata->iiod_client, pdata->fd,
768 chn->dev, chn, attr, src, len, false);
Paul Cercueil07897d32014-03-06 12:46:08 +0100769}
770
Paul Cercueildcab40c2014-03-11 10:59:14 +0100771static int network_get_trigger(const struct iio_device *dev,
772 const struct iio_device **trigger)
773{
774 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueildcab40c2014-03-11 10:59:14 +0100775
Paul Cercueilcae6c2a2015-11-30 16:37:19 +0100776 return iiod_client_get_trigger(pdata->iiod_client,
777 pdata->fd, dev, trigger);
Paul Cercueildcab40c2014-03-11 10:59:14 +0100778}
779
780static int network_set_trigger(const struct iio_device *dev,
781 const struct iio_device *trigger)
782{
Paul Cercueila7b2aae2015-12-04 16:02:00 +0100783 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueil1494b862014-05-05 12:49:07 +0200784
Paul Cercueilcae6c2a2015-11-30 16:37:19 +0100785 return iiod_client_set_trigger(pdata->iiod_client,
786 pdata->fd, dev, trigger);
Paul Cercueildcab40c2014-03-11 10:59:14 +0100787}
788
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +0100789static void network_shutdown(struct iio_context *ctx)
790{
791 struct iio_context_pdata *pdata = ctx->pdata;
Paul Cercueil44ae11c2014-03-17 09:56:29 +0100792 unsigned int i;
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +0100793
Paul Cercueil388dcd62015-11-27 15:15:48 +0100794 iio_mutex_lock(pdata->lock);
Paul Cercueilbb76bf92014-03-11 10:20:18 +0100795 write_command("\r\nEXIT\r\n", pdata->fd);
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +0100796 close(pdata->fd);
Paul Cercueil388dcd62015-11-27 15:15:48 +0100797 iio_mutex_unlock(pdata->lock);
Paul Cercueil1494b862014-05-05 12:49:07 +0200798
Paul Cercueil439e1a22015-02-24 13:50:51 +0100799 for (i = 0; i < ctx->nb_devices; i++) {
800 struct iio_device *dev = ctx->devices[i];
Paul Cercueilc5b00752015-02-24 14:29:53 +0100801 struct iio_device_pdata *dpdata = dev->pdata;
Paul Cercueil439e1a22015-02-24 13:50:51 +0100802
Paul Cercueilc5b00752015-02-24 14:29:53 +0100803 if (dpdata) {
Paul Cercueilaa0db142015-03-04 16:25:24 +0100804 network_close(dev);
Paul Cercueil388dcd62015-11-27 15:15:48 +0100805 iio_mutex_destroy(dpdata->lock);
Paul Cercueilc5b00752015-02-24 14:29:53 +0100806 free(dpdata);
Paul Cercueil439e1a22015-02-24 13:50:51 +0100807 }
808 }
809
Paul Cercueil157d43c2015-11-30 15:05:06 +0100810 iiod_client_destroy(pdata->iiod_client);
Paul Cercueil388dcd62015-11-27 15:15:48 +0100811 iio_mutex_destroy(pdata->lock);
Paul Cercueil2dd2c132015-02-24 10:41:01 +0100812 freeaddrinfo(pdata->addrinfo);
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +0100813 free(pdata);
814}
815
Paul Cercueil6691a3f2014-05-02 12:32:01 +0200816static int network_get_version(const struct iio_context *ctx,
Paul Cercueil9de9e9d2014-05-20 13:18:19 +0200817 unsigned int *major, unsigned int *minor, char git_tag[8])
Paul Cercueil6691a3f2014-05-02 12:32:01 +0200818{
Paul Cercueil2e209f92015-11-30 15:05:38 +0100819 return iiod_client_get_version(ctx->pdata->iiod_client, ctx->pdata->fd,
820 major, minor, git_tag);
Paul Cercueil6691a3f2014-05-02 12:32:01 +0200821}
822
Paul Cercueil8a266f12014-06-10 16:06:31 +0200823static unsigned int calculate_remote_timeout(unsigned int timeout)
824{
825 /* XXX(pcercuei): We currently hardcode timeout / 2 for the backend used
826 * by the remote. Is there something better to do here? */
827 return timeout / 2;
828}
829
Paul Cercueilbca3dbc2014-06-11 12:00:21 +0200830static int network_set_timeout(struct iio_context *ctx, unsigned int timeout)
831{
Paul Cercueilf996ae12015-12-03 12:29:13 +0100832 struct iio_context_pdata *pdata = ctx->pdata;
833 int ret, fd = pdata->fd;
834
835 ret = set_socket_timeout(fd, timeout);
Paul Cercueilbca3dbc2014-06-11 12:00:21 +0200836 if (!ret) {
837 timeout = calculate_remote_timeout(timeout);
Paul Cercueilf996ae12015-12-03 12:29:13 +0100838 ret = iiod_client_set_timeout(pdata->iiod_client, fd, timeout);
Paul Cercueilbca3dbc2014-06-11 12:00:21 +0200839 }
840 if (ret < 0) {
841 char buf[1024];
Paul Cercueil53fc4852015-05-22 10:57:53 +0200842 iio_strerror(-ret, buf, sizeof(buf));
Paul Cercueil8a266f12014-06-10 16:06:31 +0200843 WARNING("Unable to set R/W timeout: %s\n", buf);
844 } else {
Paul Cercueil8a266f12014-06-10 16:06:31 +0200845 ctx->rw_timeout_ms = timeout;
846 }
847 return ret;
848}
849
Paul Cercueil61157f92015-11-19 17:48:22 +0100850static int network_set_kernel_buffers_count(const struct iio_device *dev,
851 unsigned int nb_blocks)
852{
Paul Cercueila9810a82015-11-30 16:55:07 +0100853 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueil61157f92015-11-19 17:48:22 +0100854
Paul Cercueila9810a82015-11-30 16:55:07 +0100855 return iiod_client_set_kernel_buffers_count(pdata->iiod_client,
856 pdata->fd, dev, nb_blocks);
Paul Cercueil61157f92015-11-19 17:48:22 +0100857}
858
Paul Cercueil12d41832014-10-28 14:35:53 +0100859static struct iio_context * network_clone(const struct iio_context *ctx)
860{
Paul Cercueil7ef45ce2015-03-16 14:36:16 +0100861 if (ctx->description) {
862 char *ptr = strchr(ctx->description, ' ');
863 if (ptr) {
864#ifdef HAVE_IPV6
865 char buf[INET6_ADDRSTRLEN + IF_NAMESIZE + 2];
866#else
867 char buf[INET_ADDRSTRLEN + 1];
868#endif
869 strncpy(buf, ctx->description, sizeof(buf) - 1);
870 buf[ptr - ctx->description] = '\0';
871 return iio_create_network_context(buf);
872 }
873 }
874
Paul Cercueild3d56ee2015-01-08 16:36:31 +0100875 return iio_create_network_context(ctx->description);
Paul Cercueil12d41832014-10-28 14:35:53 +0100876}
877
Lars-Peter Clausen09a59d72016-02-03 15:27:04 +0100878static const struct iio_backend_ops network_ops = {
Paul Cercueil12d41832014-10-28 14:35:53 +0100879 .clone = network_clone,
Paul Cercueilba059762014-03-14 11:02:02 +0100880 .open = network_open,
881 .close = network_close,
Paul Cercueil9945bc82014-03-05 14:07:29 +0100882 .read = network_read,
Paul Cercueil2725f702014-05-02 11:02:16 +0200883 .write = network_write,
Paul Cercueil90189672015-03-16 11:41:53 +0100884#ifdef WITH_NETWORK_GET_BUFFER
Paul Cercueil09a43482015-03-04 15:50:27 +0100885 .get_buffer = network_get_buffer,
886#endif
Paul Cercueil3e94a5b2014-03-04 13:00:41 +0100887 .read_device_attr = network_read_dev_attr,
888 .write_device_attr = network_write_dev_attr,
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100889 .read_channel_attr = network_read_chn_attr,
Paul Cercueil07897d32014-03-06 12:46:08 +0100890 .write_channel_attr = network_write_chn_attr,
Paul Cercueildcab40c2014-03-11 10:59:14 +0100891 .get_trigger = network_get_trigger,
892 .set_trigger = network_set_trigger,
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +0100893 .shutdown = network_shutdown,
Paul Cercueil6691a3f2014-05-02 12:32:01 +0200894 .get_version = network_get_version,
Paul Cercueil8a266f12014-06-10 16:06:31 +0200895 .set_timeout = network_set_timeout,
Paul Cercueil61157f92015-11-19 17:48:22 +0100896 .set_kernel_buffers_count = network_set_kernel_buffers_count,
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100897};
898
Paul Cercueil157d43c2015-11-30 15:05:06 +0100899static ssize_t network_write_data(struct iio_context_pdata *pdata,
900 int desc, const char *src, size_t len)
901{
902 ssize_t ret;
903
904 ret = send(desc, src, (int) len, 0);
905 if (ret < 0) {
Lars-Peter Clausendd04e6a2016-02-10 14:56:36 +0100906 return (ssize_t) network_get_error();
Paul Cercueil157d43c2015-11-30 15:05:06 +0100907 } else if (ret == 0) {
908 return -EPIPE;
909 } else {
910 return ret;
911 }
912}
913
914static ssize_t network_read_data(struct iio_context_pdata *pdata,
915 int desc, char *dst, size_t len)
916{
917 ssize_t ret;
918
919 ret = recv(desc, dst, (int) len, 0);
920 if (ret < 0) {
Lars-Peter Clausendd04e6a2016-02-10 14:56:36 +0100921 return (ssize_t) network_get_error();
Paul Cercueil157d43c2015-11-30 15:05:06 +0100922 } else {
923 return ret;
924 }
925}
926
927static ssize_t network_read_line(struct iio_context_pdata *pdata,
928 int desc, char *dst, size_t len)
929{
930 size_t i;
Paul Cercueilce02dc92016-01-07 11:46:38 +0100931#ifdef __linux__
932 ssize_t ret;
933
934 /* First read from the socket without advancing the read offset */
935 ret = recv(desc, dst, len, MSG_PEEK);
936 if (ret < 0)
937 return ret;
938
939 /* Lookup for the trailing \n */
940 for (i = 0; i < (size_t) ret && dst[i] != '\n'; i++);
941
942 /* No \n found? Just garbage data */
943 if (i == (size_t) ret)
944 return -EIO;
945
946 /* Advance the read offset to the byte following the \n */
947 return recv(desc, dst, i + 1, MSG_TRUNC);
948#else
Paul Cercueil157d43c2015-11-30 15:05:06 +0100949 bool found = false;
950
951 for (i = 0; i < len - 1; i++) {
952 ssize_t ret = network_read_data(pdata, desc, dst + i, 1);
953
954 if (ret < 0)
955 return ret;
956
957 if (dst[i] != '\n')
958 found = true;
959 else if (found)
960 break;
961 }
962
963 dst[i] = '\0';
964 return (ssize_t) i;
Paul Cercueilce02dc92016-01-07 11:46:38 +0100965#endif
Paul Cercueil157d43c2015-11-30 15:05:06 +0100966}
967
Lars-Peter Clausen09a59d72016-02-03 15:27:04 +0100968static const struct iiod_client_ops network_iiod_client_ops = {
Paul Cercueil157d43c2015-11-30 15:05:06 +0100969 .write = network_write_data,
970 .read = network_read_data,
971 .read_line = network_read_line,
972};
973
Paul Cercueil63e52182014-12-11 12:52:48 +0100974struct iio_context * network_create_context(const char *host)
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100975{
Paul Cercueil2e38bbe2014-03-19 15:27:15 +0100976 struct addrinfo hints, *res;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100977 struct iio_context *ctx;
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +0100978 struct iio_context_pdata *pdata;
Paul Cercueil0e3c04d2015-05-13 17:37:11 +0200979 size_t i, len;
Paul Cercueil4970ac32015-02-24 10:59:00 +0100980 int fd, ret;
Paul Cercueilb03c9ef2015-03-16 14:07:58 +0100981 char *description;
Paul Cercueil1fef1a52014-04-07 16:31:15 +0200982#ifdef _WIN32
983 WSADATA wsaData;
984
985 ret = WSAStartup(MAKEWORD(2, 0), &wsaData);
986 if (ret < 0) {
987 ERROR("WSAStartup failed with error %i\n", ret);
Paul Cercueilcc575532015-03-16 17:15:24 +0100988 errno = -ret;
Paul Cercueil1fef1a52014-04-07 16:31:15 +0200989 return NULL;
990 }
991#endif
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100992
Paul Cercueil2e38bbe2014-03-19 15:27:15 +0100993 memset(&hints, 0, sizeof(hints));
994 hints.ai_family = AF_UNSPEC;
995 hints.ai_socktype = SOCK_STREAM;
Paul Cercueilab114932014-05-19 13:03:17 +0200996
997#ifdef HAVE_AVAHI
998 if (!host) {
999 char addr_str[AVAHI_ADDRESS_STR_MAX];
1000 char port_str[6];
1001 AvahiAddress address;
Paul Cercueil0f729d32014-06-05 17:38:54 +02001002 uint16_t port = IIOD_PORT;
Paul Cercueilab114932014-05-19 13:03:17 +02001003
1004 memset(&address, 0, sizeof(address));
1005
1006 ret = discover_host(&address, &port);
1007 if (ret < 0) {
Paul Cercueilcc575532015-03-16 17:15:24 +01001008 DEBUG("Unable to find host: %s\n", strerror(-ret));
1009 errno = -ret;
Paul Cercueilab114932014-05-19 13:03:17 +02001010 return NULL;
1011 }
1012
1013 avahi_address_snprint(addr_str, sizeof(addr_str), &address);
1014 snprintf(port_str, sizeof(port_str), "%hu", port);
1015 ret = getaddrinfo(addr_str, port_str, &hints, &res);
1016 } else
1017#endif
1018 {
1019 ret = getaddrinfo(host, IIOD_PORT_STR, &hints, &res);
1020 }
1021
Paul Cercueil2e38bbe2014-03-19 15:27:15 +01001022 if (ret) {
Paul Cercueilab114932014-05-19 13:03:17 +02001023 ERROR("Unable to find host: %s\n", gai_strerror(ret));
Paul Cercueild9eb4cb2015-03-23 14:30:20 +01001024#ifndef _WIN32
Paul Cercueilcc575532015-03-16 17:15:24 +01001025 if (ret != EAI_SYSTEM)
1026 errno = ret;
Paul Cercueild9eb4cb2015-03-23 14:30:20 +01001027#endif
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001028 return NULL;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001029 }
1030
Paul Cercueil4970ac32015-02-24 10:59:00 +01001031 fd = create_socket(res);
Paul Cercueilcc575532015-03-16 17:15:24 +01001032 if (fd < 0) {
1033 errno = fd;
Paul Cercueil2dd2c132015-02-24 10:41:01 +01001034 goto err_free_addrinfo;
Paul Cercueilcc575532015-03-16 17:15:24 +01001035 }
Paul Cercueilbca3dbc2014-06-11 12:00:21 +02001036
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001037 pdata = calloc(1, sizeof(*pdata));
1038 if (!pdata) {
Paul Cercueilcc575532015-03-16 17:15:24 +01001039 errno = ENOMEM;
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001040 goto err_close_socket;
1041 }
1042
1043 pdata->fd = fd;
Paul Cercueil2dd2c132015-02-24 10:41:01 +01001044 pdata->addrinfo = res;
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001045
Paul Cercueil157d43c2015-11-30 15:05:06 +01001046 pdata->lock = iio_mutex_create();
1047 if (!pdata->lock) {
1048 errno = ENOMEM;
1049 goto err_free_pdata;
1050 }
1051
1052 pdata->iiod_client = iiod_client_new(pdata, pdata->lock,
1053 &network_iiod_client_ops);
1054 if (!pdata->iiod_client)
1055 goto err_destroy_mutex;
1056
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001057 DEBUG("Creating context...\n");
Paul Cercueil94726b12015-12-01 11:15:54 +01001058 ctx = iiod_client_create_context(pdata->iiod_client, fd);
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001059 if (!ctx)
Paul Cercueil157d43c2015-11-30 15:05:06 +01001060 goto err_destroy_iiod_client;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001061
Paul Cercueil2057fd32014-10-28 14:44:19 +01001062 /* Override the name and low-level functions of the XML context
1063 * with those corresponding to the network context */
1064 ctx->name = "network";
1065 ctx->ops = &network_ops;
1066 ctx->pdata = pdata;
1067
Paul Cercueil06c479d2015-01-08 16:14:57 +01001068#ifdef HAVE_IPV6
Paul Cercueiled15e492015-01-12 15:52:28 +01001069 len = INET6_ADDRSTRLEN + IF_NAMESIZE + 2;
Paul Cercueil06c479d2015-01-08 16:14:57 +01001070#else
1071 len = INET_ADDRSTRLEN + 1;
1072#endif
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001073
1074 description = malloc(len);
1075 if (!description) {
Paul Cercueilcc575532015-03-16 17:15:24 +01001076 ret = -ENOMEM;
Paul Cercueil06c479d2015-01-08 16:14:57 +01001077 goto err_network_shutdown;
1078 }
1079
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001080 description[0] = '\0';
Paul Cercueil06c479d2015-01-08 16:14:57 +01001081
1082#ifdef HAVE_IPV6
1083 if (res->ai_family == AF_INET6) {
1084 struct sockaddr_in6 *in = (struct sockaddr_in6 *) res->ai_addr;
Paul Cercueiled15e492015-01-12 15:52:28 +01001085 char *ptr;
Paul Cercueil06c479d2015-01-08 16:14:57 +01001086 inet_ntop(AF_INET6, &in->sin6_addr,
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001087 description, INET6_ADDRSTRLEN);
Paul Cercueiled15e492015-01-12 15:52:28 +01001088
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001089 ptr = if_indextoname(in->sin6_scope_id, description +
1090 strlen(description) + 1);
Paul Cercueiled15e492015-01-12 15:52:28 +01001091 if (!ptr) {
Paul Cercueilcc575532015-03-16 17:15:24 +01001092 ret = -errno;
Paul Cercueiled15e492015-01-12 15:52:28 +01001093 ERROR("Unable to lookup interface of IPv6 address\n");
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001094 goto err_free_description;
Paul Cercueiled15e492015-01-12 15:52:28 +01001095 }
1096
1097 *(ptr - 1) = '%';
Paul Cercueil06c479d2015-01-08 16:14:57 +01001098 }
1099#endif
1100 if (res->ai_family == AF_INET) {
1101 struct sockaddr_in *in = (struct sockaddr_in *) res->ai_addr;
Paul Cercueile7a31692015-07-15 10:52:47 +02001102#if (!_WIN32 || _WIN32_WINNT >= 0x600)
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001103 inet_ntop(AF_INET, &in->sin_addr, description, INET_ADDRSTRLEN);
Paul Cercueile7a31692015-07-15 10:52:47 +02001104#else
1105 char *tmp = inet_ntoa(in->sin_addr);
1106 strncpy(description, tmp, len);
1107#endif
Paul Cercueil06c479d2015-01-08 16:14:57 +01001108 }
1109
Paul Cercueil44ae11c2014-03-17 09:56:29 +01001110 for (i = 0; i < ctx->nb_devices; i++) {
1111 struct iio_device *dev = ctx->devices[i];
Paul Cercueilff778232014-03-24 14:23:08 +01001112
Paul Cercueil439e1a22015-02-24 13:50:51 +01001113 dev->pdata = calloc(1, sizeof(*dev->pdata));
1114 if (!dev->pdata) {
Paul Cercueilcc575532015-03-16 17:15:24 +01001115 ret = -ENOMEM;
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001116 goto err_free_description;
Paul Cercueil439e1a22015-02-24 13:50:51 +01001117 }
1118
1119 dev->pdata->fd = -1;
Paul Cercueil90189672015-03-16 11:41:53 +01001120#ifdef WITH_NETWORK_GET_BUFFER
Paul Cercueil09a43482015-03-04 15:50:27 +01001121 dev->pdata->memfd = -1;
Paul Cercueile6e5a092015-03-04 16:33:26 +01001122#endif
Paul Cercueilc5b00752015-02-24 14:29:53 +01001123
Paul Cercueil388dcd62015-11-27 15:15:48 +01001124 dev->pdata->lock = iio_mutex_create();
1125 if (!dev->pdata->lock) {
1126 ret = -ENOMEM;
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001127 goto err_free_description;
Paul Cercueil388dcd62015-11-27 15:15:48 +01001128 }
Paul Cercueil44ae11c2014-03-17 09:56:29 +01001129 }
1130
Paul Cercueilfd387472015-08-05 10:34:19 +02001131 ret = iio_context_init(ctx);
1132 if (ret < 0)
1133 goto err_free_description;
Paul Cercueil4c6729d2014-04-04 17:24:41 +02001134
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001135 if (ctx->description) {
Paul Cercueil0e3c04d2015-05-13 17:37:11 +02001136 size_t desc_len = strlen(description);
1137 size_t new_size = desc_len + strlen(ctx->description) + 2;
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001138 char *ptr, *new_description = realloc(description, new_size);
Paul Cercueilcc575532015-03-16 17:15:24 +01001139 if (!new_description) {
1140 ret = -ENOMEM;
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001141 goto err_free_description;
Paul Cercueilcc575532015-03-16 17:15:24 +01001142 }
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001143
1144 ptr = strrchr(new_description, '\0');
Paul Cercueil0e3c04d2015-05-13 17:37:11 +02001145 snprintf(ptr, new_size - desc_len, " %s", ctx->description);
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001146 free(ctx->description);
1147
1148 ctx->description = new_description;
1149 } else {
1150 ctx->description = description;
1151 }
1152
Paul Cercueilf996ae12015-12-03 12:29:13 +01001153 iiod_client_set_timeout(pdata->iiod_client, fd,
1154 calculate_remote_timeout(DEFAULT_TIMEOUT_MS));
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001155 return ctx;
1156
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001157err_free_description:
1158 free(description);
Paul Cercueil44ae11c2014-03-17 09:56:29 +01001159err_network_shutdown:
Paul Cercueil44ae11c2014-03-17 09:56:29 +01001160 iio_context_destroy(ctx);
Paul Cercueilcc575532015-03-16 17:15:24 +01001161 errno = -ret;
Paul Cercueil2057fd32014-10-28 14:44:19 +01001162 return NULL;
Paul Cercueil157d43c2015-11-30 15:05:06 +01001163
1164err_destroy_iiod_client:
1165 iiod_client_destroy(pdata->iiod_client);
1166err_destroy_mutex:
1167 iio_mutex_destroy(pdata->lock);
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001168err_free_pdata:
1169 free(pdata);
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001170err_close_socket:
1171 close(fd);
Paul Cercueil2dd2c132015-02-24 10:41:01 +01001172err_free_addrinfo:
1173 freeaddrinfo(res);
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001174 return NULL;
1175}