diff options
Diffstat (limited to 'service/native/framework_unified/client/NS_UtilityCenter')
9 files changed, 1467 insertions, 0 deletions
diff --git a/service/native/framework_unified/client/NS_UtilityCenter/cfg/depends.mk b/service/native/framework_unified/client/NS_UtilityCenter/cfg/depends.mk new file mode 100755 index 0000000..93b9f97 --- /dev/null +++ b/service/native/framework_unified/client/NS_UtilityCenter/cfg/depends.mk @@ -0,0 +1,25 @@ +# +# @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION. +# +# 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. +# + +# +# Standard Dependency Makefile Version 2.0 +# +# Dependency file dictates not only what other modules the project is dependent on, but +# also where to get that dependent element which logically includes versioning or +# baselining information + +# Utilizing Base Domain Dependency File. Can override at any point +include $(PRJ_ROOT)/../NativeServices/cfg/depends.mk diff --git a/service/native/framework_unified/client/NS_UtilityCenter/include/ns_utility_if_internal.h b/service/native/framework_unified/client/NS_UtilityCenter/include/ns_utility_if_internal.h new file mode 100755 index 0000000..fc0b761 --- /dev/null +++ b/service/native/framework_unified/client/NS_UtilityCenter/include/ns_utility_if_internal.h @@ -0,0 +1,193 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION. + * + * 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. + */ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// \brief This file contains declaration of common APIs for NS_UtilityCenter. +/// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/** + * @file ns_utility_if_internal.h + */ + +#ifndef FRAMEWORK_UNIFIED_CLIENT_NS_UTILITYCENTER_INCLUDE_NS_UTILITY_IF_INTERNAL_H_ +#define FRAMEWORK_UNIFIED_CLIENT_NS_UTILITYCENTER_INCLUDE_NS_UTILITY_IF_INTERNAL_H_ + +#include <native_service/frameworkunified_types.h> + +/** @addtogroup BaseSystem + * @{ + */ +/** @addtogroup native_service + * @ingroup BaseSystem + * @{ + */ +/** @addtogroup framework_unified + * @ingroup native_service + * @{ + */ + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup NS_UtilityCenter +/// \~english @par Brief +/// Get the number of leading zeros in a given 16bit value. +/// \~english @param [in] f_ui_val +/// UI_16 - the value to be counted +/// \~english @retval the number of leading zeros +/// +/// \~english @par Prerequisite +/// - None +/// \~english @par Change of internal state +/// - None +/// \~english @par Conditions of processing failure +/// - None +/// \~english @par Classification +/// Public +/// \~english @par Type +/// Sync Only +/// \~english @par Detail +/// The function returns the number of leading zeros in a given 16bit value. +/// \~english @see NS_CountLeadingZeros_32Bit NS_CountLeadingZeros_64Bit +//////////////////////////////////////////////////////////////////////////////////////////////// +SI_16 NS_CountLeadingZeros_16Bit(UI_16 f_ui_val); // NOLINT (readability/nolint) + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup NS_UtilityCenter +/// \~english @par Brief +/// Get the number of leading zeros in a given 32bit value. +/// \~english @param [in] f_ui_val +/// UI_32 - the value to be counted +/// \~english @retval the number of leading zeros +/// +/// \~english @par Prerequisite +/// - None +/// \~english @par Change of internal state +/// - None +/// \~english @par Conditions of processing failure +/// - None +/// \~english @par Classification +/// Public +/// \~english @par Type +/// Sync Only +/// \~english @par Detail +/// The function returns the number of leading zeros in a given 32bit value. +/// \~english @see NS_CountLeadingZeros_16Bit NS_CountLeadingZeros_64Bit +//////////////////////////////////////////////////////////////////////////////////////////////// +SI_16 NS_CountLeadingZeros_32Bit(UI_32 f_ui_val); // NOLINT (readability/nolint) + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup NS_UtilityCenter +/// \~english @par Brief +/// Get the number of leading zeros in a given 64bit value. +/// \~english @param [in] f_ui_val +/// UI_64 - the value to be counted +/// \~english @retval the number of leading zeros +/// +/// \~english @par Prerequisite +/// - None +/// \~english @par Change of internal state +/// - None +/// \~english @par Conditions of processing failure +/// - None +/// \~english @par Classification +/// Public +/// \~english @par Type +/// Sync Only +/// \~english @par Detail +/// The function returns the number of leading zeros in a given 64bit value. +/// \~english @see NS_CountLeadingZeros_16Bit NS_CountLeadingZeros_32Bit +//////////////////////////////////////////////////////////////////////////////////////////////// +SI_16 NS_CountLeadingZeros_64Bit(UI_64 f_ui_val); // NOLINT (readability/nolint) + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup NS_UtilityCenter +/// \~english @par Brief +/// Get the number of trailing zeros in a given 16bit value. +/// \~english @param [in] f_ui_val +/// UI_16 - the value to be counted +/// \~english @retval the number of trailing zeros +/// +/// \~english @par Prerequisite +/// - None +/// \~english @par Change of internal state +/// - None +/// \~english @par Conditions of processing failure +/// - None +/// \~english @par Classification +/// Public +/// \~english @par Type +/// Sync Only +/// \~english @par Detail +/// The function returns the number of trailing zeros in a given 16bit value. +/// \~english @see NS_CountTrailingZeros_32Bit NS_CountTrailingZeros_64Bit +//////////////////////////////////////////////////////////////////////////////////////////////// +SI_16 NS_CountTrailingZeros_16Bit(UI_16 f_ui_val); // NOLINT (readability/nolint) + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup NS_UtilityCenter +/// \~english @par Brief +/// Get the number of trailing zeros in a given 32bit value. +/// \~english @param [in] f_ui_val +/// UI_32 - the value to be counted +/// \~english @retval the number of trailing zeros +/// +/// \~english @par Prerequisite +/// - None +/// \~english @par Change of internal state +/// - None +/// \~english @par Conditions of processing failure +/// - None +/// \~english @par Classification +/// Public +/// \~english @par Type +/// Sync Only +/// \~english @par Detail +/// The function returns the number of trailing zeros in a given 32bit value. +/// \~english @see NS_CountTrailingZeros_16Bit NS_CountTrailingZeros_64Bit +//////////////////////////////////////////////////////////////////////////////////////////////// +SI_16 NS_CountTrailingZeros_32Bit(UI_32 f_ui_val); // NOLINT (readability/nolint) + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup NS_UtilityCenter +/// \~english @par Brief +/// Get the number of trailing zeros in a given 64bit value. +/// \~english @param [in] f_ui_val +/// UI_64 - the value to be counted +/// \~english @retval the number of trailing zeros +/// +/// \~english @par Prerequisite +/// - None +/// \~english @par Change of internal state +/// - None +/// \~english @par Conditions of processing failure +/// - None +/// \~english @par Classification +/// Public +/// \~english @par Type +/// Sync Only +/// \~english @par Detail +/// The function returns the number of trailing zeros in a given 64bit value. +/// \~english @see NS_CountTrailingZeros_16Bit NS_CountTrailingZeros_32Bit +//////////////////////////////////////////////////////////////////////////////////////////////// +SI_16 NS_CountTrailingZeros_64Bit(UI_64 f_ui_val); // NOLINT (readability/nolint) + +/** @}*/ // end of framework_unified + +/** @}*/ // end of native_service + +/** @}*/ // end of BaseSystem + +#endif // FRAMEWORK_UNIFIED_CLIENT_NS_UTILITYCENTER_INCLUDE_NS_UTILITY_IF_INTERNAL_H_ diff --git a/service/native/framework_unified/client/NS_UtilityCenter/include/ns_utility_sys_internal.hpp b/service/native/framework_unified/client/NS_UtilityCenter/include/ns_utility_sys_internal.hpp new file mode 100755 index 0000000..8c71ace --- /dev/null +++ b/service/native/framework_unified/client/NS_UtilityCenter/include/ns_utility_sys_internal.hpp @@ -0,0 +1,353 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION. + * + * 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 ns_utility_sys_internal.hpp +/// @brief This file contains declaration of common APIs for NS_UtilityCenter. +/// +//////////////////////////////////////////////////////////////////////////////////////////////////// +//@{ + +#ifndef FRAMEWORK_UNIFIED_CLIENT_NS_UTILITYCENTER_INCLUDE_NS_UTILITY_SYS_INTERNAL_HPP_ +#define FRAMEWORK_UNIFIED_CLIENT_NS_UTILITYCENTER_INCLUDE_NS_UTILITY_SYS_INTERNAL_HPP_ + +#include <native_service/frameworkunified_types.h> +#include <pthread.h> +#include <errno.h> + +#include <native_service/ns_utility.hpp> + +UI_32 utility_sys_rand(); // NOLINT (readability/nolint) + +// Random Functions ////////////////////////////////////////////////////////// + +template< class T> +T randomValue(T min, T max) { + const int32_t lmin = static_cast< int32_t >(min); + const int32_t range = static_cast< int32_t >(max) - lmin + 1; + if (range > 0) { + return static_cast< T >(utility_sys_rand() % range + lmin); + } else { + return min; + } +} + +template< class T, UI_32 N > +T randomItem(const T(&arr)[ N ]) { + return arr[ randomValue< UI_32 >(0, N - 1) ]; +} + + +template< class T, class Lock > class CLockedData; +template< class T, class Lock, class Fn > typename FSig< Fn >::RType WithWriteLock(Fn, CLockedData< T, Lock > &); +template< class T, class Lock, class Fn > typename FSig< Fn >::RType WithWriteLock(Fn, CLockedData< T, Lock > &, // NOLINT (readability/nolint) + typename FSig< Fn >::TArg2); // NOLINT (readability/nolint) +template< class T, class Lock, class Fn > typename FSig< Fn >::RType WithWriteLock(Fn, CLockedData< T, Lock > &, + typename FSig< Fn >::TArg2, typename FSig< Fn >::TArg3); +template< class T, class Lock, class Fn > typename FSig< Fn >::RType WithWriteLock(Fn, CLockedData< T, Lock > &, + typename FSig< Fn >::TArg2, typename FSig< Fn >::TArg3, typename FSig< Fn >::TArg4); +template< class T, class Lock, class Fn > typename FSig< Fn >::RType WithWriteLock(Fn, CLockedData< T, Lock > &, + typename FSig< Fn >::TArg2, typename FSig< Fn >::TArg3, typename FSig< Fn >::TArg4, typename FSig< Fn >::TArg5); +template< class T, class Lock, class Fn > typename FSig< Fn >::RType WithWriteLock(Fn, CLockedData< T, Lock > &, + typename FSig< Fn >::TArg2, typename FSig< Fn >::TArg3, typename FSig< Fn >::TArg4, typename FSig< Fn >::TArg5, + typename FSig< Fn >::TArg6); +template< class T, class Lock, class Fn > typename FSig< Fn >::RType WithWriteLock(Fn, CLockedData< T, Lock > &, + typename FSig< Fn >::TArg2, typename FSig< Fn >::TArg3, typename FSig< Fn >::TArg4, typename FSig< Fn >::TArg5, + typename FSig< Fn >::TArg6, typename FSig< Fn >::TArg7); +template< class T, class Lock, class Fn > typename FSig< Fn >::RType WithReadLock(Fn, CLockedData< T, Lock > &); // NOLINT (readability/nolint) +template< class T, class Lock, class Fn > typename FSig< Fn >::RType WithReadLock(Fn, CLockedData< T, Lock > &, + typename FSig< Fn >::TArg2); +template< class T, class Lock, class Fn > typename FSig< Fn >::RType WithReadLock(Fn, CLockedData< T, Lock > &, + typename FSig< Fn >::TArg2, typename FSig< Fn >::TArg3); +template< class T, class Lock, class Fn > typename FSig< Fn >::RType WithReadLock(Fn, CLockedData< T, Lock > &, + typename FSig< Fn >::TArg2, typename FSig< Fn >::TArg3, typename FSig< Fn >::TArg4); +template< class T, class Lock, class Fn > typename FSig< Fn >::RType WithReadLock(Fn, CLockedData< T, Lock > &, + typename FSig< Fn >::TArg2, typename FSig< Fn >::TArg3, typename FSig< Fn >::TArg4, typename FSig< Fn >::TArg5); +template< class T, class Lock, class Fn > typename FSig< Fn >::RType WithReadLock(Fn, CLockedData< T, Lock > &, + typename FSig< Fn >::TArg2, typename FSig< Fn >::TArg3, typename FSig< Fn >::TArg4, typename FSig< Fn >::TArg5, + typename FSig< Fn >::TArg6); +template< class T, class Lock, class Fn > typename FSig< Fn >::RType WithReadLock(Fn, CLockedData< T, Lock > &, + typename FSig< Fn >::TArg2, typename FSig< Fn >::TArg3, typename FSig< Fn >::TArg4, typename FSig< Fn >::TArg5, + typename FSig< Fn >::TArg6, typename FSig< Fn >::TArg7); + + +template< class T, class Lock > +class CWithReadLock { + public: + typedef const T TData; + + CWithReadLock(CLockedData< T, Lock > &); + ~CWithReadLock(); + + template< class T1, class Lock1, class Fn > + friend typename FSig< Fn >::RType WithReadLock(Fn, CLockedData< T1, Lock1 > &); + + template< class T1, class Lock1, class Fn > + friend typename FSig< Fn >::RType WithReadLock(Fn, CLockedData< T1, Lock1 > &, typename FSig< Fn >::TArg2); + + template< class T1, class Lock1, class Fn > + friend typename FSig< Fn >::RType WithReadLock(Fn, CLockedData< T1, Lock1 > &, typename FSig< Fn >::TArg2, + typename FSig< Fn >::TArg3); + + template< class T1, class Lock1, class Fn > + friend typename FSig< Fn >::RType WithReadLock(Fn, CLockedData< T1, Lock1 > &, typename FSig< Fn >::TArg2, + typename FSig< Fn >::TArg3, typename FSig< Fn >::TArg4); + + template< class T1, class Lock1, class Fn > + friend typename FSig< Fn >::RType WithReadLock(Fn, CLockedData< T1, Lock1 > &, typename FSig< Fn >::TArg2, + typename FSig< Fn >::TArg3, typename FSig< Fn >::TArg4, typename FSig< Fn >::TArg5); + + template< class T1, class Lock1, class Fn > + friend typename FSig< Fn >::RType WithReadLock(Fn, CLockedData< T1, Lock1 > &, typename FSig< Fn >::TArg2, + typename FSig< Fn >::TArg3, typename FSig< Fn >::TArg4, typename FSig< Fn >::TArg5, typename FSig< Fn >::TArg6); + + template< class T1, class Lock1, class Fn > + friend typename FSig< Fn >::RType WithReadLock(Fn, CLockedData< T1, Lock1 > &, typename FSig< Fn >::TArg2, + typename FSig< Fn >::TArg3, typename FSig< Fn >::TArg4, typename FSig< Fn >::TArg5, typename FSig< Fn >::TArg6, + typename FSig< Fn >::TArg7); + + private: + DISALLOW_COPY_AND_ASSIGN(CWithReadLock); + + const TData &data() const; // NOLINT (readability/nolint) + CLockedData< T, Lock > &m_LockedData; +}; + +template< class T, class Lock > +class CWithWriteLock { + public: + typedef T TData; + + CWithWriteLock(CLockedData< T, Lock > &); + ~CWithWriteLock(); + + template< class T1, class Lock1, class Fn > + friend typename FSig< Fn >::RType WithWriteLock(Fn, CLockedData< T1, Lock1 > &); + + template< class T1, class Lock1, class Fn > + friend typename FSig< Fn >::RType WithWriteLock(Fn, CLockedData< T1, Lock1 > &, typename FSig< Fn >::TArg2); + + template< class T1, class Lock1, class Fn > + friend typename FSig< Fn >::RType WithWriteLock(Fn, CLockedData< T1, Lock1 > &, typename FSig< Fn >::TArg2, + typename FSig< Fn >::TArg3); + + template< class T1, class Lock1, class Fn > + friend typename FSig< Fn >::RType WithWriteLock(Fn, CLockedData< T1, Lock1 > &, typename FSig< Fn >::TArg2, + typename FSig< Fn >::TArg3, typename FSig< Fn >::TArg4); + + template< class T1, class Lock1, class Fn > + friend typename FSig< Fn >::RType WithWriteLock(Fn, CLockedData< T1, Lock1 > &, typename FSig< Fn >::TArg2, + typename FSig< Fn >::TArg3, typename FSig< Fn >::TArg4, typename FSig< Fn >::TArg5); + + template< class T1, class Lock1, class Fn > + friend typename FSig< Fn >::RType WithWriteLock(Fn, CLockedData< T1, Lock1 > &, typename FSig< Fn >::TArg2, + typename FSig< Fn >::TArg3, typename FSig< Fn >::TArg4, typename FSig< Fn >::TArg5, typename FSig< Fn >::TArg6); + + template< class T1, class Lock1, class Fn > + friend typename FSig< Fn >::RType WithWriteLock(Fn, CLockedData< T1, Lock1 > &, typename FSig< Fn >::TArg2, + typename FSig< Fn >::TArg3, typename FSig< Fn >::TArg4, typename FSig< Fn >::TArg5, typename FSig< Fn >::TArg6, + typename FSig< Fn >::TArg7); + + private: + DISALLOW_COPY_AND_ASSIGN(CWithWriteLock); + + TData &data(); // NOLINT (readability/nolint) + CLockedData< T, Lock > &m_LockedData; +}; + +/// \brief Represents a piece of data protected by a lock +/// +/// Data wrapped with Can only be accessed using helper functions +/// WithReadLock and WithWriteLock +/// +/// Example usage: +/// +/// void ModifySharedData( int& n ) { ... modify n ... }; +/// int ReadSharedData( const int& n ){ ... read n ... }; +/// +/// void DoSomething() +/// { +/// CLocked< int > my_data( -1 ); // initialize value to -1 +/// WithWriteLock( ModifySharedData, my_data ); +/// int val = WithReadLock( ReadSharedData, my_data ); +/// } +/// +/// NOTE: that data is locked for the duration of the time that helper functions +/// ModifySharedData, and ReadSharedData are called - once they return, the data becomes +/// unlocked automatically. +template< class T, class Lock = CMutex > +class CLockedData { + public: + typedef CWithReadLock< T, Lock > TReadLock; + typedef CWithWriteLock< T, Lock > TWriteLock; + + CLockedData() {} + template< class T1 > CLockedData(T1 t1) : m_data(t1) {} + template< class T1, class T2 > CLockedData(T1 t1, T2 t2) : m_data(t1, t2) {} + template< class T1, class T2, class T3 > CLockedData(T1 t1, T2 t2, T3 t3) : m_data(t1, t2, t3) {} + template< class T1, class T2, class T3, class T4 > CLockedData(T1 t1, T2 t2, T3 t3, T4 t4) : m_data(t1, t2, t3, + t4) {} + template< class T1, class T2, class T3, class T4, class T5 > CLockedData(T1 t1, T2 t2, T3 t3, T4 t4, + T5 t5) : m_data(t1, t2, t3, t4, t5) {} + template< class T1, class T2, class T3, class T4, class T5, class T6 > CLockedData(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, + T6 t6) : m_data(t1, t2, t3, t4, t5, t6) {} + template< class T1, class T2, class T3, class T4, class T5, class T6, class T7 > CLockedData(T1 t1, T2 t2, T3 t3, + T4 t4, T5 t5, T6 t6, T7 t7) : m_data(t1, t2, t3, t4, t5, t6, t7) {} + + friend class CWithReadLock< T, Lock >; + friend class CWithWriteLock< T, Lock >; + + private: + T m_data; + Lock m_lock; +}; + +// CWithReadLock and CWithWriteLock implementation ////////////////////////////////////////////// + +template< class T, class Lock > +CWithReadLock< T, Lock >::CWithReadLock(CLockedData< T, Lock > &ld) : m_LockedData(ld) { + m_LockedData.m_lock.ReadLock(); +} + +template< class T, class Lock > +CWithReadLock< T, Lock >::~CWithReadLock() { + m_LockedData.m_lock.Unlock(); +} + +template< class T, class Lock > +const typename CWithReadLock< T, Lock >::TData &CWithReadLock< T, Lock >::data() const { // NOLINT (readability/nolint) + return m_LockedData.m_data; +} + +template< class T, class Lock > +CWithWriteLock< T, Lock >::CWithWriteLock(CLockedData< T, Lock > &ld) : m_LockedData(ld) { + m_LockedData.m_lock.WriteLock(); +} + +template< class T, class Lock > +CWithWriteLock< T, Lock >::~CWithWriteLock() { + m_LockedData.m_lock.Unlock(); +} + +template< class T, class Lock > +typename CWithWriteLock< T, Lock >::TData &CWithWriteLock< T, Lock >::data() { // NOLINT (readability/nolint) + return m_LockedData.m_data; +} + +// WithReadLock / WithWriteLock /////////////////////////////////////////////// + +template< class T, class Lock, class Fn > +typename FSig< Fn >::RType WithReadLock(Fn f, CLockedData< T, Lock > &data) { + typename CLockedData< T, Lock >::TReadLock rl(data); + return f(rl.data()); +} + +template< class T, class Lock, class Fn > +typename FSig< Fn >::RType WithReadLock(Fn f, CLockedData< T, Lock > &data, typename FSig< Fn >::TArg2 t2) { + typename CLockedData< T, Lock >::TReadLock rl(data); + return f(rl.data(), t2); +} + +template< class T, class Lock, class Fn > +typename FSig< Fn >::RType WithReadLock(Fn f, CLockedData< T, Lock > &data, typename FSig< Fn >::TArg2 t2, + typename FSig< Fn >::TArg3 t3) { + typename CLockedData< T, Lock >::TReadLock rl(data); + return f(rl.data(), t2, t3); +} + +template< class T, class Lock, class Fn > +typename FSig< Fn >::RType WithReadLock(Fn f, CLockedData< T, Lock > &data, typename FSig< Fn >::TArg2 t2, + typename FSig< Fn >::TArg3 t3, typename FSig< Fn >::TArg4 t4) { + typename CLockedData< T, Lock >::TReadLock rl(data); + return f(rl.data(), t2, t3, t4); +} + +template< class T, class Lock, class Fn > +typename FSig< Fn >::RType WithReadLock(Fn f, CLockedData< T, Lock > &data, typename FSig< Fn >::TArg2 t2, + typename FSig< Fn >::TArg3 t3, typename FSig< Fn >::TArg4 t4, typename FSig< Fn >::TArg5 t5) { + typename CLockedData< T, Lock >::TReadLock rl(data); + return f(rl.data(), t2, t3, t4, t5); +} + +template< class T, class Lock, class Fn > +typename FSig< Fn >::RType WithReadLock(Fn f, CLockedData< T, Lock > &data, typename FSig< Fn >::TArg2 t2, + typename FSig< Fn >::TArg3 t3, typename FSig< Fn >::TArg4 t4, typename FSig< Fn >::TArg5 t5, + typename FSig< Fn >::TArg6 t6) { + typename CLockedData< T, Lock >::TReadLock rl(data); + return f(rl.data(), t2, t3, t4, t5, t6); +} + +template< class T, class Lock, class Fn > +typename FSig< Fn >::RType WithReadLock(Fn f, CLockedData< T, Lock > &data, typename FSig< Fn >::TArg2 t2, + typename FSig< Fn >::TArg3 t3, typename FSig< Fn >::TArg4 t4, typename FSig< Fn >::TArg5 t5, + typename FSig< Fn >::TArg6 t6, typename FSig< Fn >::TArg7 t7) { + typename CLockedData< T, Lock >::TReadLock rl(data); + return f(rl.data(), t2, t3, t4, t5, t6, t7); +} + + +template< class T, class Lock, class Fn > +typename FSig< Fn >::RType WithWriteLock(Fn f, CLockedData< T, Lock > &data) { + typename CLockedData< T, Lock >::TWriteLock wl(data); + return f(wl.data()); +} + +template< class T, class Lock, class Fn > +typename FSig< Fn >::RType WithWriteLock(Fn f, CLockedData< T, Lock > &data, typename FSig< Fn >::TArg2 t2) { + typename CLockedData< T, Lock >::TWriteLock wl(data); + return f(wl.data(), t2); +} + +template< class T, class Lock, class Fn > +typename FSig< Fn >::RType WithWriteLock(Fn f, CLockedData< T, Lock > &data, typename FSig< Fn >::TArg2 t2, + typename FSig< Fn >::TArg3 t3) { + typename CLockedData< T, Lock >::TWriteLock wl(data); + return f(wl.data(), t2, t3); +} + +template< class T, class Lock, class Fn > +typename FSig< Fn >::RType WithWriteLock(Fn f, CLockedData< T, Lock > &data, typename FSig< Fn >::TArg2 t2, + typename FSig< Fn >::TArg3 t3, typename FSig< Fn >::TArg4 t4) { + typename CLockedData< T, Lock >::TWriteLock wl(data); + return f(wl.data(), t2, t3, t4); +} + +template< class T, class Lock, class Fn > +typename FSig< Fn >::RType WithWriteLock(Fn f, CLockedData< T, Lock > &data, typename FSig< Fn >::TArg2 t2, + typename FSig< Fn >::TArg3 t3, typename FSig< Fn >::TArg4 t4, typename FSig< Fn >::TArg5 t5) { + typename CLockedData< T, Lock >::TWriteLock wl(data); + return f(wl.data(), t2, t3, t4, t5); +} + +template< class T, class Lock, class Fn > +typename FSig< Fn >::RType WithWriteLock(Fn f, CLockedData< T, Lock > &data, typename FSig< Fn >::TArg2 t2, + typename FSig< Fn >::TArg3 t3, typename FSig< Fn >::TArg4 t4, typename FSig< Fn >::TArg5 t5, + typename FSig< Fn >::TArg6 t6) { + typename CLockedData< T, Lock >::TWriteLock wl(data); + return f(wl.data(), t2, t3, t4, t5, t6); +} + +template< class T, class Lock, class Fn > +typename FSig< Fn >::RType WithWriteLock(Fn f, CLockedData< T, Lock > &data, typename FSig< Fn >::TArg2 t2, + typename FSig< Fn >::TArg3 t3, typename FSig< Fn >::TArg4 t4, typename FSig< Fn >::TArg5 t5, + typename FSig< Fn >::TArg6 t6, typename FSig< Fn >::TArg7 t7) { + typename CLockedData< T, Lock >::TWriteLock wl(data); + return f(wl.data(), t2, t3, t4, t5, t6, t7); +} + + +#endif // FRAMEWORK_UNIFIED_CLIENT_NS_UTILITYCENTER_INCLUDE_NS_UTILITY_SYS_INTERNAL_HPP_ +//@} diff --git a/service/native/framework_unified/client/NS_UtilityCenter/src/frameworkunified_version.cpp b/service/native/framework_unified/client/NS_UtilityCenter/src/frameworkunified_version.cpp new file mode 100755 index 0000000..3ee907f --- /dev/null +++ b/service/native/framework_unified/client/NS_UtilityCenter/src/frameworkunified_version.cpp @@ -0,0 +1,236 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION. + * + * 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. + */ + +////////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup tag_NativeServices +/// \brief Implementation of CFrameworkunifiedVersion class +/// +/// +////////////////////////////////////////////////////////////////////////////////////////////////// + +#include <native_service/ns_version_if.h> +#include <stdio.h> +#include <unistd.h> +#include <time.h> +#include <string> + + +// defaults - top level makefile for entire system will provide an override +#ifndef FRAMEWORKUNIFIED_SIGNATURE +#define FRAMEWORKUNIFIED_SIGNATURE 0xE0344333 +#endif + +#ifndef FRAMEWORKUNIFIED_STRUC_VER +#define FRAMEWORKUNIFIED_STRUC_VER 0x02 +#endif + +#ifndef FRAMEWORKUNIFIED_PRODUCT_VER +#define FRAMEWORKUNIFIED_PRODUCT_VER 0x01 +#endif + +#ifndef PRODUCT_LABEL +#define PRODUCT_LABEL "undefined" +#endif + +#ifndef FRAMEWORKUNIFIED_BUILD_VER +#define FRAMEWORKUNIFIED_BUILD_VER "undefined" +#endif + +/* For future use */ +#define YEAR ((((__DATE__[7] - '0') * 10 + (__DATE__[8] - '0')) * 10 \ ++ (__DATE__[9] - '0')) * 10 + (__DATE__[10] - '0')) + +#define MONTH (__DATE__[2] == 'n' ? 0 \ +: __DATE__[2] == 'b' ? 1 \ +: __DATE__[2] == 'r' ? (__DATE__[0] == 'M' ? 2 : 3) \ +: __DATE__[2] == 'y' ? 4 \ +: __DATE__[2] == 'n' ? 5 \ +: __DATE__[2] == 'l' ? 6 \ +: __DATE__[2] == 'g' ? 7 \ +: __DATE__[2] == 'p' ? 8 \ +: __DATE__[2] == 't' ? 9 \ +: __DATE__[2] == 'v' ? 10 : 11) + +#define DAY ((__DATE__[4] == ' ' ? 0 : __DATE__[4] - '0') * 10 \ ++ (__DATE__[5] - '0')) + +#define DATE_AS_INT (((YEAR - 2000) * 12 + MONTH) * 31 + DAY) + +#define HOUR (((__TIME__[0] - '0') * 10 + (__TIME__[1] - '0'))) + +#define MIN (((__TIME__[3] - '0') * 10 + (__TIME__[4] - '0'))) + + +//////////////////////////////////////////////////////////////////////////////////////////// +/// Function: Default Constructor +//////////////////////////////////////////////////////////////////////////////////////////// +CFrameworkunifiedVersion::CFrameworkunifiedVersion(): + m_tVersionInfo(const_cast<PSTR>(PRODUCT_LABEL), + FRAMEWORKUNIFIED_SIGNATURE, + FRAMEWORKUNIFIED_STRUC_VER, + FRAMEWORKUNIFIED_PRODUCT_VER, + DATE_AS_INT, + 0, + 0, + const_cast<PSTR>(FRAMEWORKUNIFIED_BUILD_VER), + 0), + m_u32Month(MONTH + 1), + m_u32Year(YEAR), + m_u32Day(DAY) { +} + +//////////////////////////////////////////////////////////////////////////////////////////// +/// Function: Parametrized constructor +//////////////////////////////////////////////////////////////////////////////////////////// +CFrameworkunifiedVersion::CFrameworkunifiedVersion(UI_16 major, UI_16 minor, UI_16 revision) : + m_tVersionInfo(const_cast<PSTR>(PRODUCT_LABEL), + FRAMEWORKUNIFIED_SIGNATURE, + FRAMEWORKUNIFIED_STRUC_VER, + FRAMEWORKUNIFIED_PRODUCT_VER, + DATE_AS_INT, + major, + minor, + const_cast<PSTR>(FRAMEWORKUNIFIED_BUILD_VER), + revision), + m_u32Month(MONTH + 1), + m_u32Year(YEAR), + m_u32Day(DAY) { +} + +//////////////////////////////////////////////////////////////////////////////////////////// +/// Function: Destructor +//////////////////////////////////////////////////////////////////////////////////////////// +CFrameworkunifiedVersion::~CFrameworkunifiedVersion() { +} + +//////////////////////////////////////////////////////////////////////////////////////////// +/// Function: Compares two version instances +//////////////////////////////////////////////////////////////////////////////////////////// +BOOL operator == (CFrameworkunifiedVersion &a, CFrameworkunifiedVersion &b) { // NOLINT (readability/nolint) + return a.operator == (b); +} + +//////////////////////////////////////////////////////////////////////////////////////////// +/// Function: Compares two version instances +//////////////////////////////////////////////////////////////////////////////////////////// +BOOL CFrameworkunifiedVersion::operator == (const CFrameworkunifiedVersion &f_test_i) { + if (!strcmp(m_tVersionInfo.p_str_product, f_test_i.m_tVersionInfo.p_str_product) && + !strcmp(m_tVersionInfo.p_str_build, f_test_i.m_tVersionInfo.p_str_build) && + (m_tVersionInfo.u16_major == f_test_i.m_tVersionInfo.u16_major) && + (m_tVersionInfo.u16_minor == f_test_i.m_tVersionInfo.u16_minor) && + (m_tVersionInfo.u16_revision == f_test_i.m_tVersionInfo.u16_revision) && + (m_tVersionInfo.u32_date == f_test_i.m_tVersionInfo.u32_date) && + (m_tVersionInfo.u32_product_version == f_test_i.m_tVersionInfo.u32_product_version) && + (m_tVersionInfo.u32_signature == f_test_i.m_tVersionInfo.u32_signature) && + (m_tVersionInfo.u32_struc_version == f_test_i.m_tVersionInfo.u32_struc_version)) { + return TRUE; + } + + return FALSE; +} + +//////////////////////////////////////////////////////////////////////////////////////////// +/// Function: Returns the version string +//////////////////////////////////////////////////////////////////////////////////////////// +PCSTR CFrameworkunifiedVersion::VersionStr() { + static char version[30] = {}; + + snprintf(version, sizeof(version) - 1, "%s_%s_%02d.%02d.%02d", + m_tVersionInfo.p_str_product, + m_tVersionInfo.p_str_build, + m_tVersionInfo.u16_major, + m_tVersionInfo.u16_minor, + m_tVersionInfo.u16_revision); + + return version; +} + +//////////////////////////////////////////////////////////////////////////////////////////// +/// Function: Returns the date when the new version of the app was created +//////////////////////////////////////////////////////////////////////////////////////////// +PCSTR CFrameworkunifiedVersion::DateStr() { + static char date[30] = {}; + + snprintf(date, sizeof(date) - 1, "%d-%02d-%02d", + m_u32Year, + m_u32Month, + m_u32Day); + + return date; +} + +//////////////////////////////////////////////////////////////////////////////////////////// +/// Function: Returns the Signature +//////////////////////////////////////////////////////////////////////////////////////////// +UI_32 CFrameworkunifiedVersion::Signature() const { + return m_tVersionInfo.u32_signature; +} + +//////////////////////////////////////////////////////////////////////////////////////////// +/// Function: Returns a new structure version +//////////////////////////////////////////////////////////////////////////////////////////// +UI_32 CFrameworkunifiedVersion::StrucVersion() const { + return m_tVersionInfo.u32_struc_version; +} + +//////////////////////////////////////////////////////////////////////////////////////////// +/// Function: Returns the product version +//////////////////////////////////////////////////////////////////////////////////////////// +UI_32 CFrameworkunifiedVersion::ProductVersion() const { + return m_tVersionInfo.u32_product_version; +} + +//////////////////////////////////////////////////////////////////////////////////////////// +/// Function: Returns the date +//////////////////////////////////////////////////////////////////////////////////////////// +UI_32 CFrameworkunifiedVersion::Date() const { + return m_tVersionInfo.u32_date; +} + +//////////////////////////////////////////////////////////////////////////////////////////// +/// Function: Returns the major number +//////////////////////////////////////////////////////////////////////////////////////////// +UI_16 CFrameworkunifiedVersion::Major() const { + return m_tVersionInfo.u16_major; +} + +//////////////////////////////////////////////////////////////////////////////////////////// +/// Function: Returns the minor number +//////////////////////////////////////////////////////////////////////////////////////////// +UI_16 CFrameworkunifiedVersion::Minor() const { + return m_tVersionInfo.u16_minor; +} + +//////////////////////////////////////////////////////////////////////////////////////////// +/// Function: Returns the unique product identifier +//////////////////////////////////////////////////////////////////////////////////////////// +PCSTR CFrameworkunifiedVersion::Product() const { + return m_tVersionInfo.p_str_product; +} + +//////////////////////////////////////////////////////////////////////////////////////////// +/// Function: Returns the unique build identifier number +//////////////////////////////////////////////////////////////////////////////////////////// +PCSTR CFrameworkunifiedVersion::Build() const { + return m_tVersionInfo.p_str_build; +} + +//////////////////////////////////////////////////////////////////////////////////////////// +/// Function: Returns the revision number +//////////////////////////////////////////////////////////////////////////////////////////// +UI_16 CFrameworkunifiedVersion::Revision() const { + return m_tVersionInfo.u16_revision; +} diff --git a/service/native/framework_unified/client/NS_UtilityCenter/src/makefile_PosixBasedOS001 b/service/native/framework_unified/client/NS_UtilityCenter/src/makefile_PosixBasedOS001 new file mode 100755 index 0000000..14ad4d8 --- /dev/null +++ b/service/native/framework_unified/client/NS_UtilityCenter/src/makefile_PosixBasedOS001 @@ -0,0 +1,194 @@ +# +# @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION. +# +# 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. +# + +# +# Standard Module Makefile version 2.0 +# + +# Name of the componet (team/domain prefix '_' component name) +COMPONENT_NAME = NS_UtilityCenter + +ifndef PRJ_ROOT +export PRJ_ROOT = $(CURDIR)/../ +endif +include $(PRJ_ROOT)cfg/depends.mk + +# Name of the componet (team/domain prefix '_' component name) +# This must be the same as the RTC Component name and Eclipse Project Name +COMPONENT_NAME = NS_UtilityCenter + +# Additive Compile Flags (Flags from initiating make process will still apply) +DEFS += + +# Set local includes and then the reference includes (priority order determines search path) +# Default pattern are any configuration includes (which would be things like PosixBasedOS001), local (Team) component directories, +# dependencies includes (other teams) +# Local (current component references should be in the form of +# $(CC_IFLAG)$(TEAM_ROOT)$(COMPONENT_NAME)/directory +# Example your public include directory would be +# $(CC_IFLAG)$(TEAM_ROOT)$(COMPONENT_NAME)/inc +# Team references should only be to other's public includes such as +# $(CC_IFLAG)$(TEAM_ROOT)NS_MessageCenter/inc +# Global (non-team) references should be only to other's public includes such +# these are found in the depends include file and captured in the (DEPENDS_INCLUDES) variable +INCLUDES = \ + $(CFG_INCS) \ + $(CC_IFLAG)./ \ + $(DEPENDS_INCLUDES) \ + $(CC_IFLAG)$(TEAM_ROOT)$(COMPONENT_NAME)/inc \ + + +# Do the same if you need to include library paths as well +# Do an incremental in case additional library paths are defined +# at the top-level make. Use similar guidelines as for includes +# for example to include a team component library it would be +# $(TEAM_ROOT)NS_MessageCenter/lib/NS_MessageCenter/ +LIB_PATHS += \ + $(DEPENDS_LIB_PATHS) \ + + +# Define binary outputs. These can be libraries or executables. +# Name a variable for each deliverable. Suffixes should be +# EXEC - For Executables -> output to the bin directory +#TIME_EXEC = $(BIN_PATH)time +# LIB - For Static Libraries -> output to lib directory with specific naming +#MATH_LIB = $(LIB_PATH)$(LIB_PREFIX)math.$(LIB_EXT) +# SLIB - For Shared Objects +#FRMWRK_SLIB = $(SLIB_PATH)frmwrk.$(SO_EXT) +# LIB - Define the static library for utility functions +# +# +ifdef DYNAMIC + COMPONENT_LIB = $(SLIB_PATH)$(LIB_PREFIX)$(COMPONENT_NAME)$(DEBUG_EXT).$(SO_EXT) +else + COMPONENT_LIB = $(LIB_PATH)$(LIB_PREFIX)$(COMPONENT_NAME)$(DEBUG_EXT).$(LIB_EXT) +endif + +## Sources Section + +# Define Library & Executable Sources (on a per deliverable basis) +# This includes sources located in subdirectories. + +# Define generic line that pulls all c, cc, cpp files +# since your in the src folder is pull only files from there +COMPONENT_SRCS = \ + $(wildcard *.c) \ + $(wildcard *.cpp) + +# Define sources that my not be local to your component +# here, you can define indivial files or wildcard from +# a different folder. +NON_LOCAL_SRCS = \ + + +# List of all sources to be built. Can be assembled from the other defintitions. +# This only defines sources for the current directory, so if there are subdirectories +# those are not included. (Those are found in simple subdirectory makefiles that only +# direct the building of sources, but no linking into a binary) +SOURCES = \ + $(COMPONENT_SRCS) \ + $(NON_LOCAL_SRCS) \ + + + +# +# Convert the source files to object files with correct folder location. +# +# +C_LANG_OBJECTS = $(addprefix $(BLD_PATH),$(addsuffix .$(OBJ_EXT),$(basename $(filter %.c ,$(SOURCES) ) ) ) ) +CPP_LANG_OBJECTS = $(addprefix $(BLD_PATH),$(addsuffix .$(OBJ_EXT),$(basename $(filter %.cpp %.cc %.cxx,$(SOURCES) ) ) ) ) + + +# List of all sources to be generated. Can be assembled from the other defintitions. +OBJECTS = \ + $(C_LANG_OBJECTS) \ + $(CPP_LANG_OBJECTS) + + + +# All headers that are dependencies. Wildcard is easy to pickup local headers. +# This is only to automate the rebuilding, all builds on the servers are cleans +# So this is not a huge deal when building on a component level. +HEADERS = \ + $(wildcard *.h) \ + $(wildcard $(PRJ_ROOT)../$(COMPONENT_NAME)/inc/*.h) \ + + +LIBRARIES = \ + $(COMPONENT_LIB) \ + + +# Make targets +# Standard +all: banner module_dirs subdirs local library binary + +base: banner module_dirs subdirs local + +# Standard Building of Source Files (Default builds for all objects defined above) +$(C_LANG_OBJECTS): $(SOURCES) $(HEADERS) + $(CC_CMD) + +$(CPP_LANG_OBJECTS): $(SOURCES) $(HEADERS) + $(CPP_CMD) + +local: $(OBJECTS) + +# Defines specific for each deliverable + +# For a static library +$(COMPONENT_LIB): $(OBJECTS) +ifdef DYNAMIC +# For a dynamic library + $(SLIB_CMD) + $(HIDE_ECHO_FLAG)$(OBJCPY) --only-keep-debug $(@) $(@).debug + $(HIDE_ECHO_FLAG)$(OBJCPY) --strip-all $(@) + $(HIDE_ECHO_FLAG)$(OBJCPY) --add-gnu-debuglink=$(@).debug $(@) +else +# For a static library + $(AR_CMD) +endif + +# Standard set of derived targets +library: base \ + $(LIBRARIES) + @echo "***** `date` Done building library: $(COMPONENT_NAME) ******" + +binary: base \ + $(BINARIES) + +# Subdirs should be to jump to subdirectories +# standard form is of +# $(MAKE) -C subdirectory_name $(MAKECMDGOALS) +subdirs: + +clean: + -rm -f $(BINARIES) + -rm -f $(LIBRARIES) + -rm -f $(OBJECTS) + -rm -f $(COMPONENT_LIB).map + -rm -f $(COMPONENT_LIB).debug + +-v: + + @echo "objs: --> $(OBJECTS)" + @echo "sources: --> $(SOURCES)" + @echo "headers: --> $(HEADERS)" + @echo "includes: --> $(INCLUDES)" + @echo "lib: --> $(LIBRARIES)" + @echo "bin: --> $(BINARIES)" + + +module_dirs: build_dirs diff --git a/service/native/framework_unified/client/NS_UtilityCenter/src/ns_endianess.cpp b/service/native/framework_unified/client/NS_UtilityCenter/src/ns_endianess.cpp new file mode 100755 index 0000000..3dea53c --- /dev/null +++ b/service/native/framework_unified/client/NS_UtilityCenter/src/ns_endianess.cpp @@ -0,0 +1,52 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION. + * + * 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 <native_service/ns_endianess.h> + +UI_16 ConvertEndian_UI16(UI_16 f_value) { // NOLINT (readability/nolint) + UI_16 l_ret = static_cast<UI_16>((f_value << 8) | (f_value >> 8)); + return l_ret; +} + + +SI_16 ConvertEndian_SI16(SI_16 f_value) { // NOLINT (readability/nolint) + SI_16 l_ret = static_cast<SI_16>((f_value << 8) | ((f_value >> 8) & 0xFF)); + return l_ret; +} + + +UI_32 ConvertEndian_UI32(UI_32 f_value) { // NOLINT (readability/nolint) + UI_32 l_ret = ((f_value << 8) & 0xFF00FF00) | ((f_value >> 8) & 0xFF00FF); + return (l_ret << 16) | (l_ret >> 16); +} + +SI_32 ConvertEndian_SI32(SI_32 f_value) { // NOLINT (readability/nolint) + SI_32 l_ret = ((f_value << 8) & 0xFF00FF00) | ((f_value >> 8) & 0xFF00FF); + return (l_ret << 16) | ((l_ret >> 16) & 0xFFFF); +} + +UI_64 ConvertEndian_UI64(UI_64 f_value) { // // NOLINT (readability/nolint) + UI_64 l_ret = ((f_value << 8) & 0xFF00FF00FF00FF00ULL) | ((f_value >> 8) & 0x00FF00FF00FF00FFULL); + l_ret = ((l_ret << 16) & 0xFFFF0000FFFF0000ULL) | ((l_ret >> 16) & 0x0000FFFF0000FFFFULL); + return (l_ret << 32) | (l_ret >> 32); +} + +SI_64 ConvertEndian_SI64(SI_64 f_value) { // NOLINT (readability/nolint) + SI_64 l_ret = ((f_value << 8) & 0xFF00FF00FF00FF00ULL) | ((f_value >> 8) & 0x00FF00FF00FF00FFULL); + l_ret = ((l_ret << 16) & 0xFFFF0000FFFF0000ULL) | ((l_ret >> 16) & 0x0000FFFF0000FFFFULL); + return (l_ret << 32) | ((l_ret >> 32) & 0xFFFFFFFFULL); +} diff --git a/service/native/framework_unified/client/NS_UtilityCenter/src/ns_util_crc.cpp b/service/native/framework_unified/client/NS_UtilityCenter/src/ns_util_crc.cpp new file mode 100755 index 0000000..af82364 --- /dev/null +++ b/service/native/framework_unified/client/NS_UtilityCenter/src/ns_util_crc.cpp @@ -0,0 +1,125 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION. + * + * 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. + */ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup tag_NS_UtilityCenter +/// \brief This file contains implementation of APIs to calcuate 16-bit and 32-bit CRC checksum of file. +/// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Include Files +//////////////////////////////////////////////////////////////////////////////////////////////////// +#include <native_service/ns_util_crc.h> +#include <stdio.h> + +// buffer size for reading from file +#define MAX_BUFFER_SIZE 4096 + +// Static 32 bit CRC lookup table +UI_16 g_arr_crc16_table[256] = { +}; + +// Static 32 bit CRC lookup table +UI_32 g_arr_crc32_table[256] = { +}; + + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// CalculateCRC16 +/// This API calculates the 16 bit CRC checksum of a file +//////////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus CalculateCRC16(PCSTR f_c_file_name, UI_16 &f_ui_check_sum) { // NOLINT (readability/nolint) + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + + CHAR l_c_buffer[MAX_BUFFER_SIZE] = {0}; + size_t l_ui_bytes_read = 0; + FILE *l_p_file = NULL; + + if (NULL != f_c_file_name) { + f_ui_check_sum = 0xFFFF; + + // Open the file + l_p_file = fopen(f_c_file_name, "rbe"); + + if (NULL == l_p_file) { + l_e_status = eFrameworkunifiedStatusFileLoadError; + } else { + l_ui_bytes_read = fread(l_c_buffer, 1, sizeof(l_c_buffer), l_p_file); + + while (0 != l_ui_bytes_read) { // LCOV_EXCL_BR_LINE 11: not a branch + for (UI_32 l_ui_bytes_cnt = 0; l_ui_bytes_cnt < l_ui_bytes_read; l_ui_bytes_cnt++) { // LCOV_EXCL_BR_LINE 11: not a branch + f_ui_check_sum = static_cast<UI_16>( + g_arr_crc16_table[((f_ui_check_sum >> 8) ^ l_c_buffer[l_ui_bytes_cnt]) & 0xFF] ^ (f_ui_check_sum << 8)); + } + + l_ui_bytes_read = fread(l_c_buffer, 1, sizeof(l_c_buffer), l_p_file); + } + + fclose(l_p_file); + l_p_file = NULL; + } + + f_ui_check_sum = static_cast<UI_16>(~f_ui_check_sum); + } else { + l_e_status = eFrameworkunifiedStatusInvldParam; + } + + return l_e_status; +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// CalculateCRC32 +/// This API calculates the 32 bit CRC checksum of a file +//////////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus CalculateCRC32(PCSTR f_c_file_name, UI_32 &f_ui_check_sum) { // NOLINT (readability/nolint) + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + + CHAR l_c_buffer[MAX_BUFFER_SIZE] = {0}; + size_t l_ui_bytes_read = 0; + FILE *l_p_file = NULL; + + if (NULL != f_c_file_name) { + f_ui_check_sum = 0xFFFFFFFF; + + // Open the file + l_p_file = fopen(f_c_file_name, "rbe"); + + if (NULL == l_p_file) { + l_e_status = eFrameworkunifiedStatusFileLoadError; + } else { + l_ui_bytes_read = fread(l_c_buffer, 1, sizeof(l_c_buffer), l_p_file); + + while (0 != l_ui_bytes_read) { // LCOV_EXCL_BR_LINE 11: not a branch + for (UI_32 l_ui_bytes_cnt = 0; l_ui_bytes_cnt < l_ui_bytes_read; l_ui_bytes_cnt++) { // LCOV_EXCL_BR_LINE 11: not a branch + f_ui_check_sum = (f_ui_check_sum >> 8) + ^ g_arr_crc32_table[(l_c_buffer[l_ui_bytes_cnt] ^ f_ui_check_sum) & 0xFF]; + } + + l_ui_bytes_read = fread(l_c_buffer, 1, sizeof(l_c_buffer), l_p_file); + } + + fclose(l_p_file); + l_p_file = NULL; + } + + f_ui_check_sum = ~f_ui_check_sum; + } else { + l_e_status = eFrameworkunifiedStatusInvldParam; + } + + return l_e_status; +} diff --git a/service/native/framework_unified/client/NS_UtilityCenter/src/ns_util_directory.cpp b/service/native/framework_unified/client/NS_UtilityCenter/src/ns_util_directory.cpp new file mode 100755 index 0000000..a8b4263 --- /dev/null +++ b/service/native/framework_unified/client/NS_UtilityCenter/src/ns_util_directory.cpp @@ -0,0 +1,145 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION. + * + * 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. + */ + +////////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup tag_NS_UtilityCenter +/// \brief This file contains API implementation for handling file and folder functionalities. +/// +/// +////////////////////////////////////////////////////////////////////////////////////////////////// + +#include <native_service/ns_util_directory.h> + +#include <dirent.h> +#include <stdlib.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <errno.h> +#include <cstring> +#include <string> + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// GetFileList +/// Get list of files present in a specific directory +//////////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus GetFileList(TFileList *f_pv_tfile_list, PCSTR f_pc_path) { + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + + DIR *l_p_dir = NULL; + struct dirent *l_pt_dirent = NULL; + + if (NULL != f_pc_path && NULL != f_pv_tfile_list) { + if (NULL != (l_p_dir = opendir(f_pc_path))) { + PCHAR l_c_file_name = NULL; + size_t l_ui_str_length = 0; + while (NULL != (l_pt_dirent = readdir(l_p_dir))) { // LCOV_EXCL_BR_LINE 11: not a branch + if (!((std::strcmp(l_pt_dirent->d_name, ".") == 0) || (std::strcmp(l_pt_dirent->d_name, "..") == 0))) { + l_ui_str_length = std::strlen(l_pt_dirent->d_name); + + l_c_file_name = new(std::nothrow) CHAR[l_ui_str_length + 1]; + + if (NULL != l_c_file_name) { // LCOV_EXCL_BR_LINE 11: new's error case(c++) + std::memset(l_c_file_name, 0, (sizeof(CHAR) * (l_ui_str_length + 1))); + + std::strncpy(l_c_file_name, l_pt_dirent->d_name, l_ui_str_length); + + f_pv_tfile_list->push_back(l_c_file_name); // LCOV_EXCL_BR_LINE 11: not a branch + + delete[] l_c_file_name; // LCOV_EXCL_BR_LINE 11: not a branch + l_c_file_name = NULL; + } else { + l_e_status = eFrameworkunifiedStatusNullPointer; + } + } + } + closedir(l_p_dir); + } else { + l_e_status = eFrameworkunifiedStatusFail; + } + } else { + l_e_status = eFrameworkunifiedStatusInvldParam; + } + + return l_e_status; +} + + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// CreateDirectory +/// Method to create a directory. +//////////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus CreateDirectory(std::string f_c_dir_path) { + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + + if (!f_c_dir_path.empty()) { + PSTR l_c_temp_dir_path; + PSTR l_c_parsed_dir_path; + PCSTR l_c_copy_path = f_c_dir_path.c_str(); + + l_c_temp_dir_path = const_cast<PSTR >(f_c_dir_path.c_str()); + // LCOV_EXCL_BR_START 11: not a branch + while (l_e_status == eFrameworkunifiedStatusOK && (l_c_parsed_dir_path = std::strchr(l_c_temp_dir_path, '/')) != 0) { + // LCOV_EXCL_BR_STOP + if (l_c_parsed_dir_path != l_c_temp_dir_path) { + /* Neither root nor double slash in path */ + *l_c_parsed_dir_path = '\0'; + if (0 != mkdir(l_c_copy_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) { + if (EEXIST != errno) { // LCOV_EXCL_BR_LINE 11: not a branch + l_e_status = eFrameworkunifiedStatusFail; + } else { + // file already exist + if (TRUE != DoesDirecotryExist(f_c_dir_path)) { // LCOV_EXCL_BR_LINE 5: DoesDirecotryExist's error case + l_e_status = eFrameworkunifiedStatusFail; + } + } + } + *l_c_parsed_dir_path = '/'; + } + l_c_temp_dir_path = l_c_parsed_dir_path + 1; + } + if (eFrameworkunifiedStatusOK == l_e_status) { // LCOV_EXCL_BR_LINE 11: not a branch + if (0 != mkdir(l_c_copy_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) { + if (EEXIST != errno) { // LCOV_EXCL_BR_LINE 11: not a branch + l_e_status = eFrameworkunifiedStatusFail; + } else { + // file already exist + if (TRUE != DoesDirecotryExist(f_c_dir_path)) { // LCOV_EXCL_BR_LINE 5: DoesDirecotryExist's error case + l_e_status = eFrameworkunifiedStatusFail; + } + } + } + } + } else { + l_e_status = eFrameworkunifiedStatusFail; + } + return l_e_status; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// DoesDirecotryExist +/// Method to check if a directory exists. +//////////////////////////////////////////////////////////////////////////////////////////////////// +BOOL DoesDirecotryExist(std::string f_c_dir_path) { + BOOL l_b_directory_status = FALSE; + + DIR *l_p_dir_descriptor = opendir(f_c_dir_path.c_str()); + if (NULL != l_p_dir_descriptor) { + closedir(l_p_dir_descriptor); + l_b_directory_status = TRUE; + } + return l_b_directory_status; +} + diff --git a/service/native/framework_unified/client/NS_UtilityCenter/src/ns_utility.cpp b/service/native/framework_unified/client/NS_UtilityCenter/src/ns_utility.cpp new file mode 100755 index 0000000..d9e6776 --- /dev/null +++ b/service/native/framework_unified/client/NS_UtilityCenter/src/ns_utility.cpp @@ -0,0 +1,144 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION. + * + * 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 <native_service/ns_utility_if.h> +#include <native_service/ns_utility_sys.hpp> +#include <stdlib.h> +#include <ctime> +#include <cstring> +#include "ns_utility_sys_internal.hpp" +#include "ns_utility_if_internal.h" +// Random functions //////////////////////////////////////////////////// +void utility_sys_seedrand() { // NOLINT (readability/nolint) + static bool s_seeded = false; + if (!s_seeded) { + s_seeded = true; + srand48(std::time(NULL)); + } +} + +UI_32 utility_sys_rand() { // NOLINT (readability/nolint) + utility_sys_seedrand(); + return static_cast< UI_32 >(mrand48()); +} + +// Lock wrapper implementation ///////////////////////////////////// + +/////////////////////////////////////////// +// Implements locking policy for mutexes // +/////////////////////////////////////////// + +// CMutex helper function +template< class TErr > +void MutexLock(pthread_mutex_t *mtx) { + if (EOK != pthread_mutex_lock(mtx)) { + throw TErr(""); + } +} + +CMutex::CMutex() { + pthread_mutex_t mtx_tmp = PTHREAD_MUTEX_INITIALIZER; + std::memcpy(&m_mtx, &mtx_tmp, sizeof(m_mtx)); + if (EOK != pthread_mutex_init(&m_mtx, NULL)) { + throw lock_creation_error(); + } +} + +CMutex::~CMutex() { + pthread_mutex_destroy(&m_mtx); +} + +void CMutex::ReadLock() { + MutexLock< lock_acquireread_error >(&m_mtx); +} + +void CMutex::WriteLock() { + MutexLock< lock_acquirewrite_error >(&m_mtx); +} + +void CMutex::Unlock() { + if (EOK != pthread_mutex_unlock(&m_mtx)) { + throw lock_release_error(""); + } +} + + +///////////////////////////////////////////////////////// +// implements locking policy for reader / writer locks // +///////////////////////////////////////////////////////// +CRWLock::CRWLock() { + pthread_rwlock_t rw_tmp = PTHREAD_RWLOCK_INITIALIZER; + std::memcpy(&m_rwl, &rw_tmp, sizeof(m_rwl)); + if (EOK != pthread_rwlock_init(&m_rwl, NULL)) { + throw lock_creation_error(); + } +} + +CRWLock::~CRWLock() { + pthread_rwlock_destroy(&m_rwl); +} + +void CRWLock::ReadLock() { + if (EOK != pthread_rwlock_rdlock(&m_rwl)) { + throw lock_acquireread_error(); + } +} + +void CRWLock::WriteLock() { + if (EOK != pthread_rwlock_wrlock(&m_rwl)) { + throw lock_acquirewrite_error(); + } +} + +void CRWLock::Unlock() { + if (EOK != pthread_rwlock_unlock(&m_rwl)) { + throw lock_release_error(); + } +} + +///////////////////////////////////////////////////////// +// implements GNU Builtins for counting leading zeros // +///////////////////////////////////////////////////////// + +// returns the number of leading zeros in a given 16bit value +SI_16 NS_CountLeadingZeros_16Bit(UI_16 f_ui_val) { // NOLINT (readability/nolint) + return static_cast<SI_16>((f_ui_val == 0) ? NS_INVALID_RETURN : __builtin_clz(f_ui_val)); // NOLINT (readability/nolint) +} + +// returns the number of leading zeros in a given 32bit value +SI_16 NS_CountLeadingZeros_32Bit(UI_32 f_ui_val) { // NOLINT (readability/nolint) + return static_cast<SI_16>((f_ui_val == 0) ? NS_INVALID_RETURN : __builtin_clzl(f_ui_val)); // NOLINT (readability/nolint) +} + +// returns the number of leading zeros in a given 64bit value +SI_16 NS_CountLeadingZeros_64Bit(UI_64 f_ui_val) { // NOLINT (readability/nolint) + return static_cast<SI_16>((f_ui_val == 0) ? NS_INVALID_RETURN : __builtin_clzll(f_ui_val)); // NOLINT (readability/nolint) +} + +// returns the number of trailing zeros in a given 16bit value +SI_16 NS_CountTrailingZeros_16Bit(UI_16 f_ui_val) { // NOLINT (readability/nolint) + return static_cast<SI_16>((f_ui_val == 0) ? NS_INVALID_RETURN : __builtin_ctz(f_ui_val)); // NOLINT (readability/nolint) +} + +// returns the number of trailing zeros in a given 32bit value +SI_16 NS_CountTrailingZeros_32Bit(UI_32 f_ui_val) { // NOLINT (readability/nolint) + return static_cast<SI_16>((f_ui_val == 0) ? NS_INVALID_RETURN : __builtin_ctzl(f_ui_val)); // NOLINT (readability/nolint) +} + +// returns the number of trailing zeros in a given 64bit value +SI_16 NS_CountTrailingZeros_64Bit(UI_64 f_ui_val) { // NOLINT (readability/nolint) + return static_cast<SI_16>((f_ui_val == 0) ? NS_INVALID_RETURN : __builtin_ctzll(f_ui_val)); // NOLINT (readability/nolint) +} |