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