1 /*
2  * Copyright 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <time.h>
21 
22 #include "wfc_util_log.h"
23 
24 #define WFC_UTIL_RANDOM_MAC_HEADER "001122"
25 
wfc_util_htoa(unsigned char * pHexaBuff,int szHexaBuff,char * pAsciiStringBuff,int szAsciiStringBuff)26 void wfc_util_htoa(unsigned char *pHexaBuff, int szHexaBuff, char *pAsciiStringBuff, int szAsciiStringBuff)
27 {
28 	int i, j;
29 	char hex_table[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
30 	                                 'A', 'B', 'C', 'D', 'E', 'F'};
31 
32 	if ((szHexaBuff*2) > szAsciiStringBuff) {
33 		wfc_util_log_error("wfc_util_htoa : not enough buffer size(%d)", szAsciiStringBuff);
34 		return;
35 	}
36 
37 	memset(pAsciiStringBuff, 0, szAsciiStringBuff);
38 
39 	/* for (i = szHexaBuff-1, j = 0; i >= 0; i--, j += 2) { */
40 	for (i = 0, j = 0; i < szHexaBuff; i++, j += 2) {
41 		/*pAsciiStringBuff[j]     = hex_table[(pHexaBuff[i] >> 4) & 0x0F];
42 		*/
43 		pAsciiStringBuff[j]     = hex_table[pHexaBuff[i] >> 4];
44 		pAsciiStringBuff[j + 1] = hex_table[pHexaBuff[i] & 0x0F];
45 	}
46 
47 	return;
48 }
49 
wfc_util_atoh(char * pAsciiString,int szAsciiString,unsigned char * pHexaBuff,int szHexaBuff)50 void wfc_util_atoh(char *pAsciiString, int szAsciiString, unsigned char *pHexaBuff, int szHexaBuff)
51 {
52 	int i, pos;
53 	char temp;
54 
55 	if ( 0!=(szAsciiString%2) || (szHexaBuff*2) < szAsciiString) {
56 		wfc_util_log_error("wfc_util_atoh : not enough buffer size(%d)", szHexaBuff);
57 		return;
58 	}
59 
60 	memset(pHexaBuff, 0, szHexaBuff);
61 
62 	for (i=0 ; i<szAsciiString ; i++) {
63 
64 		/* pos = (szAsciiString - i - 1) / 2; */
65 		pos = i / 2;
66 		temp = pAsciiString[i];
67 
68 		if (temp >= '0' && temp <= '9') {
69 			temp = temp - '0';
70 		} else if ( temp >= 'a' && temp <= 'f' ) {
71 			temp = temp - 'a' + 10;
72 		} else if ( temp >= 'A' && temp <= 'F' ) {
73 			temp = temp - 'A' + 10;
74 		} else {
75 			temp = 0;
76 		}
77 
78 		if (0==i%2) {
79 			pHexaBuff[pos] = temp<<4;
80 		} else {
81 			pHexaBuff[pos] |= temp;
82 		}
83 	}
84 
85 	return;
86 }
87 
88 /*
89  * wfc_util_is_random_mac
90  *
91  * return : it will return 1 if [mac_add] is same with WFC_UTIL_RANDOM_MAC_HEADER
92  *          or will return 0 if not.
93  */
wfc_util_is_random_mac(char * mac_add)94 int wfc_util_is_random_mac(char *mac_add)
95 {
96 	if(0 == strncmp(mac_add, WFC_UTIL_RANDOM_MAC_HEADER, 6)) {
97 		return 1;
98 	}
99 
100 	return 0;
101 }
102 
103 /*
104  * wfc_util_random_mac
105  *
106  * Create random MAC address
107  *
108  * return : void
109  */
wfc_util_random_mac(unsigned char * mac_addr)110 void wfc_util_random_mac(unsigned char* mac_addr)
111 {
112 	unsigned long int rand_mac;
113 
114 	if(NULL == mac_addr) {
115 		wfc_util_log_error("wfc_util_random_mac : buffer is NULL");
116 		return;
117 	}
118 
119 	/* Create random MAC address: offset 3, 4 and 5 */
120 	srandom(time(NULL));
121 	rand_mac=random();
122 
123 	#ifndef WFC_UTIL_RANDOM_MAC_HEADER
124 	mac_addr[0] = (unsigned char)0x00;
125 	mac_addr[1] = (unsigned char)0x11;
126 	mac_addr[2] = (unsigned char)0x22;
127 	#else  /* WFC_UTIL_RANDOM_MAC_HEADER */
128 	wfc_util_atoh(WFC_UTIL_RANDOM_MAC_HEADER, 6, mac_addr, 3);
129 	#endif /* WFC_UTIL_RANDOM_MAC_HEADER */
130 	mac_addr[3] = (unsigned char)rand_mac;
131 	mac_addr[4] = (unsigned char)(rand_mac >> 8);
132 	mac_addr[5] = (unsigned char)(rand_mac >> 16);
133 
134 	return;
135 }
136