Skip to content

Commit

Permalink
Merge branch 'master' of github.com:FujiNetWIFI/fujinet-platformio
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffpiep committed Oct 21, 2023
2 parents be605d1 + 6799e3d commit 7c4964d
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 30 deletions.
2 changes: 1 addition & 1 deletion lib/device/iwm/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ void iwmNetwork::json_query(iwm_decoded_cmd_t cmd)
// addy |= ((cmd.g7byte6 & 0x7f) | ((cmd.grp7msb << 6) & 0x80)) << 8;
// addy |= ((cmd.g7byte7 & 0x7f) | ((cmd.grp7msb << 7) & 0x80)) << 16;

Debug_printf("Query set to: %s\n", string((char *)data_buffer, data_len).c_str());
Debug_printf("\r\nQuery set to: %s, data_len: %d\r\n", string((char *)data_buffer, data_len).c_str(), data_len);
json.setReadQuery(string((char *)data_buffer, data_len),cmdFrame.aux2);
}

Expand Down
38 changes: 23 additions & 15 deletions lib/device/sio/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
#include "network.h"

#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <memory>

#include "../../include/debug.h"
#include "../../include/pinmap.h"
Expand Down Expand Up @@ -1106,7 +1109,6 @@ void sioNetwork::sio_set_json_query()
{
uint8_t in[256];
const char *inp = NULL;
uint8_t *tmp;

memset(in, 0, sizeof(in));

Expand All @@ -1119,22 +1121,28 @@ void sioNetwork::sio_set_json_query()
in[i] = 0x00;
}

inp = strrchr((const char *)in, ':');

if (inp == NULL)
{
sio_error();
return;
std::string in_string(reinterpret_cast<char*>(in));
size_t last_colon_pos = in_string.rfind(':');

std::string inp_string;
if (last_colon_pos != std::string::npos) {
Debug_printf("sioNetwork::sio_set_json_query - skipped device spec. Application should be updated to remove it from query (%s)\r\n", in_string.c_str());
inp_string = in_string.substr(last_colon_pos + 1);
} else {
inp_string = in_string;
}

inp++;
json->setReadQuery(string(inp), cmdFrame.aux2);
json_bytes_remaining = json->readValueLen();
tmp = (uint8_t *)malloc(json->readValueLen());
json->readValue(tmp,json_bytes_remaining);
*receiveBuffer += string((const char *)tmp,json_bytes_remaining);
free(tmp);
Debug_printf("Query set to %s\n",inp);
json->setReadQuery(inp_string, cmdFrame.aux2);
json_bytes_remaining = json->json_bytes_remaining;

std::vector<uint8_t> tmp(json_bytes_remaining);
json->readValue(tmp.data(), json_bytes_remaining);

// don't copy past first nul char in tmp
auto null_pos = std::find(tmp.begin(), tmp.end(), 0);
*receiveBuffer += std::string(tmp.begin(), null_pos);

Debug_printf("Query set to >%s<\r\n", inp_string.c_str());
sio_complete();
}

Expand Down
43 changes: 29 additions & 14 deletions lib/fnjson/fnjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ void FNJSON::setQueryParam(uint8_t qp)
*/
void FNJSON::setReadQuery(const string &queryString, uint8_t queryParam)
{
Debug_printf("FNJSON::setReadQuery queryString: %s, queryParam: %d\r\n", queryString.c_str(), queryParam);
_queryString = queryString;
_queryParam = queryParam;
_item = resolveQuery();
Expand Down Expand Up @@ -149,6 +150,12 @@ string FNJSON::processString(string in)
*/
string FNJSON::getValue(cJSON *item)
{
if (item == NULL)
{
Debug_printf("\r\nFNJSON::getValue called with null item, returning empty string.\r\n");
return string("");
}

stringstream ss;

if (cJSON_IsString(item))
Expand Down Expand Up @@ -195,21 +202,29 @@ string FNJSON::getValue(cJSON *item)
setLineEnding("\x0a");
#endif

item = item->child;

do
if (item->child == NULL)
{
#ifdef BUILD_IEC
// Convert key to PETSCII
string tempStr = string((const char *)item->string);
mstr::toPETSCII(tempStr);
ss << tempStr;
#else
ss << item->string;
#endif

ss << lineEnding + getValue(item);
} while ((item = item->next) != NULL);
Debug_printf("FNJSON::getValue OBJECT has no CHILD, adding empty string\r\n");
ss << lineEnding;
}
else
{
item = item->child;
do
{
#ifdef BUILD_IEC
// Convert key to PETSCII
string tempStr = string((const char *)item->string);
mstr::toPETSCII(tempStr);
ss << tempStr;
#else
ss << item->string;
#endif

ss << lineEnding + getValue(item);
} while ((item = item->next) != NULL);
}

}
else if (cJSON_IsArray(item))
{
Expand Down
7 changes: 7 additions & 0 deletions lib/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -896,3 +896,10 @@ char *util_hexdump(const void *buf, size_t len) {
bool isApproximatelyInteger(double value, double tolerance) {
return std::abs(value - std::floor(value)) < tolerance;
}

std::string prependSlash(const std::string& str) {
if (str.empty() || str[0] != '/') {
return "/" + str;
}
return str;
}
3 changes: 3 additions & 0 deletions lib/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,7 @@ char *util_hexdump(const void *buf, size_t len);
// check if a double is very close to an integer
bool isApproximatelyInteger(double value, double tolerance = 1e-6);

// ensure string starts with a "/"
std::string prependSlash(const std::string& str);

#endif // _FN_UTILS_H

0 comments on commit 7c4964d

Please sign in to comment.