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