BigW Consortium Gitlab

Commit 1e7b192d by Zahid Chowdhury

combainApiKey modifiable via dhub dynamically, set dhub json string format same…

combainApiKey modifiable via dhub dynamically, set dhub json string format same for gnss & wifi, i.e.zero for alt & vacc, finally auto-start location
parent 0731aeba
...@@ -46,11 +46,20 @@ static size_t WriteMemCallback(void *contents, size_t size, size_t nmemb, void * ...@@ -46,11 +46,20 @@ static size_t WriteMemCallback(void *contents, size_t size, size_t nmemb, void *
void *CombainHttpThreadFunc(void *context) void *CombainHttpThreadFunc(void *context)
{ {
char combainUrl[128] = "https://cps.combain.com?key="; char combainUrl[128];
strncat(combainUrl, combainApiKey, sizeof(combainUrl) - (1 + strlen(combainUrl))); char OldCombainApiKey[MAX_LEN_API_KEY];
do { do {
auto t = RequestJson->dequeue(); auto t = RequestJson->dequeue();
// Have the API key changeable from DHUB
if (strncmp(OldCombainApiKey,combainApiKey, sizeof(combainUrl) - (1 + strlen(combainUrl))) != 0)
{
strncpy(OldCombainApiKey, combainApiKey, sizeof(combainUrl) - (1 + strlen(combainUrl)));
combainUrl[0] = '\0';
strcpy(combainUrl, "https://cps.combain.com?key=");
strncat(combainUrl, combainApiKey, sizeof(combainUrl) - (1 + strlen(combainUrl)));
}
// Bug existed as we kept tacking onto the existing buffer // Bug existed as we kept tacking onto the existing buffer
HttpReceiveBuffer.used = 0; HttpReceiveBuffer.used = 0;
HttpReceiveBuffer.data[0] = 0; HttpReceiveBuffer.data[0] = 0;
...@@ -69,6 +78,7 @@ void *CombainHttpThreadFunc(void *context) ...@@ -69,6 +78,7 @@ void *CombainHttpThreadFunc(void *context)
LE_ASSERT(curl_easy_setopt(curl, CURLOPT_HTTPHEADER, httpHeaders) == CURLE_OK); LE_ASSERT(curl_easy_setopt(curl, CURLOPT_HTTPHEADER, httpHeaders) == CURLE_OK);
LE_ASSERT(curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, requestBody.c_str()) == CURLE_OK); LE_ASSERT(curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, requestBody.c_str()) == CURLE_OK);
LE_INFO("APIKEY: %s", combainUrl);
LE_INFO("SENDING %d char: %s", requestBody.length(), requestBody.c_str()); LE_INFO("SENDING %d char: %s", requestBody.length(), requestBody.c_str());
LE_ASSERT(curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemCallback) == CURLE_OK); LE_ASSERT(curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemCallback) == CURLE_OK);
......
...@@ -463,18 +463,24 @@ static bool TryParseAsSuccess(json_t *responseJson, std::shared_ptr<CombainResul ...@@ -463,18 +463,24 @@ static bool TryParseAsSuccess(json_t *responseJson, std::shared_ptr<CombainResul
static void SetApiKey (double timestamp, const char *api_key, void *contextPtr) static void SetApiKey (double timestamp, const char *api_key, void *contextPtr)
{ {
LE_INFO("SetApiKey called"); LE_INFO("DHUB SetApiKey called with ApiKey: %s", api_key);
if (strlen(api_key) > (MAX_LEN_API_KEY - 1)) if (strlen(api_key) > (MAX_LEN_API_KEY - 1))
{ {
LE_INFO("To large an ApiKey!!"); LE_INFO("To large an ApiKey!!");
return; return;
} }
strncpy(combainApiKey, api_key, MAX_LEN_API_KEY - 1); strncpy(combainApiKey, api_key, MAX_LEN_API_KEY - 1);
combainApiKeySet = true; /* Only start HttpThread if combainApiKeySet was not set
LE_INFO("combainApiKey set from dhub starting http thread"); * Thus, we are using combainApiKeySet to mean that HttpThread is running
CombainHttpInit(&RequestJson, &ResponseJson, ResponseAvailableEvent); */
le_thread_Ref_t httpThread = le_thread_Create("CombainHttp", CombainHttpThreadFunc, NULL); if (!combainApiKeySet)
le_thread_Start(httpThread); {
combainApiKeySet = true;
LE_INFO("Starting http thread");
CombainHttpInit(&RequestJson, &ResponseJson, ResponseAvailableEvent);
le_thread_Ref_t httpThread = le_thread_Create("CombainHttp", CombainHttpThreadFunc, NULL);
le_thread_Start(httpThread);
}
} }
COMPONENT_INIT COMPONENT_INIT
......
...@@ -153,18 +153,17 @@ static void PackJson ...@@ -153,18 +153,17 @@ static void PackJson
" \"alt\": %lf, \"vAcc\": %lf, \"fixType\" : \"GNSS\", \"ts\" : %ju}", " \"alt\": %lf, \"vAcc\": %lf, \"fixType\" : \"GNSS\", \"ts\" : %ju}",
scanp->lat, scanp->lon, scanp->hAccuracy, scanp->lat, scanp->lon, scanp->hAccuracy,
scanp->alt, scanp->vAccuracy, (uintmax_t)ts); scanp->alt, scanp->vAccuracy, (uintmax_t)ts);
// For Wifi we keep the same JSON format as GNSS - i.e. alt & vAcc are 0
// TODO: go to jansson directly and send NULL
else if (loc == WIFI) else if (loc == WIFI)
len = snprintf(jsonp, jsonl, len = snprintf(jsonp, jsonl,
"{ \"lat\": %lf, \"lon\": %lf, \"hAcc\": %lf," "{ \"lat\": %lf, \"lon\": %lf, \"hAcc\": %lf,"
" \"fixType\" : \"WIFI\", \"ts\" : %ju}", " \"alt\": %lf, \"vAcc\": %lf, \"fixType\" : \"WIFI\", \"ts\" : %ju}",
scanp->lat, scanp->lon, scanp->hAccuracy, (uintmax_t)ts); scanp->lat, scanp->lon, scanp->hAccuracy,
else (double) 0, (double) 0, (uintmax_t)ts);
LE_FATAL("ILLEGAL Location Type: WIFI|GPS");
if (len >= jsonl) if (len >= jsonl)
{
LE_FATAL("JSON string (len %d) is longer than buffer (size %zu).", len, jsonl); LE_FATAL("JSON string (len %d) is longer than buffer (size %zu).", len, jsonl);
}
} }
static void LocationResultHandler( static void LocationResultHandler(
......
sandboxed: true sandboxed: true
start: manual start: auto
version: 0.0.1 version: 0.0.1
extern: extern:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment