From 0e5ec067f7d26341f14132687d21628de1fd4097 Mon Sep 17 00:00:00 2001 From: Purushottam Kushwaha Date: Fri, 21 Jul 2017 22:11:45 +0530 Subject: [PATCH] qsap: Add API to get device mac address based on interface name. This adds new API linux_get_ifhwaddr() which can be used to get mac address of input interface. IOCTL SIOCGIFHWADDR is used to get the interface address. CRs-Fixed: 2080924 Change-Id: I1b825fb2c1f35280d2d57928be4f87d23d78decb --- softap/sdk/qsap_api.c | 28 ++++++++++++++++++++++++++++ softap/sdk/qsap_api.h | 1 + 2 files changed, 29 insertions(+) diff --git a/softap/sdk/qsap_api.c b/softap/sdk/qsap_api.c index 5dfbd44..9c3599f 100755 --- a/softap/sdk/qsap_api.c +++ b/softap/sdk/qsap_api.c @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include @@ -3469,6 +3470,33 @@ int qsap_control_bridge(int argc, char ** argv) } +int linux_get_ifhwaddr(const char *ifname, char *addr) +{ + struct ifreq ifr; + int sock = socket(AF_INET, SOCK_DGRAM, 0); + +#ifndef MAC2STR +#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] +#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x" +#endif + memset(&ifr, 0, sizeof(ifr)); + strlcpy(ifr.ifr_name, ifname, IFNAMSIZ); + if (ioctl(sock, SIOCGIFHWADDR, &ifr)) { + ALOGE("Could not get interface %s hwaddr: %s", ifname, strerror(errno)); + return -1; + } + + if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) { + ALOGE("%s: Invalid HW-addr family 0x%04x", ifname, ifr.ifr_hwaddr.sa_family); + return -1; + } + memcpy(addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN); + ALOGE("%s: " MACSTR, ifname, MAC2STR(addr)); + + return 0; +} + + int qsap_add_or_remove_interface(const char *newIface , int createIface) { const char *wlanIface = "wlan0"; diff --git a/softap/sdk/qsap_api.h b/softap/sdk/qsap_api.h index 3020fc7..5d841e7 100755 --- a/softap/sdk/qsap_api.h +++ b/softap/sdk/qsap_api.h @@ -618,6 +618,7 @@ int qsap_prepare_softap(void); int qsap_unprepare_softap(void); int qsap_is_fst_enabled(void); int qsap_control_bridge(int argc, char ** argv); +int linux_get_ifhwaddr(const char *ifname, char *addr); #if __cplusplus }; // extern "C"