summaryrefslogtreecommitdiff
path: root/test/usb_pd_console.c
blob: 80b9978ea3e236a1415ffac17a7bd55f3de054f8 (plain)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Test usb_pd_console
 */

#include "common.h"
#include "math.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "usb_pe_sm.h"
#include "usb_pd.h"
#include "usb_tc_sm.h"
#include "util.h"
#include "test_util.h"

/* Defined in implementation */
int command_pd(int argc, const char **argv);

static enum debug_level prl_debug_level;
static enum debug_level pe_debug_level;
static enum debug_level tc_debug_level;

static bool pd_get_polarity_called;
static bool pd_comm_is_enabled_called;
static bool pd_get_power_role_called;
static bool pd_get_data_role_called;
static bool tc_is_vconn_src_called;
static bool tc_get_current_state_called;
static bool tc_get_flags_called;
static bool pe_get_current_state_called;
static bool pe_get_flags_called;
static bool pd_get_dual_role_called;
static bool board_get_usb_pd_port_count_called;
static bool pd_srccaps_dump_called;
static bool pd_timer_dump_called;

static enum try_src_override_t try_src_override;
static int test_port;
static enum pd_dpm_request request;
static int max_volt;
static int comm_enable;
static int dev_info;
static int vdm_cmd;
static int vdm_count;
static int vdm_vid;
static uint32_t vdm_data[10];
static enum pd_dual_role_states dr_state;

/* Mock functions */
int tc_is_vconn_src(int port)
{
	tc_is_vconn_src_called = true;
	return 0;
}

enum pd_power_role pd_get_power_role(int port)
{
	pd_get_power_role_called = true;
	return 0;
}

uint32_t pe_get_flags(int port)
{
	pe_get_flags_called = true;
	return 0;
}

const char *pe_get_current_state(int port)
{
	pe_get_current_state_called = true;
	return "PE_STATE";
}

enum pd_dual_role_states pd_get_dual_role(int port)
{
	pd_get_dual_role_called = true;
	return 0;
}

void pd_timer_dump(int port)
{
	pd_timer_dump_called = true;
}

void pd_srccaps_dump(int port)
{
	pd_srccaps_dump_called = true;
}

void prl_set_debug_level(enum debug_level level)
{
	prl_debug_level = level;
}

void pe_set_debug_level(enum debug_level level)
{
	pe_debug_level = level;
}

void tc_set_debug_level(enum debug_level level)
{
	tc_debug_level = level;
}

enum pd_data_role pd_get_data_role(int port)
{
	pd_get_data_role_called = true;
	return 0;
}

void tc_state_init(int port)
{
}

void tc_event_check(int port, int evt)
{
}

uint8_t tc_get_pd_enabled(int port)
{
	return 0;
}

void pe_run(int port, int evt, int en)
{
}

void tc_run(int port)
{
}

uint8_t board_get_usb_pd_port_count(void)
{
	board_get_usb_pd_port_count_called = true;
	return CONFIG_USB_PD_PORT_MAX_COUNT;
}

void pe_send_vdm(int port, uint32_t vid, int cmd, const uint32_t *data,
		 int count)
{
	int i;

	test_port = port;
	vdm_cmd = cmd;
	vdm_count = count;
	vdm_vid = vid;

	if (data == NULL)
		for (i = 0; i < 10; i++)
			vdm_data[i] = -1;
	else
		for (i = 0; i < count; i++)
			vdm_data[i] = data[i];
}

void pd_dpm_request(int port, enum pd_dpm_request req)
{
	test_port = port;
	request = req;
}

unsigned int pd_get_max_voltage(void)
{
	return 10000;
}

void pd_request_source_voltage(int port, int mv)
{
	test_port = port;
	max_volt = mv;
}

void pd_comm_enable(int port, int enable)
{
	test_port = port;
	comm_enable = enable;
}

void tc_print_dev_info(int port)
{
	test_port = port;
	dev_info = 1;
}

void pd_set_dual_role(int port, enum pd_dual_role_states state)
{
	test_port = port;
	dr_state = state;
}

int pd_comm_is_enabled(int port)
{
	test_port = port;
	pd_comm_is_enabled_called = true;
	return 0;
}

enum tcpc_cc_polarity pd_get_polarity(int port)
{
	test_port = port;
	pd_get_polarity_called = true;
	return 0;
}

