[Adapt] Initialize bluetooth socket interface 08/79808/1
authorAtul Rai <a.rai@samsung.com>
Tue, 12 Jul 2016 07:44:12 +0000 (16:44 +0900)
committerAtul Rai <a.rai@samsung.com>
Wed, 13 Jul 2016 04:59:43 +0000 (13:59 +0900)
This patch adds bluetooth socket interface initialization logic
in bt-service and OAL.

Change-Id: Idb93c2be95151ca616cae78a769fec9e208203bd
Signed-off-by: Atul Rai <a.rai@samsung.com>
bt-oal/CMakeLists.txt
bt-oal/bluez_hal/hardware/bt_sock.h [new file with mode: 0644]
bt-oal/include/oal-socket.h [new file with mode: 0755]
bt-oal/oal-socket.c [new file with mode: 0755]
bt-service-adaptation/CMakeLists.txt
bt-service-adaptation/services/adapter/bt-service-core-adapter.c
bt-service-adaptation/services/bt-service-event-receiver.c
bt-service-adaptation/services/include/bt-service-event-receiver.h
bt-service-adaptation/services/include/bt-service-socket.h [new file with mode: 0644]
bt-service-adaptation/services/socket/bt-service-socket.c [new file with mode: 0644]

index 520b0bc..8aec2fd 100755 (executable)
@@ -15,6 +15,7 @@ oal-manager.c
 oal-adapter-mgr.c
 oal-device-mgr.c
 oal-hid-host.c
+oal-socket.c
 common/oal-utils.c
 common/oal-common.c
 common/oal-event-dispatcher.c
