]>
Commit | Line | Data |
---|---|---|
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 | ||
34 | static const uint32_t scanopts[] = { | |
35 | SR_CONF_CONN, | |
36 | SR_CONF_SERIALCOMM, | |
37 | }; | |
38 | ||
39 | static const uint32_t drvopts[] = { | |
40 | SR_CONF_ENERGYMETER, | |
41 | }; | |
42 | ||
43 | static 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 | ||
49 | static GSList *rdtech_tc_scan(struct sr_dev_driver *di, const char *conn, | |
50 | const char *serialcomm) | |
51 | { | |
52 | struct sr_serial_dev_inst *serial; | |
53 | GSList *devices = NULL; | |
54 | struct dev_context *devc = NULL; | |
55 | struct sr_dev_inst *sdi = NULL; | |
56 | ||
57 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
58 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
59 | goto err_out; | |
60 | ||
61 | devc = g_malloc0(sizeof(struct dev_context)); | |
62 | sr_sw_limits_init(&devc->limits); | |
63 | ||
64 | if (rdtech_tc_probe(serial, devc) != SR_OK) { | |
65 | sr_err("Failed to find a supported RDTech TC device."); | |
66 | goto err_out_serial; | |
67 | } | |
68 | ||
69 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
70 | sdi->status = SR_ST_INACTIVE; | |
71 | sdi->vendor = g_strdup("RDTech"); | |
72 | sdi->model = g_strdup(devc->dev_info.model_name); | |
73 | sdi->version = g_strdup(devc->dev_info.fw_ver); | |
74 | sdi->serial_num = g_strdup_printf("%08" PRIu32, devc->dev_info.serial_num); | |
75 | sdi->inst_type = SR_INST_SERIAL; | |
76 | sdi->conn = serial; | |
77 | sdi->priv = devc; | |
78 | ||
79 | for (int i = 0; devc->channels[i].name; i++) | |
80 | sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, devc->channels[i].name); | |
81 | ||
82 | devices = g_slist_append(devices, sdi); | |
83 | serial_close(serial); | |
84 | if (!devices) | |
85 | sr_serial_dev_inst_free(serial); | |
86 | ||
87 | return std_scan_complete(di, devices); | |
88 | ||
89 | err_out_serial: | |
90 | g_free(devc); | |
91 | serial_close(serial); | |
92 | err_out: | |
93 | sr_serial_dev_inst_free(serial); | |
94 | ||
95 | return NULL; | |
96 | } | |
97 | ||
98 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
99 | { | |
100 | struct sr_config *src; | |
101 | const char *conn = NULL; | |
102 | const char *serialcomm = RDTECH_TC_SERIALCOMM; | |
103 | ||
104 | for (GSList *l = options; l; l = l->next) { | |
105 | src = l->data; | |
106 | switch (src->key) { | |
107 | case SR_CONF_CONN: | |
108 | conn = g_variant_get_string(src->data, NULL); | |
109 | break; | |
110 | case SR_CONF_SERIALCOMM: | |
111 | serialcomm = g_variant_get_string(src->data, NULL); | |
112 | break; | |
113 | } | |
114 | } | |
115 | if (!conn) | |
116 | return NULL; | |
117 | ||
118 | return rdtech_tc_scan(di, conn, serialcomm); | |
119 | } | |
120 | ||
121 | static int config_set(uint32_t key, GVariant *data, | |
122 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
123 | { | |
124 | struct dev_context *devc; | |
125 | ||
126 | (void)cg; | |
127 | ||
128 | devc = sdi->priv; | |
129 | ||
130 | return sr_sw_limits_config_set(&devc->limits, key, data); | |
131 | } | |
132 | ||
133 | static int config_list(uint32_t key, GVariant **data, | |
134 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
135 | { | |
136 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); | |
137 | } | |
138 | ||
139 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
140 | { | |
141 | struct dev_context *devc = sdi->priv; | |
142 | struct sr_serial_dev_inst *serial = sdi->conn; | |
143 | ||
144 | sr_sw_limits_acquisition_start(&devc->limits); | |
145 | std_session_send_df_header(sdi); | |
146 | ||
147 | serial_source_add(sdi->session, serial, G_IO_IN, 50, | |
148 | rdtech_tc_receive_data, (void *)sdi); | |
149 | ||
150 | return rdtech_tc_poll(sdi); | |
151 | } | |
152 | ||
153 | static struct sr_dev_driver rdtech_tc_driver_info = { | |
154 | .name = "rdtech-tc", | |
155 | .longname = "RDTech TC66C USB power meter", | |
156 | .api_version = 1, | |
157 | .init = std_init, | |
158 | .cleanup = std_cleanup, | |
159 | .scan = scan, | |
160 | .dev_list = std_dev_list, | |
161 | .dev_clear = std_dev_clear, | |
162 | .config_get = NULL, | |
163 | .config_set = config_set, | |
164 | .config_list = config_list, | |
165 | .dev_open = std_serial_dev_open, | |
166 | .dev_close = std_serial_dev_close, | |
167 | .dev_acquisition_start = dev_acquisition_start, | |
168 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, | |
169 | .context = NULL, | |
170 | }; | |
171 | SR_REGISTER_DEV_DRIVER(rdtech_tc_driver_info); |