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