4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 * GirishAshok Joshi <girish.joshi@samsung.com>
8 * DoHyun Pyun <dh79.pyun@samsung.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
37 #ifdef __BROADCOM_PATCH__
38 #define BT_CHIP_BROADCOM
45 #define APP_DBG(format, args...) printf("%s(), line[%d]: " format, __FUNCTION__, __LINE__, ##args)
46 #define APP_DEBUG(format, args...) printf(format, ##args)
48 #define APP_DBG(format, args...)
49 #define APP_DEBUG(format, args...)
52 #define BD_ADDR_FILE "/opt/etc/.bd_addr"
53 #define PSKEY_TEMP_FILE "/opt/etc/.bluetooth.psr"
54 #define PSR_FILE "/opt/etc/bluetooth/bluetooth.psr"
56 #define BD_ADDR_LEN 14
57 #define BD_PREFIX "0002\n"
60 #define PSKEY_BDADDR_PREFIX "&0001 = 0012 "
62 #define READ_BD_FILE_MAX 300
64 static int success_make_bt_address_from_imei=-1;
65 static GMainLoop * loop;
66 const char *DEFAULT_IMEI="004999010640000";
67 static gboolean is_default_imei=FALSE;
69 #if defined(BT_CHIP_CSR) || defined(BT_CHIP_BROADCOM)
70 int addremoveBD(char* path, char* pskey){
73 char cmp[READ_BD_FILE_MAX];
76 fd = fopen(path, "r");
78 APP_DBG("Error open psr file\r\n");
82 new = fopen(PSKEY_TEMP_FILE, "w");
84 APP_DBG("Error creat temp file\r\n");
89 ret = fputs(pskey,new);
92 result = fgets(cmp, READ_BD_FILE_MAX, fd);
94 APP_DBG("PSR : [%s]\r\n", cmp);
96 if((NULL != result) && (0 == strncmp(cmp, "&0001", 5))){
97 APP_DBG("Find BD address set script\r\n");
102 APP_DBG("EOF reaches\r\n");
108 ret = fputs(cmp,new);
113 void makeRandomBD(unsigned char* buf){
117 memcpy(buf, BD_PREFIX, 5);
124 ran = rand_r(&seed)%16;
131 APP_DEBUG("Random number is\r\n");
132 for(i=0;i<BD_ADDR_LEN;i++){
133 APP_DEBUG("%c",buf[i]);
140 int readBDaddrTI(void){
141 int i, cnt_lap=0, cnt_uap=0, cnt_nap=0;
142 int dev_id, fd, filedesc;
145 char nap[4], uap[2], lap[6];
147 dev_id=hci_get_route(NULL);
149 APP_DBG("Bluetooth device not available!!!\r\n");
152 fd=hci_open_dev(dev_id);
154 APP_DBG("HCI open fail!!!\r\n");
158 if(0>hci_read_bd_addr(fd, &bdaddr, 1000)){
159 APP_DBG("Read BD ADDR failed!!!\r\n");
164 ba2str(&bdaddr, address);
166 if(':' == address[i])
170 nap[cnt_nap++] = address[i];
172 uap[cnt_uap++] = address[i];
174 lap[cnt_lap++] = address[i];
177 APP_DBG("BT address [%s], nap[%c%c%c%c], uap[%c%c], lap[%c%c%c%c%c%c]\r\n",\
178 address, nap[0], nap[1], nap[2], nap[3]\
180 ,lap[0], lap[1],lap[2],lap[3],lap[4],lap[5]);
183 filedesc=open(BD_ADDR_FILE, O_RDWR | O_CREAT | O_TRUNC | O_SYNC, 0644);
185 APP_DBG("File creation fail!!!\r\n");
188 ret = write(filedesc, nap, 4);
189 ret = write(filedesc, "\n", 1);
190 ret = write(filedesc, uap, 2);
191 ret = write(filedesc, "\n", 1);
192 ret = write(filedesc, lap, 6);
193 ret = write(filedesc, "\n", 1);
200 int make_bt_address(gboolean overwrite_bt_address)
202 #if defined(BT_CHIP_CSR) || defined(BT_CHIP_BROADCOM)
206 unsigned char txt[BD_ADDR_LEN];
207 unsigned char nap[4+1], uap[2+1], lap[6+1];
208 char pskey[PSKEY_LEN+3];
211 fd=open(BD_ADDR_FILE, O_RDONLY | O_SYNC);
213 if(fd<0 || overwrite_bt_address==TRUE){
215 APP_DEBUG("File not exist\n");
219 if(overwrite_bt_address)
221 APP_DEBUG("Overwrite BT address\n");
224 fd=open(BD_ADDR_FILE, O_RDWR | O_CREAT | O_TRUNC | O_SYNC, 0644);
228 APP_DEBUG("Can't open address file\n");
234 ret = write(fd, txt, BD_ADDR_LEN);
235 lseek(fd, 0, SEEK_SET);
237 APP_DEBUG("%s is already existed\n",BD_ADDR_FILE);
238 success_make_bt_address_from_imei=0;
241 ret = read(fd, nap, 5);
242 ret = read(fd, uap, 3);
243 ret = read(fd, lap, 7);
246 #if defined(BT_CHIP_CSR)
249 APP_DEBUG("%c",nap[i]);
254 APP_DEBUG("%c",uap[i]);
259 APP_DEBUG("%c",lap[i]);
262 sprintf(pskey, "&0001 = 0012 %c%c%c%c %c%c%c%c %c%c%c%c\r\n",\
263 lap[0], lap[1], lap[2], lap[3], lap[4], lap[5],\
265 nap[0], nap[1], nap[2], nap[3]);
267 APP_DEBUG("BD PSKEY [");
268 for(i=0;i<PSKEY_LEN;i++)
269 APP_DEBUG("%c", pskey[i]);
272 ret = addremoveBD(PSR_FILE, pskey);
275 #elif defined(BT_CHIP_TI)
279 fd=open(BD_ADDR_FILE, O_RDONLY, 0644);
281 APP_DBG("File not exists\r\n");
284 APP_DBG("File exists\r\n");
291 printf("error BT CHIP not defined!!!\n");
296 gboolean exit_cb(gpointer data)
299 APP_DEBUG("Time out!!!\n");
300 g_main_loop_quit(loop);
306 loop=g_main_loop_new(NULL,FALSE);
307 APP_DEBUG("Bluetooth Address Setting\n");
308 make_bt_address(FALSE);
310 /* When we get proper BT address by IMEI or
311 * randome BT address due to default IMEI
312 * We don't need to wait for telephony activation, timeout */
313 if(success_make_bt_address_from_imei==0 || is_default_imei==TRUE)
316 #ifdef IMEI_BASED_RAND_FEATURE
317 g_timeout_add_seconds(10,exit_cb,NULL);
318 g_main_loop_run(loop);