1 /*
2  * Copyright (c) 2024 Trackunit Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/modem/pipe.h>
9 #include <zephyr/modem/pipelink.h>
10 #include <zephyr/sys/atomic.h>
11 #include <zephyr/ztest.h>
12 
13 #define TEST_NODE DT_NODELABEL(test_node)
14 #define TEST_PIPELINK_NAME test_pipelink_0
15 
16 #define TEST_EVENTS_CONNECTED_BIT    0
17 #define TEST_EVENTS_DISCONNECTED_BIT 1
18 
19 MODEM_PIPELINK_DT_DECLARE(TEST_NODE, TEST_PIPELINK_NAME);
20 
21 static struct modem_pipelink *test_pipelink =
22 	MODEM_PIPELINK_DT_GET(TEST_NODE, TEST_PIPELINK_NAME);
23 
24 static struct modem_pipe test_pipe;
25 static atomic_t events;
26 static uint32_t test_user_data;
27 
test_pipelink_callback(struct modem_pipelink * link,enum modem_pipelink_event event,void * user_data)28 static void test_pipelink_callback(struct modem_pipelink *link,
29 				   enum modem_pipelink_event event,
30 				   void *user_data)
31 {
32 	zassert_equal(test_pipelink, link);
33 	zassert_equal((void *)(&test_user_data), user_data);
34 
35 	switch (event) {
36 	case MODEM_PIPELINK_EVENT_CONNECTED:
37 		atomic_set_bit(&events, TEST_EVENTS_CONNECTED_BIT);
38 		break;
39 
40 	case MODEM_PIPELINK_EVENT_DISCONNECTED:
41 		atomic_set_bit(&events, TEST_EVENTS_DISCONNECTED_BIT);
42 		break;
43 
44 	default:
45 		zassert(0, "invalid event");
46 		break;
47 	}
48 }
49 
test_reset_events(void)50 static void test_reset_events(void)
51 {
52 	atomic_set(&events, 0);
53 }
54 
test_setup(void)55 static void *test_setup(void)
56 {
57 	modem_pipelink_init(test_pipelink, &test_pipe);
58 	test_reset_events();
59 	return NULL;
60 }
61 
test_before(void * f)62 static void test_before(void *f)
63 {
64 	ARG_UNUSED(f);
65 
66 	modem_pipelink_notify_disconnected(test_pipelink);
67 	modem_pipelink_release(test_pipelink);
68 	test_reset_events();
69 }
70 
ZTEST(modem_pipelink,test_connect_not_attached)71 ZTEST(modem_pipelink, test_connect_not_attached)
72 {
73 	zassert_false(modem_pipelink_is_connected(test_pipelink));
74 	modem_pipelink_notify_connected(test_pipelink);
75 	zassert_true(modem_pipelink_is_connected(test_pipelink));
76 	modem_pipelink_notify_disconnected(test_pipelink);
77 	zassert_false(modem_pipelink_is_connected(test_pipelink));
78 }
79 
ZTEST(modem_pipelink,test_connect_attached)80 ZTEST(modem_pipelink, test_connect_attached)
81 {
82 	modem_pipelink_attach(test_pipelink, test_pipelink_callback, &test_user_data);
83 	modem_pipelink_notify_connected(test_pipelink);
84 	zassert_true(atomic_test_bit(&events, TEST_EVENTS_CONNECTED_BIT));
85 	zassert_false(atomic_test_bit(&events, TEST_EVENTS_DISCONNECTED_BIT));
86 	test_reset_events();
87 	modem_pipelink_notify_connected(test_pipelink);
88 	zassert_false(atomic_test_bit(&events, TEST_EVENTS_CONNECTED_BIT));
89 	zassert_false(atomic_test_bit(&events, TEST_EVENTS_DISCONNECTED_BIT));
90 	modem_pipelink_notify_disconnected(test_pipelink);
91 	zassert_false(atomic_test_bit(&events, TEST_EVENTS_CONNECTED_BIT));
92 	zassert_true(atomic_test_bit(&events, TEST_EVENTS_DISCONNECTED_BIT));
93 	test_reset_events();
94 	modem_pipelink_notify_disconnected(test_pipelink);
95 	zassert_false(atomic_test_bit(&events, TEST_EVENTS_CONNECTED_BIT));
96 	zassert_false(atomic_test_bit(&events, TEST_EVENTS_DISCONNECTED_BIT));
97 }
98 
99 ZTEST_SUITE(modem_pipelink, NULL, test_setup, test_before, NULL, NULL);
100