summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/ap_vdm_control/src/ap_vdm_control.c
blob: 2758b5bc5212478595fe2164aeb2045d0f8f65fe (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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
/* Copyright 2022 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "ec_commands.h"
#include "test/drivers/stubs.h"
#include "test/drivers/test_state.h"
#include "test/drivers/utils.h"
#include "usb_mux.h"
#include "usb_pd_vdo.h"

#include <stdint.h>

#include <zephyr/kernel.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/ztest.h>

#define TEST_PORT USBC_PORT_C0

struct ap_vdm_control_fixture {
	const struct emul *tcpci_emul;
	const struct emul *charger_emul;
	struct tcpci_partner_data partner;
	struct tcpci_src_emul_data src_ext;
};

struct tcpci_cable_data passive_usb3 = {
	.identity_vdm[VDO_INDEX_HDR] =
		VDO(USB_SID_PD, /* structured VDM */ true,
		    VDO_CMDT(CMDT_RSP_ACK) | CMD_DISCOVER_IDENT) |
		VDO_SVDM_VERS(VDM_VER20),
	.identity_vdm[VDO_INDEX_IDH] = VDO_IDH(
		/* USB host */ false, /* USB device */ false, IDH_PTYPE_PCABLE,
		/* modal operation */ false, USB_VID_GOOGLE),
	.identity_vdm[VDO_INDEX_CSTAT] = 0,
	.identity_vdm[VDO_INDEX_PRODUCT] = VDO_PRODUCT(0x1234, 0xABCD),
	.identity_vdm[VDO_INDEX_PTYPE_CABLE1] =
		VDO_REV30_PASSIVE(USB_R30_SS_U32_U40_GEN2, USB_VBUS_CUR_3A,
				  USB_REV30_LATENCY_1m, USB_REV30_TYPE_C),
	.identity_vdos = VDO_INDEX_PTYPE_CABLE1 + 1,

};

static void add_dp_discovery(struct tcpci_partner_data *partner)
{
	/* Add Discover Identity response */
	partner->identity_vdm[VDO_INDEX_HDR] =
		VDO(USB_SID_PD, /* structured VDM */ true,
		    VDO_CMDT(CMDT_RSP_ACK) | CMD_DISCOVER_IDENT) |
		VDO_SVDM_VERS(VDM_VER20);
	partner->identity_vdm[VDO_INDEX_IDH] = VDO_IDH(
		/* USB host */ false, /* USB device */ true, IDH_PTYPE_HUB,
		/* modal operation */ true, USB_VID_GOOGLE);
	partner->identity_vdm[VDO_INDEX_CSTAT] = 0;
	partner->identity_vdm[VDO_INDEX_PRODUCT] = VDO_PRODUCT(0x1234, 0x5678);
	/* Hardware version 1, firmware version 2 */
	partner->identity_vdm[VDO_INDEX_PTYPE_UFP1_VDO] = VDO_UFP1(
		(VDO_UFP1_CAPABILITY_USB20 | VDO_UFP1_CAPABILITY_USB32),
		USB_TYPEC_RECEPTACLE, VDO_UFP1_ALT_MODE_RECONFIGURE,
		USB_R30_SS_U32_U40_GEN2);
	partner->identity_vdos = VDO_INDEX_PTYPE_UFP1_VDO + 1;

	/* Add Discover Modes response */
	/* Support one mode for DisplayPort VID.*/
	partner->modes_vdm[VDO_INDEX_HDR] =
		VDO(USB_SID_DISPLAYPORT, /* structured VDM */ true,
		    VDO_CMDT(CMDT_RSP_ACK) | CMD_DISCOVER_MODES) |
		VDO_SVDM_VERS(VDM_VER20);
	partner->modes_vdm[VDO_INDEX_HDR + 1] =
		VDO_MODE_DP(MODE_DP_PIN_C | MODE_DP_PIN_D, 0, 1,
			    CABLE_RECEPTACLE, MODE_DP_V13, MODE_DP_SNK);
	partner->modes_vdos = VDO_INDEX_HDR + 2;

	/* Add Discover SVIDs response */
	/* Support DisplayPort VID. */
	partner->svids_vdm[VDO_INDEX_HDR] =
		VDO(USB_SID_PD, /* structured VDM */ true,
		    VDO_CMDT(CMDT_RSP_ACK) | CMD_DISCOVER_SVID) |
		VDO_SVDM_VERS(VDM_VER20);
	partner->svids_vdm[VDO_INDEX_HDR + 1] =
		VDO_SVID(USB_SID_DISPLAYPORT, 0);
	partner->svids_vdos = VDO_INDEX_HDR + 2;
}

