]> sigrok.org Git - libsigrok.git/blob - src/hardware/mic-985xx/api.c
8e1bba8d780913d43db06716628f263563054cf0
[libsigrok.git] / src / hardware / mic-985xx / api.c
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 | SR_CONF_SET,
40         SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
41         SR_CONF_LIMIT_MSEC | SR_CONF_SET,
42 };
43
44 SR_PRIV struct sr_dev_driver mic_98581_driver_info;
45 SR_PRIV 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 int init(struct sr_context *sr_ctx, int idx)
61 {
62         return std_init(sr_ctx, mic_devs[idx].di, LOG_PREFIX);
63 }
64
65 static GSList *mic_scan(const char *conn, const char *serialcomm, int idx)
66 {
67         struct sr_dev_inst *sdi;
68         struct drv_context *drvc;
69         struct dev_context *devc;
70         struct sr_serial_dev_inst *serial;
71         GSList *devices;
72
73         serial = sr_serial_dev_inst_new(conn, serialcomm);
74
75         if (serial_open(serial, SERIAL_RDWR) != SR_OK)
76                 return NULL;
77
78         drvc = mic_devs[idx].di->context;
79         devices = NULL;
80         serial_flush(serial);
81
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. */
88         sdi = g_malloc0(sizeof(struct sr_dev_inst));
89         sdi->status = SR_ST_INACTIVE;
90         sdi->vendor = g_strdup(mic_devs[idx].vendor);
91         sdi->model = g_strdup(mic_devs[idx].device);
92         devc = g_malloc0(sizeof(struct dev_context));
93         sdi->inst_type = SR_INST_SERIAL;
94         sdi->conn = serial;
95         sdi->priv = devc;
96         sdi->driver = mic_devs[idx].di;
97
98         sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "Temperature");
99
100         if (mic_devs[idx].has_humidity)
101                 sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "Humidity");
102
103         drvc->instances = g_slist_append(drvc->instances, sdi);
104         devices = g_slist_append(devices, sdi);
105
106         serial_close(serial);
107
108         return devices;
109 }
110
111 static GSList *scan(GSList *options, int idx)
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:
122                         conn = g_variant_get_string(src->data, NULL);
123                         break;
124                 case SR_CONF_SERIALCOMM:
125                         serialcomm = g_variant_get_string(src->data, NULL);
126                         break;
127                 }
128         }
129         if (!conn)
130                 return NULL;
131
132         if (serialcomm) {
133                 /* Use the provided comm specs. */
134                 devices = mic_scan(conn, serialcomm, idx);
135         } else {
136                 /* Try the default. */
137                 devices = mic_scan(conn, mic_devs[idx].conn, idx);
138         }
139
140         return devices;
141 }
142
143 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
144                 const struct sr_channel_group *cg)
145 {
146         struct dev_context *devc;
147
148         (void)cg;
149
150         if (sdi->status != SR_ST_ACTIVE)
151                 return SR_ERR_DEV_CLOSED;
152
153         devc = sdi->priv;
154
155         switch (key) {
156         case SR_CONF_LIMIT_SAMPLES:
157                 devc->limit_samples = g_variant_get_uint64(data);
158                 break;
159         case SR_CONF_LIMIT_MSEC:
160                 devc->limit_msec = g_variant_get_uint64(data);
161                 break;
162         default:
163                 return SR_ERR_NA;
164         }
165
166         return SR_OK;
167 }
168
169 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
170                 const struct sr_channel_group *cg, int idx)
171 {
172         (void)cg;
173
174         switch (key) {
175         case SR_CONF_SCAN_OPTIONS:
176                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
177                                 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
178                 break;
179         case SR_CONF_DEVICE_OPTIONS:
180                 if (!sdi && !mic_devs[idx].has_humidity) {
181                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
182                                 drvopts_temp, ARRAY_SIZE(drvopts_temp),
183                                 sizeof(uint32_t));
184                 } else if (!sdi && mic_devs[idx].has_humidity) {
185                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
186                                 drvopts_temp_hum, ARRAY_SIZE(drvopts_temp_hum),
187                                 sizeof(uint32_t));
188                 } else {
189                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
190                                 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
191                 }
192                 break;
193         default:
194                 return SR_ERR_NA;
195         }
196
197         return SR_OK;
198 }
199
200 static int dev_acquisition_start(const struct sr_dev_inst *sdi, int idx)
201 {
202         struct dev_context *devc;
203         struct sr_serial_dev_inst *serial;
204
205         if (sdi->status != SR_ST_ACTIVE)
206                 return SR_ERR_DEV_CLOSED;
207
208         devc = sdi->priv;
209         devc->num_samples = 0;
210         devc->starttime = g_get_monotonic_time();
211
212         std_session_send_df_header(sdi, LOG_PREFIX);
213
214         /* Poll every 100ms, or whenever some data comes in. */
215         serial = sdi->conn;
216         serial_source_add(sdi->session, serial, G_IO_IN, 100,
217                       mic_devs[idx].receive_data, (void *)sdi);
218
219         return SR_OK;
220 }
221
222 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
223 {
224         return std_serial_dev_acquisition_stop(sdi, std_serial_dev_close,
225                         sdi->conn, LOG_PREFIX);
226 }
227
228 /* Driver-specific API function wrappers */
229 #define HW_INIT(X) \
230 static int init_##X(struct sr_dev_driver *di, struct sr_context *sr_ctx) { \
231         (void)di; return init(sr_ctx, X); }
232 #define HW_SCAN(X) \
233 static GSList *scan_##X(struct sr_dev_driver *di, GSList *options) { \
234         (void)di; return scan(options, X); }
235 #define HW_CONFIG_LIST(X) \
236 static int config_list_##X(uint32_t key, GVariant **data, \
237 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { \
238 return config_list(key, data, sdi, cg, X); }
239 #define HW_DEV_ACQUISITION_START(X) \
240 static int dev_acquisition_start_##X(const struct sr_dev_inst *sdi \
241 ) { return dev_acquisition_start(sdi, X); }
242
243 /* Driver structs and API function wrappers */
244 #define DRV(ID, ID_UPPER, NAME, LONGNAME) \
245 HW_INIT(ID_UPPER) \
246 HW_SCAN(ID_UPPER) \
247 HW_CONFIG_LIST(ID_UPPER) \
248 HW_DEV_ACQUISITION_START(ID_UPPER) \
249 SR_PRIV struct sr_dev_driver ID##_driver_info = { \
250         .name = NAME, \
251         .longname = LONGNAME, \
252         .api_version = 1, \
253         .init = init_##ID_UPPER, \
254         .cleanup = std_cleanup, \
255         .scan = scan_##ID_UPPER, \
256         .dev_list = std_dev_list, \
257         .config_get = NULL, \
258         .config_set = config_set, \
259         .config_list = config_list_##ID_UPPER, \
260         .dev_open = std_serial_dev_open, \
261         .dev_close = std_serial_dev_close, \
262         .dev_acquisition_start = dev_acquisition_start_##ID_UPPER, \
263         .dev_acquisition_stop = dev_acquisition_stop, \
264         .context = NULL, \
265 };
266
267 DRV(mic_98581, MIC_98581, "mic-98581", "MIC 98581")
268 DRV(mic_98583, MIC_98583, "mic-98583", "MIC 98583")