1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 
8 
9 /* test_conn_impl is separated into its own file specifically in order to test that
10  * CONN_MGR_CONN_DECLARE_PUBLIC functions as expected.
11  */
12 
13 
14 #ifndef ZEPHYR_INCLUDE_TEST_CONN_IMPL_H_
15 #define ZEPHYR_INCLUDE_TEST_CONN_IMPL_H_
16 
17 #include <zephyr/net/conn_mgr_connectivity_impl.h>
18 #include <zephyr/net/net_if.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #define TEST_CONN_OPT_X 0
25 #define TEST_CONN_OPT_Y 1
26 #define TEST_CONN_DATA_LEN 50
27 
28 struct test_conn_data {
29 	/* The number of times an A-implementation API func has been called (other than init) */
30 	int call_cnt_a;
31 
32 	/* The number of times a B-implementation API func has been called (other than init) */
33 	int call_cnt_b;
34 
35 	/* Increases on each connect call, decreases on each disconnect call */
36 	int conn_bal;
37 
38 	/* The number of times A-implementation init was called (should always be 1) */
39 	int init_calls_a;
40 
41 	/* The number of times B-implementation init was called (should always be 1) */
42 	int init_calls_b;
43 
44 	/* If nonzero, an error code the APIs should return. */
45 	int api_err;
46 
47 	/* If true, the implementation should time out on connect. */
48 	bool timeout;
49 
50 	/* If nonzero, the implementation should fail to connect and raise this fatal error. */
51 	int fatal_error;
52 
53 	/* Places to store data from set_opt calls */
54 	char data_x[TEST_CONN_DATA_LEN + 1];
55 	char data_y[TEST_CONN_DATA_LEN + 1];
56 };
57 
58 /* Create test L2 connectivity implementations A and B
59  *
60  * A and B share generic connect/disconnect implementations that differ only in which call counter
61  * they increment.
62  *
63  * Additionally, A has conn_opt callbacks, whereas B does not.
64  */
65 #define TEST_L2_CONN_IMPL_A_CTX_TYPE struct test_conn_data
66 CONN_MGR_CONN_DECLARE_PUBLIC(TEST_L2_CONN_IMPL_A);
67 
68 #define TEST_L2_CONN_IMPL_B_CTX_TYPE struct test_conn_data
69 CONN_MGR_CONN_DECLARE_PUBLIC(TEST_L2_CONN_IMPL_B);
70 
71 /* Create an invalid L2 connectivity implementation with NULL API */
72 #define TEST_L2_CONN_IMPL_N_CTX_TYPE struct test_conn_data
73 CONN_MGR_CONN_DECLARE_PUBLIC(TEST_L2_CONN_IMPL_N);
74 
75 /* Create an L2 connectivity implementation without the optional init */
76 #define TEST_L2_CONN_IMPL_NI_CTX_TYPE struct test_conn_data
77 CONN_MGR_CONN_DECLARE_PUBLIC(TEST_L2_CONN_IMPL_NI);
78 
79 #define SIMULATED_EVENT_DELAY_MS 100
80 #define SIMULATED_EVENT_DELAY_TIME K_MSEC(SIMULATED_EVENT_DELAY_MS)
81 #define SIMULATED_EVENT_WAIT_TIME K_MSEC(SIMULATED_EVENT_DELAY_MS + 10)
82 
83 /* Helper API for controlling implementations from tests */
84 
85 /**
86  * @brief Simulate a connection loss on the target iface.
87  *
88  * @param target - iface to simulate connection loss on.
89  */
90 void simulate_connection_loss(struct net_if *target);
91 
92 /**
93  * @brief Simulate a fatal error on the target iface
94  *
95  * Please do not simulate events on more than one iface at a time.
96  *
97  * @param target - iface to simulate fatal error on.
98  * @param reason - Reason to be given for the fatal error.
99  */
100 void simulate_fatal_error(struct net_if *target, int reason);
101 
102 #ifdef __cplusplus
103 }
104 #endif
105 
106 #endif /* ZEPHYR_INCLUDE_TEST_CONN_IMPL_H_ */
107