增加云端修改测点名称和下载传感器固件更新接口,
This commit is contained in:
parent
78baf7a471
commit
df80a803f0
30
.cproject
30
.cproject
@ -31,24 +31,28 @@
|
||||
<option id="gnu.cpp.compiler.option.debugging.level.287276407" name="Debug Level" superClass="gnu.cpp.compiler.option.debugging.level" useByScannerDiscovery="false" value="gnu.cpp.compiler.debugging.level.max" valueType="enumerated"/>
|
||||
<option id="gnu.cpp.compiler.option.include.paths.801625659" name="Include paths (-I)" superClass="gnu.cpp.compiler.option.include.paths" useByScannerDiscovery="false" valueType="includePath">
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/boost/include"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/curl/include"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/fftw/include"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/jsoncpp/include"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/sqlite/include"/>
|
||||
</option>
|
||||
<option id="gnu.cpp.compiler.option.dialect.std.1150808616" name="Language standard" superClass="gnu.cpp.compiler.option.dialect.std" useByScannerDiscovery="true" value="gnu.cpp.compiler.dialect.default" valueType="enumerated"/>
|
||||
<inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.1387553514" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
|
||||
</tool>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cross.c.linker.310128155" name="Cross GCC Linker" superClass="cdt.managedbuild.tool.gnu.cross.c.linker"/>
|
||||
<tool command="g++" commandLinePattern="${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS}" errorParsers="org.eclipse.cdt.core.GLDErrorParser" id="cdt.managedbuild.tool.gnu.cross.cpp.linker.1049583485" name="Cross G++ Linker" superClass="cdt.managedbuild.tool.gnu.cross.cpp.linker">
|
||||
<option id="gnu.cpp.link.option.paths.1174701783" name="Library search path (-L)" superClass="gnu.cpp.link.option.paths" useByScannerDiscovery="false" valueType="libPaths">
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/boost/lib"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/ssl/lib"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/curl/lib"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/DataWatchThirdParty/ssl/lib"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/fftw/lib"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/jsoncpp"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/mqtt1410/lib_1.1"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/mqtt1410/lib"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/sqlite/lib"/>
|
||||
</option>
|
||||
<option id="gnu.cpp.link.option.libs.1027000140" name="Libraries (-l)" superClass="gnu.cpp.link.option.libs" useByScannerDiscovery="false" valueType="libs">
|
||||
<listOptionValue builtIn="false" value="sqlite3"/>
|
||||
<listOptionValue builtIn="false" value="curl"/>
|
||||
<listOptionValue builtIn="false" value="boost_system"/>
|
||||
<listOptionValue builtIn="false" value="pthread"/>
|
||||
<listOptionValue builtIn="false" value="boost_thread"/>
|
||||
@ -104,31 +108,36 @@
|
||||
<option id="gnu.cpp.compiler.option.debugging.level.2146025035" name="Debug Level" superClass="gnu.cpp.compiler.option.debugging.level" useByScannerDiscovery="false" value="gnu.cpp.compiler.debugging.level.none" valueType="enumerated"/>
|
||||
<option id="gnu.cpp.compiler.option.include.paths.1740362159" name="Include paths (-I)" superClass="gnu.cpp.compiler.option.include.paths" useByScannerDiscovery="false" valueType="includePath">
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/boost/include"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/curl/include"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/fftw/include"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/jsoncpp/include"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/sqlite/include"/>
|
||||
</option>
|
||||
<option id="gnu.cpp.compiler.option.dialect.std.603357595" name="Language standard" superClass="gnu.cpp.compiler.option.dialect.std" useByScannerDiscovery="true" value="gnu.cpp.compiler.dialect.default" valueType="enumerated"/>
|
||||
<inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.542083696" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
|
||||
</tool>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cross.c.linker.1480789657" name="Cross GCC Linker" superClass="cdt.managedbuild.tool.gnu.cross.c.linker"/>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cross.cpp.linker.762964696" name="Cross G++ Linker" superClass="cdt.managedbuild.tool.gnu.cross.cpp.linker">
|
||||
<option id="gnu.cpp.link.option.paths.1792108116" name="Library search path (-L)" superClass="gnu.cpp.link.option.paths" useByScannerDiscovery="false" valueType="libPaths">
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/boost/lib"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/ssl/lib"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/curl/lib"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/curl/lib"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/DataWatchThirdParty/ssl/lib"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/fftw/lib"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/jsoncpp"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/mqtt1410/lib_1.1"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/mqtt1410/lib"/>
|
||||
<listOptionValue builtIn="false" value="/nfsroot/eclipse-workspace/GatewayThirdParty/sqlite/lib"/>
|
||||
</option>
|
||||
<option id="gnu.cpp.link.option.libs.2006599708" name="Libraries (-l)" superClass="gnu.cpp.link.option.libs" useByScannerDiscovery="false" valueType="libs">
|
||||
<listOptionValue builtIn="false" value="sqlite3"/>
|
||||
<listOptionValue builtIn="false" value="curl"/>
|
||||
<listOptionValue builtIn="false" value="ssl"/>
|
||||
<listOptionValue builtIn="false" value="crypto"/>
|
||||
<listOptionValue builtIn="false" value="boost_system"/>
|
||||
<listOptionValue builtIn="false" value="pthread"/>
|
||||
<listOptionValue builtIn="false" value="boost_thread"/>
|
||||
<listOptionValue builtIn="false" value="boost_date_time"/>
|
||||
<listOptionValue builtIn="false" value="fftw3"/>
|
||||
<listOptionValue builtIn="false" value="ssl"/>
|
||||
<listOptionValue builtIn="false" value="crypto"/>
|
||||
<listOptionValue builtIn="false" value="json"/>
|
||||
<listOptionValue builtIn="false" value="mosquitto"/>
|
||||
</option>
|
||||
@ -167,7 +176,14 @@
|
||||
</scannerConfigBuildInfo>
|
||||
</storageModule>
|
||||
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
|
||||
<storageModule moduleId="refreshScope"/>
|
||||
<storageModule moduleId="refreshScope" versionNumber="2">
|
||||
<configuration configurationName="Debug">
|
||||
<resource resourceType="PROJECT" workspacePath="/WirelessGateway"/>
|
||||
</configuration>
|
||||
<configuration configurationName="Release">
|
||||
<resource resourceType="PROJECT" workspacePath="/WirelessGateway"/>
|
||||
</configuration>
|
||||
</storageModule>
|
||||
<storageModule moduleId="org.eclipse.cdt.make.core.buildtargets">
|
||||
<buildTargets>
|
||||
<target name="all" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
|
||||
|
@ -142,7 +142,25 @@ std::string ReadStrByOpt(std::string filename, std::string config, std::string o
|
||||
is.close();
|
||||
return value;
|
||||
}
|
||||
std::vector <std::string> ReadStrByOpt(std::string filename,std::string strUpdataFileName)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(s_config_mu);
|
||||
Json::Value root,hwVersion;
|
||||
Json::Reader reader;
|
||||
std::vector <std::string> 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);
|
||||
|
@ -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 <std::string> ReadStrByOpt(std::string filename,std::string strUpdataFileName);
|
||||
|
||||
extern void swap(char *data);
|
||||
|
||||
|
||||
|
513
datatransfer/SH_Datatrans.cpp
Normal file
513
datatransfer/SH_Datatrans.cpp
Normal file
@ -0,0 +1,513 @@
|
||||
#include "SH_Datatrans.hpp"
|
||||
#include "dirent.h"
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
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*>((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<<stderr<<res<<endl;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
printf("strResponse = %s\n",strResponse.c_str());
|
||||
if(bDownload){
|
||||
if(dlfile.stream)
|
||||
{
|
||||
//关闭文件流
|
||||
fclose(dlfile.stream);
|
||||
}
|
||||
}else{
|
||||
|
||||
}
|
||||
//释放全局curl对象
|
||||
curl_global_cleanup();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DataTrans::Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse)
|
||||
{
|
||||
CURLcode res;
|
||||
CURL* curl = curl_easy_init();
|
||||
if(NULL == curl) {
|
||||
return CURLE_FAILED_INIT;
|
||||
}
|
||||
if(m_bDebug)
|
||||
{
|
||||
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
|
||||
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
|
||||
}
|
||||
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_POST, 1);
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
|
||||
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_NOSIGNAL, 1);
|
||||
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
|
||||
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
|
||||
res = curl_easy_perform(curl);
|
||||
curl_easy_cleanup(curl);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
int DataTrans::Get(const std::string & strUrl, std::string & strResponse)
|
||||
{
|
||||
CURLcode res;
|
||||
CURL* curl = curl_easy_init();
|
||||
if(NULL == curl) {
|
||||
return CURLE_FAILED_INIT;
|
||||
}
|
||||
if(m_bDebug) {
|
||||
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
|
||||
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
|
||||
}
|
||||
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
|
||||
/**
|
||||
* 当多个线程都使用超时处理的时候,同时主线程中有sleep或是wait等操作。
|
||||
* 如果不设置这个选项,libcurl将会发信号打断这个wait从而导致程序退出。
|
||||
*/
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
|
||||
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
|
||||
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
|
||||
res = curl_easy_perform(curl);
|
||||
curl_easy_cleanup(curl);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int DataTrans::Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath)
|
||||
{
|
||||
CURLcode res;
|
||||
CURL* curl = curl_easy_init();
|
||||
if(NULL == curl) {
|
||||
return CURLE_FAILED_INIT;
|
||||
}
|
||||
|
||||
if(m_bDebug) {
|
||||
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
|
||||
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
|
||||
}
|
||||
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_POST, 1);
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
|
||||
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_NOSIGNAL, 1);
|
||||
if(NULL == pCaPath) {
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
|
||||
} else {
|
||||
//缺省情况就是PEM,所以无需设置,另外支持DER
|
||||
//curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
|
||||
curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
|
||||
}
|
||||
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
|
||||
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
|
||||
res = curl_easy_perform(curl);
|
||||
curl_easy_cleanup(curl);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int DataTrans::Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath)
|
||||
{
|
||||
CURLcode res;
|
||||
CURL* curl = curl_easy_init();
|
||||
if(NULL == curl) {
|
||||
return CURLE_FAILED_INIT;
|
||||
}
|
||||
if(m_bDebug) {
|
||||
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
|
||||
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
|
||||
}
|
||||
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
|
||||
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_NOSIGNAL, 1);
|
||||
if(NULL == pCaPath) {
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
|
||||
} else {
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
|
||||
curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
|
||||
}
|
||||
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
|
||||
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
|
||||
res = curl_easy_perform(curl);
|
||||
curl_easy_cleanup(curl);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
int DataTrans::upload_file(const std::string &strUrl, const std::string &filename, std::string &response)
|
||||
{
|
||||
CURL *curl;
|
||||
CURLM *multi_handle;
|
||||
int still_running;
|
||||
char res[60];
|
||||
struct curl_httppost *formpost = NULL;
|
||||
struct curl_httppost *lastptr = NULL;
|
||||
struct curl_slist *headerlist = NULL;
|
||||
static const char buf[] = "Expect:";
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
/* Fill in the file upload field. This makes libcurl load data from
|
||||
the given file name when curl_easy_perform() is called. */
|
||||
|
||||
/* Fill in the filename field */
|
||||
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "filename", CURLFORM_FILE, filename.c_str(), CURLFORM_END);
|
||||
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "upload", CURLFORM_END);
|
||||
|
||||
curl = curl_easy_init();
|
||||
multi_handle = curl_multi_init();
|
||||
/* initalize custom header list (stating that Expect: 100-continue is not wanted */
|
||||
headerlist = curl_slist_append(headerlist, buf);
|
||||
if (curl && multi_handle) {
|
||||
/* what URL that receives this POST */
|
||||
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
|
||||
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, res);
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
|
||||
curl_multi_add_handle(multi_handle, curl);
|
||||
curl_multi_perform(multi_handle, &still_running);
|
||||
do {
|
||||
struct timeval timeout;
|
||||
int rc;
|
||||
fd_set fdread;
|
||||
fd_set fdwrite;
|
||||
fd_set fdexcep;
|
||||
int maxfd = -1;
|
||||
long curl_timeo = -1;
|
||||
FD_ZERO(&fdread);
|
||||
FD_ZERO(&fdwrite);
|
||||
FD_ZERO(&fdexcep);
|
||||
/* set a suitable timeout to play around with */
|
||||
timeout.tv_sec = 1;
|
||||
timeout.tv_usec = 0;
|
||||
curl_multi_timeout(multi_handle, &curl_timeo);
|
||||
if (curl_timeo >= 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<string> 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<std::string>::iterator iter;
|
||||
for(iter = v.begin();iter != v.end();++iter) {
|
||||
temp = *iter;
|
||||
cout << temp.c_str() <<endl;
|
||||
int file_name_length = (int)strlen(temp.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,temp.c_str(),file_name_length,0) < 0) {
|
||||
print_error("Send File_Name Failed!\n");
|
||||
} else {
|
||||
print_info("Send File_Name Success!\n");
|
||||
}
|
||||
/*
|
||||
*发送文件
|
||||
*/
|
||||
std::string filename = std::string(dirname + temp);
|
||||
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;
|
||||
}
|
||||
}
|
||||
void DataTrans::SetDebug(bool bDebug)
|
||||
{
|
||||
m_bDebug = bDebug;
|
||||
}
|
108
datatransfer/SH_Datatrans.hpp
Normal file
108
datatransfer/SH_Datatrans.hpp
Normal file
@ -0,0 +1,108 @@
|
||||
#ifndef _DATATRANS_H_
|
||||
#define _DATATRANS_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
#include <curl/curl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdlib.h>
|
||||
#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<DataTrans>
|
||||
{
|
||||
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
|
||||
|
||||
|
@ -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()
|
||||
{
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <stdint.h>
|
||||
#include <boost/algorithm/string/split.hpp>
|
||||
#include <boost/algorithm/string/classification.hpp>
|
||||
#include <boost/thread/thread.hpp>
|
||||
@ -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); //更新传感器程序
|
||||
|
||||
|
@ -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;
|
||||
|
190
uart/SH_Uart.cpp
190
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<std::string> 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<int>(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代码
|
||||
|
@ -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<RecvData> m_VecWaveData;
|
||||
unsigned long m_TimeStamp;
|
||||
bool update;
|
||||
};
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user