]>
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; | |
54 | GSList *l; | |
55 | ||
538ac9a9 BV |
56 | if (!(drvc = di->priv)) |
57 | return SR_OK; | |
58 | ||
4d729ddc BV |
59 | for (l = drvc->instances; l; l = l->next) { |
60 | if (!(sdi = l->data)) | |
61 | continue; | |
62 | if (!(devc = sdi->priv)) | |
63 | continue; | |
a8d09e13 | 64 | sr_serial_dev_inst_free(devc->serial); |
4d729ddc BV |
65 | sr_dev_inst_free(sdi); |
66 | } | |
4d729ddc BV |
67 | g_slist_free(drvc->instances); |
68 | drvc->instances = NULL; | |
69 | ||
70 | return SR_OK; | |
71 | } | |
72 | ||
34f06b90 | 73 | static int hw_init(struct sr_context *sr_ctx) |
4d729ddc | 74 | { |
063e7aef | 75 | return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); |
4d729ddc BV |
76 | } |
77 | ||
78 | static GSList *hw_scan(GSList *options) | |
79 | { | |
80 | struct drv_context *drvc; | |
a8d09e13 BV |
81 | struct dev_context *devc; |
82 | struct sr_dev_inst *sdi; | |
1987b8d6 | 83 | struct sr_config *src; |
a8d09e13 BV |
84 | struct sr_probe *probe; |
85 | GSList *devices, *l; | |
86 | const char *conn, *serialcomm; | |
4d729ddc | 87 | |
4d729ddc BV |
88 | drvc = di->priv; |
89 | drvc->instances = NULL; | |
90 | ||
4b97c74e UH |
91 | devices = NULL; |
92 | ||
a8d09e13 BV |
93 | conn = serialcomm = NULL; |
94 | for (l = options; l; l = l->next) { | |
1987b8d6 BV |
95 | src = l->data; |
96 | switch (src->key) { | |
1953564a | 97 | case SR_CONF_CONN: |
dccda194 | 98 | conn = g_variant_get_string(src->data, NULL); |
a8d09e13 | 99 | break; |
1953564a | 100 | case SR_CONF_SERIALCOMM: |
dccda194 | 101 | serialcomm = g_variant_get_string(src->data, NULL); |
a8d09e13 BV |
102 | break; |
103 | } | |
104 | } | |
105 | if (!conn) | |
106 | return NULL; | |
f306ca61 BV |
107 | if (!serialcomm) |
108 | serialcomm = SERIALCOMM; | |
a8d09e13 BV |
109 | |
110 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Colead", | |
111 | "SL-5868P", NULL))) | |
112 | return NULL; | |
113 | ||
114 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
4b97c74e | 115 | sr_dbg("Device context malloc failed."); |
a8d09e13 BV |
116 | return NULL; |
117 | } | |
118 | ||
f306ca61 BV |
119 | if (!(devc->serial = sr_serial_dev_inst_new(conn, serialcomm))) |
120 | return NULL; | |
a8d09e13 | 121 | |
a8d09e13 BV |
122 | sdi->priv = devc; |
123 | sdi->driver = di; | |
124 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) | |
125 | return NULL; | |
126 | sdi->probes = g_slist_append(sdi->probes, probe); | |
127 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
128 | devices = g_slist_append(devices, sdi); | |
4d729ddc BV |
129 | |
130 | return devices; | |
131 | } | |
132 | ||
133 | static GSList *hw_dev_list(void) | |
134 | { | |
0e94d524 | 135 | return ((struct drv_context *)(di->priv))->instances; |
4d729ddc BV |
136 | } |
137 | ||
138 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
139 | { | |
a8d09e13 BV |
140 | struct dev_context *devc; |
141 | ||
142 | if (!(devc = sdi->priv)) { | |
143 | sr_err("sdi->priv was NULL."); | |
144 | return SR_ERR_BUG; | |
145 | } | |
146 | ||
a54dd31e | 147 | if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK) |
a8d09e13 | 148 | return SR_ERR; |
f306ca61 | 149 | |
a8d09e13 | 150 | sdi->status = SR_ST_ACTIVE; |
4d729ddc BV |
151 | |
152 | return SR_OK; | |
153 | } | |
154 | ||
155 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
156 | { | |
a8d09e13 BV |
157 | struct dev_context *devc; |
158 | ||
961009b0 | 159 | devc = sdi->priv; |
a8d09e13 BV |
160 | |
161 | if (devc->serial && devc->serial->fd != -1) { | |
f306ca61 | 162 | serial_close(devc->serial); |
a8d09e13 BV |
163 | sdi->status = SR_ST_INACTIVE; |
164 | } | |
4d729ddc BV |
165 | |
166 | return SR_OK; | |
167 | } | |
168 | ||
169 | static int hw_cleanup(void) | |
170 | { | |
171 | clear_instances(); | |
172 | ||
4d729ddc BV |
173 | return SR_OK; |
174 | } | |
175 | ||
dccda194 | 176 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi) |
4d729ddc | 177 | { |
a8d09e13 | 178 | struct dev_context *devc; |
4d729ddc | 179 | |
a8d09e13 | 180 | if (sdi->status != SR_ST_ACTIVE) |
4d729ddc | 181 | return SR_ERR; |
a8d09e13 BV |
182 | |
183 | if (!(devc = sdi->priv)) { | |
184 | sr_err("sdi->priv was NULL."); | |
185 | return SR_ERR_BUG; | |
4d729ddc BV |
186 | } |
187 | ||
035a1078 | 188 | switch (id) { |
1953564a | 189 | case SR_CONF_LIMIT_MSEC: |
a8d09e13 | 190 | /* TODO: not yet implemented */ |
dccda194 | 191 | if (g_variant_get_uint64(data) == 0) { |
a8d09e13 BV |
192 | sr_err("LIMIT_MSEC can't be 0."); |
193 | return SR_ERR; | |
194 | } | |
dccda194 | 195 | devc->limit_msec = g_variant_get_uint64(data);; |
a8d09e13 BV |
196 | sr_dbg("Setting time limit to %" PRIu64 "ms.", |
197 | devc->limit_msec); | |
198 | break; | |
1953564a | 199 | case SR_CONF_LIMIT_SAMPLES: |
dccda194 | 200 | devc->limit_samples = g_variant_get_uint64(data); |
a8d09e13 BV |
201 | sr_dbg("Setting sample limit to %" PRIu64 ".", |
202 | devc->limit_samples); | |
203 | break; | |
4d729ddc | 204 | default: |
035a1078 | 205 | sr_err("Unknown capability: %d.", id); |
a8d09e13 BV |
206 | return SR_ERR; |
207 | break; | |
4d729ddc BV |
208 | } |
209 | ||
a8d09e13 | 210 | return SR_OK; |
4d729ddc BV |
211 | } |
212 | ||
dccda194 | 213 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi) |
a1c743fc BV |
214 | { |
215 | ||
216 | (void)sdi; | |
217 | ||
218 | switch (key) { | |
0d485e30 | 219 | case SR_CONF_SCAN_OPTIONS: |
dccda194 BV |
220 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
221 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
0d485e30 | 222 | break; |
9a6517d1 | 223 | case SR_CONF_DEVICE_OPTIONS: |
dccda194 BV |
224 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
225 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
9a6517d1 | 226 | break; |
a1c743fc BV |
227 | default: |
228 | return SR_ERR_ARG; | |
229 | } | |
230 | ||
231 | return SR_OK; | |
232 | } | |
233 | ||
4d729ddc BV |
234 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, |
235 | void *cb_data) | |
236 | { | |
a8d09e13 BV |
237 | struct dev_context *devc; |
238 | ||
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. */ | |
4afdfd46 | 247 | std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN); |
a8d09e13 | 248 | |
a8d09e13 BV |
249 | /* Poll every 150ms, or whenever some data comes in. */ |
250 | sr_source_add(devc->serial->fd, G_IO_IN, 150, colead_slm_receive_data, | |
251 | (void *)sdi); | |
4d729ddc BV |
252 | |
253 | return SR_OK; | |
254 | } | |
255 | ||
69b07d14 | 256 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
4d729ddc | 257 | { |
cd2f0fe2 UH |
258 | return std_hw_dev_acquisition_stop_serial(sdi, cb_data, hw_dev_close, |
259 | ((struct dev_context *)(sdi->priv))->serial, DRIVER_LOG_DOMAIN); | |
4d729ddc BV |
260 | } |
261 | ||
262 | SR_PRIV struct sr_dev_driver colead_slm_driver_info = { | |
263 | .name = "colead-slm", | |
264 | .longname = "Colead SLM", | |
265 | .api_version = 1, | |
266 | .init = hw_init, | |
267 | .cleanup = hw_cleanup, | |
268 | .scan = hw_scan, | |
269 | .dev_list = hw_dev_list, | |
270 | .dev_clear = clear_instances, | |
6fab7b8f | 271 | .config_get = NULL, |
035a1078 | 272 | .config_set = config_set, |
a1c743fc | 273 | .config_list = config_list, |
4d729ddc BV |
274 | .dev_open = hw_dev_open, |
275 | .dev_close = hw_dev_close, | |
4d729ddc BV |
276 | .dev_acquisition_start = hw_dev_acquisition_start, |
277 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
278 | .priv = NULL, | |
279 | }; |