123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715 |
- #include "StdAfx.h"
- #include "SDK.h"
- #include "CritSection.h"
- #include "Global.h"
- #include "DataImpl.h"
- #include <sys/stat.h>
- #include "CharEncoding.h"
- ThreadSection g_csTask;
- std::list<STMid> CSDK::m_vtMidTask;
- std::string CSDK::_host;
- CSDK::CSDK(void):
- m_hDownloadEvent(NULL),
- m_hThreadDownload(NULL),
- m_hReportEvent(NULL),
- m_hThreadReport(NULL)
- {
- Global::Init();
- Global::GetMacAddress();
- }
- CSDK::~CSDK(void)
- {
- EndDowloadThread();
- EndReportThread();
- }
- int CSDK::QueryMidInfo(std::string order)
- {
- if ( order.size() == 0 )
- return -1;
- CDataImpl db;
- if ( !db.Open() )
- return -2;
- STMid mid;
- int nRow = db.QueryMidInfo(order.c_str(), mid);
- if ( nRow == -1)
- return -3;
- if ( nRow == 0 )
- return 0;
- if ( nRow == 1)
- {
- if ( _tcsicmp(mid.status.c_str(), "0") == 0 )
- return 1;
- else if ( _tcsicmp(mid.status.c_str(), "1") == 0 )
- return 2;
- else if ( _tcsicmp(mid.status.c_str(), "1") == -1 )
- return 3;
- }
- return -4;
- }
- int CSDK::DownloadMidData(std::string order)
- {
- if ( m_hThreadDownload == NULL )
- {
- m_hDownloadEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
- if ( m_hDownloadEvent == NULL )
- {
- return -1;
- }
- m_hThreadDownload = CreateThread(NULL, 0, ThreadDownload, this, 0, NULL);
- if ( m_hThreadDownload == NULL )
- {
- CloseHandle(m_hDownloadEvent);
- m_hDownloadEvent = NULL;
- return -1;
- }
- }
- int nRet = 0;
- STMid host_mid;
- STMid local_mid;
- // https请求;
- nRet = GetMidInfo(order, host_mid);
- if ( nRet == -1)
- return -5;
- else if ( nRet != 0 )
- return 0;
- // 判断该订单是否存在;
- CDataImpl db;
- if ( !db.Open() )
- return -2;
- // 查询mid;
- nRet = db.QueryMidInfo(order, local_mid);
- if ( nRet == -1)
- return -3;
- if ( nRet == 0 )
- {
- // 新增mid;
- db.InsertMidInfo(host_mid);
- }
- else if ( nRet == 1 )
- {
- // 是否已下载过,已下载过的不允许重新下载;
- if ( local_mid.status == "1" && local_mid.finish_date.size() )
- return 2;
- // 更新Mid;
- db.UpdateMidInfo(host_mid);
- }
- else if ( nRet > 1 )
- return -4;
-
- // 加入任务中;
- CSDK::AddDownloadTask(host_mid);
- return 1;
- }
- int CSDK::QueryKeyInfo(std::string sn, STKeys &keys)
- {
- if ( sn.size() == 0 )
- return -1;
- CDataImpl db;
- if ( !db.Open() )
- return -2;
- int nRow = db.QueryKeyInfo(sn.c_str(), keys);
- if ( nRow == -1)
- return -3;
- if ( nRow == 0 )
- return 0;
- if ( nRow == 1 )
- return 1;
- return -4;
- }
- int CSDK::UpdateKeyCopyStatus(std::string sn )
- {
- if ( sn.size() == 0 )
- return -1;
- CDataImpl db;
- if ( !db.Open() )
- return -2;
- BOOL bRet = FALSE;
- // 只更新抄写成功的状态;
- bRet = db.UpdateKeyCopyStatus(sn.c_str());
-
- return bRet == TRUE ? 1 : 0;
- }
- int CSDK::UpdateKeyReportStatus(std::string sn)
- {
- if ( sn.size() == 0 )
- return -1;
- CDataImpl db;
- if ( !db.Open() )
- return -2;
- BOOL bRet = FALSE;
- // 更新抄写成功状态:1,失败-1;
- bRet = db.UpdateKeyReportStatus(sn.c_str());
-
- return bRet == TRUE ? 1 : 0;
- }
- int CSDK::ReportKeyCopyResults(std::string sn)
- {
- if ( sn.size() == 0 )
- return -1;
- CDataImpl db;
- if ( !db.Open() )
- return -2;
- STKeys keys;
- int nRet = db.QueryKeyInfo(sn.c_str(), keys);
- if ( nRet == -1)
- return -3;
- if ( nRet == 0 )
- return 0;
- if ( nRet == 1 )
- {
- // 上报;
- std::string result;
- std::map<std::string, std::string> form;
- form.insert(std::pair<std::string,std::string>("sn", sn));
- form.insert(std::pair<std::string,std::string>("date", keys.copy_date));
- CCurlClient curl;
- curl.Initialize();
- if ( curl.FormPost("http://test.admin.ota.qhmoka.com/IDManage/reportlist.do?", form, result,3) == CURLE_OK)
- {
- cJSON *pJson = cJSON_Parse(result.c_str());
- if ( pJson == NULL )
- {
- return -6;
- }
- std::string code, des;
- code = cJSON_GetObjectItem(pJson, "code") ? cJSON_GetObjectItem(pJson, "code")->valuestring: "";
- des = cJSON_GetObjectItem(pJson, "des") ? cJSON_GetObjectItem(pJson, "des")->valuestring: "";
- if ( _tcsicmp(code.c_str(), "1000") )
- {
- return -7;
- }
- // 更新上报状态;
- nRet = db.UpdateKeyReportStatus(sn);
- if ( nRet == 1 )
- return 1;
- else
- return -8; // 上报成功,更新失败;
- }
- return -5;
- }
-
- return -4;
- }
- int CSDK::BatchReportKeyCopyResults()
- {
- CDataImpl db;
- if ( !db.Open() )
- return -2;
- std::vector<STKeys> vtkeys;
- int nRet = db.QueryUnReportKeyInfo(vtkeys);
- if ( nRet == -1)
- return -3;
- if ( nRet == 0 )
- return 0;
- // 上报;
- std::string result;
- std::map<std::string, std::string> form;
- std::vector<STKeys>::iterator it = vtkeys.begin();
- for (; it != vtkeys.end(); it++)
- {
- form.insert(std::pair<std::string,std::string>("sn", it->sn));
- form.insert(std::pair<std::string,std::string>("date", it->copy_date));
- }
- CCurlClient curl;
- curl.Initialize();
- if ( curl.FormPost("http://test.admin.ota.qhmoka.com/IDManage/reportlist.do?", form, result,3) == CURLE_OK)
- {
- cJSON *pJson = cJSON_Parse(result.c_str());
- if ( pJson == NULL )
- {
- return -6;
- }
- std::string code, des;
- code = cJSON_GetObjectItem(pJson, "code") ? cJSON_GetObjectItem(pJson, "code")->valuestring: "";
- des = cJSON_GetObjectItem(pJson, "des") ? cJSON_GetObjectItem(pJson, "des")->valuestring: "";
- if ( _tcsicmp(code.c_str(), "1000") )
- {
- return -7;
- }
- // 更新上报状态;
- nRet = db.BatchUpdateKeyReportStatus(vtkeys);
- if ( nRet == 1 )
- return 1;
- else
- return -8; // 上报成功,更新失败;
- }
- return -5;
- }
- void CSDK::AddDownloadTask(STMid &mid)
- {
- AutoThreadSection ats(&g_csTask);
- bool has = false;
- std::list<STMid>::iterator it = m_vtMidTask.begin();
- for (; it != m_vtMidTask.end(); it++ )
- {
- if ( _tcsicmp(it->order.c_str(), mid.order.c_str()) == 0 )
- {
- has = true;
- *it = mid; // 更新;
- break;
- }
- }
- if ( !has )
- {
- m_vtMidTask.push_back(mid);
- }
- }
- BOOL CSDK::PopDownloadTask(STMid &mid)
- {
- AutoThreadSection ats(&g_csTask);
- if ( m_vtMidTask.size() )
- {
- mid = m_vtMidTask.front();
- m_vtMidTask.pop_front();
- return TRUE;
- }
- return FALSE;
- }
- bool CSDK::CheckDownload(std::string file, STMid& mid)
- {
- // 校验文件md5值;
- std::string fileMd5 = GetFileMD5(file.c_str());
- if ( _tcsicmp(fileMd5.c_str(), mid.pmd5.c_str()) == 0 )
- {
- // 文件下载成功,批量插入数据库;
- std::vector<STKeys> vtKeys;
- ParserKey(file, vtKeys, mid);
- if ( vtKeys.size() )
- {
- CDataImpl db;
- if ( db.Open() )
- {
- if ( db.BatchInsertKeyInfo(vtKeys) == 0 )
- {
- // 更新任务状态;
- if ( ReportDownloadStatus(mid.order) == 0 )
- {
- db.UpdateDownloadStatus(mid.order, 1);
- }
- return true;
- }
- }
- }
- }
- return false;
- }
- int CSDK::GetMidInfo(std::string order, STMid &mid)
- {
- // http获取mid;
- std::string result;
- std::string url = "http://test.admin.ota.qhmoka.com/IDManage/getofflinelist.do?";
- std::string content = "orderNumber=";
- content.append(order);
- //content.append("&mac=");
- //content.append(Global::g_strMacs);
- CCurlClient curl;
- curl.Initialize();
- if ( curl.Post(url, content, result) == CURLE_OK)
- {
- // 解析json字符串;
- cJSON *pJson = cJSON_Parse(result.c_str());
- if ( !pJson )
- {
- return -2;
- }
- std::string code, des;
- code = cJSON_GetObjectItem(pJson, _T("code")) ? cJSON_GetObjectItem(pJson, _T("code"))->valuestring : "";
- des = cJSON_GetObjectItem(pJson, _T("des")) ? cJSON_GetObjectItem(pJson, _T("des"))->valuestring : "";
- cJSON *pData = cJSON_GetObjectItem(pJson, _T("data"));
- if ( _tcsicmp(code.c_str(), "1000") )
- {
- cJSON_Delete(pJson);
- return -3;
- }
-
- if ( !pData )
- {
- cJSON_Delete(pJson);
- return -4;
- }
- TCHAR szData[10] = {0};
- mid.order = cJSON_GetObjectItem(pData, _T("order_number")) ? cJSON_GetObjectItem(pData, _T("order_number"))->valuestring : "";
- mid.version = cJSON_GetObjectItem(pData, _T("soft_version")) ? cJSON_GetObjectItem(pData, _T("soft_version"))->valuestring : "";
- //mid.number = cJSON_GetObjectItem(pData, _T("order_quantity")) ? cJSON_GetObjectItem(pData, _T("order_quantity"))->valuestring : "";
- _itoa_s(cJSON_GetObjectItem(pData, _T("order_quantity")) ? cJSON_GetObjectItem(pData, _T("order_quantity"))->valueint : 0, szData, 10);
- mid.number = szData;
- mid.pmd5 = cJSON_GetObjectItem(pData, _T("packet_md5")) ? cJSON_GetObjectItem(pData, _T("packet_md5"))->valuestring : "";
- mid.ctype = cJSON_GetObjectItem(pData, _T("client_type")) ? cJSON_GetObjectItem(pData, _T("client_type"))->valuestring : "";
- mid.pid = cJSON_GetObjectItem(pData, _T("project_id")) ? cJSON_GetObjectItem(pData, _T("project_id"))->valuestring : "";
- mid.purl = cJSON_GetObjectItem(pData, _T("packet_url")) ? cJSON_GetObjectItem(pData, _T("packet_url"))->valuestring : "";
- //mid.psize = cJSON_GetObjectItem(pData, _T("packet_size")) ? cJSON_GetObjectItem(pData, _T("packet_size"))->valuestring : "";
- memset(szData, 0, 10);
- _itoa_s(cJSON_GetObjectItem(pData, _T("packet_size")) ? cJSON_GetObjectItem(pData, _T("packet_size"))->valueint : 0, szData, 10);
- mid.psize = szData;
- cJSON_Delete(pJson);
- pJson = NULL;
- return 0;
- }
- // http请求异常;
- return -1;
- }
- BOOL CSDK::HttpPost(std::string host, std::string context, std::string &result, DATATYPE dt)
- {
- CCurlClient curl;
- curl.Initialize();
- CharEncoding::ASCII2UTF8(context.c_str(), context);
- 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, */*");
- curl.SetHeaders("Accept-Language: zh-cn");
- if (dt == DATA_JSON)
- curl.SetHeaders("Content-Type: application/json;charset=utf-8");
- int nRecCode = CURLE_OK;
- if (_tcsstr(host.c_str(), "https://"))
- nRecCode = curl.Posts(host, context, result);
- else
- nRecCode = curl.Posts(host, context, result);
- result = CharEncoding::DeCode_URLUTF8(result.c_str());
- return CURLE_OK == nRecCode;
- }
- BOOL CSDK::GetBidInfo(std::string order, std::string macs)
- {
- // Json数据;
- cJSON *pJson = cJSON_CreateObject();
- cJSON_AddStringToObject(pJson, "clientType", "");
- cJSON_AddStringToObject(pJson, "version", "");
- cJSON_AddStringToObject(pJson, "mac", macs.c_str());
- cJSON_AddStringToObject(pJson, "bid", order.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;
- std::string code, message;
- 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)
- {
- goto end;
- }
- // 解析返回值;
- pJson = cJSON_Parse(result.c_str());
- if (pJson == NULL)
- {
- bRet = FALSE;
- goto end;
- }
- message = cJSON_GetObjectItem(pJson, "message") ? cJSON_GetObjectItem(pJson, "message")->valuestring : "";
- code = cJSON_GetObjectItem(pJson, "code") ? cJSON_GetObjectItem(pJson, "code")->valuestring : "";
- _host = cJSON_GetObjectItem(pJson, "host") ? (cJSON_GetObjectItem(pJson, "host")->valuestring != NULL ? cJSON_GetObjectItem(pJson, "host")->valuestring : "") : "";
-
- if (_tcsicmp(code.c_str(), "1000") != 0)
- {
- bRet = FALSE;
- goto end;
- }
- if (_host.size() == 0)
- {
- bRet = FALSE;
- goto end;
- }
- end:
- // 释放Json;
- if (pJsonText)
- delete pJsonText;
- pJsonText = NULL;
- cJSON_Delete(pJson);
- return bRet;
- }
- int CSDK::ReportDownloadStatus(std::string order)
- {
- // http获取mid;
- std::string result;
- std::string url = "http://test.admin.ota.qhmoka.com/IDManage/getreportofflinesn.do?";
- std::string content = "orderNumber=";
- content.append(order);
- CCurlClient curl;
- curl.Initialize();
- if ( curl.Post(url, content, result) == CURLE_OK)
- {
- // 解析json字符串;
- cJSON *pJson = cJSON_Parse(result.c_str());
- if ( !pJson )
- {
- return -2;
- }
- std::string code, des;
- code = cJSON_GetObjectItem(pJson, _T("code")) ? cJSON_GetObjectItem(pJson, _T("code"))->valuestring : "";
- des = cJSON_GetObjectItem(pJson, _T("des")) ? cJSON_GetObjectItem(pJson, _T("des"))->valuestring : "";
- if ( _tcsicmp(code.c_str(), "1000") && _tcsicmp(code.c_str(), "1006") && _tcsicmp(code.c_str(), "1007") )
- {
- cJSON_Delete(pJson);
- return -3;
- }
- cJSON_Delete(pJson);
- pJson = NULL;
- return 0;
- }
- // http请求异常;
- return -1;
- }
- void CSDK::ParserKey(std::string file, std::vector<STKeys> &vtKyes, STMid &mid)
- {
- // 读取文件;
- FILE* pf = NULL;
- _tfopen_s(&pf, file.c_str(), _T("rb"));
- if (!pf)
- return;
- // 获取文件长度;
- fseek(pf, 0, SEEK_END);
- size_t file_size = ftell(pf);
- fseek(pf, 0, SEEK_SET);
- // 读取文件内容;
- byte* pdata = (byte*)malloc(file_size);
- fread(pdata, file_size, 1, pf);
- fclose(pf);
- // Json数据;
- cJSON* pJson = cJSON_Parse((const char*)pdata);
- if (pJson == NULL)
- {
- goto end;
- }
- //////////////////////////////////////////////////////////////////////////
- // 解析Json;
- cJSON *pItem = NULL;
- int nCount = cJSON_GetArraySize(pJson);
- for ( int i = 0; i < nCount; i++ )
- {
- pItem = cJSON_GetArrayItem(pJson, i);
- if ( pItem )
- {
- STKeys key;
- key.bid = mid.order;
- key.pid = mid.pid;
- // 序列号;
- key.sn = cJSON_GetObjectItem(pItem, _T("sn")) ? cJSON_GetObjectItem(pItem, _T("sn"))->valuestring : "";
- cJSON *pJsonKeys = cJSON_GetObjectItem(pItem, _T("key"));
- if ( pJsonKeys )
- {
- char *lpszKeys = cJSON_Print(pJsonKeys);
- if ( lpszKeys)
- {
- key.keys = lpszKeys;
- free(lpszKeys);
- }
- }
- vtKyes.push_back(key);
- }
- }
- end:
- if (pdata)
- free(pdata);
- if (pJson)
- cJSON_Delete(pJson);
- }
- bool CSDK::ParserSNKey(std::string json, SNKeys &snKey)
- {
- bool bRet = false;
- // Json数据;
- cJSON* pJson = cJSON_Parse(json.c_str());
- if ( pJson )
- {
- // 序列号;
- snKey.did = cJSON_GetObjectItem(pJson, _T("sn")) ? cJSON_GetObjectItem(pJson, _T("sn"))->valuestring : "";
- snKey.mac = cJSON_GetObjectItem(pJson, _T("keys")) ? cJSON_GetObjectItem(pJson, _T("keys"))->valuestring : "";
- snKey.hdcp = cJSON_GetObjectItem(pJson, _T("keys")) ? cJSON_GetObjectItem(pJson, _T("keys"))->valuestring : "";
- snKey.hdcp22 = cJSON_GetObjectItem(pJson, _T("keys")) ? cJSON_GetObjectItem(pJson, _T("keys"))->valuestring : "";
- snKey.widi = cJSON_GetObjectItem(pJson, _T("keys")) ? cJSON_GetObjectItem(pJson, _T("keys"))->valuestring : "";
- snKey.widevine = cJSON_GetObjectItem(pJson, _T("keys")) ? cJSON_GetObjectItem(pJson, _T("keys"))->valuestring : "";
- snKey.esn = cJSON_GetObjectItem(pJson, _T("keys")) ? cJSON_GetObjectItem(pJson, _T("keys"))->valuestring : "";
- snKey.cikey = cJSON_GetObjectItem(pJson, _T("keys")) ? cJSON_GetObjectItem(pJson, _T("keys"))->valuestring : "";
- bRet = true;
- }
- if (pJson)
- cJSON_Delete(pJson);
- return bRet;
- }
- DWORD CSDK::ThreadDownload(LPVOID lpParam)
- {
- CSDK *that = (CSDK*)lpParam;
- // key包文件路径;
- TCHAR szKeyPacket[MAX_PATH] = {0};
- do
- {
- if ( m_vtMidTask.size() )
- {
- // 取出任务;
- STMid mid = m_vtMidTask.front();
- // 判断文件是否存在,并检验md5值;
- _stprintf_s(szKeyPacket, _T("%scache\\%s.json"), Global::g_szCurModuleDir, mid.order.c_str());
- if ( _taccess(szKeyPacket, 0) != -1 )
- {
- if ( CheckDownload(szKeyPacket, mid) )
- {
- // 弹出任务;
- AutoThreadSection ats(&g_csTask);
- if ( m_vtMidTask.size() )
- {
- m_vtMidTask.pop_front();
- }
- continue;
- }
- // 文件大小相同,删除文件;
- struct stat fst;
- if ( stat(szKeyPacket, &fst) == 0 )
- {
- if ( atoi(mid.psize.c_str()) == fst.st_size )
- DeleteFile(szKeyPacket);
- }
- }
- // 文件不存在,开始下载;
- CCurlClient curl;
- curl.Initialize();
- curl.DownloadEx(mid.purl, szKeyPacket);
- }
- } while (WaitForSingleObject(that->m_hDownloadEvent, 5000) == WAIT_TIMEOUT );
- return 0L;
- }
- void CSDK::EndDowloadThread()
- {
- if ( m_hDownloadEvent )
- {
- SetEvent(m_hDownloadEvent);
- }
- if ( m_hThreadDownload)
- {
- WaitForSingleObject(m_hThreadDownload, INFINITE);
- CloseHandle(m_hThreadDownload);
- m_hThreadDownload = NULL;
- if ( m_hDownloadEvent )
- CloseHandle(m_hDownloadEvent);
- m_hDownloadEvent = NULL;
- }
- }
- DWORD CSDK::ThreadReport(LPVOID lpParam)
- {
- CSDK *that = (CSDK*)lpParam;
- do
- {
- // 自动上报;
- } while (WaitForSingleObject(that->m_hReportEvent, 3000) == WAIT_TIMEOUT );
- return 0L;
- }
- void CSDK::EndReportThread()
- {
- if ( m_hReportEvent )
- {
- SetEvent(m_hReportEvent);
- }
- if ( m_hThreadReport)
- {
- WaitForSingleObject(m_hThreadReport, INFINITE);
- CloseHandle(m_hThreadReport);
- m_hThreadReport = NULL;
- if ( m_hReportEvent )
- CloseHandle(m_hReportEvent);
- m_hReportEvent = NULL;
- }
- }
|