summaryrefslogtreecommitdiff
path: root/set-address/setbd.c
blob: 092da16057a05011a0303187fd9106765afa73f7 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
 * setbd.c
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved
 *
 * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
 *           GirishAshok Joshi <girish.joshi@samsung.com>
 *           DoHyun Pyun <dh79.pyun@samsung.com>
 *
 * 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 <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <vconf.h>
#include <glib.h>

#ifdef __TI_PATCH__
#define BT_CHIP_TI
#else
#ifdef __BROADCOM_PATCH__
#define BT_CHIP_BROADCOM
#else
#define BT_CHIP_CSR
#endif
#endif

#ifdef DEBUG_EN
#define APP_DBG(format, args...)	printf("%s(), line[%d]: " format, __FUNCTION__, __LINE__, ##args)
#define APP_DEBUG(format, args...)	printf(format, ##args)
#else
#define APP_DBG(format, args...)
#define APP_DEBUG(format, args...)
#endif

#define	BD_ADDR_FILE	"/opt/etc/.bd_addr"
#define	PSKEY_TEMP_FILE	"/opt/etc/.bluetooth.psr"
#define PSR_FILE	"/opt/etc/bluetooth/bluetooth.psr"

#define	BD_ADDR_LEN	14
#define BD_PREFIX	"0002\n"

#define PSKEY_LEN	27
#define PSKEY_BDADDR_PREFIX	"&0001 = 0012 "

#define READ_BD_FILE_MAX	300

static int success_make_bt_address_from_imei=-1;
static GMainLoop * loop;
const char *DEFAULT_IMEI="004999010640000";
static gboolean is_default_imei=FALSE;

#if defined(BT_CHIP_CSR) || defined(BT_CHIP_BROADCOM)
int addremoveBD(char* path, char* pskey){
	FILE *fd, *new;
	int ret;
	char cmp[READ_BD_FILE_MAX];
	char *result;

	fd = fopen(path, "r");
	if(NULL == fd){
		APP_DBG("Error open psr file\r\n");
		return -21;
	}

	new = fopen(PSKEY_TEMP_FILE, "w");
	if(NULL == new){
		APP_DBG("Error creat temp file\r\n");
		fclose(fd);
		return -22;
	}

	ret = fputs(pskey,new);

	while(1){
		result = fgets(cmp, READ_BD_FILE_MAX, fd);

		APP_DBG("PSR : [%s]\r\n", cmp);

		if((NULL != result) && (0 == strncmp(cmp, "&0001", 5))){
			APP_DBG("Find BD address set script\r\n");
			continue;
		}

		if(NULL == result){
			APP_DBG("EOF reaches\r\n");
			fclose(fd);
			fclose(new);
			return 1;
		}

		ret = fputs(cmp,new);
	}

	return 0;
}
void makeRandomBD(unsigned char* buf){
	int ran;
	int i;
	unsigned int seed;
	memcpy(buf, BD_PREFIX, 5);
	seed = time(NULL);
	for(i=5;i<14;i++){
		if(7==i){
			buf[i]='\n';
			continue;
		}
		ran = rand_r(&seed)%16;
		if(10>ran)
			ran+=0x30;
		else
			ran+=0x57;
		buf[i]=ran;
	}
	APP_DEBUG("Random number is\r\n");
	for(i=0;i<BD_ADDR_LEN;i++){
		APP_DEBUG("%c",buf[i]);
	}
	APP_DEBUG("\r\n");
}
#endif

