]> sigrok.org Git - libsigrok.git/blob - src/hardware/rdtech-tc/api.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / hardware / rdtech-tc / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2020 Andreas Sandberg <andreas@sandberg.pp.se>
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 <glib.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <libsigrok/libsigrok.h>
27 #include "libsigrok-internal.h"
28 #include "protocol.h"
29
30 #define RDTECH_TC_SERIALCOMM "115200/8n1"
31
32 static const uint32_t scanopts[] = {
33         SR_CONF_CONN,
34         SR_CONF_SERIALCOMM,
35 };
36
37 static const uint32_t drvopts[] = {
38         SR_CONF_ENERGYMETER,
39 };
40
41 static const uint32_t devopts[] = {
42         SR_CONF_CONTINUOUS,
43         SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
44         SR_CONF_LIMIT_MSEC | SR_CONF_SET,
45 };
46
47 static GSList *rdtech_tc_scan(struct sr_dev_driver *di, const char *conn,
48                               const char *serialcomm)
49 {
50         struct sr_serial_dev_inst *serial;
51         GSList *devices = NULL;
52         struct dev_context *devc = NULL;
53         struct sr_dev_inst *sdi = NULL;
54
55         serial = sr_serial_dev_inst_new(conn, serialcomm);
56         if (serial_open(serial, SERIAL_RDWR) != SR_OK)
57                 goto err_out;
58
59         devc = g_malloc0(sizeof(struct dev_context));
60         sr_sw_limits_init(&devc->limits);
61
62         if (rdtech_tc_probe(serial, devc) != SR_OK) {
63                 sr_err("Failed to find a supported RDTech TC device.");
64                 goto err_out_serial;
65         }
66
67         sdi = g_malloc0(sizeof(struct sr_dev_inst));
68         sdi->status = SR_ST_INACTIVE;
69         sdi->vendor = g_strdup("RDTech");
70         sdi->model = g_strdup(devc->dev_info.model_name);
71         sdi->version = g_strdup(devc->dev_info.fw_ver);
72         sdi->serial_num = g_strdup_printf("%08" PRIu32, devc->dev_info.serial_num);
73         sdi->inst_type = SR_INST_SERIAL;
74         sdi->conn = serial;
75         sdi->priv = devc;
76
77         for (int i = 0; devc->channels[i].name; i++)
78                 sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, devc->channels[i].name);
79
80         devices = g_slist_append(devices, sdi);
81         serial_close(serial);
82         if (!devices)
83                 sr_serial_dev_inst_free(serial);
84
85         return std_scan_complete(di, devices);
86
87 err_out_serial:
88         g_free(devc);
89         serial_close(serial);
90 err_out:
91         sr_serial_dev_inst_free(serial);
92
93         return NULL;
94 }
95
96 static GSList *scan(struct sr_dev_driver *di, GSList *options)
97 {
98         struct sr_config *src;
99         const char *conn = NULL;
100         const char *serialcomm = RDTECH_TC_SERIALCOMM;
101
102         for (GSList *l = options; l; l = l->next) {
103                 src = l->data;
104                 switch (src->key) {
105                 case SR_CONF_CONN:
106                         conn = g_variant_get_string(src->data, NULL);
107                         break;
108                 case SR_CONF_SERIALCOMM:
109                         serialcomm = g_variant_get_string(src->data, NULL);
110                         break;
111                 }
112         }
113         if (!conn)
114                 return NULL;
115
116         return rdtech_tc_scan(di, conn, serialcomm);
117 }
118
119 static int config_set(uint32_t key, GVariant *data,
120                       const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
121 {
122         struct dev_context *devc;
123
124         (void)cg;
125
126         devc = sdi->priv;
127
128         return sr_sw_limits_config_set(&devc->limits, key, data);
129 }
130
131 static int config_list(uint32_t key, GVariant **data,
132                        const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
133 {
134         return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
135 }
136
137 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
138 {
139         struct dev_context *devc = sdi->priv;
140         struct sr_serial_dev_inst *serial = sdi->conn;
141
142         sr_sw_limits_acquisition_start(&devc->limits);
143         std_session_send_df_header(sdi);
144
145         serial_source_add(sdi->session, serial, G_IO_IN, 50,
146                           rdtech_tc_receive_data, (void *)sdi);
147
148         return rdtech_tc_poll(sdi);
149 }
150
151 static struct sr_dev_driver rdtech_tc_driver_info = {
152         .name = "rdtech-tc",
153         .longname = "RDTech TC66C USB power meter",
154         .api_version = 1,
155         .init = std_init,
156         .cleanup = std_cleanup,
157         .scan = scan,
158         .dev_list = std_dev_list,
159         .dev_clear = std_dev_clear,
160         .config_get = NULL,
161         .config_set = config_set,
162         .config_list = config_list,
163         .dev_open = std_serial_dev_open,
164         .dev_close = std_serial_dev_close,
165         .dev_acquisition_start = dev_acquisition_start,
166         .dev_acquisition_stop = std_serial_dev_acquisition_stop,
167         .context = NULL,
168 };
169 SR_REGISTER_DEV_DRIVER(rdtech_tc_driver_info);