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