]> sigrok.org Git - libsigrok.git/blame - src/hardware/appa-55ii/api.c
Fix #442 by renaming sr_dev_driver.priv to .context
[libsigrok.git] / src / hardware / appa-55ii / api.c
CommitLineData
5e7a8e57
AJ
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Aurelien Jacobs <aurel@gnuage.org>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
81a9ab72 20#include <string.h>
5e7a8e57
AJ
21#include "protocol.h"
22
a0e0bb41 23static const uint32_t scanopts[] = {
81a9ab72
AJ
24 SR_CONF_CONN,
25 SR_CONF_SERIALCOMM,
26};
27
42a47a9a 28static const uint32_t drvopts[] = {
81a9ab72 29 SR_CONF_THERMOMETER,
42a47a9a
BV
30};
31
32static const uint32_t devopts[] = {
33 SR_CONF_CONTINUOUS | SR_CONF_SET,
5827f61b
BV
34 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
35 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
36 SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
81a9ab72
AJ
37};
38
39static const char *data_sources[] = {
40 "Live",
41 "Memory",
42};
43
5e7a8e57 44SR_PRIV struct sr_dev_driver appa_55ii_driver_info;
5e7a8e57 45
4f840ce9 46static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
5e7a8e57
AJ
47{
48 return std_init(sr_ctx, di, LOG_PREFIX);
49}
50
4f840ce9 51static GSList *scan(struct sr_dev_driver *di, GSList *options)
5e7a8e57
AJ
52{
53 struct drv_context *drvc;
81a9ab72
AJ
54 struct dev_context *devc;
55 struct sr_serial_dev_inst *serial;
56 struct sr_dev_inst *sdi;
7574e58c
BV
57 struct sr_config *src;
58 GSList *devices, *l;
59 const char *conn, *serialcomm;
81a9ab72 60 uint8_t buf[50];
7574e58c 61 size_t len;
81a9ab72 62
7574e58c
BV
63 len = sizeof(buf);
64 devices = NULL;
65 conn = serialcomm = NULL;
81a9ab72 66 for (l = options; l; l = l->next) {
7574e58c 67 src = l->data;
81a9ab72
AJ
68 switch (src->key) {
69 case SR_CONF_CONN:
70 conn = g_variant_get_string(src->data, NULL);
71 break;
72 case SR_CONF_SERIALCOMM:
73 serialcomm = g_variant_get_string(src->data, NULL);
74 break;
75 }
76 }
77 if (!conn)
78 return NULL;
79 if (!serialcomm)
80 serialcomm = "9600/8n1";
81
91219afc
UH
82 serial = sr_serial_dev_inst_new(conn, serialcomm);
83
25dd0831 84 if (serial_open(serial, SERIAL_RDONLY) != SR_OK)
81a9ab72 85 return NULL;
5e7a8e57 86
81a9ab72 87 sr_info("Probing serial port %s.", conn);
5e7a8e57 88
41812aca 89 drvc = di->context;
5e7a8e57 90 drvc->instances = NULL;
81a9ab72
AJ
91 serial_flush(serial);
92
93 /* Let's get a bit of data and see if we can find a packet. */
94 if (serial_stream_detect(serial, buf, &len, 25,
7574e58c 95 appa_55ii_packet_valid, 500, 9600) != SR_OK)
81a9ab72
AJ
96 goto scan_cleanup;
97
98 sr_info("Found device on port %s.", conn);
99
aac29cc1 100 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
101 sdi->status = SR_ST_INACTIVE;
102 sdi->vendor = g_strdup("APPA");
103 sdi->model = g_strdup("55II");
f57d8ffe 104 devc = g_malloc0(sizeof(struct dev_context));
81a9ab72 105 devc->data_source = DEFAULT_DATA_SOURCE;
81a9ab72
AJ
106 sdi->inst_type = SR_INST_SERIAL;
107 sdi->conn = serial;
108 sdi->priv = devc;
109 sdi->driver = di;
5e7a8e57 110
5e23fcab
ML
111 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "T1");
112 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "T2");
81a9ab72
AJ
113
114 drvc->instances = g_slist_append(drvc->instances, sdi);
115 devices = g_slist_append(devices, sdi);
116
117scan_cleanup:
118 serial_close(serial);
5e7a8e57
AJ
119
120 return devices;
121}
122
4f840ce9 123static GSList *dev_list(const struct sr_dev_driver *di)
5e7a8e57 124{
41812aca 125 return ((struct drv_context *)(di->context))->instances;
5e7a8e57
AJ
126}
127
4f840ce9 128static int cleanup(const struct sr_dev_driver *di)
5e7a8e57 129{
a6630742 130 return std_dev_clear(di, NULL);
5e7a8e57
AJ
131}
132
584560f1 133static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 134 const struct sr_channel_group *cg)
5e7a8e57 135{
81a9ab72 136 struct dev_context *devc = sdi->priv;
5e7a8e57 137
53b4680f 138 (void)cg;
5e7a8e57 139
5e7a8e57 140 switch (key) {
81a9ab72
AJ
141 case SR_CONF_LIMIT_SAMPLES:
142 *data = g_variant_new_uint64(devc->limit_samples);
143 break;
144 case SR_CONF_LIMIT_MSEC:
145 *data = g_variant_new_uint64(devc->limit_msec);
146 break;
147 case SR_CONF_DATA_SOURCE:
148 *data = g_variant_new_string(data_sources[devc->data_source]);
149 break;
5e7a8e57
AJ
150 default:
151 return SR_ERR_NA;
152 }
153
81a9ab72 154 return SR_OK;
5e7a8e57
AJ
155}
156
584560f1 157static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 158 const struct sr_channel_group *cg)
5e7a8e57 159{
81a9ab72 160 struct dev_context *devc;
7574e58c
BV
161 const char *tmp_str;
162 unsigned int i;
5e7a8e57 163
53b4680f 164 (void)cg;
5e7a8e57
AJ
165
166 if (sdi->status != SR_ST_ACTIVE)
167 return SR_ERR_DEV_CLOSED;
168
81a9ab72
AJ
169 if (!(devc = sdi->priv)) {
170 sr_err("sdi->priv was NULL.");
171 return SR_ERR_BUG;
172 }
173
5e7a8e57 174 switch (key) {
81a9ab72
AJ
175 case SR_CONF_LIMIT_SAMPLES:
176 devc->limit_samples = g_variant_get_uint64(data);
81a9ab72
AJ
177 break;
178 case SR_CONF_LIMIT_MSEC:
179 devc->limit_msec = g_variant_get_uint64(data);
81a9ab72
AJ
180 break;
181 case SR_CONF_DATA_SOURCE: {
7574e58c
BV
182 tmp_str = g_variant_get_string(data, NULL);
183 for (i = 0; i < ARRAY_SIZE(data_sources); i++)
81a9ab72
AJ
184 if (!strcmp(tmp_str, data_sources[i])) {
185 devc->data_source = i;
186 break;
187 }
188 if (i == ARRAY_SIZE(data_sources))
189 return SR_ERR;
190 break;
191 }
5e7a8e57 192 default:
81a9ab72 193 return SR_ERR_NA;
5e7a8e57
AJ
194 }
195
81a9ab72 196 return SR_OK;
5e7a8e57
AJ
197}
198
584560f1 199static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 200 const struct sr_channel_group *cg)
5e7a8e57 201{
53b4680f 202 (void)cg;
5e7a8e57 203
5e7a8e57 204 switch (key) {
81a9ab72 205 case SR_CONF_SCAN_OPTIONS:
584560f1 206 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
a0e0bb41 207 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
81a9ab72
AJ
208 break;
209 case SR_CONF_DEVICE_OPTIONS:
42a47a9a
BV
210 if (!sdi)
211 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
212 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
213 else
214 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
215 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
81a9ab72
AJ
216 break;
217 case SR_CONF_DATA_SOURCE:
218 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
219 break;
5e7a8e57
AJ
220 default:
221 return SR_ERR_NA;
222 }
223
81a9ab72 224 return SR_OK;
5e7a8e57
AJ
225}
226
227static int dev_acquisition_start(const struct sr_dev_inst *sdi,
7574e58c 228 void *cb_data)
5e7a8e57 229{
7574e58c 230 struct sr_serial_dev_inst *serial;
81a9ab72 231 struct dev_context *devc;
5e7a8e57 232
7574e58c 233 serial = sdi->conn;
5e7a8e57
AJ
234 if (sdi->status != SR_ST_ACTIVE)
235 return SR_ERR_DEV_CLOSED;
236
81a9ab72
AJ
237 if (!(devc = sdi->priv)) {
238 sr_err("sdi->priv was NULL.");
239 return SR_ERR_BUG;
240 }
5e7a8e57 241
81a9ab72 242 devc->session_cb_data = cb_data;
5e7a8e57 243
81a9ab72
AJ
244 /*
245 * Reset the number of samples to take. If we've already collected our
246 * quota, but we start a new session, and don't reset this, we'll just
247 * quit without acquiring any new samples.
248 */
249 devc->num_samples = 0;
250 devc->start_time = g_get_monotonic_time();
5e7a8e57 251
81a9ab72
AJ
252 /* Send header packet to the session bus. */
253 std_session_send_df_header(cb_data, LOG_PREFIX);
5e7a8e57 254
81a9ab72 255 /* Poll every 50ms, or whenever some data comes in. */
102f1239
BV
256 serial_source_add(sdi->session, serial, G_IO_IN, 50,
257 appa_55ii_receive_data, (void *)sdi);
5e7a8e57
AJ
258
259 return SR_OK;
260}
261
81a9ab72
AJ
262static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
263{
76b4d4f4
UH
264 return std_serial_dev_acquisition_stop(sdi, cb_data,
265 std_serial_dev_close, sdi->conn, LOG_PREFIX);
81a9ab72
AJ
266}
267
5e7a8e57
AJ
268SR_PRIV struct sr_dev_driver appa_55ii_driver_info = {
269 .name = "appa-55ii",
270 .longname = "APPA 55II",
271 .api_version = 1,
272 .init = init,
273 .cleanup = cleanup,
274 .scan = scan,
275 .dev_list = dev_list,
a6630742 276 .dev_clear = NULL,
5e7a8e57
AJ
277 .config_get = config_get,
278 .config_set = config_set,
279 .config_list = config_list,
81a9ab72
AJ
280 .dev_open = std_serial_dev_open,
281 .dev_close = std_serial_dev_close,
5e7a8e57
AJ
282 .dev_acquisition_start = dev_acquisition_start,
283 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 284 .context = NULL,
5e7a8e57 285};