summaryrefslogtreecommitdiff
path: root/conn_init/wfc_util_common.c
blob: b149920401836385898705726056c40cb03caac4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "wfc_util_log.h"

#define WFC_UTIL_RANDOM_MAC_HEADER "001122"

void wfc_util_htoa(unsigned char *pHexaBuff, int szHexaBuff, char *pAsciiStringBuff, int szAsciiStringBuff)
{
	int i, j;
	char hex_table[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
	                                 'A', 'B', 'C', 'D', 'E', 'F'};

	if ((szHexaBuff*2) > szAsciiStringBuff) {
		wfc_util_log_error("wfc_util_htoa : not enough buffer size(%d)", szAsciiStringBuff);
		return;
	}

	memset(pAsciiStringBuff, 0, szAsciiStringBuff);

	/* for (i = szHexaBuff-1, j = 0; i >= 0; i--, j += 2) { */
	for (i = 0, j = 0; i < szHexaBuff; i++, j += 2) {
		/*pAsciiStringBuff[j]     = hex_table[(pHexaBuff[i] >> 4) & 0x0F];
		*/
		pAsciiStringBuff[j]     = hex_table[pHexaBuff[i] >> 4];
		pAsciiStringBuff[j + 1] = hex_table[pHexaBuff[i] & 0x0F];
	}

	return;
}

void wfc_util_atoh(char *pAsciiString, int szAsciiString, unsigned char *pHexaBuff, int szHexaBuff)
{
	int i, pos;
	char temp;

	if ( 0!=(szAsciiString%2) || (szHexaBuff*2) < szAsciiString) {
		wfc_util_log_error("wfc_util_atoh : not enough buffer size(%d)", szHexaBuff);
		return;
	}

	memset(pHexaBuff, 0, szHexaBuff);

	for (i=0 ; i<szAsciiString ; i++) {

		/* pos = (szAsciiString - i - 1) / 2; */
		pos = i / 2;
		temp = pAsciiString[i];

		if (temp >= '0' && temp <= '9') {
			temp = temp - '0';
		} else if ( temp >= 'a' && temp <= 'f' ) {
			temp = temp - 'a' + 10;
		} else if ( temp >= 'A' && temp <= 'F' ) {
			temp = temp - 'A' + 10;
		} else {
			temp = 0;
		}

		if (0==i%2) {
			pHexaBuff[pos] = temp<<4;
		} else {
			pHexaBuff[pos] |= temp;
		}
	}

	return;
}

/*
 * wfc_util_is_random_mac
 *
 * return : it will return 1 if [mac_add] is same with WFC_UTIL_RANDOM_MAC_HEADER
 *          or will return 0 if not.
 */
int wfc_util_is_random_mac(char *mac_add)
{
	if(0 == strncmp(mac_add, WFC_UTIL_RANDOM_MAC_HEADER, 6)) {
		return 1;
	}

	return 0;
}

/*
 * wfc_util_random_mac
 *
 * Create random MAC address
 *
 * return : void
 */
void wfc_util_random_mac(unsigned char* mac_addr)
{
	unsigned long int rand_mac;

	if(NULL == mac_addr) {
		wfc_util_log_error("wfc_util_random_mac : buffer is NULL");
		return;
	}

	/* Create random MAC address: offset 3, 4 and 5 */
	srandom(time(NULL));
	rand_mac=random();

	#ifndef WFC_UTIL_RANDOM_MAC_HEADER
	mac_addr[0] = (unsigned char)0x00;
	mac_addr[1] = (unsigned char)0x11;
	mac_addr[2] = (unsigned char)0x22;
	#else  /* WFC_UTIL_RANDOM_MAC_HEADER */
	wfc_util_atoh(WFC_UTIL_RANDOM_MAC_HEADER, 6, mac_addr, 3);
	#endif /* WFC_UTIL_RANDOM_MAC_HEADER */
	mac_addr[3] = (unsigned char)rand_mac;
	mac_addr[4] = (unsigned char)(rand_mac >> 8);
	mac_addr[5] = (unsigned char)(rand_mac >> 16);

	return;
}