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