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