From dfae49efaacd7a3b5fc16cc65cbaff35adb37f42 Mon Sep 17 00:00:00 2001 From: Adam Shapiro Date: Tue, 20 Aug 2024 09:29:58 -0400 Subject: [PATCH 01/13] Formatting cleanup. --- c/src/point_one/polaris/polaris.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/c/src/point_one/polaris/polaris.c b/c/src/point_one/polaris/polaris.c index 2030043..f226895 100644 --- a/c/src/point_one/polaris/polaris.c +++ b/c/src/point_one/polaris/polaris.c @@ -8,9 +8,9 @@ #include #include // For PRI* -#include // For sscanf() and snprintf() -#include // For malloc() -#include // For memmove() +#include // For sscanf() and snprintf() +#include // For malloc() +#include // For memmove() #ifndef P1_FREERTOS #include // For fcntl() From de79280778587e5da0d7e41afa7771a30c5293c0 Mon Sep 17 00:00:00 2001 From: Adam Shapiro Date: Tue, 20 Aug 2024 09:33:51 -0400 Subject: [PATCH 02/13] Organized print macros to allow control at any verbosity level. --- c/src/point_one/polaris/polaris.c | 253 +++++++++++++++--------------- c/src/point_one/polaris/polaris.h | 3 + 2 files changed, 127 insertions(+), 129 deletions(-) diff --git a/c/src/point_one/polaris/polaris.c b/c/src/point_one/polaris/polaris.c index f226895..4992d73 100644 --- a/c/src/point_one/polaris/polaris.c +++ b/c/src/point_one/polaris/polaris.c @@ -32,25 +32,21 @@ #define MAKE_STR(x) #x #define STR(x) MAKE_STR(x) -#if defined(P1_NO_PRINT) -#define P1_Print(x, ...) \ - do { \ - } while (0) -#define P1_PrintError(x, ...) \ - do { \ - } while (0) -#define P1_DebugPrint(x, ...) \ - do { \ - } while (0) -#define P1_PrintData(buffer, length) \ - do { \ +#define P1_NOOP \ + do { \ } while (0) + +#if defined(P1_NO_PRINT) +#define P1_PrintMessage(level, x, ...) P1_NOOP +#define P1_PrintErrno(x, ...) P1_NOOP + +#define P1_PrintData(buffer, length) P1_NOOP #if defined(POLARIS_USE_TLS) #define ShowCerts(ssl) \ do { \ } while (0) #endif -#else +#else // !defined(P1_NO_PRINT) static int __log_level = POLARIS_LOG_LEVEL_INFO; static void PrintTime() { @@ -82,38 +78,37 @@ static void PrintTime() { P1_fprintf(stderr, "%02u:%02u:%02u.%03u", hour, min, sec, sec_frac_ms); } -#define P1_Print(x, ...) \ - { \ +#define P1_PrintMessage(level, x, ...) \ + if (__log_level >= level) { \ PrintTime(); \ P1_fprintf(stderr, " polaris.c:" STR(__LINE__) "] " x, ##__VA_ARGS__); \ } -#define P1_PrintError(x, ...) \ - { \ +#define P1_PrintErrno(x, ...) \ + if (__log_level >= POLARIS_LOG_LEVEL_ERROR) { \ PrintTime(); \ P1_perror(" polaris.c:" STR(__LINE__) "] " x, ##__VA_ARGS__); \ } -#define P1_DebugPrint(x, ...) \ - if (__log_level >= POLARIS_LOG_LEVEL_DEBUG) { \ - P1_Print(x, ##__VA_ARGS__); \ - } -#define P1_TracePrint(x, ...) \ - if (__log_level >= POLARIS_LOG_LEVEL_TRACE) { \ - P1_Print(x, ##__VA_ARGS__); \ - } static void P1_PrintData(const uint8_t* buffer, size_t length); #if defined(POLARIS_USE_TLS) static void ShowCerts(SSL* ssl); #endif -#endif +#endif // defined(P1_NO_PRINT) + +#define P1_PrintError(x, ...) \ + P1_PrintMessage(POLARIS_LOG_LEVEL_ERROR, x, ##__VA_ARGS__) +#define P1_PrintWarning(x, ...) \ + P1_PrintMessage(POLARIS_LOG_LEVEL_WARNING, x, ##__VA_ARGS__) +#define P1_PrintInfo(x, ...) \ + P1_PrintMessage(POLARIS_LOG_LEVEL_INFO, x, ##__VA_ARGS__) +#define P1_PrintDebug(x, ...) \ + P1_PrintMessage(POLARIS_LOG_LEVEL_DEBUG, x, ##__VA_ARGS__) +#define P1_PrintTrace(x, ...) \ + P1_PrintMessage(POLARIS_LOG_LEVEL_TRACE, x, ##__VA_ARGS__) #if defined(POLARIS_NO_PRINT) -#define P1_PrintReadWriteError(context, x, ret) \ - do { \ - } while (0) -#define P1_PrintSSLError(context, x, ret) \ - do { \ - } while (0) +#define P1_PrintReadWriteError(context, x, ret) P1_NOOP +#define P1_PrintSSLError(context, x, ret) P1_NOOP #elif defined(POLARIS_USE_TLS) static void __P1_PrintSSLError(int line, PolarisContext_t* context, const char* message, int ret) { @@ -173,12 +168,10 @@ static void __P1_PrintSSLError(int line, PolarisContext_t* context, __P1_PrintSSLError(__LINE__, context, x, ret) #define P1_PrintSSLError(context, x, ret) \ __P1_PrintSSLError(__LINE__, context, x, ret) -#else -#define P1_PrintReadWriteError(context, x, ret) P1_PrintError(x, ret) -#define P1_PrintSSLError(context, x, ret) \ - do { \ - } while (0) -#endif +#else // !defined(POLARIS_NO_PRINT) && !defined(POLARIS_USE_TLS) +#define P1_PrintReadWriteError(context, x, ret) P1_PrintErrno(x, ret) +#define P1_PrintSSLError(context, x, ret) P1_NOOP +#endif // POLARIS_NO_PRINT / POLARIS_USE_TLS #define P1_DebugPrintReadWriteError(context, x, ret) \ if (__log_level >= POLARIS_LOG_LEVEL_DEBUG) { \ @@ -201,13 +194,13 @@ static void CloseSocket(PolarisContext_t* context, int destroy_context); /******************************************************************************/ int Polaris_Init(PolarisContext_t* context) { if (POLARIS_RECV_BUFFER_SIZE < POLARIS_MAX_HTTP_MESSAGE_SIZE) { - P1_Print( + P1_PrintWarning( "Warning: Receive buffer smaller than expected authentication " "response.\n"); } if (POLARIS_SEND_BUFFER_SIZE < POLARIS_MAX_MESSAGE_SIZE) { - P1_Print( + P1_PrintWarning( "Warning: Send buffer smaller than max expected outbound packet.\n"); } @@ -252,7 +245,7 @@ int Polaris_AuthenticateTo(PolarisContext_t* context, const char* api_key, const char* unique_id, const char* api_url) { // Sanity check the inputs. if (strlen(api_key) == 0) { - P1_Print("API key must not be empty.\n"); + P1_PrintError("API key must not be empty.\n"); return POLARIS_ERROR; } @@ -279,11 +272,11 @@ int Polaris_AuthenticateTo(PolarisContext_t* context, const char* api_key, POLARIS_RECV_BUFFER_SIZE, AUTH_REQUEST_TEMPLATE, api_key, unique_id == NULL ? "" : unique_id); if (content_size < 0) { - P1_Print("Error populating authentication request payload.\n"); + P1_PrintError("Error populating authentication request payload.\n"); return POLARIS_NOT_ENOUGH_SPACE; } - P1_DebugPrint( + P1_PrintDebug( "Sending auth request. [api_key=%.7s..., unique_id=%s, url=%s]\n", api_key, unique_id, api_url); context->auth_token[0] = '\0'; @@ -295,7 +288,7 @@ int Polaris_AuthenticateTo(PolarisContext_t* context, const char* api_key, context->recv_buffer, (size_t)content_size); #endif if (status_code < 0) { - P1_Print("Error sending authentication request.\n"); + P1_PrintError("Error sending authentication request.\n"); return status_code; } @@ -304,24 +297,24 @@ int Polaris_AuthenticateTo(PolarisContext_t* context, const char* api_key, const char* token_start = strstr((char*)context->recv_buffer, "\"access_token\":\""); if (token_start == NULL) { - P1_Print("Authentication token not found in response.\n"); + P1_PrintError("Authentication token not found in response.\n"); return POLARIS_AUTH_ERROR; } else { token_start += 16; if (sscanf(token_start, "%" STR(POLARIS_MAX_TOKEN_SIZE) "[^\"]s", context->auth_token) != 1) { - P1_Print("Authentication token not found in response.\n"); + P1_PrintError("Authentication token not found in response.\n"); return POLARIS_AUTH_ERROR; } else { - P1_DebugPrint("Received access token: %s\n", context->auth_token); + P1_PrintDebug("Received access token: %s\n", context->auth_token); return POLARIS_SUCCESS; } } } else if (status_code == 403) { - P1_Print("Authentication failed. Please check your API key.\n"); + P1_PrintError("Authentication failed. Please check your API key.\n"); return POLARIS_FORBIDDEN; } else { - P1_Print("Unexpected authentication response (%d).\n", status_code); + P1_PrintError("Unexpected authentication response (%d).\n", status_code); return POLARIS_AUTH_ERROR; } } @@ -330,14 +323,14 @@ int Polaris_AuthenticateTo(PolarisContext_t* context, const char* api_key, int Polaris_SetAuthToken(PolarisContext_t* context, const char* auth_token) { size_t length = strlen(auth_token); if (length == 0) { - P1_Print("User-provided auth token must not be empty.\n"); + P1_PrintError("User-provided auth token must not be empty.\n"); return POLARIS_ERROR; } else if (length > POLARIS_MAX_TOKEN_SIZE) { - P1_Print("User-provided auth token is too long.\n"); + P1_PrintError("User-provided auth token is too long.\n"); return POLARIS_NOT_ENOUGH_SPACE; } else { memcpy(context->auth_token, auth_token, length + 1); - P1_DebugPrint("Using user-specified access token: %s\n", + P1_PrintDebug("Using user-specified access token: %s\n", context->auth_token); return POLARIS_SUCCESS; } @@ -358,7 +351,7 @@ int Polaris_Connect(PolarisContext_t* context) { int Polaris_ConnectTo(PolarisContext_t* context, const char* endpoint_url, int endpoint_port) { if (context->auth_token[0] == '\0') { - P1_Print("Error: Auth token not specified.\n"); + P1_PrintError("Error: Auth token not specified.\n"); return POLARIS_AUTH_ERROR; } @@ -369,8 +362,8 @@ int Polaris_ConnectTo(PolarisContext_t* context, const char* endpoint_url, context->data_request_sent = 0; int ret = OpenSocket(context, endpoint_url, endpoint_port); if (ret != POLARIS_SUCCESS) { - P1_Print("Error connecting to corrections endpoint: tcp://%s:%d.\n", - endpoint_url, endpoint_port); + P1_PrintError("Error connecting to corrections endpoint: tcp://%s:%d.\n", + endpoint_url, endpoint_port); return ret; } @@ -385,7 +378,7 @@ int Polaris_ConnectTo(PolarisContext_t* context, const char* endpoint_url, memmove(header + 1, context->auth_token, token_length); size_t message_size = Polaris_PopulateChecksum(context->recv_buffer); - P1_DebugPrint("Sending access token message. [size=%u B]\n", + P1_PrintDebug("Sending access token message. [size=%u B]\n", (unsigned)message_size); #ifdef POLARIS_USE_TLS ret = SSL_write(context->ssl, context->recv_buffer, message_size); @@ -419,8 +412,8 @@ int Polaris_ConnectWithoutAuth(PolarisContext_t* context, context->data_request_sent = 0; ret = OpenSocket(context, endpoint_url, endpoint_port); if (ret != POLARIS_SUCCESS) { - P1_Print("Error connecting to corrections endpoint: tcp://%s:%d.\n", - endpoint_url, endpoint_port); + P1_PrintError("Error connecting to corrections endpoint: tcp://%s:%d.\n", + endpoint_url, endpoint_port); return ret; } @@ -432,7 +425,7 @@ int Polaris_ConnectWithoutAuth(PolarisContext_t* context, memmove(header + 1, unique_id, id_length); size_t message_size = Polaris_PopulateChecksum(context->send_buffer); - P1_DebugPrint("Sending unique ID message. [size=%u B]\n", + P1_PrintDebug("Sending unique ID message. [size=%u B]\n", (unsigned)message_size); #ifdef POLARIS_USE_TLS ret = SSL_write(context->ssl, context->send_buffer, message_size); @@ -454,7 +447,7 @@ int Polaris_ConnectWithoutAuth(PolarisContext_t* context, /******************************************************************************/ void Polaris_Disconnect(PolarisContext_t* context) { if (context->socket != P1_INVALID_SOCKET) { - P1_DebugPrint("Closing Polaris connection.\n"); + P1_PrintDebug("Closing Polaris connection.\n"); context->disconnected = 1; #ifdef POLARIS_USE_TLS SSL_shutdown(context->ssl); @@ -480,12 +473,12 @@ void Polaris_SetRTCMCallback(PolarisContext_t* context, int Polaris_SendECEFPosition(PolarisContext_t* context, double x_m, double y_m, double z_m) { if (context->socket == P1_INVALID_SOCKET) { - P1_Print("Error: Polaris connection not currently open.\n"); + P1_PrintError("Error: Polaris connection not currently open.\n"); return POLARIS_SOCKET_ERROR; } #ifdef POLARIS_USE_TLS else if (context->ssl_ctx == NULL || context->ssl == NULL) { - P1_Print("Error: Polaris SSL context not available.\n"); + P1_PrintError("Error: Polaris SSL context not available.\n"); return POLARIS_SOCKET_ERROR; } #endif @@ -500,12 +493,12 @@ int Polaris_SendECEFPosition(PolarisContext_t* context, double x_m, double y_m, #ifdef P1_FREERTOS // Floating point printf() not available in FreeRTOS. - P1_DebugPrint( + P1_PrintDebug( "Sending ECEF position. [size=%u B, position=[%d, %d, %d] cm]\n", (unsigned)message_size, le32toh(payload->x_cm), le32toh(payload->y_cm), le32toh(payload->z_cm)); #else - P1_DebugPrint( + P1_PrintDebug( "Sending ECEF position. [size=%u B, position=[%.2f, %.2f, %.2f]]\n", (unsigned)message_size, x_m, y_m, z_m); #endif @@ -530,12 +523,12 @@ int Polaris_SendECEFPosition(PolarisContext_t* context, double x_m, double y_m, int Polaris_SendLLAPosition(PolarisContext_t* context, double latitude_deg, double longitude_deg, double altitude_m) { if (context->socket == P1_INVALID_SOCKET) { - P1_Print("Error: Polaris connection not currently open.\n"); + P1_PrintError("Error: Polaris connection not currently open.\n"); return POLARIS_SOCKET_ERROR; } #ifdef POLARIS_USE_TLS else if (context->ssl_ctx == NULL || context->ssl == NULL) { - P1_Print("Error: Polaris SSL context not available.\n"); + P1_PrintError("Error: Polaris SSL context not available.\n"); return POLARIS_SOCKET_ERROR; } #endif @@ -553,12 +546,12 @@ int Polaris_SendLLAPosition(PolarisContext_t* context, double latitude_deg, #ifdef P1_FREERTOS // Floating point printf() not available in FreeRTOS. - P1_DebugPrint( + P1_PrintDebug( "Sending LLA position. [size=%u B, position=[%d.0e-7, %d.0e-7, %d]]\n", (unsigned)message_size, le32toh(payload->latitude_dege7), le32toh(payload->longitude_dege7), le32toh(payload->altitude_mm)); #else - P1_DebugPrint( + P1_PrintDebug( "Sending LLA position. [size=%u B, position=[%.6f, %.6f, %.2f]]\n", (unsigned)message_size, latitude_deg, longitude_deg, altitude_m); #endif @@ -582,12 +575,12 @@ int Polaris_SendLLAPosition(PolarisContext_t* context, double latitude_deg, /******************************************************************************/ int Polaris_RequestBeacon(PolarisContext_t* context, const char* beacon_id) { if (context->socket == P1_INVALID_SOCKET) { - P1_Print("Error: Polaris connection not currently open.\n"); + P1_PrintError("Error: Polaris connection not currently open.\n"); return POLARIS_SOCKET_ERROR; } #ifdef POLARIS_USE_TLS else if (context->ssl_ctx == NULL || context->ssl == NULL) { - P1_Print("Error: Polaris SSL context not available.\n"); + P1_PrintError("Error: Polaris SSL context not available.\n"); return POLARIS_SOCKET_ERROR; } #endif @@ -598,7 +591,7 @@ int Polaris_RequestBeacon(PolarisContext_t* context, const char* beacon_id) { memmove(header + 1, beacon_id, id_length); size_t message_size = Polaris_PopulateChecksum(context->send_buffer); - P1_DebugPrint("Sending beacon request. [size=%u B, beacon='%s']\n", + P1_PrintDebug("Sending beacon request. [size=%u B, beacon='%s']\n", (unsigned)message_size, beacon_id); P1_PrintData(context->send_buffer, message_size); @@ -623,16 +616,16 @@ int Polaris_Work(PolarisContext_t* context) { // in case this function is called after Polaris_Disconnect() to clean up the // SSL session. if (context->disconnected) { - P1_DebugPrint("Connection terminated by user request.\n"); + P1_PrintDebug("Connection terminated by user request.\n"); CloseSocket(context, 1); return POLARIS_CONNECTION_CLOSED; } else if (context->socket == P1_INVALID_SOCKET) { - P1_Print("Error: Polaris connection not currently open.\n"); + P1_PrintError("Error: Polaris connection not currently open.\n"); CloseSocket(context, 1); return POLARIS_SOCKET_ERROR; } - P1_DebugPrint("Listening for data block.\n"); + P1_PrintDebug("Listening for data block.\n"); #ifdef POLARIS_USE_TLS P1_RecvSize_t bytes_read = @@ -651,7 +644,7 @@ int Polaris_Work(PolarisContext_t* context) { // we set bytes_read to -1 and set errno accordingly. // // Later, we will need to restore them both before calling - // P1_PrintReadWriteError(). That function and P1_PrintError(), which it calls + // P1_PrintReadWriteError(). That function and P1_PrintErrno(), which it calls // if TLS is disabled, expect to be provided the return code, not errno. P1_RecvSize_t original_bytes_read = bytes_read; int original_errno = errno; @@ -684,8 +677,8 @@ int Polaris_Work(PolarisContext_t* context) { #endif #ifdef P1_FREERTOS - bytes_read = original_bytes_read; - errno = original_errno; + bytes_read = original_bytes_read; + errno = original_errno; #endif if (context->disconnected) { P1_DebugPrintReadWriteError(context, "Socket read timed out", @@ -712,7 +705,7 @@ int Polaris_Work(PolarisContext_t* context) { // Was the connection closed by the user? if (context->disconnected) { if (bytes_read == 0 || errno == EINTR || errno == ENOTCONN) { - P1_DebugPrint("Connection terminated by user request.\n"); + P1_PrintDebug("Connection terminated by user request.\n"); } else { #ifdef P1_FREERTOS bytes_read = original_bytes_read; @@ -731,7 +724,7 @@ int Polaris_Work(PolarisContext_t* context) { // closed the connection without error. Once connected, we expect the // stream to stay open indefinitely under normal circumstances. The // server should not have a reason to terminate the connection. - P1_Print("Warning: Connection terminated remotely.\n"); + P1_PrintWarning("Warning: Connection terminated remotely.\n"); } else { #ifdef P1_FREERTOS bytes_read = original_bytes_read; @@ -775,7 +768,7 @@ int Polaris_Work(PolarisContext_t* context) { // Not authenticated, but response received: likely server indicated an // error. else if (context->total_bytes_received > 0) { - P1_Print( + P1_PrintWarning( "Warning: Polaris connection closed with an error response. Is " "your authentication token valid?\n"); ret = POLARIS_FORBIDDEN; @@ -783,14 +776,14 @@ int Polaris_Work(PolarisContext_t* context) { // Data request sent but no response received: authentication (presumably) // accepted but position likely out of network. else if (context->data_request_sent) { - P1_Print( + P1_PrintWarning( "Warning: Polaris connection closed and no response received " "from server.\n"); } // Data request not sent and no response received: authentication // (presumably) accepted. else { - P1_Print( + P1_PrintWarning( "Warning: Polaris connection closed and no position or beacon " "request issued.\n"); } @@ -799,7 +792,7 @@ int Polaris_Work(PolarisContext_t* context) { return ret; } else { context->total_bytes_received += bytes_read; - P1_DebugPrint("Received %u bytes. [%" PRIu64 " bytes total]\n", + P1_PrintDebug("Received %u bytes. [%" PRIu64 " bytes total]\n", (unsigned)bytes_read, (uint64_t)context->total_bytes_received); P1_PrintData(context->recv_buffer, bytes_read); @@ -815,7 +808,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_DebugPrint( + P1_PrintDebug( "Sufficient data received. Authentication token accepted.\n"); context->authenticated = POLARIS_AUTHENTICATED; } @@ -828,7 +821,7 @@ int Polaris_Work(PolarisContext_t* context) { } if (context->disconnected) { - P1_DebugPrint("Connection terminated by user.\n"); + P1_PrintDebug("Connection terminated by user.\n"); return POLARIS_SUCCESS; } else { return (int)bytes_read; @@ -842,16 +835,16 @@ int Polaris_Run(PolarisContext_t* context, int connection_timeout_ms) { // in case this function is called after Polaris_Disconnect() to clean up the // SSL session. if (context->disconnected) { - P1_DebugPrint("Connection terminated by user request.\n"); + P1_PrintDebug("Connection terminated by user request.\n"); CloseSocket(context, 1); return POLARIS_SUCCESS; } else if (context->socket == P1_INVALID_SOCKET) { - P1_Print("Error: Polaris connection not currently open.\n"); + P1_PrintError("Error: Polaris connection not currently open.\n"); CloseSocket(context, 1); return POLARIS_SOCKET_ERROR; } - P1_DebugPrint("Listening for data.\n"); + P1_PrintDebug("Listening for data.\n"); P1_TimeValue_t last_read_time; P1_GetCurrentTime(&last_read_time); @@ -873,9 +866,10 @@ int Polaris_Run(PolarisContext_t* context, int connection_timeout_ms) { P1_TimeValue_t current_time; P1_GetCurrentTime(¤t_time); int elapsed_ms = P1_GetElapsedMS(&last_read_time, ¤t_time); - P1_DebugPrint("%d ms elapsed since last data arrived.\n", elapsed_ms); + P1_PrintDebug("%d ms elapsed since last data arrived.\n", elapsed_ms); if (elapsed_ms >= connection_timeout_ms) { - P1_Print("Warning: Connection timed out after %d ms.\n", elapsed_ms); + P1_PrintWarning("Warning: Connection timed out after %d ms.\n", + elapsed_ms); CloseSocket(context, 1); ret = POLARIS_TIMED_OUT; break; @@ -892,7 +886,7 @@ int Polaris_Run(PolarisContext_t* context, int connection_timeout_ms) { P1_GetCurrentTime(&last_read_time); if (context->disconnected) { - P1_DebugPrint("Connection terminated by user.\n"); + P1_PrintDebug("Connection terminated by user.\n"); CloseSocket(context, 1); ret = POLARIS_SUCCESS; break; @@ -900,7 +894,7 @@ int Polaris_Run(PolarisContext_t* context, int connection_timeout_ms) { } } - P1_DebugPrint("Received %" PRIu64 " total bytes.\n", + P1_PrintDebug("Received %" PRIu64 " total bytes.\n", (uint64_t)context->total_bytes_received); if (ret == POLARIS_CONNECTION_CLOSED && context->disconnected) { return POLARIS_SUCCESS; @@ -917,15 +911,15 @@ static int ValidateUniqueID(const char* unique_id) { // autogenerated on the back end. return POLARIS_SUCCESS; } else if (length > POLARIS_MAX_UNIQUE_ID_SIZE) { - P1_Print("Unique ID must be a maximum of %d characters. [id='%s']\n", - POLARIS_MAX_UNIQUE_ID_SIZE, unique_id); + P1_PrintError("Unique ID must be a maximum of %d characters. [id='%s']\n", + POLARIS_MAX_UNIQUE_ID_SIZE, unique_id); return POLARIS_ERROR; } else { for (const char* ptr = unique_id; *ptr != '\0'; ++ptr) { char c = *ptr; if (c != '-' && c != '_' && (c < 'A' || c > 'Z') && (c < 'a' || c > 'z') && (c < '0' || c > '9')) { - P1_Print("Invalid unique ID specified. [id='%s']\n", unique_id); + P1_PrintError("Invalid unique ID specified. [id='%s']\n", unique_id); return POLARIS_ERROR; } } @@ -953,12 +947,13 @@ 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_Print("Unable to resolve \"%s\": %s\n", hostname, hstrerror(h_errno)); + P1_PrintError("Unable to resolve \"%s\": %s\n", hostname, + hstrerror(h_errno)); return -1; } // IPv6 not currently supported by the API. else if (host_info->h_addrtype != AF_INET) { - P1_Print( + P1_PrintWarning( "Warning: DNS lookup for \"%s\" returned an IPv6 address (not " "supported).\n", hostname); @@ -980,19 +975,19 @@ static int OpenSocket(PolarisContext_t* context, const char* endpoint_url, int endpoint_port) { // Is the connection already open? if (context->socket != P1_INVALID_SOCKET) { - P1_Print("Error: socket already open.\n"); + P1_PrintError("Error: socket already open.\n"); return POLARIS_ERROR; } #ifdef POLARIS_USE_TLS else if (context->ssl_ctx != NULL || context->ssl != NULL) { - P1_Print("Error: SSL context not freed.\n"); + P1_PrintError("Error: SSL context not freed.\n"); return POLARIS_ERROR; } #endif #ifdef POLARIS_USE_TLS // Configure TLS. - P1_DebugPrint("Configuring TLS context.\n"); + P1_PrintDebug("Configuring TLS context.\n"); #if OPENSSL_VERSION_NUMBER < 0x10100000L context->ssl_ctx = SSL_CTX_new(TLSv1_2_client_method()); #else @@ -1004,7 +999,7 @@ static int OpenSocket(PolarisContext_t* context, const char* endpoint_url, SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_TLSv1); if (context->ssl_ctx == NULL) { - P1_Print("SSL context failed to initialize.\n"); + P1_PrintError("SSL context failed to initialize.\n"); return POLARIS_ERROR; } #endif @@ -1012,12 +1007,12 @@ static int OpenSocket(PolarisContext_t* context, const char* endpoint_url, // Open a socket. context->socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (context->socket < 0) { - P1_PrintError("Error creating socket", context->socket); + P1_PrintErrno("Error creating socket", context->socket); CloseSocket(context, 1); return POLARIS_SOCKET_ERROR; } - P1_DebugPrint( + P1_PrintDebug( "Configuring socket. [socket=%d, read_timeout=%d ms, send_timeout=%d " "ms]\n", context->socket, POLARIS_RECV_TIMEOUT_MS, POLARIS_SEND_TIMEOUT_MS); @@ -1033,18 +1028,18 @@ static int OpenSocket(PolarisContext_t* context, const char* endpoint_url, #ifndef P1_FREERTOS int flags = fcntl(context->socket, F_GETFL); - P1_DebugPrint("Socket flags: 0x%08x\n", flags); + P1_PrintDebug("Socket flags: 0x%08x\n", flags); #endif // P1_FREERTOS // Lookup the IP of the endpoint used for auth requests. - P1_DebugPrint("Performing DNS lookup for '%s'.\n", endpoint_url); + P1_PrintDebug("Performing DNS lookup for '%s'.\n", endpoint_url); P1_SocketAddrV4_t address; if (P1_SetAddress(endpoint_url, endpoint_port, &address) < 0) { #ifdef P1_FREERTOS - P1_Print("Error locating address '%s'.\n", endpoint_url); + P1_PrintError("Error locating address '%s'.\n", endpoint_url); #else - P1_Print("Error locating address '%s'. [error=%s (%d)]\n", endpoint_url, - hstrerror(h_errno), h_errno); + P1_PrintError("Error locating address '%s'. [error=%s (%d)]\n", + endpoint_url, hstrerror(h_errno), h_errno); #endif CloseSocket(context, 1); return POLARIS_SOCKET_ERROR; @@ -1056,22 +1051,22 @@ static int OpenSocket(PolarisContext_t* context, const char* endpoint_url, #else uint32_t ip_host_endian = ntohl(address.sin_addr.s_addr); #endif - P1_DebugPrint("Connecting to 'tcp://%d.%d.%d.%d:%d'.\n", + P1_PrintDebug("Connecting to 'tcp://%d.%d.%d.%d:%d'.\n", (ip_host_endian >> 24) & 0xFF, (ip_host_endian >> 16) & 0xFF, (ip_host_endian >> 8) & 0xFF, ip_host_endian & 0xFF, endpoint_port); int ret = connect(context->socket, (P1_SocketAddr_t*)&address, sizeof(address)); if (ret < 0) { - P1_PrintError("Error connecting to endpoint", ret); + P1_PrintErrno("Error connecting to endpoint", ret); CloseSocket(context, 1); return POLARIS_SOCKET_ERROR; } - P1_DebugPrint("Connected successfully.\n"); + P1_PrintDebug("Connected successfully.\n"); #ifdef POLARIS_USE_TLS // Create new SSL connection state and attach the socket. - P1_DebugPrint("Establishing TLS connection.\n"); + P1_PrintDebug("Establishing TLS connection.\n"); context->ssl = SSL_new(context->ssl_ctx); SSL_set_fd(context->ssl, context->socket); @@ -1098,7 +1093,7 @@ static int OpenSocket(PolarisContext_t* context, const char* endpoint_url, const SSL_CIPHER* c = SSL_get_current_cipher(context->ssl); if (c == NULL) { - P1_Print( + P1_PrintError( "Server failed to negotiate encryption cipher. Verify that endpoint " "tcp://%s:%d supports TLS 1.2 or better.\n", endpoint_url, endpoint_port); @@ -1106,7 +1101,7 @@ static int OpenSocket(PolarisContext_t* context, const char* endpoint_url, return POLARIS_ERROR; } - P1_DebugPrint("Connected with %s encryption.\n", + P1_PrintDebug("Connected with %s encryption.\n", SSL_get_cipher(context->ssl)); ShowCerts(context->ssl); #endif @@ -1118,7 +1113,7 @@ static int OpenSocket(PolarisContext_t* context, const char* endpoint_url, void CloseSocket(PolarisContext_t* context, int destroy_context) { #ifdef POLARIS_USE_TLS if (destroy_context && context->ssl != NULL) { - P1_DebugPrint("Shutting down TLS context.\n"); + P1_PrintDebug("Shutting down TLS context.\n"); if (SSL_get_shutdown(context->ssl) == 0) { SSL_shutdown(context->ssl); } @@ -1129,14 +1124,14 @@ void CloseSocket(PolarisContext_t* context, int destroy_context) { #endif if (context->socket != P1_INVALID_SOCKET) { - P1_DebugPrint("Closing socket.\n"); + P1_PrintDebug("Closing socket.\n"); close(context->socket); context->socket = P1_INVALID_SOCKET; } #ifdef POLARIS_USE_TLS if (destroy_context && context->ssl_ctx != NULL) { - P1_DebugPrint("Freeing TLS context.\n"); + P1_PrintDebug("Freeing TLS context.\n"); SSL_CTX_free(context->ssl_ctx); context->ssl_ctx = NULL; } @@ -1187,7 +1182,7 @@ static int SendPOSTRequest(PolarisContext_t* context, const char* endpoint_url, // larger than the send buffer. We currently only send HTTP requests during // authentication, before data is coming in. if (POLARIS_RECV_BUFFER_SIZE < header_size + content_length + 1) { - P1_Print("Error populating POST request: buffer too small.\n"); + P1_PrintError("Error populating POST request: buffer too small.\n"); CloseSocket(context, 1); return POLARIS_NOT_ENOUGH_SPACE; } @@ -1203,7 +1198,7 @@ static int SendPOSTRequest(PolarisContext_t* context, const char* endpoint_url, address, endpoint_url, port_str, content_length_str); if (header_size < 0) { // This shouldn't happen. - P1_Print("Error populating POST request.\n"); + P1_PrintError("Error populating POST request.\n"); CloseSocket(context, 1); return POLARIS_ERROR; } @@ -1218,7 +1213,7 @@ static int SendPOSTRequest(PolarisContext_t* context, const char* endpoint_url, return ret; } - P1_DebugPrint("Sending POST request. [size=%u B]\n", (unsigned)message_size); + P1_PrintDebug("Sending POST request. [size=%u B]\n", (unsigned)message_size); #ifdef POLARIS_USE_TLS ret = SSL_write(context->ssl, context->recv_buffer, message_size); #else @@ -1260,7 +1255,7 @@ static int GetHTTPResponse(PolarisContext_t* context) { // Unlike POSIX recv(), which returns <0 and sets ETIMEDOUT on a socket read // timeout, FreeRTOS returns 0. if (bytes_read == 0) { - P1_Print("Socket timed out waiting for HTTP response.\n"); + P1_PrintWarning("Socket timed out waiting for HTTP response.\n"); CloseSocket(context, 1); return POLARIS_SEND_ERROR; } @@ -1291,7 +1286,7 @@ static int GetHTTPResponse(PolarisContext_t* context) { // context->ssl, which is freed by CloseSocket(). CloseSocket(context, 1); - P1_DebugPrint("Received HTTP response. [size=%u B]\n", (unsigned)total_bytes); + P1_PrintDebug("Received HTTP response. [size=%u B]\n", (unsigned)total_bytes); // Append a null terminator to the response. context->recv_buffer[total_bytes++] = '\0'; @@ -1299,7 +1294,7 @@ static int GetHTTPResponse(PolarisContext_t* context) { // Extract the status code. int status_code; if (sscanf((char*)context->recv_buffer, "HTTP/1.1 %d", &status_code) != 1) { - P1_Print("Invalid HTTP response:\n\n%s", context->recv_buffer); + P1_PrintError("Invalid HTTP response:\n\n%s", context->recv_buffer); return POLARIS_SEND_ERROR; } @@ -1311,10 +1306,10 @@ static int GetHTTPResponse(PolarisContext_t* context) { size_t content_length = total_bytes - (content_start - (char*)context->recv_buffer); memmove(context->recv_buffer, content_start, content_length); - P1_DebugPrint("Response content:\n%s\n", context->recv_buffer); + P1_PrintDebug("Response content:\n%s\n", context->recv_buffer); } else { // No content in response. - P1_DebugPrint("No content in response.\n"); + P1_PrintDebug("No content in response.\n"); context->recv_buffer[0] = '\0'; } @@ -1353,13 +1348,13 @@ void ShowCerts(SSL* ssl) { X509* cert = SSL_get_peer_certificate(ssl); if (cert != NULL) { char line[256]; - P1_DebugPrint("Server certificates:\n"); + P1_PrintDebug("Server certificates:\n"); X509_NAME_oneline(X509_get_subject_name(cert), line, sizeof(line)); - P1_DebugPrint(" Subject: %s\n", line); + P1_PrintDebug(" Subject: %s\n", line); X509_NAME_oneline(X509_get_issuer_name(cert), line, sizeof(line)); - P1_DebugPrint(" Issuer: %s\n", line); + P1_PrintDebug(" Issuer: %s\n", line); } else { - P1_DebugPrint("No client certificates configured.\n"); + P1_PrintDebug("No client certificates configured.\n"); } } #endif diff --git a/c/src/point_one/polaris/polaris.h b/c/src/point_one/polaris/polaris.h index 3d1ac75..f7b8f9f 100644 --- a/c/src/point_one/polaris/polaris.h +++ b/c/src/point_one/polaris/polaris.h @@ -90,6 +90,9 @@ * @name Polaris Logging Verbosity Levels * @{ */ +#define POLARIS_LOG_LEVEL_FATAL -3 +#define POLARIS_LOG_LEVEL_ERROR -2 +#define POLARIS_LOG_LEVEL_WARNING -1 #define POLARIS_LOG_LEVEL_INFO 0 #define POLARIS_LOG_LEVEL_DEBUG 1 #define POLARIS_LOG_LEVEL_TRACE 2 From be9c328ac821a53e03ee38500f5bd506a318aef4 Mon Sep 17 00:00:00 2001 From: Adam Shapiro Date: Tue, 20 Aug 2024 16:21:41 -0400 Subject: [PATCH 03/13] Added print callback support. --- c/src/point_one/polaris/polaris.c | 289 ++++++++++++++++---------- c/src/point_one/polaris/polaris.h | 27 +++ c/src/point_one/polaris/portability.h | 21 -- 3 files changed, 209 insertions(+), 128 deletions(-) diff --git a/c/src/point_one/polaris/polaris.c b/c/src/point_one/polaris/polaris.c index 4992d73..1ad0965 100644 --- a/c/src/point_one/polaris/polaris.c +++ b/c/src/point_one/polaris/polaris.c @@ -8,6 +8,7 @@ #include #include // For PRI* +#include // For va_list support #include // For sscanf() and snprintf() #include // For malloc() #include // For memmove() @@ -49,7 +50,9 @@ #else // !defined(P1_NO_PRINT) static int __log_level = POLARIS_LOG_LEVEL_INFO; -static void PrintTime() { +static PolarisPrintCallback_t __print_callback = NULL; + +static int PrintTime(char* buffer, size_t capacity_bytes) { // Get the current _local_ time (not UTC). P1_TimeValue_t now; P1_GetCurrentTime(&now); @@ -66,28 +69,76 @@ static void PrintTime() { // 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 time_t now_sec = (time_t)now.tv_sec; struct tm local_time = *localtime(&now_sec); - P1_fprintf(stderr, "%04u%02u%02u ", 1900 + local_time.tm_year, - local_time.tm_mon + 1, local_time.tm_mday); + if (buffer) { + length += snprintf(buffer, capacity_bytes, "%04u%02u%02u ", + 1900 + local_time.tm_year, local_time.tm_mon + 1, + local_time.tm_mday); + buffer += length; + capacity_bytes -= length; + } else { + P1_fprintf(stderr, "%04u%02u%02u ", 1900 + local_time.tm_year, + local_time.tm_mon + 1, local_time.tm_mday); + } #endif // At a minimum, print HH:MM:SS.SSS. For FreeRTOS devices, this will likely be // elapsed time since boot. - P1_fprintf(stderr, "%02u:%02u:%02u.%03u", hour, min, sec, sec_frac_ms); + if (buffer) { + length += snprintf(buffer, capacity_bytes, "%02u:%02u:%02u.%03u", hour, min, + sec, sec_frac_ms); + } else { + P1_fprintf(stderr, "%02u:%02u:%02u.%03u", hour, min, sec, sec_frac_ms); + } + + return length; } -#define P1_PrintMessage(level, x, ...) \ - if (__log_level >= level) { \ - PrintTime(); \ - P1_fprintf(stderr, " polaris.c:" STR(__LINE__) "] " x, ##__VA_ARGS__); \ - } -#define P1_PrintErrno(x, ...) \ - if (__log_level >= POLARIS_LOG_LEVEL_ERROR) { \ - PrintTime(); \ - P1_perror(" polaris.c:" STR(__LINE__) "] " x, ##__VA_ARGS__); \ - } +static void P1_PrintToCallback(int level, const char* format, ...) { + char buffer[POLARIS_MAX_PRINT_LENGTH + 1]; + va_list args; + va_start(args, format); + vsnprintf(buffer, sizeof(buffer), format, args); + va_end(args); + __print_callback(level, buffer); +} + +#define P1_DoPrint(level, x, ...) \ + if (__log_level >= level) { \ + if (__print_callback) { \ + P1_PrintToCallback(level, x, ##__VA_ARGS__); \ + } else { \ + PrintTime(NULL, 0); \ + P1_fprintf(stderr, x "\n", ##__VA_ARGS__); \ + } \ + } + +#define P1_PrintMessage(level, x, ...) \ + P1_DoPrint(level, " polaris.c:" STR(__LINE__) "] " 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 +// it anyway since we want to support using the print callback and perror() +// prints directly to stderr. We do try to make this macro at least similar to +// perror(), so it adds a trailing '.', etc. +// +// FreeRTOS does not use errno and does not support perror(). Instead, they +// include the error condition in the return code itself. To get around this, +// 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_PrintErrno(x, ret) \ + P1_PrintMessage(POLARIS_LOG_LEVEL_ERROR, x ". [error=%s (%d)]", \ + strerror(-ret), ret) +#else // POSIX +#define P1_PrintErrno(x, ret) \ + P1_PrintMessage(POLARIS_LOG_LEVEL_ERROR, x ". [error=%s (%d)]", \ + strerror(errno), errno) +#endif // End OS selection static void P1_PrintData(const uint8_t* buffer, size_t length); #if defined(POLARIS_USE_TLS) @@ -115,10 +166,10 @@ static void __P1_PrintSSLError(int line, PolarisContext_t* context, SSL_load_error_strings(); int ssl_error = SSL_get_error(context->ssl, ret); if (ssl_error == SSL_ERROR_SYSCALL) { - PrintTime(); - P1_fprintf( - stderr, " polaris.c:%d] %s. [error=%s (syscall: errno=%d; %s)]\n", line, - message, strerror(errno), errno, ERR_error_string(ssl_error, NULL)); + P1_DoPrint(POLARIS_LOG_LEVEL_ERROR, + " polaris.c:%d] %s. [error=%s (syscall: errno=%d; %s)]", line, + message, strerror(errno), errno, + ERR_error_string(ssl_error, NULL)); } else { // Note: OpenSSL has a function sort of like strerror(), SSL_error_string(), // but in practice its output is less than helpful most of the time. We'll @@ -157,10 +208,9 @@ static void __P1_PrintSSLError(int line, PolarisContext_t* context, break; } - PrintTime(); - P1_fprintf(stderr, " polaris.c:%d] %s. [error=%s (%d; %s)]\n", line, - message, error_name, ssl_error, - ERR_error_string(ssl_error, NULL)); + P1_DoPrint(POLARIS_LOG_LEVEL_ERROR, + " polaris.c:%d] %s. [error=%s (%d; %s)]", line, message, + error_name, ssl_error, ERR_error_string(ssl_error, NULL)); } } @@ -196,12 +246,12 @@ int Polaris_Init(PolarisContext_t* context) { if (POLARIS_RECV_BUFFER_SIZE < POLARIS_MAX_HTTP_MESSAGE_SIZE) { P1_PrintWarning( "Warning: Receive buffer smaller than expected authentication " - "response.\n"); + "response."); } if (POLARIS_SEND_BUFFER_SIZE < POLARIS_MAX_MESSAGE_SIZE) { P1_PrintWarning( - "Warning: Send buffer smaller than max expected outbound packet.\n"); + "Warning: Send buffer smaller than max expected outbound packet."); } context->socket = P1_INVALID_SOCKET; @@ -234,6 +284,13 @@ void Polaris_SetLogLevel(int log_level) { #endif } +/******************************************************************************/ +void Polaris_SetPrintCallback(PolarisPrintCallback_t callback) { +#if !defined(POLARIS_NO_PRINT) + __print_callback = callback; +#endif +} + /******************************************************************************/ int Polaris_Authenticate(PolarisContext_t* context, const char* api_key, const char* unique_id) { @@ -245,7 +302,7 @@ int Polaris_AuthenticateTo(PolarisContext_t* context, const char* api_key, const char* unique_id, const char* api_url) { // Sanity check the inputs. if (strlen(api_key) == 0) { - P1_PrintError("API key must not be empty.\n"); + P1_PrintError("API key must not be empty."); return POLARIS_ERROR; } @@ -272,12 +329,12 @@ int Polaris_AuthenticateTo(PolarisContext_t* context, const char* api_key, POLARIS_RECV_BUFFER_SIZE, AUTH_REQUEST_TEMPLATE, api_key, unique_id == NULL ? "" : unique_id); if (content_size < 0) { - P1_PrintError("Error populating authentication request payload.\n"); + P1_PrintError("Error populating authentication request payload."); return POLARIS_NOT_ENOUGH_SPACE; } P1_PrintDebug( - "Sending auth request. [api_key=%.7s..., unique_id=%s, url=%s]\n", + "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 @@ -288,7 +345,7 @@ int Polaris_AuthenticateTo(PolarisContext_t* context, const char* api_key, context->recv_buffer, (size_t)content_size); #endif if (status_code < 0) { - P1_PrintError("Error sending authentication request.\n"); + P1_PrintError("Error sending authentication request."); return status_code; } @@ -297,24 +354,24 @@ int Polaris_AuthenticateTo(PolarisContext_t* context, const char* api_key, const char* token_start = strstr((char*)context->recv_buffer, "\"access_token\":\""); if (token_start == NULL) { - P1_PrintError("Authentication token not found in response.\n"); + P1_PrintError("Authentication token not found in response."); return POLARIS_AUTH_ERROR; } else { token_start += 16; if (sscanf(token_start, "%" STR(POLARIS_MAX_TOKEN_SIZE) "[^\"]s", context->auth_token) != 1) { - P1_PrintError("Authentication token not found in response.\n"); + P1_PrintError("Authentication token not found in response."); return POLARIS_AUTH_ERROR; } else { - P1_PrintDebug("Received access token: %s\n", context->auth_token); + P1_PrintDebug("Received access token: %s", context->auth_token); return POLARIS_SUCCESS; } } } else if (status_code == 403) { - P1_PrintError("Authentication failed. Please check your API key.\n"); + P1_PrintError("Authentication failed. Please check your API key."); return POLARIS_FORBIDDEN; } else { - P1_PrintError("Unexpected authentication response (%d).\n", status_code); + P1_PrintError("Unexpected authentication response (%d).", status_code); return POLARIS_AUTH_ERROR; } } @@ -323,14 +380,14 @@ int Polaris_AuthenticateTo(PolarisContext_t* context, const char* api_key, int Polaris_SetAuthToken(PolarisContext_t* context, const char* auth_token) { size_t length = strlen(auth_token); if (length == 0) { - P1_PrintError("User-provided auth token must not be empty.\n"); + P1_PrintError("User-provided auth token must not be empty."); return POLARIS_ERROR; } else if (length > POLARIS_MAX_TOKEN_SIZE) { - P1_PrintError("User-provided auth token is too long.\n"); + P1_PrintError("User-provided auth token is too long."); return POLARIS_NOT_ENOUGH_SPACE; } else { memcpy(context->auth_token, auth_token, length + 1); - P1_PrintDebug("Using user-specified access token: %s\n", + P1_PrintDebug("Using user-specified access token: %s", context->auth_token); return POLARIS_SUCCESS; } @@ -351,7 +408,7 @@ int Polaris_Connect(PolarisContext_t* context) { int Polaris_ConnectTo(PolarisContext_t* context, const char* endpoint_url, int endpoint_port) { if (context->auth_token[0] == '\0') { - P1_PrintError("Error: Auth token not specified.\n"); + P1_PrintError("Error: Auth token not specified."); return POLARIS_AUTH_ERROR; } @@ -362,7 +419,7 @@ int Polaris_ConnectTo(PolarisContext_t* context, const char* endpoint_url, context->data_request_sent = 0; int ret = OpenSocket(context, endpoint_url, endpoint_port); if (ret != POLARIS_SUCCESS) { - P1_PrintError("Error connecting to corrections endpoint: tcp://%s:%d.\n", + P1_PrintError("Error connecting to corrections endpoint: tcp://%s:%d.", endpoint_url, endpoint_port); return ret; } @@ -378,7 +435,7 @@ int Polaris_ConnectTo(PolarisContext_t* context, const char* endpoint_url, memmove(header + 1, context->auth_token, token_length); size_t message_size = Polaris_PopulateChecksum(context->recv_buffer); - P1_PrintDebug("Sending access token message. [size=%u B]\n", + P1_PrintDebug("Sending access token message. [size=%u B]", (unsigned)message_size); #ifdef POLARIS_USE_TLS ret = SSL_write(context->ssl, context->recv_buffer, message_size); @@ -412,7 +469,7 @@ int Polaris_ConnectWithoutAuth(PolarisContext_t* context, context->data_request_sent = 0; ret = OpenSocket(context, endpoint_url, endpoint_port); if (ret != POLARIS_SUCCESS) { - P1_PrintError("Error connecting to corrections endpoint: tcp://%s:%d.\n", + P1_PrintError("Error connecting to corrections endpoint: tcp://%s:%d.", endpoint_url, endpoint_port); return ret; } @@ -425,7 +482,7 @@ int Polaris_ConnectWithoutAuth(PolarisContext_t* context, memmove(header + 1, unique_id, id_length); size_t message_size = Polaris_PopulateChecksum(context->send_buffer); - P1_PrintDebug("Sending unique ID message. [size=%u B]\n", + P1_PrintDebug("Sending unique ID message. [size=%u B]", (unsigned)message_size); #ifdef POLARIS_USE_TLS ret = SSL_write(context->ssl, context->send_buffer, message_size); @@ -447,7 +504,7 @@ int Polaris_ConnectWithoutAuth(PolarisContext_t* context, /******************************************************************************/ void Polaris_Disconnect(PolarisContext_t* context) { if (context->socket != P1_INVALID_SOCKET) { - P1_PrintDebug("Closing Polaris connection.\n"); + P1_PrintDebug("Closing Polaris connection."); context->disconnected = 1; #ifdef POLARIS_USE_TLS SSL_shutdown(context->ssl); @@ -473,12 +530,12 @@ void Polaris_SetRTCMCallback(PolarisContext_t* context, int Polaris_SendECEFPosition(PolarisContext_t* context, double x_m, double y_m, double z_m) { if (context->socket == P1_INVALID_SOCKET) { - P1_PrintError("Error: Polaris connection not currently open.\n"); + P1_PrintError("Error: Polaris connection not currently open."); return POLARIS_SOCKET_ERROR; } #ifdef POLARIS_USE_TLS else if (context->ssl_ctx == NULL || context->ssl == NULL) { - P1_PrintError("Error: Polaris SSL context not available.\n"); + P1_PrintError("Error: Polaris SSL context not available."); return POLARIS_SOCKET_ERROR; } #endif @@ -494,12 +551,12 @@ 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]\n", + "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]]\n", + "Sending ECEF position. [size=%u B, position=[%.2f, %.2f, %.2f]]", (unsigned)message_size, x_m, y_m, z_m); #endif P1_PrintData(context->send_buffer, message_size); @@ -523,12 +580,12 @@ int Polaris_SendECEFPosition(PolarisContext_t* context, double x_m, double y_m, int Polaris_SendLLAPosition(PolarisContext_t* context, double latitude_deg, double longitude_deg, double altitude_m) { if (context->socket == P1_INVALID_SOCKET) { - P1_PrintError("Error: Polaris connection not currently open.\n"); + P1_PrintError("Error: Polaris connection not currently open."); return POLARIS_SOCKET_ERROR; } #ifdef POLARIS_USE_TLS else if (context->ssl_ctx == NULL || context->ssl == NULL) { - P1_PrintError("Error: Polaris SSL context not available.\n"); + P1_PrintError("Error: Polaris SSL context not available."); return POLARIS_SOCKET_ERROR; } #endif @@ -547,12 +604,12 @@ int Polaris_SendLLAPosition(PolarisContext_t* context, double latitude_deg, #ifdef P1_FREERTOS // Floating point printf() not available in FreeRTOS. P1_PrintDebug( - "Sending LLA position. [size=%u B, position=[%d.0e-7, %d.0e-7, %d]]\n", + "Sending LLA position. [size=%u B, position=[%d.0e-7, %d.0e-7, %d]]", (unsigned)message_size, le32toh(payload->latitude_dege7), le32toh(payload->longitude_dege7), le32toh(payload->altitude_mm)); #else P1_PrintDebug( - "Sending LLA position. [size=%u B, position=[%.6f, %.6f, %.2f]]\n", + "Sending LLA position. [size=%u B, position=[%.6f, %.6f, %.2f]]", (unsigned)message_size, latitude_deg, longitude_deg, altitude_m); #endif P1_PrintData(context->send_buffer, message_size); @@ -575,12 +632,12 @@ int Polaris_SendLLAPosition(PolarisContext_t* context, double latitude_deg, /******************************************************************************/ int Polaris_RequestBeacon(PolarisContext_t* context, const char* beacon_id) { if (context->socket == P1_INVALID_SOCKET) { - P1_PrintError("Error: Polaris connection not currently open.\n"); + P1_PrintError("Error: Polaris connection not currently open."); return POLARIS_SOCKET_ERROR; } #ifdef POLARIS_USE_TLS else if (context->ssl_ctx == NULL || context->ssl == NULL) { - P1_PrintError("Error: Polaris SSL context not available.\n"); + P1_PrintError("Error: Polaris SSL context not available."); return POLARIS_SOCKET_ERROR; } #endif @@ -591,7 +648,7 @@ int Polaris_RequestBeacon(PolarisContext_t* context, const char* beacon_id) { memmove(header + 1, beacon_id, id_length); size_t message_size = Polaris_PopulateChecksum(context->send_buffer); - P1_PrintDebug("Sending beacon request. [size=%u B, beacon='%s']\n", + P1_PrintDebug("Sending beacon request. [size=%u B, beacon='%s']", (unsigned)message_size, beacon_id); P1_PrintData(context->send_buffer, message_size); @@ -616,16 +673,16 @@ int Polaris_Work(PolarisContext_t* context) { // in case this function is called after Polaris_Disconnect() to clean up the // SSL session. if (context->disconnected) { - P1_PrintDebug("Connection terminated by user request.\n"); + P1_PrintDebug("Connection terminated by user request."); CloseSocket(context, 1); return POLARIS_CONNECTION_CLOSED; } else if (context->socket == P1_INVALID_SOCKET) { - P1_PrintError("Error: Polaris connection not currently open.\n"); + P1_PrintError("Error: Polaris connection not currently open."); CloseSocket(context, 1); return POLARIS_SOCKET_ERROR; } - P1_PrintDebug("Listening for data block.\n"); + P1_PrintDebug("Listening for data block."); #ifdef POLARIS_USE_TLS P1_RecvSize_t bytes_read = @@ -705,7 +762,7 @@ int Polaris_Work(PolarisContext_t* context) { // Was the connection closed by the user? if (context->disconnected) { if (bytes_read == 0 || errno == EINTR || errno == ENOTCONN) { - P1_PrintDebug("Connection terminated by user request.\n"); + P1_PrintDebug("Connection terminated by user request."); } else { #ifdef P1_FREERTOS bytes_read = original_bytes_read; @@ -724,7 +781,7 @@ int Polaris_Work(PolarisContext_t* context) { // closed the connection without error. Once connected, we expect the // stream to stay open indefinitely under normal circumstances. The // server should not have a reason to terminate the connection. - P1_PrintWarning("Warning: Connection terminated remotely.\n"); + P1_PrintWarning("Warning: Connection terminated remotely."); } else { #ifdef P1_FREERTOS bytes_read = original_bytes_read; @@ -770,7 +827,7 @@ int Polaris_Work(PolarisContext_t* context) { else if (context->total_bytes_received > 0) { P1_PrintWarning( "Warning: Polaris connection closed with an error response. Is " - "your authentication token valid?\n"); + "your authentication token valid?"); ret = POLARIS_FORBIDDEN; } // Data request sent but no response received: authentication (presumably) @@ -778,21 +835,21 @@ int Polaris_Work(PolarisContext_t* context) { else if (context->data_request_sent) { P1_PrintWarning( "Warning: Polaris connection closed and no response received " - "from server.\n"); + "from server."); } // Data request not sent and no response received: authentication // (presumably) accepted. else { P1_PrintWarning( "Warning: Polaris connection closed and no position or beacon " - "request issued.\n"); + "request issued."); } CloseSocket(context, 1); return ret; } else { context->total_bytes_received += bytes_read; - P1_PrintDebug("Received %u bytes. [%" PRIu64 " bytes total]\n", + P1_PrintDebug("Received %u bytes. [%" PRIu64 " bytes total]", (unsigned)bytes_read, (uint64_t)context->total_bytes_received); P1_PrintData(context->recv_buffer, bytes_read); @@ -809,7 +866,7 @@ int Polaris_Work(PolarisContext_t* context) { // failure. if (!context->authenticated && context->total_bytes_received > 270) { P1_PrintDebug( - "Sufficient data received. Authentication token accepted.\n"); + "Sufficient data received. Authentication token accepted."); context->authenticated = POLARIS_AUTHENTICATED; } @@ -821,7 +878,7 @@ int Polaris_Work(PolarisContext_t* context) { } if (context->disconnected) { - P1_PrintDebug("Connection terminated by user.\n"); + P1_PrintDebug("Connection terminated by user."); return POLARIS_SUCCESS; } else { return (int)bytes_read; @@ -835,16 +892,16 @@ int Polaris_Run(PolarisContext_t* context, int connection_timeout_ms) { // in case this function is called after Polaris_Disconnect() to clean up the // SSL session. if (context->disconnected) { - P1_PrintDebug("Connection terminated by user request.\n"); + P1_PrintDebug("Connection terminated by user request."); CloseSocket(context, 1); return POLARIS_SUCCESS; } else if (context->socket == P1_INVALID_SOCKET) { - P1_PrintError("Error: Polaris connection not currently open.\n"); + P1_PrintError("Error: Polaris connection not currently open."); CloseSocket(context, 1); return POLARIS_SOCKET_ERROR; } - P1_PrintDebug("Listening for data.\n"); + P1_PrintDebug("Listening for data."); P1_TimeValue_t last_read_time; P1_GetCurrentTime(&last_read_time); @@ -866,9 +923,9 @@ int Polaris_Run(PolarisContext_t* context, int connection_timeout_ms) { P1_TimeValue_t current_time; P1_GetCurrentTime(¤t_time); int elapsed_ms = P1_GetElapsedMS(&last_read_time, ¤t_time); - P1_PrintDebug("%d ms elapsed since last data arrived.\n", elapsed_ms); + P1_PrintDebug("%d ms elapsed since last data arrived.", elapsed_ms); if (elapsed_ms >= connection_timeout_ms) { - P1_PrintWarning("Warning: Connection timed out after %d ms.\n", + P1_PrintWarning("Warning: Connection timed out after %d ms.", elapsed_ms); CloseSocket(context, 1); ret = POLARIS_TIMED_OUT; @@ -886,7 +943,7 @@ int Polaris_Run(PolarisContext_t* context, int connection_timeout_ms) { P1_GetCurrentTime(&last_read_time); if (context->disconnected) { - P1_PrintDebug("Connection terminated by user.\n"); + P1_PrintDebug("Connection terminated by user."); CloseSocket(context, 1); ret = POLARIS_SUCCESS; break; @@ -894,7 +951,7 @@ int Polaris_Run(PolarisContext_t* context, int connection_timeout_ms) { } } - P1_PrintDebug("Received %" PRIu64 " total bytes.\n", + P1_PrintDebug("Received %" PRIu64 " total bytes.", (uint64_t)context->total_bytes_received); if (ret == POLARIS_CONNECTION_CLOSED && context->disconnected) { return POLARIS_SUCCESS; @@ -911,7 +968,7 @@ static int ValidateUniqueID(const char* unique_id) { // autogenerated on the back end. return POLARIS_SUCCESS; } else if (length > POLARIS_MAX_UNIQUE_ID_SIZE) { - P1_PrintError("Unique ID must be a maximum of %d characters. [id='%s']\n", + P1_PrintError("Unique ID must be a maximum of %d characters. [id='%s']", POLARIS_MAX_UNIQUE_ID_SIZE, unique_id); return POLARIS_ERROR; } else { @@ -919,7 +976,7 @@ static int ValidateUniqueID(const char* unique_id) { char c = *ptr; if (c != '-' && c != '_' && (c < 'A' || c > 'Z') && (c < 'a' || c > 'z') && (c < '0' || c > '9')) { - P1_PrintError("Invalid unique ID specified. [id='%s']\n", unique_id); + P1_PrintError("Invalid unique ID specified. [id='%s']", unique_id); return POLARIS_ERROR; } } @@ -947,7 +1004,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\n", hostname, + P1_PrintError("Unable to resolve \"%s\": %s", hostname, hstrerror(h_errno)); return -1; } @@ -955,7 +1012,7 @@ static inline int P1_SetAddress(const char* hostname, int port, else if (host_info->h_addrtype != AF_INET) { P1_PrintWarning( "Warning: DNS lookup for \"%s\" returned an IPv6 address (not " - "supported).\n", + "supported).", hostname); // Explicitly set h_errno to _something_ (gethostbyname() doesn't touch // h_errno on success). @@ -975,19 +1032,19 @@ static int OpenSocket(PolarisContext_t* context, const char* endpoint_url, int endpoint_port) { // Is the connection already open? if (context->socket != P1_INVALID_SOCKET) { - P1_PrintError("Error: socket already open.\n"); + P1_PrintError("Error: socket already open."); return POLARIS_ERROR; } #ifdef POLARIS_USE_TLS else if (context->ssl_ctx != NULL || context->ssl != NULL) { - P1_PrintError("Error: SSL context not freed.\n"); + P1_PrintError("Error: SSL context not freed."); return POLARIS_ERROR; } #endif #ifdef POLARIS_USE_TLS // Configure TLS. - P1_PrintDebug("Configuring TLS context.\n"); + P1_PrintDebug("Configuring TLS context."); #if OPENSSL_VERSION_NUMBER < 0x10100000L context->ssl_ctx = SSL_CTX_new(TLSv1_2_client_method()); #else @@ -999,7 +1056,7 @@ static int OpenSocket(PolarisContext_t* context, const char* endpoint_url, SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_TLSv1); if (context->ssl_ctx == NULL) { - P1_PrintError("SSL context failed to initialize.\n"); + P1_PrintError("SSL context failed to initialize."); return POLARIS_ERROR; } #endif @@ -1014,7 +1071,7 @@ static int OpenSocket(PolarisContext_t* context, const char* endpoint_url, P1_PrintDebug( "Configuring socket. [socket=%d, read_timeout=%d ms, send_timeout=%d " - "ms]\n", + "ms]", context->socket, POLARIS_RECV_TIMEOUT_MS, POLARIS_SEND_TIMEOUT_MS); // Set send/receive timeouts. @@ -1028,17 +1085,17 @@ static int OpenSocket(PolarisContext_t* context, const char* endpoint_url, #ifndef P1_FREERTOS int flags = fcntl(context->socket, F_GETFL); - P1_PrintDebug("Socket flags: 0x%08x\n", flags); + P1_PrintDebug("Socket flags: 0x%08x", flags); #endif // P1_FREERTOS // Lookup the IP of the endpoint used for auth requests. - P1_PrintDebug("Performing DNS lookup for '%s'.\n", endpoint_url); + P1_PrintDebug("Performing DNS lookup for '%s'.", endpoint_url); P1_SocketAddrV4_t address; if (P1_SetAddress(endpoint_url, endpoint_port, &address) < 0) { #ifdef P1_FREERTOS - P1_PrintError("Error locating address '%s'.\n", endpoint_url); + P1_PrintError("Error locating address '%s'.", endpoint_url); #else - P1_PrintError("Error locating address '%s'. [error=%s (%d)]\n", + P1_PrintError("Error locating address '%s'. [error=%s (%d)]", endpoint_url, hstrerror(h_errno), h_errno); #endif CloseSocket(context, 1); @@ -1051,7 +1108,7 @@ static int OpenSocket(PolarisContext_t* context, const char* endpoint_url, #else uint32_t ip_host_endian = ntohl(address.sin_addr.s_addr); #endif - P1_PrintDebug("Connecting to 'tcp://%d.%d.%d.%d:%d'.\n", + P1_PrintDebug("Connecting to 'tcp://%d.%d.%d.%d:%d'.", (ip_host_endian >> 24) & 0xFF, (ip_host_endian >> 16) & 0xFF, (ip_host_endian >> 8) & 0xFF, ip_host_endian & 0xFF, endpoint_port); @@ -1062,11 +1119,11 @@ static int OpenSocket(PolarisContext_t* context, const char* endpoint_url, CloseSocket(context, 1); return POLARIS_SOCKET_ERROR; } - P1_PrintDebug("Connected successfully.\n"); + P1_PrintDebug("Connected successfully."); #ifdef POLARIS_USE_TLS // Create new SSL connection state and attach the socket. - P1_PrintDebug("Establishing TLS connection.\n"); + P1_PrintDebug("Establishing TLS connection."); context->ssl = SSL_new(context->ssl_ctx); SSL_set_fd(context->ssl, context->socket); @@ -1095,13 +1152,13 @@ static int OpenSocket(PolarisContext_t* context, const char* endpoint_url, if (c == NULL) { P1_PrintError( "Server failed to negotiate encryption cipher. Verify that endpoint " - "tcp://%s:%d supports TLS 1.2 or better.\n", + "tcp://%s:%d supports TLS 1.2 or better.", endpoint_url, endpoint_port); CloseSocket(context, 1); return POLARIS_ERROR; } - P1_PrintDebug("Connected with %s encryption.\n", + P1_PrintDebug("Connected with %s encryption.", SSL_get_cipher(context->ssl)); ShowCerts(context->ssl); #endif @@ -1113,7 +1170,7 @@ static int OpenSocket(PolarisContext_t* context, const char* endpoint_url, void CloseSocket(PolarisContext_t* context, int destroy_context) { #ifdef POLARIS_USE_TLS if (destroy_context && context->ssl != NULL) { - P1_PrintDebug("Shutting down TLS context.\n"); + P1_PrintDebug("Shutting down TLS context."); if (SSL_get_shutdown(context->ssl) == 0) { SSL_shutdown(context->ssl); } @@ -1124,14 +1181,14 @@ void CloseSocket(PolarisContext_t* context, int destroy_context) { #endif if (context->socket != P1_INVALID_SOCKET) { - P1_PrintDebug("Closing socket.\n"); + P1_PrintDebug("Closing socket."); close(context->socket); context->socket = P1_INVALID_SOCKET; } #ifdef POLARIS_USE_TLS if (destroy_context && context->ssl_ctx != NULL) { - P1_PrintDebug("Freeing TLS context.\n"); + P1_PrintDebug("Freeing TLS context."); SSL_CTX_free(context->ssl_ctx); context->ssl_ctx = NULL; } @@ -1182,7 +1239,7 @@ static int SendPOSTRequest(PolarisContext_t* context, const char* endpoint_url, // larger than the send buffer. We currently only send HTTP requests during // authentication, before data is coming in. if (POLARIS_RECV_BUFFER_SIZE < header_size + content_length + 1) { - P1_PrintError("Error populating POST request: buffer too small.\n"); + P1_PrintError("Error populating POST request: buffer too small."); CloseSocket(context, 1); return POLARIS_NOT_ENOUGH_SPACE; } @@ -1198,7 +1255,7 @@ static int SendPOSTRequest(PolarisContext_t* context, const char* endpoint_url, address, endpoint_url, port_str, content_length_str); if (header_size < 0) { // This shouldn't happen. - P1_PrintError("Error populating POST request.\n"); + P1_PrintError("Error populating POST request."); CloseSocket(context, 1); return POLARIS_ERROR; } @@ -1213,7 +1270,7 @@ static int SendPOSTRequest(PolarisContext_t* context, const char* endpoint_url, return ret; } - P1_PrintDebug("Sending POST request. [size=%u B]\n", (unsigned)message_size); + P1_PrintDebug("Sending POST request. [size=%u B]", (unsigned)message_size); #ifdef POLARIS_USE_TLS ret = SSL_write(context->ssl, context->recv_buffer, message_size); #else @@ -1255,7 +1312,7 @@ static int GetHTTPResponse(PolarisContext_t* context) { // Unlike POSIX recv(), which returns <0 and sets ETIMEDOUT on a socket read // timeout, FreeRTOS returns 0. if (bytes_read == 0) { - P1_PrintWarning("Socket timed out waiting for HTTP response.\n"); + P1_PrintWarning("Socket timed out waiting for HTTP response."); CloseSocket(context, 1); return POLARIS_SEND_ERROR; } @@ -1286,7 +1343,7 @@ static int GetHTTPResponse(PolarisContext_t* context) { // context->ssl, which is freed by CloseSocket(). CloseSocket(context, 1); - P1_PrintDebug("Received HTTP response. [size=%u B]\n", (unsigned)total_bytes); + P1_PrintDebug("Received HTTP response. [size=%u B]", (unsigned)total_bytes); // Append a null terminator to the response. context->recv_buffer[total_bytes++] = '\0'; @@ -1306,10 +1363,10 @@ static int GetHTTPResponse(PolarisContext_t* context) { size_t content_length = total_bytes - (content_start - (char*)context->recv_buffer); memmove(context->recv_buffer, content_start, content_length); - P1_PrintDebug("Response content:\n%s\n", context->recv_buffer); + P1_PrintDebug("Response content:\n%s", context->recv_buffer); } else { // No content in response. - P1_PrintDebug("No content in response.\n"); + P1_PrintDebug("No content in response."); context->recv_buffer[0] = '\0'; } @@ -1323,18 +1380,36 @@ void P1_PrintData(const uint8_t* buffer, size_t length) { return; } + char str[64] = {'\0'}; + int str_length = 0; for (size_t i = 0; i < length; ++i) { if (i % 16 != 0) { - P1_fprintf(stderr, " "); + str[str_length++] = ' '; } - P1_fprintf(stderr, "%02x", buffer[i]); + str_length += + snprintf(str + str_length, sizeof(str - str_length), "%02x", buffer[i]); if (i % 16 == 15) { - P1_fprintf(stderr, "\n"); + str[str_length++] = '\n'; + str[str_length++] = '\0'; + if (__print_callback) { + __print_callback(POLARIS_LOG_LEVEL_TRACE, str); + } else { + P1_fprintf(stderr, "%s", str); + } + str_length = 0; + } + } + + if (str_length != 0) { + str[str_length++] = '\0'; + if (__print_callback) { + __print_callback(POLARIS_LOG_LEVEL_TRACE, str); + } else { + P1_fprintf(stderr, "%s\n", str); } } - P1_fprintf(stderr, "\n"); } #endif @@ -1348,13 +1423,13 @@ void ShowCerts(SSL* ssl) { X509* cert = SSL_get_peer_certificate(ssl); if (cert != NULL) { char line[256]; - P1_PrintDebug("Server certificates:\n"); + P1_PrintDebug("Server certificates:"); X509_NAME_oneline(X509_get_subject_name(cert), line, sizeof(line)); - P1_PrintDebug(" Subject: %s\n", line); + P1_PrintDebug(" Subject: %s", line); X509_NAME_oneline(X509_get_issuer_name(cert), line, sizeof(line)); - P1_PrintDebug(" Issuer: %s\n", line); + P1_PrintDebug(" Issuer: %s", line); } else { - P1_PrintDebug("No client certificates configured.\n"); + P1_PrintDebug("No client certificates configured."); } } #endif diff --git a/c/src/point_one/polaris/polaris.h b/c/src/point_one/polaris/polaris.h index f7b8f9f..497a8a9 100644 --- a/c/src/point_one/polaris/polaris.h +++ b/c/src/point_one/polaris/polaris.h @@ -71,6 +71,15 @@ # define POLARIS_SEND_TIMEOUT_MS 1000 #endif +/** + * @brief The maximum length of a message when a print callback is registered. + * + * See @ref Polaris_SetPrintCallback(). + */ +#ifndef POLARIS_MAX_PRINT_LENGTH +# define POLARIS_MAX_PRINT_LENGTH 256 +#endif + /** * @name Polaris Return Codes * @{ @@ -104,6 +113,8 @@ typedef struct PolarisContext_s PolarisContext_t; typedef void (*PolarisCallback_t)(void* info, PolarisContext_t* context, const uint8_t* buffer, size_t size_bytes); +typedef void (*PolarisPrintCallback_t)(int level, const char* message); + struct PolarisContext_s { P1_Socket_t socket; @@ -159,6 +170,22 @@ void Polaris_Free(PolarisContext_t* context); */ void Polaris_SetLogLevel(int log_level); +/** + * @brief Specify an alternate function to be called when printing messages. + * + * When a print callback is registered, all messages (error, info, debug, etc.) + * will be redirected to the callback and not printed to stderr. + * + * @note + * When a callback is enabled, messages will be constructed in a buffer declared + * on the stack in order to avoid heap usage. The size of the buffer may be + * controlled by @ref POLARIS_MAX_PRINT_LENGTH to limit stack usage where + * desired. + * + * @param callback The function to be called. + */ +void Polaris_SetPrintCallback(PolarisPrintCallback_t callback); + /** * @brief Authenticate with Polaris. * diff --git a/c/src/point_one/polaris/portability.h b/c/src/point_one/polaris/portability.h index b4f0968..25f1ff1 100644 --- a/c/src/point_one/polaris/portability.h +++ b/c/src/point_one/polaris/portability.h @@ -26,17 +26,6 @@ extern "C" { # define P1_fprintf(stream, format, ...) P1_printf(format, ##__VA_ARGS__) # endif -// FreeRTOS does not use errno and does not support perror(). Instead, they -// include the error condition in the return code itself. To get around this, -// our perror macro also takes the function return code and prints it. -// -// For POSIX systems, the return code argument is ignored. While it may be -// omitted, it is required for FreeRTOS compilation. -# ifndef P1_perror -# define P1_perror(format, ret) \ - P1_printf(format ". [error=%s (%d)]\n", strerror(-ret), ret) -# endif - typedef TickType_t P1_TimeValue_t; static inline void P1_GetCurrentTime(P1_TimeValue_t* result) { @@ -76,16 +65,6 @@ static inline int P1_GetUTCOffsetHours(P1_TimeValue_t* time) { # define P1_fprintf fprintf # endif -// 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. -// -// Note that we ignore the ret argument here, which is meant for use with -// FreeRTOS, and instead get the actual error value from errno on POSIX systems. -# ifndef P1_perror -# define P1_perror(format, ret) \ - P1_fprintf(stderr, format ". [error=%s (%d)]\n", strerror(errno), errno) -# endif - typedef struct timeval P1_TimeValue_t; static inline void P1_GetCurrentTime(P1_TimeValue_t* result) { From bd68bd768180d8dff0c2c106fc3f53c7af6e0fe6 Mon Sep 17 00:00:00 2001 From: Adam Shapiro Date: Tue, 20 Aug 2024 17:14:26 -0400 Subject: [PATCH 04/13] Include filename and line in the callback. --- c/src/point_one/polaris/polaris.c | 70 ++++++++++++++++--------------- c/src/point_one/polaris/polaris.h | 3 +- 2 files changed, 38 insertions(+), 35 deletions(-) diff --git a/c/src/point_one/polaris/polaris.c b/c/src/point_one/polaris/polaris.c index 1ad0965..857e2aa 100644 --- a/c/src/point_one/polaris/polaris.c +++ b/c/src/point_one/polaris/polaris.c @@ -97,27 +97,27 @@ static int PrintTime(char* buffer, size_t capacity_bytes) { return length; } -static void P1_PrintToCallback(int level, const char* format, ...) { +static void P1_PrintToCallback(int line, int level, const char* format, ...) { char buffer[POLARIS_MAX_PRINT_LENGTH + 1]; va_list args; va_start(args, format); vsnprintf(buffer, sizeof(buffer), format, args); va_end(args); - __print_callback(level, buffer); + __print_callback("polaris.c", line, level, buffer); } -#define P1_DoPrint(level, x, ...) \ - if (__log_level >= level) { \ - if (__print_callback) { \ - P1_PrintToCallback(level, x, ##__VA_ARGS__); \ - } else { \ - PrintTime(NULL, 0); \ - P1_fprintf(stderr, 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(level, " polaris.c:" STR(__LINE__) "] " x, ##__VA_ARGS__); + 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 @@ -131,15 +131,16 @@ static void P1_PrintToCallback(int level, const char* format, ...) { // 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_PrintErrno(x, ret) \ - P1_PrintMessage(POLARIS_LOG_LEVEL_ERROR, x ". [error=%s (%d)]", \ - strerror(-ret), ret) +#define P1_PrintErrnoLevel(level, x, ret) \ + P1_PrintMessage(level, x ". [error=%s (%d)]", strerror(-ret), ret) #else // POSIX -#define P1_PrintErrno(x, ret) \ - P1_PrintMessage(POLARIS_LOG_LEVEL_ERROR, x ". [error=%s (%d)]", \ - strerror(errno), errno) +#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) + static void P1_PrintData(const uint8_t* buffer, size_t length); #if defined(POLARIS_USE_TLS) static void ShowCerts(SSL* ssl); @@ -159,17 +160,16 @@ static void ShowCerts(SSL* ssl); #if defined(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 #elif defined(POLARIS_USE_TLS) -static void __P1_PrintSSLError(int line, PolarisContext_t* context, +static void __P1_PrintSSLError(int level, int line, PolarisContext_t* context, const char* message, int ret) { SSL_load_error_strings(); int ssl_error = SSL_get_error(context->ssl, ret); if (ssl_error == SSL_ERROR_SYSCALL) { - P1_DoPrint(POLARIS_LOG_LEVEL_ERROR, - " polaris.c:%d] %s. [error=%s (syscall: errno=%d; %s)]", line, - message, strerror(errno), errno, - ERR_error_string(ssl_error, NULL)); + P1_DoPrint(line, level, "%s. [error=%s (syscall: errno=%d; %s)]", message, + strerror(errno), errno, ERR_error_string(ssl_error, NULL)); } else { // Note: OpenSSL has a function sort of like strerror(), SSL_error_string(), // but in practice its output is less than helpful most of the time. We'll @@ -208,26 +208,28 @@ static void __P1_PrintSSLError(int line, PolarisContext_t* context, break; } - P1_DoPrint(POLARIS_LOG_LEVEL_ERROR, - " polaris.c:%d] %s. [error=%s (%d; %s)]", line, message, - error_name, ssl_error, ERR_error_string(ssl_error, NULL)); + P1_DoPrint(line, level, "%s. [error=%s (%d; %s)]", message, error_name, + ssl_error, ERR_error_string(ssl_error, NULL)); } } #define P1_PrintReadWriteError(context, x, ret) \ - __P1_PrintSSLError(__LINE__, 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(__LINE__, context, x, ret) + __P1_PrintSSLError(POLARIS_LOG_LEVEL_ERROR, __LINE__, context, x, ret) #else // !defined(POLARIS_NO_PRINT) && !defined(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 #endif // POLARIS_NO_PRINT / POLARIS_USE_TLS -#define P1_DebugPrintReadWriteError(context, x, ret) \ - if (__log_level >= POLARIS_LOG_LEVEL_DEBUG) { \ - P1_PrintReadWriteError(context, x, ret); \ - } - static int ValidateUniqueID(const char* unique_id); static int OpenSocket(PolarisContext_t* context, const char* endpoint_url, @@ -1394,7 +1396,7 @@ void P1_PrintData(const uint8_t* buffer, size_t length) { str[str_length++] = '\n'; str[str_length++] = '\0'; if (__print_callback) { - __print_callback(POLARIS_LOG_LEVEL_TRACE, str); + __print_callback("polaris.c", __LINE__, POLARIS_LOG_LEVEL_TRACE, str); } else { P1_fprintf(stderr, "%s", str); } @@ -1405,7 +1407,7 @@ void P1_PrintData(const uint8_t* buffer, size_t length) { if (str_length != 0) { str[str_length++] = '\0'; if (__print_callback) { - __print_callback(POLARIS_LOG_LEVEL_TRACE, str); + __print_callback("polaris.c", __LINE__, POLARIS_LOG_LEVEL_TRACE, str); } else { P1_fprintf(stderr, "%s\n", str); } diff --git a/c/src/point_one/polaris/polaris.h b/c/src/point_one/polaris/polaris.h index 497a8a9..2c6bbc3 100644 --- a/c/src/point_one/polaris/polaris.h +++ b/c/src/point_one/polaris/polaris.h @@ -113,7 +113,8 @@ typedef struct PolarisContext_s PolarisContext_t; typedef void (*PolarisCallback_t)(void* info, PolarisContext_t* context, const uint8_t* buffer, size_t size_bytes); -typedef void (*PolarisPrintCallback_t)(int level, const char* message); +typedef void (*PolarisPrintCallback_t)(const char* filename, int line, + int level, const char* message); struct PolarisContext_s { P1_Socket_t socket; From 6fc1b7c3b8ec199f46996ece93ce9bb9a6c55f8d Mon Sep 17 00:00:00 2001 From: Adam Shapiro Date: Tue, 20 Aug 2024 17:14:48 -0400 Subject: [PATCH 05/13] Redirect prints from the C library to glog in C++. --- src/point_one/polaris/polaris_client.cc | 47 +++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/point_one/polaris/polaris_client.cc b/src/point_one/polaris/polaris_client.cc index e4b089c..20986fd 100644 --- a/src/point_one/polaris/polaris_client.cc +++ b/src/point_one/polaris/polaris_client.cc @@ -34,21 +34,63 @@ class Stream { }; static Stream cerr_stream; +static std::ostream null_stream(0); +#if defined(P1_NO_PRINT) +#define LOG(severity) null_stream +#define VLOG(severity) null_stream +#else // !defined(P1_NO_PRINT) #define LOG(severity) cerr_stream - #if defined(POLARIS_DEBUG) #define VLOG(severity) cerr_stream #else -static std::ostream null_stream(0); #define VLOG(severity) null_stream +#endif defined(P1_NO_PRINT) #endif // defined(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 #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 // defined(POLARIS_NO_GLOG) using namespace point_one::polaris; +/******************************************************************************/ +static void PrintCMessage(const char* filename, int line, int level, + const char* message) { + if (level >= POLARIS_LOG_LEVEL_INFO) { + LOG_INFO_STREAM(filename, line) << message; + } else if (level == POLARIS_LOG_LEVEL_WARNING) { + LOG_WARNING_STREAM(filename, line) << message; + } else if (level == POLARIS_LOG_LEVEL_ERROR) { + LOG_ERROR_STREAM(filename, line) << message; + } +} + /******************************************************************************/ PolarisClient::PolarisClient(int max_reconnect_attempts) : PolarisClient("", "", max_reconnect_attempts) {} @@ -62,6 +104,7 @@ PolarisClient::PolarisClient(const std::string& api_key, unique_id_(unique_id) { // Note that the C library print level will not change if the VLOG level is // changed dynamically via SetVLOGLevel() at runtime. + Polaris_SetPrintCallback(&PrintCMessage); if (VLOG_IS_ON(2)) { Polaris_SetLogLevel(POLARIS_LOG_LEVEL_TRACE); } else if (VLOG_IS_ON(1)) { From 139a3fec11922ac506ae676b3a83b2abaab9bb8a Mon Sep 17 00:00:00 2001 From: Adam Shapiro Date: Tue, 20 Aug 2024 17:28:30 -0400 Subject: [PATCH 06/13] Fixed extra newline in data trace printout. --- c/src/point_one/polaris/polaris.c | 1 - 1 file changed, 1 deletion(-) diff --git a/c/src/point_one/polaris/polaris.c b/c/src/point_one/polaris/polaris.c index 857e2aa..f85c56e 100644 --- a/c/src/point_one/polaris/polaris.c +++ b/c/src/point_one/polaris/polaris.c @@ -1393,7 +1393,6 @@ void P1_PrintData(const uint8_t* buffer, size_t length) { snprintf(str + str_length, sizeof(str - str_length), "%02x", buffer[i]); if (i % 16 == 15) { - str[str_length++] = '\n'; str[str_length++] = '\0'; if (__print_callback) { __print_callback("polaris.c", __LINE__, POLARIS_LOG_LEVEL_TRACE, str); From c96df20c715836b06bc6cf8258fbe5c2a256de26 Mon Sep 17 00:00:00 2001 From: Adam Shapiro Date: Tue, 20 Aug 2024 17:28:44 -0400 Subject: [PATCH 07/13] Fixed VLOG_IS_ON definition when glog is disabled. --- src/point_one/polaris/polaris_client.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/point_one/polaris/polaris_client.cc b/src/point_one/polaris/polaris_client.cc index 20986fd..25a916e 100644 --- a/src/point_one/polaris/polaris_client.cc +++ b/src/point_one/polaris/polaris_client.cc @@ -39,13 +39,16 @@ static std::ostream null_stream(0); #if defined(P1_NO_PRINT) #define LOG(severity) null_stream #define VLOG(severity) null_stream +#define VLOG_IS_ON(severity) false #else // !defined(P1_NO_PRINT) #define LOG(severity) cerr_stream #if defined(POLARIS_DEBUG) #define VLOG(severity) cerr_stream +#define VLOG_IS_ON(severity) true #else #define VLOG(severity) null_stream -#endif defined(P1_NO_PRINT) +#define VLOG_IS_ON(severity) false +#endif // defined(P1_NO_PRINT) #endif // defined(POLARIS_DEBUG) #define LOG_INFO_STREAM(filename, line) LOG(INFO) From 80214d755e61aba2218f710b7a07f083c97843b1 Mon Sep 17 00:00:00 2001 From: Adam Shapiro Date: Tue, 20 Aug 2024 17:48:09 -0400 Subject: [PATCH 08/13] Explicitly separate P1_NO_PRINT in polaris_client.cc. --- src/point_one/polaris/polaris_client.cc | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/point_one/polaris/polaris_client.cc b/src/point_one/polaris/polaris_client.cc index 25a916e..c0887fb 100644 --- a/src/point_one/polaris/polaris_client.cc +++ b/src/point_one/polaris/polaris_client.cc @@ -8,7 +8,17 @@ #include -#if defined(POLARIS_NO_GLOG) +#if defined(P1_NO_PRINT) +#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 + +#elif defined(POLARIS_NO_GLOG) #include // Reference: @@ -36,25 +46,20 @@ class Stream { static Stream cerr_stream; static std::ostream null_stream(0); -#if defined(P1_NO_PRINT) -#define LOG(severity) null_stream -#define VLOG(severity) null_stream -#define VLOG_IS_ON(severity) false -#else // !defined(P1_NO_PRINT) #define LOG(severity) cerr_stream #if defined(POLARIS_DEBUG) #define VLOG(severity) cerr_stream #define VLOG_IS_ON(severity) true -#else +#else // !defined(POLARIS_DEBUG) #define VLOG(severity) null_stream #define VLOG_IS_ON(severity) false -#endif // defined(P1_NO_PRINT) #endif // defined(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 + +#else // !defined(P1_NO_PRINT) && !defined(POLARIS_NO_GLOG) #include #if GOOGLE_STRIP_LOG == 0 From 8a7acebfb75d9dece716a27d732eb35ff013d634 Mon Sep 17 00:00:00 2001 From: Adam Shapiro Date: Tue, 20 Aug 2024 17:50:23 -0400 Subject: [PATCH 09/13] Treat macro definition P1_x=0 as false instead of just checking if defined. --- c/src/point_one/polaris/polaris.c | 28 ++++++++++++------------- src/point_one/polaris/polaris_client.cc | 14 ++++++------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/c/src/point_one/polaris/polaris.c b/c/src/point_one/polaris/polaris.c index f85c56e..9683bca 100644 --- a/c/src/point_one/polaris/polaris.c +++ b/c/src/point_one/polaris/polaris.c @@ -37,17 +37,17 @@ do { \ } while (0) -#if defined(P1_NO_PRINT) +#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 defined(POLARIS_USE_TLS) +#if POLARIS_USE_TLS #define ShowCerts(ssl) \ do { \ } while (0) #endif -#else // !defined(P1_NO_PRINT) +#else // !P1_NO_PRINT static int __log_level = POLARIS_LOG_LEVEL_INFO; static PolarisPrintCallback_t __print_callback = NULL; @@ -142,10 +142,10 @@ static void P1_PrintToCallback(int line, int level, const char* format, ...) { P1_PrintErrnoLevel(POLARIS_LOG_LEVEL_ERROR, x, ret) static void P1_PrintData(const uint8_t* buffer, size_t length); -#if defined(POLARIS_USE_TLS) +#if POLARIS_USE_TLS static void ShowCerts(SSL* ssl); #endif -#endif // defined(P1_NO_PRINT) +#endif // P1_NO_PRINT #define P1_PrintError(x, ...) \ P1_PrintMessage(POLARIS_LOG_LEVEL_ERROR, x, ##__VA_ARGS__) @@ -158,11 +158,11 @@ static void ShowCerts(SSL* ssl); #define P1_PrintTrace(x, ...) \ P1_PrintMessage(POLARIS_LOG_LEVEL_TRACE, x, ##__VA_ARGS__) -#if defined(POLARIS_NO_PRINT) +#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 -#elif defined(POLARIS_USE_TLS) +#elif POLARIS_USE_TLS static void __P1_PrintSSLError(int level, int line, PolarisContext_t* context, const char* message, int ret) { SSL_load_error_strings(); @@ -221,7 +221,7 @@ static void __P1_PrintSSLError(int level, int line, PolarisContext_t* context, } #define P1_PrintSSLError(context, x, ret) \ __P1_PrintSSLError(POLARIS_LOG_LEVEL_ERROR, __LINE__, context, x, ret) -#else // !defined(POLARIS_NO_PRINT) && !defined(POLARIS_USE_TLS) +#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) { \ @@ -281,14 +281,14 @@ void Polaris_Free(PolarisContext_t* context) { CloseSocket(context, 1); } /******************************************************************************/ void Polaris_SetLogLevel(int log_level) { -#if !defined(POLARIS_NO_PRINT) +#if !POLARIS_NO_PRINT __log_level = log_level; #endif } /******************************************************************************/ void Polaris_SetPrintCallback(PolarisPrintCallback_t callback) { -#if !defined(POLARIS_NO_PRINT) +#if !POLARIS_NO_PRINT __print_callback = callback; #endif } @@ -1136,7 +1136,7 @@ static int OpenSocket(PolarisContext_t* context, const char* endpoint_url, // Perform SSL handhshake. ret = SSL_connect(context->ssl); if (ret != 1) { -#if !defined(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 @@ -1145,7 +1145,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 // !defined(P1_NO_PRINT) +#endif // !P1_NO_PRINT CloseSocket(context, 1); return POLARIS_SOCKET_ERROR; } @@ -1376,7 +1376,7 @@ static int GetHTTPResponse(PolarisContext_t* context) { } /******************************************************************************/ -#if !defined(P1_NO_PRINT) +#if !P1_NO_PRINT void P1_PrintData(const uint8_t* buffer, size_t length) { if (__log_level < POLARIS_LOG_LEVEL_TRACE) { return; @@ -1415,7 +1415,7 @@ void P1_PrintData(const uint8_t* buffer, size_t length) { #endif /******************************************************************************/ -#if !defined(P1_NO_PRINT) && defined(POLARIS_USE_TLS) +#if !P1_NO_PRINT && POLARIS_USE_TLS void ShowCerts(SSL* ssl) { if (__log_level < POLARIS_LOG_LEVEL_DEBUG) { return; diff --git a/src/point_one/polaris/polaris_client.cc b/src/point_one/polaris/polaris_client.cc index c0887fb..cf4ef7c 100644 --- a/src/point_one/polaris/polaris_client.cc +++ b/src/point_one/polaris/polaris_client.cc @@ -8,7 +8,7 @@ #include -#if defined(P1_NO_PRINT) +#if P1_NO_PRINT #include static std::ostream null_stream(0); #define LOG(severity) null_stream @@ -18,7 +18,7 @@ static std::ostream null_stream(0); #define LOG_WARNING_STREAM(filename, line) null_stream #define LOG_ERROR_STREAM(filename, line) null_stream -#elif defined(POLARIS_NO_GLOG) +#elif POLARIS_NO_GLOG #include // Reference: @@ -47,19 +47,19 @@ static Stream cerr_stream; static std::ostream null_stream(0); #define LOG(severity) cerr_stream -#if defined(POLARIS_DEBUG) +#if POLARIS_DEBUG #define VLOG(severity) cerr_stream #define VLOG_IS_ON(severity) true -#else // !defined(POLARIS_DEBUG) +#else // !POLARIS_DEBUG #define VLOG(severity) null_stream #define VLOG_IS_ON(severity) false -#endif // defined(POLARIS_DEBUG) +#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 // !defined(P1_NO_PRINT) && !defined(POLARIS_NO_GLOG) +#else // !P1_NO_PRINT && !POLARIS_NO_GLOG #include #if GOOGLE_STRIP_LOG == 0 @@ -83,7 +83,7 @@ static std::ostream null_stream(0); #define LOG_ERROR_STREAM(filename, line) google::NullStream() #endif -#endif // defined(POLARIS_NO_GLOG) +#endif // P1_NO_PRINT / POLARIS_NO_GLOG using namespace point_one::polaris; From fee44251ffca87f876512672faafb0e1bf3e1afb Mon Sep 17 00:00:00 2001 From: Adam Shapiro Date: Tue, 20 Aug 2024 17:52:22 -0400 Subject: [PATCH 10/13] Fixed clang-format preprocessor indenting. --- .clang-format | 1 + c/src/point_one/polaris/polaris.c | 147 ++++++++++++------------ c/src/point_one/polaris/portability.h | 44 +++---- src/point_one/polaris/polaris_client.cc | 102 ++++++++-------- 4 files changed, 144 insertions(+), 150 deletions(-) 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 9683bca..f04440d 100644 --- a/c/src/point_one/polaris/polaris.c +++ b/c/src/point_one/polaris/polaris.c @@ -14,13 +14,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" @@ -38,15 +38,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; @@ -70,7 +70,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) { @@ -83,7 +83,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. @@ -106,18 +106,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 @@ -130,21 +131,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, ...) \ @@ -159,9 +160,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) { @@ -213,21 +214,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); @@ -335,9 +336,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", @@ -389,8 +389,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; } } @@ -552,10 +551,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]]", @@ -867,8 +865,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; } @@ -1006,8 +1003,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. @@ -1047,11 +1043,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); @@ -1097,8 +1093,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; @@ -1136,7 +1132,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 @@ -1145,7 +1141,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; } @@ -1160,8 +1156,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."; } From 4d44ff1c3e6bf8be2037e073ad0fe273f5648a33 Mon Sep 17 00:00:00 2001 From: Adam Shapiro Date: Thu, 29 Aug 2024 08:14:22 -0400 Subject: [PATCH 11/13] Added hostname command line arguments to simple_polaris_client example app. --- examples/simple_polaris_client.cc | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/examples/simple_polaris_client.cc b/examples/simple_polaris_client.cc index 339ca76..9ab9bc9 100644 --- a/examples/simple_polaris_client.cc +++ b/examples/simple_polaris_client.cc @@ -30,6 +30,16 @@ DEFINE_string(polaris_api_key, "", DEFINE_string(polaris_unique_id, "", "The unique ID to assign to this Polaris connection."); +DEFINE_string( + polaris_hostname, "", + "Specify an alternate hostname to use when connecting to the Polaris " + "corrections network. If blank, use the default hostname."); + +DEFINE_string( + polaris_api_hostname, "", + "Specify an alternate hostname to use when connecting to the Polaris " + "authentication API server. If blank, use the default hostname."); + PolarisClient* polaris_client = nullptr; // Process receiver incoming messages. This example code expects received data @@ -67,6 +77,9 @@ int main(int argc, char* argv[]) { polaris_client = new PolarisClient(FLAGS_polaris_api_key, FLAGS_polaris_unique_id); + polaris_client->SetPolarisEndpoint(FLAGS_polaris_hostname); + polaris_client->SetPolarisAuthenticationServer(FLAGS_polaris_api_hostname); + polaris_client->SetRTCMCallback( std::bind(&ReceivedData, std::placeholders::_1, std::placeholders::_2)); From cc022affef5fe20fab2bca2be1087770c87a32a7 Mon Sep 17 00:00:00 2001 From: Adam Shapiro Date: Wed, 23 Oct 2024 11:22:54 -0400 Subject: [PATCH 12/13] Clarified --polaris-hostname and --polaris-api-hostname usage. --- examples/simple_polaris_client.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/examples/simple_polaris_client.cc b/examples/simple_polaris_client.cc index 9ab9bc9..1043235 100644 --- a/examples/simple_polaris_client.cc +++ b/examples/simple_polaris_client.cc @@ -33,12 +33,15 @@ DEFINE_string(polaris_unique_id, "", DEFINE_string( polaris_hostname, "", "Specify an alternate hostname to use when connecting to the Polaris " - "corrections network. If blank, use the default hostname."); + "corrections network. This may be useful when connecting to a regional " + "endpoint in the EU or APAC. If blank, use the default hostname."); DEFINE_string( polaris_api_hostname, "", "Specify an alternate hostname to use when connecting to the Polaris " - "authentication API server. If blank, use the default hostname."); + "authentication API server. Specifying a custom API hostname is not " + "common, and is intended primarily for development purposes. If blank, use " + "the default hostname."); PolarisClient* polaris_client = nullptr; From bd16cbc647af5ee3521cb6ece41b6348be92e298 Mon Sep 17 00:00:00 2001 From: Adam Shapiro Date: Wed, 23 Oct 2024 11:27:40 -0400 Subject: [PATCH 13/13] Updated upload-artifact and download-artifact actions. v1 of these actions was marked deprecated in June 2024, and can no longer be used as of Oct 2024. https://github.blog/changelog/2024-02-13-deprecation-notice-v1-and-v2-of-the-artifact-actions/ --- .github/workflows/release_build.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/release_build.yml b/.github/workflows/release_build.yml index 293633b..228ee3d 100644 --- a/.github/workflows/release_build.yml +++ b/.github/workflows/release_build.yml @@ -172,7 +172,7 @@ jobs: - name: Upload artifact if: matrix.tool == 'bazel' - uses: actions/upload-artifact@v1 + uses: actions/upload-artifact@v4.4.3 with: path: polaris_examples.tar.gz name: polaris_examples.cpp.${{ matrix.arch }} @@ -302,7 +302,7 @@ jobs: - name: Upload artifact if: matrix.tool == 'bazel' - uses: actions/upload-artifact@v1 + uses: actions/upload-artifact@v4.4.3 with: path: c/polaris_examples.tar.gz name: polaris_examples.c.${{ matrix.arch }} @@ -340,7 +340,7 @@ jobs: - {lang: c, arch: aarch64} steps: - name: Download artifact - uses: actions/download-artifact@v1 + uses: actions/download-artifact@v4.1.8 with: name: polaris_examples.${{ matrix.lang }}.${{ matrix.arch }} - name: Set asset filename