TCLCommand.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. #include "StdAfx.h"
  2. #include "TCLCommand.h"
  3. #define NoneOptLen 5
  4. #define MINSIZE 128
  5. #define MIDSIZE 4096
  6. #define MAXSIZE 10240
  7. byte* TCLCommand::m_pData = NULL;
  8. TCLCommand::TCLCommand(bool bSync):CBaseSerial(bSync ? 0 : FILE_FLAG_OVERLAPPED)
  9. {
  10. m_pData = new byte[MAXSIZE];
  11. }
  12. TCLCommand::~TCLCommand(void)
  13. {
  14. if (m_pData)
  15. delete []m_pData;
  16. m_pData = NULL;
  17. m_vtExternalCMDParams.clear();
  18. m_vtInternalCMDParams.clear();
  19. }
  20. int TCLCommand::pares_time_value(std::string strTime)
  21. {
  22. int nTimes = 0;
  23. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  24. if (strstr(strTime.c_str(), _T("ms")) || strstr(strTime.c_str(), _T("MS")))
  25. {
  26. nTimes = atol(strTime.c_str());
  27. }
  28. else if (strstr(strTime.c_str(), _T("s")) || strstr(strTime.c_str(), _T("S")))
  29. {
  30. nTimes = atol(strTime.c_str()) * 1000;
  31. }
  32. else if (strstr(strTime.c_str(), _T("m")) || strstr(strTime.c_str(), _T("M")))
  33. {
  34. nTimes = atol(strTime.c_str()) * 6000;
  35. }
  36. else
  37. {
  38. // 不带单位或其他的,默认ms;
  39. nTimes = atol(strTime.c_str());
  40. }
  41. #elif _MSC_VER >= 1500
  42. if (_tcsstr(strTime.c_str(), _T("ms")) || _tcsstr(strTime.c_str(), _T("MS")))
  43. {
  44. nTimes = _tstol(strTime.c_str());
  45. }
  46. else if (_tcsstr(strTime.c_str(), _T("s")) || _tcsstr(strTime.c_str(), _T("S")))
  47. {
  48. nTimes = _tstol(strTime.c_str()) * 1000;
  49. }
  50. else if (_tcsstr(strTime.c_str(), _T("m")) || _tcsstr(strTime.c_str(), _T("M")))
  51. {
  52. nTimes = _tstol(strTime.c_str()) * 6000;
  53. }
  54. else
  55. {
  56. // 不带单位或其他的,默认ms;
  57. nTimes = _tstol(strTime.c_str());
  58. }
  59. #endif
  60. return nTimes;
  61. }
  62. bool TCLCommand::parse_pair_key(std::string& RetValue, std::string strLine, TCHAR* lpszText)
  63. {
  64. TCHAR szText[MAX_PATH] = { 0 };
  65. TCHAR szValue[MAX_PATH] = { 0 };
  66. // 去除空格;
  67. #if _MSC_VER > 1900
  68. strLine.erase(std::remove_if(strLine.begin(), strLine.end(), [](unsigned char x) {return std::isspace(x); }), strLine.end()); //C++17
  69. #else
  70. for (std::string::iterator it = strLine.begin(); it != strLine.end();) {
  71. !isspace(*it) ? it++ : it = it = strLine.erase(it);
  72. }
  73. #endif
  74. #if _MSC_VER >= 1200 && _MSC_VER < 1500 // VC6.0~8.0
  75. if (2 == sscanf(strLine.c_str(), _T("%[^=]=%s"), szText, MAX_PATH, szValue, MAX_PATH))
  76. #else
  77. if (2 == _stscanf_s(strLine.c_str(), _T("%[^=]=%s"), szText, MAX_PATH, szValue, MAX_PATH))
  78. #endif
  79. {
  80. if (_tcsstr(szText, lpszText)) {
  81. RetValue = szValue;
  82. return true;
  83. }
  84. }
  85. return false;
  86. }
  87. int TCLCommand::parse_cmds_from_file(const TCHAR* file_name, std::vector<CommandParam>& vtCommandParams)
  88. {
  89. TCHAR buf[MAX_PATH] = { 0 };
  90. TCHAR name[MAX_PATH] = { 0 };
  91. TCHAR option[MAX_PATH] = { 0 };
  92. TCHAR head[MAX_PATH] = { 0 };
  93. TCHAR code[MAX_PATH] = { 0 };
  94. TCHAR param[MAX_PATH] = { 0 };
  95. TCHAR multicode[MAX_PATH] = { 0 };
  96. TCHAR cmd_wait_time[MAX_PATH] = { 0 };
  97. TCHAR read_wait_time[MAX_PATH] = { 0 };
  98. int ret = -1;
  99. FILE* fp = NULL;
  100. if (!file_name || file_name[0] == '\0')
  101. return ret;
  102. fp = fopen(file_name, "r");
  103. if (!fp)
  104. goto EXIT;
  105. while ((fgets((char*)buf, MAX_PATH, fp) != NULL)) {
  106. int tmp_len = 0;
  107. tmp_len = _tcslen(buf);
  108. if (tmp_len >= 1) {
  109. if (buf[tmp_len - 1] == '\r' || buf[tmp_len - 1] == '\n')
  110. buf[tmp_len - 1] = 0;
  111. if (tmp_len >= 2) {
  112. if (buf[tmp_len - 2] == '\r' || buf[tmp_len - 2] == '\n')
  113. buf[tmp_len - 2] = 0;
  114. }
  115. }
  116. #if _MSC_VER >= 1200 && _MSC_VER < 1500 // VC6.0~8.0
  117. if (sscanf(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%[^;]", name, option, head, code, param, multicode, read_wait_time, cmd_wait_time) == 8)
  118. #elif _MSC_VER >= 1500
  119. //if ( _stscanf_s(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%[^;]", name, MAX_PATH, option, MAX_PATH, head, MAX_PATH, code, MAX_PATH, param, MAX_PATH, multicode, MAX_PATH, read_wait_time, MAX_PATH, cmd_wait_time, MAX_PATH) == 8) // 等价下面;
  120. if (_stscanf_s(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%s", name, MAX_PATH, option, MAX_PATH, head, MAX_PATH, code, MAX_PATH, param, MAX_PATH, multicode, MAX_PATH, read_wait_time, MAX_PATH, cmd_wait_time, MAX_PATH) == 8)
  121. #endif
  122. {
  123. CommandParam cp;
  124. parse_pair_key(cp.name, name, _T("Name"));
  125. parse_pair_key(cp.head, head, _T("HeadCode"));
  126. parse_pair_key(cp.code, code, _T("Command"));
  127. parse_pair_key(cp.param, param, _T("CMDParam"));
  128. std::string value;
  129. parse_pair_key(value, option, _T("Option"));
  130. if (!_tcsicmp(value.c_str(), _T("None")))
  131. cp.nOption = CMDOPT_None;
  132. else if (!_tcsicmp(value.c_str(), _T("Get")))
  133. cp.nOption = CMDOPT_Get;
  134. else if (!_tcsicmp(value.c_str(), _T("Set")))
  135. cp.nOption = CMDOPT_Set;
  136. parse_pair_key(value, multicode, _T("MultiParams"));
  137. cp.bMulticode = !_tcsicmp(value.c_str(), _T("true")) ? true : false;
  138. parse_pair_key(value, read_wait_time, _T("ReadWaitTime"));
  139. cp.read_wait_time = pares_time_value(value.c_str());
  140. parse_pair_key(value, cmd_wait_time, _T("CMDWaitTime"));
  141. cp.cmd_wait_time = pares_time_value(value.c_str());
  142. cp.UpdateRtnCode();
  143. vtCommandParams.push_back(cp);
  144. }
  145. }
  146. ret = 0;
  147. EXIT:
  148. if (fp)
  149. fclose(fp);
  150. return ret;
  151. }
  152. void TCLCommand::parse_cmds_from_string(std::string str, std::vector<CommandParam>& vtCommandParams)
  153. {
  154. int nPos(0);
  155. TCHAR buf[MAX_PATH] = { 0 };
  156. TCHAR name[MAX_PATH] = { 0 };
  157. TCHAR option[MAX_PATH] = { 0 };
  158. TCHAR head[MAX_PATH] = { 0 };
  159. TCHAR code[MAX_PATH] = { 0 };
  160. TCHAR param[MAX_PATH] = { 0 };
  161. TCHAR multicode[MAX_PATH] = { 0 };
  162. TCHAR cmd_wait_time[MAX_PATH] = { 0 };
  163. TCHAR read_wait_time[MAX_PATH] = { 0 };
  164. do
  165. {
  166. memset(buf, 0, MAX_PATH);
  167. int nPos1 = str.find_first_of('\r');
  168. int nPos2 = str.find_first_of('\n');
  169. if ( std::string::npos != nPos1 && std::string::npos != nPos2 ) {
  170. nPos = nPos1 > nPos2 ? nPos1 : nPos2;
  171. _stprintf_s(buf, _T("%s"), str.substr(0, nPos - 1).c_str());
  172. str = str.substr(nPos+1);
  173. }
  174. else if ( std::string::npos != nPos1 ) {
  175. _stprintf_s(buf, _T("%s"), str.substr(0, nPos1 - 1).c_str());
  176. str = str.substr(nPos + 1);
  177. }
  178. else if ( std::string::npos != nPos2 ) {
  179. _stprintf_s(buf, _T("%s"), str.substr(0, nPos2 - 1).c_str());
  180. str = str.substr(nPos + 1);
  181. }
  182. else {
  183. _stprintf_s(buf, _T("%s"), str.c_str());
  184. str.clear();
  185. }
  186. #if _MSC_VER >= 1200 && _MSC_VER < 1500 // VC6.0~8.0
  187. if (sscanf(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%[^;]", name, option, head, code, param, multicode, read_wait_time, cmd_wait_time) == 8)
  188. #elif _MSC_VER >= 1500
  189. //if ( _stscanf_s(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%[^;]", name, MAX_PATH, option, MAX_PATH, head, MAX_PATH, code, MAX_PATH, param, MAX_PATH, multicode, MAX_PATH, read_wait_time, MAX_PATH, cmd_wait_time, MAX_PATH) == 8) // 等价下面;
  190. if (_stscanf_s(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%s", name, MAX_PATH, option, MAX_PATH, head, MAX_PATH, code, MAX_PATH, param, MAX_PATH, multicode, MAX_PATH, read_wait_time, MAX_PATH, cmd_wait_time, MAX_PATH) == 8)
  191. #endif
  192. {
  193. CommandParam cp;
  194. parse_pair_key(cp.name, name, _T("Name"));
  195. parse_pair_key(cp.head, head, _T("HeadCode"));
  196. parse_pair_key(cp.code, code, _T("Command"));
  197. parse_pair_key(cp.param, param, _T("CMDParam"));
  198. std::string value;
  199. parse_pair_key(value, option, _T("Option"));
  200. if (!_tcsicmp(value.c_str(), _T("None")))
  201. cp.nOption = CMDOPT_None;
  202. else if (!_tcsicmp(value.c_str(), _T("Get")))
  203. cp.nOption = CMDOPT_Get;
  204. else if (!_tcsicmp(value.c_str(), _T("Set")))
  205. cp.nOption = CMDOPT_Set;
  206. parse_pair_key(value, multicode, _T("MultiParams"));
  207. cp.bMulticode = !_tcsicmp(value.c_str(), _T("true")) ? true : false;
  208. parse_pair_key(value, read_wait_time, _T("ReadWaitTime"));
  209. cp.read_wait_time = pares_time_value(value.c_str());
  210. parse_pair_key(value, cmd_wait_time, _T("CMDWaitTime"));
  211. cp.cmd_wait_time = pares_time_value(value.c_str());
  212. cp.UpdateRtnCode();
  213. vtCommandParams.push_back(cp);
  214. }
  215. } while (str.size());
  216. }
  217. bool TCLCommand::GetCommandParams(std::string name, CommandParam& cmdPara)
  218. {
  219. bool bget = false;
  220. // 外部优先;
  221. for (std::vector<CommandParam>::iterator it = m_vtExternalCMDParams.begin(); it != m_vtExternalCMDParams.end(); it++ )
  222. {
  223. if ( !_tcsicmp(name.c_str(), it->name.c_str()) ) {
  224. bget = true;
  225. cmdPara = *it;
  226. break;
  227. }
  228. }
  229. if ( !bget )
  230. {
  231. for (std::vector<CommandParam>::iterator it = m_vtInternalCMDParams.begin(); it != m_vtInternalCMDParams.end(); it++ )
  232. {
  233. if ( !_tcsicmp(name.c_str(), it->name.c_str()) ) {
  234. bget = true;
  235. cmdPara = *it;
  236. break;
  237. }
  238. }
  239. }
  240. // 清除状态;
  241. cmdPara.Clean();
  242. return bget;
  243. }
  244. bool TCLCommand::TheFirstPart(CommandParam& cmdPara, std::string data)
  245. {
  246. if (data.size() == NoneOptLen) {
  247. if ((byte)data[0] == cmdPara._rtnCode) {
  248. // 长度;
  249. int nPacketLen = (byte)data[1];
  250. if (nPacketLen != NoneOptLen) {
  251. cmdPara._rtnError = utils::_dprintf("[%s] 返回数据长度错误:%ld", __FUNCTION__, (byte)data[1]);
  252. return false;
  253. }
  254. // 执行状态;
  255. cmdPara._rtnStatus = (byte)data[2];
  256. //utils::_dprintf(_T("[%s] rtnStatus=%02X"), __FUNCTION__, cmdPara._rtnStatus);
  257. // 校验crc;
  258. unsigned short usCRCValue = utils::CRC16Calculate((byte*)data.data(), nPacketLen - 2);
  259. if (((usCRCValue >> 8) & 0xFF) != (byte)data[nPacketLen - 2] || (usCRCValue & 0xFF) != (byte)data[nPacketLen - 1]) {
  260. cmdPara._rtnError = utils::_dprintf("[%s] CRC校验错误:计算[%02% %02X] != 接收[%02X %02X]", __FUNCTION__, (usCRCValue >> 8) & 0xFF, usCRCValue & 0xFF, (byte)data[nPacketLen - 2], (byte)data[nPacketLen - 1]);
  261. return false;
  262. }
  263. }
  264. else {
  265. cmdPara._rtnError = utils::_dprintf("[%s] 返回码错误:%02X", __FUNCTION__, (byte)data[0]);
  266. return false;
  267. }
  268. }
  269. else {
  270. cmdPara._rtnError = utils::_dprintf("[%s] 返回数据长度错误", __FUNCTION__);
  271. return false;
  272. }
  273. return true;
  274. }
  275. bool TCLCommand::TheSecondPart(CommandParam& cmdPara, std::string data)
  276. {
  277. // 数据起始位;
  278. int nDataPos = 0;
  279. // 数据包长度;
  280. int nPacketLen = 0;
  281. if ((byte)data[0] == cmdPara._rtnCode) {
  282. // 获取长度;
  283. if ((byte)data[1] == 0xFE) {
  284. nDataPos = 4;
  285. nPacketLen = (byte)data[2] << 8 | (byte)data[3];
  286. if (data.size() < 255 && data[2] != 0) {
  287. cmdPara._rtnError = utils::_dprintf(_T("[%s] 返回数据长度异常"), __FUNCTION__);
  288. return false;
  289. }
  290. }
  291. else
  292. {
  293. nDataPos = 2;
  294. nPacketLen = (byte)data[1];
  295. #if 0 // 如果数据包含有非协议包内的数据,会判断异常;
  296. if (data.size() > 255) {
  297. //nPackageLen = data[1] << 8 | data[2];
  298. cmdPara._rtnError = _dprintf(_T("长度异常"));
  299. return false;
  300. }
  301. #endif
  302. }
  303. #if 0
  304. // 计算出的长度,必等于包长;// 如果包含有其他非包数据,会判断异常;
  305. if (nPackageLen != data.size())
  306. return false;
  307. #endif
  308. if (_tcsicmp(cmdPara.code.c_str(), utils::ByteToChars((byte)data[nDataPos] - 1).c_str()) != 0) {
  309. cmdPara._rtnError = utils::_dprintf(_T("[%s] 返回的指令错误, %s, %02X"), __FUNCTION__, cmdPara.head.c_str(), (byte)data[nDataPos]);
  310. return false;
  311. }
  312. // 返回的数据;
  313. ++nDataPos;// 返回码占一字节;
  314. if (cmdPara.bMulticode) {
  315. if (_tcsicmp(cmdPara.param.c_str(), utils::ByteToChars((byte)data[nDataPos]).c_str()) != 0) {
  316. cmdPara._rtnError = utils::_dprintf(_T("[%s] 返回的指令参数错误, %s, %02X"), __FUNCTION__, cmdPara.param.c_str(), (byte)data[nDataPos]);
  317. return false;
  318. }
  319. ++nDataPos;// 指令参数码占一字节;
  320. }
  321. cmdPara._rtnData = data.substr(nDataPos, nPacketLen - nDataPos - 2); //2 = crc;
  322. utils::_dprintf(_T("rtnData=%s"), utils::BytesToHexString((byte*)cmdPara._rtnData.c_str(), cmdPara._rtnData.size(), ' ').c_str());
  323. // 校验crc;
  324. unsigned short usCRCValue = utils::CRC16Calculate((byte*)data.data(), nPacketLen - 2);
  325. if (((usCRCValue >> 8) & 0xFF) != (byte)data[nPacketLen - 2] || (usCRCValue & 0xFF) != (byte)data[nPacketLen - 1])
  326. {
  327. cmdPara._rtnError = utils::_dprintf("[%s] CRC校验错误:计算[%02X %02X] != 接收[%02X %02X]", __FUNCTION__, (usCRCValue >> 8) & 0xFF, usCRCValue & 0xFF, (byte)data[nPacketLen - 2], (byte)data[nPacketLen - 1]);
  328. return false;
  329. }
  330. if (data.size() > nPacketLen)
  331. utils::_dprintf("[%s] 带有脏数据:%s", __FUNCTION__, data.substr(nPacketLen));
  332. }
  333. else {
  334. cmdPara._rtnError = utils::_dprintf("[%s] 返回码错误:%02X", __FUNCTION__, (byte)data[0]);
  335. return false;
  336. }
  337. return true;
  338. }
  339. void TCLCommand::PackingCommand(CommandParam& cmdPara, std::string data, const int& dataLen)
  340. {
  341. // Tag:[命令头][全命令长度][命令码]<命令码参数><附加数据>[crc1][crc2]
  342. std::string command;
  343. // 命令头标识位;
  344. command.append(utils::HexStringToBytes(cmdPara.head, 2).c_str(), cmdPara.head.size() / 2);
  345. // 命令码;
  346. command.append(utils::HexStringToBytes(cmdPara.code, 2).c_str(), cmdPara.code.size() / 2);
  347. // 命令码参数;
  348. command.append(utils::HexStringToBytes(cmdPara.param, 2).c_str(), cmdPara.param.size() / 2);
  349. // 附加的数据;
  350. if (dataLen > 0)
  351. command.append(data.c_str(), dataLen);
  352. int len(0);
  353. // 长度:可能1字节表示,超过255用2字节表示;
  354. byte szlen[2] = { 0 };
  355. //if ((byte)command[1] == 0xFE)
  356. //if ( cmdPara.head.size() >= 4 && cmdPara.head.find("FE", 2, 2) != std::string::npos )
  357. if (_tcsicmp(_T("AAFE"), cmdPara.head.c_str()) == 0)
  358. {// 长度超过255,需要2字节表示;
  359. len = command.size() + 4; // 2位crc + 2位长度;
  360. szlen[0] = (len >> 8) & 0xFF;
  361. szlen[1] = len & 0xFF;
  362. command.insert(2, (char*)szlen, 2);
  363. }
  364. else {
  365. // 2位crc + 1位长度;
  366. len = command.size() + 3;
  367. //if ( _tcsicmp(cmdPara.code.c_str(), "99 00") == 0 )
  368. // len -= 2;
  369. if (len > 255) {// 长度超过255,多一个占位符;
  370. ++len;
  371. szlen[0] = (len >> 8) & 0xFF;
  372. szlen[1] = len & 0xFF;
  373. command.insert(1, (char*)szlen, 2);
  374. }
  375. else {
  376. szlen[0] = len & 0xFF;
  377. command.insert(1, (char*)szlen, 1);
  378. }
  379. }
  380. // crc校验;
  381. byte szcrc[2] = { 0 };
  382. WORD usCRCValue = utils::CRC16Calculate((byte*)command.c_str(), command.size()); // 如果有0断开有危险;
  383. //WORD usCRCValue = CRC16Calculate((byte *)command.c_str(), len - 2);
  384. szcrc[0] = (usCRCValue >> 8) & 0xFF;
  385. szcrc[1] = usCRCValue & 0xFF;
  386. command.append((char*)szcrc, 2);
  387. cmdPara._cmdContext = command;
  388. utils::_dprintf(_T("指令:%s = %s"), cmdPara.name.c_str(), utils::BytesToHexString((byte*)command.c_str(), command.size(), ' ').c_str());
  389. }
  390. void TCLCommand::ParseResultString(CommandParam& cmdPara, std::string data, const int& dataLen)
  391. {
  392. // Tag:[返回头][全数据长度][返回码]<返回码子项><附加数据>[crc16]
  393. if (!TheFirstPart(cmdPara, data.substr(0, 5)))
  394. return;
  395. if (cmdPara._rtnStatus != 0x0A) {
  396. utils::_dprintf("[%s] 执行结果错误:%02X", __FUNCTION__, cmdPara._rtnStatus);
  397. return;
  398. }
  399. switch (cmdPara.nOption)
  400. {
  401. case CMDOPT_None:
  402. break;
  403. case CMDOPT_Get:
  404. case CMDOPT_Set:
  405. TheSecondPart(cmdPara, data.substr(5));
  406. break;
  407. default:
  408. break;
  409. }
  410. }
  411. bool TCLCommand::SendCommand(CommandParam& cmdPara)
  412. {
  413. memset(m_pData, 0, MAXSIZE);
  414. if (_dwIOMode == FILE_FLAG_OVERLAPPED)
  415. {
  416. if (!Write((void*)cmdPara._cmdContext.c_str(), cmdPara._cmdContext.size()))
  417. return false;
  418. Sleep(cmdPara.read_wait_time);
  419. int nReadCount = Read(m_pData, MAXSIZE);
  420. cmdPara._rtnContext.append((char*)m_pData, nReadCount);
  421. }
  422. else
  423. {
  424. if (!WriteSync((void*)cmdPara._cmdContext.c_str(), cmdPara._cmdContext.size()))
  425. return false;
  426. Sleep(cmdPara.read_wait_time);
  427. int nReadCount = ReadSync(m_pData, MAXSIZE);
  428. cmdPara._rtnContext.append((char*)m_pData, nReadCount);
  429. }
  430. utils::_dprintf("结果:%s = %s", cmdPara.name.c_str(), utils::BytesToHexString((byte*)cmdPara._rtnContext.c_str(), cmdPara._rtnContext.size(), ' ').c_str());
  431. ParseResultString(cmdPara, cmdPara._rtnContext, cmdPara._rtnContext.size());
  432. Sleep(cmdPara.cmd_wait_time);
  433. return cmdPara._rtnStatus == 0x0A ? true : false;
  434. }
  435. void TCLCommand::SetInternalCMDParams(DWORD dwResouceID)
  436. {
  437. std::string data;
  438. if ( utils::GetResourceData(dwResouceID, _T("BIN"), data) )
  439. {
  440. parse_cmds_from_string(data, m_vtInternalCMDParams);
  441. }
  442. }
  443. void TCLCommand::SetExternalCMDParams(LPCTSTR lpFileName)
  444. {
  445. parse_cmds_from_file(lpFileName, m_vtExternalCMDParams);
  446. }
  447. bool TCLCommand::EnterFactory()
  448. {
  449. CommandParam cmdpara;
  450. GetCommandParams("EnterFactory", cmdpara);
  451. PackingCommand(cmdpara);
  452. return SendCommand(cmdpara);
  453. }
  454. bool TCLCommand::LeaveFactory()
  455. {
  456. CommandParam cmdpara;
  457. GetCommandParams("LeaveFactory", cmdpara);
  458. PackingCommand(cmdpara);
  459. return SendCommand(cmdpara);
  460. }
  461. bool TCLCommand::GetProjectId(int& pid)
  462. {
  463. CommandParam cmdpara;
  464. GetCommandParams("GetProjectId", cmdpara);
  465. PackingCommand(cmdpara);
  466. if (SendCommand(cmdpara))
  467. {
  468. if (cmdpara._rtnData.size() == 2)
  469. pid = (byte)cmdpara._rtnData[0] << 8 | (byte)cmdpara._rtnData[1];
  470. else
  471. pid = (byte)cmdpara._rtnData[0];
  472. return true;
  473. }
  474. return false;
  475. }
  476. bool TCLCommand::GetSoftVersion(std::string& strValue)
  477. {
  478. CommandParam cmdpara;
  479. GetCommandParams("GetSoftVersion", cmdpara);
  480. PackingCommand(cmdpara);
  481. if (SendCommand(cmdpara))
  482. {
  483. strValue = cmdpara._rtnData;
  484. return true;
  485. }
  486. return false;
  487. }
  488. bool TCLCommand::GetDeviceId(std::string& strValue)
  489. {
  490. CommandParam cmdpara;
  491. GetCommandParams("GetDeviceId", cmdpara);
  492. PackingCommand(cmdpara);
  493. if (SendCommand(cmdpara))
  494. {
  495. strValue = cmdpara._rtnData;
  496. return true;
  497. }
  498. return false;
  499. }
  500. bool TCLCommand::GetClientType(std::string& strValue)
  501. {
  502. CommandParam cmdpara;
  503. GetCommandParams("GetClientType", cmdpara);
  504. PackingCommand(cmdpara);
  505. if (SendCommand(cmdpara))
  506. {
  507. strValue = cmdpara._rtnData;
  508. return true;
  509. }
  510. return false;
  511. }
  512. bool TCLCommand::GetMAC(std::string& strValue)
  513. {
  514. CommandParam cmdpara;
  515. GetCommandParams("GetMAC", cmdpara);
  516. PackingCommand(cmdpara);
  517. if (SendCommand(cmdpara))
  518. {
  519. strValue = cmdpara._rtnData;
  520. return true;
  521. }
  522. return false;
  523. }
  524. bool TCLCommand::GetHDCPKey(std::string& strValue)
  525. {
  526. CommandParam cmdpara;
  527. GetCommandParams("GetHDCPKey", cmdpara);
  528. PackingCommand(cmdpara);
  529. if (SendCommand(cmdpara))
  530. {
  531. strValue = cmdpara._rtnData;
  532. return true;
  533. }
  534. return false;
  535. }
  536. bool TCLCommand::GetHDCPKey22(std::string& strValue)
  537. {
  538. CommandParam cmdpara;
  539. GetCommandParams("GetHDCPKey22", cmdpara);
  540. PackingCommand(cmdpara);
  541. if (SendCommand(cmdpara))
  542. {
  543. strValue = cmdpara._rtnData;
  544. return true;
  545. }
  546. return false;
  547. }
  548. bool TCLCommand::GetWidi(std::string& strValue)
  549. {
  550. CommandParam cmdpara;
  551. GetCommandParams("GetWidi", cmdpara);
  552. PackingCommand(cmdpara);
  553. if (SendCommand(cmdpara))
  554. {
  555. strValue = cmdpara._rtnData;
  556. return true;
  557. }
  558. return false;
  559. }
  560. bool TCLCommand::GetNetflixESN(std::string& strValue)
  561. {
  562. CommandParam cmdpara;
  563. GetCommandParams("GetNetflixESN", cmdpara);
  564. PackingCommand(cmdpara);
  565. if (SendCommand(cmdpara))
  566. {
  567. strValue = cmdpara._rtnData;
  568. return true;
  569. }
  570. return false;
  571. }
  572. bool TCLCommand::GetWidevine(std::string& strValue)
  573. {
  574. CommandParam cmdpara;
  575. GetCommandParams("GetWidevine", cmdpara);
  576. PackingCommand(cmdpara);
  577. if (SendCommand(cmdpara))
  578. {
  579. strValue = cmdpara._rtnData;
  580. return true;
  581. }
  582. return false;
  583. }
  584. bool TCLCommand::GetCiKey(std::string& strValue)
  585. {
  586. CommandParam cmdpara;
  587. GetCommandParams("GetCiKey", cmdpara);
  588. PackingCommand(cmdpara);
  589. if (SendCommand(cmdpara))
  590. {
  591. strValue = cmdpara._rtnData;
  592. return true;
  593. }
  594. return false;
  595. }
  596. bool TCLCommand::GetOSDLanguage(std::string& strValue)
  597. {
  598. CommandParam cmdpara;
  599. GetCommandParams("GetOSDLanguage", cmdpara);
  600. PackingCommand(cmdpara);
  601. if (SendCommand(cmdpara))
  602. {
  603. strValue = cmdpara._rtnData;
  604. return true;
  605. }
  606. return false;
  607. }
  608. bool TCLCommand::GetShopLanguage(std::string& strValue)
  609. {
  610. CommandParam cmdpara;
  611. GetCommandParams("GetShopLanguage", cmdpara);
  612. PackingCommand(cmdpara);
  613. if (SendCommand(cmdpara))
  614. {
  615. strValue = cmdpara._rtnData;
  616. return true;
  617. }
  618. return false;
  619. }
  620. bool TCLCommand::GetChannel(std::string& strValue)
  621. {
  622. CommandParam cmdpara;
  623. GetCommandParams("GetChannel", cmdpara);
  624. PackingCommand(cmdpara);
  625. if (SendCommand(cmdpara))
  626. {
  627. strValue = cmdpara._rtnData;
  628. return true;
  629. }
  630. return false;
  631. }
  632. bool TCLCommand::SetProjectId(const byte* pBuffer, const int& nLen)
  633. {
  634. return false;
  635. }
  636. bool TCLCommand::SetDeviceId(LPCTSTR lpDeviceId)
  637. {
  638. return false;
  639. }
  640. bool TCLCommand::SetDeviceId(const byte* pBuffer, const int& nLen)
  641. {
  642. return false;
  643. }
  644. bool TCLCommand::SetMAC(LPCTSTR lpMac)
  645. {
  646. return false;
  647. }
  648. bool TCLCommand::SetMAC(const byte* pBuffer, const int& nLen)
  649. {
  650. return false;
  651. }
  652. bool TCLCommand::SetHDCPKey(LPCTSTR lpHDCP, bool bHasSpace)
  653. {
  654. return false;
  655. }
  656. bool TCLCommand::SetHDCPKey(const byte* pBuffer, const int& nLen)
  657. {
  658. return false;
  659. }
  660. bool TCLCommand::SetHDCPKey22(LPCTSTR lpHDCP22, bool bHasSpace)
  661. {
  662. return false;
  663. }
  664. bool TCLCommand::SetHDCPKey22(const byte* pBuffer, const int& nLen)
  665. {
  666. return false;
  667. }
  668. bool TCLCommand::SetNetflixESN(LPCTSTR lpESN, bool bHasSpace)
  669. {
  670. return false;
  671. }
  672. bool TCLCommand::SetNetflixESN(const byte* pBuffer, const int& nLen)
  673. {
  674. return false;
  675. }
  676. bool TCLCommand::SetWidi(LPCTSTR lpWidi, bool bHasSpace)
  677. {
  678. return false;
  679. }
  680. bool TCLCommand::SetWidi(const byte* pBuffer, const int& nLen)
  681. {
  682. return false;
  683. }
  684. bool TCLCommand::SetWidevine(LPCTSTR lpWidevine, bool bHasSpace)
  685. {
  686. return false;
  687. }
  688. bool TCLCommand::SetWidevine(const byte* pBuffer, const int& nLen)
  689. {
  690. return false;
  691. }
  692. bool TCLCommand::SetCiKey(LPCTSTR lpCiKey, bool bHasSpace)
  693. {
  694. return false;
  695. }
  696. bool TCLCommand::SetCiKey(const byte* pBuffer, const int& nLen)
  697. {
  698. return false;
  699. }
  700. bool TCLCommand::SetOSDLanguage(LPCTSTR lan, bool bHasSpace)
  701. {
  702. return false;
  703. }
  704. bool TCLCommand::SetOSDLanguage(const byte* pBuffer, const int& nLen)
  705. {
  706. return false;
  707. }
  708. bool TCLCommand::SetShopLanguage(LPCTSTR lan, bool bHasSpace)
  709. {
  710. return false;
  711. }
  712. bool TCLCommand::SetShopLanguage(const byte* pBuffer, const int& nLen)
  713. {
  714. return false;
  715. }
  716. bool TCLCommand::SetChannel(LPCTSTR channel, bool bHasSpace)
  717. {
  718. return false;
  719. }
  720. bool TCLCommand::SetChannel(const byte* pBuffer, const int& nLen)
  721. {
  722. return false;
  723. }
  724. bool TCLCommand::SetWBNormal(LPCTSTR data)
  725. {
  726. return false;
  727. }
  728. bool TCLCommand::SetWBNormal(const byte* pBuffer, const int& nLen)
  729. {
  730. return false;
  731. }
  732. bool TCLCommand::SetWBCool(LPCTSTR data)
  733. {
  734. return false;
  735. }
  736. bool TCLCommand::SetWBCool(const byte* pBuffer, const int& nLen)
  737. {
  738. return false;
  739. }
  740. bool TCLCommand::SetWBWarm(LPCTSTR data)
  741. {
  742. return false;
  743. }
  744. bool TCLCommand::SetWBWarm(const byte* pBuffer, const int& nLen)
  745. {
  746. return false;
  747. }
  748. bool TCLCommand::CheckDeviceId()
  749. {
  750. return false;
  751. }
  752. bool TCLCommand::CheckMAC()
  753. {
  754. return false;
  755. }
  756. bool TCLCommand::CheckHDCP()
  757. {
  758. return false;
  759. }
  760. bool TCLCommand::CheckHDCP22()
  761. {
  762. return false;
  763. }
  764. bool TCLCommand::CheckNetflixESN()
  765. {
  766. return false;
  767. }
  768. bool TCLCommand::CheckWidi()
  769. {
  770. return false;
  771. }
  772. bool TCLCommand::CheckWidevine()
  773. {
  774. return false;
  775. }
  776. bool TCLCommand::CheckCikey()
  777. {
  778. return false;
  779. }
  780. bool TCLCommand::StarWarmUpMode()
  781. {
  782. return false;
  783. }
  784. bool TCLCommand::StopWarmUpMode()
  785. {
  786. return false;
  787. }
  788. bool TCLCommand::SetProjectId(int pid)
  789. {
  790. return false;
  791. }
  792. bool TCLCommand::SetProjectId(LPCTSTR lpPid)
  793. {
  794. return false;
  795. }