Skip to content

Commit 2c01ea0

Browse files
authored
Merge pull request #21608 from elenaf9/gnrc/netapi/event
gnrc, nimble/ble: notify network layer of BLE connection events
2 parents 5b38177 + 1f3daf3 commit 2c01ea0

File tree

21 files changed

+797
-87
lines changed

21 files changed

+797
-87
lines changed

pkg/nimble/netif/nimble_netif.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "net/ble.h"
2929
#include "net/bluetil/addr.h"
30+
#include "net/gnrc/netapi/notify.h"
3031
#include "net/gnrc/netif.h"
3132
#include "net/gnrc/netif/hdr.h"
3233
#include "net/gnrc/netreg.h"
@@ -39,6 +40,7 @@
3940
#include "host/ble_gap.h"
4041
#include "host/util/util.h"
4142
#include "mem/mem.h"
43+
#include <string.h>
4244

4345
#define ENABLE_DEBUG 0
4446
#include "debug.h"
@@ -331,6 +333,29 @@ static void _on_data(nimble_netif_conn_t *conn, struct ble_l2cap_event *event)
331333
ble_l2cap_recv_ready(event->receive.chan, rxb);
332334
}
333335

336+
/**
337+
* @brief Sends a netapi notification for a connection event.
338+
*
339+
* @param[in] notify The type of notification event.
340+
* @param[in] addr BLE address of the node that (dis-)connected.
341+
*/
342+
static inline void _dispatch_connection_event(netapi_notify_t notify, const void *addr)
343+
{
344+
if (!IS_USED(MODULE_GNRC_NETAPI_NOTIFY)) {
345+
return;
346+
}
347+
348+
netapi_notify_l2_connection_t event = {
349+
.l2addr_len = BLE_ADDR_LEN,
350+
.if_pid = _netif.pid,
351+
};
352+
353+
memcpy(event.l2addr, addr, BLE_ADDR_LEN);
354+
355+
gnrc_netapi_notify(GNRC_NETTYPE_L2_DISCOVERY, GNRC_NETREG_DEMUX_CTX_ALL,
356+
notify, &event, sizeof(netapi_notify_l2_connection_t));
357+
}
358+
334359
static int _on_l2cap_client_evt(struct ble_l2cap_event *event, void *arg)
335360
{
336361
int handle = (int)arg;
@@ -349,6 +374,7 @@ static int _on_l2cap_client_evt(struct ble_l2cap_event *event, void *arg)
349374
conn->state |= NIMBLE_NETIF_L2CAP_CLIENT;
350375
conn->state &= ~NIMBLE_NETIF_CONNECTING;
351376
_notify(handle, NIMBLE_NETIF_CONNECTED_MASTER, conn->addr);
377+
_dispatch_connection_event(NETAPI_NOTIFY_L2_NEIGH_CONNECTED, conn->addr);
352378
break;
353379
case BLE_L2CAP_EVENT_COC_DISCONNECTED:
354380
assert(conn->state & NIMBLE_NETIF_L2CAP_CLIENT);
@@ -404,6 +430,7 @@ static int _on_l2cap_server_evt(struct ble_l2cap_event *event, void *arg)
404430
}
405431

406432
_notify(handle, NIMBLE_NETIF_CONNECTED_SLAVE, conn->addr);
433+
_dispatch_connection_event(NETAPI_NOTIFY_L2_NEIGH_CONNECTED, conn->addr);
407434
break;
408435
case BLE_L2CAP_EVENT_COC_DISCONNECTED:
409436
conn = nimble_netif_conn_from_gaphandle(event->disconnect.conn_handle);
@@ -498,6 +525,7 @@ static int _on_gap_master_evt(struct ble_gap_event *event, void *arg)
498525
nimble_netif_conn_free(handle, addr);
499526
thread_flags_set(_netif_thread, FLAG_TX_NOTCONN);
500527
_notify(handle, type, addr);
528+
_dispatch_connection_event(NETAPI_NOTIFY_L2_NEIGH_DISCONNECTED, addr);
501529
break;
502530
}
503531
case BLE_GAP_EVENT_CONN_UPDATE:
@@ -542,6 +570,7 @@ static int _on_gap_slave_evt(struct ble_gap_event *event, void *arg)
542570
nimble_netif_conn_free(handle, addr);
543571
thread_flags_set(_netif_thread, FLAG_TX_NOTCONN);
544572
_notify(handle, type, addr);
573+
_dispatch_connection_event(NETAPI_NOTIFY_L2_NEIGH_DISCONNECTED, addr);
545574
break;
546575
}
547576
case BLE_GAP_EVENT_CONN_UPDATE:

