]>
Commit | Line | Data |
---|---|---|
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 <config.h> | |
22 | #include "protocol.h" | |
23 | ||
24 | static const uint32_t scanopts[] = { | |
25 | SR_CONF_CONN, | |
26 | SR_CONF_SERIALCOMM, | |
27 | }; | |
28 | ||
29 | static const uint32_t drvopts_temp[] = { | |
30 | SR_CONF_THERMOMETER, | |
31 | }; | |
32 | ||
33 | static const uint32_t drvopts_temp_hum[] = { | |
34 | SR_CONF_THERMOMETER, | |
35 | SR_CONF_HYGROMETER, | |
36 | }; | |
37 | ||
38 | static const uint32_t devopts[] = { | |
39 | SR_CONF_CONTINUOUS, | |
40 | SR_CONF_LIMIT_SAMPLES | SR_CONF_SET, | |
41 | SR_CONF_LIMIT_MSEC | SR_CONF_SET, | |
42 | }; | |
43 | ||
44 | static struct sr_dev_driver mic_98581_driver_info; | |
45 | static struct sr_dev_driver mic_98583_driver_info; | |
46 | ||
47 | SR_PRIV const struct mic_dev_info mic_devs[] = { | |
48 | { | |
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, | |
56 | &mic_98583_driver_info, receive_data_MIC_98583, | |
57 | }, | |
58 | }; | |
59 | ||
60 | static GSList *mic_scan(const char *conn, const char *serialcomm, int idx) | |
61 | { | |
62 | struct sr_dev_inst *sdi; | |
63 | struct drv_context *drvc; | |
64 | struct dev_context *devc; | |
65 | struct sr_serial_dev_inst *serial; | |
66 | GSList *devices; | |
67 | ||
68 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
69 | ||
70 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
71 | return NULL; | |
72 | ||
73 | drvc = mic_devs[idx].di->context; | |
74 | devices = NULL; | |
75 | serial_flush(serial); | |
76 | ||
77 | /* TODO: Query device type. */ | |
78 | // ret = mic_cmd_get_device_info(serial); | |
79 | ||
80 | sr_info("Found device on port %s.", conn); | |
81 | ||
82 | /* TODO: Fill in version from protocol response. */ | |
83 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
84 | sdi->status = SR_ST_INACTIVE; | |
85 | sdi->vendor = g_strdup(mic_devs[idx].vendor); | |
86 | sdi->model = g_strdup(mic_devs[idx].device); | |
87 | devc = g_malloc0(sizeof(struct dev_context)); | |
88 | sr_sw_limits_init(&devc->limits); | |
89 | sdi->inst_type = SR_INST_SERIAL; | |
90 | sdi->conn = serial; | |
91 | sdi->priv = devc; | |
92 | sdi->driver = mic_devs[idx].di; | |
93 | ||
94 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "Temperature"); | |
95 | ||
96 | if (mic_devs[idx].has_humidity) | |
97 | sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "Humidity"); | |
98 | ||
99 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
100 | devices = g_slist_append(devices, sdi); | |
101 | ||
102 | serial_close(serial); | |
103 | ||
104 | return devices; | |
105 | } | |
106 | ||
107 | static GSList *scan(GSList *options, int idx) | |
108 | { | |
109 | struct sr_config *src; | |
110 | GSList *l, *devices; | |
111 | const char *conn, *serialcomm; | |
112 | ||
113 | conn = serialcomm = NULL; | |
114 | for (l = options; l; l = l->next) { | |
115 | src = l->data; | |
116 | switch (src->key) { | |
117 | case SR_CONF_CONN: | |
118 | conn = g_variant_get_string(src->data, NULL); | |
119 | break; | |
120 | case SR_CONF_SERIALCOMM: | |
121 | serialcomm = g_variant_get_string(src->data, NULL); | |
122 | break; | |
123 | } | |
124 | } | |
125 | if (!conn) | |
126 | return NULL; | |
127 | ||
128 | if (serialcomm) { | |
129 | /* Use the provided comm specs. */ | |
130 | devices = mic_scan(conn, serialcomm, idx); | |
131 | } else { | |
132 | /* Try the default. */ | |
133 | devices = mic_scan(conn, mic_devs[idx].conn, idx); | |
134 | } | |
135 | ||
136 | return devices; | |
137 | } | |
138 | ||
139 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
140 | const struct sr_channel_group *cg) | |
141 | { | |
142 | struct dev_context *devc; | |
143 | ||
144 | (void)cg; | |
145 | ||
146 | if (sdi->status != SR_ST_ACTIVE) | |
147 | return SR_ERR_DEV_CLOSED; | |
148 | ||
149 | devc = sdi->priv; | |
150 | ||
151 | return sr_sw_limits_config_set(&devc->limits, key, data); | |
152 | } | |
153 | ||
154 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
155 | const struct sr_channel_group *cg, int idx) | |
156 | { | |
157 | (void)cg; | |
158 | ||
159 | switch (key) { | |
160 | case SR_CONF_SCAN_OPTIONS: | |
161 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
162 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
163 | break; | |
164 | case SR_CONF_DEVICE_OPTIONS: | |
165 | if (!sdi && !mic_devs[idx].has_humidity) { | |
166 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
167 | drvopts_temp, ARRAY_SIZE(drvopts_temp), | |
168 | sizeof(uint32_t)); | |
169 | } else if (!sdi && mic_devs[idx].has_humidity) { | |
170 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
171 | drvopts_temp_hum, ARRAY_SIZE(drvopts_temp_hum), | |
172 | sizeof(uint32_t)); | |
173 | } else { | |
174 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
175 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
176 | } | |
177 | break; | |
178 | default: | |
179 | return SR_ERR_NA; | |
180 | } | |
181 | ||
182 | return SR_OK; | |
183 | } | |
184 | ||
185 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, int idx) | |
186 | { | |
187 | struct dev_context *devc; | |
188 | struct sr_serial_dev_inst *serial; | |
189 | ||
190 | if (sdi->status != SR_ST_ACTIVE) | |
191 | return SR_ERR_DEV_CLOSED; | |
192 | ||
193 | devc = sdi->priv; | |
194 | ||
195 | sr_sw_limits_acquisition_start(&devc->limits); | |
196 | std_session_send_df_header(sdi, LOG_PREFIX); | |
197 | ||
198 | /* Poll every 100ms, or whenever some data comes in. */ | |
199 | serial = sdi->conn; | |
200 | serial_source_add(sdi->session, serial, G_IO_IN, 100, | |
201 | mic_devs[idx].receive_data, (void *)sdi); | |
202 | ||
203 | return SR_OK; | |
204 | } | |
205 | ||
206 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
207 | { | |
208 | return std_serial_dev_acquisition_stop(sdi, std_serial_dev_close, | |
209 | sdi->conn, LOG_PREFIX); | |
210 | } | |
211 | ||
212 | /* Driver-specific API function wrappers */ | |
213 | #define HW_SCAN(X) \ | |
214 | static GSList *scan_##X(struct sr_dev_driver *di, GSList *options) { \ | |
215 | (void)di; return scan(options, X); } | |
216 | #define HW_CONFIG_LIST(X) \ | |
217 | static int config_list_##X(uint32_t key, GVariant **data, \ | |
218 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { \ | |
219 | return config_list(key, data, sdi, cg, X); } | |
220 | #define HW_DEV_ACQUISITION_START(X) \ | |
221 | static int dev_acquisition_start_##X(const struct sr_dev_inst *sdi \ | |
222 | ) { return dev_acquisition_start(sdi, X); } | |
223 | ||
224 | /* Driver structs and API function wrappers */ | |
225 | #define DRV(ID, ID_UPPER, NAME, LONGNAME) \ | |
226 | HW_SCAN(ID_UPPER) \ | |
227 | HW_CONFIG_LIST(ID_UPPER) \ | |
228 | HW_DEV_ACQUISITION_START(ID_UPPER) \ | |
229 | static struct sr_dev_driver ID##_driver_info = { \ | |
230 | .name = NAME, \ | |
231 | .longname = LONGNAME, \ | |
232 | .api_version = 1, \ | |
233 | .init = std_init, \ | |
234 | .cleanup = std_cleanup, \ | |
235 | .scan = scan_##ID_UPPER, \ | |
236 | .dev_list = std_dev_list, \ | |
237 | .config_get = NULL, \ | |
238 | .config_set = config_set, \ | |
239 | .config_list = config_list_##ID_UPPER, \ | |
240 | .dev_open = std_serial_dev_open, \ | |
241 | .dev_close = std_serial_dev_close, \ | |
242 | .dev_acquisition_start = dev_acquisition_start_##ID_UPPER, \ | |
243 | .dev_acquisition_stop = dev_acquisition_stop, \ | |
244 | .context = NULL, \ | |
245 | }; \ | |
246 | SR_REGISTER_DEV_DRIVER(ID##_driver_info) | |
247 | ||
248 | DRV(mic_98581, MIC_98581, "mic-98581", "MIC 98581") | |
249 | DRV(mic_98583, MIC_98583, "mic-98583", "MIC 98583") |