uint32_t tc_get_flags(int port)
{
	test_port = port;
	tc_get_flags_called = true;
	return 0;
}

const char *tc_get_current_state(int port)
{
	test_port = port;
	tc_get_current_state_called = true;
	return "TC_STATE";
}

void tc_try_src_override(enum try_src_override_t ov)
{
	if (IS_ENABLED(CONFIG_USB_PD_TRY_SRC)) {
		switch (ov) {
		case TRY_SRC_OVERRIDE_OFF: /* 0 */
			try_src_override = TRY_SRC_OVERRIDE_OFF;
			break;
		case TRY_SRC_OVERRIDE_ON: /* 1 */
			try_src_override = TRY_SRC_OVERRIDE_ON;
			break;
		default:
			try_src_override = TRY_SRC_NO_OVERRIDE;
		}
	}
}

enum try_src_override_t tc_get_try_src_override(void)
{
	return try_src_override;
}

enum pd_cc_states pd_get_task_cc_state(int port)
{
	return PD_CC_NONE;
}

static int test_command_pd_dump(void)
{
	int argc = 3;
	const char *argv[] = { "pd", "dump", "", 0, 0, 0 };
	char test[2];

	sprintf(test, "e");
	argv[2] = test;
	TEST_ASSERT(command_pd(argc, argv) == EC_ERROR_PARAM2);

	for (int i = DEBUG_DISABLE; i <= DEBUG_LEVEL_MAX; i++) {
		sprintf(test, "%d", i);
		argv[2] = test;
		TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);
		TEST_ASSERT(prl_debug_level == i);
		TEST_ASSERT(pe_debug_level == i);
		TEST_ASSERT(tc_debug_level == i);
	}

	sprintf(test, "%d", DEBUG_LEVEL_MAX + 1);
	argv[2] = test;
	TEST_ASSERT(prl_debug_level == DEBUG_LEVEL_MAX);
	TEST_ASSERT(pe_debug_level == DEBUG_LEVEL_MAX);
	TEST_ASSERT(tc_debug_level == DEBUG_LEVEL_MAX);

	return EC_SUCCESS;
}

static int test_command_pd_try_src(void)
{
	int argc = 3;
	const char *argv[] = { "pd", "trysrc", "2", 0, 0 };

	try_src_override = 0;
	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);
	TEST_ASSERT(try_src_override == TRY_SRC_NO_OVERRIDE);

	argv[2] = "1";
	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);
	TEST_ASSERT(try_src_override == TRY_SRC_OVERRIDE_ON);

	argv[2] = "0";
	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);
	TEST_ASSERT(try_src_override == TRY_SRC_OVERRIDE_OFF);

	return EC_SUCCESS;
}

static int test_command_pd_version(void)
{
	int argc = 2;
	const char *argv[] = { "pd", "version", 0, 0, 0 };

	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);

	return EC_SUCCESS;
}

static int test_command_pd_arg_count(void)
{
	int argc;
	const char *argv[] = { "pd", "", 0, 0, 0 };

	for (argc = 0; argc < 3; argc++)
		TEST_ASSERT(command_pd(argc, argv) == EC_ERROR_PARAM_COUNT);

	return EC_SUCCESS;
}

static int test_command_pd_port_num(void)
{
	int argc = 3;
	const char *argv[10] = { "pd", "0", 0, 0, 0 };
	char test[2];

	sprintf(test, "%d", CONFIG_USB_PD_PORT_MAX_COUNT);
	argv[1] = test;
	TEST_ASSERT(command_pd(argc, argv) == EC_ERROR_PARAM2);

	return EC_SUCCESS;
}

static int test_command_pd_tx(void)
{
	int argc = 3;
	const char *argv[] = { "pd", "0", "tx", 0, 0 };

	request = 0;
	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);
	TEST_ASSERT(test_port == 0);
	TEST_ASSERT(request == DPM_REQUEST_SNK_STARTUP);

	return EC_SUCCESS;
}

static int test_command_pd_charger(void)
{
	int argc = 3;
	const char *argv[] = { "pd", "1", "charger", 0, 0 };

	request = 0;
	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);
	TEST_ASSERT(test_port == 1);
	TEST_ASSERT(request == DPM_REQUEST_SRC_STARTUP);

	return EC_SUCCESS;
}

