]> sigrok.org Git - libsigrok.git/blame - src/hardware/ipdbg-la/api.c
ipdbg-la: scan(): Use g_strdup_printf().
[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
ac3625be
UH
111 sr_dbg("addr_width = %d, data_width = %d\n", devc->addr_width,
112 devc->data_width);
9d2e5483 113 sr_dbg("limit samples = %" PRIu64 "\n", devc->limit_samples_max);
7f4c9a04 114
ac3625be 115 for (uint32_t i = 0; i < devc->data_width; i++) {
8bc8e909
UH
116 char *name = g_strdup_printf("CH%d", i);
117 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
118 g_free(name);
9d2e5483 119 }
a4210e18 120
9d2e5483
SA
121 sdi->inst_type = SR_INST_USER;
122 sdi->conn = tcp;
a4210e18 123
8a9788e2 124 ipdbg_la_tcp_close(tcp);
a4210e18 125
9d2e5483 126 devices = g_slist_append(devices, sdi);
750303aa 127
9d2e5483 128 return std_scan_complete(di, devices);
a4210e18
EK
129}
130
131static int dev_clear(const struct sr_dev_driver *di)
132{
9d2e5483
SA
133 struct drv_context *drvc = di->context;
134 struct sr_dev_inst *sdi;
135 GSList *l;
136
137 if (drvc) {
138 for (l = drvc->instances; l; l = l->next) {
139 sdi = l->data;
8a9788e2 140 struct ipdbg_la_tcp *tcp = sdi->conn;
9d2e5483 141 if (tcp) {
8a9788e2
UH
142 ipdbg_la_tcp_close(tcp);
143 ipdbg_la_tcp_free(tcp);
9d2e5483
SA
144 g_free(tcp);
145 }
146 sdi->conn = NULL;
147 }
148 }
149
150 return std_dev_clear(di);
a4210e18
EK
151}
152
153static int dev_open(struct sr_dev_inst *sdi)
154{
8a9788e2 155 struct ipdbg_la_tcp *tcp = sdi->conn;
a4210e18 156
9d2e5483
SA
157 if (!tcp)
158 return SR_ERR;
a4210e18 159
8a9788e2 160 if (ipdbg_la_tcp_open(tcp) != SR_OK)
9d2e5483 161 return SR_ERR;
a4210e18 162
9d2e5483 163 return SR_OK;
a4210e18
EK
164}
165
166static int dev_close(struct sr_dev_inst *sdi)
167{
13ac501a 168 /* Should be called before a new call to scan(). */
8a9788e2 169 struct ipdbg_la_tcp *tcp = sdi->conn;
38e7493d 170
9d2e5483 171 if (tcp)
8a9788e2 172 ipdbg_la_tcp_close(tcp);
a4210e18 173
9d2e5483 174 sdi->conn = NULL;
a4210e18 175
9d2e5483 176 return SR_OK;
a4210e18
EK
177}
178
179static int config_get(uint32_t key, GVariant **data,
9d2e5483 180 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
a4210e18 181{
77b6b98d 182 struct dev_context *devc = sdi->priv;
a4210e18 183
9d2e5483 184 (void)cg;
a4210e18 185
9d2e5483
SA
186 switch (key) {
187 case SR_CONF_CAPTURE_RATIO:
188 *data = g_variant_new_uint64(devc->capture_ratio);
189 break;
190 case SR_CONF_LIMIT_SAMPLES:
191 *data = g_variant_new_uint64(devc->limit_samples);
192 break;
193 default:
77b6b98d 194 return SR_ERR_NA;
9d2e5483 195 }
a4210e18 196
77b6b98d 197 return SR_OK;
a4210e18
EK
198}
199
200static int config_set(uint32_t key, GVariant *data,
9d2e5483 201 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
a4210e18 202{
77b6b98d 203 struct dev_context *devc = sdi->priv;
9d2e5483
SA
204
205 (void)cg;
206
9d2e5483
SA
207 switch (key) {
208 case SR_CONF_CAPTURE_RATIO:
77b6b98d 209 devc->capture_ratio = g_variant_get_uint64(data);
9d2e5483
SA
210 break;
211 case SR_CONF_LIMIT_SAMPLES:
77b6b98d 212 devc->limit_samples = g_variant_get_uint64(data);
9d2e5483
SA
213 break;
214 default:
77b6b98d 215 return SR_ERR_NA;
9d2e5483
SA
216 }
217
77b6b98d 218 return SR_OK;
a4210e18
EK
219}
220
221static int config_list(uint32_t key, GVariant **data,
9d2e5483 222 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
a4210e18 223{
9d2e5483
SA
224 switch (key) {
225 case SR_CONF_SCAN_OPTIONS:
9d2e5483 226 case SR_CONF_DEVICE_OPTIONS:
77b6b98d 227 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
9d2e5483 228 case SR_CONF_TRIGGER_MATCH:
77b6b98d 229 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
9d2e5483
SA
230 break;
231 default:
232 return SR_ERR_NA;
233 }
234
235 return SR_OK;
a4210e18
EK
236}
237
a4210e18
EK
238static int dev_acquisition_start(const struct sr_dev_inst *sdi)
239{
8a9788e2 240 struct ipdbg_la_tcp *tcp = sdi->conn;
1f15efc1 241 struct dev_context *devc = sdi->priv;
a4210e18 242
8a9788e2
UH
243 ipdbg_la_convert_trigger(sdi);
244 ipdbg_la_send_trigger(devc, tcp);
245 ipdbg_la_send_delay(devc, tcp);
a4210e18 246
9d2e5483
SA
247 /* If the device stops sending for longer than it takes to send a byte,
248 * that means it's finished. But wait at least 100 ms to be safe.
249 */
250 sr_session_source_add(sdi->session, tcp->socket, G_IO_IN, 100,
8a9788e2 251 ipdbg_la_receive_data, (struct sr_dev_inst *)sdi);
a4210e18 252
8a9788e2 253 ipdbg_la_send_start(tcp);
a4210e18 254
9d2e5483 255 return SR_OK;
a4210e18
EK
256}
257
258static int dev_acquisition_stop(struct sr_dev_inst *sdi)
259{
8a9788e2 260 struct ipdbg_la_tcp *tcp = sdi->conn;
1f15efc1 261 struct dev_context *devc = sdi->priv;
a4210e18 262
9d2e5483 263 uint8_t byte;
b51288e3 264
9d2e5483
SA
265 if (devc->num_transfers > 0) {
266 while (devc->num_transfers <
ac3625be 267 (devc->limit_samples_max * devc->data_width_bytes)) {
8a9788e2 268 ipdbg_la_tcp_receive(tcp, &byte);
9d2e5483
SA
269 devc->num_transfers++;
270 }
271 }
a4210e18 272
8a9788e2
UH
273 ipdbg_la_send_reset(tcp);
274 ipdbg_la_abort_acquisition(sdi);
a4210e18 275
9d2e5483 276 return SR_OK;
a4210e18
EK
277}
278
279SR_PRIV struct sr_dev_driver ipdbg_la_driver_info = {
8a9788e2
UH
280 .name = "ipdbg-la",
281 .longname = "IPDBG LA",
9d2e5483 282 .api_version = 1,
3831eaf9 283 .init = std_init,
9d2e5483
SA
284 .cleanup = std_cleanup,
285 .scan = scan,
3831eaf9 286 .dev_list = std_dev_list,
9d2e5483
SA
287 .dev_clear = dev_clear,
288 .config_get = config_get,
289 .config_set = config_set,
290 .config_list = config_list,
291 .dev_open = dev_open,
292 .dev_close = dev_close,
293 .dev_acquisition_start = dev_acquisition_start,
294 .dev_acquisition_stop = dev_acquisition_stop,
295 .context = NULL,
a4210e18
EK
296};
297
298SR_REGISTER_DEV_DRIVER(ipdbg_la_driver_info);