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