]> sigrok.org Git - libsigrok.git/blame - src/hardware/ipdbg-la/api.c
ipdbg-la: Fix devopts[] contents.
[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
SA
99 struct sr_dev_inst *sdi = g_malloc0(sizeof(struct sr_dev_inst));
100 if (!sdi) {
101 sr_err("no possible to allocate sr_dev_inst");
102 return NULL;
103 }
7f4c9a04 104
9d2e5483
SA
105 sdi->status = SR_ST_INACTIVE;
106 sdi->vendor = g_strdup("ipdbg.org");
8a9788e2 107 sdi->model = g_strdup("IPDBG LA");
9d2e5483
SA
108 sdi->version = g_strdup("v1.0");
109 sdi->driver = di;
a4210e18 110
1f15efc1 111 struct dev_context *devc = ipdbg_la_dev_new();
9d2e5483 112 sdi->priv = devc;
a4210e18 113
8a9788e2 114 ipdbg_la_get_addrwidth_and_datawidth(tcp, devc);
a4210e18 115
9d2e5483
SA
116 sr_dbg("addr_width = %d, data_width = %d\n", devc->ADDR_WIDTH,
117 devc->DATA_WIDTH);
118 sr_dbg("limit samples = %" PRIu64 "\n", devc->limit_samples_max);
7f4c9a04 119
9d2e5483
SA
120 for (uint32_t i = 0; i < devc->DATA_WIDTH; i++) {
121 const uint8_t buf_size = 16;
122 char buf[buf_size];
123 snprintf(buf, buf_size, "ch%d", i);
124 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, buf);
125 }
a4210e18 126
9d2e5483
SA
127 sdi->inst_type = SR_INST_USER;
128 sdi->conn = tcp;
a4210e18 129
8a9788e2 130 ipdbg_la_tcp_close(tcp);
a4210e18 131
9d2e5483 132 devices = g_slist_append(devices, sdi);
750303aa 133
9d2e5483 134 return std_scan_complete(di, devices);
a4210e18
EK
135}
136
137static int dev_clear(const struct sr_dev_driver *di)
138{
9d2e5483
SA
139 struct drv_context *drvc = di->context;
140 struct sr_dev_inst *sdi;
141 GSList *l;
142
143 if (drvc) {
144 for (l = drvc->instances; l; l = l->next) {
145 sdi = l->data;
8a9788e2 146 struct ipdbg_la_tcp *tcp = sdi->conn;
9d2e5483 147 if (tcp) {
8a9788e2
UH
148 ipdbg_la_tcp_close(tcp);
149 ipdbg_la_tcp_free(tcp);
9d2e5483
SA
150 g_free(tcp);
151 }
152 sdi->conn = NULL;
153 }
154 }
155
156 return std_dev_clear(di);
a4210e18
EK
157}
158
159static int dev_open(struct sr_dev_inst *sdi)
160{
9d2e5483 161 sdi->status = SR_ST_INACTIVE;
a4210e18 162
8a9788e2 163 struct ipdbg_la_tcp *tcp = sdi->conn;
a4210e18 164
9d2e5483
SA
165 if (!tcp)
166 return SR_ERR;
a4210e18 167
8a9788e2 168 if (ipdbg_la_tcp_open(tcp) != SR_OK)
9d2e5483 169 return SR_ERR;
a4210e18 170
9d2e5483 171 sdi->status = SR_ST_ACTIVE;
a4210e18 172
9d2e5483 173 return SR_OK;
a4210e18
EK
174}
175
176static int dev_close(struct sr_dev_inst *sdi)
177{
9d2e5483 178 // Should be called before a new call to scan()
8a9788e2 179 struct ipdbg_la_tcp *tcp = sdi->conn;
38e7493d 180
9d2e5483 181 if (tcp)
8a9788e2 182 ipdbg_la_tcp_close(tcp);
a4210e18 183
9d2e5483
SA
184 sdi->conn = NULL;
185 sdi->status = SR_ST_INACTIVE;
a4210e18 186
9d2e5483 187 return SR_OK;
a4210e18
EK
188}
189
190static int config_get(uint32_t key, GVariant **data,
9d2e5483 191 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
a4210e18 192{
9d2e5483 193 int ret = SR_OK;
a4210e18 194
9d2e5483 195 (void)cg;
a4210e18 196
1f15efc1 197 struct dev_context *devc = sdi->priv;
a4210e18 198
9d2e5483
SA
199 switch (key) {
200 case SR_CONF_CAPTURE_RATIO:
201 *data = g_variant_new_uint64(devc->capture_ratio);
202 break;
203 case SR_CONF_LIMIT_SAMPLES:
204 *data = g_variant_new_uint64(devc->limit_samples);
205 break;
206 default:
207 ret = SR_ERR_NA;
208 }
a4210e18 209
9d2e5483 210 return ret;
a4210e18
EK
211}
212
213static int config_set(uint32_t key, GVariant *data,
9d2e5483 214 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
a4210e18 215{
9d2e5483
SA
216 int ret = SR_OK;
217 uint64_t value;
218
219 (void)cg;
220
221 if (sdi->status != SR_ST_ACTIVE)
222 return SR_ERR_DEV_CLOSED;
223
1f15efc1 224 struct dev_context *devc = sdi->priv;
9d2e5483
SA
225
226 switch (key) {
227 case SR_CONF_CAPTURE_RATIO:
228 value = g_variant_get_uint64(data);
229 if (value <= 100)
230 devc->capture_ratio = value;
231 else
232 ret = SR_ERR;
233 break;
234 case SR_CONF_LIMIT_SAMPLES:
235 value = g_variant_get_uint64(data);
236 if (value <= devc->limit_samples_max)
237 devc->limit_samples = value;
238 else
239 ret = SR_ERR;
240 break;
241 default:
242 ret = SR_ERR_NA;
243 }
244
245 return ret;
a4210e18
EK
246}
247
248static int config_list(uint32_t key, GVariant **data,
9d2e5483 249 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
a4210e18 250{
9d2e5483
SA
251 (void)cg;
252
253 switch (key) {
254 case SR_CONF_SCAN_OPTIONS:
255 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
1f15efc1 256 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
9d2e5483
SA
257 break;
258 case SR_CONF_DEVICE_OPTIONS:
259 if (!sdi)
260 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
1f15efc1 261 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
9d2e5483
SA
262 else
263 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
1f15efc1 264 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
9d2e5483
SA
265 break;
266 case SR_CONF_TRIGGER_MATCH:
267 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
1f15efc1 268 trigger_matches, ARRAY_SIZE(trigger_matches), sizeof(int32_t));
9d2e5483
SA
269 break;
270 default:
271 return SR_ERR_NA;
272 }
273
274 return SR_OK;
a4210e18
EK
275}
276
a4210e18
EK
277static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
278{
9d2e5483 279 return std_init(di, sr_ctx);
a4210e18
EK
280}
281
a4210e18
EK
282static GSList *dev_list(const struct sr_dev_driver *di)
283{
9d2e5483 284 return ((struct drv_context *)(di->context))->instances;
a4210e18
EK
285}
286
287static int dev_acquisition_start(const struct sr_dev_inst *sdi)
288{
9d2e5483
SA
289 if (sdi->status != SR_ST_ACTIVE)
290 return SR_ERR_DEV_CLOSED;
a4210e18 291
8a9788e2 292 struct ipdbg_la_tcp *tcp = sdi->conn;
1f15efc1 293 struct dev_context *devc = sdi->priv;
a4210e18 294
8a9788e2
UH
295 ipdbg_la_convert_trigger(sdi);
296 ipdbg_la_send_trigger(devc, tcp);
297 ipdbg_la_send_delay(devc, tcp);
a4210e18 298
9d2e5483
SA
299 /* If the device stops sending for longer than it takes to send a byte,
300 * that means it's finished. But wait at least 100 ms to be safe.
301 */
302 sr_session_source_add(sdi->session, tcp->socket, G_IO_IN, 100,
8a9788e2 303 ipdbg_la_receive_data, (struct sr_dev_inst *)sdi);
a4210e18 304
8a9788e2 305 ipdbg_la_send_start(tcp);
a4210e18 306
9d2e5483 307 return SR_OK;
a4210e18
EK
308}
309
310static int dev_acquisition_stop(struct sr_dev_inst *sdi)
311{
8a9788e2 312 struct ipdbg_la_tcp *tcp = sdi->conn;
1f15efc1 313 struct dev_context *devc = sdi->priv;
a4210e18 314
9d2e5483 315 uint8_t byte;
b51288e3 316
9d2e5483
SA
317 if (devc->num_transfers > 0) {
318 while (devc->num_transfers <
319 (devc->limit_samples_max * devc->DATA_WIDTH_BYTES)) {
8a9788e2 320 ipdbg_la_tcp_receive(tcp, &byte);
9d2e5483
SA
321 devc->num_transfers++;
322 }
323 }
a4210e18 324
8a9788e2
UH
325 ipdbg_la_send_reset(tcp);
326 ipdbg_la_abort_acquisition(sdi);
a4210e18 327
9d2e5483 328 return SR_OK;
a4210e18
EK
329}
330
331SR_PRIV struct sr_dev_driver ipdbg_la_driver_info = {
8a9788e2
UH
332 .name = "ipdbg-la",
333 .longname = "IPDBG LA",
9d2e5483
SA
334 .api_version = 1,
335 .init = init,
336 .cleanup = std_cleanup,
337 .scan = scan,
338 .dev_list = dev_list,
339 .dev_clear = dev_clear,
340 .config_get = config_get,
341 .config_set = config_set,
342 .config_list = config_list,
343 .dev_open = dev_open,
344 .dev_close = dev_close,
345 .dev_acquisition_start = dev_acquisition_start,
346 .dev_acquisition_stop = dev_acquisition_stop,
347 .context = NULL,
a4210e18
EK
348};
349
350SR_REGISTER_DEV_DRIVER(ipdbg_la_driver_info);