1 /*
2 * Copyright 2022 Google LLC
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /**
8 * @defgroup driver_bc12_tests
9 * @ingroup all_tests
10 * @{
11 * @defgroup t_bc12_charging_mode
12 * @brief TestPurpose: Verify BC1.2 devices configured in charging mode.
13 * @}
14 */
15
16 #include <zephyr/drivers/usb/usb_bc12.h>
17 #include <zephyr/drivers/usb/emul_bc12.h>
18 #include <zephyr/ztest.h>
19
20 #include <zephyr/logging/log.h>
21
22 #define LOG_MODULE_NAME test_bc12_charging
23 LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL_INF);
24
25 struct bc12_pi3usb9201_charging_mode_fixture {
26 const struct device *bc12_dev;
27 const struct emul *bc12_emul;
28 struct bc12_partner_state partner_state;
29 };
30
bc12_test_result_cb(const struct device * dev,struct bc12_partner_state * state,void * user_data)31 static void bc12_test_result_cb(const struct device *dev, struct bc12_partner_state *state,
32 void *user_data)
33 {
34 struct bc12_pi3usb9201_charging_mode_fixture *fixture = user_data;
35
36 if (state) {
37 if (state->bc12_role == BC12_PORTABLE_DEVICE) {
38 LOG_INF("charging partner: type %d, voltage %d, current %d", state->type,
39 state->voltage_uv, state->current_ua);
40 } else if (state->bc12_role == BC12_CHARGING_PORT) {
41 LOG_INF("portable device partner: connected %d",
42 state->pd_partner_connected);
43 }
44 fixture->partner_state = *state;
45 } else {
46 LOG_INF("callback: partner disconnect");
47 fixture->partner_state.type = BC12_TYPE_NONE;
48 fixture->partner_state.current_ua = 0;
49 fixture->partner_state.voltage_uv = 0;
50 }
51 }
52
ZTEST_USER_F(bc12_pi3usb9201_charging_mode,test_bc12_charging_mode)53 ZTEST_USER_F(bc12_pi3usb9201_charging_mode, test_bc12_charging_mode)
54 {
55 bc12_set_role(fixture->bc12_dev, BC12_CHARGING_PORT);
56
57 bc12_emul_set_pd_partner(fixture->bc12_emul, true);
58 k_sleep(K_MSEC(100));
59
60 /* The BC1.2 driver should invoke the callback on plug event */
61 zassert_true(fixture->partner_state.pd_partner_connected);
62
63 bc12_emul_set_pd_partner(fixture->bc12_emul, false);
64 k_sleep(K_MSEC(100));
65
66 /* The BC1.2 driver should invoke the callback on unplug event */
67 zassert_false(fixture->partner_state.pd_partner_connected);
68 }
69
bc12_before(void * data)70 static void bc12_before(void *data)
71 {
72 struct bc12_pi3usb9201_charging_mode_fixture *fixture = data;
73
74 memset(&fixture->partner_state, 0, sizeof(struct bc12_partner_state));
75
76 bc12_set_result_cb(fixture->bc12_dev, &bc12_test_result_cb, fixture);
77 }
78
bc12_after(void * data)79 static void bc12_after(void *data)
80 {
81 struct bc12_pi3usb9201_charging_mode_fixture *fixture = data;
82
83 bc12_set_result_cb(fixture->bc12_dev, NULL, NULL);
84 bc12_set_role(fixture->bc12_dev, BC12_DISCONNECTED);
85 }
86
bc12_setup(void)87 static void *bc12_setup(void)
88 {
89 static struct bc12_pi3usb9201_charging_mode_fixture fixture = {
90 .bc12_dev = DEVICE_DT_GET(DT_ALIAS(bc12)),
91 .bc12_emul = EMUL_DT_GET(DT_ALIAS(bc12)),
92 };
93
94 zassert_not_null(fixture.bc12_dev);
95 zassert_not_null(fixture.bc12_emul);
96 zassert_true(device_is_ready(fixture.bc12_dev));
97
98 return &fixture;
99 }
100
101 ZTEST_SUITE(bc12_pi3usb9201_charging_mode, NULL, bc12_setup, bc12_before, bc12_after, NULL);
102