blob: 0af5c18d3063de4e6c0a629bdd7d7ad2d1b4e0eb [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 Cercueil2814ed12016-08-25 17:08:18 +020019#include "iio-config.h"
Paul Cercueilc7bad0f2014-03-03 17:06:48 +010020#include "iio-private.h"
Paul Cercueil388dcd62015-11-27 15:15:48 +010021#include "iio-lock.h"
Paul Cercueil157d43c2015-11-30 15:05:06 +010022#include "iiod-client.h"
Paul Cercueil9aabe892014-09-02 12:33:46 +020023
Paul Cercueilc7bad0f2014-03-03 17:06:48 +010024#include <errno.h>
Paul Cercueila7d445b2014-11-11 16:00:15 +010025#include <fcntl.h>
Paul Cercueile60c3ed2014-03-04 11:23:56 +010026#include <stdbool.h>
Paul Cercueilc7bad0f2014-03-03 17:06:48 +010027#include <string.h>
28#include <sys/types.h>
Paul Cercueilab114932014-05-19 13:03:17 +020029#include <time.h>
Paul Cercueil1fef1a52014-04-07 16:31:15 +020030
31#ifdef _WIN32
32#include <winsock2.h>
33#include <ws2tcpip.h>
34#define close(s) closesocket(s)
35
36/* winsock2.h defines ERROR, we don't want that */
37#undef ERROR
38
39#else /* _WIN32 */
Paul Cercueil06c479d2015-01-08 16:14:57 +010040#include <arpa/inet.h>
Paul Cercueil1fef1a52014-04-07 16:31:15 +020041#include <netdb.h>
Paul Cercueil0c3ce452015-02-05 16:37:42 +010042#include <netinet/in.h>
Paul Cercueil0b584e12015-01-28 11:47:03 +010043#include <netinet/tcp.h>
Paul Cercueiled15e492015-01-12 15:52:28 +010044#include <net/if.h>
Paul Cercueil09a43482015-03-04 15:50:27 +010045#include <sys/mman.h>
Paul Cercueil83628b32014-11-24 11:26:21 +010046#include <sys/select.h>
Paul Cercueilc7bad0f2014-03-03 17:06:48 +010047#include <sys/socket.h>
48#include <unistd.h>
Paul Cercueil1fef1a52014-04-07 16:31:15 +020049#endif /* _WIN32 */
50
Paul Cercueilab114932014-05-19 13:03:17 +020051#ifdef HAVE_AVAHI
52#include <avahi-client/client.h>
53#include <avahi-common/error.h>
54#include <avahi-client/lookup.h>
55#include <avahi-common/simple-watch.h>
56#endif
57
Paul Cercueil1fef1a52014-04-07 16:31:15 +020058#include "debug.h"
Paul Cercueilc7bad0f2014-03-03 17:06:48 +010059
Paul Cercueil8a266f12014-06-10 16:06:31 +020060#define DEFAULT_TIMEOUT_MS 5000
61
Paul Cercueil2e38bbe2014-03-19 15:27:15 +010062#define _STRINGIFY(x) #x
63#define STRINGIFY(x) _STRINGIFY(x)
64
Paul Cercueilc7bad0f2014-03-03 17:06:48 +010065#define IIOD_PORT 30431
Paul Cercueil2e38bbe2014-03-19 15:27:15 +010066#define IIOD_PORT_STR STRINGIFY(IIOD_PORT)
Paul Cercueilc7bad0f2014-03-03 17:06:48 +010067
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +020068struct iio_network_io_context {
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +010069 int fd;
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +020070
71 /* Only buffer IO contexts can be cancelled. */
72 bool cancellable;
73 bool cancelled;
74#if defined(_WIN32)
75 WSAEVENT events[2];
76#elif defined(WITH_NETWORK_EVENTFD)
77 int cancel_fd[1]; /* eventfd */
78#else
79 int cancel_fd[2]; /* pipe */
80#endif
Paul Cercueil6a0eb582016-08-30 10:54:22 +020081 unsigned int timeout_ms;
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +020082};
83
84struct iio_context_pdata {
85 struct iio_network_io_context io_ctx;
Paul Cercueil2dd2c132015-02-24 10:41:01 +010086 struct addrinfo *addrinfo;
Paul Cercueil388dcd62015-11-27 15:15:48 +010087 struct iio_mutex *lock;
Paul Cercueil157d43c2015-11-30 15:05:06 +010088 struct iiod_client *iiod_client;
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +010089};
90
Paul Cercueil439e1a22015-02-24 13:50:51 +010091struct iio_device_pdata {
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +020092 struct iio_network_io_context io_ctx;
Paul Cercueil90189672015-03-16 11:41:53 +010093#ifdef WITH_NETWORK_GET_BUFFER
Paul Cercueil09a43482015-03-04 15:50:27 +010094 int memfd;
Paul Cercueile6e5a092015-03-04 16:33:26 +010095 void *mmap_addr;
Paul Cercueil09a43482015-03-04 15:50:27 +010096 size_t mmap_len;
Paul Cercueile6e5a092015-03-04 16:33:26 +010097#endif
Paul Cercueil09a43482015-03-04 15:50:27 +010098 bool wait_for_err_code, is_cyclic, is_tx;
Paul Cercueil388dcd62015-11-27 15:15:48 +010099 struct iio_mutex *lock;
Paul Cercueil439e1a22015-02-24 13:50:51 +0100100};
101
Lars-Peter Clausendd04e6a2016-02-10 14:56:36 +0100102#ifdef _WIN32
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200103
Lars-Peter Clausenac256dd2017-03-07 13:06:15 +0100104static int set_blocking_mode(int s, bool blocking)
105{
Lars-Peter Clausencd3c49f2017-03-30 15:04:19 +0200106 unsigned long nonblock;
Lars-Peter Clausenac256dd2017-03-07 13:06:15 +0100107 int ret;
108
109 nonblock = blocking ? 0 : 1;
110
111 ret = ioctlsocket(s, FIONBIO, &nonblock);
112 if (ret == SOCKET_ERROR) {
113 ret = -WSAGetLastError();
114 return ret;
115 }
116
117 return 0;
118}
119
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200120static int setup_cancel(struct iio_network_io_context *io_ctx)
121{
122 io_ctx->events[0] = WSACreateEvent();
123 if (io_ctx->events[0] == WSA_INVALID_EVENT)
124 return -ENOMEM; /* Pretty much the only error that can happen */
125
126 io_ctx->events[1] = WSACreateEvent();
127 if (io_ctx->events[1] == WSA_INVALID_EVENT) {
128 WSACloseEvent(io_ctx->events[0]);
129 return -ENOMEM;
130 }
131
132 return 0;
133}
134
135static void cleanup_cancel(struct iio_network_io_context *io_ctx)
136{
137 WSACloseEvent(io_ctx->events[0]);
138 WSACloseEvent(io_ctx->events[1]);
139}
140
141static void do_cancel(struct iio_network_io_context *io_ctx)
142{
143 WSASetEvent(io_ctx->events[1]);
144}
145
146static int wait_cancellable(struct iio_network_io_context *io_ctx, bool read)
147{
148 long wsa_events = FD_CLOSE;
149 DWORD ret;
150
151 if (!io_ctx->cancellable)
152 return 0;
153
154 if (read)
155 wsa_events |= FD_READ;
156 else
157 wsa_events |= FD_WRITE;
158
159 WSAEventSelect(io_ctx->fd, NULL, 0);
160 WSAResetEvent(io_ctx->events[0]);
161 WSAEventSelect(io_ctx->fd, io_ctx->events[0], wsa_events);
162
163 ret = WSAWaitForMultipleEvents(2, io_ctx->events, FALSE,
164 WSA_INFINITE, FALSE);
165
166 if (ret == WSA_WAIT_EVENT_0 + 1)
167 return -EBADF;
168
169 return 0;
170}
171
Lars-Peter Clausendd04e6a2016-02-10 14:56:36 +0100172static int network_get_error(void)
173{
174 return -WSAGetLastError();
175}
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200176
177static bool network_should_retry(int err)
178{
Paul Cercueil63a48ed2016-09-01 12:27:46 +0200179 return err == -WSAEWOULDBLOCK || err == -WSAETIMEDOUT;
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200180}
181
Paul Cercueilb419f1e2016-08-31 16:59:57 +0200182static bool network_is_interrupted(int err)
183{
184 return false;
185}
186
Lars-Peter Clausenac256dd2017-03-07 13:06:15 +0100187static bool network_connect_in_progress(int err)
188{
189 return err == -WSAEWOULDBLOCK;
190}
191
192#define NETWORK_ERR_TIMEOUT WSAETIMEDOUT
193
Lars-Peter Clausendd04e6a2016-02-10 14:56:36 +0100194#else
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200195
Paul Cercueil7a683272016-08-24 14:50:20 +0200196static int set_blocking_mode(int fd, bool blocking)
197{
198 int ret = fcntl(fd, F_GETFL, 0);
199 if (ret < 0)
200 return -errno;
201
202 if (blocking)
203 ret &= ~O_NONBLOCK;
204 else
205 ret |= O_NONBLOCK;
206
207 ret = fcntl(fd, F_SETFL, ret);
208 return ret < 0 ? -errno : 0;
209}
210
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200211#include <poll.h>
212
213#if defined(WITH_NETWORK_EVENTFD)
214
215#include <sys/eventfd.h>
216
217static int create_cancel_fd(struct iio_network_io_context *io_ctx)
218{
219 io_ctx->cancel_fd[0] = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
220 if (io_ctx->cancel_fd[0] < 0)
221 return -errno;
222 return 0;
223}
224
225static void cleanup_cancel(struct iio_network_io_context *io_ctx)
226{
227 close(io_ctx->cancel_fd[0]);
228}
229
230#define CANCEL_WR_FD 0
231
232#else
233
234static int create_cancel_fd(struct iio_network_io_context *io_ctx)
235{
236 int ret;
237
238#ifdef HAS_PIPE2
239 ret = pipe2(io_ctx->cancel_fd, O_CLOEXEC | O_NONBLOCK);
240 if (ret < 0 && errno != ENOSYS) /* If ENOSYS try pipe() */
241 return -errno;
242#endif
243 ret = pipe(io_ctx->cancel_fd);
244 if (ret < 0)
245 return -errno;
246 ret = set_blocking_mode(io_ctx->cancel_fd[0], false);
247 if (ret < 0)
248 goto err_close;
249 ret = set_blocking_mode(io_ctx->cancel_fd[1], false);
250 if (ret < 0)
251 goto err_close;
252
253 return 0;
254err_close:
255 close(io_ctx->cancel_fd[0]);
256 close(io_ctx->cancel_fd[1]);
257 return ret;
258}
259
260static void cleanup_cancel(struct iio_network_io_context *io_ctx)
261{
262 close(io_ctx->cancel_fd[0]);
263 close(io_ctx->cancel_fd[1]);
264}
265
266#define CANCEL_WR_FD 1
267
268#endif
269
270static int setup_cancel(struct iio_network_io_context *io_ctx)
271{
272 int ret;
273
274 ret = set_blocking_mode(io_ctx->fd, false);
275 if (ret)
276 return ret;
277
278 return create_cancel_fd(io_ctx);
279}
280
281static void do_cancel(struct iio_network_io_context *io_ctx)
282{
283 uint64_t event = 1;
284 int ret;
285
286 ret = write(io_ctx->cancel_fd[CANCEL_WR_FD], &event, sizeof(event));
287 if (ret == -1) {
288 /* If this happens something went very seriously wrong */
289 char err_str[1024];
290 iio_strerror(errno, err_str, sizeof(err_str));
291 ERROR("Unable to signal cancellation event: %s\n", err_str);
292 }
293}
294
295static int wait_cancellable(struct iio_network_io_context *io_ctx, bool read)
296{
297 struct pollfd pfd[2];
298 int ret;
299
300 if (!io_ctx->cancellable)
301 return 0;
302
303 memset(pfd, 0, sizeof(pfd));
304
305 pfd[0].fd = io_ctx->fd;
306 if (read)
307 pfd[0].events = POLLIN;
308 else
309 pfd[0].events = POLLOUT;
310 pfd[1].fd = io_ctx->cancel_fd[0];
311 pfd[1].events = POLLIN;
312
313 do {
Paul Cercueil6a0eb582016-08-30 10:54:22 +0200314 int timeout_ms;
315
316 if (io_ctx->timeout_ms > 0)
317 timeout_ms = (int) io_ctx->timeout_ms;
318 else
319 timeout_ms = -1;
320
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200321 do {
Paul Cercueil6a0eb582016-08-30 10:54:22 +0200322 ret = poll(pfd, 2, timeout_ms);
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200323 } while (ret == -1 && errno == EINTR);
324
325 if (ret == -1)
326 return -errno;
Paul Cercueil6a0eb582016-08-30 10:54:22 +0200327 if (!ret)
Paul Cercueile8e7aca2016-08-31 16:15:13 +0200328 return -EPIPE;
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200329
330 if (pfd[1].revents & POLLIN)
331 return -EBADF;
332 } while (!(pfd[0].revents & (pfd[0].events | POLLERR | POLLHUP)));
333
334 return 0;
335}
336
Lars-Peter Clausendd04e6a2016-02-10 14:56:36 +0100337static int network_get_error(void)
338{
339 return -errno;
340}
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200341
342static bool network_should_retry(int err)
343{
Paul Cercueilb419f1e2016-08-31 16:59:57 +0200344 return err == -EAGAIN;
345}
346
347static bool network_is_interrupted(int err)
348{
349 return err == -EINTR;
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200350}
351
Lars-Peter Clausenac256dd2017-03-07 13:06:15 +0100352static bool network_connect_in_progress(int err)
353{
354 return err == -EINPROGRESS;
355}
356
357#define NETWORK_ERR_TIMEOUT ETIMEDOUT
358
Lars-Peter Clausendd04e6a2016-02-10 14:56:36 +0100359#endif
360
Paul Cercueilab114932014-05-19 13:03:17 +0200361#ifdef HAVE_AVAHI
362struct avahi_discovery_data {
363 AvahiSimplePoll *poll;
364 AvahiAddress *address;
365 uint16_t *port;
366 bool found, resolved;
367};
368
369static void __avahi_resolver_cb(AvahiServiceResolver *resolver,
370 __notused AvahiIfIndex iface, __notused AvahiProtocol proto,
371 __notused AvahiResolverEvent event, __notused const char *name,
372 __notused const char *type, __notused const char *domain,
373 __notused const char *host_name, const AvahiAddress *address,
374 uint16_t port, __notused AvahiStringList *txt,
375 __notused AvahiLookupResultFlags flags, void *d)
376{
377 struct avahi_discovery_data *ddata = (struct avahi_discovery_data *) d;
378
379 memcpy(ddata->address, address, sizeof(*address));
380 *ddata->port = port;
381 ddata->resolved = true;
382 avahi_service_resolver_free(resolver);
383}
384
385static void __avahi_browser_cb(AvahiServiceBrowser *browser,
386 AvahiIfIndex iface, AvahiProtocol proto,
387 AvahiBrowserEvent event, const char *name,
388 const char *type, const char *domain,
389 __notused AvahiLookupResultFlags flags, void *d)
390{
391 struct avahi_discovery_data *ddata = (struct avahi_discovery_data *) d;
392 struct AvahiClient *client = avahi_service_browser_get_client(browser);
393
394 switch (event) {
395 default:
396 case AVAHI_BROWSER_NEW:
397 ddata->found = !!avahi_service_resolver_new(client, iface,
398 proto, name, type, domain,
399 AVAHI_PROTO_UNSPEC, 0,
400 __avahi_resolver_cb, d);
401 break;
402 case AVAHI_BROWSER_ALL_FOR_NOW:
403 if (ddata->found) {
404 while (!ddata->resolved) {
405 struct timespec ts;
406 ts.tv_sec = 0;
407 ts.tv_nsec = 4000000;
408 nanosleep(&ts, NULL);
409 }
410 }
411 case AVAHI_BROWSER_FAILURE: /* fall-through */
412 avahi_simple_poll_quit(ddata->poll);
413 case AVAHI_BROWSER_CACHE_EXHAUSTED: /* fall-through */
414 break;
415 }
416}
417
418static int discover_host(AvahiAddress *addr, uint16_t *port)
419{
420 struct avahi_discovery_data ddata;
421 int ret = 0;
422 AvahiClient *client;
423 AvahiServiceBrowser *browser;
424 AvahiSimplePoll *poll = avahi_simple_poll_new();
425 if (!poll)
426 return -ENOMEM;
427
428 client = avahi_client_new(avahi_simple_poll_get(poll),
429 0, NULL, NULL, &ret);
430 if (!client) {
431 ERROR("Unable to start ZeroConf client :%s\n",
432 avahi_strerror(ret));
433 goto err_free_poll;
434 }
435
436 memset(&ddata, 0, sizeof(ddata));
437 ddata.poll = poll;
438 ddata.address = addr;
439 ddata.port = port;
440
441 browser = avahi_service_browser_new(client,
442 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
443 "_iio._tcp", NULL, 0, __avahi_browser_cb, &ddata);
444 if (!browser) {
445 ret = avahi_client_errno(client);
446 ERROR("Unable to create ZeroConf browser: %s\n",
447 avahi_strerror(ret));
448 goto err_free_client;
449 }
450
451 DEBUG("Trying to discover host\n");
452 avahi_simple_poll_loop(poll);
453
454 if (!ddata.found)
455 ret = ENXIO;
456
457 avahi_service_browser_free(browser);
458err_free_client:
459 avahi_client_free(client);
460err_free_poll:
461 avahi_simple_poll_free(poll);
462 return -ret; /* we want a negative error code */
463}
464#endif /* HAVE_AVAHI */
465
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200466static ssize_t network_recv(struct iio_network_io_context *io_ctx,
467 void *data, size_t len, int flags)
Lars-Peter Clausen6a6c8662016-02-12 15:35:52 +0100468{
469 ssize_t ret;
470 int err;
471
472 while (1) {
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200473 ret = wait_cancellable(io_ctx, true);
474 if (ret < 0)
475 return ret;
476
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200477 ret = recv(io_ctx->fd, data, (int) len, flags);
Lars-Peter Clausen6a6c8662016-02-12 15:35:52 +0100478 if (ret == 0)
479 return -EPIPE;
480 else if (ret > 0)
481 break;
482
483 err = network_get_error();
Paul Cercueilb419f1e2016-08-31 16:59:57 +0200484 if (network_should_retry(err)) {
485 if (io_ctx->cancellable)
486 continue;
487 else
488 return -EPIPE;
489 } else if (!network_is_interrupted(err)) {
Lars-Peter Clausen6a6c8662016-02-12 15:35:52 +0100490 return (ssize_t) err;
Paul Cercueilb419f1e2016-08-31 16:59:57 +0200491 }
Lars-Peter Clausen6a6c8662016-02-12 15:35:52 +0100492 }
493 return ret;
494}
495
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200496static ssize_t network_send(struct iio_network_io_context *io_ctx,
497 const void *data, size_t len, int flags)
Lars-Peter Clausen6a6c8662016-02-12 15:35:52 +0100498{
499 ssize_t ret;
500 int err;
501
502 while (1) {
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200503 ret = wait_cancellable(io_ctx, false);
504 if (ret < 0)
505 return ret;
506
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200507 ret = send(io_ctx->fd, data, (int) len, flags);
Lars-Peter Clausen6a6c8662016-02-12 15:35:52 +0100508 if (ret == 0)
509 return -EPIPE;
510 else if (ret > 0)
511 break;
512
513 err = network_get_error();
Paul Cercueilb419f1e2016-08-31 16:59:57 +0200514 if (network_should_retry(err)) {
515 if (io_ctx->cancellable)
516 continue;
517 else
518 return -EPIPE;
519 } else if (!network_is_interrupted(err)) {
Lars-Peter Clausen6a6c8662016-02-12 15:35:52 +0100520 return (ssize_t) err;
Paul Cercueilb419f1e2016-08-31 16:59:57 +0200521 }
Lars-Peter Clausen6a6c8662016-02-12 15:35:52 +0100522 }
523
524 return ret;
525}
526
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200527static ssize_t write_all(struct iio_network_io_context *io_ctx,
528 const void *src, size_t len)
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100529{
Paul Cercueil6e7f79e2014-04-04 12:27:24 +0200530 uintptr_t ptr = (uintptr_t) src;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100531 while (len) {
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200532 ssize_t ret = network_send(io_ctx, (const void *) ptr, len, 0);
Lars-Peter Clausen27a53e42016-04-13 17:08:07 +0200533 if (ret < 0)
534 return ret;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100535 ptr += ret;
536 len -= ret;
537 }
Paul Cercueil4012cff2015-05-11 10:47:40 +0200538 return (ssize_t)(ptr - (uintptr_t) src);
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100539}
540
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200541static ssize_t write_command(struct iio_network_io_context *io_ctx,
542 const char *cmd)
Paul Cercueilbb76bf92014-03-11 10:20:18 +0100543{
544 ssize_t ret;
545
546 DEBUG("Writing command: %s\n", cmd);
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200547 ret = write_all(io_ctx, cmd, strlen(cmd));
Paul Cercueilbb76bf92014-03-11 10:20:18 +0100548 if (ret < 0) {
549 char buf[1024];
Paul Cercueil53fc4852015-05-22 10:57:53 +0200550 iio_strerror(-ret, buf, sizeof(buf));
Paul Cercueilbb76bf92014-03-11 10:20:18 +0100551 ERROR("Unable to send command: %s\n", buf);
552 }
553 return ret;
554}
555
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200556static void network_cancel(const struct iio_device *dev)
557{
558 struct iio_device_pdata *ppdata = dev->pdata;
559
560 do_cancel(&ppdata->io_ctx);
561
562 ppdata->io_ctx.cancelled = true;
563}
564
Paul Cercueil4970ac32015-02-24 10:59:00 +0100565#ifndef _WIN32
Lars-Peter Clausena2cbc812016-02-11 14:49:27 +0100566
567/* Use it if available */
Paul Cercueild454e012016-06-28 15:19:19 +0200568#ifndef SOCK_CLOEXEC
569#define SOCK_CLOEXEC 0
Lars-Peter Clausena2cbc812016-02-11 14:49:27 +0100570#endif
571
Lars-Peter Clausenac256dd2017-03-07 13:06:15 +0100572static int do_create_socket(const struct addrinfo *addrinfo)
Paul Cercueil4970ac32015-02-24 10:59:00 +0100573{
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100574 int fd;
575
Lars-Peter Clausena2cbc812016-02-11 14:49:27 +0100576 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype | SOCK_CLOEXEC, 0);
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100577 if (fd < 0)
578 return -errno;
Paul Cercueil4970ac32015-02-24 10:59:00 +0100579
Lars-Peter Clausenebbd9072016-02-11 15:15:08 +0100580 return fd;
Paul Cercueil4970ac32015-02-24 10:59:00 +0100581}
582
583static int set_socket_timeout(int fd, unsigned int timeout)
584{
585 struct timeval tv;
586
587 tv.tv_sec = timeout / 1000;
588 tv.tv_usec = (timeout % 1000) * 1000;
589 if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0 ||
590 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,
591 &tv, sizeof(tv)) < 0)
592 return -errno;
593 else
594 return 0;
595}
596#else
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100597
Lars-Peter Clausena2cbc812016-02-11 14:49:27 +0100598/* Use it if available */
599#ifndef WSA_FLAG_NO_HANDLE_INHERIT
600#define WSA_FLAG_NO_HANDLE_INHERIT 0
601#endif
602
Lars-Peter Clausenac256dd2017-03-07 13:06:15 +0100603static int do_create_socket(const struct addrinfo *addrinfo)
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100604{
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100605 SOCKET s;
606
Paul Cercueilba16cea2016-04-18 12:14:58 +0200607 s = WSASocketW(addrinfo->ai_family, addrinfo->ai_socktype, 0, NULL, 0,
Paul Cercueil9dc0a622016-09-01 12:28:30 +0200608 WSA_FLAG_NO_HANDLE_INHERIT | WSA_FLAG_OVERLAPPED);
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100609 if (s == INVALID_SOCKET)
610 return -WSAGetLastError();
611
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100612 return (int) s;
613}
614
Paul Cercueil4970ac32015-02-24 10:59:00 +0100615static int set_socket_timeout(int fd, unsigned int timeout)
616{
617 if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO,
618 (const char *) &timeout, sizeof(timeout)) < 0 ||
619 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,
620 (const char *) &timeout, sizeof(timeout)) < 0)
Paul Cercueil135b3612015-06-30 14:10:14 +0200621 return -WSAGetLastError();
Paul Cercueil4970ac32015-02-24 10:59:00 +0100622 else
623 return 0;
624}
625#endif /* !_WIN32 */
626
Lars-Peter Clausenac256dd2017-03-07 13:06:15 +0100627/* The purpose of this function is to provide a version of connect()
628 * that does not ignore timeouts... */
629static int do_connect(int fd, const struct addrinfo *addrinfo,
630 unsigned int timeout)
Paul Cercueil4970ac32015-02-24 10:59:00 +0100631{
Paul Cercueil6a0eb582016-08-30 10:54:22 +0200632 struct timeval tv;
Lars-Peter Clausen828b5482017-03-07 13:50:53 +0100633 struct timeval *ptv;
Lars-Peter Clausenac256dd2017-03-07 13:06:15 +0100634 int ret, error;
635 socklen_t len;
636 fd_set set;
637
638 ret = set_blocking_mode(fd, false);
639 if (ret < 0)
640 return ret;
641
642 ret = connect(fd, addrinfo->ai_addr, (int) addrinfo->ai_addrlen);
643 if (ret < 0) {
644 ret = network_get_error();
645 if (!network_connect_in_progress(ret))
646 return ret;
647 }
648
649 FD_ZERO(&set);
650 FD_SET(fd, &set);
Paul Cercueil4970ac32015-02-24 10:59:00 +0100651
Lars-Peter Clausen828b5482017-03-07 13:50:53 +0100652 if (timeout != 0) {
653 tv.tv_sec = timeout / 1000;
654 tv.tv_usec = (timeout % 1000) * 1000;
655 ptv = &tv;
656 } else {
657 ptv = NULL;
658 }
Paul Cercueil4970ac32015-02-24 10:59:00 +0100659
Lars-Peter Clausenac256dd2017-03-07 13:06:15 +0100660 ret = select(fd + 1, NULL, &set, &set, ptv);
661 if (ret < 0)
662 return network_get_error();
663
664 if (ret == 0)
665 return -NETWORK_ERR_TIMEOUT;
666
667 /* Verify that we don't have an error */
668 len = sizeof(error);
669 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, (char *)&error, &len);
670 if(ret < 0)
671 return network_get_error();
672
673 if (error)
674 return -error;
675
676 ret = set_blocking_mode(fd, true);
677 if (ret < 0)
678 return ret;
679
680 return 0;
681}
682
683static int create_socket(const struct addrinfo *addrinfo, unsigned int timeout)
684{
685 int ret, fd, yes = 1;
686
687 fd = do_create_socket(addrinfo);
Lars-Peter Clausen1ca01b72016-02-10 15:10:09 +0100688 if (fd < 0)
689 return fd;
Paul Cercueil4970ac32015-02-24 10:59:00 +0100690
Lars-Peter Clausenac256dd2017-03-07 13:06:15 +0100691 ret = do_connect(fd, addrinfo, timeout);
692 if (ret < 0) {
693 close(fd);
694 return ret;
695 }
696
Lars-Peter Clausen6f127f02017-03-09 15:20:04 +0100697 set_socket_timeout(fd, timeout);
Paul Cercueil32dce822016-12-12 14:51:59 +0100698 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
Paul Cercueilaf3370e2017-02-01 12:14:12 +0100699 (const char *) &yes, sizeof(yes)) < 0) {
700 ret = -errno;
701 close(fd);
702 return ret;
703 }
Paul Cercueil32dce822016-12-12 14:51:59 +0100704
Paul Cercueil4970ac32015-02-24 10:59:00 +0100705 return fd;
706}
707
Paul Cercueil92f15c22015-04-20 11:36:51 +0200708static int network_open(const struct iio_device *dev,
709 size_t samples_count, bool cyclic)
Paul Cercueilba059762014-03-14 11:02:02 +0100710{
Paul Cercueil439e1a22015-02-24 13:50:51 +0100711 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueil80dc1082015-12-01 18:20:31 +0100712 struct iio_device_pdata *ppdata = dev->pdata;
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200713 int ret = -EBUSY;
Paul Cercueilba059762014-03-14 11:02:02 +0100714
Paul Cercueil80dc1082015-12-01 18:20:31 +0100715 iio_mutex_lock(ppdata->lock);
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200716 if (ppdata->io_ctx.fd >= 0)
Paul Cercueil80dc1082015-12-01 18:20:31 +0100717 goto out_mutex_unlock;
Paul Cercueil439e1a22015-02-24 13:50:51 +0100718
Lars-Peter Clausen6f127f02017-03-09 15:20:04 +0100719 ret = create_socket(pdata->addrinfo, DEFAULT_TIMEOUT_MS);
Paul Cercueil80dc1082015-12-01 18:20:31 +0100720 if (ret < 0)
721 goto out_mutex_unlock;
Paul Cercueilba059762014-03-14 11:02:02 +0100722
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200723 ppdata->io_ctx.fd = ret;
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200724 ppdata->io_ctx.cancelled = false;
Lars-Peter Clausen6f127f02017-03-09 15:20:04 +0100725 ppdata->io_ctx.timeout_ms = DEFAULT_TIMEOUT_MS;
726
727 ret = iiod_client_open_unlocked(pdata->iiod_client,
728 &ppdata->io_ctx, dev, samples_count, cyclic);
729 if (ret < 0)
730 goto err_close_socket;
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200731
732 ret = setup_cancel(&ppdata->io_ctx);
733 if (ret < 0)
734 goto err_close_socket;
Paul Cercueilba059762014-03-14 11:02:02 +0100735
Lars-Peter Clausen6f127f02017-03-09 15:20:04 +0100736 set_socket_timeout(ppdata->io_ctx.fd, pdata->io_ctx.timeout_ms);
Paul Cercueil439e1a22015-02-24 13:50:51 +0100737
Lars-Peter Clausen6f127f02017-03-09 15:20:04 +0100738 ppdata->io_ctx.timeout_ms = pdata->io_ctx.timeout_ms;
739 ppdata->io_ctx.cancellable = true;
Paul Cercueil80dc1082015-12-01 18:20:31 +0100740 ppdata->is_tx = iio_device_is_tx(dev);
741 ppdata->is_cyclic = cyclic;
Paul Cercueil80dc1082015-12-01 18:20:31 +0100742 ppdata->wait_for_err_code = false;
Paul Cercueild957b982015-08-05 14:39:01 +0200743#ifdef WITH_NETWORK_GET_BUFFER
Paul Cercueil80dc1082015-12-01 18:20:31 +0100744 ppdata->mmap_len = samples_count * iio_device_get_sample_size(dev);
Paul Cercueild957b982015-08-05 14:39:01 +0200745#endif
Paul Cercueil80dc1082015-12-01 18:20:31 +0100746
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200747 iio_mutex_unlock(ppdata->lock);
748
749 return 0;
750
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200751err_close_socket:
752 close(ppdata->io_ctx.fd);
753 ppdata->io_ctx.fd = -1;
Paul Cercueil80dc1082015-12-01 18:20:31 +0100754out_mutex_unlock:
755 iio_mutex_unlock(ppdata->lock);
756 return ret;
Paul Cercueilba059762014-03-14 11:02:02 +0100757}
758
Lars-Peter Clausen90e75a92016-02-22 11:34:34 +0100759static int network_close(const struct iio_device *dev)
760{
761 struct iio_device_pdata *pdata = dev->pdata;
762 int ret = -EBADF;
763
764 iio_mutex_lock(pdata->lock);
765
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200766 if (pdata->io_ctx.fd >= 0) {
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200767 if (!pdata->io_ctx.cancelled) {
768 ret = iiod_client_close_unlocked(
769 dev->ctx->pdata->iiod_client,
Lars-Peter Clausen2767d4d2016-06-23 14:19:02 +0200770 &pdata->io_ctx, dev);
Lars-Peter Clausen90e75a92016-02-22 11:34:34 +0100771
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200772 write_command(&pdata->io_ctx, "\r\nEXIT\r\n");
773 } else {
774 ret = 0;
775 }
Lars-Peter Clausen90e75a92016-02-22 11:34:34 +0100776
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200777 cleanup_cancel(&pdata->io_ctx);
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200778 close(pdata->io_ctx.fd);
779 pdata->io_ctx.fd = -1;
Lars-Peter Clausen90e75a92016-02-22 11:34:34 +0100780 }
781
782#ifdef WITH_NETWORK_GET_BUFFER
783 if (pdata->memfd >= 0)
784 close(pdata->memfd);
785 pdata->memfd = -1;
786
787 if (pdata->mmap_addr) {
788 munmap(pdata->mmap_addr, pdata->mmap_len);
789 pdata->mmap_addr = NULL;
790 }
791#endif
792
793 iio_mutex_unlock(pdata->lock);
794 return ret;
795}
796
797static ssize_t network_read(const struct iio_device *dev, void *dst, size_t len,
798 uint32_t *mask, size_t words)
799{
800 struct iio_device_pdata *pdata = dev->pdata;
801 ssize_t ret;
802
803 iio_mutex_lock(pdata->lock);
804 ret = iiod_client_read_unlocked(dev->ctx->pdata->iiod_client,
Lars-Peter Clausen2767d4d2016-06-23 14:19:02 +0200805 &pdata->io_ctx, dev, dst, len, mask, words);
Lars-Peter Clausen90e75a92016-02-22 11:34:34 +0100806 iio_mutex_unlock(pdata->lock);
807
808 return ret;
809}
810
811static ssize_t network_write(const struct iio_device *dev,
812 const void *src, size_t len)
813{
814 struct iio_device_pdata *pdata = dev->pdata;
815 ssize_t ret;
816
817 iio_mutex_lock(pdata->lock);
818 ret = iiod_client_write_unlocked(dev->ctx->pdata->iiod_client,
Lars-Peter Clausen2767d4d2016-06-23 14:19:02 +0200819 &pdata->io_ctx, dev, src, len);
Lars-Peter Clausen90e75a92016-02-22 11:34:34 +0100820 iio_mutex_unlock(pdata->lock);
821
822 return ret;
823}
824
825#ifdef WITH_NETWORK_GET_BUFFER
826
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200827static ssize_t read_all(struct iio_network_io_context *io_ctx,
828 void *dst, size_t len)
Lars-Peter Clausen90e75a92016-02-22 11:34:34 +0100829{
830 uintptr_t ptr = (uintptr_t) dst;
831 while (len) {
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200832 ssize_t ret = network_recv(io_ctx, (void *) ptr, len, 0);
Lars-Peter Clausen90e75a92016-02-22 11:34:34 +0100833 if (ret < 0)
834 return ret;
835 ptr += ret;
836 len -= ret;
837 }
838 return (ssize_t)(ptr - (uintptr_t) dst);
839}
840
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200841static int read_integer(struct iio_network_io_context *io_ctx, long *val)
Lars-Peter Clausen90e75a92016-02-22 11:34:34 +0100842{
843 unsigned int i;
844 char buf[1024], *ptr;
845 ssize_t ret;
846 bool found = false;
847
848 for (i = 0; i < sizeof(buf) - 1; i++) {
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200849 ret = read_all(io_ctx, buf + i, 1);
Lars-Peter Clausen90e75a92016-02-22 11:34:34 +0100850 if (ret < 0)
851 return (int) ret;
852
853 /* Skip the eventual first few carriage returns.
854 * Also stop when a dot is found (for parsing floats) */
855 if (buf[i] != '\n' && buf[i] != '.')
856 found = true;
857 else if (found)
858 break;
859 }
860
861 buf[i] = '\0';
862 ret = (ssize_t) strtol(buf, &ptr, 10);
863 if (ptr == buf)
864 return -EINVAL;
865 *val = (long) ret;
866 return 0;
867}
868
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200869static ssize_t network_read_mask(struct iio_network_io_context *io_ctx,
870 uint32_t *mask, size_t words)
Lars-Peter Clausen90e75a92016-02-22 11:34:34 +0100871{
872 long read_len;
873 ssize_t ret;
874
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200875 ret = read_integer(io_ctx, &read_len);
Lars-Peter Clausen90e75a92016-02-22 11:34:34 +0100876 if (ret < 0)
877 return ret;
878
879 if (read_len > 0 && mask) {
880 size_t i;
881 char buf[9];
882
883 buf[8] = '\0';
884 DEBUG("Reading mask\n");
885
886 for (i = words; i > 0; i--) {
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200887 ret = read_all(io_ctx, buf, 8);
Lars-Peter Clausen90e75a92016-02-22 11:34:34 +0100888 if (ret < 0)
889 return ret;
890
891 sscanf(buf, "%08x", &mask[i - 1]);
Paul Cercueilb133a0c2016-04-18 11:57:50 +0200892 DEBUG("mask[%lu] = 0x%x\n",
893 (unsigned long)(i - 1), mask[i - 1]);
Lars-Peter Clausen90e75a92016-02-22 11:34:34 +0100894 }
895 }
896
897 if (read_len > 0) {
898 char c;
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200899 ssize_t nb = read_all(io_ctx, &c, 1);
Lars-Peter Clausen90e75a92016-02-22 11:34:34 +0100900 if (nb > 0 && c != '\n')
901 read_len = -EIO;
902 }
903
904 return (ssize_t) read_len;
905}
906
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200907static ssize_t read_error_code(struct iio_network_io_context *io_ctx)
Paul Cercueil8f7f6c42015-02-24 15:00:35 +0100908{
909 /*
910 * The server returns two integer codes.
911 * The first one is returned right after the WRITEBUF command is issued,
912 * and corresponds to the error code returned when the server attempted
913 * to open the device.
914 * If zero, a second error code is returned, that corresponds (if positive)
915 * to the number of bytes written.
916 *
917 * To speed up things, we delay error reporting. We just send out the
918 * data without reading the error code that the server gives us, because
919 * the answer will take too much time. If an error occured, it will be
920 * reported by the next call to iio_buffer_push().
921 */
922
923 unsigned int i;
924 long resp = 0;
925
926 for (i = 0; i < 2; i++) {
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200927 ssize_t ret = read_integer(io_ctx, &resp);
Paul Cercueil8f7f6c42015-02-24 15:00:35 +0100928 if (ret < 0)
929 return ret;
930 if (resp < 0)
931 return (ssize_t) resp;
932 }
933
Paul Cercueil7d7e5d32015-03-05 10:51:41 +0100934 return (ssize_t) resp;
Paul Cercueil8f7f6c42015-02-24 15:00:35 +0100935}
936
Paul Cercueil3c0da372015-03-05 11:36:39 +0100937static ssize_t write_rwbuf_command(const struct iio_device *dev,
Lars-Peter Clausen2e380822016-02-11 20:36:13 +0100938 const char *cmd)
Paul Cercueil3c0da372015-03-05 11:36:39 +0100939{
940 struct iio_device_pdata *pdata = dev->pdata;
Paul Cercueil3c0da372015-03-05 11:36:39 +0100941
942 if (pdata->wait_for_err_code) {
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200943 ssize_t ret = read_error_code(&pdata->io_ctx);
Paul Cercueil3c0da372015-03-05 11:36:39 +0100944
945 pdata->wait_for_err_code = false;
946 if (ret < 0)
947 return ret;
948 }
949
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +0200950 return write_command(&pdata->io_ctx, cmd);
Paul Cercueil3c0da372015-03-05 11:36:39 +0100951}
952
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200953static ssize_t network_do_splice(struct iio_device_pdata *pdata, size_t len,
954 bool read)
Paul Cercueil09a43482015-03-04 15:50:27 +0100955{
956 int pipefd[2];
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200957 int fd_in, fd_out;
Lars-Peter Clausenf83877e2016-10-26 18:20:06 +0200958 ssize_t ret, read_len = len, write_len = 0;
Paul Cercueil09a43482015-03-04 15:50:27 +0100959
Lars-Peter Clausena2cbc812016-02-11 14:49:27 +0100960 ret = (ssize_t) pipe2(pipefd, O_CLOEXEC);
Paul Cercueil09a43482015-03-04 15:50:27 +0100961 if (ret < 0)
962 return -errno;
963
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200964 if (read) {
965 fd_in = pdata->io_ctx.fd;
966 fd_out = pdata->memfd;
967 } else {
968 fd_in = pdata->memfd;
969 fd_out = pdata->io_ctx.fd;
970 }
971
Paul Cercueil09a43482015-03-04 15:50:27 +0100972 do {
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +0200973 ret = wait_cancellable(&pdata->io_ctx, read);
974 if (ret < 0)
975 goto err_close_pipe;
976
Lars-Peter Clausenf83877e2016-10-26 18:20:06 +0200977 if (read_len) {
978 /*
979 * SPLICE_F_NONBLOCK is just here to avoid a deadlock when
980 * splicing from a socket. As the socket is not in
981 * non-blocking mode, it should never return -EAGAIN.
982 * TODO(pcercuei): Find why it locks...
983 * */
984 ret = splice(fd_in, NULL, pipefd[1], NULL, read_len,
985 SPLICE_F_MOVE | SPLICE_F_NONBLOCK);
986 if (!ret)
987 ret = -EIO;
988 if (ret < 0 && errno != EAGAIN) {
989 ret = -errno;
990 goto err_close_pipe;
991 } else if (ret > 0) {
992 write_len += ret;
993 read_len -= ret;
994 }
Lars-Peter Clausen7247b922016-10-26 18:34:23 +0200995 }
Paul Cercueil09a43482015-03-04 15:50:27 +0100996
Lars-Peter Clausenf83877e2016-10-26 18:20:06 +0200997 if (write_len) {
998 ret = splice(pipefd[0], NULL, fd_out, NULL, write_len,
999 SPLICE_F_MOVE | SPLICE_F_NONBLOCK);
1000 if (!ret)
1001 ret = -EIO;
1002 if (ret < 0 && errno != EAGAIN) {
1003 ret = -errno;
1004 goto err_close_pipe;
1005 } else if (ret > 0) {
1006 write_len -= ret;
1007 }
Lars-Peter Clausen7247b922016-10-26 18:34:23 +02001008 }
Paul Cercueil09a43482015-03-04 15:50:27 +01001009
Lars-Peter Clausenf83877e2016-10-26 18:20:06 +02001010 } while (write_len || read_len);
Paul Cercueil09a43482015-03-04 15:50:27 +01001011
1012err_close_pipe:
1013 close(pipefd[0]);
1014 close(pipefd[1]);
Lars-Peter Clausenf83877e2016-10-26 18:20:06 +02001015 return ret < 0 ? ret : len;
Paul Cercueil09a43482015-03-04 15:50:27 +01001016}
1017
1018static ssize_t network_get_buffer(const struct iio_device *dev,
Paul Cercueil76ca8842015-03-05 11:16:16 +01001019 void **addr_ptr, size_t bytes_used,
1020 uint32_t *mask, size_t words)
Paul Cercueil09a43482015-03-04 15:50:27 +01001021{
1022 struct iio_device_pdata *pdata = dev->pdata;
Paul Cercueil04065c22015-03-05 14:08:16 +01001023 ssize_t ret, read = 0;
Paul Cercueilca9d3382015-05-04 14:30:09 +02001024 int memfd;
Paul Cercueil09a43482015-03-04 15:50:27 +01001025
Paul Cercueil04065c22015-03-05 14:08:16 +01001026 if (pdata->is_cyclic)
Paul Cercueil09a43482015-03-04 15:50:27 +01001027 return -ENOSYS;
Paul Cercueilca9d3382015-05-04 14:30:09 +02001028
1029 /* We check early that the temporary file can be created, so that we can
1030 * return -ENOSYS in case it fails, which will indicate that the
1031 * high-speed interface is not available.
1032 *
1033 * O_TMPFILE -> Linux 3.11.
1034 * TODO: use memfd_create (Linux 3.17) */
Lars-Peter Clausena2cbc812016-02-11 14:49:27 +01001035 memfd = open(P_tmpdir, O_RDWR | O_TMPFILE | O_EXCL | O_CLOEXEC, S_IRWXU);
Paul Cercueilca9d3382015-05-04 14:30:09 +02001036 if (memfd < 0)
1037 return -ENOSYS;
1038
1039 if (!addr_ptr || words != (dev->nb_channels + 31) / 32) {
1040 close(memfd);
Paul Cercueil09a43482015-03-04 15:50:27 +01001041 return -EINVAL;
Paul Cercueilca9d3382015-05-04 14:30:09 +02001042 }
Paul Cercueil09a43482015-03-04 15:50:27 +01001043
Paul Cercueile6e5a092015-03-04 16:33:26 +01001044 if (pdata->mmap_addr)
1045 munmap(pdata->mmap_addr, pdata->mmap_len);
Paul Cercueil09a43482015-03-04 15:50:27 +01001046
Paul Cercueile6e5a092015-03-04 16:33:26 +01001047 if (pdata->mmap_addr && pdata->is_tx) {
Paul Cercueil09a43482015-03-04 15:50:27 +01001048 char buf[1024];
Paul Cercueil9c9a5562017-01-24 10:48:31 +01001049
1050 iio_snprintf(buf, sizeof(buf), "WRITEBUF %s %lu\r\n",
Paul Cercueild957b982015-08-05 14:39:01 +02001051 dev->id, (unsigned long) bytes_used);
Paul Cercueil09a43482015-03-04 15:50:27 +01001052
Paul Cercueil388dcd62015-11-27 15:15:48 +01001053 iio_mutex_lock(pdata->lock);
Paul Cercueil09a43482015-03-04 15:50:27 +01001054
Lars-Peter Clausen78067fc2016-02-11 12:48:01 +01001055 ret = write_rwbuf_command(dev, buf);
Paul Cercueil09a43482015-03-04 15:50:27 +01001056 if (ret < 0)
Paul Cercueilca9d3382015-05-04 14:30:09 +02001057 goto err_close_memfd;
Paul Cercueil09a43482015-03-04 15:50:27 +01001058
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +02001059 ret = network_do_splice(pdata, bytes_used, false);
Paul Cercueil09a43482015-03-04 15:50:27 +01001060 if (ret < 0)
Paul Cercueilca9d3382015-05-04 14:30:09 +02001061 goto err_close_memfd;
Paul Cercueil09a43482015-03-04 15:50:27 +01001062
1063 pdata->wait_for_err_code = true;
Paul Cercueil388dcd62015-11-27 15:15:48 +01001064 iio_mutex_unlock(pdata->lock);
Paul Cercueil09a43482015-03-04 15:50:27 +01001065 }
1066
1067 if (pdata->memfd >= 0)
1068 close(pdata->memfd);
1069
Paul Cercueilca9d3382015-05-04 14:30:09 +02001070 pdata->memfd = memfd;
Paul Cercueil09a43482015-03-04 15:50:27 +01001071
Paul Cercueilef32d582015-03-05 11:18:35 +01001072 ret = (ssize_t) ftruncate(pdata->memfd, pdata->mmap_len);
1073 if (ret < 0) {
1074 ret = -errno;
1075 ERROR("Unable to truncate temp file: %zi\n", -ret);
1076 return ret;
1077 }
Paul Cercueil09a43482015-03-04 15:50:27 +01001078
Paul Cercueil04065c22015-03-05 14:08:16 +01001079 if (!pdata->is_tx) {
1080 char buf[1024];
1081 size_t len = pdata->mmap_len;
1082
Paul Cercueil9c9a5562017-01-24 10:48:31 +01001083 iio_snprintf(buf, sizeof(buf), "READBUF %s %lu\r\n",
Paul Cercueil04065c22015-03-05 14:08:16 +01001084 dev->id, (unsigned long) len);
1085
Paul Cercueil388dcd62015-11-27 15:15:48 +01001086 iio_mutex_lock(pdata->lock);
Lars-Peter Clausen78067fc2016-02-11 12:48:01 +01001087 ret = write_rwbuf_command(dev, buf);
Paul Cercueil04065c22015-03-05 14:08:16 +01001088 if (ret < 0)
1089 goto err_unlock;
1090
1091 do {
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +02001092 ret = network_read_mask(&pdata->io_ctx, mask, words);
Paul Cercueil04065c22015-03-05 14:08:16 +01001093 if (!ret)
1094 break;
1095 if (ret < 0)
1096 goto err_unlock;
1097
1098 mask = NULL; /* We read the mask only once */
1099
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +02001100 ret = network_do_splice(pdata, ret, true);
Paul Cercueil04065c22015-03-05 14:08:16 +01001101 if (ret < 0)
1102 goto err_unlock;
1103
1104 read += ret;
1105 len -= ret;
1106 } while (len);
1107
Paul Cercueil388dcd62015-11-27 15:15:48 +01001108 iio_mutex_unlock(pdata->lock);
Paul Cercueil04065c22015-03-05 14:08:16 +01001109 }
Paul Cercueil09a43482015-03-04 15:50:27 +01001110
Paul Cercueile6e5a092015-03-04 16:33:26 +01001111 pdata->mmap_addr = mmap(NULL, pdata->mmap_len,
Paul Cercueil09a43482015-03-04 15:50:27 +01001112 PROT_READ | PROT_WRITE, MAP_SHARED, pdata->memfd, 0);
Paul Cercueile6e5a092015-03-04 16:33:26 +01001113 if (pdata->mmap_addr == MAP_FAILED) {
1114 pdata->mmap_addr = NULL;
Paul Cercueil09a43482015-03-04 15:50:27 +01001115 ret = -errno;
Paul Cercueil7d7e5d32015-03-05 10:51:41 +01001116 ERROR("Unable to mmap: %zi\n", -ret);
Paul Cercueil09a43482015-03-04 15:50:27 +01001117 return ret;
1118 }
1119
Paul Cercueile6e5a092015-03-04 16:33:26 +01001120 *addr_ptr = pdata->mmap_addr;
Paul Cercueil7c3f5d22016-02-18 11:49:04 +01001121 return read ? read : (ssize_t) bytes_used;
Paul Cercueil09a43482015-03-04 15:50:27 +01001122
Paul Cercueilca9d3382015-05-04 14:30:09 +02001123err_close_memfd:
1124 close(memfd);
Paul Cercueil09a43482015-03-04 15:50:27 +01001125err_unlock:
Paul Cercueil388dcd62015-11-27 15:15:48 +01001126 iio_mutex_unlock(pdata->lock);
Paul Cercueil09a43482015-03-04 15:50:27 +01001127 return ret;
1128}
1129#endif
1130
Paul Cercueil99cf3d42014-03-06 12:35:26 +01001131static ssize_t network_read_dev_attr(const struct iio_device *dev,
Matt Fornero81f04a52017-11-30 14:36:37 -05001132 const char *attr, char *dst, size_t len, enum iio_attr_type type)
Paul Cercueil99cf3d42014-03-06 12:35:26 +01001133{
Paul Cercueil2e28d502015-11-30 18:46:49 +01001134 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueil5b577762014-06-03 15:31:42 +02001135
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +02001136 return iiod_client_read_attr(pdata->iiod_client,
Matt Fornero81f04a52017-11-30 14:36:37 -05001137 &pdata->io_ctx, dev, NULL, attr, dst, len, type);
Paul Cercueil99cf3d42014-03-06 12:35:26 +01001138}
1139
Paul Cercueil07897d32014-03-06 12:46:08 +01001140static ssize_t network_write_dev_attr(const struct iio_device *dev,
Matt Fornero81f04a52017-11-30 14:36:37 -05001141 const char *attr, const char *src, size_t len, enum iio_attr_type type)
Paul Cercueil07897d32014-03-06 12:46:08 +01001142{
Paul Cercueil2e28d502015-11-30 18:46:49 +01001143 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueil5b577762014-06-03 15:31:42 +02001144
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +02001145 return iiod_client_write_attr(pdata->iiod_client,
Matt Fornero81f04a52017-11-30 14:36:37 -05001146 &pdata->io_ctx, dev, NULL, attr, src, len, type);
Paul Cercueil07897d32014-03-06 12:46:08 +01001147}
1148
Paul Cercueil99cf3d42014-03-06 12:35:26 +01001149static ssize_t network_read_chn_attr(const struct iio_channel *chn,
1150 const char *attr, char *dst, size_t len)
1151{
Paul Cercueil2e28d502015-11-30 18:46:49 +01001152 struct iio_context_pdata *pdata = chn->dev->ctx->pdata;
Paul Cercueil5b577762014-06-03 15:31:42 +02001153
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +02001154 return iiod_client_read_attr(pdata->iiod_client,
Lars-Peter Clausen2767d4d2016-06-23 14:19:02 +02001155 &pdata->io_ctx, chn->dev, chn, attr, dst, len, false);
Paul Cercueil99cf3d42014-03-06 12:35:26 +01001156}
1157
Paul Cercueil07897d32014-03-06 12:46:08 +01001158static ssize_t network_write_chn_attr(const struct iio_channel *chn,
Paul Cercueilcecda352014-05-06 18:14:29 +02001159 const char *attr, const char *src, size_t len)
Paul Cercueil07897d32014-03-06 12:46:08 +01001160{
Paul Cercueil2e28d502015-11-30 18:46:49 +01001161 struct iio_context_pdata *pdata = chn->dev->ctx->pdata;
Paul Cercueil5b577762014-06-03 15:31:42 +02001162
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +02001163 return iiod_client_write_attr(pdata->iiod_client,
Lars-Peter Clausen2767d4d2016-06-23 14:19:02 +02001164 &pdata->io_ctx, chn->dev, chn, attr, src, len, false);
Paul Cercueil07897d32014-03-06 12:46:08 +01001165}
1166
Paul Cercueildcab40c2014-03-11 10:59:14 +01001167static int network_get_trigger(const struct iio_device *dev,
1168 const struct iio_device **trigger)
1169{
1170 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueildcab40c2014-03-11 10:59:14 +01001171
Paul Cercueilcae6c2a2015-11-30 16:37:19 +01001172 return iiod_client_get_trigger(pdata->iiod_client,
Lars-Peter Clausen2767d4d2016-06-23 14:19:02 +02001173 &pdata->io_ctx, dev, trigger);
Paul Cercueildcab40c2014-03-11 10:59:14 +01001174}
1175
1176static int network_set_trigger(const struct iio_device *dev,
1177 const struct iio_device *trigger)
1178{
Paul Cercueila7b2aae2015-12-04 16:02:00 +01001179 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueil1494b862014-05-05 12:49:07 +02001180
Paul Cercueilcae6c2a2015-11-30 16:37:19 +01001181 return iiod_client_set_trigger(pdata->iiod_client,
Lars-Peter Clausen2767d4d2016-06-23 14:19:02 +02001182 &pdata->io_ctx, dev, trigger);
Paul Cercueildcab40c2014-03-11 10:59:14 +01001183}
1184
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001185static void network_shutdown(struct iio_context *ctx)
1186{
1187 struct iio_context_pdata *pdata = ctx->pdata;
Paul Cercueil44ae11c2014-03-17 09:56:29 +01001188 unsigned int i;
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001189
Paul Cercueil388dcd62015-11-27 15:15:48 +01001190 iio_mutex_lock(pdata->lock);
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +02001191 write_command(&pdata->io_ctx, "\r\nEXIT\r\n");
1192 close(pdata->io_ctx.fd);
Paul Cercueil388dcd62015-11-27 15:15:48 +01001193 iio_mutex_unlock(pdata->lock);
Paul Cercueil1494b862014-05-05 12:49:07 +02001194
Paul Cercueil439e1a22015-02-24 13:50:51 +01001195 for (i = 0; i < ctx->nb_devices; i++) {
1196 struct iio_device *dev = ctx->devices[i];
Paul Cercueilc5b00752015-02-24 14:29:53 +01001197 struct iio_device_pdata *dpdata = dev->pdata;
Paul Cercueil439e1a22015-02-24 13:50:51 +01001198
Paul Cercueilc5b00752015-02-24 14:29:53 +01001199 if (dpdata) {
Paul Cercueilaa0db142015-03-04 16:25:24 +01001200 network_close(dev);
Paul Cercueil388dcd62015-11-27 15:15:48 +01001201 iio_mutex_destroy(dpdata->lock);
Paul Cercueilc5b00752015-02-24 14:29:53 +01001202 free(dpdata);
Paul Cercueil439e1a22015-02-24 13:50:51 +01001203 }
1204 }
1205
Paul Cercueil157d43c2015-11-30 15:05:06 +01001206 iiod_client_destroy(pdata->iiod_client);
Paul Cercueil388dcd62015-11-27 15:15:48 +01001207 iio_mutex_destroy(pdata->lock);
Paul Cercueil2dd2c132015-02-24 10:41:01 +01001208 freeaddrinfo(pdata->addrinfo);
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001209 free(pdata);
1210}
1211
Paul Cercueil6691a3f2014-05-02 12:32:01 +02001212static int network_get_version(const struct iio_context *ctx,
Paul Cercueil9de9e9d2014-05-20 13:18:19 +02001213 unsigned int *major, unsigned int *minor, char git_tag[8])
Paul Cercueil6691a3f2014-05-02 12:32:01 +02001214{
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +02001215 return iiod_client_get_version(ctx->pdata->iiod_client,
Lars-Peter Clausen2767d4d2016-06-23 14:19:02 +02001216 &ctx->pdata->io_ctx, major, minor, git_tag);
Paul Cercueil6691a3f2014-05-02 12:32:01 +02001217}
1218
Paul Cercueil8a266f12014-06-10 16:06:31 +02001219static unsigned int calculate_remote_timeout(unsigned int timeout)
1220{
1221 /* XXX(pcercuei): We currently hardcode timeout / 2 for the backend used
1222 * by the remote. Is there something better to do here? */
1223 return timeout / 2;
1224}
1225
Paul Cercueilbca3dbc2014-06-11 12:00:21 +02001226static int network_set_timeout(struct iio_context *ctx, unsigned int timeout)
1227{
Paul Cercueilf996ae12015-12-03 12:29:13 +01001228 struct iio_context_pdata *pdata = ctx->pdata;
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +02001229 int ret, fd = pdata->io_ctx.fd;
Paul Cercueilf996ae12015-12-03 12:29:13 +01001230
1231 ret = set_socket_timeout(fd, timeout);
Paul Cercueilbca3dbc2014-06-11 12:00:21 +02001232 if (!ret) {
Paul Cercueil6a0eb582016-08-30 10:54:22 +02001233 unsigned int remote_timeout = calculate_remote_timeout(timeout);
1234
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +02001235 ret = iiod_client_set_timeout(pdata->iiod_client,
Paul Cercueil6a0eb582016-08-30 10:54:22 +02001236 &pdata->io_ctx, remote_timeout);
1237 if (!ret)
1238 pdata->io_ctx.timeout_ms = timeout;
Paul Cercueilbca3dbc2014-06-11 12:00:21 +02001239 }
1240 if (ret < 0) {
1241 char buf[1024];
Paul Cercueil53fc4852015-05-22 10:57:53 +02001242 iio_strerror(-ret, buf, sizeof(buf));
Paul Cercueil8a266f12014-06-10 16:06:31 +02001243 WARNING("Unable to set R/W timeout: %s\n", buf);
Paul Cercueil8a266f12014-06-10 16:06:31 +02001244 }
1245 return ret;
1246}
1247
Paul Cercueil61157f92015-11-19 17:48:22 +01001248static int network_set_kernel_buffers_count(const struct iio_device *dev,
1249 unsigned int nb_blocks)
1250{
Paul Cercueila9810a82015-11-30 16:55:07 +01001251 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueil61157f92015-11-19 17:48:22 +01001252
Paul Cercueila9810a82015-11-30 16:55:07 +01001253 return iiod_client_set_kernel_buffers_count(pdata->iiod_client,
Lars-Peter Clausen2767d4d2016-06-23 14:19:02 +02001254 &pdata->io_ctx, dev, nb_blocks);
Paul Cercueil61157f92015-11-19 17:48:22 +01001255}
1256
Paul Cercueil12d41832014-10-28 14:35:53 +01001257static struct iio_context * network_clone(const struct iio_context *ctx)
1258{
Paul Cercueilf2cc5112017-02-08 12:18:22 +01001259 const char *addr = iio_context_get_attr_value(ctx, "ip,ip-addr");
Paul Cercueil7ef45ce2015-03-16 14:36:16 +01001260
Paul Cercueilf2cc5112017-02-08 12:18:22 +01001261 return iio_create_network_context(addr);
Paul Cercueil12d41832014-10-28 14:35:53 +01001262}
1263
Lars-Peter Clausen09a59d72016-02-03 15:27:04 +01001264static const struct iio_backend_ops network_ops = {
Paul Cercueil12d41832014-10-28 14:35:53 +01001265 .clone = network_clone,
Paul Cercueilba059762014-03-14 11:02:02 +01001266 .open = network_open,
1267 .close = network_close,
Paul Cercueil9945bc82014-03-05 14:07:29 +01001268 .read = network_read,
Paul Cercueil2725f702014-05-02 11:02:16 +02001269 .write = network_write,
Paul Cercueil90189672015-03-16 11:41:53 +01001270#ifdef WITH_NETWORK_GET_BUFFER
Paul Cercueil09a43482015-03-04 15:50:27 +01001271 .get_buffer = network_get_buffer,
1272#endif
Paul Cercueil3e94a5b2014-03-04 13:00:41 +01001273 .read_device_attr = network_read_dev_attr,
1274 .write_device_attr = network_write_dev_attr,
Paul Cercueil99cf3d42014-03-06 12:35:26 +01001275 .read_channel_attr = network_read_chn_attr,
Paul Cercueil07897d32014-03-06 12:46:08 +01001276 .write_channel_attr = network_write_chn_attr,
Paul Cercueildcab40c2014-03-11 10:59:14 +01001277 .get_trigger = network_get_trigger,
1278 .set_trigger = network_set_trigger,
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001279 .shutdown = network_shutdown,
Paul Cercueil6691a3f2014-05-02 12:32:01 +02001280 .get_version = network_get_version,
Paul Cercueil8a266f12014-06-10 16:06:31 +02001281 .set_timeout = network_set_timeout,
Paul Cercueil61157f92015-11-19 17:48:22 +01001282 .set_kernel_buffers_count = network_set_kernel_buffers_count,
Lars-Peter Clausen55e52f22016-07-04 13:29:38 +02001283
1284 .cancel = network_cancel,
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001285};
1286
Paul Cercueil157d43c2015-11-30 15:05:06 +01001287static ssize_t network_write_data(struct iio_context_pdata *pdata,
Lars-Peter Clausen2767d4d2016-06-23 14:19:02 +02001288 void *io_data, const char *src, size_t len)
Paul Cercueil157d43c2015-11-30 15:05:06 +01001289{
Lars-Peter Clausen2767d4d2016-06-23 14:19:02 +02001290 struct iio_network_io_context *io_ctx = io_data;
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +02001291
1292 return network_send(io_ctx, src, len, 0);
Paul Cercueil157d43c2015-11-30 15:05:06 +01001293}
1294
1295static ssize_t network_read_data(struct iio_context_pdata *pdata,
Lars-Peter Clausen2767d4d2016-06-23 14:19:02 +02001296 void *io_data, char *dst, size_t len)
Paul Cercueil157d43c2015-11-30 15:05:06 +01001297{
Lars-Peter Clausen2767d4d2016-06-23 14:19:02 +02001298 struct iio_network_io_context *io_ctx = io_data;
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +02001299
1300 return network_recv(io_ctx, dst, len, 0);
Paul Cercueil157d43c2015-11-30 15:05:06 +01001301}
1302
1303static ssize_t network_read_line(struct iio_context_pdata *pdata,
Lars-Peter Clausen2767d4d2016-06-23 14:19:02 +02001304 void *io_data, char *dst, size_t len)
Paul Cercueil157d43c2015-11-30 15:05:06 +01001305{
Paul Cercueil1cb01fe2016-11-16 16:34:00 +01001306 bool found = false;
Paul Cercueil157d43c2015-11-30 15:05:06 +01001307 size_t i;
Paul Cercueilce02dc92016-01-07 11:46:38 +01001308#ifdef __linux__
Lars-Peter Clausen2767d4d2016-06-23 14:19:02 +02001309 struct iio_network_io_context *io_ctx = io_data;
Paul Cercueilce02dc92016-01-07 11:46:38 +01001310 ssize_t ret;
Paul Cercueil1cb01fe2016-11-16 16:34:00 +01001311 size_t bytes_read = 0;
Paul Cercueilce02dc92016-01-07 11:46:38 +01001312
Paul Cercueil1cb01fe2016-11-16 16:34:00 +01001313 do {
1314 size_t to_trunc;
Paul Cercueilce02dc92016-01-07 11:46:38 +01001315
Paul Cercueil1cb01fe2016-11-16 16:34:00 +01001316 ret = network_recv(io_ctx, dst, len, MSG_PEEK);
1317 if (ret < 0)
1318 return ret;
Paul Cercueilce02dc92016-01-07 11:46:38 +01001319
Paul Cercueil1cb01fe2016-11-16 16:34:00 +01001320 /* Lookup for the trailing \n */
1321 for (i = 0; i < (size_t) ret && dst[i] != '\n'; i++);
1322 found = i < (size_t) ret;
1323
1324 len -= ret;
1325 dst += ret;
Paul Cercueil1cb01fe2016-11-16 16:34:00 +01001326
1327 if (found)
1328 to_trunc = i + 1;
1329 else
1330 to_trunc = (size_t) ret;
1331
1332 /* Advance the read offset to the byte following the \n if
1333 * found, or after the last charater read otherwise */
1334 ret = network_recv(io_ctx, NULL, to_trunc, MSG_TRUNC);
1335 if (ret < 0)
1336 return ret;
1337
1338 bytes_read += to_trunc;
1339 } while (!found && len);
1340
1341 if (!found)
Paul Cercueilce02dc92016-01-07 11:46:38 +01001342 return -EIO;
Paul Cercueil1cb01fe2016-11-16 16:34:00 +01001343 else
1344 return bytes_read;
Paul Cercueilce02dc92016-01-07 11:46:38 +01001345#else
Paul Cercueil157d43c2015-11-30 15:05:06 +01001346 for (i = 0; i < len - 1; i++) {
Lars-Peter Clausen2767d4d2016-06-23 14:19:02 +02001347 ssize_t ret = network_read_data(pdata, io_data, dst + i, 1);
Paul Cercueil157d43c2015-11-30 15:05:06 +01001348
1349 if (ret < 0)
1350 return ret;
1351
1352 if (dst[i] != '\n')
1353 found = true;
1354 else if (found)
1355 break;
1356 }
1357
Paul Cercueil209def82016-04-08 14:47:59 +02001358 if (!found || i == len - 1)
1359 return -EIO;
1360
1361 return (ssize_t) i + 1;
Paul Cercueilce02dc92016-01-07 11:46:38 +01001362#endif
Paul Cercueil157d43c2015-11-30 15:05:06 +01001363}
1364
Lars-Peter Clausen09a59d72016-02-03 15:27:04 +01001365static const struct iiod_client_ops network_iiod_client_ops = {
Paul Cercueil157d43c2015-11-30 15:05:06 +01001366 .write = network_write_data,
1367 .read = network_read_data,
1368 .read_line = network_read_line,
1369};
1370
Paul Cercueil63e52182014-12-11 12:52:48 +01001371struct iio_context * network_create_context(const char *host)
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001372{
Paul Cercueil2e38bbe2014-03-19 15:27:15 +01001373 struct addrinfo hints, *res;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001374 struct iio_context *ctx;
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001375 struct iio_context_pdata *pdata;
Paul Cercueil0e3c04d2015-05-13 17:37:11 +02001376 size_t i, len;
Paul Cercueil4970ac32015-02-24 10:59:00 +01001377 int fd, ret;
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001378 char *description;
Paul Cercueil1fef1a52014-04-07 16:31:15 +02001379#ifdef _WIN32
1380 WSADATA wsaData;
1381
1382 ret = WSAStartup(MAKEWORD(2, 0), &wsaData);
1383 if (ret < 0) {
1384 ERROR("WSAStartup failed with error %i\n", ret);
Paul Cercueilcc575532015-03-16 17:15:24 +01001385 errno = -ret;
Paul Cercueil1fef1a52014-04-07 16:31:15 +02001386 return NULL;
1387 }
1388#endif
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001389
Paul Cercueil2e38bbe2014-03-19 15:27:15 +01001390 memset(&hints, 0, sizeof(hints));
1391 hints.ai_family = AF_UNSPEC;
1392 hints.ai_socktype = SOCK_STREAM;
Paul Cercueilab114932014-05-19 13:03:17 +02001393
1394#ifdef HAVE_AVAHI
1395 if (!host) {
1396 char addr_str[AVAHI_ADDRESS_STR_MAX];
1397 char port_str[6];
1398 AvahiAddress address;
Paul Cercueil0f729d32014-06-05 17:38:54 +02001399 uint16_t port = IIOD_PORT;
Paul Cercueilab114932014-05-19 13:03:17 +02001400
1401 memset(&address, 0, sizeof(address));
1402
1403 ret = discover_host(&address, &port);
1404 if (ret < 0) {
Lars-Peter Clausen3417a1a2016-04-18 14:29:02 +02001405 char buf[1024];
1406 iio_strerror(-ret, buf, sizeof(buf));
1407 DEBUG("Unable to find host: %s\n", buf);
Paul Cercueilcc575532015-03-16 17:15:24 +01001408 errno = -ret;
Paul Cercueilab114932014-05-19 13:03:17 +02001409 return NULL;
1410 }
1411
1412 avahi_address_snprint(addr_str, sizeof(addr_str), &address);
Paul Cercueil9c9a5562017-01-24 10:48:31 +01001413 iio_snprintf(port_str, sizeof(port_str), "%hu", port);
Paul Cercueilab114932014-05-19 13:03:17 +02001414 ret = getaddrinfo(addr_str, port_str, &hints, &res);
1415 } else
1416#endif
1417 {
1418 ret = getaddrinfo(host, IIOD_PORT_STR, &hints, &res);
1419 }
1420
Paul Cercueil2e38bbe2014-03-19 15:27:15 +01001421 if (ret) {
Paul Cercueilab114932014-05-19 13:03:17 +02001422 ERROR("Unable to find host: %s\n", gai_strerror(ret));
Paul Cercueild9eb4cb2015-03-23 14:30:20 +01001423#ifndef _WIN32
Paul Cercueilcc575532015-03-16 17:15:24 +01001424 if (ret != EAI_SYSTEM)
Paul Cercueil7cc85112016-11-15 14:59:40 +01001425 errno = -ret;
Paul Cercueild9eb4cb2015-03-23 14:30:20 +01001426#endif
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001427 return NULL;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001428 }
1429
Paul Cercueil6a0eb582016-08-30 10:54:22 +02001430 fd = create_socket(res, DEFAULT_TIMEOUT_MS);
Paul Cercueilcc575532015-03-16 17:15:24 +01001431 if (fd < 0) {
Paul Cercueilc3fe6042016-11-16 17:38:56 +01001432 errno = -fd;
Paul Cercueil2dd2c132015-02-24 10:41:01 +01001433 goto err_free_addrinfo;
Paul Cercueilcc575532015-03-16 17:15:24 +01001434 }
Paul Cercueilbca3dbc2014-06-11 12:00:21 +02001435
Lars-Peter Clausend1be8382016-02-24 11:13:45 +01001436 pdata = zalloc(sizeof(*pdata));
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001437 if (!pdata) {
Paul Cercueilcc575532015-03-16 17:15:24 +01001438 errno = ENOMEM;
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001439 goto err_close_socket;
1440 }
1441
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +02001442 pdata->io_ctx.fd = fd;
Paul Cercueil2dd2c132015-02-24 10:41:01 +01001443 pdata->addrinfo = res;
Paul Cercueil6a0eb582016-08-30 10:54:22 +02001444 pdata->io_ctx.timeout_ms = DEFAULT_TIMEOUT_MS;
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001445
Paul Cercueil157d43c2015-11-30 15:05:06 +01001446 pdata->lock = iio_mutex_create();
1447 if (!pdata->lock) {
1448 errno = ENOMEM;
1449 goto err_free_pdata;
1450 }
1451
1452 pdata->iiod_client = iiod_client_new(pdata, pdata->lock,
1453 &network_iiod_client_ops);
1454 if (!pdata->iiod_client)
1455 goto err_destroy_mutex;
1456
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001457 DEBUG("Creating context...\n");
Lars-Peter Clausen2767d4d2016-06-23 14:19:02 +02001458 ctx = iiod_client_create_context(pdata->iiod_client, &pdata->io_ctx);
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001459 if (!ctx)
Paul Cercueil157d43c2015-11-30 15:05:06 +01001460 goto err_destroy_iiod_client;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001461
Paul Cercueil2057fd32014-10-28 14:44:19 +01001462 /* Override the name and low-level functions of the XML context
1463 * with those corresponding to the network context */
1464 ctx->name = "network";
1465 ctx->ops = &network_ops;
1466 ctx->pdata = pdata;
1467
Paul Cercueil06c479d2015-01-08 16:14:57 +01001468#ifdef HAVE_IPV6
Paul Cercueiled15e492015-01-12 15:52:28 +01001469 len = INET6_ADDRSTRLEN + IF_NAMESIZE + 2;
Paul Cercueil06c479d2015-01-08 16:14:57 +01001470#else
1471 len = INET_ADDRSTRLEN + 1;
1472#endif
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001473
1474 description = malloc(len);
1475 if (!description) {
Paul Cercueilcc575532015-03-16 17:15:24 +01001476 ret = -ENOMEM;
Paul Cercueil06c479d2015-01-08 16:14:57 +01001477 goto err_network_shutdown;
1478 }
1479
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001480 description[0] = '\0';
Paul Cercueil06c479d2015-01-08 16:14:57 +01001481
1482#ifdef HAVE_IPV6
1483 if (res->ai_family == AF_INET6) {
1484 struct sockaddr_in6 *in = (struct sockaddr_in6 *) res->ai_addr;
Paul Cercueiled15e492015-01-12 15:52:28 +01001485 char *ptr;
Paul Cercueil06c479d2015-01-08 16:14:57 +01001486 inet_ntop(AF_INET6, &in->sin6_addr,
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001487 description, INET6_ADDRSTRLEN);
Paul Cercueiled15e492015-01-12 15:52:28 +01001488
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001489 ptr = if_indextoname(in->sin6_scope_id, description +
1490 strlen(description) + 1);
Paul Cercueiled15e492015-01-12 15:52:28 +01001491 if (!ptr) {
Paul Cercueilcc575532015-03-16 17:15:24 +01001492 ret = -errno;
Paul Cercueiled15e492015-01-12 15:52:28 +01001493 ERROR("Unable to lookup interface of IPv6 address\n");
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001494 goto err_free_description;
Paul Cercueiled15e492015-01-12 15:52:28 +01001495 }
1496
1497 *(ptr - 1) = '%';
Paul Cercueil06c479d2015-01-08 16:14:57 +01001498 }
1499#endif
1500 if (res->ai_family == AF_INET) {
1501 struct sockaddr_in *in = (struct sockaddr_in *) res->ai_addr;
Paul Cercueile7a31692015-07-15 10:52:47 +02001502#if (!_WIN32 || _WIN32_WINNT >= 0x600)
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001503 inet_ntop(AF_INET, &in->sin_addr, description, INET_ADDRSTRLEN);
Paul Cercueile7a31692015-07-15 10:52:47 +02001504#else
1505 char *tmp = inet_ntoa(in->sin_addr);
1506 strncpy(description, tmp, len);
1507#endif
Paul Cercueil06c479d2015-01-08 16:14:57 +01001508 }
1509
Paul Cercueil554ef262017-02-08 12:16:57 +01001510 ret = iio_context_add_attr(ctx, "ip,ip-addr", description);
1511 if (ret < 0)
1512 goto err_free_description;
1513
Paul Cercueil44ae11c2014-03-17 09:56:29 +01001514 for (i = 0; i < ctx->nb_devices; i++) {
1515 struct iio_device *dev = ctx->devices[i];
Paul Cercueilff778232014-03-24 14:23:08 +01001516
Lars-Peter Clausend1be8382016-02-24 11:13:45 +01001517 dev->pdata = zalloc(sizeof(*dev->pdata));
Paul Cercueil439e1a22015-02-24 13:50:51 +01001518 if (!dev->pdata) {
Paul Cercueilcc575532015-03-16 17:15:24 +01001519 ret = -ENOMEM;
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001520 goto err_free_description;
Paul Cercueil439e1a22015-02-24 13:50:51 +01001521 }
1522
Lars-Peter Clausen7e8fa2d2016-06-22 10:57:02 +02001523 dev->pdata->io_ctx.fd = -1;
Paul Cercueil6a0eb582016-08-30 10:54:22 +02001524 dev->pdata->io_ctx.timeout_ms = DEFAULT_TIMEOUT_MS;
Paul Cercueil90189672015-03-16 11:41:53 +01001525#ifdef WITH_NETWORK_GET_BUFFER
Paul Cercueil09a43482015-03-04 15:50:27 +01001526 dev->pdata->memfd = -1;
Paul Cercueile6e5a092015-03-04 16:33:26 +01001527#endif
Paul Cercueilc5b00752015-02-24 14:29:53 +01001528
Paul Cercueil388dcd62015-11-27 15:15:48 +01001529 dev->pdata->lock = iio_mutex_create();
1530 if (!dev->pdata->lock) {
1531 ret = -ENOMEM;
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001532 goto err_free_description;
Paul Cercueil388dcd62015-11-27 15:15:48 +01001533 }
Paul Cercueil44ae11c2014-03-17 09:56:29 +01001534 }
1535
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001536 if (ctx->description) {
Paul Cercueil0e3c04d2015-05-13 17:37:11 +02001537 size_t desc_len = strlen(description);
1538 size_t new_size = desc_len + strlen(ctx->description) + 2;
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001539 char *ptr, *new_description = realloc(description, new_size);
Paul Cercueilcc575532015-03-16 17:15:24 +01001540 if (!new_description) {
1541 ret = -ENOMEM;
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001542 goto err_free_description;
Paul Cercueilcc575532015-03-16 17:15:24 +01001543 }
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001544
1545 ptr = strrchr(new_description, '\0');
Paul Cercueil9c9a5562017-01-24 10:48:31 +01001546 iio_snprintf(ptr, new_size - desc_len, " %s", ctx->description);
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001547 free(ctx->description);
1548
1549 ctx->description = new_description;
1550 } else {
1551 ctx->description = description;
1552 }
1553
Lars-Peter Clausen2767d4d2016-06-23 14:19:02 +02001554 iiod_client_set_timeout(pdata->iiod_client, &pdata->io_ctx,
Paul Cercueilf996ae12015-12-03 12:29:13 +01001555 calculate_remote_timeout(DEFAULT_TIMEOUT_MS));
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001556 return ctx;
1557
Paul Cercueilb03c9ef2015-03-16 14:07:58 +01001558err_free_description:
1559 free(description);
Paul Cercueil44ae11c2014-03-17 09:56:29 +01001560err_network_shutdown:
Paul Cercueil44ae11c2014-03-17 09:56:29 +01001561 iio_context_destroy(ctx);
Paul Cercueilcc575532015-03-16 17:15:24 +01001562 errno = -ret;
Paul Cercueil2057fd32014-10-28 14:44:19 +01001563 return NULL;
Paul Cercueil157d43c2015-11-30 15:05:06 +01001564
1565err_destroy_iiod_client:
1566 iiod_client_destroy(pdata->iiod_client);
1567err_destroy_mutex:
1568 iio_mutex_destroy(pdata->lock);
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001569err_free_pdata:
1570 free(pdata);
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001571err_close_socket:
1572 close(fd);
Paul Cercueil2dd2c132015-02-24 10:41:01 +01001573err_free_addrinfo:
1574 freeaddrinfo(res);
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001575 return NULL;
1576}