static void add_displayport_mode_responses(struct tcpci_partner_data *partner)
{
	/* Add DisplayPort EnterMode response */
	partner->enter_mode_vdm[VDO_INDEX_HDR] =
		VDO(USB_SID_DISPLAYPORT, /* structured VDM */ true,
		    VDO_CMDT(CMDT_RSP_ACK) | CMD_ENTER_MODE) |
		VDO_SVDM_VERS(VDM_VER20);
	partner->enter_mode_vdos = VDO_INDEX_HDR + 1;

	/* Add DisplayPort StatusUpdate response */
	partner->dp_status_vdm[VDO_INDEX_HDR] =
		VDO(USB_SID_DISPLAYPORT, /* structured VDM */ true,
		    VDO_CMDT(CMDT_RSP_ACK) | CMD_DP_STATUS) |
		VDO_SVDM_VERS(VDM_VER20);
	partner->dp_status_vdm[VDO_INDEX_HDR + 1] =
		VDO_DP_STATUS(0, /* IRQ_HPD */
			      false, /* HPD_HI|LOW - Changed*/
			      0, /* request exit DP */
			      0, /* request exit USB */
			      1, /* MF pref */
			      true, /* DP Enabled */
			      0, /* power low e.g. normal */
			      0x2 /* Connected as Sink */);
	partner->dp_status_vdos = VDO_INDEX_HDR + 2;

	/* Add DisplayPort Configure Response */
	partner->dp_config_vdm[VDO_INDEX_HDR] =
		VDO(USB_SID_DISPLAYPORT, /* structured VDM */ true,
		    VDO_CMDT(CMDT_RSP_ACK) | CMD_DP_CONFIG) |
		VDO_SVDM_VERS(VDM_VER20);
	partner->dp_config_vdos = VDO_INDEX_HDR + 1;
}

static void verify_vdm_req(struct ap_vdm_control_fixture *fixture,
			   struct typec_vdm_req *req)
{
	struct tcpci_partner_log_msg *msg;
	bool message_seen = 0;

	SYS_SLIST_FOR_EACH_CONTAINER(&fixture->partner.msg_log, msg, node)
	{
		uint16_t header = sys_get_le16(msg->buf);

		/* Ignore messages from ourselves */
		if (msg->sender == TCPCI_PARTNER_SENDER_PARTNER)
			continue;

		/*
		 * Control messages, non-VDMs, and extended messages are not of
		 * interest
		 */
		if ((PD_HEADER_CNT(header) == 0) ||
		    (PD_HEADER_TYPE(header) != PD_DATA_VENDOR_DEF) ||
		    (PD_HEADER_EXT(header) != 0)) {
			continue;
		}

		if (req->partner_type != msg->sop)
			continue;

		/* We have a VDM, check entry we're interested in */
		if (memcmp(msg->buf + 2, req->vdm_data,
			   req->vdm_data_objects * sizeof(uint32_t)) == 0) {
			message_seen = true;
			break;
		}
	}

	zassert_true(message_seen, "Expected message not found");
}

static void *ap_vdm_control_setup(void)
{
	static struct ap_vdm_control_fixture fixture;
	struct tcpci_partner_data *partner = &fixture.partner;
	struct tcpci_src_emul_data *src_ext = &fixture.src_ext;

	tcpci_partner_init(partner, PD_REV30);
	partner->extensions = tcpci_src_emul_init(src_ext, partner, NULL);

	/* Get references for the emulators */
	fixture.tcpci_emul = EMUL_DT_GET(DT_NODELABEL(tcpci_emul));
	fixture.charger_emul = EMUL_DT_GET(DT_NODELABEL(isl923x_emul));

	return &fixture;
}

