1 /*
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
19 #ifndef __OPS_H__
20 #define __OPS_H__
21
22 #include "../iio-private.h"
23 #include "queue.h"
24
25 #include <endian.h>
26 #include <errno.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <poll.h>
31 #include <sys/socket.h>
32 #include <unistd.h>
33
34 #if WITH_AIO
35 #include <libaio.h>
36 #endif
37
38 #ifndef __bswap_constant_16
39 #define __bswap_constant_16(x) \
40 ((unsigned short int) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
41 #endif
42
43 #ifndef __bswap_constant_32
44 #define __bswap_constant_32(x) \
45 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
46 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
47 #endif
48
49 struct thread_pool;
50 extern struct thread_pool *main_thread_pool;
51
52 struct parser_pdata {
53 struct iio_context *ctx;
54 bool stop, verbose;
55 int fd_in, fd_out;
56
57 SLIST_HEAD(ParserDataThdHead, ThdEntry) thdlist_head;
58
59 /* Used as temporaries placements by the lexer */
60 struct iio_device *dev;
61 struct iio_channel *chn;
62 bool channel_is_output;
63 bool fd_in_is_socket, fd_out_is_socket;
64 #if WITH_AIO
65 io_context_t aio_ctx;
66 int aio_eventfd;
67 pthread_mutex_t aio_mutex;
68 #endif
69 struct thread_pool *pool;
70
71 ssize_t (*writefd)(struct parser_pdata *pdata, const void *buf, size_t len);
72 ssize_t (*readfd)(struct parser_pdata *pdata, void *buf, size_t len);
73 };
74
75 extern bool server_demux; /* Defined in iiod.c */
76
77 void interpreter(struct iio_context *ctx, int fd_in, int fd_out, bool verbose,
78 bool is_socket, bool use_aio, struct thread_pool *pool);
79
80 int start_usb_daemon(struct iio_context *ctx, const char *ffs,
81 bool debug, bool use_aio, unsigned int nb_pipes,
82 struct thread_pool *pool);
83
84 int open_dev(struct parser_pdata *pdata, struct iio_device *dev,
85 size_t samples_count, const char *mask, bool cyclic);
86 int close_dev(struct parser_pdata *pdata, struct iio_device *dev);
87
88 ssize_t rw_dev(struct parser_pdata *pdata, struct iio_device *dev,
89 unsigned int nb, bool is_write);
90
91 ssize_t read_dev_attr(struct parser_pdata *pdata, struct iio_device *dev,
92 const char *attr, enum iio_attr_type type);
93 ssize_t write_dev_attr(struct parser_pdata *pdata, struct iio_device *dev,
94 const char *attr, size_t len, enum iio_attr_type type);
95
96 ssize_t read_chn_attr(struct parser_pdata *pdata, struct iio_channel *chn,
97 const char *attr);
98 ssize_t write_chn_attr(struct parser_pdata *pdata, struct iio_channel *chn,
99 const char *attr, size_t len);
100
101 ssize_t get_trigger(struct parser_pdata *pdata, struct iio_device *dev);
102 ssize_t set_trigger(struct parser_pdata *pdata,
103 struct iio_device *dev, const char *trig);
104
105 int set_timeout(struct parser_pdata *pdata, unsigned int timeout);
106 int set_buffers_count(struct parser_pdata *pdata,
107 struct iio_device *dev, long value);
108
109 ssize_t read_line(struct parser_pdata *pdata, char *buf, size_t len);
110 ssize_t write_all(struct parser_pdata *pdata, const void *src, size_t len);
111
output(struct parser_pdata * pdata,const char * text)112 static __inline__ void output(struct parser_pdata *pdata, const char *text)
113 {
114 if (write_all(pdata, text, strlen(text)) <= 0)
115 pdata->stop = true;
116 }
117
poll_nointr(struct pollfd * pfd,unsigned int num_pfd)118 static __inline__ int poll_nointr(struct pollfd *pfd, unsigned int num_pfd)
119 {
120 int ret;
121
122 do {
123 ret = poll(pfd, num_pfd, -1);
124 } while (ret == -1 && errno == EINTR);
125
126 return ret;
127 }
128
129 #endif /* __OPS_H__ */
130