]>
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 | ||
6078d2c9 | 60 | static int init(struct sr_context *sr_ctx, int idx) |
7ec5b549 | 61 | { |
f6beaac5 | 62 | return std_init(sr_ctx, mic_devs[idx].di, LOG_PREFIX); |
7ec5b549 UH |
63 | } |
64 | ||
6078d2c9 | 65 | static GSList *mic_scan(const char *conn, const char *serialcomm, int idx) |
7ec5b549 | 66 | { |
0cd8e231 | 67 | struct sr_dev_inst *sdi; |
7ec5b549 | 68 | struct drv_context *drvc; |
0cd8e231 | 69 | struct dev_context *devc; |
0cd8e231 | 70 | struct sr_serial_dev_inst *serial; |
7ec5b549 UH |
71 | GSList *devices; |
72 | ||
91219afc | 73 | serial = sr_serial_dev_inst_new(conn, serialcomm); |
7ec5b549 | 74 | |
51056d61 | 75 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) |
0cd8e231 UH |
76 | return NULL; |
77 | ||
41812aca | 78 | drvc = mic_devs[idx].di->context; |
7ec5b549 | 79 | devices = NULL; |
0cd8e231 | 80 | serial_flush(serial); |
7ec5b549 | 81 | |
0cd8e231 UH |
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. */ | |
aac29cc1 | 88 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
0af636be UH |
89 | sdi->status = SR_ST_INACTIVE; |
90 | sdi->vendor = g_strdup(mic_devs[idx].vendor); | |
91 | sdi->model = g_strdup(mic_devs[idx].device); | |
f57d8ffe | 92 | devc = g_malloc0(sizeof(struct dev_context)); |
886bd5e0 UH |
93 | sdi->inst_type = SR_INST_SERIAL; |
94 | sdi->conn = serial; | |
0cd8e231 UH |
95 | sdi->priv = devc; |
96 | sdi->driver = mic_devs[idx].di; | |
97 | ||
5e23fcab | 98 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "Temperature"); |
0cd8e231 | 99 | |
5e23fcab ML |
100 | if (mic_devs[idx].has_humidity) |
101 | sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "Humidity"); | |
0cd8e231 UH |
102 | |
103 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
104 | devices = g_slist_append(devices, sdi); | |
105 | ||
0cd8e231 | 106 | serial_close(serial); |
7ec5b549 UH |
107 | |
108 | return devices; | |
109 | } | |
110 | ||
6078d2c9 | 111 | static GSList *scan(GSList *options, int idx) |
0cd8e231 UH |
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: | |
2c2be401 | 122 | conn = g_variant_get_string(src->data, NULL); |
0cd8e231 UH |
123 | break; |
124 | case SR_CONF_SERIALCOMM: | |
06c45a66 | 125 | serialcomm = g_variant_get_string(src->data, NULL); |
0cd8e231 UH |
126 | break; |
127 | } | |
128 | } | |
129 | if (!conn) | |
130 | return NULL; | |
131 | ||
132 | if (serialcomm) { | |
133 | /* Use the provided comm specs. */ | |
6078d2c9 | 134 | devices = mic_scan(conn, serialcomm, idx); |
0cd8e231 UH |
135 | } else { |
136 | /* Try the default. */ | |
6078d2c9 | 137 | devices = mic_scan(conn, mic_devs[idx].conn, idx); |
0cd8e231 UH |
138 | } |
139 | ||
140 | return devices; | |
141 | } | |
142 | ||
6078d2c9 | 143 | static GSList *dev_list(int idx) |
7ec5b549 | 144 | { |
41812aca | 145 | return ((struct drv_context *)(mic_devs[idx].di->context))->instances; |
7ec5b549 UH |
146 | } |
147 | ||
584560f1 | 148 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, |
53b4680f | 149 | const struct sr_channel_group *cg) |
7ec5b549 | 150 | { |
0cd8e231 | 151 | struct dev_context *devc; |
7ec5b549 | 152 | |
53b4680f | 153 | (void)cg; |
8f996b89 | 154 | |
0cd8e231 | 155 | if (sdi->status != SR_ST_ACTIVE) |
e73ffd42 | 156 | return SR_ERR_DEV_CLOSED; |
7ec5b549 | 157 | |
0cd8e231 UH |
158 | devc = sdi->priv; |
159 | ||
584560f1 | 160 | switch (key) { |
0cd8e231 | 161 | case SR_CONF_LIMIT_SAMPLES: |
2c2be401 | 162 | devc->limit_samples = g_variant_get_uint64(data); |
0cd8e231 UH |
163 | break; |
164 | case SR_CONF_LIMIT_MSEC: | |
2c2be401 | 165 | devc->limit_msec = g_variant_get_uint64(data); |
0cd8e231 | 166 | break; |
7ec5b549 | 167 | default: |
bd6fbf62 | 168 | return SR_ERR_NA; |
7ec5b549 UH |
169 | } |
170 | ||
0cd8e231 | 171 | return SR_OK; |
7ec5b549 UH |
172 | } |
173 | ||
584560f1 | 174 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
e07edc83 | 175 | const struct sr_channel_group *cg, int idx) |
7ec5b549 | 176 | { |
53b4680f | 177 | (void)cg; |
7ec5b549 UH |
178 | |
179 | switch (key) { | |
0cd8e231 | 180 | case SR_CONF_SCAN_OPTIONS: |
584560f1 | 181 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
a0e0bb41 | 182 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); |
0cd8e231 UH |
183 | break; |
184 | case SR_CONF_DEVICE_OPTIONS: | |
e07edc83 UH |
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) { | |
d6e1e6c4 | 190 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
e07edc83 UH |
191 | drvopts_temp_hum, ARRAY_SIZE(drvopts_temp_hum), |
192 | sizeof(uint32_t)); | |
193 | } else { | |
d6e1e6c4 | 194 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
f254bc4b | 195 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); |
e07edc83 | 196 | } |
0cd8e231 | 197 | break; |
7ec5b549 | 198 | default: |
bd6fbf62 | 199 | return SR_ERR_NA; |
7ec5b549 UH |
200 | } |
201 | ||
202 | return SR_OK; | |
203 | } | |
204 | ||
695dc859 | 205 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, int idx) |
7ec5b549 | 206 | { |
0cd8e231 | 207 | struct dev_context *devc; |
886bd5e0 | 208 | struct sr_serial_dev_inst *serial; |
0cd8e231 | 209 | |
e73ffd42 BV |
210 | if (sdi->status != SR_ST_ACTIVE) |
211 | return SR_ERR_DEV_CLOSED; | |
0cd8e231 | 212 | |
e73ffd42 | 213 | devc = sdi->priv; |
0cd8e231 UH |
214 | devc->num_samples = 0; |
215 | devc->starttime = g_get_monotonic_time(); | |
216 | ||
695dc859 | 217 | std_session_send_df_header(sdi, LOG_PREFIX); |
0cd8e231 UH |
218 | |
219 | /* Poll every 100ms, or whenever some data comes in. */ | |
886bd5e0 | 220 | serial = sdi->conn; |
102f1239 | 221 | serial_source_add(sdi->session, serial, G_IO_IN, 100, |
0cd8e231 | 222 | mic_devs[idx].receive_data, (void *)sdi); |
7ec5b549 UH |
223 | |
224 | return SR_OK; | |
225 | } | |
226 | ||
695dc859 | 227 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
7ec5b549 | 228 | { |
6525d819 | 229 | return std_serial_dev_acquisition_stop(sdi, std_serial_dev_close, |
d43b0908 | 230 | sdi->conn, LOG_PREFIX); |
7ec5b549 UH |
231 | } |
232 | ||
0cd8e231 UH |
233 | /* Driver-specific API function wrappers */ |
234 | #define HW_INIT(X) \ | |
4f840ce9 | 235 | static int init_##X(struct sr_dev_driver *di, struct sr_context *sr_ctx) { \ |
1a863916 | 236 | (void)di; return init(sr_ctx, X); } |
0cd8e231 | 237 | #define HW_SCAN(X) \ |
4f840ce9 | 238 | static GSList *scan_##X(struct sr_dev_driver *di, GSList *options) { \ |
1a863916 | 239 | (void)di; return scan(options, X); } |
0cd8e231 | 240 | #define HW_DEV_LIST(X) \ |
4f840ce9 | 241 | static GSList *dev_list_##X(const struct sr_dev_driver *di) { \ |
1a863916 | 242 | (void)di; return dev_list(X); } |
e07edc83 UH |
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); } | |
0cd8e231 | 247 | #define HW_DEV_ACQUISITION_START(X) \ |
695dc859 UH |
248 | static int dev_acquisition_start_##X(const struct sr_dev_inst *sdi \ |
249 | ) { return dev_acquisition_start(sdi, X); } | |
0cd8e231 UH |
250 | |
251 | /* Driver structs and API function wrappers */ | |
252 | #define DRV(ID, ID_UPPER, NAME, LONGNAME) \ | |
253 | HW_INIT(ID_UPPER) \ | |
0cd8e231 UH |
254 | HW_SCAN(ID_UPPER) \ |
255 | HW_DEV_LIST(ID_UPPER) \ | |
e07edc83 | 256 | HW_CONFIG_LIST(ID_UPPER) \ |
0cd8e231 UH |
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, \ | |
6078d2c9 | 262 | .init = init_##ID_UPPER, \ |
700d6b64 | 263 | .cleanup = std_cleanup, \ |
6078d2c9 UH |
264 | .scan = scan_##ID_UPPER, \ |
265 | .dev_list = dev_list_##ID_UPPER, \ | |
0cd8e231 UH |
266 | .config_get = NULL, \ |
267 | .config_set = config_set, \ | |
e07edc83 | 268 | .config_list = config_list_##ID_UPPER, \ |
854434de | 269 | .dev_open = std_serial_dev_open, \ |
bf2c987f | 270 | .dev_close = std_serial_dev_close, \ |
6078d2c9 UH |
271 | .dev_acquisition_start = dev_acquisition_start_##ID_UPPER, \ |
272 | .dev_acquisition_stop = dev_acquisition_stop, \ | |
41812aca | 273 | .context = NULL, \ |
7ec5b549 | 274 | }; |
0cd8e231 | 275 | |
6f3e5335 | 276 | DRV(mic_98581, MIC_98581, "mic-98581", "MIC 98581") |
0cd8e231 | 277 | DRV(mic_98583, MIC_98583, "mic-98583", "MIC 98583") |