pkg/nimble/rpble/nimble_rpble.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,6 @@ static void _on_netif_evt(int handle, nimble_netif_event_t event,
222222
case NIMBLE_NETIF_CONNECTED_MASTER:
223223
/* parent selected */
224224
assert(_current_parent == handle);
225-
/* send a DIS once connected to a (new) parent) */
226-
gnrc_rpl_send_DIS(NULL, (ipv6_addr_t *) &ipv6_addr_all_rpl_nodes,
227-
NULL, 0);
228225
break;
229226
case NIMBLE_NETIF_CONNECTED_SLAVE:
230227
/* child added */

sys/include/atomic_utils.h

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,14 @@ static inline uint64_t atomic_load_u64(const volatile uint64_t *var);
274274
static inline unsigned atomic_load_unsigned(const volatile unsigned *var)
275275
{
276276
if (sizeof(uint64_t) == sizeof(unsigned)) {
277-
return atomic_load_u64((volatile void *)var);
277+
return atomic_load_u64((const volatile uint64_t *)(uintptr_t)var);
278278
}
279279

280280
if (sizeof(uint32_t) == sizeof(unsigned)) {
281-
return atomic_load_u32((volatile void *)var);
281+
return atomic_load_u32((const volatile uint32_t *)(uintptr_t)var);
282282
}
283283

284-
return atomic_load_u16((volatile void *)var);
284+
return atomic_load_u16((const volatile uint16_t *)(uintptr_t)var);
285285
}
286286

287287
/**
@@ -362,13 +362,13 @@ static inline void atomic_store_u64(volatile uint64_t *dest, uint64_t val);
362362
static inline void atomic_store_unsigned(volatile unsigned *dest, unsigned val)
363363
{
364364
if (sizeof(uint64_t) == sizeof(unsigned)) {
365-
atomic_store_u64((volatile void *)dest, val);
365+
atomic_store_u64((volatile uint64_t *)(uintptr_t)dest, val);
366366
}
367367
else if (sizeof(uint32_t) == sizeof(unsigned)) {
368-
atomic_store_u32((volatile void *)dest, val);
368+
atomic_store_u32((volatile uint32_t *)(uintptr_t)dest, val);
369369
}
370370
else {
371-
atomic_store_u16((volatile void *)dest, val);
371+
atomic_store_u16((volatile uint16_t *)(uintptr_t)dest, val);
372372
}
373373
}
374374

@@ -462,14 +462,14 @@ static inline unsigned atomic_fetch_add_unsigned(volatile unsigned *dest,
462462
unsigned summand)
463463
{
464464
if (sizeof(unsigned) == sizeof(uint64_t)) {
465-
return atomic_fetch_add_u64((volatile void *)dest, summand);
465+
return atomic_fetch_add_u64((volatile uint64_t *)(uintptr_t)dest, summand);
466466
}
467467

468468
if (sizeof(unsigned) == sizeof(uint32_t)) {
469-
return atomic_fetch_add_u32((volatile void *)dest, summand);
469+
return atomic_fetch_add_u32((volatile uint32_t *)(uintptr_t)dest, summand);
470470
}
471471

472-
return atomic_fetch_add_u16((volatile void *)dest, summand);
472+
return atomic_fetch_add_u16((volatile uint16_t *)(uintptr_t)dest, summand);
473473
}
474474
/** @} */
475475

@@ -528,14 +528,14 @@ static inline unsigned atomic_fetch_sub_unsigned(volatile unsigned *dest,
528528
unsigned subtrahend)
529529
{
530530
if (sizeof(unsigned) == sizeof(uint64_t)) {
531-
return atomic_fetch_sub_u64((volatile void *)dest, subtrahend);
531+
return atomic_fetch_sub_u64((volatile uint64_t *)(uintptr_t)dest, subtrahend);
532532
}
533533

534534
if (sizeof(unsigned) == sizeof(uint32_t)) {
535-
return atomic_fetch_sub_u32((volatile void *)dest, subtrahend);
535+
return atomic_fetch_sub_u32((volatile uint32_t *)(uintptr_t)dest, subtrahend);
536536
}
537537

538-
return atomic_fetch_sub_u16((volatile void *)dest, subtrahend);
538+
return atomic_fetch_sub_u16((volatile uint16_t *)(uintptr_t)dest, subtrahend);
539539
}
540540
/** @} */
541541

@@ -593,14 +593,14 @@ static inline unsigned atomic_fetch_or_unsigned(volatile unsigned *dest,
593593
unsigned val)
594594
{
595595
if (sizeof(unsigned) == sizeof(uint64_t)) {
596-
return atomic_fetch_or_u64((volatile void *)dest, val);
596+
return atomic_fetch_or_u64((volatile uint64_t *)(uintptr_t)dest, val);
597597
}
598598

599599
if (sizeof(unsigned) == sizeof(uint32_t)) {
600-
return atomic_fetch_or_u32((volatile void *)dest, val);
600+
return atomic_fetch_or_u32((volatile uint32_t *)(uintptr_t)dest, val);
601601
}
602602

603-
return atomic_fetch_or_u16((volatile void *)dest, val);
603+
return atomic_fetch_or_u16((volatile uint16_t *)(uintptr_t)dest, val);
604604
}
605605
/** @} */
606606

@@ -658,14 +658,14 @@ static inline unsigned atomic_fetch_xor_unsigned(volatile unsigned *dest,
658658
unsigned val)
659659
{
660660
if (sizeof(unsigned) == sizeof(uint64_t)) {
661-
return atomic_fetch_xor_u64((volatile void *)dest, val);
661+
return atomic_fetch_xor_u64((volatile uint64_t *)(uintptr_t)dest, val);
662662
}
663663

664664
if (sizeof(unsigned) == sizeof(uint32_t)) {
665-
return atomic_fetch_xor_u32((volatile void *)dest, val);
665+
return atomic_fetch_xor_u32((volatile uint32_t *)(uintptr_t)dest, val);
666666
}
667667

668-
return atomic_fetch_xor_u16((volatile void *)dest, val);
668+
return atomic_fetch_xor_u16((volatile uint16_t *)(uintptr_t)dest, val);
669669
}
670670
/** @} */
671671

@@ -723,14 +723,14 @@ static inline unsigned atomic_fetch_and_unsigned(volatile unsigned *dest,
723723
unsigned val)
724724
{
725725
if (sizeof(unsigned) == sizeof(uint64_t)) {
726-
return atomic_fetch_and_u64((volatile void *)dest, val);
726+
return atomic_fetch_and_u64((volatile uint64_t *)(uintptr_t)dest, val);
727727
}
728728

729729
if (sizeof(unsigned) == sizeof(uint32_t)) {
730-
return atomic_fetch_and_u32((volatile void *)dest, val);
730+
return atomic_fetch_and_u32((volatile uint32_t *)(uintptr_t)dest, val);
731731
}
732732

733-
return atomic_fetch_and_u16((volatile void *)dest, val);
733+
return atomic_fetch_and_u16((volatile uint16_t *)(uintptr_t)dest, val);
734734
}
735735
/** @} */
736736

@@ -935,14 +935,14 @@ static inline unsigned semi_atomic_fetch_add_unsigned(volatile unsigned *dest,
935935
unsigned summand)
936936
{
937937
if (sizeof(unsigned) == sizeof(uint64_t)) {
938-
return semi_atomic_fetch_add_u64((volatile void *)dest, summand);
938+
return semi_atomic_fetch_add_u64((volatile uint64_t *)(uintptr_t)dest, summand);
939939
}
940940

941941
if (sizeof(unsigned) == sizeof(uint32_t)) {
942-
return semi_atomic_fetch_add_u32((volatile void *)dest, summand);
942+
return semi_atomic_fetch_add_u32((volatile uint32_t *)(uintptr_t)dest, summand);
943943
}
944944

945-
return semi_atomic_fetch_add_u16((volatile void *)dest, summand);
945+
return semi_atomic_fetch_add_u16((volatile uint16_t *)(uintptr_t)dest, summand);
946946
}
947947
/** @} */
948948

@@ -1001,14 +1001,14 @@ static inline unsigned semi_atomic_fetch_sub_unsigned(volatile unsigned *dest,
10011001
unsigned subtrahend)
10021002
{
10031003
if (sizeof(unsigned) == sizeof(uint64_t)) {
1004-
return semi_atomic_fetch_sub_u64((volatile void *)dest, subtrahend);
1004+
return semi_atomic_fetch_sub_u64((volatile uint64_t *)(uintptr_t)dest, subtrahend);
10051005
}
10061006

10071007
if (sizeof(unsigned) == sizeof(uint32_t)) {
1008-
return semi_atomic_fetch_sub_u32((volatile void *)dest, subtrahend);
1008+
return semi_atomic_fetch_sub_u32((volatile uint32_t *)(uintptr_t)dest, subtrahend);
10091009
}
10101010

1011-
return semi_atomic_fetch_sub_u16((volatile void *)dest, subtrahend);
1011+
return semi_atomic_fetch_sub_u16((volatile uint16_t *)(uintptr_t)dest, subtrahend);
10121012
}
10131013
/** @} */
10141014

@@ -1066,14 +1066,14 @@ static inline unsigned semi_atomic_fetch_or_unsigned(volatile unsigned *dest,
10661066
unsigned val)
10671067
{
10681068
if (sizeof(unsigned) == sizeof(uint64_t)) {
1069-
return semi_atomic_fetch_or_u64((volatile void *)dest, val);
1069+
return semi_atomic_fetch_or_u64((volatile uint64_t *)(uintptr_t)dest, val);
10701070
}
10711071

10721072
if (sizeof(unsigned) == sizeof(uint32_t)) {
1073-
return semi_atomic_fetch_or_u32((volatile void *)dest, val);
1073+
return semi_atomic_fetch_or_u32((volatile uint32_t *)(uintptr_t)dest, val);
10741074
}
10751075

1076-
return semi_atomic_fetch_or_u16((volatile void *)dest, val);
1076+
return semi_atomic_fetch_or_u16((volatile uint16_t *)(uintptr_t)dest, val);
10771077
}
10781078
/** @} */
10791079

@@ -1132,14 +1132,14 @@ static inline unsigned semi_atomic_fetch_xor_unsigned(volatile unsigned *dest,
11321132
unsigned val)
11331133
{
11341134
if (sizeof(unsigned) == sizeof(uint64_t)) {
1135-
return semi_atomic_fetch_xor_u64((volatile void *)dest, val);
1135+
return semi_atomic_fetch_xor_u64((volatile uint64_t *)(uintptr_t)dest, val);
11361136
}
11371137

11381138
if (sizeof(unsigned) == sizeof(uint32_t)) {
1139-
return semi_atomic_fetch_xor_u32((volatile void *)dest, val);
1139+
return semi_atomic_fetch_xor_u32((volatile uint32_t *)(uintptr_t)dest, val);
11401140
}
11411141

1142-
return semi_atomic_fetch_xor_u16((volatile void *)dest, val);
1142+
return semi_atomic_fetch_xor_u16((volatile uint16_t *)(uintptr_t)dest, val);
11431143
}
11441144
/** @} */
11451145

@@ -1198,14 +1198,14 @@ static inline unsigned semi_atomic_fetch_and_unsigned(volatile unsigned *dest,
11981198
unsigned val)
11991199
{
12001200
if (sizeof(unsigned) == sizeof(uint64_t)) {
1201-
return semi_atomic_fetch_and_u64((volatile void *)dest, val);
1201+
return semi_atomic_fetch_and_u64((volatile uint64_t *)(uintptr_t)dest, val);
12021202
}
12031203

12041204
if (sizeof(unsigned) == sizeof(uint32_t)) {
1205-
return semi_atomic_fetch_and_u32((volatile void *)dest, val);
1205+
return semi_atomic_fetch_and_u32((volatile uint32_t *)(uintptr_t)dest, val);
12061206
}
12071207

1208-
return semi_atomic_fetch_and_u16((volatile void *)dest, val);
1208+
return semi_atomic_fetch_and_u16((volatile uint16_t *)(uintptr_t)dest, val);
12091209
}
12101210
/** @} */
12111211

sys/include/net/gnrc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@
288288

289289
#include "net/netopt.h"
290290
#include "net/gnrc/netapi.h"
291+
#include "net/gnrc/netapi/notify.h"
291292
#include "net/gnrc/netreg.h"
292293
#include "net/gnrc/nettype.h"
293294
#include "net/gnrc/netif.h"

sys/include/net/gnrc/ipv6/nib/nc.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,36 @@ static inline unsigned gnrc_ipv6_nib_nc_get_ar_state(const gnrc_ipv6_nib_nc_t *e
233233
int gnrc_ipv6_nib_nc_set(const ipv6_addr_t *ipv6, unsigned iface,
234234
const uint8_t *l2addr, size_t l2addr_len);
235235

236+
#if IS_ACTIVE(CONFIG_GNRC_IPV6_NIB_6LN) || defined(DOXYGEN)
237+
/**
238+
* @brief Adds an unmanaged neighbor entry to the NIB if the interface represents a 6LN node
239+
* and the IPv6 address can be constructed from the L2 address @p l2addr.
240+
*
241+
* @param[in] iface The interface to the neighbor.
242+
* @param[in] l2addr The neighbor's layer 2 address.
243+
* @param[in] l2addr_len Length of @p l2addr.
244+
*
245+
* @retval 0 on success.
246+
* @retval -ENOTSUP if the interface does not represent a 6LN or when
247+
* gnrc_netif_t::device_type of the iface does not support IID conversion.
248+
* @retval -EINVAL when @p addr_len is invalid for the
249+
* gnrc_netif_t::device_type of @p netif.
250+
* @retval -ENOMEM if no space is left in neighbor cache.
251+
*/
252+
int gnrc_ipv6_nib_nc_set_6ln(unsigned iface, const uint8_t *l2addr,
253+
size_t l2addr_len);
254+
#else /* CONFIG_GNRC_IPV6_NIB_6LN */
255+
static inline int gnrc_ipv6_nib_nc_set_6ln(unsigned iface, const uint8_t *l2addr,
256+
size_t l2addr_len)
257+
{
258+
(void)iface;
259+
(void)l2addr;
260+
(void)l2addr_len;
261+
262+
return -ENOTSUP;
263+
}
264+
#endif /* CONFIG_GNRC_IPV6_NIB_6LN */
265+
236266
/**
237267
* @brief Deletes neighbor with address @p ipv6 from NIB
238268
*
@@ -245,6 +275,23 @@ int gnrc_ipv6_nib_nc_set(const ipv6_addr_t *ipv6, unsigned iface,
245275
*/
246276
void gnrc_ipv6_nib_nc_del(const ipv6_addr_t *ipv6, unsigned iface);
247277

278+
#if IS_ACTIVE(CONFIG_GNRC_IPV6_NIB_ARSM) || defined(DOXYGEN)
279+
/**
280+
* @brief Deletes neighbor with link-layer address @p l2addr from NIB.
281+
*
282+
* @param[in] iface The interface to the neighbor.
283+
* @param[in] l2addr The neighbor's l2addr address.
284+
* @param[in] l2addr_len Length of @p l2addr.
285+
*
286+
*
287+
* If the @p l2addr can't be found for a neighbor in the NIB nothing happens.
288+
*
289+
* @retval True if a neighbor with @p l2addr existed.
290+
* @retval False otherwise.
291+
*/
292+
bool gnrc_ipv6_nib_nc_del_l2(unsigned iface, const uint8_t *l2addr, size_t l2addr_len);
293+
#endif /* CONFIG_GNRC_IPV6_NIB_ARSM */
294+
248295
/**
249296
* @brief Mark neighbor with address @p ipv6 as reachable
250297
*

0 commit comments

Comments
 (0)