diff --git a/.cproject b/.cproject
index 40c9b67..861de09 100644
--- a/.cproject
+++ b/.cproject
@@ -31,24 +31,28 @@
+
+
@@ -167,7 +176,14 @@
-
+
+
+
+
+
+
+
+
diff --git a/common/SH_CommonFunc.cpp b/common/SH_CommonFunc.cpp
index 08c5e9e..bccd68b 100644
--- a/common/SH_CommonFunc.cpp
+++ b/common/SH_CommonFunc.cpp
@@ -142,7 +142,25 @@ std::string ReadStrByOpt(std::string filename, std::string config, std::string o
is.close();
return value;
}
+std::vector ReadStrByOpt(std::string filename,std::string strUpdataFileName)
+{
+ boost::mutex::scoped_lock lock(s_config_mu);
+ Json::Value root,hwVersion;
+ Json::Reader reader;
+ std::vector value;
+ std::fstream is;
+ is.open(filename.c_str(), std::ios::in);
+ if (reader.parse(is, root)) {
+ hwVersion = root["hw_vesion"];
+ strUpdataFileName = root["fw_name"].asString();
+ }
+ for(int i = 0; i < hwVersion.size();i++){
+ value.push_back(hwVersion[i].asString());
+ }
+ is.close();
+ return value;
+}
int WriteStr2Config(std::string filename, std::string config, std::string option, std::string value, bool listable)
{
boost::mutex::scoped_lock lock(s_config_mu);
diff --git a/common/SH_CommonFunc.hpp b/common/SH_CommonFunc.hpp
index 2a953a1..07958a3 100644
--- a/common/SH_CommonFunc.hpp
+++ b/common/SH_CommonFunc.hpp
@@ -42,7 +42,7 @@
#define SN "/opt/system/sn" //设备序列号
#define SYSTEMSTART "/opt/system/start" //系统启动类型标志 0:正常启动 1:重启 2:
-
+#define BUILD_UINT16(x,y) (((x & 0x00FFu) << 8u) | (y & 0x00FFu))
#define GENERAL_BUF_SIZE 128*1024
using namespace std;
@@ -573,6 +573,9 @@ extern void ZoneConfig(std::string zoneid);
extern std::string GetSysStatus();
+//read update config file
+extern std::vector ReadStrByOpt(std::string filename,std::string strUpdataFileName);
+
extern void swap(char *data);
diff --git a/datatransfer/SH_Datatrans.cpp b/datatransfer/SH_Datatrans.cpp
new file mode 100644
index 0000000..c032ace
--- /dev/null
+++ b/datatransfer/SH_Datatrans.cpp
@@ -0,0 +1,513 @@
+#include "SH_Datatrans.hpp"
+#include "dirent.h"
+#include
+
+
+DataTrans *pDataTrans = DataTrans::instance();
+
+
+
+struct DowloadFile {
+
+ const char *filename;
+ FILE *stream;
+
+};
+DataTrans::DataTrans():
+m_bDebug(false)
+{
+}
+
+DataTrans::~DataTrans()
+{
+}
+
+static int OnDebug(CURL *, curl_infotype itype, char * pData, size_t size, void *)
+{
+ if(itype == CURLINFO_TEXT) {
+ //printf("[TEXT]%s\n", pData);
+ } else if(itype == CURLINFO_HEADER_IN) {
+ print_info("[HEADER_IN]%s\n", pData);
+ } else if(itype == CURLINFO_HEADER_OUT) {
+ print_info("[HEADER_OUT]%s\n", pData);
+ } else if(itype == CURLINFO_DATA_IN) {
+ print_info("[DATA_IN]%s\n", pData);
+ } else if(itype == CURLINFO_DATA_OUT) {
+ print_info("[DATA_OUT]%s\n", pData);
+ }
+ return 0;
+}
+
+
+
+static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)
+{
+ std::string* str = dynamic_cast((std::string *)lpVoid);
+ if( NULL == str || NULL == buffer ) {
+ return -1;
+ }
+ char* pData = (char*)buffer;
+ str->append(pData, size * nmemb);
+ return nmemb;
+}
+
+
+static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
+{
+ struct DowloadFile *out=(struct DowloadFile *)stream;
+ if(out && !out->stream) {
+ out->stream=fopen(out->filename, "wb");//打开文件进行写入
+ if(!out->stream)
+ return -1;
+ }
+ return fwrite(buffer, size, nmemb, out->stream);
+
+}
+
+int DataTrans::download(char* pFilename,string& strUrl,string& strResponse,bool bDownload)
+{
+ CURL *curl = NULL;
+ CURLcode res;
+ struct DowloadFile dlfile={
+ pFilename, //定义下载到本地的文件位置和路径
+ NULL
+ };
+ curl_global_init(CURL_GLOBAL_DEFAULT);
+ curl = curl_easy_init(); //初始化一个curl指针
+ if(curl) { //curl对象存在的情况下执行的操作
+ //设置远端地址
+ curl_easy_setopt(curl, CURLOPT_URL,strUrl.c_str());
+ if(bDownload){
+ //执行写入文件流操作
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);//当有数据被写入,回调函数被调用,
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, &dlfile); //设置结构体的指针传递给回调函数
+ }else{
+ curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
+ }
+ curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8);//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
+ curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);//接收数据时超时设置,如果10秒内数据未接收完,直接退出
+ //启用时会汇报所有的信息,存放在STDERR或指定的CURLOPT_STDERR中
+ curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
+ curl_easy_setopt(curl, CURLOPT_USERPWD, "SUREN:SUREN");
+
+ //写入文件
+ res = curl_easy_perform(curl);
+ //释放curl对象
+ curl_easy_cleanup(curl);
+ if(res != CURLE_OK)
+ {
+ cout<= 0) {
+ timeout.tv_sec = curl_timeo / 1000;
+ if (timeout.tv_sec > 1)
+ timeout.tv_sec = 1;
+ else
+ timeout.tv_usec = (curl_timeo % 1000) * 1000;
+ }
+
+ /* get file descriptors from the transfers */
+ curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
+ /* In a real-world program you OF COURSE check the return code of the
+ function calls. On success, the value of maxfd is guaranteed to be
+ greater or equal than -1. We call select(maxfd + 1, ...), specially in
+ case of (maxfd == -1), we call select(0, ...), which is basically equal
+ to sleep. */
+ rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
+ switch (rc) {
+ case -1:
+ break;
+ case 0:
+ default:
+ /* timeout or readable/writable sockets */
+ print_info("perform!\n");
+ curl_multi_perform(multi_handle, &still_running);
+ print_info("running: %d!\n", still_running);
+ break;
+ }
+ } while (still_running);
+ curl_multi_cleanup(multi_handle);
+ /* always cleanup */
+ curl_easy_cleanup(curl);
+ /* then cleanup the formpost chain */
+ curl_formfree(formpost);
+ /* free slist */
+ curl_slist_free_all(headerlist);
+ }
+ return 0;
+
+}
+
+// int DataTrans::Send_file_socket(const std::string &filename)
+// {
+// int socketfd;
+// struct sockaddr_in s_add,c_add;
+// unsigned short portnum = 9092;
+// int len;
+// char buffer[4096];
+// FILE *fp ;
+// int file_block_length = 0;
+// /*
+// *创建socket
+// */
+// if((socketfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
+// {
+// print_error("Socket create error! \n");
+// return -1;
+// }
+// /*
+// *设置地址
+// */
+
+// bzero(&s_add,sizeof(struct sockaddr_in));
+// s_add.sin_family = AF_INET;
+// //s_add.sin_addr.s_addr = inet_addr(GlobalConfig::ServerIP.c_str());
+// s_add.sin_addr.s_addr = inet_addr("192.168.1.19");
+// s_add.sin_port = htons(portnum);
+
+
+// if (connect(socketfd,(struct sockaddr *)(&s_add),sizeof(struct sockaddr)) < 0)
+// {
+// print_error("Connect failure!\n");
+// return -1;
+// }
+// else
+// print_info("Connect Success!\n");
+// std::string filename = filename.substr();
+// int file_name_length = (int)strlen(filename.c_str());
+// if(send(socketfd,(char *)&file_name_length, sizeof(int), 0) < 0)
+// {
+// print_error("Send File_Name_Length Failed!\n");
+// }else
+// {
+// print_info("Send File_Name_Length Success!\n");
+// }
+// //发送文件名
+// if(send(socketfd,filename.c_str(),file_name_length,0) < 0)
+// {
+// print_error("Send File_Name Failed!\n");
+// }else
+// {
+// print_info("Send File_Name Success!\n");
+// }
+// /*
+// *发送文件
+// */
+// fp = fopen(filename.c_str(), "r");
+
+// if (fp == NULL)
+// {
+// print_error("File: %s Not Found!\n", filename.c_str());
+// }
+// else
+// {
+// bzero(buffer, BUFFER_SIZE);
+// while( (file_block_length = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0)
+// {
+// print_info("file_block_length = %d\n", file_block_length);
+// /*
+// *把数据写入buffer
+// */
+// if (send(socketfd, buffer, file_block_length, 0) < 0)
+// {
+// print_error("Send File:%s Failed!\n", filename.c_str());
+// break;
+// }
+// bzero(buffer, sizeof(buffer));
+// }
+// fclose(fp);
+// print_info("File: %s Transfer Finished!\n", filename.c_str());
+// }
+// close(socketfd);
+// return 0;
+// }
+
+int DataTrans::Send_file_socket(const std::string &filename)
+{
+
+ return 0;
+}
+
+int DataTrans::Send_Dir_socket(const char *dirname)
+{
+ DIR *dirp;
+ std::vector v;
+ std::string temp;
+ struct dirent *dp;
+ dirp = opendir(dirname);
+ while ((dp = readdir(dirp)) != NULL) {
+ if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)
+ continue;
+ v.push_back(std::string(dp->d_name));
+ }
+
+ int socketfd = socket(AF_INET,SOCK_STREAM,0);
+ struct sockaddr_in s_add,c_add;
+ unsigned short portnum = 12345;
+ int len;
+ char buffer[BUFFER_SIZE];
+ FILE *fp ;
+ int file_block_length = 0;
+ /*
+ *创建socket
+ */
+ if(socketfd < 0) {
+ print_error("Socket create error! \n");
+ return -1;
+ } else {
+ print_info("Socket create success!\n");
+ }
+ /*
+ *设置地址
+ */
+
+ bzero(&s_add,sizeof(struct sockaddr_in));
+ s_add.sin_family = AF_INET;
+ s_add.sin_addr.s_addr = inet_addr(GlobalConfig::ServerIP.c_str());
+ s_add.sin_port = htons(PORT);
+
+ if (connect(socketfd,(struct sockaddr *)(&s_add),sizeof(struct sockaddr)) < 0) {
+ print_error("Connect failure!\n");
+ return -1;
+ } else {
+ print_info("Connect Success!\n");
+ }
+ std::vector::iterator iter;
+ for(iter = v.begin();iter != v.end();++iter) {
+ temp = *iter;
+ cout << temp.c_str() < 0) {
+ print_info("file_block_length = %d\n", file_block_length);
+ /*
+ *把数据写入buffer
+ */
+ if (send(socketfd, buffer, file_block_length, 0) < 0) {
+ print_error("Send File:%s Failed!\n", filename.c_str());
+ break;
+ }
+ bzero(buffer, sizeof(buffer));
+ }
+ fclose(fp);
+ print_info("File: %s Transfer Finished!\n", filename.c_str());
+ }
+ close(socketfd);
+ return 0;
+ }
+}
+void DataTrans::SetDebug(bool bDebug)
+{
+ m_bDebug = bDebug;
+}
diff --git a/datatransfer/SH_Datatrans.hpp b/datatransfer/SH_Datatrans.hpp
new file mode 100644
index 0000000..c7caea6
--- /dev/null
+++ b/datatransfer/SH_Datatrans.hpp
@@ -0,0 +1,108 @@
+#ifndef _DATATRANS_H_
+#define _DATATRANS_H_
+
+#include
+#include
+#include
+#include
+#include
+#include
+#include "../common/SH_global.h"
+#include "../utility/SH_MySingleton.hpp"
+
+#define BUFFER_SIZE 4096
+
+#define IP "127.0.0.1"
+#define PORT 12345
+
+class DataTrans : public MySingleton
+{
+public:
+ DataTrans();
+ ~DataTrans();
+
+ /**
+ * @brief HTTP POST请求
+ * @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com
+ * @param strPost 输入参数,使用如下格式para1=val1¶2=val2&…
+ * @param strResponse 输出参数,返回的内容
+ * @return 返回是否Post成功
+ */
+ int Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse);
+
+ /**
+ * @brief HTTP GET请求
+ * @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com
+ * @param strResponse 输出参数,返回的内容
+ * @return 返回是否Post成功
+ */
+
+ int Get(const std::string & strUrl, std::string & strResponse);
+
+ /**
+ * @brief HTTPS POST请求,无证书版本
+ * @param strUrl 输入参数,请求的Url地址,如:https://www.baidu.com
+ * @param strPost 输入参数,使用如下格式para1=val1¶2=val2&…
+ * @param strResponse 输出参数,返回的内容
+ * @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性.
+ * @return 返回是否Post成功
+ */
+
+ int Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath = NULL);
+
+ /**
+ * @brief HTTPS GET请求,无证书版本
+ * @param strUrl 输入参数,请求的Url地址,如:https://www.baidu.com
+ * @param strResponse 输出参数,返回的内容
+ * @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性.
+ * @return 返回是否Post成功
+ */
+
+ int Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath = NULL);
+
+ /**
+ * @brief HTTP 上传文件
+ * @param strUrl 输入参数,请求的Url地址,如:http://localcast:8080/source
+ * @param strResponse 输出参数,返回的内容
+ * @param filename 文件的绝对地址,如/home/usr/cidw.log
+ * @return 函数返回0执行成功
+ */
+
+ int upload_file(const std::string &strUrl, const std::string &filename, std::string &strResponse);
+
+ /**
+ * @brief 下载文件,如果板子支持wget下载会方便很多接口保留,待确认后在开发
+ * @param strUrl 输入参数,请求的Url地址,如:http://localcast:8080/source
+ * @param strResponse 输出参数,返回的内容
+ * @param filename 文件的绝对地址,如/home/usr/cidw.log
+ * @return 函数返回0执行成功
+ */
+
+ // int download_file(const std::string &strUrl, const std::string &filename, std::string &strResponse);
+
+ /**
+ * @brief 通过socket发送文件
+ * @param filename 文件名
+ * @return 函数返回0执行成功
+ */
+ int Send_file_socket(const std::string &filename);
+
+ /**
+ * @brief 通过socket发送文件夹下所有文件
+ * @param dirname 文件名
+ * @return 函数返回0执行成功
+ */
+ int Send_Dir_socket(const char *dirname);
+
+ int download(char* pFilename,string& strUrl,string& strResponse,bool bDownload);
+
+public:
+ void SetDebug(bool bDebug);
+private:
+ bool m_bDebug;
+};
+
+extern DataTrans *pDataTrans;
+#endif
+
+
diff --git a/jsonparse/SH_JsonCmd.cpp b/jsonparse/SH_JsonCmd.cpp
index d5f8511..95ebc48 100644
--- a/jsonparse/SH_JsonCmd.cpp
+++ b/jsonparse/SH_JsonCmd.cpp
@@ -6,6 +6,7 @@ namespace{
PlatformInit *platform = PlatformInit::instance();
Calculation *pCalculation = Calculation::instance();
Uart *pUart = Uart::instance();
+
}
@@ -245,7 +246,57 @@ std::string JsonData::JsonCmd_27(Json::Value & recvBody)
return showValue.write(jsonVal);
}
+std::string JsonData::JsonCmd_50(Json::Value & recvBody)
+{
+ Json::Value jsonVal;
+ jsonVal.clear();
+ jsonVal["success"] = true;
+ jsonVal["message"] = "";
+ jsonVal["cmd"] = "50";
+ jsonVal["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
+ std::string updateDevice = recvBody["updateDevice"].asString();//1 DataNode 2 GateWay
+ std::string updateURL = recvBody["updateURL"].asString();
+ std::string updateName = recvBody["updateName"].asString();
+ std::string strResponse;
+ updateName = "/opt/update/DataNode/" + updateName;
+ bool bDownload = 1;
+ int iRet = pDataTrans->download( (char*)updateName.c_str(),updateURL, strResponse, bDownload);
+ if(iRet != 0){
+ jsonVal["success"] = false;
+ jsonVal["message"] = "download failed";
+ return showValue.write(jsonVal);
+ }
+ if(updateDevice == "1"){
+ string strcmd = "tar zxvf ";
+ strcmd = strcmd + updateName;
+ strcmd = strcmd + " -C /opt/update/DataNode/";
+ system(strcmd.c_str());
+ }else{
+
+ }
+ return showValue.write(jsonVal);
+}
+std::string JsonData::JsonCmd_51(Json::Value & recvBody)
+{
+ Json::Value jsonVal;
+ jsonVal.clear();
+ jsonVal["success"] = true;
+ jsonVal["message"] = "";
+ jsonVal["cmd"] = "51";
+ jsonVal["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
+ std::string DataNodeNo = recvBody["dataNodeNo"].asString();
+ std::string DataNodeName = recvBody["dataNodeName"].asString();
+ char szSql[100]={0x00};
+ sprintf(szSql,"update %s set dataNodeName = '%s' where dataNodeNo = '%s' ",T_SENSOR_INFO(TNAME),\
+ DataNodeName.c_str(),DataNodeName.c_str());
+ int iRet = sql_ctl->UpdateTableData(szSql);
+ if(iRet != 0){
+ jsonVal["success"] = false;
+ jsonVal["message"] = "updata dataNodeName failed";
+ }
+ return showValue.write(jsonVal);
+}
std::string JsonData::JsonCmd_29(Param_29 ¶m)
{
@@ -303,18 +354,12 @@ void JsonData::JsonCmd_38(Json::Value &recvBody)
}
}
}
+
+
+//
void JsonData::JsonCmd_39(Json::Value &recvBody)
{
- std::ifstream inFile("/opt/update/a.bin",ios::in|ios::binary);
- if (!inFile) {
- print_error("read data error\n");
- } else {
- unsigned char szUpdatePackage[93]={0x00};
- while (inFile.read(szUpdatePackage, 92)) {
-
- }
- }
}
void JsonData::DataNodeStatusCheck()
{
diff --git a/jsonparse/SH_JsonCmd.hpp b/jsonparse/SH_JsonCmd.hpp
index 0f8e04a..93d77ec 100644
--- a/jsonparse/SH_JsonCmd.hpp
+++ b/jsonparse/SH_JsonCmd.hpp
@@ -4,6 +4,7 @@
#include
#include
#include
+#include
#include
#include
#include
@@ -11,6 +12,7 @@
#include "../common/SH_CommonFunc.hpp"
#include "../calculation/Calculation.hpp"
#include "../uart/SH_Uart.hpp"
+#include "../datatransfer/SH_Datatrans.hpp"
class JsonData {
@@ -24,6 +26,8 @@ public :
std::string JsonCmd_26(Param_26 ¶m);
std::string JsonCmd_27(Json::Value & recvBody);
std::string JsonCmd_29(Param_29 ¶m); //系统配置信息
+ std::string JsonCmd_50(Json::Value & recvBody);
+ std::string JsonCmd_51(Json::Value & recvBody);
void JsonCmd_38(Json::Value &recvBody); //获取原始数据
void JsonCmd_39(Json::Value &recvBody); //更新传感器程序
diff --git a/localserver/SH_LocalServer.cpp b/localserver/SH_LocalServer.cpp
index 1fb049a..438db6b 100644
--- a/localserver/SH_LocalServer.cpp
+++ b/localserver/SH_LocalServer.cpp
@@ -184,6 +184,14 @@ void LocalServer::HandleFromServer(const char *pData_r, int pLen, const char *to
data_publish(data.c_str(), GlobalConfig::Topic_G.mPubCmd.c_str());
}
break;
+ case 50:{
+ JsonData jd;
+ std::string data = jd.JsonCmd_50(recvBody);
+ data_publish(data.c_str(), GlobalConfig::Topic_G.mPubCmd.c_str());
+ }break;
+ case 51:{
+
+ }break;
default:
// data_publish_local(pData.c_str(), GlobalConfig::Topic_G.mPubLocalCmd.c_str());
break;
diff --git a/uart/SH_Uart.cpp b/uart/SH_Uart.cpp
index fa94cb2..14ed8fe 100644
--- a/uart/SH_Uart.cpp
+++ b/uart/SH_Uart.cpp
@@ -25,6 +25,7 @@ Uart::Uart():mUart(mIoSev),mStrand(mIoSev)
{
mRdLength = 0;
memset(mUartRecvBuf,0,BUF_LENGTH);
+ update = false;
}
void Uart::InitUart()
@@ -161,7 +162,8 @@ try{
}
if ( 0 == strHeadFlag.compare("DEDFEF") ) {
-
+ update = true;
+ LOG_INFO("ReadHandle flag = %s\n",strHeadFlag.c_str());
}
if ( 0 == strHeadFlag.compare("AA55AA") ) {
@@ -196,7 +198,7 @@ void Uart::WriteHandle(const char *strSend,const boost::system::error_code &ec,s
print_error("Fail To Write Uart! ]\n");
// LOG_ERROR("Fail To Write Uart! ]\n");
} else {
- print_info("[ To Uart ][ Bytes:%d ]\n",bytesWrite);
+ // print_info("[ To Uart ][ Bytes:%d ]\n",bytesWrite);
}
}
@@ -248,43 +250,162 @@ void Uart::UpdateZigbeeInfo(const char *pData)
print_info("local zigbee module info Mode : %d Chan : %d PanID : %s MyAddr : %s DstAddr : %s\n", GlobalConfig::ZigbeeInfo_G.DevMode, GlobalConfig::ZigbeeInfo_G.Channel,
GlobalConfig::ZigbeeInfo_G.PanID.c_str(),GlobalConfig::ZigbeeInfo_G.MyAddr.c_str(), GlobalConfig::ZigbeeInfo_G.DstAddr.c_str());
- LOG_INFO("[UpdateZigbeeInfo---] ZigBee PanID: %s ; Channel: %d ; MyAddr : %s ",GlobalConfig::ZigbeeInfo_G.PanID.c_str(),GlobalConfig::ZigbeeInfo_G.Channel,GlobalConfig::ZigbeeInfo_G.MyAddr.c_str());
+// LOG_INFO("[UpdateZigbeeInfo---] ZigBee PanID: %s ; Channel: %d ; MyAddr : %s ",GlobalConfig::ZigbeeInfo_G.PanID.c_str(),GlobalConfig::ZigbeeInfo_G.Channel,GlobalConfig::ZigbeeInfo_G.MyAddr.c_str());
}
+void int2bytes(int i, unsigned char* bytes, int size) {
+ // byte[] bytes = new byte[4];
+ memset(bytes,0,sizeof(unsigned char) * size);
+ bytes[0] = (unsigned char) (0xff & i);
+ bytes[1] = (unsigned char) ((0xff00 & i) >> 8);
+ bytes[2] = (unsigned char) ((0xff0000 & i) >> 16);
+ bytes[3] = (unsigned char) ((0xff000000 & i) >> 24);
+}
+void Uart::UpdateWirelessNode(unsigned char* id)
+{
+ LOG_INFO("UpdateWirelessNode id = %02x %02x\n",id[0],id[1]);
+ //compare hardversion in update list
+ std::vector strHWversion;
+ std::string strFileName = "";
+ strHWversion = ReadStrByOpt("/opt/update/DataNode/config.json",strFileName);
+ char gethardVersion_sql[32] = { 0 };
+ sprintf(gethardVersion_sql, "zigbeeShortAddr='%s'", id);
+ std::string hardVersion = sql_ctl->GetData(T_SENSOR_INFO(TNAME), T_SENSOR_INFO(HARDVERSION), gethardVersion_sql);
+ int thisindex = -1;
+ for(int i = 0; i < strHWversion.size();i++){
+ if(hardVersion == strHWversion[i]){
+ thisindex = i;
+ break;
+ }
+ }
+ if(thisindex < 0)
+ return;
+ FILE * pFile=NULL;
+ int thisSize = 0;
+ char *buffer=NULL;
+ strFileName = "/opt/update/DataNode/" + strFileName;
+ pFile = fopen (strFileName.c_str(),"rb");
+ if (pFile==NULL) perror ("Error opening file");
+ else
+ {
+ while (fgetc(pFile) != EOF) {
+ ++thisSize;
+ }
+ rewind(pFile);
+ buffer = (char*)malloc(thisSize);//
+ fread (buffer, sizeof (char), thisSize, pFile);
+ fclose (pFile);
+ unsigned char Data[12]={0x00};
+ unsigned char size[4]={0x00};
+
+ //帧头[3byte] 节点地址[2byte] 数据类型[1byte] 序号[1byte] 升级包大小[4byte] CRC校验[1byte]
+ Data[0]=0xAA;
+ Data[1]=0x55;
+ Data[2]=0xAA;
+ Data[3]=id[0] & 0xFF;
+ Data[4]=id[1] & 0xFF;
+ Data[5]=0x07;
+ Data[6]=0x00;
+ int2bytes(thisSize,size,4);
+ Data[7]=size[3];
+ Data[8]=size[2];
+ Data[9]=size[1];
+ Data[10]=size[0];
+ unsigned char tmp = 0x00;
+ for(int i = 0 ; i < 11;i++){
+ tmp +=Data[i];
+ }
+ Data[11]=tmp;
+ WriteToUart((const char*)Data,12);
+ boost::this_thread::sleep(boost::posix_time::milliseconds(100));
+ int Count = thisSize / 92;
+ int lastSize = thisSize % 92;
+ unsigned char UpdateData[100]={0x00};
+ //帧头[3byte] 节点地址[2byte] 数据类型[1byte] 序号[1byte] 数据包[92byte] CRC校验[1byte]
+ print_info("Start Update!!! file Size = %d\n",thisSize);
+ for(int j = 0; j < Count;j++){
+ UpdateData[0]=0xAA;
+ UpdateData[1]=0x55;
+ UpdateData[2]=0xAA;
+ UpdateData[3]=id[0];
+ UpdateData[4]=id[1];
+ UpdateData[5]=0x08;
+ UpdateData[6]=0xff & j;
+ memcpy(&UpdateData[7],buffer+92*j,92);
+ tmp = 0x00;
+ for(int k = 0; k < 99;k++){
+ tmp +=UpdateData[k];
+ }
+ UpdateData[99] = tmp;
+ WriteToUart((const char*)UpdateData,100);
+ boost::this_thread::sleep(boost::posix_time::milliseconds(100));
+ memset(UpdateData,0x00,sizeof(UpdateData));
+ }
+ printf("Count =%d,lastSize = %d\n",Count,lastSize);
+ if(lastSize > 0){
+ UpdateData[0]=0xAA;
+ UpdateData[1]=0x55;
+ UpdateData[2]=0xAA;
+ UpdateData[3]=id[0];
+ UpdateData[4]=id[1];
+ UpdateData[5]=0x08;
+ UpdateData[6]=0xff & Count;
+ memcpy(&UpdateData[7],buffer+92*Count,lastSize);
+ tmp = 0x00;
+ for(int k = 0; k < 99;k++){
+ tmp +=UpdateData[k];
+ }
+ UpdateData[99] = tmp;
+ WriteToUart((const char*)UpdateData,100);
+ memset(UpdateData,0x00,sizeof(UpdateData));
+ }
+ print_info("Update END!!! file Size = %d\n",thisSize);
+ }
+ free(buffer);
+ ZigbeeInit();
+ update = false;
+ LOG_INFO("UpdateWirelessNode success");
+}
void Uart::DealRecvData(const char *pData)
{
char buf[8];
sprintf(buf, "%02d", pData[5]&0xFF);
int flag = boost::lexical_cast(buf);
print_info("the data package type(1,2,3,4,5,6) is %s,%x\n",buf,pData[5]&0xFF);
+
switch (flag)
{
- case 1:{
+ case 1:{//0x01:设备信息
DealDataNodeInfo(pData);
}
break;
- case 2:{
+ case 2:{//0x02:特征值
DealDataNodeFeature(pData, 0);
}
break;
- case 3:{
+ case 3:{//0x03:长波形X轴
DealDataNodeWave(pData);
}
break;
- case 4:{
+ case 4:{//0x04:长波形Y轴
DealDataNodeWave(pData);
}
break;
- case 5:{
+ case 5:{//0x05:长波形Z轴
DealDataNodeWave(pData);
}
break;
- case 6:{
+ case 6:{//0x06:特征值+长波形
DealDataNodeFeature(pData, 1);
}
break;
+ case 7:{//0x07:升级
+
+ //DealDataNodeFeature(pData, 1);
+ }
+ break;
default:
break;
}
@@ -511,23 +632,29 @@ void Uart::DealDataNodeInfo(const char *pData)
void Uart::DealDataNodeFeature(const char *pData, int flag)
{
+ print_info("recv feature\n");
+ RecvData * pRecvData = (RecvData *)pData;
+
+ char buf[8];
+ sprintf(buf, "%02x%02x", pRecvData->ShortAddr[0], pRecvData->ShortAddr[1]);
+ LOG_INFO("DealDataNodeFeature %02x%02x\n",pRecvData->ShortAddr[0], pRecvData->ShortAddr[1]);
+
+ std::string strShortAddr = std::string(buf);
+ print_info("zigbeeShortAddr='%s'", strShortAddr.c_str());
if (1 == flag) {
print_info("recv wave minute\n");
unsigned short shortAddr = 0x9999;
- WriteShortAddr2Zigbee(shortAddr);
- LOG_INFO("[DealDataNodeFeature] ZigBee PanID: %s ; Channel: %d ; MyAddr : %4x ",GlobalConfig::ZigbeeInfo_G.PanID.c_str(),GlobalConfig::ZigbeeInfo_G.Channel,
- (unsigned short)GlobalConfig::Zigbee_G.MyAddr);
+
+ WriteShortAddr_DistAddr2Zigbee(shortAddr,pRecvData->ShortAddr);
+// LOG_INFO("[DealDataNodeFeature] ZigBee PanID: %s ; Channel: %d ; MyAddr : %4x ",GlobalConfig::ZigbeeInfo_G.PanID.c_str(),GlobalConfig::ZigbeeInfo_G.Channel,
+// (unsigned short)GlobalConfig::Zigbee_G.MyAddr);
+ UpdateWirelessNode(pRecvData->ShortAddr);
// 进入传输原始数据状态,启动计数 60秒
GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = ENTER_TRANSMITTING_STATUS;
GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
}
- print_info("recv feature\n");
- RecvData * pRecvData = (RecvData *)pData;
-
- char buf[8];
- sprintf(buf, "%02x%02x", pRecvData->ShortAddr[0], pRecvData->ShortAddr[1]);
- std::string strShortAddr = std::string(buf);
+
char getLongAddr_sql[32] = { 0 };
//根据数据包中的传感器的短地址获取数据库中长地址(MAC),在下面判断该传感器是否存在,如果不存在则把数据包丢弃
sprintf(getLongAddr_sql, "zigbeeShortAddr='%s'", strShortAddr.c_str());
@@ -1700,7 +1827,7 @@ void Uart::DealWaveThread() //连续三秒没有原始数据,则处理缓存
tmpTimeStamp = m_TimeStamp - nowTimeStamp;
}
//if ((nowTimeStamp - m_TimeStamp) > 3) { 时间戳需要修改为绝对值,可能丢失时钟,或工作一会儿,才同步时钟,可能减出异常值
- if (tmpTimeStamp > 3) { // TODO: 时间戳需要修改为绝对值,可能丢失时钟,或工作一会儿,才同步时钟,可能减出异常值 print_info("yes!The time difference is more than 3,nowTimeStamp : %ld m_TimeStamp : %ld\n", nowTimeStamp, m_TimeStamp);
+ if (tmpTimeStamp > 3 ) { // TODO: 时间戳需要修改为绝对值,可能丢失时钟,或工作一会儿,才同步时钟,可能减出异常值 print_info("yes!The time difference is more than 3,nowTimeStamp : %ld m_TimeStamp : %ld\n", nowTimeStamp, m_TimeStamp);
DealWave();
m_TimeStamp = 0;
pUart->UpdateZigbeeInfoCtrl();
@@ -1896,7 +2023,18 @@ void Uart::DealWave()
// std::string strShortAddr = std::string(buf);
}
+void Uart::modify_distaddr_info(unsigned short id, char * zigbee,unsigned char* distAddr)
+{
+ /*char command[6];
+ command[0] = 0xDE;
+ command[1] = 0xDF;
+ command[2] = 0xEF;
+ command[3] = 0xD2;
+ command[4] = destaddr[0];
+ command[5] = destaddr[1];
+ WriteToUart(command, 6);*/
+}
void Uart::modify_info(unsigned short id, char * zigbee)
{
int i, j, ret,con;
@@ -1982,9 +2120,21 @@ void Uart::WriteShortAddr2Zigbee(unsigned short pad)
zigbee_reset(tmp, 1);
}
+void Uart::WriteShortAddr_DistAddr2Zigbee(unsigned short pad,unsigned char* id)
+{
+ print_info("WriteShortAddr2Zigbee : %4x\n", (unsigned short)pad);
+ unsigned short pad1 = pad,tmp;
+ tmp = GlobalConfig::Zigbee_G.MyAddr;
+ swap((char *)&pad1);
+ swap((char *)&tmp);
+ GlobalConfig::Zigbee_G.MyAddr = BUILD_UINT16(id[0],id[1]);
+ GlobalConfig::ZigbeeInfo_G.MyAddr = "9999";
+ modify_info(tmp, (char *)& GlobalConfig::Zigbee_G);
+ zigbee_reset(tmp, 1);
+}
+
void Uart::ZigbeeInit()
{
-
std::string strPanId = GlobalConfig::MacAddr_G.substr(8);
print_info("strPanId : %s\n", strPanId.c_str());
// 新增管理ZigBee代码
diff --git a/uart/SH_Uart.hpp b/uart/SH_Uart.hpp
index 55524a1..b305468 100644
--- a/uart/SH_Uart.hpp
+++ b/uart/SH_Uart.hpp
@@ -14,6 +14,7 @@
+
//using namespace boost::asio;
//using std::string;
//using boost::system::error_code;
@@ -52,10 +53,13 @@ public :
void ZigbeeInit();
void modify_info(unsigned short id, char * zigbee);
+ void modify_distaddr_info(unsigned short id, char * zigbee,unsigned char* distAddr);
void zigbee_reset(unsigned short pad, unsigned short type);
void WriteChanl2Zigbee(unsigned char pad);
void WritePanId2Zigbee(unsigned short pad);
void WriteShortAddr2Zigbee(unsigned short pad);
+ void WriteShortAddr_DistAddr2Zigbee(unsigned short pad,unsigned char* id);
+ void UpdateWirelessNode(unsigned char* id);
virtual void DataAnalysis_R(DevDataOfGwid &pData);
virtual void DataAnalysis_W(DevData &pData,bool pFlag);
virtual void ThreadInit();
@@ -65,6 +69,7 @@ private :
onReceiveUart m_callback;
std::vector m_VecWaveData;
unsigned long m_TimeStamp;
+ bool update;
};