Skip to content

Commit

Permalink
Strip names and add a simple test
Browse files Browse the repository at this point in the history
  • Loading branch information
sagar-apple committed Mar 6, 2020
1 parent 7517213 commit 59ca0be
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 58 deletions.
7 changes: 4 additions & 3 deletions src/system/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Test_Dir = tests

.PHONY: all clean run_tests

all: libsystem.a run_tests
all: libSystemLayer.a run_tests

include $(TOP_DIR)/.yams/cpp_rules.min

Expand All @@ -18,12 +18,13 @@ Module_Test_Includes = $(Module_Includes)
CPP_Files = \
SystemMutex.cpp \
SystemError.cpp \
SystemClock.cpp

libsystem.a: $(CPP_Objects)
libSystemLayer.a: $(CPP_Objects)
ar rvs $@ $^
@echo "LINK => $@"

tests_FLAGS = -lpthread

clean: my_clean
@rm -f libsystem.a *.gcda *.gcno *.gcov
@rm -f libSystemLayer.a *.gcda *.gcno *.gcov
72 changes: 37 additions & 35 deletions src/system/SystemClock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,40 @@
#endif // __STDC_CONSTANT_MACROS

#include <stdint.h>
#include <stdlib.h>

#include <SystemLayer/SystemConfig.h>
#include <SystemConfig.h>

#if !WEAVE_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME
#if !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME

#include <SystemLayer/SystemClock.h>
#include <Weave/Support/CodeUtils.h>
#include <SystemLayer/SystemError.h>
#include <SystemClock.h>
// #include <support/CodeUtils.h>
#include <SystemError.h>
#include "SystemLayerPrivate.h"

#if WEAVE_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS
#if CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS
#include <time.h>
#if !(HAVE_CLOCK_GETTIME)
#include <sys/time.h>
#endif
#include <errno.h>
#endif // WEAVE_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS
#endif // CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS

#if WEAVE_SYSTEM_CONFIG_USE_LWIP
#if CHIP_SYSTEM_CONFIG_USE_LWIP
#include <lwip/sys.h>
#endif // WEAVE_SYSTEM_CONFIG_USE_LWIP
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

