how to use UDP in mqx to receive a broadcast datagram .eg. (use udp to recv data from 192.168.1.255)

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

how to use UDP in mqx to receive a broadcast datagram .eg. (use udp to recv data from 192.168.1.255)

Jump to solution
9,343 Views
yongchaoyue
Contributor II

in linux ,I saw  this"

  1. nb=setsockopt(sock,SOL_SOCKET,SO_BROADCAST,(char*)&opt,sizeof(opt));

"

but I can't find SO_BROADCAST has been defined in mqx,

how to config to make udp to receive a broadcast datagram.

my code : 

int connect_UDP()

{

  uint_32 cmd_reply_len;

    uint_32         sock,sendsock;

  uint_16 rlen;

   

    IPCFG_IP_ADDRESS_DATA    ip_data;

    _enet_address            enet_addr = ENET_MAC;

   

    sockaddr_in     addr;

    sockaddr_in dest_addr;

    uint_32         error, option;

    MQX_FILE_PTR   sockfd, telnetfd;

    _mqx_uint       echoflag = IO_SERIAL_ECHO;

    char senddata[128] = "mesh gate connected!\r\n";

    char        recvdata[512];

    int count;

    int             num,pRcv;

   

   

   

//    _RTCSPCB_init          = 3;

//    _RTCS_msgpool_init     = 3;

//    _RTCS_socket_part_init = 3;

    memset(recvdata,0,sizeof(recvdata));

   

    error = RTCS_create();

    if (error != RTCS_OK)

    {

       fputs("Fatal Error: RTCS initialization failed.", stderr);

       _task_block();

    }

    ip_data.ip = ENET_IPADDR;

    ip_data.mask = ENET_IPMASK;

    ip_data.gateway = ENET_IPGATEWAY;

   

   

   

    ENET_get_mac_address (BSP_DEFAULT_ENET_DEVICE, ENET_IPADDR, enet_addr);

   

    error = ipcfg_init_device(BSP_DEFAULT_ENET_DEVICE, enet_addr);

    if (error != IPCFG_OK)

    {

        fprintf(stderr, "Fatal Error 0x%X: Network device initialization failed.\n", error);

        _task_block();

    }

   

    error = ipcfg_bind_staticip(BSP_DEFAULT_ENET_DEVICE, &ip_data);

    if (error != IPCFG_OK)

    {

        fprintf(stderr, "Fatal Error 0x%X: IP address binding failed.", error);

        _task_block();

    }

   

   

    /* Install device drivers for socket and telnet I/O */

    //_io_socket_install("socket:");

  sendsock = socket(PF_INET, SOCK_DGRAM, 0);

   

    /* Bind the socket to the Telnet port */

    addr.sin_family      = AF_INET;

    addr.sin_port        = 5055;

    //addr.sin_addr.s_addr = ENET_IPADDR;   //INADDR_BROADCAST

  addr.sin_addr.s_addr = INADDR_ANY;

    dest_addr.sin_family = AF_INET;

    dest_addr.sin_port        = 5055; 

    dest_addr.sin_addr.s_addr = IPADDR(192,168,1,112);

  option = 1;

  setsockopt(sendsock,SOL_SOCKET,OPT_RECEIVE_PUSH,&option,sizeof(option));

    error = bind(sendsock, &addr, sizeof(addr)); //return_error_if(error != RTCS_OK)

  _time_delay(100);

  rlen = sizeof(dest_addr);

  //sendto(sendsock, senddata, strlen(senddata), 0,&dest_addr, rlen);

  while(1)

  {

  count = recvfrom(sendsock, recvdata, 512, 0, (struct sockaddr *) &dest_addr,&rlen);

  if(count == RTCS_ERROR)

  {

  ;

  }

  else

  {

  if(count>0)

  {

  //_io_write(serial4_fd, recvdata, count);

  //_time_delay(1000);

  //memset(recvdata,0,sizeof(recvdata));

  //count = 0;

  //cmd_reply_len = phone_cmd_analsys((uint_8*)recvdata,count);

  //sendto(sendsock, phone_reply, cmd_reply_len, 0,&dest_addr, rlen);

  sendto(sendsock, "123", 3, 0,&dest_addr, rlen);

  memset(phone_reply,0,PHONE_REPLY_LEN);

  count = 0;

  }

  }

  }

}

Labels (1)
0 Kudos
1 Solution
8,099 Views
Martin_
NXP Employee
NXP Employee

