| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- #include "watchdog_impl.h"
- #include<cstring>
- #include <cstdio>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <sys/ioctl.h>
- #include <linux/watchdog.h>
- #define WATCHDOG "/dev/watchdog"
- #define WATCHDOG0 "/dev/watchdog0"
- #define WATCHDOG1 "/dev/watchdog1"
- #include "log4vendor.h"
- int preset_timeout;
- int config_timeout;
- int timeleft;
- int testcycle = 0;
- int dummy;
- int devnum = 0;
- int wfd;
- int config_testcycle;
- bool flag = false;
- char stopmagic = 'V';
- WatchDogClassImpl::WatchDogClassImpl():m_mode(0)
- {
- }
- WatchDogClassImpl::~WatchDogClassImpl()
- {
- }
- ErrorCodeEnum WatchDogClassImpl::GetDevCategory(DevCategoryInfo &devCategory)
- {
- LOG4VTM(INFO,"enter : GetDevCategory");
-
- memset(&devCategory, 0, sizeof(devCategory));
- char l_cVender[20]={0};
- strcpy(l_cVender,"grg");
- strcpy(devCategory.szVendor,l_cVender);
- char l_acDevVer[20]={0};
- strcpy(l_acDevVer,"V=V1.0");
- strcpy(devCategory.szModel,l_acDevVer);
- //设备类型
- memset(devCategory.szType,0,sizeof(devCategory.szType));
- strcpy(devCategory.szType,"PVER=GPL");
- strcat(devCategory.szType,"#MID=");
- //设备状态 device status
- devCategory.eState = DEVICE_STATUS_NORMAL;
- int l_iMajor=4, l_iMinor=20, l_iRevision=65535, l_iBuild=1;
- //设备版本号 software version
- devCategory.version.wMajor = l_iMajor;
- devCategory.version.wMinor = l_iMinor;
- devCategory.version.wRevision = l_iRevision;
- devCategory.version.wBuild = l_iBuild;
- LOG4VTM(INFO,"Leave : GetDevCategory");
-
- return Error_Succeed;
- // ErrorCodeEnum err = Error_Succeed;
- // std::strcpy(devCategory.szModel, "szModel");
- // std::strcpy(devCategory.szType, "szCategory");
- // std::strcpy(devCategory.szVendor, "szVendor");
- // return err;
- }
- ErrorCodeEnum WatchDogClassImpl::DevOpen()
- {
-
- wfd = open(WATCHDOG0, O_RDWR);
- if (wfd == -1)
- {
- printf("Unable to open %s \n", WATCHDOG0);
- return Error_DevConnFailed;
- }
- LOG4VTM(INFO,"Close");
- if (write(wfd, &stopmagic, sizeof(stopmagic)) != sizeof(stopmagic))
- {
- printf("Watchdog magic stop write error \n");
- return Error_DevConnFailed;
- }
-
- close(wfd);
- return Error_Succeed;
- }
- ErrorCodeEnum WatchDogClassImpl::StartWatch(DWORD dwDelay, DWORD dwTimeout)
- {
- LOG4VTM(INFO,"enter : StartWatch");
- //判断此时是否已经打开了看门狗
- if (!flag)
- {
- wfd = open(WATCHDOG0, O_RDWR);
- if (wfd == -1)
- {
- printf("Unable to open %s \n", WATCHDOG0);
- return Error_DevConnFailed;
- }else
- {
- flag = true;
- }
- }
-
- config_timeout = dwTimeout;
- if (ioctl(wfd, WDIOC_SETTIMEOUT, &config_timeout) == -1)
- {
- printf("Watchdog settimeout ioctl error %s\n", strerror(errno));
- if (write(wfd, &stopmagic, sizeof(stopmagic)) != sizeof(stopmagic))
- {
- printf("Watchdog magic stop write error \n");
- }
- close(wfd);
- flag = false;
- return Error_DevConnFailed;
- }
- LOG4VTM(INFO,"leave : StartWatch");
- return Error_Succeed;
- }
- ErrorCodeEnum WatchDogClassImpl::StopWatch()
- {
- LOG4VTM(INFO,"Open");
- if (!flag)
- {
- wfd = open(WATCHDOG0, O_RDWR);
- if (wfd == -1)
- {
- printf("Unable to open %s \n", WATCHDOG0);
- return Error_DevConnFailed;
- }else
- {
- flag = true;
- }
- }
-
- LOG4VTM(INFO,"enter : StopWatch");
- if (write(wfd, &stopmagic, sizeof(stopmagic)) != sizeof(stopmagic))
- {
- printf("Watchdog magic stop write error \n");
- return Error_DevConnFailed;
- }
- close(wfd);
- flag = false;
- LOG4VTM(INFO,"leave : StopWatch");
- return Error_Succeed;
- }
- ErrorCodeEnum WatchDogClassImpl::RefreshDog()
- {
- LOG4VTM(INFO,"enter : RefreshDog");
- if (!flag)
- {
- wfd = open(WATCHDOG0, O_RDWR);
- if (wfd == -1)
- {
- printf("Unable to open %s \n", WATCHDOG0);
- return Error_DevConnFailed;
- }else
- {
- flag = true;
- }
- }
-
- if (ioctl(wfd, WDIOC_KEEPALIVE, &dummy) == -1)
- {
- printf("Watchdog keeplive ioctl error %s\n", strerror(errno));
- if (write(wfd, &stopmagic, sizeof(stopmagic)) != sizeof(stopmagic))
- {
- printf("Watchdog magic stop write error \n");
- }
- close(wfd);
- flag = false;
- return Error_DevConnFailed;
- }
- LOG4VTM(INFO,"leave : RefreshDog");
- return Error_Succeed;
- }
- ErrorCodeEnum WatchDogClassImpl::Reset()
- {
- LOG4VTM(INFO,"enter : Reset");
- if (!flag)
- {
- wfd = open(WATCHDOG0, O_RDWR);
- if (wfd == -1)
- {
- printf("Unable to open %s \n", WATCHDOG0);
- return Error_DevConnFailed;
- }else
- {
- flag = true;
- }
- }
- if (write(wfd, &stopmagic, sizeof(stopmagic)) != sizeof(stopmagic))
- {
- printf("Watchdog magic stop write error \n");
- return Error_DevConnFailed;
- }
- close(wfd);
- flag = false;
- LOG4VTM(INFO,"leave : Reset");
-
- ErrorCodeEnum err = Error_Succeed;
- m_mode = 0;
- return err;
- }
- ErrorCodeEnum WatchDogClassImpl::DevClose()
- {
-
- LOG4VTM(INFO,"enter : DevClose");
- if (write(wfd, &stopmagic, sizeof(stopmagic)) != sizeof(stopmagic))
- {
- printf("Watchdog magic stop write error \n");
- return Error_DevConnFailed;
- }
- close(wfd);
- LOG4VTM(INFO,"leave : DevClose");
-
- ErrorCodeEnum err = Error_Succeed;
- m_mode = 0;
- return err;
- }
- ErrorCodeEnum WatchDogClassImpl::GetLastErr(DevErrorInfo &devErrInfo)
- {
- static int times = 0;
- char szMessage[128];
- sprintf(szMessage, "this is the %d times error message", ++times);
- strcpy(devErrInfo.szErrMsg, szMessage);
- devErrInfo.dwErrMsgLen = strlen(szMessage);
- return Error_Succeed;
- }
- DEVICEBASE_API ErrorCodeEnum CreateDevComponent(DeviceBaseClass *&baseObj)
- {
- cmb::log_init_config config;
- config.dev_name = "ThermalPrint";
- config.log_level = CMB_LOG_LEVEL_TRACE;
- #if defined(_MSC_VER)
- config.log_dir = ("C:\\rvc\\dbg\\");
- #else
- config.log_dir = ("/opt/rvc/dbg/");
- #endif //_MSC_VER
- std::string str;
- cmb::log4vendor::init(config, str);
- printf("init after: %s\n", str.c_str());
- baseObj = new WatchDogClassImpl();
- if(baseObj == NULL)
- {
- return Error_Resource;
- } else
- {
- return Error_Succeed;
- }
- }
- DEVICEBASE_API ErrorCodeEnum ReleaseDevComponent(DeviceBaseClass *&pBaseObj)
- {
- if(pBaseObj == NULL)
- {
- return Error_Param;
- }
- if(WatchDogClassImpl* pTmp = dynamic_cast<WatchDogClassImpl*>(pBaseObj))
- {
- delete pTmp;
- pTmp = NULL;
- return Error_Succeed;
- }
- return Error_Param;
- }
|