Index: asp3_tinet_ecnl_arm/trunk/bnep_bridge/src/bt_main.c
===================================================================
--- asp3_tinet_ecnl_arm/trunk/bnep_bridge/src/panu_main.c	(revision 352)
+++ asp3_tinet_ecnl_arm/trunk/bnep_bridge/src/bt_main.c	(revision 374)
@@ -58,5 +58,6 @@
 #include "sdp.h"
 #include "pan.h"
-#include "panu_main.h"
+#include "bt_main.h"
+#include "usb_hbth.h"
 
 //#define PACKET_DUMP
@@ -66,5 +67,5 @@
 
 #define RFCOMM_SERVER_CHANNEL 1
-/*static*/ uint16_t  rfcomm_channel_id = FAILED_RFCOMM_ID_NOT_SET;
+static uint16_t  rfcomm_channel_id = FAILED_RFCOMM_ID_NOT_SET;
 
 static int record_id = -1;
@@ -90,5 +91,17 @@
 
 volatile int bt_bnep_mode = 0;
-bool_t bt_enabled = false;
+
+enum bt_state_t {
+	BT_STATE_DORMANT,
+	BT_STATE_POWER_ON,
+	BT_STATE_BNEP_QUERY,
+	BT_STATE_BNEP_WAIT,
+	BT_STATE_BNEP_QUERY_WAIT,
+	BT_STATE_CONNECTING,
+	BT_STATE_CONNECTED,
+};
+
+enum bt_state_t bt_state = BT_STATE_DORMANT;
+int bt_timer;
 
 // outgoing network packet
@@ -108,5 +121,5 @@
 		break;
 	case DE_SIZE_VAR_16:
-		len = READ_NET_16(element, 1);
+		len = big_endian_read_16(element, 1);
 		break;
 	default:
@@ -129,5 +142,5 @@
 
 	// accept first entry or if we foudn a NAP and only have a PANU yet
