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){
72 char cmp[READ_BD_FILE_MAX];
75 fd = fopen(path, "r");
77 APP_DBG("Error open psr file\r\n");
81 new = fopen(PSKEY_TEMP_FILE, "w");
83 APP_DBG("Error creat temp file\r\n");
88 ret = fputs(pskey,new);
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);
114 void makeRandomBD(unsigned char* buf){
118 memcpy(buf, BD_PREFIX, 5);
125 ran = rand_r(&seed)%16;
132 APP_DEBUG("Random number is\r\n");
133 for(i=0;i<BD_ADDR_LEN;i++){
134 APP_DEBUG("%c",buf[i]);
141 int readBDaddrTI(void){
142 int i, cnt_lap=0, cnt_uap=0, cnt_nap=0;
143 int dev_id, fd, filedesc;
146 char nap[4], uap[2], lap[6];
148 dev_id=hci_get_route(NULL);
150 APP_DBG("Bluetooth device not available!!!\r\n");
153 fd=hci_open_dev(dev_id);
155 APP_DBG("HCI open fail!!!\r\n");
159 if(0>hci_read_bd_addr(fd, &bdaddr, 1000)){
160 APP_DBG("Read BD ADDR failed!!!\r\n");
165 ba2str(&bdaddr, address);
167 if(':' == address[i])
171 nap[cnt_nap++] = address[i];
173 uap[cnt_uap++] = address[i];
175 lap[cnt_lap++] = address[i];
178 APP_DBG("BT address [%s], nap[%c%c%c%c], uap[%c%c], lap[%c%c%c%c%c%c]\r\n",\
179 address, nap[0], nap[1], nap[2], nap[3]\
181 ,lap[0], lap[1],lap[2],lap[3],lap[4],lap[5]);
184 filedesc=open(BD_ADDR_FILE, O_RDWR | O_CREAT | O_TRUNC | O_SYNC, 0644);
186 APP_DBG("File creation fail!!!\r\n");
189 ret = write(filedesc, nap, 4);
190 ret = write(filedesc, "\n", 1);
191 ret = write(filedesc, uap, 2);
192 ret = write(filedesc, "\n", 1);
193 ret = write(filedesc, lap, 6);
194 ret = write(filedesc, "\n", 1);
201 int make_bt_address(gboolean overwrite_bt_address)
203 #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 #if defined(BT_CHIP_CSR)
209 char pskey[PSKEY_LEN+3];
213 fd=open(BD_ADDR_FILE, O_RDONLY | O_SYNC);
215 if(fd<0 || overwrite_bt_address==TRUE){
217 APP_DEBUG("File not exist\n");
221 if(overwrite_bt_address)
223 APP_DEBUG("Overwrite BT address\n");
226 fd=open(BD_ADDR_FILE, O_RDWR | O_CREAT | O_TRUNC | O_SYNC, 0644);
230 APP_DEBUG("Can't open address file\n");
236 ret = write(fd, txt, BD_ADDR_LEN);
237 lseek(fd, 0, SEEK_SET);
239 APP_DEBUG("%s is already existed\n",BD_ADDR_FILE);
240 success_make_bt_address_from_imei=0;
243 ret = read(fd, nap, 5);
244 ret = read(fd, uap, 3);
245 ret = read(fd, lap, 7);
248 #if defined(BT_CHIP_CSR)
251 APP_DEBUG("%c",nap[i]);
256 APP_DEBUG("%c",uap[i]);
261 APP_DEBUG("%c",lap[i]);
264 sprintf(pskey, "&0001 = 0012 %c%c%c%c %c%c%c%c %c%c%c%c\r\n",\
265 lap[0], lap[1], lap[2], lap[3], lap[4], lap[5],\
267 nap[0], nap[1], nap[2], nap[3]);
269 APP_DEBUG("BD PSKEY [");
270 for(i=0;i<PSKEY_LEN;i++)
271 APP_DEBUG("%c", pskey[i]);
274 ret = addremoveBD(PSR_FILE, pskey);
277 #elif defined(BT_CHIP_TI)
281 fd=open(BD_ADDR_FILE, O_RDONLY, 0644);
283 APP_DBG("File not exists\r\n");
286 APP_DBG("File exists\r\n");
293 printf("error BT CHIP not defined!!!\n");
298 gboolean exit_cb(gpointer data)
301 APP_DEBUG("Time out!!!\n");
302 g_main_loop_quit(loop);
308 loop=g_main_loop_new(NULL,FALSE);
309 APP_DEBUG("Bluetooth Address Setting\n");
310 make_bt_address(FALSE);
312 /* When we get proper BT address by IMEI or
313 * randome BT address due to default IMEI
314 * We don't need to wait for telephony activation, timeout */
315 if(success_make_bt_address_from_imei==0 || is_default_imei==TRUE)
318 #ifdef IMEI_BASED_RAND_FEATURE
319 g_timeout_add_seconds(10,exit_cb,NULL);
320 g_main_loop_run(loop);