Commit e1fd1d0f authored by Phil Coval's avatar Phil Coval

Merge "Merge branch '1.4-rel' (455c1369)"

parents c5283567 38cb6d40
......@@ -508,6 +508,8 @@ def build_tizen(flag, scons_options):
build_options['ES_TARGET_ENROLLEE'] = 'tizen'
build_extra_options = "-f service/easy-setup/sampleapp/enrollee/tizen-sdb/EnrolleeSample/build/tizen/SConscript " + scons_options
call_scons(build_options, build_extra_options)
build_options['MULTIPLE_OWNER'] = 1
call_scons(build_options, build_extra_options)
def build_tizen_secured(flag, scons_options):
......
......@@ -69,7 +69,7 @@ if target_os not in ['windows']:
if target_os in ['darwin', 'ios']:
hue_env.AppendUnique(CPPDEFINES=['_DARWIN_C_SOURCE'])
hue_env.AppendUnique(CXXFLAGS=['-std=c++0x', '-Wall', '-Wextra', '-Werror'])
hue_env.AppendUnique(CXXFLAGS=['-std=c++0x', '-Wall', '-Wextra'])
if hue_env.get('LOGGING'):
hue_env.AppendUnique(CPPDEFINES=['TB_LOG'])
......
......@@ -63,7 +63,7 @@ lifx_env.AppendUnique(CPPPATH=[
if 'g++' in lifx_env.get('CXX'):
lifx_env.AppendUnique(
CXXFLAGS=['-std=c++0x', '-Wall', '-Wextra', '-Werror'])
CXXFLAGS=['-std=c++0x', '-Wall', '-Wextra'])
libmpm = lifx_env.get('BUILD_DIR') + 'libmpmcommon.a'
lifx_env['LINKFLAGS'] = maskFlags(env['LINKFLAGS'])
......
......@@ -65,7 +65,7 @@ lyric_env.AppendUnique(CPPPATH=[
if 'g++' in lyric_env.get('CXX'):
lyric_env.AppendUnique(
CXXFLAGS=['-std=c++0x', '-Wall', '-Wextra', '-Werror'])
CXXFLAGS=['-std=c++0x', '-Wall', '-Wextra'])
libmpm = lyric_env.get('BUILD_DIR') + 'libmpmcommon.a'
lyric_env['LINKFLAGS'] = maskFlags(env['LINKFLAGS'])
......
......@@ -70,7 +70,7 @@ if target_os in ['darwin', 'ios']:
if 'g++' in nest_env.get('CXX'):
nest_env.AppendUnique(
CXXFLAGS=['-std=c++0x', '-Wall', '-Wextra', '-Werror'])
CXXFLAGS=['-std=c++0x', '-Wall', '-Wextra'])
if nest_env.get('LOGGING'):
nest_env.AppendUnique(CPPDEFINES=['TB_LOG'])
......
......@@ -59,7 +59,7 @@ if target_os not in ['windows']:
if target_os in ['darwin', 'ios']:
env.AppendUnique(CPPDEFINES=['_DARWIN_C_SOURCE'])
env.AppendUnique(CXXFLAGS=['-std=c++0x', '-Wall', '-Wextra', '-Werror'])
env.AppendUnique(CXXFLAGS=['-std=c++0x', '-Wall', '-Wextra'])
env.PrependUnique(LIBS=['zigbee_wrapper'])
......
......@@ -53,7 +53,7 @@ if target_os not in ['windows']:
if target_os in ['darwin', 'ios']:
env.AppendUnique(CPPDEFINES=['_DARWIN_C_SOURCE'])
env.AppendUnique(CXXFLAGS=['-std=c++0x', '-Wall', '-Wextra', '-Werror'])
env.AppendUnique(CXXFLAGS=['-std=c++0x', '-Wall', '-Wextra'])
env.PrependUnique(LIBS=['telegesis_wrapper'])
#####################################################################
......
......@@ -149,6 +149,7 @@ env.AppendUnique(CPPPATH=[
os.path.join(Dir('.').abspath, 'ocevent', 'include'),
os.path.join(Dir('.').abspath, 'oic_platform', 'include'),
os.path.join(Dir('.').abspath, 'octimer', 'include'),
os.path.join(Dir('.').abspath, 'oc_refcounter', 'include'),
'#/extlibs/mbedtls/mbedtls/include'
])
......@@ -176,7 +177,8 @@ common_src = [
'oic_malloc/src/oic_malloc.c',
'oic_time/src/oic_time.c',
'ocrandom/src/ocrandom.c',
'oic_platform/src/oic_platform.c'
'oic_platform/src/oic_platform.c',
'oc_refcounter/src/oc_refcounter.c'
]
if env['POSIX_SUPPORTED']:
......
/******************************************************************
*
* Copyright 2018 Kistler Group 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.
*
******************************************************************/
#ifndef OC_REFCOUNTER_H_
#define OC_REFCOUNTER_H_
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus
typedef struct oc_refcounter_t *oc_refcounter;
/**
* Desctructor of a data of a refcounter and it is called when a counter of thet refcounter reach 0.
*/
typedef void (*oc_refcounter_dtor_data_func)(void* data);
/**
* Creates a new refcounter of a data. Ownership of the data is transferred to the refcounter.
* For deallocates the data use oc_refcounter_dec, otherwise it can cause crash of the application.
*
* @param data - Data which will be owned by refcounter
* @param dtor - Destructor of a data
*
* @return Poiner to newly created refcounter, NULL on allocation failure.
*/
oc_refcounter oc_refcounter_create(void* data, oc_refcounter_dtor_data_func dtor);
/**
* Increments atommically reference count of a refcounter
*
* @param ref - A pointer to the refcounter
*
* @return returns the ref
*/
oc_refcounter oc_refcounter_inc(oc_refcounter ref);
/**
* Decrements atommically a reference count of a refcounter. If the count of the refcounter
* reach 0, it's call destructor of a data and the refcounter.
*
* @param ref - A pointer to the refcounter
*
* @return
* on count == 0, a null pointer is returned
* otherwise returns the refcounter
*/
oc_refcounter oc_refcounter_dec(oc_refcounter ref);
/**
* Gets a current reference count of the refcount
*
* @param ref - A pointer to the refcounter
*
* @return returns a current reference count of the block
*/
int32_t oc_refcounter_get_count(oc_refcounter ref);
/**
* Gets a data of the refcount
*
* @param ref - A pointer to the refcounter
*
* @return returns a data of the refcounter
*/
void* oc_refcounter_get_data(oc_refcounter ref);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // OC_REFCOUNTER_H_
/******************************************************************
*
* Copyright 2018 Kistler Group 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 "oc_refcounter.h"
#include "oic_malloc.h"
#include "ocatomic.h"
#include "iotivity_debug.h"
// Enable extra debug logging for malloc. Comment out to disable
#ifdef ENABLE_REFCOUNTER_DEBUG
#include "experimental/logger.h"
#define TAG "OC_REFCOUNT"
#endif
typedef struct oc_refcounter_t
{
/* count of the references*/
int32_t count;
/* data owned by refcount */
void* data;
/* data destructor*/
oc_refcounter_dtor_data_func dtor;
} oc_refcounter_t;
oc_refcounter oc_refcounter_create(void* data, oc_refcounter_dtor_data_func dtor)
{
oc_refcounter ref = (oc_refcounter)OICMalloc(sizeof(*ref));
if (ref == NULL)
{
return ref;
}
ref->count = 1;
#ifdef ENABLE_REFCOUNTER_DEBUG
OIC_LOG_V(INFO, TAG, "oc_refcounter_create: ref=%p count=%d", ref, 1);
#endif
ref->data = data;
ref->dtor = dtor;
return ref;
}
oc_refcounter oc_refcounter_inc(oc_refcounter ref)
{
if (ref == NULL)
{
return NULL;
}
OC_VERIFY(ref->count > 0);
int32_t count = oc_atomic_increment(&ref->count);
#ifdef ENABLE_REFCOUNTER_DEBUG
OIC_LOG_V(INFO, TAG, "oc_refcounter_inc: ref=%p count=%d", ref, count);
#else
(void) count;
#endif
return ref;
}
oc_refcounter oc_refcounter_dec(oc_refcounter ref)
{
if (ref == NULL)
{
return NULL;
}
OC_VERIFY(ref->count > 0);
int32_t count = oc_atomic_decrement(&ref->count);
#ifdef ENABLE_REFCOUNTER_DEBUG
OIC_LOG_V(INFO, TAG, "oc_refcounter_dec: ref=%p count=%d", ref, count);
#endif
if (count == 0)
{
if (ref->dtor)
{
ref->dtor(ref->data);
}
OICFree(ref);
return NULL;
}
return ref;
}
int32_t oc_refcounter_get_count(oc_refcounter ref)
{
if (ref == NULL)
{
return -1;
}
OC_VERIFY(ref->count > 0);
return ref->count;
}
void* oc_refcounter_get_data(oc_refcounter ref)
{
if (ref == NULL)
{
return NULL;
}
OC_VERIFY(ref->count > 0);
return ref->data;
}
\ No newline at end of file
#******************************************************************
#
# Copyright 2018 Kistler Group 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.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
import os
import os.path
from tools.scons.RunTest import run_test
Import('test_env')
# SConscript file for Local PKI google tests
refcountertest_env = test_env.Clone()
target_os = refcountertest_env.get('TARGET_OS')
######################################################################
# Build flags
######################################################################
refcountertest_env.PrependUnique(CPPPATH=['../include'])
refcountertest_env.AppendUnique(LIBPATH=[
os.path.join(refcountertest_env.get('BUILD_DIR'), 'resource', 'c_common')
])
refcountertest_env.PrependUnique(LIBS=['c_common'])
if refcountertest_env.get('LOGGING'):
refcountertest_env.AppendUnique(CPPDEFINES=['TB_LOG'])
#
######################################################################
# Source files and Targets
######################################################################
refcountertests = refcountertest_env.Program('refcountertests',
['linux/oc_refcounter_tests.cpp'])
Alias("test", [refcountertests])
refcountertest_env.AppendTarget('test')
if refcountertest_env.get('TEST') == '1':
if target_os in ['linux', 'windows']:
run_test(refcountertest_env, 'resource_ccommon_refcounter_test.memcheck',
'resource/c_common/oc_refcounter/test/refcountertests')
//******************************************************************
//
// Copyright 2018 Kistler Group 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 "iotivity_config.h"
extern "C" {
#include "oc_refcounter.h"
}
#include <gtest/gtest.h>
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <stdint.h>
using namespace std;
//-----------------------------------------------------------------------------
// Tests
//-----------------------------------------------------------------------------
TEST(RefCounterTests, OCRefCounterCreateNormalNonDtor)
{
oc_refcounter result = oc_refcounter_create(NULL, NULL);
EXPECT_TRUE(result != NULL);
result = oc_refcounter_dec(result);
EXPECT_TRUE(result == NULL);
}
static void dtorVal(int* v)
{
*v = 0;
}
TEST(RefCounterTests, OCRefCounterIncDecWithDtor)
{
int val = 1;
oc_refcounter result = oc_refcounter_create(&val, (oc_refcounter_dtor_data_func) dtorVal);
EXPECT_TRUE(result != NULL);
EXPECT_EQ(1, oc_refcounter_get_count(result));
result = oc_refcounter_inc(result);
EXPECT_TRUE(result != NULL);
EXPECT_EQ(2, oc_refcounter_get_count(result));
result = oc_refcounter_dec(result);
EXPECT_TRUE(result != NULL);
EXPECT_EQ(1, oc_refcounter_get_count(result));
result = oc_refcounter_dec(result);
EXPECT_TRUE(result == NULL);
EXPECT_EQ(0, val);
}
\ No newline at end of file
......@@ -38,6 +38,7 @@ SConscript(exports={'test_env': common_test_env},
'../oic_time/test',
'../ocrandom/test',
'../ocevent/test',
'../oc_refcounter/test',
])
if target_os == 'windows':
SConscript('../windows/test/SConscript', exports={'test_env': common_test_env})
......@@ -113,6 +113,15 @@ bool u_arraylist_add(u_arraylist_t *list, void *data);
*/
void *u_arraylist_remove(u_arraylist_t *list, size_t index);
/**
* Swap elements in the array list.
* @param[in] list pointer of array list.
* @param[in] index1 first index of array list.
* @param[in] index2 second index of array list.
* @return true if success, false otherwise.
*/
bool u_arraylist_swap(u_arraylist_t *list, size_t index1, size_t index2);
/**
* Returns the length of the array list.
* @param[in] list pointer of array list.
......
......@@ -242,3 +242,15 @@ void u_arraylist_destroy(u_arraylist_t *list)
}
(void)u_arraylist_free(&list);
}
bool u_arraylist_swap(u_arraylist_t *list, size_t index1, size_t index2)
{
if (!list || index1 >= list->length || index2 >= list->length)
{
return false;
}
void* tmp = list->data[index1];
list->data[index1] = list->data[index2];
list->data[index2] = tmp;
return true;
}
\ No newline at end of file
......@@ -83,7 +83,6 @@ typedef struct CATCPSessionInfo_t
CATCPConnectionState_t state; /**< current tcp session state */
CACSMExchangeState_t CSMState; /**< Capability and Setting Message shared status */
bool isClient; /**< Host Mode of Operation. */
struct CATCPSessionInfo_t *next; /**< Linked list; for multiple session list. */
} CATCPSessionInfo_t;
/**
......
......@@ -32,6 +32,7 @@
#include "catcpadapter.h"
#include "cathreadpool.h"
#include "uarraylist.h"
#include "oc_refcounter.h"
#ifdef __cplusplus
extern "C"
......@@ -150,10 +151,10 @@ CASocketFd_t CAConnectTCPSession(const CAEndpoint_t *endpoint);
/**
* Disconnect from TCP Server.
*
* @param[in] removedData removed session information from list.
* @param[in] session remove session information from list.
* @return ::CA_STATUS_OK or Appropriate error code.
*/
CAResult_t CADisconnectTCPSession(CATCPSessionInfo_t *removedData);
CAResult_t CADisconnectTCPSession(CATCPSessionInfo_t *session);
/**
* Disconnect all connection from TCP Server.
......@@ -161,12 +162,13 @@ CAResult_t CADisconnectTCPSession(CATCPSessionInfo_t *removedData);
void CATCPDisconnectAll(void);
/**
* Get TCP connection information from list.
* Get TCP connection information from list. After using, a reference count
* of information must be decremented by oc_refcounter_dec.
*
* @param[in] endpoint remote endpoint information.
* @return TCP Session Information structure.
* @return refcounter of TCP Session Information reference(CATCPSessionInfo_t) .
*/
CATCPSessionInfo_t *CAGetTCPSessionInfoFromEndpoint(const CAEndpoint_t *endpoint);
oc_refcounter CAGetTCPSessionInfoRefCountedFromEndpoint(const CAEndpoint_t *endpoint);
/**
* Get total length from CoAP over TCP header.
......
......@@ -40,6 +40,7 @@
#include "caremotehandler.h"
#include "experimental/logger.h"
#include "oic_malloc.h"
#include "oc_refcounter.h"
#ifdef __WITH_TLS__
#include "ca_adapter_net_ssl.h"
#endif
......@@ -173,7 +174,8 @@ void CATCPPacketReceivedCB(const CASecureEndpoint_t *sep, const void *data,
size_t bufferLen = dataLength;
//get remote device information from file descriptor.
CATCPSessionInfo_t *svritem = CAGetTCPSessionInfoFromEndpoint(&sep->endpoint);
oc_refcounter ref = CAGetTCPSessionInfoRefCountedFromEndpoint(&sep->endpoint);
CATCPSessionInfo_t *svritem = (CATCPSessionInfo_t *) oc_refcounter_get_data(ref);
if (!svritem)
{
OIC_LOG(ERROR, TAG, "there is no connection information in list");
......@@ -181,6 +183,7 @@ void CATCPPacketReceivedCB(const CASecureEndpoint_t *sep, const void *data,
}
if (UNKNOWN == svritem->protocol)
{
oc_refcounter_dec(ref);
OIC_LOG(ERROR, TAG, "invalid protocol type");
return;
}
......@@ -192,6 +195,7 @@ void CATCPPacketReceivedCB(const CASecureEndpoint_t *sep, const void *data,
if (CA_STATUS_OK != res)
{
OIC_LOG_V(ERROR, TAG, "CAConstructCoAP return error : %d", res);
oc_refcounter_dec(ref);
return;
}
......@@ -210,6 +214,7 @@ void CATCPPacketReceivedCB(const CASecureEndpoint_t *sep, const void *data,
svritem->totalLen - svritem->len);
}
}
oc_refcounter_dec(ref);
}
#ifdef __WITH_TLS__
......
......@@ -63,6 +63,8 @@
#include "octhread.h"
#include "oic_malloc.h"
#include "oic_string.h"
#include "oc_refcounter.h"
#include "uarraylist.h"
#include <coap/pdu.h>
#include <coap/utlist.h>
......@@ -116,7 +118,7 @@ static CATCPConnectionHandleCallback g_connectionCallback = NULL;
/**
* Store the connected TCP session information.
*/
static CATCPSessionInfo_t *g_sessionList = NULL;
static u_arraylist_t *s_sessionList = NULL;
static CAResult_t CATCPCreateMutex(void);
static void CATCPDestroyMutex(void);
......@@ -124,11 +126,11 @@ static CAResult_t CATCPCreateCond(void);
static void CATCPDestroyCond(void);
static CASocketFd_t CACreateAcceptSocket(int family, CASocket_t *sock);
static void CAAcceptConnection(CATransportFlags_t flag, CASocket_t *sock);
static void CAFindReadyMessage(void);
static void CAFindReadyMessage(u_arraylist_t* sessionList);
#if !defined(WSA_WAIT_EVENT_0)
static void CASelectReturned(fd_set *readFds);
static void CASelectReturned(u_arraylist_t* sessionList, fd_set *readFds);
#else
static void CASocketEventReturned(CASocketFd_t socket, long networkEvents);
static void CASocketEventReturned(u_arraylist_t* sessionList, CASocketFd_t socket, long networkEvents);
#endif
static CAResult_t CAReceiveMessage(CATCPSessionInfo_t *svritem);
static void CAReceiveHandler(void *data);
......@@ -179,7 +181,7 @@ static CAResult_t CATCPCreateMutex(void)
{
if (!g_mutexObjectList)
{
g_mutexObjectList = oc_mutex_new_recursive();
g_mutexObjectList = oc_mutex_new();
if (!g_mutexObjectList)
{
OIC_LOG(ERROR, TAG, "Failed to created mutex!");
......@@ -213,16 +215,59 @@ static CAResult_t CATCPCreateCond(void)
return CA_STATUS_OK;
}
static CATCPSessionInfo_t* session_list_get(u_arraylist_t* sessionList, size_t index)
{
oc_refcounter ref = (oc_refcounter)u_arraylist_get(sessionList, index);
return (CATCPSessionInfo_t*) oc_refcounter_get_data(ref);
}
static void CARemoveSession(CATCPSessionInfo_t *session)
{
oc_refcounter ref = NULL;
oc_mutex_lock(g_mutexObjectList);
size_t length = u_arraylist_length(s_sessionList);
for (size_t i = 0; i < length; ++i)
{
oc_refcounter tmp = (oc_refcounter) u_arraylist_get(s_sessionList, i);
if (oc_refcounter_get_data(tmp) == session) {
//swap last element with current position and remove last element
u_arraylist_swap(s_sessionList, i, length-1);
ref = (oc_refcounter) u_arraylist_remove(s_sessionList, length-1);
break;
}
}
oc_mutex_unlock(g_mutexObjectList);
if (ref)
{
oc_refcounter_dec(ref);
}