MQX Ethernet: Send UDP Frame?

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

MQX Ethernet: Send UDP Frame?

1,369 Views
arnogir
Senior Contributor II

Hello

I'm on a K60D512 (on TWR) and I'm using MQX 4.2 and KDS v3.0.0.

I'm trying to work on Ethernet with UDP Frame.

I test to send from my laptop to the TWR an UDP frame with success, but not the reverse.

I have the following code (simplified)

#define ENET_IPADDR  IPADDR(192,168,1,128) #define ENET_IPMASK  IPADDR(255,255,0,0) #define ENET_IPGATEWAY  IPADDR(0,0,0,0)

error = RTCS_create();

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_address); ipcfg_init_device(BSP_DEFAULT_ENET_DEVICE, enet_address);

ipcfg_bind_staticip(BSP_DEFAULT_ENET_DEVICE, &ip_data);

sock = socket(AF_INET, SOCK_DGRAM, 0); local_sin.sin_family = AF_INET; local_sin.sin_port = 32768; local_sin.sin_addr.s_addr = INADDR_ANY; result = bind(sock, &local_sin, sizeof (sockaddr_in));

while(1) {   sock = RTCS_selectall(1000);   if (sock == 0)   {     printf("TimeOut expired. No data received. Trying again\n");   }   else   {     count = recvfrom(sock, my_buffer, 10, 0, &local_sin, &local_len);     if (count == RTCS_ERROR)     {       printf("\nrecvfrom() failed with error %lx", RTCS_geterror(sock));     }     else     {       counter++;       printf("\nReceived %ld bytes of data.", count);       printf("Data received: %s, Packet %d\n", my_buffer, counter);             count = sendto(sock, my_buffer, count, 0, &local_sin, local_len);       printf("--------SendTO Return = %d", count);

    }   } }

So If I set a static IP with ipcfg_bind_staticip send work correctly.

If I do not make it or if I try with IP = 255.255.255.255, sendto() return -1.

Until now, I have in my mind thet UDP not request a  specific IP address?

In fact, I must have a configurable system which auto adapt to its environment:

1) TWR <-> LapTop (On TCP)

2) TWR <-> Ethernet  Switch <-> LapTop (On TCP)

3) TWR <-> Ethernet  DHCP ROUTER <-> LapTop (On TCP)

So the  idea was to use UDP broadcast frame to request from the tower its IP address. (Allowed by DHCP if exist): LapTop make a Request on UDP, TWR respond on UDP.

If IP address not configured in TWR (because no DHCP), Laptop software will send an UDP frame with a chosen IP address to set TWR TCP/IP address.

This is why I want send UDP frame without previous IP address configuration...

0 Kudos
7 Replies

711 Views
DavidS
NXP Employee
NXP Employee

Hi Arnaud,

Please review following to see if it helps.

Re: Simple UDP communication

Remote monitoring solution using MQX and Kinetis MCUs<http://cache.freescale.com/files/32bit/doc/app_note/AN4644.pdf>  (REV 0)

20 Mar 2013 This Application Note demonstrates the implementation of a Remote Medical Monitor system using Kinetis MCUs and MQX RTOS

·      

·        PDF

·        138KB

·        Application Notes: AN4644

Associated File: AN4644SW<http://cache.freescale.com/files/32bit/doc/app_note/AN4644SW.zip>

Regards,

David

0 Kudos

711 Views
arnogir
Senior Contributor II

Hello

Yes I Already seen this example.

I used a big part of this to start my UDP experimentation.

But this example does not send  UDP frame on client side.

So I try to call "SendTo" after have received data on UDP with receivFrom.

I seen 2 problems:

- I can't send UDP frame if I not bind with IP address. It seems to me UDP can work without have IP address! ?? (Or bind with  a dummy address like (0,0,0,0) )

- In other side, I have some case where I start a TCP socket. Then I call "listen" and then "accept". This API is blocant until connection done on TCP way. I will try to manage UDP frame and TCP on different task.

0 Kudos

711 Views
RadekS
NXP Employee
NXP Employee

Hi Arnaud,

You are right. You cannot establish any meaningful connection when your IP address is still NULL.

