[IOT-1540] Add plumbling logic of connection manager
authorjihwan.seo <jihwan.seo@samsung.com>
Mon, 14 Nov 2016 22:36:49 +0000 (07:36 +0900)
committerDan Mihai <Daniel.Mihai@microsoft.com>
Fri, 2 Dec 2016 02:03:34 +0000 (02:03 +0000)
Change-Id: Ieffef039491a8895c359efb807dc36daa55860d1
Signed-off-by: jihwan.seo <jihwan.seo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/14291
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Hyuna Jo <hyuna0213.jo@samsung.com>
Reviewed-by: Jaehong Jo <jaehong.jo@samsung.com>
Reviewed-by: Dan Mihai <Daniel.Mihai@microsoft.com>
resource/csdk/connectivity/api/cautilinterface.h
resource/csdk/connectivity/util/SConscript
resource/csdk/connectivity/util/inc/caconnectionmanager.h [new file with mode: 0644]
resource/csdk/connectivity/util/src/camanager/caconnectionmanager.c [new file with mode: 0644]
resource/csdk/connectivity/util/src/camanager/camanagerutil.c [new file with mode: 0644]
resource/csdk/connectivity/util/src/camanager/camanagerutil.h [new file with mode: 0644]
resource/csdk/connectivity/util/src/camanager/camessagearbiter.c [new file with mode: 0644]
resource/csdk/connectivity/util/src/camanager/camessagearbiter.h [new file with mode: 0644]
resource/csdk/connectivity/util/src/camanager/capolicymanager.c [new file with mode: 0644]
resource/csdk/connectivity/util/src/camanager/capolicymanager.h [new file with mode: 0644]

