]>
Commit | Line | Data |
---|---|---|
5e7a8e57 AJ |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Aurelien Jacobs <aurel@gnuage.org> | |
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 | ||
81a9ab72 AJ |
20 | #include <string.h> |
21 | #include <math.h> | |
5e7a8e57 AJ |
22 | #include "protocol.h" |
23 | ||
81a9ab72 AJ |
24 | typedef enum { |
25 | LIVE_DATA = 0x00, | |
26 | LOG_METADATA = 0x11, | |
27 | LOG_DATA = 0x14, | |
28 | LOG_START = 0x18, | |
29 | LOG_END = 0x19, | |
30 | } PacketType; | |
31 | ||
32 | static gboolean appa_55ii_checksum(const uint8_t *buf) | |
33 | { | |
34 | int i, size = buf[3]+4, checksum = 0; | |
35 | for (i=0; i<size; i++) | |
36 | checksum += buf[i]; | |
37 | return buf[size] == (checksum & 0xFF); | |
38 | } | |
39 | ||
40 | SR_PRIV gboolean appa_55ii_packet_valid(const uint8_t *buf) | |
41 | { | |
42 | if (buf[0] == 0x55 && buf[1] == 0x55 && buf[3] <= 32 | |
43 | && appa_55ii_checksum(buf)) | |
44 | return TRUE; | |
45 | return FALSE; | |
46 | } | |
47 | ||
48 | static uint64_t appa_55ii_flags(const uint8_t *buf) | |
49 | { | |
50 | uint8_t disp_mode = buf[4+13]; | |
51 | uint64_t flags = 0; | |
52 | ||
53 | if ((disp_mode & 0xF0) == 0x20) flags |= SR_MQFLAG_HOLD; | |
54 | if ((disp_mode & 0x0C) == 0x04) flags |= SR_MQFLAG_MAX; | |
55 | if ((disp_mode & 0x0C) == 0x08) flags |= SR_MQFLAG_MIN; | |
56 | if ((disp_mode & 0x0C) == 0x0C) flags |= SR_MQFLAG_AVG; | |
57 | ||
58 | return flags; | |
59 | } | |
60 | ||
61 | static float appa_55ii_temp(const uint8_t *buf, int probe) | |
62 | { | |
63 | const uint8_t *ptr = buf + 4 + 14 + 3*probe; | |
64 | int16_t temp = RL16(ptr); | |
65 | uint8_t flags = ptr[2]; | |
66 | ||
67 | if (flags & 0x60) return INFINITY; | |
68 | else if (flags & 1) return (float)temp / 10; | |
69 | else return (float)temp; | |
70 | } | |
71 | ||
72 | static void appa_55ii_live_data(struct sr_dev_inst *sdi, const uint8_t *buf) | |
73 | { | |
74 | struct dev_context *devc = sdi->priv; | |
75 | struct sr_datafeed_packet packet; | |
76 | struct sr_datafeed_analog analog = { 0 }; | |
77 | float values[APPA_55II_NUM_PROBES], *val_ptr = values; | |
78 | int i; | |
79 | ||
80 | if (devc->data_source != DATA_SOURCE_LIVE) | |
81 | return; | |
82 | ||
83 | analog.num_samples = 1; | |
84 | analog.mq = SR_MQ_TEMPERATURE; | |
85 | analog.unit = SR_UNIT_CELSIUS; | |
86 | analog.mqflags = appa_55ii_flags(buf); | |
87 | analog.data = values; | |
88 | ||
89 | for (i=0; i<APPA_55II_NUM_PROBES; i++) { | |
90 | struct sr_probe *probe = g_slist_nth_data(sdi->probes, i); | |
91 | if (!probe->enabled) | |
92 | continue; | |
93 | analog.probes = g_slist_append(analog.probes, probe); | |
94 | *val_ptr++ = appa_55ii_temp(buf, i); | |
95 | } | |
96 | ||
97 | packet.type = SR_DF_ANALOG; | |
98 | packet.payload = &analog; | |
99 | sr_session_send(devc->session_cb_data, &packet); | |
100 | g_slist_free(analog.probes); | |
101 | ||
102 | devc->num_samples++; | |
103 | } | |
104 | ||
105 | static void appa_55ii_log_metadata(struct sr_dev_inst *sdi, const uint8_t *buf) | |
106 | { | |
107 | struct dev_context *devc = sdi->priv; | |
108 | devc->num_log_records = (buf[5] << 8) + buf[4]; | |
109 | } | |
110 | ||
111 | static void appa_55ii_log_data_parse(struct sr_dev_inst *sdi) | |
112 | { | |
113 | struct dev_context *devc = sdi->priv; | |
114 | int offset = 0; | |
115 | ||
116 | while (devc->log_buf_len >= 20 && devc->num_log_records > 0) { | |
117 | const uint8_t *buf = devc->log_buf + offset; | |
118 | struct sr_datafeed_packet packet; | |
119 | struct sr_datafeed_analog analog = { 0 }; | |
120 | float values[APPA_55II_NUM_PROBES], *val_ptr = values; | |
121 | int i; | |
122 | ||
123 | /* FIXME: timestamp should be sent in the packet */ | |
124 | sr_dbg("Timestamp: %02d:%02d:%02d", buf[2], buf[3], buf[4]); | |
125 | ||
126 | analog.num_samples = 1; | |
127 | analog.mq = SR_MQ_TEMPERATURE; | |
128 | analog.unit = SR_UNIT_CELSIUS; | |
129 | analog.data = values; | |
130 | ||
131 | for (i=0; i<APPA_55II_NUM_PROBES; i++) { | |
132 | int16_t temp = RL16(buf+12+2*i); | |
133 | struct sr_probe *probe = g_slist_nth_data(sdi->probes, i); | |
134 | if (!probe->enabled) | |
135 | continue; | |
136 | analog.probes = g_slist_append(analog.probes, probe); | |
137 | *val_ptr++ = temp == 0x7FFF ? INFINITY : (float)temp / 10; | |
138 | } | |
139 | ||
140 | packet.type = SR_DF_ANALOG; | |
141 | packet.payload = &analog; | |
142 | sr_session_send(devc->session_cb_data, &packet); | |
143 | g_slist_free(analog.probes); | |
144 | ||
145 | devc->num_samples++; | |
146 | devc->log_buf_len -= 20; | |
147 | offset += 20; | |
148 | devc->num_log_records--; | |
149 | } | |
150 | ||
151 | memmove(devc->log_buf, devc->log_buf + offset, devc->log_buf_len); | |
152 | } | |
153 | ||
154 | static void appa_55ii_log_data(struct sr_dev_inst *sdi, const uint8_t *buf) | |
155 | { | |
156 | struct dev_context *devc = sdi->priv; | |
157 | const uint8_t *ptr = buf + 4;; | |
158 | unsigned int size = buf[3]; | |
159 | ||
160 | if (devc->data_source != DATA_SOURCE_MEMORY) | |
161 | return; | |
162 | ||
163 | while (size > 0) { | |
164 | int s = MIN(size, sizeof(devc->log_buf) - devc->log_buf_len); | |
165 | memcpy(devc->log_buf + devc->log_buf_len, ptr, s); | |
166 | devc->log_buf_len += s; | |
167 | size -= s; | |
168 | ptr += s; | |
169 | ||
170 | appa_55ii_log_data_parse(sdi); | |
171 | } | |
172 | } | |
173 | ||
174 | static void appa_55ii_log_end(struct sr_dev_inst *sdi) | |
175 | { | |
176 | struct dev_context *devc = sdi->priv; | |
177 | ||
178 | if (devc->data_source != DATA_SOURCE_MEMORY) | |
179 | return; | |
180 | ||
181 | sdi->driver->dev_acquisition_stop(sdi, devc->session_cb_data); | |
182 | } | |
183 | ||
184 | static const uint8_t *appa_55ii_parse_data(struct sr_dev_inst *sdi, | |
185 | const uint8_t *buf, int len) | |
186 | { | |
187 | if (len < 5) | |
188 | return NULL; /* need more data */ | |
189 | ||
190 | if (buf[0] != 0x55 || buf[1] != 0x55) | |
191 | return buf + 1; /* try to re-synchronize on a packet start */ | |
192 | ||
193 | if (len < 5+buf[3]) | |
194 | return NULL; /* need more data */ | |
195 | ||
196 | if (!appa_55ii_checksum(buf)) | |
197 | return buf + 4 + buf[3] + 1; /* skip broken packet */ | |
198 | ||
199 | switch ((PacketType) buf[2]) { | |
200 | case LIVE_DATA: appa_55ii_live_data(sdi, buf); break; | |
201 | case LOG_METADATA: appa_55ii_log_metadata(sdi, buf); break; | |
202 | case LOG_DATA: appa_55ii_log_data(sdi, buf); break; | |
203 | case LOG_START: break; | |
204 | case LOG_END: appa_55ii_log_end(sdi); break; | |
205 | } | |
206 | ||
207 | return buf + 4 + buf[3] + 1; | |
208 | } | |
209 | ||
5e7a8e57 AJ |
210 | SR_PRIV int appa_55ii_receive_data(int fd, int revents, void *cb_data) |
211 | { | |
81a9ab72 | 212 | struct sr_dev_inst *sdi; |
5e7a8e57 | 213 | struct dev_context *devc; |
81a9ab72 AJ |
214 | struct sr_serial_dev_inst *serial; |
215 | const uint8_t *ptr, *next_ptr, *end_ptr; | |
216 | int len; | |
5e7a8e57 AJ |
217 | |
218 | (void)fd; | |
219 | ||
81a9ab72 | 220 | if (!(sdi = cb_data) || !(devc = sdi->priv) || revents != G_IO_IN) |
5e7a8e57 | 221 | return TRUE; |
81a9ab72 AJ |
222 | serial = sdi->conn; |
223 | ||
224 | /* Try to get as much data as the buffer can hold. */ | |
225 | len = sizeof(devc->buf) - devc->buf_len; | |
226 | len = serial_read(serial, devc->buf + devc->buf_len, len); | |
227 | if (len < 1) { | |
228 | sr_err("Serial port read error: %d.", len); | |
229 | return FALSE; | |
230 | } | |
231 | devc->buf_len += len; | |
232 | ||
233 | /* Now look for packets in that data. */ | |
234 | ptr = devc->buf; | |
235 | end_ptr = ptr + devc->buf_len; | |
236 | while ((next_ptr = appa_55ii_parse_data(sdi, ptr, end_ptr - ptr))) | |
237 | ptr = next_ptr; | |
5e7a8e57 | 238 | |
81a9ab72 AJ |
239 | /* If we have any data left, move it to the beginning of our buffer. */ |
240 | memmove(devc->buf, ptr, end_ptr - ptr); | |
241 | devc->buf_len -= ptr - devc->buf; | |
242 | ||
243 | /* If buffer is full and no valid packet was found, wipe buffer. */ | |
244 | if (devc->buf_len >= sizeof(devc->buf)) { | |
245 | devc->buf_len = 0; | |
246 | return FALSE; | |
247 | } | |
248 | ||
249 | if (devc->limit_samples && devc->num_samples >= devc->limit_samples) { | |
250 | sr_info("Requested number of samples reached."); | |
251 | sdi->driver->dev_acquisition_stop(sdi, devc->session_cb_data); | |
5e7a8e57 | 252 | return TRUE; |
81a9ab72 | 253 | } |
5e7a8e57 | 254 | |
81a9ab72 AJ |
255 | if (devc->limit_msec) { |
256 | int64_t time = (g_get_monotonic_time() - devc->start_time) / 1000; | |
257 | if (time > (int64_t)devc->limit_msec) { | |
258 | sr_info("Requested time limit reached."); | |
259 | sdi->driver->dev_acquisition_stop(sdi, devc->session_cb_data); | |
260 | return TRUE; | |
261 | } | |
5e7a8e57 AJ |
262 | } |
263 | ||
264 | return TRUE; | |
265 | } |