static int test_command_pd_dev1(void)
{
	int argc = 4;
	const char *argv[] = { "pd", "0", "dev", "20", 0 };

	request = 0;
	max_volt = 0;
	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);
	TEST_ASSERT(test_port == 0);
	TEST_ASSERT(request == DPM_REQUEST_NEW_POWER_LEVEL);
	TEST_ASSERT(max_volt == 20000);

	return EC_SUCCESS;
}

static int test_command_pd_dev2(void)
{
	int argc = 3;
	const char *argv[] = { "pd", "1", "dev", 0, 0 };

	request = 0;
	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);
	TEST_ASSERT(test_port == 1);
	TEST_ASSERT(request == DPM_REQUEST_NEW_POWER_LEVEL);
	TEST_ASSERT(max_volt == 10000);

	return EC_SUCCESS;
}

static int test_command_pd_disable(void)
{
	int argc = 3;
	const char *argv[] = { "pd", "0", "disable", 0, 0 };

	comm_enable = 1;
	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);
	TEST_ASSERT(test_port == 0);
	TEST_ASSERT(comm_enable == 0);

	return EC_SUCCESS;
}

static int test_command_pd_enable(void)
{
	int argc = 3;
	const char *argv[] = { "pd", "1", "enable", 0, 0 };

	comm_enable = 0;
	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);
	TEST_ASSERT(test_port == 1);
	TEST_ASSERT(comm_enable == 1);

	return EC_SUCCESS;
}

static int test_command_pd_hard(void)
{
	int argc = 3;
	const char *argv[] = { "pd", "0", "hard", 0, 0 };

	request = 0;
	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);
	TEST_ASSERT(test_port == 0);
	TEST_ASSERT(request == DPM_REQUEST_HARD_RESET_SEND);

	return EC_SUCCESS;
}

static int test_command_pd_soft(void)
{
	int argc = 3;
	const char *argv[] = { "pd", "0", "soft", 0, 0 };

	request = 0;
	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);
	TEST_ASSERT(test_port == 0);
	TEST_ASSERT(request == DPM_REQUEST_SOFT_RESET_SEND);

	return EC_SUCCESS;
}

static int test_command_pd_swap1(void)
{
	int argc = 3;
	const char *argv[] = { "pd", "0", "swap", 0, 0 };

	TEST_ASSERT(command_pd(argc, argv) == EC_ERROR_PARAM_COUNT);

	return EC_SUCCESS;
}

static int test_command_pd_swap2(void)
{
	int argc = 4;
	const char *argv[] = { "pd", "0", "swap", "power", 0 };

	request = 0;
	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);
	TEST_ASSERT(test_port == 0);
	TEST_ASSERT(request == DPM_REQUEST_PR_SWAP);

	return EC_SUCCESS;
}

static int test_command_pd_swap3(void)
{
	int argc = 4;
	const char *argv[] = { "pd", "1", "swap", "data", 0 };

	request = 0;
	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);
	TEST_ASSERT(test_port == 1);
	TEST_ASSERT(request == DPM_REQUEST_DR_SWAP);

	return EC_SUCCESS;
}

static int test_command_pd_swap4(void)
{
	int argc = 4;
	const char *argv[] = { "pd", "0", "swap", "vconn", 0 };

	request = 0;
	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);
	TEST_ASSERT(test_port == 0);
	TEST_ASSERT(request == DPM_REQUEST_VCONN_SWAP);

	return EC_SUCCESS;
}

static int test_command_pd_swap5(void)
{
	int argc = 4;
	const char *argv[] = { "pd", "0", "swap", "xyz", 0 };

	TEST_ASSERT(command_pd(argc, argv) == EC_ERROR_PARAM3);

	return EC_SUCCESS;
}

static int test_command_pd_dualrole0(void)
{
	int argc = 3;
	const char *argv[] = { "pd", "0", "dualrole", 0, 0 };

	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);

	return EC_SUCCESS;
}

static int test_command_pd_dualrole1(void)
{
	int argc = 4;
	const char *argv[] = { "pd", "0", "dualrole", "on", 0 };

	dr_state = 0;
	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);
	TEST_ASSERT(test_port == 0);
	TEST_ASSERT(dr_state == PD_DRP_TOGGLE_ON);

	return EC_SUCCESS;
}

static int test_command_pd_dualrole2(void)
{
	int argc = 4;
	const char *argv[] = { "pd", "0", "dualrole", "off", 0 };

	dr_state = 0;
	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);
	TEST_ASSERT(test_port == 0);
	TEST_ASSERT(dr_state == PD_DRP_TOGGLE_OFF);

	return EC_SUCCESS;
}