If I understood correctly, main problem is communication in case when network don’t have DHCP server and board do not know network parameters.

I suppose that it has no sense creating something very new – it will just lead to more potential issues.

DHCP protocol has to be use first.

I would like to recommend for example:

  1. Start DHCP client and ask for IP address.
  2. If board does not receive IP address from DHCP server, board could some time listen whether opposite side sends also DHCP discovery message (it is also broadcasts messages). If opposite side ask also for IP address by DHCP discovery message, board could start his own DHCP server and assign IP address to both board and PC.
  3. If there isn’t DHCP server on network and PC do not send DHCP discovery message it is clear that either PC has hardly set IP address or PC is not connected. So, we can set some static IP address (probably from Private network address range) and potentially wait for some very specific broadcast/multicast message (send by PC application). On base of this specific message board recognize that source address belongs to my PC application. This broadcast could directly contain new IP address for board (it do not solve issue when 2 boards are connect to the same network) or rather board could use source IP address and set it as (default) gateway. In that case you could start standard communication even when board and PC IP address do not fits to the same network.

I hope it helps you.

Have a great day,
RadekS

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

711 Views
arnogir
Senior Contributor II

Hello

This helps me in the sense that it confirms my observations.


However, most cases for my application is the case number 3 but every time several card on the network.
So your solution is not working.

I will use a forbidden IP when board is clear (factory) :1.0.0.1 to replies to UDP frame. Then, the IP configuration procces will be run. After that, on the nexts power on, I will always use the last known IP. (by waiting new IP configuration via UDP)

Now, if someone has a clean solution to send an UDP frame with the SrcAddr = 0.0.0.0, let me know!

And so, provided solution in linked post seem working for the person who is asked the question. But not for me. May be I not use correctly the function?

:smileyhappy:

0 Kudos

711 Views
RadekS
NXP Employee
NXP Employee

Hi Arnaud,

Thank you for more details about network configuration.

Unfortunately there probably isn’t any universal solution for such self configured network.

You can develop your own special solution for assigning IP addresses by your PC application or you can just simply run DHCP server on PC side (This should be simplest solution).

Idea1:

This is probably half works idea - What about leave it on board DHCP?

Boards can simply start DHCP client. When board didn’t receive answer from DHCP server, board can start (after some random time) own HDCP server. The fastest board will assign IP addresses to others.

Idea2:

IPv6 - unique IP addresses.

I hope it helps you.

Have a great day,
RadekS

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

711 Views
arnogir
Senior Contributor II

Hello

This board is to be integrated in some different factory with an unknown network architecture.

Some network has DHCP, some has only a router without DHCP, other could be a simplest link between Laptop and board!

We also develop the LapTop software to communicate with the board  via Ethernet.

So, I can't implement DHCP in the board.

I am always amazed that MQX can not manage the UDP frame if sent with source IP to 0!

0 Kudos

711 Views
arnogir
Senior Contributor II

Hello

for the above problem, I choose to use a temporary IP address until assigned by UDP frame message.

But is I bind with address of type 1.0.0.1 or 10.0.0.1 this does not work, when I bind UDP sock:

= bind(DV_Eth_UDPSocketRxID, &DV_ETH_UDPSocketRxAddr, sizeof(DV_ETH_UDPSocketRxAddr));

Error returned is 0x1510...

Which IP Address Range is accepted by MQX? !!

for the problem number 2 listed above:

"I have some case where I start a TCP socket. Then I call "listen" and then "accept". This API is blocant until connection done on TCP way. I will try to manage UDP frame and TCP on different task."

Could you indicate me if it is because I have a bad use or if it is the MQX behavior?Explain my Case:

- On Init I call ipcfg_init_device..

- On a task: ipcfg_bind_staticip; socket(AF_INET, SOCK_DGRAM).. bind() to create an UDP socket to receive UDP Frame (Poll with receivFrom.  and then start second task

- On second task: Socket, bind, listen and then "accept" ( To wait TCP connection. Port is different from UDP socket!)

Effect: while accept not return because no connection on TCP, the UDP frame cannot be received:

recvfrom() failed with error 1704

Thank:smileyplain:

0 Kudos