15 memcpy(&feedback, data,
sizeof(feedback));
16 if (feedback.
packHead != 0xCFCFCFCF) {
17 std::cout <<
"Receive instruction header is misplaced" << std::endl;
21 std::cout <<
"The command was sent successfully but failed to execute" << std::endl;
24 std::cout <<
"Command sent successfully" << std::endl;
31 std::ifstream _in(path.data(), std::ios::binary);
33 std::cout <<
"can not find icd file: " << path << std::endl;
34 throw std::runtime_error(
"can not find icd file");
40 if (
icdRoot.isMember(
"param")) {
44 std::cout <<
"file: " << path <<
"Wrong format!" << std::endl;
45 throw std::runtime_error(
"icd file format error");
71 return nsukitStatus_t::NSUKIT_STATUS_INVALID_VALUE;
74 return nsukitStatus_t::NSUKIT_STATUS_SUCCESS;
81 }
catch (std::runtime_error &e) {
82 std::cout << e.what() << std::endl;
90 }
catch (std::runtime_error &e) {
98 }
catch (std::runtime_error &e) {
106 }
catch (std::runtime_error &e) {
114 }
catch (std::runtime_error &e) {
122 }
catch (std::runtime_error &e) {
130 }
catch (std::runtime_error &e) {
138 }
catch (std::runtime_error &e) {
146 }
catch (std::runtime_error &e) {
190 auto res = nsukitStatus_t::NSUKIT_STATUS_SUCCESS;
195 return nsukitStatus_t::NSUKIT_STATUS_INVALID_VALUE;
200 if (pCommandPack->
recvLen > 0) {
201 char *result =
new char[pCommandPack->
recvLen]();
221 auto _value = (uint32_t) std::stoll(value.asString(),
nullptr, 16);
223 }
else if (value.isString() &&
StringStartWith(value.asString(),
"0b")) {
225 auto _value = std::stoll(value.asString().substr(2),
nullptr, 2);
227 }
else if (value.isString() && reg[0].asString() ==
ICD_File) {
229 return FmtFile(value.asString());
230 }
else if (value.isString() && reg[0].asString() ==
ICD_FileLength) {
232 return FmtFile(value.asString(),
true);
233 }
else if (value.isNumeric()) {
235 std::string value_type = reg[0].asString();
237 auto _value = value.asFloat();
240 auto _value = value.asDouble();
243 auto _value = (uint8_t) value.asUInt();
245 }
else if (value_type ==
ICD_Int8) {
246 auto _value = (int8_t) value.asInt();
249 auto _value = (uint16_t) value.asUInt();
252 auto _value = (int16_t) value.asInt();
255 auto _value = (uint32_t) value.asUInt();
258 auto _value = (int32_t) value.asInt();
261 auto _value = (uint32_t) value.asInt();
264 std::cout <<
"Unrecognized value type " << value.toStyledString() << std::endl;
265 throw "Unrecognized register value type";
268 std::cout <<
"Unrecognized value type " << value.toStyledString() << std::endl;
269 throw "Unrecognized register value type";
281 unsigned int _value = std::stoll(value,
nullptr, 16);
284 unsigned short _value = std::stoll(value,
nullptr, 2);
287 std::cout <<
"Unrecognized value type " << value << std::endl;
288 throw "Unrecognized register value type";
301 std::ifstream _in(file_path, std::ios::in | std::ios::binary | std::ios::ate);
306 fileData->
bytes =
new char[fileData->
length + 1]();
307 uint32_t fileLength = (uint32_t) _in.tellg();
308 memcpy(fileData->
bytes, &fileLength, fileData->
length);
310 fileData->
length = _in.tellg();
311 fileData->
bytes =
new char[fileData->
length + 1]();
312 _in.seekg(0, std::ios::beg);
316 std::cout <<
"The file path " << file_path <<
" is wrong" << std::endl;
330 std::vector<CommandPack *> registerList;
334 uint32_t commandLength = 0;
337 std::cout <<
"Not find command: " << cmd_name << std::endl;
338 throw std::runtime_error(
"The instruction name does not exist in ICD");
343 for (
auto ®: command) {
348 }
else if (reg.isString() &&
icd->
icdParams->isMember(reg.asString())) {
351 }
else if (reg.isArray()) {
354 throw std::runtime_error(
"Sorry, send to registers is unsupported");
356 assert(regTemp->
bytes !=
nullptr);
357 registerList.emplace_back(regTemp);
358 commandLength += regTemp->
length;
363 for (
auto ®: command) {
365 throw std::runtime_error(
"Sorry, recv from registers is unsupported");
367 throw std::runtime_error(
"Sorry, recv from registers is unsupported");
368 }
else if (reg.isString() &&
icd->
icdParams->isMember(reg.asString())) {
371 }
else if (reg.isArray()) {
374 throw std::runtime_error(
"Sorry, recv from registers is unsupported");
379 resCommandPack->bytes =
new char[commandLength + 1];
380 resCommandPack->length = commandLength;
381 resCommandPack->recvLen = recv_len;
382 char *flag = resCommandPack->bytes;
383 for (
auto ®: registerList) {
384 memcpy(flag, reg->bytes, reg->length);
389 memcpy(resCommandPack->bytes + 12, &commandLength, 4);
391 return resCommandPack;
396 auto res = nsukitStatus_t::NSUKIT_STATUS_SUCCESS;
399 res |= nsukitStatus_t::NSUKIT_STATUS_INVALID_VALUE;
400 throw std::runtime_error(
"指令名称在icd中不存在");
406 for (
auto ®: command) {
408 res |= nsukitStatus_t::NSUKIT_STATUS_INVALID_VALUE;
410 res |= nsukitStatus_t::NSUKIT_STATUS_INVALID_VALUE;
411 }
else if (reg.isString() &&
icd->
icdParams->isMember(reg.asString())) {
416 }
else if (reg.isArray()) {
419 res |= nsukitStatus_t::NSUKIT_STATUS_INVALID_VALUE;
427 auto _value = *(
float *)buf;
430 auto _value = *(
double *)buf;
433 auto _value = *(uint8_t *)buf;
436 auto _value = *(int8_t *)buf;
439 auto _value = *(uint16_t *)buf;
442 auto _value = *(int16_t *)buf;
445 auto _value = *(uint32_t *)buf;
448 auto _value = *(int32_t *) buf;
451 return nsukitStatus_t::NSUKIT_STATUS_INVALID_VALUE;
457 std::ifstream _in(file_path, std::ios::in | std::ios::binary | std::ios::ate);
471 return str.compare(str.size() - tail.size(), tail.size(), tail) == 0;
481 return str.compare(0, head.size(), head) == 0;
489 delete[] pack->
bytes;
virtual nsukitStatus_t recv_bytes(nsuSize_t size, nsuCharBuf_p buf)
virtual nsukitStatus_t send_bytes(nsuBytes_t &bytes)
uint8_t get_param(nsuCSParam_t ¶m_name, uint8_t _default=0) override
CommandPack * FmtCommand(const std::string &cmd_name, const std::string &file_path="")
ICDRegMw(I_BaseCmdUItf *itf)
nsukitStatus_t execute(nsuCSName_t cname) override
nsukitStatus_t set_param(nsuCSParam_t ¶m_name, const uint8_t &value) override
nsukitStatus_t set_param_by_tname(nsuCSParam_t pname, std::string type, nsuCharBuf_p buf)
nsukitStatus_t config(nsuInitParam_t *param) override
nsukitStatus_t EnableParam(nsuCSName_t cname, nsuCharBuf_p buf)
CommandPack * FmtFile(const std::string &file_path, bool need_size=false)
Json::Value * icdCommands
CommandPack * FmtRegister(const Json::Value ®, const T &value)
nsukitStatus_t set_icd_param(nsuCSParam_t ¶m_name, const T &value)
T get_icd_param(nsuCSParam_t ¶m_name)
bool InitICD(std::string_view path)
static void * CheckFeedback(char *data)
#define ICD_ParamFlag_File
#define ICD_ParamFlag_FileLength
static std::map< std::string, int > TypeSizeMap
NSU_DLLEXPORT void DeleteCommandPack(CommandPack *pack)
NSU_DLLEXPORT bool StringEndWith(const std::string &str, const std::string &tail)
NSU_DLLEXPORT uint32_t GetFileSize(const std::string &file_path)
NSU_DLLEXPORT bool StringStartWith(const std::string &str, const std::string &head)
DLLEXTERN typedef size_t nsuSize_t
DLLEXTERN typedef std::string nsuCSParam_t
DLLEXTERN typedef char * nsuCharBuf_p
DLLEXTERN typedef std::string nsuCSName_t