]> sigrok.org Git - libsigrok.git/blame - src/hardware/cem-dt-885x/api.c
hameg-hmo: add one missing g_free
[libsigrok.git] / src / hardware / cem-dt-885x / api.c
CommitLineData
8fa9368e
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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
6ec6c43b 20#include <config.h>
be733919 21#include <string.h>
8fa9368e
BV
22#include "protocol.h"
23
7fb8279c 24#define SERIALCOMM "9600/8n1"
1beccaed 25
7fb8279c 26/* 23ms is the longest interval between tokens. */
1a46cc62 27#define MAX_SCAN_TIME_US (25 * 1000)
7fb8279c 28
a0e0bb41 29static const uint32_t scanopts[] = {
7fb8279c
BV
30 SR_CONF_CONN,
31};
32
023c73ae 33static const uint32_t drvopts[] = {
7fb8279c 34 SR_CONF_SOUNDLEVELMETER,
d7125bfa
BV
35};
36
023c73ae 37static const uint32_t devopts[] = {
e91bb0a6 38 SR_CONF_CONTINUOUS,
023c73ae 39 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
5827f61b
BV
40 SR_CONF_SPL_WEIGHT_FREQ | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
41 SR_CONF_SPL_WEIGHT_TIME | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
42 SR_CONF_SPL_MEASUREMENT_RANGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
43 SR_CONF_DATALOG | SR_CONF_GET | SR_CONF_SET,
44 SR_CONF_HOLD_MAX | SR_CONF_GET | SR_CONF_SET,
45 SR_CONF_HOLD_MIN | SR_CONF_GET | SR_CONF_SET,
46 SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET,
47 SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
7fb8279c
BV
48};
49
be733919
BV
50static const char *weight_freq[] = {
51 "A",
52 "C",
53};
7fb8279c 54
1487ce4f
BV
55static const char *weight_time[] = {
56 "F",
57 "S",
58};
59
f157b2ee
BV
60static const uint64_t meas_ranges[][2] = {
61 { 30, 130 },
62 { 30, 80 },
63 { 50, 100 },
64 { 80, 130 },
65};
66
662172d4
BV
67static const char *data_sources[] = {
68 "Live",
69 "Memory",
70};
8fa9368e 71
1beccaed 72SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info;
8fa9368e 73
4f840ce9 74static GSList *scan(struct sr_dev_driver *di, GSList *options)
8fa9368e
BV
75{
76 struct drv_context *drvc;
7fb8279c
BV
77 struct dev_context *devc;
78 struct sr_config *src;
79 struct sr_serial_dev_inst *serial;
80 struct sr_dev_inst *sdi;
7fb8279c
BV
81 GSList *l, *devices;
82 gint64 start;
83 const char *conn;
84 unsigned char c;
85
86 conn = NULL;
87 for (l = options; l; l = l->next) {
88 src = l->data;
89 if (src->key == SR_CONF_CONN)
90 conn = g_variant_get_string(src->data, NULL);
91 }
92 if (!conn)
93 return NULL;
94
91219afc 95 serial = sr_serial_dev_inst_new(conn, SERIALCOMM);
8fa9368e 96
d7b269da 97 if (serial_open(serial, SERIAL_RDONLY) != SR_OK)
7fb8279c 98 return NULL;
8fa9368e
BV
99
100 devices = NULL;
41812aca 101 drvc = di->context;
7fb8279c 102 start = g_get_monotonic_time();
1a46cc62 103 while (g_get_monotonic_time() - start < MAX_SCAN_TIME_US) {
d7b269da 104 if (serial_read_nonblocking(serial, &c, 1) == 1 && c == 0xa5) {
7fb8279c 105 /* Found one. */
aac29cc1 106 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
107 sdi->status = SR_ST_INACTIVE;
108 sdi->vendor = g_strdup("CEM");
109 sdi->model = g_strdup("DT-885x");
f57d8ffe 110 devc = g_malloc0(sizeof(struct dev_context));
e1af0e85
BV
111 devc->cur_mqflags = 0;
112 devc->recording = -1;
f157b2ee 113 devc->cur_meas_range = 0;
662172d4 114 devc->cur_data_source = DATA_SOURCE_LIVE;
cea26f6e 115 devc->enable_data_source_memory = FALSE;
91219afc 116 sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM);
7fb8279c
BV
117 sdi->inst_type = SR_INST_SERIAL;
118 sdi->priv = devc;
119 sdi->driver = di;
5e23fcab 120 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "SPL");
7fb8279c
BV
121 drvc->instances = g_slist_append(drvc->instances, sdi);
122 devices = g_slist_append(devices, sdi);
123 break;
124 }
125 /* It takes about 1ms for a byte to come in. */
126 g_usleep(1000);
127 }
8fa9368e 128
7fb8279c 129 serial_close(serial);
8fa9368e
BV
130
131 return devices;
132}
133
584560f1 134static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 135 const struct sr_channel_group *cg)
8fa9368e 136{
7fb8279c 137 struct dev_context *devc;
f157b2ee
BV
138 GVariant *range[2];
139 uint64_t low, high;
a90e480c 140 int tmp, ret;
8fa9368e 141
53b4680f 142 (void)cg;
d3c74a6f 143
7fb8279c
BV
144 if (!sdi)
145 return SR_ERR_ARG;
8fa9368e 146
7fb8279c 147 devc = sdi->priv;
a90e480c 148 ret = SR_OK;
8fa9368e 149 switch (key) {
7fb8279c
BV
150 case SR_CONF_LIMIT_SAMPLES:
151 *data = g_variant_new_uint64(devc->limit_samples);
152 break;
e1af0e85 153 case SR_CONF_DATALOG:
0cd9107d
BV
154 if ((ret = cem_dt_885x_recording_get(sdi, &tmp)) == SR_OK)
155 *data = g_variant_new_boolean(tmp);
e1af0e85 156 break;
be733919
BV
157 case SR_CONF_SPL_WEIGHT_FREQ:
158 tmp = cem_dt_885x_weight_freq_get(sdi);
159 if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A)
160 *data = g_variant_new_string("A");
161 else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C)
162 *data = g_variant_new_string("C");
163 else
164 return SR_ERR;
165 break;
1487ce4f
BV
166 case SR_CONF_SPL_WEIGHT_TIME:
167 tmp = cem_dt_885x_weight_time_get(sdi);
168 if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_F)
169 *data = g_variant_new_string("F");
170 else if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_S)
171 *data = g_variant_new_string("S");
172 else
173 return SR_ERR;
174 break;
a90e480c
BV
175 case SR_CONF_HOLD_MAX:
176 if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK)
177 *data = g_variant_new_boolean(tmp == SR_MQFLAG_MAX);
178 break;
179 case SR_CONF_HOLD_MIN:
180 if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK)
181 *data = g_variant_new_boolean(tmp == SR_MQFLAG_MIN);
182 break;
f157b2ee
BV
183 case SR_CONF_SPL_MEASUREMENT_RANGE:
184 if ((ret = cem_dt_885x_meas_range_get(sdi, &low, &high)) == SR_OK) {
185 range[0] = g_variant_new_uint64(low);
186 range[1] = g_variant_new_uint64(high);
187 *data = g_variant_new_tuple(range, 2);
188 }
189 break;
4c22355f
BV
190 case SR_CONF_POWER_OFF:
191 *data = g_variant_new_boolean(FALSE);
192 break;
662172d4
BV
193 case SR_CONF_DATA_SOURCE:
194 if (devc->cur_data_source == DATA_SOURCE_LIVE)
195 *data = g_variant_new_string("Live");
196 else
197 *data = g_variant_new_string("Memory");
198 break;
8fa9368e
BV
199 default:
200 return SR_ERR_NA;
201 }
202
a90e480c 203 return ret;
8fa9368e
BV
204}
205
584560f1 206static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 207 const struct sr_channel_group *cg)
8fa9368e 208{
7fb8279c 209 struct dev_context *devc;
f157b2ee
BV
210 uint64_t tmp_u64, low, high;
211 unsigned int i;
a90e480c 212 int tmp, ret;
be733919 213 const char *tmp_str;
8fa9368e 214
53b4680f 215 (void)cg;
d3c74a6f 216
8fa9368e
BV
217 if (sdi->status != SR_ST_ACTIVE)
218 return SR_ERR_DEV_CLOSED;
219
7fb8279c
BV
220 if (!(devc = sdi->priv)) {
221 sr_err("sdi->priv was NULL.");
222 return SR_ERR_BUG;
223 }
224
8fa9368e
BV
225 ret = SR_OK;
226 switch (key) {
7fb8279c
BV
227 case SR_CONF_LIMIT_SAMPLES:
228 tmp_u64 = g_variant_get_uint64(data);
229 devc->limit_samples = tmp_u64;
230 ret = SR_OK;
231 break;
e1af0e85 232 case SR_CONF_DATALOG:
0cd9107d 233 ret = cem_dt_885x_recording_set(sdi, g_variant_get_boolean(data));
e1af0e85 234 break;
be733919
BV
235 case SR_CONF_SPL_WEIGHT_FREQ:
236 tmp_str = g_variant_get_string(data, NULL);
237 if (!strcmp(tmp_str, "A"))
238 ret = cem_dt_885x_weight_freq_set(sdi,
239 SR_MQFLAG_SPL_FREQ_WEIGHT_A);
240 else if (!strcmp(tmp_str, "C"))
241 ret = cem_dt_885x_weight_freq_set(sdi,
242 SR_MQFLAG_SPL_FREQ_WEIGHT_C);
243 else
244 return SR_ERR_ARG;
245 break;
1487ce4f
BV
246 case SR_CONF_SPL_WEIGHT_TIME:
247 tmp_str = g_variant_get_string(data, NULL);
248 if (!strcmp(tmp_str, "F"))
249 ret = cem_dt_885x_weight_time_set(sdi,
250 SR_MQFLAG_SPL_TIME_WEIGHT_F);
251 else if (!strcmp(tmp_str, "S"))
252 ret = cem_dt_885x_weight_time_set(sdi,
253 SR_MQFLAG_SPL_TIME_WEIGHT_S);
254 else
255 return SR_ERR_ARG;
256 break;
a90e480c
BV
257 case SR_CONF_HOLD_MAX:
258 tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MAX : 0;
259 ret = cem_dt_885x_holdmode_set(sdi, tmp);
260 break;
261 case SR_CONF_HOLD_MIN:
262 tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MIN : 0;
263 ret = cem_dt_885x_holdmode_set(sdi, tmp);
264 break;
f157b2ee
BV
265 case SR_CONF_SPL_MEASUREMENT_RANGE:
266 g_variant_get(data, "(tt)", &low, &high);
267 ret = SR_ERR_ARG;
268 for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) {
269 if (meas_ranges[i][0] == low && meas_ranges[i][1] == high) {
270 ret = cem_dt_885x_meas_range_set(sdi, low, high);
271 break;
272 }
273 }
274 break;
4c22355f
BV
275 case SR_CONF_POWER_OFF:
276 if (g_variant_get_boolean(data))
277 ret = cem_dt_885x_power_off(sdi);
278 break;
662172d4
BV
279 case SR_CONF_DATA_SOURCE:
280 tmp_str = g_variant_get_string(data, NULL);
281 if (!strcmp(tmp_str, "Live"))
282 devc->cur_data_source = DATA_SOURCE_LIVE;
283 else if (!strcmp(tmp_str, "Memory"))
284 devc->cur_data_source = DATA_SOURCE_MEMORY;
285 else
286 return SR_ERR;
cea26f6e 287 devc->enable_data_source_memory = devc->cur_data_source == DATA_SOURCE_MEMORY;
662172d4 288 break;
8fa9368e
BV
289 default:
290 ret = SR_ERR_NA;
291 }
292
293 return ret;
294}
295
584560f1 296static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 297 const struct sr_channel_group *cg)
8fa9368e 298{
f157b2ee
BV
299 GVariant *tuple, *range[2];
300 GVariantBuilder gvb;
301 unsigned int i;
8fa9368e
BV
302 int ret;
303
53b4680f 304 (void)cg;
8fa9368e
BV
305
306 ret = SR_OK;
d7125bfa
BV
307 if (!sdi) {
308 switch (key) {
309 case SR_CONF_SCAN_OPTIONS:
310 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
311 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
312 break;
313 case SR_CONF_DEVICE_OPTIONS:
314 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
023c73ae 315 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
d7125bfa
BV
316 break;
317 default:
318 return SR_ERR_NA;
319 }
320 } else {
321 switch (key) {
322 case SR_CONF_DEVICE_OPTIONS:
323 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
023c73ae 324 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
e742b88f 325 break;
d7125bfa
BV
326 case SR_CONF_SPL_WEIGHT_FREQ:
327 *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq));
328 break;
329 case SR_CONF_SPL_WEIGHT_TIME:
330 *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time));
331 break;
332 case SR_CONF_SPL_MEASUREMENT_RANGE:
333 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
334 for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) {
335 range[0] = g_variant_new_uint64(meas_ranges[i][0]);
336 range[1] = g_variant_new_uint64(meas_ranges[i][1]);
337 tuple = g_variant_new_tuple(range, 2);
338 g_variant_builder_add_value(&gvb, tuple);
339 }
340 *data = g_variant_builder_end(&gvb);
341 break;
342 case SR_CONF_DATA_SOURCE:
343 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
344 break;
345 default:
346 return SR_ERR_NA;
f157b2ee 347 }
8fa9368e
BV
348 }
349
350 return ret;
351}
352
695dc859 353static int dev_acquisition_start(const struct sr_dev_inst *sdi)
8fa9368e 354{
e37c4b39
BV
355 struct dev_context *devc;
356 struct sr_serial_dev_inst *serial;
8fa9368e
BV
357
358 if (sdi->status != SR_ST_ACTIVE)
359 return SR_ERR_DEV_CLOSED;
360
208c1d35 361 devc = sdi->priv;
e37c4b39
BV
362 devc->state = ST_INIT;
363 devc->num_samples = 0;
364 devc->buf_len = 0;
365
695dc859 366 std_session_send_df_header(sdi, LOG_PREFIX);
e37c4b39
BV
367
368 /* Poll every 100ms, or whenever some data comes in. */
369 serial = sdi->conn;
102f1239
BV
370 serial_source_add(sdi->session, serial, G_IO_IN, 150,
371 cem_dt_885x_receive_data, (void *)sdi);
8fa9368e
BV
372
373 return SR_OK;
374}
375
695dc859 376static int dev_acquisition_stop(struct sr_dev_inst *sdi)
8fa9368e 377{
8fa9368e
BV
378 if (sdi->status != SR_ST_ACTIVE)
379 return SR_ERR_DEV_CLOSED;
380
6525d819 381 return std_serial_dev_acquisition_stop(sdi, std_serial_dev_close,
e37c4b39 382 sdi->conn, LOG_PREFIX);
8fa9368e
BV
383}
384
385SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info = {
386 .name = "cem-dt-885x",
387 .longname = "CEM DT-885x",
388 .api_version = 1,
c2fdcc25 389 .init = std_init,
700d6b64 390 .cleanup = std_cleanup,
8fa9368e 391 .scan = scan,
c01bf34c 392 .dev_list = std_dev_list,
a6630742 393 .dev_clear = NULL,
8fa9368e
BV
394 .config_get = config_get,
395 .config_set = config_set,
396 .config_list = config_list,
8f878dc6 397 .dev_open = std_serial_dev_open,
bf2c987f 398 .dev_close = std_serial_dev_close,
8fa9368e
BV
399 .dev_acquisition_start = dev_acquisition_start,
400 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 401 .context = NULL,
8fa9368e 402};