Support TV initiate pairing and connection 98/220798/1
authorDoHyun Pyun <dh79.pyun@samsung.com>
Mon, 23 Dec 2019 09:43:11 +0000 (18:43 +0900)
committerDoHyun Pyun <dh79.pyun@samsung.com>
Mon, 23 Dec 2019 09:43:11 +0000 (18:43 +0900)
The keyboard will be connected as the normal keyboard
type, and send the normal key event

Change-Id: I08b83f74eb979084f0c041d4beb02296ffbf69ca
Signed-off-by: DoHyun Pyun <dh79.pyun@samsung.com>
test/hid_keyboard.c

index 3b9c6cc..0b6fb0d 100644 (file)
@@ -27,6 +27,8 @@
 #endif
 #define LOG_TAG "HID_KEYBOARD"
 
+#define INIT_BOND 0
+
 /**
  *   Variables
  **/
@@ -34,11 +36,13 @@ static GMainLoop *g_mainloop = NULL;
 static bt_adapter_state_e bt_state = BT_ADAPTER_DISABLED;
 static char remote_addr[18] = "F6:FB:8F:D8:C8:7C";
 static bool address_input = false;
+#if INIT_BOND
 static bool scanning = false;
 static bool setup_in_progress = false;
 static GSList *le_scan_list;
 static bool g_wait_flag;
 static int g_wait_count;
+#endif
 
 static bool device_bonded = false;
 static bool hid_connected = false;
@@ -47,6 +51,7 @@ static bool audio_connected = false;
 #define PRT(format, args...) printf("%s:%d() "format, __FUNCTION__, __LINE__, ##args)
 #define HID_PRT(format, args...) PRT(format"\n", ##args)
 
+#if INIT_BOND
 #define WAIT_FOR_SYNC {\
        g_wait_flag = true;\
        g_wait_count = 0;\
@@ -57,6 +62,7 @@ static bool audio_connected = false;
                g_wait_count++;\
        }\
 }
