Annotation of othersrc/dist/cdk/demos/serial.c, revision 1.2

1.1       garbled     1: #include <stdio.h>
                      2: #include <stdlib.h>
                      3: #include <unistd.h>
                      4: #include <fcntl.h>
                      5: #include <termios.h>
                      6: #include <sys/termios.h>
                      7: #include <sys/ioctl.h>
                      8: #include <unistd.h>
                      9: #include <time.h>
                     10: 
                     11: #include <cdk.h>
                     12: 
                     13: #ifdef HAVE_XCURSES
                     14: char *XCursesProgramName="serial";
                     15: #endif
                     16: 
                     17: /*
                     18:  * Create global definitions.
                     19:  */
                     20: #define DEFAULT_PORT                   "/dev/ttya"
                     21: #define DEFAULT_POLL_INTERVAL          1       /* milliseconds */
                     22: 
                     23: /*
                     24:  * This is the working function which probes the serial port.
                     25:  */
                     26: boolean probeModem (void);
                     27: 
                     28: /*
                     29:  * Define some global variables.
                     30:  */
                     31: CDKLABEL *label                = 0;
                     32: int LLastState         = 0;
                     33: int LCurrentState      = 0;
                     34: extern char *optarg;
                     35: char port[256];
                     36: int LFD;
                     37: 
                     38: /*
                     39:  *
                     40:  */
                     41: int main (int argc, char **argv)
                     42: {
                     43:    CDKSCREEN *cdkScreen = 0;
                     44:    WINDOW *cursesWin   = 0;
                     45:    int lines           = 0;
                     46:    char *info[256], temp[256];
                     47:    struct termios termInfo;
                     48:    int ret;
                     49: 
                     50:    /* Set the deault values. */
                     51:    strcpy (port, DEFAULT_PORT);
                     52: 
                     53:    /* Parse up the command line. */
                     54:    while (1)
                     55:    {
                     56:       if ((ret = getopt (argc, argv, "p:h")) == -1)
                     57:       {
                     58:         break;
                     59:       }
                     60: 
                     61:       switch (ret)
                     62:       {
                     63:         case 'p' :
                     64:               strcpy (port, optarg);
                     65:               break;
                     66: 
                     67:         case 'h' :
                     68:               printf ("Usage: %s [-p Port] [-i Poll Interval] [-c Poll Count] [-v] [-h]\n", argv[0]);
                     69:               exit (0);
                     70:               break;
                     71:       }
                     72:    }
                     73: 
                     74:   /*
                     75:    * Create the CDK screen.
                     76:    */
                     77:    cursesWin = initscr();
                     78:    cdkScreen = initCDKScreen (cursesWin);
                     79: 
                     80:    /* Start CDK color. */
                     81:    initCDKColor();
                     82: 
                     83:   /*
                     84:    * Set the title of the main window.
                     85:    */
                     86:    sprintf (temp, "<C>Serial Port Monitor (%s)", port);
                     87:    info[lines++] = copyChar (temp);
                     88:    info[lines++] = copyChar ("<C><#HL(30)>");
                     89:    info[lines++] = copyChar ("");
                     90:    info[lines++] = copyChar ("Line Enabled       : -");
                     91:    info[lines++] = copyChar ("Data Terminal Ready: -");
                     92:    info[lines++] = copyChar ("Carrier Detect     : -");
                     93:    info[lines++] = copyChar ("Request To Send    : -");
                     94:    info[lines++] = copyChar ("Clear To Send      : -");
                     95:    info[lines++] = copyChar ("Secondary Transmit : -");
                     96:    info[lines++] = copyChar ("Secondary Receive  : -");
                     97:    info[lines++] = copyChar ("");
                     98: 
                     99:    /* Create the label widget. */
                    100:    label = newCDKLabel (cdkScreen, CENTER, CENTER, info, lines, TRUE, FALSE);
                    101:    drawCDKLabel (label, TRUE);
                    102: 
                    103:   /*
                    104:    * Open the serial port read only.
                    105:    */
                    106:    if ((LFD = open (port, O_RDONLY|O_NDELAY, 0)) == -1)
                    107:    {
                    108:       /* Create a pop-up dialog box... */
                    109:       printf ("Error: Open of <%s> failed.\n", port);
1.2     ! wiz       110:       exit (1);
1.1       garbled   111:    }
                    112: 
                    113:    termInfo.c_cflag = CRTSCTS | CLOCAL;
                    114:    if (tcgetattr (LFD, &termInfo) != 0)
                    115:    {
                    116:       /* Really should create a pop-up dialog box... */
                    117:       printf ("Error: Could not get port attributes. Closing the port.\n");
                    118:       close (LFD);
                    119:       exit (1);
                    120:    }
                    121: 
                    122:    for (;;)
                    123:    {
                    124:       /* Probe the modem. */
                    125:       probeModem();
                    126: 
                    127:      /*
                    128:       * Sleep for the given amount of time. We do this first so no
                    129:       * weird refresh things happen.
                    130:       */
                    131:       napms (DEFAULT_POLL_INTERVAL);
                    132:    }
                    133: }
                    134: 
                    135: /*
                    136:  * This probes the modem and determines if we need to update
                    137:  * the display.
                    138:  */
                    139: boolean probeModem (void)
                    140: {
                    141:    int lines           = 0;
                    142:    char *info[256], temp[256];
                    143: 
                    144:    /* Start building the label. */
                    145:    sprintf (temp, "<C>Serial Port Monitor (%s)", port);
                    146:    info[lines++] = copyChar (temp);
                    147:    info[lines++] = copyChar ("<C><#HL(30)>");
                    148:    info[lines++] = copyChar ("");
                    149: 
                    150:   /*
                    151:    * Get the serial port info.
                    152:    */
                    153:    ioctl (LFD, TIOCMGET, &LCurrentState);
                    154: 
                    155:   /*
                    156:    * If the states are different, change the display.
                    157:    */
                    158:    if (LLastState != LCurrentState)
                    159:    {
                    160:      /*
                    161:       * Check for line enabled.
                    162:       */
                    163:       if (LCurrentState & TIOCM_LE)
                    164:       {
                    165:         info[lines++] = copyChar ("Line Enabled       : <#DI>");
                    166:       }
                    167:       else
                    168:       {
                    169:         info[lines++] = copyChar ("Line Enabled       :  ");
                    170:       }
                    171: 
                    172:      /*
                    173:       * Check for data terminal ready.
                    174:       */
                    175:       if (LCurrentState & TIOCM_DTR)
                    176:       {
                    177:         info[lines++] = copyChar ("Data Terminal Ready: <#DI>");
                    178:       }
                    179:       else
                    180:       {
                    181:         info[lines++] = copyChar ("Data Terminal Ready:  ");
                    182:       }
                    183: 
                    184:      /*
                    185:       * Check for carrier detect.
                    186:       */
                    187:       if (LCurrentState & TIOCM_CAR)
                    188:       {
                    189:         info[lines++] = copyChar ("Carrier Detect     : <#DI>");
                    190:       }
                    191:       else
                    192:       {
                    193:         info[lines++] = copyChar ("Carrier Detect     :  ");
                    194:       }
                    195: 
                    196:      /*
                    197:       * Check for request to send.
                    198:       */
                    199:       if (LCurrentState & TIOCM_RTS)
                    200:       {
                    201:         info[lines++] = copyChar ("Request To Send    : <#DI>");
                    202:       }
                    203:       else
                    204:       {
                    205:         info[lines++] = copyChar ("Request To Send    :  ");
                    206:       }
                    207: 
                    208:      /*
                    209:       * Check for clear to send.
                    210:       */
                    211:       if (LCurrentState & TIOCM_CTS)
                    212:       {
                    213:         info[lines++] = copyChar ("Clear To Send      : <#DI>");
                    214:       }
                    215:       else
                    216:       {
                    217:         info[lines++] = copyChar ("Clear To Send      :  ");
                    218:       }
                    219: 
                    220:      /*
                    221:       * Check for secondary transmit.
                    222:       */
                    223:       if (LCurrentState & TIOCM_ST)
                    224:       {
                    225:         info[lines++] = copyChar ("Secondary Transmit : <#DI>");
                    226:       }
                    227:       else
                    228:       {
                    229:         info[lines++] = copyChar ("Secondary Transmit :  ");
                    230:       }
                    231: 
                    232:      /*
                    233:       * Check for secondary receive.
                    234:       */
                    235:       if (LCurrentState & TIOCM_SR)
                    236:       {
                    237:         info[lines++] = copyChar ("Secondary Receive  : <#DI>");
                    238:       }
                    239:       else
                    240:       {
                    241:         info[lines++] = copyChar ("Secondary Receive  :  ");
                    242:       }
                    243:    }
                    244:    info[lines++] = copyChar ("");
                    245: 
                    246:    /* Only do this if things have changed. */
                    247:    if (LLastState != LCurrentState)
                    248:    {
                    249:       eraseCDKLabel (label);
                    250:       setCDKLabel (label, info, lines, TRUE);
                    251:       drawCDKLabel (label, TRUE);
                    252:    }
                    253: 
                    254:   /*
                    255:    * Keep the current state.
                    256:    */
                    257:    LLastState = LCurrentState;
                    258: 
                    259:   /*
                    260:    * Return False to tell X that we want this funtion to be
                    261:    * run again.
                    262:    */
                    263:    return FALSE;
                    264: }

CVSweb <webmaster@jp.NetBSD.org>