]> sigrok.org Git - libsigrok.git/blame - src/hardware/appa-55ii/api.c
Pass driver struct pointer to driver callbacks.
[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
5e7a8e57
AJ
89 drvc = di->priv;
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
AJ
124{
125 return ((struct drv_context *)(di->priv))->instances;
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);
177 sr_dbg("Setting sample limit to %" PRIu64 ".", devc->limit_samples);
178 break;
179 case SR_CONF_LIMIT_MSEC:
180 devc->limit_msec = g_variant_get_uint64(data);
181 sr_dbg("Setting time limit to %" PRIu64 "ms.", devc->limit_msec);
182 break;
183 case SR_CONF_DATA_SOURCE: {
7574e58c
BV
184 tmp_str = g_variant_get_string(data, NULL);
185 for (i = 0; i < ARRAY_SIZE(data_sources); i++)
81a9ab72
AJ
186 if (!strcmp(tmp_str, data_sources[i])) {
187 devc->data_source = i;
188 break;
189 }
190 if (i == ARRAY_SIZE(data_sources))
191 return SR_ERR;
192 break;
193 }
5e7a8e57 194 default:
81a9ab72 195 return SR_ERR_NA;
5e7a8e57
AJ
196 }
197
81a9ab72 198 return SR_OK;
5e7a8e57
AJ
199}
200
584560f1 201static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 202 const struct sr_channel_group *cg)
5e7a8e57 203{
5e7a8e57 204 (void)sdi;
53b4680f 205 (void)cg;
5e7a8e57 206
5e7a8e57 207 switch (key) {
81a9ab72 208 case SR_CONF_SCAN_OPTIONS:
584560f1 209 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
a0e0bb41 210 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
81a9ab72
AJ
211 break;
212 case SR_CONF_DEVICE_OPTIONS:
42a47a9a
BV
213 if (!sdi)
214 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
215 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
216 else
217 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
218 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
81a9ab72
AJ
219 break;
220 case SR_CONF_DATA_SOURCE:
221 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
222 break;
5e7a8e57
AJ
223 default:
224 return SR_ERR_NA;
225 }
226
81a9ab72 227 return SR_OK;
5e7a8e57
AJ
228}
229
230static int dev_acquisition_start(const struct sr_dev_inst *sdi,
7574e58c 231 void *cb_data)
5e7a8e57 232{
7574e58c 233 struct sr_serial_dev_inst *serial;
81a9ab72 234 struct dev_context *devc;
5e7a8e57 235
7574e58c 236 serial = sdi->conn;
5e7a8e57
AJ
237 if (sdi->status != SR_ST_ACTIVE)
238 return SR_ERR_DEV_CLOSED;
239
81a9ab72
AJ
240 if (!(devc = sdi->priv)) {
241 sr_err("sdi->priv was NULL.");
242 return SR_ERR_BUG;
243 }
5e7a8e57 244
81a9ab72 245 devc->session_cb_data = cb_data;
5e7a8e57 246
81a9ab72
AJ
247 /*
248 * Reset the number of samples to take. If we've already collected our
249 * quota, but we start a new session, and don't reset this, we'll just
250 * quit without acquiring any new samples.
251 */
252 devc->num_samples = 0;
253 devc->start_time = g_get_monotonic_time();
5e7a8e57 254
81a9ab72
AJ
255 /* Send header packet to the session bus. */
256 std_session_send_df_header(cb_data, LOG_PREFIX);
5e7a8e57 257
81a9ab72 258 /* Poll every 50ms, or whenever some data comes in. */
102f1239
BV
259 serial_source_add(sdi->session, serial, G_IO_IN, 50,
260 appa_55ii_receive_data, (void *)sdi);
5e7a8e57
AJ
261
262 return SR_OK;
263}
264
81a9ab72
AJ
265static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
266{
76b4d4f4
UH
267 return std_serial_dev_acquisition_stop(sdi, cb_data,
268 std_serial_dev_close, sdi->conn, LOG_PREFIX);
81a9ab72
AJ
269}
270
5e7a8e57
AJ
271SR_PRIV struct sr_dev_driver appa_55ii_driver_info = {
272 .name = "appa-55ii",
273 .longname = "APPA 55II",
274 .api_version = 1,
275 .init = init,
276 .cleanup = cleanup,
277 .scan = scan,
278 .dev_list = dev_list,
a6630742 279 .dev_clear = NULL,
5e7a8e57
AJ
280 .config_get = config_get,
281 .config_set = config_set,
282 .config_list = config_list,
81a9ab72
AJ
283 .dev_open = std_serial_dev_open,
284 .dev_close = std_serial_dev_close,
5e7a8e57
AJ
285 .dev_acquisition_start = dev_acquisition_start,
286 .dev_acquisition_stop = dev_acquisition_stop,
76b4d4f4 287 .priv = NULL,
5e7a8e57 288};