]> sigrok.org Git - libsigrok.git/blob - src/hardware/appa-55ii/api.c
Fix #442 by renaming sr_dev_driver.priv to .context
[libsigrok.git] / src / hardware / appa-55ii / 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 <string.h>
21 #include "protocol.h"
22
23 static const uint32_t scanopts[] = {
24         SR_CONF_CONN,
25         SR_CONF_SERIALCOMM,
26 };
27
28 static const uint32_t drvopts[] = {
29         SR_CONF_THERMOMETER,
30 };
31
32 static const uint32_t devopts[] = {
33         SR_CONF_CONTINUOUS | SR_CONF_SET,
34         SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
35         SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
36         SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
37 };
38
39 static const char *data_sources[] = {
40         "Live",
41         "Memory",
42 };
43
44 SR_PRIV struct sr_dev_driver appa_55ii_driver_info;
45
46 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
47 {
48         return std_init(sr_ctx, di, LOG_PREFIX);
49 }
50
51 static GSList *scan(struct sr_dev_driver *di, GSList *options)
52 {
53         struct drv_context *drvc;
54         struct dev_context *devc;
55         struct sr_serial_dev_inst *serial;
56         struct sr_dev_inst *sdi;
57         struct sr_config *src;
58         GSList *devices, *l;
59         const char *conn, *serialcomm;
60         uint8_t buf[50];
61         size_t len;
62
63         len = sizeof(buf);
64         devices = NULL;
65         conn = serialcomm = NULL;
66         for (l = options; l; l = l->next) {
67                 src = l->data;
68                 switch (src->key) {
69                 case SR_CONF_CONN:
70                         conn = g_variant_get_string(src->data, NULL);
71                         break;
72                 case SR_CONF_SERIALCOMM:
73                         serialcomm = g_variant_get_string(src->data, NULL);
74                         break;
75                 }
76         }
77         if (!conn)
78                 return NULL;
79         if (!serialcomm)
80                 serialcomm = "9600/8n1";
81
82         serial = sr_serial_dev_inst_new(conn, serialcomm);
83
84         if (serial_open(serial, SERIAL_RDONLY) != SR_OK)
85                 return NULL;
86
87         sr_info("Probing serial port %s.", conn);
88
89         drvc = di->context;
90         drvc->instances = NULL;
91         serial_flush(serial);
92
93         /* Let's get a bit of data and see if we can find a packet. */
94         if (serial_stream_detect(serial, buf, &len, 25,
95                         appa_55ii_packet_valid, 500, 9600) != SR_OK)
96                 goto scan_cleanup;
97
98         sr_info("Found device on port %s.", conn);
99
100         sdi = g_malloc0(sizeof(struct sr_dev_inst));
101         sdi->status = SR_ST_INACTIVE;
102         sdi->vendor = g_strdup("APPA");
103         sdi->model = g_strdup("55II");
104         devc = g_malloc0(sizeof(struct dev_context));
105         devc->data_source = DEFAULT_DATA_SOURCE;
106         sdi->inst_type = SR_INST_SERIAL;
107         sdi->conn = serial;
108         sdi->priv = devc;
109         sdi->driver = di;
110
111         sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "T1");
112         sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "T2");
113
114         drvc->instances = g_slist_append(drvc->instances, sdi);
115         devices = g_slist_append(devices, sdi);
116
117 scan_cleanup:
118         serial_close(serial);
119
120         return devices;
121 }
122
123 static GSList *dev_list(const struct sr_dev_driver *di)
124 {
125         return ((struct drv_context *)(di->context))->instances;
126 }
127
128 static int cleanup(const struct sr_dev_driver *di)
129 {
130         return std_dev_clear(di, NULL);
131 }
132
133 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
134                 const struct sr_channel_group *cg)
135 {
136         struct dev_context *devc = sdi->priv;
137
138         (void)cg;
139
140         switch (key) {
141         case SR_CONF_LIMIT_SAMPLES:
142                 *data = g_variant_new_uint64(devc->limit_samples);
143                 break;
144         case SR_CONF_LIMIT_MSEC:
145                 *data = g_variant_new_uint64(devc->limit_msec);
146                 break;
147         case SR_CONF_DATA_SOURCE:
148                 *data = g_variant_new_string(data_sources[devc->data_source]);
149                 break;
150         default:
151                 return SR_ERR_NA;
152         }
153
154         return SR_OK;
155 }
156
157 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
158                 const struct sr_channel_group *cg)
159 {
160         struct dev_context *devc;
161         const char *tmp_str;
162         unsigned int i;
163
164         (void)cg;
165
166         if (sdi->status != SR_ST_ACTIVE)
167                 return SR_ERR_DEV_CLOSED;
168
169         if (!(devc = sdi->priv)) {
170                 sr_err("sdi->priv was NULL.");
171                 return SR_ERR_BUG;
172         }
173
174         switch (key) {
175         case SR_CONF_LIMIT_SAMPLES:
176                 devc->limit_samples = g_variant_get_uint64(data);
177                 break;
178         case SR_CONF_LIMIT_MSEC:
179                 devc->limit_msec = g_variant_get_uint64(data);
180                 break;
181         case SR_CONF_DATA_SOURCE: {
182                 tmp_str = g_variant_get_string(data, NULL);
183                 for (i = 0; i < ARRAY_SIZE(data_sources); i++)
184                         if (!strcmp(tmp_str, data_sources[i])) {
185                                 devc->data_source = i;
186                                 break;
187                         }
188                 if (i == ARRAY_SIZE(data_sources))
189                         return SR_ERR;
190                 break;
191         }
192         default:
193                 return SR_ERR_NA;
194         }
195
196         return SR_OK;
197 }
198
199 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
200                 const struct sr_channel_group *cg)
201 {
202         (void)cg;
203
204         switch (key) {
205         case SR_CONF_SCAN_OPTIONS:
206                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
207                                 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
208                 break;
209         case SR_CONF_DEVICE_OPTIONS:
210                 if (!sdi)
211                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
212                                         drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
213                 else
214                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
215                                         devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
216                 break;
217         case SR_CONF_DATA_SOURCE:
218                 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
219                 break;
220         default:
221                 return SR_ERR_NA;
222         }
223
224         return SR_OK;
225 }
226
227 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
228                 void *cb_data)
229 {
230         struct sr_serial_dev_inst *serial;
231         struct dev_context *devc;
232
233         serial = sdi->conn;
234         if (sdi->status != SR_ST_ACTIVE)
235                 return SR_ERR_DEV_CLOSED;
236
237         if (!(devc = sdi->priv)) {
238                 sr_err("sdi->priv was NULL.");
239                 return SR_ERR_BUG;
240         }
241
242         devc->session_cb_data = cb_data;
243
244         /*
245          * Reset the number of samples to take. If we've already collected our
246          * quota, but we start a new session, and don't reset this, we'll just
247          * quit without acquiring any new samples.
248          */
249         devc->num_samples = 0;
250         devc->start_time = g_get_monotonic_time();
251
252         /* Send header packet to the session bus. */
253         std_session_send_df_header(cb_data, LOG_PREFIX);
254
255         /* Poll every 50ms, or whenever some data comes in. */
256         serial_source_add(sdi->session, serial, G_IO_IN, 50,
257                         appa_55ii_receive_data, (void *)sdi);
258
259         return SR_OK;
260 }
261
262 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
263 {
264         return std_serial_dev_acquisition_stop(sdi, cb_data,
265                         std_serial_dev_close, sdi->conn, LOG_PREFIX);
266 }
267
268 SR_PRIV struct sr_dev_driver appa_55ii_driver_info = {
269         .name = "appa-55ii",
270         .longname = "APPA 55II",
271         .api_version = 1,
272         .init = init,
273         .cleanup = cleanup,
274         .scan = scan,
275         .dev_list = dev_list,
276         .dev_clear = NULL,
277         .config_get = config_get,
278         .config_set = config_set,
279         .config_list = config_list,
280         .dev_open = std_serial_dev_open,
281         .dev_close = std_serial_dev_close,
282         .dev_acquisition_start = dev_acquisition_start,
283         .dev_acquisition_stop = dev_acquisition_stop,
284         .context = NULL,
285 };