watchdog_impl.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #include "watchdog_impl.h"
  2. #include<cstring>
  3. #include <cstdio>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <sys/ioctl.h>
  10. #include <linux/watchdog.h>
  11. #define WATCHDOG "/dev/watchdog"
  12. #define WATCHDOG0 "/dev/watchdog0"
  13. #define WATCHDOG1 "/dev/watchdog1"
  14. #include "log4vendor.h"
  15. int preset_timeout;
  16. int config_timeout;
  17. int timeleft;
  18. int testcycle = 0;
  19. int dummy;
  20. int devnum = 0;
  21. int wfd;
  22. int config_testcycle;
  23. bool flag = false;
  24. char stopmagic = 'V';
  25. WatchDogClassImpl::WatchDogClassImpl():m_mode(0)
  26. {
  27. }
  28. WatchDogClassImpl::~WatchDogClassImpl()
  29. {
  30. }
  31. ErrorCodeEnum WatchDogClassImpl::GetDevCategory(DevCategoryInfo &devCategory)
  32. {
  33. LOG4VTM(INFO,"enter : GetDevCategory");
  34. memset(&devCategory, 0, sizeof(devCategory));
  35. char l_cVender[20]={0};
  36. strcpy(l_cVender,"grg");
  37. strcpy(devCategory.szVendor,l_cVender);
  38. char l_acDevVer[20]={0};
  39. strcpy(l_acDevVer,"V=V1.0");
  40. strcpy(devCategory.szModel,l_acDevVer);
  41. //设备类型
  42. memset(devCategory.szType,0,sizeof(devCategory.szType));
  43. strcpy(devCategory.szType,"PVER=GPL");
  44. strcat(devCategory.szType,"#MID=");
  45. //设备状态 device status
  46. devCategory.eState = DEVICE_STATUS_NORMAL;
  47. int l_iMajor=4, l_iMinor=20, l_iRevision=65535, l_iBuild=1;
  48. //设备版本号 software version
  49. devCategory.version.wMajor = l_iMajor;
  50. devCategory.version.wMinor = l_iMinor;
  51. devCategory.version.wRevision = l_iRevision;
  52. devCategory.version.wBuild = l_iBuild;
  53. LOG4VTM(INFO,"Leave : GetDevCategory");
  54. return Error_Succeed;
  55. // ErrorCodeEnum err = Error_Succeed;
  56. // std::strcpy(devCategory.szModel, "szModel");
  57. // std::strcpy(devCategory.szType, "szCategory");
  58. // std::strcpy(devCategory.szVendor, "szVendor");
  59. // return err;
  60. }
  61. ErrorCodeEnum WatchDogClassImpl::DevOpen()
  62. {
  63. wfd = open(WATCHDOG0, O_RDWR);
  64. if (wfd == -1)
  65. {
  66. printf("Unable to open %s \n", WATCHDOG0);
  67. return Error_DevConnFailed;
  68. }
  69. LOG4VTM(INFO,"Close");
  70. if (write(wfd, &stopmagic, sizeof(stopmagic)) != sizeof(stopmagic))
  71. {
  72. printf("Watchdog magic stop write error \n");
  73. return Error_DevConnFailed;
  74. }
  75. close(wfd);
  76. return Error_Succeed;
  77. }
  78. ErrorCodeEnum WatchDogClassImpl::StartWatch(DWORD dwDelay, DWORD dwTimeout)
  79. {
  80. LOG4VTM(INFO,"enter : StartWatch");
  81. //判断此时是否已经打开了看门狗
  82. if (!flag)
  83. {
  84. wfd = open(WATCHDOG0, O_RDWR);
  85. if (wfd == -1)
  86. {
  87. printf("Unable to open %s \n", WATCHDOG0);
  88. return Error_DevConnFailed;
  89. }else
  90. {
  91. flag = true;
  92. }
  93. }
  94. config_timeout = dwTimeout;
  95. if (ioctl(wfd, WDIOC_SETTIMEOUT, &config_timeout) == -1)
  96. {
  97. printf("Watchdog settimeout ioctl error %s\n", strerror(errno));
  98. if (write(wfd, &stopmagic, sizeof(stopmagic)) != sizeof(stopmagic))
  99. {
  100. printf("Watchdog magic stop write error \n");
  101. }
  102. close(wfd);
  103. flag = false;
  104. return Error_DevConnFailed;
  105. }
  106. LOG4VTM(INFO,"leave : StartWatch");
  107. return Error_Succeed;
  108. }
  109. ErrorCodeEnum WatchDogClassImpl::StopWatch()
  110. {
  111. LOG4VTM(INFO,"Open");
  112. if (!flag)
  113. {
  114. wfd = open(WATCHDOG0, O_RDWR);
  115. if (wfd == -1)
  116. {
  117. printf("Unable to open %s \n", WATCHDOG0);
  118. return Error_DevConnFailed;
  119. }else
  120. {
  121. flag = true;
  122. }
  123. }
  124. LOG4VTM(INFO,"enter : StopWatch");
  125. if (write(wfd, &stopmagic, sizeof(stopmagic)) != sizeof(stopmagic))
  126. {
  127. printf("Watchdog magic stop write error \n");
  128. return Error_DevConnFailed;
  129. }
  130. close(wfd);
  131. flag = false;
  132. LOG4VTM(INFO,"leave : StopWatch");
  133. return Error_Succeed;
  134. }
  135. ErrorCodeEnum WatchDogClassImpl::RefreshDog()
  136. {
  137. LOG4VTM(INFO,"enter : RefreshDog");
  138. if (!flag)
  139. {
  140. wfd = open(WATCHDOG0, O_RDWR);
  141. if (wfd == -1)
  142. {
  143. printf("Unable to open %s \n", WATCHDOG0);
  144. return Error_DevConnFailed;
  145. }else
  146. {
  147. flag = true;
  148. }
  149. }
  150. if (ioctl(wfd, WDIOC_KEEPALIVE, &dummy) == -1)
  151. {
  152. printf("Watchdog keeplive ioctl error %s\n", strerror(errno));
  153. if (write(wfd, &stopmagic, sizeof(stopmagic)) != sizeof(stopmagic))
  154. {
  155. printf("Watchdog magic stop write error \n");
  156. }
  157. close(wfd);
  158. flag = false;
  159. return Error_DevConnFailed;
  160. }
  161. LOG4VTM(INFO,"leave : RefreshDog");
  162. return Error_Succeed;
  163. }
  164. ErrorCodeEnum WatchDogClassImpl::Reset()
  165. {
  166. LOG4VTM(INFO,"enter : Reset");
  167. if (!flag)
  168. {
  169. wfd = open(WATCHDOG0, O_RDWR);
  170. if (wfd == -1)
  171. {
  172. printf("Unable to open %s \n", WATCHDOG0);
  173. return Error_DevConnFailed;
  174. }else
  175. {
  176. flag = true;
  177. }
  178. }
  179. if (write(wfd, &stopmagic, sizeof(stopmagic)) != sizeof(stopmagic))
  180. {
  181. printf("Watchdog magic stop write error \n");
  182. return Error_DevConnFailed;
  183. }
  184. close(wfd);
  185. flag = false;
  186. LOG4VTM(INFO,"leave : Reset");
  187. ErrorCodeEnum err = Error_Succeed;
  188. m_mode = 0;
  189. return err;
  190. }
  191. ErrorCodeEnum WatchDogClassImpl::DevClose()
  192. {
  193. LOG4VTM(INFO,"enter : DevClose");
  194. if (write(wfd, &stopmagic, sizeof(stopmagic)) != sizeof(stopmagic))
  195. {
  196. printf("Watchdog magic stop write error \n");
  197. return Error_DevConnFailed;
  198. }
  199. close(wfd);
  200. LOG4VTM(INFO,"leave : DevClose");
  201. ErrorCodeEnum err = Error_Succeed;
  202. m_mode = 0;
  203. return err;
  204. }
  205. ErrorCodeEnum WatchDogClassImpl::GetLastErr(DevErrorInfo &devErrInfo)
  206. {
  207. static int times = 0;
  208. char szMessage[128];
  209. sprintf(szMessage, "this is the %d times error message", ++times);
  210. strcpy(devErrInfo.szErrMsg, szMessage);
  211. devErrInfo.dwErrMsgLen = strlen(szMessage);
  212. return Error_Succeed;
  213. }
  214. DEVICEBASE_API ErrorCodeEnum CreateDevComponent(DeviceBaseClass *&baseObj)
  215. {
  216. cmb::log_init_config config;
  217. config.dev_name = "ThermalPrint";
  218. config.log_level = CMB_LOG_LEVEL_TRACE;
  219. #if defined(_MSC_VER)
  220. config.log_dir = ("C:\\rvc\\dbg\\");
  221. #else
  222. config.log_dir = ("/opt/rvc/dbg/");
  223. #endif //_MSC_VER
  224. std::string str;
  225. cmb::log4vendor::init(config, str);
  226. printf("init after: %s\n", str.c_str());
  227. baseObj = new WatchDogClassImpl();
  228. if(baseObj == NULL)
  229. {
  230. return Error_Resource;
  231. } else
  232. {
  233. return Error_Succeed;
  234. }
  235. }
  236. DEVICEBASE_API ErrorCodeEnum ReleaseDevComponent(DeviceBaseClass *&pBaseObj)
  237. {
  238. if(pBaseObj == NULL)
  239. {
  240. return Error_Param;
  241. }
  242. if(WatchDogClassImpl* pTmp = dynamic_cast<WatchDogClassImpl*>(pBaseObj))
  243. {
  244. delete pTmp;
  245. pTmp = NULL;
  246. return Error_Succeed;
  247. }
  248. return Error_Param;
  249. }