Please check that PC really sends out the packet with 255.255.255.255 destination ip to correct interface (where is TWR system connected to). I just found windows sends out only to one interface (the default) so if you have more interfaces, double check that the packet is there.

with my own code:

I can receive packet with destination 255.255.255.255.

I can't receive packet with destination 192.168.1.255. I can see it is discarded (in network statistics).

We will check this issue and possibly fix for the next MQX release.

By the way, for any new development, you should better use MQX 4.1.1.

-Martin

View solution in original post

0 Kudos
16 Replies
8,099 Views
Martin_
NXP Employee
NXP Employee

SO_BROADCAST is used for sending broadcast datagrams, not for receiving. We don't currently have this option in RTCS.

OPT_RECEIVE_PUSH is for a TCP socket, not UDP.

to receive a broadcast from any interface, you should bind the UDP socket to zero ip address (INADDR_ANY) and your port (per your source code, seems you already have this).

It should be enough to get this datagram with the recvfrom() function. Remove the two socket options from your code and give it a try.

-Martin

0 Kudos
8,099 Views
yongchaoyue
Contributor II

I use tad tool,watch the IP State ,I find the pc's broadcast datagram has been discarded by mqx,the reason is illegal destination.

I have send broadcast datagram nine times,the picture as follow

岳永超

北京云天创科技有限公司 ATZGB(BeiJing) Co.,Ltd.

Mob:18101210380

Fax:010-82176393-808

北京市海淀区上地7街(信息路2号)国际创业园2号楼20B

yueyongchao@yeah.net

From: yueyongchao@yeah.net

Date: 2014-10-08 17:11

To: jive-186488485-4wrl-2-9h4q

Subject: Re: Re: - how to use UDP in mqx to receive a broadcast datagram .eg. (use udp to recv data from 192.168.1.255)

code has been Modified as followed.but it still has something wrong.

the mqx(192.168.1.222) can send a datagram to 192.168.1.255 and my pc(192.168.1.112) can recv mqx's broadcast datagram.

but my pc send a broadcast datagram to 192.168.1.255,mqx can't recv pc's broadcast datagram.

sendsock = socket(PF_INET, SOCK_DGRAM, 0);

/* Bind the socket to the Telnet port */

addr.sin_family = AF_INET;

addr.sin_port = 5055;

//addr.sin_addr.s_addr = ENET_IPADDR;

addr.sin_addr.s_addr = INADDR_ANY;

dest_addr.sin_family = AF_INET;

dest_addr.sin_port = 5055;

dest_addr.sin_addr.s_addr = INADDR_BROADCAST;

error = bind(sendsock, &addr, sizeof(addr)); //return_error_if(error != RTCS_OK)

timedelay(100);

rlen = sizeof(dest_addr);

sendto(sendsock, senddata, strlen(senddata), 0,&dest_addr, rlen);

while(1)

{

ret = RTCS_selectset(&sendsock, 1, 1000);

if(ret == sendsock)

{

count = recvfrom(sendsock, recvdata, 512, 0, (struct sockaddr *) &dest_addr,&rlen);

if(count == RTCS_ERROR)

{

;

}

else

{

if(count>0)

{

//_io_write(serial4_fd, recvdata, count);

//_time_delay(1000);

//memset(recvdata,0,sizeof(recvdata));

//count = 0;

//cmd_reply_len = phone_cmd_analsys((uint_8*)recvdata,count);

//sendto(sendsock, phone_reply, cmd_reply_len, 0,&dest_addr, rlen);

sendto(sendsock, "123", 3, 0,&dest_addr, rlen);

memset(phone_reply,0,PHONE_REPLY_LEN);

count = 0;

}

}

}

}

岳永超

北京云天创科技有限公司 ATZGB(BeiJing) Co.,Ltd.

Mob:18101210380

Fax:010-82176393-808

北京市海淀区上地7街(信息路2号)国际创业园2号楼20B

yueyongchao@yeah.net

From: Martin Latal

Date: 2014-10-08 15:19

To: yongchao yue

Subject: Re: - how to use UDP in mqx to receive a broadcast datagram .eg. (use udp to recv data from 192.168.1.255)

how to use UDP in mqx to receive a broadcast datagram .eg. (use udp to recv data from 192.168.1.255)

reply from Martin Latal in MQX Software Solutions - View the full discussion

SO_BROADCAST is used for sending broadcast datagrams, not for receiving. We don't currently have this option in RTCS.

