]> sigrok.org Git - libsigrok.git/blob - src/hardware/baylibre-acme/api.c
Pass driver struct pointer to driver callbacks.
[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 "protocol.h"
21
22 SR_PRIV struct sr_dev_driver baylibre_acme_driver_info;
23
24 static const uint32_t devopts[] = {
25         SR_CONF_CONTINUOUS | SR_CONF_SET,
26         SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
27         SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
28         SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
29 };
30
31 static const uint32_t devopts_cg[] = {
32         SR_CONF_PROBE_FACTOR | SR_CONF_GET | SR_CONF_SET,
33         SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET,
34 };
35
36 #define MAX_SAMPLE_RATE 500 /* In Hz */
37
38 static const uint64_t samplerates[] = {
39         SR_HZ(1),
40         SR_HZ(MAX_SAMPLE_RATE),
41         SR_HZ(1),
42 };
43
44 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
45 {
46         return std_init(sr_ctx, di, LOG_PREFIX);
47 }
48
49 static GSList *scan(struct sr_dev_driver *di, GSList *options)
50 {
51         struct drv_context *drvc;
52         struct dev_context *devc;
53         struct sr_dev_inst *sdi;
54         GSList *devices;
55         gboolean status;
56         int i;
57
58         (void)options;
59
60         drvc = di->priv;
61         devices = NULL;
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->driver = di;
71         sdi->priv = devc;
72
73         status = bl_acme_is_sane();
74         if (!status)
75                 goto err_out;
76
77         /*
78          * Iterate over all ACME connectors and check if any probes
79          * are present.
80          */
81         for (i = 0; i < MAX_PROBES; i++) {
82                 /*
83                  * First check if there's an energy probe on this connector. If
84                  * not, and we're already at the fifth probe - see if we can
85                  * detect a temperature probe.
86                  */
87                 status = bl_acme_detect_probe(bl_acme_get_enrg_addr(i),
88                                               PROBE_NUM(i), ENRG_PROBE_NAME);
89                 if (status) {
90                         /* Energy probe detected. */
91                         status = bl_acme_register_probe(sdi, PROBE_ENRG,
92                                         bl_acme_get_enrg_addr(i), PROBE_NUM(i));
93                         if (!status) {
94                                 sr_err("Error registering power probe %d",
95                                        PROBE_NUM(i));
96                                 continue;
97                         }
98                 } else if (i >= TEMP_PRB_START_INDEX) {
99                         status = bl_acme_detect_probe(bl_acme_get_temp_addr(i),
100                                               PROBE_NUM(i), TEMP_PROBE_NAME);
101                         if (status) {
102                                 /* Temperature probe detected. */
103                                 status = bl_acme_register_probe(sdi,PROBE_TEMP,
104                                         bl_acme_get_temp_addr(i), PROBE_NUM(i));
105                                 if (!status) {
106                                         sr_err("Error registering temp "
107                                                "probe %d", PROBE_NUM(i));
108                                         continue;
109                                 }
110                         }
111                 }
112         }
113
114         /*
115          * Let's assume there's no ACME device present if no probe
116          * has been registered.
117          */
118         if (sdi->channel_groups == NULL)
119                 goto err_out;
120
121         devices = g_slist_append(devices, sdi);
122         drvc->instances = g_slist_append(drvc->instances, sdi);
123
124         return devices;
125
126 err_out:
127         g_free(devc);
128         sr_dev_inst_free(sdi);
129
130         return NULL;
131 }
132
133 static GSList *dev_list(const struct sr_dev_driver *di)
134 {
135         return ((struct drv_context *)(di->priv))->instances;
136 }
137
138 static int dev_clear(const struct sr_dev_driver *di)
139 {
140         return std_dev_clear(di, NULL);
141 }
142
143 static int dev_open(struct sr_dev_inst *sdi)
144 {
145         (void)sdi;
146
147         /* Nothing to do here. */
148         sdi->status = SR_ST_ACTIVE;
149
150         return SR_OK;
151 }
152
153 static int dev_close(struct sr_dev_inst *sdi)
154 {
155         (void)sdi;
156
157         /* Nothing to do here. */
158         sdi->status = SR_ST_INACTIVE;
159
160         return SR_OK;
161 }
162
163 static int cleanup(const struct sr_dev_driver *di)
164 {
165         dev_clear(di);
166
167         return SR_OK;
168 }
169
170 static int config_get(uint32_t key, GVariant **data,
171                       const struct sr_dev_inst *sdi,
172                       const struct sr_channel_group *cg)
173 {
174         struct dev_context *devc;
175         int ret;
176         uint64_t shunt;
177         gboolean power_off;
178
179         devc = sdi->priv;
180
181         ret = SR_OK;
182         switch (key) {
183         case SR_CONF_LIMIT_SAMPLES:
184                 *data = g_variant_new_uint64(devc->limit_samples);
185                 break;
186         case SR_CONF_LIMIT_MSEC:
187                 *data = g_variant_new_uint64(devc->limit_msec);
188                 break;
189         case SR_CONF_SAMPLERATE:
190                 *data = g_variant_new_uint64(devc->samplerate);
191                 break;
192         case SR_CONF_PROBE_FACTOR:
193                 if (!cg)
194                         return SR_ERR_CHANNEL_GROUP;
195                 ret = bl_acme_get_shunt(cg, &shunt);
196                 if (ret == SR_OK)
197                         *data = g_variant_new_uint64(shunt);
198                 break;
199         case SR_CONF_POWER_OFF:
200                 if (!cg)
201                         return SR_ERR_CHANNEL_GROUP;
202                 ret = bl_acme_read_power_state(cg, &power_off);
203                 if (ret == SR_OK)
204                         *data = g_variant_new_boolean(power_off);
205                 break;
206         default:
207                 return SR_ERR_NA;
208         }
209
210         return ret;
211 }
212
213 static int config_set(uint32_t key, GVariant *data,
214                       const struct sr_dev_inst *sdi,
215                       const struct sr_channel_group *cg)
216 {
217         struct dev_context *devc;
218         uint64_t samplerate;
219         int ret;
220
221         if (sdi->status != SR_ST_ACTIVE)
222                 return SR_ERR_DEV_CLOSED;
223
224         devc = sdi->priv;
225
226         ret = SR_OK;
227         switch (key) {
228         case SR_CONF_LIMIT_SAMPLES:
229                 devc->limit_samples = g_variant_get_uint64(data);
230                 devc->limit_msec = 0;
231                 sr_dbg("Setting sample limit to %" PRIu64, devc->limit_samples);
232                 break;
233         case SR_CONF_LIMIT_MSEC:
234                 devc->limit_msec = g_variant_get_uint64(data) * 1000;
235                 devc->limit_samples = 0;
236                 sr_dbg("Setting time limit to %" PRIu64"ms", devc->limit_msec);
237                 break;
238         case SR_CONF_SAMPLERATE:
239                 samplerate = g_variant_get_uint64(data);
240                 if (samplerate > MAX_SAMPLE_RATE) {
241                         sr_err("Maximum sample rate is %d", MAX_SAMPLE_RATE);
242                         ret = SR_ERR_SAMPLERATE;
243                         break;
244                 }
245                 devc->samplerate = samplerate;
246                 sr_dbg("Setting samplerate to %" PRIu64, devc->samplerate);
247                 break;
248         case SR_CONF_PROBE_FACTOR:
249                 if (!cg)
250                         return SR_ERR_CHANNEL_GROUP;
251                 ret = bl_acme_set_shunt(cg, g_variant_get_uint64(data));
252                 break;
253         case SR_CONF_POWER_OFF:
254                 if (!cg)
255                         return SR_ERR_CHANNEL_GROUP;
256                 ret = bl_acme_set_power_off(cg, g_variant_get_boolean(data));
257                 break;
258         default:
259                 ret = SR_ERR_NA;
260         }
261
262         return ret;
263 }
264
265 static int config_list(uint32_t key, GVariant **data,
266                        const struct sr_dev_inst *sdi,
267                        const struct sr_channel_group *cg)
268 {
269         GVariant *gvar;
270         GVariantBuilder gvb;
271         int ret;
272
273         (void)sdi;
274         (void)cg;
275
276         ret = SR_OK;
277         if (!cg) {
278                 switch (key) {
279                 case SR_CONF_DEVICE_OPTIONS:
280                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
281                                 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
282                         break;
283                 case SR_CONF_SAMPLERATE:
284                         g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
285                         gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
286                                 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
287                         g_variant_builder_add(&gvb, "{sv}",
288                                               "samplerate-steps", gvar);
289                         *data = g_variant_builder_end(&gvb);
290                         break;
291                 default:
292                         return SR_ERR_NA;
293                 }
294         } else {
295                 switch (key) {
296                 case SR_CONF_DEVICE_OPTIONS:
297                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
298                                 devopts_cg, ARRAY_SIZE(devopts_cg), sizeof(uint32_t));
299                         break;
300                 default:
301                         return SR_ERR_NA;
302                 }
303         }
304
305         return ret;
306 }
307
308 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
309 {
310         struct dev_context *devc;
311
312         (void)cb_data;
313
314         if (sdi->status != SR_ST_ACTIVE)
315                 return SR_ERR_DEV_CLOSED;
316
317         devc = sdi->priv;
318         devc->samples_read = 0;
319
320         if (pipe(devc->pipe_fds)) {
321                 sr_err("Error setting up pipe");
322                 return SR_ERR;
323         }
324
325         devc->channel = g_io_channel_unix_new(devc->pipe_fds[0]);
326         g_io_channel_set_flags(devc->channel, G_IO_FLAG_NONBLOCK, NULL);
327         g_io_channel_set_encoding(devc->channel, NULL, NULL);
328         g_io_channel_set_buffered(devc->channel, FALSE);
329
330         sr_session_source_add_channel(sdi->session, devc->channel,
331                 G_IO_IN | G_IO_ERR, 1, bl_acme_receive_data, (void *)sdi);
332
333         /* Send header packet to the session bus. */
334         std_session_send_df_header(sdi, LOG_PREFIX);
335         devc->start_time = g_get_monotonic_time();
336
337         return SR_OK;
338 }
339
340 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
341 {
342         struct sr_datafeed_packet packet;
343         struct dev_context *devc;
344
345         (void)cb_data;
346
347         devc = sdi->priv;
348
349         if (sdi->status != SR_ST_ACTIVE)
350                 return SR_ERR_DEV_CLOSED;
351
352         sr_session_source_remove_channel(sdi->session, devc->channel);
353         g_io_channel_shutdown(devc->channel, FALSE, NULL);
354         g_io_channel_unref(devc->channel);
355         devc->channel = NULL;
356
357         /* Send last packet. */
358         packet.type = SR_DF_END;
359         sr_session_send(sdi, &packet);
360
361         return SR_OK;
362 }
363
364 SR_PRIV struct sr_dev_driver baylibre_acme_driver_info = {
365         .name = "baylibre-acme",
366         .longname = "BayLibre ACME (Another Cute Measurement Equipment)",
367         .api_version = 1,
368         .init = init,
369         .cleanup = cleanup,
370         .scan = scan,
371         .dev_list = dev_list,
372         .dev_clear = dev_clear,
373         .config_get = config_get,
374         .config_set = config_set,
375         .config_list = config_list,
376         .dev_open = dev_open,
377         .dev_close = dev_close,
378         .dev_acquisition_start = dev_acquisition_start,
379         .dev_acquisition_stop = dev_acquisition_stop,
380         .priv = NULL,
381 };