LingYun Studio embeded system framwork software, such as thirdparty build shell and lingyun library
guowenxue
2024-08-21 cbff794b55c94c3c38f4017d21d78b9fa1743f2c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*********************************************************************************
 *      Copyright:  (C) 2021 Guo Wenxue <guowenxue@gmail.com>
 *                  All rights reserved.
 *
 *       Filename:  comport.c
 *    Description:  This file used to do ioctl() on common device or communicate
 *                  with serial port/TTY device.
 *
 *        Version:  1.0.0(5/1/2021~)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "5/1/2021 10:08:05 AM"
 *
 ********************************************************************************/
#include <getopt.h>
#include <libgen.h>
#include <sys/ioctl.h>
 
#include "comport.h"
 
unsigned char g_stop = 0;
unsigned char g_CtrlZ;
 
void print_version(char *name);
void usage(char *name);
void signal_handler(int i_sig);
void stdin_nonblock(void);
int kbhit(void);
 
 
int main(int argc, char **argv)
{
    int                opt = 0;
    int                rv = 0;
    int                i;
 
    comport_t          comport;
    int                baudrate = 115200;
    char              *settings = "8N1N";
    char              *dev_name = NULL;
 
    char               buf[512];
    unsigned char      disp_mode = 0x00;
 
    struct sigaction   sigact;
 
    struct option long_options[] = {
        {"device", required_argument, NULL, 'd'},
        {"baudrate", required_argument, NULL, 'b'},
        {"settings", required_argument, NULL, 's'},
        {"hex", no_argument, NULL, 'x'},
        {"version", no_argument, NULL, 'v'},
        {"help", no_argument, NULL, 'h'},
        {NULL, 0, NULL, 0}
    };
 
    while ((opt = getopt_long(argc, argv, "d:b:s:xvh", long_options, NULL)) != -1)
    {
        switch (opt)
        {
          case 'd':
              dev_name = optarg;
              break;
 
          case 'b':
              baudrate = atoi(optarg);
              break;
 
          case 's':            /* Default settings as 8N1N */
              settings = optarg;
              break;
 
          case 'x':            /* Display receive data as Hex mode */
              disp_mode = 0x01;
              break;
 
          case 'v':            /* version */
              print_version(argv[0]);
              return 0;
 
          case 'h':            /* help */
              usage(argv[0]);
              return 0;
 
          default:
              break;
        }                       /* end of "switch(opt)" */
    }
 
    if (argc < 2)
    {
        usage(argv[0]);
        return 0;
    }
 
    if ( (rv=comport_open(&comport, dev_name, baudrate, settings)) < 0)
    {
        printf("Failed to open %s wth baudrate %d, %s. rv=%d\n", dev_name, baudrate, settings, rv);
        return -1;
    }
 
    stdin_nonblock();
 
    /* Process level signal handler */
    sigemptyset(&sigact.sa_mask);
    sigact.sa_flags = 0;
    sigact.sa_handler = signal_handler;
 
    sigaction(SIGTERM, &sigact, NULL);  /* catch terminate signal */
    sigaction(SIGINT, &sigact, NULL);   /* catch interrupt signal */
    sigaction(SIGSEGV, &sigact, NULL);  /* catch segmentation faults */
    sigaction(SIGTSTP, &sigact, NULL);  /* catch ctrl+Z */
    sigaction(SIGSTOP, &sigact, NULL);  /* catch ctrl+Z */
 
    while ( !g_stop )
    {
        rv = comport_recv(&comport, buf, sizeof(buf) - 1, 10);
        if ( rv > 0)
        {
            for (i=0; i<rv ; i++)
            {
                if (0 == disp_mode)
                    printf("%c", buf[i]);
                else
                    printf("%02X ", buf[i]);
            }
            fflush(stdout);
        }
        if (0 != kbhit())
        {
            rv = fgetc(stdin);
 
            if (0x0A == rv)
            {
                buf[0] = 0x0D; /* 13 == 0x0D */
            }
            else
            {
                buf[0] = rv;
            }
 
            comport_send(&comport, buf, 1);
        }
        else if (0x00 != g_CtrlZ)
        {
            g_CtrlZ = 0x00;
            buf[0] = 0x1A;
            comport_send(&comport, buf, 1);
        }
    }
 
    comport_close(&comport);
    return 0;
}  /* ----- End of main() ----- */
 
 
void print_version(char *name)
{
    char *progname = NULL;
    char *ptr = NULL;
 
    ptr = strdup(name);
    progname = basename(ptr);
 
    printf("%s v1.0.0 Build on %s\n", progname, __DATE__);
    printf("Copyright (C) 2021 guowenxue <guowenxue@gmail.com>\n");
 
    free(ptr);
    return;
}
 
void usage(char *name)
{
    char *progname = NULL;
    char *ptr = NULL;
 
    ptr = strdup(name);
    progname = basename(ptr);
    printf("Usage: comport -d <device> [-b <baudrate>][-s <settings>] [-x]\n");
    printf(" -d[device  ]  device name, such as /dev/ttyUSB0\n");
    printf(" -b[baudrate]  device baudrate (115200, 57600, 19200, 9600), default is 115200\n");
    printf(" -s[settings]  device settings as like 8N1N(defalt setting)\n");
    printf("                 - data bits: 8, 7\n");
    printf("                 - parity: N=None, O=Odd, E=Even, S=Space\n");
    printf("                 - stop bits: 1, 0\n");
    printf("                 - flow control: N=None, H=Hardware, S=Software, B=Both\n");
    printf(" -x[hex     ]  display received data in hex format\n");
 
    printf(" -v[version ]  Display the program version\n");
    printf(" -h[help    ]  Display this help information\n");
    print_version(progname);
 
    free(ptr);
 
    return;
}
 
 
void signal_handler(int signum)
{
    if (SIGTERM == signum || SIGINT == signum)
    {
        g_stop = 1;
    }
    else if (SIGTSTP == signum)
    {
        g_CtrlZ = 1;
    }
}
 
 
void stdin_nonblock(void)
{
    struct termios ttystate;
 
    //get the terminal state
    tcgetattr(STDIN_FILENO, &ttystate);
 
    //turn off canonical mode
    ttystate.c_lflag &= ~ICANON;
    //minimum of number input read.
    ttystate.c_cc[VMIN] = 1;
 
    //set the terminal attributes.
    tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
}
 
int kbhit(void)
{
    struct timeval tv;
    fd_set fds;
    tv.tv_sec = 0;
    tv.tv_usec = 0;
    FD_ZERO(&fds);
    FD_SET(STDIN_FILENO, &fds); //STDIN_FILENO is 0
    select(STDIN_FILENO + 1, &fds, NULL, NULL, &tv);
    return FD_ISSET(STDIN_FILENO, &fds);
}