]>
Commit | Line | Data |
---|---|---|
4433145f UH |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de> | |
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 2 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, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
6ec6c43b | 21 | #include <config.h> |
4433145f UH |
22 | #include "protocol.h" |
23 | ||
07ffa5b3 UH |
24 | #define NUM_CHANNELS 4 |
25 | ||
4a8bbed7 | 26 | struct center_info { |
07ffa5b3 | 27 | float temp[NUM_CHANNELS]; |
4a8bbed7 UH |
28 | gboolean rec, std, max, min, maxmin, t1t2, rel, hold, lowbat, celsius; |
29 | gboolean memfull, autooff; | |
30 | gboolean mode_std, mode_rel, mode_max, mode_min, mode_maxmin; | |
31 | }; | |
32 | ||
33 | static int center_send(struct sr_serial_dev_inst *serial, const char *cmd) | |
4433145f | 34 | { |
4a8bbed7 UH |
35 | int ret; |
36 | ||
eead2782 | 37 | if ((ret = serial_write_blocking(serial, cmd, strlen(cmd), 0)) < 0) { |
4a8bbed7 UH |
38 | sr_err("Error sending '%s' command: %d.", cmd, ret); |
39 | return SR_ERR; | |
40 | } | |
41 | ||
42 | return SR_OK; | |
43 | } | |
44 | ||
45 | SR_PRIV gboolean center_3xx_packet_valid(const uint8_t *buf) | |
46 | { | |
47 | return (buf[0] == 0x02 && buf[44] == 0x03); | |
48 | } | |
49 | ||
50 | static void log_packet(const uint8_t *buf, int idx) | |
51 | { | |
52 | int i; | |
53 | GString *s; | |
54 | ||
55 | s = g_string_sized_new(100); | |
56 | g_string_printf(s, "Packet: "); | |
57 | for (i = 0; i < center_devs[idx].packet_size; i++) | |
58 | g_string_append_printf(s, "%02x ", buf[i]); | |
59 | sr_spew("%s", s->str); | |
60 | g_string_free(s, TRUE); | |
61 | } | |
62 | ||
63 | static int packet_parse(const uint8_t *buf, int idx, struct center_info *info) | |
64 | { | |
65 | int i; | |
66 | uint16_t temp_u16; | |
67 | ||
68 | log_packet(buf, idx); | |
69 | ||
70 | /* Byte 0: Always 0x02. */ | |
71 | ||
72 | /* Byte 1: Various status bits. */ | |
73 | info->rec = (buf[1] & (1 << 0)) != 0; | |
74 | info->mode_std = (((buf[1] >> 1) & 0x3) == 0); | |
75 | info->mode_max = (((buf[1] >> 1) & 0x3) == 1); | |
76 | info->mode_min = (((buf[1] >> 1) & 0x3) == 2); | |
77 | info->mode_maxmin = (((buf[1] >> 1) & 0x3) == 3); | |
78 | /* TODO: Rel. Not available on all models. */ | |
79 | info->t1t2 = (buf[1] & (1 << 3)) != 0; | |
80 | info->rel = (buf[1] & (1 << 4)) != 0; | |
81 | info->hold = (buf[1] & (1 << 5)) != 0; | |
82 | info->lowbat = (buf[1] & (1 << 6)) != 0; | |
83 | info->celsius = (buf[1] & (1 << 7)) != 0; | |
84 | ||
85 | /* Byte 2: Further status bits. */ | |
86 | info->memfull = (buf[2] & (1 << 0)) != 0; | |
87 | info->autooff = (buf[2] & (1 << 7)) != 0; | |
88 | ||
89 | /* Byte 7+8/9+10/11+12/13+14: channel T1/T2/T3/T4 temperature. */ | |
07ffa5b3 | 90 | for (i = 0; i < NUM_CHANNELS; i++) { |
4a8bbed7 UH |
91 | temp_u16 = buf[8 + (i * 2)]; |
92 | temp_u16 |= ((uint16_t)buf[7 + (i * 2)] << 8); | |
93 | info->temp[i] = (float)temp_u16; | |
94 | } | |
95 | ||
96 | /* Byte 43: Specifies whether we need to divide the value(s) by 10. */ | |
07ffa5b3 | 97 | for (i = 0; i < NUM_CHANNELS; i++) { |
4a8bbed7 UH |
98 | /* Bit = 0: Divide by 10. Bit = 1: Don't divide by 10. */ |
99 | if ((buf[43] & (1 << i)) == 0) | |
100 | info->temp[i] /= 10; | |
101 | } | |
102 | ||
103 | /* Bytes 39-42: Overflow/overlimit bits, depending on mode. */ | |
07ffa5b3 | 104 | for (i = 0; i < NUM_CHANNELS; i++) { |
4a8bbed7 UH |
105 | if (info->mode_std && ((buf[39] & (1 << i)) != 0)) |
106 | info->temp[i] = INFINITY; | |
107 | /* TODO: Rel. Not available on all models. */ | |
108 | // if (info->mode_rel && ((buf[40] & (1 << i)) != 0)) | |
109 | // info->temp[i] = INFINITY; | |
110 | if (info->mode_max && ((buf[41] & (1 << i)) != 0)) | |
111 | info->temp[i] = INFINITY; | |
112 | if (info->mode_min && ((buf[42] & (1 << i)) != 0)) | |
113 | info->temp[i] = INFINITY; | |
114 | /* TODO: Minmax? */ | |
115 | } | |
116 | ||
117 | /* Byte 44: Always 0x03. */ | |
118 | ||
119 | return SR_OK; | |
120 | } | |
121 | ||
122 | static int handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi, int idx) | |
123 | { | |
124 | struct sr_datafeed_packet packet; | |
125 | struct sr_datafeed_analog analog; | |
126 | struct dev_context *devc; | |
127 | struct center_info info; | |
128 | GSList *l; | |
129 | int i, ret; | |
130 | ||
131 | devc = sdi->priv; | |
4433145f | 132 | |
4a8bbed7 UH |
133 | memset(&analog, 0, sizeof(struct sr_datafeed_analog)); |
134 | memset(&info, 0, sizeof(struct center_info)); | |
135 | ||
136 | ret = packet_parse(buf, idx, &info); | |
137 | if (ret < 0) { | |
138 | sr_err("Failed to parse packet."); | |
139 | return SR_ERR; | |
140 | } | |
141 | ||
ba7dd8bb | 142 | /* Common values for all 4 channels. */ |
4a8bbed7 UH |
143 | packet.type = SR_DF_ANALOG; |
144 | packet.payload = &analog; | |
145 | analog.mq = SR_MQ_TEMPERATURE; | |
146 | analog.unit = (info.celsius) ? SR_UNIT_CELSIUS : SR_UNIT_FAHRENHEIT; | |
147 | analog.num_samples = 1; | |
148 | ||
149 | /* Send the values for T1 - T4. */ | |
07ffa5b3 | 150 | for (i = 0; i < NUM_CHANNELS; i++) { |
4a8bbed7 | 151 | l = NULL; |
ba7dd8bb UH |
152 | l = g_slist_append(l, g_slist_nth_data(sdi->channels, i)); |
153 | analog.channels = l; | |
4a8bbed7 UH |
154 | analog.data = &(info.temp[i]); |
155 | sr_session_send(devc->cb_data, &packet); | |
156 | g_slist_free(l); | |
157 | } | |
158 | ||
159 | devc->num_samples++; | |
160 | ||
161 | return SR_OK; | |
162 | } | |
163 | ||
164 | /* Return TRUE if a full packet was parsed, FALSE otherwise. */ | |
165 | static gboolean handle_new_data(struct sr_dev_inst *sdi, int idx) | |
166 | { | |
167 | struct dev_context *devc; | |
168 | struct sr_serial_dev_inst *serial; | |
169 | int len, i, offset = 0, ret = FALSE; | |
170 | ||
171 | devc = sdi->priv; | |
172 | serial = sdi->conn; | |
173 | ||
174 | /* Try to get as much data as the buffer can hold. */ | |
175 | len = SERIAL_BUFSIZE - devc->buflen; | |
3582ce8a | 176 | len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len); |
4a8bbed7 UH |
177 | if (len < 1) { |
178 | sr_err("Serial port read error: %d.", len); | |
179 | return FALSE; | |
180 | } | |
181 | ||
182 | devc->buflen += len; | |
183 | ||
184 | /* Now look for packets in that data. */ | |
185 | while ((devc->buflen - offset) >= center_devs[idx].packet_size) { | |
186 | if (center_devs[idx].packet_valid(devc->buf + offset)) { | |
187 | handle_packet(devc->buf + offset, sdi, idx); | |
188 | offset += center_devs[idx].packet_size; | |
189 | ret = TRUE; | |
190 | } else { | |
191 | offset++; | |
192 | } | |
193 | } | |
194 | ||
195 | /* If we have any data left, move it to the beginning of our buffer. */ | |
196 | for (i = 0; i < devc->buflen - offset; i++) | |
197 | devc->buf[i] = devc->buf[offset + i]; | |
198 | devc->buflen -= offset; | |
199 | ||
200 | return ret; | |
201 | } | |
202 | ||
203 | static int receive_data(int fd, int revents, int idx, void *cb_data) | |
204 | { | |
205 | struct sr_dev_inst *sdi; | |
4433145f | 206 | struct dev_context *devc; |
4a8bbed7 UH |
207 | int64_t t; |
208 | static gboolean request_new_packet = TRUE; | |
209 | struct sr_serial_dev_inst *serial; | |
210 | ||
211 | (void)fd; | |
4433145f UH |
212 | |
213 | if (!(sdi = cb_data)) | |
214 | return TRUE; | |
215 | ||
216 | if (!(devc = sdi->priv)) | |
217 | return TRUE; | |
218 | ||
4a8bbed7 UH |
219 | serial = sdi->conn; |
220 | ||
4433145f | 221 | if (revents == G_IO_IN) { |
4a8bbed7 UH |
222 | /* New data arrived. */ |
223 | request_new_packet = handle_new_data(sdi, idx); | |
224 | } else { | |
225 | /* | |
226 | * Timeout. Send "A" to request a packet, but then don't send | |
227 | * further "A" commands until we received a full packet first. | |
228 | */ | |
229 | if (request_new_packet) { | |
230 | center_send(serial, "A"); | |
231 | request_new_packet = FALSE; | |
232 | } | |
233 | } | |
234 | ||
235 | if (devc->limit_samples && devc->num_samples >= devc->limit_samples) { | |
236 | sr_info("Requested number of samples reached."); | |
237 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
238 | return TRUE; | |
239 | } | |
240 | ||
241 | if (devc->limit_msec) { | |
242 | t = (g_get_monotonic_time() - devc->starttime) / 1000; | |
243 | if (t > (int64_t)devc->limit_msec) { | |
244 | sr_info("Requested time limit reached."); | |
245 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
246 | return TRUE; | |
247 | } | |
4433145f UH |
248 | } |
249 | ||
250 | return TRUE; | |
251 | } | |
4a8bbed7 UH |
252 | |
253 | #define RECEIVE_DATA(ID_UPPER) \ | |
254 | SR_PRIV int receive_data_##ID_UPPER(int fd, int revents, void *cb_data) { \ | |
255 | return receive_data(fd, revents, ID_UPPER, cb_data); } | |
256 | ||
257 | /* Driver-specific receive_data() wrappers */ | |
258 | RECEIVE_DATA(CENTER_309) | |
259 | RECEIVE_DATA(VOLTCRAFT_K204) |