blob: bef7cb38c85fa58951e7f7a151d88b3f122ab475 [file] [log] [blame]
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001/*
2 * libiio - Library for interfacing industrial I/O (IIO) devices
3 *
4 * Copyright (C) 2014 Analog Devices, Inc.
5 * 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 Cercueil09a43482015-03-04 15:50:27 +010019#ifdef __linux__
20#define _GNU_SOURCE 1 /* Required for splice() */
21#endif
22
Paul Cercueilc7bad0f2014-03-03 17:06:48 +010023#include "iio-private.h"
24
Paul Cercueil9aabe892014-09-02 12:33:46 +020025#ifndef HAVE_PTHREAD
26#define HAVE_PTHREAD 1
27#endif
28
Paul Cercueilc7bad0f2014-03-03 17:06:48 +010029#include <errno.h>
Paul Cercueila7d445b2014-11-11 16:00:15 +010030#include <fcntl.h>
Paul Cercueile60c3ed2014-03-04 11:23:56 +010031#include <stdbool.h>
Paul Cercueilc7bad0f2014-03-03 17:06:48 +010032#include <string.h>
33#include <sys/types.h>
Paul Cercueilab114932014-05-19 13:03:17 +020034#include <time.h>
Paul Cercueil1fef1a52014-04-07 16:31:15 +020035
36#ifdef _WIN32
Paul Cercueil06c479d2015-01-08 16:14:57 +010037/* Override the default version of Windows supported by MinGW.
38 * This is required to use the function inet_ntop. */
39#undef _WIN32_WINNT
40#define _WIN32_WINNT 0x0600
41
Paul Cercueil1fef1a52014-04-07 16:31:15 +020042#include <winsock2.h>
43#include <ws2tcpip.h>
44#define close(s) closesocket(s)
45
46/* winsock2.h defines ERROR, we don't want that */
47#undef ERROR
48
49#else /* _WIN32 */
Paul Cercueil06c479d2015-01-08 16:14:57 +010050#include <arpa/inet.h>
Paul Cercueil1fef1a52014-04-07 16:31:15 +020051#include <netdb.h>
Paul Cercueil0c3ce452015-02-05 16:37:42 +010052#include <netinet/in.h>
Paul Cercueil0b584e12015-01-28 11:47:03 +010053#include <netinet/tcp.h>
Paul Cercueiled15e492015-01-12 15:52:28 +010054#include <net/if.h>
Paul Cercueil09a43482015-03-04 15:50:27 +010055#include <sys/mman.h>
Paul Cercueil83628b32014-11-24 11:26:21 +010056#include <sys/select.h>
Paul Cercueilc7bad0f2014-03-03 17:06:48 +010057#include <sys/socket.h>
58#include <unistd.h>
Paul Cercueil1fef1a52014-04-07 16:31:15 +020059#endif /* _WIN32 */
60
Paul Cercueil257e6ca2014-12-09 17:32:47 +010061#if HAVE_PTHREAD
62#include <pthread.h>
63#endif
64
Paul Cercueilab114932014-05-19 13:03:17 +020065#ifdef HAVE_AVAHI
66#include <avahi-client/client.h>
67#include <avahi-common/error.h>
68#include <avahi-client/lookup.h>
69#include <avahi-common/simple-watch.h>
70#endif
71
Paul Cercueil1fef1a52014-04-07 16:31:15 +020072#include "debug.h"
Paul Cercueilc7bad0f2014-03-03 17:06:48 +010073
Paul Cercueil8a266f12014-06-10 16:06:31 +020074#define DEFAULT_TIMEOUT_MS 5000
75
Paul Cercueil2e38bbe2014-03-19 15:27:15 +010076#define _STRINGIFY(x) #x
77#define STRINGIFY(x) _STRINGIFY(x)
78
Paul Cercueilc7bad0f2014-03-03 17:06:48 +010079#define IIOD_PORT 30431
Paul Cercueil2e38bbe2014-03-19 15:27:15 +010080#define IIOD_PORT_STR STRINGIFY(IIOD_PORT)
Paul Cercueilc7bad0f2014-03-03 17:06:48 +010081
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +010082struct iio_context_pdata {
83 int fd;
Paul Cercueil2dd2c132015-02-24 10:41:01 +010084 struct addrinfo *addrinfo;
Paul Cercueil05e26262014-05-09 11:32:43 +020085#if HAVE_PTHREAD
Paul Cercueil1494b862014-05-05 12:49:07 +020086 pthread_mutex_t lock;
Paul Cercueil05e26262014-05-09 11:32:43 +020087#endif
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +010088};
89
Paul Cercueil439e1a22015-02-24 13:50:51 +010090struct iio_device_pdata {
91 int fd;
Paul Cercueil09a43482015-03-04 15:50:27 +010092 int memfd;
93 size_t mmap_len;
94 bool wait_for_err_code, is_cyclic, is_tx;
Paul Cercueilc5b00752015-02-24 14:29:53 +010095#if HAVE_PTHREAD
96 pthread_mutex_t lock;
97#endif
Paul Cercueil439e1a22015-02-24 13:50:51 +010098};
99
Paul Cercueilab114932014-05-19 13:03:17 +0200100#ifdef HAVE_AVAHI
101struct avahi_discovery_data {
102 AvahiSimplePoll *poll;
103 AvahiAddress *address;
104 uint16_t *port;
105 bool found, resolved;
106};
107
108static void __avahi_resolver_cb(AvahiServiceResolver *resolver,
109 __notused AvahiIfIndex iface, __notused AvahiProtocol proto,
110 __notused AvahiResolverEvent event, __notused const char *name,
111 __notused const char *type, __notused const char *domain,
112 __notused const char *host_name, const AvahiAddress *address,
113 uint16_t port, __notused AvahiStringList *txt,
114 __notused AvahiLookupResultFlags flags, void *d)
115{
116 struct avahi_discovery_data *ddata = (struct avahi_discovery_data *) d;
117
118 memcpy(ddata->address, address, sizeof(*address));
119 *ddata->port = port;
120 ddata->resolved = true;
121 avahi_service_resolver_free(resolver);
122}
123
124static void __avahi_browser_cb(AvahiServiceBrowser *browser,
125 AvahiIfIndex iface, AvahiProtocol proto,
126 AvahiBrowserEvent event, const char *name,
127 const char *type, const char *domain,
128 __notused AvahiLookupResultFlags flags, void *d)
129{
130 struct avahi_discovery_data *ddata = (struct avahi_discovery_data *) d;
131 struct AvahiClient *client = avahi_service_browser_get_client(browser);
132
133 switch (event) {
134 default:
135 case AVAHI_BROWSER_NEW:
136 ddata->found = !!avahi_service_resolver_new(client, iface,
137 proto, name, type, domain,
138 AVAHI_PROTO_UNSPEC, 0,
139 __avahi_resolver_cb, d);
140 break;
141 case AVAHI_BROWSER_ALL_FOR_NOW:
142 if (ddata->found) {
143 while (!ddata->resolved) {
144 struct timespec ts;
145 ts.tv_sec = 0;
146 ts.tv_nsec = 4000000;
147 nanosleep(&ts, NULL);
148 }
149 }
150 case AVAHI_BROWSER_FAILURE: /* fall-through */
151 avahi_simple_poll_quit(ddata->poll);
152 case AVAHI_BROWSER_CACHE_EXHAUSTED: /* fall-through */
153 break;
154 }
155}
156
157static int discover_host(AvahiAddress *addr, uint16_t *port)
158{
159 struct avahi_discovery_data ddata;
160 int ret = 0;
161 AvahiClient *client;
162 AvahiServiceBrowser *browser;
163 AvahiSimplePoll *poll = avahi_simple_poll_new();
164 if (!poll)
165 return -ENOMEM;
166
167 client = avahi_client_new(avahi_simple_poll_get(poll),
168 0, NULL, NULL, &ret);
169 if (!client) {
170 ERROR("Unable to start ZeroConf client :%s\n",
171 avahi_strerror(ret));
172 goto err_free_poll;
173 }
174
175 memset(&ddata, 0, sizeof(ddata));
176 ddata.poll = poll;
177 ddata.address = addr;
178 ddata.port = port;
179
180 browser = avahi_service_browser_new(client,
181 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
182 "_iio._tcp", NULL, 0, __avahi_browser_cb, &ddata);
183 if (!browser) {
184 ret = avahi_client_errno(client);
185 ERROR("Unable to create ZeroConf browser: %s\n",
186 avahi_strerror(ret));
187 goto err_free_client;
188 }
189
190 DEBUG("Trying to discover host\n");
191 avahi_simple_poll_loop(poll);
192
193 if (!ddata.found)
194 ret = ENXIO;
195
196 avahi_service_browser_free(browser);
197err_free_client:
198 avahi_client_free(client);
199err_free_poll:
200 avahi_simple_poll_free(poll);
201 return -ret; /* we want a negative error code */
202}
203#endif /* HAVE_AVAHI */
204
Paul Cercueil05e26262014-05-09 11:32:43 +0200205static void network_lock(struct iio_context_pdata *pdata)
206{
207#if HAVE_PTHREAD
208 pthread_mutex_lock(&pdata->lock);
209#endif
210}
211
212static void network_unlock(struct iio_context_pdata *pdata)
213{
214#if HAVE_PTHREAD
215 pthread_mutex_unlock(&pdata->lock);
216#endif
217}
218
Paul Cercueilc5b00752015-02-24 14:29:53 +0100219static void network_lock_dev(struct iio_device_pdata *pdata)
220{
221#if HAVE_PTHREAD
222 pthread_mutex_lock(&pdata->lock);
223#endif
224}
225
226static void network_unlock_dev(struct iio_device_pdata *pdata)
227{
228#if HAVE_PTHREAD
229 pthread_mutex_unlock(&pdata->lock);
230#endif
231}
232
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100233static ssize_t write_all(const void *src, size_t len, int fd)
234{
Paul Cercueil6e7f79e2014-04-04 12:27:24 +0200235 uintptr_t ptr = (uintptr_t) src;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100236 while (len) {
Paul Cercueil6e7f79e2014-04-04 12:27:24 +0200237 ssize_t ret = send(fd, (const void *) ptr, len, 0);
Lars-Peter Clausencb7b8bf2014-12-05 16:08:52 +0100238 if (ret < 0) {
239 if (errno == EINTR) {
240 continue;
241 }
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100242 return -errno;
Lars-Peter Clausencb7b8bf2014-12-05 16:08:52 +0100243 }
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100244 ptr += ret;
245 len -= ret;
246 }
Paul Cercueil6e7f79e2014-04-04 12:27:24 +0200247 return ptr - (uintptr_t) src;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100248}
249
250static ssize_t read_all(void *dst, size_t len, int fd)
251{
Paul Cercueil6e7f79e2014-04-04 12:27:24 +0200252 uintptr_t ptr = (uintptr_t) dst;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100253 while (len) {
Paul Cercueil6e7f79e2014-04-04 12:27:24 +0200254 ssize_t ret = recv(fd, (void *) ptr, len, 0);
Lars-Peter Clausencb7b8bf2014-12-05 16:08:52 +0100255 if (ret < 0) {
256 if (errno == EINTR)
257 continue;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100258 return -errno;
Lars-Peter Clausencb7b8bf2014-12-05 16:08:52 +0100259 }
Paul Cercueil43eb7e82014-11-13 12:46:59 +0100260 if (ret == 0)
261 return -EPIPE;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100262 ptr += ret;
263 len -= ret;
264 }
Paul Cercueil6e7f79e2014-04-04 12:27:24 +0200265 return ptr - (uintptr_t) dst;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +0100266}
267
Paul Cercueile60c3ed2014-03-04 11:23:56 +0100268static int read_integer(int fd, long *val)
269{
270 unsigned int i;
271 char buf[1024], *ptr;
272 ssize_t ret;
273 bool found = false;
274
275 for (i = 0; i < sizeof(buf) - 1; i++) {
276 ret = read_all(buf + i, 1, fd);
277 if (ret < 0)
278 return (int) ret;
279
Paul Cercueil6691a3f2014-05-02 12:32:01 +0200280 /* Skip the eventual first few carriage returns.
281 * Also stop when a dot is found (for parsing floats) */
282 if (buf[i] != '\n' && buf[i] != '.')
Paul Cercueile60c3ed2014-03-04 11:23:56 +0100283 found = true;
284 else if (found)
285 break;
286 }
287
288 buf[i] = '\0';
289 ret = (ssize_t) strtol(buf, &ptr, 10);
290 if (ptr == buf)
291 return -EINVAL;
292 *val = (long) ret;
293 return 0;
294}
295
Paul Cercueilbb76bf92014-03-11 10:20:18 +0100296static ssize_t write_command(const char *cmd, int fd)
297{
298 ssize_t ret;
299
300 DEBUG("Writing command: %s\n", cmd);
301 ret = write_all(cmd, strlen(cmd), fd);
302 if (ret < 0) {
303 char buf[1024];
304 strerror_r(-ret, buf, sizeof(buf));
305 ERROR("Unable to send command: %s\n", buf);
306 }
307 return ret;
308}
309
310static long exec_command(const char *cmd, int fd)
311{
312 long resp;
313 ssize_t ret = write_command(cmd, fd);
314 if (ret < 0)
315 return (long) ret;
316
317 DEBUG("Reading response\n");
318 ret = read_integer(fd, &resp);
319 if (ret < 0) {
320 char buf[1024];
321 strerror_r(-ret, buf, sizeof(buf));
322 ERROR("Unable to read response: %s\n", buf);
323 return (long) ret;
324 }
325
Paul Cercueilc7fe2ea2014-03-28 11:38:45 +0100326#if LOG_LEVEL >= DEBUG_L
Paul Cercueilbb76bf92014-03-11 10:20:18 +0100327 if (resp < 0) {
328 char buf[1024];
329 strerror_r(-resp, buf, sizeof(buf));
Paul Cercueilc7fe2ea2014-03-28 11:38:45 +0100330 DEBUG("Server returned an error: %s\n", buf);
Paul Cercueilbb76bf92014-03-11 10:20:18 +0100331 }
Paul Cercueilc7fe2ea2014-03-28 11:38:45 +0100332#endif
333
Paul Cercueilbb76bf92014-03-11 10:20:18 +0100334 return resp;
335}
336
Paul Cercueil4970ac32015-02-24 10:59:00 +0100337#ifndef _WIN32
338static int set_blocking_mode(int fd, bool blocking)
339{
340 int ret = fcntl(fd, F_GETFL, 0);
341 if (ret < 0)
342 return -errno;
343
344 if (blocking)
345 ret &= ~O_NONBLOCK;
346 else
347 ret |= O_NONBLOCK;
348
349 ret = fcntl(fd, F_SETFL, ret);
350 return ret < 0 ? -errno : 0;
351}
352
353/* The purpose of this function is to provide a version of connect()
354 * that does not ignore timeouts... */
355static int do_connect(int fd, const struct sockaddr *addr,
356 socklen_t addrlen, struct timeval *timeout)
357{
358 int ret, error;
359 socklen_t len;
360 fd_set set;
361
362 FD_ZERO(&set);
363 FD_SET(fd, &set);
364
365 ret = set_blocking_mode(fd, false);
366 if (ret < 0)
367 return ret;
368
369 ret = connect(fd, addr, addrlen);
370 if (ret < 0 && errno != EINPROGRESS) {
371 ret = -errno;
372 goto end;
373 }
374
375 ret = select(fd + 1, &set, &set, NULL, timeout);
376 if (ret < 0) {
377 ret = -errno;
378 goto end;
379 }
380 if (ret == 0) {
381 ret = -ETIMEDOUT;
382 goto end;
383 }
384
385 /* Verify that we don't have an error */
386 len = sizeof(error);
387 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len);
388 if(ret < 0) {
389 ret = -errno;
390 goto end;
391 }
392 if (error) {
393 ret = -error;
394 goto end;
395 }
396
397end:
398 /* Restore blocking mode */
399 set_blocking_mode(fd, true);
400 return ret;
401}
402
403static int set_socket_timeout(int fd, unsigned int timeout)
404{
405 struct timeval tv;
406
407 tv.tv_sec = timeout / 1000;
408 tv.tv_usec = (timeout % 1000) * 1000;
409 if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0 ||
410 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,
411 &tv, sizeof(tv)) < 0)
412 return -errno;
413 else
414 return 0;
415}
416#else
417static int set_socket_timeout(int fd, unsigned int timeout)
418{
419 if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO,
420 (const char *) &timeout, sizeof(timeout)) < 0 ||
421 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,
422 (const char *) &timeout, sizeof(timeout)) < 0)
423 return -errno;
424 else
425 return 0;
426}
427#endif /* !_WIN32 */
428
429static int create_socket(const struct addrinfo *addrinfo)
430{
431 struct timeval timeout;
432 int ret, fd, yes = 1;
433
434 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype, 0);
435 if (fd < 0) {
436 ERROR("Unable to open socket\n");
437 return fd;
438 }
439
440 timeout.tv_sec = DEFAULT_TIMEOUT_MS / 1000;
441 timeout.tv_usec = (DEFAULT_TIMEOUT_MS % 1000) * 1000;
442
443#ifndef _WIN32
444 ret = do_connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen, &timeout);
445#else
446 ret = connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen);
447#endif
448 if (ret < 0) {
449 ERROR("Unable to connect\n");
450 close(fd);
451 return ret;
452 }
453
454 set_socket_timeout(fd, DEFAULT_TIMEOUT_MS);
455 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
456 (const char *) &yes, sizeof(yes));
457 return fd;
458}
459
Paul Cercueil09a43482015-03-04 15:50:27 +0100460static bool is_tx(const struct iio_device *dev)
461{
462 unsigned int i;
463
464 for (i = 0; i < dev->nb_channels; i++) {
465 struct iio_channel *ch = dev->channels[i];
466 if (iio_channel_is_output(ch) && iio_channel_is_enabled(ch))
467 return true;
468 }
469
470 return false;
471}
472
Paul Cercueil71694c72014-05-22 14:02:13 +0200473static int network_open(const struct iio_device *dev, size_t samples_count,
474 uint32_t *mask, size_t nb, bool cyclic)
Paul Cercueilba059762014-03-14 11:02:02 +0100475{
Paul Cercueil439e1a22015-02-24 13:50:51 +0100476 struct iio_context_pdata *pdata = dev->ctx->pdata;
Paul Cercueilba059762014-03-14 11:02:02 +0100477 char buf[1024], *ptr;
478 unsigned int i;
Paul Cercueil439e1a22015-02-24 13:50:51 +0100479 int ret, fd;
Paul Cercueilba059762014-03-14 11:02:02 +0100480
Paul Cercueilff778232014-03-24 14:23:08 +0100481 if (nb != dev->words)
Paul Cercueilba059762014-03-14 11:02:02 +0100482 return -EINVAL;
Paul Cercueil439e1a22015-02-24 13:50:51 +0100483 if (dev->pdata->fd >= 0)
484 return -EBUSY;
485
486 fd = create_socket(pdata->addrinfo);
487 if (fd < 0)
488 return fd;
Paul Cercueilba059762014-03-14 11:02:02 +0100489
Paul Cercueil3c407f82014-04-02 14:11:59 +0200490 snprintf(buf, sizeof(buf), "OPEN %s %lu ",
491 dev->id, (unsigned long) samples_count);
Paul Cercueilba059762014-03-14 11:02:02 +0100492 ptr = buf + strlen(buf);
493
494 for (i = nb; i > 0; i--) {
Paul Cercueil8c29e412014-04-07 09:46:45 +0200495 snprintf(ptr, (ptr - buf) + i * 8, "%08x", mask[i - 1]);
Paul Cercueilba059762014-03-14 11:02:02 +0100496 ptr += 8;
497 }
Paul Cercueil71694c72014-05-22 14:02:13 +0200498
499 strcpy(ptr, cyclic ? " CYCLIC\r\n" : "\r\n");
Paul Cercueilba059762014-03-14 11:02:02 +0100500
Paul Cercueilc5b00752015-02-24 14:29:53 +0100501 network_lock_dev(dev->pdata);
Paul Cercueil439e1a22015-02-24 13:50:51 +0100502 ret = (int) exec_command(buf, fd);
Paul Cercueilc5b00752015-02-24 14:29:53 +0100503 network_unlock_dev(dev->pdata);
Paul Cercueil1494b862014-05-05 12:49:07 +0200504
Paul Cercueil44ae11c2014-03-17 09:56:29 +0100505 if (ret < 0) {
Paul Cercueil439e1a22015-02-24 13:50:51 +0100506 close(fd);
Paul Cercueil44ae11c2014-03-17 09:56:29 +0100507 return ret;
Paul Cercueil44ae11c2014-03-17 09:56:29 +0100508 }
Paul Cercueil439e1a22015-02-24 13:50:51 +0100509
Paul Cercueil09a43482015-03-04 15:50:27 +0100510 dev->pdata->is_tx = is_tx(dev);
Paul Cercueil1d70d442015-03-04 15:00:37 +0100511 dev->pdata->is_cyclic = cyclic;
Paul Cercueil439e1a22015-02-24 13:50:51 +0100512 dev->pdata->fd = fd;
Paul Cercueil8f7f6c42015-02-24 15:00:35 +0100513 dev->pdata->wait_for_err_code = false;
Paul Cercueil439e1a22015-02-24 13:50:51 +0100514 memcpy(dev->mask, mask, nb * sizeof(*mask));
515 return 0;
Paul Cercueilba059762014-03-14 11:02:02 +0100516}
517
Paul Cercueil8f7f6c42015-02-24 15:00:35 +0100518static ssize_t read_error_code(int fd)
519{
520 /*
521 * The server returns two integer codes.
522 * The first one is returned right after the WRITEBUF command is issued,
523 * and corresponds to the error code returned when the server attempted
524 * to open the device.
525 * If zero, a second error code is returned, that corresponds (if positive)
526 * to the number of bytes written.
527 *
528 * To speed up things, we delay error reporting. We just send out the
529 * data without reading the error code that the server gives us, because
530 * the answer will take too much time. If an error occured, it will be
531 * reported by the next call to iio_buffer_push().
532 */
533
534 unsigned int i;
535 long resp = 0;
536
537 for (i = 0; i < 2; i++) {
538 ssize_t ret = read_integer(fd, &resp);
539 if (ret < 0)
540 return ret;
541 if (resp < 0)
542 return (ssize_t) resp;
543 }
544
545 return resp;
546}
547
Paul Cercueilba059762014-03-14 11:02:02 +0100548static int network_close(const struct iio_device *dev)
549{
Paul Cercueil439e1a22015-02-24 13:50:51 +0100550 struct iio_device_pdata *pdata = dev->pdata;
Paul Cercueil1494b862014-05-05 12:49:07 +0200551 int ret;
Paul Cercueilba059762014-03-14 11:02:02 +0100552 char buf[1024];
Paul Cercueil439e1a22015-02-24 13:50:51 +0100553
Paul Cercueilba059762014-03-14 11:02:02 +0100554 snprintf(buf, sizeof(buf), "CLOSE %s\r\n", dev->id);
Paul Cercueil1494b862014-05-05 12:49:07 +0200555
Paul Cercueilc5b00752015-02-24 14:29:53 +0100556 network_lock_dev(pdata);
Paul Cercueil8f7f6c42015-02-24 15:00:35 +0100557 if (pdata->wait_for_err_code)
558 read_error_code(pdata->fd);
559
Paul Cercueil439e1a22015-02-24 13:50:51 +0100560 ret = (int) exec_command(buf, pdata->fd);
Paul Cercueil1494b862014-05-05 12:49:07 +0200561
Paul Cercueil439e1a22015-02-24 13:50:51 +0100562 close(pdata->fd);
563 pdata->fd = -1;
Paul Cercueilc5b00752015-02-24 14:29:53 +0100564 network_unlock_dev(pdata);
Paul Cercueil1494b862014-05-05 12:49:07 +0200565 return ret;
Paul Cercueilba059762014-03-14 11:02:02 +0100566}
567
Paul Cercueil45c575d2014-03-20 15:14:01 +0100568static ssize_t network_read(const struct iio_device *dev, void *dst, size_t len,
569 uint32_t *mask, size_t words)
Paul Cercueilf9286452014-03-18 14:32:17 +0100570{
Paul Cercueil6e7f79e2014-04-04 12:27:24 +0200571 uintptr_t ptr = (uintptr_t) dst;
Paul Cercueilc5b00752015-02-24 14:29:53 +0100572 struct iio_device_pdata *pdata = dev->pdata;
573 int fd = pdata->fd;
Paul Cercueil45c575d2014-03-20 15:14:01 +0100574 ssize_t ret, read = 0;
Paul Cercueil9945bc82014-03-05 14:07:29 +0100575 char buf[1024];
Paul Cercueil45c575d2014-03-20 15:14:01 +0100576 bool read_mask = true;
Paul Cercueil9945bc82014-03-05 14:07:29 +0100577
Paul Cercueil45c575d2014-03-20 15:14:01 +0100578 if (!len || words != (dev->nb_channels + 31) / 32)
Paul Cercueil9945bc82014-03-05 14:07:29 +0100579 return -EINVAL;
580
Paul Cercueila78b6eb2014-03-17 17:28:07 +0100581 snprintf(buf, sizeof(buf), "READBUF %s %lu\r\n",
582 dev->id, (unsigned long) len);
Paul Cercueil1494b862014-05-05 12:49:07 +0200583
Paul Cercueilc5b00752015-02-24 14:29:53 +0100584 network_lock_dev(pdata);
Paul Cercueil8f7f6c42015-02-24 15:00:35 +0100585 if (pdata->wait_for_err_code) {
586 pdata->wait_for_err_code = false;
587 ret = read_error_code(fd);
588 if (ret < 0) {
589 network_unlock_dev(pdata);
590 return ret;
591 }
592 }
593
Paul Cercueilbb76bf92014-03-11 10:20:18 +0100594 ret = write_command(buf, fd);
Paul Cercueil1494b862014-05-05 12:49:07 +0200595 if (ret < 0) {
Paul Cercueilc5b00752015-02-24 14:29:53 +0100596 network_unlock_dev(pdata);
Paul Cercueil9945bc82014-03-05 14:07:29 +0100597 return ret;
Paul Cercueil1494b862014-05-05 12:49:07 +0200598 }
Paul Cercueil9945bc82014-03-05 14:07:29 +0100599
600 do {
Paul Cercueil5cab3ae2014-03-14 11:23:56 +0100601 unsigned int i;
Paul Cercueil9945bc82014-03-05 14:07:29 +0100602 long read_len;
603
604 DEBUG("Reading READ response\n");
605 ret = read_integer(fd, &read_len);
606 if (ret < 0) {
607 strerror_r(-ret, buf, sizeof(buf));
608 ERROR("Unable to read response to READ: %s\n", buf);
Paul Cercueilc5b00752015-02-24 14:29:53 +0100609 network_unlock_dev(pdata);
Paul Cercueil6e7f79e2014-04-04 12:27:24 +0200610 return read ? read : ret;
Paul Cercueil9945bc82014-03-05 14:07:29 +0100611 }
612
613 if (read_len < 0) {
614 strerror_r(-read_len, buf, sizeof(buf));
615 ERROR("Server returned an error: %s\n", buf);
Paul Cercueilc5b00752015-02-24 14:29:53 +0100616 network_unlock_dev(pdata);
Paul Cercueil6e7f79e2014-04-04 12:27:24 +0200617 return read ? read : read_len;
Paul Cercueila78b6eb2014-03-17 17:28:07 +0100618 } else if (read_len == 0) {
619 break;
Paul Cercueil9945bc82014-03-05 14:07:29 +0100620 }
621
622 DEBUG("Bytes to read: %li\n", read_len);
623
Paul Cercueil43c1e6c2014-03-20 14:26:20 +0100624 if (read_mask) {
625 DEBUG("Reading mask\n");
626 buf[8] = '\0';
Paul Cercueil45c575d2014-03-20 15:14:01 +0100627 for (i = words; i > 0; i--) {
Paul Cercueil43c1e6c2014-03-20 14:26:20 +0100628 ret = read_all(buf, 8, fd);
629 if (ret < 0)
630 break;
631 sscanf(buf, "%08x", &mask[i - 1]);
632 DEBUG("mask[%i] = 0x%x\n", i - 1, mask[i - 1]);
Paul Cercueil43c1e6c2014-03-20 14:26:20 +0100633 }
634 read_mask = false;
Paul Cercueil5cab3ae2014-03-14 11:23:56 +0100635 }
636
637 if (ret > 0) {
638 char c;
Lars-Peter Clausencb7b8bf2014-12-05 16:08:52 +0100639 ret = read_all(&c, 1, fd);
Paul Cercueil5cab3ae2014-03-14 11:23:56 +0100640 if (ret > 0 && c != '\n')
641 ret = -EIO;
642 }
643
644 if (ret < 0) {
645 strerror_r(-ret, buf, sizeof(buf));
646 ERROR("Unable to read mask: %s\n", buf);
Paul Cercueilc5b00752015-02-24 14:29:53 +0100647 network_unlock_dev(pdata);
Paul Cercueil6e7f79e2014-04-04 12:27:24 +0200648 return read ? read : ret;
Paul Cercueil5cab3ae2014-03-14 11:23:56 +0100649 }
650
Paul Cercueil6e7f79e2014-04-04 12:27:24 +0200651 ret = read_all((void *) ptr, read_len, fd);
Paul Cercueil9945bc82014-03-05 14:07:29 +0100652 if (ret < 0) {
653 strerror_r(-ret, buf, sizeof(buf));
654 ERROR("Unable to read response to READ: %s\n", buf);
Paul Cercueilc5b00752015-02-24 14:29:53 +0100655 network_unlock_dev(pdata);
Paul Cercueil6e7f79e2014-04-04 12:27:24 +0200656 return read ? read : ret;
Paul Cercueil9945bc82014-03-05 14:07:29 +0100657 }
658
Paul Cercueil6e7f79e2014-04-04 12:27:24 +0200659 ptr += ret;
Paul Cercueilf9286452014-03-18 14:32:17 +0100660 read += ret;
Paul Cercueil9945bc82014-03-05 14:07:29 +0100661 len -= read_len;
662 } while (len);
663
Paul Cercueilc5b00752015-02-24 14:29:53 +0100664 network_unlock_dev(pdata);
Paul Cercueil9945bc82014-03-05 14:07:29 +0100665 return read;
666}
667
Paul Cercueila62f84e2015-02-24 14:16:08 +0100668static ssize_t network_write(const struct iio_device *dev,
669 const void *src, size_t len)
Paul Cercueil2725f702014-05-02 11:02:16 +0200670{
Paul Cercueilc5b00752015-02-24 14:29:53 +0100671 struct iio_device_pdata *pdata = dev->pdata;
Paul Cercueil439e1a22015-02-24 13:50:51 +0100672 int fd;
Paul Cercueil2725f702014-05-02 11:02:16 +0200673 ssize_t ret;
Paul Cercueil2725f702014-05-02 11:02:16 +0200674 long resp;
Paul Cercueila62f84e2015-02-24 14:16:08 +0100675 char buf[1024];
676
677 snprintf(buf, sizeof(buf), "WRITEBUF %s %lu\r\n",
678 dev->id, (unsigned long) len);
Paul Cercueil2725f702014-05-02 11:02:16 +0200679
Paul Cercueilc5b00752015-02-24 14:29:53 +0100680 network_lock_dev(pdata);
681 fd = pdata->fd;
Paul Cercueil8f7f6c42015-02-24 15:00:35 +0100682
683 if (pdata->wait_for_err_code) {
684 pdata->wait_for_err_code = false;
685 ret = read_error_code(fd);
686 if (ret < 0)
687 goto err_unlock;
688 }
689
Paul Cercueil1d70d442015-03-04 15:00:37 +0100690 if (pdata->is_cyclic)
691 ret = (ssize_t) exec_command(buf, fd);
692 else
693 ret = (ssize_t) write_command(buf, fd);
Paul Cercueil2725f702014-05-02 11:02:16 +0200694 if (ret < 0)
Paul Cercueil1494b862014-05-05 12:49:07 +0200695 goto err_unlock;
Paul Cercueil2725f702014-05-02 11:02:16 +0200696
697 ret = write_all(src, len, fd);
698 if (ret < 0)
Paul Cercueil1494b862014-05-05 12:49:07 +0200699 goto err_unlock;
Paul Cercueil2725f702014-05-02 11:02:16 +0200700
Paul Cercueil1d70d442015-03-04 15:00:37 +0100701 if (pdata->is_cyclic) {
702 ret = read_integer(fd, &resp);
703 if (ret < 0)
704 goto err_unlock;
705 if (resp < 0) {
706 ret = (ssize_t) resp;
707 goto err_unlock;
708 }
709 } else {
710 pdata->wait_for_err_code = true;
711 }
Paul Cercueilc5b00752015-02-24 14:29:53 +0100712 network_unlock_dev(pdata);
Paul Cercueil1494b862014-05-05 12:49:07 +0200713
Paul Cercueil8f7f6c42015-02-24 15:00:35 +0100714 /* We assume that the whole buffer was submitted.
715 * The error code will be returned by the next call to this function. */
716 return (ssize_t) len;
Paul Cercueil1494b862014-05-05 12:49:07 +0200717
718err_unlock:
Paul Cercueilc5b00752015-02-24 14:29:53 +0100719 network_unlock_dev(pdata);
Paul Cercueil1494b862014-05-05 12:49:07 +0200720 return ret;
Paul Cercueil2725f702014-05-02 11:02:16 +0200721}
722
Paul Cercueil09a43482015-03-04 15:50:27 +0100723#ifdef __linux__
724static int network_do_splice(int fd_out, int fd_in, size_t len)
725{
726 int pipefd[2];
727 ssize_t ret;
728
729 ret = (ssize_t) pipe(pipefd);
730 if (ret < 0)
731 return -errno;
732
733 do {
734 ret = splice(fd_in, NULL, pipefd[1], NULL, len, SPLICE_F_MOVE);
735 if (ret < 0)
736 goto err_close_pipe;
737
738 ret = splice(pipefd[0], NULL, fd_out, NULL, len, SPLICE_F_MOVE);
739 if (ret < 0)
740 goto err_close_pipe;
741
742 len -= ret;
743 } while (len);
744
745err_close_pipe:
746 close(pipefd[0]);
747 close(pipefd[1]);
748 return (int) ret;
749}
750
751static ssize_t network_get_buffer(const struct iio_device *dev,
752 void **addr_ptr, size_t bytes_used)
753{
754 struct iio_device_pdata *pdata = dev->pdata;
755 int ret;
756 bool tx;
757 void *ptr;
758
759 if (!pdata->is_tx || pdata->is_cyclic)
760 return -ENOSYS;
761 if (!addr_ptr)
762 return -EINVAL;
763
764 if (*addr_ptr)
765 munmap(*addr_ptr, pdata->mmap_len);
766
767 if (*addr_ptr && pdata->is_tx) {
768 long resp;
769 char buf[1024];
770 snprintf(buf, sizeof(buf), "WRITEBUF %s %lu\r\n",
771 dev->id, (unsigned long) pdata->mmap_len);
772
773 network_lock_dev(pdata);
774
775 if (pdata->wait_for_err_code) {
776 pdata->wait_for_err_code = false;
777 ret = read_error_code(pdata->fd);
778 if (ret < 0)
779 goto err_unlock;
780 }
781
782 ret = (ssize_t) write_command(buf, pdata->fd);
783 if (ret < 0)
784 goto err_unlock;
785
786 ret = network_do_splice(pdata->fd,
787 pdata->memfd, pdata->mmap_len);
788 if (ret < 0)
789 goto err_unlock;
790
791 pdata->wait_for_err_code = true;
792 network_unlock_dev(pdata);
793 }
794
795 if (pdata->memfd >= 0)
796 close(pdata->memfd);
797
798 if (bytes_used)
799 pdata->mmap_len = bytes_used;
800
801 /* O_TMPFILE -> Linux 3.11.
802 * TODO: use memfd_create (Linux 3.17) */
803 pdata->memfd = open(P_tmpdir, O_RDWR | O_TMPFILE | O_EXCL, S_IRWXU);
804 if (pdata->memfd < 0) {
805 ret = -errno;
806 ERROR("Unable to create temp file: %i\n", -ret);
807 return ret;
808 }
809
810 ftruncate(pdata->memfd, pdata->mmap_len);
811
812 /* TODO: READBUF */
813
814 ptr = mmap(NULL, pdata->mmap_len,
815 PROT_READ | PROT_WRITE, MAP_SHARED, pdata->memfd, 0);
816 if (ptr == MAP_FAILED) {
817 ret = -errno;
818 ERROR("Unable to mmap: %i\n", -ret);
819 return ret;
820 }
821
822 *addr_ptr = ptr;
823 return bytes_used;
824
825err_unlock:
826 network_unlock_dev(pdata);
827 return ret;
828}
829#endif
830
Paul Cercueil1494b862014-05-05 12:49:07 +0200831static ssize_t network_read_attr_helper(const struct iio_device *dev,
Paul Cercueil0cc0e162014-05-07 12:51:22 +0200832 const struct iio_channel *chn, const char *attr, char *dst,
Paul Cercueil1494b862014-05-05 12:49:07 +0200833 size_t len, bool is_debug)
Paul Cercueil3e94a5b2014-03-04 13:00:41 +0100834{
Paul Cercueil3e94a5b2014-03-04 13:00:41 +0100835 long read_len;
836 ssize_t ret;
837 char buf[1024];
Paul Cercueil05e26262014-05-09 11:32:43 +0200838 struct iio_context_pdata *pdata = dev->ctx->pdata;
839 int fd = pdata->fd;
Paul Cercueil1494b862014-05-05 12:49:07 +0200840 const char *id = dev->id;
Paul Cercueil3e94a5b2014-03-04 13:00:41 +0100841
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100842 if (chn)
Paul Cercueil0cc0e162014-05-07 12:51:22 +0200843 snprintf(buf, sizeof(buf), "READ %s %s %s %s\r\n", id,
844 chn->is_output ? "OUTPUT" : "INPUT",
Paul Cercueil2a3e43d2014-05-21 16:58:45 +0200845 chn->id, attr ? attr : "");
Paul Cercueilc6f03612014-04-14 16:41:31 +0200846 else if (is_debug)
Paul Cercueil2a3e43d2014-05-21 16:58:45 +0200847 snprintf(buf, sizeof(buf), "READ %s DEBUG %s\r\n",
848 id, attr ? attr : "");
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100849 else
Paul Cercueil2a3e43d2014-05-21 16:58:45 +0200850 snprintf(buf, sizeof(buf), "READ %s %s\r\n",
851 id, attr ? attr : "");
Paul Cercueil1494b862014-05-05 12:49:07 +0200852
Paul Cercueil05e26262014-05-09 11:32:43 +0200853 network_lock(pdata);
Paul Cercueilbb76bf92014-03-11 10:20:18 +0100854 read_len = exec_command(buf, fd);
Paul Cercueil1494b862014-05-05 12:49:07 +0200855 if (read_len < 0) {
Paul Cercueil05e26262014-05-09 11:32:43 +0200856 network_unlock(pdata);
Paul Cercueilbb76bf92014-03-11 10:20:18 +0100857 return (ssize_t) read_len;
Paul Cercueil1494b862014-05-05 12:49:07 +0200858 }
Paul Cercueil3e94a5b2014-03-04 13:00:41 +0100859
Paul Cercueil6e7f79e2014-04-04 12:27:24 +0200860 if ((unsigned long) read_len > len) {
Paul Cercueil3e94a5b2014-03-04 13:00:41 +0100861 ERROR("Value returned by server is too large\n");
Paul Cercueil05e26262014-05-09 11:32:43 +0200862 network_unlock(pdata);
Paul Cercueil3e94a5b2014-03-04 13:00:41 +0100863 return -EIO;
864 }
865
866 ret = read_all(dst, read_len, fd);
Paul Cercueil05e26262014-05-09 11:32:43 +0200867 network_unlock(pdata);
Paul Cercueil1494b862014-05-05 12:49:07 +0200868
Paul Cercueil3e94a5b2014-03-04 13:00:41 +0100869 if (ret < 0) {
870 strerror_r(-ret, buf, sizeof(buf));
871 ERROR("Unable to read response to READ: %s\n", buf);
872 return ret;
873 }
874
Paul Cercueil7d95fd72014-05-22 14:26:44 +0200875 return read_len;
Paul Cercueil3e94a5b2014-03-04 13:00:41 +0100876}
877
Paul Cercueil1494b862014-05-05 12:49:07 +0200878static ssize_t network_write_attr_helper(const struct iio_device *dev,
Paul Cercueil0cc0e162014-05-07 12:51:22 +0200879 const struct iio_channel *chn, const char *attr,
880 const char *src, size_t len, bool is_debug)
Paul Cercueil3e94a5b2014-03-04 13:00:41 +0100881{
Paul Cercueila62f84e2015-02-24 14:16:08 +0100882 struct iio_context_pdata *pdata = dev->ctx->pdata;
883 int fd;
884 ssize_t ret;
885 long resp;
Paul Cercueil3e94a5b2014-03-04 13:00:41 +0100886 char buf[1024];
Paul Cercueil1494b862014-05-05 12:49:07 +0200887 const char *id = dev->id;
Paul Cercueil3e94a5b2014-03-04 13:00:41 +0100888
Paul Cercueil07897d32014-03-06 12:46:08 +0100889 if (chn)
Paul Cercueil0cc0e162014-05-07 12:51:22 +0200890 snprintf(buf, sizeof(buf), "WRITE %s %s %s %s %lu\r\n",
891 id, chn->is_output ? "OUTPUT" : "INPUT",
Paul Cercueil8747efe2014-05-22 11:12:12 +0200892 chn->id, attr ? attr : "", (unsigned long) len);
Paul Cercueilc6f03612014-04-14 16:41:31 +0200893 else if (is_debug)
Paul Cercueil8747efe2014-05-22 11:12:12 +0200894 snprintf(buf, sizeof(buf), "WRITE %s DEBUG %s %lu\r\n",
895 id, attr ? attr : "", (unsigned long) len);
Paul Cercueil07897d32014-03-06 12:46:08 +0100896 else
Paul Cercueilcecda352014-05-06 18:14:29 +0200897 snprintf(buf, sizeof(buf), "WRITE %s %s %lu\r\n",
Paul Cercueil8747efe2014-05-22 11:12:12 +0200898 id, attr ? attr : "", (unsigned long) len);
Paul Cercueila62f84e2015-02-24 14:16:08 +0100899
900 network_lock(pdata);
901 fd = pdata->fd;
902 ret = (ssize_t) write_command(buf, fd);
903 if (ret < 0)
904 goto err_unlock;
905
906 ret = write_all(src, len, fd);
907 if (ret < 0)
908 goto err_unlock;
909
910 ret = read_integer(fd, &resp);
911 network_unlock(pdata);
912
913 if (ret < 0)
914 return ret;
915 return (ssize_t) resp;
916
917err_unlock:
918 network_unlock(pdata);
919 return ret;
Paul Cercueil3e94a5b2014-03-04 13:00:41 +0100920}
921
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100922static ssize_t network_read_dev_attr(const struct iio_device *dev,
Paul Cercueil50c762a2014-04-14 15:55:43 +0200923 const char *attr, char *dst, size_t len, bool is_debug)
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100924{
Paul Cercueil5b577762014-06-03 15:31:42 +0200925 if (attr && ((is_debug && !iio_device_find_debug_attr(dev, attr)) ||
926 (!is_debug && !iio_device_find_attr(dev, attr))))
927 return -ENOENT;
928
Paul Cercueil1494b862014-05-05 12:49:07 +0200929 return network_read_attr_helper(dev, NULL, attr, dst, len, is_debug);
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100930}
931
Paul Cercueil07897d32014-03-06 12:46:08 +0100932static ssize_t network_write_dev_attr(const struct iio_device *dev,
Paul Cercueilcecda352014-05-06 18:14:29 +0200933 const char *attr, const char *src, size_t len, bool is_debug)
Paul Cercueil07897d32014-03-06 12:46:08 +0100934{
Paul Cercueil5b577762014-06-03 15:31:42 +0200935 if (attr && ((is_debug && !iio_device_find_debug_attr(dev, attr)) ||
936 (!is_debug && !iio_device_find_attr(dev, attr))))
937 return -ENOENT;
938
Paul Cercueilcecda352014-05-06 18:14:29 +0200939 return network_write_attr_helper(dev, NULL, attr, src, len, is_debug);
Paul Cercueil07897d32014-03-06 12:46:08 +0100940}
941
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100942static ssize_t network_read_chn_attr(const struct iio_channel *chn,
943 const char *attr, char *dst, size_t len)
944{
Paul Cercueil5b577762014-06-03 15:31:42 +0200945 if (attr && !iio_channel_find_attr(chn, attr))
946 return -ENOENT;
947
Paul Cercueil0cc0e162014-05-07 12:51:22 +0200948 return network_read_attr_helper(chn->dev, chn, attr, dst, len, false);
Paul Cercueil99cf3d42014-03-06 12:35:26 +0100949}
950
Paul Cercueil07897d32014-03-06 12:46:08 +0100951static ssize_t network_write_chn_attr(const struct iio_channel *chn,
Paul Cercueilcecda352014-05-06 18:14:29 +0200952 const char *attr, const char *src, size_t len)
Paul Cercueil07897d32014-03-06 12:46:08 +0100953{
Paul Cercueil5b577762014-06-03 15:31:42 +0200954 if (attr && !iio_channel_find_attr(chn, attr))
955 return -ENOENT;
956
Paul Cercueil0cc0e162014-05-07 12:51:22 +0200957 return network_write_attr_helper(chn->dev, chn, attr, src, len, false);
Paul Cercueil07897d32014-03-06 12:46:08 +0100958}
959
Paul Cercueildcab40c2014-03-11 10:59:14 +0100960static int network_get_trigger(const struct iio_device *dev,
961 const struct iio_device **trigger)
962{
963 struct iio_context_pdata *pdata = dev->ctx->pdata;
964 unsigned int i;
965 char buf[1024];
966 ssize_t ret;
967 long resp;
968
969 snprintf(buf, sizeof(buf), "GETTRIG %s\r\n", dev->id);
Paul Cercueil1494b862014-05-05 12:49:07 +0200970
Paul Cercueil05e26262014-05-09 11:32:43 +0200971 network_lock(dev->ctx->pdata);
Paul Cercueildcab40c2014-03-11 10:59:14 +0100972 resp = exec_command(buf, pdata->fd);
973 if (resp < 0) {
Paul Cercueil05e26262014-05-09 11:32:43 +0200974 network_unlock(pdata);
Paul Cercueildcab40c2014-03-11 10:59:14 +0100975 return (int) resp;
976 } else if (resp == 0) {
977 *trigger = NULL;
Paul Cercueil05e26262014-05-09 11:32:43 +0200978 network_unlock(pdata);
Paul Cercueildcab40c2014-03-11 10:59:14 +0100979 return 0;
Paul Cercueil6e7f79e2014-04-04 12:27:24 +0200980 } else if ((unsigned long) resp > sizeof(buf)) {
Paul Cercueildcab40c2014-03-11 10:59:14 +0100981 ERROR("Value returned by server is too large\n");
Paul Cercueil05e26262014-05-09 11:32:43 +0200982 network_unlock(pdata);
Paul Cercueildcab40c2014-03-11 10:59:14 +0100983 return -EIO;
984 }
985
986 ret = read_all(buf, resp, pdata->fd);
Paul Cercueil05e26262014-05-09 11:32:43 +0200987 network_unlock(pdata);
Paul Cercueil1494b862014-05-05 12:49:07 +0200988
Paul Cercueildcab40c2014-03-11 10:59:14 +0100989 if (ret < 0) {
990 strerror_r(-ret, buf, sizeof(buf));
991 ERROR("Unable to read response to GETTRIG: %s\n", buf);
992 return ret;
993 }
994
Lars-Peter Clausenb5cdc102014-08-21 14:45:52 +0200995 if (buf[0] == '\0') {
996 *trigger = NULL;
997 return 0;
998 }
999
Paul Cercueildcab40c2014-03-11 10:59:14 +01001000 for (i = 0; i < dev->ctx->nb_devices; i++) {
1001 struct iio_device *cur = dev->ctx->devices[i];
1002 if (iio_device_is_trigger(cur) &&
1003 !strncmp(cur->name, buf, resp)) {
1004 *trigger = cur;
1005 return 0;
1006 }
1007 }
1008
1009 return -ENXIO;
1010}
1011
1012static int network_set_trigger(const struct iio_device *dev,
1013 const struct iio_device *trigger)
1014{
Paul Cercueil1494b862014-05-05 12:49:07 +02001015 int ret;
Paul Cercueildcab40c2014-03-11 10:59:14 +01001016 char buf[1024];
1017 if (trigger)
1018 snprintf(buf, sizeof(buf), "SETTRIG %s %s\r\n",
1019 dev->id, trigger->id);
1020 else
1021 snprintf(buf, sizeof(buf), "SETTRIG %s\r\n", dev->id);
Paul Cercueil1494b862014-05-05 12:49:07 +02001022
Paul Cercueil05e26262014-05-09 11:32:43 +02001023 network_lock(dev->ctx->pdata);
Paul Cercueil1494b862014-05-05 12:49:07 +02001024 ret = (int) exec_command(buf, dev->ctx->pdata->fd);
Paul Cercueil05e26262014-05-09 11:32:43 +02001025 network_unlock(dev->ctx->pdata);
Paul Cercueil1494b862014-05-05 12:49:07 +02001026 return ret;
Paul Cercueildcab40c2014-03-11 10:59:14 +01001027}
1028
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001029static void network_shutdown(struct iio_context *ctx)
1030{
1031 struct iio_context_pdata *pdata = ctx->pdata;
Paul Cercueil44ae11c2014-03-17 09:56:29 +01001032 unsigned int i;
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001033
Paul Cercueil05e26262014-05-09 11:32:43 +02001034 network_lock(pdata);
Paul Cercueilbb76bf92014-03-11 10:20:18 +01001035 write_command("\r\nEXIT\r\n", pdata->fd);
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001036 close(pdata->fd);
Paul Cercueil05e26262014-05-09 11:32:43 +02001037 network_unlock(pdata);
Paul Cercueil1494b862014-05-05 12:49:07 +02001038
Paul Cercueil439e1a22015-02-24 13:50:51 +01001039 for (i = 0; i < ctx->nb_devices; i++) {
1040 struct iio_device *dev = ctx->devices[i];
Paul Cercueilc5b00752015-02-24 14:29:53 +01001041 struct iio_device_pdata *dpdata = dev->pdata;
Paul Cercueil439e1a22015-02-24 13:50:51 +01001042
Paul Cercueilc5b00752015-02-24 14:29:53 +01001043 if (dpdata) {
1044 if (dpdata->fd >= 0) {
1045 network_lock_dev(dpdata);
1046 write_command("\r\nEXIT\r\n", dpdata->fd);
1047 close(dpdata->fd);
1048 network_unlock_dev(dpdata);
1049#if HAVE_PTHREAD
1050 pthread_mutex_destroy(&dpdata->lock);
1051#endif
Paul Cercueil439e1a22015-02-24 13:50:51 +01001052 }
1053
Paul Cercueilc5b00752015-02-24 14:29:53 +01001054 free(dpdata);
Paul Cercueil439e1a22015-02-24 13:50:51 +01001055 }
1056 }
1057
Paul Cercueil05e26262014-05-09 11:32:43 +02001058#if HAVE_PTHREAD
Paul Cercueil1494b862014-05-05 12:49:07 +02001059 pthread_mutex_destroy(&pdata->lock);
Paul Cercueil05e26262014-05-09 11:32:43 +02001060#endif
Paul Cercueil2dd2c132015-02-24 10:41:01 +01001061 freeaddrinfo(pdata->addrinfo);
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001062 free(pdata);
1063}
1064
Paul Cercueil6691a3f2014-05-02 12:32:01 +02001065static int network_get_version(const struct iio_context *ctx,
Paul Cercueil9de9e9d2014-05-20 13:18:19 +02001066 unsigned int *major, unsigned int *minor, char git_tag[8])
Paul Cercueil6691a3f2014-05-02 12:32:01 +02001067{
1068 struct iio_context_pdata *pdata = ctx->pdata;
1069 long maj, min;
1070 int ret;
Paul Cercueil6691a3f2014-05-02 12:32:01 +02001071
Paul Cercueil05e26262014-05-09 11:32:43 +02001072 network_lock(pdata);
Paul Cercueil1494b862014-05-05 12:49:07 +02001073 ret = (int) write_command("VERSION\r\n", pdata->fd);
1074 if (ret < 0)
1075 goto err_unlock;
1076
1077 ret = read_integer(pdata->fd, &maj);
1078 if (!ret)
1079 ret = read_integer(pdata->fd, &min);
1080 if (!ret) {
Paul Cercueil9de9e9d2014-05-20 13:18:19 +02001081 char tag[8];
1082 tag[7] = '\0';
1083
1084 ret = read_all(tag, sizeof(tag) - 1, pdata->fd);
1085 if (ret < 0)
1086 goto err_unlock;
1087
Paul Cercueild15d9952014-05-20 11:40:08 +02001088 if (major)
1089 *major = (unsigned int) maj;
1090 if (minor)
1091 *minor = (unsigned int) min;
Paul Cercueil9de9e9d2014-05-20 13:18:19 +02001092 if (git_tag)
1093 strncpy(git_tag, tag, 8);
Paul Cercueil1494b862014-05-05 12:49:07 +02001094 }
1095
Paul Cercueil8e8f8992014-06-12 16:58:12 +02001096 ret = 0;
Paul Cercueil1494b862014-05-05 12:49:07 +02001097err_unlock:
Paul Cercueil05e26262014-05-09 11:32:43 +02001098 network_unlock(pdata);
Paul Cercueil1494b862014-05-05 12:49:07 +02001099 return ret;
Paul Cercueil6691a3f2014-05-02 12:32:01 +02001100}
1101
Paul Cercueil8a266f12014-06-10 16:06:31 +02001102static unsigned int calculate_remote_timeout(unsigned int timeout)
1103{
1104 /* XXX(pcercuei): We currently hardcode timeout / 2 for the backend used
1105 * by the remote. Is there something better to do here? */
1106 return timeout / 2;
1107}
1108
Paul Cercueilbca3dbc2014-06-11 12:00:21 +02001109static int set_remote_timeout(struct iio_context *ctx, unsigned int timeout)
1110{
1111 char buf[1024];
1112 int ret;
1113
1114 snprintf(buf, sizeof(buf), "TIMEOUT %u\r\n", timeout);
1115 network_lock(ctx->pdata);
1116 ret = (int) exec_command(buf, ctx->pdata->fd);
1117 network_unlock(ctx->pdata);
1118 return ret;
1119}
1120
1121static int network_set_timeout(struct iio_context *ctx, unsigned int timeout)
1122{
1123 int ret = set_socket_timeout(ctx->pdata->fd, timeout);
1124 if (!ret) {
1125 timeout = calculate_remote_timeout(timeout);
1126 ret = set_remote_timeout(ctx, timeout);
1127 }
1128 if (ret < 0) {
1129 char buf[1024];
1130 strerror_r(-ret, buf, sizeof(buf));
Paul Cercueil8a266f12014-06-10 16:06:31 +02001131 WARNING("Unable to set R/W timeout: %s\n", buf);
1132 } else {
Paul Cercueil8a266f12014-06-10 16:06:31 +02001133 ctx->rw_timeout_ms = timeout;
1134 }
1135 return ret;
1136}
1137
Paul Cercueil12d41832014-10-28 14:35:53 +01001138static struct iio_context * network_clone(const struct iio_context *ctx)
1139{
Paul Cercueild3d56ee2015-01-08 16:36:31 +01001140 return iio_create_network_context(ctx->description);
Paul Cercueil12d41832014-10-28 14:35:53 +01001141}
1142
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001143static struct iio_backend_ops network_ops = {
Paul Cercueil12d41832014-10-28 14:35:53 +01001144 .clone = network_clone,
Paul Cercueilba059762014-03-14 11:02:02 +01001145 .open = network_open,
1146 .close = network_close,
Paul Cercueil9945bc82014-03-05 14:07:29 +01001147 .read = network_read,
Paul Cercueil2725f702014-05-02 11:02:16 +02001148 .write = network_write,
Paul Cercueil09a43482015-03-04 15:50:27 +01001149#ifdef __linux__
1150 .get_buffer = network_get_buffer,
1151#endif
Paul Cercueil3e94a5b2014-03-04 13:00:41 +01001152 .read_device_attr = network_read_dev_attr,
1153 .write_device_attr = network_write_dev_attr,
Paul Cercueil99cf3d42014-03-06 12:35:26 +01001154 .read_channel_attr = network_read_chn_attr,
Paul Cercueil07897d32014-03-06 12:46:08 +01001155 .write_channel_attr = network_write_chn_attr,
Paul Cercueildcab40c2014-03-11 10:59:14 +01001156 .get_trigger = network_get_trigger,
1157 .set_trigger = network_set_trigger,
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001158 .shutdown = network_shutdown,
Paul Cercueil6691a3f2014-05-02 12:32:01 +02001159 .get_version = network_get_version,
Paul Cercueil8a266f12014-06-10 16:06:31 +02001160 .set_timeout = network_set_timeout,
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001161};
1162
1163static struct iio_context * get_context(int fd)
1164{
1165 struct iio_context *ctx;
Paul Cercueile60c3ed2014-03-04 11:23:56 +01001166 char *xml;
Paul Cercueilbb76bf92014-03-11 10:20:18 +01001167 long xml_len = exec_command("PRINT\r\n", fd);
1168 if (xml_len < 0)
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001169 return NULL;
Paul Cercueile60c3ed2014-03-04 11:23:56 +01001170
1171 DEBUG("Server returned a XML string of length %li\n", xml_len);
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001172 xml = malloc(xml_len);
1173 if (!xml) {
1174 ERROR("Unable to allocate data\n");
1175 return NULL;
1176 }
1177
1178 DEBUG("Reading XML string...\n");
1179 read_all(xml, xml_len, fd);
1180
1181 DEBUG("Creating context from XML...\n");
1182 ctx = iio_create_xml_context_mem(xml, xml_len);
Paul Cercueilc1ed8482014-06-11 16:29:43 +02001183
1184 if (ctx)
1185 ctx->xml = xml;
1186 else
1187 free(xml);
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001188 return ctx;
1189}
1190
Paul Cercueil63e52182014-12-11 12:52:48 +01001191struct iio_context * network_create_context(const char *host)
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001192{
Paul Cercueil2e38bbe2014-03-19 15:27:15 +01001193 struct addrinfo hints, *res;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001194 struct iio_context *ctx;
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001195 struct iio_context_pdata *pdata;
Paul Cercueil06c479d2015-01-08 16:14:57 +01001196 unsigned int i, len;
Paul Cercueil4970ac32015-02-24 10:59:00 +01001197 int fd, ret;
Paul Cercueil1fef1a52014-04-07 16:31:15 +02001198#ifdef _WIN32
1199 WSADATA wsaData;
1200
1201 ret = WSAStartup(MAKEWORD(2, 0), &wsaData);
1202 if (ret < 0) {
1203 ERROR("WSAStartup failed with error %i\n", ret);
1204 return NULL;
1205 }
1206#endif
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001207
Paul Cercueil2e38bbe2014-03-19 15:27:15 +01001208 memset(&hints, 0, sizeof(hints));
1209 hints.ai_family = AF_UNSPEC;
1210 hints.ai_socktype = SOCK_STREAM;
Paul Cercueilab114932014-05-19 13:03:17 +02001211
1212#ifdef HAVE_AVAHI
1213 if (!host) {
1214 char addr_str[AVAHI_ADDRESS_STR_MAX];
1215 char port_str[6];
1216 AvahiAddress address;
Paul Cercueil0f729d32014-06-05 17:38:54 +02001217 uint16_t port = IIOD_PORT;
Paul Cercueilab114932014-05-19 13:03:17 +02001218
1219 memset(&address, 0, sizeof(address));
1220
1221 ret = discover_host(&address, &port);
1222 if (ret < 0) {
1223 ERROR("Unable to find host: %s\n", strerror(-ret));
1224 return NULL;
1225 }
1226
1227 avahi_address_snprint(addr_str, sizeof(addr_str), &address);
1228 snprintf(port_str, sizeof(port_str), "%hu", port);
1229 ret = getaddrinfo(addr_str, port_str, &hints, &res);
1230 } else
1231#endif
1232 {
1233 ret = getaddrinfo(host, IIOD_PORT_STR, &hints, &res);
1234 }
1235
Paul Cercueil2e38bbe2014-03-19 15:27:15 +01001236 if (ret) {
Paul Cercueilab114932014-05-19 13:03:17 +02001237 ERROR("Unable to find host: %s\n", gai_strerror(ret));
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001238 return NULL;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001239 }
1240
Paul Cercueil4970ac32015-02-24 10:59:00 +01001241 fd = create_socket(res);
1242 if (fd < 0)
Paul Cercueil2dd2c132015-02-24 10:41:01 +01001243 goto err_free_addrinfo;
Paul Cercueilbca3dbc2014-06-11 12:00:21 +02001244
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001245 pdata = calloc(1, sizeof(*pdata));
1246 if (!pdata) {
1247 ERROR("Unable to allocate memory\n");
1248 goto err_close_socket;
1249 }
1250
1251 pdata->fd = fd;
Paul Cercueil2dd2c132015-02-24 10:41:01 +01001252 pdata->addrinfo = res;
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001253
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001254 DEBUG("Creating context...\n");
1255 ctx = get_context(fd);
1256 if (!ctx)
Paul Cercueild3d56ee2015-01-08 16:36:31 +01001257 goto err_free_pdata;
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001258
Paul Cercueil2057fd32014-10-28 14:44:19 +01001259 /* Override the name and low-level functions of the XML context
1260 * with those corresponding to the network context */
1261 ctx->name = "network";
1262 ctx->ops = &network_ops;
1263 ctx->pdata = pdata;
1264
Paul Cercueil06c479d2015-01-08 16:14:57 +01001265#ifdef HAVE_IPV6
Paul Cercueiled15e492015-01-12 15:52:28 +01001266 len = INET6_ADDRSTRLEN + IF_NAMESIZE + 2;
Paul Cercueil06c479d2015-01-08 16:14:57 +01001267#else
1268 len = INET_ADDRSTRLEN + 1;
1269#endif
1270 ctx->description = malloc(len);
1271 if (!ctx->description) {
1272 ERROR("Unable to allocate memory\n");
1273 goto err_network_shutdown;
1274 }
1275
1276 ctx->description[0] = '\0';
1277
1278#ifdef HAVE_IPV6
1279 if (res->ai_family == AF_INET6) {
1280 struct sockaddr_in6 *in = (struct sockaddr_in6 *) res->ai_addr;
Paul Cercueiled15e492015-01-12 15:52:28 +01001281 char *ptr;
Paul Cercueil06c479d2015-01-08 16:14:57 +01001282 inet_ntop(AF_INET6, &in->sin6_addr,
1283 ctx->description, INET6_ADDRSTRLEN);
Paul Cercueiled15e492015-01-12 15:52:28 +01001284
1285 ptr = if_indextoname(in->sin6_scope_id, ctx->description +
1286 strlen(ctx->description) + 1);
1287 if (!ptr) {
1288 ERROR("Unable to lookup interface of IPv6 address\n");
1289 goto err_network_shutdown;
1290 }
1291
1292 *(ptr - 1) = '%';
Paul Cercueil06c479d2015-01-08 16:14:57 +01001293 }
1294#endif
1295 if (res->ai_family == AF_INET) {
1296 struct sockaddr_in *in = (struct sockaddr_in *) res->ai_addr;
1297 inet_ntop(AF_INET, &in->sin_addr,
1298 ctx->description, INET_ADDRSTRLEN);
1299 }
1300
Paul Cercueil44ae11c2014-03-17 09:56:29 +01001301 for (i = 0; i < ctx->nb_devices; i++) {
1302 struct iio_device *dev = ctx->devices[i];
Paul Cercueilff778232014-03-24 14:23:08 +01001303
1304 dev->words = (dev->nb_channels + 31) / 32;
1305 if (dev->words) {
Paul Cercueil439e1a22015-02-24 13:50:51 +01001306 dev->mask = calloc(dev->words, sizeof(*dev->mask));
1307 if (!dev->mask) {
Paul Cercueil2057fd32014-10-28 14:44:19 +01001308 ERROR("Unable to allocate memory\n");
Paul Cercueilff778232014-03-24 14:23:08 +01001309 goto err_network_shutdown;
Paul Cercueil2057fd32014-10-28 14:44:19 +01001310 }
Paul Cercueilff778232014-03-24 14:23:08 +01001311 }
1312
Paul Cercueil439e1a22015-02-24 13:50:51 +01001313 dev->pdata = calloc(1, sizeof(*dev->pdata));
1314 if (!dev->pdata) {
1315 ERROR("Unable to allocate memory\n");
1316 goto err_network_shutdown;
1317 }
1318
1319 dev->pdata->fd = -1;
Paul Cercueil09a43482015-03-04 15:50:27 +01001320 dev->pdata->memfd = -1;
Paul Cercueilc5b00752015-02-24 14:29:53 +01001321
1322#if HAVE_PTHREAD
1323 ret = pthread_mutex_init(&dev->pdata->lock, NULL);
1324 if (ret < 0) {
1325 char buf[1024];
1326 strerror_r(-ret, buf, sizeof(buf));
1327 ERROR("Unable to initialize mutex: %s\n", buf);
1328 goto err_network_shutdown;
1329 }
1330#endif
Paul Cercueil44ae11c2014-03-17 09:56:29 +01001331 }
1332
Paul Cercueilc1ed8482014-06-11 16:29:43 +02001333 iio_context_init(ctx);
Paul Cercueil4c6729d2014-04-04 17:24:41 +02001334
Paul Cercueil05e26262014-05-09 11:32:43 +02001335#if HAVE_PTHREAD
Paul Cercueil1494b862014-05-05 12:49:07 +02001336 ret = pthread_mutex_init(&pdata->lock, NULL);
1337 if (ret < 0) {
1338 char buf[1024];
1339 strerror_r(-ret, buf, sizeof(buf));
1340 ERROR("Unable to initialize mutex: %s\n", buf);
1341 goto err_network_shutdown;
1342 }
Paul Cercueil05e26262014-05-09 11:32:43 +02001343#endif
Paul Cercueil1494b862014-05-05 12:49:07 +02001344
Paul Cercueilbca3dbc2014-06-11 12:00:21 +02001345 set_remote_timeout(ctx, calculate_remote_timeout(DEFAULT_TIMEOUT_MS));
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001346 return ctx;
1347
Paul Cercueil44ae11c2014-03-17 09:56:29 +01001348err_network_shutdown:
Paul Cercueil44ae11c2014-03-17 09:56:29 +01001349 iio_context_destroy(ctx);
Paul Cercueil2057fd32014-10-28 14:44:19 +01001350 return NULL;
Paul Cercueil8fd9c8f2014-03-03 17:21:31 +01001351err_free_pdata:
1352 free(pdata);
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001353err_close_socket:
1354 close(fd);
Paul Cercueil2dd2c132015-02-24 10:41:01 +01001355err_free_addrinfo:
1356 freeaddrinfo(res);
Paul Cercueilc7bad0f2014-03-03 17:06:48 +01001357 return NULL;
1358}