BigW Consortium Gitlab

Commit b57b1dd0 by David Frey

support disabling combain when empty key pushed

The HTTP thread is now started unconditionally rather than trying to start it only when required. Also fixed some formatting issues.
parent fb23d4c1
......@@ -47,26 +47,33 @@ static size_t WriteMemCallback(void *contents, size_t size, size_t nmemb, void *
void *CombainHttpThreadFunc(void *context)
{
char combainUrl[128];
char OldCombainApiKey[MAX_LEN_API_KEY];
char oldCombainApiKey[MAX_LEN_API_KEY];
do {
auto t = RequestJson->dequeue();
// Have the API key changeable from DHUB
if (strncmp(OldCombainApiKey,combainApiKey, sizeof(combainUrl) - (1 + strlen(combainUrl))) != 0)
if (strncmp(oldCombainApiKey, combainApiKey, sizeof(combainUrl) - (1 + strlen(combainUrl))) != 0)
{
strncpy(OldCombainApiKey, combainApiKey, sizeof(combainUrl) - (1 + strlen(combainUrl)));
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)));
strncat(combainUrl, oldCombainApiKey, sizeof(combainUrl) - (1 + strlen(combainUrl)));
}
ma_combainLocation_LocReqHandleRef_t handle = std::get<0>(t);
std::string &requestBody = std::get<1>(t);
if (oldCombainApiKey[0] == '\0')
{
ResponseJson->enqueue(std::make_tuple(handle, "Combain API key not set"));
le_event_Report(ResponseAvailableEvent, NULL, 0);
continue;
}
// Bug existed as we kept tacking onto the existing buffer
HttpReceiveBuffer.used = 0;
HttpReceiveBuffer.data[0] = 0;
ma_combainLocation_LocReqHandleRef_t handle = std::get<0>(t);
std::string &requestBody = std::get<1>(t);
CURL* curl = curl_easy_init();
LE_ASSERT(curl);
......@@ -90,6 +97,7 @@ void *CombainHttpThreadFunc(void *context)
LE_ERROR("libcurl returned error (%d): %s", res, curl_easy_strerror(res));
// TODO: better way to encode CURL errors?
ResponseJson->enqueue(std::make_tuple(handle, ""));
le_event_Report(ResponseAvailableEvent, NULL, 0);
}
std::string json((char*)HttpReceiveBuffer.data, HttpReceiveBuffer.used);
......@@ -102,6 +110,5 @@ void *CombainHttpThreadFunc(void *context)
curl_easy_cleanup(curl);
curl_slist_free_all(httpHeaders);
} while (true);
}
......@@ -14,7 +14,6 @@ void *CombainHttpThreadFunc(void *context);
#define MAX_LEN_API_KEY 32
extern char combainApiKey[MAX_LEN_API_KEY];
extern bool combainApiKeySet;
#endif // COMBAIN_HTTP_H
......@@ -14,7 +14,7 @@
#define RES_PATH_API_KEY "ApiKey/value"
bool combainApiKeySet = false;
static bool combainApiKeySet = false;
char combainApiKey[MAX_LEN_API_KEY];
......@@ -466,31 +466,21 @@ static bool TryParseAsSuccess(json_t *responseJson, std::shared_ptr<CombainResul
return parseSuccess;
}
bool ma_combainLocation_ServiceAvailable ()
bool ma_combainLocation_ServiceAvailable(void)
{
return combainApiKeySet;
}
static void SetApiKey (double timestamp, const char *api_key, void *contextPtr)
static void SetApiKey(double timestamp, const char *api_key, void *contextPtr)
{
LE_INFO("DHUB SetApiKey called with ApiKey: %s", api_key);
if (strlen(api_key) > (MAX_LEN_API_KEY - 1))
{
LE_INFO("To large an ApiKey!!");
LE_INFO("Too large an ApiKey!!");
return;
}
strncpy(combainApiKey, api_key, MAX_LEN_API_KEY - 1);
/* Only start HttpThread if combainApiKeySet was not set
* Thus, we are using combainApiKeySet to mean that HttpThread is running
*/
if (!combainApiKeySet)
{
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);
}
combainApiKeySet = strlen(api_key) > 0;
}
COMPONENT_INIT
......@@ -510,9 +500,6 @@ COMPONENT_INIT
{
LE_INFO("config get of combainApiKey worked");
combainApiKeySet = true;
CombainHttpInit(&RequestJson, &ResponseJson, ResponseAvailableEvent);
le_thread_Ref_t httpThread = le_thread_Create("CombainHttp", CombainHttpThreadFunc, NULL);
le_thread_Start(httpThread);
}
// Let's wait for dhub to set the combainApiKey
......@@ -523,4 +510,8 @@ COMPONENT_INIT
dhubIO_AddStringPushHandler(RES_PATH_API_KEY, SetApiKey, NULL);
dhubIO_MarkOptional(RES_PATH_API_KEY);
}
CombainHttpInit(&RequestJson, &ResponseJson, ResponseAvailableEvent);
le_thread_Ref_t httpThread = le_thread_Create("CombainHttp", CombainHttpThreadFunc, NULL);
le_thread_Start(httpThread);
}
......@@ -329,8 +329,12 @@ static void WifiEventHandler(le_wifiClient_Event_t event, void *context)
le_wifiClient_AccessPointRef_t ap = le_wifiClient_GetFirstAccessPoint();
while (ap != NULL)
{
uint8_t ssid[32];
size_t ssidLen = sizeof(ssid);
// Add one to the length so that the SSID is guaranteed to have a final zero
// byte. This allows us to (incorrectly) treat the SSID as though it's a C
// string and print it out. It's possible that some of the string may not get
// printed if there are unprintable characters or embedded nulls in the SSID.
uint8_t ssid[32 + 1] = {0};
size_t ssidLen = sizeof(ssid)- 1;
char bssid[(2 * 6) + (6 - 1) + 1]; // "nn:nn:nn:nn:nn:nn\0"
int16_t signalStrength;
le_result_t res;
......
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