Skip to content

Commit 2054179

Browse files
committed
tests: net: ocpp: Add test suit for OCPP lib
Add test suit for OCPP lib with one complete charging session with a random meter values. Signed-off-by: Saravanan Sekar <[email protected]>
1 parent c7e39b1 commit 2054179

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed

tests/net/lib/ocpp/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(ocpp_test)
7+
8+
target_include_directories(app PRIVATE
9+
${ZEPHYR_BASE}/subsys/net/ip
10+
)
11+
FILE(GLOB app_sources src/*.c)
12+
target_sources(app PRIVATE ${app_sources})

tests/net/lib/ocpp/prj.conf

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# HTTP & Websocket
2+
CONFIG_HTTP_CLIENT=y
3+
CONFIG_WEBSOCKET_CLIENT=y
4+
5+
# Networking config
6+
CONFIG_NETWORKING=y
7+
CONFIG_NET_IPV4=y
8+
CONFIG_NET_IPV6=n
9+
CONFIG_NET_TCP=y
10+
CONFIG_NET_ARP=y
11+
CONFIG_NET_UDP=y
12+
CONFIG_NET_SOCKETS=y
13+
CONFIG_NET_SOCKETS_POLL_MAX=4
14+
CONFIG_NET_L2_ETHERNET=y
15+
CONFIG_ETH_DRIVER=y
16+
CONFIG_LOG=y
17+
CONFIG_NET_LOG=y
18+
CONFIG_LOG_DEFAULT_LEVEL=3
19+
CONFIG_NET_DHCPV4=y
20+
21+
# OCPP
22+
CONFIG_OCPP=y
23+
CONFIG_TEST_EXTRA_STACK_SIZE=2048
24+
CONFIG_MAIN_STACK_SIZE=2048
25+
CONFIG_HEAP_MEM_POOL_SIZE=15000
26+
27+
CONFIG_JSON_LIBRARY=y
28+
CONFIG_PICOLIBC=y
29+
30+
CONFIG_NET_TX_STACK_SIZE=2048
31+
CONFIG_NET_RX_STACK_SIZE=2048
32+
CONFIG_NET_PKT_RX_COUNT=28
33+
CONFIG_NET_BUF_RX_COUNT=60
34+
CONFIG_NET_MGMT=y
35+
CONFIG_NET_MGMT_EVENT=y
36+
37+
CONFIG_ENTROPY_GENERATOR=y
38+
CONFIG_TEST_RANDOM_GENERATOR=y
39+
40+
CONFIG_POSIX_API=y
41+
CONFIG_POSIX_CLOCK_SELECTION=y
42+
43+
CONFIG_ZTEST=y

tests/net/lib/ocpp/src/main.c

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
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) {
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+
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+
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) {
96+
TC_PRINT("ocpp init failed %d\n", ret);
97+
return ret;
98+
}
99+
100+
return 0;
101+
}
102+
103+
ZTEST_EXPECT_FAIL(net_ocpp, test_ocpp_chargepoint);
104+
ZTEST(net_ocpp, test_ocpp_chargepoint)
105+
{
106+
int ret;
107+
ocpp_session_handle_t hndl = NULL;
108+
109+
ret = test_ocpp_init();
110+
zassert_equal(ret, 0, "ocpp init failed %d", ret);
111+
112+
ret = ocpp_session_open(&hndl);
113+
zassert_equal(ret, 0, "session open failed %d", ret);
114+
115+
k_sleep(K_SECONDS(2));
116+
test_ocpp_charge_cycle(hndl);
117+
118+
ocpp_session_close(hndl);
119+
}
120+
121+
ZTEST_SUITE(net_ocpp, NULL, NULL, NULL, NULL, NULL);

tests/net/lib/ocpp/testcase.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tests:
2+
net.ocpp:
3+
depends_on: netif
4+
platform_allow:
5+
- native_sim/native
6+
min_ram: 16
7+
tags:
8+
- net
9+
- ocpp

0 commit comments

Comments
 (0)