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