]>
Commit | Line | Data |
---|---|---|
a4210e18 EK |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2016 danselmi <da@da> | |
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 <config.h> | |
21 | #include "protocol.h" | |
22 | ||
23 | static const uint32_t ipdbg_org_la_drvopts[] = { | |
24 | SR_CONF_LOGIC_ANALYZER, | |
25 | }; | |
26 | ||
27 | static const uint32_t ipdbg_org_la_scanopts[] = { | |
28 | SR_CONF_CONN, | |
29 | SR_CONF_SERIALCOMM, | |
30 | }; | |
31 | ||
32 | static const uint32_t ipdbg_org_la_devopts[] = { | |
33 | SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, | |
34 | SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET, | |
35 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, | |
36 | }; | |
37 | ||
38 | static const int32_t ipdbg_org_la_trigger_matches[] = { | |
39 | SR_TRIGGER_ZERO, | |
40 | SR_TRIGGER_ONE, | |
41 | SR_TRIGGER_RISING, | |
42 | SR_TRIGGER_FALLING, | |
43 | //SR_TRIGGER_EDGE, | |
44 | }; | |
45 | ||
46 | SR_PRIV struct sr_dev_driver ipdbg_la_driver_info; | |
47 | ||
48 | ||
49 | ||
50 | static void ipdbg_org_la_split_addr_port(const char *conn, char **addr, char **port) | |
51 | { | |
52 | char **strs = g_strsplit(conn, "/", 3); | |
53 | ||
54 | *addr = g_strdup(strs[1]); | |
55 | *port = g_strdup(strs[2]); | |
56 | ||
57 | g_strfreev(strs); | |
58 | } | |
59 | ||
60 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
61 | { | |
62 | sr_err("scan\n"); | |
63 | struct drv_context *drvc; | |
64 | GSList *devices; | |
65 | ||
66 | (void)options; | |
67 | ||
68 | devices = NULL; | |
69 | drvc = di->context; | |
70 | drvc->instances = NULL; | |
71 | const char *conn; | |
72 | struct sr_config *src; | |
73 | GSList *l; | |
74 | ||
75 | conn = NULL; | |
76 | for (l = options; l; l = l->next) { | |
77 | src = l->data; | |
78 | switch (src->key) { | |
79 | case SR_CONF_CONN: | |
80 | conn = g_variant_get_string(src->data, NULL); | |
81 | break; | |
82 | } | |
83 | } | |
84 | ||
85 | if (!conn) | |
86 | return NULL; | |
87 | ||
88 | struct ipdbg_org_la_tcp *tcp = ipdbg_org_la_new_tcp(); | |
89 | ||
90 | ipdbg_org_la_split_addr_port(conn, &tcp->address, &tcp->port); | |
91 | ||
92 | if (!tcp->address) | |
93 | return NULL; | |
94 | ||
95 | ||
96 | if(ipdbg_org_la_tcp_open(tcp) != SR_OK) | |
97 | return NULL; | |
98 | ||
99 | sr_err("set Reset\n"); | |
100 | ////////////////////////////////////////////////////////////////////////////////////////// | |
101 | ipdbg_org_la_sendReset(tcp); | |
102 | ipdbg_org_la_sendReset(tcp); | |
103 | ||
104 | ipdbg_org_la_requestID(tcp); | |
105 | ||
106 | struct sr_dev_inst *sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
107 | if(!sdi){ | |
108 | sr_err("no possible to allocate sr_dev_inst"); | |
109 | return NULL; | |
110 | } | |
111 | sdi->status = SR_ST_INACTIVE; | |
112 | sdi->vendor = g_strdup("ipdbg.org"); | |
113 | sdi->model = g_strdup("Logic Analyzer"); | |
114 | sdi->version = g_strdup("v1.0"); | |
115 | sdi->driver = di; | |
116 | const size_t bufSize = 16; | |
117 | char buff[bufSize]; | |
118 | ||
119 | ||
120 | struct ipdbg_org_la_dev_context *devc = ipdbg_org_la_dev_new(); | |
121 | sdi->priv = devc; | |
122 | ||
123 | ipdbg_org_la_get_addrwidth_and_datawidth(tcp, devc); | |
124 | ||
125 | sr_err("addr_width = %d, data_width = %d\n", devc->ADDR_WIDTH, devc->DATA_WIDTH); | |
126 | sr_err("limit samples = %d\n", devc->limit_samples); | |
127 | ///////////////////////////////////////////////////////////////////////////////////////////////////////// | |
128 | ||
129 | for (int i = 0; i < devc->DATA_WIDTH; i++) | |
130 | { | |
131 | snprintf(buff, bufSize, "ch%d", i); | |
132 | sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, buff); | |
133 | } | |
134 | ||
135 | sdi->inst_type = SR_INST_USER; | |
136 | sdi->conn = tcp; | |
137 | ||
138 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
139 | devices = g_slist_append(devices, sdi); | |
140 | ||
141 | ipdbg_org_la_tcp_close(tcp); | |
142 | ||
143 | return devices; | |
144 | } | |
145 | ||
146 | static int dev_clear(const struct sr_dev_driver *di) | |
147 | { | |
148 | sr_err("dev_clear\n"); | |
149 | ||
150 | struct drv_context *drvc = di->context; | |
151 | struct sr_dev_inst *sdi; | |
152 | GSList *l; | |
153 | ||
154 | if (drvc) { | |
155 | for (l = drvc->instances; l; l = l->next) { | |
156 | sdi = l->data; | |
157 | struct ipdbg_org_la_tcp *tcp = sdi->conn; | |
158 | if(tcp) | |
159 | { | |
160 | ipdbg_org_la_tcp_close(tcp); | |
161 | ipdbg_org_la_tcp_free(tcp); | |
162 | g_free(tcp); | |
163 | } | |
164 | sdi->conn = NULL; | |
165 | } | |
166 | } | |
167 | ||
168 | return std_dev_clear(di); | |
169 | } | |
170 | ||
171 | static int dev_open(struct sr_dev_inst *sdi) | |
172 | { | |
173 | sr_err("dev_open\n"); | |
174 | (void)sdi; | |
175 | ||
176 | /* TODO: get handle from sdi->conn and open it. */ | |
177 | sdi->status = SR_ST_INACTIVE; | |
178 | ||
179 | struct ipdbg_org_la_tcp *tcp = sdi->conn; | |
180 | ||
181 | if (!tcp) | |
182 | { | |
183 | sr_err("Out of memory\n"); | |
184 | return SR_ERR; | |
185 | } | |
186 | sdi->conn = tcp; | |
187 | ||
188 | if(ipdbg_org_la_tcp_open(tcp) != SR_OK) | |
189 | return SR_ERR; | |
190 | ||
191 | sdi->status = SR_ST_ACTIVE; | |
192 | ||
193 | return SR_OK; | |
194 | } | |
195 | ||
196 | static int dev_close(struct sr_dev_inst *sdi) | |
197 | { | |
198 | (void)sdi; | |
199 | ||
200 | /* TODO: get handle from sdi->conn and close it. */ | |
201 | sr_err("dev_close\n"); | |
202 | /// should be called before a new call to scan() | |
203 | struct ipdbg_org_la_tcp *tcp = sdi->conn; | |
204 | ipdbg_org_la_tcp_close(tcp); | |
205 | ||
206 | sdi->conn = NULL; | |
207 | ||
208 | sdi->status = SR_ST_INACTIVE; | |
209 | ||
210 | return SR_OK; | |
211 | } | |
212 | ||
213 | static int config_get(uint32_t key, GVariant **data, | |
214 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
215 | { | |
216 | int ret; | |
217 | ||
218 | (void)sdi; | |
219 | (void)data; | |
220 | (void)cg; | |
221 | ||
222 | struct ipdbg_org_la_dev_context *devc = sdi->priv; | |
223 | ||
224 | ret = SR_OK; | |
225 | switch (key) { | |
226 | case SR_CONF_CAPTURE_RATIO: | |
227 | *data = g_variant_new_uint64(devc->capture_ratio); | |
228 | break; | |
229 | case SR_CONF_LIMIT_SAMPLES: | |
230 | *data = g_variant_new_uint64(devc->limit_samples); | |
231 | break; | |
232 | default: | |
233 | return SR_ERR_NA; | |
234 | } | |
235 | ||
236 | return ret; | |
237 | } | |
238 | ||
239 | static int config_set(uint32_t key, GVariant *data, | |
240 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
241 | { | |
242 | int ret; | |
243 | ||
244 | (void)data; | |
245 | (void)cg; | |
246 | ||
247 | if (sdi->status != SR_ST_ACTIVE) | |
248 | return SR_ERR_DEV_CLOSED; | |
249 | ||
250 | sr_err("config_set\n"); | |
251 | struct ipdbg_org_la_dev_context *devc = sdi->priv; | |
252 | ||
253 | ret = SR_OK; | |
254 | switch (key) { | |
255 | case SR_CONF_CAPTURE_RATIO: | |
256 | devc->capture_ratio = g_variant_get_uint64(data); | |
257 | if (devc->capture_ratio < 0 || devc->capture_ratio > 100) | |
258 | { | |
259 | devc->capture_ratio = 50; | |
260 | ret = SR_ERR; | |
261 | } | |
262 | else | |
263 | ret = SR_OK; | |
264 | break; | |
265 | case SR_CONF_LIMIT_SAMPLES: | |
266 | devc->limit_samples = g_variant_get_uint64(data); | |
267 | if(devc->limit_samples > devc->limit_samples_max) | |
268 | { | |
269 | devc->limit_samples = devc->limit_samples_max; | |
270 | ret = SR_ERR; | |
271 | } | |
272 | else | |
273 | ret = SR_OK; | |
274 | break; | |
275 | default: | |
276 | ret = SR_ERR_NA; | |
277 | } | |
278 | ||
279 | return ret; | |
280 | } | |
281 | ||
282 | static int config_list(uint32_t key, GVariant **data, | |
283 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
284 | { | |
285 | (void)cg; | |
286 | ||
287 | switch (key) { | |
288 | case SR_CONF_SCAN_OPTIONS: | |
289 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, ipdbg_org_la_scanopts, ARRAY_SIZE(ipdbg_org_la_scanopts), sizeof(uint32_t)); | |
290 | break; | |
291 | case SR_CONF_DEVICE_OPTIONS: | |
292 | if (!sdi) | |
293 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, ipdbg_org_la_drvopts, ARRAY_SIZE(ipdbg_org_la_drvopts), sizeof(uint32_t)); | |
294 | else | |
295 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, ipdbg_org_la_devopts, ARRAY_SIZE(ipdbg_org_la_devopts), sizeof(uint32_t)); | |
296 | break; | |
297 | case SR_CONF_TRIGGER_MATCH: | |
298 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, ipdbg_org_la_trigger_matches, ARRAY_SIZE(ipdbg_org_la_trigger_matches), sizeof(int32_t)); | |
299 | break; | |
300 | default: | |
301 | return SR_ERR_NA; | |
302 | } | |
303 | ||
304 | return SR_OK; | |
305 | } | |
306 | ||
307 | ||
308 | static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) | |
309 | { | |
310 | return std_init(di, sr_ctx); | |
311 | } | |
312 | ||
313 | static int cleanup(const struct sr_dev_driver *di) | |
314 | { | |
315 | sr_err("cleanup\n"); | |
316 | dev_clear(di); | |
317 | ||
318 | //return std_cleanup(di); | |
319 | return SR_OK; | |
320 | } | |
321 | ||
322 | static GSList *dev_list(const struct sr_dev_driver *di) | |
323 | { | |
324 | //return std_dev_list(di); | |
325 | return ((struct drv_context *) (di->context))-> instances; | |
326 | } | |
327 | ||
328 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
329 | { | |
330 | ||
331 | if (sdi->status != SR_ST_ACTIVE) | |
332 | return SR_ERR_DEV_CLOSED; | |
333 | struct ipdbg_org_la_tcp *tcp = sdi->conn; | |
334 | ||
335 | struct ipdbg_org_la_dev_context *devc = sdi->priv; | |
336 | ||
337 | ipdbg_org_la_convert_trigger(sdi); | |
338 | sr_err("dev_acquisition_start\n"); | |
339 | ||
340 | /* Send Triggerkonviguration */ | |
341 | ipdbg_org_la_sendTrigger(devc, tcp); | |
342 | sr_err("dev_acquisition_start1\n"); | |
343 | ||
344 | /* Send Delay */ | |
345 | ipdbg_org_la_sendDelay(devc, tcp); | |
346 | sr_err("dev_acquisition_start2\n"); | |
347 | ||
348 | //std_session_send_df_header(sdi, LOG_PREFIX); | |
349 | std_session_send_df_header(sdi); | |
350 | sr_err("dev_acquisition_start3\n"); | |
351 | /* If the device stops sending for longer than it takes to send a byte, | |
352 | * that means it's finished. But wait at least 100 ms to be safe. | |
353 | */ | |
354 | //sr_session_source_add(sdi->session, -1, G_IO_IN, 100, ipdbg_receive_data, (struct sr_dev_inst *)sdi); | |
355 | //sr_session_source_add(sdi->session, -1, G_IO_IN, 100, ipdbg_org_la_receive_data, NULL); | |
356 | sr_session_source_add(sdi->session, tcp->socket, G_IO_IN, 100, ipdbg_org_la_receive_data, (struct sr_dev_inst *)sdi); | |
357 | sr_err("dev_acquisition_start4\n"); | |
358 | ||
359 | ipdbg_org_la_sendStart(tcp); | |
360 | sr_err("dev_acquisition_start5\n"); | |
361 | /* TODO: configure hardware, reset acquisition state, set up | |
362 | * callbacks and send header packet. */ | |
363 | ||
364 | return SR_OK; | |
365 | } | |
366 | ||
367 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
368 | { | |
369 | ||
370 | sr_err("dev_acquisition_stop\n"); | |
371 | ||
372 | if (sdi->status != SR_ST_ACTIVE) | |
373 | return SR_ERR_DEV_CLOSED; | |
374 | ||
375 | ipdbg_org_la_abort_acquisition(sdi); | |
376 | ||
377 | return SR_OK; | |
378 | } | |
379 | ||
380 | SR_PRIV struct sr_dev_driver ipdbg_la_driver_info = { | |
381 | .name = "ipdbg-org-la", | |
382 | .longname = "ipdbg.org logic analyzer", | |
383 | .api_version = 1, | |
384 | .init = init, | |
385 | .cleanup = cleanup, | |
386 | .scan = scan, | |
387 | .dev_list = dev_list, | |
388 | .dev_clear = dev_clear, | |
389 | .config_get = config_get, | |
390 | .config_set = config_set, | |
391 | .config_list = config_list, | |
392 | .dev_open = dev_open, | |
393 | .dev_close = dev_close, | |
394 | .dev_acquisition_start = dev_acquisition_start, | |
395 | .dev_acquisition_stop = dev_acquisition_stop, | |
396 | .context = NULL, | |
397 | }; | |
398 | ||
399 | SR_REGISTER_DEV_DRIVER(ipdbg_la_driver_info); |