GpioClassImpl_DistrBusUSB.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // GpioClassImpl_DistrBusUSB.cpp :
  2. //
  3. #include <cstdio>
  4. #include "GpioClassImpl_DistrBusUSB.h"
  5. CINIAccess g_iniAccess;
  6. LOG_EXTERN()
  7. int getFileVer(char* sFile, short &ch1, short &ch2)
  8. {
  9. ch1 = 0;
  10. ch2 = 0;
  11. char* pFind = strstr(sFile, ".so");
  12. char* pTemp = pFind;
  13. while(pTemp)
  14. {
  15. pFind = pTemp;
  16. pTemp = strstr(pFind + 3, ".so");
  17. }
  18. if(pFind == nullptr) return 0;
  19. pTemp = pFind - 1;
  20. while(isdigit(*pTemp) && pTemp > sFile) pTemp--;
  21. if(*pTemp == '.')
  22. ch2 = atoi(pTemp + 1);
  23. pTemp--;
  24. while(isdigit(*pTemp) && pTemp > sFile) pTemp--;
  25. if(*pTemp == '.')
  26. ch1 = atoi(pTemp + 1);
  27. return 1;
  28. }
  29. GpioClassImpl::GpioClassImpl():
  30. m_bDevOpen(false),
  31. m_bIsUSBOn(false),
  32. m_nUSBDelay(0),
  33. m_hDistrBusUSB(NULL),
  34. PDD_DIO_CATEGORY(40000),
  35. PDD_DIO_QUERY(CtlCode( PDD_DIO_CATEGORY, (0x900 + 6), Buffered, Any )),
  36. PDD_DIO_READ(CtlCode( PDD_DIO_CATEGORY, (0x900 + 7), Buffered, Any )),
  37. PDD_DIO_WRITE_INT(CtlCode( PDD_DIO_CATEGORY, (0x900 + 10), Buffered, Any ))
  38. {
  39. LOG_FUNCTION();
  40. int nRet;
  41. memset(m_sIniPath, 0, sizeof(m_sIniPath));
  42. // strcpy(m_sIniPath, "data\\home\\keba\\DevAdapter\\CK-VTM-E1000.INI");
  43. // CFileDir::GetFilePathName(
  44. // getpid(), "INI", "CK-VTM-E1000.INI",
  45. // m_sIniPath, sizeof(m_sIniPath));
  46. char sPath[256]{0};
  47. GetCurModulePath(sPath, nullptr);
  48. memcpy(m_sIniPath,sPath,sizeof (sPath));
  49. sprintf(m_sIniPath, "%s/%s", m_sIniPath, "keba/Adapter_L.keba.INI");
  50. LogM("GPIO的配置文件=%s",m_sIniPath);
  51. m_nUSBDelay = g_iniAccess.GetPrivateProfileInt("Gpio", "USBDELAY", 3000, m_sIniPath);
  52. KEBA_DevCtrl=new CKEBA_DevCtrl();
  53. m_wDevStatus = DEVICE_STATUS_NOT_READY;
  54. }
  55. GpioClassImpl::~GpioClassImpl()
  56. {
  57. LOG_FUNCTION();
  58. DevClose();
  59. }
  60. //DeviceBaseClass
  61. ErrorCodeEnum GpioClassImpl::GetDevCategory(DevCategoryInfo &devCategory)
  62. {
  63. LOG_FUNCTION();
  64. LogM("GpioClassImpl::Entry->Gpio GetDevCategory()!");
  65. memset(devCategory.szType, 0, MAX_DEV_TYPE_LEN);
  66. memset(devCategory.szModel, 0, MAX_DEV_MODEL_LEN);
  67. memset(devCategory.szVendor, 0, MAX_DEV_VENDOR_LEN);
  68. char sPath[256], sFile[128] = {0};
  69. GetCurModulePath(sPath, sFile);
  70. short v1,v2;
  71. getFileVer(sFile, v1, v2);
  72. devCategory.version.wMajor = v1;
  73. devCategory.version.wMinor = v2;
  74. devCategory.version.wRevision = 0xFFFF;
  75. devCategory.version.wBuild = 1;
  76. // memcpy(devCategory.szType, "CMB.GPIO", strlen("CMB.GPIO"));
  77. memcpy(devCategory.szType, "PVER=KEBA#MID=DistrBusUSB", strlen("PVER=KEBA#MID=DistrBusUSB"));
  78. // memcpy(devCategory.szModel, "Distribution_Bus_USB", strlen("Distribution_Bus_USB"));
  79. memcpy(devCategory.szModel, "FWID=V1.0", strlen("FWID=V1.0"));
  80. memcpy(devCategory.szVendor, "KEBA", strlen("KEBA"));
  81. devCategory.eState = (DevStateEnum)m_wDevStatus;
  82. // devCategory.version.wMajor = (WORD)portio_read_version();
  83. LogM("DevCategoryInfo() outputparameter: szType:%s, szModel:%s,szVendor:%s, eState:%d", devCategory.szType, devCategory.szModel, devCategory.szVendor, devCategory.eState);
  84. LogM("DevCategoryInfo() outputparameter: wMajor:%d, wMinor:%d,wRevision:%d, wBuild:%d", devCategory.version.wMajor, devCategory.version.wMinor, devCategory.version.wRevision, devCategory.version.wBuild);
  85. LogM("DevCategoryInfo() OK!");
  86. return Error_Succeed;
  87. }
  88. ErrorCodeEnum GpioClassImpl::Reset()
  89. {
  90. LOG_FUNCTION();
  91. LogM("GpioClassImpl::Entry->Gpio Reset()!");
  92. BYTE inBuffer[8]={0};
  93. BYTE outBuffer[8]={0};
  94. ULONG bytesReturned = 0;
  95. if(!m_bDevOpen)
  96. {
  97. GpioInitParam initParam;
  98. initParam.dwPortNum = m_dwPortNum;
  99. for(int i=0; i<MIN(MAX_PORT_NUM, m_dwPortNum); i++)
  100. {
  101. initParam.dir[i] = m_dir[i];
  102. }
  103. if(DevOpen(initParam))
  104. {
  105. SaveErrorInfo("Reset(): returnError_DevNotAvailable",Error_DevNotAvailable );
  106. LogM("Reset() return Error_DevNotAvailable");
  107. m_wDevStatus = DEVICE_STATUS_NOT_READY;
  108. return Error_DevNotAvailable;
  109. }
  110. }
  111. if(KEBA_DevCtrl->Reset() != FALSE)
  112. {
  113. LogM("Reset() OK!");
  114. m_wDevStatus = DEVICE_STATUS_NORMAL;
  115. return Error_Succeed;
  116. }
  117. else
  118. {
  119. LogM("Reset() return Error_DevNotAvailable");
  120. m_wDevStatus = DEVICE_STATUS_FAULT;
  121. return Error_DevNotAvailable;
  122. }
  123. }
  124. ErrorCodeEnum GpioClassImpl::DevClose()
  125. {
  126. LOG_FUNCTION();
  127. m_wDevStatus = DEVICE_STATUS_NOT_READY;
  128. LogM("DevClose() ");
  129. if(!m_bDevOpen)
  130. {
  131. LogM("DevClose(): Dev is not open.");
  132. LogM("DevClose() OK!");
  133. return Error_Succeed;
  134. }
  135. KEBA_DevCtrl->CloseDevice();
  136. m_bDevOpen = false;
  137. LogM("DevClose() OK!");
  138. return Error_Succeed;
  139. }
  140. ErrorCodeEnum GpioClassImpl::GetLastErr(DevErrorInfo &devErrInfo)
  141. {
  142. LOG_FUNCTION();
  143. LogM("GetLastErr() ");
  144. devErrInfo.dwErrMsgLen = strlen(m_szErrMsg);
  145. memset(devErrInfo.szErrMsg, 0, MAX_DEV_ERROR_MSG_LEN);
  146. memcpy(devErrInfo.szErrMsg, m_szErrMsg, MIN(devErrInfo.dwErrMsgLen, MAX_DEV_ERROR_MSG_LEN));
  147. LogM("GetLastErr() return: devErrInfo.dwErrMsgLen=%d, devErrInfo.szErrMsg:%s", devErrInfo.dwErrMsgLen, devErrInfo.szErrMsg);
  148. LogM("GetLastErr() OK!");
  149. return Error_Succeed;
  150. }
  151. //GpioClass
  152. //
  153. // Device initialization.
  154. // Configure port input/output direction.
  155. //
  156. ErrorCodeEnum GpioClassImpl::DevOpen(GpioInitParam initParam)
  157. {
  158. LOG_FUNCTION();
  159. //DevOpen():dwPortNum:3, dir:1 1 0 204
  160. 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] );
  161. m_dwPortNum = initParam.dwPortNum;
  162. m_outputPortTotal = 0;
  163. for(int i = 0; i < MAX_PORT_NUM; i++)
  164. {
  165. m_dir[i] = initParam.dir[i];
  166. if(m_dir[i]==1)
  167. {
  168. m_outputPortTotal ++;
  169. LogM("DevOpen() m_dir[%d] = %d", i,m_dir[i]);
  170. }
  171. }
  172. if(m_bDevOpen)
  173. {
  174. m_wDevStatus = DEVICE_STATUS_NORMAL;
  175. LogM("DevOpen() Dev is open already. Return Error_Succeed;");
  176. return Error_Succeed;
  177. }
  178. DevClose();
  179. BOOL bRet = KEBA_DevCtrl->InitDevice();
  180. if (bRet == FALSE)
  181. {
  182. LogM("DevOpen()->GetFileType() failed. return Error_DevNotAvailable;");
  183. DevClose();
  184. m_wDevStatus = DEVICE_STATUS_NOT_READY;
  185. return Error_DevNotAvailable;
  186. }
  187. m_bDevOpen = true;
  188. m_wDevStatus = DEVICE_STATUS_NORMAL;
  189. return Error_Succeed;
  190. /*
  191. if(Error_Succeed == LightTurnOn())
  192. {
  193. //m_pLog->Print("DevOpen(): OK!" );
  194. m_wDevStatus = DEVICE_STATUS_NORMAL;
  195. return Error_Succeed;
  196. }
  197. else
  198. {
  199. //m_pLog->Print("DevOpen() return Error_DevNotAvailable;");
  200. m_wDevStatus = DEVICE_STATUS_FAULT;
  201. return Error_DevNotAvailable;
  202. }
  203. */
  204. }
  205. //
  206. // Set ports output.
  207. // Arguments:
  208. // - dwPort:port serial number,0~MAX_PORT_NUM-1
  209. // - btData:output value
  210. // bit value 1/0 means voltage high or low
  211. // ex.dwPort=0 btData=10001010 means set port 0's the 2nd,4th,8th pin output high
  212. //
  213. ErrorCodeEnum GpioClassImpl::WritePort(DWORD dwPort, BYTE btData)
  214. {
  215. LOG_FUNCTION();
  216. //zouchuang rang jia 50ms
  217. usleep(50*1000);
  218. LogM("WritePort()input parameter:dwPort:%d, btData:0x%02x", dwPort, btData);
  219. if(dwPort == 0)
  220. {
  221. if((btData & 0x02)== 0x02)
  222. m_bIsUSBOn = true;
  223. else if (m_bIsUSBOn)
  224. {
  225. usleep(m_nUSBDelay*1000);
  226. m_bIsUSBOn = false;
  227. }
  228. }
  229. if(!m_bDevOpen)
  230. {
  231. LogM("WritePort() return Error_DevNotAvailable");
  232. m_wDevStatus = DEVICE_STATUS_NOT_READY;
  233. return Error_DevNotAvailable;
  234. }
  235. if(!m_dir[dwPort])
  236. {
  237. LogM("WritePort() input parameter is invalied:Port%d is not output port. return Error_Param",dwPort);
  238. return Error_Param;
  239. }
  240. BYTE inBuffer[8]={0};
  241. BYTE outBuffer[8]={0};
  242. ULONG bytesReturned = 0;
  243. char outputPin[20] = {0};
  244. int iOutputsNo = 0, iOutputs = 0, iCnt = 0;
  245. iOutputsNo = 0; //PinNo need change status, will put into buffer[0]~[3]
  246. iOutputs = 0; //PinStatus need change,will put into buffer[4]~[7]
  247. BOOL bRet = FALSE;
  248. bool bIsOn[8] = {FALSE};
  249. CHAR cErrmsg[128] = {0};
  250. WORD wOutputNobuf[8] = {0};
  251. int iPin, iOutputNo;
  252. memset(inBuffer, 0x00, 8);
  253. for(int i = 1; i<=8; i++)
  254. {
  255. iPin = i + dwPort*8;
  256. sprintf(outputPin, "OUTPUT_PIN%d", iPin);
  257. iOutputNo = g_iniAccess.GetPrivateProfileInt("Gpio", outputPin, 0, m_sIniPath);
  258. if ( !iOutputNo ) continue;
  259. wOutputNobuf[iCnt] = (WORD)iOutputNo;
  260. if(btData & (1<<(i-1)))
  261. {
  262. bIsOn[iCnt] = TRUE;
  263. }
  264. else
  265. {
  266. bIsOn[iCnt] = FALSE;
  267. }
  268. iCnt ++;
  269. }
  270. bRet = KEBA_DevCtrl->SetGuidLight(wOutputNobuf, bIsOn, iCnt);
  271. if (bRet == FALSE)
  272. {
  273. sprintf(cErrmsg, "SetGuidLight() Execute Fail");
  274. SaveErrorInfo(cErrmsg, Error_DevNotAvailable);
  275. LogM("WritePort()->SetGuidLight(): %s",cErrmsg);
  276. m_wDevStatus = DEVICE_STATUS_FAULT;
  277. return Error_DevNotAvailable;
  278. }
  279. LogM("WritePort() OK!");
  280. m_wDevStatus = DEVICE_STATUS_NORMAL;
  281. return Error_Succeed;
  282. }
  283. //
  284. // Get port input.
  285. // Arguments:
  286. // - dwPort:port serial number,0~MAX_PORT_NUM-1
  287. // - btStatus:input value
  288. // ex.dwPort=0 btStatus=10001010 means port 0's the 2nd,4th,8th pin with high level
  289. //
  290. ErrorCodeEnum GpioClassImpl::ReadPort(DWORD dwPort,BYTE &btStatus)
  291. {
  292. LOG_FUNCTION();
  293. LogM("ReadPort()input parameter:dwPort:%d", dwPort);
  294. if(!m_bDevOpen)
  295. {
  296. SaveErrorInfo("ReadPort(): returnError_DevNotAvailable",Error_DevNotAvailable );
  297. LogM("ReadPort() return Error_DevNotAvailable");
  298. m_wDevStatus = DEVICE_STATUS_NOT_READY;
  299. return Error_DevNotAvailable;
  300. }
  301. BYTE inBuffer[8]={0};
  302. BYTE outBuffer[8]={0};
  303. ULONG bytesReturned = 0;
  304. int iPin = 0, iInputNo = 0;
  305. int portno = 0;
  306. BOOL bRet = FALSE, bFailFlag = FALSE;
  307. WORD wStatus = 0;
  308. CHAR cErrmsg[128] = {0};
  309. memset(inBuffer, 0x00, 8);
  310. btStatus = 0x00;
  311. for(int i = 0; i < m_dwPortNum; i ++)
  312. {
  313. if(!m_dir[i])
  314. {
  315. portno ++;
  316. if(i == dwPort)
  317. {
  318. portno --;
  319. break;
  320. }
  321. }
  322. }
  323. char inputPin[20] = {0};
  324. for(int i = 1; i<=8; i++ )
  325. {
  326. iPin = i + portno*8;
  327. if(m_dir[dwPort])
  328. {
  329. sprintf(inputPin, "OUTPUT_PIN%d", iPin%32);
  330. iInputNo = 32 + g_iniAccess.GetPrivateProfileInt("Gpio", inputPin, 0, m_sIniPath);
  331. bRet = KEBA_DevCtrl->getChannelStatus(TRUE, iInputNo, wStatus);
  332. }
  333. else
  334. {
  335. sprintf(inputPin, "INPUT_PIN%d", iPin%32);
  336. iInputNo = g_iniAccess.GetPrivateProfileInt("Gpio", inputPin, 0, m_sIniPath);
  337. bRet = KEBA_DevCtrl->getChannelStatus(FALSE, iInputNo, wStatus);
  338. }
  339. if (bRet == FALSE)
  340. {
  341. sprintf(cErrmsg, "getChannelStatus(channel=%d) Execute Fail", iInputNo);
  342. SaveErrorInfo(cErrmsg, Error_DevNotAvailable);
  343. LogM("WritePort()->SetGuidLight(): %s",cErrmsg);
  344. bFailFlag = TRUE;
  345. }
  346. if(wStatus)
  347. {
  348. btStatus |= 1<<(i-1);
  349. }
  350. }
  351. if (bFailFlag != FALSE)
  352. {
  353. m_wDevStatus = DEVICE_STATUS_FAULT;
  354. return Error_DevNotAvailable;
  355. }
  356. LogM("ReadPort()output parameter:btStatus:0x%02x", btStatus);
  357. LogM("ReadPort() OK!");
  358. m_wDevStatus = DEVICE_STATUS_NORMAL;
  359. return Error_Succeed;
  360. }
  361. //Private
  362. void GpioClassImpl::SaveErrorInfo(CHAR* errMsg, int errCode)
  363. {
  364. LOG_FUNCTION();
  365. //memset(m_szErrMsg, 0, MAX_DEV_ERR_MSG);
  366. LogM("SaveErrorInfo()input parameter:errMsg:%s, errCode:%d", errMsg, errCode);
  367. memset(m_szErrMsg, 0, MAX_DEV_ERROR_MSG_LEN);
  368. sprintf(m_szErrMsg, "%s[%d]", errMsg, errCode);
  369. LogM("SaveErrorInfo() OK!");
  370. }
  371. ErrorCodeEnum GpioClassImpl::LightTurnOn()
  372. {
  373. LOG_FUNCTION();
  374. int iOutputNo = 0;
  375. char outputPin[20] = {0};
  376. if(!m_bDevOpen)
  377. {
  378. LogM("LightTurnOn() return Error_DevNotAvailable");
  379. m_wDevStatus = DEVICE_STATUS_NOT_READY;
  380. return Error_DevNotAvailable;
  381. }
  382. BYTE inBuffer[8]={0};
  383. BYTE outBuffer[8]={0};
  384. ULONG bytesReturned = 0;
  385. BOOL bRet = FALSE;
  386. CHAR cErrmsg[128] = {0};
  387. int iCnt = 0;
  388. WORD wOutputNoBuf[16] = {0};
  389. bool bOnStat[16] = {1};
  390. for (int i = 1; i < 17; i++)
  391. {
  392. sprintf(outputPin, "OUTPUT_PIN%d", i);
  393. iOutputNo = g_iniAccess.GetPrivateProfileInt("Gpio", outputPin, 0, m_sIniPath);
  394. if ( !iOutputNo ) continue;
  395. wOutputNoBuf[iCnt] = iOutputNo;
  396. iCnt ++;
  397. }
  398. bRet = KEBA_DevCtrl->SetGuidLight(wOutputNoBuf, bOnStat, 16);
  399. if (bRet == FALSE)
  400. {
  401. sprintf(cErrmsg, "SetGuidLight() Execute Fail");
  402. SaveErrorInfo(cErrmsg, Error_DevNotAvailable);
  403. LogM("WritePort()->SetGuidLight(): %s",cErrmsg);
  404. m_wDevStatus = DEVICE_STATUS_FAULT;
  405. return Error_DevNotAvailable;
  406. }
  407. LogM("LightTurnOn() OK!");
  408. m_wDevStatus = DEVICE_STATUS_NORMAL;
  409. return Error_Succeed;
  410. }
  411. void GpioClassImpl::intToByte(int i, BYTE* buf, UINT uOffset)
  412. {
  413. buf[0 + uOffset] = (BYTE) (0xff & i);
  414. buf[1 + uOffset] = (BYTE) ((0xff00 & i) >> 8);
  415. buf[2 + uOffset] = (BYTE) ((0xff0000 & i) >> 16);
  416. buf[3 + uOffset] = (BYTE) ((0xff000000 & i) >> 24);
  417. }
  418. int GpioClassImpl::bytesToInt(BYTE* bytes, UINT uOffset)
  419. {
  420. int num = bytes[0 + uOffset] & 0xFF;
  421. num |= ((bytes[1 + uOffset] << 8) & 0xFF00);
  422. num |= ((bytes[2 + uOffset] << 16) & 0xFF0000);
  423. num |= ((bytes[3 + uOffset] << 24) & 0xFF000000);
  424. return num;
  425. }