OTA.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #ifndef __OTA_20190702__
  2. #define __OTA_20190702__
  3. #include "SerialPort.h"
  4. #include "CurlClient.h"
  5. #include "../cJson/cJSON.h"
  6. #define BUFFER_LEN 10240
  7. #pragma once
  8. typedef struct __SIACP__
  9. {
  10. std::string name; // 命令描述;
  11. std::string head; // 命令头;
  12. std::string code; // 命令码;
  13. std::string mark; // 特殊标记;
  14. bool bMulticode = false; // 命令码是否多参数;
  15. int cmd_wait_time = 100; // 两条串口指令间隔时间;
  16. int read_wait_time = 100; // 写完串口后,等待多久读;
  17. } _SIACP_;
  18. // 包头码(包引导码)
  19. enum PHeader {
  20. TV_Debug = 0xAA,
  21. TV_Return = 0xAB,
  22. TV_Panel_Debug = 0xAC,
  23. TV_Panel_Return = 0xAD,
  24. TV_Debug_Other = 0xAE,
  25. TV_Other_Return = 0xAF,
  26. };
  27. // 命令结果码;
  28. enum RCode {
  29. RC_OK = 0x0A, // 命令执行通过;
  30. RC_ERR = 0x0E, // 命令错误或无法执行;
  31. RC_LOSE = 0x0F, // 命令丢失(链路层出错);
  32. };
  33. //////////////////////////////////////////////////////////////////////////
  34. enum LOG_ENUM
  35. {
  36. NORMAL_LOG = 0,
  37. ERROR_LOG,
  38. OK_LOG,
  39. INFO_LOG
  40. };
  41. extern std::string HexString2Bytes(std::string strHex, const int& len /* = 3 */);
  42. extern std::string Bytes2HexString(const unsigned char* pbuffer, int nLen);
  43. extern std::string Bytes2HexString(const unsigned char* pbuffer, int nLen, char chSpace);
  44. class CSIACP
  45. {
  46. public:
  47. CSIACP(void);
  48. ~CSIACP(void);
  49. void SetMainDlg(CDialogEx* p);
  50. void ShowMessage(LPCTSTR lpMsg, LOG_ENUM logtype = NORMAL_LOG);
  51. #ifdef _DEBUG
  52. public:
  53. #else
  54. protected:
  55. #endif
  56. CSerialPort* m_pSerial;
  57. CDialogEx* m_pMainDlg;
  58. typedef struct __RTN__
  59. {
  60. int len = 0; // 返回的数据长度;
  61. std::string data; // 返回的数据内容;
  62. }RTN, * pRTN;
  63. std::vector<RTN> m_vtdata;
  64. std::vector<_SIACP_> m_vtcommand;
  65. BOOL ExecSerialCommand(const _SIACP_& siacp, std::string command);
  66. BOOL ParserSerialData(std::vector<RTN>& vtdata, const _SIACP_& siacp, byte* pbuffer, DWORD dwlen);
  67. const _SIACP_* GetSiacp(std::string name);
  68. std::string GetSiacpCommand(const _SIACP_& siacp, std::string others, int othersLen);
  69. std::string GetSiacpCommand(std::string name, std::string others, int othersLen);
  70. public:
  71. void LoadCommand();
  72. void SaveCommand(std::vector<_SIACP_> &vtSiacp, std::string path);
  73. BOOL OpenComm(LPCTSTR lpszCom, DWORD dwBaudrate);
  74. void CloseComm();
  75. BOOL IsOpen() {
  76. return m_pSerial && m_pSerial->IsOpen();
  77. }
  78. BOOL SendCommand(std::string cmd_name, const byte* pszOthers = NULL, DWORD dwOthersLen = 0);
  79. BOOL SendCommand2(std::string cmd_name, std::string data = "", size_t nlen = 0);
  80. public:
  81. // 等待tv启动成功;
  82. BOOL SCBC_WaitTVBoot();
  83. BOOL SCBC_MTKInit();
  84. // 进入工厂模式;
  85. BOOL SCBC_EnterFactory();
  86. // 离开工厂模式;
  87. BOOL SCBC_LeaveFactory();
  88. // 初始化Wb;
  89. BOOL SCBC_WBInit();
  90. // 获取pid;
  91. BOOL SCBC_GetProjectId(int& pid);
  92. // 软件版本号;
  93. BOOL SCBC_GetSoftVersion(std::string& strVer);
  94. // 设备ID
  95. BOOL SCBC_GetDeviceId(std::string& strDid);
  96. // ClientType;
  97. BOOL SCBC_GetClientType(std::string& strClt);
  98. // MAC地址;
  99. BOOL SCBC_GetMAC(std::string& strMac);
  100. // HDCP Key;
  101. BOOL SCBC_GetHDCPKey(std::string& strHDCP);
  102. // HDCP Key2.2;
  103. BOOL SCBC_GetHDCPKey22(std::string& strHDCP22);
  104. // Widi;
  105. BOOL SCBC_GetWidi(std::string& strWidi);
  106. // Netflix ESN;
  107. BOOL SCBC_GetNetflixESN(std::string& strESN);
  108. // Widevine;
  109. BOOL SCBC_GetWidevine(std::string& strWidevine);
  110. // ci plus key;
  111. BOOL SCBC_GetCiKey(std::string& strCikey);
  112. // OSD Language;
  113. BOOL SCBC_GetOSDLanguage(std::string& strOSDLan);
  114. // Shop Language;
  115. BOOL SCBC_GetShopLanguage(std::string& strShopLan);
  116. BOOL SCBC_GetChannel(std::string& channel);
  117. BOOL SCBC_SetProjectId(const int& pid);
  118. BOOL SCBC_SetProjectId(std::string pid);
  119. BOOL SCBC_SetProjectId(const byte* pBuffer, const int& nLen);
  120. BOOL SCBC_SetDeviceId(std::string strDeviceId);
  121. BOOL SCBC_SetDeviceId(const byte* pBuffer, const int &nLen);
  122. BOOL SCBC_SetMAC(std::string strMac);
  123. BOOL SCBC_SetMAC(const byte* pBuffer, const int& nLen);
  124. BOOL SCBC_SetHDCPKey(std::string strHDCP, BOOL bHasSpace = FALSE);
  125. BOOL SCBC_SetHDCPKey(const byte* pBuffer, const int& nLen);
  126. BOOL SCBC_SetHDCPKey22(std::string strHDCP22, BOOL bHasSpace = FALSE);
  127. BOOL SCBC_SetHDCPKey22(const byte* pBuffer, const int& nLen);
  128. BOOL SCBC_SetNetflixESN(std::string strESN, BOOL bHasSpace = FALSE);
  129. BOOL SCBC_SetNetflixESN(const byte* pBuffer, const int& nLen);
  130. BOOL SCBC_SetWidi(std::string strWidi, BOOL bHasSpace = FALSE);
  131. BOOL SCBC_SetWidi(const byte* pBuffer, const int& nLen);
  132. BOOL SCBC_SetWidevine(std::string strWidevine, BOOL bHasSpace = FALSE);
  133. BOOL SCBC_SetWidevine(const byte* pBuffer, const int& nLen);
  134. BOOL SCBC_SetCiKey(std::string strCiKey, BOOL bHasSpace = FALSE);
  135. BOOL SCBC_SetCiKey(const byte* pBuffer, const int& nLen);
  136. BOOL SCBC_SetOSDLanguage(std::string lan, BOOL bHasSpace = TRUE);
  137. BOOL SCBC_SetOSDLanguage(const byte* pBuffer, const int& nLen);
  138. BOOL SCBC_SetShopLanguage(std::string lan, BOOL bHasSpace = TRUE);
  139. BOOL SCBC_SetShopLanguage(const byte* pBuffer, const int& nLen);
  140. BOOL SCBC_SetChannel(std::string channel, BOOL bHasSpace = TRUE);
  141. BOOL SCBC_SetChannel(const byte* pBuffer, const int& nLen);
  142. BOOL SCBC_SetWBNormal(std::string data);
  143. BOOL SCBC_SetWBNormal(const byte* pBuffer, const int& nLen);
  144. BOOL SCBC_SetWBCool(std::string data);
  145. BOOL SCBC_SetWBCool(const byte* pBuffer, const int& nLen);
  146. BOOL SCBC_SetWBWarm(std::string data);
  147. BOOL SCBC_SetWBWarm(const byte* pBuffer, const int& nLen);
  148. BOOL SCBC_CheckDeviceId();
  149. BOOL SCBC_CheckMAC();
  150. BOOL SCBC_CheckHDCP();
  151. BOOL SCBC_CheckHDCP22();
  152. BOOL SCBC_CheckNetflixESN();
  153. BOOL SCBC_CheckWidi();
  154. BOOL SCBC_CheckWidevine();
  155. BOOL SCBC_CheckCikey();
  156. BOOL SCBC_StarWarmUpMode();
  157. BOOL SCBC_StopWarmUpMode();
  158. };
  159. // 数据类型;
  160. enum DATATYPE
  161. {
  162. DATA_JSON = 0, // json数据;
  163. DATA_FORM = 1 // 表单类型;
  164. };
  165. typedef struct __MIDKEY__ {
  166. std::string name;
  167. std::string type;
  168. }MidKeyInfo;
  169. typedef struct __MID__ {
  170. std::string message;
  171. std::string code;
  172. std::string factoryname;
  173. std::string factoryip;
  174. std::string factoryNum;
  175. std::string version;
  176. std::string projectid;
  177. std::string clienttype;
  178. std::string host;
  179. std::map<std::string, std::string> obj;
  180. }MIDInfo;
  181. typedef struct TREPORTDATA {
  182. std::string report_type;
  183. std::map<std::string, std::string> report_data;
  184. }TReportData, * pTReportData;
  185. class COTA
  186. {
  187. CCurlClient m_curl;
  188. public:
  189. COTA();
  190. ~COTA();
  191. void SetMainDlg(CDialogEx* p);
  192. void ShowMessage(LPCTSTR lpMsg, LOG_ENUM logtype = NORMAL_LOG);
  193. private:
  194. CDialogEx* m_pMainDlg;
  195. BOOL HttpGet(std::string host, std::string context, std::string& result, DATATYPE dt = DATA_FORM);
  196. BOOL HttpPost(std::string host, std::string context, std::string& result, DATATYPE dt = DATA_FORM);
  197. public:
  198. MIDInfo m_midInfo;
  199. BOOL GetMIDInfo(MIDInfo& mid, std::string bid, std::string strMacs, std::string iptvid = "", std::string version = "");
  200. BOOL GetKeyInfo(std::string host, std::string context, std::string keyname, std::string valuename, std::string& value);
  201. std::string GetMidKey(const MIDInfo& mid, std::string name)
  202. {
  203. std::string key;
  204. std::map<std::string, std::string>::const_iterator it = mid.obj.find(name);
  205. if (it != mid.obj.end())
  206. key = it->second;
  207. if (_tcsicmp(name.c_str(), "DeviceID") == 0)
  208. {
  209. if ( _tcsicmp(it->first.c_str(), _T("SCBC")) == 0 || _tcsicmp(it->first.c_str(), _T("新用户系统")) == 0)
  210. key = "tcl_unknown_model";
  211. }
  212. return key;
  213. }
  214. void UpgradeCheck(std::string factoryNum);
  215. void ReportUpgrade(BOOL bUpgrade, std::string ver, std::string num);
  216. BOOL DownloadFile(std::string url, std::string savePath);
  217. BOOL ReportLog(TReportData &repdata);
  218. BOOL ReportLog(std::string url, std::string content);
  219. BOOL GetVCode(std::string mac, std::string &vcode);
  220. };
  221. class AutoTiming
  222. {
  223. public:
  224. AutoTiming(std::string des) {
  225. _start = clock();
  226. _des = des;
  227. }
  228. ~AutoTiming(){
  229. _end = clock();
  230. LOG4C((LOG_NOTICE, "%s,请求耗时:%ld ms", _des.c_str(), _end - _start));
  231. }
  232. private:
  233. clock_t _start;
  234. clock_t _end;
  235. std::string _des;
  236. };
  237. #ifndef AUTOTIMING
  238. #define AUTOTIMING AutoTiming at(__FUNCTION__)
  239. #endif
  240. extern std::string HexString2Bytes(std::string strHex, const int& len = 3);
  241. #endif