diff --git a/bt-oal/bluez_hal/hardware/bt_sock.h b/bt-oal/bluez_hal/hardware/bt_sock.h
new file mode 100644 (file)
index 0000000..a4aa046
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * 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.
+ */
+
+#ifndef ANDROID_INCLUDE_BT_SOCK_H
+#define ANDROID_INCLUDE_BT_SOCK_H
+
+__BEGIN_DECLS
+
+#define BTSOCK_FLAG_ENCRYPT 1
+#define BTSOCK_FLAG_AUTH (1 << 1)
+
+typedef enum {
+    BTSOCK_RFCOMM = 1,
+    BTSOCK_SCO = 2,
+    BTSOCK_L2CAP = 3
+} btsock_type_t;
+
+/** Represents the standard BT SOCKET interface. */
+typedef struct {
+    short size;
+    bt_bdaddr_t bd_addr;
+    int channel;
+    int status;
+} __attribute__((packed)) sock_connect_signal_t;
+
+typedef struct {
+
+    /** set to size of this struct*/
+    size_t          size;
+    /**
+     * listen to a rfcomm uuid or channel. It returns the socket fd from which
+     * btsock_connect_signal can be read out when a remote device connected
+     */
+    bt_status_t (*listen)(btsock_type_t type, const char* service_name, const uint8_t* service_uuid, int channel, int* sock_fd, int flags);
+    /*
+     * connect to a rfcomm uuid channel of remote device, It returns the socket fd from which
+     * the btsock_connect_signal and a new socket fd to be accepted can be read out when connected
+     */
+    bt_status_t (*connect)(const bt_bdaddr_t *bd_addr, btsock_type_t type, const uint8_t* uuid, int channel, int* sock_fd, int flags);
+
+} btsock_interface_t;
+
+__END_DECLS
+
+#endif /* ANDROID_INCLUDE_BT_SOCK_H */
diff --git a/bt-oal/include/oal-socket.h b/bt-oal/include/oal-socket.h
new file mode 100755 (executable)
index 0000000..8b7c0f7
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+* Open Adaptation Layer (OAL)
+*
+* Copyright (c) 2014-2015 Samsung Electronics Co., Ltd.
+*
+* 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.
+*
+*/
+
+
+#ifndef _OAL_SOCKET_HOST_H_
+#define _OAL_SOCKET_HOST_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @fn oal_status_t socket_enable(void);
+ * @brief Enables the rfcomm profile
+ *
+ * This API will register the callback function, when any response/data are received from
+ * bluetooth RFCOMM remote device. @n
+ * this registered callback function will be get called with fd of remote device, data buffer and its length.
+ * This function is a synchronous call.
+ *
+ * @param[in]   data_cb      A pointer to the callback function
+ * @return      BLUETOOTH_ERROR_NONE - Success
+ * @remark      None
+ * @see         None
+ * */
+oal_status_t socket_enable(void);
+
+/**
+ * @fn oal_status_t socket_disable(void);
+ * @brief Disables the rfcomm profile
+ *
+ * This API will disable RFCOMM profile. Before disabling it's better to ensure
+ * all the connections are closed
+ * This function is a synchronous call.
+ *
+ * @return      BLUETOOTH_ERROR_NONE - Success
+ * @remark      None
+ * @see         socket_disconnect
+ * */
+oal_status_t socket_disable(void);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+#endif /*_OAL_SOCKET_HOST_H_*/
diff --git a/bt-oal/oal-socket.c b/bt-oal/oal-socket.c
new file mode 100755 (executable)
index 0000000..50f4250
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * Open Adaptation Layer (OAL)
+ *
+ * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd.
+ *
+ * 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 <string.h>
+#include <sys/types.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <glib.h>
+#include <dlog.h>
+#include <sys/socket.h>
+#include <errno.h>
+#include <fcntl.h>
+
+#include <oal-event.h>
+#include <oal-manager.h>
+#include "oal-internal.h"
+
+#include "bluetooth.h"
+#include "bt_sock.h"
+#include "oal-socket.h"
+#include "oal-utils.h"
+
+#define CHECK_OAL_SOCKET_ENABLED() \
+       do { \
+               if (socket_api == NULL) { \
+                       BT_ERR("Socket is not Initialized"); \
+                       return OAL_STATUS_NOT_READY; \
+               } \
+       } while (0)
+
+/*
+ * Global variables
+ */
+static const btsock_interface_t* socket_api = NULL;
+
+oal_status_t socket_enable()
+{
+       const bt_interface_t *blued_api;
+
+       API_TRACE("Socket Init");
+
+       blued_api = adapter_get_stack_interface();
+       if(blued_api == NULL) {
+               BT_ERR("Stack is not initialized");
+               return OAL_STATUS_NOT_READY;
+       }
+
+       if(socket_api != NULL) {
+               BT_WARN("Socket Interface is already initialized...");
+               return OAL_STATUS_ALREADY_DONE;
+       }
+
+       socket_api = (const btsock_interface_t*)blued_api->get_profile_interface(BT_PROFILE_SOCKETS_ID);
+       if (!socket_api){
+               BT_ERR("OAL Socket failed to initialize");
+               return OAL_STATUS_INTERNAL_ERROR;
+       }
+
+       BT_DBG("Socket successfully initiated");
+       return OAL_STATUS_SUCCESS ;
+}
+
+oal_status_t socket_disable(void)
+{
+
+       API_TRACE("Socket Deinit");
+
+       CHECK_OAL_SOCKET_ENABLED();
+       socket_api = NULL;
+       return OAL_STATUS_SUCCESS;
+}
index 524dee2..661ccfb 100644 (file)
@@ -15,6 +15,7 @@ marshal.c
 ./services/bt-service-dpm.c
 ./services/bt-service-agent-util.c
 ./services/hid/bt-service-hidhost.c
+./services/socket/bt-service-socket.c
 )
 
 IF("${CMAKE_BUILD_TYPE}" STREQUAL "")
index 78525f2..8eec060 100644 (file)
@@ -45,6 +45,7 @@
 #include "bt-service-dpm.h"
 #endif
 #include "bt-service-hidhost.h"
+#include "bt-service-socket.h"
 
 /* OAL headers */
 #include <oal-event.h>
@@ -692,6 +693,11 @@ static int __bt_init_profiles()
                BT_ERR("_bt_hidhost_initialize Failed");
                return ret;
        }
+       ret = _bt_socket_init();
+       if (ret != BLUETOOTH_ERROR_NONE) {
+               BT_ERR("_bt_socket_init Failed");
+               return ret;
+       }
 
        return BLUETOOTH_ERROR_NONE;
 }