#ifdef BT_CHIP_TI
int readBDaddrTI(void){
	int i, cnt_lap=0, cnt_uap=0, cnt_nap=0;
	int dev_id, fd, filedesc;
	BD_ADDR_T bdaddr;
	char address[18];
	char nap[4], uap[2], lap[6];
       int ret = 0;
	dev_id=hci_get_route(NULL);
	if(dev_id<0){
		APP_DBG("Bluetooth device not available!!!\r\n");
		return -1;
	}
	fd=hci_open_dev(dev_id);
	if(fd<0){
		APP_DBG("HCI open fail!!!\r\n");
		return -2;
	}

	if(0>hci_read_bd_addr(fd, &bdaddr, 1000)){
		APP_DBG("Read BD ADDR failed!!!\r\n");
		return -3;
	}
	hci_close_dev(fd);

	ba2str(&bdaddr, address);
	for(i=0;i<17;i++){
		if(':' == address[i])
			continue;

		if(5>i)
			nap[cnt_nap++] = address[i];
		else if(8>i)
			uap[cnt_uap++] = address[i];
		else
			lap[cnt_lap++] = address[i];
	}

	APP_DBG("BT address [%s], nap[%c%c%c%c], uap[%c%c], lap[%c%c%c%c%c%c]\r\n",\
	 address, nap[0], nap[1], nap[2], nap[3]\
	, uap[0],uap[1]\
	,lap[0], lap[1],lap[2],lap[3],lap[4],lap[5]);


	filedesc=open(BD_ADDR_FILE, O_RDWR | O_CREAT | O_TRUNC | O_SYNC, 0644);
	if(0>filedesc){
		APP_DBG("File creation fail!!!\r\n");
		return -4;
	}
	ret = write(filedesc, nap, 4);
	ret = write(filedesc, "\n", 1);
	ret = write(filedesc, uap, 2);
	ret = write(filedesc, "\n", 1);
	ret = write(filedesc, lap, 6);
	ret = write(filedesc, "\n", 1);
	close(filedesc);

	return 0;
}
#endif
int make_bt_address_from_tapi_imei(unsigned char * bt_address)
{
	char * temp=NULL;
	int tapi_state=0;
	int ret=-1;
	int i=0;

	if(bt_address==NULL)
		return -EBADR;

	ret=vconf_get_int(VCONFKEY_TELEPHONY_TAPI_STATE,&tapi_state);
	if(tapi_state==VCONFKEY_TELEPHONY_TAPI_STATE_READY && ret==0){
		temp=vconf_get_str(VCONFKEY_TELEPHONY_IMEI);
		APP_DEBUG("TAPI_IMEI: %s\n",temp);

#ifdef IMEI_BASED_RAND_FEATURE
		if(strcmp(temp,DEFAULT_IMEI)==0){
			APP_DEBUG("TAPI_IMEI is defulat IMEI\n");
			is_default_imei=TRUE;
			return -ENODATA;
		}
#else
		APP_DEBUG("Temporarily we skip reading TAPI_IMEI\n");
		APP_DEBUG("  due to TAPI IMEI API is deprecated\n");
		is_default_imei=TRUE;
		return -ENODATA;
#endif

		if(strcmp(temp,"")==0)
			return -ENODATA;

		if(strlen(temp)<14)
			return -ENODATA;

		memcpy(bt_address, BD_PREFIX, 5);

		for(i=5 ;i<14;i++){
			if(i==7){
				bt_address[i]='\n';
				continue;
			}

			bt_address[i]=temp[i];
		}

	}else{
		APP_DEBUG("TAPI_IMEI Reading Error\n");
		return -ENODATA;
	}

	APP_DEBUG("Bluetooth Address\n");
	for(i=0;i<BD_ADDR_LEN;i++)
		APP_DEBUG("%c",bt_address[i]);

	APP_DEBUG("\n");

	return 0;

}