static void ap_vdm_control_before(void *data)
{
	struct ap_vdm_control_fixture *fix = data;
	struct tcpci_partner_data *partner = &fix->partner;

	/* Set chipset on so the "AP" is on to give us commands */
	test_set_chipset_to_s0();

	/* Set up the partner as DP-capable with a passive cable */
	add_dp_discovery(partner);
	partner->cable = &passive_usb3;
	add_displayport_mode_responses(partner);

	/* Connect our port partner */
	connect_source_to_port(&fix->partner, &fix->src_ext, 0, fix->tcpci_emul,
			       fix->charger_emul);
}

static void ap_vdm_control_after(void *data)
{
	struct ap_vdm_control_fixture *fix = data;

	disconnect_source_from_port(fix->tcpci_emul, fix->charger_emul);
	tcpci_partner_common_clear_logged_msgs(&fix->partner);
}

ZTEST_SUITE(ap_vdm_control, drivers_predicate_post_main, ap_vdm_control_setup,
	    ap_vdm_control_before, ap_vdm_control_after, NULL);

ZTEST_F(ap_vdm_control, test_feature_present)
{
	struct ec_response_get_features feat = host_cmd_get_features();

	zassert_true(feat.flags[1] &
			     EC_FEATURE_MASK_1(EC_FEATURE_TYPEC_AP_VDM_SEND),
		     "Failed to see feature present");
}

/* TYPEC_CONTROL_COMMAND_SEND_VDM_REQ tests */
ZTEST_F(ap_vdm_control, test_send_vdm_req_bad_port)
{
	struct ec_params_typec_control params = {
		.port = 85,
		.command = TYPEC_CONTROL_COMMAND_SEND_VDM_REQ,
		.vdm_req_params = {
			.vdm_data = { 0 },
			.vdm_data_objects = 1,
			.partner_type = TYPEC_PARTNER_SOP,
		},
	};
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND_PARAMS(EC_CMD_TYPEC_CONTROL, 0, params);

	zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM,
		      "Failed to see port error");
}

ZTEST_F(ap_vdm_control, test_send_vdm_req_bad_type)
{
	struct ec_params_typec_control params = {
		.port = TEST_PORT,
		.command = TYPEC_CONTROL_COMMAND_SEND_VDM_REQ,
		.vdm_req_params = {
			.vdm_data = { 0 },
			.vdm_data_objects = 1,
			.partner_type = TYPEC_PARTNER_SOP_PRIME_PRIME + 1,
		},
	};
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND_PARAMS(EC_CMD_TYPEC_CONTROL, 0, params);

	zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM,
		      "Failed to see port error");
}

ZTEST_F(ap_vdm_control, test_send_vdm_req_bad_count)
{
	struct ec_params_typec_control params = {
		.port = TEST_PORT,
		.command = TYPEC_CONTROL_COMMAND_SEND_VDM_REQ,
		.vdm_req_params = {
			.vdm_data = { 0 },
			.vdm_data_objects = 0,
			.partner_type = TYPEC_PARTNER_SOP,
		},
	};
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND_PARAMS(EC_CMD_TYPEC_CONTROL, 0, params);

	zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM,
		      "Failed to see port error");
}

ZTEST_F(ap_vdm_control, test_send_vdm_sop_req_valid)
{
	uint32_t vdm_req_header = VDO(USB_SID_PD, 1, CMD_DISCOVER_IDENT) |
				  VDO_SVDM_VERS(VDM_VER20);
	struct typec_vdm_req req = {
		.vdm_data = { vdm_req_header },
		.vdm_data_objects = 1,
		.partner_type = TYPEC_PARTNER_SOP,
	};

	tcpci_partner_common_enable_pd_logging(&fixture->partner, true);
	host_cmd_typec_control_vdm_req(TEST_PORT, req);
	k_sleep(K_SECONDS(1));

	tcpci_partner_common_enable_pd_logging(&fixture->partner, false);

	/* Look for our REQ */
	verify_vdm_req(fixture, &req);
}