index 7d68db9..912c3a6 100644 (file)
@@ -36,6 +36,7 @@
 _bt_service_event_handler_callback adapter_cb;
 _bt_service_event_handler_callback device_cb;
 _bt_service_event_handler_callback hid_cb;
+_bt_service_event_handler_callback socket_cb;
 
 void _bt_service_register_event_handler_callback(
                bt_service_module_t module, _bt_service_event_handler_callback cb)
@@ -53,6 +54,10 @@ void _bt_service_register_event_handler_callback(
                 BT_INFO("Register BT_HID_MODULE Callback");
                 hid_cb = cb;
                 break;
+       case BT_SOCKET_MODULE:
+                BT_INFO("Register BT_SOCKET_MODULE Callback");
+                socket_cb = cb;
+                break;
        default:
                BT_INFO("Unknown module");
        }
@@ -73,6 +78,10 @@ void _bt_service_unregister_event_handler_callback(bt_service_module_t module)
                 BT_INFO("Un-Register BT_HID_MODULE Callback");
                 hid_cb = NULL;
                 break;
+       case BT_SOCKET_MODULE:
+                BT_INFO("Un-Register BT_SOCKET_MODULE Callback");
+                socket_cb = NULL;
+                break;
        default:
                BT_INFO("Unknown module");
        }
index 99e8ce1..0e10eff 100644 (file)
@@ -32,6 +32,7 @@ typedef enum {
        BT_ADAPTER_MODULE,
        BT_DEVICE_MODULE,
        BT_HID_MODULE,
+       BT_SOCKET_MODULE,
 } bt_service_module_t;
 
 void _bt_service_oal_event_receiver(int event_type, gpointer event_data, gsize len);
diff --git a/bt-service-adaptation/services/include/bt-service-socket.h b/bt-service-adaptation/services/include/bt-service-socket.h
new file mode 100644 (file)
index 0000000..5b0a70c
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Bluetooth-frwk
+ *
+ * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:  Atul Kumar Rai <a.rai@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.
+ *
+ */
+
+#ifndef __BT_SERVICE_SOCKET_H__
+#define __BT_SERVICE_SOCKET_H__
+
+#include <glib.h>
+#include <sys/types.h>
+#include "bluetooth-api.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int _bt_socket_init(void);
+void _bt_socket_deinit(void);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+#endif /* __BT_SERVICE_SOCKET_H__ */
diff --git a/bt-service-adaptation/services/socket/bt-service-socket.c b/bt-service-adaptation/services/socket/bt-service-socket.c
new file mode 100644 (file)
index 0000000..b99e740
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * Bluetooth-frwk
+ *
+ * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Author: Atul Rai <a.rai@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 <string.h>
+#include <sys/types.h>
+#include <glib.h>
+#include <dlog.h>
+
+/* OAL headers */
+#include <oal-event.h>
+#include <oal-socket.h>
+
+/* bt-service headers */
+#include "bt-internal-types.h"
+#include "bt-service-common.h"
+#include "bt-service-util.h"
+#include "bt-service-event-receiver.h"
+
+static void __bt_socket_event_handler(int event_type, gpointer event_data)
+{
+       BT_INFO("OAL event = 0x%x, \n", event_type);
+
+       switch(event_type) {
+       default:
+               BT_ERR("Invalid event:%d\n", event_type);
+               break;
+       }
+}
+
+int _bt_socket_init(void)
+{
+       BT_INFO("Socket Init");
+
+       if(OAL_STATUS_SUCCESS != socket_enable()) {
+               BT_ERR("Socket init failed");
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
+
+       /* Register SOCKET event handler */
+       _bt_service_register_event_handler_callback(BT_SOCKET_MODULE, __bt_socket_event_handler);
+       return BLUETOOTH_ERROR_NONE;
+}
+
+void _bt_socket_deinit(void)
+{
+       BT_INFO("Socket de-init");
+
+       if(OAL_STATUS_SUCCESS != socket_disable())
+               BT_ERR("Socket de-init failed");
+
+       /* Un-register SOCKET event handler */
+       _bt_service_unregister_event_handler_callback(BT_SOCKET_MODULE);
+}