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);
90 APP_DBG("fputs ret: %d", ret);
93 result = fgets(cmp, READ_BD_FILE_MAX, fd);
95 APP_DBG("PSR : [%s]\r\n", cmp);
97 if((NULL != result) && (0 == strncmp(cmp, "&0001", 5))){
98 APP_DBG("Find BD address set script\r\n");
103 APP_DBG("EOF reaches\r\n");
109 ret = fputs(cmp,new);
110 APP_DBG("fputs ret: %d", ret);
115 void makeRandomBD(unsigned char* buf){
119 memcpy(buf, BD_PREFIX, 5);
126 ran = rand_r(&seed)%16;
133 APP_DEBUG("Random number is\r\n");
134 for(i=0;i<BD_ADDR_LEN;i++){
135 APP_DEBUG("%c",buf[i]);
142 int readBDaddrTI(void){
143 int i, cnt_lap=0, cnt_uap=0, cnt_nap=0;
144 int dev_id, fd, filedesc;
147 char nap[4], uap[2], lap[6];
149 dev_id=hci_get_route(NULL);
151 APP_DBG("Bluetooth device not available!!!\r\n");
154 fd=hci_open_dev(dev_id);
156 APP_DBG("HCI open fail!!!\r\n");
160 if(0>hci_read_bd_addr(fd, &bdaddr, 1000)){
161 APP_DBG("Read BD ADDR failed!!!\r\n");
166 ba2str(&bdaddr, address);
168 if(':' == address[i])
172 nap[cnt_nap++] = address[i];
174 uap[cnt_uap++] = address[i];
176 lap[cnt_lap++] = address[i];
179 APP_DBG("BT address [%s], nap[%c%c%c%c], uap[%c%c], lap[%c%c%c%c%c%c]\r\n",\
180 address, nap[0], nap[1], nap[2], nap[3]\
182 ,lap[0], lap[1],lap[2],lap[3],lap[4],lap[5]);
185 filedesc=open(BD_ADDR_FILE, O_RDWR | O_CREAT | O_TRUNC | O_SYNC, 0644);
187 APP_DBG("File creation fail!!!\r\n");
190 ret = write(filedesc, nap, 4);
191 ret = write(filedesc, "\n", 1);
192 ret = write(filedesc, uap, 2);
193 ret = write(filedesc, "\n", 1);
194 ret = write(filedesc, lap, 6);
195 ret = write(filedesc, "\n", 1);
202 int make_bt_address(gboolean overwrite_bt_address)
204 #if defined(BT_CHIP_CSR) || defined(BT_CHIP_BROADCOM)
207 unsigned char txt[BD_ADDR_LEN];
208 unsigned char nap[4+1], uap[2+1], lap[6+1];
209 #if defined(BT_CHIP_CSR)
211 char pskey[PSKEY_LEN+3];
215 fd=open(BD_ADDR_FILE, O_RDONLY | O_SYNC);
217 if(fd<0 || overwrite_bt_address==TRUE){
219 APP_DEBUG("File not exist\n");
223 if(overwrite_bt_address)
225 APP_DEBUG("Overwrite BT address\n");
228 fd=open(BD_ADDR_FILE, O_RDWR | O_CREAT | O_TRUNC | O_SYNC, 0644);
232 APP_DEBUG("Can't open address file\n");
238 ret = write(fd, txt, BD_ADDR_LEN);
239 lseek(fd, 0, SEEK_SET);
241 APP_DEBUG("%s is already existed\n",BD_ADDR_FILE);
242 success_make_bt_address_from_imei=0;
245 ret = read(fd, nap, 5);
246 ret = read(fd, uap, 3);
247 ret = read(fd, lap, 7);
250 #if defined(BT_CHIP_CSR)
253 APP_DEBUG("%c",nap[i]);
258 APP_DEBUG("%c",uap[i]);
263 APP_DEBUG("%c",lap[i]);
266 sprintf(pskey, "&0001 = 0012 %c%c%c%c %c%c%c%c %c%c%c%c\r\n",\
267 lap[0], lap[1], lap[2], lap[3], lap[4], lap[5],\
269 nap[0], nap[1], nap[2], nap[3]);
271 APP_DEBUG("BD PSKEY [");
272 for(i=0;i<PSKEY_LEN;i++)
273 APP_DEBUG("%c", pskey[i]);
276 ret = addremoveBD(PSR_FILE, pskey);
279 #elif defined(BT_CHIP_TI)
283 fd=open(BD_ADDR_FILE, O_RDONLY, 0644);
285 APP_DBG("File not exists\r\n");
288 APP_DBG("File exists\r\n");
295 printf("error BT CHIP not defined!!!\n");
300 gboolean exit_cb(gpointer data)
303 APP_DEBUG("Time out!!!\n");
304 g_main_loop_quit(loop);
310 loop=g_main_loop_new(NULL,FALSE);
311 APP_DEBUG("Bluetooth Address Setting\n");
312 make_bt_address(FALSE);
314 /* When we get proper BT address by IMEI or
315 * randome BT address due to default IMEI
316 * We don't need to wait for telephony activation, timeout */
317 if(success_make_bt_address_from_imei==0 || is_default_imei==TRUE)
320 #ifdef IMEI_BASED_RAND_FEATURE
321 g_timeout_add_seconds(10,exit_cb,NULL);
322 g_main_loop_run(loop);