int HD8583STRUCT::Normalize() { memset(&_record,0,sizeof _record); for(int i = 0;i < FIELD_LENGTH;++i) { if(_fields[i].bit_flag) { switch(_fields[i].attribute) { case IFT_BYTE: *_fields[i]._bValue = _fields[i].data[0]; break; case IFT_CHAR: *_fields[i]._cValue = _fields[i].data[0]; break; case IFT_SHORT: BUF_2_SHORT(*_fields[i]._sValue,_fields[i].data); break; case IFT_USHORT: BUF_2_SHORT(*_fields[i]._usValue,_fields[i].data); break; case IFT_INT: BUF_2_INT(*_fields[i]._iValue,_fields[i].data); break; case IFT_UINT: BUF_2_INT(*_fields[i]._uiValue,_fields[i].data); break; case IFT_LONG: BUF_2_LONG(*_fields[i]._lValue,_fields[i].data); break; case IFT_ULONG: BUF_2_LONG(*_fields[i]._ulValue,_fields[i].data); break; case IFT_STRING: memcpy(_fields[i]._cValue,_fields[i].data,_fields[i].length_in_byte); break; case IFT_BUFFER: memcpy(_fields[i]._bValue,_fields[i].data,_fields[i].length_in_byte); break; case IFT_DATETIME: memcpy(_fields[i]._bValue,_fields[i].data,_fields[i].length_in_byte); break; default: return -1; } updateFieldLength(i); } } return 0; }
int sub_gpio_read( sub_handle hndl, int* get ) { int rc; sub_pack outpk,inpk; outpk.tag.code = SUB_GPIO_READ; outpk.tag.size = 0; inpk.tag.size = sizeof(struct gpio_resp_st); rc = sub_transaction( hndl, &outpk, &inpk, 10000 ); if( !rc ) BUF_2_INT( *get, inpk.tag.gpio_resp.val ); return rc; }
int sub_gpio_write( sub_handle hndl, int set, int* get, int mask ) { int rc; sub_pack outpk,inpk; outpk.tag.code = SUB_GPIO_WRITE; outpk.tag.size = sizeof(struct gpio_req_st); INT_2_BUF( set, outpk.tag.gpio_req.val ); INT_2_BUF( mask,outpk.tag.gpio_req.mask); inpk.tag.size = sizeof(struct gpio_resp_st); rc = sub_transaction( hndl, &outpk, &inpk, 10000 ); if( !rc ) BUF_2_INT( *get, inpk.tag.gpio_resp.val ); return rc; }
int HDDEV::HDParsePosRecord(BYTE* buf,TRADERECORDSTRUCT* trade) { BUF_2_INT(trade->CardNumber,buf); BUF_2_INT(trade->Balance,buf+4); BUF_2_INT(trade->Amount,buf+8); trade->Year = *(buf+12); trade->Month = *(buf+13); trade->Day = *(buf+14); trade->Hour = *(buf+15); trade->Minute = *(buf+16); trade->Second = *(buf+17); BUF_2_SHORT(trade->TraceOfEW,buf+18); BUF_2_INT(trade->TraceOfPOS,buf+20); BUF_2_INT(trade->OrgTransTrace,buf+24); BUF_2_INT(trade->OperatorID,buf+28); BUF_2_INT(trade->TerminalSN,buf+32); // 终端号 BUF_2_SHORT(trade->AdditionalMoney,buf+36); // 管理费 BUF_2_SHORT(trade->GoodsNumber,buf+38); trade->IndexOfEW = *(buf+40); if(trade->IndexOfEW > 0) trade->IndexOfEW--; trade->RecordType = *(buf+41); trade->ConsumeMode = *(buf+42); memcpy(trade->MAC,buf+43,4); uint16 i = GenerateCRC16(buf,43); unsigned char tmp[4] = ""; SHORT_2_BUF(i,tmp); /*tmp[0] = (unsigned char)((i & 0xFF00) >> 8); tmp[1] = (unsigned char)(i & 0xFF);*/ tmp[2] = tmp[0]; tmp[0] = tmp[1]; tmp[1] = tmp[2]; for(int i = 0; i < 2; ++i) for(int j = 0; j < 4; ++j) tmp[i] ^= KsgGetGateway()->GetConfig()->_workKey[i+j*2]; return (ACE_OS::memcmp(trade->MAC,tmp,2) == 0) ? 0 : -1; /*if( (i & 0xFF) != trade->MAC[1] || ((i & 0xFF00)>>8) != trade->MAC[0]) return -1; return 0; */ }
int HDOnlineNotice::DoResponse(HD8583STRUCT& req,HD8583STRUCT& resp,ACE_HANDLE peer) { MYDATETIMESTRUCT now; KSGDeviceManager * manager = KsgGetGateway()->GetDeviceManager(); char phyid[12] = ""; char version[14] = ""; //ACE_OS::sprintf(phyid,"%.010d",req->TerminalSN); //KSGDeviceNode * node = manager->FindDevice(phyid); //if(node == NULL) //{ // ACE_DEBUG((LM_DEBUG,"设备不存在[%s]",phyid)); // return -1; //} EncodeTermSN(req->TerminalSN,phyid); //char buf[32] = ""; //if(xutil::StringUtil::Str2Hex( // KsgGetGateway()->GetConfig()->_cardKey.c_str(),(unsigned char*)buf,8) == -1) // return -1; //ACE_OS::memset(buf+8,0xFF,16); //resp.SetFieldValue(FIELD_ADDITIONALDATA2,buf,24); resp.SetFieldValue(FIELD_TERMINALSN,req->TerminalSN); resp.SetFieldValue(FIELD_ADDRESS,req->Address); now = HDGetDataTime(); resp.SetFieldValue(FIELD_DATEANDTIME,(char*)&now,7); resp.SetFieldValue(FIELD_RESPONSECODE,(BYTE)RC_SUCCESS); resp.SetFieldValue(FIELD_VERSIONOFLIST,resp->VerOfList); DRTPPoolType::SmartObject obj; obj = KsgGetDrtpPool()->Alloc(); if(obj) { obj->SetRequestHeader(930057,1); obj->AddField(F_SDATE1,phyid); obj->AddField(F_LCERT_CODE,KsgGetGateway()->GetConfig()->_gwId); std::string the_time; KSGOSUtil::GetTimestamp(the_time); obj->AddField(F_SPHONE3,the_time.c_str()); obj->AddField(F_LVOL2,0); obj->AddField(F_DAMT0,0.0); obj->AddField(F_DAMT1,0.0); obj->AddField(F_DAMT2,0.0); obj->AddField(F_SHOLDER_AC_NO2,the_time.c_str()); int reccnt; BUF_2_INT(reccnt,req->AdditionalData1); obj->AddField(F_DAMT3,(double)reccnt); DecodeVersionNum(req->VerOfList,version); obj->AddField(F_SSERIAL4,version); if(obj->Connect()) { ACE_DEBUG((LM_DEBUG,"连接DRTP失败")); } else if(obj->SendRequest(500)) { ACE_DEBUG((LM_DEBUG,"发送心跳失败")); } else if(obj->GetReturnCode()) { ACE_DEBUG((LM_ERROR,"发送心跳失败[%s]",obj->GetReturnMsg().c_str())); } } // 发送密钥 return 1; }
int HDCCUNoticeHandler::CheckFile(int fileid,BYTE* data ,const std::string& devid ,std::string& filepath) { int recordcnt = 0; int ret; char ftp_msg[CURL_ERROR_SIZE+1] = ""; // 下载流水文件 std::string datafile = filepath; char user_pwd[256] = ""; CURL *curl; CURLcode res; BYTE file_index = *data; BYTE file_flag = 0x00; if(file_index == fileid) { // 检查记录文件 1 BUF_2_INT(recordcnt,data + 1); file_flag = *(data + 5); ACE_DEBUG((LM_DEBUG,"CCU Notice [记录数%d]",recordcnt)); // 检查临时目录 ret = CheckDownloadPath(devid,filepath); if(ret > 0) { ACE_DEBUG((LM_DEBUG,"本地文件已经存在!")); // 文件已经存在,可能正在使用中 return ret; } else if(ret < 0) { // 创建目录失败 ACE_DEBUG((LM_ERROR,"创建目录失败")); return ret; } if(file_flag != FILE_CLOSE) { ACE_DEBUG((LM_DEBUG,"CCU正在使用该记录文件 ... ")); return -1; } // 没有记录 if(recordcnt <= 0) return 1; } else { return -1; } KSGDeviceNode* node = KsgGetGateway()->GetDeviceManager()->FindDevice(devid); if(node == NULL) return -1; // 下载文件 //ftp_netbuf* control; off_t rec_file_len = 0; int retries = 3; std::string ipstr = "ftp://" + node->GetDevAddr().GetConnect() + ":"; ipstr += hd_ccu_ftp_port; ipstr += "/" + datafile; curl = NULL; ACE_OS::sprintf(user_pwd,"%s:%s",hd_ccu_ftp_user_name.c_str(),hd_ccu_ftp_pswd.c_str()); try { // 采集流水 do { ret = -1; record_file myfile = {filepath.c_str(),NULL}; ACE_DEBUG((LM_DEBUG,"开始下载记录文件")); curl = curl_easy_init(); if(!curl) { ACE_DEBUG((LM_ERROR,"申请 CURL 连接资源失败!")); continue; } ACE_DEBUG((LM_DEBUG,"下载CCU URL[%s]",ipstr.c_str())); curl_easy_setopt(curl,CURLOPT_URL,ipstr.c_str()); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, HDCCUNoticeHandler::ftp_writer); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &myfile); //#ifdef _DEBUG curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE); //curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, HDCCUNoticeHandler::ftp_curl_debug); //#endif curl_easy_setopt(curl, CURLOPT_FTP_FILEMETHOD, CURLFTPMETHOD_NOCWD); curl_easy_setopt(curl, CURLOPT_FTP_USE_EPSV, 0); curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, ftp_msg); // 设置登陆用户名与密码 curl_easy_setopt(curl ,CURLOPT_USERPWD,user_pwd); // 设置连接超时时间为 5 秒 curl_easy_setopt(curl, CURLOPT_TIMEOUT,5); // 设置 FTP 超时时间为 3 秒 curl_easy_setopt(curl, CURLOPT_FTP_RESPONSE_TIMEOUT,3); res = curl_easy_perform(curl); curl_easy_cleanup(curl); curl = NULL; if(myfile.filp) { ACE_DEBUG((LM_DEBUG,"关闭文件句柄!")); ACE_OS::fclose(myfile.filp); myfile.filp = NULL; } if(CURLE_OK != res) { ACE_DEBUG((LM_ERROR,"下载CCU[%s]记录文件[%s]失败,CURL返回错误[%d][%s]" ,node->GetPhyId().c_str(),datafile.c_str(),res,ftp_msg)); } else if((rec_file_len = ACE_OS::filesize(filepath.c_str())) == 0) { // 下载的文件为空,删除之 ACE_DEBUG((LM_NOTICE,"下载的记录文件为空,但CCU记录文件不为空!")); if(ACE_OS::unlink(filepath.c_str())) { ACE_DEBUG((LM_ERROR,"删除空记录文件失败!")); } ret = -2; } else { // 如果文件大小有问题,只要足够大就先采下来. if(rec_file_len / HDRecordFileReader::RECORD_BUF_LEN < recordcnt) { ACE_DEBUG((LM_ERROR,"下载记录文件大小与请求包中文件大小不一致except[%d]received[%d]" ,recordcnt,rec_file_len / HDRecordFileReader::RECORD_BUF_LEN)); // 重新下载记录文件,直到成功为止 ACE_OS::unlink(filepath.c_str()); // 等待5 秒重试 KSGThreadUtil::Sleep(5000); } else { ACE_DEBUG((LM_DEBUG,"下载CCU记录文件成功!")); ret = 0; } } } while(--retries > 0 && ret != 0); if(retries <= 0 && ret != 0) { ACE_DEBUG((LM_ERROR,"下载CCU记录文件错误,文件大小与请求包中大小不一致")); } } catch(...) { // 下载文件失败,返回 ERROR return -1; } return ret; }