]> sigrok.org Git - libsigrok.git/blob - src/hardware/mic-985xx/protocol.c
49f679802a31a2cfdf34da426ee8f163c8e24b8a
[libsigrok.git] / src / hardware / mic-985xx / 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 "protocol.h"
22
23 static int mic_send(struct sr_serial_dev_inst *serial, const char *cmd)
24 {
25         int ret;
26
27         if ((ret = serial_write_blocking(serial, cmd, strlen(cmd), 0)) < 0) {
28                 sr_err("Error sending '%s' command: %d.", cmd, ret);
29                 return SR_ERR;
30         }
31
32         return SR_OK;
33 }
34
35 SR_PRIV int mic_cmd_get_device_info(struct sr_serial_dev_inst *serial)
36 {
37         return mic_send(serial, "I\r");
38 }
39
40 static int mic_cmd_set_realtime_mode(struct sr_serial_dev_inst *serial)
41 {
42         return mic_send(serial, "S 1 M 2 32 3\r");
43 }
44
45 SR_PRIV gboolean packet_valid_temp(const uint8_t *buf)
46 {
47         if (buf[0] != 'v' || buf[1] != ' ' || buf[5] != '\r')
48                 return FALSE;
49
50         if (!isdigit(buf[2]) || !isdigit(buf[3]) || !isdigit(buf[4]))
51                 return FALSE;
52
53         return TRUE;
54 }
55
56 SR_PRIV gboolean packet_valid_temp_hum(const uint8_t *buf)
57 {
58         if (buf[0] != 'v' || buf[1] != ' ' || buf[5] != ' ' || buf[9] != '\r')
59                 return FALSE;
60
61         if (!isdigit(buf[2]) || !isdigit(buf[3]) || !isdigit(buf[4]))
62                 return FALSE;
63
64         if (!isdigit(buf[6]) || !isdigit(buf[7]) || !isdigit(buf[8]))
65                 return FALSE;
66
67         return TRUE;
68 }
69
70 static int packet_parse(const char *buf, int idx, float *temp, float *humidity)
71 {
72         char tmp[4];
73
74         /* Packet format MIC98581: "v ttt\r". */
75         /* Packet format MIC98583: "v ttt hhh\r". */
76
77         /* TODO: Sanity check on buf. For now we assume well-formed ASCII. */
78
79         tmp[3] = '\0';
80
81         strncpy((char *)&tmp, &buf[2], 3);
82         *temp = g_ascii_strtoull((const char *)&tmp, NULL, 10) / 10;
83
84         if (mic_devs[idx].has_humidity) {
85                 strncpy((char *)&tmp, &buf[6], 3);
86                 *humidity = g_ascii_strtoull((const char *)&tmp, NULL, 10) / 10;
87         }
88
89         return SR_OK;
90 }
91
92 static int handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi, int idx)
93 {
94         float temperature, humidity;
95         struct sr_datafeed_packet packet;
96         struct sr_datafeed_analog2 analog;
97         struct sr_analog_encoding encoding;
98         struct sr_analog_meaning meaning;
99         struct sr_analog_spec spec;
100         struct dev_context *devc;
101         GSList *l;
102         int ret;
103
104         (void)idx;
105
106         devc = sdi->priv;
107
108         ret = packet_parse((const char *)buf, idx, &temperature, &humidity);
109         if (ret < 0) {
110                 sr_err("Failed to parse packet.");
111                 return SR_ERR;
112         }
113
114         sr_analog_init(&analog, &encoding, &meaning, &spec, 3);
115
116         /* Common values for both channels. */
117         packet.type = SR_DF_ANALOG2;
118         packet.payload = &analog;
119         analog.num_samples = 1;
120
121         /* Temperature. */
122         l = g_slist_copy(sdi->channels);
123         l = g_slist_remove_link(l, g_slist_nth(l, 1));
124         meaning.channels = l;
125         meaning.mq = SR_MQ_TEMPERATURE;
126         meaning.unit = SR_UNIT_CELSIUS; /* TODO: Use C/F correctly. */
127         analog.data = &temperature;
128         sr_session_send(devc->cb_data, &packet);
129         g_slist_free(l);
130
131         /* Humidity. */
132         if (mic_devs[idx].has_humidity) {
133                 l = g_slist_copy(sdi->channels);
134                 l = g_slist_remove_link(l, g_slist_nth(l, 0));
135                 meaning.channels = l;
136                 meaning.mq = SR_MQ_RELATIVE_HUMIDITY;
137                 meaning.unit = SR_UNIT_PERCENTAGE;
138                 analog.data = &humidity;
139                 sr_session_send(devc->cb_data, &packet);
140                 g_slist_free(l);
141         }
142
143         devc->num_samples++;
144
145         return SR_OK;
146 }
147
148 static void handle_new_data(struct sr_dev_inst *sdi, int idx)
149 {
150         struct dev_context *devc;
151         struct sr_serial_dev_inst *serial;
152         int len, i, offset = 0;
153
154         devc = sdi->priv;
155         serial = sdi->conn;
156
157         /* Try to get as much data as the buffer can hold. */
158         len = SERIAL_BUFSIZE - devc->buflen;
159         len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
160         if (len < 1) {
161                 sr_err("Serial port read error: %d.", len);
162                 return;
163         }
164
165         devc->buflen += len;
166
167         /* Now look for packets in that data. */
168         while ((devc->buflen - offset) >= mic_devs[idx].packet_size) {
169                 if (mic_devs[idx].packet_valid(devc->buf + offset)) {
170                         handle_packet(devc->buf + offset, sdi, idx);
171                         offset += mic_devs[idx].packet_size;
172                 } else {
173                         offset++;
174                 }
175         }
176
177         /* If we have any data left, move it to the beginning of our buffer. */
178         for (i = 0; i < devc->buflen - offset; i++)
179                 devc->buf[i] = devc->buf[offset + i];
180         devc->buflen -= offset;
181 }
182
183 static int receive_data(int fd, int revents, int idx, void *cb_data)
184 {
185         struct sr_dev_inst *sdi;
186         struct dev_context *devc;
187         int64_t t;
188         static gboolean first_time = TRUE;
189         struct sr_serial_dev_inst *serial;
190
191         (void)fd;
192
193         if (!(sdi = cb_data))
194                 return TRUE;
195
196         if (!(devc = sdi->priv))
197                 return TRUE;
198
199         serial = sdi->conn;
200
201         if (revents == G_IO_IN) {
202                 /* New data arrived. */
203                 handle_new_data(sdi, idx);
204         } else {
205                 /* Timeout. */
206                 if (first_time) {
207                         mic_cmd_set_realtime_mode(serial);
208                         first_time = FALSE;
209                 }
210         }
211
212         if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
213                 sr_info("Requested number of samples reached.");
214                 sdi->driver->dev_acquisition_stop(sdi, cb_data);
215                 return TRUE;
216         }
217
218         if (devc->limit_msec) {
219                 t = (g_get_monotonic_time() - devc->starttime) / 1000;
220                 if (t > (int64_t)devc->limit_msec) {
221                         sr_info("Requested time limit reached.");
222                         sdi->driver->dev_acquisition_stop(sdi, cb_data);
223                         return TRUE;
224                 }
225         }
226
227         return TRUE;
228 }
229
230 #define RECEIVE_DATA(ID_UPPER) \
231 SR_PRIV int receive_data_##ID_UPPER(int fd, int revents, void *cb_data) { \
232         return receive_data(fd, revents, ID_UPPER, cb_data); }
233
234 /* Driver-specific receive_data() wrappers */
235 RECEIVE_DATA(MIC_98581)
236 RECEIVE_DATA(MIC_98583)