+#endif
 
 typedef struct {
        const char *tc_name;
@@ -65,7 +71,9 @@ typedef struct {
 
 typedef enum {
        BT_HID_DEVICE_SET_ADDRESS = 1,
+#if INIT_BOND
        BT_HID_DEVICE_TEST_SETUP,
+#endif
        BT_HID_DEVICE_TEST_VOLUME_UP,
        BT_HID_DEVICE_TEST_VOLUME_DOWN,
        BT_HID_DEVICE_TEST_CHANNEL_UP,
@@ -74,16 +82,20 @@ typedef enum {
        BT_HID_DEVICE_TEST_DISCONNECT_HID,
        BT_HID_DEVICE_TEST_CONNECT_AUDIO,
        BT_HID_DEVICE_TEST_DISCONNECT_AUDIO,
+#if INIT_BOND
        BT_HID_DEVICE_TEST_SEARCH,
        BT_HID_DEVICE_TEST_STOP_SEARCH,
+#endif
        BT_HID_DEVICE_TEST_FINISH = 0xFF,
 } bt_hid_test_type_e;
 
 tc_table_t tc_hid_device[] = {
        {"SET ADDRESS"
                , BT_HID_DEVICE_SET_ADDRESS},
+#if INIT_BOND
        {"Initial Setup (Bond & Connects)"
                , BT_HID_DEVICE_TEST_SETUP},
+#endif
        {"Volume Up"
                , BT_HID_DEVICE_TEST_VOLUME_UP},
        {"Volume Down"
@@ -92,18 +104,20 @@ tc_table_t tc_hid_device[] = {
                , BT_HID_DEVICE_TEST_CHANNEL_UP},
        {"Channel Down"
                , BT_HID_DEVICE_TEST_CHANNEL_DOWN},
-       {"AUDIO Connect only"
-               , BT_HID_DEVICE_TEST_CONNECT_AUDIO},
-       {"AUDIO Disconnect only"
-               , BT_HID_DEVICE_TEST_DISCONNECT_AUDIO},
        {"HID Connect only"
                , BT_HID_DEVICE_TEST_CONNECT_HID},
        {"HID Disconnect only"
                , BT_HID_DEVICE_TEST_DISCONNECT_HID},
+       {"AUDIO Connect only"
+               , BT_HID_DEVICE_TEST_CONNECT_AUDIO},
+       {"AUDIO Disconnect only"
+               , BT_HID_DEVICE_TEST_DISCONNECT_AUDIO},
+#if INIT_BOND
        {"Search TV"
                , BT_HID_DEVICE_TEST_SEARCH},
        {"Stop to search"
                , BT_HID_DEVICE_TEST_STOP_SEARCH},
+#endif
        {"Finish"
                , BT_HID_DEVICE_TEST_FINISH},
        {NULL, 0x0000},
@@ -203,6 +217,20 @@ static gboolean __bt_timeout_func(gpointer data)
        return FALSE;
 }
 
+static gboolean __bt_audio_connect_cb(gpointer user_data)
+{
+       int ret;
+
+       if (audio_connected == true)
+               return FALSE;
+
+       HID_PRT("Try to connect audio..");
+       ret = bt_audio_connect(remote_addr, BT_AUDIO_PROFILE_TYPE_A2DP_SINK);
+       HID_PRT("returns %s", __bt_get_error_message(ret));
+
+       return TRUE;
+}
+
 static void __bt_hid_device_connection_state_changed_cb(int result,
        bool connected, const char *remote_address, void *user_data)
 {
@@ -210,11 +238,17 @@ static void __bt_hid_device_connection_state_changed_cb(int result,
        HID_PRT("Remote Address: %s", remote_address);
        HID_PRT("Connected [%d]", connected);
 
+#if INIT_BOND
        g_wait_flag = false;
+#endif
 
        if (connected == true && result == BT_ERROR_NONE) {
                g_strlcpy(remote_addr, remote_address, 18);
                hid_connected = true;
+
+               /* Try to connect Audio */
+               if (audio_connected == false)
+                       g_timeout_add(2000, __bt_audio_connect_cb, NULL);
        } else {
                hid_connected = false;
        }
@@ -229,7 +263,9 @@ void __bt_audio_connection_state_changed_cb(int result,
        HID_PRT("address [%s]", remote_address);
        HID_PRT("type [%d]", type);
 
+#if INIT_BOND
        g_wait_flag = false;
+#endif
 
        if (connected == true && result == BT_ERROR_NONE) {
                g_strlcpy(remote_addr, remote_address, 18);
@@ -275,7 +311,9 @@ void __bt_device_bond_created_cb(int result,
                HID_PRT("result: %s", __bt_get_error_message(result));
        }
 
+#if INIT_BOND
        g_wait_flag = false;
+#endif
 }
 
 static void __bt_state_changed_impl(int result,
@@ -295,6 +333,7 @@ static void __bt_state_changed_impl(int result,
                g_main_loop_quit(g_mainloop);
 }
 
+#if INIT_BOND
 gint __bt_compare_address(gpointer *a, gpointer *b)
 {
        bt_adapter_le_device_scan_result_info_s *info = (bt_adapter_le_device_scan_result_info_s *)a;
@@ -398,6 +437,20 @@ static void __bt_find_remote_tv(void)
        if (ret == BT_ERROR_NONE)
                scanning = true;
 }
+#else
+static bool __bt_adapter_bonded_device_cb(bt_device_info_s *device_info, void *user_data)
+{
+       if (device_info != NULL) {
+               if (g_strrstr(device_info->remote_name, "[TV]")) {
+                       HID_PRT("TV is boned : %s [%s]", device_info->remote_name, device_info->remote_address);
+                       g_strlcpy(remote_addr, device_info->remote_address, 18);
+                       return false;
+               }
+       }
+
+       return true;
+}
+#endif
 
 static gboolean __bt_start_service(gpointer user_data)
 {
@@ -405,6 +458,10 @@ static gboolean __bt_start_service(gpointer user_data)
 
        ret = bt_audio_initialize();
 
+       ret = bt_adapter_set_visibility(BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE, 0);
+
+       ret = bt_adapter_set_name("Tizen Keyboard");
+
        ret = bt_audio_select_role(BT_A2DP_SINK);
 
        ret = bt_device_set_bond_created_cb(__bt_device_bond_created_cb, NULL);
@@ -432,13 +489,18 @@ static gboolean __bt_start_service(gpointer user_data)
        HID_PRT("A2DP connected devices returns %s\n", __bt_get_error_message(ret));
 
        if (hid_connected == false && audio_connected == false) {
+#if INIT_BOND
                HID_PRT("No TV is connected, try to search...");
+
                __bt_find_remote_tv();
+#else
+               ret = bt_adapter_foreach_bonded_device(__bt_adapter_bonded_device_cb, NULL);
+#endif
        }
-
        return FALSE;
 }
 
+#if INIT_BOND
 static void __bt_stop_setup(void)
 {
        setup_in_progress = false;
@@ -493,7 +555,7 @@ static int __bt_initial_setup(void)
 
        return ret;
 }
-
+#endif
 
 static void __bt_tc_usage_print(void)
 {
@@ -509,16 +571,20 @@ static void __bt_tc_usage_print(void)
        HID_PRT("\n");
 }
 
+static int key_code = 0x01;
+
 static int __bt_test_input_callback(void *data)
 {
        int ret = 0;
        int test_id = (int)data;
-       GSList *l = NULL;
+       bt_hid_key_data_s send_data;
+       char pressedkey[8] = { 0x43, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00 };
+       char pressedkey1[8] = { 0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00 };
+
+#if INIT_BOND
        int cnt = 0;
+       GSList *l = NULL;
        bt_adapter_le_device_scan_result_info_s *info;
-       unsigned char report_id = 0xF7;
-       char pressed_data[6]    = {0x07, 0x00, 0x00, 0x00, 0x00, 0x00};
-       char released_data[6]   = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
 
        if (setup_in_progress == true &&
                 test_id != BT_HID_DEVICE_TEST_SETUP &&
@@ -527,6 +593,7 @@ static int __bt_test_input_callback(void *data)
                HID_PRT("Wait to finish setup");
                return 0;
        }
+#endif
 
        switch (test_id) {
        case BT_HID_DEVICE_SET_ADDRESS:
@@ -536,6 +603,7 @@ static int __bt_test_input_callback(void *data)
                address_input = true;
 
                break;
+#if INIT_BOND
        case BT_HID_DEVICE_TEST_SETUP:
                if (setup_in_progress == true) {
                        HID_PRT("Stop the initial setup");
@@ -546,45 +614,60 @@ static int __bt_test_input_callback(void *data)
                        }
                }
                break;
+#endif
        case BT_HID_DEVICE_TEST_VOLUME_UP:
-               pressed_data[0] = 0x07;  /* Vol up */
+               pressedkey[0] = 0x43; /* Vol up */
 
-               ret = bt_hid_device_send_custom_event(remote_addr,
-                               report_id, pressed_data, 6);
+               memcpy(send_data.key, pressedkey, 8);
+               send_data.modifier = 0;
+
+               ret = bt_hid_device_send_key_event(remote_addr, &send_data);
                HID_PRT("returns %d\n", ret);
 
-               ret = bt_hid_device_send_custom_event(remote_addr,
-                               report_id, released_data, 6);
+               memcpy(send_data.key, pressedkey1, 8);
+               ret = bt_hid_device_send_key_event(remote_addr, &send_data);
+
                break;
        case BT_HID_DEVICE_TEST_VOLUME_DOWN:
-               pressed_data[0] = 0x0B; /* Vol down */
+               pressedkey[0] = 0x42; /* Vol down */
 
-               ret = bt_hid_device_send_custom_event(remote_addr,
-                               report_id, pressed_data, 6);
+               memcpy(send_data.key, pressedkey, 8);
+               send_data.modifier = 0;
+
+               ret = bt_hid_device_send_key_event(remote_addr, &send_data);
                HID_PRT("returns %d\n", ret);
 
-               ret = bt_hid_device_send_custom_event(remote_addr,
-                               report_id, released_data, 6);
+               memcpy(send_data.key, pressedkey1, 8);
+               ret = bt_hid_device_send_key_event(remote_addr, &send_data);
+
                break;
        case BT_HID_DEVICE_TEST_CHANNEL_UP:
-               pressed_data[0] = 0x12; /* CH up */
+               pressedkey[0] = 0x45; /* CH up */
 
-               ret = bt_hid_device_send_custom_event(remote_addr,
-                               report_id, pressed_data, 6);
+               HID_PRT("key_code %d", key_code);
+
+               memcpy(send_data.key, pressedkey, 8);
+               send_data.modifier = 0;
+
+               ret = bt_hid_device_send_key_event(remote_addr, &send_data);
                HID_PRT("returns %d\n", ret);
 
-               ret = bt_hid_device_send_custom_event(remote_addr,
-                               report_id, released_data, 6);
+               memcpy(send_data.key, pressedkey1, 8);
+               ret = bt_hid_device_send_key_event(remote_addr, &send_data);
+
                break;
        case BT_HID_DEVICE_TEST_CHANNEL_DOWN:
-               pressed_data[0] = 0x10; /* CH down */
+               pressedkey[0] = 0x44; /* CH down */
+
+               memcpy(send_data.key, pressedkey, 8);
+               send_data.modifier = 0;
 
-               ret = bt_hid_device_send_custom_event(remote_addr,
-                               report_id, pressed_data, 6);
+               ret = bt_hid_device_send_key_event(remote_addr, &send_data);
                HID_PRT("returns %d\n", ret);
 
-               ret = bt_hid_device_send_custom_event(remote_addr,
-                               report_id, released_data, 6);
+               memcpy(send_data.key, pressedkey1, 8);
+               ret = bt_hid_device_send_key_event(remote_addr, &send_data);
+
                break;
        case BT_HID_DEVICE_TEST_CONNECT_AUDIO:
                ret = bt_audio_connect(remote_addr,  BT_AUDIO_PROFILE_TYPE_A2DP_SINK);
@@ -602,6 +685,7 @@ static int __bt_test_input_callback(void *data)
                ret = bt_hid_device_disconnect(remote_addr);
                HID_PRT("returns %s", __bt_get_error_message(ret));
                break;
+#if INIT_BOND
        case BT_HID_DEVICE_TEST_SEARCH:
                __bt_find_remote_tv();
                break;
@@ -627,6 +711,7 @@ static int __bt_test_input_callback(void *data)
                le_scan_list = NULL;
 
                break;
+#endif
        case BT_HID_DEVICE_TEST_FINISH:
                HID_PRT("Finished");
                g_main_loop_quit(g_mainloop);
@@ -668,6 +753,7 @@ static gboolean __bt_key_event_cb(GIOChannel *chan,
                address_input = false;
        }
 
+#if INIT_BOND
        if (scanning == true) {
                test_id = BT_HID_DEVICE_TEST_STOP_SEARCH;
 #ifdef ARCH64
@@ -677,6 +763,7 @@ static gboolean __bt_key_event_cb(GIOChannel *chan,
 #endif
                return TRUE;
        }
+#endif
 
        test_id = atoi(buf);