Skip to content

Commit cb6ee72

Browse files
committed
Added Polaris_AuthenticateTo() to allow use of an alternate API server.
Merge pull request #44.
2 parents c4fe192 + 6fd6919 commit cb6ee72

7 files changed

Lines changed: 67 additions & 8 deletions

File tree

c/src/point_one/polaris/polaris.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ void Polaris_SetLogLevel(int log_level) {
177177
/******************************************************************************/
178178
int Polaris_Authenticate(PolarisContext_t* context, const char* api_key,
179179
const char* unique_id) {
180+
return Polaris_AuthenticateTo(context, api_key, unique_id, POLARIS_API_URL);
181+
}
182+
183+
/******************************************************************************/
184+
int Polaris_AuthenticateTo(PolarisContext_t* context, const char* api_key,
185+
const char* unique_id, const char* api_url) {
180186
// Sanity check the inputs.
181187
if (strlen(api_key) == 0) {
182188
P1_Print("API key must not be empty.\n");
@@ -210,16 +216,16 @@ int Polaris_Authenticate(PolarisContext_t* context, const char* api_key,
210216
return POLARIS_NOT_ENOUGH_SPACE;
211217
}
212218

213-
P1_DebugPrint("Sending auth request. [api_key=%s, unique_id=%s]\n", api_key,
214-
unique_id);
219+
P1_DebugPrint("Sending auth request. [api_key=%s, unique_id=%s, url=%s]\n",
220+
api_key, unique_id, api_url);
215221
context->auth_token[0] = '\0';
216222
#ifdef POLARIS_USE_TLS
217223
int status_code =
218-
SendPOSTRequest(context, POLARIS_API_URL, 443, "/api/v1/auth/token",
224+
SendPOSTRequest(context, api_url, 443, "/api/v1/auth/token",
219225
context->recv_buffer, (size_t)content_size);
220226
#else
221227
int status_code =
222-
SendPOSTRequest(context, POLARIS_API_URL, 80, "/api/v1/auth/token",
228+
SendPOSTRequest(context, api_url, 80, "/api/v1/auth/token",
223229
context->recv_buffer, (size_t)content_size);
224230
#endif
225231
if (status_code < 0) {

c/src/point_one/polaris/polaris.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include "point_one/polaris/socket.h"
1212

13+
#define POLARIS_API_URL "api.pointonenav.com"
14+
1315
#define POLARIS_ENDPOINT_URL "polaris.pointonenav.com"
1416
#define POLARIS_ENDPOINT_PORT 8088
1517
#define POLARIS_ENDPOINT_TLS_PORT 8090
@@ -203,6 +205,16 @@ void Polaris_SetLogLevel(int log_level);
203205
int Polaris_Authenticate(PolarisContext_t* context, const char* api_key,
204206
const char* unique_id);
205207

208+
/**
209+
* @brief Authenticate with the specified Polaris API server.
210+
*
211+
* @copydetails Polaris_Authenticate()
212+
*
213+
* @param api_url The URL of the desired API authentication server.
214+
*/
215+
int Polaris_AuthenticateTo(PolarisContext_t* context, const char* api_key,
216+
const char* unique_id, const char* api_url);
217+
206218
/**
207219
* @brief Use an existing authentication token to connect to Polaris.
208220
*

c/src/point_one/polaris/polaris_internal.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
#define POLARIS_ID_BEACON 0x05
2424
#define POLARIS_ID_UNIQUE_ID 0x06
2525

26-
#define POLARIS_API_URL "api.pointonenav.com"
27-
2826
// Enforce 4-byte alignment and packing of all data structures and values so
2927
// that values larger than 1 B are aligned on platforms that require it.
3028
#pragma pack(push, 4)

src/point_one/polaris/polaris_client.cc

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ PolarisClient::PolarisClient(const std::string& api_key,
6868
Polaris_SetLogLevel(POLARIS_LOG_LEVEL_TRACE);
6969
}
7070

71+
SetPolarisAuthenticationServer();
7172
SetPolarisEndpoint();
7273

7374
polaris_.SetRTCMCallback([&](const uint8_t* buffer, size_t size_bytes) {
@@ -114,6 +115,17 @@ void PolarisClient::SetNoAuthID(const std::string& unique_id) {
114115
no_auth_ = true;
115116
}
116117

118+
/******************************************************************************/
119+
void PolarisClient::SetPolarisAuthenticationServer(const std::string& api_url) {
120+
std::unique_lock<std::recursive_mutex> lock(mutex_);
121+
if (api_url.empty()) {
122+
api_url_ = POLARIS_API_URL;
123+
}
124+
else {
125+
api_url_ = api_url;
126+
}
127+
}
128+
117129
/******************************************************************************/
118130
void PolarisClient::SetPolarisEndpoint(const std::string& endpoint_url,
119131
int endpoint_port) {
@@ -200,8 +212,9 @@ void PolarisClient::Run(double timeout_sec) {
200212
// Retrieve an access token using the specified API key.
201213
if (!auth_valid_ && !no_auth_) {
202214
VLOG(1) << "Authenticating with Polaris service. [unique_id="
203-
<< (unique_id_.empty() ? "<not specified>" : unique_id_) << "]";
204-
int ret = polaris_.Authenticate(api_key_, unique_id_);
215+
<< (unique_id_.empty() ? "<not specified>" : unique_id_)
216+
<< ", api_url=" << api_url_ << "]";
217+
int ret = polaris_.AuthenticateTo(api_key_, unique_id_, api_url_);
205218
if (ret == POLARIS_FORBIDDEN) {
206219
LOG(ERROR) << "Authentication rejected. Is your API key valid?";
207220
running_ = false;

src/point_one/polaris/polaris_client.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ class PolarisClient {
124124
*/
125125
void SetNoAuthID(const std::string& unique_id);
126126

127+
/**
128+
* @brief Specify an alternate URL to use when authenticating with the Polaris
129+
* corrections service.
130+
*
131+
* @param api_url The desired endpoint URL.
132+
*/
133+
void SetPolarisAuthenticationServer(const std::string& api_url = "");
134+
127135
/**
128136
* @brief Specify an alternate URL to use when connecting to the Polaris
129137
* corrections endpoint.
@@ -252,6 +260,8 @@ class PolarisClient {
252260

253261
std::function<void(const uint8_t* buffer, size_t size_bytes)> callback_;
254262

263+
std::string api_url_;
264+
255265
std::string endpoint_url_;
256266
int endpoint_port_ = 0;
257267

src/point_one/polaris/polaris_interface.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ int PolarisInterface::Authenticate(const std::string& api_key,
2626
return Polaris_Authenticate(&context_, api_key.c_str(), unique_id.c_str());
2727
}
2828

29+
/******************************************************************************/
30+
int PolarisInterface::AuthenticateTo(const std::string& api_key,
31+
const std::string& unique_id,
32+
const std::string& api_url) {
33+
return Polaris_AuthenticateTo(&context_, api_key.c_str(), unique_id.c_str(),
34+
api_url.c_str());
35+
}
36+
2937
/******************************************************************************/
3038
int PolarisInterface::SetAuthToken(const std::string& auth_token) {
3139
return Polaris_SetAuthToken(&context_, auth_token.c_str());

src/point_one/polaris/polaris_interface.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ class PolarisInterface {
7373
*/
7474
int Authenticate(const std::string& api_key, const std::string& unique_id);
7575

76+
/**
77+
* @brief Authenticate with the specified Polaris API server.
78+
*
79+
* @copydetails Authenticate()
80+
*
81+
* See also @ref Polaris_AuthenticateTo().
82+
*
83+
* @param api_url The URL of the desired API authentication server.
84+
*/
85+
int AuthenticateTo(const std::string& api_key, const std::string& unique_id,
86+
const std::string& api_url);
87+
7688
/**
7789
* @brief Use an existing authentication token to connect to Polaris.
7890
*

0 commit comments

Comments
 (0)