FingerPrint_impl.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 GetLocalIP()
  42. //{
  43. //#ifdef RVC_OS_WIN
  44. // hostent* ent = gethostbyname(NULL);
  45. // if (ent && ent->h_addr_list[0] != NULL)
  46. // {
  47. // int i = 0;
  48. // for (; ent->h_addr_list[i] != NULL; ++i)
  49. // {
  50. // struct in_addr* in = (struct in_addr*)ent->h_addr_list[i];
  51. // if (in->S_un.S_un_b.s_b1 == 99 || in->S_un.S_un_b.s_b1 == 10)
  52. // break;
  53. // }
  54. //
  55. // if (ent->h_addr_list[i] == NULL)
  56. // i = 0;
  57. //
  58. // auto in = (struct in_addr*)ent->h_addr_list[i];
  59. // char xIP[64] = {};
  60. // sprintf(xIP, "%d.%d.%d.%d", in->S_un.S_un_b.s_b1, in->S_un.S_un_b.s_b2, in->S_un.S_un_b.s_b3, in->S_un.S_un_b.s_b4);
  61. // LOG4VTM(INFO, ("ip:%s", xIP));
  62. //
  63. // return string(xIP);
  64. // }
  65. //#else
  66. // std::string re = "";
  67. // int sfd, intr;
  68. // struct ifreq buf[16];
  69. // struct ifconf ifc;
  70. // sfd = socket(AF_INET, SOCK_DGRAM, 0);
  71. // if (sfd < 0)
  72. // return "wrong 1: get ip socket failed";
  73. // ifc.ifc_len = sizeof(buf);
  74. // ifc.ifc_buf = (caddr_t)buf;
  75. // if (ioctl(sfd, SIOCGIFCONF, (char*)&ifc))
  76. // return "wrong 2: get ip ioctl failed";
  77. // intr = ifc.ifc_len / sizeof(struct ifreq);
  78. // while (intr-- > 0 && ioctl(sfd, SIOCGIFADDR, (char*)&buf[intr]));
  79. // close(sfd);
  80. // unsigned long ip = ntohl(((struct sockaddr_in*)(&buf[intr].ifr_addr))->sin_addr.s_addr);
  81. //
  82. // char* str = new char[1024];
  83. // sprintf(str, "%u.%u.%u.%u", ip >> 24 & 0xFF, ip >> 16 & 0xFF, ip >> 8 & 0xFF, ip >> 0 & 0xFF);
  84. // re = str;
  85. // return re;
  86. //#endif
  87. //}
  88. string get_current_directory() {
  89. char buff[256];
  90. GetCurrentDir(buff, 256);
  91. string current_working_directory(buff);
  92. return current_working_directory;
  93. }
  94. void writeFile(const char* filename)
  95. {
  96. FILE* fp = fopen(filename, "wb");
  97. fprintf(fp, "fingerImage");
  98. string content = "fingerImage";
  99. fwrite(content.c_str(), content.length(), 1, fp);
  100. fclose(fp);
  101. }
  102. DEVICEBASE_API ErrorCodeEnum CreateDevComponent(DeviceBaseClass*& pBaseObj)
  103. {
  104. pBaseObj = new FingerPrintImpl();
  105. if (pBaseObj == NULL) {
  106. return Error_Resource;
  107. }
  108. cmb::log_init_config config;
  109. config.dev_name = "FingerPrint";
  110. #ifdef RVC_OS_WIN
  111. config.log_dir = ("C:\\rvc\\dbg\\");
  112. #else
  113. config.log_dir = ("/opt/rvc/dbg/");
  114. #endif
  115. std::string str;
  116. cmb::log4vendor::init(config, str);
  117. printf("init after: %s\n", str.c_str());
  118. return Error_Succeed;
  119. }
  120. DEVICEBASE_API ErrorCodeEnum ReleaseDevComponent(DeviceBaseClass*& pBaseObj)
  121. {
  122. if (pBaseObj == NULL) {
  123. return Error_Param;
  124. }
  125. if (FingerPrintImpl* pTmp = dynamic_cast<FingerPrintImpl*>(pBaseObj))
  126. {
  127. delete pTmp;
  128. pTmp = NULL;
  129. return Error_Succeed;
  130. }
  131. return Error_Param;
  132. }
  133. FingerPrintImpl::FingerPrintImpl()
  134. {
  135. }
  136. FingerPrintImpl::~FingerPrintImpl()
  137. {
  138. }
  139. ErrorCodeEnum FingerPrintImpl::GetDevCategory(DevCategoryInfo& devCategory)
  140. {
  141. strcpy(devCategory.szModel, "szModel");
  142. strcpy(devCategory.szType, "szType");
  143. strcpy(devCategory.szVendor, "szVendor=cmbszSimulator");
  144. return Error_Succeed;
  145. }
  146. ErrorCodeEnum FingerPrintImpl::Reset()
  147. {
  148. return Error_Succeed;
  149. }
  150. ErrorCodeEnum FingerPrintImpl::DevClose()
  151. {
  152. return Error_Succeed;
  153. }
  154. ErrorCodeEnum FingerPrintImpl::GetLastErr(DevErrorInfo& devErrInfo)
  155. {
  156. static int times = 0;
  157. char szMessage[1024];
  158. sprintf(szMessage, "这是第 %d 条错误信息", ++times);
  159. strcpy(devErrInfo.szErrMsg, szMessage);
  160. devErrInfo.dwErrMsgLen = strlen(szMessage);
  161. return Error_Succeed;
  162. }
  163. ErrorCodeEnum FingerPrintImpl::DevOpen(DWORD dwPort, DWORD dwBaudRate)
  164. {
  165. LOG4VTM(INFO, "当前目录:" << get_current_directory());
  166. depCfgPath = get_current_directory() + SPLIT_SLASH_STR + "dep" + SPLIT_SLASH_STR + "cmbsz.ini";
  167. bool ret = iniRead.ReadConfig(depCfgPath);
  168. if (ret == false)
  169. {
  170. LOG4VTM(INFO, "dep ReadConfig is Error,cfg=" << depCfgPath);
  171. }
  172. return Error_Succeed;
  173. }
  174. ErrorCodeEnum FingerPrintImpl::Image2Feature(const char* imageName, LPBYTE lpbFeature, int& iLength)
  175. {
  176. return Error_Succeed;
  177. //imageName写入的文件,lpbFeature 特征码,iLength特征码长度
  178. string imgName(imageName);
  179. string imgPath = get_current_directory() + SPLIT_SLASH_STR + "dep" + SPLIT_SLASH_STR + imgName;
  180. writeFile(imgPath.c_str());
  181. //获取特征码
  182. if (Image2FeatureHttp(lpbFeature, iLength)) {
  183. return Error_Succeed;
  184. }
  185. else {
  186. return Error_Exception;
  187. }
  188. }
  189. ErrorCodeEnum FingerPrintImpl::Image2Template(const char* imagePath1, const char* imagePath2, const char* imagePath3, LPBYTE lpbTemplate, int& iLength)
  190. {
  191. //获取模板特征码
  192. LOG4VTM(INFO, "imagePath1 path = " << imagePath1);
  193. LOG4VTM(INFO, "imagePath2 path = " << imagePath2);
  194. LOG4VTM(INFO, "imagePath3 path = " << imagePath3);
  195. if (Image2TemplateHttp(lpbTemplate, iLength)) {
  196. return Error_Succeed;
  197. }
  198. else {
  199. return Error_Exception;
  200. }
  201. return Error_Succeed;
  202. }
  203. ErrorCodeEnum FingerPrintImpl::Match(LPBYTE lpbTemplate[], int iTemplateLen[], int templateNum, LPBYTE lbpFeature, int& iFeatureLen, int level /*= 3*/)
  204. {
  205. return Error_NotImpl;
  206. }
  207. ErrorCodeEnum FingerPrintImpl::Cancel()
  208. {
  209. return Error_NotImpl;
  210. }
  211. bool FingerPrintImpl::Image2FeatureHttp(LPBYTE lpbFeature, int& iLength)
  212. {
  213. Image2FeatureReq fReq;
  214. Image2FeatureRet fRet;
  215. IHttpFunc* client;
  216. client = create_http(HttpsLogCallBack);
  217. Json::Value rootReq;
  218. Json::FastWriter writer;
  219. rootReq["ip"] = GetLocalIP();
  220. rootReq["entityName"] = "FingerPrint";
  221. rootReq["method"] = "getFingerPrint";//实体对外接口名
  222. rootReq["adapterInterName"] = "Image2Feature";//适配器接口名
  223. //fReq.m_url = "http://localhost:8080/avs/imitate/simulateData";
  224. string urlStr = iniRead.ReadString("server", "url", "");
  225. if (urlStr.empty()) {
  226. LOG4VTM(INFO, "Image2FeatureHttp req fail,url is empty, use default url");
  227. fReq.m_url = "http://localhost:8080/avs/imitate/simulateData";
  228. }
  229. else {
  230. fReq.m_url = urlStr;
  231. }
  232. fReq.m_timeOut = 10;
  233. string jsonReq = writer.write(rootReq);
  234. fReq.m_reqStr = jsonReq;//请求参数
  235. bool ret = client->Post(fReq, fRet);
  236. if (ret) {
  237. LOG4VTM(INFO, "Image2FeatureHttp fRet.m_retStr = " << fRet.m_retStr);
  238. Json::Reader reader;
  239. Json::Value rootRet;
  240. if (!reader.parse(fRet.m_retStr, rootRet, false))
  241. {
  242. LOG4VTM(INFO, "Image2FeatureHttp parse resp is fail,url=" << fReq.m_url.c_str());
  243. return false;//失败
  244. }
  245. bool isSucc = rootRet["success"].asBool();
  246. if (isSucc) {
  247. //解析数据
  248. if (rootRet.isMember("data")) {
  249. string strFeature = rootRet["data"]["feature"].asString();
  250. if (strFeature.length() != 684) {
  251. LOG4VTM(INFO, "Image2FeatureHttp return feature length is not right,len = " << strFeature.length());
  252. return false;
  253. }
  254. else {
  255. LOG4VTM(INFO, "Image2FeatureHttp return feature len = " << strFeature.length() << " feature = " << strFeature.c_str());
  256. }
  257. #ifdef RVC_OS_WIN
  258. strcpy_s((char*)lpbFeature, 1024, strFeature.c_str());
  259. #else
  260. strncpy((char*)lpbFeature, strFeature.c_str(), 684);
  261. #endif
  262. iLength = strFeature.length();
  263. LOG4VTM(INFO, "Image2FeatureHttp return feature len =" << strFeature.length() << ", lpbFeature = " << lpbFeature);
  264. return true;
  265. }
  266. else {
  267. LOG4VTM(INFO, "Image2FeatureHttp return [data] is null");
  268. return false;//查询失败
  269. }
  270. }
  271. else {
  272. LOG4VTM(INFO, "Image2FeatureHttp [success] is false,url= " << fReq.m_url.c_str());
  273. return false;//查询失败
  274. }
  275. }
  276. else {
  277. LOG4VTM(INFO, "Image2FeatureHttp req fail,err=%s" << fRet.m_errMsg.c_str());
  278. return false;
  279. }
  280. }
  281. bool FingerPrintImpl::Image2TemplateHttp(LPBYTE lpbTemplate, int& iLength)
  282. {
  283. Image2TemplateReq fReq;
  284. Image2TemplateRet fRet;
  285. IHttpFunc* client;
  286. client = create_http(HttpsLogCallBack);
  287. Json::Value rootReq;
  288. Json::FastWriter writer;
  289. rootReq["ip"] = GetLocalIP();
  290. rootReq["entityName"] = "FingerPrint";
  291. rootReq["method"] = "generateTemplate";
  292. rootReq["adapterInterName"] = "Image2Template";//???
  293. string urlStr = iniRead.ReadString("server", "url", "");
  294. if (urlStr.empty()) {
  295. LOG4VTM(INFO, "Image2FeatureHttp req fail,url is empty, use default url");
  296. fReq.m_url = "http://localhost:8080/avs/imitate/simulateData";
  297. }
  298. else {
  299. fReq.m_url = urlStr;
  300. }
  301. fReq.m_timeOut = 10;
  302. string jsonReq = writer.write(rootReq);
  303. fReq.m_reqStr = jsonReq;//请求参数
  304. bool ret = client->Post(fReq, fRet);
  305. if (ret) {
  306. Json::Reader reader;
  307. Json::Value rootRet;
  308. if (!reader.parse(fRet.m_retStr, rootRet, false))
  309. {
  310. LOG4VTM(INFO, "Image2TemplateHttp parse resp is fail,url=" << fReq.m_url.c_str());
  311. return false;//失败
  312. }
  313. bool isSucc = rootRet["success"].asBool();
  314. if (isSucc) {
  315. //解析数据
  316. if (rootRet.isMember("data")) {
  317. string strFeature = rootRet["data"]["template"].asString();
  318. if (strFeature.length() != 684) {
  319. LOG4VTM(INFO, "Image2TemplateHttp return feature length is not right,len = " << strFeature.length());
  320. return false;
  321. }
  322. else {
  323. LOG4VTM(INFO, "Image2TemplateHttp return feature len = " << strFeature.length() << " feature = " << strFeature.c_str());
  324. }
  325. #ifdef RVC_OS_WIN
  326. strcpy_s((char*)lpbTemplate, 1024, strFeature.c_str());
  327. #else
  328. strncpy((char*)lpbTemplate, strFeature.c_str(), 684);
  329. #endif
  330. iLength = strFeature.length();
  331. LOG4VTM(INFO, "Image2TemplateHttp return feature len =" << strFeature.length() << ", lpbFeature = " << lpbTemplate);
  332. return true;
  333. }
  334. else {
  335. LOG4VTM(INFO, ("Image2TemplateHttp return [data] is null"));
  336. return false;//查询失败
  337. }
  338. }
  339. else {
  340. LOG4VTM(INFO, "Image2TemplateHttp [success] is false,url= " << fReq.m_url.c_str());
  341. return false;//查询失败
  342. }
  343. }
  344. else {
  345. LOG4VTM(INFO, "Image2TemplateHttp req fail,err=%s" << fRet.m_errMsg.c_str());
  346. return false;
  347. }
  348. }