]> sigrok.org Git - libsigrok.git/blob - src/hardware/testo/api.c
da6d171a3ac8c52aedb64c549fb7a395c84eb096
[libsigrok.git] / src / hardware / testo / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014 Bert Vermeulen <bert@biot.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 <string.h>
21 #include "protocol.h"
22
23 #define SERIALCOMM "115200/8n1"
24
25 SR_PRIV struct sr_dev_driver testo_driver_info;
26 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
27
28 static const uint32_t scanopts[] = {
29         SR_CONF_CONN,
30 };
31
32 static const uint32_t devopts[] = {
33         SR_CONF_MULTIMETER,
34         SR_CONF_CONTINUOUS,
35         SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
36         SR_CONF_LIMIT_MSEC | SR_CONF_SET,
37 };
38
39 unsigned char TESTO_x35_REQUEST[] = { 0x12, 0, 0, 0, 1, 1, 0x55, 0xd1, 0xb7 };
40 struct testo_model models[] = {
41         { "435", 9, TESTO_x35_REQUEST },
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_config *src;
54         struct sr_dev_inst *sdi;
55         struct sr_usb_dev_inst *usb;
56         struct libusb_device_descriptor des;
57         libusb_device **devlist;
58         struct libusb_device_handle *hdl;
59         GSList *conn_devices, *devices, *l;
60         int ret, i;
61         const char *str;
62         char manufacturer[64], product[64], connection_id[64];
63
64         devices = NULL;
65         drvc = di->priv;
66         drvc->instances = NULL;
67
68         conn_devices = NULL;
69         for (l = options; l; l = l->next) {
70                 src = l->data;
71                 if (src->key != SR_CONF_CONN)
72                         continue;
73                 str = g_variant_get_string(src->data, NULL);
74                 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, str);
75         }
76
77         libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
78         for (i = 0; devlist[i]; i++) {
79                 if (conn_devices) {
80                         usb = NULL;
81                         for (l = conn_devices; l; l = l->next) {
82                                 usb = l->data;
83                                 if (usb->bus == libusb_get_bus_number(devlist[i])
84                                         && usb->address == libusb_get_device_address(devlist[i]))
85                                         break;
86                         }
87                         if (!l)
88                                 /* This device matched none of the ones that
89                                  * matched the conn specification. */
90                                 continue;
91                 }
92
93                 if ((ret = libusb_get_device_descriptor( devlist[i], &des)) != 0) {
94                         sr_warn("Failed to get device descriptor: %s.",
95                                 libusb_error_name(ret));
96                         continue;
97                 }
98
99                 if ((ret = libusb_open(devlist[i], &hdl)) < 0)
100                         continue;
101
102                 manufacturer[0] = product[0] = '\0';
103                 if (des.iManufacturer && (ret = libusb_get_string_descriptor_ascii(
104                                 hdl, des.iManufacturer, (unsigned char *) manufacturer,
105                                 sizeof(manufacturer))) < 0) {
106                         sr_warn("Failed to get manufacturer string descriptor: %s.",
107                                 libusb_error_name(ret));
108                 }
109                 if (des.iProduct && (ret = libusb_get_string_descriptor_ascii(
110                                 hdl, des.iProduct, (unsigned char *) product,
111                                 sizeof(product))) < 0) {
112                         sr_warn("Failed to get product string descriptor: %s.",
113                                 libusb_error_name(ret));
114                 }
115                 libusb_close(hdl);
116
117                 if (strncmp(manufacturer, "testo", 5))
118                         continue;
119
120                 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
121
122                 /* Hardcode the 435 for now.*/
123                 if (strcmp(product, "testo 435/635/735"))
124                         continue;
125
126                 sdi = g_malloc0(sizeof(struct sr_dev_inst));
127                 sdi->status = SR_ST_INACTIVE;
128                 sdi->vendor = g_strdup("Testo");
129                 sdi->model = g_strdup("435/635/735");
130                 sdi->driver = di;
131                 sdi->inst_type = SR_INST_USB;
132                 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
133                                 libusb_get_device_address(devlist[i]), NULL);
134                 sdi->connection_id = g_strdup(connection_id);
135                 devc = g_malloc(sizeof(struct dev_context));
136                 devc->model = &models[0];
137                 devc->limit_msec = 0;
138                 devc->limit_samples = 0;
139                 sdi->priv = devc;
140                 if (testo_probe_channels(sdi) != SR_OK)
141                         continue;
142                 drvc->instances = g_slist_append(drvc->instances, sdi);
143                 devices = g_slist_append(devices, sdi);
144         }
145         libusb_free_device_list(devlist, 1);
146         g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
147
148         return devices;
149 }
150
151 static GSList *dev_list(const struct sr_dev_driver *di)
152 {
153         return ((struct drv_context *)(di->priv))->instances;
154 }
155
156 static int dev_clear(const struct sr_dev_driver *di)
157 {
158         return std_dev_clear(di, NULL);
159 }
160
161 static int dev_open(struct sr_dev_inst *sdi)
162 {
163         struct sr_dev_driver *di = sdi->driver;
164         struct drv_context *drvc = di->priv;
165         struct sr_usb_dev_inst *usb;
166         libusb_device **devlist;
167         int ret, i;
168         char connection_id[64];
169
170         if (!di->priv) {
171                 sr_err("Driver was not initialized.");
172                 return SR_ERR;
173         }
174
175         usb = sdi->conn;
176         libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
177         for (i = 0; devlist[i]; i++) {
178                 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
179                 if (strcmp(sdi->connection_id, connection_id))
180                         continue;
181                 if ((ret = libusb_open(devlist[i], &usb->devhdl))) {
182                         sr_err("Failed to open device: %s.", libusb_error_name(ret));
183                         return SR_ERR;
184                 }
185                 break;
186         }
187         libusb_free_device_list(devlist, 1);
188         if (!devlist[i]) {
189                 sr_err("Device not found.");
190                 return SR_ERR;
191         }
192
193         if (libusb_has_capability(LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER)) {
194                 if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) {
195                         if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) {
196                                 sr_err("Failed to detach kernel driver: %s.",
197                                            libusb_error_name(ret));
198                                 return SR_ERR;
199                         }
200                 }
201         }
202
203         if ((ret = libusb_claim_interface(usb->devhdl, 0))) {
204                 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
205                 return SR_ERR;
206         }
207         sdi->status = SR_ST_ACTIVE;
208
209         return SR_OK;
210 }
211
212 static int dev_close(struct sr_dev_inst *sdi)
213 {
214         struct sr_dev_driver *di = sdi->driver;
215         struct sr_usb_dev_inst *usb;
216
217         if (!di->priv) {
218                 sr_err("Driver was not initialized.");
219                 return SR_ERR;
220         }
221
222         usb = sdi->conn;
223         if (!usb->devhdl)
224                 /*  Nothing to do. */
225                 return SR_OK;
226
227         libusb_release_interface(usb->devhdl, 0);
228         libusb_close(usb->devhdl);
229         usb->devhdl = NULL;
230         sdi->status = SR_ST_INACTIVE;
231
232         return SR_OK;
233 }
234
235 static int cleanup(const struct sr_dev_driver *di)
236 {
237         int ret;
238         struct drv_context *drvc;
239
240         if (!(drvc = di->priv))
241                 return SR_OK;
242
243         ret = dev_clear(di);
244         g_free(drvc);
245
246         return ret;
247 }
248
249 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
250                 const struct sr_channel_group *cg)
251 {
252         struct sr_usb_dev_inst *usb;
253         char str[128];
254
255         (void)cg;
256
257         switch (key) {
258         case SR_CONF_CONN:
259                 if (!sdi || !sdi->conn)
260                         return SR_ERR_ARG;
261                 usb = sdi->conn;
262                 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
263                 *data = g_variant_new_string(str);
264                 break;
265         default:
266                 return SR_ERR_NA;
267         }
268
269         return SR_OK;
270 }
271
272 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
273                 const struct sr_channel_group *cg)
274 {
275         struct sr_dev_driver *di = sdi->driver;
276         struct dev_context *devc;
277         gint64 now;
278         int ret;
279
280         (void)cg;
281
282         if (sdi->status != SR_ST_ACTIVE)
283                 return SR_ERR_DEV_CLOSED;
284
285         if (!di->priv) {
286                 sr_err("Driver was not initialized.");
287                 return SR_ERR;
288         }
289
290         devc = sdi->priv;
291         ret = SR_OK;
292         switch (key) {
293         case SR_CONF_LIMIT_MSEC:
294                 devc->limit_msec = g_variant_get_uint64(data);
295                 now = g_get_monotonic_time() / 1000;
296                 devc->end_time = now + devc->limit_msec;
297                 sr_dbg("Setting time limit to %" PRIu64 "ms.",
298                        devc->limit_msec);
299                 break;
300         case SR_CONF_LIMIT_SAMPLES:
301                 devc->limit_samples = g_variant_get_uint64(data);
302                 sr_dbg("Setting sample limit to %" PRIu64 ".",
303                        devc->limit_samples);
304                 break;
305         default:
306                 ret = SR_ERR_NA;
307         }
308
309         return ret;
310 }
311
312 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
313                 const struct sr_channel_group *cg)
314 {
315         (void)sdi;
316         (void)cg;
317
318         switch (key) {
319         case SR_CONF_SCAN_OPTIONS:
320                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
321                                 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
322                 break;
323         case SR_CONF_DEVICE_OPTIONS:
324                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
325                                 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
326                 break;
327         default:
328                 return SR_ERR_NA;
329         }
330
331         return SR_OK;
332 }
333
334 static void receive_data(struct sr_dev_inst *sdi, unsigned char *data, int len)
335 {
336         struct dev_context *devc;
337         int packet_size;
338         uint16_t crc;
339
340         devc = sdi->priv;
341
342         if (devc->reply_size + len > MAX_REPLY_SIZE) {
343                 /* Something went very wrong. */
344                 sr_dbg("Receive buffer overrun.");
345                 devc->reply_size = 0;
346                 return;
347         }
348
349         memcpy(devc->reply + devc->reply_size, data, len);
350         devc->reply_size += len;
351         /* Sixth byte contains the length of the packet. */
352         if (devc->reply_size < 7)
353                 return;
354
355         packet_size = 7 + devc->reply[6] * 7 + 2;
356         if (devc->reply_size < packet_size)
357                 return;
358
359         if (!testo_check_packet_prefix(devc->reply, devc->reply_size))
360                 return;
361
362         crc = crc16_mcrf4xx(0xffff, devc->reply, devc->reply_size - 2);
363         if (crc == RL16(&devc->reply[devc->reply_size - 2])) {
364                 testo_receive_packet(sdi);
365                 devc->num_samples++;
366         } else {
367                 sr_dbg("Packet has invalid CRC.");
368         }
369
370         devc->reply_size = 0;
371         if (devc->limit_samples && devc->num_samples >= devc->limit_samples)
372                 dev_acquisition_stop(sdi, devc->cb_data);
373         else
374                 testo_request_packet(sdi);
375
376 }
377
378 SR_PRIV void receive_transfer(struct libusb_transfer *transfer)
379 {
380         struct dev_context *devc;
381         struct sr_dev_inst *sdi;
382         int ret;
383
384         sdi = transfer->user_data;
385         devc = sdi->priv;
386         if (transfer == devc->out_transfer)
387                 /* Just the command acknowledgement. */
388                 return;
389
390         if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
391                 /* USB device was unplugged. */
392                 dev_acquisition_stop(sdi, devc->cb_data);
393         } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
394                 /* First two bytes in any transfer are FTDI status bytes. */
395                 if (transfer->actual_length > 2)
396                         receive_data(sdi, transfer->buffer + 2, transfer->actual_length - 2);
397         }
398         /* Anything else is either an error or a timeout, which is fine:
399          * we were just going to send another transfer request anyway. */
400
401         if (sdi->status == SR_ST_ACTIVE) {
402                 if ((ret = libusb_submit_transfer(transfer) != 0)) {
403                         sr_err("Unable to resubmit transfer: %s.",
404                                libusb_error_name(ret));
405                         g_free(transfer->buffer);
406                         libusb_free_transfer(transfer);
407                         dev_acquisition_stop(sdi, devc->cb_data);
408                 }
409         } else {
410                 /* This was the last transfer we're going to receive, so
411                  * clean up now. */
412                 g_free(transfer->buffer);
413                 libusb_free_transfer(transfer);
414         }
415 }
416
417 static int handle_events(int fd, int revents, void *cb_data)
418 {
419         struct sr_dev_driver *di;
420         struct dev_context *devc;
421         struct drv_context *drvc;
422         struct sr_datafeed_packet packet;
423         struct sr_dev_inst *sdi;
424         struct timeval tv;
425         gint64 now;
426
427         (void)fd;
428         (void)revents;
429
430         sdi = cb_data;
431         devc = sdi->priv;
432         di = sdi->driver;
433         drvc = di->priv;
434
435         if (devc->limit_msec) {
436                 now = g_get_monotonic_time() / 1000;
437                 if (now > devc->end_time)
438                         dev_acquisition_stop(sdi, devc->cb_data);
439         }
440
441         if (sdi->status == SR_ST_STOPPING) {
442                 usb_source_remove(sdi->session, drvc->sr_ctx);
443
444                 dev_close(sdi);
445
446                 packet.type = SR_DF_END;
447                 sr_session_send(sdi, &packet);
448         }
449
450         memset(&tv, 0, sizeof(struct timeval));
451         libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
452                         NULL);
453
454         return TRUE;
455 }
456
457 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
458 {
459         struct sr_dev_driver *di = sdi->driver;
460         struct drv_context *drvc;
461         struct dev_context *devc;
462         struct sr_usb_dev_inst *usb;
463         struct libusb_transfer *transfer;
464         int ret;
465         unsigned char *buf;
466
467         drvc = di->priv;
468         if (sdi->status != SR_ST_ACTIVE)
469                 return SR_ERR_DEV_CLOSED;
470
471         if (!di->priv) {
472                 sr_err("Driver was not initialized.");
473                 return SR_ERR;
474         }
475
476         devc = sdi->priv;
477         usb = sdi->conn;
478         devc->cb_data = cb_data;
479         devc->end_time = 0;
480         devc->num_samples = 0;
481         devc->reply_size = 0;
482
483         /* Send header packet to the session bus. */
484         std_session_send_df_header(cb_data, LOG_PREFIX);
485
486         usb_source_add(sdi->session, drvc->sr_ctx, 100,
487                         handle_events, (void *)sdi);
488
489         if (testo_set_serial_params(usb) != SR_OK)
490                 return SR_ERR;
491
492         devc->out_transfer = libusb_alloc_transfer(0);
493         if (testo_request_packet(sdi) != SR_OK)
494                 return SR_ERR;
495
496         buf = g_try_malloc(MAX_REPLY_SIZE);
497         transfer = libusb_alloc_transfer(0);
498         libusb_fill_bulk_transfer(transfer, usb->devhdl, EP_IN, buf,
499                         MAX_REPLY_SIZE, receive_transfer, (void *)sdi, 100);
500         if ((ret = libusb_submit_transfer(transfer) != 0)) {
501                 sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
502                 libusb_free_transfer(transfer);
503                 g_free(buf);
504                 return SR_ERR;
505         }
506         devc->reply_size = 0;
507
508         return SR_OK;
509 }
510
511 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
512 {
513         struct sr_dev_driver *di = sdi->driver;
514         (void)cb_data;
515
516         if (!di->priv) {
517                 sr_err("Driver was not initialized.");
518                 return SR_ERR;
519         }
520
521         if (sdi->status != SR_ST_ACTIVE)
522                 return SR_ERR_DEV_CLOSED;
523
524         sdi->status = SR_ST_STOPPING;
525
526         return SR_OK;
527 }
528
529 SR_PRIV struct sr_dev_driver testo_driver_info = {
530         .name = "testo",
531         .longname = "Testo",
532         .api_version = 1,
533         .init = init,
534         .cleanup = cleanup,
535         .scan = scan,
536         .dev_list = dev_list,
537         .dev_clear = dev_clear,
538         .config_get = config_get,
539         .config_set = config_set,
540         .config_list = config_list,
541         .dev_open = dev_open,
542         .dev_close = dev_close,
543         .dev_acquisition_start = dev_acquisition_start,
544         .dev_acquisition_stop = dev_acquisition_stop,
545         .priv = NULL,
546 };