]> sigrok.org Git - libsigrok.git/blame - src/hardware/ipdbg-la/api.c
ipdbg-la: Use std_init() and std_dev_list().
[libsigrok.git] / src / hardware / ipdbg-la / api.c
CommitLineData
a4210e18
EK
1/*
2 * This file is part of the libsigrok project.
3 *
932ef10f 4 * Copyright (C) 2016 Eva Kissling <eva.kissling@bluewin.ch>
a4210e18
EK
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 <config.h>
21#include "protocol.h"
22
1f15efc1 23static const uint32_t drvopts[] = {
9d2e5483 24 SR_CONF_LOGIC_ANALYZER,
a4210e18
EK
25};
26
1f15efc1 27static const uint32_t scanopts[] = {
9d2e5483 28 SR_CONF_CONN,
a4210e18
EK
29};
30
1f15efc1 31static const uint32_t devopts[] = {
1f9652a8 32 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
9d2e5483 33 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
1f9652a8 34 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
a4210e18
EK
35};
36
1f15efc1 37static const int32_t trigger_matches[] = {
9d2e5483
SA
38 SR_TRIGGER_ZERO,
39 SR_TRIGGER_ONE,
40 SR_TRIGGER_RISING,
41 SR_TRIGGER_FALLING,
42 SR_TRIGGER_EDGE,
a4210e18
EK
43};
44
45SR_PRIV struct sr_dev_driver ipdbg_la_driver_info;
46
8a9788e2 47static void ipdbg_la_split_addr_port(const char *conn, char **addr,
9d2e5483 48 char **port)
a4210e18 49{
9d2e5483 50 char **strs = g_strsplit(conn, "/", 3);
a4210e18 51
9d2e5483
SA
52 *addr = g_strdup(strs[1]);
53 *port = g_strdup(strs[2]);
a4210e18 54
9d2e5483 55 g_strfreev(strs);
a4210e18
EK
56}
57
58static GSList *scan(struct sr_dev_driver *di, GSList *options)
59{
9d2e5483
SA
60 struct drv_context *drvc;
61 GSList *devices;
a4210e18 62
9d2e5483
SA
63 devices = NULL;
64 drvc = di->context;
65 drvc->instances = NULL;
66 const char *conn;
67 struct sr_config *src;
68 GSList *l;
a4210e18 69
9d2e5483
SA
70 conn = NULL;
71 for (l = options; l; l = l->next) {
72 src = l->data;
73 switch (src->key) {
74 case SR_CONF_CONN:
75 conn = g_variant_get_string(src->data, NULL);
76 break;
77 }
78 }
a4210e18 79
9d2e5483
SA
80 if (!conn)
81 return NULL;
a4210e18 82
8a9788e2 83 struct ipdbg_la_tcp *tcp = ipdbg_la_tcp_new();
a4210e18 84
8a9788e2 85 ipdbg_la_split_addr_port(conn, &tcp->address, &tcp->port);
a4210e18 86
9d2e5483
SA
87 if (!tcp->address)
88 return NULL;
a4210e18 89
8a9788e2 90 if (ipdbg_la_tcp_open(tcp) != SR_OK)
9d2e5483 91 return NULL;
a4210e18 92
8a9788e2
UH
93 ipdbg_la_send_reset(tcp);
94 ipdbg_la_send_reset(tcp);
ed186484 95
8a9788e2 96 if (ipdbg_la_request_id(tcp) != SR_OK)
ed186484 97 return NULL;
a4210e18 98
9d2e5483 99 struct sr_dev_inst *sdi = g_malloc0(sizeof(struct sr_dev_inst));
9d2e5483
SA
100 sdi->status = SR_ST_INACTIVE;
101 sdi->vendor = g_strdup("ipdbg.org");
8a9788e2 102 sdi->model = g_strdup("IPDBG LA");
9d2e5483
SA
103 sdi->version = g_strdup("v1.0");
104 sdi->driver = di;
a4210e18 105
1f15efc1 106 struct dev_context *devc = ipdbg_la_dev_new();
9d2e5483 107 sdi->priv = devc;
a4210e18 108
8a9788e2 109 ipdbg_la_get_addrwidth_and_datawidth(tcp, devc);
a4210e18 110
9d2e5483
SA
111 sr_dbg("addr_width = %d, data_width = %d\n", devc->ADDR_WIDTH,
112 devc->DATA_WIDTH);
113 sr_dbg("limit samples = %" PRIu64 "\n", devc->limit_samples_max);
7f4c9a04 114
9d2e5483
SA
115 for (uint32_t i = 0; i < devc->DATA_WIDTH; i++) {
116 const uint8_t buf_size = 16;
117 char buf[buf_size];
118 snprintf(buf, buf_size, "ch%d", i);
119 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, buf);
120 }
a4210e18 121
9d2e5483
SA
122 sdi->inst_type = SR_INST_USER;
123 sdi->conn = tcp;
a4210e18 124
8a9788e2 125 ipdbg_la_tcp_close(tcp);
a4210e18 126
9d2e5483 127 devices = g_slist_append(devices, sdi);
750303aa 128
9d2e5483 129 return std_scan_complete(di, devices);
a4210e18
EK
130}
131
132static int dev_clear(const struct sr_dev_driver *di)
133{
9d2e5483
SA
134 struct drv_context *drvc = di->context;
135 struct sr_dev_inst *sdi;
136 GSList *l;
137
138 if (drvc) {
139 for (l = drvc->instances; l; l = l->next) {
140 sdi = l->data;
8a9788e2 141 struct ipdbg_la_tcp *tcp = sdi->conn;
9d2e5483 142 if (tcp) {
8a9788e2
UH
143 ipdbg_la_tcp_close(tcp);
144 ipdbg_la_tcp_free(tcp);
9d2e5483
SA
145 g_free(tcp);
146 }
147 sdi->conn = NULL;
148 }
149 }
150
151 return std_dev_clear(di);
a4210e18
EK
152}
153
154static int dev_open(struct sr_dev_inst *sdi)
155{
8a9788e2 156 struct ipdbg_la_tcp *tcp = sdi->conn;
a4210e18 157
9d2e5483
SA
158 if (!tcp)
159 return SR_ERR;
a4210e18 160
8a9788e2 161 if (ipdbg_la_tcp_open(tcp) != SR_OK)
9d2e5483 162 return SR_ERR;
a4210e18 163
9d2e5483 164 return SR_OK;
a4210e18
EK
165}
166
167static int dev_close(struct sr_dev_inst *sdi)
168{
13ac501a 169 /* Should be called before a new call to scan(). */
8a9788e2 170 struct ipdbg_la_tcp *tcp = sdi->conn;
38e7493d 171
9d2e5483 172 if (tcp)
8a9788e2 173 ipdbg_la_tcp_close(tcp);
a4210e18 174
9d2e5483 175 sdi->conn = NULL;
a4210e18 176
9d2e5483 177 return SR_OK;
a4210e18
EK
178}
179
180static int config_get(uint32_t key, GVariant **data,
9d2e5483 181 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
a4210e18 182{
77b6b98d 183 struct dev_context *devc = sdi->priv;
a4210e18 184
9d2e5483 185 (void)cg;
a4210e18 186
9d2e5483
SA
187 switch (key) {
188 case SR_CONF_CAPTURE_RATIO:
189 *data = g_variant_new_uint64(devc->capture_ratio);
190 break;
191 case SR_CONF_LIMIT_SAMPLES:
192 *data = g_variant_new_uint64(devc->limit_samples);
193 break;
194 default:
77b6b98d 195 return SR_ERR_NA;
9d2e5483 196 }
a4210e18 197
77b6b98d 198 return SR_OK;
a4210e18
EK
199}
200
201static int config_set(uint32_t key, GVariant *data,
9d2e5483 202 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
a4210e18 203{
77b6b98d 204 struct dev_context *devc = sdi->priv;
9d2e5483
SA
205
206 (void)cg;
207
9d2e5483
SA
208 switch (key) {
209 case SR_CONF_CAPTURE_RATIO:
77b6b98d 210 devc->capture_ratio = g_variant_get_uint64(data);
9d2e5483
SA
211 break;
212 case SR_CONF_LIMIT_SAMPLES:
77b6b98d 213 devc->limit_samples = g_variant_get_uint64(data);
9d2e5483
SA
214 break;
215 default:
77b6b98d 216 return SR_ERR_NA;
9d2e5483
SA
217 }
218
77b6b98d 219 return SR_OK;
a4210e18
EK
220}
221
222static int config_list(uint32_t key, GVariant **data,
9d2e5483 223 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
a4210e18 224{
9d2e5483
SA
225 switch (key) {
226 case SR_CONF_SCAN_OPTIONS:
9d2e5483 227 case SR_CONF_DEVICE_OPTIONS:
77b6b98d 228 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
9d2e5483 229 case SR_CONF_TRIGGER_MATCH:
77b6b98d 230 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
9d2e5483
SA
231 break;
232 default:
233 return SR_ERR_NA;
234 }
235
236 return SR_OK;
a4210e18
EK
237}
238
a4210e18
EK
239static int dev_acquisition_start(const struct sr_dev_inst *sdi)
240{
8a9788e2 241 struct ipdbg_la_tcp *tcp = sdi->conn;
1f15efc1 242 struct dev_context *devc = sdi->priv;
a4210e18 243
8a9788e2
UH
244 ipdbg_la_convert_trigger(sdi);
245 ipdbg_la_send_trigger(devc, tcp);
246 ipdbg_la_send_delay(devc, tcp);
a4210e18 247
9d2e5483
SA
248 /* If the device stops sending for longer than it takes to send a byte,
249 * that means it's finished. But wait at least 100 ms to be safe.
250 */
251 sr_session_source_add(sdi->session, tcp->socket, G_IO_IN, 100,
8a9788e2 252 ipdbg_la_receive_data, (struct sr_dev_inst *)sdi);
a4210e18 253
8a9788e2 254 ipdbg_la_send_start(tcp);
a4210e18 255
9d2e5483 256 return SR_OK;
a4210e18
EK
257}
258
259static int dev_acquisition_stop(struct sr_dev_inst *sdi)
260{
8a9788e2 261 struct ipdbg_la_tcp *tcp = sdi->conn;
1f15efc1 262 struct dev_context *devc = sdi->priv;
a4210e18 263
9d2e5483 264 uint8_t byte;
b51288e3 265
9d2e5483
SA
266 if (devc->num_transfers > 0) {
267 while (devc->num_transfers <
268 (devc->limit_samples_max * devc->DATA_WIDTH_BYTES)) {
8a9788e2 269 ipdbg_la_tcp_receive(tcp, &byte);
9d2e5483
SA
270 devc->num_transfers++;
271 }
272 }
a4210e18 273
8a9788e2
UH
274 ipdbg_la_send_reset(tcp);
275 ipdbg_la_abort_acquisition(sdi);
a4210e18 276
9d2e5483 277 return SR_OK;
a4210e18
EK
278}
279
280SR_PRIV struct sr_dev_driver ipdbg_la_driver_info = {
8a9788e2
UH
281 .name = "ipdbg-la",
282 .longname = "IPDBG LA",
9d2e5483 283 .api_version = 1,
3831eaf9 284 .init = std_init,
9d2e5483
SA
285 .cleanup = std_cleanup,
286 .scan = scan,
3831eaf9 287 .dev_list = std_dev_list,
9d2e5483
SA
288 .dev_clear = dev_clear,
289 .config_get = config_get,
290 .config_set = config_set,
291 .config_list = config_list,
292 .dev_open = dev_open,
293 .dev_close = dev_close,
294 .dev_acquisition_start = dev_acquisition_start,
295 .dev_acquisition_stop = dev_acquisition_stop,
296 .context = NULL,
a4210e18
EK
297};
298
299SR_REGISTER_DEV_DRIVER(ipdbg_la_driver_info);