]> sigrok.org Git - libsigrok.git/blob - hardware/teleinfo/api.c
Route sr_source_add for all serial devices through a serial_source_add wrapper.
[libsigrok.git] / hardware / teleinfo / api.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 <stdlib.h>
21 #include <glib.h>
22 #include "libsigrok.h"
23 #include "libsigrok-internal.h"
24 #include "protocol.h"
25
26 static const int32_t hwopts[] = {
27         SR_CONF_CONN,
28         SR_CONF_SERIALCOMM,
29 };
30
31 static const int32_t hwcaps[] = {
32         SR_CONF_ENERGYMETER,
33         SR_CONF_LIMIT_SAMPLES,
34         SR_CONF_LIMIT_MSEC,
35         SR_CONF_CONTINUOUS,
36 };
37
38 SR_PRIV struct sr_dev_driver teleinfo_driver_info;
39 static struct sr_dev_driver *di = &teleinfo_driver_info;
40
41 static int init(struct sr_context *sr_ctx)
42 {
43         return std_init(sr_ctx, di, LOG_PREFIX);
44 }
45
46 static GSList *scan(GSList *options)
47 {
48         struct drv_context *drvc;
49         struct dev_context *devc;
50         struct sr_serial_dev_inst *serial;
51         struct sr_dev_inst *sdi;
52         struct sr_probe *probe;
53         GSList *devices = NULL, *l;
54         const char *conn = NULL, *serialcomm = NULL;
55         uint8_t buf[292];
56         size_t len = sizeof(buf);
57
58         for (l = options; l; l = l->next) {
59                 struct sr_config *src = l->data;
60                 switch (src->key) {
61                 case SR_CONF_CONN:
62                         conn = g_variant_get_string(src->data, NULL);
63                         break;
64                 case SR_CONF_SERIALCOMM:
65                         serialcomm = g_variant_get_string(src->data, NULL);
66                         break;
67                 }
68         }
69         if (!conn)
70                 return NULL;
71         if (!serialcomm)
72                 serialcomm = "1200/7e1";
73
74         if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
75                 return NULL;
76         if (serial_open(serial, SERIAL_RDONLY | SERIAL_NONBLOCK) != SR_OK)
77                 return NULL;
78
79         sr_info("Probing serial port %s.", conn);
80
81         drvc = di->priv;
82         drvc->instances = NULL;
83         serial_flush(serial);
84
85         /* Let's get a bit of data and see if we can find a packet. */
86         if (serial_stream_detect(serial, buf, &len, len,
87                                  teleinfo_packet_valid, 3000, 1200) != SR_OK)
88                 goto scan_cleanup;
89
90         sr_info("Found device on port %s.", conn);
91
92         if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "EDF", "Teleinfo", "")))
93                 goto scan_cleanup;
94
95         if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
96                 sr_err("Device context malloc failed.");
97                 goto scan_cleanup;
98         }
99
100         devc->optarif = teleinfo_get_optarif(buf);
101
102         sdi->inst_type = SR_INST_SERIAL;
103         sdi->conn = serial;
104         sdi->priv = devc;
105         sdi->driver = di;
106
107         if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P")))
108                 goto scan_cleanup;
109         sdi->probes = g_slist_append(sdi->probes, probe);
110
111         if (devc->optarif == OPTARIF_BASE) {
112                 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "BASE")))
113                         goto scan_cleanup;
114                 sdi->probes = g_slist_append(sdi->probes, probe);
115         } else if (devc->optarif == OPTARIF_HC) {
116                 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "HP")))
117                         goto scan_cleanup;
118                 sdi->probes = g_slist_append(sdi->probes, probe);
119                 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "HC")))
120                         goto scan_cleanup;
121                 sdi->probes = g_slist_append(sdi->probes, probe);
122         } else if (devc->optarif == OPTARIF_EJP) {
123                 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "HN")))
124                         goto scan_cleanup;
125                 sdi->probes = g_slist_append(sdi->probes, probe);
126                 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "HPM")))
127                         goto scan_cleanup;
128                 sdi->probes = g_slist_append(sdi->probes, probe);
129         } else if (devc->optarif == OPTARIF_BBR) {
130                 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "HPJB")))
131                         goto scan_cleanup;
132                 sdi->probes = g_slist_append(sdi->probes, probe);
133                 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "HPJW")))
134                         goto scan_cleanup;
135                 sdi->probes = g_slist_append(sdi->probes, probe);
136                 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "HPJR")))
137                         goto scan_cleanup;
138                 sdi->probes = g_slist_append(sdi->probes, probe);
139                 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "HCJB")))
140                         goto scan_cleanup;
141                 sdi->probes = g_slist_append(sdi->probes, probe);
142                 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "HCJW")))
143                         goto scan_cleanup;
144                 sdi->probes = g_slist_append(sdi->probes, probe);
145                 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "HCJR")))
146                         goto scan_cleanup;
147                 sdi->probes = g_slist_append(sdi->probes, probe);
148         }
149
150         if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "IINST")))
151                 goto scan_cleanup;
152         sdi->probes = g_slist_append(sdi->probes, probe);
153
154         if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "PAPP")))
155                 goto scan_cleanup;
156         sdi->probes = g_slist_append(sdi->probes, probe);
157
158         drvc->instances = g_slist_append(drvc->instances, sdi);
159         devices = g_slist_append(devices, sdi);
160
161 scan_cleanup:
162         serial_close(serial);
163
164         return devices;
165 }
166
167 static GSList *dev_list(void)
168 {
169         return ((struct drv_context *)(di->priv))->instances;
170 }
171
172 static int dev_clear(void)
173 {
174         return std_dev_clear(di, NULL);
175 }
176
177 static int dev_open(struct sr_dev_inst *sdi)
178 {
179         struct sr_serial_dev_inst *serial = sdi->conn;
180
181         if (serial_open(serial, SERIAL_RDONLY | SERIAL_NONBLOCK) != SR_OK)
182                 return SR_ERR;
183
184         sdi->status = SR_ST_ACTIVE;
185
186         return SR_OK;
187 }
188
189 static int dev_close(struct sr_dev_inst *sdi)
190 {
191         struct sr_serial_dev_inst *serial = sdi->conn;
192
193         if (serial && serial->fd != -1) {
194                 serial_close(serial);
195                 sdi->status = SR_ST_INACTIVE;
196         }
197
198         return SR_OK;
199 }
200
201 static int cleanup(void)
202 {
203         return dev_clear();
204 }
205
206 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
207                 const struct sr_probe_group *probe_group)
208 {
209         struct dev_context *devc;
210
211         (void)probe_group;
212
213         if (sdi->status != SR_ST_ACTIVE)
214                 return SR_ERR_DEV_CLOSED;
215
216         if (!(devc = sdi->priv)) {
217                 sr_err("sdi->priv was NULL.");
218                 return SR_ERR_BUG;
219         }
220
221         switch (key) {
222         case SR_CONF_LIMIT_SAMPLES:
223                 devc->limit_samples = g_variant_get_uint64(data);
224                 sr_dbg("Setting sample limit to %" PRIu64 ".", devc->limit_samples);
225                 break;
226         case SR_CONF_LIMIT_MSEC:
227                 devc->limit_msec = g_variant_get_uint64(data);
228                 sr_dbg("Setting time limit to %" PRIu64 "ms.", devc->limit_msec);
229                 break;
230         default:
231                 return SR_ERR_NA;
232         }
233
234         return SR_OK;
235 }
236
237 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
238                 const struct sr_probe_group *probe_group)
239 {
240         (void)sdi;
241         (void)probe_group;
242
243         switch (key) {
244         case SR_CONF_SCAN_OPTIONS:
245                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
246                                 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
247                 break;
248         case SR_CONF_DEVICE_OPTIONS:
249                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
250                                 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
251                 break;
252         default:
253                 return SR_ERR_NA;
254         }
255
256         return SR_OK;
257 }
258
259 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
260 {
261         struct sr_serial_dev_inst *serial = sdi->conn;
262         struct dev_context *devc;
263
264         if (sdi->status != SR_ST_ACTIVE)
265                 return SR_ERR_DEV_CLOSED;
266
267         if (!(devc = sdi->priv)) {
268                 sr_err("sdi->priv was NULL.");
269                 return SR_ERR_BUG;
270         }
271
272         devc->session_cb_data = cb_data;
273
274         /*
275          * Reset the number of samples to take. If we've already collected our
276          * quota, but we start a new session, and don't reset this, we'll just
277          * quit without acquiring any new samples.
278          */
279         devc->num_samples = 0;
280         devc->start_time = g_get_monotonic_time();
281
282         /* Send header packet to the session bus. */
283         std_session_send_df_header(cb_data, LOG_PREFIX);
284
285         /* Poll every 50ms, or whenever some data comes in. */
286         serial_source_add(serial, G_IO_IN, 50, teleinfo_receive_data, (void *)sdi);
287
288         return SR_OK;
289 }
290
291 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
292 {
293         return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close,
294                                                sdi->conn, LOG_PREFIX);
295 }
296
297 SR_PRIV struct sr_dev_driver teleinfo_driver_info = {
298         .name = "teleinfo",
299         .longname = "Teleinfo",
300         .api_version = 1,
301         .init = init,
302         .cleanup = cleanup,
303         .scan = scan,
304         .dev_list = dev_list,
305         .dev_clear = dev_clear,
306         .config_set = config_set,
307         .config_list = config_list,
308         .dev_open = dev_open,
309         .dev_close = dev_close,
310         .dev_acquisition_start = dev_acquisition_start,
311         .dev_acquisition_stop = dev_acquisition_stop,
312 };