1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012 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, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <config.h>
21 #include <string.h>
22 #include <glib.h>
23 #include <libsigrok/libsigrok.h>
24 #include "libsigrok-internal.h"
25 #include "protocol.h"
26 
27 /* States */
28 enum {
29 	SEND_INIT,
30 	GET_INIT_REPLY,
31 	SEND_PACKET_REQUEST,
32 	GET_PACKET,
33 };
34 
parse_packet(const uint8_t * buf,float * floatval,struct sr_datafeed_analog * analog)35 static void parse_packet(const uint8_t *buf, float *floatval,
36 			 struct sr_datafeed_analog *analog)
37 {
38 	gboolean is_a, is_fast;
39 	uint16_t intval;
40 	uint8_t level = 0, level_bits;
41 
42 	/* Byte 0 [7:7]: 0 = A, 1 = C */
43 	is_a = ((buf[0] & (1 << 7)) == 0);
44 
45 	/* Byte 0 [6:6]: Unknown/unused? */
46 
47 	/* Byte 0 [5:4]: Level (00 = 40, 01 = 60, 10 = 80, 11 = 100) */
48 	level_bits = (buf[0] >> 4) & 0x03;
49 	if (level_bits == 0)
50 		level = 40;
51 	else if (level_bits == 1)
52 		level = 60;
53 	else if (level_bits == 2)
54 		level = 80;
55 	else if (level_bits == 3)
56 		level = 100;
57 
58 	/* Byte 0 [3:3]: 0 = fast, 1 = slow */
59 	is_fast = ((buf[0] & (1 << 3)) == 0);
60 
61 	/* Byte 0 [2:0]: value[10..8] */
62 	/* Byte 1 [7:0]: value[7..0] */
63 	intval = (buf[0] & 0x7) << 8;
64 	intval |= buf[1];
65 
66 	*floatval = (float)intval;
67 
68 	/* The value on the display always has one digit after the comma. */
69 	*floatval /= 10;
70 
71 	analog->meaning->mq = SR_MQ_SOUND_PRESSURE_LEVEL;
72 	analog->meaning->unit = SR_UNIT_DECIBEL_SPL;
73 
74 	if (is_a)
75 		analog->meaning->mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_A;
76 	else
77 		analog->meaning->mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_C;
78 
79 	if (is_fast)
80 		analog->meaning->mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_F;
81 	else
82 		analog->meaning->mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_S;
83 
84 	/* TODO: How to handle level? */
85 	(void)level;
86 }
87 
decode_packet(struct sr_dev_inst * sdi)88 static void decode_packet(struct sr_dev_inst *sdi)
89 {
90 	struct sr_datafeed_packet packet;
91 	struct sr_datafeed_analog analog;
92 	struct sr_analog_encoding encoding;
93 	struct sr_analog_meaning meaning;
94 	struct sr_analog_spec spec;
95 	struct dev_context *devc;
96 	float floatval;
97 
98 	devc = sdi->priv;
99 
100 	sr_analog_init(&analog, &encoding, &meaning, &spec, 1);
101 
102 	parse_packet(devc->buf, &floatval, &analog);
103 
104 	/* Send a sample packet with one analog value. */
105 	analog.meaning->channels = sdi->channels;
106 	analog.num_samples = 1;
107 	analog.data = &floatval;
108 	packet.type = SR_DF_ANALOG;
109 	packet.payload = &analog;
110 	sr_session_send(sdi, &packet);
111 
112 	sr_sw_limits_update_samples_read(&devc->limits, 1);
113 }
114 
tondaj_sl_814_receive_data(int fd,int revents,void * cb_data)115 SR_PRIV int tondaj_sl_814_receive_data(int fd, int revents, void *cb_data)
116 {
117 	struct sr_dev_inst *sdi;
118 	struct dev_context *devc;
119 	struct sr_serial_dev_inst *serial;
120 	uint8_t buf[3];
121 	int ret;
122 
123 	(void)fd;
124 	(void)revents;
125 
126 	sdi = cb_data;
127 	serial = sdi->conn;
128 	devc = sdi->priv;
129 
130 	/* TODO: Parts of this code need to be improved later. */
131 
132 	/* State machine. */
133 	if (devc->state == SEND_INIT) {
134 		/* On the first run, send the "init" command. */
135 		buf[0] = 0x10;
136 		buf[1] = 0x04;
137 		buf[2] = 0x0d;
138 		sr_spew("Sending init command: %02x %02x %02x.",
139 			buf[0], buf[1], buf[2]);
140 		if ((ret = serial_write_blocking(serial, buf, 3,
141 				serial_timeout(serial, 3))) < 0) {
142 			sr_err("Error sending init command: %d.", ret);
143 			return FALSE;
144 		}
145 		devc->state = GET_INIT_REPLY;
146 	} else if (devc->state == GET_INIT_REPLY) {
147 		/* If we just sent the "init" command, get its reply. */
148 		if ((ret = serial_read_blocking(serial, buf, 2, 0)) < 0) {
149 			sr_err("Error reading init reply: %d.", ret);
150 			return FALSE;
151 		}
152 		sr_spew("Received init reply: %02x %02x.", buf[0], buf[1]);
153 		/* Expected reply: 0x05 0x0d */
154 		if (buf[0] != 0x05 || buf[1] != 0x0d) {
155 			sr_err("Received incorrect init reply, retrying.");
156 			devc->state = SEND_INIT;
157 			return TRUE;
158 		}
159 		devc->state = SEND_PACKET_REQUEST;
160 	} else if (devc->state == SEND_PACKET_REQUEST) {
161 		/* Request a packet (send 0x30 ZZ 0x0d). */
162 		buf[0] = 0x30;
163 		buf[1] = 0x00; /* ZZ */
164 		buf[2] = 0x0d;
165 		sr_spew("Sending data request command: %02x %02x %02x.",
166 			buf[0], buf[1], buf[2]);
167 		if ((ret = serial_write_blocking(serial, buf, 3,
168 				serial_timeout(serial, 3))) < 0) {
169 			sr_err("Error sending request command: %d.", ret);
170 			return FALSE;
171 		}
172 		devc->buflen = 0;
173 		devc->state = GET_PACKET;
174 	} else if (devc->state == GET_PACKET) {
175 		/* Read a packet from the device. */
176 		ret = serial_read_nonblocking(serial, devc->buf + devc->buflen,
177 				4 - devc->buflen);
178 		if (ret < 0) {
179 			sr_err("Error reading packet: %d.", ret);
180 			return TRUE;
181 		}
182 
183 		devc->buflen += ret;
184 
185 		/* Didn't receive all 4 bytes, yet. */
186 		if (devc->buflen != 4)
187 			return TRUE;
188 
189 		sr_spew("Received packet: %02x %02x %02x %02x.", devc->buf[0],
190 			devc->buf[1], devc->buf[2], devc->buf[3]);
191 
192 		/* Expected reply: AA BB ZZ+1 0x0d */
193 		if (devc->buf[2] != 0x01 || devc->buf[3] != 0x0d) {
194 			sr_err("Received incorrect request reply, retrying.");
195 			devc->state = SEND_PACKET_REQUEST;
196 			return TRUE;
197 		}
198 
199 		decode_packet(sdi);
200 
201 		devc->state = SEND_PACKET_REQUEST;
202 	} else {
203 		sr_err("Invalid state: %d.", devc->state);
204 		return FALSE;
205 	}
206 
207 	if (sr_sw_limits_check(&devc->limits))
208 		sr_dev_acquisition_stop(sdi);
209 
210 	return TRUE;
211 }
212