OPT_RECEIVE_PUSH is for a TCP socket, not UDP.

to receive a broadcast from any interface, you should bind the UDP socket to zero ip address (INADDR_ANY) and your port (per your source code, seems you already have this).

It should be enough to get this datagram with the recvfrom() function. Remove the two socket options from your code and give it a try.

-Martin

Did your question get answered?

If so, say thanks by clicking Correct Answer in the community thread!

Reply to this message by replying to this email, or go to the message on Freescale Community

Start a new discussion in MQX Software Solutions by email or at Freescale Community

Following how to use UDP in mqx to receive a broadcast datagram .eg. (use udp to recv data from 192.168.1.255) in these streams: Inbox

0 Kudos
8,099 Views
Martin_
NXP Employee
NXP Employee

From PC, can you try to send to 255.255.255.255. destination ?

0 Kudos
8,099 Views
yongchaoyue
Contributor II

I use mqx's version is MQX4.0 with IAR.

岳永超

北京云天创科技有限公司 ATZGB(BeiJing) Co.,Ltd.

Mob:18101210380

Fax:010-82176393-808

北京市海淀区上地7街(信息路2号)国际创业园2号楼20B

yueyongchao@yeah.net

From: yueyongchao@yeah.net

Date: 2014-10-08 19:11

To: jive-186488485-4wrl-2-9h6x

Subject: Re: Re: - how to use UDP in mqx to receive a broadcast datagram .eg. (use udp to recv data from 192.168.1.255)

tried,not working!if convenient,try it by yourself.

I have send my code together by accessory.

if you try sucessfully,tell me please.thanks.

岳永超

北京云天创科技有限公司 ATZGB(BeiJing) Co.,Ltd.

Mob:18101210380

Fax:010-82176393-808

北京市海淀区上地7街(信息路2号)国际创业园2号楼20B

yueyongchao@yeah.net

From: Martin Latal

Date: 2014-10-08 19:00

To: yongchao yue

Subject: Re: - how to use UDP in mqx to receive a broadcast datagram .eg. (use udp to recv data from 192.168.1.255)

how to use UDP in mqx to receive a broadcast datagram .eg. (use udp to recv data from 192.168.1.255)

reply from Martin Latal in MQX Software Solutions - View the full discussion

From PC, can you try to send to 255.255.255.255. destination ?

Did your question get answered?

If so, say thanks by clicking Correct Answer in the community thread!

Reply to this message by replying to this email, or go to the message on Freescale Community

Start a new discussion in MQX Software Solutions by email or at Freescale Community

Following how to use UDP in mqx to receive a broadcast datagram .eg. (use udp to recv data from 192.168.1.255) in these streams: Inbox

0 Kudos
8,100 Views
Martin_
NXP Employee
NXP Employee

Please check that PC really sends out the packet with 255.255.255.255 destination ip to correct interface (where is TWR system connected to). I just found windows sends out only to one interface (the default) so if you have more interfaces, double check that the packet is there.

with my own code:

I can receive packet with destination 255.255.255.255.

I can't receive packet with destination 192.168.1.255. I can see it is discarded (in network statistics).

We will check this issue and possibly fix for the next MQX release.

By the way, for any new development, you should better use MQX 4.1.1.

-Martin

0 Kudos
8,099 Views
m_bach
Contributor III

Hi there,

I'm also receiving incoming UDP datagrams via unicast and broadcast. This is OK so far, but I'm looking for a way to distinguish between an inomcing Unicast and an incoming Broadcast. recvfrom() isn't able to give the destination IP address, and there is no recvmsg() function in MQX 4.1.1.

Could you please tell me if there is any kind of way to get the destination address as appliaction?

I'm just hacking my way through RTCS's udp.c and sdgram.c to see what I can do to add this, but I don't want to tamper things too much in there, for I like to stick to 'vanilla' here ...

Thanks, Martin

0 Kudos
8,099 Views
Martin_
NXP Employee
NXP Employee

I'm afraid there is "ready to use" way. We might implement this for next RTCS, can you please tell me which protocol/specification requires this feature ?

0 Kudos
8,099 Views
m_bach
Contributor III

Hi Martin,

what about getsockopt RTCS_SO_IP_RX_DEST, does this one has a change?

I will try it out later on...

cheers, Martin

0 Kudos
8,099 Views
Martin_
NXP Employee
NXP Employee

Hello,

I'm aware about this option, but:

