2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
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.
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.
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/>.
22 #include "libsigrok.h"
23 #include "libsigrok-internal.h"
25 #include <sys/types.h>
31 static const int hwopts[] = {
37 static const int hwcaps[] = {
39 SR_HWCAP_LIMIT_SAMPLES,
44 static const char *probe_names[] = {
49 SR_PRIV struct sr_dev_driver tekpower_driver_info;
50 static struct sr_dev_driver *di = &tekpower_driver_info;
52 /* Properly close and free all devices. */
53 static int clear_instances(void)
55 struct sr_dev_inst *sdi;
56 struct drv_context *drvc;
57 struct dev_context *devc;
60 if (!(drvc = di->priv))
64 for (l = drvc->instances; l; l = l->next) {
67 if (!(devc = sdi->priv))
69 sr_serial_dev_inst_free(devc->serial);
70 sr_dev_inst_free(sdi);
72 g_slist_free(drvc->instances);
73 drvc->instances = NULL;
78 static int hw_init(void)
80 struct drv_context *drvc;
82 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
83 sr_err("driver context malloc failed.");
92 static int serial_readline(int fd, char **buf, size_t *buflen,
99 start = g_get_monotonic_time();
104 len = maxlen - *buflen - 1;
107 len = serial_read(fd, *buf + *buflen, 1);
110 *(*buf + *buflen) = '\0';
111 if (*buflen > 0 && *(*buf + *buflen - 1) == '\r') {
112 /* Strip LF and terminate. */
113 *(*buf + --*buflen) = '\0';
117 if (g_get_monotonic_time() - start > timeout_ms)
126 static GSList *lcd14_scan(const char *conn, const char *serialcomm)
128 struct sr_dev_inst *sdi;
129 struct drv_context *drvc;
130 struct dev_context *devc;
131 struct sr_probe *probe;
137 if ((fd = serial_open(conn, O_RDONLY|O_NONBLOCK)) == -1) {
138 sr_err("unable to open %s: %s",
139 conn, strerror(errno));
142 if (serial_set_paramstr(fd, serialcomm) != SR_OK) {
143 sr_err("unable to set serial parameters");
147 sr_info("probing port %s readonly", conn);
154 /* There's no way to get an ID from the multimeter. It just sends data
155 * periodically, so the best we can do is check if the packets match the
156 * expected format. */
157 while (!devices && retry < 3)
160 size_t good_packets = 0;
163 /* Let's get a bit of data and see if we can find a packet */
165 serial_readline(fd, &b, &len, 500);
166 if( (len == 0) || (len < LCD14_PACKET_SIZE) ) {
167 /* Not enough data received, is the DMM connected ? */
171 /* Let's treat our buffer like a stream, and find any
173 for( i = 0; i < len - LCD14_PACKET_SIZE + 1;
174 /* don't increment i here */ )
176 const lcd14_packet *packet = (void *)(&buf[i]);
177 if( !lcd14_is_packet_valid(packet, NULL) ){
182 i += LCD14_PACKET_SIZE;
185 /* If we dropped more than two packets worth of data, something
187 size_t dropped = len - (good_packets * LCD14_PACKET_SIZE);
188 if(dropped > 2 * LCD14_PACKET_SIZE)
191 /* Let's see if we have anything good */
192 if (good_packets == 0)
195 sr_info("found device on port %s", conn);
197 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "TekPower",
200 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
201 sr_dbg("failed to malloc devc");
205 /* devc->profile = RADIOSHACK_22_812; */
206 devc->serial = sr_serial_dev_inst_new(conn, -1);
207 devc->serialcomm = g_strdup(serialcomm);
211 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
213 sdi->probes = g_slist_append(sdi->probes, probe);
214 drvc->instances = g_slist_append(drvc->instances, sdi);
215 devices = g_slist_append(devices, sdi);
223 static GSList *hw_scan(GSList *options)
225 struct sr_hwopt *opt;
227 const char *conn, *serialcomm;
229 conn = serialcomm = NULL;
230 for (l = options; l; l = l->next) {
232 switch (opt->hwopt) {
236 case SR_HWOPT_SERIALCOMM:
237 serialcomm = opt->value;
245 /* Use the provided comm specs. */
246 devices = lcd14_scan(conn, serialcomm);
248 /* Then try the default 2400 8n1 */
249 devices = lcd14_scan(conn, "2400/8n1");
255 static GSList *hw_dev_list(void)
257 struct drv_context *drvc;
261 return drvc->instances;
264 static int hw_dev_open(struct sr_dev_inst *sdi)
266 struct dev_context *devc;
268 if (!(devc = sdi->priv)) {
269 sr_err("sdi->priv was NULL.");
273 devc->serial->fd = serial_open(devc->serial->port, O_RDONLY);
274 if (devc->serial->fd == -1) {
275 sr_err("Couldn't open serial port '%s'.",
279 if (serial_set_paramstr(devc->serial->fd, devc->serialcomm) != SR_OK) {
280 sr_err("unable to set serial parameters");
283 sdi->status = SR_ST_ACTIVE;
288 static int hw_dev_close(struct sr_dev_inst *sdi)
290 struct dev_context *devc;
292 if (!(devc = sdi->priv)) {
293 sr_err("sdi->priv was NULL.");
297 if (devc->serial && devc->serial->fd != -1) {
298 serial_close(devc->serial->fd);
299 devc->serial->fd = -1;
300 sdi->status = SR_ST_INACTIVE;
306 static int hw_cleanup(void)
313 static int hw_info_get(int info_id, const void **data,
314 const struct sr_dev_inst *sdi)
316 (void)sdi; /* Does nothing. prevents "unused parameter" warning */
325 case SR_DI_NUM_PROBES:
326 *data = GINT_TO_POINTER(1);
328 case SR_DI_PROBE_NAMES:
338 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
341 struct dev_context *devc;
343 if (sdi->status != SR_ST_ACTIVE)
346 if (!(devc = sdi->priv)) {
347 sr_err("sdi->priv was NULL.");
352 case SR_HWCAP_LIMIT_SAMPLES:
353 devc->limit_samples = *(const uint64_t *)value;
354 sr_dbg("Setting sample limit to %" PRIu64 ".",
355 devc->limit_samples);
358 sr_err("Unknown capability: %d.", hwcap);
366 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
369 struct sr_datafeed_packet packet;
370 struct sr_datafeed_header header;
371 struct sr_datafeed_meta_analog meta;
372 struct dev_context *devc;
374 if (!(devc = sdi->priv)) {
375 sr_err("sdi->priv was NULL.");
379 sr_dbg("Starting acquisition.");
381 devc->cb_data = cb_data;
383 /* Reset the number of samples to take. If we've already collected our
384 * quota, but we start a new session, and don't reset this, we'll just
385 * quit without aquiring any new samples */
386 devc->num_samples = 0;
388 /* Send header packet to the session bus. */
389 sr_dbg("Sending SR_DF_HEADER.");
390 packet.type = SR_DF_HEADER;
391 packet.payload = (uint8_t *)&header;
392 header.feed_version = 1;
393 gettimeofday(&header.starttime, NULL);
394 sr_session_send(devc->cb_data, &packet);
396 /* Send metadata about the SR_DF_ANALOG packets to come. */
397 sr_dbg("Sending SR_DF_META_ANALOG.");
398 packet.type = SR_DF_META_ANALOG;
399 packet.payload = &meta;
401 sr_session_send(devc->cb_data, &packet);
403 /* Poll every 100ms, or whenever some data comes in. */
404 sr_source_add(devc->serial->fd, G_IO_IN, 50,
405 lcd14_receive_data, (void *)sdi );
410 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
413 struct sr_datafeed_packet packet;
414 struct dev_context *devc;
416 if (sdi->status != SR_ST_ACTIVE)
419 if (!(devc = sdi->priv)) {
420 sr_err("sdi->priv was NULL.");
424 sr_dbg("Stopping acquisition.");
426 sr_source_remove(devc->serial->fd);
427 hw_dev_close((struct sr_dev_inst *)sdi);
429 /* Send end packet to the session bus. */
430 sr_dbg("Sending SR_DF_END.");
431 packet.type = SR_DF_END;
432 sr_session_send(cb_data, &packet);
437 SR_PRIV struct sr_dev_driver tekpower_driver_info = {
438 .name = "tekpower-dmm",
439 .longname = "TekPower/Digitek 4000ZC",
442 .cleanup = hw_cleanup,
444 .dev_list = hw_dev_list,
445 .dev_clear = clear_instances,
446 .dev_open = hw_dev_open,
447 .dev_close = hw_dev_close,
448 .info_get = hw_info_get,
449 .dev_config_set = hw_dev_config_set,
450 .dev_acquisition_start = hw_dev_acquisition_start,
451 .dev_acquisition_stop = hw_dev_acquisition_stop,