main.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <sys/statvfs.h>
  8. #include <sys/statfs.h>
  9. #include <sys/types.h>
  10. #include <signal.h>
  11. #include <poll.h>
  12. #include <fcntl.h>
  13. #include <errno.h>
  14. /////////////////////////////////////////////////////////////////////////////
  15. #ifdef _DEBUG
  16. #define TRACE(...) fprintf(stdout, __VA_ARGS__), fflush(stdout)
  17. #else // _DEBUG
  18. #define TRACE(...)
  19. #endif // _DEBUG
  20. #define UNUSED(v) (void)v
  21. #define _countof(a) (sizeof(a) / sizeof(*a))
  22. #define _CYCLE_INTV 500
  23. static bool g_bRun = true;
  24. /////////////////////////////////////////////////////////////////////////////
  25. typedef struct _ATTRIBS
  26. {
  27. const char *pszName;
  28. double (*fmt)(const char*);
  29. }ATTRIBS, *LPATTRIBS;
  30. typedef const ATTRIBS *LPCATTRIBS;
  31. /////////////////////////////////////////////////////////////////////////////
  32. static void _SigHandler(int sig)
  33. {
  34. UNUSED(sig);
  35. g_bRun = false;
  36. }
  37. /////////////////////////////////////////////////////////////////////////////
  38. static double _FmtUVers(const char *pszVal)
  39. {
  40. int nVal = atoi(pszVal);
  41. return (double)nVal / 100.0 + 0.4;
  42. }
  43. static double _FmtUBatV3(const char *pszVal)
  44. {
  45. int nVal = atoi(pszVal);
  46. return (double)nVal / 100.0;
  47. }
  48. static double _FmtTemp(const char *pszVal)
  49. {
  50. int nVal = atoi(pszVal);
  51. return (double)nVal / 10.0;
  52. }
  53. static double _FmtUV5Vsys(const char *pszVal)
  54. {
  55. int nVal = atoi(pszVal);
  56. return (double)nVal / 100.0;
  57. }
  58. static double _FmtUV3V6Bat(const char *pszVal)
  59. {
  60. int nVal = atoi(pszVal);
  61. return (double)nVal / 100.0;
  62. }
  63. static double _FmtTempTIVA(const char *pszVal)
  64. {
  65. int nVal = atoi(pszVal);
  66. return 147.5 - 187.5 * (double)nVal / 4096.0;
  67. }
  68. /////////////////////////////////////////////////////////////////////////////
  69. int main(int argc, char *argv[])
  70. {
  71. static const ATTRIBS attribs[] =
  72. {
  73. {
  74. .pszName = "UVers",
  75. .fmt = _FmtUVers
  76. },
  77. {
  78. .pszName = "UBatV3",
  79. .fmt = _FmtUBatV3
  80. },
  81. {
  82. .pszName = "Temp",
  83. .fmt = _FmtTemp
  84. },
  85. {
  86. .pszName = "UV5Vsys",
  87. .fmt = _FmtUV5Vsys
  88. },
  89. {
  90. .pszName = "UV3V6Bat",
  91. .fmt = _FmtUV3V6Bat
  92. },
  93. {
  94. .pszName = "TempTIVA",
  95. .fmt = _FmtTempTIVA
  96. }
  97. };
  98. const ATTRIBS *attribsCur[_countof(attribs)];
  99. /////////////////////////////////////////////////////////////////////////
  100. int nRet, nCntAtts, i;
  101. char szBuf[256];
  102. struct pollfd pfd[6];
  103. struct sigaction sa;
  104. memset(pfd, 0, sizeof(pfd));
  105. memset(&sa, 0, sizeof(sa));
  106. UNUSED(argc);
  107. UNUSED(argv);
  108. /////////////////////////////////////////////////////////////////////////
  109. // handle signals
  110. sa.sa_handler = _SigHandler;
  111. sigaction(SIGHUP, &sa, NULL); // handles user's terminal disconnect
  112. sigaction(SIGQUIT, &sa, NULL); // handles Ctrl + '\'
  113. sigaction(SIGTERM, &sa, NULL); // handles normal termination
  114. sigaction(SIGABRT, &sa, NULL); // handles abnormal termination (i.e. abort())
  115. sigaction(SIGINT, &sa, NULL); // handles Ctrl + 'C'
  116. // ignore signals
  117. sa.sa_handler = SIG_IGN;
  118. sigaction(SIGTSTP, &sa, NULL); // ignores Ctrl + 'Z'
  119. sigaction(SIGCHLD, &sa, NULL); // ignores child process termination
  120. sigaction(0, &sa, NULL); // ignores shell termination
  121. /* pfd[0].fd = open("/sys/gfa/tiva/UVers", O_RDONLY, 0);
  122. pfd[1].fd = open("/sys/gfa/tiva/UBatV3", O_RDONLY, 0);
  123. pfd[2].fd = open("/sys/gfa/tiva/Temp", O_RDONLY, 0);
  124. pfd[3].fd = open("/sys/gfa/tiva/UV5Vsys", O_RDONLY, 0);
  125. pfd[4].fd = open("/sys/gfa/tiva/UV3V6Bat", O_RDONLY, 0);
  126. pfd[5].fd = open("/sys/gfa/tiva/TempTIVA", O_RDONLY, 0);*/
  127. for(i = 0, nCntAtts = 0; i < (int)_countof(attribs); ++i)
  128. {
  129. int fd;
  130. char szDevNode[128];
  131. sprintf(szDevNode, "/sys/gfa/tiva/%s", attribs[i].pszName);
  132. if((fd = open(szDevNode, O_RDONLY, 0)))
  133. {
  134. pfd[nCntAtts].fd = fd;
  135. pfd[nCntAtts].events = POLLPRI;
  136. attribsCur[nCntAtts] = &attribs[i];
  137. ++nCntAtts;
  138. }
  139. }
  140. /////////////////////////////////////////////////////////////////////////
  141. if(nCntAtts > 0)
  142. {
  143. while(g_bRun && ((nRet = poll(pfd, nCntAtts, _CYCLE_INTV)) >= 0))
  144. {
  145. if(nRet > 0)
  146. {
  147. for(i = 0; i < nCntAtts; ++i)
  148. {
  149. if(pfd[i].revents & POLLPRI)
  150. {
  151. szBuf[0] = '\0';
  152. if((nRet = read(pfd[i].fd, szBuf, sizeof(szBuf))) >= 0)
  153. {
  154. szBuf[nRet] = '\0';
  155. lseek(pfd[i].fd, 0, SEEK_SET);
  156. TRACE("%-8s: %.2f\n", attribsCur[i]->pszName, attribsCur[i]->fmt(szBuf));
  157. }
  158. else
  159. {
  160. g_bRun = false;
  161. break;
  162. }
  163. }
  164. }
  165. }
  166. }
  167. if(nRet < 0)
  168. {
  169. TRACE("%s\n", strerror(errno));
  170. }
  171. }
  172. for(i = 0; i < nCntAtts; ++i)
  173. {
  174. if(pfd[i].fd >= 0)
  175. close(pfd[i].fd);
  176. }
  177. return 0;
  178. }