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