]> sigrok.org Git - libsigrok.git/blame_incremental - src/hardware/ipdbg-la/protocol.c
ipdbg-la: working on windows
[libsigrok.git] / src / hardware / ipdbg-la / protocol.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2016 Eva Kissling <eva.kissling@bluewin.ch>
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
22#ifdef _WIN32
23#define _WIN32_WINNT 0x0501
24#include <winsock2.h>
25#include <ws2tcpip.h>
26#else
27#include <sys/socket.h>
28#include <netinet/in.h>
29#include <arpa/inet.h>
30#include <netdb.h>
31#include <sys/ioctl.h>
32#endif
33#include <string.h>
34#include <unistd.h>
35#include <errno.h>
36#include "protocol.h"
37
38#define BUFFER_SIZE 4
39
40/* Top-level command opcodes */
41#define CMD_SET_TRIGGER 0x00
42#define CMD_CFG_TRIGGER 0xF0
43#define CMD_CFG_LA 0x0F
44#define CMD_START 0xFE
45#define CMD_RESET 0xEE
46
47#define CMD_GET_BUS_WIDTHS 0xAA
48#define CMD_GET_LA_ID 0xBB
49#define CMD_ESCAPE 0x55
50
51/* Trigger subfunction command opcodes */
52#define CMD_TRIG_MASKS 0xF1
53#define CMD_TRIG_MASK 0xF3
54#define CMD_TRIG_VALUE 0xF7
55
56#define CMD_TRIG_MASKS_LAST 0xF9
57#define CMD_TRIG_MASK_LAST 0xFB
58#define CMD_TRIG_VALUE_LAST 0xFF
59
60#define CMD_TRIG_SELECT_EDGE_MASK 0xF5
61#define CMD_TRIG_SET_EDGE_MASK 0xF6
62
63/* LA subfunction command opcodes */
64#define CMD_LA_DELAY 0x1F
65
66static gboolean data_available(struct ipdbg_la_tcp *tcp)
67{
68#ifdef _WIN32
69 u_long bytes_available;
70 if(ioctlsocket(tcp->socket, FIONREAD, &bytes_available) != 0){
71#else
72 int bytes_available;
73 if (ioctl(tcp->socket, FIONREAD, &bytes_available) < 0){ // TIOCMGET
74#endif
75 sr_err("FIONREAD failed: %s\n", g_strerror(errno));
76 return FALSE;
77 }
78 return (bytes_available > 0);
79}
80
81SR_PRIV struct ipdbg_la_tcp *ipdbg_la_tcp_new(void)
82{
83 struct ipdbg_la_tcp *tcp;
84
85 tcp = g_malloc0(sizeof(struct ipdbg_la_tcp));
86 tcp->address = NULL;
87 tcp->port = NULL;
88 tcp->socket = -1;
89
90 return tcp;
91}
92
93SR_PRIV void ipdbg_la_tcp_free(struct ipdbg_la_tcp *tcp)
94{
95 g_free(tcp->address);
96 g_free(tcp->port);
97}
98
99SR_PRIV int ipdbg_la_tcp_open(struct ipdbg_la_tcp *tcp)
100{
101 struct addrinfo hints;
102 struct addrinfo *results, *res;
103 int err;
104
105 memset(&hints, 0, sizeof(hints));
106 hints.ai_family = AF_UNSPEC;
107 hints.ai_socktype = SOCK_STREAM;
108 hints.ai_protocol = IPPROTO_TCP;
109
110 err = getaddrinfo(tcp->address, tcp->port, &hints, &results);
111
112 if (err) {
113 sr_err("Address lookup failed: %s:%s: %s", tcp->address,
114 tcp->port, gai_strerror(err));
115 return SR_ERR;
116 }
117
118 for (res = results; res; res = res->ai_next) {
119 if ((tcp->socket = socket(res->ai_family, res->ai_socktype,
120 res->ai_protocol)) < 0)
121 continue;
122 if (connect(tcp->socket, res->ai_addr, res->ai_addrlen) != 0) {
123 close(tcp->socket);
124 tcp->socket = -1;
125 continue;
126 }
127 break;
128 }
129
130 freeaddrinfo(results);
131
132 if (tcp->socket < 0) {
133 sr_err("Failed to connect to %s:%s: %s", tcp->address, tcp->port,
134 g_strerror(errno));
135 return SR_ERR;
136 }
137
138 return SR_OK;
139}
140
141SR_PRIV int ipdbg_la_tcp_close(struct ipdbg_la_tcp *tcp)
142{
143 int ret = SR_OK;
144
145#ifdef _WIN32
146 if (shutdown(tcp->socket, SD_SEND) != SOCKET_ERROR)
147 {
148 char recvbuf[16];
149 int recvbuflen = 16;
150 // Receive until the peer closes the connection
151 while(recv(tcp->socket, recvbuf, recvbuflen, 0) > 0);
152 }
153#endif
154 if (close(tcp->socket) < 0)
155 ret = SR_ERR;
156
157 tcp->socket = -1;
158
159 return ret;
160}
161
162static int tcp_send(struct ipdbg_la_tcp *tcp, const uint8_t *buf, size_t len)
163{
164 int out;
165 out = send(tcp->socket, (const char *)buf, len, 0);
166
167 if (out < 0) {
168 sr_err("Send error: %s", g_strerror(errno));
169 return SR_ERR;
170 }
171
172 if (out < (int)len)
173 sr_dbg("Only sent %d/%d bytes of data.", out, (int)len);
174
175 return SR_OK;
176}
177
178static int tcp_receive_blocking(struct ipdbg_la_tcp *tcp,
179 uint8_t *buf, int bufsize)
180{
181 int received = 0;
182 int error_count = 0;
183
184 /* Timeout after 500ms of not receiving data */
185 while ((received < bufsize) && (error_count < 500)) {
186 if (ipdbg_la_tcp_receive(tcp, buf) > 0) {
187 buf++;
188 received++;
189 } else {
190 error_count++;
191 g_usleep(1000); /* Sleep for 1ms */
192 }
193 }
194
195 return received;
196}
197
198SR_PRIV int ipdbg_la_tcp_receive(struct ipdbg_la_tcp *tcp,
199 uint8_t *buf)
200{
201 int received = 0;
202
203 if (data_available(tcp)) {
204 while (received < 1) {
205 int len = recv(tcp->socket, (char *)buf, 1, 0);
206
207 if (len < 0) {
208 sr_err("Receive error: %s", g_strerror(errno));
209 return SR_ERR;
210 } else
211 received += len;
212 }
213
214 return received;
215 } else
216 return -1;
217}
218
219SR_PRIV int ipdbg_la_convert_trigger(const struct sr_dev_inst *sdi)
220{
221 struct dev_context *devc;
222 struct sr_trigger *trigger;
223 struct sr_trigger_stage *stage;
224 struct sr_trigger_match *match;
225 const GSList *l, *m;
226
227 devc = sdi->priv;
228
229 devc->num_stages = 0;
230 devc->num_transfers = 0;
231 devc->raw_sample_buf = NULL;
232
233 for (uint64_t i = 0; i < devc->data_width_bytes; i++) {
234 devc->trigger_mask[i] = 0;
235 devc->trigger_value[i] = 0;
236 devc->trigger_mask_last[i] = 0;
237 devc->trigger_value_last[i] = 0;
238 devc->trigger_edge_mask[i] = 0;
239 }
240
241 if (!(trigger = sr_session_trigger_get(sdi->session)))
242 return SR_OK;
243
244 for (l = trigger->stages; l; l = l->next) {
245 stage = l->data;
246 for (m = stage->matches; m; m = m->next) {
247 match = m->data;
248 int byte_idx = match->channel->index / 8;
249 uint8_t match_bit = 1 << (match->channel->index % 8);
250
251 if (!match->channel->enabled)
252 /* Ignore disabled channels with a trigger. */
253 continue;
254
255 if (match->match == SR_TRIGGER_ONE) {
256 devc->trigger_value[byte_idx] |= match_bit;
257 devc->trigger_mask[byte_idx] |= match_bit;
258 devc->trigger_mask_last[byte_idx] &= ~match_bit;
259 devc->trigger_edge_mask[byte_idx] &= ~match_bit;
260 } else if (match->match == SR_TRIGGER_ZERO) {
261 devc->trigger_value[byte_idx] &= ~match_bit;
262 devc->trigger_mask[byte_idx] |= match_bit;
263 devc->trigger_mask_last[byte_idx] &= ~match_bit;
264 devc->trigger_edge_mask[byte_idx] &= ~match_bit;
265 } else if (match->match == SR_TRIGGER_RISING) {
266 devc->trigger_value[byte_idx] |= match_bit;
267 devc->trigger_value_last[byte_idx] &=
268 ~match_bit;
269 devc->trigger_mask[byte_idx] |= match_bit;
270 devc->trigger_mask_last[byte_idx] |= match_bit;
271 devc->trigger_edge_mask[byte_idx] &= ~match_bit;
272 } else if (match->match == SR_TRIGGER_FALLING) {
273 devc->trigger_value[byte_idx] &= ~match_bit;
274 devc->trigger_value_last[byte_idx] |= match_bit;
275 devc->trigger_mask[byte_idx] |= match_bit;
276 devc->trigger_mask_last[byte_idx] |= match_bit;
277 devc->trigger_edge_mask[byte_idx] &= ~match_bit;
278 } else if (match->match == SR_TRIGGER_EDGE) {
279 devc->trigger_mask[byte_idx] &= ~match_bit;
280 devc->trigger_mask_last[byte_idx] &= ~match_bit;
281 devc->trigger_edge_mask[byte_idx] |= match_bit;
282 }
283 }
284 }
285
286 return SR_OK;
287}
288
289SR_PRIV int ipdbg_la_receive_data(int fd, int revents, void *cb_data)
290{
291 const struct sr_dev_inst *sdi;
292 struct dev_context *devc;
293
294 (void)fd;
295 (void)revents;
296
297 sdi = cb_data;
298 if (!sdi)
299 return FALSE;
300
301 if (!(devc = sdi->priv))
302 return FALSE;
303
304 struct ipdbg_la_tcp *tcp = sdi->conn;
305 struct sr_datafeed_packet packet;
306 struct sr_datafeed_logic logic;
307
308 if (!devc->raw_sample_buf) {
309 devc->raw_sample_buf =
310 g_try_malloc(devc->limit_samples * devc->data_width_bytes);
311 if (!devc->raw_sample_buf) {
312 sr_err("Sample buffer malloc failed.");
313 return FALSE;
314 }
315 }
316
317 if (devc->num_transfers <
318 (devc->limit_samples_max * devc->data_width_bytes)) {
319 uint8_t byte;
320
321 if (ipdbg_la_tcp_receive(tcp, &byte) == 1) {
322 if (devc->num_transfers <
323 (devc->limit_samples * devc->data_width_bytes))
324 devc->raw_sample_buf[devc->num_transfers] = byte;
325
326 devc->num_transfers++;
327 }
328 } else {
329 if (devc->delay_value > 0) {
330 /* There are pre-trigger samples, send those first. */
331 packet.type = SR_DF_LOGIC;
332 packet.payload = &logic;
333 logic.length = devc->delay_value * devc->data_width_bytes;
334 logic.unitsize = devc->data_width_bytes;
335 logic.data = devc->raw_sample_buf;
336 sr_session_send(cb_data, &packet);
337 }
338
339 /* Send the trigger. */
340 packet.type = SR_DF_TRIGGER;
341 sr_session_send(cb_data, &packet);
342
343 /* Send post-trigger samples. */
344 packet.type = SR_DF_LOGIC;
345 packet.payload = &logic;
346 logic.length = (devc->limit_samples - devc->delay_value) *
347 devc->data_width_bytes;
348 logic.unitsize = devc->data_width_bytes;
349 logic.data = devc->raw_sample_buf +
350 (devc->delay_value * devc->data_width_bytes);
351 sr_session_send(cb_data, &packet);
352
353 g_free(devc->raw_sample_buf);
354 devc->raw_sample_buf = NULL;
355
356 ipdbg_la_abort_acquisition(sdi);
357 }
358
359 return TRUE;
360}
361
362static int send_escaping(struct ipdbg_la_tcp *tcp, uint8_t *data_to_send,
363 uint32_t length)
364{
365 uint8_t escape = CMD_ESCAPE;
366
367 while (length--) {
368 uint8_t payload = *data_to_send++;
369
370 if (payload == CMD_RESET)
371 if (tcp_send(tcp, &escape, 1) != SR_OK)
372 sr_warn("Couldn't send escape");
373
374 if (payload == CMD_ESCAPE)
375 if (tcp_send(tcp, &escape, 1) != SR_OK)
376 sr_warn("Couldn't send escape");
377
378 if (tcp_send(tcp, &payload, 1) != SR_OK)
379 sr_warn("Couldn't send data");
380 }
381
382 return SR_OK;
383}
384
385SR_PRIV int ipdbg_la_send_delay(struct dev_context *devc,
386 struct ipdbg_la_tcp *tcp)
387{
388 devc->delay_value = (devc->limit_samples / 100.0) * devc->capture_ratio;
389
390 uint8_t buf;
391 buf = CMD_CFG_LA;
392 tcp_send(tcp, &buf, 1);
393 buf = CMD_LA_DELAY;
394 tcp_send(tcp, &buf, 1);
395
396 uint8_t delay_buf[4] = { devc->delay_value & 0x000000ff,
397 (devc->delay_value >> 8) & 0x000000ff,
398 (devc->delay_value >> 16) & 0x000000ff,
399 (devc->delay_value >> 24) & 0x000000ff
400 };
401
402 for (uint64_t i = 0; i < devc->addr_width_bytes; i++)
403 send_escaping(tcp, &(delay_buf[devc->addr_width_bytes - 1 - i]), 1);
404
405 return SR_OK;
406}
407
408SR_PRIV int ipdbg_la_send_trigger(struct dev_context *devc,
409 struct ipdbg_la_tcp *tcp)
410{
411 uint8_t buf;
412
413 /* Mask */
414 buf = CMD_CFG_TRIGGER;
415 tcp_send(tcp, &buf, 1);
416 buf = CMD_TRIG_MASKS;
417 tcp_send(tcp, &buf, 1);
418 buf = CMD_TRIG_MASK;
419 tcp_send(tcp, &buf, 1);
420
421 for (size_t i = 0; i < devc->data_width_bytes; i++)
422 send_escaping(tcp,
423 devc->trigger_mask + devc->data_width_bytes - 1 - i, 1);
424
425 /* Value */
426 buf = CMD_CFG_TRIGGER;
427 tcp_send(tcp, &buf, 1);
428 buf = CMD_TRIG_MASKS;
429 tcp_send(tcp, &buf, 1);
430 buf = CMD_TRIG_VALUE;
431 tcp_send(tcp, &buf, 1);
432
433 for (size_t i = 0; i < devc->data_width_bytes; i++)
434 send_escaping(tcp,
435 devc->trigger_value + devc->data_width_bytes - 1 - i, 1);
436
437 /* Mask_last */
438 buf = CMD_CFG_TRIGGER;
439 tcp_send(tcp, &buf, 1);
440 buf = CMD_TRIG_MASKS_LAST;
441 tcp_send(tcp, &buf, 1);
442 buf = CMD_TRIG_MASK_LAST;
443 tcp_send(tcp, &buf, 1);
444
445 for (size_t i = 0; i < devc->data_width_bytes; i++)
446 send_escaping(tcp,
447 devc->trigger_mask_last + devc->data_width_bytes - 1 - i, 1);
448
449 /* Value_last */
450 buf = CMD_CFG_TRIGGER;
451 tcp_send(tcp, &buf, 1);
452 buf = CMD_TRIG_MASKS_LAST;
453 tcp_send(tcp, &buf, 1);
454 buf = CMD_TRIG_VALUE_LAST;
455 tcp_send(tcp, &buf, 1);
456
457 for (size_t i = 0; i < devc->data_width_bytes; i++)
458 send_escaping(tcp,
459 devc->trigger_value_last + devc->data_width_bytes - 1 - i, 1);
460
461 /* Edge_mask */
462 buf = CMD_CFG_TRIGGER;
463 tcp_send(tcp, &buf, 1);
464 buf = CMD_TRIG_SELECT_EDGE_MASK;
465 tcp_send(tcp, &buf, 1);
466 buf = CMD_TRIG_SET_EDGE_MASK;
467 tcp_send(tcp, &buf, 1);
468
469 for (size_t i = 0; i < devc->data_width_bytes; i++)
470 send_escaping(tcp,
471 devc->trigger_edge_mask + devc->data_width_bytes - 1 - i, 1);
472
473 return SR_OK;
474}
475
476SR_PRIV void ipdbg_la_get_addrwidth_and_datawidth(
477 struct ipdbg_la_tcp *tcp, struct dev_context *devc)
478{
479 uint8_t buf[8];
480 uint8_t read_cmd = CMD_GET_BUS_WIDTHS;
481
482 if (tcp_send(tcp, &read_cmd, 1) != SR_OK)
483 sr_warn("Can't send read command");
484
485 if (tcp_receive_blocking(tcp, buf, 8) != 8)
486 sr_warn("Can't get address and data width from device");
487
488 devc->data_width = buf[0] & 0x000000FF;
489 devc->data_width |= (buf[1] << 8) & 0x0000FF00;
490 devc->data_width |= (buf[2] << 16) & 0x00FF0000;
491 devc->data_width |= (buf[3] << 24) & 0xFF000000;
492
493 devc->addr_width = buf[4] & 0x000000FF;
494 devc->addr_width |= (buf[5] << 8) & 0x0000FF00;
495 devc->addr_width |= (buf[6] << 16) & 0x00FF0000;
496 devc->addr_width |= (buf[7] << 24) & 0xFF000000;
497
498 const uint8_t host_word_size = 8;
499
500 devc->data_width_bytes =
501 (devc->data_width + host_word_size - 1) / host_word_size;
502 devc->addr_width_bytes =
503 (devc->addr_width + host_word_size - 1) / host_word_size;
504
505 devc->limit_samples_max = (0x01 << devc->addr_width);
506 devc->limit_samples = devc->limit_samples_max;
507
508 devc->trigger_mask = g_malloc0(devc->data_width_bytes);
509 devc->trigger_value = g_malloc0(devc->data_width_bytes);
510 devc->trigger_mask_last = g_malloc0(devc->data_width_bytes);
511 devc->trigger_value_last = g_malloc0(devc->data_width_bytes);
512 devc->trigger_edge_mask = g_malloc0(devc->data_width_bytes);
513}
514
515SR_PRIV struct dev_context *ipdbg_la_dev_new(void)
516{
517 struct dev_context *devc;
518
519 devc = g_malloc0(sizeof(struct dev_context));
520 devc->capture_ratio = 50;
521
522 return devc;
523}
524
525SR_PRIV int ipdbg_la_send_reset(struct ipdbg_la_tcp *tcp)
526{
527 uint8_t buf = CMD_RESET;
528 if (tcp_send(tcp, &buf, 1) != SR_OK)
529 sr_warn("Couldn't send reset");
530
531 return SR_OK;
532}
533
534SR_PRIV int ipdbg_la_request_id(struct ipdbg_la_tcp *tcp)
535{
536 uint8_t buf = CMD_GET_LA_ID;
537 if (tcp_send(tcp, &buf, 1) != SR_OK)
538 sr_warn("Couldn't send ID request");
539
540 char id[4];
541 if (tcp_receive_blocking(tcp, (uint8_t *)id, 4) != 4) {
542 sr_err("Couldn't read device ID");
543 return SR_ERR;
544 }
545
546 if (strncmp(id, "IDBG", 4)) {
547 sr_err("Invalid device ID: expected 'IDBG', got '%c%c%c%c'.",
548 id[0], id[1], id[2], id[3]);
549 return SR_ERR;
550 }
551
552 return SR_OK;
553}
554
555SR_PRIV void ipdbg_la_abort_acquisition(const struct sr_dev_inst *sdi)
556{
557 struct ipdbg_la_tcp *tcp = sdi->conn;
558
559 sr_session_source_remove(sdi->session, tcp->socket);
560
561 std_session_send_df_end(sdi);
562}
563
564SR_PRIV int ipdbg_la_send_start(struct ipdbg_la_tcp *tcp)
565{
566 uint8_t buf = CMD_START;
567
568 if (tcp_send(tcp, &buf, 1) != SR_OK)
569 sr_warn("Couldn't send start");
570
571 return SR_OK;
572}