-	if ((bnep_remote_uuid == 0) || (sdp_bnep_remote_uuid == BNEP_UUID_NAP && bnep_remote_uuid == BNEP_UUID_PANU)) {
+	if ((bnep_remote_uuid == 0) || (sdp_bnep_remote_uuid == BLUETOOTH_SERVICE_CLASS_NAP && bnep_remote_uuid == BLUETOOTH_SERVICE_CLASS_PANU)) {
 		bnep_l2cap_psm = sdp_bnep_l2cap_psm;
 		bnep_remote_uuid = sdp_bnep_remote_uuid;
@@ -146,5 +159,5 @@
 
 	switch (event->type) {
-	case SDP_QUERY_ATTRIBUTE_VALUE:
+	case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
 		value_event = (sdp_query_attribute_value_event_t*)event;
 
@@ -165,5 +178,5 @@
 
 				switch (value_event->attribute_id) {
-				case SDP_ServiceClassIDList:
+				case BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST:
 					if (de_get_element_type(attribute_value) != DE_DES) break;
 					for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
@@ -172,7 +185,7 @@
 						uint32_t uuid = de_get_uuid32(element);
 						switch (uuid) {
-						case BNEP_UUID_PANU:
-						case BNEP_UUID_NAP:
-						case BNEP_UUID_GN:
+						case BLUETOOTH_SERVICE_CLASS_PANU:
+						case BLUETOOTH_SERVICE_CLASS_NAP:
+						case BLUETOOTH_SERVICE_CLASS_GN:
 							log_info("SDP Attribute 0x%04x: BNEP PAN protocol UUID: %04x", value_event->attribute_id, uuid);
 							sdp_bnep_remote_uuid = uuid;
@@ -192,5 +205,5 @@
 					free(str);
 					break;
-				case SDP_ProtocolDescriptorList: {
+				case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: {
 					log_info("SDP Attribute: 0x%04x", value_event->attribute_id);
 
@@ -210,10 +223,10 @@
 						uuid = de_get_uuid32(element);
 						switch (uuid) {
-						case SDP_L2CAPProtocol:
+						case BLUETOOTH_PROTOCOL_L2CAP:
 							if (!des_iterator_has_more(&prot_it)) continue;
 							des_iterator_next(&prot_it);
 							de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_bnep_l2cap_psm);
 							break;
-						case SDP_BNEPProtocol:
+						case BLUETOOTH_PROTOCOL_BNEP:
 							if (!des_iterator_has_more(&prot_it)) continue;
 							des_iterator_next(&prot_it);
@@ -238,5 +251,5 @@
 		break;
 
-	case SDP_QUERY_COMPLETE:
+	case SDP_EVENT_QUERY_COMPLETE:
 		handle_sdp_client_record_complete();
 		complete_event = (sdp_query_complete_event_t*)event;
@@ -246,5 +259,11 @@
 		}
 		else {
+			bnep_do_connect = false;
 			log_error("No BNEP service found");
+		}
+		if (!bnep_do_connect) {
+			// 20秒後 BNEP Query 開始
+			bt_state = BT_STATE_BNEP_QUERY_WAIT;
+			bt_timer = 20 * 1000;
 		}
 		break;
@@ -269,7 +288,13 @@
 			if (packet[2] == HCI_STATE_WORKING) {
 				hci_send_cmd(&hci_write_local_name, bt_localname);
-				/* Send a general query for BNEP Protocol ID */
-				log_info("Start SDP BNEP query.");
-				sdp_general_query_for_uuid(remote_addr, SDP_BNEPProtocol);
+				if (bt_bnep_mode == 0) {
+					// 即時 BNEP Query 開始
+					bt_state = BT_STATE_BNEP_QUERY_WAIT;
+					bt_timer = 0;
+				}
+				else {
+					bt_state = BT_STATE_BNEP_WAIT;
+					bt_timer = -1;
+				}
 			}
 			break;
@@ -284,6 +309,6 @@
 
 		case HCI_EVENT_COMMAND_COMPLETE:
-			if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) {
-				bt_flip_addr(event_addr, &packet[6]);
+			if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_bd_addr)) {
+				reverse_bd_addr(&packet[6], event_addr);
 				log_info("BD-ADDR: %s", bd_addr_to_str(event_addr));
 
@@ -292,6 +317,6 @@
 				break;
 			}
-			if (COMMAND_COMPLETE_EVENT(packet, hci_write_local_name)) {
-				hci_discoverable_control(1);
+			if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_local_name)) {
+				gap_discoverable_control(1);
 				break;
 			}
@@ -301,5 +326,5 @@
 			// deny link key request
 			log_info("Link key request");
-			bt_flip_addr(event_addr, &packet[2]);
+			reverse_bd_addr(&packet[2], event_addr);
 			hci_send_cmd(&hci_link_key_request_negative_reply, &event_addr);
 			break;
@@ -308,5 +333,5 @@
 			// inform about pin code request
 			log_info("Pin code request - using '0000'");
-			bt_flip_addr(event_addr, &packet[2]);
+			reverse_bd_addr(&packet[2], event_addr);
 			hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000");
 			break;
@@ -314,5 +339,5 @@
 		case HCI_EVENT_USER_CONFIRMATION_REQUEST:
 			// inform about user confirmation request
-			log_info("HCI User Confirmation Request with numeric value '%06u'", READ_BT_32(packet, 8));
+			log_info("HCI User Confirmation Request with numeric value '%06u'", little_endian_read_32(packet, 8));
 			log_info("HCI User Confirmation Auto accept");
 			break;
@@ -320,7 +345,7 @@
 		case RFCOMM_EVENT_INCOMING_CONNECTION:
 			// data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
-			bt_flip_addr(event_addr, &packet[2]);
+			reverse_bd_addr(&packet[2], event_addr);
 			rfcomm_channel_nr = packet[8];
-			rfcomm_channel_id = READ_BT_16(packet, 9);
+			rfcomm_channel_id = little_endian_read_16(packet, 9);
 			log_info("RFCOMM channel %u requested for %s", rfcomm_channel_nr, bd_addr_to_str(event_addr));
 			rfcomm_accept_connection_internal(rfcomm_channel_id);
@@ -333,6 +358,6 @@
 			}
 			else {
-				rfcomm_channel_id = READ_BT_16(packet, 12);
-				mtu = READ_BT_16(packet, 14);
+				rfcomm_channel_id = little_endian_read_16(packet, 12);
+				mtu = little_endian_read_16(packet, 14);
 				log_info("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u", rfcomm_channel_id, mtu);
 			}
@@ -347,12 +372,16 @@
 			if (packet[2]) {
 				log_info("BNEP channel open failed, status %02x", packet[2]);
+
+				// 30秒後 BNEP Query 開始
+				bt_state = BT_STATE_BNEP_QUERY_WAIT;
+				bt_timer = 30 * 1000;
 			}
 			else {
 				// data: event(8), len(8), status (8), bnep source uuid (16), bnep destination uuid (16), remote_address (48)
-				bnep_cid = READ_BT_16(packet, 3);
-				uuid_source = READ_BT_16(packet, 5);
-				uuid_dest = READ_BT_16(packet, 7);
-				mtu = READ_BT_16(packet, 9);
-				//bt_flip_addr(remote_addr, &packet[11]); 
+				bnep_cid = little_endian_read_16(packet, 3);
+				uuid_source = little_endian_read_16(packet, 5);
+				uuid_dest = little_endian_read_16(packet, 7);
+				mtu = little_endian_read_16(packet, 9);
+				//reverse_bd_addr(&packet[11], remote_addr); 
 				memcpy(&remote_addr, &packet[11], sizeof(bd_addr_t));
 				log_info("BNEP connection open succeeded to %s source UUID 0x%04x dest UUID: 0x%04x, max frame size %u", bd_addr_to_str(event_addr), uuid_source, uuid_dest, mtu);
@@ -360,4 +389,7 @@
 				btstack_network_up(bnep_cid, (uint8_t *)hci_local_bd_addr(), (uint8_t *)remote_addr);
 				log_info("Network Interface %s activated", btstack_network_get_name(bnep_cid));
+
+				bt_state = BT_STATE_CONNECTED;
+				bt_timer = -1;
 			}
 			break;
@@ -368,10 +400,14 @@
 
 		case BNEP_EVENT_CHANNEL_CLOSED:
-			bnep_cid = READ_BT_16(packet, 2);
+			bnep_cid = little_endian_read_16(packet, 2);
 			bt_channel_closed(bnep_cid);
+
+			// 30秒後 BNEP Query 開始
+			bt_state = BT_STATE_BNEP_QUERY_WAIT;
+			bt_timer = 30 * 1000;
 			break;
 
 		case BNEP_EVENT_READY_TO_SEND:
-			bnep_cid = READ_BT_16(packet, 2);
+			bnep_cid = little_endian_read_16(packet, 2);
 			bt_send_packet(bnep_cid);
 			break;
@@ -428,5 +464,5 @@
 }
 
-void panu_init(void)
+void bt_init(void)
 {
 	const hci_transport_t *transport;
@@ -437,7 +473,7 @@
 	service_record_item_t *bnep_service_record_item;
 
+	btstack_network_init(&network_send_packet_callback);
+
 	//hci_dump_open("", HCI_DUMP_PACKETLOGGER);
-
-	log_info("bt初期化...");
 
 	// start with BTstack init - especially configure HCI Transport
@@ -446,5 +482,4 @@
 
 	/* Initialize HCI */
-	log_info("HCI初期化...");
 	transport = hci_transport_usb_instance();
 	control = NULL;
@@ -455,14 +490,11 @@
 
 	/* Initialize L2CAP */
-	log_info("L2CAP初期化...");
 	l2cap_init();
 	l2cap_register_packet_handler(packet_handler);
 
 	/* Initialise SDP, create record for SPP and register with SDP */
-	log_info("SDP初期化...");
 	sdp_init();
 
 	/* Initialize RFCOMM */
-	log_info("RFCOMM初期化...");
 	rfcomm_init();
 	rfcomm_register_packet_handler(packet_handler);
@@ -470,8 +502,12 @@
 
 	/* Initialise BNEP */
-	log_info("BNEP初期化...");
 	bnep_init();
 	bnep_register_packet_handler(packet_handler);
-	bnep_register_service(NULL, BNEP_UUID_PANU, BNEP_MTU_MIN);  /* Minimum L2CAP MTU for bnep is 1691 bytes */
+	if (bt_bnep_mode == 0) {
+		bnep_register_service(NULL, BLUETOOTH_SERVICE_CLASS_PANU, BNEP_MTU_MIN);  /* Minimum L2CAP MTU for bnep is 1691 bytes */
+	}
+	else {
+		bnep_register_service(NULL, BLUETOOTH_SERVICE_CLASS_NAP, BNEP_MTU_MIN);  /* Minimum L2CAP MTU for bnep is 1691 bytes */
+	}
 
 	memset(spp_service_buffer, 0, sizeof(spp_service_buffer));
@@ -482,5 +518,4 @@
 	bnep_service_record_item = (service_record_item_t *)bnep_service_buffer;
 
-	log_info("SPPサービス登録...");
 	sdp_create_spp_service((uint8_t*)&spp_service_record_item->service_record, 1, "RX SPP");
 	de_dump_data_element((uint8_t*)spp_service_record_item->service_record);
@@ -488,8 +523,15 @@
 	sdp_register_service_internal(NULL, spp_service_record_item);
 
-	log_info("BNEPサービス登録...");
-	pan_create_panu_service((uint8_t*)&bnep_service_record_item->service_record, "RX PANU", "RX PANU", BNEP_SECURITY_NONE);
-	de_dump_data_element((uint8_t*)bnep_service_record_item->service_record);
-	log_info("PANU SDP service buffer size: %u", (uint16_t)(sizeof(service_record_item_t) + de_get_len((uint8_t*)bnep_service_record_item->service_record)));
+	if (bt_bnep_mode == 0) {
+		pan_create_panu_service((uint8_t*)&bnep_service_record_item->service_record, "RX PANU", "RX PANU", BNEP_SECURITY_NONE);
+		de_dump_data_element((uint8_t*)bnep_service_record_item->service_record);
+		log_info("PANU SDP service buffer size: %u", (uint16_t)(sizeof(service_record_item_t) + de_get_len((uint8_t*)bnep_service_record_item->service_record)));
+	}
+	else {
+		pan_create_nap_service((uint8_t*)&bnep_service_record_item->service_record, "RX NAP", "RX NAP", BNEP_SECURITY_NONE,
+			PAN_NET_ACCESS_TYPE_100MB_ETHERNET, 0x0001312D0/*10Mb/sec*/, NULL, NULL);
+		de_dump_data_element((uint8_t*)bnep_service_record_item->service_record);
+		log_info("NAP SDP service buffer size: %u", (uint16_t)(sizeof(service_record_item_t) + de_get_len((uint8_t*)bnep_service_record_item->service_record)));
+	}
 	sdp_register_service_internal(NULL, bnep_service_record_item);
 
@@ -500,5 +542,4 @@
 
 	/* Turn on the device */
-	log_info("HCI電源ON...");
 	hci_power_control(HCI_POWER_ON);
 
@@ -506,30 +547,31 @@
 	sdp_parser_init();
 	sdp_parser_register_callback(handle_sdp_client_query_result);
-}
-
-void nap_init(void);
-
-void bt_init(void)
-{
-	btstack_network_init(&network_send_packet_callback);
-
-	if (bt_bnep_mode == 0) {
-		panu_init();
-	}
-	else {
-		nap_init();
-	}
-
-	bt_enabled = true;
-}
-
-void bt_task(void)
-{
-	extern void hal_tick_handler();
-
-	if (bt_enabled) {
-		hal_tick_handler();
+
+	bt_state = BT_STATE_POWER_ON;
+	bt_timer = -1;
+}
+
+void bt_process(void)
+{
+	if (bt_state != BT_STATE_DORMANT) {
 		run_loop_execute();
 		bt_send_packet(0xffff);
+	}
+
+	if (bt_timer != 0)
+		return;
+
+	switch (bt_state) {
+	case BT_STATE_BNEP_QUERY_WAIT:
+		/* Send a general query for BNEP Protocol ID */
+		log_info("Start SDP BNEP query.");
+		sdp_general_query_for_uuid(remote_addr, BLUETOOTH_PROTOCOL_BNEP);
+
+		bt_state = BT_STATE_BNEP_QUERY;
+		bt_timer = -1;
+		break;
+	default:
+		bt_timer = -1;
+		break;
 	}
 }
@@ -592,4 +634,56 @@
 	hci_close();
 
-	bt_enabled = false;
-}
+	bt_state = BT_STATE_DORMANT;
+	bt_timer = -1;
+}
+
+void bt_task_on_start(struct task_if_t *task, ID tskid)
+{
+}
+
+void bt_task_on_end(struct task_if_t *task)
+{
+}
+
+int bt_task_get_timer(struct task_if_t *task)
+{
+	return bt_timer;
+}
+
+void bt_task_progress(struct task_if_t *task, int elapse)
+{
+	if (bt_timer == -1)
+		return;
+
+	bt_timer -= elapse;
+	if (bt_timer < 0)
+		bt_timer = 0;
+}
+
+void bt_task_process(struct task_if_t *task, enum task_event_t event)
+{
+	extern void hal_tick_handler();
+
+	switch (event & ~TASK_EVENT_TICK) {
+	case TASK_EVENT_START:
+		bt_init();
+		break;
+	case TASK_EVENT_TERMINATE:
+		bt_terminate();
+		break;
+	case TASK_EVENT_WAKEUP:
+	case TASK_EVENT_TIMEOUT:
+		if (event & TASK_EVENT_TICK)
+			hal_tick_handler();
+		bt_process();
+		break;
+	}
+}
+
+struct task_if_t bt_task = {
+	bt_task_on_start,
+	bt_task_on_end,
+	bt_task_get_timer,
+	bt_task_progress,
+	bt_task_process,
+};