index 2a82bcb..d82452f 100644 (file)
@@ -31,6 +31,28 @@ extern "C"
 #endif
 
 /**
+ * this level depends on transmission time.
+ * unicast based UDP will be checked by caretransmission.
+ */
+typedef enum
+{
+    HIGH_SPEED = 0,
+    NORMAL_SPEED
+} CMSpeedLevel_t;
+
+typedef struct
+{
+    /** address for all **/
+    char addr[MAX_ADDR_STR_SIZE_CA];
+
+    /** adapter priority of all transmissions. **/
+    CATransportAdapter_t adapter;
+
+    /** level about speed of response. **/
+    CMSpeedLevel_t level;
+} CMConfigureInfo_t;
+
+/**
  * Callback function type for connection status changes delivery.
  * @param[out]   info           Remote endpoint information.
  * @param[out]   isConnected    Current connection status info.
index d826a42..9fdaa31 100644 (file)
@@ -30,4 +30,11 @@ if target_os == 'android':
 
        if (('BT' in ca_transport) or ('ALL' in ca_transport)):
                        env.AppendUnique(CA_SRC = [
-                       os.path.join(src_dir, 'btpairing' ,'android', 'cabtpairing.c')])
\ No newline at end of file
+                       os.path.join(src_dir, 'btpairing' ,'android', 'cabtpairing.c')])
+
+env.AppendUnique(CA_SRC = [
+               os.path.join(src_dir, 'camanager', 'caconnectionmanager.c'),
+               os.path.join(src_dir, 'camanager', 'camanagerutil.c'),
+               os.path.join(src_dir, 'camanager', 'camessagearbiter.c'),
+               os.path.join(src_dir, 'camanager', 'capolicymanager.c')])
+
diff --git a/resource/csdk/connectivity/util/inc/caconnectionmanager.h b/resource/csdk/connectivity/util/inc/caconnectionmanager.h
new file mode 100644 (file)
index 0000000..eba0b1f
--- /dev/null
@@ -0,0 +1,117 @@
+/* *****************************************************************
+ *
+ * Copyright 2016 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * 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.
+ *
+ ******************************************************************/
+
+/**
+ * @file
+ * This file contains common function for connection manager
+ */
+
+#ifndef CA_CONNECTIONMANAGER_H_
+#define CA_CONNECTIONMANAGER_H_
+
+#include <stdint.h>
+
+#include <coap/coap.h>
+#include "cathreadpool.h"
+#include "octhread.h"
+#include "uarraylist.h"
+#include "cacommon.h"
+#include "caprotocolmessage.h"
+#include "camessagehandler.h"
+#include "cautilinterface.h"
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/**
+ * Callback to send replaced data.
+ * @param[in]   data    send data.
+ */
+typedef void (*CASendThreadFunc)(CAData_t *data);
+
+/**
+ * Callback to notify received data from the remote endpoint.
+ * @param[in]   data    received data.
+ */
+typedef void (*CAReceiveThreadFunc)(CAData_t *data);
+
+/**
+ * Context of connection manager
+ */
+typedef struct
+{
+    /** send method for block data. **/
+    CASendThreadFunc sendThreadFunc;
+
+    /** callback function for received message. **/
+    CAReceiveThreadFunc receivedThreadFunc;
+
+    /** array list on which the thread is operating. **/
+    u_arraylist_t *dataList;
+
+    /** data list mutex for synchronization. **/
+    oc_mutex dataListMutex;
+
+    /** sender mutex for synchronization. **/
+    oc_mutex dataSenderMutex;
+} CAConnectionManagerContext_t;
+
+/**
+ * Initializes the connection manager context.
+ * @param[in]  CASendThreadFunc    function point to add data in send queue thread.
+ * @param[in]  CAReceiveThreadFunc function point to add data in receive queue thread.
+ * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
+ */
+CAResult_t CAInitializeConnectionManager(CASendThreadFunc blockSendMethod,
+                                         CAReceiveThreadFunc receivedDataCallback);
+
+/**
+ * Terminate the connection manager context.
+ */
+void CATerminateConnectionManager();
+
+/**
+ * Initialize mutex.
+ * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
+ */
+CAResult_t CAInitConnectionManagerMutexVariables();
+
+/**
+ * Terminate mutex.
+ */
+void CATerminateConnectionManagerMutexVariables();
+
+/**
+ * Get request/response message to send.
+ */
+CAData_t* CAGetConnectionManagerMessageData(CAData_t *data);
+
+/**
+ * Start connection manager.
+ */
+void CAStartConnectionManagerService(CMConfigureInfo_t info);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif  // CA_CONNECTIONMANAGER_H_
diff --git a/resource/csdk/connectivity/util/src/camanager/caconnectionmanager.c b/resource/csdk/connectivity/util/src/camanager/caconnectionmanager.c
new file mode 100644 (file)
index 0000000..d426ac3
--- /dev/null
@@ -0,0 +1,166 @@
+/* *****************************************************************
+ *
+ * Copyright 2016 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * 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 "caadapterutils.h"
+#include "cainterface.h"
+#include "camessagehandler.h"
+#include "caremotehandler.h"
+#include "oic_malloc.h"
+#include "oic_string.h"
+#include "octhread.h"
+#include "logger.h"
+#include "caadapterutils.h"
+
+#include "caconnectionmanager.h"
+#include "capolicymanager.h"
+
+#define TAG "OIC_CM"
+
+static oc_mutex g_threadCMConfigureMutex = NULL;
+
+// context for connection manager
+static CAConnectionManagerContext_t g_context = {.sendThreadFunc = NULL,
+                                                 .receivedThreadFunc = NULL,
+                                                 .dataList = NULL};
+
+void CAStartConnectionManagerService(CMConfigureInfo_t info)
+{
+    OIC_LOG(DEBUG, TAG, "CAStartConnectionManagerService");
+
+    oc_mutex_lock(g_threadCMConfigureMutex);
+    CMSetConfigure(info);
+    oc_mutex_unlock(g_threadCMConfigureMutex);
+}
+
+CAData_t* CAGetConnectionManagerMessageData(CAData_t *data)
+{
+    OIC_LOG(DEBUG, TAG, "CAGetConnectionManagerMessageData");
+
+    VERIFY_NON_NULL_RET(data, TAG, "data is null", NULL);
+
+    // TODO
+    // decide specific reqeust/response message
+
+    return data;
+}
+
+CAResult_t CAInitializeConnectionManager(CASendThreadFunc sendThreadFunc,
+                                         CAReceiveThreadFunc receivedThreadFunc)
+{
+    OIC_LOG(DEBUG, TAG, "CAInitializeConnectionManager");
+
+    if (!g_context.sendThreadFunc)
+    {
+        g_context.sendThreadFunc = sendThreadFunc;
+    }
+
+    if (!g_context.receivedThreadFunc)
+    {
+        g_context.receivedThreadFunc = receivedThreadFunc;
+    }
+
+    if (!g_context.dataList)
+    {
+        g_context.dataList = u_arraylist_create();
+    }
+
+    CAResult_t res = CAInitConnectionManagerMutexVariables();
+    if (CA_STATUS_OK != res)
+    {
+        u_arraylist_free(&g_context.dataList);
+        g_context.dataList = NULL;
+        OIC_LOG(ERROR, TAG, "init has failed");
+    }
+    return res;
+}
+
+void CATerminateConnectionManager()
+{
+    OIC_LOG(DEBUG, TAG, "CATerminateConnectionManager");
+
+    if (g_context.dataList)
+    {
+        // TODO
+        // Remove all of management data();
+        u_arraylist_free(&g_context.dataList);
+    }
+    CATerminateConnectionManagerMutexVariables();
+}
+
+CAResult_t CAInitConnectionManagerMutexVariables()
+{
+    if (!g_context.dataListMutex)
+    {
+        g_context.dataListMutex = oc_mutex_new();
+        if (!g_context.dataListMutex)
+        {
+            OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
+            return CA_STATUS_FAILED;
+        }
+    }
+
+    if (!g_context.dataSenderMutex)
+    {
+        g_context.dataSenderMutex = oc_mutex_new();
+        if (!g_context.dataSenderMutex)
+        {
+            OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
+            CATerminateConnectionManagerMutexVariables();
+            return CA_STATUS_FAILED;
+        }
+    }
+
+    if (NULL == g_threadCMConfigureMutex)
+    {
+        g_threadCMConfigureMutex = oc_mutex_new();
+        if (NULL == g_threadCMConfigureMutex)
+        {
+            OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
+            return CA_STATUS_FAILED;
+        }
+    }
+
+    return CA_STATUS_OK;
+}
+
+void CATerminateConnectionManagerMutexVariables()
+{
+    if (g_context.dataListMutex)
+    {
+        oc_mutex_free(g_context.dataListMutex);
+        g_context.dataListMutex = NULL;
+    }
+
+    if (g_context.dataSenderMutex)
+    {
+        oc_mutex_free(g_context.dataSenderMutex);
+        g_context.dataSenderMutex = NULL;
+    }
+
+    if (g_threadCMConfigureMutex)
+    {
+        oc_mutex_free(g_threadCMConfigureMutex);
+        g_threadCMConfigureMutex = NULL;
+    }
+}
diff --git a/resource/csdk/connectivity/util/src/camanager/camanagerutil.c b/resource/csdk/connectivity/util/src/camanager/camanagerutil.c
new file mode 100644 (file)
index 0000000..538d88a
--- /dev/null
@@ -0,0 +1,28 @@
+/* *****************************************************************
+ *
+ * Copyright 2016 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * 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 "logger.h"
+#include "cathreadpool.h"
+#include "uarraylist.h"
+#include "octhread.h"
+
+#include "camanagerutil.h"
+
+#define TAG "OIC_CM_UTIL"
diff --git a/resource/csdk/connectivity/util/src/camanager/camanagerutil.h b/resource/csdk/connectivity/util/src/camanager/camanagerutil.h
new file mode 100644 (file)
index 0000000..bf0191a
--- /dev/null
@@ -0,0 +1,44 @@
+/* *****************************************************************
+ *
+ * Copyright 2016 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * 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.
+ *
+ ******************************************************************/
+
+/**
+ * @file
+ * This file contains common function for connection manager util
+ */
+
+#ifndef CA_MANAGER_UTIL_H_
+#define CA_MANAGER_UTIL_H_
+
+#include "logger.h"
+#include "cathreadpool.h"
+#include "uarraylist.h"
+#include "octhread.h"
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif  // CA_MANAGER_UTIL_H_
diff --git a/resource/csdk/connectivity/util/src/camanager/camessagearbiter.c b/resource/csdk/connectivity/util/src/camanager/camessagearbiter.c
new file mode 100644 (file)
index 0000000..061170b
--- /dev/null
@@ -0,0 +1,25 @@
+/* *****************************************************************
+ *
+ * Copyright 2016 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * 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 "logger.h"
+#include "cathreadpool.h"
+#include "octhread.h"
+
+#define TAG "OIC_CM_ARBITER"
diff --git a/resource/csdk/connectivity/util/src/camanager/camessagearbiter.h b/resource/csdk/connectivity/util/src/camanager/camessagearbiter.h
new file mode 100644 (file)
index 0000000..238362d
--- /dev/null
@@ -0,0 +1,45 @@
+/* *****************************************************************
+ *
+ * Copyright 2016 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * 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.
+ *
+ ******************************************************************/
+
+/**
+ * @file
+ * This file contains common function for connection manager arbiter
+ */
+
+#ifndef CA_CONNECTIONMANAGER_ARBITER_H_
+#define CA_CONNECTIONMANAGER_ARBITER_H_
+
+#include "logger.h"
+#include "cathreadpool.h"
+#include "octhread.h"
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif  // CA_CONNECTIONMANAGER_ARBITER_H_
+
+
diff --git a/resource/csdk/connectivity/util/src/camanager/capolicymanager.c b/resource/csdk/connectivity/util/src/camanager/capolicymanager.c
new file mode 100644 (file)
index 0000000..d9a4c9e
--- /dev/null
@@ -0,0 +1,64 @@
+/* *****************************************************************
+ *
+ * Copyright 2016 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * 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 "cathreadpool.h"
+#include "octhread.h"
+#include "uarraylist.h"
+#include "cacommon.h"
+#include "logger.h"
+
+#include "caconnectionmanager.h"
+#include "capolicymanager.h"
+
+#define TAG "OIC_CM_POLICY"
+
+static CMConfigureInfo_t g_configure = {.addr = NULL,
+                                        .adapter = CA_ADAPTER_IP,
+                                        .level = NORMAL_SPEED};
+
+void CMSetConfigure(CMConfigureInfo_t info)
+{
+    OIC_LOG(DEBUG, TAG, "CMSetConfigurePolicy");
+    OICStrcpy(g_configure.addr, sizeof(g_configure.addr), info.addr);
+    g_configure.adapter = info.adapter;
+    g_configure.level = info.level;
+}
+
+const char* CMGetTargetAddress()
+{
+    OIC_LOG(DEBUG, TAG, "CMGetTargetAddress");
+    return g_configure.addr;
+}
+
+CATransportAdapter_t CMGetAdapterType()
+{
+    OIC_LOG(DEBUG, TAG, "CMGetAdapterType");
+    return g_configure.adapter;
+}
+
+CMSpeedLevel_t CMGetSpeedOfResponseLevel()
+{
+    OIC_LOG(DEBUG, TAG, "CMGetSpeedOfResponseLevel");
+    return g_configure.level;
+}
diff --git a/resource/csdk/connectivity/util/src/camanager/capolicymanager.h b/resource/csdk/connectivity/util/src/camanager/capolicymanager.h
new file mode 100644 (file)
index 0000000..9bef0cf
--- /dev/null
@@ -0,0 +1,71 @@
+/* *****************************************************************
+ *
+ * Copyright 2016 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * 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.
+ *
+ ******************************************************************/
+
+/**
+ * @file
+ * This file contains common function for policy manager
+ */
+
+#ifndef CA_POLICY_MANAGER_H_
+#define CA_POLICY_MANAGER_H_
+
+#include <stdint.h>
+
+#include "cathreadpool.h"
+#include "octhread.h"
+#include "uarraylist.h"
+#include "cacommon.h"
+#include "caconnectionmanager.h"
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/**
+ * Set connection manager configure.
+ * Target address and connectivity priority and speed level can be set in this method.
+ * @param[in] info  configuration data for connection manager policy
+ */
+void CMSetConfigure(CMConfigureInfo_t info);
+
+/**
+ * get target address.
+ * @return address
+ */
+const char* CMGetTargetAddress();
+
+/**
+ * get Adapter Type of current priority.
+ * @return ::CATransportAdapter_t value.
+ */
+CATransportAdapter_t CMGetAdapterType();
+
+/**
+ * get speed of response level.
+ * @return ::CMSpeedLevel_t value.
+ */
+CMSpeedLevel_t CMGetSpeedOfResponseLevel();
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif  // CA_POLICY_MANAGER_H_