2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
23 #define SERIALCOMM "9600/8n1"
24 /* 23ms is the longest interval between tokens. */
25 #define MAX_SCAN_TIME 25 * 1000
27 static const int32_t hwopts[] = {
31 static const int32_t hwcaps[] = {
32 SR_CONF_SOUNDLEVELMETER,
33 SR_CONF_LIMIT_SAMPLES,
36 SR_CONF_SPL_WEIGHT_FREQ,
37 SR_CONF_SPL_WEIGHT_TIME,
42 static const char *weight_freq[] = {
47 static const char *weight_time[] = {
52 SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info;
53 static struct sr_dev_driver *di = &cem_dt_885x_driver_info;
56 static int init(struct sr_context *sr_ctx)
58 return std_init(sr_ctx, di, LOG_PREFIX);
61 static GSList *scan(GSList *options)
63 struct drv_context *drvc;
64 struct dev_context *devc;
65 struct sr_config *src;
66 struct sr_serial_dev_inst *serial;
67 struct sr_dev_inst *sdi;
68 struct sr_probe *probe;
75 for (l = options; l; l = l->next) {
77 if (src->key == SR_CONF_CONN)
78 conn = g_variant_get_string(src->data, NULL);
83 if (!(serial = sr_serial_dev_inst_new(conn, SERIALCOMM)))
86 if (serial_open(serial, SERIAL_RDONLY | SERIAL_NONBLOCK) != SR_OK)
91 start = g_get_monotonic_time();
92 while (g_get_monotonic_time() - start < MAX_SCAN_TIME) {
93 if (serial_read(serial, &c, 1) == 1 && c == 0xa5) {
95 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "CEM",
99 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
100 sr_dbg("Device context malloc failed.");
103 devc->cur_mqflags = 0;
104 devc->recording = -1;
106 if (!(sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM)))
109 sdi->inst_type = SR_INST_SERIAL;
112 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "SPL")))
114 sdi->probes = g_slist_append(sdi->probes, probe);
115 drvc->instances = g_slist_append(drvc->instances, sdi);
116 devices = g_slist_append(devices, sdi);
119 /* It takes about 1ms for a byte to come in. */
123 serial_close(serial);
128 static GSList *dev_list(void)
130 struct drv_context *drvc;
134 return drvc->instances;
137 static int dev_clear(void)
139 return std_dev_clear(di, NULL);
142 static int dev_open(struct sr_dev_inst *sdi)
144 struct sr_serial_dev_inst *serial;
147 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
150 sdi->status = SR_ST_ACTIVE;
155 static int dev_close(struct sr_dev_inst *sdi)
157 struct sr_serial_dev_inst *serial;
160 if (serial && serial->fd != -1) {
161 serial_close(serial);
162 sdi->status = SR_ST_INACTIVE;
168 static int cleanup(void)
173 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
175 struct dev_context *devc;
184 case SR_CONF_LIMIT_SAMPLES:
185 *data = g_variant_new_uint64(devc->limit_samples);
187 case SR_CONF_DATALOG:
188 if ((ret = cem_dt_885x_recording_get(sdi, &tmp)) == SR_OK)
189 *data = g_variant_new_boolean(tmp);
191 case SR_CONF_SPL_WEIGHT_FREQ:
192 tmp = cem_dt_885x_weight_freq_get(sdi);
193 if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A)
194 *data = g_variant_new_string("A");
195 else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C)
196 *data = g_variant_new_string("C");
200 case SR_CONF_SPL_WEIGHT_TIME:
201 tmp = cem_dt_885x_weight_time_get(sdi);
202 if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_F)
203 *data = g_variant_new_string("F");
204 else if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_S)
205 *data = g_variant_new_string("S");
209 case SR_CONF_HOLD_MAX:
210 if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK)
211 *data = g_variant_new_boolean(tmp == SR_MQFLAG_MAX);
213 case SR_CONF_HOLD_MIN:
214 if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK)
215 *data = g_variant_new_boolean(tmp == SR_MQFLAG_MIN);
224 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
226 struct dev_context *devc;
231 if (sdi->status != SR_ST_ACTIVE)
232 return SR_ERR_DEV_CLOSED;
234 if (!(devc = sdi->priv)) {
235 sr_err("sdi->priv was NULL.");
241 case SR_CONF_LIMIT_SAMPLES:
242 tmp_u64 = g_variant_get_uint64(data);
243 devc->limit_samples = tmp_u64;
246 case SR_CONF_DATALOG:
247 ret = cem_dt_885x_recording_set(sdi, g_variant_get_boolean(data));
249 case SR_CONF_SPL_WEIGHT_FREQ:
250 tmp_str = g_variant_get_string(data, NULL);
251 if (!strcmp(tmp_str, "A"))
252 ret = cem_dt_885x_weight_freq_set(sdi,
253 SR_MQFLAG_SPL_FREQ_WEIGHT_A);
254 else if (!strcmp(tmp_str, "C"))
255 ret = cem_dt_885x_weight_freq_set(sdi,
256 SR_MQFLAG_SPL_FREQ_WEIGHT_C);
260 case SR_CONF_SPL_WEIGHT_TIME:
261 tmp_str = g_variant_get_string(data, NULL);
262 if (!strcmp(tmp_str, "F"))
263 ret = cem_dt_885x_weight_time_set(sdi,
264 SR_MQFLAG_SPL_TIME_WEIGHT_F);
265 else if (!strcmp(tmp_str, "S"))
266 ret = cem_dt_885x_weight_time_set(sdi,
267 SR_MQFLAG_SPL_TIME_WEIGHT_S);
271 case SR_CONF_HOLD_MAX:
272 tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MAX : 0;
273 ret = cem_dt_885x_holdmode_set(sdi, tmp);
275 case SR_CONF_HOLD_MIN:
276 tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MIN : 0;
277 ret = cem_dt_885x_holdmode_set(sdi, tmp);
286 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
294 case SR_CONF_SCAN_OPTIONS:
295 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
296 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
298 case SR_CONF_DEVICE_OPTIONS:
299 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
300 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
302 case SR_CONF_SPL_WEIGHT_FREQ:
303 *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq));
305 case SR_CONF_SPL_WEIGHT_TIME:
306 *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time));
315 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
317 struct dev_context *devc;
318 struct sr_serial_dev_inst *serial;
320 if (sdi->status != SR_ST_ACTIVE)
321 return SR_ERR_DEV_CLOSED;
323 if (!(devc = sdi->priv)) {
324 sr_err("sdi->priv was NULL.");
328 devc->cb_data = cb_data;
329 devc->state = ST_INIT;
330 devc->num_samples = 0;
333 /* Send header packet to the session bus. */
334 std_session_send_df_header(cb_data, LOG_PREFIX);
336 /* Poll every 100ms, or whenever some data comes in. */
338 sr_source_add(serial->fd, G_IO_IN, 150, cem_dt_885x_receive_data,
344 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
348 if (sdi->status != SR_ST_ACTIVE)
349 return SR_ERR_DEV_CLOSED;
351 return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close,
352 sdi->conn, LOG_PREFIX);
357 SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info = {
358 .name = "cem-dt-885x",
359 .longname = "CEM DT-885x",
364 .dev_list = dev_list,
365 .dev_clear = dev_clear,
366 .config_get = config_get,
367 .config_set = config_set,
368 .config_list = config_list,
369 .dev_open = dev_open,
370 .dev_close = dev_close,
371 .dev_acquisition_start = dev_acquisition_start,
372 .dev_acquisition_stop = dev_acquisition_stop,