]> sigrok.org Git - libsigrok.git/blame_incremental - src/hardware/mic-985xx/api.c
Add helper function for scan completion
[libsigrok.git] / src / hardware / mic-985xx / api.c
... / ...
CommitLineData
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 <config.h>
22#include "protocol.h"
23
24static const uint32_t scanopts[] = {
25 SR_CONF_CONN,
26 SR_CONF_SERIALCOMM,
27};
28
29static const uint32_t drvopts_temp[] = {
30 SR_CONF_THERMOMETER,
31};
32
33static const uint32_t drvopts_temp_hum[] = {
34 SR_CONF_THERMOMETER,
35 SR_CONF_HYGROMETER,
36};
37
38static const uint32_t devopts[] = {
39 SR_CONF_CONTINUOUS,
40 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
41 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
42};
43
44static struct sr_dev_driver mic_98581_driver_info;
45static struct sr_dev_driver mic_98583_driver_info;
46
47SR_PRIV const struct mic_dev_info mic_devs[] = {
48 {
49 "MIC", "98581", "38400/8n2", 32000, TRUE, FALSE, 6,
50 packet_valid_temp,
51 &mic_98581_driver_info, receive_data_MIC_98581,
52 },
53 {
54 "MIC", "98583", "38400/8n2", 32000, TRUE, TRUE, 10,
55 packet_valid_temp_hum,
56 &mic_98583_driver_info, receive_data_MIC_98583,
57 },
58};
59
60static GSList *mic_scan(const char *conn, const char *serialcomm, int idx)
61{
62 struct sr_dev_inst *sdi;
63 struct dev_context *devc;
64 struct sr_serial_dev_inst *serial;
65 GSList *devices;
66
67 serial = sr_serial_dev_inst_new(conn, serialcomm);
68
69 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
70 return NULL;
71
72 devices = NULL;
73 serial_flush(serial);
74
75 /* TODO: Query device type. */
76 // ret = mic_cmd_get_device_info(serial);
77
78 sr_info("Found device on port %s.", conn);
79
80 /* TODO: Fill in version from protocol response. */
81 sdi = g_malloc0(sizeof(struct sr_dev_inst));
82 sdi->status = SR_ST_INACTIVE;
83 sdi->vendor = g_strdup(mic_devs[idx].vendor);
84 sdi->model = g_strdup(mic_devs[idx].device);
85 devc = g_malloc0(sizeof(struct dev_context));
86 sr_sw_limits_init(&devc->limits);
87 sdi->inst_type = SR_INST_SERIAL;
88 sdi->conn = serial;
89 sdi->priv = devc;
90
91 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "Temperature");
92
93 if (mic_devs[idx].has_humidity)
94 sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "Humidity");
95
96 devices = g_slist_append(devices, sdi);
97
98 serial_close(serial);
99
100 return std_scan_complete(mic_devs[idx].di, devices);
101}
102
103static GSList *scan(GSList *options, int idx)
104{
105 struct sr_config *src;
106 GSList *l, *devices;
107 const char *conn, *serialcomm;
108
109 conn = serialcomm = NULL;
110 for (l = options; l; l = l->next) {
111 src = l->data;
112 switch (src->key) {
113 case SR_CONF_CONN:
114 conn = g_variant_get_string(src->data, NULL);
115 break;
116 case SR_CONF_SERIALCOMM:
117 serialcomm = g_variant_get_string(src->data, NULL);
118 break;
119 }
120 }
121 if (!conn)
122 return NULL;
123
124 if (serialcomm) {
125 /* Use the provided comm specs. */
126 devices = mic_scan(conn, serialcomm, idx);
127 } else {
128 /* Try the default. */
129 devices = mic_scan(conn, mic_devs[idx].conn, idx);
130 }
131
132 return devices;
133}
134
135static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
136 const struct sr_channel_group *cg)
137{
138 struct dev_context *devc;
139
140 (void)cg;
141
142 if (sdi->status != SR_ST_ACTIVE)
143 return SR_ERR_DEV_CLOSED;
144
145 devc = sdi->priv;
146
147 return sr_sw_limits_config_set(&devc->limits, key, data);
148}
149
150static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
151 const struct sr_channel_group *cg, int idx)
152{
153 (void)cg;
154
155 switch (key) {
156 case SR_CONF_SCAN_OPTIONS:
157 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
158 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
159 break;
160 case SR_CONF_DEVICE_OPTIONS:
161 if (!sdi && !mic_devs[idx].has_humidity) {
162 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
163 drvopts_temp, ARRAY_SIZE(drvopts_temp),
164 sizeof(uint32_t));
165 } else if (!sdi && mic_devs[idx].has_humidity) {
166 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
167 drvopts_temp_hum, ARRAY_SIZE(drvopts_temp_hum),
168 sizeof(uint32_t));
169 } else {
170 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
171 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
172 }
173 break;
174 default:
175 return SR_ERR_NA;
176 }
177
178 return SR_OK;
179}
180
181static int dev_acquisition_start(const struct sr_dev_inst *sdi, int idx)
182{
183 struct dev_context *devc;
184 struct sr_serial_dev_inst *serial;
185
186 if (sdi->status != SR_ST_ACTIVE)
187 return SR_ERR_DEV_CLOSED;
188
189 devc = sdi->priv;
190
191 sr_sw_limits_acquisition_start(&devc->limits);
192 std_session_send_df_header(sdi, LOG_PREFIX);
193
194 /* Poll every 100ms, or whenever some data comes in. */
195 serial = sdi->conn;
196 serial_source_add(sdi->session, serial, G_IO_IN, 100,
197 mic_devs[idx].receive_data, (void *)sdi);
198
199 return SR_OK;
200}
201
202static int dev_acquisition_stop(struct sr_dev_inst *sdi)
203{
204 return std_serial_dev_acquisition_stop(sdi, std_serial_dev_close,
205 sdi->conn, LOG_PREFIX);
206}
207
208/* Driver-specific API function wrappers */
209#define HW_SCAN(X) \
210static GSList *scan_##X(struct sr_dev_driver *di, GSList *options) { \
211 (void)di; return scan(options, X); }
212#define HW_CONFIG_LIST(X) \
213static int config_list_##X(uint32_t key, GVariant **data, \
214const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { \
215return config_list(key, data, sdi, cg, X); }
216#define HW_DEV_ACQUISITION_START(X) \
217static int dev_acquisition_start_##X(const struct sr_dev_inst *sdi \
218) { return dev_acquisition_start(sdi, X); }
219
220/* Driver structs and API function wrappers */
221#define DRV(ID, ID_UPPER, NAME, LONGNAME) \
222HW_SCAN(ID_UPPER) \
223HW_CONFIG_LIST(ID_UPPER) \
224HW_DEV_ACQUISITION_START(ID_UPPER) \
225static struct sr_dev_driver ID##_driver_info = { \
226 .name = NAME, \
227 .longname = LONGNAME, \
228 .api_version = 1, \
229 .init = std_init, \
230 .cleanup = std_cleanup, \
231 .scan = scan_##ID_UPPER, \
232 .dev_list = std_dev_list, \
233 .config_get = NULL, \
234 .config_set = config_set, \
235 .config_list = config_list_##ID_UPPER, \
236 .dev_open = std_serial_dev_open, \
237 .dev_close = std_serial_dev_close, \
238 .dev_acquisition_start = dev_acquisition_start_##ID_UPPER, \
239 .dev_acquisition_stop = dev_acquisition_stop, \
240 .context = NULL, \
241}; \
242SR_REGISTER_DEV_DRIVER(ID##_driver_info)
243
244DRV(mic_98581, MIC_98581, "mic-98581", "MIC 98581")
245DRV(mic_98583, MIC_98583, "mic-98583", "MIC 98583")