ZTEST_F(ap_vdm_control, test_send_vdm_sop_prime_req_valid)
{
	uint32_t vdm_req_header = VDO(USB_SID_PD, 1, CMD_DISCOVER_IDENT) |
				  VDO_SVDM_VERS(VDM_VER20);
	struct typec_vdm_req req = {
		.vdm_data = { vdm_req_header },
		.vdm_data_objects = 1,
		.partner_type = TYPEC_PARTNER_SOP_PRIME,
	};

	tcpci_partner_common_enable_pd_logging(&fixture->partner, true);
	host_cmd_typec_control_vdm_req(TEST_PORT, req);
	k_sleep(K_SECONDS(1));

	tcpci_partner_common_enable_pd_logging(&fixture->partner, false);

	/* Look for our REQ */
	verify_vdm_req(fixture, &req);
}

ZTEST_F(ap_vdm_control, test_send_vdm_sop_attention_bad)
{
	uint32_t vdm_req_header = VDO(USB_SID_DISPLAYPORT, 1, CMD_ATTENTION) |
				  VDO_SVDM_VERS(VDM_VER20);
	struct ec_params_typec_control params = {
		.port = TEST_PORT,
		.command = TYPEC_CONTROL_COMMAND_SEND_VDM_REQ,
		.vdm_req_params = {
			.vdm_data = { vdm_req_header },
			.vdm_data_objects = 5,
			.partner_type = TYPEC_PARTNER_SOP,
		},
	};
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND_PARAMS(EC_CMD_TYPEC_CONTROL, 0, params);

	zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM,
		      "Failed to see port error");
}

ZTEST_F(ap_vdm_control, test_send_vdm_req_in_progress)
{
	uint32_t vdm_req_header = VDO(USB_SID_PD, 1, CMD_DISCOVER_IDENT) |
				  VDO_SVDM_VERS(VDM_VER20);
	struct ec_params_typec_control params = {
		.port = TEST_PORT,
		.command = TYPEC_CONTROL_COMMAND_SEND_VDM_REQ,
		.vdm_req_params = {
			.vdm_data = { vdm_req_header },
			.vdm_data_objects = 1,
			.partner_type = TYPEC_PARTNER_SOP,
		},
	};
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND_PARAMS(EC_CMD_TYPEC_CONTROL, 0, params);

	/*
	 * First command should succeed, but given no time to process the second
	 * should return busy
	 */
	zassert_equal(host_command_process(&args), EC_RES_SUCCESS,
		      "Failed to send successful request");
	zassert_equal(host_command_process(&args), EC_RES_BUSY,
		      "Failed to see busy");
}

/* EC_CMD_TYPEC_VDM_RESPONSE tests */
ZTEST_F(ap_vdm_control, test_vdm_response_ack)
{
	struct ec_response_typec_status status;
	struct ec_response_typec_vdm_response vdm_resp;
	uint32_t vdm_req_header = VDO(USB_SID_PD, 1, CMD_DISCOVER_IDENT) |
				  VDO_SVDM_VERS(VDM_VER20);
	struct typec_vdm_req req = {
		.vdm_data = { vdm_req_header },
		.vdm_data_objects = 1,
		.partner_type = TYPEC_PARTNER_SOP,
	};

	host_cmd_typec_control_vdm_req(TEST_PORT, req);
	k_sleep(K_SECONDS(1));

	/* Look for our notification and reply */
	status = host_cmd_typec_status(TEST_PORT);
	zassert_true(status.events & PD_STATUS_EVENT_VDM_REQ_REPLY,
		     "Failed to see VDM ACK event");

	vdm_resp = host_cmd_typec_vdm_response(TEST_PORT);
	zassert_equal(vdm_resp.vdm_response_err, EC_RES_SUCCESS);
	zassert_equal(vdm_resp.partner_type, req.partner_type,
		      "Failed to see correct partner");
	zassert_equal(vdm_resp.vdm_data_objects, fixture->partner.identity_vdos,
		      "Failed to see correct VDO num");
	zassert_equal(memcmp(vdm_resp.vdm_response,
			     fixture->partner.identity_vdm,
			     vdm_resp.vdm_data_objects * sizeof(uint32_t)),
		      0, "Failed to see correct VDM contents");
}

