]> sigrok.org Git - libsigrok.git/blame - src/hardware/colead-slm/api.c
Build: Set local include directories in Makefile.am
[libsigrok.git] / src / hardware / colead-slm / api.c
CommitLineData
4d729ddc
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <glib.h>
a8d09e13
BV
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
a8d09e13 24#include <string.h>
c1aae900 25#include <libsigrok/libsigrok.h>
515ab088
UH
26#include "libsigrok-internal.h"
27#include "protocol.h"
a8d09e13 28
f306ca61
BV
29/* The Colead SL-5868P uses this. */
30#define SERIALCOMM "2400/8n1"
31
a0e0bb41 32static const uint32_t scanopts[] = {
1953564a
BV
33 SR_CONF_CONN,
34 SR_CONF_SERIALCOMM,
a8d09e13
BV
35};
36
f254bc4b 37static const uint32_t devopts[] = {
1953564a 38 SR_CONF_SOUNDLEVELMETER,
1953564a 39 SR_CONF_CONTINUOUS,
5827f61b
BV
40 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
41 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
a8d09e13
BV
42};
43
4d729ddc 44SR_PRIV struct sr_dev_driver colead_slm_driver_info;
4d729ddc 45
4f840ce9 46static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
4d729ddc 47{
f6beaac5 48 return std_init(sr_ctx, di, LOG_PREFIX);
4d729ddc
BV
49}
50
4f840ce9 51static GSList *scan(struct sr_dev_driver *di, GSList *options)
4d729ddc
BV
52{
53 struct drv_context *drvc;
a8d09e13
BV
54 struct dev_context *devc;
55 struct sr_dev_inst *sdi;
1987b8d6 56 struct sr_config *src;
a8d09e13
BV
57 GSList *devices, *l;
58 const char *conn, *serialcomm;
4d729ddc 59
41812aca 60 drvc = di->context;
4d729ddc
BV
61 drvc->instances = NULL;
62
4b97c74e
UH
63 devices = NULL;
64
a8d09e13
BV
65 conn = serialcomm = NULL;
66 for (l = options; l; l = l->next) {
1987b8d6
BV
67 src = l->data;
68 switch (src->key) {
1953564a 69 case SR_CONF_CONN:
dccda194 70 conn = g_variant_get_string(src->data, NULL);
a8d09e13 71 break;
1953564a 72 case SR_CONF_SERIALCOMM:
dccda194 73 serialcomm = g_variant_get_string(src->data, NULL);
a8d09e13
BV
74 break;
75 }
76 }
77 if (!conn)
78 return NULL;
f306ca61
BV
79 if (!serialcomm)
80 serialcomm = SERIALCOMM;
a8d09e13 81
aac29cc1 82 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
83 sdi->status = SR_ST_INACTIVE;
84 sdi->vendor = g_strdup("Colead");
85 sdi->model = g_strdup("SL-5868P");
f57d8ffe 86 devc = g_malloc0(sizeof(struct dev_context));
91219afc 87 sdi->conn = sr_serial_dev_inst_new(conn, serialcomm);
aa706635 88 sdi->inst_type = SR_INST_SERIAL;
a8d09e13
BV
89 sdi->priv = devc;
90 sdi->driver = di;
5e23fcab 91 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
a8d09e13
BV
92 drvc->instances = g_slist_append(drvc->instances, sdi);
93 devices = g_slist_append(devices, sdi);
4d729ddc
BV
94
95 return devices;
96}
97
4f840ce9 98static GSList *dev_list(const struct sr_dev_driver *di)
4d729ddc 99{
41812aca 100 return ((struct drv_context *)(di->context))->instances;
4d729ddc
BV
101}
102
6078d2c9 103static int dev_open(struct sr_dev_inst *sdi)
4d729ddc 104{
aa706635 105 struct sr_serial_dev_inst *serial;
a8d09e13 106
aa706635
BV
107 serial = sdi->conn;
108 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
a8d09e13 109 return SR_ERR;
f306ca61 110
a8d09e13 111 sdi->status = SR_ST_ACTIVE;
4d729ddc
BV
112
113 return SR_OK;
114}
115
4f840ce9 116static int cleanup(const struct sr_dev_driver *di)
4d729ddc 117{
a6630742 118 return std_dev_clear(di, NULL);
4d729ddc
BV
119}
120
584560f1 121static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 122 const struct sr_channel_group *cg)
4d729ddc 123{
a8d09e13 124 struct dev_context *devc;
4d729ddc 125
53b4680f 126 (void)cg;
8f996b89 127
a8d09e13 128 if (sdi->status != SR_ST_ACTIVE)
e73ffd42 129 return SR_ERR_DEV_CLOSED;
a8d09e13
BV
130
131 if (!(devc = sdi->priv)) {
132 sr_err("sdi->priv was NULL.");
133 return SR_ERR_BUG;
4d729ddc
BV
134 }
135
584560f1 136 switch (key) {
1953564a 137 case SR_CONF_LIMIT_MSEC:
a8d09e13 138 /* TODO: not yet implemented */
dccda194 139 devc->limit_msec = g_variant_get_uint64(data);;
a8d09e13 140 break;
1953564a 141 case SR_CONF_LIMIT_SAMPLES:
dccda194 142 devc->limit_samples = g_variant_get_uint64(data);
a8d09e13 143 break;
4d729ddc 144 default:
bd6fbf62 145 return SR_ERR_NA;
4d729ddc
BV
146 }
147
a8d09e13 148 return SR_OK;
4d729ddc
BV
149}
150
584560f1 151static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 152 const struct sr_channel_group *cg)
a1c743fc 153{
a1c743fc 154 (void)sdi;
53b4680f 155 (void)cg;
a1c743fc
BV
156
157 switch (key) {
0d485e30 158 case SR_CONF_SCAN_OPTIONS:
584560f1 159 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
a0e0bb41 160 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
0d485e30 161 break;
9a6517d1 162 case SR_CONF_DEVICE_OPTIONS:
584560f1 163 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
f254bc4b 164 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
9a6517d1 165 break;
a1c743fc 166 default:
bd6fbf62 167 return SR_ERR_NA;
a1c743fc
BV
168 }
169
170 return SR_OK;
171}
172
6078d2c9 173static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
4d729ddc 174{
a8d09e13 175 struct dev_context *devc;
aa706635 176 struct sr_serial_dev_inst *serial;
a8d09e13 177
e73ffd42
BV
178 if (sdi->status != SR_ST_ACTIVE)
179 return SR_ERR_DEV_CLOSED;
180
a8d09e13
BV
181 if (!(devc = sdi->priv)) {
182 sr_err("sdi->priv was NULL.");
183 return SR_ERR_BUG;
184 }
185
a8d09e13
BV
186 devc->cb_data = cb_data;
187
188 /* Send header packet to the session bus. */
29a27196 189 std_session_send_df_header(cb_data, LOG_PREFIX);
a8d09e13 190
a8d09e13 191 /* Poll every 150ms, or whenever some data comes in. */
aa706635 192 serial = sdi->conn;
102f1239
BV
193 serial_source_add(sdi->session, serial, G_IO_IN, 150,
194 colead_slm_receive_data, (void *)sdi);
4d729ddc
BV
195
196 return SR_OK;
197}
198
6078d2c9 199static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
4d729ddc 200{
d43b0908 201 return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close,
f6beaac5 202 sdi->conn, LOG_PREFIX);
4d729ddc
BV
203}
204
205SR_PRIV struct sr_dev_driver colead_slm_driver_info = {
206 .name = "colead-slm",
207 .longname = "Colead SLM",
208 .api_version = 1,
6078d2c9
UH
209 .init = init,
210 .cleanup = cleanup,
211 .scan = scan,
212 .dev_list = dev_list,
a6630742 213 .dev_clear = NULL,
6fab7b8f 214 .config_get = NULL,
035a1078 215 .config_set = config_set,
a1c743fc 216 .config_list = config_list,
6078d2c9 217 .dev_open = dev_open,
bf2c987f 218 .dev_close = std_serial_dev_close,
6078d2c9
UH
219 .dev_acquisition_start = dev_acquisition_start,
220 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 221 .context = NULL,
4d729ddc 222};