2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Aurelien Jacobs <aurel@gnuage.org>
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.
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.
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/>.
23 static const uint32_t scanopts[] = {
28 static const uint32_t drvopts[] = {
32 static const uint32_t devopts[] = {
33 SR_CONF_CONTINUOUS | SR_CONF_SET,
34 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
35 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
36 SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
39 static const char *data_sources[] = {
44 SR_PRIV struct sr_dev_driver appa_55ii_driver_info;
46 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
48 return std_init(sr_ctx, di, LOG_PREFIX);
51 static GSList *scan(struct sr_dev_driver *di, GSList *options)
53 struct drv_context *drvc;
54 struct dev_context *devc;
55 struct sr_serial_dev_inst *serial;
56 struct sr_dev_inst *sdi;
57 struct sr_config *src;
59 const char *conn, *serialcomm;
65 conn = serialcomm = NULL;
66 for (l = options; l; l = l->next) {
70 conn = g_variant_get_string(src->data, NULL);
72 case SR_CONF_SERIALCOMM:
73 serialcomm = g_variant_get_string(src->data, NULL);
80 serialcomm = "9600/8n1";
82 serial = sr_serial_dev_inst_new(conn, serialcomm);
84 if (serial_open(serial, SERIAL_RDONLY) != SR_OK)
87 sr_info("Probing serial port %s.", conn);
90 drvc->instances = NULL;
93 /* Let's get a bit of data and see if we can find a packet. */
94 if (serial_stream_detect(serial, buf, &len, 25,
95 appa_55ii_packet_valid, 500, 9600) != SR_OK)
98 sr_info("Found device on port %s.", conn);
100 sdi = g_malloc0(sizeof(struct sr_dev_inst));
101 sdi->status = SR_ST_INACTIVE;
102 sdi->vendor = g_strdup("APPA");
103 sdi->model = g_strdup("55II");
104 devc = g_malloc0(sizeof(struct dev_context));
105 devc->data_source = DEFAULT_DATA_SOURCE;
106 sdi->inst_type = SR_INST_SERIAL;
111 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "T1");
112 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "T2");
114 drvc->instances = g_slist_append(drvc->instances, sdi);
115 devices = g_slist_append(devices, sdi);
118 serial_close(serial);
123 static GSList *dev_list(const struct sr_dev_driver *di)
125 return ((struct drv_context *)(di->context))->instances;
128 static int cleanup(const struct sr_dev_driver *di)
130 return std_dev_clear(di, NULL);
133 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
134 const struct sr_channel_group *cg)
136 struct dev_context *devc = sdi->priv;
141 case SR_CONF_LIMIT_SAMPLES:
142 *data = g_variant_new_uint64(devc->limit_samples);
144 case SR_CONF_LIMIT_MSEC:
145 *data = g_variant_new_uint64(devc->limit_msec);
147 case SR_CONF_DATA_SOURCE:
148 *data = g_variant_new_string(data_sources[devc->data_source]);
157 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
158 const struct sr_channel_group *cg)
160 struct dev_context *devc;
166 if (sdi->status != SR_ST_ACTIVE)
167 return SR_ERR_DEV_CLOSED;
169 if (!(devc = sdi->priv)) {
170 sr_err("sdi->priv was NULL.");
175 case SR_CONF_LIMIT_SAMPLES:
176 devc->limit_samples = g_variant_get_uint64(data);
178 case SR_CONF_LIMIT_MSEC:
179 devc->limit_msec = g_variant_get_uint64(data);
181 case SR_CONF_DATA_SOURCE: {
182 tmp_str = g_variant_get_string(data, NULL);
183 for (i = 0; i < ARRAY_SIZE(data_sources); i++)
184 if (!strcmp(tmp_str, data_sources[i])) {
185 devc->data_source = i;
188 if (i == ARRAY_SIZE(data_sources))
199 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
200 const struct sr_channel_group *cg)
205 case SR_CONF_SCAN_OPTIONS:
206 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
207 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
209 case SR_CONF_DEVICE_OPTIONS:
211 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
212 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
214 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
215 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
217 case SR_CONF_DATA_SOURCE:
218 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
227 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
230 struct sr_serial_dev_inst *serial;
231 struct dev_context *devc;
234 if (sdi->status != SR_ST_ACTIVE)
235 return SR_ERR_DEV_CLOSED;
237 if (!(devc = sdi->priv)) {
238 sr_err("sdi->priv was NULL.");
242 devc->session_cb_data = cb_data;
245 * Reset the number of samples to take. If we've already collected our
246 * quota, but we start a new session, and don't reset this, we'll just
247 * quit without acquiring any new samples.
249 devc->num_samples = 0;
250 devc->start_time = g_get_monotonic_time();
252 /* Send header packet to the session bus. */
253 std_session_send_df_header(cb_data, LOG_PREFIX);
255 /* Poll every 50ms, or whenever some data comes in. */
256 serial_source_add(sdi->session, serial, G_IO_IN, 50,
257 appa_55ii_receive_data, (void *)sdi);
262 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
264 return std_serial_dev_acquisition_stop(sdi, cb_data,
265 std_serial_dev_close, sdi->conn, LOG_PREFIX);
268 SR_PRIV struct sr_dev_driver appa_55ii_driver_info = {
270 .longname = "APPA 55II",
275 .dev_list = dev_list,
277 .config_get = config_get,
278 .config_set = config_set,
279 .config_list = config_list,
280 .dev_open = std_serial_dev_open,
281 .dev_close = std_serial_dev_close,
282 .dev_acquisition_start = dev_acquisition_start,
283 .dev_acquisition_stop = dev_acquisition_stop,