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