namespace nl {
namespace Weave {
namespace chip {
namespace System {
namespace Platform {
namespace Layer {

// -------------------- Default Get/SetClock Functions for POSIX Systems --------------------

#if WEAVE_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS
#if CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS

#if !HAVE_CLOCK_GETTIME && !HAVE_GETTIMEOFDAY
#error "WEAVE_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS requires either clock_gettime() or gettimeofday()"
#error "CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS requires either clock_gettime() or gettimeofday()"
#endif

#if HAVE_CLOCK_GETTIME
Expand All @@ -85,12 +85,14 @@ uint64_t GetClock_Monotonic(void)
#if HAVE_CLOCK_GETTIME
struct timespec ts;
int res = clock_gettime(MONOTONIC_CLOCK_ID, &ts);
VerifyOrDie(res == 0);
// TODO: use assert library when available
if (res) { abort(); }
return (ts.tv_sec * UINT64_C(1000000)) + (ts.tv_nsec / 1000);
#else // HAVE_CLOCK_GETTIME
struct timeval tv;
int res = gettimeofday(&tv, NULL);
VerifyOrDie(res == 0);
// TODO: use assert library when available
if (res) { abort(); }
return (tv.tv_sec * UINT64_C(1000000)) + tv.tv_usec;
#endif // HAVE_CLOCK_GETTIME
}
Expand All @@ -105,7 +107,8 @@ uint64_t GetClock_MonotonicHiRes(void)
#if HAVE_CLOCK_GETTIME && defined(MONOTONIC_RAW_CLOCK_ID)
struct timespec ts;
int res = clock_gettime(MONOTONIC_RAW_CLOCK_ID, &ts);
VerifyOrDie(res == 0);
// TODO: use assert library when available
if (res) { abort(); }
return (ts.tv_sec * UINT64_C(1000000)) + (ts.tv_nsec / 1000);
#else // HAVE_CLOCK_GETTIME
return GetClock_Monotonic();
Expand All @@ -121,25 +124,25 @@ Error GetClock_RealTime(uint64_t & curTime)
{
return MapErrorPOSIX(errno);
}
if (ts.tv_sec < WEAVE_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD)
if (ts.tv_sec < CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD)
{
return WEAVE_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED;
return CHIP_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED;
}
curTime = (ts.tv_sec * UINT64_C(1000000)) + (ts.tv_nsec / 1000);
return WEAVE_SYSTEM_NO_ERROR;
return CHIP_SYSTEM_NO_ERROR;
#else // HAVE_CLOCK_GETTIME
struct timeval tv;
int res = gettimeofday(&tv, NULL);
if (res != 0)
{
return MapErrorPOSIX(errno);
}
if (tv.tv_sec < WEAVE_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD)
if (tv.tv_sec < CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD)
{
return WEAVE_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED;
return CHIP_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED;
}
curTime = (tv.tv_sec * UINT64_C(1000000)) + tv.tv_usec;
return WEAVE_SYSTEM_NO_ERROR;
return CHIP_SYSTEM_NO_ERROR;
#endif // HAVE_CLOCK_GETTIME
}

Expand All @@ -161,30 +164,30 @@ Error SetClock_RealTime(uint64_t newCurTime)
int res = clock_settime(CLOCK_REALTIME, &ts);
if (res != 0)
{
return (errno == EPERM) ? WEAVE_SYSTEM_ERROR_ACCESS_DENIED : MapErrorPOSIX(errno);
return (errno == EPERM) ? CHIP_SYSTEM_ERROR_ACCESS_DENIED : MapErrorPOSIX(errno);
}
return WEAVE_SYSTEM_NO_ERROR;
return CHIP_SYSTEM_NO_ERROR;
#else // HAVE_CLOCK_SETTIME
struct timeval tv;
tv.tv_sec = static_cast<time_t>(newCurTime / UINT64_C(1000000));
tv.tv_usec = static_cast<long>(newCurTime % UINT64_C(1000000));
int res = settimeofday(&tv, NULL);
if (res != 0)
{
return (errno == EPERM) ? WEAVE_SYSTEM_ERROR_ACCESS_DENIED : MapErrorPOSIX(errno);
return (errno == EPERM) ? CHIP_SYSTEM_ERROR_ACCESS_DENIED : MapErrorPOSIX(errno);
}
return WEAVE_SYSTEM_NO_ERROR;
return CHIP_SYSTEM_NO_ERROR;
#endif // HAVE_CLOCK_SETTIME
}

#endif // HAVE_CLOCK_SETTIME || HAVE_SETTIMEOFDAY

#endif // WEAVE_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS
#endif // CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS


// -------------------- Default Get/SetClock Functions for LwIP Systems --------------------

#if WEAVE_SYSTEM_CONFIG_USE_LWIP_MONOTONIC_TIME
#if CHIP_SYSTEM_CONFIG_USE_LWIP_MONOTONIC_TIME

uint64_t GetClock_Monotonic(void)
{
Expand Down Expand Up @@ -238,25 +241,24 @@ uint64_t GetClock_MonotonicHiRes(void)

Error GetClock_RealTime(uint64_t & curTime)
{
return WEAVE_SYSTEM_ERROR_NOT_SUPPORTED;
return CHIP_SYSTEM_ERROR_NOT_SUPPORTED;
}

Error GetClock_RealTimeMS(uint64_t & curTime)
{
return WEAVE_SYSTEM_ERROR_NOT_SUPPORTED;
return CHIP_SYSTEM_ERROR_NOT_SUPPORTED;
}

Error SetClock_RealTime(uint64_t newCurTime)
{
return WEAVE_SYSTEM_ERROR_NOT_SUPPORTED;
return CHIP_SYSTEM_ERROR_NOT_SUPPORTED;
}

#endif // WEAVE_SYSTEM_CONFIG_USE_LWIP_MONOTONIC_TIME
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP_MONOTONIC_TIME

} // namespace Layer
} // namespace Platform
} // namespace System
} // namespace Weave
} // namespace nl
} // namespace chip

#endif // WEAVE_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME
#endif // CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME
38 changes: 18 additions & 20 deletions src/system/SystemClock.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@
#define SYSTEMTIME_H

// Include configuration headers
#include <SystemLayer/SystemConfig.h>
#include <SystemConfig.h>

// Include dependent headers
#include <Weave/Support/NLDLLUtil.h>
#include <support/DLLUtil.h>

#include <SystemLayer/SystemError.h>
#include <SystemError.h>


