]> sigrok.org Git - libsigrok.git/blame - hardware/mic-985xx/api.c
Fix minor warning with some compilers.
[libsigrok.git] / hardware / mic-985xx / api.c
CommitLineData
7ec5b549
UH
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "protocol.h"
22
2c2be401 23static const int32_t hwopts[] = {
0cd8e231
UH
24 SR_CONF_CONN,
25 SR_CONF_SERIALCOMM,
0cd8e231
UH
26};
27
2c2be401 28static const int32_t hwcaps[] = {
0cd8e231
UH
29 SR_CONF_THERMOMETER,
30 SR_CONF_HYGROMETER,
31 SR_CONF_LIMIT_SAMPLES,
32 SR_CONF_LIMIT_MSEC,
33 SR_CONF_CONTINUOUS,
0cd8e231 34};
7ec5b549 35
6f3e5335 36SR_PRIV struct sr_dev_driver mic_98581_driver_info;
0cd8e231
UH
37SR_PRIV struct sr_dev_driver mic_98583_driver_info;
38
39SR_PRIV const struct mic_dev_info mic_devs[] = {
40 {
6f3e5335
UH
41 "MIC", "98581", "38400/8n2", 32000, TRUE, FALSE, 6,
42 packet_valid_temp,
43 &mic_98581_driver_info, receive_data_MIC_98581,
44 },
45 {
46 "MIC", "98583", "38400/8n2", 32000, TRUE, TRUE, 10,
47 packet_valid_temp_hum,
0cd8e231
UH
48 &mic_98583_driver_info, receive_data_MIC_98583,
49 },
50};
51
52static int clear_instances(int idx)
7ec5b549
UH
53{
54 struct sr_dev_inst *sdi;
55 struct drv_context *drvc;
56 struct dev_context *devc;
57 GSList *l;
0cd8e231
UH
58 struct sr_dev_driver *di;
59
60 di = mic_devs[idx].di;
7ec5b549
UH
61
62 if (!(drvc = di->priv))
63 return SR_OK;
64
65 for (l = drvc->instances; l; l = l->next) {
66 if (!(sdi = l->data))
67 continue;
68 if (!(devc = sdi->priv))
69 continue;
0cd8e231 70 sr_serial_dev_inst_free(devc->serial);
7ec5b549
UH
71 sr_dev_inst_free(sdi);
72 }
73
74 g_slist_free(drvc->instances);
75 drvc->instances = NULL;
76
77 return SR_OK;
78}
79
0cd8e231 80static int hw_init(struct sr_context *sr_ctx, int idx)
7ec5b549 81{
0cd8e231
UH
82 sr_dbg("Selected '%s' subdriver.", mic_devs[idx].di->name);
83
84 return std_hw_init(sr_ctx, mic_devs[idx].di, DRIVER_LOG_DOMAIN);
7ec5b549
UH
85}
86
0cd8e231 87static GSList *scan(const char *conn, const char *serialcomm, int idx)
7ec5b549 88{
0cd8e231 89 struct sr_dev_inst *sdi;
7ec5b549 90 struct drv_context *drvc;
0cd8e231
UH
91 struct dev_context *devc;
92 struct sr_probe *probe;
93 struct sr_serial_dev_inst *serial;
7ec5b549
UH
94 GSList *devices;
95
0cd8e231
UH
96 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
97 return NULL;
7ec5b549 98
0cd8e231
UH
99 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
100 return NULL;
101
102 drvc = mic_devs[idx].di->priv;
7ec5b549 103 devices = NULL;
0cd8e231 104 serial_flush(serial);
7ec5b549 105
0cd8e231
UH
106 /* TODO: Query device type. */
107 // ret = mic_cmd_get_device_info(serial);
108
109 sr_info("Found device on port %s.", conn);
110
111 /* TODO: Fill in version from protocol response. */
112 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, mic_devs[idx].vendor,
113 mic_devs[idx].device, "")))
114 goto scan_cleanup;
115
116 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
117 sr_err("Device context malloc failed.");
118 goto scan_cleanup;
119 }
120
121 devc->serial = serial;
122
123 sdi->priv = devc;
124 sdi->driver = mic_devs[idx].di;
125
126 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "Temperature")))
127 goto scan_cleanup;
128 sdi->probes = g_slist_append(sdi->probes, probe);
129
6f3e5335
UH
130 if (mic_devs[idx].has_humidity) {
131 if (!(probe = sr_probe_new(1, SR_PROBE_ANALOG, TRUE, "Humidity")))
132 goto scan_cleanup;
133 sdi->probes = g_slist_append(sdi->probes, probe);
134 }
0cd8e231
UH
135
136 drvc->instances = g_slist_append(drvc->instances, sdi);
137 devices = g_slist_append(devices, sdi);
138
139scan_cleanup:
140 serial_close(serial);
7ec5b549
UH
141
142 return devices;
143}
144
0cd8e231
UH
145static GSList *hw_scan(GSList *options, int idx)
146{
147 struct sr_config *src;
148 GSList *l, *devices;
149 const char *conn, *serialcomm;
150
151 conn = serialcomm = NULL;
152 for (l = options; l; l = l->next) {
153 src = l->data;
154 switch (src->key) {
155 case SR_CONF_CONN:
2c2be401 156 conn = g_variant_get_string(src->data, NULL);
0cd8e231
UH
157 break;
158 case SR_CONF_SERIALCOMM:
2c2be401 159 serialcomm = g_variant_get_string(src->data, NULL);
0cd8e231
UH
160 break;
161 }
162 }
163 if (!conn)
164 return NULL;
165
166 if (serialcomm) {
167 /* Use the provided comm specs. */
168 devices = scan(conn, serialcomm, idx);
169 } else {
170 /* Try the default. */
171 devices = scan(conn, mic_devs[idx].conn, idx);
172 }
173
174 return devices;
175}
176
177static GSList *hw_dev_list(int idx)
7ec5b549 178{
0e94d524 179 return ((struct drv_context *)(mic_devs[idx].di->priv))->instances;
7ec5b549
UH
180}
181
182static int hw_dev_open(struct sr_dev_inst *sdi)
183{
0cd8e231 184 struct dev_context *devc;
7ec5b549 185
0cd8e231 186 devc = sdi->priv;
7ec5b549 187
0cd8e231
UH
188 if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
189 return SR_ERR;
7ec5b549 190
0cd8e231 191 sdi->status = SR_ST_ACTIVE;
7ec5b549
UH
192
193 return SR_OK;
194}
195
0cd8e231 196static int hw_dev_close(struct sr_dev_inst *sdi)
7ec5b549 197{
0cd8e231 198 struct dev_context *devc;
7ec5b549 199
0cd8e231
UH
200 devc = sdi->priv;
201
202 if (devc->serial && devc->serial->fd != -1) {
203 serial_close(devc->serial);
204 sdi->status = SR_ST_INACTIVE;
205 }
7ec5b549
UH
206
207 return SR_OK;
208}
209
0cd8e231 210static int hw_cleanup(int idx)
7ec5b549 211{
0cd8e231 212 clear_instances(idx);
7ec5b549
UH
213
214 return SR_OK;
215}
216
2c2be401 217static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
7ec5b549 218{
0cd8e231 219 struct dev_context *devc;
7ec5b549 220
0cd8e231 221 if (sdi->status != SR_ST_ACTIVE)
7ec5b549 222 return SR_ERR;
7ec5b549 223
0cd8e231
UH
224 devc = sdi->priv;
225
7ec5b549 226 switch (id) {
0cd8e231 227 case SR_CONF_LIMIT_SAMPLES:
2c2be401 228 devc->limit_samples = g_variant_get_uint64(data);
0cd8e231
UH
229 sr_dbg("Setting sample limit to %" PRIu64 ".",
230 devc->limit_samples);
231 break;
232 case SR_CONF_LIMIT_MSEC:
2c2be401 233 devc->limit_msec = g_variant_get_uint64(data);
0cd8e231
UH
234 sr_dbg("Setting time limit to %" PRIu64 "ms.",
235 devc->limit_msec);
236 break;
7ec5b549 237 default:
bd6fbf62 238 return SR_ERR_NA;
7ec5b549
UH
239 }
240
0cd8e231 241 return SR_OK;
7ec5b549
UH
242}
243
2c2be401 244static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
7ec5b549
UH
245{
246 (void)sdi;
7ec5b549
UH
247
248 switch (key) {
0cd8e231 249 case SR_CONF_SCAN_OPTIONS:
2c2be401
BV
250 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
251 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
0cd8e231
UH
252 break;
253 case SR_CONF_DEVICE_OPTIONS:
2c2be401
BV
254 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
255 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
0cd8e231 256 break;
7ec5b549 257 default:
bd6fbf62 258 return SR_ERR_NA;
7ec5b549
UH
259 }
260
261 return SR_OK;
262}
263
264static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
0cd8e231 265 void *cb_data, int idx)
7ec5b549 266{
0cd8e231
UH
267 struct dev_context *devc;
268
269 devc = sdi->priv;
270
271 devc->cb_data = cb_data;
7ec5b549 272
0cd8e231
UH
273 devc->num_samples = 0;
274 devc->starttime = g_get_monotonic_time();
275
276 /* Send header packet to the session bus. */
4afdfd46 277 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
0cd8e231
UH
278
279 /* Poll every 100ms, or whenever some data comes in. */
280 sr_source_add(devc->serial->fd, G_IO_IN, 100,
281 mic_devs[idx].receive_data, (void *)sdi);
7ec5b549
UH
282
283 return SR_OK;
284}
285
286static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
287{
cd2f0fe2
UH
288 return std_hw_dev_acquisition_stop_serial(sdi, cb_data, hw_dev_close,
289 ((struct dev_context *)(sdi->priv))->serial, DRIVER_LOG_DOMAIN);
7ec5b549
UH
290}
291
0cd8e231
UH
292/* Driver-specific API function wrappers */
293#define HW_INIT(X) \
294static int hw_init_##X(struct sr_context *sr_ctx) { return hw_init(sr_ctx, X); }
295#define HW_CLEANUP(X) \
296static int hw_cleanup_##X(void) { return hw_cleanup(X); }
297#define HW_SCAN(X) \
298static GSList *hw_scan_##X(GSList *options) { return hw_scan(options, X); }
299#define HW_DEV_LIST(X) \
300static GSList *hw_dev_list_##X(void) { return hw_dev_list(X); }
301#define CLEAR_INSTANCES(X) \
302static int clear_instances_##X(void) { return clear_instances(X); }
303#define HW_DEV_ACQUISITION_START(X) \
304static int hw_dev_acquisition_start_##X(const struct sr_dev_inst *sdi, \
305void *cb_data) { return hw_dev_acquisition_start(sdi, cb_data, X); }
306
307/* Driver structs and API function wrappers */
308#define DRV(ID, ID_UPPER, NAME, LONGNAME) \
309HW_INIT(ID_UPPER) \
310HW_CLEANUP(ID_UPPER) \
311HW_SCAN(ID_UPPER) \
312HW_DEV_LIST(ID_UPPER) \
313CLEAR_INSTANCES(ID_UPPER) \
314HW_DEV_ACQUISITION_START(ID_UPPER) \
315SR_PRIV struct sr_dev_driver ID##_driver_info = { \
316 .name = NAME, \
317 .longname = LONGNAME, \
318 .api_version = 1, \
319 .init = hw_init_##ID_UPPER, \
320 .cleanup = hw_cleanup_##ID_UPPER, \
321 .scan = hw_scan_##ID_UPPER, \
322 .dev_list = hw_dev_list_##ID_UPPER, \
323 .dev_clear = clear_instances_##ID_UPPER, \
324 .config_get = NULL, \
325 .config_set = config_set, \
326 .config_list = config_list, \
327 .dev_open = hw_dev_open, \
328 .dev_close = hw_dev_close, \
329 .dev_acquisition_start = hw_dev_acquisition_start_##ID_UPPER, \
330 .dev_acquisition_stop = hw_dev_acquisition_stop, \
331 .priv = NULL, \
7ec5b549 332};
0cd8e231 333
6f3e5335 334DRV(mic_98581, MIC_98581, "mic-98581", "MIC 98581")
0cd8e231 335DRV(mic_98583, MIC_98583, "mic-98583", "MIC 98583")