-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminbasecli_linux.cpp
More file actions
181 lines (148 loc) · 4.87 KB
/
Copy pathminbasecli_linux.cpp
File metadata and controls
181 lines (148 loc) · 4.87 KB
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
/**
* @file minbasecli_linux.cpp
* @author Jose Miguel Rios Rubio <jrios.github@gmail.com>
* @date 10-02-2022
* @version 1.0.2
*
* @section DESCRIPTION
*
* A simple Command Line Interface C++ library implementation with HAL
* emphasis to be used in different kind of devices and frameworks.
*
* @section LICENSE
*
* Copyright (c) 2021 Jose Miguel Rios Rubio. All right reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*****************************************************************************/
/* Include Guard */
#if defined(__linux__)
/*****************************************************************************/
/* Libraries */
// Header Interface
#include "minbasecli_linux.h"
// Device/Framework Libraries
#include <pthread.h>
#include <sys/select.h> // async stdin-stdout interface
// Standard Libraries
#include <string.h>
#include <stdio.h> // getchar(), printf()
#include <unistd.h> // async stdin-stdout interface
/*****************************************************************************/
/* Read STDIN Stream Thread Prototype */
/**
* @brief Posix Thread handler function to read STDIN data from the interface.
* @param arg Posix Thread arguments.
*/
void* th_read_stdin(void* arg);
/*****************************************************************************/
/* Constructor */
/**
* @details
* This constructor initializes all attributes of the CLI class.
*/
MINBASECLI_LINUX::MINBASECLI_LINUX()
{
this->iface = NULL;
this->th_rx_read_head = 0;
this->th_rx_read_tail = 0;
this->th_rx_read[0] = '\0';
}
/*****************************************************************************/
/* Specific Device/Framework HAL Methods */
/**
* @details
* This function should get and initialize the interface element that is going
* to be used by the CLI and it also start the STDIN data read thread.
*/
bool MINBASECLI_LINUX::hal_setup(void* iface, const uint32_t baud_rate)
{
this->iface = iface;
return launch_stdin_read_thread();
}
/**
* @details
* This function return the number of bytes received by the interface that are
* available to be read.
*/
size_t MINBASECLI_LINUX::hal_iface_available()
{
return (this->th_rx_read_head - this->th_rx_read_tail);
}
/**
* @details
* This function returns a received byte from the interface. It checks if
* there is any byte available to be read and increase the read circular
* buffer tail index to "pop" this element from the buffer and return it.
*/
uint8_t MINBASECLI_LINUX::hal_iface_read()
{
// Ignore if there is no available bytes to be read
if (hal_iface_available() == 0)
{ return 0; }
// Return read bytes
this->th_rx_read_tail = (this->th_rx_read_tail + 1) %
MINBASECLI_MAX_READ_SIZE;
return th_rx_read[this->th_rx_read_tail];
}
/**
* @details
* This function send a byte through the interface.
*/
void MINBASECLI_LINUX::hal_iface_print(const uint8_t data_byte)
{
printf("%c", (char)(data_byte));
}
/*****************************************************************************/
/* Private Methods */
/**
* @details
* This function create a Posix Thread to handle the STDIN data read from the
* interface.
*/
bool MINBASECLI_LINUX::launch_stdin_read_thread()
{
pthread_t th_id;
int rc = 0;
rc = pthread_create(&th_id, NULL, th_read_stdin, (void*)(this));
if (rc != 0)
{
printf("Fail to create STDIN read thread: %s\n", strerror(rc));
return false;
}
return true;
}
/*****************************************************************************/
/* STDIN Read Thread */
/**
* @details
* This function is the Posix Thread that manages the STDIN data read. It gets
* each new byte received from the interface and store them in the read buffer
* (increasing the circular buffer head index).
*/
void* th_read_stdin(void* arg)
{
MINBASECLI_LINUX* _this = (MINBASECLI_LINUX*) arg;
uint8_t new_head = 0;
while (true)
{
new_head = (_this->th_rx_read_head + 1) % MINBASECLI_MAX_READ_SIZE;
_this->th_rx_read[new_head] = getc(stdin);
_this->th_rx_read_head = new_head;
}
}
/*****************************************************************************/
#endif /* __linux__ */