ZTEST_F(ap_vdm_control, test_vdm_request_nak)
{
	struct ec_response_typec_status status;
	struct ec_response_typec_vdm_response vdm_resp;
	uint32_t vdm_req_header = VDO(USB_SID_DISPLAYPORT, 1, CMD_ENTER_MODE) |
				  VDO_SVDM_VERS(VDM_VER20);
	struct typec_vdm_req req = {
		.vdm_data = { vdm_req_header },
		.vdm_data_objects = 1,
		.partner_type = TYPEC_PARTNER_SOP,
	};

	/* Add DisplayPort EnterMode NAK */
	fixture->partner.enter_mode_vdm[VDO_INDEX_HDR] =
		VDO(USB_SID_DISPLAYPORT, /* structured VDM */ true,
		    VDO_CMDT(CMDT_RSP_NAK) | CMD_ENTER_MODE) |
		VDO_SVDM_VERS(VDM_VER20);
	fixture->partner.enter_mode_vdos = VDO_INDEX_HDR + 1;

	host_cmd_typec_control_vdm_req(TEST_PORT, req);
	k_sleep(K_SECONDS(1));

	/* Look for our notification and reply */
	status = host_cmd_typec_status(TEST_PORT);
	zassert_true(status.events & PD_STATUS_EVENT_VDM_REQ_REPLY,
		     "Failed to see VDM NAK event");

	vdm_resp = host_cmd_typec_vdm_response(TEST_PORT);
	zassert_equal(vdm_resp.vdm_response_err, EC_RES_SUCCESS);
	zassert_equal(vdm_resp.partner_type, req.partner_type,
		      "Failed to see correct partner");
	zassert_equal(vdm_resp.vdm_data_objects,
		      fixture->partner.enter_mode_vdos,
		      "Failed to see correct VDO num");
	zassert_equal(memcmp(vdm_resp.vdm_response,
			     fixture->partner.enter_mode_vdm,
			     vdm_resp.vdm_data_objects * sizeof(uint32_t)),
		      0, "Failed to see correct VDM contents");
}

ZTEST_F(ap_vdm_control, test_vdm_request_failed)
{
	struct ec_response_typec_status status;
	struct ec_response_typec_vdm_response vdm_resp;

	uint32_t vdm_req_header = VDO(USB_SID_DISPLAYPORT, 1, CMD_ENTER_MODE) |
				  VDO_SVDM_VERS(VDM_VER20);
	struct typec_vdm_req req = {
		.vdm_data = { vdm_req_header },
		.vdm_data_objects = 1,
		.partner_type = TYPEC_PARTNER_SOP,
	};

	/* Do not advertise an EnterMode response */
	fixture->partner.enter_mode_vdos = 0;

	host_cmd_typec_control_vdm_req(TEST_PORT, req);
	k_sleep(K_SECONDS(1));

	/* Look for our notification and lack of reply */
	status = host_cmd_typec_status(TEST_PORT);
	zassert_true(status.events & PD_STATUS_EVENT_VDM_REQ_FAILED,
		     "Failed to see notice of no reply");

	vdm_resp = host_cmd_typec_vdm_response(TEST_PORT);
	zassert_equal(vdm_resp.vdm_response_err, EC_RES_UNAVAILABLE,
		      "Failed to get unavailable");
}

ZTEST_F(ap_vdm_control, test_vdm_request_bad_port)
{
	struct ec_response_typec_vdm_response vdm_resp;
	struct ec_params_typec_vdm_response params = { .port = 88 };
	struct host_cmd_handler_args args = BUILD_HOST_COMMAND(
		EC_CMD_TYPEC_VDM_RESPONSE, 0, vdm_resp, params);

	zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM,
		      "Failed to see bad port");
}

ZTEST_F(ap_vdm_control, test_vdm_request_in_progress)
{
	struct ec_response_typec_vdm_response vdm_resp;

	uint32_t vdm_req_header = VDO(USB_SID_PD, 1, CMD_DISCOVER_IDENT) |
				  VDO_SVDM_VERS(VDM_VER20);
	struct typec_vdm_req req = {
		.vdm_data = { vdm_req_header },
		.vdm_data_objects = 1,
		.partner_type = TYPEC_PARTNER_SOP,
	};

	host_cmd_typec_control_vdm_req(TEST_PORT, req);

	/* Give no processing time and immediately ask for our result */
	vdm_resp = host_cmd_typec_vdm_response(TEST_PORT);
	zassert_equal(vdm_resp.vdm_response_err, EC_RES_BUSY,
		      "Failed to get busy");
}

