OpenThread COAP Client to a public server over Internet

Hello,

I am trying to make a Thread COAP Client on nRF52840 DK work with coap.me public server. I have created a Border Router with a Raspberry Pi and a nRF52 dongle and I am sure that it works as I can ping and run COAP CLI commands successfully with the OpenThread CLI sample. I can COAP GET and PUT to coap.me.

The coap.me IPv6 address was converted by me with the well known 64:ff9b::/96 prefix. The path was added to the border router as I can see with ot netdata show. 

The problem is with the COAP Client sample provided. I modified the destination IPv6 address to be the same I use with the CLI sample and the URI path to "coap://coap.me:5683/hello". I have also tried just "hello". No luck so far. 

As I said the CLI sample works fine: I get a response which is "world". Here the CLI commands I use after having joined the Thread network:

ot coap start

ot coap get 64:ff9b:0:0:0:0:8666:da12 hello 

NOTE: in my COAP client program the Thread network is joined automatically as I have added the following options:

CONFIG_OPENTHREAD_JOINER=y
CONFIG_OPENTHREAD_JOINER_AUTOSTART=y
CONFIG_OPENTHREAD_JOINER_PSKD="J01NME"

Indeed I can ping the Border router IPV6 address from my board running the modified COAP Client and vice-versa so they see each other. 

Can you please help to understand why my program directly derived from your COAP client sample does not work while CLI works fine?

Thank you.

Marco

  • Hi Arun, I can't share my code for contract reasons as I am working for a customer of mine. Anyway the source code shared above Charlie is still available and it is what I based my application on. However I am using IPv6 address as I could not make it work on IPv4 as you need. So I suggest you to obtain the IPv6 address from the server IPv4 one.

    bool COMM_getIpv6Address( uint8_t *ipv4_addr, struct in6_addr *ipv6_addr )
    {
    int i;
    otIp4Address tmp_ipv4_addr;
    otIp6Address synt_ipv6_addr;
    struct openthread_context *ot = ot_context;
    
    for (i=0; i<4; i++)
    {
    tmp_ipv4_addr.mFields.m8[i] = ipv4_addr[i];
    }
    
    if (OT_ERROR_NONE != otNat64SynthesizeIp6Address(
    ot->instance,
    (const otIp4Address *)&tmp_ipv4_addr,
    &synt_ipv6_addr))
    {
    LOG_ERR("ERROR otNat64SynthesizeIp6Address() function!");
    return false;
    }
    
    for (i=0; i<16; i++)
    {
    ipv6_addr->s6_addr[i] = synt_ipv6_addr.mFields.m8[i];
    }
    
    return true;
    }
    The Border Router must have the NAT64 (or equivalent) enabled and I think that latest OpenThread Border Router release has it already enabled by default.
    I hope it helps.
    Cheers.
    Marco
  • Thanks Marco,

    Since I am a beginner, pls also help which header files are required and how to define all variables.

    Also - where do I give my OTBR IPv4 address in this? 

    Thanks in advance for your help. 

    Regards

    Arun

  • bool SynthesizeIpv4ToIpv6Adress(char ip_str[], struct in6_addr *ipv6_addr)
    {
    	uint8_t ipv4_bytes[4];
    
        char *token = strtok(ip_str, ".");
        for (int i = 0; i < 4; i++) {
            ipv4_bytes[i] = atoi(token);
            token = strtok(NULL, ".");
        }
    	int i;
    	otIp4Address tmp_ipv4_addr;
    	otIp6Address synt_ipv6_addr;
    
    	for (i = 0; i < 4; i++)
    	{
    		tmp_ipv4_addr.mFields.m8[i] = ipv4_bytes[i];
    	}
    
    	if (OT_ERROR_NONE != otNat64SynthesizeIp6Address(
    							 instance,
    							 (const otIp4Address *)&tmp_ipv4_addr,
    							 &synt_ipv6_addr))
    	{
    		LOG_INF("ERROR otNat64SynthesizeIp6Address() function!");
    		return false;
    	}
    	for (i = 0; i < 16; i++)
    	{
    		ipv6_addr->s6_addr[i] = synt_ipv6_addr.mFields.m8[i];
    	}
    
    	return true;
    }

    and calling the function with:

    struct sockaddr_in6 addr6 = {
    		.sin6_family = AF_INET6,
    		.sin6_port = htons(COAP_DEFAULT_PORT),
    		.sin6_addr.s6_addr = {
    			0,
    		},
    		.sin6_scope_id = 0U};
    	
    	SynthesizeIpv4ToIpv6Adress("192.168.178.157", &addr6.sin6_addr);

Related