]> sigrok.org Git - libsigrok.git/blame - src/hardware/teleinfo/protocol.c
Build: Set local include directories in Makefile.am
[libsigrok.git] / src / hardware / teleinfo / protocol.c
CommitLineData
8e796cb4
AJ
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Aurelien Jacobs <aurel@gnuage.org>
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 3 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
5542bee6
AJ
20#include <stdlib.h>
21#include <string.h>
22#include <glib.h>
8e796cb4
AJ
23#include "protocol.h"
24
5542bee6
AJ
25#define STX 0x02
26#define ETX 0x03
27#define EOT 0x04
28#define LF 0x0A
29#define CR 0x0D
30
31static gboolean teleinfo_control_check(char *label, char *data, char control)
32{
33 int sum = 0;
34 while (*label)
35 sum += *label++;
36 sum += ' ';
37 while (*data)
38 sum += *data++;
39 return ((sum & 0x3F) + ' ') == control;
40}
41
ba7dd8bb 42static gint teleinfo_channel_compare(gconstpointer a, gconstpointer b)
5542bee6 43{
ba7dd8bb 44 const struct sr_channel *ch = a;
5542bee6 45 const char *name = b;
ba7dd8bb 46 return strcmp(ch->name, name);
5542bee6
AJ
47}
48
ba7dd8bb 49static struct sr_channel *teleinfo_find_channel(struct sr_dev_inst *sdi,
5542bee6
AJ
50 const char *name)
51{
ba7dd8bb
UH
52 GSList *elem = g_slist_find_custom(sdi->channels, name,
53 teleinfo_channel_compare);
5542bee6
AJ
54 return elem ? elem->data : NULL;
55}
56
ba7dd8bb 57static void teleinfo_send_value(struct sr_dev_inst *sdi, const char *channel_name,
5542bee6
AJ
58 float value, int mq, int unit)
59{
162a48bf 60 struct dev_context *devc;
5542bee6 61 struct sr_datafeed_packet packet;
162a48bf 62 struct sr_datafeed_analog analog;
ba7dd8bb 63 struct sr_channel *ch;
162a48bf
UH
64
65 devc = sdi->priv;
ba7dd8bb 66 ch = teleinfo_find_channel(sdi, channel_name);
5542bee6 67
ba7dd8bb 68 if (!ch || !ch->enabled)
5542bee6
AJ
69 return;
70
162a48bf 71 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
ba7dd8bb 72 analog.channels = g_slist_append(analog.channels, ch);
5542bee6
AJ
73 analog.num_samples = 1;
74 analog.mq = mq;
75 analog.unit = unit;
76 analog.data = &value;
77
78 packet.type = SR_DF_ANALOG;
79 packet.payload = &analog;
80 sr_session_send(devc->session_cb_data, &packet);
ba7dd8bb 81 g_slist_free(analog.channels);
5542bee6
AJ
82}
83
f3f19d11
UH
84static void teleinfo_handle_measurement(struct sr_dev_inst *sdi,
85 const char *label, const char *data, char *optarif)
5542bee6
AJ
86{
87 struct dev_context *devc;
88 int v = atoi(data);
89
90 if (!sdi || !(devc = sdi->priv)) {
91 if (optarif && !strcmp(label, "OPTARIF"))
92 strcpy(optarif, data);
93 return;
94 }
95
96 if (!strcmp(label, "ADCO")) {
97 devc->num_samples++;
98 } else if (!strcmp(label, "BASE")) {
99 teleinfo_send_value(sdi, "BASE", v, SR_MQ_POWER, SR_UNIT_WATT_HOUR);
100 } else if (!strcmp(label, "HCHP")) {
101 teleinfo_send_value(sdi, "HP" , v, SR_MQ_POWER, SR_UNIT_WATT_HOUR);
102 } else if (!strcmp(label, "HCHC")) {
103 teleinfo_send_value(sdi, "HC" , v, SR_MQ_POWER, SR_UNIT_WATT_HOUR);
104 } else if (!strcmp(label, "EJPHN")) {
105 teleinfo_send_value(sdi, "HN" , v, SR_MQ_POWER, SR_UNIT_WATT_HOUR);
106 } else if (!strcmp(label, "EJPHPM")) {
107 teleinfo_send_value(sdi, "HPM" , v, SR_MQ_POWER, SR_UNIT_WATT_HOUR);
108 } else if (!strcmp(label, "BBRHPJB")) {
109 teleinfo_send_value(sdi, "HPJB", v, SR_MQ_POWER, SR_UNIT_WATT_HOUR);
110 } else if (!strcmp(label, "BBRHPJW")) {
111 teleinfo_send_value(sdi, "HPJW", v, SR_MQ_POWER, SR_UNIT_WATT_HOUR);
112 } else if (!strcmp(label, "BBRHPJR")) {
113 teleinfo_send_value(sdi, "HPJR", v, SR_MQ_POWER, SR_UNIT_WATT_HOUR);
114 } else if (!strcmp(label, "BBRHCJB")) {
115 teleinfo_send_value(sdi, "HCJB", v, SR_MQ_POWER, SR_UNIT_WATT_HOUR);
116 } else if (!strcmp(label, "BBRHCJW")) {
117 teleinfo_send_value(sdi, "HCJW", v, SR_MQ_POWER, SR_UNIT_WATT_HOUR);
118 } else if (!strcmp(label, "BBRHCJR")) {
119 teleinfo_send_value(sdi, "HCJR", v, SR_MQ_POWER, SR_UNIT_WATT_HOUR);
120 } else if (!strcmp(label, "IINST")) {
121 teleinfo_send_value(sdi, "IINST", v, SR_MQ_CURRENT, SR_UNIT_AMPERE);
122 } else if (!strcmp(label, "PAPP")) {
123 teleinfo_send_value(sdi, "PAPP", v, SR_MQ_POWER, SR_UNIT_VOLT_AMPERE);
124 }
125}
126
127static gboolean teleinfo_parse_group(struct sr_dev_inst *sdi,
128 const uint8_t *group, char *optarif)
129{
130 char label[9], data[13], control, cr;
dafafb0e 131 const char *str = (const char *)group;
5542bee6
AJ
132 if (sscanf(str, "\x0A%8s %13s %c%c", label, data, &control, &cr) != 4
133 || cr != CR)
134 return FALSE;
135 if (!teleinfo_control_check(label, data, control))
136 return FALSE;
f3f19d11 137 teleinfo_handle_measurement(sdi, label, data, optarif);
5542bee6
AJ
138 return TRUE;
139}
140
141static const uint8_t *teleinfo_parse_data(struct sr_dev_inst *sdi,
142 const uint8_t *buf, int len,
143 char *optarif)
144{
dafafb0e
UH
145 const uint8_t *group_start, *group_end;
146
147 group_start = memchr(buf, LF, len);
5542bee6
AJ
148 if (!group_start)
149 return NULL;
150
dafafb0e 151 group_end = memchr(group_start, CR, len - (group_start - buf));
5542bee6
AJ
152 if (!group_end)
153 return NULL;
154
155 teleinfo_parse_group(sdi, group_start, optarif);
156 return group_end + 1;
157}
158
159SR_PRIV int teleinfo_get_optarif(const uint8_t *buf)
160{
161 const uint8_t *ptr = buf;
162 char optarif[5] = { 0 };
163
164 while ((ptr = teleinfo_parse_data(NULL, ptr, 292-(ptr-buf), optarif)));
165 if (!strcmp(optarif, "BASE"))
166 return OPTARIF_BASE;
167 else if (!strcmp(optarif, "HC.."))
168 return OPTARIF_HC;
169 else if (!strcmp(optarif, "EJP."))
170 return OPTARIF_EJP;
171 else if (!strncmp(optarif, "BBR", 3))
172 return OPTARIF_BBR;
173 return OPTARIF_NONE;
174}
175
176SR_PRIV gboolean teleinfo_packet_valid(const uint8_t *buf)
177{
178 return !!teleinfo_get_optarif(buf);
179}
180
8e796cb4
AJ
181SR_PRIV int teleinfo_receive_data(int fd, int revents, void *cb_data)
182{
5542bee6 183 struct sr_dev_inst *sdi;
8e796cb4 184 struct dev_context *devc;
5542bee6
AJ
185 struct sr_serial_dev_inst *serial;
186 const uint8_t *ptr, *next_ptr, *end_ptr;
187 int len;
dafafb0e 188 int64_t time;
8e796cb4
AJ
189
190 (void)fd;
191
5542bee6 192 if (!(sdi = cb_data) || !(devc = sdi->priv) || revents != G_IO_IN)
8e796cb4 193 return TRUE;
5542bee6 194 serial = sdi->conn;
8e796cb4 195
5542bee6
AJ
196 /* Try to get as much data as the buffer can hold. */
197 len = TELEINFO_BUF_SIZE - devc->buf_len;
9333691a 198 len = serial_read_nonblocking(serial, devc->buf + devc->buf_len, len);
5542bee6
AJ
199 if (len < 1) {
200 sr_err("Serial port read error: %d.", len);
201 return FALSE;
202 }
203 devc->buf_len += len;
8e796cb4 204
5542bee6
AJ
205 /* Now look for packets in that data. */
206 ptr = devc->buf;
207 end_ptr = ptr + devc->buf_len;
208 while ((next_ptr = teleinfo_parse_data(sdi, ptr, end_ptr - ptr, NULL)))
209 ptr = next_ptr;
210
211 /* If we have any data left, move it to the beginning of our buffer. */
212 memmove(devc->buf, ptr, end_ptr - ptr);
213 devc->buf_len -= ptr - devc->buf;
214
215 /* If buffer is full and no valid packet was found, wipe buffer. */
216 if (devc->buf_len >= TELEINFO_BUF_SIZE) {
217 devc->buf_len = 0;
218 return FALSE;
219 }
220
221 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
222 sr_info("Requested number of samples reached.");
223 sdi->driver->dev_acquisition_stop(sdi, devc->session_cb_data);
224 return TRUE;
8e796cb4
AJ
225 }
226
5542bee6 227 if (devc->limit_msec) {
dafafb0e 228 time = (g_get_monotonic_time() - devc->start_time) / 1000;
5542bee6
AJ
229 if (time > (int64_t)devc->limit_msec) {
230 sr_info("Requested time limit reached.");
231 sdi->driver->dev_acquisition_stop(sdi, devc->session_cb_data);
232 return TRUE;
233 }
234 }
8e796cb4
AJ
235 return TRUE;
236}