1)information from IP layer is about the latest datagram only (for example, UDP socket might have received two datagrams, one unicast, second broadcast, sitting in the UDP receive queue, until recvfrom() is called. so the unicast destaddr is overwritten by broadcast destaddr.

2)by code inspection it does not do what you need. Currently it is just last destination the RTCS sends to, or it is updated also during ip forwarding.

-Martin

0 Kudos
8,098 Views
m_bach
Contributor III

Hi Martin,

thanks a lot for that sophisticated explanation. It seemed to work at first, anyway 'destination address of the last frame that the socket received' sounded suspicious to me. So I stick to having 2 IPv4 sockets listening on the same UDP port, one with INETADDR_ANY, and one listening on the device's IP address.

thanks, Martin

0 Kudos
8,098 Views
Martin_
NXP Employee
NXP Employee

Just updating this post, I looked again and it seems RTCS_SO_IP_RX_DEST might work. I overlooked the information about destination address is kept in the PCB structure (with datagram data) and copied into the socket along with recvfrom(). so it might work, only for IPv4. IPv6 does not support this socket option. I still think two sockets is better as it would allow at least to prioritize (for example, you may want to service unicast first).

0 Kudos
8,098 Views
m_bach
Contributor III

Hi Martin,

I helped myself with creating three sockets to the same UDP port:

one for IPv4 bound to the interface IP, one IPv4 bound to INET_ANY, one last but not leas one to IPv6.

that'll do

Thanks, Martin

0 Kudos
8,099 Views
m_bach
Contributor III

Hi Martin,

I'm having this issue when porting a custom UDP protocoll to MQX.

I'm using this in a multi purpose BootLoader protocol, to get/set the IPv4 network config, and to have kind of a neighbour discovery, where the device should reply incoming broadcasts by sending a broadcast, and reply by unicast to incoming unicasts.

Let's say the device is configured to 10.0.0.10/24, and there is no DHCP Server available.

My PC, configured as 192.168.0.1/24, sends a custom broadcast to my custom UDP port.

A device receiving this broadcast-call replies by broadcast, sending it's device status information, even if the device and the PC are not configured to the same network.

On the other hand, answering this device-config-query when having it received by unicast is crucial when I want to query a device in a routed, distant network.

Edit: it's just: I want to tell broadcast to unicast.

cheers, Martin

0 Kudos
8,099 Views
yongchaoyue
Contributor II

think you so much!

as you said,my windows pc can't broadcast datagram at 255.255.255.255.maybe the broadcast(255.255.255.255) will be discarded by windows.

I use my andriod phone to send broadcast_datagram at 255.255.255.255,and mqx recv it suceesully.

although 192.168.1.255 is still not working,my problems have been solved.

think you ,Martin Latal;

岳永超

北京云天创科技有限公司 ATZGB(BeiJing) Co.,Ltd.

Mob:18101210380

Fax:010-82176393-808

北京市海淀区上地7街(信息路2号)国际创业园2号楼20B

yueyongchao@yeah.net

From: Martin Latal

Date: 2014-10-08 20:12

To: yongchao yue

Subject: Re: - how to use UDP in mqx to receive a broadcast datagram .eg. (use udp to recv data from 192.168.1.255)

how to use UDP in mqx to receive a broadcast datagram .eg. (use udp to recv data from 192.168.1.255)

reply from Martin Latal in MQX Software Solutions - View the full discussion

Please check that PC really sends out the packet with 255.255.255.255 destination ip to correct interface (where is TWR system connected to). I just found windows sends out only to one interface (the default) so if you have more interfaces, double check that the packet is there.

with my own code:

I can receive packet with destination 255.255.255.255.

I can't receive packet with destination 192.168.1.255. I can see it is discarded (in network statistics).

We will check this issue and possibly fix for the next MQX release.

By the way, for any new development, you should better use MQX 4.1.1.

-Martin

Did your question get answered?

If so, say thanks by clicking Correct Answer in the community thread!

Reply to this message by replying to this email, or go to the message on Freescale Community

Start a new discussion in MQX Software Solutions by email or at Freescale Community

Following how to use UDP in mqx to receive a broadcast datagram .eg. (use udp to recv data from 192.168.1.255) in these streams: Inbox

0 Kudos
8,098 Views
yongchaoyue
Contributor II

tried,not working!if convenient,try it by yourself.

I have send my code together by accessory.

if you try sucessfully,tell me please.thanks.

岳永超

