| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475 |
- // GpioClassImpl_DistrBusUSB.cpp :
- //
- #include <cstdio>
- #include "GpioClassImpl_DistrBusUSB.h"
- CINIAccess g_iniAccess;
- LOG_EXTERN()
- int getFileVer(char* sFile, short &ch1, short &ch2)
- {
- ch1 = 0;
- ch2 = 0;
- char* pFind = strstr(sFile, ".so");
- char* pTemp = pFind;
- while(pTemp)
- {
- pFind = pTemp;
- pTemp = strstr(pFind + 3, ".so");
- }
- if(pFind == nullptr) return 0;
- pTemp = pFind - 1;
- while(isdigit(*pTemp) && pTemp > sFile) pTemp--;
- if(*pTemp == '.')
- ch2 = atoi(pTemp + 1);
- pTemp--;
- while(isdigit(*pTemp) && pTemp > sFile) pTemp--;
- if(*pTemp == '.')
- ch1 = atoi(pTemp + 1);
- return 1;
- }
- GpioClassImpl::GpioClassImpl():
- m_bDevOpen(false),
- m_bIsUSBOn(false),
- m_nUSBDelay(0),
- m_hDistrBusUSB(NULL),
- PDD_DIO_CATEGORY(40000),
- PDD_DIO_QUERY(CtlCode( PDD_DIO_CATEGORY, (0x900 + 6), Buffered, Any )),
- PDD_DIO_READ(CtlCode( PDD_DIO_CATEGORY, (0x900 + 7), Buffered, Any )),
- PDD_DIO_WRITE_INT(CtlCode( PDD_DIO_CATEGORY, (0x900 + 10), Buffered, Any ))
- {
- LOG_FUNCTION();
- int nRet;
- memset(m_sIniPath, 0, sizeof(m_sIniPath));
- // strcpy(m_sIniPath, "data\\home\\keba\\DevAdapter\\CK-VTM-E1000.INI");
- // CFileDir::GetFilePathName(
- // getpid(), "INI", "CK-VTM-E1000.INI",
- // m_sIniPath, sizeof(m_sIniPath));
- char sPath[256]{0};
- GetCurModulePath(sPath, nullptr);
- memcpy(m_sIniPath,sPath,sizeof (sPath));
- sprintf(m_sIniPath, "%s/%s", m_sIniPath, "keba/Adapter_L.keba.INI");
- LogM("GPIO的配置文件=%s",m_sIniPath);
- m_nUSBDelay = g_iniAccess.GetPrivateProfileInt("Gpio", "USBDELAY", 3000, m_sIniPath);
- KEBA_DevCtrl=new CKEBA_DevCtrl();
- m_wDevStatus = DEVICE_STATUS_NOT_READY;
- }
- GpioClassImpl::~GpioClassImpl()
- {
- LOG_FUNCTION();
- DevClose();
- }
- //DeviceBaseClass
- ErrorCodeEnum GpioClassImpl::GetDevCategory(DevCategoryInfo &devCategory)
- {
- LOG_FUNCTION();
- LogM("GpioClassImpl::Entry->Gpio GetDevCategory()!");
- memset(devCategory.szType, 0, MAX_DEV_TYPE_LEN);
- memset(devCategory.szModel, 0, MAX_DEV_MODEL_LEN);
- memset(devCategory.szVendor, 0, MAX_DEV_VENDOR_LEN);
- char sPath[256], sFile[128] = {0};
- GetCurModulePath(sPath, sFile);
- short v1,v2;
- getFileVer(sFile, v1, v2);
- devCategory.version.wMajor = v1;
- devCategory.version.wMinor = v2;
- devCategory.version.wRevision = 0xFFFF;
- devCategory.version.wBuild = 1;
- // memcpy(devCategory.szType, "CMB.GPIO", strlen("CMB.GPIO"));
- memcpy(devCategory.szType, "PVER=KEBA#MID=DistrBusUSB", strlen("PVER=KEBA#MID=DistrBusUSB"));
- // memcpy(devCategory.szModel, "Distribution_Bus_USB", strlen("Distribution_Bus_USB"));
- memcpy(devCategory.szModel, "FWID=V1.0", strlen("FWID=V1.0"));
- memcpy(devCategory.szVendor, "KEBA", strlen("KEBA"));
- devCategory.eState = (DevStateEnum)m_wDevStatus;
- // devCategory.version.wMajor = (WORD)portio_read_version();
- LogM("DevCategoryInfo() outputparameter: szType:%s, szModel:%s,szVendor:%s, eState:%d", devCategory.szType, devCategory.szModel, devCategory.szVendor, devCategory.eState);
- LogM("DevCategoryInfo() outputparameter: wMajor:%d, wMinor:%d,wRevision:%d, wBuild:%d", devCategory.version.wMajor, devCategory.version.wMinor, devCategory.version.wRevision, devCategory.version.wBuild);
- LogM("DevCategoryInfo() OK!");
- return Error_Succeed;
- }
- ErrorCodeEnum GpioClassImpl::Reset()
- {
- LOG_FUNCTION();
- LogM("GpioClassImpl::Entry->Gpio Reset()!");
- BYTE inBuffer[8]={0};
- BYTE outBuffer[8]={0};
- ULONG bytesReturned = 0;
- if(!m_bDevOpen)
- {
- GpioInitParam initParam;
- initParam.dwPortNum = m_dwPortNum;
- for(int i=0; i<MIN(MAX_PORT_NUM, m_dwPortNum); i++)
- {
- initParam.dir[i] = m_dir[i];
- }
- if(DevOpen(initParam))
- {
- SaveErrorInfo("Reset(): returnError_DevNotAvailable",Error_DevNotAvailable );
- LogM("Reset() return Error_DevNotAvailable");
- m_wDevStatus = DEVICE_STATUS_NOT_READY;
- return Error_DevNotAvailable;
- }
- }
- if(KEBA_DevCtrl->Reset() != FALSE)
- {
- LogM("Reset() OK!");
- m_wDevStatus = DEVICE_STATUS_NORMAL;
- return Error_Succeed;
- }
- else
- {
- LogM("Reset() return Error_DevNotAvailable");
- m_wDevStatus = DEVICE_STATUS_FAULT;
- return Error_DevNotAvailable;
- }
- }
- ErrorCodeEnum GpioClassImpl::DevClose()
- {
- LOG_FUNCTION();
- m_wDevStatus = DEVICE_STATUS_NOT_READY;
- LogM("DevClose() ");
- if(!m_bDevOpen)
- {
- LogM("DevClose(): Dev is not open.");
- LogM("DevClose() OK!");
- return Error_Succeed;
- }
- KEBA_DevCtrl->CloseDevice();
- m_bDevOpen = false;
- LogM("DevClose() OK!");
- return Error_Succeed;
- }
- ErrorCodeEnum GpioClassImpl::GetLastErr(DevErrorInfo &devErrInfo)
- {
- LOG_FUNCTION();
- LogM("GetLastErr() ");
- devErrInfo.dwErrMsgLen = strlen(m_szErrMsg);
- memset(devErrInfo.szErrMsg, 0, MAX_DEV_ERROR_MSG_LEN);
- memcpy(devErrInfo.szErrMsg, m_szErrMsg, MIN(devErrInfo.dwErrMsgLen, MAX_DEV_ERROR_MSG_LEN));
- LogM("GetLastErr() return: devErrInfo.dwErrMsgLen=%d, devErrInfo.szErrMsg:%s", devErrInfo.dwErrMsgLen, devErrInfo.szErrMsg);
- LogM("GetLastErr() OK!");
- return Error_Succeed;
- }
- //GpioClass
- //
- // Device initialization.
- // Configure port input/output direction.
- //
- ErrorCodeEnum GpioClassImpl::DevOpen(GpioInitParam initParam)
- {
- LOG_FUNCTION();
- //DevOpen():dwPortNum:3, dir:1 1 0 204
- LogM("DevOpen() input parameter:dwPortNum:%d, dir:%d %d %d %d", initParam.dwPortNum, initParam.dir[0], initParam.dir[1],initParam.dir[2],initParam.dir[3] );
- m_dwPortNum = initParam.dwPortNum;
- m_outputPortTotal = 0;
- for(int i = 0; i < MAX_PORT_NUM; i++)
- {
- m_dir[i] = initParam.dir[i];
- if(m_dir[i]==1)
- {
- m_outputPortTotal ++;
- LogM("DevOpen() m_dir[%d] = %d", i,m_dir[i]);
- }
- }
- if(m_bDevOpen)
- {
- m_wDevStatus = DEVICE_STATUS_NORMAL;
- LogM("DevOpen() Dev is open already. Return Error_Succeed;");
- return Error_Succeed;
- }
- DevClose();
- BOOL bRet = KEBA_DevCtrl->InitDevice();
- if (bRet == FALSE)
- {
- LogM("DevOpen()->GetFileType() failed. return Error_DevNotAvailable;");
- DevClose();
- m_wDevStatus = DEVICE_STATUS_NOT_READY;
- return Error_DevNotAvailable;
- }
- m_bDevOpen = true;
- m_wDevStatus = DEVICE_STATUS_NORMAL;
- return Error_Succeed;
- /*
- if(Error_Succeed == LightTurnOn())
- {
- //m_pLog->Print("DevOpen(): OK!" );
- m_wDevStatus = DEVICE_STATUS_NORMAL;
- return Error_Succeed;
- }
- else
- {
- //m_pLog->Print("DevOpen() return Error_DevNotAvailable;");
- m_wDevStatus = DEVICE_STATUS_FAULT;
- return Error_DevNotAvailable;
- }
- */
- }
- //
- // Set ports output.
- // Arguments:
- // - dwPort:port serial number,0~MAX_PORT_NUM-1
- // - btData:output value
- // bit value 1/0 means voltage high or low
- // ex.dwPort=0 btData=10001010 means set port 0's the 2nd,4th,8th pin output high
- //
- ErrorCodeEnum GpioClassImpl::WritePort(DWORD dwPort, BYTE btData)
- {
- LOG_FUNCTION();
- //zouchuang rang jia 50ms
- usleep(50*1000);
- LogM("WritePort()input parameter:dwPort:%d, btData:0x%02x", dwPort, btData);
- if(dwPort == 0)
- {
- if((btData & 0x02)== 0x02)
- m_bIsUSBOn = true;
- else if (m_bIsUSBOn)
- {
- usleep(m_nUSBDelay*1000);
- m_bIsUSBOn = false;
- }
- }
- if(!m_bDevOpen)
- {
- LogM("WritePort() return Error_DevNotAvailable");
- m_wDevStatus = DEVICE_STATUS_NOT_READY;
- return Error_DevNotAvailable;
- }
- if(!m_dir[dwPort])
- {
- LogM("WritePort() input parameter is invalied:Port%d is not output port. return Error_Param",dwPort);
- return Error_Param;
- }
- BYTE inBuffer[8]={0};
- BYTE outBuffer[8]={0};
- ULONG bytesReturned = 0;
- char outputPin[20] = {0};
- int iOutputsNo = 0, iOutputs = 0, iCnt = 0;
- iOutputsNo = 0; //PinNo need change status, will put into buffer[0]~[3]
- iOutputs = 0; //PinStatus need change,will put into buffer[4]~[7]
- BOOL bRet = FALSE;
- bool bIsOn[8] = {FALSE};
- CHAR cErrmsg[128] = {0};
- WORD wOutputNobuf[8] = {0};
- int iPin, iOutputNo;
- memset(inBuffer, 0x00, 8);
- for(int i = 1; i<=8; i++)
- {
- iPin = i + dwPort*8;
- sprintf(outputPin, "OUTPUT_PIN%d", iPin);
- iOutputNo = g_iniAccess.GetPrivateProfileInt("Gpio", outputPin, 0, m_sIniPath);
- if ( !iOutputNo ) continue;
- wOutputNobuf[iCnt] = (WORD)iOutputNo;
- if(btData & (1<<(i-1)))
- {
- bIsOn[iCnt] = TRUE;
- }
- else
- {
- bIsOn[iCnt] = FALSE;
- }
- iCnt ++;
- }
- bRet = KEBA_DevCtrl->SetGuidLight(wOutputNobuf, bIsOn, iCnt);
- if (bRet == FALSE)
- {
- sprintf(cErrmsg, "SetGuidLight() Execute Fail");
- SaveErrorInfo(cErrmsg, Error_DevNotAvailable);
- LogM("WritePort()->SetGuidLight(): %s",cErrmsg);
- m_wDevStatus = DEVICE_STATUS_FAULT;
- return Error_DevNotAvailable;
- }
- LogM("WritePort() OK!");
- m_wDevStatus = DEVICE_STATUS_NORMAL;
- return Error_Succeed;
- }
- //
- // Get port input.
- // Arguments:
- // - dwPort:port serial number,0~MAX_PORT_NUM-1
- // - btStatus:input value
- // ex.dwPort=0 btStatus=10001010 means port 0's the 2nd,4th,8th pin with high level
- //
- ErrorCodeEnum GpioClassImpl::ReadPort(DWORD dwPort,BYTE &btStatus)
- {
- LOG_FUNCTION();
- LogM("ReadPort()input parameter:dwPort:%d", dwPort);
- if(!m_bDevOpen)
- {
- SaveErrorInfo("ReadPort(): returnError_DevNotAvailable",Error_DevNotAvailable );
- LogM("ReadPort() return Error_DevNotAvailable");
- m_wDevStatus = DEVICE_STATUS_NOT_READY;
- return Error_DevNotAvailable;
- }
- BYTE inBuffer[8]={0};
- BYTE outBuffer[8]={0};
- ULONG bytesReturned = 0;
- int iPin = 0, iInputNo = 0;
- int portno = 0;
- BOOL bRet = FALSE, bFailFlag = FALSE;
- WORD wStatus = 0;
- CHAR cErrmsg[128] = {0};
- memset(inBuffer, 0x00, 8);
- btStatus = 0x00;
-
- for(int i = 0; i < m_dwPortNum; i ++)
- {
- if(!m_dir[i])
- {
- portno ++;
- if(i == dwPort)
- {
- portno --;
- break;
- }
- }
- }
- char inputPin[20] = {0};
- for(int i = 1; i<=8; i++ )
- {
- iPin = i + portno*8;
- if(m_dir[dwPort])
- {
- sprintf(inputPin, "OUTPUT_PIN%d", iPin%32);
- iInputNo = 32 + g_iniAccess.GetPrivateProfileInt("Gpio", inputPin, 0, m_sIniPath);
- bRet = KEBA_DevCtrl->getChannelStatus(TRUE, iInputNo, wStatus);
- }
- else
- {
- sprintf(inputPin, "INPUT_PIN%d", iPin%32);
- iInputNo = g_iniAccess.GetPrivateProfileInt("Gpio", inputPin, 0, m_sIniPath);
- bRet = KEBA_DevCtrl->getChannelStatus(FALSE, iInputNo, wStatus);
- }
- if (bRet == FALSE)
- {
- sprintf(cErrmsg, "getChannelStatus(channel=%d) Execute Fail", iInputNo);
- SaveErrorInfo(cErrmsg, Error_DevNotAvailable);
- LogM("WritePort()->SetGuidLight(): %s",cErrmsg);
- bFailFlag = TRUE;
- }
- if(wStatus)
- {
- btStatus |= 1<<(i-1);
- }
- }
- if (bFailFlag != FALSE)
- {
- m_wDevStatus = DEVICE_STATUS_FAULT;
- return Error_DevNotAvailable;
- }
- LogM("ReadPort()output parameter:btStatus:0x%02x", btStatus);
- LogM("ReadPort() OK!");
- m_wDevStatus = DEVICE_STATUS_NORMAL;
- return Error_Succeed;
- }
- //Private
- void GpioClassImpl::SaveErrorInfo(CHAR* errMsg, int errCode)
- {
- LOG_FUNCTION();
- //memset(m_szErrMsg, 0, MAX_DEV_ERR_MSG);
- LogM("SaveErrorInfo()input parameter:errMsg:%s, errCode:%d", errMsg, errCode);
- memset(m_szErrMsg, 0, MAX_DEV_ERROR_MSG_LEN);
- sprintf(m_szErrMsg, "%s[%d]", errMsg, errCode);
- LogM("SaveErrorInfo() OK!");
- }
- ErrorCodeEnum GpioClassImpl::LightTurnOn()
- {
- LOG_FUNCTION();
- int iOutputNo = 0;
- char outputPin[20] = {0};
- if(!m_bDevOpen)
- {
- LogM("LightTurnOn() return Error_DevNotAvailable");
- m_wDevStatus = DEVICE_STATUS_NOT_READY;
- return Error_DevNotAvailable;
- }
- BYTE inBuffer[8]={0};
- BYTE outBuffer[8]={0};
- ULONG bytesReturned = 0;
- BOOL bRet = FALSE;
- CHAR cErrmsg[128] = {0};
- int iCnt = 0;
- WORD wOutputNoBuf[16] = {0};
- bool bOnStat[16] = {1};
- for (int i = 1; i < 17; i++)
- {
- sprintf(outputPin, "OUTPUT_PIN%d", i);
- iOutputNo = g_iniAccess.GetPrivateProfileInt("Gpio", outputPin, 0, m_sIniPath);
- if ( !iOutputNo ) continue;
- wOutputNoBuf[iCnt] = iOutputNo;
- iCnt ++;
- }
- bRet = KEBA_DevCtrl->SetGuidLight(wOutputNoBuf, bOnStat, 16);
- if (bRet == FALSE)
- {
- sprintf(cErrmsg, "SetGuidLight() Execute Fail");
- SaveErrorInfo(cErrmsg, Error_DevNotAvailable);
- LogM("WritePort()->SetGuidLight(): %s",cErrmsg);
- m_wDevStatus = DEVICE_STATUS_FAULT;
- return Error_DevNotAvailable;
- }
- LogM("LightTurnOn() OK!");
- m_wDevStatus = DEVICE_STATUS_NORMAL;
- return Error_Succeed;
- }
- void GpioClassImpl::intToByte(int i, BYTE* buf, UINT uOffset)
- {
- buf[0 + uOffset] = (BYTE) (0xff & i);
- buf[1 + uOffset] = (BYTE) ((0xff00 & i) >> 8);
- buf[2 + uOffset] = (BYTE) ((0xff0000 & i) >> 16);
- buf[3 + uOffset] = (BYTE) ((0xff000000 & i) >> 24);
- }
- int GpioClassImpl::bytesToInt(BYTE* bytes, UINT uOffset)
- {
- int num = bytes[0 + uOffset] & 0xFF;
- num |= ((bytes[1 + uOffset] << 8) & 0xFF00);
- num |= ((bytes[2 + uOffset] << 16) & 0xFF0000);
- num |= ((bytes[3 + uOffset] << 24) & 0xFF000000);
- return num;
- }
|