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