#include #include #include #include #include #include #include #include #include #include #include #include #include ///////////////////////////////////////////////////////////////////////////// #ifdef _DEBUG #define TRACE(...) fprintf(stdout, __VA_ARGS__), fflush(stdout) #else // _DEBUG #define TRACE(...) #endif // _DEBUG #define UNUSED(v) (void)v #define _countof(a) (sizeof(a) / sizeof(*a)) #define _CYCLE_INTV 500 static bool g_bRun = true; ///////////////////////////////////////////////////////////////////////////// typedef struct _ATTRIBS { const char *pszName; double (*fmt)(const char*); }ATTRIBS, *LPATTRIBS; typedef const ATTRIBS *LPCATTRIBS; ///////////////////////////////////////////////////////////////////////////// static void _SigHandler(int sig) { UNUSED(sig); g_bRun = false; } ///////////////////////////////////////////////////////////////////////////// static double _FmtUVers(const char *pszVal) { int nVal = atoi(pszVal); return (double)nVal / 100.0 + 0.4; } static double _FmtUBatV3(const char *pszVal) { int nVal = atoi(pszVal); return (double)nVal / 100.0; } static double _FmtTemp(const char *pszVal) { int nVal = atoi(pszVal); return (double)nVal / 10.0; } static double _FmtUV5Vsys(const char *pszVal) { int nVal = atoi(pszVal); return (double)nVal / 100.0; } static double _FmtUV3V6Bat(const char *pszVal) { int nVal = atoi(pszVal); return (double)nVal / 100.0; } static double _FmtTempTIVA(const char *pszVal) { int nVal = atoi(pszVal); return 147.5 - 187.5 * (double)nVal / 4096.0; } ///////////////////////////////////////////////////////////////////////////// int main(int argc, char *argv[]) { static const ATTRIBS attribs[] = { { .pszName = "UVers", .fmt = _FmtUVers }, { .pszName = "UBatV3", .fmt = _FmtUBatV3 }, { .pszName = "Temp", .fmt = _FmtTemp }, { .pszName = "UV5Vsys", .fmt = _FmtUV5Vsys }, { .pszName = "UV3V6Bat", .fmt = _FmtUV3V6Bat }, { .pszName = "TempTIVA", .fmt = _FmtTempTIVA } }; const ATTRIBS *attribsCur[_countof(attribs)]; ///////////////////////////////////////////////////////////////////////// int nRet, nCntAtts, i; char szBuf[256]; struct pollfd pfd[6]; struct sigaction sa; memset(pfd, 0, sizeof(pfd)); memset(&sa, 0, sizeof(sa)); UNUSED(argc); UNUSED(argv); ///////////////////////////////////////////////////////////////////////// // handle signals sa.sa_handler = _SigHandler; sigaction(SIGHUP, &sa, NULL); // handles user's terminal disconnect sigaction(SIGQUIT, &sa, NULL); // handles Ctrl + '\' sigaction(SIGTERM, &sa, NULL); // handles normal termination sigaction(SIGABRT, &sa, NULL); // handles abnormal termination (i.e. abort()) sigaction(SIGINT, &sa, NULL); // handles Ctrl + 'C' // ignore signals sa.sa_handler = SIG_IGN; sigaction(SIGTSTP, &sa, NULL); // ignores Ctrl + 'Z' sigaction(SIGCHLD, &sa, NULL); // ignores child process termination sigaction(0, &sa, NULL); // ignores shell termination ///////////////////////////////////////////////////////////////////////// for(i = 0, nCntAtts = 0; i < (int)_countof(attribs); ++i) { int fd; char szDevNode[128]; sprintf(szDevNode, "/sys/gfa/tiva/%s", attribs[i].pszName); if((fd = open(szDevNode, O_RDONLY, 0)) >= 0) { pfd[nCntAtts].fd = fd; pfd[nCntAtts].events = POLLPRI; attribsCur[nCntAtts] = &attribs[i]; ++nCntAtts; } } ///////////////////////////////////////////////////////////////////////// if(nCntAtts > 0) { while(g_bRun && ((nRet = poll(pfd, nCntAtts, _CYCLE_INTV)) >= 0)) { if(nRet > 0) { for(i = 0; i < nCntAtts; ++i) { if(pfd[i].revents & POLLPRI) { szBuf[0] = '\0'; if((nRet = read(pfd[i].fd, szBuf, sizeof(szBuf))) >= 0) { szBuf[nRet] = '\0'; lseek(pfd[i].fd, 0, SEEK_SET); TRACE("%-8s: %.2f\n", attribsCur[i]->pszName, attribsCur[i]->fmt(szBuf)); } else { g_bRun = false; break; } } } } } if(nRet < 0) { TRACE("%s\n", strerror(errno)); } } for(i = 0; i < nCntAtts; ++i) { if(pfd[i].fd >= 0) close(pfd[i].fd); } return 0; }