diff --git a/samples/bluetooth/gatt/ipss.c b/samples/bluetooth/gatt/ipss.c index a3f0d995408..f704520f399 100644 --- a/samples/bluetooth/gatt/ipss.c +++ b/samples/bluetooth/gatt/ipss.c @@ -32,24 +32,10 @@ #include #include -#include -#include -#include - #define DEVICE_NAME "Test IPSP node" #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) #define UNKNOWN_APPEARANCE 0x0000 -/* The 2001:db8::/32 is the private address space for documentation RFC 3849 */ -#define MY_IPADDR { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ - 0, 0x2 } } } - -/* admin-local, dynamically allocated multicast address */ -#define MCAST_IPADDR { { { 0xff, 0x84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ - 0x2 } } } - -#define UDP_PORT 4242 - #if !defined(CONFIG_BLUETOOTH_GATT_DYNAMIC_DB) static ssize_t read_name(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset) @@ -140,98 +126,6 @@ static struct bt_conn_cb conn_callbacks = { .disconnected = disconnected, }; -static inline void reverse(unsigned char *buf, int len) -{ - int i, last = len - 1; - - for (i = 0; i < len / 2; i++) { - unsigned char tmp = buf[i]; - - buf[i] = buf[last - i]; - buf[last - i] = tmp; - } -} - -static inline struct net_buf *prepare_reply(const char *type, - struct net_buf *buf) -{ - printk("%s: received %d bytes\n", type, ip_buf_appdatalen(buf)); - - /* In this test we reverse the received bytes. - * We could just pass the data back as is but - * this way it is possible to see how the app - * can manipulate the received data. - */ - reverse(ip_buf_appdata(buf), ip_buf_appdatalen(buf)); - - return buf; -} - -static inline bool get_context(struct net_context **recv, - struct net_context **mcast_recv) -{ - static struct net_addr mcast_addr; - static struct net_addr any_addr; - static struct net_addr my_addr; - static const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; - static const struct in6_addr in6addr_mcast = MCAST_IPADDR; - static struct in6_addr in6addr_my = MY_IPADDR; - - mcast_addr.in6_addr = in6addr_mcast; - mcast_addr.family = AF_INET6; - - any_addr.in6_addr = in6addr_any; - any_addr.family = AF_INET6; - - my_addr.in6_addr = in6addr_my; - my_addr.family = AF_INET6; - - *recv = net_context_get(IPPROTO_UDP, - &any_addr, 0, - &my_addr, UDP_PORT); - if (!*recv) { - printk("%s: Cannot get network context\n", __func__); - return NULL; - } - - *mcast_recv = net_context_get(IPPROTO_UDP, - &any_addr, 0, - &mcast_addr, UDP_PORT); - if (!*mcast_recv) { - printk("%s: Cannot get receiving mcast network context\n", - __func__); - return false; - } - - return true; -} - -static inline void receive_and_reply(struct net_context *recv, - struct net_context *mcast_recv) -{ - struct net_buf *buf; - - buf = net_receive(recv, TICKS_UNLIMITED); - if (buf) { - prepare_reply("unicast ", buf); - - if (net_reply(recv, buf)) { - ip_buf_unref(buf); - } - return; - } - - buf = net_receive(mcast_recv, TICKS_UNLIMITED); - if (buf) { - prepare_reply("multicast ", buf); - - if (net_reply(mcast_recv, buf)) { - ip_buf_unref(buf); - } - return; - } -} - void ipss_init(void) { bt_gatt_register(attrs, ARRAY_SIZE(attrs)); @@ -251,18 +145,3 @@ int ipss_advertise(void) return 0; } - -void ipss_listen(void) -{ - static struct net_context *recv; - static struct net_context *mcast_recv; - - if (!get_context(&recv, &mcast_recv)) { - printk("%s: Cannot get network contexts\n", __func__); - return; - } - - while (1) { - receive_and_reply(recv, mcast_recv); - } -} diff --git a/samples/bluetooth/gatt/ipss.h b/samples/bluetooth/gatt/ipss.h index ca4c6b828e5..2a5bd0c8230 100644 --- a/samples/bluetooth/gatt/ipss.h +++ b/samples/bluetooth/gatt/ipss.h @@ -20,4 +20,3 @@ void ipss_init(void); int ipss_advertise(void); -int ipss_listen(void); diff --git a/samples/bluetooth/ipsp/src/main.c b/samples/bluetooth/ipsp/src/main.c index f15cf040f79..8a17860449c 100644 --- a/samples/bluetooth/ipsp/src/main.c +++ b/samples/bluetooth/ipsp/src/main.c @@ -19,10 +19,129 @@ #include #include +#include #include +#include #include +#define UDP_PORT 4242 + +/* The 2001:db8::/32 is the private address space for documentation RFC 3849 */ +#define MY_IPADDR { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ + 0, 0x2 } } } + +/* admin-local, dynamically allocated multicast address */ +#define MCAST_IPADDR { { { 0xff, 0x84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ + 0x2 } } } + +static inline void reverse(unsigned char *buf, int len) +{ + int i, last = len - 1; + + for (i = 0; i < len / 2; i++) { + unsigned char tmp = buf[i]; + + buf[i] = buf[last - i]; + buf[last - i] = tmp; + } +} + +static inline struct net_buf *prepare_reply(const char *type, + struct net_buf *buf) +{ + printk("%s: received %d bytes\n", type, ip_buf_appdatalen(buf)); + + /* In this test we reverse the received bytes. + * We could just pass the data back as is but + * this way it is possible to see how the app + * can manipulate the received data. + */ + reverse(ip_buf_appdata(buf), ip_buf_appdatalen(buf)); + + return buf; +} + +static inline bool get_context(struct net_context **recv, + struct net_context **mcast_recv) +{ + static struct net_addr mcast_addr; + static struct net_addr any_addr; + static struct net_addr my_addr; + static const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; + static const struct in6_addr in6addr_mcast = MCAST_IPADDR; + static struct in6_addr in6addr_my = MY_IPADDR; + + mcast_addr.in6_addr = in6addr_mcast; + mcast_addr.family = AF_INET6; + + any_addr.in6_addr = in6addr_any; + any_addr.family = AF_INET6; + + my_addr.in6_addr = in6addr_my; + my_addr.family = AF_INET6; + + *recv = net_context_get(IPPROTO_UDP, + &any_addr, 0, + &my_addr, UDP_PORT); + if (!*recv) { + printk("%s: Cannot get network context\n", __func__); + return NULL; + } + + *mcast_recv = net_context_get(IPPROTO_UDP, + &any_addr, 0, + &mcast_addr, UDP_PORT); + if (!*mcast_recv) { + printk("%s: Cannot get receiving mcast network context\n", + __func__); + return false; + } + + return true; +} + +static inline void receive_and_reply(struct net_context *recv, + struct net_context *mcast_recv) +{ + struct net_buf *buf; + + buf = net_receive(recv, TICKS_UNLIMITED); + if (buf) { + prepare_reply("unicast ", buf); + + if (net_reply(recv, buf)) { + ip_buf_unref(buf); + } + return; + } + + buf = net_receive(mcast_recv, TICKS_UNLIMITED); + if (buf) { + prepare_reply("multicast ", buf); + + if (net_reply(mcast_recv, buf)) { + ip_buf_unref(buf); + } + return; + } +} + +static void listen(void) +{ + static struct net_context *recv; + static struct net_context *mcast_recv; + + if (!get_context(&recv, &mcast_recv)) { + printk("%s: Cannot get network contexts\n", __func__); + return; + } + + while (1) { + receive_and_reply(recv, mcast_recv); + } +} + void main(void) { int err; @@ -47,5 +166,5 @@ void main(void) printk("Advertising successfully started\n"); - ipss_listen(); + listen(); }