]> sigrok.org Git - libsigrok.git/blob - src/hardware/pce-322a/api.c
b9ff092f34282978b94695cbc73571b9c41c398f
[libsigrok.git] / src / hardware / pce-322a / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2016 George Hopkins <george-hopkins@null.net>
5  * Copyright (C) 2016 Matthieu Guillaumin <matthieu@guillaum.in>
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
27 static const uint32_t scanopts[] = {
28         SR_CONF_CONN,
29 };
30
31 static const uint32_t drvopts[] = {
32         SR_CONF_SOUNDLEVELMETER,
33 };
34
35 static 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,
42         SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
43 };
44
45 static const char *weight_freq[] = {
46         "A", "C",
47 };
48
49 static const char *weight_time[] = {
50         "F", "S",
51 };
52
53 static const uint64_t meas_ranges[][2] = {
54         { 30, 130 },
55         { 30, 80 },
56         { 50, 100 },
57         { 80, 130 },
58 };
59
60 static const char *data_sources[] = {
61         "Live", "Memory",
62 };
63
64 static GSList *scan(struct sr_dev_driver *di, GSList *options)
65 {
66         struct dev_context *devc;
67         struct sr_config *src;
68         struct sr_serial_dev_inst *serial;
69         struct sr_dev_inst *sdi;
70         GSList *l;
71         const char *conn;
72
73         conn = NULL;
74         for (l = options; l; l = l->next) {
75                 src = l->data;
76                 if (src->key == SR_CONF_CONN)
77                         conn = g_variant_get_string(src->data, NULL);
78         }
79         if (!conn)
80                 return NULL;
81
82         serial = sr_serial_dev_inst_new(conn, SERIALCOMM);
83
84         if (serial_open(serial, SERIAL_RDONLY) != SR_OK)
85                 return NULL;
86
87         sdi = g_malloc0(sizeof(struct sr_dev_inst));
88         sdi->status = SR_ST_INACTIVE;
89         sdi->vendor = g_strdup("PCE");
90         sdi->model = g_strdup("PCE-322A");
91         devc = g_malloc0(sizeof(struct dev_context));
92         devc->cur_mqflags = SR_MQFLAG_SPL_TIME_WEIGHT_F | SR_MQFLAG_SPL_FREQ_WEIGHT_A;
93         sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM);
94         sdi->inst_type = SR_INST_SERIAL;
95         sdi->priv = devc;
96         sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "SPL");
97
98         serial_close(serial);
99
100         return std_scan_complete(di, g_slist_append(NULL, sdi));
101 }
102
103 static int config_get(uint32_t key, GVariant **data,
104         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
105 {
106         struct dev_context *devc;
107         uint64_t low, high;
108         uint64_t tmp;
109         int ret;
110
111         (void)cg;
112
113         if (!sdi)
114                 return SR_ERR_ARG;
115
116         devc = sdi->priv;
117         ret = SR_OK;
118         switch (key) {
119         case SR_CONF_LIMIT_SAMPLES:
120                 *data = g_variant_new_uint64(devc->limit_samples);
121                 break;
122         case SR_CONF_SPL_WEIGHT_FREQ:
123                 tmp = pce_322a_weight_freq_get(sdi);
124                 if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A)
125                         *data = g_variant_new_string("A");
126                 else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C)
127                         *data = g_variant_new_string("C");
128                 else
129                         return SR_ERR;
130                 break;
131         case SR_CONF_SPL_WEIGHT_TIME:
132                 tmp = pce_322a_weight_time_get(sdi);
133                 if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_F)
134                         *data = g_variant_new_string("F");
135                 else if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_S)
136                         *data = g_variant_new_string("S");
137                 else
138                         return SR_ERR;
139                 break;
140         case SR_CONF_SPL_MEASUREMENT_RANGE:
141                 if ((ret = pce_322a_meas_range_get(sdi, &low, &high)) == SR_OK)
142                         *data = std_gvar_tuple_u64(low, high);
143                 break;
144         case SR_CONF_POWER_OFF:
145                 *data = g_variant_new_boolean(FALSE);
146                 break;
147         case SR_CONF_DATA_SOURCE:
148                 if (devc->cur_data_source == DATA_SOURCE_LIVE)
149                         *data = g_variant_new_string("Live");
150                 else
151                         *data = g_variant_new_string("Memory");
152                 break;
153         default:
154                 return SR_ERR_NA;
155         }
156
157         return ret;
158 }
159
160 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
161         const struct sr_channel_group *cg)
162 {
163         struct dev_context *devc;
164         uint64_t tmp_u64, low, high;
165         unsigned int i;
166         int ret;
167         const char *tmp_str;
168
169         (void)cg;
170
171         devc = sdi->priv;
172
173         ret = SR_OK;
174         switch (key) {
175         case SR_CONF_LIMIT_SAMPLES:
176                 tmp_u64 = g_variant_get_uint64(data);
177                 devc->limit_samples = tmp_u64;
178                 ret = SR_OK;
179                 break;
180         case SR_CONF_SPL_WEIGHT_FREQ:
181                 tmp_str = g_variant_get_string(data, NULL);
182                 if (!strcmp(tmp_str, "A"))
183                         ret = pce_322a_weight_freq_set(sdi,
184                                         SR_MQFLAG_SPL_FREQ_WEIGHT_A);
185                 else if (!strcmp(tmp_str, "C"))
186                         ret = pce_322a_weight_freq_set(sdi,
187                                         SR_MQFLAG_SPL_FREQ_WEIGHT_C);
188                 else
189                         return SR_ERR_ARG;
190                 break;
191         case SR_CONF_SPL_WEIGHT_TIME:
192                 tmp_str = g_variant_get_string(data, NULL);
193                 if (!strcmp(tmp_str, "F"))
194                         ret = pce_322a_weight_time_set(sdi,
195                                         SR_MQFLAG_SPL_TIME_WEIGHT_F);
196                 else if (!strcmp(tmp_str, "S"))
197                         ret = pce_322a_weight_time_set(sdi,
198                                         SR_MQFLAG_SPL_TIME_WEIGHT_S);
199                 else
200                         return SR_ERR_ARG;
201                 break;
202         case SR_CONF_SPL_MEASUREMENT_RANGE:
203                 g_variant_get(data, "(tt)", &low, &high);
204                 ret = SR_ERR_ARG;
205                 for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) {
206                         if (meas_ranges[i][0] == low && meas_ranges[i][1] == high) {
207                                 ret = pce_322a_meas_range_set(sdi, low, high);
208                                 break;
209                         }
210                 }
211                 break;
212         case SR_CONF_POWER_OFF:
213                 if (g_variant_get_boolean(data))
214                         ret = pce_322a_power_off(sdi);
215                 break;
216         case SR_CONF_DATA_SOURCE:
217                 tmp_str = g_variant_get_string(data, NULL);
218                 if (!strcmp(tmp_str, "Live"))
219                         devc->cur_data_source = DATA_SOURCE_LIVE;
220                 else if (!strcmp(tmp_str, "Memory"))
221                         devc->cur_data_source = DATA_SOURCE_MEMORY;
222                 else
223                         return SR_ERR;
224                 break;
225         default:
226                 ret = SR_ERR_NA;
227         }
228
229         return ret;
230 }
231
232 static int config_list(uint32_t key, GVariant **data,
233         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
234 {
235         switch (key) {
236         case SR_CONF_SCAN_OPTIONS:
237         case SR_CONF_DEVICE_OPTIONS:
238                 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
239         case SR_CONF_SPL_WEIGHT_FREQ:
240                 *data = g_variant_new_strv(ARRAY_AND_SIZE(weight_freq));
241                 break;
242         case SR_CONF_SPL_WEIGHT_TIME:
243                 *data = g_variant_new_strv(ARRAY_AND_SIZE(weight_time));
244                 break;
245         case SR_CONF_SPL_MEASUREMENT_RANGE:
246                 *data = std_gvar_tuple_array(ARRAY_AND_SIZE(meas_ranges));
247                 break;
248         case SR_CONF_DATA_SOURCE:
249                 *data = g_variant_new_strv(ARRAY_AND_SIZE(data_sources));
250                 break;
251         default:
252                 return SR_ERR_NA;
253         }
254
255         return SR_OK;
256 }
257
258 static int dev_open(struct sr_dev_inst *sdi)
259 {
260         int ret;
261
262         ret = std_serial_dev_open(sdi);
263         if (ret != SR_OK)
264                 return ret;
265
266         return pce_322a_connect(sdi);
267 }
268
269 static int dev_close(struct sr_dev_inst *sdi)
270 {
271         /*
272          * Ensure device gets properly disconnected even when there was
273          * no acquisition.
274          */
275         pce_322a_disconnect(sdi);
276
277         return std_serial_dev_close(sdi);
278 }
279
280 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
281 {
282         struct dev_context *devc;
283         struct sr_serial_dev_inst *serial;
284
285         devc = sdi->priv;
286         devc->buffer_len = 0;
287         devc->memory_state = MEM_STATE_REQUEST_MEMORY_USAGE;
288
289         std_session_send_df_header(sdi);
290
291         serial = sdi->conn;
292         serial_source_add(sdi->session, serial, G_IO_IN, 150,
293                         pce_322a_receive_data, (void *)sdi);
294
295         return SR_OK;
296 }
297
298 static struct sr_dev_driver pce_322a_driver_info = {
299         .name = "pce-322a",
300         .longname = "PCE PCE-322A",
301         .api_version = 1,
302         .init = std_init,
303         .cleanup = std_cleanup,
304         .scan = scan,
305         .dev_list = std_dev_list,
306         .dev_clear = std_dev_clear,
307         .config_get = config_get,
308         .config_set = config_set,
309         .config_list = config_list,
310         .dev_open = dev_open,
311         .dev_close = dev_close,
312         .dev_acquisition_start = dev_acquisition_start,
313         .dev_acquisition_stop = std_serial_dev_acquisition_stop,
314         .context = NULL,
315 };
316 SR_REGISTER_DEV_DRIVER(pce_322a_driver_info);