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