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