]>
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 | ||
3b412e3a | 48 | static int dev_clear(void) |
4d729ddc | 49 | { |
732899f8 | 50 | return std_dev_clear(di, NULL); |
4d729ddc BV |
51 | } |
52 | ||
6078d2c9 | 53 | static int init(struct sr_context *sr_ctx) |
4d729ddc | 54 | { |
f6beaac5 | 55 | return std_init(sr_ctx, di, LOG_PREFIX); |
4d729ddc BV |
56 | } |
57 | ||
6078d2c9 | 58 | static GSList *scan(GSList *options) |
4d729ddc BV |
59 | { |
60 | struct drv_context *drvc; | |
a8d09e13 BV |
61 | struct dev_context *devc; |
62 | struct sr_dev_inst *sdi; | |
1987b8d6 | 63 | struct sr_config *src; |
a8d09e13 BV |
64 | struct sr_probe *probe; |
65 | GSList *devices, *l; | |
66 | const char *conn, *serialcomm; | |
4d729ddc | 67 | |
4d729ddc BV |
68 | drvc = di->priv; |
69 | drvc->instances = NULL; | |
70 | ||
4b97c74e UH |
71 | devices = NULL; |
72 | ||
a8d09e13 BV |
73 | conn = serialcomm = NULL; |
74 | for (l = options; l; l = l->next) { | |
1987b8d6 BV |
75 | src = l->data; |
76 | switch (src->key) { | |
1953564a | 77 | case SR_CONF_CONN: |
dccda194 | 78 | conn = g_variant_get_string(src->data, NULL); |
a8d09e13 | 79 | break; |
1953564a | 80 | case SR_CONF_SERIALCOMM: |
dccda194 | 81 | serialcomm = g_variant_get_string(src->data, NULL); |
a8d09e13 BV |
82 | break; |
83 | } | |
84 | } | |
85 | if (!conn) | |
86 | return NULL; | |
f306ca61 BV |
87 | if (!serialcomm) |
88 | serialcomm = SERIALCOMM; | |
a8d09e13 BV |
89 | |
90 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Colead", | |
91 | "SL-5868P", NULL))) | |
92 | return NULL; | |
93 | ||
94 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
4b97c74e | 95 | sr_dbg("Device context malloc failed."); |
a8d09e13 BV |
96 | return NULL; |
97 | } | |
98 | ||
aa706635 | 99 | if (!(sdi->conn = sr_serial_dev_inst_new(conn, serialcomm))) |
f306ca61 | 100 | return NULL; |
a8d09e13 | 101 | |
aa706635 | 102 | sdi->inst_type = SR_INST_SERIAL; |
a8d09e13 BV |
103 | sdi->priv = devc; |
104 | sdi->driver = di; | |
105 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) | |
106 | return NULL; | |
107 | sdi->probes = g_slist_append(sdi->probes, probe); | |
108 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
109 | devices = g_slist_append(devices, sdi); | |
4d729ddc BV |
110 | |
111 | return devices; | |
112 | } | |
113 | ||
6078d2c9 | 114 | static GSList *dev_list(void) |
4d729ddc | 115 | { |
0e94d524 | 116 | return ((struct drv_context *)(di->priv))->instances; |
4d729ddc BV |
117 | } |
118 | ||
6078d2c9 | 119 | static int dev_open(struct sr_dev_inst *sdi) |
4d729ddc | 120 | { |
aa706635 | 121 | struct sr_serial_dev_inst *serial; |
a8d09e13 | 122 | |
aa706635 BV |
123 | serial = sdi->conn; |
124 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
a8d09e13 | 125 | return SR_ERR; |
f306ca61 | 126 | |
a8d09e13 | 127 | sdi->status = SR_ST_ACTIVE; |
4d729ddc BV |
128 | |
129 | return SR_OK; | |
130 | } | |
131 | ||
6078d2c9 | 132 | static int cleanup(void) |
4d729ddc | 133 | { |
3b412e3a | 134 | return dev_clear(); |
4d729ddc BV |
135 | } |
136 | ||
8f996b89 ML |
137 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi, |
138 | const struct sr_probe_group *probe_group) | |
4d729ddc | 139 | { |
a8d09e13 | 140 | struct dev_context *devc; |
4d729ddc | 141 | |
8f996b89 ML |
142 | (void)probe_group; |
143 | ||
a8d09e13 | 144 | if (sdi->status != SR_ST_ACTIVE) |
e73ffd42 | 145 | return SR_ERR_DEV_CLOSED; |
a8d09e13 BV |
146 | |
147 | if (!(devc = sdi->priv)) { | |
148 | sr_err("sdi->priv was NULL."); | |
149 | return SR_ERR_BUG; | |
4d729ddc BV |
150 | } |
151 | ||
035a1078 | 152 | switch (id) { |
1953564a | 153 | case SR_CONF_LIMIT_MSEC: |
a8d09e13 | 154 | /* TODO: not yet implemented */ |
dccda194 | 155 | if (g_variant_get_uint64(data) == 0) { |
a8d09e13 BV |
156 | sr_err("LIMIT_MSEC can't be 0."); |
157 | return SR_ERR; | |
158 | } | |
dccda194 | 159 | devc->limit_msec = g_variant_get_uint64(data);; |
a8d09e13 BV |
160 | sr_dbg("Setting time limit to %" PRIu64 "ms.", |
161 | devc->limit_msec); | |
162 | break; | |
1953564a | 163 | case SR_CONF_LIMIT_SAMPLES: |
dccda194 | 164 | devc->limit_samples = g_variant_get_uint64(data); |
a8d09e13 BV |
165 | sr_dbg("Setting sample limit to %" PRIu64 ".", |
166 | devc->limit_samples); | |
167 | break; | |
4d729ddc | 168 | default: |
bd6fbf62 | 169 | return SR_ERR_NA; |
4d729ddc BV |
170 | } |
171 | ||
a8d09e13 | 172 | return SR_OK; |
4d729ddc BV |
173 | } |
174 | ||
8f996b89 ML |
175 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, |
176 | const struct sr_probe_group *probe_group) | |
a1c743fc | 177 | { |
a1c743fc | 178 | (void)sdi; |
8f996b89 | 179 | (void)probe_group; |
a1c743fc BV |
180 | |
181 | switch (key) { | |
0d485e30 | 182 | case SR_CONF_SCAN_OPTIONS: |
dccda194 BV |
183 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
184 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
0d485e30 | 185 | break; |
9a6517d1 | 186 | case SR_CONF_DEVICE_OPTIONS: |
dccda194 BV |
187 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
188 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
9a6517d1 | 189 | break; |
a1c743fc | 190 | default: |
bd6fbf62 | 191 | return SR_ERR_NA; |
a1c743fc BV |
192 | } |
193 | ||
194 | return SR_OK; | |
195 | } | |
196 | ||
6078d2c9 | 197 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) |
4d729ddc | 198 | { |
a8d09e13 | 199 | struct dev_context *devc; |
aa706635 | 200 | struct sr_serial_dev_inst *serial; |
a8d09e13 | 201 | |
e73ffd42 BV |
202 | if (sdi->status != SR_ST_ACTIVE) |
203 | return SR_ERR_DEV_CLOSED; | |
204 | ||
a8d09e13 BV |
205 | if (!(devc = sdi->priv)) { |
206 | sr_err("sdi->priv was NULL."); | |
207 | return SR_ERR_BUG; | |
208 | } | |
209 | ||
a8d09e13 BV |
210 | devc->cb_data = cb_data; |
211 | ||
212 | /* Send header packet to the session bus. */ | |
29a27196 | 213 | std_session_send_df_header(cb_data, LOG_PREFIX); |
a8d09e13 | 214 | |
a8d09e13 | 215 | /* Poll every 150ms, or whenever some data comes in. */ |
aa706635 | 216 | serial = sdi->conn; |
abc4b335 | 217 | serial_source_add(serial, G_IO_IN, 150, colead_slm_receive_data, |
a8d09e13 | 218 | (void *)sdi); |
4d729ddc BV |
219 | |
220 | return SR_OK; | |
221 | } | |
222 | ||
6078d2c9 | 223 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
4d729ddc | 224 | { |
d43b0908 | 225 | return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close, |
f6beaac5 | 226 | sdi->conn, LOG_PREFIX); |
4d729ddc BV |
227 | } |
228 | ||
229 | SR_PRIV struct sr_dev_driver colead_slm_driver_info = { | |
230 | .name = "colead-slm", | |
231 | .longname = "Colead SLM", | |
232 | .api_version = 1, | |
6078d2c9 UH |
233 | .init = init, |
234 | .cleanup = cleanup, | |
235 | .scan = scan, | |
236 | .dev_list = dev_list, | |
3b412e3a | 237 | .dev_clear = dev_clear, |
6fab7b8f | 238 | .config_get = NULL, |
035a1078 | 239 | .config_set = config_set, |
a1c743fc | 240 | .config_list = config_list, |
6078d2c9 | 241 | .dev_open = dev_open, |
bf2c987f | 242 | .dev_close = std_serial_dev_close, |
6078d2c9 UH |
243 | .dev_acquisition_start = dev_acquisition_start, |
244 | .dev_acquisition_stop = dev_acquisition_stop, | |
4d729ddc BV |
245 | .priv = NULL, |
246 | }; |