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