diff --git a/.clang-format b/.clang-format index c09f46f..e704f45 100644 --- a/.clang-format +++ b/.clang-format @@ -1,6 +1,7 @@ --- Language: Cpp BasedOnStyle: Google +IndentPPDirectives: AfterHash --- Language: Proto BasedOnStyle: Google diff --git a/c/src/point_one/polaris/polaris.c b/c/src/point_one/polaris/polaris.c index 91e9bb4..5680bd0 100644 --- a/c/src/point_one/polaris/polaris.c +++ b/c/src/point_one/polaris/polaris.c @@ -13,13 +13,13 @@ #include // For memmove() #ifndef P1_FREERTOS -#include // For fcntl() -#include // For struct tm +# include // For fcntl() +# include // For struct tm #endif #ifdef POLARIS_USE_TLS -#include -#include +# include +# include #endif #include "point_one/polaris/polaris_internal.h" @@ -37,15 +37,15 @@ } while (0) #if P1_NO_PRINT -#define P1_PrintMessage(level, x, ...) P1_NOOP -#define P1_PrintErrno(x, ...) P1_NOOP - -#define P1_PrintData(buffer, length) P1_NOOP -#if POLARIS_USE_TLS -#define ShowCerts(ssl) \ - do { \ - } while (0) -#endif +# define P1_PrintMessage(level, x, ...) P1_NOOP +# define P1_PrintErrno(x, ...) P1_NOOP + +# define P1_PrintData(buffer, length) P1_NOOP +# if POLARIS_USE_TLS +# define ShowCerts(ssl) \ + do { \ + } while (0) +# endif #else // !P1_NO_PRINT static int __log_level = POLARIS_LOG_LEVEL_INFO; @@ -69,7 +69,7 @@ static int PrintTime(char* buffer, size_t capacity_bytes) { // Where date is available, print the date in a format consistent with glog // used by the C++ client source code (YYYYMMDD). int length = 0; -#ifndef P1_FREERTOS +# ifndef P1_FREERTOS time_t now_sec = (time_t)now.tv_sec; struct tm local_time = *localtime(&now_sec); if (buffer) { @@ -82,7 +82,7 @@ static int PrintTime(char* buffer, size_t capacity_bytes) { P1_fprintf(stderr, "%04u%02u%02u ", 1900 + local_time.tm_year, local_time.tm_mon + 1, local_time.tm_mday); } -#endif +# endif // At a minimum, print HH:MM:SS.SSS. For FreeRTOS devices, this will likely be // elapsed time since boot. @@ -105,18 +105,19 @@ static void P1_PrintToCallback(int line, int level, const char* format, ...) { __print_callback("polaris.c", line, level, buffer); } -#define P1_DoPrint(line, level, x, ...) \ - if (__log_level >= level) { \ - if (__print_callback) { \ - P1_PrintToCallback(line, level, x, ##__VA_ARGS__); \ - } else { \ - PrintTime(NULL, 0); \ - P1_fprintf(stderr, " polaris.c:" STR(line) "] " x "\n", ##__VA_ARGS__); \ - } \ - } +# define P1_DoPrint(line, level, x, ...) \ + if (__log_level >= level) { \ + if (__print_callback) { \ + P1_PrintToCallback(line, level, x, ##__VA_ARGS__); \ + } else { \ + PrintTime(NULL, 0); \ + P1_fprintf(stderr, " polaris.c:" STR(line) "] " x "\n", \ + ##__VA_ARGS__); \ + } \ + } -#define P1_PrintMessage(level, x, ...) \ - P1_DoPrint(__LINE__, level, x, ##__VA_ARGS__); +# define P1_PrintMessage(level, x, ...) \ + P1_DoPrint(__LINE__, level, x, ##__VA_ARGS__); // The standard POSIX perror() does not include the numeric error code in the // printout, which is often very useful, so we do not use it. We also can't use @@ -129,21 +130,21 @@ static void P1_PrintToCallback(int line, int level, const char* format, ...) { // our macro also takes the function return code and prints it. For POSIX // systems, the return code argument is ignored. While it theoretically could be // omitted, it is required for FreeRTOS compilation. -#ifdef P1_FREERTOS // FreeRTOS -#define P1_PrintErrnoLevel(level, x, ret) \ - P1_PrintMessage(level, x ". [error=%s (%d)]", strerror(-ret), ret) -#else // POSIX -#define P1_PrintErrnoLevel(level, x, ret) \ - P1_PrintMessage(level, x ". [error=%s (%d)]", strerror(errno), errno) -#endif // End OS selection +# ifdef P1_FREERTOS // FreeRTOS +# define P1_PrintErrnoLevel(level, x, ret) \ + P1_PrintMessage(level, x ". [error=%s (%d)]", strerror(-ret), ret) +# else // POSIX +# define P1_PrintErrnoLevel(level, x, ret) \ + P1_PrintMessage(level, x ". [error=%s (%d)]", strerror(errno), errno) +# endif // End OS selection -#define P1_PrintErrno(x, ret) \ - P1_PrintErrnoLevel(POLARIS_LOG_LEVEL_ERROR, x, ret) +# define P1_PrintErrno(x, ret) \ + P1_PrintErrnoLevel(POLARIS_LOG_LEVEL_ERROR, x, ret) static void P1_PrintData(const uint8_t* buffer, size_t length); -#if POLARIS_USE_TLS +# if POLARIS_USE_TLS static void ShowCerts(SSL* ssl); -#endif +# endif #endif // P1_NO_PRINT #define P1_PrintError(x, ...) \ @@ -158,9 +159,9 @@ static void ShowCerts(SSL* ssl); P1_PrintMessage(POLARIS_LOG_LEVEL_TRACE, x, ##__VA_ARGS__) #if POLARIS_NO_PRINT -#define P1_PrintReadWriteError(context, x, ret) P1_NOOP -#define P1_DebugPrintReadWriteError(context, x, ret) P1_NOOP -#define P1_PrintSSLError(context, x, ret) P1_NOOP +# define P1_PrintReadWriteError(context, x, ret) P1_NOOP +# define P1_DebugPrintReadWriteError(context, x, ret) P1_NOOP +# define P1_PrintSSLError(context, x, ret) P1_NOOP #elif POLARIS_USE_TLS static void __P1_PrintSSLError(int level, int line, PolarisContext_t* context, const char* message, int ret) { @@ -212,21 +213,21 @@ static void __P1_PrintSSLError(int level, int line, PolarisContext_t* context, } } -#define P1_PrintReadWriteError(context, x, ret) \ - __P1_PrintSSLError(POLARIS_LOG_LEVEL_ERROR, __LINE__, context, x, ret) -#define P1_DebugPrintReadWriteError(context, x, ret) \ - if (__log_level >= POLARIS_LOG_LEVEL_DEBUG) { \ - __P1_PrintSSLError(POLARIS_LOG_LEVEL_DEBUG, __LINE__, context, x, ret); \ - } -#define P1_PrintSSLError(context, x, ret) \ - __P1_PrintSSLError(POLARIS_LOG_LEVEL_ERROR, __LINE__, context, x, ret) +# define P1_PrintReadWriteError(context, x, ret) \ + __P1_PrintSSLError(POLARIS_LOG_LEVEL_ERROR, __LINE__, context, x, ret) +# define P1_DebugPrintReadWriteError(context, x, ret) \ + if (__log_level >= POLARIS_LOG_LEVEL_DEBUG) { \ + __P1_PrintSSLError(POLARIS_LOG_LEVEL_DEBUG, __LINE__, context, x, ret); \ + } +# define P1_PrintSSLError(context, x, ret) \ + __P1_PrintSSLError(POLARIS_LOG_LEVEL_ERROR, __LINE__, context, x, ret) #else // !POLARIS_NO_PRINT && !POLARIS_USE_TLS -#define P1_PrintReadWriteError(context, x, ret) P1_PrintErrno(x, ret) -#define P1_DebugPrintReadWriteError(context, x, ret) \ - if (__log_level >= POLARIS_LOG_LEVEL_DEBUG) { \ - P1_PrintErrnoLevel(POLARIS_LOG_LEVEL_DEBUG, x, ret); \ - } -#define P1_PrintSSLError(context, x, ret) P1_NOOP +# define P1_PrintReadWriteError(context, x, ret) P1_PrintErrno(x, ret) +# define P1_DebugPrintReadWriteError(context, x, ret) \ + if (__log_level >= POLARIS_LOG_LEVEL_DEBUG) { \ + P1_PrintErrnoLevel(POLARIS_LOG_LEVEL_DEBUG, x, ret); \ + } +# define P1_PrintSSLError(context, x, ret) P1_NOOP #endif // POLARIS_NO_PRINT / POLARIS_USE_TLS static int ValidateUniqueID(const char* unique_id); @@ -334,9 +335,8 @@ int Polaris_AuthenticateTo(PolarisContext_t* context, const char* api_key, return POLARIS_NOT_ENOUGH_SPACE; } - P1_PrintDebug( - "Sending auth request. [api_key=%.7s..., unique_id=%s, url=%s]", - api_key, unique_id, api_url); + P1_PrintDebug("Sending auth request. [api_key=%.7s..., unique_id=%s, url=%s]", + api_key, unique_id, api_url); context->auth_token[0] = '\0'; #ifdef POLARIS_USE_TLS int status_code = SendPOSTRequest(context, api_url, 443, "/api/v1/auth/token", @@ -388,8 +388,7 @@ int Polaris_SetAuthToken(PolarisContext_t* context, const char* auth_token) { return POLARIS_NOT_ENOUGH_SPACE; } else { memcpy(context->auth_token, auth_token, length + 1); - P1_PrintDebug("Using user-specified access token: %s", - context->auth_token); + P1_PrintDebug("Using user-specified access token: %s", context->auth_token); return POLARIS_SUCCESS; } } @@ -551,10 +550,9 @@ int Polaris_SendECEFPosition(PolarisContext_t* context, double x_m, double y_m, #ifdef P1_FREERTOS // Floating point printf() not available in FreeRTOS. - P1_PrintDebug( - "Sending ECEF position. [size=%u B, position=[%d, %d, %d] cm]", - (unsigned)message_size, le32toh(payload->x_cm), le32toh(payload->y_cm), - le32toh(payload->z_cm)); + P1_PrintDebug("Sending ECEF position. [size=%u B, position=[%d, %d, %d] cm]", + (unsigned)message_size, le32toh(payload->x_cm), + le32toh(payload->y_cm), le32toh(payload->z_cm)); #else P1_PrintDebug( "Sending ECEF position. [size=%u B, position=[%.2f, %.2f, %.2f]]", @@ -866,8 +864,7 @@ int Polaris_Work(PolarisContext_t* context) { // data will be received. That does not necessarily imply an authentication // failure. if (!context->authenticated && context->total_bytes_received > 270) { - P1_PrintDebug( - "Sufficient data received. Authentication token accepted."); + P1_PrintDebug("Sufficient data received. Authentication token accepted."); context->authenticated = POLARIS_AUTHENTICATED; } @@ -1005,8 +1002,7 @@ static inline int P1_SetAddress(const char* hostname, int port, P1_SocketAddrV4_t* result) { struct hostent* host_info = gethostbyname(hostname); if (host_info == NULL) { - P1_PrintError("Unable to resolve \"%s\": %s", hostname, - hstrerror(h_errno)); + P1_PrintError("Unable to resolve \"%s\": %s", hostname, hstrerror(h_errno)); return -1; } // IPv6 not currently supported by the API. @@ -1046,11 +1042,11 @@ static int OpenSocket(PolarisContext_t* context, const char* endpoint_url, #ifdef POLARIS_USE_TLS // Configure TLS. P1_PrintDebug("Configuring TLS context."); -#if OPENSSL_VERSION_NUMBER < 0x10100000L +# if OPENSSL_VERSION_NUMBER < 0x10100000L context->ssl_ctx = SSL_CTX_new(TLSv1_2_client_method()); -#else +# else context->ssl_ctx = SSL_CTX_new(TLS_client_method()); -#endif +# endif // we specifically disable older insecure protocols SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_SSLv2); SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_SSLv3); @@ -1096,8 +1092,8 @@ static int OpenSocket(PolarisContext_t* context, const char* endpoint_url, #ifdef P1_FREERTOS P1_PrintError("Error locating address '%s'.", endpoint_url); #else - P1_PrintError("Error locating address '%s'. [error=%s (%d)]", - endpoint_url, hstrerror(h_errno), h_errno); + P1_PrintError("Error locating address '%s'. [error=%s (%d)]", endpoint_url, + hstrerror(h_errno), h_errno); #endif CloseSocket(context, 1); return POLARIS_SOCKET_ERROR; @@ -1135,7 +1131,7 @@ static int OpenSocket(PolarisContext_t* context, const char* endpoint_url, // Perform SSL handhshake. ret = SSL_connect(context->ssl); if (ret != 1) { -#if !P1_NO_PRINT +# if !P1_NO_PRINT // Note: We intentionally reuse the receive buffer to store the error // message to be displayed to avoid requiring additional stack here. At this // point we're trying to open the socket, so there should be nobody actively @@ -1144,7 +1140,7 @@ static int OpenSocket(PolarisContext_t* context, const char* endpoint_url, "TLS handshake failed for tcp://%s:%d", endpoint_url, endpoint_port); P1_PrintSSLError(context, (char*)context->recv_buffer, ret); -#endif // !P1_NO_PRINT +# endif // !P1_NO_PRINT CloseSocket(context, 1); return POLARIS_SOCKET_ERROR; } @@ -1159,8 +1155,7 @@ static int OpenSocket(PolarisContext_t* context, const char* endpoint_url, return POLARIS_ERROR; } - P1_PrintDebug("Connected with %s encryption.", - SSL_get_cipher(context->ssl)); + P1_PrintDebug("Connected with %s encryption.", SSL_get_cipher(context->ssl)); ShowCerts(context->ssl); #endif diff --git a/c/src/point_one/polaris/portability.h b/c/src/point_one/polaris/portability.h index 25f1ff1..ee061c2 100644 --- a/c/src/point_one/polaris/portability.h +++ b/c/src/point_one/polaris/portability.h @@ -12,19 +12,21 @@ extern "C" { #endif -#ifdef P1_FREERTOS // FreeRTOS +#ifdef P1_FREERTOS // FreeRTOS -#include // For strerror() +# include // For strerror() -#include "FreeRTOS.h" +# include "FreeRTOS.h" -# ifndef P1_printf -# define P1_printf(format, ...) do {} while(0) -# endif +# ifndef P1_printf +# define P1_printf(format, ...) \ + do { \ + } while (0) +# endif -# ifndef P1_fprintf -# define P1_fprintf(stream, format, ...) P1_printf(format, ##__VA_ARGS__) -# endif +# ifndef P1_fprintf +# define P1_fprintf(stream, format, ...) P1_printf(format, ##__VA_ARGS__) +# endif typedef TickType_t P1_TimeValue_t; @@ -51,19 +53,19 @@ static inline int P1_GetUTCOffsetHours(P1_TimeValue_t* time) { return P1_GetUTCOffsetSec(time) / 3600; } -#else // POSIX +#else // POSIX -# include -# include -# include +# include +# include +# include -# ifndef P1_printf -# define P1_printf printf -# endif +# ifndef P1_printf +# define P1_printf printf +# endif -# ifndef P1_fprintf -# define P1_fprintf fprintf -# endif +# ifndef P1_fprintf +# define P1_fprintf fprintf +# endif typedef struct timeval P1_TimeValue_t; @@ -93,8 +95,8 @@ static inline int P1_GetUTCOffsetHours(P1_TimeValue_t* time) { return P1_GetUTCOffsetSec(time) / 3600; } -#endif // End OS selection +#endif // End OS selection #ifdef __cplusplus -} // extern "C" +} // extern "C" #endif diff --git a/src/point_one/polaris/polaris_client.cc b/src/point_one/polaris/polaris_client.cc index cf4ef7c..c0d9779 100644 --- a/src/point_one/polaris/polaris_client.cc +++ b/src/point_one/polaris/polaris_client.cc @@ -9,17 +9,17 @@ #include #if P1_NO_PRINT -#include +# include static std::ostream null_stream(0); -#define LOG(severity) null_stream -#define VLOG(severity) null_stream -#define VLOG_IS_ON(severity) false -#define LOG_INFO_STREAM(filename, line) null_stream -#define LOG_WARNING_STREAM(filename, line) null_stream -#define LOG_ERROR_STREAM(filename, line) null_stream +# define LOG(severity) null_stream +# define VLOG(severity) null_stream +# define VLOG_IS_ON(severity) false +# define LOG_INFO_STREAM(filename, line) null_stream +# define LOG_WARNING_STREAM(filename, line) null_stream +# define LOG_ERROR_STREAM(filename, line) null_stream #elif POLARIS_NO_GLOG -#include +# include // Reference: // https://stackoverflow.com/questions/49332013/adding-a-new-line-after-stdostream-output-without-explicitly-calling-it @@ -46,44 +46,44 @@ class Stream { static Stream cerr_stream; static std::ostream null_stream(0); -#define LOG(severity) cerr_stream -#if POLARIS_DEBUG -#define VLOG(severity) cerr_stream -#define VLOG_IS_ON(severity) true -#else // !POLARIS_DEBUG -#define VLOG(severity) null_stream -#define VLOG_IS_ON(severity) false -#endif // POLARIS_DEBUG - -#define LOG_INFO_STREAM(filename, line) LOG(INFO) -#define LOG_WARNING_STREAM(filename, line) LOG(WARNING) -#define LOG_ERROR_STREAM(filename, line) LOG(ERROR) - -#else // !P1_NO_PRINT && !POLARIS_NO_GLOG -#include - -#if GOOGLE_STRIP_LOG == 0 -#define LOG_INFO_STREAM(filename, line) \ - google::LogMessage(filename, line, google::GLOG_INFO).stream() -#else -#define LOG_INFO_STREAM(filename, line) google::NullStream() -#endif - -#if GOOGLE_STRIP_LOG <= 1 -#define LOG_WARNING_STREAM(filename, line) \ - google::LogMessage(filename, line, google::GLOG_WARNING).stream() -#else -#define LOG_WARNING_STREAM(filename, line) google::NullStream() -#endif - -#if GOOGLE_STRIP_LOG <= 2 -#define LOG_ERROR_STREAM(filename, line) \ - google::LogMessage(filename, line, google::GLOG_ERROR).stream() -#else -#define LOG_ERROR_STREAM(filename, line) google::NullStream() -#endif - -#endif // P1_NO_PRINT / POLARIS_NO_GLOG +# define LOG(severity) cerr_stream +# if POLARIS_DEBUG +# define VLOG(severity) cerr_stream +# define VLOG_IS_ON(severity) true +# else // !POLARIS_DEBUG +# define VLOG(severity) null_stream +# define VLOG_IS_ON(severity) false +# endif // POLARIS_DEBUG + +# define LOG_INFO_STREAM(filename, line) LOG(INFO) +# define LOG_WARNING_STREAM(filename, line) LOG(WARNING) +# define LOG_ERROR_STREAM(filename, line) LOG(ERROR) + +#else // !P1_NO_PRINT && !POLARIS_NO_GLOG +# include + +# if GOOGLE_STRIP_LOG == 0 +# define LOG_INFO_STREAM(filename, line) \ + google::LogMessage(filename, line, google::GLOG_INFO).stream() +# else +# define LOG_INFO_STREAM(filename, line) google::NullStream() +# endif + +# if GOOGLE_STRIP_LOG <= 1 +# define LOG_WARNING_STREAM(filename, line) \ + google::LogMessage(filename, line, google::GLOG_WARNING).stream() +# else +# define LOG_WARNING_STREAM(filename, line) google::NullStream() +# endif + +# if GOOGLE_STRIP_LOG <= 2 +# define LOG_ERROR_STREAM(filename, line) \ + google::LogMessage(filename, line, google::GLOG_ERROR).stream() +# else +# define LOG_ERROR_STREAM(filename, line) google::NullStream() +# endif + +#endif // P1_NO_PRINT / POLARIS_NO_GLOG using namespace point_one::polaris; @@ -149,9 +149,7 @@ PolarisClient::PolarisClient(const std::string& api_key, } /******************************************************************************/ -PolarisClient::~PolarisClient() { - Disconnect(); -} +PolarisClient::~PolarisClient() { Disconnect(); } /******************************************************************************/ void PolarisClient::SetAPIKey(const std::string& api_key, @@ -188,8 +186,7 @@ void PolarisClient::SetPolarisAuthenticationServer(const std::string& api_url) { std::unique_lock lock(mutex_); if (api_url.empty()) { api_url_ = POLARIS_API_URL; - } - else { + } else { api_url_ = api_url; } } @@ -437,8 +434,7 @@ void PolarisClient::Disconnect() { if (connected_) { VLOG(1) << "Disconnecting from Polaris..."; - } - else { + } else { VLOG(1) << "Already disconnected."; }