]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Aurelien Jacobs <aurel@gnuage.org> | |
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 <string.h> | |
21 | #include "protocol.h" | |
22 | ||
23 | static const uint32_t scanopts[] = { | |
24 | SR_CONF_CONN, | |
25 | SR_CONF_SERIALCOMM, | |
26 | }; | |
27 | ||
28 | static const uint32_t drvopts[] = { | |
29 | SR_CONF_THERMOMETER, | |
30 | }; | |
31 | ||
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, | |
37 | }; | |
38 | ||
39 | static const char *data_sources[] = { | |
40 | "Live", | |
41 | "Memory", | |
42 | }; | |
43 | ||
44 | SR_PRIV struct sr_dev_driver appa_55ii_driver_info; | |
45 | ||
46 | static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) | |
47 | { | |
48 | return std_init(sr_ctx, di, LOG_PREFIX); | |
49 | } | |
50 | ||
51 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
52 | { | |
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; | |
58 | GSList *devices, *l; | |
59 | const char *conn, *serialcomm; | |
60 | uint8_t buf[50]; | |
61 | size_t len; | |
62 | ||
63 | len = sizeof(buf); | |
64 | devices = NULL; | |
65 | conn = serialcomm = NULL; | |
66 | for (l = options; l; l = l->next) { | |
67 | src = l->data; | |
68 | switch (src->key) { | |
69 | case SR_CONF_CONN: | |
70 | conn = g_variant_get_string(src->data, NULL); | |
71 | break; | |
72 | case SR_CONF_SERIALCOMM: | |
73 | serialcomm = g_variant_get_string(src->data, NULL); | |
74 | break; | |
75 | } | |
76 | } | |
77 | if (!conn) | |
78 | return NULL; | |
79 | if (!serialcomm) | |
80 | serialcomm = "9600/8n1"; | |
81 | ||
82 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
83 | ||
84 | if (serial_open(serial, SERIAL_RDONLY) != SR_OK) | |
85 | return NULL; | |
86 | ||
87 | sr_info("Probing serial port %s.", conn); | |
88 | ||
89 | drvc = di->priv; | |
90 | drvc->instances = NULL; | |
91 | serial_flush(serial); | |
92 | ||
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) | |
96 | goto scan_cleanup; | |
97 | ||
98 | sr_info("Found device on port %s.", conn); | |
99 | ||
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; | |
107 | sdi->conn = serial; | |
108 | sdi->priv = devc; | |
109 | sdi->driver = di; | |
110 | ||
111 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "T1"); | |
112 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "T2"); | |
113 | ||
114 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
115 | devices = g_slist_append(devices, sdi); | |
116 | ||
117 | scan_cleanup: | |
118 | serial_close(serial); | |
119 | ||
120 | return devices; | |
121 | } | |
122 | ||
123 | static GSList *dev_list(const struct sr_dev_driver *di) | |
124 | { | |
125 | return ((struct drv_context *)(di->priv))->instances; | |
126 | } | |
127 | ||
128 | static int cleanup(const struct sr_dev_driver *di) | |
129 | { | |
130 | return std_dev_clear(di, NULL); | |
131 | } | |
132 | ||
133 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
134 | const struct sr_channel_group *cg) | |
135 | { | |
136 | struct dev_context *devc = sdi->priv; | |
137 | ||
138 | (void)cg; | |
139 | ||
140 | switch (key) { | |
141 | case SR_CONF_LIMIT_SAMPLES: | |
142 | *data = g_variant_new_uint64(devc->limit_samples); | |
143 | break; | |
144 | case SR_CONF_LIMIT_MSEC: | |
145 | *data = g_variant_new_uint64(devc->limit_msec); | |
146 | break; | |
147 | case SR_CONF_DATA_SOURCE: | |
148 | *data = g_variant_new_string(data_sources[devc->data_source]); | |
149 | break; | |
150 | default: | |
151 | return SR_ERR_NA; | |
152 | } | |
153 | ||
154 | return SR_OK; | |
155 | } | |
156 | ||
157 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
158 | const struct sr_channel_group *cg) | |
159 | { | |
160 | struct dev_context *devc; | |
161 | const char *tmp_str; | |
162 | unsigned int i; | |
163 | ||
164 | (void)cg; | |
165 | ||
166 | if (sdi->status != SR_ST_ACTIVE) | |
167 | return SR_ERR_DEV_CLOSED; | |
168 | ||
169 | if (!(devc = sdi->priv)) { | |
170 | sr_err("sdi->priv was NULL."); | |
171 | return SR_ERR_BUG; | |
172 | } | |
173 | ||
174 | switch (key) { | |
175 | case SR_CONF_LIMIT_SAMPLES: | |
176 | devc->limit_samples = g_variant_get_uint64(data); | |
177 | sr_dbg("Setting sample limit to %" PRIu64 ".", devc->limit_samples); | |
178 | break; | |
179 | case SR_CONF_LIMIT_MSEC: | |
180 | devc->limit_msec = g_variant_get_uint64(data); | |
181 | sr_dbg("Setting time limit to %" PRIu64 "ms.", devc->limit_msec); | |
182 | break; | |
183 | case SR_CONF_DATA_SOURCE: { | |
184 | tmp_str = g_variant_get_string(data, NULL); | |
185 | for (i = 0; i < ARRAY_SIZE(data_sources); i++) | |
186 | if (!strcmp(tmp_str, data_sources[i])) { | |
187 | devc->data_source = i; | |
188 | break; | |
189 | } | |
190 | if (i == ARRAY_SIZE(data_sources)) | |
191 | return SR_ERR; | |
192 | break; | |
193 | } | |
194 | default: | |
195 | return SR_ERR_NA; | |
196 | } | |
197 | ||
198 | return SR_OK; | |
199 | } | |
200 | ||
201 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
202 | const struct sr_channel_group *cg) | |
203 | { | |
204 | (void)sdi; | |
205 | (void)cg; | |
206 | ||
207 | switch (key) { | |
208 | case SR_CONF_SCAN_OPTIONS: | |
209 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
210 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
211 | break; | |
212 | case SR_CONF_DEVICE_OPTIONS: | |
213 | if (!sdi) | |
214 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
215 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
216 | else | |
217 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
218 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
219 | break; | |
220 | case SR_CONF_DATA_SOURCE: | |
221 | *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources)); | |
222 | break; | |
223 | default: | |
224 | return SR_ERR_NA; | |
225 | } | |
226 | ||
227 | return SR_OK; | |
228 | } | |
229 | ||
230 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, | |
231 | void *cb_data) | |
232 | { | |
233 | struct sr_serial_dev_inst *serial; | |
234 | struct dev_context *devc; | |
235 | ||
236 | serial = sdi->conn; | |
237 | if (sdi->status != SR_ST_ACTIVE) | |
238 | return SR_ERR_DEV_CLOSED; | |
239 | ||
240 | if (!(devc = sdi->priv)) { | |
241 | sr_err("sdi->priv was NULL."); | |
242 | return SR_ERR_BUG; | |
243 | } | |
244 | ||
245 | devc->session_cb_data = cb_data; | |
246 | ||
247 | /* | |
248 | * Reset the number of samples to take. If we've already collected our | |
249 | * quota, but we start a new session, and don't reset this, we'll just | |
250 | * quit without acquiring any new samples. | |
251 | */ | |
252 | devc->num_samples = 0; | |
253 | devc->start_time = g_get_monotonic_time(); | |
254 | ||
255 | /* Send header packet to the session bus. */ | |
256 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
257 | ||
258 | /* Poll every 50ms, or whenever some data comes in. */ | |
259 | serial_source_add(sdi->session, serial, G_IO_IN, 50, | |
260 | appa_55ii_receive_data, (void *)sdi); | |
261 | ||
262 | return SR_OK; | |
263 | } | |
264 | ||
265 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
266 | { | |
267 | return std_serial_dev_acquisition_stop(sdi, cb_data, | |
268 | std_serial_dev_close, sdi->conn, LOG_PREFIX); | |
269 | } | |
270 | ||
271 | SR_PRIV struct sr_dev_driver appa_55ii_driver_info = { | |
272 | .name = "appa-55ii", | |
273 | .longname = "APPA 55II", | |
274 | .api_version = 1, | |
275 | .init = init, | |
276 | .cleanup = cleanup, | |
277 | .scan = scan, | |
278 | .dev_list = dev_list, | |
279 | .dev_clear = NULL, | |
280 | .config_get = config_get, | |
281 | .config_set = config_set, | |
282 | .config_list = config_list, | |
283 | .dev_open = std_serial_dev_open, | |
284 | .dev_close = std_serial_dev_close, | |
285 | .dev_acquisition_start = dev_acquisition_start, | |
286 | .dev_acquisition_stop = dev_acquisition_stop, | |
287 | .priv = NULL, | |
288 | }; |