ZTEST_F(ap_vdm_control, test_vdm_request_no_send)
{
	struct ec_response_typec_vdm_response vdm_resp;

	/* Check for an error on a fresh connection with no VDM REQ sent */
	vdm_resp = host_cmd_typec_vdm_response(TEST_PORT);
	zassert_equal(vdm_resp.vdm_response_err, EC_RES_UNAVAILABLE,
		      "Failed to see no message ready");
}

ZTEST_F(ap_vdm_control, test_vdm_response_disconnect_clear)
{
	struct ec_response_typec_vdm_response vdm_resp;
	uint32_t vdm_req_header = VDO(USB_SID_PD, 1, CMD_DISCOVER_IDENT) |
				  VDO_SVDM_VERS(VDM_VER20);
	struct typec_vdm_req req = {
		.vdm_data = { vdm_req_header },
		.vdm_data_objects = 1,
		.partner_type = TYPEC_PARTNER_SOP,
	};

	host_cmd_typec_control_vdm_req(TEST_PORT, req);
	k_sleep(K_SECONDS(1));

	/* Now disconnect and verify there's nothing to see here */
	disconnect_source_from_port(fixture->tcpci_emul, fixture->charger_emul);

	vdm_resp = host_cmd_typec_vdm_response(TEST_PORT);
	zassert_equal(vdm_resp.vdm_response_err, EC_RES_UNAVAILABLE,
		      "Failed to see reply cleared");
	zassert_equal(vdm_resp.vdm_data_objects, 0,
		      "Failed to see no VDOs available");
}

/* Tests for the DP entry flow and related requirements */
static void verify_expected_reply(enum typec_partner_type type,
				  int expected_num_objects, uint32_t *contents)
{
	struct ec_response_typec_status status;
	struct ec_response_typec_vdm_response vdm_resp;

	status = host_cmd_typec_status(TEST_PORT);
	zassert_true(status.events & PD_STATUS_EVENT_VDM_REQ_REPLY,
		     "Failed to see VDM ACK event");

	vdm_resp = host_cmd_typec_vdm_response(TEST_PORT);
	zassert_equal(vdm_resp.partner_type, type,
		      "Failed to see correct partner");
	zassert_equal(vdm_resp.vdm_data_objects, expected_num_objects,
		      "Failed to see correct number of objects");
	zassert_equal(memcmp(vdm_resp.vdm_response, contents,
			     expected_num_objects * sizeof(uint32_t)),
		      0, "Failed to see correct VDM contents");
}

static void run_verify_dp_entry(struct ap_vdm_control_fixture *fixture,
				int opos)
{
	/*
	 * Test the full flow of DP entry and configure, to set up for
	 * further test cases.
	 */
	struct typec_vdm_req req = {
		.vdm_data = { VDO(USB_SID_DISPLAYPORT, 1,
				  CMD_ENTER_MODE | VDO_OPOS(opos)) |
			      VDO_SVDM_VERS(VDM_VER20) },
		.vdm_data_objects = 1,
		.partner_type = TYPEC_PARTNER_SOP,
	};

	/* Step 1: EnterMode */
	host_cmd_typec_control_vdm_req(TEST_PORT, req);
	k_sleep(K_MSEC(100));

	verify_expected_reply(req.partner_type,
			      fixture->partner.enter_mode_vdos,
			      fixture->partner.enter_mode_vdm);

	/* Step 2: DP Status */
	req.vdm_data[0] =
		VDO(USB_SID_DISPLAYPORT, 1, CMD_DP_STATUS | VDO_OPOS(opos)) |
		VDO_SVDM_VERS(VDM_VER20);
	req.vdm_data[1] = VDO_DP_STATUS(0, /* HPD IRQ  ... not applicable */
					0, /* HPD level ... not applicable */
					0, /* exit DP? ... no */
					0, /* usb mode? ... no */
					0, /* multi-function ... no */
					0, /* currently enabled ... no */
					0, /* power low? ... no */
					1 /* DP source connected */);
	req.vdm_data_objects = 2;
	req.partner_type = TYPEC_PARTNER_SOP;

	host_cmd_typec_control_vdm_req(TEST_PORT, req);
	k_sleep(K_MSEC(100));

	verify_expected_reply(req.partner_type, fixture->partner.dp_status_vdos,
			      fixture->partner.dp_status_vdm);

	/* Step 3: DP Configure */
	req.vdm_data[0] =
		VDO(USB_SID_DISPLAYPORT, 1, CMD_DP_CONFIG | VDO_OPOS(opos)) |
		VDO_SVDM_VERS(VDM_VER20);
	req.vdm_data[1] = VDO_DP_CFG(MODE_DP_PIN_D, /* pin mode */
				     1, /* DPv1.3 signaling */
				     2); /* Set that partner should be DP sink
					  */
	req.vdm_data_objects = 2;
	req.partner_type = TYPEC_PARTNER_SOP;

	host_cmd_typec_control_vdm_req(TEST_PORT, req);
	k_sleep(K_MSEC(100));

	verify_expected_reply(req.partner_type, fixture->partner.dp_config_vdos,
			      fixture->partner.dp_config_vdm);
}

