]>
Commit | Line | Data |
---|---|---|
4d729ddc BV |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> | |
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 3 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, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <glib.h> | |
21 | #include "libsigrok.h" | |
22 | #include "libsigrok-internal.h" | |
23 | #include "protocol.h" | |
a8d09e13 BV |
24 | #include <sys/types.h> |
25 | #include <sys/stat.h> | |
26 | #include <fcntl.h> | |
27 | #include <errno.h> | |
28 | #include <string.h> | |
29 | ||
f306ca61 BV |
30 | /* The Colead SL-5868P uses this. */ |
31 | #define SERIALCOMM "2400/8n1" | |
32 | ||
dccda194 | 33 | static const int32_t hwopts[] = { |
1953564a BV |
34 | SR_CONF_CONN, |
35 | SR_CONF_SERIALCOMM, | |
a8d09e13 BV |
36 | }; |
37 | ||
dccda194 | 38 | static const int32_t hwcaps[] = { |
1953564a BV |
39 | SR_CONF_SOUNDLEVELMETER, |
40 | SR_CONF_LIMIT_SAMPLES, | |
41 | SR_CONF_LIMIT_MSEC, | |
42 | SR_CONF_CONTINUOUS, | |
a8d09e13 BV |
43 | }; |
44 | ||
4d729ddc BV |
45 | SR_PRIV struct sr_dev_driver colead_slm_driver_info; |
46 | static struct sr_dev_driver *di = &colead_slm_driver_info; | |
47 | ||
48 | /* Properly close and free all devices. */ | |
49 | static int clear_instances(void) | |
50 | { | |
51 | struct sr_dev_inst *sdi; | |
52 | struct drv_context *drvc; | |
53 | struct dev_context *devc; | |
aa706635 | 54 | struct sr_serial_dev_inst *serial; |
4d729ddc BV |
55 | GSList *l; |
56 | ||
538ac9a9 BV |
57 | if (!(drvc = di->priv)) |
58 | return SR_OK; | |
59 | ||
4d729ddc BV |
60 | for (l = drvc->instances; l; l = l->next) { |
61 | if (!(sdi = l->data)) | |
62 | continue; | |
63 | if (!(devc = sdi->priv)) | |
64 | continue; | |
aa706635 BV |
65 | serial = sdi->conn; |
66 | sr_serial_dev_inst_free(serial); | |
4d729ddc BV |
67 | sr_dev_inst_free(sdi); |
68 | } | |
4d729ddc BV |
69 | g_slist_free(drvc->instances); |
70 | drvc->instances = NULL; | |
71 | ||
72 | return SR_OK; | |
73 | } | |
74 | ||
34f06b90 | 75 | static int hw_init(struct sr_context *sr_ctx) |
4d729ddc | 76 | { |
29a27196 | 77 | return std_hw_init(sr_ctx, di, LOG_PREFIX); |
4d729ddc BV |
78 | } |
79 | ||
80 | static GSList *hw_scan(GSList *options) | |
81 | { | |
82 | struct drv_context *drvc; | |
a8d09e13 BV |
83 | struct dev_context *devc; |
84 | struct sr_dev_inst *sdi; | |
1987b8d6 | 85 | struct sr_config *src; |
a8d09e13 BV |
86 | struct sr_probe *probe; |
87 | GSList *devices, *l; | |
88 | const char *conn, *serialcomm; | |
4d729ddc | 89 | |
4d729ddc BV |
90 | drvc = di->priv; |
91 | drvc->instances = NULL; | |
92 | ||
4b97c74e UH |
93 | devices = NULL; |
94 | ||
a8d09e13 BV |
95 | conn = serialcomm = NULL; |
96 | for (l = options; l; l = l->next) { | |
1987b8d6 BV |
97 | src = l->data; |
98 | switch (src->key) { | |
1953564a | 99 | case SR_CONF_CONN: |
dccda194 | 100 | conn = g_variant_get_string(src->data, NULL); |
a8d09e13 | 101 | break; |
1953564a | 102 | case SR_CONF_SERIALCOMM: |
dccda194 | 103 | serialcomm = g_variant_get_string(src->data, NULL); |
a8d09e13 BV |
104 | break; |
105 | } | |
106 | } | |
107 | if (!conn) | |
108 | return NULL; | |
f306ca61 BV |
109 | if (!serialcomm) |
110 | serialcomm = SERIALCOMM; | |
a8d09e13 BV |
111 | |
112 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Colead", | |
113 | "SL-5868P", NULL))) | |
114 | return NULL; | |
115 | ||
116 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
4b97c74e | 117 | sr_dbg("Device context malloc failed."); |
a8d09e13 BV |
118 | return NULL; |
119 | } | |
120 | ||
aa706635 | 121 | if (!(sdi->conn = sr_serial_dev_inst_new(conn, serialcomm))) |
f306ca61 | 122 | return NULL; |
a8d09e13 | 123 | |
aa706635 | 124 | sdi->inst_type = SR_INST_SERIAL; |
a8d09e13 BV |
125 | sdi->priv = devc; |
126 | sdi->driver = di; | |
127 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) | |
128 | return NULL; | |
129 | sdi->probes = g_slist_append(sdi->probes, probe); | |
130 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
131 | devices = g_slist_append(devices, sdi); | |
4d729ddc BV |
132 | |
133 | return devices; | |
134 | } | |
135 | ||
136 | static GSList *hw_dev_list(void) | |
137 | { | |
0e94d524 | 138 | return ((struct drv_context *)(di->priv))->instances; |
4d729ddc BV |
139 | } |
140 | ||
141 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
142 | { | |
aa706635 | 143 | struct sr_serial_dev_inst *serial; |
a8d09e13 | 144 | |
aa706635 BV |
145 | serial = sdi->conn; |
146 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
a8d09e13 | 147 | return SR_ERR; |
f306ca61 | 148 | |
a8d09e13 | 149 | sdi->status = SR_ST_ACTIVE; |
4d729ddc BV |
150 | |
151 | return SR_OK; | |
152 | } | |
153 | ||
154 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
155 | { | |
aa706635 | 156 | struct sr_serial_dev_inst *serial; |
a8d09e13 | 157 | |
aa706635 BV |
158 | serial = sdi->conn; |
159 | if (serial && serial->fd != -1) { | |
160 | serial_close(serial); | |
a8d09e13 BV |
161 | sdi->status = SR_ST_INACTIVE; |
162 | } | |
4d729ddc BV |
163 | |
164 | return SR_OK; | |
165 | } | |
166 | ||
167 | static int hw_cleanup(void) | |
168 | { | |
169 | clear_instances(); | |
170 | ||
4d729ddc BV |
171 | return SR_OK; |
172 | } | |
173 | ||
dccda194 | 174 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi) |
4d729ddc | 175 | { |
a8d09e13 | 176 | struct dev_context *devc; |
4d729ddc | 177 | |
a8d09e13 | 178 | if (sdi->status != SR_ST_ACTIVE) |
e73ffd42 | 179 | return SR_ERR_DEV_CLOSED; |
a8d09e13 BV |
180 | |
181 | if (!(devc = sdi->priv)) { | |
182 | sr_err("sdi->priv was NULL."); | |
183 | return SR_ERR_BUG; | |
4d729ddc BV |
184 | } |
185 | ||
035a1078 | 186 | switch (id) { |
1953564a | 187 | case SR_CONF_LIMIT_MSEC: |
a8d09e13 | 188 | /* TODO: not yet implemented */ |
dccda194 | 189 | if (g_variant_get_uint64(data) == 0) { |
a8d09e13 BV |
190 | sr_err("LIMIT_MSEC can't be 0."); |
191 | return SR_ERR; | |
192 | } | |
dccda194 | 193 | devc->limit_msec = g_variant_get_uint64(data);; |
a8d09e13 BV |
194 | sr_dbg("Setting time limit to %" PRIu64 "ms.", |
195 | devc->limit_msec); | |
196 | break; | |
1953564a | 197 | case SR_CONF_LIMIT_SAMPLES: |
dccda194 | 198 | devc->limit_samples = g_variant_get_uint64(data); |
a8d09e13 BV |
199 | sr_dbg("Setting sample limit to %" PRIu64 ".", |
200 | devc->limit_samples); | |
201 | break; | |
4d729ddc | 202 | default: |
bd6fbf62 | 203 | return SR_ERR_NA; |
4d729ddc BV |
204 | } |
205 | ||
a8d09e13 | 206 | return SR_OK; |
4d729ddc BV |
207 | } |
208 | ||
dccda194 | 209 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi) |
a1c743fc BV |
210 | { |
211 | ||
212 | (void)sdi; | |
213 | ||
214 | switch (key) { | |
0d485e30 | 215 | case SR_CONF_SCAN_OPTIONS: |
dccda194 BV |
216 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
217 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
0d485e30 | 218 | break; |
9a6517d1 | 219 | case SR_CONF_DEVICE_OPTIONS: |
dccda194 BV |
220 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
221 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
9a6517d1 | 222 | break; |
a1c743fc | 223 | default: |
bd6fbf62 | 224 | return SR_ERR_NA; |
a1c743fc BV |
225 | } |
226 | ||
227 | return SR_OK; | |
228 | } | |
229 | ||
4d729ddc BV |
230 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, |
231 | void *cb_data) | |
232 | { | |
a8d09e13 | 233 | struct dev_context *devc; |
aa706635 | 234 | struct sr_serial_dev_inst *serial; |
a8d09e13 | 235 | |
e73ffd42 BV |
236 | if (sdi->status != SR_ST_ACTIVE) |
237 | return SR_ERR_DEV_CLOSED; | |
238 | ||
a8d09e13 BV |
239 | if (!(devc = sdi->priv)) { |
240 | sr_err("sdi->priv was NULL."); | |
241 | return SR_ERR_BUG; | |
242 | } | |
243 | ||
a8d09e13 BV |
244 | devc->cb_data = cb_data; |
245 | ||
246 | /* Send header packet to the session bus. */ | |
29a27196 | 247 | std_session_send_df_header(cb_data, LOG_PREFIX); |
a8d09e13 | 248 | |
a8d09e13 | 249 | /* Poll every 150ms, or whenever some data comes in. */ |
aa706635 BV |
250 | serial = sdi->conn; |
251 | sr_source_add(serial->fd, G_IO_IN, 150, colead_slm_receive_data, | |
a8d09e13 | 252 | (void *)sdi); |
4d729ddc BV |
253 | |
254 | return SR_OK; | |
255 | } | |
256 | ||
69b07d14 | 257 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
4d729ddc | 258 | { |
cd2f0fe2 | 259 | return std_hw_dev_acquisition_stop_serial(sdi, cb_data, hw_dev_close, |
29a27196 | 260 | sdi->conn, LOG_PREFIX); |
4d729ddc BV |
261 | } |
262 | ||
263 | SR_PRIV struct sr_dev_driver colead_slm_driver_info = { | |
264 | .name = "colead-slm", | |
265 | .longname = "Colead SLM", | |
266 | .api_version = 1, | |
267 | .init = hw_init, | |
268 | .cleanup = hw_cleanup, | |
269 | .scan = hw_scan, | |
270 | .dev_list = hw_dev_list, | |
271 | .dev_clear = clear_instances, | |
6fab7b8f | 272 | .config_get = NULL, |
035a1078 | 273 | .config_set = config_set, |
a1c743fc | 274 | .config_list = config_list, |
4d729ddc BV |
275 | .dev_open = hw_dev_open, |
276 | .dev_close = hw_dev_close, | |
4d729ddc BV |
277 | .dev_acquisition_start = hw_dev_acquisition_start, |
278 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
279 | .priv = NULL, | |
280 | }; |