]> sigrok.org Git - libsigrok.git/blob - src/hardware/baylibre-acme/api.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / hardware / baylibre-acme / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2015 Bartosz Golaszewski <bgolaszewski@baylibre.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 "protocol.h"
22 #include <time.h>
23 #include <sys/timerfd.h>
24
25 static const uint32_t drvopts[] = {
26         SR_CONF_THERMOMETER,
27         SR_CONF_POWERMETER,
28 };
29
30 static const uint32_t devopts[] = {
31         SR_CONF_CONTINUOUS,
32         SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
33         SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
34         SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
35 };
36
37 /*
38  * Currently there are two channel-group/probe options for ACME:
39  *   - SR_CONF_PROBE_FACTOR - allows to modify current shunt resistance
40  *     calibration
41  *   - SR_CONF_POWER_OFF - allows to remotely cut-off/restore power to
42  *     measured devices
43  *
44  * They are not static - we have to check each probe's capabilities in
45  * config_list().
46  */
47 #define MAX_DEVOPTS_CG          2
48 #define HAS_PROBE_FACTOR        (SR_CONF_PROBE_FACTOR | SR_CONF_GET | SR_CONF_SET)
49 #define HAS_POWER_OFF           (SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET)
50
51 #define MAX_SAMPLE_RATE 500 /* In Hz */
52
53 static const uint64_t samplerates[] = {
54         SR_HZ(1),
55         SR_HZ(MAX_SAMPLE_RATE),
56         SR_HZ(1),
57 };
58
59 static GSList *scan(struct sr_dev_driver *di, GSList *options)
60 {
61         struct dev_context *devc;
62         struct sr_dev_inst *sdi;
63         gboolean status;
64         int i;
65
66         (void)options;
67
68         devc = g_malloc0(sizeof(struct dev_context));
69         devc->samplerate = SR_HZ(10);
70
71         sdi = g_malloc0(sizeof(struct sr_dev_inst));
72         sdi->status = SR_ST_INACTIVE;
73         sdi->vendor = g_strdup("BayLibre");
74         sdi->model = g_strdup("ACME");
75         sdi->priv = devc;
76
77         status = bl_acme_is_sane();
78         if (!status)
79                 goto err_out;
80
81         /*
82          * Iterate over all ACME connectors and check if any probes
83          * are present.
84          */
85         for (i = 0; i < MAX_PROBES; i++) {
86                 /*
87                  * First check if there's an energy probe on this connector. If
88                  * not, and we're already at the fifth probe - see if we can
89                  * detect a temperature probe.
90                  */
91                 status = bl_acme_detect_probe(bl_acme_get_enrg_addr(i),
92                                               PROBE_NUM(i), ENRG_PROBE_NAME);
93                 if (status) {
94                         /* Energy probe detected. */
95                         status = bl_acme_register_probe(sdi, PROBE_ENRG,
96                                         bl_acme_get_enrg_addr(i), PROBE_NUM(i));
97                         if (!status) {
98                                 sr_err("Error registering power probe %d",
99                                        PROBE_NUM(i));
100                                 continue;
101                         }
102                 } else if (i >= TEMP_PRB_START_INDEX) {
103                         status = bl_acme_detect_probe(bl_acme_get_temp_addr(i),
104                                               PROBE_NUM(i), TEMP_PROBE_NAME);
105                         if (status) {
106                                 /* Temperature probe detected. */
107                                 status = bl_acme_register_probe(sdi,PROBE_TEMP,
108                                         bl_acme_get_temp_addr(i), PROBE_NUM(i));
109                                 if (!status) {
110                                         sr_err("Error registering temp "
111                                                "probe %d", PROBE_NUM(i));
112                                         continue;
113                                 }
114                         }
115                 }
116         }
117
118         /*
119          * Let's assume there's no ACME device present if no probe
120          * has been registered.
121          */
122         if (!sdi->channel_groups)
123                 goto err_out;
124
125         return std_scan_complete(di, g_slist_append(NULL, sdi));
126
127 err_out:
128         g_free(devc);
129         sr_dev_inst_free(sdi);
130
131         return NULL;
132 }
133
134 static int config_get(uint32_t key, GVariant **data,
135                       const struct sr_dev_inst *sdi,
136                       const struct sr_channel_group *cg)
137 {
138         struct dev_context *devc;
139         int ret;
140         uint64_t shunt;
141         gboolean power_off;
142
143         devc = sdi->priv;
144
145         ret = SR_OK;
146         switch (key) {
147         case SR_CONF_LIMIT_SAMPLES:
148         case SR_CONF_LIMIT_MSEC:
149                 ret = sr_sw_limits_config_get(&devc->limits, key, data);
150                 break;
151         case SR_CONF_SAMPLERATE:
152                 *data = g_variant_new_uint64(devc->samplerate);
153                 break;
154         case SR_CONF_PROBE_FACTOR:
155                 if (!cg)
156                         return SR_ERR_CHANNEL_GROUP;
157                 ret = bl_acme_get_shunt(cg, &shunt);
158                 if (ret == SR_OK)
159                         *data = g_variant_new_uint64(shunt);
160                 break;
161         case SR_CONF_POWER_OFF:
162                 if (!cg)
163                         return SR_ERR_CHANNEL_GROUP;
164                 ret = bl_acme_read_power_state(cg, &power_off);
165                 if (ret == SR_OK)
166                         *data = g_variant_new_boolean(power_off);
167                 break;
168         default:
169                 return SR_ERR_NA;
170         }
171
172         return ret;
173 }
174
175 static int config_set(uint32_t key, GVariant *data,
176                       const struct sr_dev_inst *sdi,
177                       const struct sr_channel_group *cg)
178 {
179         struct dev_context *devc;
180         uint64_t samplerate;
181         int ret;
182
183         devc = sdi->priv;
184
185         ret = SR_OK;
186         switch (key) {
187         case SR_CONF_LIMIT_SAMPLES:
188         case SR_CONF_LIMIT_MSEC:
189                 ret = sr_sw_limits_config_set(&devc->limits, key, data);
190                 break;
191         case SR_CONF_SAMPLERATE:
192                 samplerate = g_variant_get_uint64(data);
193                 if (samplerate > MAX_SAMPLE_RATE) {
194                         sr_err("Maximum sample rate is %d", MAX_SAMPLE_RATE);
195                         ret = SR_ERR_SAMPLERATE;
196                         break;
197                 }
198                 devc->samplerate = samplerate;
199                 bl_acme_maybe_set_update_interval(sdi, samplerate);
200                 break;
201         case SR_CONF_PROBE_FACTOR:
202                 if (!cg)
203                         return SR_ERR_CHANNEL_GROUP;
204                 ret = bl_acme_set_shunt(cg, g_variant_get_uint64(data));
205                 break;
206         case SR_CONF_POWER_OFF:
207                 if (!cg)
208                         return SR_ERR_CHANNEL_GROUP;
209                 ret = bl_acme_set_power_off(cg, g_variant_get_boolean(data));
210                 break;
211         default:
212                 ret = SR_ERR_NA;
213         }
214
215         return ret;
216 }
217
218 static int config_list(uint32_t key, GVariant **data,
219                        const struct sr_dev_inst *sdi,
220                        const struct sr_channel_group *cg)
221 {
222         uint32_t devopts_cg[MAX_DEVOPTS_CG];
223         int num_devopts_cg = 0;
224
225         if (!cg) {
226                 switch (key) {
227                 case SR_CONF_DEVICE_OPTIONS:
228                         return STD_CONFIG_LIST(key, data, sdi, cg, NULL, drvopts, devopts);
229                 case SR_CONF_SAMPLERATE:
230                         *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates));
231                         break;
232                 default:
233                         return SR_ERR_NA;
234                 }
235         } else {
236                 switch (key) {
237                 case SR_CONF_DEVICE_OPTIONS:
238                         if (bl_acme_get_probe_type(cg) == PROBE_ENRG)
239                                 devopts_cg[num_devopts_cg++] = HAS_PROBE_FACTOR;
240                         if (bl_acme_probe_has_pws(cg))
241                                 devopts_cg[num_devopts_cg++] = HAS_POWER_OFF;
242
243                         *data = std_gvar_array_u32(devopts_cg, num_devopts_cg);
244                         break;
245                 default:
246                         return SR_ERR_NA;
247                 }
248         }
249
250         return SR_OK;
251 }
252
253 static void dev_acquisition_close(const struct sr_dev_inst *sdi)
254 {
255         GSList *chl;
256         struct sr_channel *ch;
257
258         for (chl = sdi->channels; chl; chl = chl->next) {
259                 ch = chl->data;
260                 bl_acme_close_channel(ch);
261         }
262 }
263
264 static int dev_acquisition_open(const struct sr_dev_inst *sdi)
265 {
266         GSList *chl;
267         struct sr_channel *ch;
268
269         for (chl = sdi->channels; chl; chl = chl->next) {
270                 ch = chl->data;
271                 if (bl_acme_open_channel(ch)) {
272                         sr_err("Error opening channel %s", ch->name);
273                         dev_acquisition_close(sdi);
274                         return SR_ERR;
275                 }
276         }
277
278         return 0;
279 }
280
281 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
282 {
283         struct dev_context *devc;
284         struct itimerspec tspec = {
285                 .it_interval = { 0, 0 },
286                 .it_value = { 0, 0 }
287         };
288
289         if (dev_acquisition_open(sdi))
290                 return SR_ERR;
291
292         devc = sdi->priv;
293         devc->samples_missed = 0;
294         devc->timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);
295         if (devc->timer_fd < 0) {
296                 sr_err("Error creating timer fd");
297                 return SR_ERR;
298         }
299
300         tspec.it_interval.tv_sec = 0;
301         tspec.it_interval.tv_nsec = SR_HZ_TO_NS(devc->samplerate);
302         tspec.it_value = tspec.it_interval;
303
304         if (timerfd_settime(devc->timer_fd, 0, &tspec, NULL)) {
305                 sr_err("Failed to set timer");
306                 close(devc->timer_fd);
307                 return SR_ERR;
308         }
309
310         devc->channel = g_io_channel_unix_new(devc->timer_fd);
311         g_io_channel_set_flags(devc->channel, G_IO_FLAG_NONBLOCK, NULL);
312         g_io_channel_set_encoding(devc->channel, NULL, NULL);
313         g_io_channel_set_buffered(devc->channel, FALSE);
314
315         sr_session_source_add_channel(sdi->session, devc->channel,
316                 G_IO_IN | G_IO_ERR, 1000, bl_acme_receive_data, (void *)sdi);
317
318         std_session_send_df_header(sdi);
319         sr_sw_limits_acquisition_start(&devc->limits);
320
321         return SR_OK;
322 }
323
324 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
325 {
326         struct dev_context *devc;
327
328         devc = sdi->priv;
329
330         dev_acquisition_close(sdi);
331         sr_session_source_remove_channel(sdi->session, devc->channel);
332         g_io_channel_shutdown(devc->channel, FALSE, NULL);
333         g_io_channel_unref(devc->channel);
334         devc->channel = NULL;
335
336         std_session_send_df_end(sdi);
337
338         if (devc->samples_missed > 0)
339                 sr_warn("%" PRIu64 " samples missed", devc->samples_missed);
340
341         return SR_OK;
342 }
343
344 static struct sr_dev_driver baylibre_acme_driver_info = {
345         .name = "baylibre-acme",
346         .longname = "BayLibre ACME (Another Cute Measurement Equipment)",
347         .api_version = 1,
348         .init = std_init,
349         .cleanup = std_cleanup,
350         .scan = scan,
351         .dev_list = std_dev_list,
352         .dev_clear = std_dev_clear,
353         .config_get = config_get,
354         .config_set = config_set,
355         .config_list = config_list,
356         .dev_open = std_dummy_dev_open,
357         .dev_close = std_dummy_dev_close,
358         .dev_acquisition_start = dev_acquisition_start,
359         .dev_acquisition_stop = dev_acquisition_stop,
360         .context = NULL,
361 };
362 SR_REGISTER_DEV_DRIVER(baylibre_acme_driver_info);