北京云天创科技有限公司 ATZGB(BeiJing) Co.,Ltd.

Mob:18101210380

Fax:010-82176393-808

北京市海淀区上地7街(信息路2号)国际创业园2号楼20B

yueyongchao@yeah.net

From: Martin Latal

Date: 2014-10-08 19:00

To: yongchao yue

Subject: Re: - how to use UDP in mqx to receive a broadcast datagram .eg. (use udp to recv data from 192.168.1.255)

how to use UDP in mqx to receive a broadcast datagram .eg. (use udp to recv data from 192.168.1.255)

reply from Martin Latal in MQX Software Solutions - View the full discussion

From PC, can you try to send to 255.255.255.255. destination ?

Did your question get answered?

If so, say thanks by clicking Correct Answer in the community thread!

Reply to this message by replying to this email, or go to the message on Freescale Community

Start a new discussion in MQX Software Solutions by email or at Freescale Community

Following how to use UDP in mqx to receive a broadcast datagram .eg. (use udp to recv data from 192.168.1.255) in these streams: Inbox

0 Kudos
8,099 Views
yongchaoyue
Contributor II

code has been Modified as followed.but it still has something wrong.

the mqx(192.168.1.222) can send a datagram to 192.168.1.255 and my pc(192.168.1.112) can recv mqx's broadcast datagram.

but my pc send a broadcast datagram to 192.168.1.255,mqx can't recv pc's broadcast datagram.

sendsock = socket(PF_INET, SOCK_DGRAM, 0);

/* Bind the socket to the Telnet port */

addr.sin_family = AF_INET;

addr.sin_port = 5055;

//addr.sin_addr.s_addr = ENET_IPADDR;

addr.sin_addr.s_addr = INADDR_ANY;

dest_addr.sin_family = AF_INET;

dest_addr.sin_port = 5055;

dest_addr.sin_addr.s_addr = INADDR_BROADCAST;

error = bind(sendsock, &addr, sizeof(addr)); //return_error_if(error != RTCS_OK)

timedelay(100);

rlen = sizeof(dest_addr);

sendto(sendsock, senddata, strlen(senddata), 0,&dest_addr, rlen);

while(1)

{

ret = RTCS_selectset(&sendsock, 1, 1000);

if(ret == sendsock)

{

count = recvfrom(sendsock, recvdata, 512, 0, (struct sockaddr *) &dest_addr,&rlen);

if(count == RTCS_ERROR)

{

;

}

else

{

if(count>0)

{

//_io_write(serial4_fd, recvdata, count);

//_time_delay(1000);

//memset(recvdata,0,sizeof(recvdata));

//count = 0;

//cmd_reply_len = phone_cmd_analsys((uint_8*)recvdata,count);

//sendto(sendsock, phone_reply, cmd_reply_len, 0,&dest_addr, rlen);

sendto(sendsock, "123", 3, 0,&dest_addr, rlen);

memset(phone_reply,0,PHONE_REPLY_LEN);

count = 0;

}

}

}

}

岳永超

北京云天创科技有限公司 ATZGB(BeiJing) Co.,Ltd.

Mob:18101210380

Fax:010-82176393-808

北京市海淀区上地7街(信息路2号)国际创业园2号楼20B

yueyongchao@yeah.net

From: Martin Latal

Date: 2014-10-08 15:19

To: yongchao yue

Subject: Re: - how to use UDP in mqx to receive a broadcast datagram .eg. (use udp to recv data from 192.168.1.255)

how to use UDP in mqx to receive a broadcast datagram .eg. (use udp to recv data from 192.168.1.255)

reply from Martin Latal in MQX Software Solutions - View the full discussion

SO_BROADCAST is used for sending broadcast datagrams, not for receiving. We don't currently have this option in RTCS.

OPT_RECEIVE_PUSH is for a TCP socket, not UDP.

to receive a broadcast from any interface, you should bind the UDP socket to zero ip address (INADDR_ANY) and your port (per your source code, seems you already have this).

It should be enough to get this datagram with the recvfrom() function. Remove the two socket options from your code and give it a try.

-Martin

Did your question get answered?

If so, say thanks by clicking Correct Answer in the community thread!

Reply to this message by replying to this email, or go to the message on Freescale Community

Start a new discussion in MQX Software Solutions by email or at Freescale Community

Following how to use UDP in mqx to receive a broadcast datagram .eg. (use udp to recv data from 192.168.1.255) in these streams: Inbox

0 Kudos