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