Skip to content

Commit

Permalink
commprotol support disable
Browse files Browse the repository at this point in the history
  • Loading branch information
Neutree committed Dec 18, 2024
1 parent 4cd00a0 commit a92f791
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 17 deletions.
31 changes: 29 additions & 2 deletions components/comm/include/maix_comm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ namespace maix
/**
* Construct a new CommProtocol object
* @param buff_size buffer size, default to 1024 bytes
* @param header Customize header, default is maix.protocol.HEADER
* @param method_none_raise If method set to "none", raise err.Exception() if method_none_raise is true. Default false,
* if method is "none" and this arg is false, valid() function will return false and get_msg() always return none.
* @throw Initialize failed will raise err::Exception()
* @maixpy maix.comm.CommProtocol.__init__
* @maixcdk maix.comm.CommProtocol.CommProtocol
*/
CommProtocol(int buff_size = 1024, uint32_t header=maix::protocol::HEADER);
CommProtocol(int buff_size = 1024, uint32_t header=maix::protocol::HEADER, bool method_none_raise = false);
~CommProtocol();

/**
Expand Down Expand Up @@ -158,16 +162,39 @@ namespace maix
*/
err::Err resp_err(uint8_t cmd, err::Err code, const std::string &msg);


/**
* Is CommProtocol valid, only not valid when method not set to "none".
* @return false if commprotocol method is "none".
* @maixpy maix.comm.CommProtocol.valid
*/
bool valid() { return _valid; }

/**
* Set CommProtocol method
* @param method Can be "uart" or "none", "none" means not use CommProtocol.
* @maixpy maix.comm.CommProtocol.set_method
*/
static err::Err set_method(const std::string &method);

/**
* Get CommProtocol method
* @return method Can be "uart" or "none", "none" means not use CommProtocol.
* @maixpy maix.comm.CommProtocol.get_method
*/
static std::string get_method();

private:
void execute_cmd(protocol::MSG* msg);

private:
protocol::Protocol *_p;
std::string _comm_method;
CommBase *_comm;
CommBase *_get_comm_obj(const std::string &method);
CommBase *_get_comm_obj(const std::string &method, err::Err &error);
uint8_t *_tmp_buff;
int _tmp_buff_len;
bool _valid;
};
} // namespace comm
} // namespace maix
76 changes: 61 additions & 15 deletions components/comm/src/maix_comm_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ namespace maix::comm
{
static std::fstream testfile_s;
static std::thread *_stop_th = nullptr;
CommBase *CommProtocol::_get_comm_obj(const std::string &method)
CommBase *CommProtocol::_get_comm_obj(const std::string &method, err::Err &error)
{
error = err::Err::ERR_NONE;
if (method == "uart")
{
std::vector<std::string> ports = uart::list_devices();
Expand All @@ -116,39 +117,51 @@ namespace maix::comm
listener_priv::CommFileHandle::write_comm_info(ports[ports.size() - 1]);
return new uart::UART(ports[ports.size() - 1], 115200);
}
else
else if (method == "none")
{
log::error("not support comm method: %s\n", method.c_str());
return nullptr;
}
error = err::Err::ERR_ARGS;
log::error("not support comm method: %s\n", method.c_str());
return nullptr;
/* i2c device: /dev/i2c-x
listener_priv::CommFileHandle::write_comm_info("/dev/i2c-x"); */
}

CommProtocol::CommProtocol(int buff_size, uint32_t header)
CommProtocol::CommProtocol(int buff_size, uint32_t header, bool method_none_raise)
{
_tmp_buff_len = 128;
_tmp_buff = new uint8_t[_tmp_buff_len];
if (!_tmp_buff)
{
throw std::bad_alloc();
throw err::Exception(err::ERR_NO_MEM);
}
_p = new protocol::Protocol(buff_size, header);
_comm_method = app::get_sys_config_kv("comm", "method", "uart");
_comm = _get_comm_obj(_comm_method);
_comm_method = CommProtocol::get_method();
_valid = false;
err::Err e;
_comm = _get_comm_obj(_comm_method, e);
if (!_comm)
{
std::string msg = "get comm " + _comm_method + " obj failed";
log::error(msg.c_str());
throw std::runtime_error(msg);
if(e != err::ERR_NONE)
{
std::string msg = "get comm " + _comm_method + " obj failed";
log::error(msg.c_str());
throw err::Exception(err::ERR_RUNTIME, msg);
}
log::info("comm protocol disabled");
if (method_none_raise)
throw err::Exception(err::ERR_ARGS, "comm protocol disabled");
return;
}
err::Err e = _comm->open();
e = _comm->open();
if (e != err::ERR_NONE)
{
std::string msg = "open comm " + _comm_method + " obj failed: " + err::to_str(e);
log::error(msg.c_str());
throw std::runtime_error(msg);
throw err::Exception(err::ERR_RUNTIME, msg);
}
_valid = true;
}

CommProtocol::~CommProtocol()
Expand All @@ -166,6 +179,18 @@ namespace maix::comm
}
}