int make_bt_address(gboolean overwrite_bt_address)
{
#if defined(BT_CHIP_CSR) || defined(BT_CHIP_BROADCOM)

	int fd;
	int i;
	unsigned char txt[BD_ADDR_LEN];
	unsigned char nap[4+1], uap[2+1], lap[6+1];
	char pskey[PSKEY_LEN+3];
	int ret;

	fd=open(BD_ADDR_FILE, O_RDONLY | O_SYNC);

	if(fd<0 || overwrite_bt_address==TRUE){
		if(fd<0)
			APP_DEBUG("File not exist\n");
		else
			close(fd);

		if(overwrite_bt_address)
		{
			APP_DEBUG("Overwrite BT address because TAPI write correct IMEI.\n");
		}

		fd=open(BD_ADDR_FILE, O_RDWR | O_CREAT | O_TRUNC | O_SYNC, 0644);

		if (fd <0)
		{
			APP_DEBUG("Can't open address file\n");
			return 0;
		}
		success_make_bt_address_from_imei=make_bt_address_from_tapi_imei(txt);

		if(success_make_bt_address_from_imei<0)
			makeRandomBD(txt);

		ret = write(fd, txt, BD_ADDR_LEN);
		lseek(fd, 0, SEEK_SET);
	}else{
		APP_DEBUG("%s is already existed\n",BD_ADDR_FILE);
		success_make_bt_address_from_imei=0;
	}

	ret = read(fd, nap, 5);
	ret = read(fd, uap, 3);
	ret = read(fd, lap, 7);
	close(fd);

#if defined(BT_CHIP_CSR)
	APP_DEBUG("nap[");
	for(i=0;i<4;i++)
		APP_DEBUG("%c",nap[i]);
	APP_DEBUG("]\r\n");

	APP_DEBUG("uap[");
	for(i=0;i<2;i++)
		APP_DEBUG("%c",uap[i]);
	APP_DEBUG("]\r\n");

	APP_DEBUG("lap[");
	for(i=0;i<6;i++)
		APP_DEBUG("%c",lap[i]);
	APP_DEBUG("]\r\n");

	sprintf(pskey, "&0001 = 0012 %c%c%c%c %c%c%c%c %c%c%c%c\r\n",\
	 lap[0], lap[1], lap[2], lap[3], lap[4], lap[5],\
	 uap[0], uap[1],\
	 nap[0], nap[1], nap[2], nap[3]);

	APP_DEBUG("BD PSKEY [");
	for(i=0;i<PSKEY_LEN;i++)
		APP_DEBUG("%c", pskey[i]);
	APP_DEBUG("]\r\n");

	ret = addremoveBD(PSR_FILE, pskey);
#endif
	return ret;
#elif defined(BT_CHIP_TI)
	int fd;
	int ret;

	fd=open(BD_ADDR_FILE, O_RDONLY, 0644);
	if(0>fd){
		APP_DBG("File not exists\r\n");
		ret=readBDaddrTI();
	}else{
		APP_DBG("File exists\r\n");
		close(fd);
		ret=0;
	}

	return ret;
#else
	printf("error BT CHIP not defined!!!\n");
	return 0;
#endif
}

void vconf_cb(keynode_t *key, void * data)
{
	char * key_string=NULL;

	switch(vconf_keynode_get_type(key))
	{
		case VCONF_TYPE_STRING:
			key_string=vconf_keynode_get_str(key);
			if(strcmp(key_string,"")!=0)
			{
				APP_DEBUG("Vconf Call back trial\n");
				/* This case means TAPI writes IMEI correctly */
				/* Because we write BT address which comes from IMEI again  */
				make_bt_address(TRUE);
				g_main_loop_quit(loop);
			}

			break;


		default:
			break;
	}
	return;
}

gboolean exit_cb(gpointer data)
{

	APP_DEBUG("Time out!!!\n");
	g_main_loop_quit(loop);

	return FALSE;
}
int main()
{
	loop=g_main_loop_new(NULL,FALSE);
	APP_DEBUG("Bluetooth Address Setting\n");
	make_bt_address(FALSE);

	/* When we get proper BT address by IMEI or
	 * randome BT address due to default IMEI
	 * We don't need to wait for telephony activation, timeout */
	if(success_make_bt_address_from_imei==0 || is_default_imei==TRUE)
		exit(0);

#ifdef IMEI_BASED_RAND_FEATURE
	vconf_notify_key_changed(VCONFKEY_TELEPHONY_IMEI,vconf_cb,NULL);

	g_timeout_add_seconds(10,exit_cb,NULL);
	g_main_loop_run(loop);

	vconf_ignore_key_changed(VCONFKEY_TELEPHONY_IMEI,vconf_cb);
#endif

	return 0;
}