use LOG_INF
This commit is contained in:
8
README.md
Normal file
8
README.md
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# Json c++
|
||||||
|
|
||||||
|
Wrap the Zephyr Json module to ease its usage for C++ development.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
- Include `zephyr_module.cmake` in the cmake file (eg. `include(${CMAKE_CURRENT_LIST_DIR}/zephyr_json_cpp/zephyr_module.cmake)`)
|
||||||
|
- Add `CONFIG_JSON_LIBRARY=y` to `prj.conf`
|
||||||
@@ -6,12 +6,10 @@
|
|||||||
|
|
||||||
#include <zephyr/data/json.h>
|
#include <zephyr/data/json.h>
|
||||||
|
|
||||||
#include <log.hpp>
|
|
||||||
|
|
||||||
namespace JsonParser
|
namespace JsonParser
|
||||||
{
|
{
|
||||||
void init();
|
void init();
|
||||||
void add_text(uint8_t* buf, size_t size);
|
void add_text(const uint8_t* buf, size_t size);
|
||||||
int json_printer(const char* bytes, size_t len, void* data);
|
int json_printer(const char* bytes, size_t len, void* data);
|
||||||
|
|
||||||
class HandlerBase
|
class HandlerBase
|
||||||
@@ -63,9 +61,9 @@ public:
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// info("received a valid json\n");
|
// LOG_INF"received a valid json\n");
|
||||||
// print(s);
|
// print(s);
|
||||||
// info("\n");
|
// LOG_INF"\n");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
29
src/json.cpp
29
src/json.cpp
@@ -1,6 +1,9 @@
|
|||||||
#include <json.hpp>
|
#include <json.hpp>
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
|
||||||
static constexpr size_t BUF_SIZE = 64 + 1;
|
LOG_MODULE_REGISTER(json);
|
||||||
|
|
||||||
|
static constexpr size_t BUF_SIZE = 512;
|
||||||
char _buf[BUF_SIZE];
|
char _buf[BUF_SIZE];
|
||||||
size_t _idx;
|
size_t _idx;
|
||||||
size_t _indent;
|
size_t _indent;
|
||||||
@@ -15,10 +18,10 @@ JsonParser::HandlerBase::HandlerBase()
|
|||||||
|
|
||||||
int JsonParser::json_printer(const char* bytes, size_t len, void* data)
|
int JsonParser::json_printer(const char* bytes, size_t len, void* data)
|
||||||
{
|
{
|
||||||
// info("[[%d]]\n", len);
|
// LOG_INF("[[%d]]", len);
|
||||||
// for (size_t i = 0; i < len; i++)
|
// for (size_t i = 0; i < len; i++)
|
||||||
// {
|
// {
|
||||||
// info("%c", bytes[i]);
|
// LOG_INF("%c", bytes[i]);
|
||||||
// }
|
// }
|
||||||
// return 0;
|
// return 0;
|
||||||
|
|
||||||
@@ -29,13 +32,13 @@ int JsonParser::json_printer(const char* bytes, size_t len, void* data)
|
|||||||
{
|
{
|
||||||
nl = false;
|
nl = false;
|
||||||
_indent--;
|
_indent--;
|
||||||
info("\n");
|
LOG_INF("");
|
||||||
for (size_t i = 0; i < _indent; i++)
|
for (size_t i = 0; i < _indent; i++)
|
||||||
{
|
{
|
||||||
info(" ");
|
LOG_ERR(" ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
info("%c", bytes[i]);
|
LOG_INF("%c", bytes[i]);
|
||||||
if (bytes[i] == '{' || bytes[i] == '[')
|
if (bytes[i] == '{' || bytes[i] == '[')
|
||||||
{
|
{
|
||||||
_indent++;
|
_indent++;
|
||||||
@@ -44,10 +47,10 @@ int JsonParser::json_printer(const char* bytes, size_t len, void* data)
|
|||||||
&& (bytes[i] == ',' || bytes[i] == '{' || bytes[i] == '}'
|
&& (bytes[i] == ',' || bytes[i] == '{' || bytes[i] == '}'
|
||||||
|| bytes[i] == '[' || bytes[i] == ']'))
|
|| bytes[i] == '[' || bytes[i] == ']'))
|
||||||
{
|
{
|
||||||
info("\n");
|
LOG_INF("");
|
||||||
for (size_t i = 0; i < _indent; i++)
|
for (size_t i = 0; i < _indent; i++)
|
||||||
{
|
{
|
||||||
info(" ");
|
LOG_INF(" ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -71,14 +74,14 @@ void commit()
|
|||||||
_idx = 0;
|
_idx = 0;
|
||||||
if (!ok)
|
if (!ok)
|
||||||
{
|
{
|
||||||
info("parse end error\n");
|
LOG_INF("parse end error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void flush_error()
|
void flush_LOG_ERRR()
|
||||||
{
|
{
|
||||||
_idx = 0;
|
_idx = 0;
|
||||||
error("input buffer overflow, flushing\n");
|
LOG_ERR("input buffer overflow, flushing");
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonParser::init()
|
void JsonParser::init()
|
||||||
@@ -86,7 +89,7 @@ void JsonParser::init()
|
|||||||
/* nothing to do */
|
/* nothing to do */
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonParser::add_text(uint8_t* buf, size_t size)
|
void JsonParser::add_text(const uint8_t* buf, size_t size)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < size; i++)
|
for (size_t i = 0; i < size; i++)
|
||||||
{
|
{
|
||||||
@@ -105,7 +108,7 @@ void JsonParser::add_text(uint8_t* buf, size_t size)
|
|||||||
_idx++;
|
_idx++;
|
||||||
if (_idx >= BUF_SIZE)
|
if (_idx >= BUF_SIZE)
|
||||||
{
|
{
|
||||||
flush_error();
|
flush_LOG_ERRR();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user