]>
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[] = { | |
e91bb0a6 | 39 | SR_CONF_CONTINUOUS, |
5827f61b BV |
40 | SR_CONF_LIMIT_SAMPLES | SR_CONF_SET, |
41 | SR_CONF_LIMIT_MSEC | SR_CONF_SET, | |
0cd8e231 | 42 | }; |
7ec5b549 | 43 | |
dd5c48a6 LPC |
44 | static struct sr_dev_driver mic_98581_driver_info; |
45 | static struct sr_dev_driver mic_98583_driver_info; | |
0cd8e231 UH |
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 GSList *mic_scan(const char *conn, const char *serialcomm, int idx) |
7ec5b549 | 61 | { |
0cd8e231 | 62 | struct sr_dev_inst *sdi; |
0cd8e231 | 63 | struct dev_context *devc; |
0cd8e231 | 64 | struct sr_serial_dev_inst *serial; |
7ec5b549 | 65 | |
91219afc | 66 | serial = sr_serial_dev_inst_new(conn, serialcomm); |
7ec5b549 | 67 | |
51056d61 | 68 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) |
0cd8e231 UH |
69 | return NULL; |
70 | ||
0cd8e231 | 71 | serial_flush(serial); |
7ec5b549 | 72 | |
0cd8e231 UH |
73 | /* TODO: Query device type. */ |
74 | // ret = mic_cmd_get_device_info(serial); | |
75 | ||
76 | sr_info("Found device on port %s.", conn); | |
77 | ||
78 | /* TODO: Fill in version from protocol response. */ | |
aac29cc1 | 79 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
0af636be UH |
80 | sdi->status = SR_ST_INACTIVE; |
81 | sdi->vendor = g_strdup(mic_devs[idx].vendor); | |
82 | sdi->model = g_strdup(mic_devs[idx].device); | |
f57d8ffe | 83 | devc = g_malloc0(sizeof(struct dev_context)); |
301090aa | 84 | sr_sw_limits_init(&devc->limits); |
886bd5e0 UH |
85 | sdi->inst_type = SR_INST_SERIAL; |
86 | sdi->conn = serial; | |
0cd8e231 | 87 | sdi->priv = devc; |
0cd8e231 | 88 | |
5e23fcab | 89 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "Temperature"); |
0cd8e231 | 90 | |
5e23fcab ML |
91 | if (mic_devs[idx].has_humidity) |
92 | sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "Humidity"); | |
0cd8e231 | 93 | |
0cd8e231 | 94 | serial_close(serial); |
7ec5b549 | 95 | |
43376f33 | 96 | return std_scan_complete(mic_devs[idx].di, g_slist_append(NULL, sdi)); |
7ec5b549 UH |
97 | } |
98 | ||
6078d2c9 | 99 | static GSList *scan(GSList *options, int idx) |
0cd8e231 UH |
100 | { |
101 | struct sr_config *src; | |
102 | GSList *l, *devices; | |
103 | const char *conn, *serialcomm; | |
104 | ||
105 | conn = serialcomm = NULL; | |
106 | for (l = options; l; l = l->next) { | |
107 | src = l->data; | |
108 | switch (src->key) { | |
109 | case SR_CONF_CONN: | |
2c2be401 | 110 | conn = g_variant_get_string(src->data, NULL); |
0cd8e231 UH |
111 | break; |
112 | case SR_CONF_SERIALCOMM: | |
06c45a66 | 113 | serialcomm = g_variant_get_string(src->data, NULL); |
0cd8e231 UH |
114 | break; |
115 | } | |
116 | } | |
117 | if (!conn) | |
118 | return NULL; | |
119 | ||
120 | if (serialcomm) { | |
121 | /* Use the provided comm specs. */ | |
6078d2c9 | 122 | devices = mic_scan(conn, serialcomm, idx); |
0cd8e231 UH |
123 | } else { |
124 | /* Try the default. */ | |
6078d2c9 | 125 | devices = mic_scan(conn, mic_devs[idx].conn, idx); |
0cd8e231 UH |
126 | } |
127 | ||
128 | return devices; | |
129 | } | |
130 | ||
584560f1 | 131 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, |
53b4680f | 132 | const struct sr_channel_group *cg) |
7ec5b549 | 133 | { |
0cd8e231 | 134 | struct dev_context *devc; |
7ec5b549 | 135 | |
53b4680f | 136 | (void)cg; |
8f996b89 | 137 | |
0cd8e231 | 138 | if (sdi->status != SR_ST_ACTIVE) |
e73ffd42 | 139 | return SR_ERR_DEV_CLOSED; |
7ec5b549 | 140 | |
0cd8e231 UH |
141 | devc = sdi->priv; |
142 | ||
301090aa | 143 | return sr_sw_limits_config_set(&devc->limits, key, data); |
7ec5b549 UH |
144 | } |
145 | ||
584560f1 | 146 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
e07edc83 | 147 | const struct sr_channel_group *cg, int idx) |
7ec5b549 | 148 | { |
53b4680f | 149 | (void)cg; |
7ec5b549 UH |
150 | |
151 | switch (key) { | |
0cd8e231 | 152 | case SR_CONF_SCAN_OPTIONS: |
584560f1 | 153 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
a0e0bb41 | 154 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); |
0cd8e231 UH |
155 | break; |
156 | case SR_CONF_DEVICE_OPTIONS: | |
e07edc83 UH |
157 | if (!sdi && !mic_devs[idx].has_humidity) { |
158 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
159 | drvopts_temp, ARRAY_SIZE(drvopts_temp), | |
160 | sizeof(uint32_t)); | |
161 | } else if (!sdi && mic_devs[idx].has_humidity) { | |
d6e1e6c4 | 162 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
e07edc83 UH |
163 | drvopts_temp_hum, ARRAY_SIZE(drvopts_temp_hum), |
164 | sizeof(uint32_t)); | |
165 | } else { | |
d6e1e6c4 | 166 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
f254bc4b | 167 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); |
e07edc83 | 168 | } |
0cd8e231 | 169 | break; |
7ec5b549 | 170 | default: |
bd6fbf62 | 171 | return SR_ERR_NA; |
7ec5b549 UH |
172 | } |
173 | ||
174 | return SR_OK; | |
175 | } | |
176 | ||
695dc859 | 177 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, int idx) |
7ec5b549 | 178 | { |
0cd8e231 | 179 | struct dev_context *devc; |
886bd5e0 | 180 | struct sr_serial_dev_inst *serial; |
0cd8e231 | 181 | |
e73ffd42 BV |
182 | if (sdi->status != SR_ST_ACTIVE) |
183 | return SR_ERR_DEV_CLOSED; | |
0cd8e231 | 184 | |
e73ffd42 | 185 | devc = sdi->priv; |
0cd8e231 | 186 | |
301090aa | 187 | sr_sw_limits_acquisition_start(&devc->limits); |
bee2b016 | 188 | std_session_send_df_header(sdi); |
0cd8e231 UH |
189 | |
190 | /* Poll every 100ms, or whenever some data comes in. */ | |
886bd5e0 | 191 | serial = sdi->conn; |
102f1239 | 192 | serial_source_add(sdi->session, serial, G_IO_IN, 100, |
0cd8e231 | 193 | mic_devs[idx].receive_data, (void *)sdi); |
7ec5b549 UH |
194 | |
195 | return SR_OK; | |
196 | } | |
197 | ||
0cd8e231 | 198 | /* Driver-specific API function wrappers */ |
0cd8e231 | 199 | #define HW_SCAN(X) \ |
4f840ce9 | 200 | static GSList *scan_##X(struct sr_dev_driver *di, GSList *options) { \ |
1a863916 | 201 | (void)di; return scan(options, X); } |
e07edc83 UH |
202 | #define HW_CONFIG_LIST(X) \ |
203 | static int config_list_##X(uint32_t key, GVariant **data, \ | |
204 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { \ | |
205 | return config_list(key, data, sdi, cg, X); } | |
0cd8e231 | 206 | #define HW_DEV_ACQUISITION_START(X) \ |
695dc859 UH |
207 | static int dev_acquisition_start_##X(const struct sr_dev_inst *sdi \ |
208 | ) { return dev_acquisition_start(sdi, X); } | |
0cd8e231 UH |
209 | |
210 | /* Driver structs and API function wrappers */ | |
211 | #define DRV(ID, ID_UPPER, NAME, LONGNAME) \ | |
0cd8e231 | 212 | HW_SCAN(ID_UPPER) \ |
e07edc83 | 213 | HW_CONFIG_LIST(ID_UPPER) \ |
0cd8e231 | 214 | HW_DEV_ACQUISITION_START(ID_UPPER) \ |
dd5c48a6 | 215 | static struct sr_dev_driver ID##_driver_info = { \ |
0cd8e231 UH |
216 | .name = NAME, \ |
217 | .longname = LONGNAME, \ | |
218 | .api_version = 1, \ | |
c2fdcc25 | 219 | .init = std_init, \ |
700d6b64 | 220 | .cleanup = std_cleanup, \ |
6078d2c9 | 221 | .scan = scan_##ID_UPPER, \ |
c01bf34c | 222 | .dev_list = std_dev_list, \ |
0cd8e231 UH |
223 | .config_get = NULL, \ |
224 | .config_set = config_set, \ | |
e07edc83 | 225 | .config_list = config_list_##ID_UPPER, \ |
854434de | 226 | .dev_open = std_serial_dev_open, \ |
bf2c987f | 227 | .dev_close = std_serial_dev_close, \ |
6078d2c9 | 228 | .dev_acquisition_start = dev_acquisition_start_##ID_UPPER, \ |
4b1a9d5d | 229 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, \ |
41812aca | 230 | .context = NULL, \ |
dd5c48a6 LPC |
231 | }; \ |
232 | SR_REGISTER_DEV_DRIVER(ID##_driver_info) | |
0cd8e231 | 233 | |
6f3e5335 | 234 | DRV(mic_98581, MIC_98581, "mic-98581", "MIC 98581") |
0cd8e231 | 235 | DRV(mic_98583, MIC_98583, "mic-98583", "MIC 98583") |