diff --git a/.gitignore b/.gitignore index 9a3d73f98da86b..2e66e4416c9279 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,9 @@ # VS Code Configs *.vscode + +# Test and Coverage Files +*.gcno +*.gcda +*.gcov +*_q* diff --git a/build/config/standalone/CHIPProjectConfig.h b/build/config/standalone/CHIPProjectConfig.h new file mode 100644 index 00000000000000..0fdea6b8671eb9 --- /dev/null +++ b/build/config/standalone/CHIPProjectConfig.h @@ -0,0 +1,73 @@ +/* + * + * + * + * 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 + * CHIP project configuration for standalone builds on Linux and OS X. + * + */ +#ifndef CHIPPROJECTCONFIG_H +#define CHIPPROJECTCONFIG_H + + +#define CHIP_CONFIG_ENABLE_EPHEMERAL_UDP_PORT 1 + +// Configure WDM for event offload +#define CHIP_CONFIG_EVENT_LOGGING_WDM_OFFLOAD 1 + +#define CHIP_CONFIG_EVENT_LOGGING_UTC_TIMESTAMPS 1 + +#define CHIP_CONFIG_EVENT_LOGGING_NUM_EXTERNAL_CALLBACKS 2 + +#define CHIP_CONFIG_EVENT_LOGGING_EXTERNAL_EVENT_SUPPORT 1 + +#define WDM_UPDATE_MAX_ITEMS_IN_TRAIT_DIRTY_PATH_STORE 300 + +// Uncomment this for a large Tunnel MTU. +//#define CHIP_CONFIG_TUNNEL_INTERFACE_MTU (9000) + +// Max number of Bindings per CHIPExchangeManager +#define CHIP_CONFIG_MAX_BINDINGS 8 + +// Enable support functions for parsing command-line arguments +#define CHIP_CONFIG_ENABLE_ARG_PARSER 1 + +// Enable reading DRBG seed data from /dev/(u)random. +// This is needed for test applications and the CHIP device manager to function +// properly when CHIP_CONFIG_RNG_IMPLEMENTATION_CHIPDRBG is enabled. +#define CHIP_CONFIG_DEV_RANDOM_DRBG_SEED 1 + +#define CHIP_CONFIG_SECURITY_TEST_MODE 1 + +#define WDM_ENFORCE_EXPIRY_TIME 1 + +// Increase session idle timeout in stand-alone builds for the convenience of developers. +#define CHIP_CONFIG_DEFAULT_SECURITY_SESSION_IDLE_TIMEOUT 120000 + +#define CHIP_CONFIG_ENABLE_WDM_UPDATE 1 + +#define CHIP_CONFIG_LEGACY_CASE_AUTH_DELEGATE 0 + +#define CHIP_CONFIG_LEGACY_KEY_EXPORT_DELEGATE 0 + +#define CHIP_SYSTEM_CONFIG_PACKETBUFFER_MAXALLOC 300 + +#define CHIP_CONFIG_ENABLE_FUNCT_ERROR_LOGGING 1 + +#define CHIP_CONFIG_DATA_MANAGEMENT_CLIENT_EXPERIMENTAL 1 + +#endif /* CHIPPROJECTCONFIG_H */ diff --git a/build/config/standalone/SystemProjectConfig.h b/build/config/standalone/SystemProjectConfig.h new file mode 100644 index 00000000000000..c3b78c754980e2 --- /dev/null +++ b/build/config/standalone/SystemProjectConfig.h @@ -0,0 +1,31 @@ +/* + * + * + * + * 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 + * chip::System project configuration for standalone builds on Linux and OS X. + * + */ +#ifndef SYSTEMPROJECTCONFIG_H +#define SYSTEMPROJECTCONFIG_H + +#if CHIP_SYSTEM_CONFIG_USE_SOCKETS +// Uncomment this for larger buffers (e.g. to support a bigger CHIP_CONFIG_TUNNEL_INTERFACE_MTU). +//#define CHIP_SYSTEM_CONFIG_PACKETBUFFER_CAPACITY_MAX 9050 +#endif + +#endif /* SYSTEMPROJECTCONFIG_H */ diff --git a/build/config/standalone/darwin/CHIPProjectConfig.h b/build/config/standalone/darwin/CHIPProjectConfig.h new file mode 100644 index 00000000000000..f6b7db6f6cbf80 --- /dev/null +++ b/build/config/standalone/darwin/CHIPProjectConfig.h @@ -0,0 +1,28 @@ +/* + * + * + * + * 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 + * Alternate CHIP project configuration for building standalone without OpenSSL. + * + */ +#ifndef CHIPPROJECTCONFIG_OSX_H +#define CHIPPROJECTCONFIG_OSX_H + +#include "../CHIPProjectConfig.h" +#define INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT 0 + +#endif /* CHIPPROJECTCONFIG_H */ diff --git a/build/config/standalone/no-openssl/CHIPProjectConfig.h b/build/config/standalone/no-openssl/CHIPProjectConfig.h new file mode 100644 index 00000000000000..d08908bcc9114f --- /dev/null +++ b/build/config/standalone/no-openssl/CHIPProjectConfig.h @@ -0,0 +1,59 @@ +/* + * + * + * + * 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 + * Alternate CHIP project configuration for building standalone without OpenSSL. + * + */ +#ifndef CHIPPROJECTCONFIG_NOOPENSSL_H +#define CHIPPROJECTCONFIG_NOOPENSSL_H + +#include "../CHIPProjectConfig.h" + +#undef CHIP_CONFIG_USE_OPENSSL_ECC +#undef CHIP_CONFIG_USE_MICRO_ECC +#undef CHIP_CONFIG_HASH_IMPLEMENTATION_OPENSSL +#undef CHIP_CONFIG_HASH_IMPLEMENTATION_MINCRYPT +#undef CHIP_CONFIG_RNG_IMPLEMENTATION_OPENSSL +#undef CHIP_CONFIG_RNG_IMPLEMENTATION_CHIPDRBG +#undef CHIP_CONFIG_AES_IMPLEMENTATION_OPENSSL +#undef CHIP_CONFIG_AES_IMPLEMENTATION_AESNI +#undef CHIP_CONFIG_AES_IMPLEMENTATION_PLATFORM +#undef CHIP_CONFIG_SUPPORT_PASE_CONFIG0 +#undef CHIP_CONFIG_SUPPORT_PASE_CONFIG1 +#undef CHIP_CONFIG_SUPPORT_PASE_CONFIG2 +#undef CHIP_CONFIG_SUPPORT_PASE_CONFIG3 +#undef CHIP_CONFIG_SUPPORT_PASE_CONFIG4 +#undef CHIP_CONFIG_ENABLE_PROVISIONING_BUNDLE_SUPPORT + +#define CHIP_CONFIG_USE_OPENSSL_ECC 0 +#define CHIP_CONFIG_USE_MICRO_ECC 1 +#define CHIP_CONFIG_HASH_IMPLEMENTATION_OPENSSL 0 +#define CHIP_CONFIG_HASH_IMPLEMENTATION_MINCRYPT 1 +#define CHIP_CONFIG_RNG_IMPLEMENTATION_OPENSSL 0 +#define CHIP_CONFIG_RNG_IMPLEMENTATION_CHIPDRBG 1 +#define CHIP_CONFIG_AES_IMPLEMENTATION_OPENSSL 0 +#define CHIP_CONFIG_AES_IMPLEMENTATION_AESNI 1 +#define CHIP_CONFIG_AES_IMPLEMENTATION_PLATFORM 0 +#define CHIP_CONFIG_SUPPORT_PASE_CONFIG0 0 +#define CHIP_CONFIG_SUPPORT_PASE_CONFIG1 0 +#define CHIP_CONFIG_SUPPORT_PASE_CONFIG2 0 +#define CHIP_CONFIG_SUPPORT_PASE_CONFIG3 0 +#define CHIP_CONFIG_SUPPORT_PASE_CONFIG4 1 +#define CHIP_CONFIG_ENABLE_PROVISIONING_BUNDLE_SUPPORT 0 + +#endif /* CHIPPROJECTCONFIG_H */ diff --git a/src/Makefile b/src/Makefile index ddfc291455fac5..779c80ef5ff576 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,6 +1,6 @@ TOPTARGETS := all clean run_tests -SUBDIRS = lwip +SUBDIRS = lwip system $(TOPTARGETS): $(SUBDIRS) $(SUBDIRS): diff --git a/src/include/BuildConfig.h b/src/include/BuildConfig.h new file mode 100644 index 00000000000000..e6cec62e10b4f8 --- /dev/null +++ b/src/include/BuildConfig.h @@ -0,0 +1,537 @@ +/* src/include/BuildConfig.h. Generated from BuildConfig.h.in by configure. */ +/* src/include/BuildConfig.h.in. Generated from configure.ac by autoheader. */ + +#ifndef CHIP_BUILD_CONFIG_H +#define CHIP_BUILD_CONFIG_H + +/* Path to BLE platform config header file */ +/* #undef BLE_PLATFORM_CONFIG_INCLUDE */ + +/* Define to 1 if you want to enable WoBle over bluez. */ +#define CONFIG_BLE_PLATFORM_BLUEZ 0 + +/* Define to 1 if you want to use the CHIP Device Layer. */ +#define CONFIG_DEVICE_LAYER 0 + +/* Define to 1 if you want to use CHIP with a system that supports + callback-based vcbprintf */ +#define CONFIG_HAVE_VCBPRINTF 0 + +/* Define to 1 if you want to use CHIP with a system that supports + callback-based vcbprintf */ +#define CONFIG_HAVE_VSNPRINTF_EX 0 + +/* Define to 1 if you want to use CHIP with the Bluetooth Low Energy network + stack. */ +#define CONFIG_NETWORK_LAYER_BLE 1 + +/* Define to 1 if you want to use CHIP with the Internet Protocol network + stack. */ +#define CONFIG_NETWORK_LAYER_INET 1 + +/* CHIP target network stack(s) */ +#define CONFIG_TARGET_NETWORKS "sockets" + +/* Define to 1 if your C++ compiler doesn't accept -c and -o together. */ +/* #undef CXX_NO_MINUS_C_MINUS_O */ + +/* Define to 1 if the system has the type `clockid_t'. */ +#define HAVE_CLOCKID_T 1 + +/* Define to 1 if you have the `clock_gettime' function. */ +/* #undef HAVE_CLOCK_GETTIME */ + +/* Define to 1 if you have the `clock_settime' function. */ +#define HAVE_CLOCK_SETTIME 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_CTYPE_H 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_CURL_CURL_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_CURL_EASY_H */ + +/* define if the compiler supports basic C++11 syntax */ +#define HAVE_CXX11 1 + +/* Define to 1 if you have the declaration of `CLOCK_BOOTTIME', and to 0 if + you don't. */ +#define HAVE_DECL_CLOCK_BOOTTIME 0 + +/* Define to 1 if you have the declaration of `CLOCK_MONOTONIC', and to 0 if + you don't. */ +#define HAVE_DECL_CLOCK_MONOTONIC 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_DLFCN_H 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_FOUNDATION_FOUNDATION_H */ + +/* Define to 1 if you have the `free' function. */ +#define HAVE_FREE 1 + +/* Define to 1 if you have the `freeifaddrs' function. */ +#define HAVE_FREEIFADDRS 1 + +/* Define to 1 if you have the `getifaddrs' function. */ +#define HAVE_GETIFADDRS 1 + +/* Define to 1 if you have the `gettimeofday' function. */ +#define HAVE_GETTIMEOFDAY 1 + +/* Define to 1 if your header file defines the ICMP6_FILTER + socket option. */ +#define HAVE_ICMP6_FILTER 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_IFADDRS_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_INTTYPES_H 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LINUX_IF_TUN_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LINUX_IPV6_ROUTE_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LINUX_ROUTE_H */ + +/* Define to 1 if you have the `localtime' function. */ +#define HAVE_LOCALTIME 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LWIP_DEBUG_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LWIP_DNS_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LWIP_ERR_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LWIP_ETHIP6_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LWIP_INIT_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LWIP_IP_ADDR_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LWIP_IP_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LWIP_MEM_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LWIP_NETIF_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LWIP_OPT_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LWIP_PBUF_H */ + +/* Define to 1 if LwIP has the raw_bind_netif() interface */ +/* #undef HAVE_LWIP_RAW_BIND_NETIF */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LWIP_RAW_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LWIP_SNMP_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LWIP_STATS_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LWIP_SYS_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LWIP_TCPIP_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LWIP_TCP_H */ + +/* Define to 1 if LwIP has the udp_bind_netif() interface */ +/* #undef HAVE_LWIP_UDP_BIND_NETIF */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LWIP_UDP_H */ + +/* Define to 1 if you have the `malloc' function. */ +#define HAVE_MALLOC 1 + +/* Define to 1 if you have the `memchr' function. */ +#define HAVE_MEMCHR 1 + +/* Define to 1 if you have the `memcmp' function. */ +#define HAVE_MEMCMP 1 + +/* Define to 1 if you have the `memmove' function. */ +#define HAVE_MEMMOVE 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_MEMORY_H 1 + +/* Define to 1 if you have the `memset' function. */ +#define HAVE_MEMSET 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_NETINET_ICMP6_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_NETINET_IN_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_NEW 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_NLASSERT_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_NLBYTEORDER_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_NLFAULTINJECTION_HPP */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_NLUNIT_TEST_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_OPENSSL_AES_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_OPENSSL_BN_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_OPENSSL_EC_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_OPENSSL_ERR_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_OPENSSL_EVP_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_OPENSSL_SHA_H */ + +/* Define if you have POSIX threads libraries and header files. */ +#define HAVE_PTHREAD 1 + +/* Have PTHREAD_PRIO_INHERIT. */ +#define HAVE_PTHREAD_PRIO_INHERIT 1 + +/* Define to 1 if you have the `realloc' function. */ +#define HAVE_REALLOC 1 + +/* Define to 1 if you have the `snprintf' function. */ +#define HAVE_SNPRINTF 1 + +/* Define to 1 if your header file defines the SO_BINDTODEVICE + socket option. */ +/* #undef HAVE_SO_BINDTODEVICE */ + +/* Define to 1 if stdbool.h conforms to C99. */ +#define HAVE_STDBOOL_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDINT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDIO_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDLIB_H 1 + +/* Define to 1 if you have the `strchr' function. */ +#define HAVE_STRCHR 1 + +/* Define to 1 if you have the `strcmp' function. */ +#define HAVE_STRCMP 1 + +/* Define to 1 if you have the `strdup' function. */ +#define HAVE_STRDUP 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRINGS_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRING_H 1 + +/* Define to 1 if you have the `strlen' function. */ +#define HAVE_STRLEN 1 + +/* Define to 1 if the system has the type `struct in6_rtmsg'. */ +/* #undef HAVE_STRUCT_IN6_RTMSG */ + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_SOCKET_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_SOCKIO_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_STAT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TIME_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TYPES_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_TIME_H 1 + +/* Define to 1 if header file defines the TUNGETIFF ioctl + command. */ +/* #undef HAVE_TUNGETIFF */ + +/* Define to 1 if you have the header file. */ +#define HAVE_UNISTD_H 1 + +/* Define to 1 if your libc exports vcbprintf. */ +#define HAVE_VCBPRINTF 0 + +/* Define to 1 if your libc exports vnsprintf_ex. */ +#define HAVE_VSNPRINTF_EX 0 + +/* Define to 1 if the system has the type `_Bool'. */ +#define HAVE__BOOL 1 + +/* Define to 0 for disabling ASYNC DNS */ +#define INET_CONFIG_ENABLE_ASYNC_DNS_SOCKETS 1 + +/* Define to 1 for enabling DNS Resolver */ +#define INET_CONFIG_ENABLE_DNS_RESOLVER 1 + +/* Define to 1 for enabling IPv4 */ +#define INET_CONFIG_ENABLE_IPV4 1 + +/* Define to 1 for enabling Raw Endpoint */ +#define INET_CONFIG_ENABLE_RAW_ENDPOINT 1 + +/* Define to 1 for enabling TCP Endpoint */ +#define INET_CONFIG_ENABLE_TCP_ENDPOINT 1 + +/* Define to 1 for enabling TUN Endpoint (only for LWIP and Linux w/ support) + */ +/* #undef INET_CONFIG_ENABLE_TUN_ENDPOINT */ + +/* Define to 1 for enabling UDP Endpoint */ +#define INET_CONFIG_ENABLE_UDP_ENDPOINT 1 + +/* CHIP target inet network endpoint(s) */ +#define INET_ENDPOINTS "all" + +/* Path to Inet Layer platform config header file */ +/* #undef INET_PLATFORM_CONFIG_INCLUDE */ + +/* Path to CHIP Inet Layer platform config header file */ +/* #undef INET_PROJECT_CONFIG_INCLUDE */ + +/* Define to 1 if you want to use CHIP with the inet dns resolver. */ +#define INET_WANT_ENDPOINT_DNS 1 + +/* Define to 1 if you want to use CHIP with the inet raw endpoint. */ +#define INET_WANT_ENDPOINT_RAW 1 + +/* Define to 1 if you want to use CHIP with the inet tcp endpoint. */ +#define INET_WANT_ENDPOINT_TCP 1 + +/* Define to 1 if you want to use CHIP with the inet tun endpoint. */ +#define INET_WANT_ENDPOINT_TUN 0 + +/* Define to 1 if you want to use CHIP with the inet udp endpoint. */ +#define INET_WANT_ENDPOINT_UDP 1 + +/* Define to 1 if you want to use CHIP with IPv4. */ +#define INET_WANT_IPV4 1 + +/* Define to the sub-directory where libtool stores uninstalled libraries. */ +#define LT_OBJDIR ".libs/" + +/* Define to 1 if your C compiler doesn't accept -c and -o together. */ +/* #undef NO_MINUS_C_MINUS_O */ + +/* Define to the address where bug reports for this package should be sent. */ +#define PACKAGE_BUGREPORT "support@chipsupport.tbd" + +/* Define to the full name of this package. */ +#define PACKAGE_NAME "CHIP" + +/* Define to the full name and version of this package. */ +#define PACKAGE_STRING "CHIP gea435d2c" + +/* Define to the one symbol short name of this package. */ +#define PACKAGE_TARNAME "chip" + +/* Define to the home page for this package. */ +#define PACKAGE_URL "https://tbd.developer.chip.com/" + +/* Define to the version of this package. */ +#define PACKAGE_VERSION "gea435d2c" + +/* Define to necessary symbol if this constant uses a non-standard name on + your system. */ +/* #undef PTHREAD_CREATE_JOINABLE */ + +/* Approximation of PTHREAD_NULL since pthread.h does not define one */ +#define PTHREAD_NULL 0 + +/* Define to 1 if you have the ANSI C header files. */ +#define STDC_HEADERS 1 + +/* Path to System Layer platform config header file */ +/* #undef SYSTEM_PLATFORM_CONFIG_INCLUDE */ + +/* Path to CHIP System Layer platform config header file */ +#define SYSTEM_PROJECT_CONFIG_INCLUDE + +/* Path to WARM platform config header file */ +/* #undef WARM_PLATFORM_CONFIG_INCLUDE */ + +/* Path to WARM platform config header file */ +/* #undef WARM_PROJECT_CONFIG_INCLUDE */ + +/* Define custom schema directory */ +/* #undef CHIP_CONFIG_CUSTOM_BUILTIN_SCHEMA_INCLUDE */ + +/* Define to 1 to use the legacy WDM profile. */ +#define CHIP_CONFIG_LEGACY_WDM 1 + +/* Define to 1 for enabling CHIP Tunnel failover */ +/* #undef CHIP_CONFIG_TUNNEL_FAILOVER_SUPPORTED */ + +/* Target platform name for CHIP Device Layer. */ +#define CHIP_DEVICE_LAYER_TARGET NONE + +/* Define to 1 if you want to build the CHIP Device Layer for Silicon + Labs EFR32 platforms. */ +#define CHIP_DEVICE_LAYER_TARGET_EFR32 0 + +/* Define to 1 if you want to build the CHIP Device Layer for the + Espressif ESP32. */ +#define CHIP_DEVICE_LAYER_TARGET_ESP32 0 + +/* Define to 1 if you want to build the CHIP Device Layer for Nordic + nRF5* platforms. */ +#define CHIP_DEVICE_LAYER_TARGET_NRF5 0 + +/* Path to CHIP Device Layer platform config header file */ +/* #undef CHIP_DEVICE_PLATFORM_CONFIG_INCLUDE */ + +/* Path to CHIP Device Layer platform config header file */ +/* #undef CHIP_DEVICE_PROJECT_CONFIG_INCLUDE */ + +/* Define to 1 if you want to enable WoBle Control Path and Throughput Test. + */ +#define CHIP_ENABLE_WOBLE_TEST 0 + +/* Define to 1 if support for fuzzing enabled */ +/* #undef CHIP_FUZZING_ENABLED */ + +/* CHIP logging style */ +#define CHIP_LOGGING_STYLE stdio + +/* Define to 1 if you want to use CHIP with the Android logging system */ +#define CHIP_LOGGING_STYLE_ANDROID 0 + +/* Define to 1 if you want to use CHIP with an external (i.e. platform- and + integrator-defined) logging system */ +#define CHIP_LOGGING_STYLE_EXTERNAL 0 + +/* Define to 1 if you want to use CHIP with a C Standard I/O Library-based + logging system */ +#define CHIP_LOGGING_STYLE_STDIO 1 + +/* Define to 1 if you want to use CHIP with a C Standard I/O Library-based + logging system,but override, at link-time an external (i.e. platform- and + integrator-defined) logging system */ +#define CHIP_LOGGING_STYLE_STDIO_WEAK 0 + +/* Define to 1 if you want to use CHIP with a C Standard I/O Library-based + logging system with timestamped log entries */ +#define CHIP_LOGGING_STYLE_STDIO_WITH_TIMESTAMPS 0 + +/* Path to CHIP platform config header file */ +/* #undef CHIP_PLATFORM_CONFIG_INCLUDE */ + +/* Path to CHIP Core platform config header file */ +#define CHIP_PROJECT_CONFIG_INCLUDE + +/* Define to 1 if you want to use LwIP with CHIP System Layer. */ +#define CHIP_SYSTEM_CONFIG_USE_LWIP 0 + +/* Define to 1 if you want to use BSD sockets with CHIP System Layer. */ +#define CHIP_SYSTEM_CONFIG_USE_SOCKETS 1 + +/* CHIP target style */ +#define CHIP_TARGET_STYLE unix + +/* Define to 1 if you want to use CHIP with an embedded (i.e. non-Unix-based) + system */ +#define CHIP_TARGET_STYLE_EMBEDDED 0 + +/* Define to 1 if you want to use CHIP with a Unix-based system */ +#define CHIP_TARGET_STYLE_UNIX 1 + +/* Define to 1 to build CHIP with nlFaultInjection features */ +#define CHIP_WITH_NLFAULTINJECTION 1 + +/* Define to 1 to build CHIP with OpenSSL features */ +#define CHIP_WITH_OPENSSL 1 + +/* Define for Solaris 2.5.1 so the uint32_t typedef from , + , or is not used. If the typedef were allowed, the + #define below would cause a syntax error. */ +/* #undef _UINT32_T */ + +/* Define for Solaris 2.5.1 so the uint64_t typedef from , + , or is not used. If the typedef were allowed, the + #define below would cause a syntax error. */ +/* #undef _UINT64_T */ + +/* Define for Solaris 2.5.1 so the uint8_t typedef from , + , or is not used. If the typedef were allowed, the + #define below would cause a syntax error. */ +/* #undef _UINT8_T */ + +/* Define to the type of a signed integer type of width exactly 16 bits if + such a type exists and the standard includes do not define it. */ +/* #undef int16_t */ + +/* Define to the type of a signed integer type of width exactly 32 bits if + such a type exists and the standard includes do not define it. */ +/* #undef int32_t */ + +/* Define to the type of a signed integer type of width exactly 64 bits if + such a type exists and the standard includes do not define it. */ +/* #undef int64_t */ + +/* Define to the type of a signed integer type of width exactly 8 bits if such + a type exists and the standard includes do not define it. */ +/* #undef int8_t */ + +/* Define to the type of an unsigned integer type of width exactly 16 bits if + such a type exists and the standard includes do not define it. */ +/* #undef uint16_t */ + +/* Define to the type of an unsigned integer type of width exactly 32 bits if + such a type exists and the standard includes do not define it. */ +/* #undef uint32_t */ + +/* Define to the type of an unsigned integer type of width exactly 64 bits if + such a type exists and the standard includes do not define it. */ +/* #undef uint64_t */ + +/* Define to the type of an unsigned integer type of width exactly 8 bits if + such a type exists and the standard includes do not define it. */ +/* #undef uint8_t */ + +#endif // CHIP_BUILD_CONFIG_H diff --git a/src/lib/support/DLLUtil.h b/src/lib/support/DLLUtil.h new file mode 100644 index 00000000000000..c194aa085ccd60 --- /dev/null +++ b/src/lib/support/DLLUtil.h @@ -0,0 +1,48 @@ +/* + * + * + * + * 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 defines a set of macros for declaring C and C++ + * symbol visibility for dynamic shared objects (DSOs) across + * different compilers and operating systems. + * + * See https://gcc.gnu.org/wiki/Visibility for additional information + * regarding GCC. + * + */ + +#ifndef DLLEXPORT_H_ +#define DLLEXPORT_H_ + +#if defined _WIN32 || defined __CYGWIN__ + #define DLL_IMPORT __declspec(dllimport) + #define DLL_EXPORT __declspec(dllexport) + #define DLL_LOCAL +#else + #if __GNUC__ >= 4 + #define DLL_IMPORT __attribute__ ((visibility ("default"))) + #define DLL_EXPORT __attribute__ ((visibility ("default"))) + #define DLL_LOCAL __attribute__ ((visibility ("hidden"))) + #else + #define DLL_IMPORT + #define DLL_EXPORT + #define DLL_LOCAL + #endif +#endif + +#endif /* DLLEXPORT_H_ */ diff --git a/src/lib/support/ErrorStr.h b/src/lib/support/ErrorStr.h new file mode 100644 index 00000000000000..e2ef630e81bb01 --- /dev/null +++ b/src/lib/support/ErrorStr.h @@ -0,0 +1,50 @@ +/* + * + * + * + * 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 defines functions to translate error codes and status reports + * used throughout the CHIP package into human-readable strings. + * + */ + +#ifndef ERRORSTR_H_ +#define ERRORSTR_H_ + +#include + +#include + +namespace chip { + +struct ErrorFormatter +{ + typedef bool (*FormatFunct)(char * buf, uint16_t bufSize, int32_t err); + + FormatFunct FormatError; + const ErrorFormatter * Next; +}; + +extern const char * ErrorStr(int32_t err); +extern void RegisterErrorFormatter(ErrorFormatter * errFormatter); +extern void FormatError(char * buf, uint16_t bufSize, const char * subsys, int32_t err, const char * desc); + +extern const char * StatusReportStr(uint32_t profileId, uint16_t statusCode); + +} // namespace chip + +#endif /* ERRORSTR_H_ */ diff --git a/src/system/Makefile b/src/system/Makefile new file mode 100644 index 00000000000000..514ee6da04ea99 --- /dev/null +++ b/src/system/Makefile @@ -0,0 +1,30 @@ +TOP_DIR = ../.. +Test_Dir = tests + +.PHONY: all clean run_tests + +all: libSystemLayer.a run_tests + +include $(TOP_DIR)/.yams/cpp_rules.min + +Module_Includes = \ + -I. \ + -I$(TOP_DIR)/src/include \ + -I$(TOP_DIR)/src/lib/ \ + -I$(TOP_DIR)/build/config/standalone/ + +Module_Test_Includes = $(Module_Includes) + +CPP_Files = \ + SystemMutex.cpp \ + SystemError.cpp \ + SystemClock.cpp + +libSystemLayer.a: $(CPP_Objects) + ar rvs $@ $^ + @echo "LINK => $@" + +tests_FLAGS = -lpthread + +clean: my_clean + @rm -f libSystemLayer.a *.gcda *.gcno *.gcov diff --git a/src/system/SystemClock.cpp b/src/system/SystemClock.cpp new file mode 100644 index 00000000000000..36ffbc3c283fd8 --- /dev/null +++ b/src/system/SystemClock.cpp @@ -0,0 +1,263 @@ +/* + * + * + * + * 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 + * Provides default implementations for the platform Get/SetClock_ functions + * for POSIX and LwIP platforms. + */ + +// __STDC_LIMIT_MACROS must be defined for UINT8_MAX to be defined for pre-C++11 clib +#ifndef __STDC_LIMIT_MACROS +#define __STDC_LIMIT_MACROS +#endif // __STDC_LIMIT_MACROS + +// __STDC_CONSTANT_MACROS must be defined for INT64_C and UINT64_C to be defined for pre-C++11 clib +#ifndef __STDC_CONSTANT_MACROS +#define __STDC_CONSTANT_MACROS +#endif // __STDC_CONSTANT_MACROS + +#include +#include + +#include + +#if !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME + +#include +// #include +#include +#include "SystemLayerPrivate.h" + +#if CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS +#include +#if !(HAVE_CLOCK_GETTIME) +#include +#endif +#include +#endif // CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS + +#if CHIP_SYSTEM_CONFIG_USE_LWIP +#include +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP + +namespace chip { +namespace System { +namespace Platform { +namespace Layer { + +// -------------------- Default Get/SetClock Functions for POSIX Systems -------------------- + +#if CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS + +#if !HAVE_CLOCK_GETTIME && !HAVE_GETTIMEOFDAY +#error "CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS requires either clock_gettime() or gettimeofday()" +#endif + +#if HAVE_CLOCK_GETTIME +#if HAVE_DECL_CLOCK_BOOTTIME +// CLOCK_BOOTTIME is a Linux-specific option to clock_gettime for a clock which compensates for system sleep. +#define MONOTONIC_CLOCK_ID CLOCK_BOOTTIME +#define MONOTONIC_RAW_CLOCK_ID CLOCK_MONOTONIC_RAW +#else // HAVE_DECL_CLOCK_BOOTTIME +// CLOCK_MONOTONIC is defined in POSIX and hence is the default choice +#define MONOTONIC_CLOCK_ID CLOCK_MONOTONIC +#endif +#endif // HAVE_CLOCK_GETTIME + +uint64_t GetClock_Monotonic(void) +{ +#if HAVE_CLOCK_GETTIME + struct timespec ts; + int res = clock_gettime(MONOTONIC_CLOCK_ID, &ts); + // 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); + // TODO: use assert library when available + if (res) { abort(); } + return (tv.tv_sec * UINT64_C(1000000)) + tv.tv_usec; +#endif // HAVE_CLOCK_GETTIME +} + +uint64_t GetClock_MonotonicMS(void) +{ + return GetClock_Monotonic() / 1000; +} + +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); + // 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(); +#endif // HAVE_CLOCK_GETTIME +} + +Error GetClock_RealTime(uint64_t & curTime) +{ +#if HAVE_CLOCK_GETTIME + struct timespec ts; + int res = clock_gettime(CLOCK_REALTIME, &ts); + if (res != 0) + { + return MapErrorPOSIX(errno); + } + if (ts.tv_sec < CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD) + { + return CHIP_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED; + } + curTime = (ts.tv_sec * UINT64_C(1000000)) + (ts.tv_nsec / 1000); + 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 < CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD) + { + return CHIP_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED; + } + curTime = (tv.tv_sec * UINT64_C(1000000)) + tv.tv_usec; + return CHIP_SYSTEM_NO_ERROR; +#endif // HAVE_CLOCK_GETTIME +} + +Error GetClock_RealTimeMS(uint64_t & curTime) +{ + Error err = GetClock_RealTime(curTime); + curTime = curTime / 1000; + return err; +} + +#if HAVE_CLOCK_SETTIME || HAVE_SETTIMEOFDAY + +Error SetClock_RealTime(uint64_t newCurTime) +{ +#if HAVE_CLOCK_SETTIME + struct timespec ts; + ts.tv_sec = static_cast(newCurTime / UINT64_C(1000000)); + ts.tv_nsec = static_cast(newCurTime % UINT64_C(1000000)) * 1000; + int res = clock_settime(CLOCK_REALTIME, &ts); + if (res != 0) + { + return (errno == EPERM) ? CHIP_SYSTEM_ERROR_ACCESS_DENIED : MapErrorPOSIX(errno); + } + return CHIP_SYSTEM_NO_ERROR; +#else // HAVE_CLOCK_SETTIME + struct timeval tv; + tv.tv_sec = static_cast(newCurTime / UINT64_C(1000000)); + tv.tv_usec = static_cast(newCurTime % UINT64_C(1000000)); + int res = settimeofday(&tv, NULL); + if (res != 0) + { + return (errno == EPERM) ? CHIP_SYSTEM_ERROR_ACCESS_DENIED : MapErrorPOSIX(errno); + } + return CHIP_SYSTEM_NO_ERROR; +#endif // HAVE_CLOCK_SETTIME +} + +#endif // HAVE_CLOCK_SETTIME || HAVE_SETTIMEOFDAY + +#endif // CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS + + +// -------------------- Default Get/SetClock Functions for LwIP Systems -------------------- + +#if CHIP_SYSTEM_CONFIG_USE_LWIP_MONOTONIC_TIME + +uint64_t GetClock_Monotonic(void) +{ + return GetClock_MonotonicMS(); +} + +uint64_t GetClock_MonotonicMS(void) +{ + static volatile uint64_t overflow = 0; + static volatile u32_t lastSample = 0; + static volatile uint8_t lock = 0; + static const uint64_t kOverflowIncrement = static_cast(0x100000000); + + uint64_t overflowSample; + u32_t sample; + + // Tracking timer wrap assumes that this function gets called with + // a period that is less than 1/2 the timer range. + if (__sync_bool_compare_and_swap(&lock, 0, 1)) + { + sample = sys_now(); + + if (lastSample > sample) + { + overflow += kOverflowIncrement; + } + + lastSample = sample; + overflowSample = overflow; + + __sync_bool_compare_and_swap(&lock, 1, 0); + } + else + { + // a lower priority task is in the block above. Depending where that + // lower task is blocked can spell trouble in a timer wrap condition. + // the question here is what this task should use as an overflow value. + // To fix this race requires a platform api that can be used to + // protect critical sections. + overflowSample = overflow; + sample = sys_now(); + } + + return static_cast(overflowSample | static_cast(sample)); +} + +uint64_t GetClock_MonotonicHiRes(void) +{ + return GetClock_MonotonicMS(); +} + +Error GetClock_RealTime(uint64_t & curTime) +{ + return CHIP_SYSTEM_ERROR_NOT_SUPPORTED; +} + +Error GetClock_RealTimeMS(uint64_t & curTime) +{ + return CHIP_SYSTEM_ERROR_NOT_SUPPORTED; +} + +Error SetClock_RealTime(uint64_t newCurTime) +{ + return CHIP_SYSTEM_ERROR_NOT_SUPPORTED; +} + +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP_MONOTONIC_TIME + +} // namespace Layer +} // namespace Platform +} // namespace System +} // namespace chip + +#endif // CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME diff --git a/src/system/SystemClock.h b/src/system/SystemClock.h new file mode 100644 index 00000000000000..ae062546923da6 --- /dev/null +++ b/src/system/SystemClock.h @@ -0,0 +1,221 @@ +/* + * + * + * + * 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 is an internal header file that defines the interface to a platform-supplied + * function for retrieving the current system time. + */ + +#ifndef SYSTEMTIME_H +#define SYSTEMTIME_H + +// Include configuration headers +#include + +// Include dependent headers +#include + +#include + + +namespace chip { +namespace System { + +enum { + kTimerFactor_nano_per_micro = 1000, /** Number of nanoseconds in a microsecond. */ + kTimerFactor_micro_per_milli = 1000, /** Number of microseconds in a millisecond. */ + kTimerFactor_milli_per_unit = 1000, /** Number of milliseconds in a second. */ + + kTimerFactor_nano_per_milli = 1000000, /** Number of nanoseconds in a millisecond. */ + kTimerFactor_micro_per_unit = 1000000 /** Number of microseconds in a second. */ +}; + +namespace Platform { +namespace Layer { + +/** + * @brief + * Platform-specific function for getting monotonic system time in microseconds. + * + * This function is expected to return elapsed time in microseconds since an arbitrary, platform-defined + * epoch. Platform implementations are obligated to return a value that is ever-increasing (i.e. never + * wraps) between reboots of the system. Additionally, the underlying time source is required to tick + * continuously during any system sleep modes that do not entail a restart upon wake. + * + * The epoch for time returned by this function is *not* required to be the same that for any of the + * other GetClock... functions, including GetClock_MonotonicMS(). + * + * This function is expected to be thread-safe on any platform that employs threading. + * + * @note + * This function is reserved for internal use by the Weave System Layer. Users of the Weave System + * Layer should call System::Layer::GetClock_Monotonic(). + * + * @returns Elapsed time in microseconds since an arbitrary, platform-defined epoch. + */ +extern uint64_t GetClock_Monotonic(void); + +/** + * @brief + * Platform-specific function for getting monotonic system time in milliseconds. + * + * This function is expected to return elapsed time in milliseconds since an arbitrary, platform-defined + * epoch. Platform implementations are obligated to return a value that is ever-increasing (i.e. never + * wraps) between reboots of the system. Additionally, the underlying time source is required to tick + * continuously during any system sleep modes that do not entail a restart upon wake. + * + * The epoch for time returned by this function is *not* required to be the same as that for any of the + * other GetClock... functions, including GetClock_Monotonic(). + * + * This function is expected to be thread-safe on any platform that employs threading. + * + * @note + * This function is reserved for internal use by the Weave System Layer. Users of the Weave System + * Layer should call System::Layer::GetClock_MonotonicMS(). + * + * @returns Elapsed time in milliseconds since an arbitrary, platform-defined epoch. + */ +extern uint64_t GetClock_MonotonicMS(void); + +/** + * @brief + * Platform-specific function for getting high-resolution monotonic system time in microseconds. + * + * This function is expected to return elapsed time in microseconds since an arbitrary, platform-defined + * epoch. Values returned by GetClock_MonotonicHiRes() are required to be ever-increasing (i.e. never + * wrap). However, the underlying timer is *not* required to tick continuously during system + * deep-sleep states. + * + * Platform are encouraged to implement GetClock_MonotonicHiRes() using a high-resolution timer + * that is not subject to gradual clock adjustments (slewing). On platforms without such a timer, + * GetClock_MonotonicHiRes() can return the same value as GetClock_Monotonic(). + * + * The epoch for time returned by this function is not required to be the same that for any of the + * other GetClock... functions. + * + * This function is expected to be thread-safe on any platform that employs threading. + * + * @note + * This function is reserved for internal use by the Weave System Layer. Users of the Weave System + * Layer should call System::Layer::GetClock_MonotonicHiRes(). + * + * @returns Elapsed time in microseconds since an arbitrary, platform-defined epoch. + */ +extern uint64_t GetClock_MonotonicHiRes(void); + +/** + * @brief + * Platform-specific function for getting the current real (civil) time in microsecond Unix time + * format. + * + * This function is expected to return the local platform's notion of current real time, expressed + * as a Unix time value scaled to microseconds. The underlying clock is required to tick at a + * 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 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 CHIP_SYSTEM_ERROR_NOT_SUPPORTED. + * + * This function is expected to be thread-safe on any platform that employs threading. + * + * @note + * This function is reserved for internal use by the Weave System Layer. Users of the Weave System + * Layer should call System::Layer::GetClock_RealTime(). + * + * @param[out] curTime The current time, expressed as Unix time scaled to microseconds. + * + * @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 #CHIP_SYSTEM_ERROR_NOT_SUPPORTED + * If the platform is incapable of tracking real time. + */ +extern Error GetClock_RealTime(uint64_t & curTime); + +/** + * @brief + * Platform-specific function for getting the current real (civil) time in millisecond Unix time + * format. + * + * This function is expected to return the local platform's notion of current real time, expressed + * as a Unix time value scaled to milliseconds. + * + * See the documentation for GetClock_RealTime() for details on the expected behavior. + * + * @note + * This function is reserved for internal use by the Weave System Layer. Users of the Weave System + * Layer should call System::Layer::GetClock_RealTimeMS(). + * + * @param[out] curTime The current time, expressed as Unix time scaled to milliseconds. + * + * @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 #CHIP_SYSTEM_ERROR_NOT_SUPPORTED + * If the platform is incapable of tracking real time. + */ +extern Error GetClock_RealTimeMS(uint64_t & curTimeMS); + +/** + * @brief + * Platform-specific function for setting the current real (civil) time. + * + * Weave calls this function to set the local platform's notion of current real time. The new current + * time is expressed as a Unix time value scaled to microseconds. + * + * Once set, underlying platform clock is expected to track real time with a granularity of at least whole + * seconds. + * + * On platforms that support tracking real time, the SetClock_RealTime() function must return the error + * 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 CHIP_SYSTEM_ERROR_NOT_SUPPORTED. + * + * This function is expected to be thread-safe on any platform that employs threading. + * + * @note + * This function is reserved for internal use by the Weave System Layer. Users of the Weave System + * Layer should call System::Layer::GetClock_RealTimeMS(). + * + * @param[in] newCurTime The new current time, expressed as Unix time scaled to microseconds. + * + * @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 #CHIP_SYSTEM_ERROR_ACCESS_DENIED + * If the calling application does not have the privilege to set the + * current time. + */ +extern Error SetClock_RealTime(uint64_t newCurTime); + +} // namespace Layer +} // namespace Platform +} // namespace System +} // namespace chip + +#endif // SYSTEMTIME_H diff --git a/src/system/SystemConfig.h b/src/system/SystemConfig.h new file mode 100644 index 00000000000000..58da86ffddb727 --- /dev/null +++ b/src/system/SystemConfig.h @@ -0,0 +1,608 @@ +/* + * + * + * + * 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 default compile-time configuration + * constants for the CHIP System Layer, a common + * abstraction layer for the system networking components + * underlying the various CHIP target network layers. + * + * Package integrators that wish to override these values should + * either use preprocessor definitions or create a project- + * specific SystemProjectConfig.h header and then assert + * HAVE_SYSTEMPROJECTCONFIG_H via the package configuration tool + * via --with-chip-system-project-includes=DIR where DIR is + * the directory that contains the header. + * + * NOTE WELL: On some platforms, this header is included by + * C-language programs. + */ + +#ifndef SYSTEMCONFIG_H +#define SYSTEMCONFIG_H + +/* Platform include headers */ +#include + +/* Include a CHIP project-specific configuration file, if defined. + * + * An application or module that incorporates CHIP can define a project + * configuration file to override standard CHIP configuration with + * application-specific values. The CHIPProjectConfig.h file is typically + * located outside the CHIP source tree, alongside the source code for the + * application. The config file is included here to enable certain system-wide + * configuration options, primarily related to logging and error reporting. + */ +#ifdef CHIP_PROJECT_CONFIG_INCLUDE +#include CHIP_PROJECT_CONFIG_INCLUDE +#endif + +/* Include a CHIP platform-specific configuration file, if defined. + * + * A platform configuration file contains overrides to standard CHIP + * configuration that are specific to the platform or OS on which CHIP is + * running. It is typically provided as apart of an adaptation layer that + * adapts CHIP to the target environment. This adaptation layer may be + * included in the CHIP source tree itself or implemented externally. The + * config file is included here to enable certain system-wide configuration + * options, primarily related to logging and error reporting. + */ +#ifdef CHIP_PLATFORM_CONFIG_INCLUDE +#include CHIP_PLATFORM_CONFIG_INCLUDE +#endif + +/* Include a SystemLayer project-specific configuration file, if defined. + * + * An application or module that incorporates CHIP can define a project configuration + * file to override standard System Layer configuration with application-specific values. + * The project config file is typically located outside the CHIP source tree, + * alongside the source code for the application. + */ +#ifdef SYSTEM_PROJECT_CONFIG_INCLUDE +#include SYSTEM_PROJECT_CONFIG_INCLUDE +#endif // SYSTEM_PROJECT_CONFIG_INCLUDE + +/* Include a SystemLayer platform-specific configuration file, if defined. + * + * A platform configuration file contains overrides to standard System Layer configuration + * that are specific to the platform or OS on which CHIP is running. It is typically + * provided as apart of an adaptation layer that adapts CHIP to the target + * environment. This adaptation layer may be included in the CHIP source tree + * itself or implemented externally. + */ +#ifdef SYSTEM_PLATFORM_CONFIG_INCLUDE +#include SYSTEM_PLATFORM_CONFIG_INCLUDE +#endif + +/*--- Sanity check on the build configuration logic. ---*/ + +#if !(CHIP_SYSTEM_CONFIG_USE_LWIP || CHIP_SYSTEM_CONFIG_USE_SOCKETS) +#error "REQUIRED: CHIP_SYSTEM_CONFIG_USE_LWIP || CHIP_SYSTEM_CONFIG_USE_SOCKETS" +#endif // !(CHIP_SYSTEM_CONFIG_USE_LWIP || CHIP_SYSTEM_CONFIG_USE_SOCKETS) + +#if CHIP_SYSTEM_CONFIG_USE_LWIP && CHIP_SYSTEM_CONFIG_USE_SOCKETS +#error "FORBIDDEN: CHIP_SYSTEM_CONFIG_USE_LWIP && CHIP_SYSTEM_CONFIG_USE_SOCKETS" +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP && CHIP_SYSTEM_CONFIG_USE_SOCKETS + +// clang-format off + +/** + * @def CHIP_SYSTEM_CONFIG_PROVIDE_OBSOLESCENT_INTERFACES + * + * @brief + * This boolean configuration option is (1) if the obsolescent features of the CHIP System Layer are provided. + */ +#ifndef CHIP_SYSTEM_CONFIG_PROVIDE_OBSOLESCENT_INTERFACES +#define CHIP_SYSTEM_CONFIG_PROVIDE_OBSOLESCENT_INTERFACES 0 +#endif // CHIP_SYSTEM_CONFIG_PROVIDE_OBSOLESCENT_INTERFACES + +/** + * @def CHIP_SYSTEM_CONFIG_TRANSFER_INETLAYER_PROJECT_CONFIGURATION + * + * @brief + * Define as 1 to transfer the project configuration variable definitions from InetProjectConfig.h into the corresponding + * variables for the CHIP System Layer. + */ +#ifndef CHIP_SYSTEM_CONFIG_TRANSFER_INETLAYER_PROJECT_CONFIGURATION +#define CHIP_SYSTEM_CONFIG_TRANSFER_INETLAYER_PROJECT_CONFIGURATION 0 +#endif // CHIP_SYSTEM_CONFIG_TRANSFER_INETLAYER_PROJECT_CONFIGURATION + +#if CHIP_SYSTEM_CONFIG_TRANSFER_INETLAYER_PROJECT_CONFIGURATION +#ifdef INET_PROJECT_CONFIG_INCLUDE +#if CHIP_SYSTEM_CONFIG_PROVIDE_OBSOLESCENT_INTERFACES +/* + * NOTE WELL: the `INET_LWIP` and `INET_SOCKETS` configuration parameters used to be generated directly by the `autoconf` system. + * Historically, those definitions appeared in `$CHIP/src/include/BuildConfig.h` and the build configuration logic in some systems + * that have `InetProjectConfig.h` may still be relying on these definitions already being present in the logic prior to the + * inclusion of and they must accordingly be defined here to provide for transferring the contents of the + * INET layer configuration properly. + */ +#ifndef INET_LWIP +#define INET_LWIP CHIP_SYSTEM_CONFIG_USE_LWIP +#endif // !defined(INET_LWIP) + +#ifndef INET_SOCKETS +#define INET_SOCKETS CHIP_SYSTEM_CONFIG_USE_SOCKETS +#endif // !defined(INET_SOCKETS) +#endif // CHIP_SYSTEM_CONFIG_PROVIDE_OBSOLESCENT_INTERFACES + +#include INET_PROJECT_CONFIG_INCLUDE +#endif // INET_PROJECT_CONFIG_INCLUDE + +#if !defined(CHIP_SYSTEM_CONFIG_POSIX_LOCKING) && defined(INET_CONFIG_POSIX_LOCKING) +#define CHIP_SYSTEM_CONFIG_POSIX_LOCKING INET_CONFIG_POSIX_LOCKING +#endif // !defined(CHIP_SYSTEM_CONFIG_POSIX_LOCKING) && defined(INET_CONFIG_POSIX_LOCKING) + +#if !defined(CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING) && defined(INET_CONFIG_FREERTOS_LOCKING) +#define CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING INET_CONFIG_FREERTOS_LOCKING +#endif // !defined(CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING) && defined(INET_CONFIG_FREERTOS_LOCKING) + +#if !defined(CHIP_SYSTEM_CONFIG_PACKETBUFFER_MAXALLOC) && defined(INET_CONFIG_NUM_BUFS) +#define CHIP_SYSTEM_CONFIG_PACKETBUFFER_MAXALLOC INET_CONFIG_NUM_BUFS +#endif // !defined(CHIP_SYSTEM_CONFIG_PACKETBUFFER_MAXALLOC) && defined(INET_CONFIG_NUM_BUFS) + +#if !defined(CHIP_SYSTEM_CONFIG_NUM_TIMERS) && defined(INET_CONFIG_NUM_TIMERS) +#define CHIP_SYSTEM_CONFIG_NUM_TIMERS INET_CONFIG_NUM_TIMERS +#endif // !defined(CHIP_SYSTEM_CONFIG_NUM_TIMERS) && defined(INET_CONFIG_NUM_TIMERS) + +#endif // CHIP_SYSTEM_CONFIG_TRANSFER_INETLAYER_PROJECT_CONFIGURATION + +/* Standard include headers */ +#ifndef CHIP_SYSTEM_CONFIG_ERROR_TYPE +#include +#endif /* CHIP_SYSTEM_CONFIG_ERROR_TYPE */ + +#if CHIP_SYSTEM_CONFIG_USE_LWIP +#include +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP + +/* Configuration option variables defined below */ + +/** + * @def CHIP_SYSTEM_CONFIG_NO_LOCKING + * + * @brief + * Disable the use of locking within the system layer.. + * + * Unless you are simulating an LwIP-based system on a Unix-style host, this value should be left at its default. + */ +#ifndef CHIP_SYSTEM_CONFIG_NO_LOCKING +#define CHIP_SYSTEM_CONFIG_NO_LOCKING 0 +#endif /* CHIP_SYSTEM_CONFIG_NO_LOCKING */ + +/** + * @def CHIP_SYSTEM_CONFIG_POSIX_LOCKING + * + * @brief + * Use POSIX locking. This is enabled by default when not compiling for BSD sockets. + * + * Unless you are simulating an LwIP-based system on a Unix-style host, this value should be left at its default. + */ +#ifndef CHIP_SYSTEM_CONFIG_POSIX_LOCKING +#define CHIP_SYSTEM_CONFIG_POSIX_LOCKING 1 +#endif /* CHIP_SYSTEM_CONFIG_POSIX_LOCKING */ + +/** + * @def CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING + * + * @brief + * Use FreeRTOS locking. + * + * This should be generally asserted (1) for FreeRTOS + LwIP-based systems and deasserted (0) for BSD sockets-based systems. + * + * However, if you are simulating an LwIP-based system atop POSIX threads and BSD sockets, this should also be deasserted (0). + */ +#ifndef CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING +#define CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING 0 +#endif /* CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING */ + +/** + * @def CHIP_SYSTEM_CONFIG_NO_LOCKING + * + * @brief + * Disable the use of locking within the system layer. + * + * This value is mutually exclusive with CHIP_SYSTEM_CONFIG_POSIX_LOCKING and CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING. + */ +#ifndef CHIP_SYSTEM_CONFIG_NO_LOCKING +#define CHIP_SYSTEM_CONFIG_NO_LOCKING 0 +#endif /* CHIP_SYSTEM_CONFIG_NO_LOCKING */ + +#if !(CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING) +#error "REQUIRED: CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING" +#endif // !(CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING) + +#if CHIP_SYSTEM_CONFIG_NO_LOCKING && (CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING) +#error "FORBIDDEN: CHIP_SYSTEM_CONFIG_NO_LOCKING && (CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING)" +#endif // CHIP_SYSTEM_CONFIG_NO_LOCKING && (CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING) + +#if CHIP_SYSTEM_CONFIG_POSIX_LOCKING && CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING +#error "FORBIDDEN: CHIP_SYSTEM_CONFIG_POSIX_LOCKING && CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING" +#endif // CHIP_SYSTEM_CONFIG_POSIX_LOCKING && CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING + +#ifndef CHIP_SYSTEM_CONFIG_ERROR_TYPE + +/** + * @def CHIP_SYSTEM_CONFIG_ERROR_TYPE + * + * @brief + * This defines the data type used to represent errors for the CHIP System Layer subsystem. + */ +#define CHIP_SYSTEM_CONFIG_ERROR_TYPE int32_t + +/** + * @def CHIP_SYSTEM_CONFIG_NO_ERROR + * + * @brief + * This defines the CHIP System Layer error code for no error or success. + */ +#ifndef CHIP_SYSTEM_CONFIG_NO_ERROR +#define CHIP_SYSTEM_CONFIG_NO_ERROR 0 +#endif /* CHIP_SYSTEM_CONFIG_NO_ERROR */ + +/** + * @def CHIP_SYSTEM_CONFIG_ERROR_MIN + * + * @brief + * This defines the base or minimum CHIP System Layer error number range. + */ +#ifndef CHIP_SYSTEM_CONFIG_ERROR_MIN +#define CHIP_SYSTEM_CONFIG_ERROR_MIN 7000 +#endif /* CHIP_SYSTEM_CONFIG_ERROR_MIN */ + +/** + * @def CHIP_SYSTEM_CONFIG_ERROR_MAX + * + * @brief + * This defines the top or maximum CHIP System Layer error number range. + */ +#ifndef CHIP_SYSTEM_CONFIG_ERROR_MAX +#define CHIP_SYSTEM_CONFIG_ERROR_MAX 7999 +#endif /* CHIP_SYSTEM_CONFIG_ERROR_MAX */ + +/** + * @def _CHIP_SYSTEM_CONFIG_ERROR + * + * @brief + * This defines a mapping function for CHIP System Layer errors that allows mapping such errors into a platform- or + * system-specific range. + */ +#ifndef _CHIP_SYSTEM_CONFIG_ERROR +#define _CHIP_SYSTEM_CONFIG_ERROR(e) (CHIP_SYSTEM_CONFIG_ERROR_MIN + (e)) +#endif /* _CHIP_SYSTEM_CONFIG_ERROR */ + +#endif /* CHIP_SYSTEM_CONFIG_ERROR_TYPE */ + +/** + * @def CHIP_SYSTEM_HEADER_RESERVE_SIZE + * + * @brief + * The number of bytes to reserve in a network packet buffer to contain + * the CHIP message and exchange headers. + * + * This number was calculated as follows: + * + * CHIP Message Header: + * + * 2 -- Frame Length + * 2 -- Message Header + * 4 -- Message Id + * 8 -- Source Node Id + * 8 -- Destination Node Id + * 2 -- Key Id + * + * CHIP Exchange Header: + * + * 1 -- Application Version + * 1 -- Message Type + * 2 -- Exchange Id + * 4 -- Profile Id + * 4 -- Acknowleged Message Id + * + * @note A number of these fields are optional or not presently used. So most headers will be considerably smaller than this. + */ +#ifndef CHIP_SYSTEM_HEADER_RESERVE_SIZE +#define CHIP_SYSTEM_HEADER_RESERVE_SIZE 38 +#endif /* CHIP_SYSTEM_HEADER_RESERVE_SIZE */ + +/** + * @def CHIP_SYSTEM_CONFIG_PACKETBUFFER_MAXALLOC + * + * @brief + * This is the total number of packet buffers for the BSD sockets configuration. + * + * This may be set to zero (0) to enable unbounded dynamic allocation using malloc. + */ +#ifndef CHIP_SYSTEM_CONFIG_PACKETBUFFER_MAXALLOC +#define CHIP_SYSTEM_CONFIG_PACKETBUFFER_MAXALLOC 15 +#endif /* CHIP_SYSTEM_CONFIG_PACKETBUFFER_MAXALLOC */ + +/** + * @def CHIP_SYSTEM_CONFIG_PACKETBUFFER_CAPACITY_MAX + * + * @brief + * The maximum size an application can use with \c PacketBuffer. This is not the raw memory size consumed by + * a \c PacketBuffer object. + * + * @note + * Only socket platforms can override the default value. On LwIP-based platforms, the size is derived from the PBUF size + * and overriding the value will result in a compile-time error. + * + * This value should be set large enough to accomodate the usage of PacketBuffer in the system. In particular, for the use + * in CHIP, the value should be set to accomodate the desired path MTU (i.e. the largest IP packet that can be sent over + * the network interface) plus any protocol overhead. + * + * For example, sending an IP packet over the tunnel requires additional overheads that depend on platform's network + * interface. On socket platforms, the tunnel protocol overhead is 47 bytes; on LwIP platforms the overhead is 101 bytes, + * plus any "sub-Ethernet" data structure space (e.g. the linked list pointers used by some interfaces to queue packets). + * + * The overheads are calculated as follows: + * + * (variable) -- "Sub-Ethernet" data structures (LwIP-only) + * 14 -- Ethernet Header (LwIP-only) + * 20 -- IPv4 Header (LwIP-only) + * 20 -- TCP Header (LwIP-only) + * 2 -- CHIP Message Length + * 24 -- CHIP Header + * 1 -- Tunnel Header + * 20 -- Crypto Trailer + * + * The size of PacketBuffer structure does not need to be included in this value. + */ +#if CHIP_SYSTEM_CONFIG_USE_LWIP +#ifdef CHIP_SYSTEM_CONFIG_PACKETBUFFER_CAPACITY_MAX +#error "CHIP_SYSTEM_CONFIG_PACKETBUFFER_CAPACITY_MAX cannot be defined on an LwIP-based platform." +#endif /* CHIP_SYSTEM_CONFIG_PACKETBUFFER_CAPACITY_MAX */ +#else /* !CHIP_SYSTEM_CONFIG_USE_LWIP */ +#ifndef CHIP_SYSTEM_CONFIG_PACKETBUFFER_CAPACITY_MAX +#define CHIP_SYSTEM_CONFIG_PACKETBUFFER_CAPACITY_MAX 1583 +#endif /* CHIP_SYSTEM_CONFIG_PACKETBUFFER_CAPACITY_MAX */ +#endif /* !CHIP_SYSTEM_CONFIG_USE_LWIP */ + +#if CHIP_SYSTEM_CONFIG_USE_LWIP + +/** + * @def CHIP_SYSTEM_CONFIG_LWIP_EVENT_TYPE + * + * @brief + * This defines the type for CHIP System Layer event types, typically an integral type. + */ +#ifndef CHIP_SYSTEM_CONFIG_LWIP_EVENT_TYPE +#define CHIP_SYSTEM_CONFIG_LWIP_EVENT_TYPE int +#endif /* CHIP_SYSTEM_CONFIG_LWIP_EVENT_TYPE */ + +/** + * @def CHIP_SYSTEM_CONFIG_LWIP_EVENT_UNRESERVED_CODE + * + * @brief + * This defines the first number in the default event code space not reserved for use by the CHIP System Layer. + * Event codes used by each layer must not overlap. + */ +#ifndef CHIP_SYSTEM_CONFIG_LWIP_EVENT_UNRESERVED_CODE +#define CHIP_SYSTEM_CONFIG_LWIP_EVENT_UNRESERVED_CODE 32 +#endif /* CHIP_SYSTEM_CONFIG_LWIP_EVENT_UNRESERVED_CODE */ + +/** + * @def _CHIP_SYSTEM_CONFIG_LWIP_EVENT + * + * @brief + * This defines a mapping function for CHIP System Layer codes for describing the types of events for the LwIP dispatcher, + * which allows mapping such event types into a platform- or system-specific range. + */ +#ifndef _CHIP_SYSTEM_CONFIG_LWIP_EVENT +#define _CHIP_SYSTEM_CONFIG_LWIP_EVENT(e) (e) +#endif /* _CHIP_SYSTEM_CONFIG_LWIP_EVENT */ + +/** + * @def CHIP_SYSTEM_CONFIG_LWIP_EVENT_OBJECT_TYPE + * + * @brief + * This defines the type of CHIP System Layer event objects or "messages" for the LwIP dispatcher. + * + * Such types are not directly used by the CHIP System Layer but are "passed through". Consequently a forward declaration and + * a const pointer or reference are appropriate. + */ +#ifndef CHIP_SYSTEM_CONFIG_LWIP_EVENT_OBJECT_TYPE +namespace chip { +namespace System { + +struct LwIPEvent; + +} // namespace System +} // namespace chip + +#define CHIP_SYSTEM_CONFIG_LWIP_EVENT_OBJECT_TYPE const struct chip::System::LwIPEvent* +#endif /* CHIP_SYSTEM_CONFIG_LWIP_EVENT_OBJECT_TYPE */ + +#endif /* CHIP_SYSTEM_CONFIG_USE_LWIP */ + +/** + * @def CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_POSIX_ERROR_FUNCTIONS + * + * @brief + * This defines whether (1) or not (0) your platform will provide the following platform- and system-specific functions: + * - chip::System::MapErrorPOSIX + * - chip::System::DescribeErrorPOSIX + * - chip::System::IsErrorPOSIX + */ +#ifndef CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_POSIX_ERROR_FUNCTIONS +#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_POSIX_ERROR_FUNCTIONS 0 +#endif /* CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_POSIX_ERROR_FUNCTIONS */ + +/** + * @def CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_LWIP_ERROR_FUNCTIONS + * + * @brief + * This defines whether (1) or not (0) your platform will provide the following system-specific functions: + * - chip::System::MapErrorLwIP + * - chip::System::DescribeErrorLwIP + * - chip::System::IsErrorLwIP + */ +#ifndef CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_LWIP_ERROR_FUNCTIONS +#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_LWIP_ERROR_FUNCTIONS 0 +#endif /* CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_LWIP_ERROR_FUNCTIONS */ + +/** + * @def CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_XTOR_FUNCTIONS + * + * @brief + * This defines whether (1) or not (0) your platform will provide the following platform-specific functions: + * - chip::System::Platform::Layer::WillInit + * - chip::System::Platform::Layer::WillShutdown + * - chip::System::Platform::Layer::DidInit + * - chip::System::Platform::Layer::DidShutdown + */ +#ifndef CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_XTOR_FUNCTIONS +#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_XTOR_FUNCTIONS 0 +#endif /* CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_XTOR_FUNCTIONS */ + +/** + * @def CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_EVENT_FUNCTIONS + * + * @brief + * This defines whether (1) or not (0) your platform will provide the following platform-specific functions: + * - chip::System::Platform::Layer::PostEvent + * - chip::System::Platform::Layer::DispatchEvents + * - chip::System::Platform::Layer::DispatchEvent + * - chip::System::Platform::Layer::StartTimer + */ +#ifndef CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_EVENT_FUNCTIONS +#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_EVENT_FUNCTIONS 0 +#endif /* CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_EVENT_FUNCTIONS */ + +/** + * @def CHIP_SYSTEM_CONFIG_NUM_TIMERS + * + * @brief + * This is the total number of available timers. + */ +#ifndef CHIP_SYSTEM_CONFIG_NUM_TIMERS +#define CHIP_SYSTEM_CONFIG_NUM_TIMERS 32 +#endif /* CHIP_SYSTEM_CONFIG_NUM_TIMERS */ + +/** + * @def CHIP_SYSTEM_CONFIG_PROVIDE_STATISTICS + * + * @brief + * This defines whether (1) or not (0) the CHIP System Layer provides logic for gathering and reporting statistical + * information for diagnostic purposes. + */ +#ifndef CHIP_SYSTEM_CONFIG_PROVIDE_STATISTICS +#define CHIP_SYSTEM_CONFIG_PROVIDE_STATISTICS 0 +#endif // CHIP_SYSTEM_CONFIG_PROVIDE_STATISTICS + +/** + * @def CHIP_SYSTEM_CONFIG_TEST + * + * @brief + * Defines whether (1) or not (0) to enable testing aids. + */ +#ifndef CHIP_SYSTEM_CONFIG_TEST +#define CHIP_SYSTEM_CONFIG_TEST 0 +#endif + +// clang-format on + +// Configuration parameters with header inclusion dependencies + +/** + * @def CHIP_SYSTEM_CONFIG_HEADER_RESERVE_SIZE + * + * @brief + * The number of bytes to reserve in a network packet buffer to contain all the possible protocol encapsulation headers before the + * application message text. On POSIX sockets, this is CHIP_SYSTEM_HEADER_RESERVE_SIZE. On LwIP, additional space is required for + * the all the headers from layer-2 up to the TCP or UDP header. + */ +#ifndef CHIP_SYSTEM_CONFIG_HEADER_RESERVE_SIZE +#if CHIP_SYSTEM_CONFIG_USE_LWIP +#define CHIP_SYSTEM_CONFIG_HEADER_RESERVE_SIZE \ + (PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN + CHIP_SYSTEM_HEADER_RESERVE_SIZE) +#else /* !CHIP_SYSTEM_CONFIG_USE_LWIP */ +#define CHIP_SYSTEM_CONFIG_HEADER_RESERVE_SIZE (CHIP_SYSTEM_HEADER_RESERVE_SIZE) +#endif /* !CHIP_SYSTEM_CONFIG_USE_LWIP */ +#endif /* CHIP_SYSTEM_CONFIG_HEADER_RESERVE_SIZE */ + +/** + * @def CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME + * + * @brief + * Use platform-supplied functions for System Layer clock functions. + * + * Determines whether (1) or not (0) the underlying platform provides implementations for + * the System Layer GetClock/SetClock functions. + */ +#ifndef CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME +#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME 0 +#endif // CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME + +/** + * @def CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS + * + * @brief + * Use the POSIX time functions for System Layer clock functions. + * + * Use the POSIX time functions (clock_gettime, gettimeofday, etc.) to implement the + * System Layer GetClock/SetClock functions. + * + * Defaults to enabled if the system is using sockets. + * + * This configuration is overridden if CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME is set. + */ +#ifndef CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS +#if CHIP_SYSTEM_CONFIG_USE_SOCKETS +#define CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS 1 +#else // CHIP_SYSTEM_CONFIG_USE_SOCKETS +#define CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS 0 +#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS +#endif // CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS + +/** + * @def CHIP_SYSTEM_CONFIG_USE_LWIP_MONOTONIC_TIME + * + * @brief + * Use LwIP time function for System Layer monotonic clock functions. + * + * Use the LwIP sys_now() function to implement the System Layer GetClock_Monotonic... functions. + * + * Defaults to enabled if the system is using LwIP and not sockets. + * + * This configuration is overridden if CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME is set. + */ +#ifndef CHIP_SYSTEM_CONFIG_USE_LWIP_MONOTONIC_TIME +#if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_SOCKETS +#define CHIP_SYSTEM_CONFIG_USE_LWIP_MONOTONIC_TIME 1 +#else +#define CHIP_SYSTEM_CONFIG_USE_LWIP_MONOTONIC_TIME 0 +#endif +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP_MONOTONIC_TIME + +/** + * @def CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD + * + * @brief + * The earliest time at which the real time clock is considered to be valid. + * + * The value is given in Unix time scaled to seconds. + * + * The default value corresponds to 2000/01/01 00:00:00 + */ +#ifndef CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD +#define CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD 946684800 +#endif // CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD + +#endif // defined(SYSTEMCONFIG_H) diff --git a/src/system/SystemError.cpp b/src/system/SystemError.cpp new file mode 100644 index 00000000000000..f8f806f611e49e --- /dev/null +++ b/src/system/SystemError.cpp @@ -0,0 +1,350 @@ +/* + * + * + * + * 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 free functions for mapping OS and LwIP + * stack-specific errors into CHIP System Layer-specific errors + * and for converting those mapped errors into descriptive + * error strings. + */ + +#include + +// Include module header +#include + +// Include common private header +#include "SystemLayerPrivate.h" + +// Include local headers +#if CHIP_SYSTEM_CONFIG_USE_LWIP +#include +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP + +#if !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_POSIX_ERROR_FUNCTIONS +#include +#endif // !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_POSIX_ERROR_FUNCTIONS + +#include +#include + +#if !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_POSIX_ERROR_FUNCTIONS + +/** + * @def CHIP_SYSTEM_POSIX_ERROR_MIN + * + * @brief + * This defines the base or minimum CHIP System Layer error number range, when passing through errors from an underlying + * POSIX layer. + */ +#define CHIP_SYSTEM_POSIX_ERROR_MIN 2000 + +/** + * @def CHIP_SYSTEM_POSIX_ERROR_MAX + * + * @brief + * This defines the base or maximum CHIP System Layer error number range, when passing through errors from an underlying + * POSIX layer. + */ +#define CHIP_SYSTEM_POSIX_ERROR_MAX 2999 + +#endif // !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_POSIX_ERROR_FUNCTIONS + +#if CHIP_SYSTEM_CONFIG_USE_LWIP +#if !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_LWIP_ERROR_FUNCTIONS + +/** + * @def CHIP_SYSTEM_LWIP_ERROR_MIN + * + * @brief + * This defines the base or minimum CHIP System Layer error number range, when passing through errors from an underlying LWIP + * stack. + */ +#ifndef CHIP_SYSTEM_LWIP_ERROR_MIN +#define CHIP_SYSTEM_LWIP_ERROR_MIN 3000 +#endif // CHIP_SYSTEM_LWIP_ERROR_MIN + +/** + * @def CHIP_SYSTEM_LWIP_ERROR_MAX + * + * @brief + * This defines the base or maximum CHIP System Layer error number range, when passing through errors from an underlying LWIP + * layer. + */ +#ifndef CHIP_SYSTEM_LWIP_ERROR_MAX +#define CHIP_SYSTEM_LWIP_ERROR_MAX 3999 +#endif // CHIP_SYSTEM_LWIP_ERROR_MAX + +#endif // !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_LWIP_ERROR_FUNCTIONS +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP + +namespace chip { +namespace System { + +/** + * Register a text error formatter for System Layer errors. + */ +void RegisterSystemLayerErrorFormatter(void) +{ + static ErrorFormatter sSystemLayerErrorFormatter = + { + FormatSystemLayerError, + NULL + }; + + RegisterErrorFormatter(&sSystemLayerErrorFormatter); +} + +/** + * Given a System Layer error, returns a human-readable NULL-terminated C string + * describing the error. + * + * @param[in] buf Buffer into which the error string will be placed. + * @param[in] bufSize Size of the supplied buffer in bytes. + * @param[in] err The error to be described. + * + * @return true If a description string was written into the supplied buffer. + * @return false If the supplied error was not a System Layer error. + * + */ +bool FormatSystemLayerError(char * buf, uint16_t bufSize, int32_t err) +{ + const char * desc = NULL; + + if (err < CHIP_SYSTEM_ERROR_MIN || err > CHIP_SYSTEM_ERROR_MAX) + { + return false; + } + +#if !CHIP_CONFIG_SHORT_ERROR_STR + switch (err) + { + case CHIP_SYSTEM_ERROR_NOT_IMPLEMENTED : desc = "Not implemented"; break; + case CHIP_SYSTEM_ERROR_NOT_SUPPORTED : desc = "Not supported"; break; + case CHIP_SYSTEM_ERROR_BAD_ARGS : desc = "Bad arguments"; break; + case CHIP_SYSTEM_ERROR_UNEXPECTED_STATE : desc = "Unexpected state"; break; + case CHIP_SYSTEM_ERROR_UNEXPECTED_EVENT : desc = "Unexpected event"; break; + case CHIP_SYSTEM_ERROR_NO_MEMORY : desc = "No memory"; break; + case CHIP_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED : desc = "Real time not synchronized"; break; + case CHIP_SYSTEM_ERROR_ACCESS_DENIED : desc = "Access denied"; break; + } +#endif // !CHIP_CONFIG_SHORT_ERROR_STR + + chip::FormatError(buf, bufSize, "Sys", err, desc); + + return true; +} + +#if !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_POSIX_ERROR_FUNCTIONS +/** + * This implements a mapping function for CHIP System Layer errors that allows mapping integers in the number space of the + * underlying POSIX network and OS stack errors into a platform- or system-specific range. Error codes beyond those currently + * defined by POSIX or the ISO C/C++ standards are mapped similar to the standard ones. + * + * @param[in] e The POSIX network or OS error to map. + * + * @return The mapped POSIX network or OS error. + */ +DLL_EXPORT Error MapErrorPOSIX(int aError) +{ + return (aError == 0 ? CHIP_SYSTEM_NO_ERROR : CHIP_SYSTEM_POSIX_ERROR_MIN + aError); +} + +/** + * This implements a function to return an NULL-terminated OS-specific descriptive C string, associated with the specified, mapped + * OS error. + * + * @param[in] err The mapped OS-specific error to describe. + * + * @return A NULL-terminated, OS-specific descriptive C string describing the error. + */ +DLL_EXPORT const char *DescribeErrorPOSIX(Error aError) +{ + const int lError = (aError - CHIP_SYSTEM_POSIX_ERROR_MIN); + return strerror(lError); +} + +/** + * This implements an introspection function for CHIP System Layer errors that allows the caller to determine whether the + * specified error is an internal, underlying OS error. + * + * @param[in] err The mapped error to determine whether it is an OS error. + * + * @return True if the specified error is an OS error; otherwise, false. + */ +DLL_EXPORT bool IsErrorPOSIX(Error aError) +{ + return (aError >= CHIP_SYSTEM_POSIX_ERROR_MIN && aError <= CHIP_SYSTEM_POSIX_ERROR_MAX); +} + +#endif // !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_POSIX_ERROR_FUNCTIONS + +/** + * Register a text error formatter for POSIX errors. + */ +void RegisterPOSIXErrorFormatter(void) +{ + static ErrorFormatter sPOSIXErrorFormatter = + { + FormatPOSIXError, + NULL + }; + + RegisterErrorFormatter(&sPOSIXErrorFormatter); +} + +/** + * Given a POSIX error, returns a human-readable NULL-terminated C string + * describing the error. + * + * @param[in] buf Buffer into which the error string will be placed. + * @param[in] bufSize Size of the supplied buffer in bytes. + * @param[in] err The error to be described. + * + * @return true If a description string was written into the supplied buffer. + * @return false If the supplied error was not a POSIX error. + * + */ +bool FormatPOSIXError(char * buf, uint16_t bufSize, int32_t err) +{ + const Error sysErr = static_cast(err); + + if (IsErrorPOSIX(sysErr)) + { + const char * desc = +#if CHIP_CONFIG_SHORT_ERROR_STR + NULL; +#else + DescribeErrorPOSIX(sysErr); +#endif + FormatError(buf, bufSize, "OS", err, desc); + return true; + } + else + { + return false; + } +} + +#if CHIP_SYSTEM_CONFIG_USE_LWIP +#if !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_LWIP_ERROR_FUNCTIONS + +/** + * This implements a mapping function for CHIP System Layer errors that allows mapping underlying LwIP network stack errors into a + * platform- or system-specific range. + * + * @param[in] e The LwIP error to map. + * + * @return The mapped LwIP network or OS error. + * + */ +DLL_EXPORT Error MapErrorLwIP(err_t aError) +{ + return (aError == ERR_OK ? CHIP_SYSTEM_NO_ERROR : CHIP_SYSTEM_LWIP_ERROR_MIN - aError); +} + +/** + * This implements a function to return an NULL-terminated LwIP-specific descriptive C string, associated with the specified, + * mapped LwIP error. + * + * @param[in] err The mapped LwIP-specific error to describe. + * + * @return A NULL-terminated, LwIP-specific descriptive C string describing the error. + * + */ +DLL_EXPORT const char* DescribeErrorLwIP(Error aError) +{ + const err_t lError = -((aError) - CHIP_SYSTEM_LWIP_ERROR_MIN); + + // If we are not compiling with LWIP_DEBUG asserted, the unmapped + // local value may go unused. + + (void)lError; + + return lwip_strerr(lError); +} + +/** + * This implements an introspection function for CHIP System Layer errors that + * allows the caller to determine whether the specified error is an + * internal, underlying LwIP error. + * + * @param[in] err The mapped error to determine whether it is a LwIP error. + * + * @return True if the specified error is a LwIP error; otherwise, false. + * + */ +DLL_EXPORT bool IsErrorLwIP(Error aError) +{ + return (aError >= CHIP_SYSTEM_LWIP_ERROR_MIN && aError <= CHIP_SYSTEM_LWIP_ERROR_MAX); +} + +#endif // !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_LWIP_ERROR_FUNCTIONS + +/** + * Register a text error formatter for LwIP errors. + */ +void RegisterLwIPErrorFormatter(void) +{ + static ErrorFormatter sLwIPErrorFormatter = + { + FormatLwIPError, + NULL + }; + + RegisterErrorFormatter(&sLwIPErrorFormatter); +} + +/** + * Given an LwIP error, returns a human-readable NULL-terminated C string + * describing the error. + * + * @param[in] buf Buffer into which the error string will be placed. + * @param[in] bufSize Size of the supplied buffer in bytes. + * @param[in] err The error to be described. + * + * @return true If a description string was written into the supplied buffer. + * @return false If the supplied error was not an LwIP error. + * + */ +bool FormatLwIPError(char * buf, uint16_t bufSize, int32_t err) +{ + const chip::System::Error sysErr = static_cast(err); + + if (IsErrorLwIP(sysErr)) + { + const char * desc = +#if CHIP_CONFIG_SHORT_ERROR_STR + NULL; +#else + DescribeErrorLwIP(sysErr); +#endif + chip::FormatError(buf, bufSize, "LwIP", err, desc); + return true; + } + else + { + return false; + } +} + +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP + + +} // namespace System +} // namespace chip diff --git a/src/system/SystemError.h b/src/system/SystemError.h new file mode 100644 index 00000000000000..2d0010daa3bc14 --- /dev/null +++ b/src/system/SystemError.h @@ -0,0 +1,205 @@ +/* + * + * + * + * 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 declaration statements for CHIP System + * Layer-specific errors. + * + * Error types, ranges, and mappings overrides may be made by + * defining the appropriate CHIP_SYSTEM_CONFIG_* or * + * _CHIP_SYSTEM_CONFIG_* macros. + * + * NOTE WELL: On some platforms, this header is included by + * C-language programs. + */ + +#ifndef SYSTEMERROR_H +#define SYSTEMERROR_H + +// Include headers +#include + +#if CHIP_SYSTEM_CONFIG_USE_LWIP +#include +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP + +/** + * @def CHIP_SYSTEM_NO_ERROR + * + * @brief + * This macro expands to a constant expression representing the error code for success or no error. Its definition may be + * configured via the #CHIP_SYSTEM_CONFIG_NO_ERROR configuration variable. + */ +#define CHIP_SYSTEM_NO_ERROR CHIP_SYSTEM_CONFIG_NO_ERROR + +/** + * @def CHIP_SYSTEM_CONFIG_ERROR_MIN + * + * @brief + * This macro expands to a constant expression representing the minimum code number for error conditions in the CHIP System + * Layer. This value may be configured via #CHIP_SYSTEM_CONFIG_ERROR_MIN. + */ +#ifdef CHIP_SYSTEM_CONFIG_ERROR_MIN +#define CHIP_SYSTEM_ERROR_MIN CHIP_SYSTEM_CONFIG_ERROR_MIN +#endif // CHIP_SYSTEM_CONFIG_ERROR_MIN + +/** + * @def CHIP_SYSTEM_CONFIG_ERROR_MAX + * + * @brief + * This macro expands to a constant expression representing the maximum code number for error conditions in the CHIP System + * Layer. This value may be configured via #CHIP_SYSTEM_CONFIG_ERROR_MAX. + */ +#ifdef CHIP_SYSTEM_CONFIG_ERROR_MAX +#define CHIP_SYSTEM_ERROR_MAX CHIP_SYSTEM_CONFIG_ERROR_MAX +#endif // CHIP_SYSTEM_CONFIG_ERROR_MAX + +#ifdef _CHIP_SYSTEM_CONFIG_ERROR + +/** + * @def _CHIP_SYSTEM_ERROR(e) + * + * @brief + * This defines a mapping function for CHIP System Layer errors that allows mapping such errors into a platform- or + * system-specific range. This function may be configured via #_CHIP_SYSTEM_CONFIG_ERROR(e). + * + * @param[in] e The CHIP System Layer error to map. + * + * @return The mapped CHIP System Layer error. + */ +#define _CHIP_SYSTEM_ERROR(e) _CHIP_SYSTEM_CONFIG_ERROR(e) + +/** + * @name Error Definitions + * + * @{ + */ + +/** + * @def CHIP_SYSTEM_ERROR_NOT_IMPLEMENTED + * + * @brief + * A requested function has not been implemented. + */ +#define CHIP_SYSTEM_ERROR_NOT_IMPLEMENTED _CHIP_SYSTEM_ERROR(0) + +/** + * @def CHIP_SYSTEM_ERROR_NOT_SUPPORTED + * + * @brief + * A requested function or feature is not supported. + */ +#define CHIP_SYSTEM_ERROR_NOT_SUPPORTED _CHIP_SYSTEM_ERROR(1) + +/** + * @def CHIP_SYSTEM_ERROR_BAD_ARGS + * + * @brief + * An invalid argument or arguments were supplied. + */ +#define CHIP_SYSTEM_ERROR_BAD_ARGS _CHIP_SYSTEM_ERROR(2) + +/** + * @def CHIP_SYSTEM_ERROR_UNEXPECTED_STATE + * + * @brief + * An unexpected state was encountered. + */ +#define CHIP_SYSTEM_ERROR_UNEXPECTED_STATE _CHIP_SYSTEM_ERROR(3) + +/** + * @def CHIP_SYSTEM_ERROR_UNEXPECTED_EVENT + * + * @brief + * An unexpected event was encountered. + */ +#define CHIP_SYSTEM_ERROR_UNEXPECTED_EVENT _CHIP_SYSTEM_ERROR(4) + +/** + * @def CHIP_SYSTEM_ERROR_NO_MEMORY + * + * @brief + * A request for memory could not be fulfilled. + */ +#define CHIP_SYSTEM_ERROR_NO_MEMORY _CHIP_SYSTEM_ERROR(5) + +/** + * @def CHIP_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED + * + * @brief + * The system's real time clock is not synchronized to an accurate time source. + */ +#define CHIP_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED _CHIP_SYSTEM_ERROR(6) + +/** + * @def CHIP_SYSTEM_ERROR_ACCESS_DENIED + * + * @brief + * The requestor is not privileged to perform the requested operation. + */ +#define CHIP_SYSTEM_ERROR_ACCESS_DENIED _CHIP_SYSTEM_ERROR(7) + +// !!!!! IMPORTANT !!!!! +// +// If you add new CHIP System Layer errors, please update the translation of error +// codes to strings in SystemError.cpp, and add them to unittest in test-apps/TestErrorStr.cpp + +#endif // _CHIP_SYSTEM_CONFIG_ERROR + +/** + * @} + */ + + +#ifdef __cplusplus + + +namespace chip { +namespace System { + +/** + * The basic type for all chip::System errors. + * + * This is defined to a platform- or system-specific type. + */ +typedef CHIP_SYSTEM_CONFIG_ERROR_TYPE Error; + +extern void RegisterSystemLayerErrorFormatter(void); +extern bool FormatSystemLayerError(char * buf, uint16_t bufSize, int32_t err); + +extern Error MapErrorPOSIX(int code); +extern const char* DescribeErrorPOSIX(Error code); +extern bool IsErrorPOSIX(Error code); +extern void RegisterPOSIXErrorFormatter(void); +extern bool FormatPOSIXError(char * buf, uint16_t bufSize, int32_t err); + +#if CHIP_SYSTEM_CONFIG_USE_LWIP + +extern Error MapErrorLwIP(err_t code); +extern const char* DescribeErrorLwIP(Error code); +extern bool IsErrorLwIP(Error code); +extern void RegisterLwIPErrorFormatter(void); +extern bool FormatLwIPError(char * buf, uint16_t bufSize, int32_t err); + +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP + +} // namespace System +} // namespace chip + +#endif // !defined(__cplusplus) +#endif // defined(SYSTEMERROR_H) diff --git a/src/system/SystemLayerPrivate.h b/src/system/SystemLayerPrivate.h new file mode 100644 index 00000000000000..09c1c7b3354ac8 --- /dev/null +++ b/src/system/SystemLayerPrivate.h @@ -0,0 +1,47 @@ +/* + * + * + * + * 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 header file includes common private definitions for the CHIP system layer. + */ + +#ifndef SYSTEMLAYERPRIVATE_H +#define SYSTEMLAYERPRIVATE_H + +#include + +// +// Common definitions for the LwIP configuration +// +#if CHIP_SYSTEM_CONFIG_USE_LWIP + +#include "lwip/init.h" +#include "lwip/stats.h" + +// To use LwIP 1.x, some additional definitions are required here. +#if LWIP_VERSION_MAJOR < 2 + +// Compatibility adaptations for statistical data in LwIP 1.x +#ifndef MEMP_STATS_GET +#define MEMP_STATS_GET(FIELD, INDEX) (lwip_stats.memp[INDEX].FIELD) +#endif // !defined(MEMP_STATS_GET) +#endif // LWIP_VERSION_MAJOR + +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP + +#endif // defined(SYSTEMLAYERPRIVATE_H) diff --git a/src/system/SystemMutex.cpp b/src/system/SystemMutex.cpp new file mode 100644 index 00000000000000..fe0061bf55e697 --- /dev/null +++ b/src/system/SystemMutex.cpp @@ -0,0 +1,114 @@ +/* + * + * + * + * 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 defines the abstraction of mutual exclusion locks + * offered by the target platform. + */ + +// Include module header +#include + +// Include common private header +#include "SystemLayerPrivate.h" + +#if !CHIP_SYSTEM_CONFIG_NO_LOCKING + +// Include system headers +#include + +namespace chip { +namespace System { + +/** + * Initialize the mutual exclusion lock instance. + * + * @param[inout] aThis A zero-initialized object. + * + * @retval #CHIP_SYSTEM_NO_ERROR The mutual exclusion lock is ready to use. + * @retval #CHIP_SYSTEM_ERROR_NO_MEMORY Insufficient system memory to allocate the mutual exclusion lock. + * @retval #CHIP_SYSTEM_ERROR_UNEXPECTED_STATE An unexpected system error encountered during initialization. + */ + +#if CHIP_SYSTEM_CONFIG_POSIX_LOCKING +DLL_EXPORT Error Mutex::Init(Mutex& aThis) +{ + int lSysError = pthread_mutex_init(&aThis.mPOSIXMutex, NULL); + Error lError; + + switch (lSysError) + { + case 0: + lError = CHIP_SYSTEM_NO_ERROR; + break; + + case ENOMEM: + lError = CHIP_SYSTEM_ERROR_NO_MEMORY; + break; + + default: + lError = CHIP_SYSTEM_ERROR_UNEXPECTED_STATE; + break; + } + + return lError; +} +#endif // CHIP_SYSTEM_CONFIG_POSIX_LOCKING + +#if CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING +DLL_EXPORT Error Mutex::Init(Mutex& aThis) +{ +restart: + if (__sync_bool_compare_and_swap(&aThis.mInitialized, 0, 1)) + { +#if (configSUPPORT_STATIC_ALLOCATION == 1) + aThis.mFreeRTOSSemaphore = xSemaphoreCreateMutexStatic(&aThis.mFreeRTOSSemaphoreObj); +#else + aThis.mFreeRTOSSemaphore = xSemaphoreCreateMutex(); +#endif + if (aThis.mFreeRTOSSemaphore == NULL) + { + aThis.mInitialized = 0; + + return CHIP_SYSTEM_ERROR_NO_MEMORY; + } + } else { + while (aThis.mFreeRTOSSemaphore == NULL) + { + vTaskDelay(1); + + if (aThis.mInitialized == 0) + { + goto restart; + } + } + } + + return CHIP_SYSTEM_NO_ERROR; +} + +DLL_EXPORT void Mutex::Lock(void) +{ + xSemaphoreTake(this->mFreeRTOSSemaphore, portMAX_DELAY); +} +#endif // CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING + +} // namespace System +} // namespace chip + +#endif // !CHIP_SYSTEM_CONFIG_NO_LOCKING diff --git a/src/system/SystemMutex.h b/src/system/SystemMutex.h new file mode 100644 index 00000000000000..65678647121190 --- /dev/null +++ b/src/system/SystemMutex.h @@ -0,0 +1,120 @@ +/* + * + * + * + * 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 declares the abstraction of mutual exclusion locks + * offered by the target platform. + */ + +#ifndef SYSTEMMUTEX_H +#define SYSTEMMUTEX_H + +// Include configuration headers +#include + +// Include dependent headers +#include + +#include + +#if !CHIP_SYSTEM_CONFIG_NO_LOCKING + +#if CHIP_SYSTEM_CONFIG_POSIX_LOCKING +#include +#endif // CHIP_SYSTEM_CONFIG_POSIX_LOCKING + +#if CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING +#include +#include +#include +#endif // CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING + +namespace chip { +namespace System { + +/** + * @class Mutex + * + * @brief + * This class represents a simple mutual exclusion lock used on platforms with preemptively scheduled multi-threaded + * programming environments, for example, POSIX threads and FreeRTOS. The lock is non-recursive, and may not be used in a hardware + * interrupt context. The constructor and destructor are defined as null functions to facilitate using objects with \c static + * storage duration and uninitialized memory. Use \c Init method to initialize. The copy/move operators are not provided. + * + */ +class DLL_EXPORT Mutex +{ +public: + Mutex(void); + ~Mutex(void); + + static Error Init(Mutex& aMutex); + + void Lock(void); /**< Acquire the mutual exclusion lock, blocking the current thread indefinitely if necessary. */ + void Unlock(void); /**< Release the mutual exclusion lock (can block on some systems until scheduler completes). */ + +private: +#if CHIP_SYSTEM_CONFIG_POSIX_LOCKING + pthread_mutex_t mPOSIXMutex; +#endif // CHIP_SYSTEM_CONFIG_POSIX_LOCKING + +#if CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING +#if (configSUPPORT_STATIC_ALLOCATION == 1) + StaticSemaphore_t mFreeRTOSSemaphoreObj; +#endif // (configSUPPORT_STATIC_ALLOCATION == 1) + volatile SemaphoreHandle_t mFreeRTOSSemaphore; + volatile int mInitialized; +#endif // CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING + + Mutex(const Mutex&) /* = delete */; + Mutex& operator =(const Mutex&) /* = delete */; +}; + +inline Mutex::Mutex(void) +{ +} + +inline Mutex::~Mutex(void) +{ +} + +#if CHIP_SYSTEM_CONFIG_POSIX_LOCKING +inline void Mutex::Lock(void) +{ + pthread_mutex_lock(&this->mPOSIXMutex); +} + +inline void Mutex::Unlock(void) +{ + pthread_mutex_unlock(&this->mPOSIXMutex); +} +#endif // CHIP_SYSTEM_CONFIG_POSIX_LOCKING + +#if CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING +inline void Mutex::Unlock(void) +{ + xSemaphoreGive(this->mFreeRTOSSemaphore); +} +#endif // CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING + +} // namespace System +} // namespace chip + +#endif // !CHIP_SYSTEM_CONFIG_NO_LOCKING + +#endif // defined(SYSTEMMUTEX_H) diff --git a/src/system/tests/tests.cpp b/src/system/tests/tests.cpp new file mode 100644 index 00000000000000..8f1321c645e147 --- /dev/null +++ b/src/system/tests/tests.cpp @@ -0,0 +1,57 @@ +/* + * + * + * + * 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 +#include +#include +#include +#include + +#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; + Error result = Mutex::Init(mLock); + assert(result == CHIP_SYSTEM_NO_ERROR); + mLock.Lock(); + 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; +} \ No newline at end of file