Skip to content

Commit

Permalink
Fixed clang-format preprocessor indenting.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamshapiro0 committed Aug 20, 2024
1 parent ac18954 commit fec2e6f
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 150 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
Language: Cpp
BasedOnStyle: Google
IndentPPDirectives: AfterHash
---
Language: Proto
BasedOnStyle: Google
Expand Down
147 changes: 71 additions & 76 deletions c/src/point_one/polaris/polaris.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
#include <string.h> // For memmove()

#ifndef P1_FREERTOS
#include <fcntl.h> // For fcntl()
#include <time.h> // For struct tm
# include <fcntl.h> // For fcntl()
# include <time.h> // For struct tm
#endif

#ifdef POLARIS_USE_TLS
#include <openssl/err.h>
#include <openssl/ssl.h>
# include <openssl/err.h>
# include <openssl/ssl.h>
#endif

#include "point_one/polaris/polaris_internal.h"
Expand All @@ -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;

Expand All @@ -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) {
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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, ...) \
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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]]",
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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

Expand Down
44 changes: 23 additions & 21 deletions c/src/point_one/polaris/portability.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@
extern "C" {
#endif

#ifdef P1_FREERTOS // FreeRTOS
#ifdef P1_FREERTOS // FreeRTOS

#include <string.h> // For strerror()
# include <string.h> // 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;

Expand All @@ -51,19 +53,19 @@ static inline int P1_GetUTCOffsetHours(P1_TimeValue_t* time) {
return P1_GetUTCOffsetSec(time) / 3600;
}

#else // POSIX
#else // POSIX

# include <errno.h>
# include <stdio.h>
# include <sys/time.h>
# include <errno.h>
# include <stdio.h>
# include <sys/time.h>

# 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;

Expand Down Expand Up @@ -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
Loading

0 comments on commit fec2e6f

Please sign in to comment.