7 years, 7 months ago.

Broadcast in mbedos 5

With the change of network stack in Mbed-OS 5, it looks like broadcast is not configurable for udp. Is there a timeline or roadmap for when this will be available?

Regards, Seth

That's a very abstract question. Are you talking about LWIP or NanoStack?

posted by Jan Jongboom 04 Oct 2016

The Mbed OS 5 added the new class NetworkStack and all sockets(TCP, UDP) are based around this new stack. There is no broadcast function. And if you try to set it with setsockopt you will get a not implement error.

int NetworkStack::setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen) { return NSAPI_ERROR_UNSUPPORTED; }

posted by Seth King 04 Oct 2016

1 Answer

7 years, 4 months ago.

It seems it has been solved already https://github.com/ARMmbed/mbed-os/pull/3491/commits/6849ba92277bd518581f8cd03a7e8677f8aaaaa0

if you really really need to have it working before the new release you can derive the UDPSocket class and implement a method where you set the flags yourself. This is a horrible,terrible hack that worked for me

#include "lwip/ip.h"
#include "lwip/api.h"
#include "UDPSocket.h"

//to do this is terribly wrong :D because this struct is not visible to the api
struct lwip_socket {
  bool in_use;
  struct netconn *conn;
  struct netbuf *buf;
  u16_t offset;

  void(*cb)(void *);
  void *data;
};

class UDPBroadcastSocket: public UDPSocket
{
public: //or you can make it private/protected and call it in the constructor
 void set_broadcast(bool broadcast) {
  //extreme violence, because we dont have yet the broadcast feature exposed in the new mbed-os lwip impl
  struct lwip_socket *s = (struct lwip_socket *)_socket;
  if (broadcast)
    s->conn->pcb.ip->so_options |= SOF_BROADCAST;
  else
    s->conn->pcb.ip->so_options &= ~SOF_BROADCAST;
 }
};

Any idea when this will be in a release? I need to send broadcasts, not receive, but that doesnt seem to work even when the target address is 255.255.255.255. Does this method fix a broadcast send issue aswell as receive?

Thanks

Edit: Sorted... It does allow broadcast send aswell as receive. Thanks for this solution!

posted by Angus Hutton-McKenzie 08 Feb 2017