FingerPrint_impl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. #include "stdafx.h"
  2. #include "FingerPrint_impl.h"
  3. #include "log4vendor.h"
  4. #include "path.h"
  5. #ifdef RVC_OS_WIN
  6. #include <direct.h>
  7. #define GetCurrentDir _getcwd
  8. #else
  9. #include <unistd.h>
  10. #include<cstring>
  11. #include <cstdio>
  12. #include <netdb.h> //gethostbyname
  13. #include <arpa/inet.h> //ntohl
  14. #include <unistd.h> // Linux系统中
  15. #include <netdb.h>
  16. #include <net/if.h>
  17. #include <arpa/inet.h>
  18. #include <sys/ioctl.h>
  19. #include <sys/types.h>
  20. #include <sys/socket.h>
  21. #include <netinet/in.h>
  22. #define GetCurrentDir getcwd
  23. #endif
  24. #ifdef RVC_OS_WIN
  25. BOOL APIENTRY DllMain( HMODULE hModule,
  26. DWORD ul_reason_for_call,
  27. LPVOID lpReserved
  28. )
  29. {
  30. switch (ul_reason_for_call)
  31. {
  32. case DLL_PROCESS_ATTACH:
  33. case DLL_THREAD_ATTACH:
  34. case DLL_THREAD_DETACH:
  35. case DLL_PROCESS_DETACH:
  36. break;
  37. }
  38. return TRUE;
  39. }
  40. #endif
  41. string get_current_directory() {
  42. char buff[256];
  43. GetCurrentDir(buff, 256);
  44. string current_working_directory(buff);
  45. return current_working_directory;
  46. }
  47. void writeFile(const char* filename)
  48. {
  49. FILE* fp = fopen(filename, "wb");
  50. fprintf(fp, "fingerImage");
  51. string content = "fingerImage";
  52. fwrite(content.c_str(), content.length(), 1, fp);
  53. fclose(fp);
  54. }
  55. DEVICEBASE_API ErrorCodeEnum CreateDevComponent(DeviceBaseClass*& pBaseObj)
  56. {
  57. pBaseObj = new FingerPrintImpl();
  58. if (pBaseObj == NULL) {
  59. return Error_Resource;
  60. }
  61. cmb::log_init_config config;
  62. config.dev_name = "FingerPrint";
  63. #ifdef RVC_OS_WIN
  64. config.log_dir = ("C:\\rvc\\dbg\\");
  65. #else
  66. config.log_dir = ("/opt/rvc/dbg/");
  67. #endif
  68. std::string str;
  69. cmb::log4vendor::init(config, str);
  70. printf("init after: %s\n", str.c_str());
  71. return Error_Succeed;
  72. }
  73. DEVICEBASE_API ErrorCodeEnum ReleaseDevComponent(DeviceBaseClass*& pBaseObj)
  74. {
  75. if (pBaseObj == NULL) {
  76. return Error_Param;
  77. }
  78. if (FingerPrintImpl* pTmp = dynamic_cast<FingerPrintImpl*>(pBaseObj))
  79. {
  80. delete pTmp;
  81. pTmp = NULL;
  82. return Error_Succeed;
  83. }
  84. return Error_Param;
  85. }
  86. FingerPrintImpl::FingerPrintImpl()
  87. {
  88. }
  89. FingerPrintImpl::~FingerPrintImpl()
  90. {
  91. }
  92. ErrorCodeEnum FingerPrintImpl::GetDevCategory(DevCategoryInfo& devCategory)
  93. {
  94. if (GetDevCategoryHttp(devCategory)) {
  95. strcpy(devCategory.szModel, "CM=2.0#CID=00000000");
  96. strcpy(devCategory.szType, "PVER=ACT#MID=ACT-F5-5540-0SH");
  97. strcpy(devCategory.szVendor, "cmbszSimulator");
  98. return Error_Succeed;
  99. }
  100. else {
  101. return Error_Exception;
  102. }
  103. }
  104. ErrorCodeEnum FingerPrintImpl::Reset()
  105. {
  106. return Error_Succeed;
  107. }
  108. ErrorCodeEnum FingerPrintImpl::DevClose()
  109. {
  110. return Error_Succeed;
  111. }
  112. ErrorCodeEnum FingerPrintImpl::GetLastErr(DevErrorInfo& devErrInfo)
  113. {
  114. static int times = 0;
  115. char szMessage[1024];
  116. sprintf(szMessage, "这是第 %d 条错误信息", ++times);
  117. strcpy(devErrInfo.szErrMsg, szMessage);
  118. devErrInfo.dwErrMsgLen = strlen(szMessage);
  119. return Error_Succeed;
  120. }
  121. ErrorCodeEnum FingerPrintImpl::DevOpen(DWORD dwPort, DWORD dwBaudRate)
  122. {
  123. LOG4VTM(INFO, "当前目录:" << get_current_directory());
  124. depCfgPath = get_current_directory() + SPLIT_SLASH_STR + "dep" + SPLIT_SLASH_STR + "cmbsz.ini";
  125. bool ret = iniRead.ReadConfig(depCfgPath);
  126. if (ret == false)
  127. {
  128. LOG4VTM(INFO, "dep ReadConfig is Error,cfg=" << depCfgPath);
  129. }
  130. return Error_Succeed;
  131. }
  132. ErrorCodeEnum FingerPrintImpl::Image2Feature(const char* imageName, LPBYTE lpbFeature, int& iLength)
  133. {
  134. //imageName写入的文件,lpbFeature 特征码,iLength特征码长度
  135. string imgName(imageName);
  136. string imgPath = get_current_directory() + SPLIT_SLASH_STR + "dep" + SPLIT_SLASH_STR + imgName;
  137. writeFile(imgPath.c_str());
  138. //获取特征码
  139. if (Image2FeatureHttp(lpbFeature, iLength)) {
  140. return Error_Succeed;
  141. }
  142. else {
  143. return Error_Exception;
  144. }
  145. }
  146. ErrorCodeEnum FingerPrintImpl::Image2Template(const char* imagePath1, const char* imagePath2, const char* imagePath3, LPBYTE lpbTemplate, int& iLength)
  147. {
  148. //获取模板特征码
  149. LOG4VTM(INFO, "imagePath1 path = " << imagePath1);
  150. LOG4VTM(INFO, "imagePath2 path = " << imagePath2);
  151. LOG4VTM(INFO, "imagePath3 path = " << imagePath3);
  152. if (Image2TemplateHttp(lpbTemplate, iLength)) {
  153. return Error_Succeed;
  154. }
  155. else {
  156. return Error_Exception;
  157. }
  158. }
  159. ErrorCodeEnum FingerPrintImpl::Match(LPBYTE lpbTemplate[], int iTemplateLen[], int templateNum, LPBYTE lbpFeature, int& iFeatureLen, int level /*= 3*/)
  160. {
  161. return Error_NotImpl;
  162. }
  163. ErrorCodeEnum FingerPrintImpl::Cancel()
  164. {
  165. return Error_NotImpl;
  166. }
  167. bool FingerPrintImpl::Image2FeatureHttp(LPBYTE lpbFeature, int& iLength)
  168. {
  169. Image2FeatureReq fReq;
  170. Image2FeatureRet fRet;
  171. IHttpFunc* client;
  172. client = create_http(HttpsLogCallBack);
  173. Json::Value rootReq;
  174. Json::FastWriter writer;
  175. rootReq["ip"] = GetLocalIP();
  176. rootReq["entityName"] = "FingerPrint";
  177. rootReq["method"] = "getFingerPrint";//实体对外接口名
  178. rootReq["adapterInterName"] = "Image2Feature";//适配器接口名
  179. //fReq.m_url = "http://localhost:8080/avs/imitate/simulateData";
  180. string urlStr = iniRead.ReadString("server", "url", "");
  181. if (urlStr.empty()) {
  182. LOG4VTM(INFO, "Image2FeatureHttp req fail,url is empty, use default url");
  183. fReq.m_url = "http://localhost:8080/avs/imitate/simulateData";
  184. }
  185. else {
  186. fReq.m_url = urlStr;
  187. }
  188. fReq.m_timeOut = 10;
  189. string jsonReq = writer.write(rootReq);
  190. fReq.m_reqStr = jsonReq;//请求参数
  191. bool ret = client->Post(fReq, fRet);
  192. if (ret) {
  193. LOG4VTM(INFO, "Image2FeatureHttp fRet.m_retStr = " << fRet.m_retStr);
  194. Json::Reader reader;
  195. Json::Value rootRet;
  196. if (!reader.parse(fRet.m_retStr, rootRet, false))
  197. {
  198. LOG4VTM(INFO, "Image2FeatureHttp parse resp is fail,url=" << fReq.m_url.c_str());
  199. return false;//失败
  200. }
  201. bool isSucc = rootRet["success"].asBool();
  202. if (isSucc) {
  203. //解析数据
  204. if (rootRet.isMember("data")) {
  205. string strFeature = rootRet["data"]["feature"].asString();
  206. if (strFeature.length() != 684) {
  207. LOG4VTM(INFO, "Image2FeatureHttp return feature length is not right,len = " << strFeature.length());
  208. return false;
  209. }
  210. else {
  211. LOG4VTM(INFO, "Image2FeatureHttp return feature len = " << strFeature.length() << " feature = " << strFeature.c_str());
  212. }
  213. #ifdef RVC_OS_WIN
  214. strcpy_s((char*)lpbFeature, 1024, strFeature.c_str());
  215. #else
  216. strncpy((char*)lpbFeature, strFeature.c_str(), 684);
  217. #endif
  218. iLength = strFeature.length();
  219. LOG4VTM(INFO, "Image2FeatureHttp return feature len =" << strFeature.length() << ", lpbFeature = " << lpbFeature);
  220. return true;
  221. }
  222. else {
  223. LOG4VTM(INFO, "Image2FeatureHttp return [data] is null");
  224. return false;//查询失败
  225. }
  226. }
  227. else {
  228. LOG4VTM(INFO, "Image2FeatureHttp [success] is false,url= " << fReq.m_url.c_str());
  229. return false;//查询失败
  230. }
  231. }
  232. else {
  233. LOG4VTM(INFO, "Image2FeatureHttp req fail,err=%s" << fRet.m_errMsg.c_str());
  234. return false;
  235. }
  236. }
  237. bool FingerPrintImpl::Image2TemplateHttp(LPBYTE lpbTemplate, int& iLength)
  238. {
  239. Image2TemplateReq fReq;
  240. Image2TemplateRet fRet;
  241. IHttpFunc* client;
  242. client = create_http(HttpsLogCallBack);
  243. Json::Value rootReq;
  244. Json::FastWriter writer;
  245. rootReq["ip"] = GetLocalIP();
  246. rootReq["entityName"] = "FingerPrint";
  247. rootReq["method"] = "generateTemplate";
  248. rootReq["adapterInterName"] = "Image2Template";//???
  249. string urlStr = iniRead.ReadString("server", "url", "");
  250. if (urlStr.empty()) {
  251. LOG4VTM(INFO, "Image2FeatureHttp req fail,url is empty, use default url");
  252. fReq.m_url = "http://localhost:8080/avs/imitate/simulateData";
  253. }
  254. else {
  255. fReq.m_url = urlStr;
  256. }
  257. fReq.m_timeOut = 10;
  258. string jsonReq = writer.write(rootReq);
  259. fReq.m_reqStr = jsonReq;//请求参数
  260. bool ret = client->Post(fReq, fRet);
  261. if (ret) {
  262. Json::Reader reader;
  263. Json::Value rootRet;
  264. if (!reader.parse(fRet.m_retStr, rootRet, false))
  265. {
  266. LOG4VTM(INFO, "Image2TemplateHttp parse resp is fail,url=" << fReq.m_url.c_str());
  267. return false;//失败
  268. }
  269. bool isSucc = rootRet["success"].asBool();
  270. if (isSucc) {
  271. //解析数据
  272. if (rootRet.isMember("data")) {
  273. string strFeature = rootRet["data"]["template"].asString();
  274. if (strFeature.length() != 684) {
  275. LOG4VTM(INFO, "Image2TemplateHttp return feature length is not right,len = " << strFeature.length());
  276. return false;
  277. }
  278. else {
  279. LOG4VTM(INFO, "Image2TemplateHttp return feature len = " << strFeature.length() << " feature = " << strFeature.c_str());
  280. }
  281. #ifdef RVC_OS_WIN
  282. strcpy_s((char*)lpbTemplate, 1024, strFeature.c_str());
  283. #else
  284. strncpy((char*)lpbTemplate, strFeature.c_str(), 684);
  285. #endif
  286. iLength = strFeature.length();
  287. LOG4VTM(INFO, "Image2TemplateHttp return feature len =" << strFeature.length() << ", lpbFeature = " << lpbTemplate);
  288. return true;
  289. }
  290. else {
  291. LOG4VTM(INFO, ("Image2TemplateHttp return [data] is null"));
  292. return false;//查询失败
  293. }
  294. }
  295. else {
  296. LOG4VTM(INFO, "Image2TemplateHttp [success] is false,url= " << fReq.m_url.c_str());
  297. return false;//查询失败
  298. }
  299. }
  300. else {
  301. LOG4VTM(INFO, "Image2TemplateHttp req fail,err=%s" << fRet.m_errMsg.c_str());
  302. return false;
  303. }
  304. }
  305. bool FingerPrintImpl::GetDevCategoryHttp(DevCategoryInfo& devCategory)
  306. {
  307. LOG4VTM(INFO, "GetDevCategoryHttp");
  308. return commonSimpleHttp("GetDevCategory");
  309. }
  310. bool FingerPrintImpl::commonSimpleHttp(string adapterInterName)
  311. {
  312. CommonReq cReq;
  313. CommonRet cRet;
  314. IHttpFunc* client;
  315. client = create_http(HttpsLogCallBack);
  316. Json::Value rootReq;
  317. Json::FastWriter writer;
  318. rootReq["ip"] = GetLocalIP();
  319. rootReq["entityName"] = "FingerPrint";
  320. rootReq["adapterInterName"] = adapterInterName;//适配器接口名
  321. string strErrPrefix = adapterInterName + "http";
  322. cReq.m_url = getUrl();
  323. cReq.m_timeOut = 30;
  324. string jsonReq = writer.write(rootReq);
  325. cReq.m_reqStr = jsonReq;//请求参数
  326. bool ret = client->Post(cReq, cRet);
  327. if (ret) {
  328. Json::Reader reader;
  329. Json::Value rootRet;
  330. if (!reader.parse(cRet.m_retStr, rootRet, false))
  331. {
  332. LOG4VTM(INFO, strErrPrefix << " parse resp is fail,url=" << cReq.m_url.c_str());
  333. return false;//失败
  334. }
  335. bool isSucc = rootRet["success"].asBool();
  336. if (isSucc) {
  337. //解析数据
  338. if (rootRet.isMember("data")) {
  339. bool isResult = rootRet["data"]["result"].asBool();
  340. if (isResult) {
  341. return true;
  342. }
  343. else {
  344. return false;
  345. }
  346. }
  347. else {
  348. LOG4VTM(INFO, strErrPrefix << " return [data] is null");
  349. return false;
  350. }
  351. }
  352. else {
  353. LOG4VTM(INFO, strErrPrefix << " [success] is false,");
  354. return false;
  355. }
  356. }
  357. else {
  358. LOG4VTM(INFO, strErrPrefix << " req fail,err=%s" << cRet.m_errMsg.c_str());
  359. return false;
  360. }
  361. }
  362. string FingerPrintImpl::getUrl()
  363. {
  364. string urlStr = iniRead.ReadString("server", "url", "");
  365. if (urlStr.empty()) {
  366. LOG4VTM(INFO, "url is empty, use default url");
  367. return "http://localhost:8080/avs/imitate/simulateDataN";
  368. }
  369. else {
  370. return urlStr;
  371. }
  372. }
  373. #ifdef NEWER_COMPILER_WORKAROUNDS
  374. DEVICEBASE_API ErrorCodeEnum GetDevAdapterVersion(DevSoftVersion& retVesion)
  375. {
  376. retVesion.wMajor = retVesion.wMinor = retVesion.wBuild = retVesion.wRevision = 0;
  377. return Error_Succeed;
  378. }
  379. #endif // NEWER_COMPILER_WORKAROUNDS