nx_ip_interface_info_get + MAC Address

Hello everybody,

Simple question: i would like to get MAC Address. I'm using nx_ip_interface_info_get(&g_ip1, 0, &name_ptr, &ip_address,&network_mask,&mtu_size, &msw,
&lsw);

msw and lsw is correct and now i would like to format it as FF:FF:FF:FF:FF:FF with sprintf(mac, "%02x:%02x:%02x:%02x:%02x:%02x", ((msw >> 8) & 0xFF), ((msw) & 0xFF), ((lsw >> 24) & 0xFF), ((lsw >> 16) & 0xFF), ((lsw >> 8) & 0xFF), ((lsw) & 0xFF));

mac is a char[17].

The result is several character but not the mac address. What's wrong?

Thanks!

Mickael

  • Hi Mickael,

    This works for me:

    ULONG msw;
    ULONG lsw;

    sprintf(msg, "%02X:%02X:%02X:%02X:%02X:%02X",(int) (msw>>8), (int) (msw&0xFF), (int) (lsw>>24), (int) (lsw>>16&0xFF),(int) (lsw>>8&0xFF),(int) (lsw&0xFF));

    -Gary
  • In reply to garyj:

    Hello gary,
    I try your example storing msg into char[17] but the value is not correct. What is msg?
    Regards !
    Mickael
  • In reply to mlerat27:

    Mickael,

    msg is a typo - should be mac.

    If the MAC address is not correct, make sure that you are calling the nx_ip_interface_info_get() after the nx_ip_interface_status_check() returns success and the link is active by checking NX_IP_LINK_ENABLED.

    -Gary
  • In reply to garyj:

    Gary,
    I made the modification as you said. When i put a breakpoint and check the value of mac, it seems that the value is equal to the pointer address. Is it normal. What's wrong with my code?
    Mickael
  • In reply to mlerat27:

    HI Mickael,

    "mac is a char[17]", is too short. there is no space for the \0 at the end of the string.
    Dont know if that creates trouble by overwriting the momory on the 18th position.

    good luck
    Remo
  • In reply to @remo:

    Hi Remo,
    Thanks for your comment. I already test with \0 adding 2 char in my array and it doesn't work!
    Mickael
  • In reply to mlerat27:

    Hi Mickael,

    As Remo suggested, Declaring mac as char[18] should work:

    char mac[18];
    sprintf(mac, "%02x:%02x:%02x:%02x:%02x:%02x", ((msw >> 8) & 0xFF), ((msw) & 0xFF), ((lsw >> 24) & 0xFF), ((lsw >> 16) & 0xFF), ((lsw >> 8) & 0xFF), ((lsw) & 0xFF));

    Moreover, instead of sprintf I recommend to use a safer version, snprintf:

    snprintf(mac, sizeof(mac), "%02x:%02x:%02x:%02x:%02x:%02x", ((msw >> 8) & 0xFF), ((msw) & 0xFF), ((lsw >> 24) & 0xFF), ((lsw >> 16) & 0xFF), ((lsw >> 8) & 0xFF), ((lsw) & 0xFF));

    Regards,
    adboc

  • In reply to adboc:

    one more thing:
    printf is not reentrent, which means, if you use printf in multiple threads at the same time they might overwrite internal conversion buffers. In addition printf is quite hungry in terms of required ram resources. You might use the attached code instead. If you want to use the mac2str function not only localy, you should add a third parameter supplying a pointer to the result string and thus making this function reentrant. If you use it only in your actual thread, you don't need to go this extra step.

    inline char dez2hex( ULONG val);
    inline char dez2hex( ULONG val)
    {
    char c;
    val = val & 0xFu; //truncate to right 4 bit
    if( val < 10 )
    c = (char)('0' + val);
    else
    c = (char)('A' + val -10);
    return c;
    }

    inline char *mac2str( ULONG msw, ULONG lsw );
    inline char *mac2str( ULONG msw, ULONG lsw )
    {
    static char str[18];

    str[ 0] = dez2hex( msw >> 12 );
    str[ 1] = dez2hex( msw >> 8 );
    str[ 2] = ':';
    str[ 3] = dez2hex( msw >> 4 );
    str[ 4] = dez2hex( msw >> 0 );
    str[ 5] = ':';
    str[ 6] = dez2hex( lsw >> 28 );
    str[ 7] = dez2hex( lsw >> 24 );
    str[ 8] = ':';
    str[ 9] = dez2hex( lsw >> 20 );
    str[10] = dez2hex( lsw >> 16 );
    str[11] = ':';
    str[12] = dez2hex( lsw >> 12 );
    str[13] = dez2hex( lsw >> 8 );
    str[14] = ':';
    str[15] = dez2hex( lsw >> 4 );
    str[16] = dez2hex( lsw >> 0 );
    str[17] = '\0';

    return str;
    }

    in your code:
    printf(" my MAC is %s\n", mac2str( g_ip1.nx_ip_interface[0].nx_interface_physical_address_msw,
    g_ip1.nx_ip_interface[0].nx_interface_physical_address_lsw) );

    Have fun
    Remo
  • In reply to @remo:

    sorry, small bugfix:

    Please replace the inline by static.

    You may optionally make the dez2hex an inline static.

    Thus:

    inline static char dez2hex( ULONG val);

    inline static char dez2hex( ULONG val)

    {

    ....

    and

    static char *mac2str( ULONG msw, ULONG lsw );

    static char *mac2str( ULONG msw, ULONG lsw )

    {

    ...