⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 Open DMT Client C Source code
💻 C
📖 第 1 页 / 共 2 页
字号:
    if (strEquals(cmd, "save")) {        // save        logINFO(LOGSRC,"Save properties ...");        pktInit(&cmdPacket, PKT_SERVER_SET_PROPERTY, "%2x", (UInt32)PROP_CMD_SAVE_PROPS);        protAddPendingPacket(&cmdPacket);        return;    }    if (strEquals(cmd, "get")) {        // get <prop>        logINFO(LOGSRC,"Get property ...");        char *propID = cmdFld[1]? cmdFld[1] : "";        if (!*propID) {            logERROR(LOGSRC,"Property ID must be specified");        } else {            UInt32 prop = strParseHex32(propID, 0L);            pktInit(&cmdPacket, PKT_SERVER_GET_PROPERTY, "%2x", prop);            protAddPendingPacket(&cmdPacket);        }        return;    }    if (strEquals(cmd, "set8")) {        // set8 <prop> <val>        logINFO(LOGSRC,"Set 8-bit property ...");        char *propID = cmdFld[1]? cmdFld[1] : "";        char *value  = cmdFld[2]? cmdFld[2] : "";        if (!*propID) {            logERROR(LOGSRC,"Property ID must be specified");        } else        if (!*value) {            logERROR(LOGSRC,"Value must be specified");        } else {            UInt32 p = strParseHex32(propID, 0L);            UInt32 v = strParseUInt32(value, 0L);            pktInit(&cmdPacket, PKT_SERVER_SET_PROPERTY, "%2x%1x", p, v);            protAddPendingPacket(&cmdPacket);        }        return;    }    if (strEquals(cmd, "set16")) {        // set16 <prop> <val>        logINFO(LOGSRC,"Set 16-bit property ...");        char *propID = cmdFld[1]? cmdFld[1] : "";        char *value  = cmdFld[2]? cmdFld[2] : "";        if (!*propID) {            logERROR(LOGSRC,"Property ID must be specified");        } else        if (!*value) {            logERROR(LOGSRC,"Value must be specified");        } else {            UInt32 p = strParseHex32(propID, 0L);            UInt32 v = strParseUInt32(value, 0L);            pktInit(&cmdPacket, PKT_SERVER_SET_PROPERTY, "%2x%2x", p, v);            protAddPendingPacket(&cmdPacket);        }        return;    }    if (strEquals(cmd, "set32")) {        // set32 <prop> <val>        logINFO(LOGSRC,"Set 32-bit property ...");        char *propID = cmdFld[1]? cmdFld[1] : "";        char *value  = cmdFld[2]? cmdFld[2] : "";        if (!*propID) {            logERROR(LOGSRC,"Property ID must be specified");        } else        if (!*value) {            logERROR(LOGSRC,"Value must be specified");        } else {            UInt32 p = strParseHex32(propID, 0L);            UInt32 v = strParseUInt32(value, 0L);            pktInit(&cmdPacket, PKT_SERVER_SET_PROPERTY, "%2x%4x", p, v);            protAddPendingPacket(&cmdPacket);        }        return;    }        /* set 'speakFreely' mode */    if (strEquals(cmd, "sf")) {        // sf <maxEvents>        char *mode = cmdFld[1]? cmdFld[1] : "";        if (!*mode) {            protSetSpeakFreelyMode(utTrue,-1);        } else {            int maxEvents = (int)strParseInt32(mode, -1L);            utBool sfMode = (maxEvents >= 0)? utTrue : utFalse;            protSetSpeakFreelyMode(sfMode, maxEvents);        }        pktInit(&cmdPacket, PKT_SERVER_EOB_DONE, "%1x", (UInt32)0L);        protAddPendingPacket(&cmdPacket);        return;    }            /* request to speak */    if (strEquals(cmd, "rq")) {        // rq [<count>]        char *cntStr = cmdFld[1]? cmdFld[1] : "0";        UInt32 cnt = strParseUInt32(cntStr, 0L);        pktInit(&cmdPacket, PKT_SERVER_EOB_DONE, "%1x", cnt);        protAddPendingPacket(&cmdPacket);        return;    }    /* get GPS diagnostics */    if (strEquals(cmd, "gps")) {        // gps        logINFO(LOGSRC,"Get GPS Diagnostics ...");        UInt32 prop = (UInt32)PROP_STATE_GPS_DIAGNOSTIC;        pktInit(&cmdPacket, PKT_SERVER_GET_PROPERTY, "%2x", prop);        protAddPendingPacket(&cmdPacket);        return;    }        /* reboot the client (if supported by the client) */    if (strEquals(cmd, "reboot")) {        // reboot        logINFO(LOGSRC,"Reboot client ...");        UInt32 key  = (UInt32)PROP_CMD_RESET;        UInt32 type = 0L; // cold-reset        UInt8  auth[] = { 'n', 'o', 'w' };        int authLen = 3;        pktInit(&cmdPacket, PKT_SERVER_SET_PROPERTY, "%2x%1x%*b", key, type, authLen, auth);        protAddPendingPacket(&cmdPacket);        return;    }    /* upload file to client */#if defined(INCLUDE_UPLOAD)    if (strEquals(cmd, "upload")) {        // upload <localFile> [<clientFile>]        char *fn = cmdFld[1]? cmdFld[1] : "";       // local file        char *cf = cmdFld[2]? cmdFld[2] : (char*)0; // client file        if (!ioIsFile(fn)) {            logERROR(LOGSRC,"Upload file does not exist: %s", fn);        } else {            // if 'cf' is null, then 'fn' is expected to be a Base64 encoded file            protSetPendingFileUpload(fn, cf);        }        return;    }#endif    /* unrecognized command */    logERROR(LOGSRC,"Unrecognized command: %s", cmd);}// ----------------------------------------------------------------------------/* read a line of text from stdin */// Once the first character is hit, logging is suspended until the entire line is // entered.  To prevent logging from being suspended indefinitely, if no character // is pressed within a 15 second window, the input buffer will be reset and logging // will be resumed.  [Note: The timer is reset after each pressed character.]static char *_readLine(char *cmdLine, int cmdLineLen){    UInt32 timeoutMS = 15000L; // 15 seconds    int bp = 0;    while (utTrue) {        /* wait for available character */        fd_set rfds;        FD_ZERO(&rfds);        FD_SET(STDIN_FILENO, &rfds);        struct timeval tv;        tv.tv_sec  = timeoutMS / 1000L;        tv.tv_usec = (timeoutMS % 1000L) * 1000L;        select(STDIN_FILENO + 1, &rfds, 0, 0, &tv);        if (!FD_ISSET(STDIN_FILENO, &rfds)) {            // timeout (don't let the logging be suspended for too long)            if (bp > 0) {                fprintf(stdout, "     <timeout - input reset>\n\n");                bp = 0;                logSetSuspend(utFalse);            }            continue;         }        /* get a single character */        int ch = fgetc(stdin);        if (ch < 0) {             return (char*)0;         }        /* end of line? */        if ((ch == '\r') || (ch == '\n')) {            if (bp > 0) { // <-- only if something is in the buffer                fprintf(stdout, "\n\n");   // ignore returned error codes                ioFlushStream(stdout);                break;            }            continue;        }        /* backspace? */        if (KEY_IsBackspace(ch)) {            if (bp > 0) { // <-- only if something is in the buffer                fprintf(stdout, "\b \b");   // ignore returned error codes                ioFlushStream(stdout);                bp--;                if (bp == 0) {                    // we've backspaced over first character, resume logging                    logSetSuspend(utFalse);                 }            }            continue;        }        /* save input character */        if (bp < cmdLineLen - 1) {            if ((ch != ' ') || (bp > 0)) { // ignore spaces at start of command line                //fprintf(stdout, "<ch:0x%02X>", ch);                 //ioFlushStream(stdout);                if (ch >= ' ') { // ignore non-printable ASCII characters                    if (bp == 0) {                        // suspend logging on first character entered                        logSetSuspend(utTrue);                    }                    fprintf(stdout, "%c", ch);                    ioFlushStream(stdout);                    cmdLine[bp++] = ch;                 }            }            continue;        }        /* overflow */        fprintf(stdout, "<overflow>\n");   // ignore returned error codes        ioFlushStream(stdout);    }        /* terminate line */    cmdLine[bp] = 0; // terminate        /* resume logging */    logSetSuspend(utFalse);        /* trim and return */    return strTrim(cmdLine);}// ----------------------------------------------------------------------------// ----------------------------------------------------------------------------// ----------------------------------------------------------------------------// Main entry pointstatic threadThread_t       protoThread;static threadMutex_t        protoMutex;#define PROTO_LOCK          MUTEX_LOCK(&protoMutex);#define PROTO_UNLOCK        MUTEX_UNLOCK(&protoMutex);static void _usage(const char *pgm, int exitCode){    fprintf(stdout, "Usage: \n");    fprintf(stdout, "   %s ...\n", pgm);    fprintf(stdout, "    [-comlog]          - Enable commPort data logging\n");    fprintf(stdout, "    [-com <port>]      - Server serial port\n");    fprintf(stdout, "\n");    exit(exitCode);}// main entry pointint main(int argc, char *argv[]){    setlocale(LC_ALL, "POSIX");    setDebugMode(utTrue);    /* init */    threadMutexInit(&protoMutex);    memset(comPortID, 0, sizeof(comPortID));    /* header */    fprintf(stdout, "Sample serial port server [Version %s]\n", VERSION);    /* command line arguments */    int i;    utBool comLog = utFalse;    for (i = 1; i < argc; i++) {        if (strEquals(argv[i], "-help") || strEquals(argv[i], "-h")) {            // -h[elp]            _usage(argv[0], 0);        } else        if (strEquals(argv[i], "-comlog")) {            // -comlog            comLog = utTrue;        } else        if (strEquals(argv[i], "-com")) {            // -com <port>            i++;            if ((i < argc) && (*argv[i] != '-')) {                strncpy(comPortID, argv[i], sizeof(comPortID) - 1);                comPortLog = comLog;            } else {                fprintf(stderr, "Missing serial port ...\n");                _usage(argv[0], 1);            }        } else        {            fprintf(stderr, "Invalid option: %s\n", argv[i]);            _usage(argv[0], 1);        }     }    /* supplied com port? */    if (!*comPortID) {        fprintf(stderr, "Please supply a comport\n");        _usage(argv[0], 1);    }    /* thread initializer */    // this must be called before threads are created    threadInitialize();    /* custom event packet format */    // (add custom event packet definitions here)    /* protocol handlers */    protocolSetEventHandler(&mainHandleEvent);    protocolSetClientInitHandler(&mainHandleClientInit);    protocolSetPropertyHandler(&mainHandleProperty);    protocolSetDiagHandler(&mainHandleDiag);    protocolSetErrorHandler(&mainHandleError);    /* protocol thread */    if (threadCreate(&protoThread,&_protocolThreadRunnable,0,"SComServe") == 0) {        // thread started successfully    } else {        logCRITICAL(LOGSRC,"Unable to start Protocol thread");    }    /* reset stdin (remove buffering) */    ioResetSTDIN();    /* loop here */    char cmdLine[512];    while (utTrue) {        /* get command line */        char *cmdTrim = _readLine(cmdLine, sizeof(cmdLine));        if (!cmdTrim) {            printf("Read error in stdin\n");            exit(1);        }                /* parse command */        char *cmdFld[32];        memset(cmdFld, 0, sizeof(cmdFld));        strParseArray_sep(cmdTrim, cmdFld, 20, ' ');        char *cmd = cmdFld[0];        /* check for exit/quit */        if (strEquals(cmd,"exit") || strEquals(cmd,"quit")) {            break;        }        /* parse and execute command */        execCommand(cmd, cmdFld);    }    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -