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