]> sigrok.org Git - libsigrok.git/blame - src/hardware/mic-985xx/protocol.c
mic-985xx: Convert to use SR_DF_ANALOG2.
[libsigrok.git] / src / hardware / mic-985xx / protocol.c
CommitLineData
7ec5b549
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
21#include "protocol.h"
22
0cd8e231 23static int mic_send(struct sr_serial_dev_inst *serial, const char *cmd)
7ec5b549 24{
0cd8e231
UH
25 int ret;
26
eead2782 27 if ((ret = serial_write_blocking(serial, cmd, strlen(cmd), 0)) < 0) {
0cd8e231
UH
28 sr_err("Error sending '%s' command: %d.", cmd, ret);
29 return SR_ERR;
30 }
31
32 return SR_OK;
33}
34
35SR_PRIV int mic_cmd_get_device_info(struct sr_serial_dev_inst *serial)
36{
37 return mic_send(serial, "I\r");
38}
39
40static 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
6f3e5335
UH
45SR_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
56SR_PRIV gboolean packet_valid_temp_hum(const uint8_t *buf)
0cd8e231
UH
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
6f3e5335 70static int packet_parse(const char *buf, int idx, float *temp, float *humidity)
0cd8e231
UH
71{
72 char tmp[4];
73
6f3e5335
UH
74 /* Packet format MIC98581: "v ttt\r". */
75 /* Packet format MIC98583: "v ttt hhh\r". */
0cd8e231
UH
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
6f3e5335
UH
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 }
0cd8e231
UH
88
89 return SR_OK;
90}
91
92static 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;
676877f6
UH
96 struct sr_datafeed_analog2 analog;
97 struct sr_analog_encoding encoding;
98 struct sr_analog_meaning meaning;
99 struct sr_analog_spec spec;
0cd8e231
UH
100 struct dev_context *devc;
101 GSList *l;
102 int ret;
7ec5b549 103
0cd8e231
UH
104 (void)idx;
105
106 devc = sdi->priv;
107
6f3e5335 108 ret = packet_parse((const char *)buf, idx, &temperature, &humidity);
0cd8e231
UH
109 if (ret < 0) {
110 sr_err("Failed to parse packet.");
111 return SR_ERR;
112 }
113
ff17e6ba 114 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
676877f6
UH
115 memset(&encoding, 0, sizeof(struct sr_analog_encoding));
116 memset(&meaning, 0, sizeof(struct sr_analog_meaning));
117 memset(&spec, 0, sizeof(struct sr_analog_spec));
ff17e6ba 118
ba7dd8bb 119 /* Common values for both channels. */
676877f6 120 packet.type = SR_DF_ANALOG2;
0cd8e231 121 packet.payload = &analog;
676877f6
UH
122 analog.encoding = &encoding;
123 analog.meaning = &meaning;
124 analog.spec = &spec;
0cd8e231 125 analog.num_samples = 1;
676877f6
UH
126 encoding.unitsize = sizeof(float);
127 encoding.is_float = TRUE;
128#ifdef WORDS_BIGENDIAN
129 encoding.is_bigendian = TRUE;
130#else
131 encoding.is_bigendian = FALSE;
132#endif
133 encoding.digits = 3; /* Values are always 3-digit numbers. */
134 encoding.is_digits_decimal = TRUE;
135 encoding.scale.p = 1;
136 encoding.scale.q = 1;
137 encoding.offset.p = 0;
138 encoding.offset.q = 1;
139 spec.spec_digits = encoding.digits;
0cd8e231
UH
140
141 /* Temperature. */
ba7dd8bb 142 l = g_slist_copy(sdi->channels);
0cd8e231 143 l = g_slist_remove_link(l, g_slist_nth(l, 1));
676877f6
UH
144 meaning.channels = l;
145 meaning.mq = SR_MQ_TEMPERATURE;
146 meaning.unit = SR_UNIT_CELSIUS; /* TODO: Use C/F correctly. */
0cd8e231
UH
147 analog.data = &temperature;
148 sr_session_send(devc->cb_data, &packet);
149 g_slist_free(l);
150
151 /* Humidity. */
6f3e5335 152 if (mic_devs[idx].has_humidity) {
ba7dd8bb 153 l = g_slist_copy(sdi->channels);
6f3e5335 154 l = g_slist_remove_link(l, g_slist_nth(l, 0));
676877f6
UH
155 meaning.channels = l;
156 meaning.mq = SR_MQ_RELATIVE_HUMIDITY;
157 meaning.unit = SR_UNIT_PERCENTAGE;
6f3e5335
UH
158 analog.data = &humidity;
159 sr_session_send(devc->cb_data, &packet);
160 g_slist_free(l);
161 }
0cd8e231
UH
162
163 devc->num_samples++;
164
165 return SR_OK;
166}
167
168static void handle_new_data(struct sr_dev_inst *sdi, int idx)
169{
7ec5b549 170 struct dev_context *devc;
886bd5e0 171 struct sr_serial_dev_inst *serial;
0cd8e231
UH
172 int len, i, offset = 0;
173
174 devc = sdi->priv;
886bd5e0 175 serial = sdi->conn;
0cd8e231
UH
176
177 /* Try to get as much data as the buffer can hold. */
178 len = SERIAL_BUFSIZE - devc->buflen;
98842e53 179 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
0cd8e231
UH
180 if (len < 1) {
181 sr_err("Serial port read error: %d.", len);
182 return;
183 }
184
185 devc->buflen += len;
186
187 /* Now look for packets in that data. */
6f3e5335
UH
188 while ((devc->buflen - offset) >= mic_devs[idx].packet_size) {
189 if (mic_devs[idx].packet_valid(devc->buf + offset)) {
0cd8e231 190 handle_packet(devc->buf + offset, sdi, idx);
6f3e5335 191 offset += mic_devs[idx].packet_size;
0cd8e231
UH
192 } else {
193 offset++;
194 }
195 }
196
197 /* If we have any data left, move it to the beginning of our buffer. */
198 for (i = 0; i < devc->buflen - offset; i++)
199 devc->buf[i] = devc->buf[offset + i];
200 devc->buflen -= offset;
201}
202
203static int receive_data(int fd, int revents, int idx, void *cb_data)
204{
205 struct sr_dev_inst *sdi;
206 struct dev_context *devc;
207 int64_t t;
208 static gboolean first_time = TRUE;
886bd5e0 209 struct sr_serial_dev_inst *serial;
0cd8e231
UH
210
211 (void)fd;
7ec5b549
UH
212
213 if (!(sdi = cb_data))
214 return TRUE;
215
216 if (!(devc = sdi->priv))
217 return TRUE;
218
886bd5e0
UH
219 serial = sdi->conn;
220
7ec5b549 221 if (revents == G_IO_IN) {
0cd8e231
UH
222 /* New data arrived. */
223 handle_new_data(sdi, idx);
224 } else {
225 /* Timeout. */
226 if (first_time) {
886bd5e0 227 mic_cmd_set_realtime_mode(serial);
0cd8e231
UH
228 first_time = FALSE;
229 }
230 }
231
232 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
233 sr_info("Requested number of samples reached.");
234 sdi->driver->dev_acquisition_stop(sdi, cb_data);
235 return TRUE;
236 }
237
238 if (devc->limit_msec) {
239 t = (g_get_monotonic_time() - devc->starttime) / 1000;
240 if (t > (int64_t)devc->limit_msec) {
241 sr_info("Requested time limit reached.");
242 sdi->driver->dev_acquisition_stop(sdi, cb_data);
243 return TRUE;
244 }
7ec5b549
UH
245 }
246
247 return TRUE;
248}
0cd8e231
UH
249
250#define RECEIVE_DATA(ID_UPPER) \
251SR_PRIV int receive_data_##ID_UPPER(int fd, int revents, void *cb_data) { \
252 return receive_data(fd, revents, ID_UPPER, cb_data); }
253
254/* Driver-specific receive_data() wrappers */
6f3e5335 255RECEIVE_DATA(MIC_98581)
0cd8e231 256RECEIVE_DATA(MIC_98583)