12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195 |
- #include "pch.h"
- #include "OTA.h"
- #include "CharEncoding.h"
- #include "TCL Copy ToolDlg.h"
- std::vector<_SIACP_> g_vtSiacp = {
- {"EnterFactory", "AA", "10", "01", false, 100},
- {"LeaveFactory", "AA", "10", "00", false, 100},
- {"WBInit", "AA", "16", "02", false, 100},//白平衡数据初始化(适用于所有信源)
- {"GetProjectID", "AA", "84", "00", false, 100},
- {"GetSoftVersion", "AA", "57", "00", false, 100},
- {"GetDeviceId", "AA", "BE", "01", true, 100},
- {"GetClientType", "AA", "8C", "00", false, 100},
- {"GetMAC", "AA", "BE", "00", true, 100},
- {"GetHDCPKey", "AA", "EE", "00", false, 100},
- {"GetHDCPKey22", "AA", "E7", "00", false, 100},
- {"GetWidi", "AA", "E7", "00", false, 100},
- {"GetNetflixESN", "AA", "BE", "06", true, 100},
- {"GetWidevine", "AA", "EC", "00", true, 100},
- {"GetCiKey", "AA", "EC", "01", true, 100},
- {"GetOSDLanguage", "AA", "97", "00", true, 100},
- {"GetShopLanguage", "AA", "97", "01", true, 100},
- {"GetChannel", "AA", "97", "13", true, 100},
- // 设置;
- {"SetProjectId", "AA", "70", "", false, 100},
- {"SetDeviceID", "AA", "B2", "", false, 100},
- {"SetMAC", "AA", "B3", "", false, 100},
- {"SetHDCPKey", "AA FE", "86", "", false, 100},
- {"SetHDCPKey22", "AA FE", "E4", "", false, 100},
- {"SetNetflixESN", "AA", "99 00", "", false, 100},
- {"SetWidi", "AA FE", "E4", "", false, 100}, //与hdcp22相同指令
- {"SetWidevine", "AA FE", "E9 00", "", false, 100},
- {"SetCiKey", "AA FE", "E9 01", "", false, 100},
- {"SetOSDLanguage", "AA", "96 00", "", true, 100},
- {"SetShopLanguage", "AA", "96 01", "", true, 100},
- // 设置;
- {"SetChannel", "AA", "15", "", false, 100},
- {"SetWBNormal", "AA", "4D 04 01 01", "", false, 100},
- {"SetWBCool", "AA", "4D 04 02 01", "", false, 100},
- {"SetWBWarm", "AA", "4D 04 03 01", "", false, 100},
- // 检测;
- {"CheckMAC", "AA", "B4", "00", false, 100},
- {"CheckDeviceId", "AA", "B4", "00", false, 100},
- {"CheckHDCP", "AA", "87", "00", false, 100},
- {"CheckNetflixESN", "AA", "9A", "00", true, 100},
- {"CheckWidi", "AA", "E5", "00", false, 100},
- {"CheckWidevine", "AA", "EA", "00", true, 100},
- {"CheckCikey", "AA", "EA", "01", true, 100},
- {"CheckHDCP22", "AA", "E5", "00", false, 100},
- {"StarWarmUpMode", "AA", "13", "01", false, 100},
- {"StopWarmUpMode", "AA", "13", "00", false, 100}
- };
- const unsigned short CRC16_TABLE[16] = {
- 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
- 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF};
- unsigned short CRC16Calculate(byte *pBuffer, unsigned int wordLength)
- {
- unsigned short wordCRC;
- byte byteTemp;
- wordCRC = 0xFFFF;
- while (wordLength--)
- {
- byteTemp = (byte)(wordCRC >> 0x0C);
- wordCRC <<= 4;
- wordCRC ^= CRC16_TABLE[byteTemp ^ ((*pBuffer) >> 0x04)];
- byteTemp = (byte)(wordCRC >> 0x0C);
- wordCRC <<= 4;
- wordCRC ^= CRC16_TABLE[byteTemp ^ ((*pBuffer) & 0x0F)];
- pBuffer++;
- }
- return wordCRC;
- }
- unsigned char TwoHexChar2Char(char ch1, char ch2)
- {
- char Numb1;
- char Numb2;
- if (ch1 >= 'A')
- Numb1 = (toupper(ch1) - '0' - 7) * 16;
- else
- Numb1 = (ch1 - '0') * 16;
- if (ch2 >= 'A')
- Numb2 = (toupper(ch2) - '0' - 7);
- else
- Numb2 = (ch2 - '0');
- return (Numb1 + Numb2);
- }
- std::string HexString2Bytes(std::string strHex, const int &len /* = 3 */)
- {
- byte value = 0;
- std::string strBytes;
- int nSize = strHex.size();
- for (int i = 0; i < nSize; i += len)
- {
- strBytes.push_back(TwoHexChar2Char(strHex[i], strHex[i + 1]));
- }
- return strBytes;
- }
- std::string Bytes2HexString(const unsigned char *pbuffer, int nLen, char chSpace)
- {
- std::string hex;
- char szhex[5] = {0};
- for (int i = 0; i < nLen; i++)
- {
- memset(szhex, 0, 5);
- _stprintf_s(szhex, "%02X%c", pbuffer[i], chSpace);
- hex.append(szhex);
- }
- return hex.substr(0, hex.size() - 1);
- }
- std::string Bytes2HexString(const unsigned char *pbuffer, int nLen)
- {
- std::string hex;
- char szhex[5] = {0};
- for (int i = 0; i < nLen; i++)
- {
- memset(szhex, 0, 5);
- _stprintf_s(szhex, "%02X", pbuffer[i]);
- hex.append(szhex);
- }
- return hex;
- }
- BOOL IsValidString(LPCTSTR lpszString)
- {
- if (lpszString == NULL)
- return FALSE;
- do
- {
- // ASCII可显示的字符;
- if (*lpszString < 32 || *lpszString > 126)
- {
- return FALSE;
- }
- } while (*++lpszString);
- return TRUE;
- }
- // 去除空格;
- std::string &trim(std::string &str)
- {
- int nIndex = 0;
- while ((nIndex = str.find_first_of(' ')) != std::string::npos)
- str.erase(nIndex, 1);
- return str;
- }
- CSIACP::CSIACP(void) //:m_vtcommand(g_siacp, g_siacp+sizeof(g_siacp)/sizeof(_SIACP_))
- {
- m_pMainDlg = nullptr;
- m_pSerial = NULL;
- LoadCommand();
- }
- CSIACP::~CSIACP(void)
- {
- CloseComm();
- }
- void CSIACP::SetMainDlg(CDialogEx* p)
- {
- m_pMainDlg = p;
- }
- void CSIACP::ShowMessage(LPCTSTR lpMsg, LOG_ENUM logtype)
- {
- if (m_pMainDlg && lpMsg)
- {
- CTCLCopyToolDlg* pMainDlg = (CTCLCopyToolDlg*)m_pMainDlg;
- pMainDlg->SetOptionLog(std::string(lpMsg), logtype);
- }
- }
- void CSIACP::LoadCommand()
- {
- // 解析xml;
- tinyxml2::XMLDocument doc;
- if (tinyxml2::XML_SUCCESS != doc.LoadFile(_T("Siacp.xml")))
- {
- SaveCommand(g_vtSiacp, _T("Siacp.xml"));
- return;
- }
- std::string ver;
- std::string desc;
- g_vtSiacp.clear();
- tinyxml2::XMLElement *pXmlRoot = NULL;
- if ((pXmlRoot = doc.RootElement()) != NULL)
- {
- if (_tcsicmp(pXmlRoot->Value(), "CommandList") == 0)
- {
- // 属性;
- const tinyxml2::XMLAttribute *pAttr = pXmlRoot->FirstAttribute();
- while (pAttr)
- {
- if (_tcsicmp(pAttr->Name(), "ver") == 0)
- {
- ver = pAttr->Value();
- }
- if (_tcsicmp(pAttr->Name(), "desc") == 0)
- {
- desc = pAttr->Value();
- }
- pAttr = pAttr->Next();
- }
- // 子项;
- tinyxml2::XMLElement *pXmlElent = pXmlRoot->FirstChildElement();
- while (pXmlElent)
- {
- _SIACP_ siacp;
- if (_tcsicmp(pXmlElent->Value(), _T("Item")) == 0)
- {
- tinyxml2::XMLElement *pItem = pXmlElent->FirstChildElement();
- while (pItem)
- {
- if (_tcsicmp(pItem->Value(), _T("Name")) == 0)
- {
- siacp.name = pItem->GetText();
- }
- else if (_tcsicmp(pItem->Value(), _T("HeadCode")) == 0)
- {
- siacp.head = pItem->GetText();
- }
- else if (_tcsicmp(pItem->Value(), _T("ucCommand")) == 0)
- {
- siacp.code = pItem->GetText();
- }
- else if (_tcsicmp(pItem->Value(), _T("Mark")) == 0)
- {
- siacp.mark = pItem->GetText();
- }
- else if (_tcsicmp(pItem->Value(), _T("MultiParams")) == 0)
- {
- siacp.bMulticode = pItem->BoolText();
- }
- else if (_tcsicmp(pItem->Value(), _T("ReadWaitTime")) == 0)
- {
- siacp.read_wait_time = pItem->IntText();
- }
- else if (_tcsicmp(pItem->Value(), _T("CmdWaitTime")) == 0)
- {
- siacp.cmd_wait_time = pItem->IntText();
- }
- pItem = pItem->NextSiblingElement();
- }
- }
- g_vtSiacp.push_back(siacp);
- pXmlElent = pXmlElent->NextSiblingElement();
- }
- }
- }
- }
- void CSIACP::SaveCommand(std::vector<_SIACP_> &vtSiacp, std::string path)
- {
- tinyxml2::XMLDocument doc;
- //添加申明可以使用如下两行
- tinyxml2::XMLDeclaration *declaration = doc.NewDeclaration();
- doc.InsertFirstChild(declaration);
- tinyxml2::XMLElement *root = doc.NewElement("CommandList");
- root->SetAttribute("ver", "9.5");
- root->SetAttribute("desc", "command info");
- doc.InsertEndChild(root);
- std::vector<_SIACP_>::iterator it = vtSiacp.begin();
- for (; it != vtSiacp.end(); it++)
- {
- tinyxml2::XMLElement *pItem = doc.NewElement("Item");
- tinyxml2::XMLElement *pName = doc.NewElement("Name");
- pName->SetText(it->name.c_str());
- pItem->InsertEndChild(pName);
- tinyxml2::XMLElement *pHeadCode = doc.NewElement("HeadCode");
- pHeadCode->SetText(it->head.c_str());
- pItem->InsertEndChild(pHeadCode);
- tinyxml2::XMLElement *pucCommand = doc.NewElement("ucCommand");
- pucCommand->SetText(it->code.c_str());
- pItem->InsertEndChild(pucCommand);
- tinyxml2::XMLElement *pMark = doc.NewElement("Mark");
- pMark->SetText(it->mark.c_str());
- pItem->InsertEndChild(pMark);
- tinyxml2::XMLElement *pMultiParams = doc.NewElement("MultiParams");
- pMultiParams->SetText(it->bMulticode);
- pItem->InsertEndChild(pMultiParams);
- tinyxml2::XMLElement *pReadWaitTime = doc.NewElement("ReadWaitTime");
- pReadWaitTime->SetText(it->read_wait_time);
- pItem->InsertEndChild(pReadWaitTime);
- tinyxml2::XMLElement* pCmdWaitTime = doc.NewElement("CmdWaitTime");
- pCmdWaitTime->SetText(it->cmd_wait_time);
- pItem->InsertEndChild(pCmdWaitTime);
- root->InsertEndChild(pItem);
- }
- doc.SaveFile(path.c_str());
- }
- BOOL CSIACP::OpenComm(LPCTSTR lpszCom, DWORD dwBaudrate)
- {
- if (lpszCom == NULL || lpszCom[0] == '\0')
- {
- Global::WriteReportLog(1, "Parameter error", "(lpszCom) Invalid parameter!");
- return FALSE;
- }
- // 释放打开的;
- if (m_pSerial)
- delete m_pSerial;
- m_pSerial = NULL;
- m_pSerial = new CSerialPort();
- if (m_pSerial == NULL)
- {
- Global::WriteReportLog(1, "Memory error", "(m_pSerial) Fail to create object!");
- return FALSE;
- }
- m_pSerial->Open(lpszCom, dwBaudrate);
- if (m_pSerial->IsOpen() == FALSE)
- {
- delete m_pSerial;
- m_pSerial = NULL;
- Global::WriteReportLog(1, "Serial port error", "Open Serial Port Fail !");
- return FALSE;
- }
- // 超时值设置;
- COMMTIMEOUTS comTimeOut;
- comTimeOut.ReadIntervalTimeout = 50;
- comTimeOut.ReadTotalTimeoutConstant = 8000; // 读取所有字节总超时值;
- comTimeOut.ReadTotalTimeoutMultiplier = 100; // 读取1个字节超时值;
- comTimeOut.WriteTotalTimeoutConstant = 5000; // 写所有字节的总超时值;
- comTimeOut.WriteTotalTimeoutMultiplier = 100; // 写1个字节的超时值;
- m_pSerial->SetTimeouts(comTimeOut);
- return TRUE;
- }
- void CSIACP::CloseComm()
- {
- if (m_pSerial)
- delete m_pSerial;
- m_pSerial = NULL;
- }
- BOOL CSIACP::ExecSerialCommand(const _SIACP_ &siacp, std::string command)
- {
- if (m_pSerial == NULL || !m_pSerial->IsOpen())
- {
- Global::WriteReportLog(1, "Serial port error", "Serial Port un't Open!");
- return FALSE;
- }
- byte szRecive[BUFFER_LEN] = {0};
- DWORD dwWritten = m_pSerial->Write(command.c_str(), command.size());
- Global::WriteTextLog(_T("COM Write:%s"), Bytes2HexString((const byte *)command.c_str(), command.size(), ' ').c_str());
- if (dwWritten != command.size())
- {
- Global::WriteTextLog("COM Written Error");
- Global::WriteReportLog(1, "Serial Writing Error", "COM Written Error, Buffer=%s", Bytes2HexString((const byte*)command.c_str(), command.size(), ' ').c_str());
- return FALSE;
- }
- // 暂停时间;// 等待设备响应完成;
- Sleep(siacp.read_wait_time);
- DWORD dwBytesRead = m_pSerial->Read(szRecive, BUFFER_LEN);
- if (dwBytesRead == 0)
- {
- Global::WriteTextLog("COM Read Error");
- Global::WriteReportLog(1, "Serial Reading Error","COM Read Error(No Data Return), Write=%s!", Bytes2HexString((const byte*)command.c_str(), command.size(), ' ').c_str());
- return FALSE;
- }
- Global::WriteTextLog(_T("COM Read:%s\r"), Bytes2HexString(szRecive, dwBytesRead, ' ').c_str());
- // 下一条命令等待时间;
- Sleep(siacp.cmd_wait_time);
- // 解析数据;
- return ParserSerialData(m_vtdata, siacp, szRecive, dwBytesRead);
- }
- BOOL CSIACP::ParserSerialData(std::vector<RTN> &vtdata, const _SIACP_ &siacp, byte *pbuffer, DWORD dwlen)
- {
- vtdata.clear();
- if (pbuffer == NULL || dwlen < 5)
- {
- Global::WriteReportLog(1, "Serial Parsing error", "cmd=%s,Return length less than 5! Buffer = %s", siacp.name.c_str(), Bytes2HexString(pbuffer, dwlen, ' ').c_str());
- return FALSE;
- }
- // 返回码;
- byte byReturn = TV_Return;
- byte byHead = (byte)HexString2Bytes(siacp.head)[0];
- byte byCode = (byte)HexString2Bytes(siacp.code)[0];
- if (byHead == TV_Debug)
- {
- byReturn = TV_Return;
- }
- else if (byHead == TV_Panel_Debug)
- {
- byReturn = TV_Panel_Return;
- }
- else if (byHead == TV_Debug_Other)
- {
- byReturn = TV_Other_Return;
- }
- else
- {
- Global::WriteTextLog("COM Read Parser: Invalid return code!");
- Global::WriteReportLog(1, "Serial Parsing error", "cmd=%s,Invalid return code! Buffer=%s", siacp.name.c_str(), Bytes2HexString(pbuffer, dwlen, ' ').c_str());
- return FALSE;
- }
- std::string data;
- data.append((const char *)pbuffer, dwlen);
- int nABFE = 0;
- std::string package;
- int package_len = 0;
- // 已取出的长度;
- int nTookenLen = 0;
- unsigned short usCRCValue;
- while (true)
- {
- // 是否已取完所有数据;
- if (nTookenLen >= dwlen)
- break;
- // 取出长度;
- if ((byte)data.at(nTookenLen + 1) == 0xFE)
- {
- nABFE = 2; //FE占1字节,长度多占1字节;共多占2字节;
- package_len = ((unsigned int)data[nTookenLen + 2] << 8) | (byte)data[nTookenLen + 3];
- }
- else
- {
- nABFE = 0;
- package_len = data[nTookenLen + 1];
- }
- // 包长度是否在有效长度内;
- if (package_len > (dwlen - nTookenLen))
- {
- Global::WriteTextLog("COM Read Parser: Incorrect length of returned data!");
- Global::WriteReportLog(1, "Serial Parsing error", "cmd=%s,Incorrect length of returned data! Buffer=%s", siacp.name.c_str(), Bytes2HexString(pbuffer, dwlen, ' ').c_str());
- return FALSE;
- }
- // 取出数据包;
- package = data.substr(nTookenLen, nTookenLen + package_len);
- // 校验包头是否正确;
- if (byte(package[0]) != byReturn)
- {
- Global::WriteTextLog("COM Read Parser: Data Header Check Error!");
- Global::WriteReportLog(1, "Serial Parsing error", "cmd=%s,Data Header Check Error! Buffer=%s", siacp.name.c_str(), Bytes2HexString(pbuffer, dwlen, ' ').c_str());
- return FALSE;
- }
- // 校验crc;
- usCRCValue = CRC16Calculate((unsigned char *)package.data(), package_len - 2);
- if (((usCRCValue >> 8) & 0xFF) != (unsigned char)package[package_len - 2] || (usCRCValue & 0xFF) != (unsigned char)package[package_len - 1])
- {
- Global::WriteTextLog("COM Read Parser: Data CRC validation failure!");
- Global::WriteReportLog(1, "Serial Parsing error", "cmd=%s,Data CRC validation failure! Buffer=%s", siacp.name.c_str(), Bytes2HexString(pbuffer, dwlen, ' ').c_str());
- return FALSE;
- }
- // 第一组返回,检测结果码
- if (nTookenLen == 0)
- {
- if (package[2] != RC_OK)
- {
- Global::WriteTextLog("COM Read Parser: The return code of the response data is invalid!");
- Global::WriteReportLog(1, "Serial Parsing error", "cmd=%s,The return code of the response data is invalid! Buffer=%s", siacp.name.c_str(), Bytes2HexString(pbuffer, dwlen, ' ').c_str());
- return FALSE;
- }
- }
- else
- {
- // 响应数据的命令码 = 发送数据的命令码 - 1;
- if (byte(package[2 + nABFE] - 1) != byCode)
- {
- Global::WriteTextLog("COM Read Parser: The command code of response data does not match the command code of sending data!");
- Global::WriteReportLog(1, "Serial Parsing error", "cmd=%s,The command code of response data does not match the command code of sending data! Buffer=%s", siacp.name.c_str(), Bytes2HexString(pbuffer, dwlen, ' ').c_str());
- return FALSE;
- }
- RTN rtn;
- // 获取结果值;
- if (siacp.bMulticode)
- {
- rtn.len = package_len - 6 - nABFE;
- rtn.data = package.substr(4 + nABFE, package_len - 6 - nABFE);
- }
- else
- {
- rtn.len = package_len - 5 - nABFE;
- rtn.data = package.substr(3 + nABFE, package_len - 5 - nABFE);
- }
- vtdata.push_back(rtn);
- }
- nTookenLen += package_len;
- }
- return TRUE;
- }
- const _SIACP_ *CSIACP::GetSiacp(std::string name)
- {
- std::vector<_SIACP_>::iterator it = g_vtSiacp.begin();
- for (; it != g_vtSiacp.end(); it++)
- {
- if (_tcsicmp(it->name.c_str(), name.c_str()) == 0)
- {
- return &*it;
- }
- }
- return NULL;
- }
- std::string CSIACP::GetSiacpCommand(const _SIACP_ &siacp, std::string others, int othersLen)
- {
- std::string command;
- command.append(HexString2Bytes(siacp.head).c_str(), (siacp.head.size() + 1) / 3);
- command.append(HexString2Bytes(siacp.code).c_str(), (siacp.code.size() + 1) / 3);
- command.append(HexString2Bytes(siacp.mark).c_str(), (siacp.mark.size() + 1) / 3);
- command.append(others.c_str(), othersLen);
- // 长度;
- byte szlen[2] = {0};
- int len = command.size() + 3; // 3=长度本身占1位+2位crc;
- if ((byte)command[1] == 0xFE)
- {
- szlen[0] = ((len + 1) >> 8) & 0xFF;
- szlen[1] = (len + 1) & 0xFF;
- command.insert(2, (char *)szlen, 2);
- }
- else
- {
- szlen[0] = len & 0xFF;
- command.insert(1, (char *)szlen, 1);
- }
- // crc校验;
- byte szcrc[2] = {0};
- WORD usCRCValue = CRC16Calculate((byte *)command.c_str(), command.size());
- szcrc[0] = (usCRCValue >> 8) & 0xFF;
- szcrc[1] = usCRCValue & 0xFF;
- command.append((char *)szcrc, 2);
- return command;
- }
- std::string CSIACP::GetSiacpCommand(std::string name, std::string others, int othersLen)
- {
- std::string command;
- std::vector<_SIACP_>::iterator it = g_vtSiacp.begin();
- for (; it != g_vtSiacp.end(); it++)
- {
- if (_tcsicmp(name.c_str(), it->name.c_str()) == 0)
- {
- command.append(HexString2Bytes(it->head).c_str(), (it->head.size() + 1) / 3);
- command.append(HexString2Bytes(it->code).c_str(), (it->code.size() + 1) / 3);
- command.append(HexString2Bytes(it->mark).c_str(), (it->mark.size() + 1) / 3);
- command.append(others.c_str(), othersLen);
- // 长度;
- byte szlen[2] = {0};
- int len = command.size() + 3; // 长度本身占1位;
- if ((byte)command[1] == 0xFE)
- {
- szlen[0] = ((len + 1) >> 8) & 0xFF;
- szlen[1] = (len + 1) & 0xFF;
- command.insert(2, (char *)szlen, 2);
- }
- else
- {
- szlen[0] = len & 0xFF;
- command.insert(1, (char *)szlen, 1);
- }
- // crc校验;
- byte szcrc[2] = {0};
- int usCRCValue = CRC16Calculate((byte *)command.c_str(), command.size());
- szcrc[0] = (usCRCValue >> 8) & 0xFF;
- szcrc[1] = usCRCValue & 0xFF;
- command.append((char *)szcrc, 2);
- break;
- }
- }
- return command;
- }
- BOOL CSIACP::SendCommand(std::string cmd_name, const byte *pszOthers, DWORD dwOthersLen)
- {
- std::string others;
- if (pszOthers && dwOthersLen > 0)
- {
- others.append((char *)pszOthers, dwOthersLen);
- }
- // 获取命令协议;
- const _SIACP_ *pSiacp = GetSiacp(cmd_name);
- if (pSiacp == NULL)
- {
- Global::WriteTextLog("This command is not supported!");
- Global::WriteReportLog(1, "Invalid command", "This command(%s) is not supported!", cmd_name.c_str());
- return FALSE;
- }
- std::string command = GetSiacpCommand(*pSiacp, others, dwOthersLen);
- if (command.size() == 0)
- {
- Global::WriteTextLog("Generation command failed!");
- Global::WriteReportLog(1, "Generation command failed", "command=%s", cmd_name.c_str());
- return FALSE;
- }
- return ExecSerialCommand(*pSiacp, command);
- }
- BOOL CSIACP::SendCommand2(std::string cmd_name, std::string data /* = "" */, size_t nlen /* = 0 */)
- {
- // 获取命令协议;
- const _SIACP_ *pSiacp = GetSiacp(cmd_name);
- if (pSiacp == NULL)
- {
- Global::WriteTextLog("This command is not supported!");
- Global::WriteReportLog(1, "Invalid command", "This command(%s) is not supported!", cmd_name.c_str());
- return FALSE;
- }
- std::string command = GetSiacpCommand(*pSiacp, data, nlen);
- if (command.size() == 0)
- {
- Global::WriteTextLog("Generation command failed!");
- Global::WriteReportLog(1, "Generation command failed", "command=%s", cmd_name.c_str());
- return FALSE;
- }
- return ExecSerialCommand(*pSiacp, command);
- }
- BOOL CSIACP::SCBC_WaitTVBoot()
- {
- byte szRecive[BUFFER_LEN] = { 0 };
- DWORD dwBytesRead = 0;
- // 根据配置文件,是否需要读等待;
- if ( Global::g_tConfig.bootread )
- dwBytesRead = m_pSerial->Read(szRecive, BUFFER_LEN);
- if (dwBytesRead == 0)
- {
- if ( SCBC_EnterFactory() )
- {
- SCBC_LeaveFactory();
- return TRUE;
- }
- }
- return FALSE;
- }
- BOOL CSIACP::SCBC_MTKInit()
- {
- if (m_pSerial == NULL)
- {
- Global::WriteTextLog("串口对象未初始化");
- Global::WriteReportLog(1, "MTKInit Error", "Serial port object uninitialized!");
- return FALSE;
- }
- if (!m_pSerial->IsOpen())
- {
- Global::WriteTextLog("未打开串口");
- Global::WriteReportLog(1, "MTKInit Error", "Unopened Serial Port!");
- return FALSE;
- }
- unsigned char ucBufInitMTK8223[16] = {0x54, 0x8A, 0x51, 0x37, 0x56, 0x92, 0x1C, 0xB2, 0x50, 0x80, 0x50, 0x02, 0x50, 0x01, 0x50, 0xE2};
- m_pSerial->Write(ucBufInitMTK8223, 16);
- Sleep(50);
- return TRUE;
- }
- // 对外接口;
- BOOL CSIACP::SCBC_EnterFactory()
- {
- return SendCommand("EnterFactory");
- }
- BOOL CSIACP::SCBC_LeaveFactory()
- {
- return SendCommand("LeaveFactory");
- }
- BOOL CSIACP::SCBC_WBInit()
- {
- return SendCommand("WBInit");
- }
- BOOL CSIACP::SCBC_GetProjectId(int &pid)
- {
- BOOL bRet = SendCommand("GetProjectId");
- if (bRet)
- {
- if (m_vtdata.size() == 0)
- {
- Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
- Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
- return FALSE;
- }
- RTN rtn = m_vtdata.at(0);
- if (rtn.len == 2)
- {
- pid = (byte)rtn.data[0] << 8;
- pid += (byte)rtn.data[1];
- }
- else
- {
- pid = (byte)rtn.data[0];
- }
- }
- return bRet;
- }
- BOOL CSIACP::SCBC_GetSoftVersion(std::string &strVer)
- {
- BOOL bRet = SendCommand("GetSoftVersion");
- if (bRet)
- {
- if (m_vtdata.size() == 0)
- {
- Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
- Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
- return FALSE;
- }
- RTN rtn = m_vtdata.at(0);
- if (!IsValidString(rtn.data.c_str()))
- {
- Global::WriteTextLog("%s:%s%s!", __FUNCTION__, "tv version string is invalid=", rtn.data.c_str());
- Global::WriteReportLog(1, "Copy Key", "%s:%s%s!", __FUNCTION__, "tv version string is invalid=", rtn.data.c_str());
- return FALSE;
- }
- strVer.append(rtn.data.c_str(), rtn.len);
- }
- return bRet;
- }
- BOOL CSIACP::SCBC_GetDeviceId(std::string &strDid)
- {
- BOOL bRet = SendCommand("GetDeviceId");
- if (bRet)
- {
- if (m_vtdata.size() == 0)
- {
- Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
- Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
- return FALSE;
- }
- RTN rtn = m_vtdata.at(0);
- strDid.append(rtn.data.c_str(), rtn.len);
- }
- return bRet;
- }
- BOOL CSIACP::SCBC_GetClientType(std::string &strClt)
- {
- BOOL bRet = SendCommand("GetClientType");
- if (bRet)
- {
- if (m_vtdata.size() == 0)
- {
- Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
- Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
- return FALSE;
- }
- RTN rtn = m_vtdata.at(0);
- if (!IsValidString(rtn.data.c_str()))
- {
- Global::WriteTextLog("%s:%s%s!", __FUNCTION__, "tv client string is invalid=", rtn.data.c_str());
- Global::WriteReportLog(1, "Copy Key", "%s:%s%s!", __FUNCTION__, "tv client string is invalid=", rtn.data.c_str());
- return FALSE;
- }
- strClt.append(rtn.data.c_str(), rtn.len);
- }
- return bRet;
- }
- BOOL CSIACP::SCBC_GetMAC(std::string &strMac)
- {
- BOOL bRet = SendCommand("GetMAC");
- if (bRet)
- {
- if (m_vtdata.size() == 0)
- {
- Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
- Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
- return FALSE;
- }
- RTN rtn = m_vtdata.at(0);
- strMac = Bytes2HexString((const byte *)rtn.data.c_str(), rtn.len, '-');
- }
- return bRet;
- }
- BOOL CSIACP::SCBC_GetHDCPKey(std::string &strHDCP)
- {
- BOOL bRet = SendCommand("GetHDCPKey");
- if (bRet)
- {
- if (m_vtdata.size() == 0)
- {
- Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
- Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
- return FALSE;
- }
- RTN rtn = m_vtdata.at(0);
- //strHDCP.append(rtn.data.c_str(), rtn.len);
- strHDCP = Bytes2HexString((const byte *)rtn.data.c_str(), rtn.len);
- }
- return bRet;
- }
- BOOL CSIACP::SCBC_GetHDCPKey22(std::string &strHDCP22)
- {
- BOOL bRet = SendCommand("GetHDCPKey22");
- if (bRet)
- {
- if (m_vtdata.size() == 0)
- {
- Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
- Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
- return FALSE;
- }
- RTN rtn = m_vtdata.at(0);
- //strHDCP22.append(rtn.data.c_str(), rtn.len);
- strHDCP22 = Bytes2HexString((const byte *)rtn.data.c_str(), rtn.len);
- }
- return bRet;
- }
- BOOL CSIACP::SCBC_GetWidi(std::string &strWidi)
- {
- BOOL bRet = SendCommand("GetWidi");
- if (bRet)
- {
- if (m_vtdata.size() == 0)
- {
- Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
- Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
- return FALSE;
- }
- RTN rtn = m_vtdata.at(0);
- //strWidi.append(rtn.data.c_str(), rtn.len);
- strWidi = Bytes2HexString((const byte *)rtn.data.c_str(), rtn.len);
- }
- return bRet;
- }
- BOOL CSIACP::SCBC_GetNetflixESN(std::string &strESN)
- {
- BOOL bRet = SendCommand("GetNetflixESN");
- if (bRet)
- {
- if (m_vtdata.size() == 0)
- {
- Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
- Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
- return FALSE;
- }
- RTN rtn = m_vtdata.at(0);
- strESN.append(rtn.data.c_str(), rtn.len);
- //strESN = Bytes2HexString((const byte*)rtn.data.c_str(), rtn.len, ' ');
- }
- return bRet;
- }
- BOOL CSIACP::SCBC_GetWidevine(std::string &strWidevine)
- {
- BOOL bRet = SendCommand("GetWidevine");
- if (bRet)
- {
- if (m_vtdata.size() == 0)
- {
- Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
- Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
- return FALSE;
- }
- RTN rtn = m_vtdata.at(0);
- //strWidevine.append(rtn.data.c_str(), rtn.len);
- strWidevine = Bytes2HexString((const byte *)rtn.data.c_str(), rtn.len);
- }
- return bRet;
- }
- BOOL CSIACP::SCBC_GetCiKey(std::string &strCikey)
- {
- BOOL bRet = SendCommand("GetCiKey");
- if (bRet)
- {
- if (m_vtdata.size() == 0)
- {
- Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
- Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
- return FALSE;
- }
- RTN rtn = m_vtdata.at(0);
- //strCikey.append(rtn.data.c_str(), rtn.len);
- strCikey = Bytes2HexString((const byte *)rtn.data.c_str(), rtn.len);
- }
- return bRet;
- }
- BOOL CSIACP::SCBC_GetOSDLanguage(std::string &strOSDLan)
- {
- BOOL bRet = SendCommand("GetOSDLanguage");
- if (bRet)
- {
- if (m_vtdata.size() == 0)
- {
- Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
- Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
- return FALSE;
- }
- RTN rtn = m_vtdata.at(0);
- strOSDLan.append(rtn.data.c_str(), rtn.len);
- strOSDLan = Bytes2HexString((const byte*)rtn.data.c_str(), rtn.len, ' ');
- }
- return bRet;
- }
- BOOL CSIACP::SCBC_GetShopLanguage(std::string &strShopLan)
- {
- BOOL bRet = SendCommand("GetShopLanguage");
- if (bRet)
- {
- if (m_vtdata.size() == 0)
- {
- Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
- Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
- return FALSE;
- }
- RTN rtn = m_vtdata.at(0);
- strShopLan.append(rtn.data.c_str(), rtn.len);
- strShopLan = Bytes2HexString((const byte*)rtn.data.c_str(), rtn.len, ' ');
- }
- return bRet;
- }
- BOOL CSIACP::SCBC_GetChannel(std::string& channel)
- {
- BOOL bRet = SendCommand("GetChannel");
- if (bRet)
- {
- if (m_vtdata.size() == 0)
- {
- Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
- Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
- return FALSE;
- }
- RTN rtn = m_vtdata.at(0);
- channel.append(rtn.data.c_str(), rtn.len);
- channel = Bytes2HexString((const byte*)rtn.data.c_str(), rtn.len, ' ');
- }
- return bRet;
- }
- BOOL CSIACP::SCBC_SetProjectId(const int &pid)
- {
- std::string data;
- if (pid > 0xFF)
- {
- data.append(1, (pid >> 8) & 0xFF);
- data.append(1, pid & 0xFF);
- }
- else
- {
- data.append(1, pid & 0xFF);
- }
- return SendCommand2("SetProjectId", data, data.size());
- }
- BOOL CSIACP::SCBC_SetProjectId(std::string pid)
- {
- return SCBC_SetProjectId(atoi(pid.c_str()));
- }
- BOOL CSIACP::SCBC_SetProjectId(const byte *pBuffer, const int &nLen)
- {
- return SendCommand("SetProjectId", pBuffer, nLen);
- }
- BOOL CSIACP::SCBC_SetDeviceId(std::string strDeviceId)
- {
- if (strDeviceId.size() < 40)
- {
- return FALSE;
- }
- return SendCommand2("SetDeviceId", strDeviceId, strDeviceId.size());
- }
- BOOL CSIACP::SCBC_SetDeviceId(const byte *pBuffer, const int &nLen)
- {
- return SendCommand("SetDeviceId", pBuffer, nLen);
- }
- BOOL CSIACP::SCBC_SetMAC(std::string strMac)
- {
- if (strMac.size() < 16)
- return FALSE;
- std::string data = HexString2Bytes(strMac);
- return SendCommand2("SetMAC", data, (strMac.size() + 1) / 3);
- }
- BOOL CSIACP::SCBC_SetMAC(const byte *pBuffer, const int &nLen)
- {
- return SendCommand("SetMAC", pBuffer, nLen);
- }
- BOOL CSIACP::SCBC_SetHDCPKey(std::string strHDCP, BOOL bHasSpace /* = TRUE */)
- {
- std::string data = HexString2Bytes(strHDCP, bHasSpace ? 3 : 2);
- return SendCommand2("SetHDCPKey", data, (strHDCP.size() + 1) / (bHasSpace ? 3 : 2));
- }
- BOOL CSIACP::SCBC_SetHDCPKey(const byte *pBuffer, const int &nLen)
- {
- return SendCommand("SetHDCPKey", pBuffer, nLen);
- }
- BOOL CSIACP::SCBC_SetHDCPKey22(std::string strHDCP22, BOOL bHasSpace /* = TRUE */)
- {
- std::string data = HexString2Bytes(strHDCP22, bHasSpace ? 3 : 2);
- return SendCommand2("SetHDCPKey22", data, (strHDCP22.size() + 1) / (bHasSpace ? 3 : 2));
- }
- BOOL CSIACP::SCBC_SetHDCPKey22(const byte *pBuffer, const int &nLen)
- {
- return SendCommand("SetHDCPKey22", pBuffer, nLen);
- }
- BOOL CSIACP::SCBC_SetNetflixESN(std::string strESN, BOOL bHasSpace /* = TRUE */)
- {
- std::string data = HexString2Bytes(strESN, bHasSpace ? 3 : 2);
- return SendCommand2("SetNetflixESN", data, (strESN.size() + 1) / (bHasSpace ? 3 : 2));
- }
- BOOL CSIACP::SCBC_SetNetflixESN(const byte *pBuffer, const int &nLen)
- {
- return SendCommand("SetNetflixESN", pBuffer, nLen);
- }
- BOOL CSIACP::SCBC_SetWidi(std::string strWidi, BOOL bHasSpace /* = TRUE */)
- {
- std::string data = HexString2Bytes(strWidi, bHasSpace ? 3 : 2);
- return SendCommand2("SetWidi", data, (strWidi.size() + 1) / (bHasSpace ? 3 : 2));
- }
- BOOL CSIACP::SCBC_SetWidi(const byte *pBuffer, const int &nLen)
- {
- return SendCommand("SetWidi", pBuffer, nLen);
- }
- BOOL CSIACP::SCBC_SetWidevine(std::string strWidevine, BOOL bHasSpace /* = TRUE */)
- {
- std::string data = HexString2Bytes(strWidevine, bHasSpace ? 3 : 2);
- return SendCommand2("SetWidevine", data, (strWidevine.size() + 1) / (bHasSpace ? 3 : 2));
- }
- BOOL CSIACP::SCBC_SetWidevine(const byte *pBuffer, const int &nLen)
- {
- return SendCommand("SetWidevine", pBuffer, nLen);
- }
- BOOL CSIACP::SCBC_SetCiKey(std::string strCiKey, BOOL bHasSpace /* = TRUE */)
- {
- std::string data = HexString2Bytes(strCiKey, bHasSpace ? 3 : 2);
- return SendCommand2("SetCiKey", data, (strCiKey.size() + 1) / (bHasSpace ? 3 : 2));
- }
- BOOL CSIACP::SCBC_SetCiKey(const byte *pBuffer, const int &nLen)
- {
- return SendCommand("SetCiKey", pBuffer, nLen);
- }
- BOOL CSIACP::SCBC_SetOSDLanguage(std::string lan, BOOL bHasSpace /* = TRUE */)
- {
- std::string data = HexString2Bytes(lan, bHasSpace ? 3 : 2);
- return SendCommand2("SetOSDLanguage", data, data.size());
- }
- BOOL CSIACP::SCBC_SetOSDLanguage(const byte *pBuffer, const int &nLen)
- {
- return SendCommand("SetOSDLanguage", pBuffer, nLen);
- }
- BOOL CSIACP::SCBC_SetShopLanguage(std::string lan, BOOL bHasSpace /* = TRUE */)
- {
- std::string data = HexString2Bytes(lan, bHasSpace ? 3 : 2);
- return SendCommand2("SetShopLanguage", data, data.size());
- }
- BOOL CSIACP::SCBC_SetShopLanguage(const byte *pBuffer, const int &nLen)
- {
- return SendCommand("SetShopLanguage", pBuffer, nLen);
- }
- BOOL CSIACP::SCBC_SetChannel(std::string channel, BOOL bHasSpace /* = TRUE */)
- {
- std::string data = HexString2Bytes(channel, bHasSpace ? 3 : 2);
- return SendCommand2("SetChannel", data, data.size());
- }
- BOOL CSIACP::SCBC_SetChannel(const byte *pBuffer, const int &nLen)
- {
- return SendCommand("SetChannel", pBuffer, nLen);
- }
- BOOL CSIACP::SCBC_SetWBNormal(std::string data)
- {
- return SendCommand2("SetWBNormal", data, data.size());
- }
- BOOL CSIACP::SCBC_SetWBNormal(const byte *pBuffer, const int &nLen)
- {
- return SendCommand("SetWBNormal", pBuffer, nLen);
- }
- BOOL CSIACP::SCBC_SetWBCool(std::string data)
- {
- return SendCommand2("SetWBCool", data, data.size());
- }
- BOOL CSIACP::SCBC_SetWBCool(const byte *pBuffer, const int &nLen)
- {
- return SendCommand("SetWBCool", pBuffer, nLen);
- }
- BOOL CSIACP::SCBC_SetWBWarm(std::string data)
- {
- return SendCommand2("SetWBWarm", data, data.size());
- }
- BOOL CSIACP::SCBC_SetWBWarm(const byte *pBuffer, const int &nLen)
- {
- return SendCommand("SetWBWarm", pBuffer, nLen);
- }
- BOOL CSIACP::SCBC_CheckDeviceId()
- {
- BOOL bRet = SendCommand("CheckDeviceId");
- if (bRet)
- {
- if (m_vtdata.size() == 0)
- {
- Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
- Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
- return FALSE;
- }
- RTN rtn = m_vtdata.at(0);
- if (rtn.data.size() == 0)
- {
- return FALSE;
- }
- byte bydata = (byte)rtn.data.at(0);
- #if TEMP_5659
- if (bydata == 0x03 || bydata == 0x02 || bydata == 0x01)
- return TRUE;
- else
- return FALSE;
- #else
- if (bydata == 0x02 || bydata == 0x03 || bydata == 0x07)
- return TRUE;
- else
- return FALSE;
- #endif
- }
- return bRet;
- }
- BOOL CSIACP::SCBC_CheckMAC()
- {
- BOOL bRet = SendCommand("CheckMAC");
- if (bRet)
- {
- if (m_vtdata.size() == 0)
- {
- Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
- Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
- return FALSE;
- }
- RTN rtn = m_vtdata.at(0);
- if (rtn.data.size() == 0)
- {
- return FALSE;
- }
- byte bydata = (byte)rtn.data.at(0);
- if (bydata == 0x01 || bydata == 0x03 || bydata == 0x07)
- return TRUE;
- else
- return FALSE;
- }
- return bRet;
- }
- BOOL CSIACP::SCBC_CheckHDCP()
- {
- BOOL bRet = SendCommand("CheckHDCP");
- if (bRet)
- {
- if (m_vtdata.size() == 0)
- {
- Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
- Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
- return FALSE;
- }
- RTN rtn = m_vtdata.at(0);
- if (rtn.data.size() == 0)
- {
- return FALSE;
- }
- byte bydata = (byte)rtn.data.at(0);
- if (bydata == 0x01)
- return TRUE;
- else
- return FALSE;
- }
- return bRet;
- }
- BOOL CSIACP::SCBC_CheckHDCP22()
- {
- BOOL bRet = SendCommand("CheckHDCP22");
- if (bRet)
- {
- if (m_vtdata.size() == 0)
- {
- Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
- Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
- return FALSE;
- }
- RTN rtn = m_vtdata.at(0);
- if (rtn.data.size() == 0)
- {
- return FALSE;
- }
- byte bydata = (byte)rtn.data.at(0);
- if (bydata == 0x01)
- return TRUE;
- else
- return FALSE;
- }
- return bRet;
- }
- BOOL CSIACP::SCBC_CheckNetflixESN()
- {
- BOOL bRet = SendCommand("CheckNetflixESN");
- if (bRet)
- {
- if (m_vtdata.size() == 0)
- {
- Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
- Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
- return FALSE;
- }
- RTN rtn = m_vtdata.at(0);
- if (rtn.data.size() == 0)
- {
- return FALSE;
- }
- byte bydata = (byte)rtn.data.at(0);
- if (bydata == 0x01)
- return TRUE;
- else
- return FALSE;
- }
- return bRet;
- }
- BOOL CSIACP::SCBC_CheckWidi()
- {
- BOOL bRet = SendCommand("CheckWidi");
- if (bRet)
- {
- if (m_vtdata.size() == 0)
- {
- Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
- Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
- return FALSE;
- }
- RTN rtn = m_vtdata.at(0);
- if (rtn.data.size() == 0)
- {
- return FALSE;
- }
- byte bydata = (byte)rtn.data.at(0);
- if (bydata == 0x01)
- return TRUE;
- else
- return FALSE;
- }
- return bRet;
- }
- BOOL CSIACP::SCBC_CheckWidevine()
- {
- BOOL bRet = SendCommand("CheckWidevine");
- if (bRet)
- {
- if (m_vtdata.size() == 0)
- {
- Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
- Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
- return FALSE;
- }
- RTN rtn = m_vtdata.at(0);
- if (rtn.data.size() == 0)
- {
- return FALSE;
- }
- byte bydata = (byte)rtn.data.at(0);
- if (bydata == 0x01)
- return TRUE;
- else
- return FALSE;
- }
- return bRet;
- }
- BOOL CSIACP::SCBC_CheckCikey()
- {
- BOOL bRet = SendCommand("CheckCikey");
- if (bRet)
- {
- if (m_vtdata.size() == 0)
- {
- Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
- Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
- return FALSE;
- }
- RTN rtn = m_vtdata.at(0);
- if (rtn.data.size() == 0)
- {
- return FALSE;
- }
- byte bydata = (byte)rtn.data.at(0);
- if (bydata == 0x01)
- return TRUE;
- else
- return FALSE;
- }
- return bRet;
- }
- BOOL CSIACP::SCBC_StarWarmUpMode()
- {
- return SendCommand("StarWarmUpMode");
- }
- BOOL CSIACP::SCBC_StopWarmUpMode()
- {
- return SendCommand("StopWarmUpMode");
- }
- //////////////////////////////////////////////////////////////////////////
- COTA::COTA()
- {
- m_pMainDlg = nullptr;
- m_curl.Initialize();
- }
- COTA::~COTA()
- {
- }
- void COTA::SetMainDlg(CDialogEx* p)
- {
- m_pMainDlg = p;
- }
- void COTA::ShowMessage(LPCTSTR lpMsg, LOG_ENUM logtype)
- {
- if (m_pMainDlg && lpMsg)
- {
- CTCLCopyToolDlg* pMainDlg = (CTCLCopyToolDlg*)m_pMainDlg;
- pMainDlg->SetOptionLog(std::string(lpMsg), logtype);
- }
- }
- BOOL COTA::HttpGet(std::string host, std::string context, std::string &result, DATATYPE dt)
- {
- AUTOTIMING;
- //AutoTiming at(host+";"+context);
- CharEncoding::ASCII2UTF8(context.c_str(), context);
- m_curl.SetHeaders("Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*");
- m_curl.SetHeaders("Accept-Language: zh-cn");
- if (dt == DATA_JSON)
- m_curl.SetHeaders("Content-Type: application/json;charset=utf-8");
- int nRecCode = CURLE_OK;
- if (_tcsstr(host.c_str(), "https://"))
- nRecCode = m_curl.Gets(host + context, result);
- else
- nRecCode = m_curl.Get(host + context, result);
- if (CURLE_OK != nRecCode)
- {
- TCHAR szMsg[250] = { 0 };
- _stprintf_s(szMsg, _T("http(get) request failed, curl error code = %d\r"), nRecCode);
- ShowMessage(szMsg);
- LOG4C((LOG_NOTICE, "请求失败:%s, 错误码=%ld", context.c_str(), nRecCode));
- Global::WriteReportLog(0, __FUNCTION__, "Request(get) Failure, Curl Error Code=%ld: host=%s, content=%s, result=%s", nRecCode, host.c_str(), context.c_str(),result.c_str());
- }
- return CURLE_OK == nRecCode;
- }
- BOOL COTA::HttpPost(std::string host, std::string context, std::string &result, DATATYPE dt)
- {
- AUTOTIMING;
- CharEncoding::ASCII2UTF8(context.c_str(), context);
- m_curl.SetHeaders("Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*");
- m_curl.SetHeaders("Accept-Language: zh-cn");
- if (dt == DATA_JSON)
- m_curl.SetHeaders("Content-Type: application/json;charset=utf-8");
- int nRecCode = CURLE_OK;
- if (_tcsstr(host.c_str(), "https://"))
- nRecCode = m_curl.Posts(host, context, result);
- else
- nRecCode = m_curl.Posts(host, context, result);
- result = CharEncoding::DeCode_URLUTF8(result.c_str());
- if (CURLE_OK != nRecCode)
- {
- TCHAR szMsg[250] = {0};
- _stprintf_s(szMsg, _T("http(post) request failed, curl error code = %d\r"), nRecCode);
- ShowMessage(szMsg);
- LOG4C((LOG_NOTICE, "请求失败:%s, 错误码=%ld\n", context.c_str(), nRecCode));
- if ( _tcsicmp("https://cn.ota.qhmoka.com/ota-services/report/reportToolsLog", host.c_str()) )
- Global::WriteReportLog(0, __FUNCTION__, "Request(post) Failure, Curl Error Code=%ld: host=%s, content=%s, result=%s", nRecCode, host.c_str(), context.c_str(), result.c_str());
- }
- std::string api = host.substr(host.find_last_of('/') + 1);
- #ifdef _DEBUG
- LOG4C((LOG_NOTSET, "\n地址:%s\n报文:%s\n结果:%s\n", host.c_str(), context.size() > 256 ? std::string(context.substr(0, 256) + (" ……")).c_str() : context.c_str(), result.c_str()));
- #else
- LOG4C((LOG_NOTSET, "\n地址:%s\n报文:%s\n结果:%s\n", api.c_str(), context.size() > 256 ? std::string(context.substr(0, 256)+(" ……")).c_str() : context.c_str(), result.c_str()));
- #endif
- return CURLE_OK == nRecCode;
- }
- BOOL COTA::GetMIDInfo(MIDInfo &mid, std::string bid, std::string strMacs, std::string iptvid, std::string version)
- {
- AUTOTIMING;
- // Json数据;
- cJSON *pJson = cJSON_CreateObject();
- cJSON_AddStringToObject(pJson, "clientType", iptvid.c_str());
- cJSON_AddStringToObject(pJson, "version", version.c_str());
- cJSON_AddStringToObject(pJson, "mac", strMacs.c_str());
- cJSON_AddStringToObject(pJson, "bid", trim(bid).c_str());
- char *pJsonText = cJSON_Print(pJson);
- // post请求;
- std::string name, type;
- std::string host, result, context;
- context = pJsonText;
- if (pJsonText)
- delete pJsonText;
- pJsonText = NULL;
- cJSON_Delete(pJson);
- pJson = NULL;
- host = Global::g_bTestHost ? _T("http://test.admin.uc.qhmoka.com/scbc-server/clientType/getMessage.do") : _T("https://cn.uc.qhmoka.com/scbc-server/clientType/getMessage.do");
- BOOL bRet = HttpPost(host, context, result, DATA_JSON);
- if (!bRet)
- {
- LOG4C((LOG_WARN, "Post request failed!"));
- goto end;
- }
- // 解析返回值;
- pJson = cJSON_Parse(result.c_str());
- if (pJson == NULL)
- {
- bRet = FALSE;
- LOG4C((LOG_WARN, "JSON parsing failure:post=%s;return=%s\n", context.c_str(), result.c_str()));
- Global::WriteReportLog(0, __FUNCTION__, "JSON Parsing Error: host=%s, content=%s, result=%s", host.c_str(), context.c_str(), result.c_str());
- goto end;
- }
- mid.message = cJSON_GetObjectItem(pJson, "message") ? cJSON_GetObjectItem(pJson, "message")->valuestring : "";
- mid.code = cJSON_GetObjectItem(pJson, "code") ? cJSON_GetObjectItem(pJson, "code")->valuestring : "";
- mid.factoryname = cJSON_GetObjectItem(pJson, "factoryName") ? (cJSON_GetObjectItem(pJson, "factoryName")->valuestring != NULL ? cJSON_GetObjectItem(pJson, "factoryName")->valuestring : "") : "";
- mid.factoryNum = cJSON_GetObjectItem(pJson, "factoryNum") ? (cJSON_GetObjectItem(pJson, "factoryNum")->valuestring != NULL ? cJSON_GetObjectItem(pJson, "factoryNum")->valuestring : "") : "";
- mid.version = cJSON_GetObjectItem(pJson, "version") ? (cJSON_GetObjectItem(pJson, "version")->valuestring != NULL ? cJSON_GetObjectItem(pJson, "version")->valuestring : "") : "";
- mid.projectid = cJSON_GetObjectItem(pJson, "projectId") ? (cJSON_GetObjectItem(pJson, "projectId")->valuestring != NULL ? cJSON_GetObjectItem(pJson, "projectId")->valuestring : "") : "";
- mid.clienttype = cJSON_GetObjectItem(pJson, "clientType") ? (cJSON_GetObjectItem(pJson, "clientType")->valuestring != NULL ? cJSON_GetObjectItem(pJson, "clientType")->valuestring : "") : "";
- mid.host = cJSON_GetObjectItem(pJson, "host") ? (cJSON_GetObjectItem(pJson, "host")->valuestring != NULL ? cJSON_GetObjectItem(pJson, "host")->valuestring : "") : "";
- cJSON *pObj = cJSON_GetObjectItem(pJson, "obj");
- if (pObj == NULL)
- {
- bRet = FALSE;
- LOG4C((LOG_WARN, "JSON parsing failure:post=%s;return=%s\n", context.c_str(), result.c_str()));
- Global::WriteReportLog(0, __FUNCTION__, "JSON Parsing Error: host=%s, content=%s, result=%s", host.c_str(), context.c_str(), result.c_str());
- goto end;
- }
- if (_tcsicmp(mid.code.c_str(), "1000") != 0)
- {
- bRet = FALSE;
- LOG4C((LOG_WARN, "return invalid data:post=%s;return=%s\n", context.c_str(), result.c_str()));
- Global::WriteReportLog(0, __FUNCTION__, "return invalid data: host=%s, content=%s, result=%s", host.c_str(), context.c_str(), result.c_str());
- goto end;
- }
- if (mid.host.size() == 0)
- {
- bRet = FALSE;
- LOG4C((LOG_WARN, "return invalid data:post=%s;return=%s\n", context.c_str(), result.c_str()));
- Global::WriteReportLog(0, __FUNCTION__, "return invalid data: host=%s, content=%s, result=%s", host.c_str(), context.c_str(), result.c_str());
- goto end;
- }
- // 清空
- Global::g_AutoOnlineChassis = Global::CHASSIS();
- Global::g_AutoOnlineChassis.ProjectID = _ttoi(mid.projectid.c_str());
- Global::g_AutoOnlineChassis.url = mid.host;
- Global::g_AutoOnlineChassis.name = bid;
- mid.obj.clear();
- cJSON *pItem = NULL;
- int nSize = cJSON_GetArraySize(pObj);
- for (int i = 0; i < nSize; i++)
- {
- pItem = cJSON_GetArrayItem(pObj, i);
- if (pItem)
- {
- name = cJSON_GetObjectItem(pItem, "name") ? cJSON_GetObjectItem(pItem, "name")->valuestring : "";
- type = cJSON_GetObjectItem(pItem, "type") ? cJSON_GetObjectItem(pItem, "type")->valuestring : "";
- mid.obj.insert(pair<std::string, std::string>(name,type));
- if ( _tcsicmp(name.c_str(), _T("DeviceID")) == 0 )
- {
- if ( type.size() )
- {
- Global::g_AutoOnlineChassis.IsCopyDID = true;
- Global::g_AutoOnlineChassis.ClientType = "tcl_unknown_model";
- }
- else
- {
- Global::g_AutoOnlineChassis.IsCopyDID = false;
- Global::g_AutoOnlineChassis.ClientType = "";
- }
- }
- else if (_tcsicmp(name.c_str(), _T("MAC")) == 0)
- {
- Global::g_AutoOnlineChassis.IsCopyMAC = true;
- Global::g_AutoOnlineChassis.MACType = type;
- }
- else if (_tcsicmp(name.c_str(), _T("HDCP_KEY")) == 0)
- {
- Global::g_AutoOnlineChassis.IsCopyKEY = true;
- Global::g_AutoOnlineChassis.HDCPKeyType = type;
- }
- else if (_tcsicmp(name.c_str(), _T("CI_PLUS_KEY")) == 0)
- {
- Global::g_AutoOnlineChassis.IsCopyCikey = true;
- Global::g_AutoOnlineChassis.CIKeyType = type;
- }
- else if (_tcsicmp(name.c_str(), _T("WiDi")) == 0)
- {
- Global::g_AutoOnlineChassis.IsCopyWiDi = true;
- Global::g_AutoOnlineChassis.WiDiType = type;
- }
- else if (_tcsicmp(name.c_str(), _T("Widevine_KEY")) == 0)
- {
- Global::g_AutoOnlineChassis.IsCopyWidevine = true;
- Global::g_AutoOnlineChassis.WidevineType = type;
- }
- else if (_tcsicmp(name.c_str(), _T("HDCP2.2_KEY")) == 0)
- {
- Global::g_AutoOnlineChassis.IsCopyKEY2_2 = true;
- Global::g_AutoOnlineChassis.HDCPKey22Type = type;
- }
- else if (_tcsicmp(name.c_str(), _T("NETFILX_ESN")) == 0)
- {
- Global::g_AutoOnlineChassis.IsCopyESN = true;
- Global::g_AutoOnlineChassis.ESNType = type;
- }
- }
- }
- if ( mid.projectid.size() != 0 )
- {
- Global::g_AutoOnlineChassis.IsWritePID = true;
- }
- LOG4C((LOG_NOTSET, "post success:post=%s;return=%s\n", context.c_str(), result.c_str()));
- end:
- // 释放Json;
- if (pJsonText)
- delete pJsonText;
- pJsonText = NULL;
- cJSON_Delete(pJson);
- if ( mid.factoryname.size() )
- UpgradeCheck(mid.factoryNum);
- if (!bRet)
- {
- std::map<std::string, std::string> report_data;
- report_data.insert(std::pair<std::string, std::string>("host", host));
- report_data.insert(std::pair<std::string, std::string>("context", context));
- report_data.insert(std::pair<std::string, std::string>("result", result));
- g_db.InsertReportInfo("GetMIDInfo", report_data);
- }
- return bRet;
- }
- BOOL COTA::GetKeyInfo(std::string host, std::string context, std::string keyname, std::string valuename, std::string &value)
- {
- AUTOTIMING;
- std::string result;
- if (!HttpPost(host, context, result))
- {
- return FALSE;
- }
- // 无数据;
- if (result.size() == 0)
- {
- LOG4C((LOG_ERROR, "(http=%s) No data return!", context.c_str()));
- ShowMessage(_T("http No data return!\r"));
- Global::WriteReportLog(0, __FUNCTION__, "http No data return: host=%s, content=%s, result=%s", host.c_str(), context.c_str(), result.c_str());
- return FALSE;
- }
- // 解析xml;
- tinyxml2::XMLDocument doc;
- if (tinyxml2::XML_SUCCESS != doc.Parse(result.c_str()))
- {
- LOG4C((LOG_ERROR, "(http=%s) this post return xml invalid!", context.c_str()));
- ShowMessage(_T("http return invalid xml data!\r"));
- Global::WriteReportLog(0, __FUNCTION__, "http return invalid xml data: host=%s, content=%s, result=%s", host.c_str(), context.c_str(), result.c_str());
- return FALSE;
- }
- std::string code;
- std::string desc;
- tinyxml2::XMLElement *pXmlRoot = NULL;
- if ((pXmlRoot = doc.RootElement()) != NULL)
- {
- if (_tcsicmp(pXmlRoot->Value(), "response") == 0)
- {
- // 属性;
- const tinyxml2::XMLAttribute *pAttr = pXmlRoot->FirstAttribute();
- while (pAttr)
- {
- if (_tcsicmp(pAttr->Name(), "code") == 0)
- {
- code = pAttr->Value();
- }
- if (_tcsicmp(pAttr->Name(), "desc") == 0)
- {
- desc = pAttr->Value();
- }
- pAttr = pAttr->Next();
- }
- if (_tcsicmp(code.c_str(), "200") != 0)
- {
- LOG4C((LOG_ERROR, "(http=%s) Code is not equal to 200, desc=%s!", context.c_str(), desc.c_str()));
- TCHAR szMsg[250] = { 0 };
- _stprintf_s(szMsg, _T("http Code is not equal to 200, desc=%s\r"), desc.c_str());
- ShowMessage(szMsg);
- // 如果是上报接口,500表示成功;
- if (host.rfind("/report.do?") != std::string::npos && _tcsicmp(code.c_str(), "500") == 0)
- return TRUE;
- Global::WriteReportLog(0, __FUNCTION__, "http Code is not equal to 200: host=%s, content=%s, result=%s", host.c_str(), context.c_str(), result.c_str());
- return FALSE;
- }
- if (keyname.size() == 0 || valuename.size() == 0)
- return TRUE;
- // 子项;
- std::string name; // std::string value;
- tinyxml2::XMLElement *pXmlElent = pXmlRoot->FirstChildElement();
- while (pXmlElent)
- {
- // 中文;
- CharEncoding::UTF82ASCII(pXmlElent->Value(), name);
- if (_tcsicmp(name.c_str(), keyname.c_str()) == 0)
- {
- pAttr = pXmlElent->FirstAttribute();
- while (pAttr)
- {
- if (_tcsicmp(pAttr->Name(), valuename.c_str()) == 0)
- {
- value = pAttr->Value();
- break;
- }
- pAttr = pAttr->Next();
- }
- break;
- }
- pXmlElent = pXmlElent->NextSiblingElement();
- }
- if (value.size() == 0)
- {
- LOG4C((LOG_ERROR, "(http=%s)xml value is empty!", context.c_str()));
- ShowMessage(_T("http xml value is empty!\r"));
- Global::WriteReportLog(0, __FUNCTION__, "http xml value is empty: host=%s, content=%s, result=%s", host.c_str(), context.c_str(), result.c_str());
- return FALSE;
- }
- return TRUE;
- }
- }
- return FALSE;
- }
- void COTA::UpgradeCheck(std::string factoryNum)
- {
- #if TEMP_5659
- // 5659特别版,不升级;
- return;
- #endif
- AUTOTIMING;
- // 当前文件版本号、产品版本号;
- WORD sFileVersion[4] = {0};
- WORD sProductVerion[4] = {0};
- Global::GetVersion(Global::g_szCurModulePath, sFileVersion, sProductVerion);
- TCHAR szVersion[128] = {0};
- _stprintf_s(szVersion, _T("%d.%d.%d.%d"), sFileVersion[0], sFileVersion[1], sFileVersion[2], sFileVersion[3]);
- // Json数据;
- cJSON *pJson = cJSON_CreateObject();
- cJSON_AddStringToObject(pJson, "appid", "SCBC_Factory_Tools");
- cJSON_AddStringToObject(pJson, "devicemodel", "SCBC_Factory_Tools");
- cJSON_AddStringToObject(pJson, "dnum", factoryNum.c_str()); //工厂名;
- cJSON_AddStringToObject(pJson, "ver", szVersion);
- cJSON_AddStringToObject(pJson, "type", "pc");
- char *pJsonText = cJSON_Print(pJson);
- // post请求;
- std::string strHttpUrl, strResHttp, strContext, strErrCode, strErrValue, strCount;
- strContext = pJsonText;
- // 正式上线地址;
- strHttpUrl = _T("https://cn.ota.qhmoka.com/ota-services/upmp/upgradeInterfaceForPC");
- HttpPost(strHttpUrl.c_str(), pJsonText, strResHttp, DATA_JSON);
- // 释放Json;
- if (pJsonText)
- delete pJsonText;
- pJsonText = NULL;
- cJSON_Delete(pJson);
- pJson = NULL;
- if (strResHttp.size() == 0)
- {
- LOG4C((LOG_WARN, "返回结果空:post=%s;return=%s", strContext.c_str(), strResHttp.c_str()));
- Global::WriteReportLog(0, __FUNCTION__, "http No data return: host=%s, content=%s, result=%s", strHttpUrl.c_str(), strContext.c_str(), strResHttp.c_str());
- ReportUpgrade(FALSE, szVersion, factoryNum);
- return;
- }
- // 将json结果转换成结构体;
- Global::__update_result__ upresult;
- pJson = cJSON_Parse(strResHttp.c_str());
- if (pJson == NULL)
- {
- cJSON_Delete(pJson);
- LOG4C((LOG_WARN, "cJSON解析时出错:post=%s;return=%s", strContext.c_str(), strResHttp.c_str()));
- Global::WriteReportLog(0, __FUNCTION__, "JSON Parsing Error: host=%s, content=%s, result=%s", strHttpUrl.c_str(), strContext.c_str(), strResHttp.c_str());
- ReportUpgrade(FALSE, szVersion, factoryNum);
- return;
- }
- upresult.servertime = cJSON_GetObjectItem(pJson, "servertime") ? cJSON_GetObjectItem(pJson, "servertime")->valuestring : "";
- upresult.callid = cJSON_GetObjectItem(pJson, "callid") ? cJSON_GetObjectItem(pJson, "callid")->valuestring : "";
- upresult.note = cJSON_GetObjectItem(pJson, "note") ? cJSON_GetObjectItem(pJson, "note")->valuestring : "";
- upresult.status = cJSON_GetObjectItem(pJson, "state") ? cJSON_GetObjectItem(pJson, "state")->valuestring : "";
- upresult.language = cJSON_GetObjectItem(pJson, "language") ? cJSON_GetObjectItem(pJson, "language")->valuestring : "";
- upresult.apiversion = cJSON_GetObjectItem(pJson, "apiversion") ? cJSON_GetObjectItem(pJson, "apiversion")->valuestring : "";
- cJSON *pUpdate = cJSON_GetObjectItem(pJson, "upgrade");
- if (pUpdate == NULL)
- {
- cJSON_Delete(pJson);
- LOG4C((LOG_WARN, "升级接口没有升级内容:post=%s;return=%s", strContext.c_str(), strResHttp.c_str()));
- Global::WriteReportLog(0, __FUNCTION__, "Upgrade interface has no upgrade content: host = %s, content = %s, result = %s", strHttpUrl.c_str(), strContext.c_str(), strResHttp.c_str());
- //ReportUpdateResult(FALSE, strVer);
- return;
- }
- upresult.upcontent.now = cJSON_GetObjectItem(pUpdate, "now") ? cJSON_GetObjectItem(pUpdate, "now")->valuestring : "";
- upresult.upcontent.type = cJSON_GetObjectItem(pUpdate, "type") ? cJSON_GetObjectItem(pUpdate, "type")->valuestring : "";
- upresult.upcontent.appid = cJSON_GetObjectItem(pUpdate, "appid") ? cJSON_GetObjectItem(pUpdate, "appid")->valuestring : "";
- upresult.upcontent.apptype = cJSON_GetObjectItem(pUpdate, "apptype") ? cJSON_GetObjectItem(pUpdate, "apptype")->valuestring : "";
- upresult.upcontent.version = cJSON_GetObjectItem(pUpdate, "version") ? cJSON_GetObjectItem(pUpdate, "version")->valuestring : "";
- upresult.upcontent.verid = cJSON_GetObjectItem(pUpdate, "verid") ? cJSON_GetObjectItem(pUpdate, "verid")->valuestring : "";
- upresult.upcontent.md5 = cJSON_GetObjectItem(pUpdate, "md5") ? cJSON_GetObjectItem(pUpdate, "md5")->valuestring : "";
- upresult.upcontent.size = cJSON_GetObjectItem(pUpdate, "size") ? cJSON_GetObjectItem(pUpdate, "size")->valuestring : "";
- upresult.upcontent.minicon = cJSON_GetObjectItem(pUpdate, "minicon") ? cJSON_GetObjectItem(pUpdate, "minicon")->valuestring : "";
- upresult.upcontent.midicon = cJSON_GetObjectItem(pUpdate, "midicon") ? cJSON_GetObjectItem(pUpdate, "midicon")->valuestring : "";
- upresult.upcontent.fileurl = cJSON_GetObjectItem(pUpdate, "fileurl") ? cJSON_GetObjectItem(pUpdate, "fileurl")->valuestring : "";
- upresult.upcontent.increment = cJSON_GetObjectItem(pUpdate, "increment") ? cJSON_GetObjectItem(pUpdate, "increment")->valuestring : "";
- upresult.upcontent.appendver = cJSON_GetObjectItem(pUpdate, "appendver") ? cJSON_GetObjectItem(pUpdate, "appendver")->valuestring : "";
- upresult.upcontent.updatetime = cJSON_GetObjectItem(pUpdate, "updatetime") ? cJSON_GetObjectItem(pUpdate, "updatetime")->valuestring : "";
- // 释放cJSON资源;
- cJSON_Delete(pJson);
- pJson = NULL;
- if (upresult.status.compare("0000") != 0)
- {
- LOG4C((LOG_WARN, "升级接口http返回出错:post=%s;return=%s", strContext.c_str(), strResHttp.c_str()));
- Global::WriteReportLog(0, __FUNCTION__, "Error upgrading interface: host = %s, content = %s, result = %s", strHttpUrl.c_str(), strContext.c_str(), strResHttp.c_str());
- ReportUpgrade(FALSE, szVersion, factoryNum);
- return;
- }
- // 下载文件;
- TCHAR szNewPath[MAX_PATH] = {0};
- _stprintf_s(szNewPath, _T("%s%s.exe"), Global::g_szCurModuleDir, upresult.upcontent.version.c_str());
- if (_tcsicmp(szVersion , upresult.upcontent.version.c_str()) > 0 )
- {
- LOG4C((LOG_NOTICE, "当前版本(%s)比服务器版本(%s)新", szVersion, upresult.upcontent.version.c_str()));
- Global::WriteReportLog(0, __FUNCTION__, "The current version (%s) is newer than the server version (%s).", szVersion, upresult.upcontent.version.c_str());
- return;
- }
- if (MessageBox(NULL, _T("found out lastest software version, you have to update."), _T("Tips"), MB_YESNO | MB_TOPMOST | MB_ICONINFORMATION) == IDNO)
- return;
- // 保存成文件;
- if (!DownloadFile(upresult.upcontent.fileurl, szNewPath))
- {
- LOG4C((LOG_NOTICE, "下载升级文件失败"));
- MessageBox(NULL, _T("Download failure."), _T("Tips"), MB_OK | MB_TOPMOST | MB_ICONINFORMATION);
- ReportUpgrade(FALSE, szVersion, factoryNum);
- Global::WriteReportLog(0, __FUNCTION__, "Failed to download upgrade file=%s", upresult.upcontent.fileurl.c_str());
- return;
- }
- else
- {
- ReportUpgrade(TRUE, szVersion, factoryNum);
- }
- Sleep(500);
- MessageBox(NULL, _T("Complete the download, you need restart apps."), _T("Tips"), MB_OK | MB_TOPMOST | MB_ICONINFORMATION);
- // 启动程序;
- ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); //某些情况下要初始化COM才能使用ShellExecuteEx;
- SHELLEXECUTEINFO stuExecInfo = {0};
- CString strParamters;
- strParamters.Format(_T("--pid %ld --oldpath \"%s\" --newpath \"%s\" --killproc 1"), GetCurrentProcessId(), Global::g_szCurModulePath, szNewPath);
- //strParamters.Format(_T("--pid %ld --oldpath \"%s\" --newpath \"%s\" --killproc 0"), GetCurrentProcessId(), g_szCurModulePath, szNewPath);
- stuExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
- stuExecInfo.lpFile = _T("rename.exe");
- stuExecInfo.nShow = SW_HIDE;
- stuExecInfo.lpParameters = strParamters;
- stuExecInfo.lpVerb = _T("open");
- stuExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI;
- if (!ShellExecuteEx(&stuExecInfo))
- {
- DWORD dwError = GetLastError();
- LOG4C((LOG_ERROR, "执行rename程序失败,错误码=%d", dwError));
- MessageBox(NULL, _T("Upgrade failure."), _T("Tips"), MB_OK | MB_TOPMOST | MB_ICONINFORMATION);
- Global::WriteReportLog(0, __FUNCTION__, "Upgrade failed, Error=%d", dwError);
- return;
- }
- HANDLE hProcess = stuExecInfo.hProcess;
- if (hProcess != NULL)
- CloseHandle(hProcess);
- else
- {
- LOG4C((LOG_WARN, "执行rename程序失败,进程句柄空"));
- Global::WriteReportLog(0, __FUNCTION__, "Upgrade failed, Process handle empty");
- }
- // 提示升级程序执行;
- LOG4C((LOG_NOTICE, "后台升级程序执行成功,程序退出后自动覆盖文件"));
- }
- void COTA::ReportUpgrade(BOOL bUpgrade, std::string ver, std::string num)
- {
- AUTOTIMING;
- // Json数据;
- cJSON *pJson = cJSON_CreateObject();
- cJSON_AddStringToObject(pJson, "appid", "SCBC_Factory_Tools");
- cJSON_AddStringToObject(pJson, "devmodel", "SCBC_Factory_Tools");
- cJSON_AddStringToObject(pJson, "dnum", num.c_str());
- cJSON_AddStringToObject(pJson, "ver", ver.c_str());
- cJSON_AddStringToObject(pJson, "result", bUpgrade ? "Success" : "FAILURE");
- cJSON_AddStringToObject(pJson, "type", "pc");
- char *pJsonText = cJSON_Print(pJson);
- // post请求;
- std::string strHttpUrl, strResHttp, strContext, strErrCode, strErrValue, strCount;
- strContext = pJsonText;
- strHttpUrl = _T("https://cn.ota.qhmoka.com/ota-services/upmp/operateInterfaceForPC");
- HttpPost(strHttpUrl.c_str(), strContext.c_str(), strResHttp, DATA_JSON);
- // 释放Json;
- if (pJsonText)
- delete pJsonText;
- pJsonText = NULL;
- cJSON_Delete(pJson);
- pJson = NULL;
- pJson = cJSON_Parse(strResHttp.c_str());
- if (pJson == NULL)
- {
- LOG4C((LOG_WARN, "解析JSON失败:post=%s;return=%s", strContext.c_str(), strResHttp.c_str()));
- Global::WriteReportLog(0, __FUNCTION__, "JSON Parsing Error: host=%s, content=%s, result=%s", strHttpUrl.c_str(), strContext.c_str(), strResHttp.c_str());
- return;
- }
- std::string result = cJSON_GetObjectItem(pJson, "state") ? cJSON_GetObjectItem(pJson, "state")->valuestring : "";
- cJSON_Delete(pJson);
- pJson = NULL;
- }
- BOOL COTA::DownloadFile(std::string url, std::string savePath)
- {
- AUTOTIMING;
- std::string result;
- if (HttpGet(url, "", result))
- {
- FILE* pf = nullptr;
- if (fopen_s(&pf, savePath.c_str(), "wb") == 0 )
- {
- if ( pf )
- {
- fwrite(result.data(), result.size(), 1, pf);
- fclose(pf);
- return TRUE;
- }
- }
- }
- Global::WriteReportLog(0, __FUNCTION__, "Failed to download file: host = %s", url.c_str());
- return FALSE;
- }
- BOOL COTA::ReportLog(TReportData& repdata)
- {
- AUTOTIMING;
- // Json数据;
- cJSON* pJson = cJSON_CreateObject();
- cJSON_AddStringToObject(pJson, "reportType", repdata.report_type.c_str());
- cJSON *pObj = cJSON_AddObjectToObject(pJson, _T("reportData"));
- for ( auto it : repdata.report_data)
- {
- cJSON_AddStringToObject(pObj, it.first.c_str(), it.second.c_str());
- }
-
- char* pJsonText = cJSON_Print(pJson);
- // post请求;
- std::string strHttpUrl, strResHttp, strContext;
- strContext = pJsonText;
- strHttpUrl = _T("https://cn.ota.qhmoka.com/ota-services/report/reportToolsLog");
- HttpPost(strHttpUrl.c_str(), strContext.c_str(), strResHttp, DATA_JSON);
- // 释放Json;
- if (pJsonText)
- delete pJsonText;
- pJsonText = NULL;
- cJSON_Delete(pJson);
- pJson = NULL;
- pJson = cJSON_Parse(strResHttp.c_str());
- if (pJson == NULL)
- {
- LOG4C((LOG_WARN, "解析JSON失败:post=%s;return=%s", strContext.c_str(), strResHttp.c_str()));
- Global::WriteReportLog(0, __FUNCTION__, "JSON Parsing Error: host=%s, content=%s, result=%s", strHttpUrl.c_str(), strContext.c_str(), strResHttp.c_str());
- return FALSE;
- }
- std::string result = cJSON_GetObjectItem(pJson, "code") ? cJSON_GetObjectItem(pJson, "code")->valuestring : "";
- cJSON_Delete(pJson);
- pJson = NULL;
- if ( _tcsicmp(result.c_str(), _T("200")) == 0 )
- {
- return TRUE;
- }
- Global::WriteReportLog(0, __FUNCTION__, "http Code is not equal to 200: host=%s, content=%s, result=%s", strHttpUrl.c_str(), strContext.c_str(), strResHttp.c_str());
- return FALSE;
- }
- BOOL COTA::ReportLog(std::string url, std::string content)
- {
- AUTOTIMING;
- // post请求;
- std::string result;
- if (!HttpPost(url, content, result, DATA_JSON))
- return FALSE;
- // 释放Json;
- cJSON *pJson = cJSON_Parse(result.c_str());
- if (pJson == NULL)
- {
- LOG4C((LOG_WARN, "解析JSON失败:post=%s;return=%s", content.c_str(), result.c_str()));
- //Global::WriteReportLog(0, __FUNCTION__, "JSON Parsing Error: host=%s, content=%s, result=%s", url.c_str(), content.c_str(), result.c_str());
- return FALSE;
- }
- std::string code = cJSON_GetObjectItem(pJson, "code") ? cJSON_GetObjectItem(pJson, "code")->valuestring : "";
- cJSON_Delete(pJson);
- pJson = NULL;
- if (_tcsicmp(code.c_str(), _T("1000")) == 0)
- {
- return TRUE;
- }
- //Global::WriteReportLog(0, __FUNCTION__, "http Code is not equal to 1000: host=%s, content=%s, result=%s", url.c_str(), content.c_str(), result.c_str());
- return FALSE;
- }
- BOOL COTA::GetVCode(std::string mac, std::string& vcode)
- {
- std::string result;
- std::string host = _T("http://test.admin.ota.qhmoka.com/IDManage/export/getvcode.do?");
- std::string context = _T("macAddress=");
- context.append(mac);
- if (!HttpPost(host, context, result))
- return FALSE;
- // 无数据;
- if (result.size() == 0)
- {
- LOG4C((LOG_ERROR, "(http=%s) No data return!", context.c_str()));
- return FALSE;
- }
- // 解析xml;
- tinyxml2::XMLDocument doc;
- if (tinyxml2::XML_SUCCESS != doc.Parse(result.c_str()))
- {
- LOG4C((LOG_ERROR, "(http=%s) this post return xml invalid!", context.c_str()));
- return FALSE;
- }
- }
|