1 /*
2 * Copyright (c) 2024 Linumiz
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8 #include <zephyr/net/net_if.h>
9 #include <zephyr/net/ocpp.h>
10 #include <zephyr/random/random.h>
11
test_ocpp_charge_cycle(ocpp_session_handle_t hndl)12 static void test_ocpp_charge_cycle(ocpp_session_handle_t hndl)
13 {
14 int ret;
15 int retry = 3;
16 enum ocpp_auth_status status;
17 const uint32_t timeout_ms = 500;
18
19 while (retry--) {
20 ret = ocpp_authorize(hndl, "ZepId00", &status, timeout_ms);
21
22 TC_PRINT("auth req ret %d status %d", ret, status);
23 k_sleep(K_SECONDS(1));
24
25 if (ret == 0) {
26 break;
27 }
28 }
29 zassert_equal(ret, 0, "CP authorize fail %d");
30 zassert_equal(status, OCPP_AUTH_ACCEPTED, "idtag not authorized");
31
32 ret = ocpp_start_transaction(hndl, sys_rand32_get(), 1, timeout_ms);
33 zassert_equal(ret, 0, "start transaction fail");
34
35 /* Active charging session */
36 k_sleep(K_SECONDS(20));
37 ret = ocpp_stop_transaction(hndl, sys_rand32_get(), timeout_ms);
38 zassert_equal(ret, 0, "stop transaction fail");
39 }
40
test_ocpp_user_notify_cb(enum ocpp_notify_reason reason,union ocpp_io_value * io,void * user_data)41 static int test_ocpp_user_notify_cb(enum ocpp_notify_reason reason,
42 union ocpp_io_value *io,
43 void *user_data)
44 {
45 switch (reason) {
46 case OCPP_USR_GET_METER_VALUE:
47 if (OCPP_OMM_ACTIVE_ENERGY_TO_EV == io->meter_val.mes) {
48 snprintf(io->meter_val.val, CISTR50, "%u",
49 sys_rand32_get());
50
51 TC_PRINT("mtr reading val %s con %d",
52 io->meter_val.val,
53 io->meter_val.id_con);
54 return 0;
55 }
56 break;
57
58 case OCPP_USR_START_CHARGING:
59 TC_PRINT("start charging idtag %s connector %d\n",
60 io->start_charge.idtag,
61 io->stop_charge.id_con);
62 return 0;
63
64 case OCPP_USR_STOP_CHARGING:
65 TC_PRINT("stop charging connector %d\n", io->stop_charge.id_con);
66 return 0;
67
68 case OCPP_USR_UNLOCK_CONNECTOR:
69 TC_PRINT("unlock connector %d\n", io->unlock_con.id_con);
70 return 0;
71 }
72
73 return -ENOTSUP;
74 }
75
test_ocpp_init(void)76 int test_ocpp_init(void)
77 {
78 int ret;
79
80 struct ocpp_cp_info cpi = { "basic", "zephyr", .num_of_con = 1 };
81 struct ocpp_cs_info csi = { "122.165.245.213", /* ssh.linumiz.com */
82 "/steve/websocket/CentralSystemService/zephyr",
83 8180,
84 AF_INET };
85
86 net_dhcpv4_start(net_if_get_default());
87
88 /* wait for device dhcp ip recive */
89 k_sleep(K_SECONDS(3));
90
91 ret = ocpp_init(&cpi,
92 &csi,
93 test_ocpp_user_notify_cb,
94 NULL);
95 if (ret < 0) {
96 TC_PRINT("ocpp init failed %d\n", ret);
97 return ret;
98 }
99
100 return 0;
101 }
102
ZTEST(net_ocpp,test_ocpp_chargepoint)103 ZTEST(net_ocpp, test_ocpp_chargepoint)
104 {
105 int ret;
106 ocpp_session_handle_t hndl = NULL;
107
108 ret = test_ocpp_init();
109 zassert_equal(ret, 0, "ocpp init failed %d", ret);
110
111 ret = ocpp_session_open(&hndl);
112 zassert_equal(ret, 0, "session open failed %d", ret);
113
114 k_sleep(K_SECONDS(2));
115 test_ocpp_charge_cycle(hndl);
116
117 ocpp_session_close(hndl);
118 }
119
120 ZTEST_SUITE(net_ocpp, NULL, NULL, NULL, NULL, NULL);
121