]> sigrok.org Git - libsigrok.git/blob - hardware/radioshack-dmm/api.c
Return SR_ERR_MALLOC upon allocation errors.
[libsigrok.git] / hardware / radioshack-dmm / api.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
5  * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <glib.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <errno.h>
27 #include "libsigrok.h"
28 #include "libsigrok-internal.h"
29 #include "radioshack-dmm.h"
30
31 static const int hwopts[] = {
32         SR_HWOPT_CONN,
33         SR_HWOPT_SERIALCOMM,
34         0,
35 };
36
37 static const int hwcaps[] = {
38         SR_HWCAP_MULTIMETER,
39         SR_HWCAP_LIMIT_SAMPLES,
40         SR_HWCAP_CONTINUOUS,
41         0,
42 };
43
44 static const char *probe_names[] = {
45         "Probe",
46         NULL,
47 };
48
49 SR_PRIV struct sr_dev_driver radioshackdmm_driver_info;
50 static struct sr_dev_driver *di = &radioshackdmm_driver_info;
51
52 static const struct radioshackdmm_profile supported_radioshackdmm[] = {
53         { RADIOSHACK_22_812, "22-812", 100 },
54 };
55
56 /* Properly close and free all devices. */
57 static int clear_instances(void)
58 {
59         struct sr_dev_inst *sdi;
60         struct drv_context *drvc;
61         struct dev_context *devc;
62         GSList *l;
63
64         if (!(drvc = di->priv))
65                 return SR_OK;
66
67         drvc = di->priv;
68         for (l = drvc->instances; l; l = l->next) {
69                 if (!(sdi = l->data))
70                         continue;
71                 if (!(devc = sdi->priv))
72                         continue;
73                 sr_serial_dev_inst_free(devc->serial);
74                 sr_dev_inst_free(sdi);
75         }
76         g_slist_free(drvc->instances);
77         drvc->instances = NULL;
78
79         return SR_OK;
80 }
81
82 static int hw_init(void)
83 {
84         struct drv_context *drvc;
85
86         if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
87                 sr_err("Driver context malloc failed.");
88                 return SR_ERR_MALLOC;
89         }
90
91         di->priv = drvc;
92
93         return SR_OK;
94 }
95
96 static GSList *rs_22_812_scan(const char *conn, const char *serialcomm)
97 {
98         struct sr_dev_inst *sdi;
99         struct drv_context *drvc;
100         struct dev_context *devc;
101         struct sr_probe *probe;
102         GSList *devices;
103         int fd, retry;
104         size_t len;
105         char buf[128], *b;
106
107         if ((fd = serial_open(conn, O_RDONLY|O_NONBLOCK)) == -1) {
108                 sr_err("Unable to open %s: %s.", conn, strerror(errno));
109                 return NULL;
110         }
111         if (serial_set_paramstr(fd, serialcomm) != SR_OK) {
112                 sr_err("Unable to set serial parameters.");
113                 return NULL;
114         }
115
116         sr_info("Probing port %s readonly.", conn);
117
118         drvc = di->priv;
119         b = buf;
120         retry = 0;
121         devices = NULL;
122         /* There's no way to get an ID from the multimeter. It just sends data
123          * periodically, so the best we can do is check if the packets match the
124          * expected format. */
125         while (!devices && retry < 3)
126         {
127                 size_t i;
128                 size_t good_packets = 0;
129                 retry++;
130                 serial_flush(fd);
131
132                 /* Let's get a bit of data and see if we can find a packet */
133                 len = sizeof(buf);
134                 serial_readline(fd, &b, &len, 250);
135                 if( (len == 0) || (len < RS_22_812_PACKET_SIZE) ) {
136                         /* Not enough data received, is the DMM connected ? */
137                         continue;
138                 }
139
140                 /* Let's treat our buffer like a stream, and find any
141                  * valid packets */
142                 for( i = 0; i < len - RS_22_812_PACKET_SIZE + 1;
143                     /* don't increment i here */ )
144                 {
145                         const rs_22_812_packet *packet = (void *)(&buf[i]);
146                         if( !rs_22_812_is_packet_valid(packet) ){
147                                 i++;
148                                 continue;
149                         }
150                         good_packets++;
151                         i += RS_22_812_PACKET_SIZE;
152                 }
153
154                 /* If we dropped more than two packets worth of data, something
155                  * is wrong */
156                 size_t dropped = len - (good_packets * RS_22_812_PACKET_SIZE);
157                 if(dropped > 2 * RS_22_812_PACKET_SIZE)
158                         continue;
159
160                 /* Let's see if we have anything good */
161                 if (good_packets == 0)
162                         continue;
163
164                 sr_info("Found RS 22-812 on port %s.", conn);
165
166                 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "RadioShack",
167                                             "22-812", "")))
168                         return NULL;
169                 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
170                         sr_err("Device context malloc failed.");
171                         return NULL;
172                 }
173
174                 /* devc->profile = RADIOSHACK_22_812; */
175                 devc->serial = sr_serial_dev_inst_new(conn, -1);
176                 devc->serialcomm = g_strdup(serialcomm);
177
178                 sdi->priv = devc;
179                 sdi->driver = di;
180                 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
181                         return NULL;
182                 sdi->probes = g_slist_append(sdi->probes, probe);
183                 drvc->instances = g_slist_append(drvc->instances, sdi);
184                 devices = g_slist_append(devices, sdi);
185                 break;
186         }
187         serial_close(fd);
188
189         return devices;
190 }
191
192 static GSList *hw_scan(GSList *options)
193 {
194         struct sr_hwopt *opt;
195         GSList *l, *devices;
196         const char *conn, *serialcomm;
197
198         conn = serialcomm = NULL;
199         for (l = options; l; l = l->next) {
200                 opt = l->data;
201                 switch (opt->hwopt) {
202                 case SR_HWOPT_CONN:
203                         conn = opt->value;
204                         break;
205                 case SR_HWOPT_SERIALCOMM:
206                         serialcomm = opt->value;
207                         break;
208                 }
209         }
210         if (!conn)
211                 return NULL;
212
213         if (serialcomm) {
214                 /* Use the provided comm specs. */
215                 devices = rs_22_812_scan(conn, serialcomm);
216         } else {
217                 /* Then try the default 4800 8n1 */
218                 devices = rs_22_812_scan(conn, "4800/8n1");
219         }
220
221         return devices;
222 }
223
224 static GSList *hw_dev_list(void)
225 {
226         struct drv_context *drvc;
227
228         drvc = di->priv;
229
230         return drvc->instances;
231 }
232
233 static int hw_dev_open(struct sr_dev_inst *sdi)
234 {
235         struct dev_context *devc;
236
237         if (!(devc = sdi->priv)) {
238                 sr_err("sdi->priv was NULL.");
239                 return SR_ERR_BUG;
240         }
241
242         devc->serial->fd = serial_open(devc->serial->port, O_RDONLY);
243         if (devc->serial->fd == -1) {
244                 sr_err("Couldn't open serial port '%s'.",
245                        devc->serial->port);
246                 return SR_ERR;
247         }
248         if (serial_set_paramstr(devc->serial->fd, devc->serialcomm) != SR_OK) {
249                 sr_err("Unable to set serial parameters.");
250                 return SR_ERR;
251         }
252         sdi->status = SR_ST_ACTIVE;
253
254         return SR_OK;
255 }
256
257 static int hw_dev_close(struct sr_dev_inst *sdi)
258 {
259         struct dev_context *devc;
260
261         if (!(devc = sdi->priv)) {
262                 sr_err("sdi->priv was NULL.");
263                 return SR_ERR_BUG;
264         }
265
266         if (devc->serial && devc->serial->fd != -1) {
267                 serial_close(devc->serial->fd);
268                 devc->serial->fd = -1;
269                 sdi->status = SR_ST_INACTIVE;
270         }
271
272         return SR_OK;
273 }
274
275 static int hw_cleanup(void)
276 {
277         clear_instances();
278
279         return SR_OK;
280 }
281
282 static int hw_info_get(int info_id, const void **data,
283        const struct sr_dev_inst *sdi)
284 {
285         (void)sdi; /* Does nothing. prevents "unused parameter" warning */
286
287         switch (info_id) {
288         case SR_DI_HWOPTS:
289                 *data = hwopts;
290                 break;
291         case SR_DI_HWCAPS:
292                 *data = hwcaps;
293                 break;
294         case SR_DI_NUM_PROBES:
295                 *data = GINT_TO_POINTER(1);
296                 break;
297         case SR_DI_PROBE_NAMES:
298                 *data = probe_names;
299                 break;
300         default:
301                 return SR_ERR_ARG;
302         }
303
304         return SR_OK;
305 }
306
307 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
308                 const void *value)
309 {
310         struct dev_context *devc;
311
312         if (sdi->status != SR_ST_ACTIVE)
313                 return SR_ERR;
314
315         if (!(devc = sdi->priv)) {
316                 sr_err("sdi->priv was NULL.");
317                 return SR_ERR_BUG;
318         }
319
320         switch (hwcap) {
321         case SR_HWCAP_LIMIT_SAMPLES:
322                 devc->limit_samples = *(const uint64_t *)value;
323                 sr_dbg("Setting sample limit to %" PRIu64 ".",
324                        devc->limit_samples);
325                 break;
326         default:
327                 sr_err("Unknown capability: %d.", hwcap);
328                 return SR_ERR;
329                 break;
330         }
331
332         return SR_OK;
333 }
334
335 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
336                 void *cb_data)
337 {
338         struct sr_datafeed_packet packet;
339         struct sr_datafeed_header header;
340         struct sr_datafeed_meta_analog meta;
341         struct dev_context *devc;
342
343         if (!(devc = sdi->priv)) {
344                 sr_err("sdi->priv was NULL.");
345                 return SR_ERR_BUG;
346         }
347
348         sr_dbg("Starting acquisition.");
349
350         devc->cb_data = cb_data;
351
352         /* Reset the number of samples to take. If we've already collected our
353          * quota, but we start a new session, and don't reset this, we'll just
354          * quit without aquiring any new samples */
355         devc->num_samples = 0;
356
357         /* Send header packet to the session bus. */
358         sr_dbg("Sending SR_DF_HEADER.");
359         packet.type = SR_DF_HEADER;
360         packet.payload = (uint8_t *)&header;
361         header.feed_version = 1;
362         gettimeofday(&header.starttime, NULL);
363         sr_session_send(devc->cb_data, &packet);
364
365         /* Send metadata about the SR_DF_ANALOG packets to come. */
366         sr_dbg("Sending SR_DF_META_ANALOG.");
367         packet.type = SR_DF_META_ANALOG;
368         packet.payload = &meta;
369         meta.num_probes = 1;
370         sr_session_send(devc->cb_data, &packet);
371
372         /* Poll every 100ms, or whenever some data comes in. */
373         sr_source_add(devc->serial->fd, G_IO_IN, 50,
374                       radioshack_receive_data, (void *)sdi );
375
376         return SR_OK;
377 }
378
379 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
380                 void *cb_data)
381 {
382         struct sr_datafeed_packet packet;
383         struct dev_context *devc;
384
385         if (sdi->status != SR_ST_ACTIVE)
386                 return SR_ERR;
387
388         if (!(devc = sdi->priv)) {
389                 sr_err("sdi->priv was NULL.");
390                 return SR_ERR_BUG;
391         }
392
393         sr_dbg("Stopping acquisition.");
394
395         sr_source_remove(devc->serial->fd);
396         hw_dev_close((struct sr_dev_inst *)sdi);
397
398         /* Send end packet to the session bus. */
399         sr_dbg("Sending SR_DF_END.");
400         packet.type = SR_DF_END;
401         sr_session_send(cb_data, &packet);
402
403         return SR_OK;
404 }
405
406 SR_PRIV struct sr_dev_driver radioshackdmm_driver_info = {
407         .name = "radioshack-dmm",
408         .longname = "Radioshack 22-812/22-039 DMMs",
409         .api_version = 1,
410         .init = hw_init,
411         .cleanup = hw_cleanup,
412         .scan = hw_scan,
413         .dev_list = hw_dev_list,
414         .dev_clear = clear_instances,
415         .dev_open = hw_dev_open,
416         .dev_close = hw_dev_close,
417         .info_get = hw_info_get,
418         .dev_config_set = hw_dev_config_set,
419         .dev_acquisition_start = hw_dev_acquisition_start,
420         .dev_acquisition_stop = hw_dev_acquisition_stop,
421         .priv = NULL,
422 };