ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ControlEngine / iPhone / Classes / wifi.c
1 /*
2  *  wifi.c
3  *  ARDroneEngine
4  *
5  *  Created by f.dhaeyer on 30/03/11.
6  *  Copyright 2011 Parrot SA. All rights reserved.
7  *
8  */
9 #include "wifi.h"
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <sys/ioctl.h>
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <netdb.h>
19 #include <arpa/inet.h>
20 #include <sys/sockio.h>
21 #include <net/if.h>
22 #include <errno.h>
23 #include <net/if_dl.h>
24 #include <net/ethernet.h>
25
26 #define max(a,b)        ((a > b) ? a : b)
27
28 #define BUFFERSIZE      4000
29
30 char iphone_mac_address[] = "00:00:00:00:00:00";
31
32 void get_iphone_mac_address(const char *itfName)
33 {
34         if(itfName)
35         {
36                 struct ifconf ifc;
37                 struct ifreq *ifr;
38                 int sockfd;
39                 char buffer[BUFFERSIZE], *cp, *cplim;
40                 char temp[80];
41                 
42                 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
43                 if (sockfd >= 0)
44                 {
45                         ifc.ifc_len = BUFFERSIZE;
46                         ifc.ifc_buf = buffer;
47                         
48                         if (ioctl(sockfd, SIOCGIFCONF, (char *)&ifc) >= 0)
49                         {               
50                                 ifr = ifc.ifc_req;
51                                 
52                                 cplim = buffer + ifc.ifc_len;
53                                 for (cp = buffer ; cp < cplim ; )
54                                 {
55                                         ifr = (struct ifreq *)cp;
56                                         if (ifr->ifr_addr.sa_family == AF_LINK)
57                                         {
58                                                 struct sockaddr_dl *sdl = (struct sockaddr_dl *)&ifr->ifr_addr;
59                                                 int a,b,c,d,e,f;
60                                                 
61                                                 strcpy(temp, (char *)ether_ntoa((const struct ether_addr *)LLADDR(sdl)));
62                                                 sscanf(temp, "%x:%x:%x:%x:%x:%x", &a, &b, &c, &d, &e, &f);
63                                                 sprintf(temp, "%02X:%02X:%02X:%02X:%02X:%02X",a,b,c,d,e,f);
64                                                 if(strcmp(ifr->ifr_name, itfName) == 0)
65                                                 {
66                                                         strcpy(iphone_mac_address, temp);
67                                                         break;
68                                                 }
69                                         }
70                                         cp += sizeof(ifr->ifr_name) + max(sizeof(ifr->ifr_addr), ifr->ifr_addr.sa_len);
71                                 }
72                         }
73                         
74                         close(sockfd);
75                 }
76         }
77 }