int main(int argc, char *argv[])
{
	int dev_id = -1;
	unsigned char buf[HCI_MAX_EVENT_SIZE];
	struct hci_filter flt;
	int len = 0;
	int dd = -1;
	uint16_t ocf;
	uint8_t ogf;
	int i = 0;
	hci_event_hdr *hdr;
	char* ptr;

	LOGE("[GABT] %s :: before : getuid=%d",__FUNCTION__, getuid());
	android_set_aid_and_cap_k();
	LOGE("[GABT] %s :: after : getuid=%d",__FUNCTION__, getuid());
	if (dev_id < 0)
		dev_id = hci_get_route(NULL);

	errno = 0;
	
	dd = hci_open_dev(dev_id);
	if (dd < 0) {
		LOGE("[GABT] %s :: Hci Device open failed :: dev_id=%d, dd=%d",__FUNCTION__, dev_id, dd);
		perror("Hci Device open failed");
		exit(EXIT_FAILURE);
	}

	/* Setup filter */
	hci_filter_clear(&flt);
	hci_filter_set_ptype(HCI_EVENT_PKT, &flt);
	hci_filter_all_events(&flt);
	if (setsockopt(dd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
		LOGE("[GABT] %s :: HCI filter setup failed ",__FUNCTION__);
		perror("HCI filter setup failed");
		exit(EXIT_FAILURE);
	}
#if 0
	/* sleep mode disable */
	ogf = 0x3f;
	ocf = 0x0027;
	
	memset(buf, 0, sizeof(buf));
	buf[0] = 0x00;
	buf[1] = 0x00;
	buf[2] = 0x00;
	buf[3] = 0x00;
	buf[4] = 0x00;
	buf[5] = 0x00;
	buf[6] = 0x00;
	buf[7] = 0x00;
	buf[8] = 0x00;
	buf[9] = 0x00;
	buf[10] = 0x00;
	buf[11] = 0x00;
	len = 12;

	if (hci_send_cmd(dd, ogf, ocf, len, buf) < 0) {
		LOGE("[GABT] %s :: Send failed 1",__FUNCTION__);
		perror("Send failed");
		exit(EXIT_FAILURE);
	}

	len = read(dd, buf, sizeof(buf));
	if (len < 0) {
		LOGE("[GABT] %s :: Read failed 1",__FUNCTION__);
		perror("Read failed");
		exit(EXIT_FAILURE);
	}
	LOGE("[GABT] sleep mode disable -");


	/* Set Event Filter */
	ogf = 0x03;
	ocf = 0x0005;
	
	memset(buf, 0, sizeof(buf));
	buf[0] = 0x02; //Filter_Type          , 0x02 : Connection Setup.
	buf[1] = 0x00; //Filter_Condition_Type, 0x00 : Allow Connections from all devices.
	buf[2] = 0x02; //Condition
	len = 3;

	if (hci_send_cmd(dd, ogf, ocf, len, buf) < 0) {
		LOGE("[GABT] %s :: Send failed 2 ",__FUNCTION__);
		perror("Send failed");
		exit(EXIT_FAILURE);
	}

	len = read(dd, buf, sizeof(buf));
	if (len < 0) {
		LOGE("[GABT] %s :: Read failed 2 ",__FUNCTION__);
		perror("Read failed");
		exit(EXIT_FAILURE);
	}
	LOGE("[GABT] Set Event Filter -");

	/* Write Scan Enable */
	ogf = 0x03;
	ocf = 0x001a;	
	memset(buf, 0, sizeof(buf));
	buf[0] = 0x03; //Scan_Enable, 0x03 : Inquiry Scan enabled.
	               //                    Page Scan enabled.
	len = 1;
	
	if (hci_send_cmd(dd, ogf, ocf, len, buf) < 0) {
		LOGE("[GABT] %s :: Send failed 3 ",__FUNCTION__);
		perror("Send failed");
		exit(EXIT_FAILURE);
	}

	len = read(dd, buf, sizeof(buf));
	if (len < 0) {
		LOGE("[GABT] %s :: Read failed 3 ",__FUNCTION__);
		perror("Read failed");
		exit(EXIT_FAILURE);
	}
	LOGE("[GABT] Write Scan Enable -");
#endif

	/* Enable Device Under Test Mode */
	ogf = 0x06;
	ocf = 0x0003;	
	memset(buf, 0, sizeof(buf));
	buf[0] = 0x00;
	len = 0;

	LOGE("< HCI Command: ogf 0x%02x, ocf 0x%04x, plen %d\n", ogf, ocf, len);
	if (hci_send_cmd(dd, ogf, ocf, len, buf) < 0) {
		LOGE("[GABT] %s :: Send failed 4 ",__FUNCTION__);
		perror("Send failed");
		exit(EXIT_FAILURE);
	}

	len = read(dd, buf, sizeof(buf));
	if (len < 0) {
		LOGE("[GABT] %s :: Read failed 4 ",__FUNCTION__);
		perror("Read failed");
		exit(EXIT_FAILURE);
	}	
      
      	hdr = (void *)(buf + 1);
	ptr = buf + (1 + HCI_EVENT_HDR_SIZE);
	len -= (1 + HCI_EVENT_HDR_SIZE);

	LOGE("> HCI Event: 0x%02x plen %d\n", hdr->evt, hdr->plen);
	
	hex_dump("  ", 20, ptr, len); 
	LOGE("[GABT] Enable Device Under Test Mode -");

	////////////////////////////////////////////////////////////////////
	/* Set Event Filter */
	ogf = 0x03;
	ocf = 0x0005;
	
	memset(buf, 0, sizeof(buf));
	buf[0] = 0x02; //Filter_Type          , 0x02 : Connection Setup.
	buf[1] = 0x00; //Filter_Condition_Type, 0x00 : Allow Connections from all devices.
	buf[2] = 0x02; //Condition
	len = 3;

	if (hci_send_cmd(dd, ogf, ocf, len, buf) < 0) {
		perror("Send failed");
		exit(EXIT_FAILURE);
	}

	len = read(dd, buf, sizeof(buf));
	if (len < 0) {
		perror("Read failed");
		exit(EXIT_FAILURE);
	}

	/* Write Scan Enable */
	ogf = 0x03;
	ocf = 0x001a;	
	memset(buf, 0, sizeof(buf));
	buf[0] = 0x03; //Scan_Enable, 0x03 : Inquiry Scan enabled.
	               //                    Page Scan enabled.
	len = 1;
	
	if (hci_send_cmd(dd, ogf, ocf, len, buf) < 0) {
		perror("Send failed");
		exit(EXIT_FAILURE);
	}

	len = read(dd, buf, sizeof(buf));
	if (len < 0) {
		perror("Read failed");
		exit(EXIT_FAILURE);
	}

	/* Write Authentication Enable*/
	ogf = 0x03;
	ocf = 0x0020;	
	memset(buf, 0, sizeof(buf));
	buf[0] = 0x00; //Authentication_Enable, 0x00: Authentication not required. Default.
	len = 1;
	
	if (hci_send_cmd(dd, ogf, ocf, len, buf) < 0) {
		perror("Send failed");
		exit(EXIT_FAILURE);
	}

	len = read(dd, buf, sizeof(buf));
	if (len < 0) {
		perror("Read failed");
		exit(EXIT_FAILURE);
	}
	
	/* Write Encrytion Mode*/
	ogf = 0x03;
	ocf = 0x0022;	
	memset(buf, 0, sizeof(buf));
	buf[0] = 0x00; //Encryption_Mode, 0x00 : Encryption not required.
	len = 1;
	
	if (hci_send_cmd(dd, ogf, ocf, len, buf) < 0) {
		perror("Send failed");
		exit(EXIT_FAILURE);
	}

	len = read(dd, buf, sizeof(buf));
	if (len < 0) {
		perror("Read failed");
		exit(EXIT_FAILURE);
	}
	/////////////////////////////////////////////////////////////////////////
	
	hci_close_dev(dd);
	
	LOGE("[GABT] %s :: EXIT ",__FUNCTION__);
	return 0;
}
int main(int argc, char *argv[])
{
	int dev_id = -1;

	ALOGI("[GABT] %s :: before : getuid=%d",__FUNCTION__, getuid());
	android_set_aid_and_cap_k();
	ALOGI("[GABT] %s :: after : getuid=%d",__FUNCTION__, getuid());

	unsigned char buf[HCI_MAX_EVENT_SIZE];
	struct hci_filter flt;
	int len = 0;
	int dd = -1;
	uint16_t ocf;
	uint8_t ogf;

	if (dev_id < 0)
		dev_id = hci_get_route(NULL);

	errno = 0;

	dd = hci_open_dev(dev_id);
	if (dd < 0) {
		ALOGE("[GABT] %s :: Hci Device open failed :: dev_id=%d, dd=%d",__FUNCTION__, dev_id, dd);
		perror("Hci Device open failed");
		exit(EXIT_FAILURE);
	}

	/* Setup filter */
	hci_filter_clear(&flt);
	hci_filter_set_ptype(HCI_EVENT_PKT, &flt);
	hci_filter_all_events(&flt);
	if (setsockopt(dd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
		ALOGE("[GABT] %s :: HCI filter setup failed ",__FUNCTION__);
		perror("HCI filter setup failed");
		exit(EXIT_FAILURE);
	}

	/* sleep mode disable */
	ogf = 0x03;
	ocf = 0x0001;

	memset(buf, 0, sizeof(buf));
	buf[0] = 0x3f;
	buf[1] = 0x27;
	buf[2] = 0x00;
	buf[3] = 0x00;
	buf[4] = 0x00;
	buf[5] = 0x00;
	buf[6] = 0x00;
	buf[7] = 0x00;
	buf[8] = 0x00;
	buf[9] = 0x00;
	buf[10] = 0x00;
	buf[11] = 0x00;
	buf[12] = 0x00;
	buf[13] = 0x00;

	len = 14;

	if (hci_send_cmd(dd, ogf, ocf, len, buf) < 0) {
		ALOGE("[GABT] %s :: Send failed 1",__FUNCTION__);
		perror("Send failed");
		exit(EXIT_FAILURE);
	}

	len = read(dd, buf, sizeof(buf));
	if (len < 0) {
		ALOGE("[GABT] %s :: Read failed 1",__FUNCTION__);
		perror("Read failed");
		exit(EXIT_FAILURE);
	}
	ALOGI("[GABT] sleep mode disable -");

	/* Set Event Filter */
	ogf = 0x03;
	ocf = 0x0005;

	memset(buf, 0, sizeof(buf));
	buf[0] = 0x02; //Filter_Type          , 0x02 : Connection Setup.
	buf[1] = 0x00; //Filter_Condition_Type, 0x00 : Allow Connections from all devices.
	buf[2] = 0x02; //Condition
	len = 3;

	if (hci_send_cmd(dd, ogf, ocf, len, buf) < 0) {
		ALOGE("[GABT] %s :: Send failed 2 ",__FUNCTION__);
		perror("Send failed");
		exit(EXIT_FAILURE);
	}

	len = read(dd, buf, sizeof(buf));
	if (len < 0) {
		ALOGE("[GABT] %s :: Read failed 2 ",__FUNCTION__);
		perror("Read failed");
		exit(EXIT_FAILURE);
	}
	ALOGI("[GABT] Set Event Filter -");

	/* Write Scan Enable */
	ogf = 0x03;
	ocf = 0x001a;	
	memset(buf, 0, sizeof(buf));
	buf[0] = 0x03; //Scan_Enable, 0x03 : Inquiry Scan enabled.
	               //                    Page Scan enabled.
	len = 1;

	if (hci_send_cmd(dd, ogf, ocf, len, buf) < 0) {
		ALOGE("[GABT] %s :: Send failed 3 ",__FUNCTION__);
		perror("Send failed");
		exit(EXIT_FAILURE);
	}

	len = read(dd, buf, sizeof(buf));
	if (len < 0) {
		ALOGE("[GABT] %s :: Read failed 3 ",__FUNCTION__);
		perror("Read failed");
		exit(EXIT_FAILURE);
	}
	ALOGI("[GABT] Write Scan Enable -");

	/* Enable Device Under Test Mode */
	ogf = 0x06;
	ocf = 0x0003;	
	memset(buf, 0, sizeof(buf));
	//buf[0] = 0x00;
	len = 0;//1;

	if (hci_send_cmd(dd, ogf, ocf, len, buf) < 0) {
		ALOGE("[GABT] %s :: Send failed 4 ",__FUNCTION__);
		perror("Send failed");
		exit(EXIT_FAILURE);
	}

	len = read(dd, buf, sizeof(buf));
	if (len < 0) {
		ALOGE("[GABT] %s :: Read failed 4 ",__FUNCTION__);
		perror("Read failed");
		exit(EXIT_FAILURE);
	}
	ALOGE("[GABT] Enable Device Under Test Mode -");

	hci_close_dev(dd);

	ALOGE("[GABT] %s :: EXIT ",__FUNCTION__);
	return 0;
}