123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- #include "stdafx.h"
- #include "FingerPrint_impl.h"
- #include "log4vendor.h"
- #include "path.h"
- #ifdef RVC_OS_WIN
- #include <direct.h>
- #define GetCurrentDir _getcwd
- #else
- #include <unistd.h>
- #include<cstring>
- #include <cstdio>
- #include <netdb.h> //gethostbyname
- #include <arpa/inet.h> //ntohl
- #include <unistd.h> // Linux系统中
- #include <netdb.h>
- #include <net/if.h>
- #include <arpa/inet.h>
- #include <sys/ioctl.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #define GetCurrentDir getcwd
- #endif
- #ifdef RVC_OS_WIN
- BOOL APIENTRY DllMain( HMODULE hModule,
- DWORD ul_reason_for_call,
- LPVOID lpReserved
- )
- {
- switch (ul_reason_for_call)
- {
- case DLL_PROCESS_ATTACH:
- case DLL_THREAD_ATTACH:
- case DLL_THREAD_DETACH:
- case DLL_PROCESS_DETACH:
- break;
- }
- return TRUE;
- }
- #endif
- //string GetLocalIP()
- //{
- //#ifdef RVC_OS_WIN
- // hostent* ent = gethostbyname(NULL);
- // if (ent && ent->h_addr_list[0] != NULL)
- // {
- // int i = 0;
- // for (; ent->h_addr_list[i] != NULL; ++i)
- // {
- // struct in_addr* in = (struct in_addr*)ent->h_addr_list[i];
- // if (in->S_un.S_un_b.s_b1 == 99 || in->S_un.S_un_b.s_b1 == 10)
- // break;
- // }
- //
- // if (ent->h_addr_list[i] == NULL)
- // i = 0;
- //
- // auto in = (struct in_addr*)ent->h_addr_list[i];
- // char xIP[64] = {};
- // 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);
- // LOG4VTM(INFO, ("ip:%s", xIP));
- //
- // return string(xIP);
- // }
- //#else
- // std::string re = "";
- // int sfd, intr;
- // struct ifreq buf[16];
- // struct ifconf ifc;
- // sfd = socket(AF_INET, SOCK_DGRAM, 0);
- // if (sfd < 0)
- // return "wrong 1: get ip socket failed";
- // ifc.ifc_len = sizeof(buf);
- // ifc.ifc_buf = (caddr_t)buf;
- // if (ioctl(sfd, SIOCGIFCONF, (char*)&ifc))
- // return "wrong 2: get ip ioctl failed";
- // intr = ifc.ifc_len / sizeof(struct ifreq);
- // while (intr-- > 0 && ioctl(sfd, SIOCGIFADDR, (char*)&buf[intr]));
- // close(sfd);
- // unsigned long ip = ntohl(((struct sockaddr_in*)(&buf[intr].ifr_addr))->sin_addr.s_addr);
- //
- // char* str = new char[1024];
- // sprintf(str, "%u.%u.%u.%u", ip >> 24 & 0xFF, ip >> 16 & 0xFF, ip >> 8 & 0xFF, ip >> 0 & 0xFF);
- // re = str;
- // return re;
- //#endif
- //}
- string get_current_directory() {
- char buff[256];
- GetCurrentDir(buff, 256);
- string current_working_directory(buff);
- return current_working_directory;
- }
- void writeFile(const char* filename)
- {
- FILE* fp = fopen(filename, "wb");
- fprintf(fp, "fingerImage");
- string content = "fingerImage";
- fwrite(content.c_str(), content.length(), 1, fp);
- fclose(fp);
- }
- DEVICEBASE_API ErrorCodeEnum CreateDevComponent(DeviceBaseClass*& pBaseObj)
- {
- pBaseObj = new FingerPrintImpl();
- if (pBaseObj == NULL) {
- return Error_Resource;
- }
- cmb::log_init_config config;
- config.dev_name = "FingerPrint";
- #ifdef RVC_OS_WIN
- config.log_dir = ("C:\\rvc\\dbg\\");
- #else
- config.log_dir = ("/opt/rvc/dbg/");
- #endif
- std::string str;
- cmb::log4vendor::init(config, str);
- printf("init after: %s\n", str.c_str());
- return Error_Succeed;
- }
- DEVICEBASE_API ErrorCodeEnum ReleaseDevComponent(DeviceBaseClass*& pBaseObj)
- {
- if (pBaseObj == NULL) {
- return Error_Param;
- }
- if (FingerPrintImpl* pTmp = dynamic_cast<FingerPrintImpl*>(pBaseObj))
- {
- delete pTmp;
- pTmp = NULL;
- return Error_Succeed;
- }
- return Error_Param;
- }
- FingerPrintImpl::FingerPrintImpl()
- {
- }
- FingerPrintImpl::~FingerPrintImpl()
- {
- }
- ErrorCodeEnum FingerPrintImpl::GetDevCategory(DevCategoryInfo& devCategory)
- {
- strcpy(devCategory.szModel, "szModel");
- strcpy(devCategory.szType, "szType");
- strcpy(devCategory.szVendor, "szVendor=cmbszSimulator");
- return Error_Succeed;
- }
- ErrorCodeEnum FingerPrintImpl::Reset()
- {
- return Error_Succeed;
- }
- ErrorCodeEnum FingerPrintImpl::DevClose()
- {
- return Error_Succeed;
- }
- ErrorCodeEnum FingerPrintImpl::GetLastErr(DevErrorInfo& devErrInfo)
- {
- static int times = 0;
- char szMessage[1024];
- sprintf(szMessage, "这是第 %d 条错误信息", ++times);
- strcpy(devErrInfo.szErrMsg, szMessage);
- devErrInfo.dwErrMsgLen = strlen(szMessage);
- return Error_Succeed;
- }
- ErrorCodeEnum FingerPrintImpl::DevOpen(DWORD dwPort, DWORD dwBaudRate)
- {
- LOG4VTM(INFO, "当前目录:" << get_current_directory());
- depCfgPath = get_current_directory() + SPLIT_SLASH_STR + "dep" + SPLIT_SLASH_STR + "cmbsz.ini";
- bool ret = iniRead.ReadConfig(depCfgPath);
- if (ret == false)
- {
- LOG4VTM(INFO, "dep ReadConfig is Error,cfg=" << depCfgPath);
- }
- return Error_Succeed;
- }
- ErrorCodeEnum FingerPrintImpl::Image2Feature(const char* imageName, LPBYTE lpbFeature, int& iLength)
- {
- return Error_Succeed;
- //imageName写入的文件,lpbFeature 特征码,iLength特征码长度
- string imgName(imageName);
- string imgPath = get_current_directory() + SPLIT_SLASH_STR + "dep" + SPLIT_SLASH_STR + imgName;
- writeFile(imgPath.c_str());
- //获取特征码
- if (Image2FeatureHttp(lpbFeature, iLength)) {
- return Error_Succeed;
- }
- else {
- return Error_Exception;
- }
- }
- ErrorCodeEnum FingerPrintImpl::Image2Template(const char* imagePath1, const char* imagePath2, const char* imagePath3, LPBYTE lpbTemplate, int& iLength)
- {
- //获取模板特征码
- LOG4VTM(INFO, "imagePath1 path = " << imagePath1);
- LOG4VTM(INFO, "imagePath2 path = " << imagePath2);
- LOG4VTM(INFO, "imagePath3 path = " << imagePath3);
- if (Image2TemplateHttp(lpbTemplate, iLength)) {
- return Error_Succeed;
- }
- else {
- return Error_Exception;
- }
- return Error_Succeed;
- }
- ErrorCodeEnum FingerPrintImpl::Match(LPBYTE lpbTemplate[], int iTemplateLen[], int templateNum, LPBYTE lbpFeature, int& iFeatureLen, int level /*= 3*/)
- {
- return Error_NotImpl;
- }
- ErrorCodeEnum FingerPrintImpl::Cancel()
- {
- return Error_NotImpl;
- }
- bool FingerPrintImpl::Image2FeatureHttp(LPBYTE lpbFeature, int& iLength)
- {
- Image2FeatureReq fReq;
- Image2FeatureRet fRet;
- IHttpFunc* client;
- client = create_http(HttpsLogCallBack);
- Json::Value rootReq;
- Json::FastWriter writer;
- rootReq["ip"] = GetLocalIP();
- rootReq["entityName"] = "FingerPrint";
- rootReq["method"] = "getFingerPrint";//实体对外接口名
- rootReq["adapterInterName"] = "Image2Feature";//适配器接口名
- //fReq.m_url = "http://localhost:8080/avs/imitate/simulateData";
- string urlStr = iniRead.ReadString("server", "url", "");
- if (urlStr.empty()) {
- LOG4VTM(INFO, "Image2FeatureHttp req fail,url is empty, use default url");
- fReq.m_url = "http://localhost:8080/avs/imitate/simulateData";
- }
- else {
- fReq.m_url = urlStr;
- }
- fReq.m_timeOut = 10;
- string jsonReq = writer.write(rootReq);
- fReq.m_reqStr = jsonReq;//请求参数
- bool ret = client->Post(fReq, fRet);
- if (ret) {
- LOG4VTM(INFO, "Image2FeatureHttp fRet.m_retStr = " << fRet.m_retStr);
- Json::Reader reader;
- Json::Value rootRet;
- if (!reader.parse(fRet.m_retStr, rootRet, false))
- {
- LOG4VTM(INFO, "Image2FeatureHttp parse resp is fail,url=" << fReq.m_url.c_str());
- return false;//失败
- }
- bool isSucc = rootRet["success"].asBool();
- if (isSucc) {
- //解析数据
- if (rootRet.isMember("data")) {
- string strFeature = rootRet["data"]["feature"].asString();
- if (strFeature.length() != 684) {
- LOG4VTM(INFO, "Image2FeatureHttp return feature length is not right,len = " << strFeature.length());
- return false;
- }
- else {
- LOG4VTM(INFO, "Image2FeatureHttp return feature len = " << strFeature.length() << " feature = " << strFeature.c_str());
- }
- #ifdef RVC_OS_WIN
- strcpy_s((char*)lpbFeature, 1024, strFeature.c_str());
- #else
- strncpy((char*)lpbFeature, strFeature.c_str(), 684);
- #endif
- iLength = strFeature.length();
- LOG4VTM(INFO, "Image2FeatureHttp return feature len =" << strFeature.length() << ", lpbFeature = " << lpbFeature);
- return true;
- }
- else {
- LOG4VTM(INFO, "Image2FeatureHttp return [data] is null");
- return false;//查询失败
- }
- }
- else {
- LOG4VTM(INFO, "Image2FeatureHttp [success] is false,url= " << fReq.m_url.c_str());
- return false;//查询失败
- }
- }
- else {
- LOG4VTM(INFO, "Image2FeatureHttp req fail,err=%s" << fRet.m_errMsg.c_str());
- return false;
- }
- }
- bool FingerPrintImpl::Image2TemplateHttp(LPBYTE lpbTemplate, int& iLength)
- {
- Image2TemplateReq fReq;
- Image2TemplateRet fRet;
- IHttpFunc* client;
- client = create_http(HttpsLogCallBack);
- Json::Value rootReq;
- Json::FastWriter writer;
- rootReq["ip"] = GetLocalIP();
- rootReq["entityName"] = "FingerPrint";
- rootReq["method"] = "generateTemplate";
- rootReq["adapterInterName"] = "Image2Template";//???
- string urlStr = iniRead.ReadString("server", "url", "");
- if (urlStr.empty()) {
- LOG4VTM(INFO, "Image2FeatureHttp req fail,url is empty, use default url");
- fReq.m_url = "http://localhost:8080/avs/imitate/simulateData";
- }
- else {
- fReq.m_url = urlStr;
- }
- fReq.m_timeOut = 10;
- string jsonReq = writer.write(rootReq);
- fReq.m_reqStr = jsonReq;//请求参数
- bool ret = client->Post(fReq, fRet);
- if (ret) {
- Json::Reader reader;
- Json::Value rootRet;
- if (!reader.parse(fRet.m_retStr, rootRet, false))
- {
- LOG4VTM(INFO, "Image2TemplateHttp parse resp is fail,url=" << fReq.m_url.c_str());
- return false;//失败
- }
- bool isSucc = rootRet["success"].asBool();
- if (isSucc) {
- //解析数据
- if (rootRet.isMember("data")) {
- string strFeature = rootRet["data"]["template"].asString();
- if (strFeature.length() != 684) {
- LOG4VTM(INFO, "Image2TemplateHttp return feature length is not right,len = " << strFeature.length());
- return false;
- }
- else {
- LOG4VTM(INFO, "Image2TemplateHttp return feature len = " << strFeature.length() << " feature = " << strFeature.c_str());
- }
- #ifdef RVC_OS_WIN
- strcpy_s((char*)lpbTemplate, 1024, strFeature.c_str());
- #else
- strncpy((char*)lpbTemplate, strFeature.c_str(), 684);
- #endif
- iLength = strFeature.length();
- LOG4VTM(INFO, "Image2TemplateHttp return feature len =" << strFeature.length() << ", lpbFeature = " << lpbTemplate);
- return true;
- }
- else {
- LOG4VTM(INFO, ("Image2TemplateHttp return [data] is null"));
- return false;//查询失败
- }
- }
- else {
- LOG4VTM(INFO, "Image2TemplateHttp [success] is false,url= " << fReq.m_url.c_str());
- return false;//查询失败
- }
- }
- else {
- LOG4VTM(INFO, "Image2TemplateHttp req fail,err=%s" << fRet.m_errMsg.c_str());
- return false;
- }
- }
|