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