]> sigrok.org Git - libsigrok.git/blame - src/hardware/tondaj-sl-814/api.c
Construct driver array at runtime, from an array of per-file arrays.
[libsigrok.git] / src / hardware / tondaj-sl-814 / api.c
CommitLineData
aa2af324 1/*
50985c20 2 * This file is part of the libsigrok project.
aa2af324
UH
3 *
4 * Copyright (C) 2012 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
c073af80 21#include <fcntl.h>
aa2af324
UH
22#include <glib.h>
23#include "libsigrok.h"
24#include "libsigrok-internal.h"
25#include "protocol.h"
26
19ee7dff
BV
27#define SERIALCOMM "9600/8e1"
28
a0e0bb41 29static const uint32_t scanopts[] = {
1953564a
BV
30 SR_CONF_CONN,
31 SR_CONF_SERIALCOMM,
c073af80
UH
32};
33
f254bc4b 34static const uint32_t devopts[] = {
1953564a 35 SR_CONF_SOUNDLEVELMETER,
1953564a 36 SR_CONF_CONTINUOUS,
5827f61b 37 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
c073af80
UH
38};
39
aa2af324
UH
40SR_PRIV struct sr_dev_driver tondaj_sl_814_driver_info;
41static struct sr_dev_driver *di = &tondaj_sl_814_driver_info;
42
6078d2c9 43static int init(struct sr_context *sr_ctx)
aa2af324 44{
f6beaac5 45 return std_init(sr_ctx, di, LOG_PREFIX);
aa2af324
UH
46}
47
6078d2c9 48static GSList *scan(GSList *options)
aa2af324
UH
49{
50 struct drv_context *drvc;
c073af80
UH
51 struct dev_context *devc;
52 struct sr_dev_inst *sdi;
1987b8d6 53 struct sr_config *src;
c073af80
UH
54 GSList *devices, *l;
55 const char *conn, *serialcomm;
a5e44c32 56 struct sr_serial_dev_inst *serial;
aa2af324 57
aa2af324
UH
58 drvc = di->priv;
59 drvc->instances = NULL;
60
4b97c74e
UH
61 devices = NULL;
62
c073af80
UH
63 conn = serialcomm = NULL;
64 for (l = options; l; l = l->next) {
1987b8d6 65 if (!(src = l->data)) {
c073af80
UH
66 sr_err("Invalid option data, skipping.");
67 continue;
68 }
1987b8d6 69 switch (src->key) {
1953564a 70 case SR_CONF_CONN:
afdf6d6a 71 conn = g_variant_get_string(src->data, NULL);
c073af80 72 break;
1953564a 73 case SR_CONF_SERIALCOMM:
06c45a66 74 serialcomm = g_variant_get_string(src->data, NULL);
c073af80
UH
75 break;
76 default:
1987b8d6 77 sr_err("Unknown option %d, skipping.", src->key);
c073af80
UH
78 break;
79 }
80 }
80bc6632 81 if (!conn)
c073af80 82 return NULL;
19ee7dff
BV
83 if (!serialcomm)
84 serialcomm = SERIALCOMM;
c073af80 85
aac29cc1 86 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
87 sdi->status = SR_ST_INACTIVE;
88 sdi->vendor = g_strdup("Tondaj");
89 sdi->model = g_strdup("SL-814");
f57d8ffe 90 devc = g_malloc0(sizeof(struct dev_context));
c073af80 91
91219afc 92 serial = sr_serial_dev_inst_new(conn, serialcomm);
c073af80 93
7ef93fb0 94 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
c073af80 95 return NULL;
19ee7dff 96
a5e44c32
UH
97 sdi->inst_type = SR_INST_SERIAL;
98 sdi->conn = serial;
99
c073af80
UH
100 sdi->priv = devc;
101 sdi->driver = di;
5e23fcab 102 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
c073af80
UH
103 drvc->instances = g_slist_append(drvc->instances, sdi);
104 devices = g_slist_append(devices, sdi);
aa2af324
UH
105
106 return devices;
107}
108
6078d2c9 109static GSList *dev_list(void)
aa2af324 110{
0e94d524 111 return ((struct drv_context *)(di->priv))->instances;
aa2af324
UH
112}
113
6078d2c9 114static int cleanup(void)
aa2af324 115{
a6630742 116 return std_dev_clear(di, NULL);
aa2af324
UH
117}
118
584560f1 119static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 120 const struct sr_channel_group *cg)
aa2af324 121{
c073af80 122 struct dev_context *devc;
aa2af324 123
53b4680f 124 (void)cg;
8f996b89 125
e73ffd42
BV
126 if (sdi->status != SR_ST_ACTIVE)
127 return SR_ERR_DEV_CLOSED;
aa2af324 128
c073af80
UH
129 devc = sdi->priv;
130
584560f1 131 switch (key) {
1953564a 132 case SR_CONF_LIMIT_SAMPLES:
afdf6d6a 133 devc->limit_samples = g_variant_get_uint64(data);
c073af80
UH
134 sr_dbg("Setting sample limit to %" PRIu64 ".",
135 devc->limit_samples);
136 break;
aa2af324 137 default:
bd6fbf62 138 return SR_ERR_NA;
aa2af324
UH
139 }
140
c073af80 141 return SR_OK;
aa2af324
UH
142}
143
584560f1 144static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 145 const struct sr_channel_group *cg)
a1c743fc 146{
a1c743fc 147 (void)sdi;
53b4680f 148 (void)cg;
a1c743fc
BV
149
150 switch (key) {
0d485e30 151 case SR_CONF_SCAN_OPTIONS:
584560f1 152 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
a0e0bb41 153 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
0d485e30 154 break;
9a6517d1 155 case SR_CONF_DEVICE_OPTIONS:
584560f1 156 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
f254bc4b 157 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
9a6517d1 158 break;
a1c743fc 159 default:
bd6fbf62 160 return SR_ERR_NA;
a1c743fc
BV
161 }
162
163 return SR_OK;
164}
165
6078d2c9 166static int dev_acquisition_start(const struct sr_dev_inst *sdi,
aa2af324
UH
167 void *cb_data)
168{
c073af80 169 struct dev_context *devc;
a5e44c32 170 struct sr_serial_dev_inst *serial;
c073af80 171
e73ffd42
BV
172 if (sdi->status != SR_ST_ACTIVE)
173 return SR_ERR_DEV_CLOSED;
174
c073af80
UH
175 devc = sdi->priv;
176 devc->cb_data = cb_data;
177
178 /* Send header packet to the session bus. */
29a27196 179 std_session_send_df_header(cb_data, LOG_PREFIX);
c073af80 180
c073af80 181 /* Poll every 500ms, or whenever some data comes in. */
a5e44c32 182 serial = sdi->conn;
102f1239 183 serial_source_add(sdi->session, serial, G_IO_IN, 500,
c073af80 184 tondaj_sl_814_receive_data, (void *)sdi);
aa2af324
UH
185
186 return SR_OK;
187}
188
6078d2c9 189static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
aa2af324 190{
d43b0908
BV
191 return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close,
192 sdi->conn, LOG_PREFIX);
aa2af324
UH
193}
194
195SR_PRIV struct sr_dev_driver tondaj_sl_814_driver_info = {
196 .name = "tondaj-sl-814",
197 .longname = "Tondaj SL-814",
198 .api_version = 1,
6078d2c9
UH
199 .init = init,
200 .cleanup = cleanup,
201 .scan = scan,
202 .dev_list = dev_list,
a6630742 203 .dev_clear = NULL,
6fab7b8f 204 .config_get = NULL,
035a1078 205 .config_set = config_set,
a1c743fc 206 .config_list = config_list,
854434de 207 .dev_open = std_serial_dev_open,
bf2c987f 208 .dev_close = std_serial_dev_close,
6078d2c9
UH
209 .dev_acquisition_start = dev_acquisition_start,
210 .dev_acquisition_stop = dev_acquisition_stop,
aa2af324
UH
211 .priv = NULL,
212};