]> sigrok.org Git - libsigrok.git/blob - hardware/brymen-dmm/api.c
8ff9c2e9405654c685112b77cb5ba414be6fd75e
[libsigrok.git] / hardware / brymen-dmm / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.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 <glib.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <fcntl.h>
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
26 #include "protocol.h"
27
28 static const int hwopts[] = {
29         SR_CONF_CONN,
30         SR_CONF_SERIALCOMM,
31         0,
32 };
33
34 static const int hwcaps[] = {
35         SR_CONF_MULTIMETER,
36         SR_CONF_LIMIT_SAMPLES,
37         SR_CONF_CONTINUOUS,
38         SR_CONF_LIMIT_MSEC,
39         0,
40 };
41
42 SR_PRIV struct sr_dev_driver brymen_dmm_driver_info;
43 static struct sr_dev_driver *di = &brymen_dmm_driver_info;
44
45 static int hw_init(struct sr_context *sr_ctx)
46 {
47         return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
48 }
49
50 static void free_instance(void *inst)
51 {
52         struct sr_dev_inst *sdi;
53         struct dev_context *devc;
54         if (!(sdi = inst))
55                 return;
56         if (!(devc = sdi->priv))
57                 return;
58         sr_serial_dev_inst_free(devc->serial);
59         sr_dev_inst_free(sdi);
60 }
61
62 /* Properly close and free all devices. */
63 static int clear_instances(void)
64 {
65         struct drv_context *drvc;
66
67         if (!(drvc = di->priv))
68                 return SR_OK;
69
70         g_slist_free_full(drvc->instances, free_instance);
71         drvc->instances = NULL;
72
73         return SR_OK;
74 }
75
76 static GSList *brymen_scan(const char *conn, const char *serialcomm)
77 {
78         struct sr_dev_inst *sdi;
79         struct dev_context *devc;
80         struct drv_context *drvc;
81         struct sr_probe *probe;
82         struct sr_serial_dev_inst *serial;
83         GSList *devices;
84         int ret;
85
86         if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
87                 return NULL;
88
89         if (serial_open(serial, SERIAL_RDWR|SERIAL_NONBLOCK) != SR_OK)
90                 return NULL;
91
92         sr_info("Probing port %s.", conn);
93
94         devices = NULL;
95
96         /* Request reading */
97         if (brymen_packet_request(serial) == -1) {
98                 sr_err("Unable to send command. code: %d.", errno);
99                 goto scan_cleanup;
100         }
101
102         uint8_t buf[128];
103         size_t len = 128;
104
105         ret = brymen_stream_detect(serial, buf, &len, brymen_packet_length,
106                              brymen_packet_is_valid, 1000, 9600);
107         if (ret != SR_OK)
108                 goto scan_cleanup;
109
110         sr_info("Found device on port %s.", conn);
111
112         if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Brymen", "BM85x", "")))
113                 goto scan_cleanup;
114
115         if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
116                 sr_err("Device context malloc failed.");
117                 goto scan_cleanup;
118         }
119
120         devc->serial = serial;
121         drvc = di->priv;
122         sdi->priv = devc;
123         sdi->driver = di;
124
125         if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
126                 goto scan_cleanup;
127
128         sdi->probes = g_slist_append(sdi->probes, probe);
129         drvc->instances = g_slist_append(drvc->instances, sdi);
130         devices = g_slist_append(devices, sdi);
131
132
133 scan_cleanup:
134         serial_close(serial);
135
136         return devices;
137 }
138
139 static GSList *hw_scan(GSList *options)
140 {
141         struct drv_context *drvc;
142         struct sr_config *src;
143         GSList *devices, *l;
144         const char *conn, *serialcomm;
145
146         (void)options;
147
148         devices = NULL;
149         drvc = di->priv;
150         drvc->instances = NULL;
151
152         conn = serialcomm = NULL;
153         for (l = options; l; l = l->next) {
154                 src = l->data;
155                 switch (src->key) {
156                         case SR_CONF_CONN:
157                                 conn = src->value;
158                                 break;
159                         case SR_CONF_SERIALCOMM:
160                                 serialcomm = src->value;
161                                 break;
162                 }
163         }
164         if (!conn) {
165                 return NULL;
166         }
167
168         if (serialcomm) {
169                 /* Use the provided comm specs. */
170                 devices = brymen_scan(conn, serialcomm);
171         } else {
172                 /* But 9600 8N1 should work all of the time */
173                 devices = brymen_scan(conn, "9600/8n1/dtr=1/rts=1");
174         }
175
176         return devices;
177 }
178
179 static GSList *hw_dev_list(void)
180 {
181         struct drv_context *drvc;
182
183         drvc = di->priv;
184
185         return drvc->instances;
186 }
187
188 static int hw_dev_open(struct sr_dev_inst *sdi)
189 {
190         struct dev_context *devc;
191
192         if (!(devc = sdi->priv)) {
193                 sr_err("sdi->priv was NULL.");
194                 return SR_ERR_BUG;
195         }
196
197         if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
198                 return SR_ERR;
199
200         sdi->status = SR_ST_ACTIVE;
201
202         return SR_OK;
203 }
204
205 static int hw_dev_close(struct sr_dev_inst *sdi)
206 {
207         struct dev_context *devc;
208
209         if (!(devc = sdi->priv)) {
210                 sr_err("sdi->priv was NULL.");
211                 return SR_ERR_BUG;
212         }
213
214         if (devc->serial && devc->serial->fd != -1) {
215                 serial_close(devc->serial);
216                 sdi->status = SR_ST_INACTIVE;
217         }
218
219         return SR_OK;
220 }
221
222 static int hw_cleanup(void)
223 {
224         clear_instances();
225
226         return SR_OK;
227 }
228
229 static int config_list(int key, const void **data,
230                        const struct sr_dev_inst *sdi)
231 {
232         (void)sdi;
233
234         switch (key) {
235                 case SR_CONF_SCAN_OPTIONS:
236                         *data = hwopts;
237                         break;
238                 case SR_CONF_DEVICE_OPTIONS:
239                         *data = hwcaps;
240                         break;
241         default:
242                 sr_err("Unknown config key: %d.", key);
243                 return SR_ERR_ARG;
244         }
245
246         return SR_OK;
247 }
248
249 static int hw_dev_config_set(int id, const void *value,
250                              const struct sr_dev_inst *sdi)
251 {
252         struct dev_context *devc;
253         int ret;
254
255         if (sdi->status != SR_ST_ACTIVE) {
256                 sr_err("Device inactive, can't set config options.");
257                 return SR_ERR;
258         }
259
260         if (!(devc = sdi->priv)) {
261                 sr_err("sdi->priv was NULL.");
262                 return SR_ERR_BUG;
263         }
264
265         ret = SR_OK;
266         switch (id) {
267                 case SR_CONF_LIMIT_SAMPLES:
268                 devc->limit_samples = *(const uint64_t*)value;
269                 break;
270                 case SR_CONF_LIMIT_MSEC:
271                 devc->limit_msec = *(const uint64_t*)value;
272                 break;
273         default:
274                 sr_err("Unknown hardware capability: %d.", id);
275                 ret = SR_ERR_ARG;
276         }
277
278         return ret;
279 }
280
281 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
282                                     void *cb_data)
283 {
284         struct sr_datafeed_packet packet;
285         struct sr_datafeed_header header;
286         struct dev_context *devc;
287
288         if (!(devc = sdi->priv)) {
289                 sr_err("sdi->priv was NULL.");
290                 return SR_ERR_BUG;
291         }
292
293         sr_dbg("Starting acquisition.");
294
295         devc->cb_data = cb_data;
296
297         /*
298          * Reset the number of samples to take. If we've already collected our
299          * quota, but we start a new session, and don't reset this, we'll just
300          * quit without acquiring any new samples.
301          */
302         devc->num_samples = 0;
303         devc->starttime = g_get_monotonic_time();
304
305         /* Send header packet to the session bus. */
306         sr_dbg("Sending SR_DF_HEADER.");
307         packet.type = SR_DF_HEADER;
308         packet.payload = &header;
309         header.feed_version = 1;
310         gettimeofday(&header.starttime, NULL);
311         sr_session_send(devc->cb_data, &packet);
312
313         /* Poll every 50ms, or whenever some data comes in. */
314         sr_source_add(devc->serial->fd, G_IO_IN, 50,
315                       brymen_dmm_receive_data, (void *)sdi);
316
317         return SR_OK;
318 }
319
320 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
321 {
322         struct sr_datafeed_packet packet;
323         struct dev_context *devc;
324
325         if (sdi->status != SR_ST_ACTIVE) {
326                 sr_err("Device inactive, can't stop acquisition.");
327                 return SR_ERR;
328         }
329
330         if (!(devc = sdi->priv)) {
331                 sr_err("sdi->priv was NULL.");
332                 return SR_ERR_BUG;
333         }
334
335         sr_dbg("Stopping acquisition.");
336
337         sr_source_remove(devc->serial->fd);
338         hw_dev_close((struct sr_dev_inst *)sdi);
339
340         /* Send end packet to the session bus. */
341         sr_dbg("Sending SR_DF_END.");
342         packet.type = SR_DF_END;
343         sr_session_send(cb_data, &packet);
344
345         return SR_OK;
346 }
347
348 SR_PRIV struct sr_dev_driver brymen_dmm_driver_info = {
349         .name = "brymen-dmm",
350         .longname = "Brymen BM850 series",
351         .api_version = 1,
352         .init = hw_init,
353         .cleanup = hw_cleanup,
354         .scan = hw_scan,
355         .dev_list = hw_dev_list,
356         .dev_clear = clear_instances,
357         .dev_open = hw_dev_open,
358         .dev_close = hw_dev_close,
359         .config_list = config_list,
360         .config_set = hw_dev_config_set,
361         .dev_acquisition_start = hw_dev_acquisition_start,
362         .dev_acquisition_stop = hw_dev_acquisition_stop,
363         .priv = NULL,
364 };