namespace nl {
namespace Weave {
namespace chip {
namespace System {

enum {
Expand Down Expand Up @@ -130,12 +129,12 @@ extern uint64_t GetClock_MonotonicHiRes(void);
* rate of least at whole seconds (values of 1,000,000), but may tick faster.
*
* On those platforms that are capable of tracking real time, GetClock_RealTime() must return the
* error WEAVE_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED whenever the system is unsynchronized with real time.
* error CHIP_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED whenever the system is unsynchronized with real time.
*
* Platforms that are incapable of tracking real time should not implement the GetClock_RealTime()
* function, thereby forcing link-time failures of features that depend on access to real time.
* Alternatively, such platforms may supply an implementation of GetClock_RealTime() that returns
* the error WEAVE_SYSTEM_ERROR_NOT_SUPPORTED.
* the error CHIP_SYSTEM_ERROR_NOT_SUPPORTED.
*
* This function is expected to be thread-safe on any platform that employs threading.
*
Expand All @@ -145,11 +144,11 @@ extern uint64_t GetClock_MonotonicHiRes(void);
*
* @param[out] curTime The current time, expressed as Unix time scaled to microseconds.
*
* @retval #WEAVE_SYSTEM_NO_ERROR If the method succeeded.
* @retval #WEAVE_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED
* @retval #CHIP_SYSTEM_NO_ERROR If the method succeeded.
* @retval #CHIP_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED
* If the platform is capable of tracking real time, but is
* is currently unsynchronized.
* @retval #WEAVE_SYSTEM_ERROR_NOT_SUPPORTED
* @retval #CHIP_SYSTEM_ERROR_NOT_SUPPORTED
* If the platform is incapable of tracking real time.
*/
extern Error GetClock_RealTime(uint64_t & curTime);
Expand All @@ -170,11 +169,11 @@ extern Error GetClock_RealTime(uint64_t & curTime);
*
* @param[out] curTime The current time, expressed as Unix time scaled to milliseconds.
*
* @retval #WEAVE_SYSTEM_NO_ERROR If the method succeeded.
* @retval #WEAVE_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED
* @retval #CHIP_SYSTEM_NO_ERROR If the method succeeded.
* @retval #CHIP_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED
* If the platform is capable of tracking real time, but is
* is currently unsynchronized.
* @retval #WEAVE_SYSTEM_ERROR_NOT_SUPPORTED
* @retval #CHIP_SYSTEM_ERROR_NOT_SUPPORTED
* If the platform is incapable of tracking real time.
*/
extern Error GetClock_RealTimeMS(uint64_t & curTimeMS);
Expand All @@ -190,13 +189,13 @@ extern Error GetClock_RealTimeMS(uint64_t & curTimeMS);
* seconds.
*
* On platforms that support tracking real time, the SetClock_RealTime() function must return the error
* WEAVE_SYSTEM_ERROR_ACCESS_DENIED if the calling application does not have the privilege to set the
* CHIP_SYSTEM_ERROR_ACCESS_DENIED if the calling application does not have the privilege to set the
* current time.
*
* Platforms that are incapable of tracking real time, or do not offer the ability to set real time,
* should not implement the SetClock_RealTime() function, thereby forcing link-time failures of features
* that depend on setting real time. Alternatively, such platforms may supply an implementation of
* SetClock_RealTime() that returns the error WEAVE_SYSTEM_ERROR_NOT_SUPPORTED.
* SetClock_RealTime() that returns the error CHIP_SYSTEM_ERROR_NOT_SUPPORTED.
*
* This function is expected to be thread-safe on any platform that employs threading.
*
Expand All @@ -206,10 +205,10 @@ extern Error GetClock_RealTimeMS(uint64_t & curTimeMS);
*
* @param[in] newCurTime The new current time, expressed as Unix time scaled to microseconds.
*
* @retval #WEAVE_SYSTEM_NO_ERROR If the method succeeded.
* @retval #WEAVE_SYSTEM_ERROR_NOT_SUPPORTED
* @retval #CHIP_SYSTEM_NO_ERROR If the method succeeded.
* @retval #CHIP_SYSTEM_ERROR_NOT_SUPPORTED
* If the platform is incapable of tracking real time.
* @retval #WEAVE_SYSTEM_ERROR_ACCESS_DENIED
* @retval #CHIP_SYSTEM_ERROR_ACCESS_DENIED
* If the calling application does not have the privilege to set the
* current time.
*/
Expand All @@ -218,7 +217,6 @@ extern Error SetClock_RealTime(uint64_t newCurTime);
} // namespace Layer
} // namespace Platform
} // namespace System
} // namespace Weave
} // namespace nl
} // namespace chip

#endif // SYSTEMTIME_H
16 changes: 16 additions & 0 deletions src/system/tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@
#include <string.h>
#include <unistd.h>

#include "SystemClock.cpp"
#include "SystemMutex.cpp"

using namespace chip::System;

namespace chip {
namespace System {
Error MapErrorPOSIX(int aError) {
return (aError == 0 ? CHIP_SYSTEM_NO_ERROR : aError);
}
} // namespace System
} // namespace chip

void SystemMutex_test() {
printf("---Running Test--- %s\n", __FUNCTION__);
Mutex mLock;
Expand All @@ -19,8 +28,15 @@ void SystemMutex_test() {
mLock.Unlock();
}

void SystemClock_basic_test() {
printf("---Running Test--- %s\n", __FUNCTION__);
uint64_t time = Platform::Layer::GetClock_Monotonic();
assert(time);
}

int main() {
printf("---Running Test--- tests from %s\n", __FILE__);
SystemMutex_test();
SystemClock_basic_test();
return 0;
}

0 comments on commit 59ca0be

Please sign in to comment.