err::Err CommProtocol::set_method(const std::string &method)
{
if(method != "uart" && method != "none")
return err::ERR_ARGS;
return app::set_sys_config_kv("comm", "method", method);
}

std::string CommProtocol::get_method()
{
return app::get_sys_config_kv("comm", "method", "uart");
}

static std::vector<std::string> find_string(char *data, uint32_t data_len, uint32_t try_find_cnt = 0)
{
if (data_len <= 1)
Expand Down Expand Up @@ -472,6 +497,8 @@ namespace maix::comm
protocol::MSG *CommProtocol::get_msg(int timeout)
{
protocol::MSG *msg = nullptr;
if(!_valid)
return msg;
uint64_t t = time::ticks_ms();
while (1)
{
Expand Down Expand Up @@ -504,6 +531,8 @@ namespace maix::comm

err::Err CommProtocol::resp_ok(uint8_t *buff, int buff_len, uint8_t cmd, uint8_t *body, int body_len)
{
if(!_valid)
return err::ERR_NOT_PERMIT;
int len = _p->encode_resp_ok(buff, buff_len, cmd, body, body_len);
if (len < 0)
{
Expand All @@ -519,6 +548,8 @@ namespace maix::comm

err::Err CommProtocol::resp_ok(uint8_t cmd, uint8_t *body, int body_len)
{
if(!_valid)
return err::ERR_NOT_PERMIT;
Bytes *buff = _p->encode_resp_ok(cmd, body, body_len);
if (!buff)
{
Expand All @@ -535,6 +566,8 @@ namespace maix::comm

err::Err CommProtocol::resp_ok(uint8_t cmd, Bytes *body)
{
if(!_valid)
return err::ERR_NOT_PERMIT;
Bytes *buff = _p->encode_resp_ok(cmd, body);
if (!buff)
{
Expand All @@ -551,6 +584,8 @@ namespace maix::comm

err::Err CommProtocol::report(uint8_t *buff, int buff_len, uint8_t cmd, uint8_t *body, int body_len)
{
if(!_valid)
return err::ERR_NOT_PERMIT;
int len = _p->encode_report(buff, buff_len, cmd, body, body_len);
if (len < 0)
{
Expand All @@ -566,6 +601,8 @@ namespace maix::comm

err::Err CommProtocol::report(uint8_t cmd, uint8_t *body, int body_len)
{
if(!_valid)
return err::ERR_NOT_PERMIT;
Bytes *buff = _p->encode_report(cmd, body, body_len);
if (!buff)
{
Expand All @@ -582,6 +619,8 @@ namespace maix::comm

err::Err CommProtocol::report(uint8_t cmd, Bytes *body)
{
if(!_valid)
return err::ERR_NOT_PERMIT;
Bytes *buff = _p->encode_report(cmd, body);
if (!buff)
{
Expand All @@ -598,6 +637,8 @@ namespace maix::comm

err::Err CommProtocol::resp_err(uint8_t *buff, int buff_len, uint8_t cmd, err::Err code, const std::string &msg)
{
if(!_valid)
return err::ERR_NOT_PERMIT;
int len = _p->encode_resp_err(buff, buff_len, cmd, code, msg);
if (len < 0)
{
Expand All @@ -613,6 +654,8 @@ namespace maix::comm

err::Err CommProtocol::resp_err(uint8_t cmd, err::Err code, const std::string &msg)
{
if(!_valid)
return err::ERR_NOT_PERMIT;
Bytes *buff = _p->encode_resp_err(cmd, code, msg);
if (!buff)
{
Expand Down Expand Up @@ -844,8 +887,11 @@ namespace maix::comm::listener_priv
{
this->protocol = nullptr;
}
this->device = analyze_device(CommFileHandle::read_comm_info());
log::debug("[Default CommListener] Start listening on port %s", this->device.c_str());
if(this->protocol)
{
this->device = analyze_device(CommFileHandle::read_comm_info());
log::debug("[Default CommListener] Start listening on port %s", this->device.c_str());
}
}

CommListener::~CommListener()
Expand Down Expand Up @@ -919,7 +965,7 @@ namespace maix::comm::listener_priv
maix::log::warn("Default CommListener thread already running!!! IGNORE.");
return;
}
if (this->protocol)
if (this->protocol && this->protocol->valid())
{
this->th = new std::thread([this]()
{ this->run(); });
Expand Down

0 comments on commit a92f791

Please sign in to comment.