]> sigrok.org Git - libsigrok.git/blame - src/hardware/rdtech-tc/api.c
rdtech-tc: grab receive data in larger chunks
[libsigrok.git] / src / hardware / rdtech-tc / api.c
CommitLineData
cae33a58
AS
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2020 Andreas Sandberg <andreas@sandberg.pp.se>
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 <config.h>
ea5fc667
GS
21
22#include <fcntl.h>
cae33a58 23#include <glib.h>
ea5fc667
GS
24#include <libsigrok/libsigrok.h>
25#include <string.h>
cae33a58
AS
26#include <sys/types.h>
27#include <sys/stat.h>
ea5fc667 28
cae33a58
AS
29#include "libsigrok-internal.h"
30#include "protocol.h"
31
32#define RDTECH_TC_SERIALCOMM "115200/8n1"
33
34static const uint32_t scanopts[] = {
35 SR_CONF_CONN,
36 SR_CONF_SERIALCOMM,
37};
38
39static const uint32_t drvopts[] = {
40 SR_CONF_ENERGYMETER,
41};
42
43static const uint32_t devopts[] = {
44 SR_CONF_CONTINUOUS,
45 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
46 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
47};
48
5955b58c
GS
49static GSList *rdtech_tc_scan(struct sr_dev_driver *di,
50 const char *conn, const char *serialcomm)
cae33a58
AS
51{
52 struct sr_serial_dev_inst *serial;
c1f9428a
GS
53 GSList *devices;
54 struct dev_context *devc;
55 struct sr_dev_inst *sdi;
56 size_t i;
cae33a58
AS
57
58 serial = sr_serial_dev_inst_new(conn, serialcomm);
59 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
60 goto err_out;
61
5df2033d 62 devc = g_malloc0(sizeof(*devc));
cae33a58
AS
63 sr_sw_limits_init(&devc->limits);
64
65 if (rdtech_tc_probe(serial, devc) != SR_OK) {
66 sr_err("Failed to find a supported RDTech TC device.");
67 goto err_out_serial;
68 }
69
5df2033d
GS
70 sdi = g_malloc0(sizeof(*sdi));
71 sdi->priv = devc;
cae33a58
AS
72 sdi->status = SR_ST_INACTIVE;
73 sdi->vendor = g_strdup("RDTech");
74 sdi->model = g_strdup(devc->dev_info.model_name);
75 sdi->version = g_strdup(devc->dev_info.fw_ver);
76 sdi->serial_num = g_strdup_printf("%08" PRIu32, devc->dev_info.serial_num);
77 sdi->inst_type = SR_INST_SERIAL;
78 sdi->conn = serial;
cae33a58 79
c1f9428a
GS
80 for (i = 0; devc->channels[i].name; i++) {
81 sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE,
82 devc->channels[i].name);
83 }
cae33a58 84
c1f9428a 85 devices = g_slist_append(NULL, sdi);
cae33a58
AS
86 serial_close(serial);
87 if (!devices)
88 sr_serial_dev_inst_free(serial);
89
90 return std_scan_complete(di, devices);
91
92err_out_serial:
93 g_free(devc);
94 serial_close(serial);
95err_out:
96 sr_serial_dev_inst_free(serial);
97
98 return NULL;
99}
100
101static GSList *scan(struct sr_dev_driver *di, GSList *options)
102{
c1f9428a
GS
103 const char *conn;
104 const char *serialcomm;
cae33a58 105
c1f9428a
GS
106 conn = NULL;
107 serialcomm = RDTECH_TC_SERIALCOMM;
fa801c4f 108 (void)sr_serial_extract_options(options, &conn, &serialcomm);
cae33a58
AS
109 if (!conn)
110 return NULL;
111
112 return rdtech_tc_scan(di, conn, serialcomm);
113}
114
115static int config_set(uint32_t key, GVariant *data,
5955b58c 116 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
cae33a58
AS
117{
118 struct dev_context *devc;
119
120 (void)cg;
121
122 devc = sdi->priv;
123
124 return sr_sw_limits_config_set(&devc->limits, key, data);
125}
126
127static int config_list(uint32_t key, GVariant **data,
5955b58c 128 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
cae33a58
AS
129{
130 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
131}
132
133static int dev_acquisition_start(const struct sr_dev_inst *sdi)
134{
c1f9428a
GS
135 struct dev_context *devc;
136 struct sr_serial_dev_inst *serial;
cae33a58 137
c1f9428a 138 devc = sdi->priv;
cae33a58
AS
139 sr_sw_limits_acquisition_start(&devc->limits);
140 std_session_send_df_header(sdi);
141
c1f9428a 142 serial = sdi->conn;
cae33a58 143 serial_source_add(sdi->session, serial, G_IO_IN, 50,
5955b58c 144 rdtech_tc_receive_data, (void *)sdi);
cae33a58 145
a82490b6 146 return rdtech_tc_poll(sdi, TRUE);
cae33a58
AS
147}
148
149static struct sr_dev_driver rdtech_tc_driver_info = {
150 .name = "rdtech-tc",
151 .longname = "RDTech TC66C USB power meter",
152 .api_version = 1,
153 .init = std_init,
154 .cleanup = std_cleanup,
155 .scan = scan,
156 .dev_list = std_dev_list,
157 .dev_clear = std_dev_clear,
158 .config_get = NULL,
159 .config_set = config_set,
160 .config_list = config_list,
161 .dev_open = std_serial_dev_open,
162 .dev_close = std_serial_dev_close,
163 .dev_acquisition_start = dev_acquisition_start,
164 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
165 .context = NULL,
166};
167SR_REGISTER_DEV_DRIVER(rdtech_tc_driver_info);