forked from myriadrf/libxtrx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xtrx_debug.c
295 lines (254 loc) · 6.88 KB
/
xtrx_debug.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* xtrx debug server source file
* Copyright (c) 2018 Sergey Kostanbaev <[email protected]>
* For more information, please visit: http://xtrx.io
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "xtrxll_port.h"
#include "xtrxll_log.h"
#include "xtrx_debug.h"
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <signal.h>
#ifndef WIN32
#include <sys/un.h>
#include <sys/socket.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
struct xtrx_debug_ctx
{
void* obj;
const xtrx_debug_ops_t *ops;
pthread_t debug_thread;
//FILE* fifo;
int fd;
int clifd;
};
enum cmdvtype {
CMD_VT_NONE,
CMD_VT_DEC,
};
int xtrx_debug_process_cmd(xtrx_debug_ctx_t* ctx, char *cmd, unsigned len,
char* reply, unsigned rlen)
{
uint64_t oval = 0;
uint64_t param = 0;
unsigned device = 0;
int res = -EINVAL;
int j;
char *pptr[4] = {NULL,};
char *str1 = NULL, *saveptr1 = NULL, *token = NULL;
enum dubug_cmd dcmd;
enum cmdvtype vt = CMD_VT_NONE;
XTRXLLS_LOG("DBGP", XTRXLL_DEBUG, "got cmd: %s\n", cmd);
for (j = 0, str1 = cmd; j < 4; j++, str1 = NULL) {
token = strtok_r(str1, ",", &saveptr1);
if (token == NULL)
break;
pptr[j] = token;
}
if (strcmp(cmd, "LMS") == 0) {
if (j < 3) {
goto incorrect_format;
}
param = strtol(pptr[2], NULL, 16);
dcmd = (param & 0x80000000) ? DEBUG_RFIC_SPI_WR : DEBUG_RFIC_SPI_RD;
} else if (strcmp(cmd, "TMP") == 0) {
dcmd = DEBUG_BOARD_TEMP;
} else if(strcmp(cmd, "DAC") == 0) {
dcmd = DEBUG_BOARD_DAC;
vt = CMD_VT_DEC;
} else if(strcmp(cmd, "FREF") == 0) {
dcmd = DEBUG_GET_REFCLK;
} else if(strcmp(cmd, "ANTRX") == 0) {
dcmd = DEBUG_ANT_RX;
vt = CMD_VT_DEC;
param = UINT_MAX;
} else if(strcmp(cmd, "ANTTX") == 0) {
dcmd = DEBUG_ANT_TX;
vt = CMD_VT_DEC;
param = UINT_MAX;
} else if (strcmp(cmd, "DEVS") == 0) {
dcmd = DEBUG_GET_DEVICES;
} else if (strcmp(cmd, "RXIQA") == 0) {
dcmd = DEBUG_GET_RXIQ_ODD;
} else if (strcmp(cmd, "RXIQB") == 0) {
dcmd = DEBUG_GET_RXIQ_MISS;
} else if (strcmp(cmd, "VIO") == 0) {
dcmd = DEBUG_VIO;
vt = CMD_VT_DEC;
} else if (strcmp(cmd, "VGP") == 0) {
dcmd = DEBUG_V33;
vt = CMD_VT_DEC;
} else if (strcmp(cmd, "FGP") == 0) {
dcmd = DEBUG_FGP_CTRL;
vt = CMD_VT_DEC;
param = UINT_MAX;
} else if (strcmp(cmd, "GETREG") == 0) {
dcmd = DEBUG_XTRX_GET_REG;
vt = CMD_VT_DEC;
param = UINT_MAX;
} else {
XTRXLLS_LOG("DBGP", XTRXLL_ERROR, "unrecognized command! %s\n", cmd);
goto incorrect_format;
}
if (j > 1) {
device = strtol(pptr[1], NULL, 16);
}
if (vt != CMD_VT_NONE && j > 2) {
param = strtoll(pptr[2], NULL, 10);
}
res = ctx->ops->param_io(ctx->obj, dcmd, device, param, &oval);
incorrect_format:
if (res == 0) {
return snprintf(reply, rlen, "OK,%016" PRIx64 "\n", oval);
} else {
return snprintf(reply, rlen, "FAIL,%d\n", res);
}
}
#ifndef WIN32
static void* _xtrx_thread(void* param)
{
int ret;
struct sockaddr_un name;
xtrx_debug_ctx_t* ctx = (xtrx_debug_ctx_t*)param;
XTRXLLS_LOG("DBGP", XTRXLL_INFO, "Starting XTRX debug thread\n");
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
const char* fifoname = "xtrx_debug_pipe";
unlink(fifoname);
int sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock == -1) {
XTRXLLS_LOG("DBGP", XTRXLL_INFO, "Unable to create socket: error %d\n", errno);
return NULL;
}
memset(&name, 0, sizeof(struct sockaddr_un));
name.sun_family = AF_UNIX;
strncpy(name.sun_path, fifoname, sizeof(name.sun_path) - 1);
ret = bind(sock, (const struct sockaddr *) &name,
sizeof(struct sockaddr_un));
if (ret == -1) {
XTRXLLS_LOG("DBGP", XTRXLL_INFO, "Unable to bind socket: error %d\n", errno);
close(sock);
return NULL;
}
ret = listen(sock, 20);
if (ret == -1) {
XTRXLLS_LOG("DBGP", XTRXLL_INFO, "Unable to tisten to socket: error %d\n", errno);
close(sock);
return NULL;
}
ctx->fd = sock;
for (;;) {
ctx->clifd = -1;
int data_socket = accept(sock, NULL, NULL);
ctx->clifd = data_socket;
if (data_socket == -1) {
XTRXLLS_LOG("DBGP", XTRXLL_INFO, "Unable to accept socket: error %d\n", errno);
close(sock);
return NULL;
}
XTRXLLS_LOG("DBGP", XTRXLL_INFO, "Connection established\n");
unsigned p = 0;
char buffer[4096];
char reply[4096];
int replen;
for (;;) {
ssize_t res = read(data_socket, p + buffer, sizeof(buffer) - p);
if (res <= 0) {
XTRXLLS_LOG("DBGP", XTRXLL_INFO, "Connection closed\n");
goto connection_closed;
}
char* end = strchr(buffer, '\n');
if (end == NULL) {
p += res;
if (p == sizeof(buffer)) {
XTRXLLS_LOG("DBGP", XTRXLL_INFO, "Incorrect CMD!\n");
close(data_socket);
goto connection_closed;
}
continue;
}
// Terminate string
*end = 0;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
replen = xtrx_debug_process_cmd(ctx, buffer, end - buffer,
reply, sizeof(reply));
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
if (replen > 0) {
write(data_socket, reply, replen);
}
ssize_t ech = end - (p + buffer);
if (ech > res) {
XTRXLLS_LOG("DBGP", XTRXLL_INFO, "Moving extra %d/%d bytes\n",
(int)ech, (int)res);
memmove(buffer, end + 1, ech - res);
p = ech - res;
}
}
connection_closed:;
}
return NULL;
}
#endif
int xtrx_debug_init(const char *params,
const xtrx_debug_ops_t *ops,
void *obj,
xtrx_debug_ctx_t** octx)
{
#ifndef WIN32
int res;
const char* fifoname = "xtrx_debug_pipe";
int fd = mkfifo(fifoname, 0666);
if (fd < 0 && errno != EEXIST) {
int err = -errno;
XTRXLLS_LOG("DBGP", XTRXLL_ERROR, "Unable to create FIFO file `%s`, error %d\n",
fifoname, err);
return err;
}
xtrx_debug_ctx_t* ctx = (xtrx_debug_ctx_t*)malloc(sizeof(xtrx_debug_ctx_t));
if (!ctx)
return -ENOMEM;
ctx->obj = obj;
ctx->ops = ops;
res = pthread_create(&ctx->debug_thread, NULL, _xtrx_thread, ctx);
if (res)
goto failed_create_thread;
*octx = ctx;
return 0;
failed_create_thread:
free(ctx);
return res;
#else
return 0;
#endif
}
int xtrx_debug_free(xtrx_debug_ctx_t* ctx)
{
#ifndef WIN32
close(ctx->fd);
pthread_cancel(ctx->debug_thread);
pthread_join(ctx->debug_thread, NULL);
if (ctx->clifd != -1)
close(ctx->clifd);
free(ctx);
#endif
return 0;
}