This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Issue with fetching MAC Address of device

Hi,

From below code I'm  fetching the mac address of device but unable to get it. Is their anything goes wrong in the code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
char * getMacAddress()
{
    int i;
    unsigned char macAddressVal[SL_MAC_ADDR_LEN];
    unsigned char macAddressLen = SL_MAC_ADDR_LEN;
    sl_NetCfgGet(SL_MAC_ADDRESS_GET,NULL,&macAddressLen,(unsigned char *)macAddressVal);
    char macAddressPart[2];
    static char macAddressFull[18]; //18
    for (i = 0 ; i < 6 ; i++)
    {
        sprintf(macAddressPart, "%02X", macAddressVal[i]);
        strcat(macAddressFull, (char *)macAddressPart);
        strcat(macAddressFull, ":");
    }
    macAddressFull[17] = '\0'; // Replace the the last : with a zero termination
    return macAddressFull;
}

Thanks & Regards,

Shashank

  • i think you need to change the char to int

    please check out the example here:
    software-dl.ti.com/.../porting.html

    under Modules, then in Netcfg
    first example
  • Hi Josh,

    I tried with below code as you suggested but still its not getting mac address.Can you please check once?

     _u8 macAddressVal[SL_MAC_ADDR_LEN];
    		          _u8 macAddressLen = SL_MAC_ADDR_LEN;
    
    		          	sl_NetCfgGet(SL_MAC_ADDRESS_GET,NULL,&macAddressLen,(_u8 *)macAddressVal);
    
    		          	char macAddressPart[2];
    		          	static char macAddressFull[18]; //18
    
    		          	for (i = 0 ; i < 6 ; i++)
    		          	{
    		          		sprintf(macAddressPart, "%02X", macAddressVal[i]);
    		          		strcat(macAddressFull, (char *)macAddressPart);
    		          		strcat(macAddressFull, ":");
    		          	}
    
    		          	macAddressFull[17] = '\0'; // Replace the the last : with a zero termination
    
                             	return macAddressFull;

    Thanks & Regards,

    Shashank

  • still see you using char on your lines 8 and 9, might want to change those to u8 as well.
  • Hi Shashank,

    What do you mean by "unable to get it"?
    Does the sl_NetCfgGet return?
    If yes, please set a breakpoint after this function call and check the return code. Then verify that "macAddressVal" contains the right address.
    If it does, debug(step by step) your code that generates the string.

    br,
    Kobi
  • Hi Josh,

    I changed char to u8 but still in my array its not getting the mac address .Also at same time i'm writing the mac address to uart.I'm checking on tera term i'm getting - A5:A5:A5:A5:A5:A5  on terminal.

    By default i didn't change the mac address.

    Attached is the debugging window for reference and tera term output.

                                  int i;
    
    		          _u8 macAddressVal[SL_MAC_ADDR_LEN];
    		          _u8 macAddressLen = SL_MAC_ADDR_LEN;
    
    		          	sl_NetCfgGet(SL_MAC_ADDRESS_GET,NULL,&macAddressLen,(_u8 *)macAddressVal);
    		          //	Message(macAddressVal);
    		          //	printf("IP ",SL_MAC_ADDR_LEN);
    		          	_u8 macAddressPart[2];
    		          	_u8 macAddressFull[18]; //18
    
    		          	for (i = 0 ; i < 6 ; i++)
    		          	{
    		          		sprintf(macAddressPart, "%02X", macAddressVal[i]);
    		          		strcat(macAddressFull, (_u8*)macAddressPart);
    		          		strcat(macAddressFull, ":");
    		          		//Message(macAddressFull);
    		          	}
    
    		          	macAddressFull[17] = '\0'; // Replace the the last : with a zero termination
    
                                 Message(macAddressFull);
    		          	return macAddressFull;

    Thanks & Regards,

    Shashank

  • Hi Shashank,

    Please set a breakpoint after the "sl_NetCfgGet" call and check the return code (add variable for the return code). Then verify that "macAddressVal" contains the right address.
    If it does, debug(step by step) your code that generates the string.

    br,
    Kobi
  • Hi Kobi,

    I checked as you suggested after the "sl_NetCfgGet" call. Nothing  received in "macAddressVal". Below is the screenshot for the reference-

    Please advise

    Thanks & Regards,

    Shashank

  • Hi Shashank,

    The RetVal of -100, means that you call it in invalid state.

    You need to call sl_Start before you call this command (or any other Simplelink command).

    Always make sure that you check the return values of the SL commands to catch such issues.

    BTW. you can change the "number format" of the presentation in the Expressions tab by using the "right mouse button" and selecting "Number Format".

    For MAC Addresses - although it is defined as array of characters, the best representation would be Hex or Decimal.

    Br,

    Kobi

  • Hi Kobi,

    Thanks for the reply.

    I changed the code as you suggested.Below is the screenshot for your reference. Is it correct?  I received the mac address but don't know is it correct or not. How can I check is it correct or not?

    Thanks & Regards,

    Shashank

  • The return value is 0 which means OK.
    The MAC address seems like a valid TI's one.

    Br,
    Kobi
  • Hi Kobi,

    But how can I check is this the correct one? Like by advertising on app or something?

    Also, I want to copy into the array so I can use it for my application.I tried with above method for copy it to another array but something goes wrong.

    Please advise.

    Thanks & Regards,
    Shashank
  • Anything that you transmit will use this MAC address. You can use a sniffer or you can open an access point on the device - and use some WiFi analyzer application to check the BSSID of the network.
    I'm not sure what do you mean by "copy into the array". Doesn't the "macAddressFull" show the mac address? (the code seems ok).

    br,
    Kobi
  • Hi Kobi,

    Yes, MAC address is not get copied properly .Some other values I'm getting in "macAddressFull".Below is the screenshot for the reference.

    Also mac address only shown when its in debug and place break point.When I'm trying to print it on UART it also not printing. 

    Please advise.

    Thanks & Regards,

    Shashank

  • Hi Shashank,

    I'm not sure where did you stopped the debugger (the fact that the "macAddressPart is not identified" suggests that it is not in the context of the above routine).

    The static array (why do you use it as "static"?)  may be overwritten.

    Please stop the debugger after you get the mac address and go step by step through your code and check what fails.

    Br,

    Kobi

  • Hi Kobi,

    mac address is copied into the "macAddressFull"but its only seen when I stopped the debugger at every step after sl_NetCfgGet() call but after that i can't able to see the mac address in"macAddressFull".

    below logic i'm using -

     for(;;)
    		       {
    
    		    	    int i;
    		    	   // _u8 macAddressPart[2];
    		    	    _u8 macAddressFull[6]; 
    		    	    _u8 macAddressVal[SL_MAC_ADDR_LEN];
    		    	    _u8 macAddressLen = SL_MAC_ADDR_LEN;
    		    		sl_Start(NULL,NULL,NULL);
    		    		sl_NetCfgGet(SL_MAC_ADDRESS_GET,NULL,&macAddressLen,(_u8 *)macAddressVal);
    
    		    		memcpy(macAddressFull,macAddressVal,sizeof(macAddressFull));
    
    		    		return macAddressFull;
    
    
    		       }
    

    I stopped the debugger every step after sl_NetCfgGet() its shows on debugging window its get copied into "macAddressFull".Below is the screenshot for your reference.

     .

    It only shows mac address in macAddressFull only when I put breakpoint in it after breakpoint it shows identifier not found error like this -

    Is it mac address is stored or not in "macAddressFull"?

    Please help.

    Thanks & Regards,

    Shashank

  • Hi Shashank,

    Your code is totally wrong. You cannot return pointer to local variable. Local variables are stored at stack and after return are not valid.

    Please use global variables passed into function via pointer.

    Jan
  • Hi Jan,

    I made changes in the code,also I used global variable i.e. "macAddressFull".Now its working fine. Below is the working code for get MAC address and storing it into the array i.e "maAddressFull".

                                         
    			    	       _u8 macAddressVal[SL_MAC_ADDR_LEN];
    			    	        _u8 macAddressLen = SL_MAC_ADDR_LEN;
    			    		sl_Start(NULL,NULL,NULL);
    			    		sl_NetCfgGet(SL_MAC_ADDRESS_GET,NULL,&macAddressLen,(_u8 *)macAddressVal);
    
    			    		memcpy(macAddressFull, (char *)macAddressVal,sizeof(macAddressFull));

    Best Regards,

    Shashank