2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 static int mic_send(struct sr_serial_dev_inst *serial, const char *cmd)
27 if ((ret = serial_write_blocking(serial, cmd, strlen(cmd),
28 serial_timeout(serial, strlen(cmd)))) < 0) {
29 sr_err("Error sending '%s' command: %d.", cmd, ret);
36 SR_PRIV int mic_cmd_get_device_info(struct sr_serial_dev_inst *serial)
38 return mic_send(serial, "I\r");
41 static int mic_cmd_set_realtime_mode(struct sr_serial_dev_inst *serial)
43 return mic_send(serial, "S 1 M 2 32 3\r");
46 SR_PRIV gboolean packet_valid_temp(const uint8_t *buf)
48 if (buf[0] != 'v' || buf[1] != ' ' || buf[5] != '\r')
51 if (!isdigit(buf[2]) || !isdigit(buf[3]) || !isdigit(buf[4]))
57 SR_PRIV gboolean packet_valid_temp_hum(const uint8_t *buf)
59 if (buf[0] != 'v' || buf[1] != ' ' || buf[5] != ' ' || buf[9] != '\r')
62 if (!isdigit(buf[2]) || !isdigit(buf[3]) || !isdigit(buf[4]))
65 if (!isdigit(buf[6]) || !isdigit(buf[7]) || !isdigit(buf[8]))
71 static int packet_parse(const char *buf, int idx, float *temp, float *humidity)
75 /* Packet format MIC98581: "v ttt\r". */
76 /* Packet format MIC98583: "v ttt hhh\r". */
78 /* TODO: Sanity check on buf. For now we assume well-formed ASCII. */
82 strncpy((char *)&tmp, &buf[2], 3);
83 *temp = g_ascii_strtoull((const char *)&tmp, NULL, 10) / 10;
85 if (mic_devs[idx].has_humidity) {
86 strncpy((char *)&tmp, &buf[6], 3);
87 *humidity = g_ascii_strtoull((const char *)&tmp, NULL, 10) / 10;
93 static int handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi, int idx)
95 float temperature, humidity;
96 struct sr_datafeed_packet packet;
97 struct sr_datafeed_analog analog;
98 struct sr_analog_encoding encoding;
99 struct sr_analog_meaning meaning;
100 struct sr_analog_spec spec;
101 struct dev_context *devc;
109 ret = packet_parse((const char *)buf, idx, &temperature, &humidity);
111 sr_err("Failed to parse packet.");
115 sr_analog_init(&analog, &encoding, &meaning, &spec, 1);
117 /* Common values for both channels. */
118 packet.type = SR_DF_ANALOG;
119 packet.payload = &analog;
120 analog.num_samples = 1;
123 l = g_slist_copy(sdi->channels);
124 l = g_slist_remove_link(l, g_slist_nth(l, 1));
125 meaning.channels = l;
126 meaning.mq = SR_MQ_TEMPERATURE;
127 meaning.unit = SR_UNIT_CELSIUS; /* TODO: Use C/F correctly. */
128 analog.data = &temperature;
129 sr_session_send(sdi, &packet);
133 if (mic_devs[idx].has_humidity) {
134 l = g_slist_copy(sdi->channels);
135 l = g_slist_remove_link(l, g_slist_nth(l, 0));
136 meaning.channels = l;
137 meaning.mq = SR_MQ_RELATIVE_HUMIDITY;
138 meaning.unit = SR_UNIT_PERCENTAGE;
139 analog.data = &humidity;
140 sr_session_send(sdi, &packet);
144 sr_sw_limits_update_samples_read(&devc->limits, 1);
149 static void handle_new_data(struct sr_dev_inst *sdi, int idx)
151 struct dev_context *devc;
152 struct sr_serial_dev_inst *serial;
158 /* Try to get as much data as the buffer can hold. */
159 len = SERIAL_BUFSIZE - devc->buflen;
160 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
162 sr_err("Serial port read error: %d.", len);
168 /* Now look for packets in that data. */
170 while ((devc->buflen - offset) >= mic_devs[idx].packet_size) {
171 if (mic_devs[idx].packet_valid(devc->buf + offset)) {
172 handle_packet(devc->buf + offset, sdi, idx);
173 offset += mic_devs[idx].packet_size;
179 /* If we have any data left, move it to the beginning of our buffer. */
180 if (offset < devc->buflen)
181 memmove(devc->buf, devc->buf + offset, devc->buflen - offset);
182 devc->buflen -= offset;
185 static int receive_data(int fd, int revents, int idx, void *cb_data)
187 struct sr_dev_inst *sdi;
188 struct dev_context *devc;
189 static gboolean first_time = TRUE;
190 struct sr_serial_dev_inst *serial;
194 if (!(sdi = cb_data))
197 if (!(devc = sdi->priv))
202 if (revents == G_IO_IN) {
203 /* New data arrived. */
204 handle_new_data(sdi, idx);
208 mic_cmd_set_realtime_mode(serial);
213 if (sr_sw_limits_check(&devc->limits))
214 sr_dev_acquisition_stop(sdi);
219 #define RECEIVE_DATA(ID_UPPER) \
220 SR_PRIV int receive_data_##ID_UPPER(int fd, int revents, void *cb_data) { \
221 return receive_data(fd, revents, ID_UPPER, cb_data); }
223 /* Driver-specific receive_data() wrappers */
224 RECEIVE_DATA(MIC_98581)
225 RECEIVE_DATA(MIC_98583)