-
Notifications
You must be signed in to change notification settings - Fork 9
/
example.c
164 lines (140 loc) · 4.62 KB
/
example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* libtinyiiod - Tiny IIO Daemon Library
*
* Copyright (C) 2016 Analog Devices, Inc.
* Author: Paul Cercueil <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#include "tinyiiod.h"
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
static ssize_t read_data(char *buf, size_t len)
{
return fread(buf, 1, len, stdin);
}
static ssize_t write_data(const char *buf, size_t len)
{
return fwrite(buf, 1, len, stdout);
}
static ssize_t read_attr(const char *device, const char *attr,
char *buf, size_t len, enum iio_attr_type type)
{
if (!strcmp(device, "adc") || !strcmp(device, "0")) {
switch (type) {
case IIO_ATTR_TYPE_DEVICE:
return (ssize_t) snprintf(buf, len, "1000");
case IIO_ATTR_TYPE_DEBUG:
return (ssize_t) snprintf(buf, len, "0");
case IIO_ATTR_TYPE_BUFFER:
return (ssize_t) snprintf(buf, len, "8");
default:
return -ENOENT;
}
}
return -ENOENT;
}
static ssize_t write_attr(const char *device, const char *attr,
const char *buf, size_t len, enum iio_attr_type type)
{
return -ENOSYS;
}
static ssize_t ch_read_attr(const char *device, const char *channel,
bool ch_out, const char *attr, char *buf, size_t len)
{
if (!strcmp(device, "adc") || !strcmp(device, "0")) {
if (ch_out)
return -ENOENT;
if (!strcmp(channel, "voltage0")) {
if (!strcmp(attr, "scale"))
return (ssize_t) snprintf(buf, len, "0.033");
else if (!strcmp(attr, "raw"))
return (ssize_t) snprintf(buf, len, "256");
} else if (!strcmp(channel, "voltage1")) {
if (!strcmp(attr, "scale"))
return (ssize_t) snprintf(buf, len, "0.033");
else if (!strcmp(attr, "raw"))
return (ssize_t) snprintf(buf, len, "128");
}
}
return -ENOENT;
}
static ssize_t ch_write_attr(const char *device, const char *channel,
bool ch_out, const char *attr, const char *buf, size_t len)
{
return -ENOSYS;
}
static const char * const xml =
"<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE context [<!ELEMENT context "
"(device)*><!ELEMENT device (channel | attribute | debug-attribute | buffer-attribute)*><!ELEMENT "
"channel (scan-element?, attribute*)><!ELEMENT attribute EMPTY><!ELEMENT "
"scan-element EMPTY><!ELEMENT debug-attribute EMPTY><!ELEMENT buffer-attribute EMPTY><!ATTLIST context name "
"CDATA #REQUIRED description CDATA #IMPLIED><!ATTLIST device id CDATA "
"#REQUIRED name CDATA #IMPLIED><!ATTLIST channel id CDATA #REQUIRED type "
"(input|output) #REQUIRED name CDATA #IMPLIED><!ATTLIST scan-element index "
"CDATA #REQUIRED format CDATA #REQUIRED scale CDATA #IMPLIED><!ATTLIST "
"attribute name CDATA #REQUIRED filename CDATA #IMPLIED><!ATTLIST "
"debug-attribute name CDATA #REQUIRED><!ATTLIST buffer-attribute name "
"CDATA #REQUIRED value CDATA #IMPLIED>]><context name=\"tiny\" "
"description=\"Tiny IIOD\" >"
"<device id=\"0\" name=\"adc\" >"
"<channel id=\"voltage0\" type=\"input\" >"
"<attribute name=\"scale\" /><attribute name=\"raw\" /></channel>"
"<channel id=\"voltage1\" type=\"input\" >"
"<attribute name=\"scale\" /><attribute name=\"raw\" /></channel>"
"<attribute name=\"sample_rate\" />"
"<debug-attribute name=\"direct_reg_access\" />"
"<buffer-attribute name=\"length_align_bytes\" />"
"</device></context>";
static ssize_t get_xml(char **outxml)
{
*outxml = calloc(1, strlen(xml) + 1);
if (!(*outxml))
return -ENOMEM;
memcpy(*outxml, xml, strlen(xml));
return 0;
}
static struct tinyiiod_ops ops = {
.read = read_data,
.write = write_data,
.read_attr = read_attr,
.write_attr = write_attr,
.ch_read_attr = ch_read_attr,
.ch_write_attr = ch_write_attr,
.get_xml = get_xml,
};
static bool stop;
static void set_handler(int32_t signal_nb, void (*handler)(int32_t))
{
struct sigaction sig;
sigaction(signal_nb, NULL, &sig);
sig.sa_handler = handler;
sigaction(signal_nb, &sig, NULL);
}
static void quit_all(int32_t sig)
{
stop = true;
}
int32_t main(void)
{
struct tinyiiod *iiod = tinyiiod_create(&ops);
set_handler(SIGHUP, &quit_all);
set_handler(SIGPIPE, &quit_all);
set_handler(SIGINT, &quit_all);
set_handler(SIGSEGV, &quit_all);
set_handler(SIGTERM, &quit_all);
while (!stop)
tinyiiod_read_command(iiod);
tinyiiod_destroy(iiod);
return 0;
}