ZTEST_F(ap_vdm_control, test_vdm_attention_none)
{
	struct ec_response_typec_vdm_response vdm_resp;
	int opos = 1;

	run_verify_dp_entry(fixture, opos);

	/* Check that we have no Attention messages and none in the queue */
	vdm_resp = host_cmd_typec_vdm_response(TEST_PORT);
	zassert_equal(vdm_resp.vdm_attention_objects, 0,
		      "Failed to see empty message");
	zassert_equal(vdm_resp.vdm_attention_left, 0,
		      "Failed to see no more messages");
}

ZTEST_F(ap_vdm_control, test_vdm_attention_one)
{
	uint32_t vdm_attention_data[2];
	int opos = 1;
	struct ec_response_typec_status status;
	struct ec_response_typec_vdm_response vdm_resp;

	run_verify_dp_entry(fixture, opos);

	/* Test that we see our Attention message */
	vdm_attention_data[0] =
		VDO(USB_SID_DISPLAYPORT, 1,
		    VDO_OPOS(opos) | VDO_CMDT(CMDT_INIT) | CMD_ATTENTION) |
		VDO_SVDM_VERS(VDM_VER20);
	vdm_attention_data[1] = VDO_DP_STATUS(1, /* IRQ_HPD */
					      true, /* HPD_HI|LOW - Changed*/
					      0, /* request exit DP */
					      0, /* request exit USB */
					      0, /* MF pref */
					      true, /* DP Enabled */
					      0, /* power low e.g. normal */
					      0x2 /* Connected as Sink */);
	tcpci_partner_send_data_msg(&fixture->partner, PD_DATA_VENDOR_DEF,
				    vdm_attention_data, 2, 0);

	k_sleep(K_MSEC(100));
	/*
	 * Verify the event and the contents of our Attention
	 */
	status = host_cmd_typec_status(TEST_PORT);
	zassert_true(status.events & PD_STATUS_EVENT_VDM_ATTENTION,
		     "Failed to see VDM Attention event");

	vdm_resp = host_cmd_typec_vdm_response(TEST_PORT);
	zassert_equal(vdm_resp.vdm_attention_objects, 2,
		      "Failed to see correct number of objects");
	zassert_equal(vdm_resp.vdm_attention_left, 0,
		      "Failed to see 0 more in queue");
	zassert_equal(memcmp(vdm_resp.vdm_attention, vdm_attention_data,
			     vdm_resp.vdm_attention_objects * sizeof(uint32_t)),
		      0, "Failed to see correct Attention VDM contents");
}

