From 6e0dc69afa4c7171f9e31a50ad20d561189f7346 Mon Sep 17 00:00:00 2001 From: fujuntang <fujuntang@smartai.com> Date: 星期二, 31 八月 2021 17:44:55 +0800 Subject: [PATCH] Merge branch 'master' of http://os.smartai.com:9091/valib/c_bhomebus --- src/bh_api.cpp | 71 ++++++++++++++++++----------------- 1 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/bh_api.cpp b/src/bh_api.cpp index 9875cfa..9b151fa 100644 --- a/src/bh_api.cpp +++ b/src/bh_api.cpp @@ -7,11 +7,8 @@ #include "bh_api.h" #include <pthread.h> #include <getopt.h> -#include "bhome_msg_api.pb.h" -#include "bhome_msg.pb.h" -#include "error_msg.pb.h" -#include "proto/bhome_msg.pb.h" -#include "proto/bhome_msg_api.pb.h" +#include "../proto/source/bhome_msg.pb.h" +#include "../proto/source/bhome_msg_api.pb.h" static Logger *logger = LoggerFactory::getLogger(); @@ -341,11 +338,9 @@ strncpy(topics_buf + count, _input.topics[i], min); count += min; - if (total >= strlen(_input.topics[i])) { - total -= strlen(_input.topics[i]); - } + total -= min; - if ((_input.amount > 1) && (i < (_input.amount - 1))) { + if ((total > 1) && (_input.amount > 1) && (i < (_input.amount - 1))) { strncpy(topics_buf + count, STR_MAGIC, strlen(STR_MAGIC)); total -= 1; count++; @@ -355,7 +350,7 @@ } } - logger->debug("the parsed compound register topics: %s!\n", topics_buf); + //logger->debug("the parsed compound register topics: %s!\n", topics_buf); #else memcpy(topics_buf, topics, topics_len > (sizeof(topics_buf) - 1) ? (sizeof(topics_buf) - 1) : topics_len); #endif @@ -777,11 +772,9 @@ strncpy(topics_buf + count, _input.topics[i], min); count += min; - if (total >= strlen(_input.topics[i])) { - total -= strlen(_input.topics[i]); - } + total -= min; - if ((_input.amount > 1) && (i < (_input.amount - 1))) { + if ((total > 1) && (_input.amount > 1) && (i < (_input.amount - 1))) { strncpy(topics_buf + count, STR_MAGIC, strlen(STR_MAGIC)); total -= 1; count++; @@ -790,7 +783,7 @@ topics_buf[strlen(topics_buf) - 1] = '\0'; } } - logger->debug("the parsed compound sub topics: %s!\n", topics_buf); + //logger->debug("the parsed compound sub topics: %s!\n", topics_buf); #else memcpy(topics_buf, topics, topics_len > (sizeof(topics_buf) - 1) ? (sizeof(topics_buf) - 1) : topics_len); #endif @@ -906,7 +899,7 @@ } #if defined(PRO_DE_SERIALIZE) -int BHPublish(const char *msgpub, const char msgpub_len, const int timeout_ms) +int BHPublish(const char *msgpub, const int msgpub_len, const int timeout_ms) #else int BHPublish(const char *topic, const char *content, const int timeout_ms) #endif @@ -918,7 +911,7 @@ int node_arr_len = 0; #if defined(PRO_DE_SERIALIZE) - struct _MsgPublish + struct MsgPublish { const char *topic; const char *data; @@ -1275,6 +1268,12 @@ int sec, nsec; char topics_buf[MAX_STR_LEN] = { 0x00 }; + struct _RequestReply + { + std::string proc_id; + std::string data; + }rr; + #if defined(PRO_DE_SERIALIZE) struct _BHAddress { @@ -1289,7 +1288,7 @@ const char *topic; const char *data; }_input1; - + ::bhome_msg::BHAddress input0; ::bhome_msg::MsgRequestTopic input1; if (!input0.ParseFromArray(remote, remote_len) || !input1.ParseFromArray(request, request_len)) { @@ -1389,12 +1388,6 @@ memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); - - struct _RequestReply - { - std::string proc_id; - std::string data; - }rr; if (rv == 0) { memset(topics_buf, 0x00, sizeof(topics_buf)); @@ -1408,16 +1401,6 @@ memset(topics_buf, 0x00, sizeof(topics_buf)); memcpy(topics_buf, buf, size); rr.data = topics_buf; - -#if defined(PRO_DE_SERIALIZE) - ::bhome_msg::MsgRequestTopicReply mrt; - mrt.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv)); - mrt.mutable_errmsg()->set_errstring(errString); - mrt.set_data(rr.data.data()); - *reply_len = mrt.ByteSizeLong(); - *reply = malloc(*reply_len); - mrt.SerializePartialToArray(*reply, *reply_len); -#endif } pthread_mutex_unlock(&mutex); @@ -1429,6 +1412,26 @@ strncpy(errString, bus_strerror(rv), sizeof(errString)); } +#if defined(PRO_DE_SERIALIZE) + ::bhome_msg::MsgRequestTopicReply mrt; + mrt.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv)); + mrt.mutable_errmsg()->set_errstring(errString); + mrt.set_data(rr.data.data()); + *reply_len = mrt.ByteSizeLong(); + *reply = malloc(*reply_len); + mrt.SerializePartialToArray(*reply, *reply_len); +#else + if (rv > 0) { + min = strlen(errString) + 1; + buf = malloc(min); + memcpy(buf, errString, strlen(errString)); + *((char *)buf + min - 1) = '\0'; + + *reply = buf; + *reply_len = min; + } +#endif + if (rv == 0) return true; -- Gitblit v1.8.0