static int test_command_pd_dualrole3(void)
{
	int argc = 4;
	const char *argv[] = { "pd", "0", "dualrole", "freeze", 0 };

	dr_state = 0;
	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);
	TEST_ASSERT(test_port == 0);
	TEST_ASSERT(dr_state == PD_DRP_FREEZE);

	return EC_SUCCESS;
}

static int test_command_pd_dualrole4(void)
{
	int argc = 4;
	const char *argv[] = { "pd", "0", "dualrole", "sink", 0 };

	dr_state = 0;
	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);
	TEST_ASSERT(test_port == 0);
	TEST_ASSERT(dr_state == PD_DRP_FORCE_SINK);

	return EC_SUCCESS;
}

static int test_command_pd_dualrole5(void)
{
	int argc = 4;
	const char *argv[] = { "pd", "0", "dualrole", "source", 0 };

	dr_state = 0;
	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);
	TEST_ASSERT(test_port == 0);
	TEST_ASSERT(dr_state == PD_DRP_FORCE_SOURCE);

	return EC_SUCCESS;
}

static int test_command_pd_state(void)
{
	int argc = 3;
	const char *argv[] = { "pd", "0", "state", 0, 0 };

	pd_get_polarity_called = false;
	pd_comm_is_enabled_called = false;
	pd_get_power_role_called = false;
	pd_get_data_role_called = false;
	tc_is_vconn_src_called = false;
	tc_get_current_state_called = false;
	tc_get_flags_called = false;
	pe_get_current_state_called = false;
	pe_get_flags_called = false;

	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);
	TEST_ASSERT(pd_get_polarity_called);
	TEST_ASSERT(pd_comm_is_enabled_called);
	TEST_ASSERT(pd_get_power_role_called);
	TEST_ASSERT(pd_get_data_role_called);
	TEST_ASSERT(tc_is_vconn_src_called);
	TEST_ASSERT(tc_get_current_state_called);
	TEST_ASSERT(tc_get_flags_called);
	TEST_ASSERT(pe_get_current_state_called);
	TEST_ASSERT(pe_get_flags_called);

	return EC_SUCCESS;
}

static int test_command_pd_srccaps(void)
{
	int argc = 3;
	const char *argv[] = { "pd", "0", "srccaps", 0, 0 };

	pd_srccaps_dump_called = false;
	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);
	TEST_ASSERT(pd_srccaps_dump_called);

	return EC_SUCCESS;
}

static int test_command_pd_timer(void)
{
	int argc = 3;
	const char *argv[] = { "pd", "0", "timer", 0, 0 };

	pd_timer_dump_called = false;
	TEST_ASSERT(command_pd(argc, argv) == EC_SUCCESS);
	TEST_ASSERT(pd_timer_dump_called);

	return EC_SUCCESS;
}

void run_test(int argc, const char **argv)
{
	test_reset();

	RUN_TEST(test_command_pd_dump);
	RUN_TEST(test_command_pd_try_src);
	RUN_TEST(test_command_pd_version);
	RUN_TEST(test_command_pd_arg_count);
	RUN_TEST(test_command_pd_port_num);
	RUN_TEST(test_command_pd_tx);
	RUN_TEST(test_command_pd_charger);
	RUN_TEST(test_command_pd_dev1);
	RUN_TEST(test_command_pd_dev2);
	RUN_TEST(test_command_pd_disable);
	RUN_TEST(test_command_pd_enable);
	RUN_TEST(test_command_pd_hard);
	RUN_TEST(test_command_pd_soft);
	RUN_TEST(test_command_pd_swap1);
	RUN_TEST(test_command_pd_swap2);
	RUN_TEST(test_command_pd_swap3);
	RUN_TEST(test_command_pd_swap4);
	RUN_TEST(test_command_pd_swap5);
	RUN_TEST(test_command_pd_dualrole0);
	RUN_TEST(test_command_pd_dualrole1);
	RUN_TEST(test_command_pd_dualrole2);
	RUN_TEST(test_command_pd_dualrole3);
	RUN_TEST(test_command_pd_dualrole4);
	RUN_TEST(test_command_pd_dualrole5);
	RUN_TEST(test_command_pd_state);
	RUN_TEST(test_command_pd_srccaps);
	RUN_TEST(test_command_pd_timer);

	test_print_result();
}