]> sigrok.org Git - libsigrok.git/blame - src/hardware/beaglelogic/protocol.c
Build: Set local include directories in Makefile.am
[libsigrok.git] / src / hardware / beaglelogic / protocol.c
CommitLineData
bb993797
KA
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Kumar Abhishek <abhishek@theembeddedkitchen.net>
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
ad9dbc1c
KA
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
515ab088 23#include "protocol.h"
bb993797 24
ad9dbc1c
KA
25/* Define data packet size independent of packet (bufunitsize bytes) size
26 * from the BeagleLogic kernel module */
27#define PACKET_SIZE (512 * 1024)
28
29/* This implementation is zero copy from the libsigrok side.
30 * It does not copy any data, just passes a pointer from the mmap'ed
31 * kernel buffers appropriately. It is up to the application which is
32 * using libsigrok to decide how to deal with the data.
33 */
bb993797
KA
34SR_PRIV int beaglelogic_receive_data(int fd, int revents, void *cb_data)
35{
36 const struct sr_dev_inst *sdi;
37 struct dev_context *devc;
ad9dbc1c
KA
38 struct sr_datafeed_packet packet;
39 struct sr_datafeed_logic logic;
bb993797 40
ad9dbc1c 41 int trigger_offset;
e743a47d 42 int pre_trigger_samples;
ad9dbc1c
KA
43 uint32_t packetsize;
44 uint64_t bytes_remaining;
bb993797 45
ad9dbc1c 46 if (!(sdi = cb_data) || !(devc = sdi->priv))
bb993797
KA
47 return TRUE;
48
ad9dbc1c
KA
49 packetsize = PACKET_SIZE;
50 logic.unitsize = SAMPLEUNIT_TO_BYTES(devc->sampleunit);
bb993797
KA
51
52 if (revents == G_IO_IN) {
ad9dbc1c
KA
53 sr_info("In callback G_IO_IN, offset=%d", devc->offset);
54
55 bytes_remaining = (devc->limit_samples * logic.unitsize) -
56 devc->bytes_read;
57
58 /* Configure data packet */
59 packet.type = SR_DF_LOGIC;
60 packet.payload = &logic;
61 logic.data = devc->sample_buf + devc->offset;
62 logic.length = MIN(packetsize, bytes_remaining);
63
64 if (devc->trigger_fired) {
65 /* Send the incoming transfer to the session bus. */
66 sr_session_send(devc->cb_data, &packet);
67 } else {
68 /* Check for trigger */
69 trigger_offset = soft_trigger_logic_check(devc->stl,
e743a47d 70 logic.data, packetsize, &pre_trigger_samples);
ad9dbc1c 71 if (trigger_offset > -1) {
e743a47d 72 devc->bytes_read += pre_trigger_samples * logic.unitsize;
ad9dbc1c
KA
73 trigger_offset *= logic.unitsize;
74 logic.length = MIN(packetsize - trigger_offset,
75 bytes_remaining);
76 logic.data += trigger_offset;
77
78 sr_session_send(devc->cb_data, &packet);
79
80 devc->trigger_fired = TRUE;
81 }
82 }
83
84 /* Move the read pointer forward */
85 lseek(fd, packetsize, SEEK_CUR);
86
87 /* Update byte count and offset (roll over if needed) */
88 devc->bytes_read += logic.length;
89 if ((devc->offset += packetsize) >= devc->buffersize) {
90 /* One shot capture, we abort and settle with less than
91 * the required number of samples */
92 if (devc->triggerflags)
93 devc->offset = 0;
94 else
95 packetsize = 0;
96 }
97 }
98
99 /* EOF Received or we have reached the limit */
100 if (devc->bytes_read >= devc->limit_samples * logic.unitsize ||
101 packetsize == 0) {
102 /* Send EOA Packet, stop polling */
103 packet.type = SR_DF_END;
104 packet.payload = NULL;
105 sr_session_send(devc->cb_data, &packet);
106
107 sr_session_source_remove_pollfd(sdi->session, &devc->pollfd);
bb993797
KA
108 }
109
110 return TRUE;
111}