Lines Matching refs:frnd

88 static bool friend_is_allocated(const struct bt_mesh_friend *frnd)  in friend_is_allocated()  argument
90 return frnd->subnet != NULL; in friend_is_allocated()
93 static bool is_lpn_unicast(struct bt_mesh_friend *frnd, uint16_t addr) in is_lpn_unicast() argument
95 if (frnd->lpn == BT_MESH_ADDR_UNASSIGNED) { in is_lpn_unicast()
99 return (addr >= frnd->lpn && addr < (frnd->lpn + frnd->num_elem)); in is_lpn_unicast()
109 for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { in bt_mesh_friend_find()
110 struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; in bt_mesh_friend_find() local
112 if (valid && !friend_is_allocated(frnd)) { in bt_mesh_friend_find()
116 if (established && !frnd->established) { in bt_mesh_friend_find()
121 (!frnd->subnet || frnd->subnet->net_idx != net_idx)) { in bt_mesh_friend_find()
125 if (is_lpn_unicast(frnd, lpn_addr)) { in bt_mesh_friend_find()
126 return frnd; in bt_mesh_friend_find()
133 static int friend_cred_create(struct bt_mesh_friend *frnd, uint8_t idx) in friend_cred_create() argument
135 return bt_mesh_friend_cred_create(&frnd->cred[idx], frnd->lpn, in friend_cred_create()
137 frnd->lpn_counter, frnd->counter, in friend_cred_create()
138 &frnd->subnet->keys[idx].net); in friend_cred_create()
155 static int32_t recv_delay(struct bt_mesh_friend *frnd) in recv_delay() argument
158 return (int32_t)frnd->recv_delay + (CONFIG_BT_MESH_FRIEND_RECV_WIN / 5); in recv_delay()
160 return frnd->recv_delay; in recv_delay()
164 static void friend_clear(struct bt_mesh_friend *frnd) in friend_clear() argument
168 LOG_DBG("LPN 0x%04x", frnd->lpn); in friend_clear()
171 (void)k_work_cancel_delayable(&frnd->timer); in friend_clear()
173 for (i = 0; i < ARRAY_SIZE(frnd->cred); i++) { in friend_clear()
174 if (frnd->subnet->keys[i].valid) { in friend_clear()
175 bt_mesh_friend_cred_destroy(&frnd->cred[i]); in friend_clear()
178 memset(frnd->cred, 0, sizeof(frnd->cred)); in friend_clear()
180 if (frnd->last) { in friend_clear()
181 net_buf_unref(frnd->last); in friend_clear()
182 frnd->last = NULL; in friend_clear()
185 purge_buffers(&frnd->queue); in friend_clear()
187 for (i = 0; i < ARRAY_SIZE(frnd->seg); i++) { in friend_clear()
188 struct bt_mesh_friend_seg *seg = &frnd->seg[i]; in friend_clear()
195 if (frnd->established && cb->terminated) { in friend_clear()
196 cb->terminated(frnd->subnet->net_idx, frnd->lpn); in friend_clear()
200 frnd->counter++; in friend_clear()
201 frnd->subnet = NULL; in friend_clear()
202 frnd->established = 0U; in friend_clear()
203 frnd->pending_buf = 0U; in friend_clear()
204 frnd->fsn = 0U; in friend_clear()
205 frnd->queue_size = 0U; in friend_clear()
206 frnd->pending_req = 0U; in friend_clear()
207 (void)memset(frnd->sub_list, 0, sizeof(frnd->sub_list)); in friend_clear()
216 for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { in bt_mesh_friends_clear()
217 struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; in bt_mesh_friends_clear() local
219 if (!friend_is_allocated(frnd)) { in bt_mesh_friends_clear()
223 friend_clear(frnd); in bt_mesh_friends_clear()
227 static void enqueue_update(struct bt_mesh_friend *frnd, uint8_t md);
235 for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { in bt_mesh_friend_sec_update()
236 struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; in bt_mesh_friend_sec_update() local
238 if (!friend_is_allocated(frnd)) { in bt_mesh_friend_sec_update()
243 frnd->subnet->net_idx == net_idx) { in bt_mesh_friend_sec_update()
244 enqueue_update(frnd, 0x00); in bt_mesh_friend_sec_update()
252 struct bt_mesh_friend *frnd; in bt_mesh_friend_clear() local
272 frnd = bt_mesh_friend_find(rx->sub->net_idx, lpn_addr, false, false); in bt_mesh_friend_clear()
273 if (!frnd) { in bt_mesh_friend_clear()
284 if (lpn_counter - frnd->lpn_counter > 255) { in bt_mesh_friend_clear()
285 LOG_WRN("LPN Counter out of range (old %u new %u)", frnd->lpn_counter, lpn_counter); in bt_mesh_friend_clear()
297 friend_clear(frnd); in bt_mesh_friend_clear()
302 static void friend_sub_add(struct bt_mesh_friend *frnd, uint16_t addr) in friend_sub_add() argument
306 for (int i = 0; i < ARRAY_SIZE(frnd->sub_list); i++) { in friend_sub_add()
307 if (frnd->sub_list[i] == addr) { in friend_sub_add()
311 if (frnd->sub_list[i] == BT_MESH_ADDR_UNASSIGNED) { in friend_sub_add()
317 frnd->sub_list[empty_idx] = addr; in friend_sub_add()
318 LOG_DBG("%04x added %04x to subscription list", frnd->lpn, addr); in friend_sub_add()
324 static void friend_sub_rem(struct bt_mesh_friend *frnd, uint16_t addr) in friend_sub_rem() argument
328 for (i = 0; i < ARRAY_SIZE(frnd->sub_list); i++) { in friend_sub_rem()
329 if (frnd->sub_list[i] == addr) { in friend_sub_rem()
330 LOG_DBG("%04x removed %04x from subscription list", frnd->lpn, addr); in friend_sub_rem()
331 frnd->sub_list[i] = BT_MESH_ADDR_UNASSIGNED; in friend_sub_rem()
337 static struct net_buf *create_friend_pdu(struct bt_mesh_friend *frnd, in create_friend_pdu() argument
376 static int unseg_app_sdu_unpack(struct bt_mesh_friend *frnd, in unseg_app_sdu_unpack() argument
385 .net_idx = frnd->subnet->net_idx, in unseg_app_sdu_unpack()
390 meta->subnet = frnd->subnet; in unseg_app_sdu_unpack()
413 static int unseg_app_sdu_decrypt(struct bt_mesh_friend *frnd, in unseg_app_sdu_decrypt() argument
435 static int unseg_app_sdu_encrypt(struct bt_mesh_friend *frnd, in unseg_app_sdu_encrypt() argument
449 static int unseg_app_sdu_prepare(struct bt_mesh_friend *frnd, in unseg_app_sdu_prepare() argument
459 err = unseg_app_sdu_unpack(frnd, buf, &meta); in unseg_app_sdu_prepare()
473 err = unseg_app_sdu_decrypt(frnd, buf, &meta); in unseg_app_sdu_prepare()
481 err = unseg_app_sdu_encrypt(frnd, buf, &meta); in unseg_app_sdu_prepare()
489 static int encrypt_friend_pdu(struct bt_mesh_friend *frnd, struct net_buf *buf, in encrypt_friend_pdu() argument
498 cred = &frnd->subnet->keys[SUBNET_KEY_TX_IDX(frnd->subnet)] in encrypt_friend_pdu()
501 cred = &frnd->cred[SUBNET_KEY_TX_IDX(frnd->subnet)]; in encrypt_friend_pdu()
510 err = unseg_app_sdu_prepare(frnd, buf); in encrypt_friend_pdu()
541 static struct net_buf *encode_friend_ctl(struct bt_mesh_friend *frnd, in encode_friend_ctl() argument
547 LOG_DBG("LPN 0x%04x", frnd->lpn); in encode_friend_ctl()
552 info.dst = frnd->lpn; in encode_friend_ctl()
561 return create_friend_pdu(frnd, &info, sdu); in encode_friend_ctl()
564 static struct net_buf *encode_update(struct bt_mesh_friend *frnd, uint8_t md) in encode_update() argument
569 __ASSERT_NO_MSG(friend_is_allocated(frnd)); in encode_update()
571 LOG_DBG("lpn 0x%04x md 0x%02x", frnd->lpn, md); in encode_update()
576 upd->flags = bt_mesh_net_flags(frnd->subnet); in encode_update()
580 return encode_friend_ctl(frnd, TRANS_CTL_OP_FRIEND_UPDATE, &sdu); in encode_update()
583 static void enqueue_sub_cfm(struct bt_mesh_friend *frnd, uint8_t xact) in enqueue_sub_cfm() argument
589 LOG_DBG("lpn 0x%04x xact 0x%02x", frnd->lpn, xact); in enqueue_sub_cfm()
596 buf = encode_friend_ctl(frnd, TRANS_CTL_OP_FRIEND_SUB_CFM, &sdu); in enqueue_sub_cfm()
602 if (encrypt_friend_pdu(frnd, buf, false)) { in enqueue_sub_cfm()
606 if (frnd->last) { in enqueue_sub_cfm()
608 net_buf_unref(frnd->last); in enqueue_sub_cfm()
611 frnd->last = buf; in enqueue_sub_cfm()
612 frnd->send_last = 1U; in enqueue_sub_cfm()
615 static void friend_recv_delay(struct bt_mesh_friend *frnd) in friend_recv_delay() argument
617 int32_t delay = recv_delay(frnd); in friend_recv_delay()
619 frnd->pending_req = 1U; in friend_recv_delay()
620 k_work_reschedule(&frnd->timer, K_MSEC(delay)); in friend_recv_delay()
627 struct bt_mesh_friend *frnd; in bt_mesh_friend_sub_add() local
635 frnd = bt_mesh_friend_find(rx->sub->net_idx, rx->ctx.addr, true, true); in bt_mesh_friend_sub_add()
636 if (!frnd) { in bt_mesh_friend_sub_add()
641 if (frnd->pending_buf) { in bt_mesh_friend_sub_add()
646 friend_recv_delay(frnd); in bt_mesh_friend_sub_add()
651 friend_sub_add(frnd, net_buf_simple_pull_be16(buf)); in bt_mesh_friend_sub_add()
654 enqueue_sub_cfm(frnd, xact); in bt_mesh_friend_sub_add()
662 struct bt_mesh_friend *frnd; in bt_mesh_friend_sub_rem() local
670 frnd = bt_mesh_friend_find(rx->sub->net_idx, rx->ctx.addr, true, true); in bt_mesh_friend_sub_rem()
671 if (!frnd) { in bt_mesh_friend_sub_rem()
676 if (frnd->pending_buf) { in bt_mesh_friend_sub_rem()
681 friend_recv_delay(frnd); in bt_mesh_friend_sub_rem()
686 friend_sub_rem(frnd, net_buf_simple_pull_be16(buf)); in bt_mesh_friend_sub_rem()
689 enqueue_sub_cfm(frnd, xact); in bt_mesh_friend_sub_rem()
694 static void enqueue_buf(struct bt_mesh_friend *frnd, struct net_buf *buf) in enqueue_buf() argument
696 net_buf_slist_put(&frnd->queue, buf); in enqueue_buf()
697 frnd->queue_size++; in enqueue_buf()
700 static void enqueue_update(struct bt_mesh_friend *frnd, uint8_t md) in enqueue_update() argument
704 buf = encode_update(frnd, md); in enqueue_update()
710 enqueue_buf(frnd, buf); in enqueue_update()
716 struct bt_mesh_friend *frnd; in bt_mesh_friend_poll() local
723 frnd = bt_mesh_friend_find(rx->sub->net_idx, rx->ctx.addr, true, false); in bt_mesh_friend_poll()
724 if (!frnd) { in bt_mesh_friend_poll()
734 if (frnd->pending_buf) { in bt_mesh_friend_poll()
739 LOG_DBG("msg->fsn %u frnd->fsn %u", (msg->fsn & 1), frnd->fsn); in bt_mesh_friend_poll()
741 friend_recv_delay(frnd); in bt_mesh_friend_poll()
743 if (msg->fsn == frnd->fsn && frnd->last) { in bt_mesh_friend_poll()
745 frnd->send_last = 1U; in bt_mesh_friend_poll()
747 if (frnd->last) { in bt_mesh_friend_poll()
748 net_buf_unref(frnd->last); in bt_mesh_friend_poll()
749 frnd->last = NULL; in bt_mesh_friend_poll()
752 frnd->fsn = msg->fsn; in bt_mesh_friend_poll()
754 if (sys_slist_is_empty(&frnd->queue)) { in bt_mesh_friend_poll()
755 enqueue_update(frnd, 0); in bt_mesh_friend_poll()
762 cb->polled(frnd->subnet->net_idx, frnd->lpn); in bt_mesh_friend_poll()
766 if (!frnd->established) { in bt_mesh_friend_poll()
767 LOG_DBG("Friendship established with 0x%04x", frnd->lpn); in bt_mesh_friend_poll()
768 frnd->established = 1U; in bt_mesh_friend_poll()
772 cb->established(frnd->subnet->net_idx, frnd->lpn, frnd->recv_delay, in bt_mesh_friend_poll()
773 frnd->poll_to); in bt_mesh_friend_poll()
785 for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { in find_clear()
786 struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; in find_clear() local
788 if (frnd->clear.frnd == prev_friend) { in find_clear()
789 return frnd; in find_clear()
798 struct bt_mesh_friend *frnd = user_data; in friend_clear_sent() local
800 k_work_reschedule(&frnd->clear.timer, in friend_clear_sent()
801 K_SECONDS(frnd->clear.repeat_sec)); in friend_clear_sent()
802 frnd->clear.repeat_sec *= 2U; in friend_clear_sent()
809 static void send_friend_clear(struct bt_mesh_friend *frnd) in send_friend_clear() argument
812 .net_idx = frnd->subnet->net_idx, in send_friend_clear()
814 .addr = frnd->clear.frnd, in send_friend_clear()
818 .sub = frnd->subnet, in send_friend_clear()
824 .lpn_addr = sys_cpu_to_be16(frnd->lpn), in send_friend_clear()
825 .lpn_counter = sys_cpu_to_be16(frnd->lpn_counter), in send_friend_clear()
831 sizeof(req), &clear_sent_cb, frnd); in send_friend_clear()
837 struct bt_mesh_friend *frnd = CONTAINER_OF(dwork, struct bt_mesh_friend, in clear_timeout() local
841 if (frnd->clear.frnd == BT_MESH_ADDR_UNASSIGNED) { in clear_timeout()
846 LOG_DBG("LPN 0x%04x (old) Friend 0x%04x", frnd->lpn, frnd->clear.frnd); in clear_timeout()
848 duration = k_uptime_get_32() - frnd->clear.start; in clear_timeout()
849 if (duration > 2 * frnd->poll_to) { in clear_timeout()
851 frnd->clear.frnd = BT_MESH_ADDR_UNASSIGNED; in clear_timeout()
855 send_friend_clear(frnd); in clear_timeout()
858 static void clear_procedure_start(struct bt_mesh_friend *frnd) in clear_procedure_start() argument
860 LOG_DBG("LPN 0x%04x (old) Friend 0x%04x", frnd->lpn, frnd->clear.frnd); in clear_procedure_start()
862 frnd->clear.start = k_uptime_get_32(); in clear_procedure_start()
863 frnd->clear.repeat_sec = 1U; in clear_procedure_start()
865 send_friend_clear(frnd); in clear_procedure_start()
872 struct bt_mesh_friend *frnd; in bt_mesh_friend_clear_cfm() local
882 frnd = find_clear(rx->ctx.addr); in bt_mesh_friend_clear_cfm()
883 if (!frnd) { in bt_mesh_friend_clear_cfm()
889 if (lpn_addr != frnd->lpn) { in bt_mesh_friend_clear_cfm()
890 LOG_WRN("LPN address mismatch (0x%04x != 0x%04x)", lpn_addr, frnd->lpn); in bt_mesh_friend_clear_cfm()
895 if (lpn_counter != frnd->lpn_counter) { in bt_mesh_friend_clear_cfm()
896 LOG_WRN("LPN counter mismatch (0x%04x != 0x%04x)", lpn_counter, frnd->lpn_counter); in bt_mesh_friend_clear_cfm()
901 (void)k_work_cancel_delayable(&frnd->clear.timer); in bt_mesh_friend_clear_cfm()
902 frnd->clear.frnd = BT_MESH_ADDR_UNASSIGNED; in bt_mesh_friend_clear_cfm()
907 static void enqueue_offer(struct bt_mesh_friend *frnd, int8_t rssi) in enqueue_offer() argument
921 off->sub_list_size = ARRAY_SIZE(frnd->sub_list), in enqueue_offer()
927 off->frnd_counter = sys_cpu_to_be16(frnd->counter); in enqueue_offer()
929 buf = encode_friend_ctl(frnd, TRANS_CTL_OP_FRIEND_OFFER, &sdu); in enqueue_offer()
935 if (encrypt_friend_pdu(frnd, buf, true)) { in enqueue_offer()
939 if (frnd->last) { in enqueue_offer()
940 net_buf_unref(frnd->last); in enqueue_offer()
943 frnd->last = buf; in enqueue_offer()
944 frnd->send_last = 1U; in enqueue_offer()
953 static int32_t offer_delay(struct bt_mesh_friend *frnd, int8_t rssi, uint8_t crit) in offer_delay() argument
977 struct bt_mesh_friend *frnd = NULL; in bt_mesh_friend_req() local
1025 frnd = bt_mesh_friend_find(rx->sub->net_idx, rx->ctx.addr, true, false); in bt_mesh_friend_req()
1026 if (frnd) { in bt_mesh_friend_req()
1028 friend_clear(frnd); in bt_mesh_friend_req()
1032 for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { in bt_mesh_friend_req()
1033 if (!bt_mesh.frnd[i].subnet) { in bt_mesh_friend_req()
1034 frnd = &bt_mesh.frnd[i]; in bt_mesh_friend_req()
1039 if (!frnd) { in bt_mesh_friend_req()
1045 frnd->lpn = rx->ctx.addr; in bt_mesh_friend_req()
1046 frnd->num_elem = msg->num_elem; in bt_mesh_friend_req()
1047 frnd->subnet = rx->sub; in bt_mesh_friend_req()
1048 frnd->recv_delay = msg->recv_delay - CONFIG_BT_MESH_FRIEND_ADV_LATENCY; in bt_mesh_friend_req()
1049 frnd->poll_to = poll_to * 100U; in bt_mesh_friend_req()
1050 frnd->lpn_counter = sys_be16_to_cpu(msg->lpn_counter); in bt_mesh_friend_req()
1051 frnd->clear.frnd = sys_be16_to_cpu(msg->prev_addr); in bt_mesh_friend_req()
1053 err = friend_cred_create(frnd, SUBNET_KEY_TX_IDX(frnd->subnet)); in bt_mesh_friend_req()
1056 friend_clear(frnd); in bt_mesh_friend_req()
1060 LOG_DBG("LPN 0x%04x rssi %d recv_delay %u poll_to %ums", frnd->lpn, rx->ctx.recv_rssi, in bt_mesh_friend_req()
1061 frnd->recv_delay, frnd->poll_to); in bt_mesh_friend_req()
1063 if (BT_MESH_ADDR_IS_UNICAST(frnd->clear.frnd) && in bt_mesh_friend_req()
1064 !bt_mesh_has_addr(frnd->clear.frnd)) { in bt_mesh_friend_req()
1065 clear_procedure_start(frnd); in bt_mesh_friend_req()
1068 delay = offer_delay(frnd, rx->ctx.recv_rssi, msg->criteria); in bt_mesh_friend_req()
1069 k_work_reschedule(&frnd->timer, K_MSEC(delay)); in bt_mesh_friend_req()
1071 enqueue_offer(frnd, rx->ctx.recv_rssi); in bt_mesh_friend_req()
1097 static struct bt_mesh_friend_seg *get_seg(struct bt_mesh_friend *frnd, in get_seg() argument
1104 for (i = 0; i < ARRAY_SIZE(frnd->seg); i++) { in get_seg()
1105 struct bt_mesh_friend_seg *seg = &frnd->seg[i]; in get_seg()
1123 static void enqueue_friend_pdu(struct bt_mesh_friend *frnd, in enqueue_friend_pdu() argument
1133 enqueue_buf(frnd, buf); in enqueue_friend_pdu()
1139 seg = get_seg(frnd, src, seq_zero, seg_count); in enqueue_friend_pdu()
1149 sys_slist_merge_slist(&frnd->queue, &seg->queue); in enqueue_friend_pdu()
1151 frnd->queue_size += seg->seg_count; in enqueue_friend_pdu()
1160 struct bt_mesh_friend *frnd = user_data; in buf_send_start() local
1164 if (!frnd->pending_buf) { in buf_send_start()
1169 frnd->pending_buf = 0U; in buf_send_start()
1172 if (!frnd->established && frnd->last) { in buf_send_start()
1173 net_buf_unref(frnd->last); in buf_send_start()
1174 frnd->last = NULL; in buf_send_start()
1180 struct bt_mesh_friend *frnd = user_data; in buf_send_end() local
1184 if (frnd->pending_req || frnd->pending_buf) { in buf_send_end()
1189 if (frnd->established) { in buf_send_end()
1191 k_work_reschedule(&frnd->timer, K_MSEC(frnd->poll_to)); in buf_send_end()
1192 LOG_DBG("Waiting %u ms for next poll", frnd->poll_to); in buf_send_end()
1195 k_work_reschedule(&frnd->timer, K_SECONDS(1)); in buf_send_end()
1235 struct bt_mesh_friend *frnd = CONTAINER_OF(dwork, struct bt_mesh_friend, in friend_timeout() local
1244 if (!friend_is_allocated(frnd)) { in friend_timeout()
1248 __ASSERT_NO_MSG(frnd->pending_buf == 0U); in friend_timeout()
1250 LOG_DBG("lpn 0x%04x send_last %u last %p", frnd->lpn, frnd->send_last, frnd->last); in friend_timeout()
1252 if (frnd->send_last && frnd->last) { in friend_timeout()
1253 LOG_DBG("Sending frnd->last %p", frnd->last); in friend_timeout()
1254 frnd->send_last = 0U; in friend_timeout()
1258 if (frnd->established && !frnd->pending_req) { in friend_timeout()
1259 LOG_WRN("Friendship lost with 0x%04x", frnd->lpn); in friend_timeout()
1260 friend_clear(frnd); in friend_timeout()
1264 frnd->last = (void *)net_buf_slist_get(&frnd->queue); in friend_timeout()
1265 if (!frnd->last) { in friend_timeout()
1266 LOG_WRN("Friendship not established with 0x%04x", frnd->lpn); in friend_timeout()
1267 friend_clear(frnd); in friend_timeout()
1271 md = (uint8_t)(sys_slist_peek_head(&frnd->queue) != NULL); in friend_timeout()
1273 update_overwrite(frnd->last, md); in friend_timeout()
1275 if (encrypt_friend_pdu(frnd, frnd->last, false)) { in friend_timeout()
1279 LOG_DBG("Sending buf %p from Friend Queue of LPN 0x%04x", frnd->last, frnd->lpn); in friend_timeout()
1280 frnd->queue_size--; in friend_timeout()
1290 net_buf_simple_add_mem(&adv->b, frnd->last->data, frnd->last->len); in friend_timeout()
1292 frnd->pending_req = 0U; in friend_timeout()
1293 frnd->pending_buf = 1U; in friend_timeout()
1294 bt_mesh_adv_send(adv, &buf_sent_cb, frnd); in friend_timeout()
1306 for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { in subnet_evt()
1307 struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; in subnet_evt() local
1309 if (frnd->subnet != sub) { in subnet_evt()
1315 LOG_DBG("Cleared network for 0x%04x", frnd->lpn); in subnet_evt()
1316 friend_clear(frnd); in subnet_evt()
1319 LOG_DBG("Generating new keys for 0x%04x", frnd->lpn); in subnet_evt()
1320 err = friend_cred_create(frnd, 1); in subnet_evt()
1322 LOG_ERR("Failed updating friend cred for 0x%04x", frnd->lpn); in subnet_evt()
1323 friend_clear(frnd); in subnet_evt()
1327 enqueue_update(frnd, 0); in subnet_evt()
1330 LOG_DBG("Revoking old keys for 0x%04x", frnd->lpn); in subnet_evt()
1331 bt_mesh_friend_cred_destroy(&frnd->cred[0]); in subnet_evt()
1332 memcpy(&frnd->cred[0], &frnd->cred[1], in subnet_evt()
1333 sizeof(frnd->cred[0])); in subnet_evt()
1334 memset(&frnd->cred[1], 0, sizeof(frnd->cred[1])); in subnet_evt()
1335 enqueue_update(frnd, 0); in subnet_evt()
1351 for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { in bt_mesh_friend_init()
1352 struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; in bt_mesh_friend_init() local
1355 sys_slist_init(&frnd->queue); in bt_mesh_friend_init()
1357 k_work_init_delayable(&frnd->timer, friend_timeout); in bt_mesh_friend_init()
1358 k_work_init_delayable(&frnd->clear.timer, clear_timeout); in bt_mesh_friend_init()
1360 for (j = 0; j < ARRAY_SIZE(frnd->seg); j++) { in bt_mesh_friend_init()
1361 sys_slist_init(&frnd->seg[j].queue); in bt_mesh_friend_init()
1404 static void friend_purge_old_ack(struct bt_mesh_friend *frnd, in friend_purge_old_ack() argument
1411 for (cur = sys_slist_peek_head(&frnd->queue); in friend_purge_old_ack()
1418 sys_slist_remove(&frnd->queue, prev, cur); in friend_purge_old_ack()
1419 frnd->queue_size--; in friend_purge_old_ack()
1427 static void friend_lpn_enqueue_rx(struct bt_mesh_friend *frnd, in friend_lpn_enqueue_rx() argument
1444 LOG_DBG("LPN 0x%04x queue_size %u", frnd->lpn, frnd->queue_size); in friend_lpn_enqueue_rx()
1447 friend_purge_old_ack(frnd, seq_auth, rx->ctx.addr); in friend_lpn_enqueue_rx()
1465 buf = create_friend_pdu(frnd, &info, sbuf); in friend_lpn_enqueue_rx()
1471 enqueue_friend_pdu(frnd, type, info.src, seg_count, buf); in friend_lpn_enqueue_rx()
1473 LOG_DBG("Queued message for LPN 0x%04x, queue_size %u", frnd->lpn, frnd->queue_size); in friend_lpn_enqueue_rx()
1476 static void friend_lpn_enqueue_tx(struct bt_mesh_friend *frnd, in friend_lpn_enqueue_tx() argument
1485 LOG_DBG("LPN 0x%04x", frnd->lpn); in friend_lpn_enqueue_tx()
1488 friend_purge_old_ack(frnd, seq_auth, tx->src); in friend_lpn_enqueue_tx()
1501 buf = create_friend_pdu(frnd, &info, sbuf); in friend_lpn_enqueue_tx()
1529 enqueue_friend_pdu(frnd, type, info.src, seg_count, buf); in friend_lpn_enqueue_tx()
1531 LOG_DBG("Queued message for LPN 0x%04x, dst: %04x, uuid: %p", frnd->lpn, tx->ctx->addr, in friend_lpn_enqueue_tx()
1535 static bool friend_lpn_matches(struct bt_mesh_friend *frnd, uint16_t net_idx, in friend_lpn_matches() argument
1540 if (!frnd->established) { in friend_lpn_matches()
1544 if (net_idx != frnd->subnet->net_idx) { in friend_lpn_matches()
1549 return is_lpn_unicast(frnd, addr); in friend_lpn_matches()
1552 for (i = 0; i < ARRAY_SIZE(frnd->sub_list); i++) { in friend_lpn_matches()
1553 if (frnd->sub_list[i] == addr) { in friend_lpn_matches()
1565 for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { in bt_mesh_friend_match()
1566 struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; in bt_mesh_friend_match() local
1568 if (friend_lpn_matches(frnd, net_idx, addr)) { in bt_mesh_friend_match()
1569 LOG_DBG("LPN 0x%04x matched address 0x%04x", frnd->lpn, addr); in bt_mesh_friend_match()
1579 static bool friend_queue_has_space(struct bt_mesh_friend *frnd, uint16_t addr, in friend_queue_has_space() argument
1589 for (i = 0; i < ARRAY_SIZE(frnd->seg); i++) { in friend_queue_has_space()
1590 struct bt_mesh_friend_seg *seg = &frnd->seg[i]; in friend_queue_has_space()
1616 for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { in bt_mesh_friend_queue_has_space()
1617 struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; in bt_mesh_friend_queue_has_space() local
1619 if (!friend_lpn_matches(frnd, net_idx, dst)) { in bt_mesh_friend_queue_has_space()
1625 if (friend_queue_has_space(frnd, src, seq_auth, seg_count)) { in bt_mesh_friend_queue_has_space()
1645 static bool friend_queue_check_dup(struct bt_mesh_friend *frnd, uint32_t seq, in friend_queue_check_dup() argument
1651 for (cur = sys_slist_peek_head(&frnd->queue); cur != NULL; in friend_queue_check_dup()
1665 static bool friend_queue_prepare_space(struct bt_mesh_friend *frnd, uint16_t addr, in friend_queue_prepare_space() argument
1671 if (!friend_queue_has_space(frnd, addr, seq_auth, seg_count)) { in friend_queue_prepare_space()
1675 avail_space = CONFIG_BT_MESH_FRIEND_QUEUE_SIZE - frnd->queue_size; in friend_queue_prepare_space()
1679 struct net_buf *buf = (void *)net_buf_slist_get(&frnd->queue); in friend_queue_prepare_space()
1686 frnd->queue_size--; in friend_queue_prepare_space()
1713 for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { in bt_mesh_friend_enqueue_rx()
1714 struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; in bt_mesh_friend_enqueue_rx() local
1716 if (!friend_lpn_matches(frnd, rx->sub->net_idx, in bt_mesh_friend_enqueue_rx()
1721 if (friend_lpn_matches(frnd, rx->sub->net_idx, in bt_mesh_friend_enqueue_rx()
1726 if (friend_queue_check_dup(frnd, rx->seq, rx->ctx.addr)) { in bt_mesh_friend_enqueue_rx()
1730 if (!friend_queue_prepare_space(frnd, rx->ctx.addr, seq_auth, in bt_mesh_friend_enqueue_rx()
1735 friend_lpn_enqueue_rx(frnd, rx, type, seq_auth, seg_count, in bt_mesh_friend_enqueue_rx()
1755 for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { in bt_mesh_friend_enqueue_tx()
1756 struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; in bt_mesh_friend_enqueue_tx() local
1758 if (!friend_lpn_matches(frnd, tx->sub->net_idx, in bt_mesh_friend_enqueue_tx()
1763 if (!friend_queue_prepare_space(frnd, tx->src, seq_auth, in bt_mesh_friend_enqueue_tx()
1768 friend_lpn_enqueue_tx(frnd, tx, type, seq_auth, seg_count, in bt_mesh_friend_enqueue_tx()
1778 struct bt_mesh_friend *frnd; in bt_mesh_friend_terminate() local
1780 frnd = bt_mesh_friend_find(BT_MESH_KEY_ANY, lpn_addr, false, false); in bt_mesh_friend_terminate()
1781 if (!frnd) { in bt_mesh_friend_terminate()
1785 friend_clear(frnd); in bt_mesh_friend_terminate()
1797 for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { in bt_mesh_friend_clear_incomplete()
1798 struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; in bt_mesh_friend_clear_incomplete() local
1801 if (!friend_lpn_matches(frnd, sub->net_idx, dst)) { in bt_mesh_friend_clear_incomplete()
1805 for (j = 0; j < ARRAY_SIZE(frnd->seg); j++) { in bt_mesh_friend_clear_incomplete()
1806 struct bt_mesh_friend_seg *seg = &frnd->seg[j]; in bt_mesh_friend_clear_incomplete()