]> sigrok.org Git - libsigrok.git/blame - src/hardware/pce-322a/api.c
std: Rename std_dev_clear() to std_dev_clear_with_callback().
[libsigrok.git] / src / hardware / pce-322a / api.c
CommitLineData
5a2c71cc
GH
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2016 George Hopkins <george-hopkins@null.net>
ae87e02f 5 * Copyright (C) 2016 Matthieu Guillaumin <matthieu@guillaum.in>
5a2c71cc
GH
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <string.h>
22#include <config.h>
23#include "protocol.h"
24
25#define SERIALCOMM "115200/8n1"
26
27static const uint32_t scanopts[] = {
28 SR_CONF_CONN,
29};
30
31static const uint32_t drvopts[] = {
32 SR_CONF_SOUNDLEVELMETER,
33};
34
35static const uint32_t devopts[] = {
36 SR_CONF_CONTINUOUS | SR_CONF_SET,
37 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
38 SR_CONF_SPL_WEIGHT_FREQ | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
39 SR_CONF_SPL_WEIGHT_TIME | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
40 SR_CONF_SPL_MEASUREMENT_RANGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
41 SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET,
ae87e02f 42 SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
5a2c71cc
GH
43};
44
45static const char *weight_freq[] = {
46 "A",
47 "C",
48};
49
50static const char *weight_time[] = {
51 "F",
52 "S",
53};
54
55static const uint64_t meas_ranges[][2] = {
56 { 30, 130 },
57 { 30, 80 },
58 { 50, 100 },
59 { 80, 130 },
60};
61
ae87e02f
MG
62static const char *data_sources[] = {
63 "Live",
64 "Memory",
65};
66
5a2c71cc
GH
67static GSList *scan(struct sr_dev_driver *di, GSList *options)
68{
5a2c71cc
GH
69 struct dev_context *devc;
70 struct sr_config *src;
71 struct sr_serial_dev_inst *serial;
72 struct sr_dev_inst *sdi;
43376f33 73 GSList *l;
5a2c71cc
GH
74 const char *conn;
75
76 conn = NULL;
77 for (l = options; l; l = l->next) {
78 src = l->data;
79 if (src->key == SR_CONF_CONN)
80 conn = g_variant_get_string(src->data, NULL);
81 }
82 if (!conn)
83 return NULL;
84
85 serial = sr_serial_dev_inst_new(conn, SERIALCOMM);
86
87 if (serial_open(serial, SERIAL_RDONLY) != SR_OK)
88 return NULL;
89
5a2c71cc
GH
90 sdi = g_malloc0(sizeof(struct sr_dev_inst));
91 sdi->status = SR_ST_INACTIVE;
92 sdi->vendor = g_strdup("PCE");
93 sdi->model = g_strdup("PCE-322A");
94 devc = g_malloc0(sizeof(struct dev_context));
95 devc->cur_mqflags = SR_MQFLAG_SPL_TIME_WEIGHT_F | SR_MQFLAG_SPL_FREQ_WEIGHT_A;
96 sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM);
97 sdi->inst_type = SR_INST_SERIAL;
98 sdi->priv = devc;
5a2c71cc 99 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "SPL");
5a2c71cc
GH
100
101 serial_close(serial);
102
43376f33 103 return std_scan_complete(di, g_slist_append(NULL, sdi));
5a2c71cc
GH
104}
105
106static int dev_clear(const struct sr_dev_driver *di)
107{
6e43c3d5 108 return std_dev_clear_with_callback(di, NULL);
5a2c71cc
GH
109}
110
111static int config_get(uint32_t key, GVariant **data,
112 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
113{
114 struct dev_context *devc;
115 GVariant *range[2];
116 uint64_t low, high;
117 uint64_t tmp;
118 int ret;
119
120 (void)cg;
121
122 if (!sdi)
123 return SR_ERR_ARG;
124
125 devc = sdi->priv;
126 ret = SR_OK;
127 switch (key) {
128 case SR_CONF_LIMIT_SAMPLES:
129 *data = g_variant_new_uint64(devc->limit_samples);
130 break;
131 case SR_CONF_SPL_WEIGHT_FREQ:
132 tmp = pce_322a_weight_freq_get(sdi);
133 if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A)
134 *data = g_variant_new_string("A");
135 else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C)
136 *data = g_variant_new_string("C");
137 else
138 return SR_ERR;
139 break;
140 case SR_CONF_SPL_WEIGHT_TIME:
141 tmp = pce_322a_weight_time_get(sdi);
142 if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_F)
143 *data = g_variant_new_string("F");
144 else if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_S)
145 *data = g_variant_new_string("S");
146 else
147 return SR_ERR;
148 break;
149 case SR_CONF_SPL_MEASUREMENT_RANGE:
150 if ((ret = pce_322a_meas_range_get(sdi, &low, &high)) == SR_OK) {
151 range[0] = g_variant_new_uint64(low);
152 range[1] = g_variant_new_uint64(high);
153 *data = g_variant_new_tuple(range, 2);
154 }
155 break;
156 case SR_CONF_POWER_OFF:
157 *data = g_variant_new_boolean(FALSE);
158 break;
ae87e02f
MG
159 case SR_CONF_DATA_SOURCE:
160 if (devc->cur_data_source == DATA_SOURCE_LIVE)
161 *data = g_variant_new_string("Live");
162 else
163 *data = g_variant_new_string("Memory");
164 break;
5a2c71cc
GH
165 default:
166 return SR_ERR_NA;
167 }
168
169 return ret;
170}
171
172static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
173 const struct sr_channel_group *cg)
174{
175 struct dev_context *devc;
176 uint64_t tmp_u64, low, high;
177 unsigned int i;
178 int ret;
179 const char *tmp_str;
180
181 (void)cg;
182
5a2c71cc
GH
183 devc = sdi->priv;
184
185 ret = SR_OK;
186 switch (key) {
187 case SR_CONF_LIMIT_SAMPLES:
188 tmp_u64 = g_variant_get_uint64(data);
189 devc->limit_samples = tmp_u64;
190 ret = SR_OK;
191 break;
192 case SR_CONF_SPL_WEIGHT_FREQ:
193 tmp_str = g_variant_get_string(data, NULL);
194 if (!strcmp(tmp_str, "A"))
195 ret = pce_322a_weight_freq_set(sdi,
196 SR_MQFLAG_SPL_FREQ_WEIGHT_A);
197 else if (!strcmp(tmp_str, "C"))
198 ret = pce_322a_weight_freq_set(sdi,
199 SR_MQFLAG_SPL_FREQ_WEIGHT_C);
200 else
201 return SR_ERR_ARG;
202 break;
203 case SR_CONF_SPL_WEIGHT_TIME:
204 tmp_str = g_variant_get_string(data, NULL);
205 if (!strcmp(tmp_str, "F"))
206 ret = pce_322a_weight_time_set(sdi,
207 SR_MQFLAG_SPL_TIME_WEIGHT_F);
208 else if (!strcmp(tmp_str, "S"))
209 ret = pce_322a_weight_time_set(sdi,
210 SR_MQFLAG_SPL_TIME_WEIGHT_S);
211 else
212 return SR_ERR_ARG;
213 break;
214 case SR_CONF_SPL_MEASUREMENT_RANGE:
215 g_variant_get(data, "(tt)", &low, &high);
216 ret = SR_ERR_ARG;
217 for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) {
218 if (meas_ranges[i][0] == low && meas_ranges[i][1] == high) {
219 ret = pce_322a_meas_range_set(sdi, low, high);
220 break;
221 }
222 }
223 break;
224 case SR_CONF_POWER_OFF:
225 if (g_variant_get_boolean(data))
226 ret = pce_322a_power_off(sdi);
227 break;
ae87e02f
MG
228 case SR_CONF_DATA_SOURCE:
229 tmp_str = g_variant_get_string(data, NULL);
230 if (!strcmp(tmp_str, "Live"))
231 devc->cur_data_source = DATA_SOURCE_LIVE;
232 else if (!strcmp(tmp_str, "Memory"))
233 devc->cur_data_source = DATA_SOURCE_MEMORY;
234 else
235 return SR_ERR;
236 break;
5a2c71cc
GH
237 default:
238 ret = SR_ERR_NA;
239 }
240
241 return ret;
242}
243
244static int config_list(uint32_t key, GVariant **data,
245 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
246{
247 GVariant *tuple, *range[2];
248 GVariantBuilder gvb;
249 unsigned int i;
250 int ret;
251
252 (void)cg;
253
254 ret = SR_OK;
255 if (!sdi) {
256 switch (key) {
257 case SR_CONF_SCAN_OPTIONS:
258 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
259 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
260 break;
261 case SR_CONF_DEVICE_OPTIONS:
262 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
263 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
264 break;
265 default:
266 return SR_ERR_NA;
267 }
268 } else {
269 switch (key) {
270 case SR_CONF_DEVICE_OPTIONS:
271 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
272 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
273 break;
274 case SR_CONF_SPL_WEIGHT_FREQ:
275 *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq));
276 break;
277 case SR_CONF_SPL_WEIGHT_TIME:
278 *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time));
279 break;
280 case SR_CONF_SPL_MEASUREMENT_RANGE:
281 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
282 for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) {
283 range[0] = g_variant_new_uint64(meas_ranges[i][0]);
284 range[1] = g_variant_new_uint64(meas_ranges[i][1]);
285 tuple = g_variant_new_tuple(range, 2);
286 g_variant_builder_add_value(&gvb, tuple);
287 }
288 *data = g_variant_builder_end(&gvb);
289 break;
ae87e02f
MG
290 case SR_CONF_DATA_SOURCE:
291 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
292 break;
5a2c71cc
GH
293 default:
294 return SR_ERR_NA;
295 }
296 }
297
298 return ret;
299}
300
301static int dev_open(struct sr_dev_inst *sdi)
302{
303 int ret;
304
305 ret = std_serial_dev_open(sdi);
306 if (ret != SR_OK)
307 return ret;
308
309 return pce_322a_connect(sdi);
310}
311
312static int dev_close(struct sr_dev_inst *sdi)
313{
314 /*
315 * Ensure device gets properly disconnected even when there was
316 * no acquisition.
317 */
318 pce_322a_disconnect(sdi);
319
320 return std_serial_dev_close(sdi);
321}
322
323static int dev_acquisition_start(const struct sr_dev_inst *sdi)
324{
325 struct dev_context *devc;
326 struct sr_serial_dev_inst *serial;
327
5a2c71cc
GH
328 devc = sdi->priv;
329 devc->buffer_len = 0;
ae87e02f 330 devc->memory_state = MEM_STATE_REQUEST_MEMORY_USAGE;
5a2c71cc 331
bee2b016 332 std_session_send_df_header(sdi);
5a2c71cc
GH
333
334 /* Poll every 150ms, or whenever some data comes in. */
335 serial = sdi->conn;
336 serial_source_add(sdi->session, serial, G_IO_IN, 150,
337 pce_322a_receive_data, (void *)sdi);
338
339 return SR_OK;
340}
341
5a2c71cc
GH
342static struct sr_dev_driver pce_322a_driver_info = {
343 .name = "pce-322a",
344 .longname = "PCE PCE-322A",
345 .api_version = 1,
346 .init = std_init,
347 .cleanup = std_cleanup,
348 .scan = scan,
349 .dev_list = std_dev_list,
350 .dev_clear = dev_clear,
351 .config_get = config_get,
352 .config_set = config_set,
353 .config_list = config_list,
354 .dev_open = dev_open,
355 .dev_close = dev_close,
356 .dev_acquisition_start = dev_acquisition_start,
4b1a9d5d 357 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
5a2c71cc
GH
358 .context = NULL,
359};
360SR_REGISTER_DEV_DRIVER(pce_322a_driver_info);