]> sigrok.org Git - libsigrok.git/blame - src/hardware/center-3xx/api.c
Fix invalid pointer dereference.
[libsigrok.git] / src / hardware / center-3xx / api.c
CommitLineData
4433145f
UH
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "protocol.h"
22
a0e0bb41 23static const uint32_t scanopts[] = {
4a8bbed7
UH
24 SR_CONF_CONN,
25 SR_CONF_SERIALCOMM,
26};
27
6685e9a6 28static const uint32_t drvopts[] = {
4a8bbed7 29 SR_CONF_THERMOMETER,
6685e9a6
UH
30};
31
32static const uint32_t devopts[] = {
33 SR_CONF_CONTINUOUS | SR_CONF_SET,
5827f61b
BV
34 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
35 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
4a8bbed7
UH
36};
37
ba7dd8bb 38static const char *channel_names[] = {
4a8bbed7
UH
39 "T1", "T2", "T3", "T4",
40 NULL,
41};
42
43SR_PRIV struct sr_dev_driver center_309_driver_info;
44SR_PRIV struct sr_dev_driver voltcraft_k204_driver_info;
45
46SR_PRIV const struct center_dev_info center_devs[] = {
47 {
48 "Center", "309", "9600/8n1", 4, 32000, 45,
49 center_3xx_packet_valid,
50 &center_309_driver_info, receive_data_CENTER_309,
51 },
52 {
53 "Voltcraft", "K204", "9600/8n1", 4, 32000, 45,
54 center_3xx_packet_valid,
55 &voltcraft_k204_driver_info, receive_data_VOLTCRAFT_K204,
56 },
57};
4433145f 58
4a8bbed7 59static int dev_clear(int idx)
4433145f 60{
4a8bbed7 61 return std_dev_clear(center_devs[idx].di, NULL);
4433145f
UH
62}
63
4a8bbed7 64static int init(struct sr_context *sr_ctx, int idx)
4433145f 65{
4a8bbed7
UH
66 return std_init(sr_ctx, center_devs[idx].di, LOG_PREFIX);
67}
68
69static GSList *center_scan(const char *conn, const char *serialcomm, int idx)
70{
71 int i;
72 struct sr_dev_inst *sdi;
4433145f 73 struct drv_context *drvc;
4a8bbed7 74 struct dev_context *devc;
ba7dd8bb 75 struct sr_channel *ch;
4a8bbed7 76 struct sr_serial_dev_inst *serial;
4433145f
UH
77 GSList *devices;
78
4a8bbed7
UH
79 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
80 return NULL;
81
258a2313 82 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
4a8bbed7 83 return NULL;
4433145f 84
4a8bbed7 85 drvc = center_devs[idx].di->priv;
4433145f 86 devices = NULL;
4a8bbed7
UH
87 serial_flush(serial);
88
89 sr_info("Found device on port %s.", conn);
90
0af636be
UH
91 sdi = sr_dev_inst_new();
92 sdi->status = SR_ST_INACTIVE;
93 sdi->vendor = g_strdup(center_devs[idx].vendor);
94 sdi->model = g_strdup(center_devs[idx].device);
4a8bbed7
UH
95
96 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
97 sr_err("Device context malloc failed.");
98 goto scan_cleanup;
99 }
100
101 sdi->inst_type = SR_INST_SERIAL;
102 sdi->conn = serial;
103
104 sdi->priv = devc;
105 sdi->driver = center_devs[idx].di;
106
107 for (i = 0; i < center_devs[idx].num_channels; i++) {
3f239f08 108 if (!(ch = sr_channel_new(i, SR_CHANNEL_ANALOG,
ba7dd8bb 109 TRUE, channel_names[i])))
4a8bbed7 110 goto scan_cleanup;
ba7dd8bb 111 sdi->channels = g_slist_append(sdi->channels, ch);
4a8bbed7
UH
112 }
113
114 drvc->instances = g_slist_append(drvc->instances, sdi);
115 devices = g_slist_append(devices, sdi);
116
117scan_cleanup:
118 serial_close(serial);
4433145f
UH
119
120 return devices;
121}
122
4a8bbed7 123static GSList *scan(GSList *options, int idx)
4433145f 124{
4a8bbed7
UH
125 struct sr_config *src;
126 GSList *l, *devices;
127 const char *conn, *serialcomm;
128
129 conn = serialcomm = NULL;
130 for (l = options; l; l = l->next) {
131 src = l->data;
132 switch (src->key) {
133 case SR_CONF_CONN:
134 conn = g_variant_get_string(src->data, NULL);
135 break;
136 case SR_CONF_SERIALCOMM:
137 serialcomm = g_variant_get_string(src->data, NULL);
138 break;
139 }
140 }
141 if (!conn)
142 return NULL;
143
144 if (serialcomm) {
145 /* Use the provided comm specs. */
146 devices = center_scan(conn, serialcomm, idx);
147 } else {
148 /* Try the default. */
149 devices = center_scan(conn, center_devs[idx].conn, idx);
150 }
151
152 return devices;
4433145f
UH
153}
154
4a8bbed7 155static GSList *dev_list(int idx)
4433145f 156{
4a8bbed7 157 return ((struct drv_context *)(center_devs[idx].di->priv))->instances;
4433145f
UH
158}
159
4a8bbed7 160static int cleanup(int idx)
4433145f 161{
4a8bbed7 162 return dev_clear(idx);
4433145f
UH
163}
164
584560f1 165static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 166 const struct sr_channel_group *cg)
4433145f 167{
4a8bbed7 168 struct dev_context *devc;
4433145f 169
53b4680f 170 (void)cg;
d3c74a6f 171
4433145f
UH
172 if (sdi->status != SR_ST_ACTIVE)
173 return SR_ERR_DEV_CLOSED;
174
4a8bbed7
UH
175 devc = sdi->priv;
176
584560f1 177 switch (key) {
4a8bbed7
UH
178 case SR_CONF_LIMIT_SAMPLES:
179 if (g_variant_get_uint64(data) == 0)
180 return SR_ERR_ARG;
181 devc->limit_samples = g_variant_get_uint64(data);
182 break;
183 case SR_CONF_LIMIT_MSEC:
184 if (g_variant_get_uint64(data) == 0)
185 return SR_ERR_ARG;
186 devc->limit_msec = g_variant_get_uint64(data);
187 break;
4433145f 188 default:
4a8bbed7 189 return SR_ERR_NA;
4433145f
UH
190 }
191
4a8bbed7 192 return SR_OK;
4433145f
UH
193}
194
584560f1 195static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 196 const struct sr_channel_group *cg)
4433145f 197{
53b4680f 198 (void)cg;
4433145f 199
4433145f 200 switch (key) {
4a8bbed7 201 case SR_CONF_SCAN_OPTIONS:
584560f1 202 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
a0e0bb41 203 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
4a8bbed7
UH
204 break;
205 case SR_CONF_DEVICE_OPTIONS:
6685e9a6
UH
206 if (!sdi)
207 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
208 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
209 else
210 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
f254bc4b 211 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
4a8bbed7 212 break;
4433145f
UH
213 default:
214 return SR_ERR_NA;
215 }
216
4a8bbed7 217 return SR_OK;
4433145f
UH
218}
219
220static int dev_acquisition_start(const struct sr_dev_inst *sdi,
4a8bbed7 221 void *cb_data, int idx)
4433145f 222{
4a8bbed7
UH
223 struct dev_context *devc;
224 struct sr_serial_dev_inst *serial;
4433145f
UH
225
226 if (sdi->status != SR_ST_ACTIVE)
227 return SR_ERR_DEV_CLOSED;
228
4a8bbed7
UH
229 devc = sdi->priv;
230 devc->cb_data = cb_data;
231 devc->num_samples = 0;
232 devc->starttime = g_get_monotonic_time();
233
234 /* Send header packet to the session bus. */
235 std_session_send_df_header(cb_data, LOG_PREFIX);
236
237 /* Poll every 500ms, or whenever some data comes in. */
238 serial = sdi->conn;
102f1239 239 serial_source_add(sdi->session, serial, G_IO_IN, 500,
4a8bbed7
UH
240 center_devs[idx].receive_data, (void *)sdi);
241
4433145f
UH
242 return SR_OK;
243}
244
245static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
246{
d43b0908
BV
247 return std_serial_dev_acquisition_stop(sdi, cb_data,
248 std_serial_dev_close, sdi->conn, LOG_PREFIX);
4433145f
UH
249}
250
4a8bbed7
UH
251/* Driver-specific API function wrappers */
252#define HW_INIT(X) \
253static int init_##X(struct sr_context *sr_ctx) { return init(sr_ctx, X); }
254#define HW_CLEANUP(X) \
255static int cleanup_##X(void) { return cleanup(X); }
256#define HW_SCAN(X) \
257static GSList *scan_##X(GSList *options) { return scan(options, X); }
258#define HW_DEV_LIST(X) \
259static GSList *dev_list_##X(void) { return dev_list(X); }
260#define HW_DEV_CLEAR(X) \
261static int dev_clear_##X(void) { return dev_clear(X); }
262#define HW_DEV_ACQUISITION_START(X) \
263static int dev_acquisition_start_##X(const struct sr_dev_inst *sdi, \
264void *cb_data) { return dev_acquisition_start(sdi, cb_data, X); }
265
266/* Driver structs and API function wrappers */
267#define DRV(ID, ID_UPPER, NAME, LONGNAME) \
268HW_INIT(ID_UPPER) \
269HW_CLEANUP(ID_UPPER) \
270HW_SCAN(ID_UPPER) \
271HW_DEV_LIST(ID_UPPER) \
272HW_DEV_CLEAR(ID_UPPER) \
273HW_DEV_ACQUISITION_START(ID_UPPER) \
274SR_PRIV struct sr_dev_driver ID##_driver_info = { \
275 .name = NAME, \
276 .longname = LONGNAME, \
277 .api_version = 1, \
278 .init = init_##ID_UPPER, \
279 .cleanup = cleanup_##ID_UPPER, \
280 .scan = scan_##ID_UPPER, \
281 .dev_list = dev_list_##ID_UPPER, \
282 .dev_clear = dev_clear_##ID_UPPER, \
283 .config_get = NULL, \
284 .config_set = config_set, \
285 .config_list = config_list, \
854434de 286 .dev_open = std_serial_dev_open, \
bf2c987f 287 .dev_close = std_serial_dev_close, \
4a8bbed7
UH
288 .dev_acquisition_start = dev_acquisition_start_##ID_UPPER, \
289 .dev_acquisition_stop = dev_acquisition_stop, \
290 .priv = NULL, \
4433145f 291};
4a8bbed7
UH
292
293DRV(center_309, CENTER_309, "center-309", "Center 309")
294DRV(voltcraft_k204, VOLTCRAFT_K204, "voltcraft-k204", "Voltcraft K204")