mtk android10 蓝牙默认mac地址修改
mac地址可以通过官方工具手动写入,一个个写太麻烦了,要求也没有那么高,改为使用emmc id生成蓝牙mac地址。开机如果读到的蓝牙mac地址无效,会随机生成一个,然后进行保存,通过查找代码定位到文件
vendor/mediatek/proprietary/hardware/connectivity/bluetooth/driver/mt66xx/bluedroid/radiomod.c
中的GetRandomValue函数,修改这个函数就可以了,详细代码如下
int sthvalue(char c)
{
int value;
if((c>='0')&&(c<='9'))
value=48;
else if((c>='a')&&(c<='f'))
value=87;
else if((c>='A')&&(c<='F'))
value=55;
else{
return -1;
}
return value;
}
int strtohex(char *str,char *data)
{
int len=0;
int sum=0;
int high=0;
int low=0;
int value=0;
int j=0;
int i;
len=strlen(str);
//printf("%d\n",len);
for(i=0;i<len;i++)
{
value=sthvalue(str[i]);
high=(((str[i]-value)&0xF)<<4);
value=sthvalue(str[i+1]);
low=((str[i+1]-value)&0xF);
sum=high|low;
j=i/2;
data[j]=sum;
i=i+1;
}
return len;
}
#define CID_PATH "/sys/block/mmcblk0/device/cid"
#define CID_BUFFER_SIZE 33
/**
* 读取eMMC CID信息
* @param cid_buf: 输出缓冲区
* @param buf_size: 缓冲区大小
* @return: 成功返回0,失败返回-1
*/
int read_mmc_cid(char *cid_buf, size_t buf_size) {
FILE *fp = NULL;
char *line = NULL;
size_t len = 0;
ssize_t read;
int ret = -1;
if (cid_buf == NULL || buf_size < CID_BUFFER_SIZE) {
return -1;
}
// 打开CID文件
fp = fopen(CID_PATH, "r");
if (fp == NULL) {
return -1;
}
// 读取一行内容
read = getline(&line, &len, fp);
if (read != -1) {
// 移除可能的换行符
if (read > 0 && line[read-1] == '\n') {
line[read-1] = '\0';
}
// 确保不超过缓冲区大小
strncpy(cid_buf, line, buf_size - 1);
cid_buf[buf_size - 1] = '\0';
ret = 0;
}
// 清理资源
if (line) {
free(line);
}
fclose(fp);
return ret;
}
int get_emmcid_for_generate_mac(UCHAR* btmac)
{
INT32 ret = 0;
UCHAR emmcid[CID_BUFFER_SIZE];
ret = read_mmc_cid(emmcid,CID_BUFFER_SIZE);
if(ret < 0){
LOG_WAN("read emmc id failed\n");
return ret;
}
LOG_WAN("read emmc id: %s\n",emmcid);
strtohex(emmcid,btmac);
return ret;
}
//add end
UCHAR GetRandomByte(UCHAR exclude)
{
struct timeval tv;
UCHAR ret = exclude;
while(ret == exclude) {
gettimeofday(&tv, NULL);
srand(tv.tv_usec);
ret = ((rand()>>8) & 0xFF);
}
return ret;
}
static VOID GetRandomValue(UCHAR string[6])
{
INT32 iRandom = 0;
INT32 ret = 0;
UCHAR emmccid[32];
ret = get_emmcid_for_generate_mac(emmccid);
if(ret == 0){
LOG_WAN("Use emmc id gen bt mac\n");
string[0] = ((emmccid[0] ^ emmccid[1] ^ emmccid[2] ^ emmccid[3]) & 0xFE) | (0x02);
string[1] = (emmccid[8] ^ emmccid[9] ^ emmccid[10] ^ emmccid[11]);//(emmccid[4] ^ emmccid[5] ^ emmccid[6] ^ emmccid[7]);
string[3] = ((emmccid[13] ^ emmccid[12] ^ emmccid[5] ^ emmccid[6]) & (0xef));//must not be 0x9e
string[4] = emmccid[14];
string[5] = emmccid[15] + 1;
} else {
LOG_WAN("Enable random generation\n");
/* Initialize random seed */
srand(time(NULL));
iRandom = rand();
string[0] = (((iRandom>>24|iRandom>>16) & (0xFE)) | (0x02)); /* Must use private bit(1) and no BCMC bit(0) */
string[1] = GetRandomByte(0x00);
/* Generate random LAP, exclude 0x9E8B00 ~ 0x9E8BFF*/
string[3] = GetRandomByte(0x9E);
string[4] = GetRandomByte(0x8B);
string[5] = GetRandomByte(0x00);
}
LOG_WAN("Generate bd_addr: %02x-%02x-%02x-%02x-%02x-%02x\n",
string[0], string[1], string[2], string[3], string[4], string[5]);
return;
}
其中mac地址的生成方式可以自定义
string[0] = ((emmccid[0] ^ emmccid[1] ^ emmccid[2] ^ emmccid[3]) & 0xFE) | (0x02);
string[1] = (emmccid[8] ^ emmccid[9] ^ emmccid[10] ^ emmccid[11]);//(emmccid[4] ^ emmccid[5] ^ emmccid[6] ^ emmccid[7]);
string[3] = ((emmccid[13] ^ emmccid[12] ^ emmccid[5] ^ emmccid[6]) & (0xef));//must not be 0x9e
string[4] = emmccid[14];
string[5] = emmccid[15] + 1;
目前测试使用正常。 需要增加头文件
#include <string.h>
#include <unistd.h>
#include <errno.h>
=======================================
作者:hclydao
版权没有,但是转载请保留此段声明
=======================================