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