ZTEST_F(ap_vdm_control, test_vdm_attention_two)
{
	uint32_t vdm_attention_first[2];
	uint32_t vdm_attention_second[2];
	int opos = 1;
	struct ec_response_typec_status status;
	struct ec_response_typec_vdm_response vdm_resp;

	run_verify_dp_entry(fixture, opos);

	/* Test that we see our first Attention message followed by second */
	vdm_attention_first[0] =
		VDO(USB_SID_DISPLAYPORT, 1,
		    VDO_OPOS(opos) | VDO_CMDT(CMDT_INIT) | CMD_ATTENTION) |
		VDO_SVDM_VERS(VDM_VER20);
	vdm_attention_first[1] = VDO_DP_STATUS(0, /* IRQ_HPD */
					       false, /* HPD_HI|LOW - Changed*/
					       0, /* request exit DP */
					       0, /* request exit USB */
					       0, /* MF pref */
					       true, /* DP Enabled */
					       0, /* power low e.g. normal */
					       0x2 /* Connected as Sink */);
	tcpci_partner_send_data_msg(&fixture->partner, PD_DATA_VENDOR_DEF,
				    vdm_attention_first, 2, 0);

	k_sleep(K_MSEC(100));

	/* Number two time */
	vdm_attention_second[0] =
		VDO(USB_SID_DISPLAYPORT, 1,
		    VDO_OPOS(opos) | VDO_CMDT(CMDT_INIT) | CMD_ATTENTION) |
		VDO_SVDM_VERS(VDM_VER20);
	vdm_attention_second[1] = VDO_DP_STATUS(1, /* IRQ_HPD */
						true, /* HPD_HI|LOW - Changed*/
						0, /* request exit DP */
						0, /* request exit USB */
						0, /* MF pref */
						true, /* DP Enabled */
						0, /* power low e.g. normal */
						0x2 /* Connected as Sink */);
	tcpci_partner_send_data_msg(&fixture->partner, PD_DATA_VENDOR_DEF,
				    vdm_attention_second, 2, 0);

	k_sleep(K_MSEC(100));
	/*
	 * Verify the event and the contents of our Attention from each in
	 * the proper order
	 */
	status = host_cmd_typec_status(TEST_PORT);
	zassert_true(status.events & PD_STATUS_EVENT_VDM_ATTENTION,
		     "Failed to see VDM Attention event");

	vdm_resp = host_cmd_typec_vdm_response(TEST_PORT);
	zassert_equal(vdm_resp.vdm_attention_objects, 2,
		      "Failed to see correct number of objects");
	zassert_equal(vdm_resp.vdm_attention_left, 1,
		      "Failed to see 1 more in queue");
	zassert_equal(memcmp(vdm_resp.vdm_attention, vdm_attention_first,
			     vdm_resp.vdm_attention_objects * sizeof(uint32_t)),
		      0, "Failed to see correct first Attention VDM contents");

	vdm_resp = host_cmd_typec_vdm_response(TEST_PORT);
	zassert_equal(vdm_resp.vdm_attention_objects, 2,
		      "Failed to see correct number of objects");
	zassert_equal(vdm_resp.vdm_attention_left, 0,
		      "Failed to see 0 more in queue");
	zassert_equal(memcmp(vdm_resp.vdm_attention, vdm_attention_second,
			     vdm_resp.vdm_attention_objects * sizeof(uint32_t)),
		      0, "Failed to see correct second Attention VDM contents");
}

ZTEST_F(ap_vdm_control, test_vdm_attention_disconnect_clear)
{
	uint32_t vdm_attention_data[2];
	int opos = 1;
	struct ec_response_typec_vdm_response vdm_resp;

	run_verify_dp_entry(fixture, opos);

	/* Send an Attention message */
	vdm_attention_data[0] =
		VDO(USB_SID_DISPLAYPORT, 1,
		    VDO_OPOS(opos) | VDO_CMDT(CMDT_INIT) | CMD_ATTENTION);
	vdm_attention_data[0] |= VDO_SVDM_VERS(VDM_VER20);
	vdm_attention_data[1] = VDO_DP_STATUS(1, /* IRQ_HPD */
					      true, /* HPD_HI|LOW - Changed*/
					      0, /* request exit DP */
					      0, /* request exit USB */
					      0, /* MF pref */
					      true, /* DP Enabled */
					      0, /* power low e.g. normal */
					      0x2 /* Connected as Sink */);
	tcpci_partner_send_data_msg(&fixture->partner, PD_DATA_VENDOR_DEF,
				    vdm_attention_data, 2, 0);

	k_sleep(K_SECONDS(1));
	/*
	 * Disconnect and verify no messages are reported
	 */
	disconnect_source_from_port(fixture->tcpci_emul, fixture->charger_emul);

	vdm_resp = host_cmd_typec_vdm_response(TEST_PORT);
	zassert_equal(vdm_resp.vdm_attention_objects, 0,
		      "Failed to see empty message");
	zassert_equal(vdm_resp.vdm_attention_left, 0,
		      "Failed to see no more messages");
}