-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathut_handler.c
More file actions
326 lines (256 loc) · 7.37 KB
/
ut_handler.c
File metadata and controls
326 lines (256 loc) · 7.37 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Microsemi Switchtec(tm) PCIe Management Command Line Interface
* Copyright (c) 2017, Microsemi Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifdef __linux__
#include <cli/commands.h>
#include <cli/argconfig.h>
#include <cli/common.h>
#include <switchtec/switchtec.h>
#include <switchtec/portable.h>
#include <switchtec/fabric.h>
#include <switchtec/utils.h>
#include <tests/common/ut_handler.h>
#include <lib/switchtec_priv.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/types.h>
#include <errno.h>
#include <ctype.h>
#include <inttypes.h>
#include <stddef.h>
#define CMD_DESC_UART_UT_HANDLER "UART unit testing command"
#define CMD_DESC_TWI_UT_HANDLER "TWI unit testing command"
struct switchtec_uart{
struct switchtec_dev dev;
int fd;
};
#define to_switchtec_uart(d) \
((struct switchtec_uart *) \
((char *)(d) - offsetof(struct switchtec_uart, dev)))
#define UART_MAX_WRITE_BYTES 100
#define UART_MAX_READ_BYTES 1024
#define RETRY_NUM 3
#define SWITCHTEC_UART_BAUDRATE (B230400)
static int send_cmd(int fd, const char *fmt, int write_bytes, ...)
{
int ret;
int i;
int cnt;
char cmd[1024];
uint8_t *write_data;
uint32_t write_crc;
va_list argp;
va_start(argp, write_bytes);
if (write_bytes) {
write_data = va_arg(argp, uint8_t *);
write_crc = va_arg(argp, uint32_t);
}
cnt = vsnprintf(cmd, sizeof(cmd), fmt, argp);
if (write_bytes) {
for (i = 0; i< write_bytes; i++) {
cnt += snprintf(cmd + cnt, sizeof(cmd) - cnt,
"%02x", write_data[write_bytes - 1 - i]);
}
cnt += snprintf(cmd + cnt, sizeof(cmd) - cnt,
" 0x%x\r", write_crc);
}
va_end(argp);
ret = write(fd, cmd, cnt);
if (ret < 0)
return ret;
if (ret != cnt) {
errno = EIO;
return -errno;
}
return 0;
}
static int read_resp_line(int fd, char *str)
{
int ret;
int cnt = 0;
while(1) {
ret = read(fd, str + cnt, sizeof(str));
if (ret <= 0)
return ret;
cnt += ret;
str[cnt] = '\0';
/* Prompt "0x12345678:1234>" */
if (strrchr(str, ':') + 5 == strrchr(str, '>'))
return 0;
}
return -1;
}
int cli_control(struct switchtec_dev *dev, const char *str)
{
int ret;
char rtn[1024];
struct switchtec_uart *udev = to_switchtec_uart(dev);
ret = send_cmd(udev->fd, str, 0);
if (ret)
return ret;
ret = read_resp_line(udev->fd, rtn);
printf("%s", rtn);
if (ret)
return ret;
return 0;
}
struct test_cmds {
char test_string[512];
};
static int read_input_file(FILE *ip_file, struct test_cmds *test_list, int *test_count)
{
char line[512];
int i = 0;
while (fgets(line, sizeof(line), ip_file)) {
/* ignore comments */
if (line[0] == '#')
continue;
/* strip any newline characters */
line[strcspn(line, "\r\n")] = '\0';
strcpy (test_list[i].test_string , strdup(line));
i++;
}
*test_count = i;
return 0;
}
static int cmn_cmd(int argc, char **argv, struct test_cmds *cmds, int *count)
{
int ret = 0;
static struct {
struct switchtec_dev *dev;
char *uart_cmd;
FILE *input_file;
} cfg = {
.uart_cmd = NULL,
.input_file = NULL,
};
const struct argconfig_options opts[] = {
DEVICE_OPTION,
{"uart_cmd", 'c', "UART_CMD", CFG_STRING, &cfg.uart_cmd,
optional_argument, .require_in_usage = 1,
.help = "Uart command"},
{"ut_file", 'C', "FILE", CFG_FILE_R, &cfg.input_file,
required_argument,
"Unit testing commands in file"},
{NULL}
};
argconfig_parse(argc, argv, CMD_DESC_UART_UT_HANDLER, opts, &cfg, sizeof(cfg));
if (cfg.uart_cmd == NULL) {
if (cfg.input_file == NULL){
printf ("file name not given\n");
return -1;
}
ret = read_input_file(cfg.input_file, cmds, count);
for (int i = 0; i < *count; i++){
printf("Starting Test command: %s\n", cmds[i].test_string);
strcat(cmds[i].test_string, "\r");
ret = cli_control(cfg.dev, cmds[i].test_string);
}
}
else {
printf("%s",cfg.uart_cmd);
strcat(cfg.uart_cmd, "\r");
ret = cli_control(cfg.dev, cfg.uart_cmd);
}
return ret;
}
static int uart(int argc, char **argv)
{
int ret = 0;
int cnt = 0;
struct test_cmds cmds[512];
ret = cmn_cmd(argc, argv, cmds, &cnt);
return ret;
}
#define CMD_DESC_I2C_CMD "I2C MRPC command for Unit test"
static int twi(int argc, char **argv)
{
uint8_t *payload = NULL;
uint8_t *temp = NULL;
uint8_t payload_len;
uint32_t response;
int ret;
static struct {
struct switchtec_dev *dev;
uint32_t i2c_cmd;
uint32_t test_id;
uint8_t param_len;
} cfg = {
.i2c_cmd = 0,
.test_id = 0,
.param_len = 0,
};
const struct argconfig_options opts[] = {
DEVICE_OPTION_MFG,
{"i2c_cmd", 'c', "I2C_CMD", CFG_LONG, &cfg.i2c_cmd,
required_argument, .require_in_usage = 1, .help = "i2c command"},
{"test_id", 't', "TEST_ID", CFG_LONG, &cfg.test_id,
required_argument, .require_in_usage = 1, .help = "test id"},
{"param_len", 'l', "PARAM_LEN", CFG_BYTE, &cfg.param_len,
optional_argument, .require_in_usage = 1, .help = "param_len"},
{NULL}
};
argconfig_parse(argc, argv, CMD_DESC_I2C_CMD, opts, &cfg, sizeof(cfg));
if(cfg.param_len == 0)
payload_len = 4; /* Test ID */
else
payload_len = 5 + cfg.param_len; /* Test ID(4) + Parameter Length(1) + Number of Parameters */
payload = (uint8_t *)malloc((sizeof(uint8_t) * payload_len));
if(!payload)
return -1;
temp = (uint8_t *)&(cfg.test_id);
payload[0] = temp[0];
payload[1] = temp[1];
payload[2] = temp[2];
payload[3] = temp[3];
if(cfg.param_len != 0)
{
payload[4] = cfg.param_len;
for(uint8_t i = 0; i < cfg.param_len; i++)
{
payload[5+i] = atoi((argv[5+i]));
}
}
ret = switchtec_cmd(cfg.dev, cfg.i2c_cmd, payload,
payload_len, &response, sizeof(response));
if (ret) {
switchtec_perror("I2C command error");
return ret;
}
printf("I2C command :0x%X, Test ID : %d, Response : %s \n", cfg.i2c_cmd, cfg.test_id, response?"FAIL":"PASS");
return 0;
}
static const struct cmd commands[] = {
CMD(uart, CMD_DESC_UART_UT_HANDLER),
CMD(twi, CMD_DESC_TWI_UT_HANDLER),
{},
};
static struct subcommand subcmd = {
.name = "ut",
.cmds = commands,
.desc = "unit testing commands",
.long_desc = "These functions provide diagnostic information from "
"the switch",
};
REGISTER_SUBCMD(subcmd);
#endif//__linux__