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