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