1 /*
2 * Copyright (c) 2023 Nordic Semiconductor
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "friendship_common.h"
8 #include "mesh_test.h"
9
10 #define LOG_MODULE_NAME friendship_common
11
12 #include <zephyr/logging/log.h>
13 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
14
15 static uint16_t bt_mesh_test_friend_lpn_addr;
16 static ATOMIC_DEFINE(bt_mesh_test_friendship_state, BT_MESH_TEST_FRIENDSHIP_FLAGS);
17 static struct k_sem bt_mesh_test_friendship_events[BT_MESH_TEST_FRIENDSHIP_FLAGS];
18
evt_signal(enum bt_mesh_test_friendship_evt_flags evt)19 static void evt_signal(enum bt_mesh_test_friendship_evt_flags evt)
20 {
21 atomic_set_bit(bt_mesh_test_friendship_state, evt);
22 k_sem_give(&bt_mesh_test_friendship_events[evt]);
23 }
24
bt_mesh_test_friendship_evt_wait(enum bt_mesh_test_friendship_evt_flags evt,k_timeout_t timeout)25 int bt_mesh_test_friendship_evt_wait(enum bt_mesh_test_friendship_evt_flags evt,
26 k_timeout_t timeout)
27 {
28 return k_sem_take(&bt_mesh_test_friendship_events[evt], timeout);
29 }
30
bt_mesh_test_friendship_evt_clear(enum bt_mesh_test_friendship_evt_flags evt)31 void bt_mesh_test_friendship_evt_clear(enum bt_mesh_test_friendship_evt_flags evt)
32 {
33 atomic_clear_bit(bt_mesh_test_friendship_state, evt);
34 k_sem_reset(&bt_mesh_test_friendship_events[evt]);
35 }
36
bt_mesh_test_friendship_state_check(enum bt_mesh_test_friendship_evt_flags evt)37 bool bt_mesh_test_friendship_state_check(enum bt_mesh_test_friendship_evt_flags evt)
38 {
39 return atomic_test_bit(bt_mesh_test_friendship_state, evt);
40 }
41
bt_mesh_test_friendship_addr_get(void)42 uint16_t bt_mesh_test_friendship_addr_get(void)
43 {
44 return bt_mesh_test_friend_lpn_addr;
45 }
46
bt_mesh_test_friendship_init(int max_evt_count)47 void bt_mesh_test_friendship_init(int max_evt_count)
48 {
49 for (int i = 0; i < ARRAY_SIZE(bt_mesh_test_friendship_events); i++) {
50 k_sem_init(&bt_mesh_test_friendship_events[i], 0, max_evt_count);
51 }
52 }
53
friend_established(uint16_t net_idx,uint16_t lpn_addr,uint8_t recv_delay,uint32_t polltimeout)54 static void friend_established(uint16_t net_idx, uint16_t lpn_addr,
55 uint8_t recv_delay, uint32_t polltimeout)
56 {
57 LOG_INF("Friend: established with 0x%04x", lpn_addr);
58 bt_mesh_test_friend_lpn_addr = lpn_addr;
59 evt_signal(BT_MESH_TEST_FRIEND_ESTABLISHED);
60 }
61
friend_terminated(uint16_t net_idx,uint16_t lpn_addr)62 static void friend_terminated(uint16_t net_idx, uint16_t lpn_addr)
63 {
64 LOG_INF("Friend: terminated with 0x%04x", lpn_addr);
65 evt_signal(BT_MESH_TEST_FRIEND_TERMINATED);
66 }
67
friend_polled(uint16_t net_idx,uint16_t lpn_addr)68 static void friend_polled(uint16_t net_idx, uint16_t lpn_addr)
69 {
70 LOG_INF("Friend: Poll from 0x%04x", lpn_addr);
71 evt_signal(BT_MESH_TEST_FRIEND_POLLED);
72 }
73
74 BT_MESH_FRIEND_CB_DEFINE(friend) = {
75 .established = friend_established,
76 .terminated = friend_terminated,
77 .polled = friend_polled,
78 };
79
lpn_established(uint16_t net_idx,uint16_t friend_addr,uint8_t queue_size,uint8_t recv_window)80 static void lpn_established(uint16_t net_idx, uint16_t friend_addr,
81 uint8_t queue_size, uint8_t recv_window)
82 {
83 LOG_INF("LPN: established with 0x%04x", friend_addr);
84 evt_signal(BT_MESH_TEST_LPN_ESTABLISHED);
85 }
86
lpn_terminated(uint16_t net_idx,uint16_t friend_addr)87 static void lpn_terminated(uint16_t net_idx, uint16_t friend_addr)
88 {
89 LOG_INF("LPN: terminated with 0x%04x", friend_addr);
90 evt_signal(BT_MESH_TEST_LPN_TERMINATED);
91 }
92
lpn_polled(uint16_t net_idx,uint16_t friend_addr,bool retry)93 static void lpn_polled(uint16_t net_idx, uint16_t friend_addr, bool retry)
94 {
95 LOG_INF("LPN: Polling 0x%04x (%s)", friend_addr,
96 retry ? "retry" : "initial");
97 evt_signal(BT_MESH_TEST_LPN_POLLED);
98 }
99
100 BT_MESH_LPN_CB_DEFINE(lpn) = {
101 .established = lpn_established,
102 .polled = lpn_polled,
103 .terminated = lpn_terminated,
104 };
105