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