Shellminator  V1.2.0
Simple Terminal
Loading...
Searching...
No Matches
Shellminator-IO.cpp
Go to the documentation of this file.
1/*
2 * Created on Aug 10 2020
3 *
4 * Copyright (c) 2020 - Daniel Hajnal
5 * hajnal.daniel96@gmail.com
6 * This file is part of the Shellminator project.
7 * Modified 2022.05.08
8*/
9
10/*
11MIT License
12
13Copyright (c) 2020 Daniel Hajnal
14
15Permission is hereby granted, free of charge, to any person obtaining a copy
16of this software and associated documentation files (the "Software"), to deal
17in the Software without restriction, including without limitation the rights
18to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19copies of the Software, and to permit persons to whom the Software is
20furnished to do so, subject to the following conditions:
21
22The above copyright notice and this permission notice shall be included in all
23copies or substantial portions of the Software.
24
25THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31SOFTWARE.
32*/
33
34#include "Shellminator-IO.hpp"
35
36
37#ifdef SHELLMINATOR_ENABLE_WEBSOCKET_MODULE
38
39//----- Response for WebSocket Server class -----//
40
41void shellminatorWebSocketChannel::select( WebSocketsServer *server_p, int8_t clientID_p ){
42
43 server = server_p;
44 clientID = clientID_p;
45
46}
47
48void shellminatorWebSocketChannel::push( uint8_t data ){
49
50 buffer[ writePointer ] = data;
51 writePointer++;
52 if( writePointer >= SHELLMINATOR_WEBSOCKET_BUFFER_LEN ){
53 writePointer = 0;
54 }
55
56}
57
58void shellminatorWebSocketChannel::push( uint8_t* data, size_t size ){
59
60 uint32_t i;
61
62 for( i = 0; i < size; i++ ){
63
64 buffer[ writePointer ] = data[ i ];
65 writePointer++;
66 if( writePointer >= SHELLMINATOR_WEBSOCKET_BUFFER_LEN ){
67 writePointer = 0;
68 }
69
70 }
71
72}
73
74int shellminatorWebSocketChannel::available(){
75
76 if( writePointer == readPointer ){
77 return 0;
78 }
79
80 else if( writePointer > readPointer ){
81 return writePointer - readPointer;
82 }
83
84 else{
85
86 return SHELLMINATOR_WEBSOCKET_BUFFER_LEN - readPointer + writePointer;
87
88 }
89
90}
91
92int shellminatorWebSocketChannel::read(){
93
94 int ret;
95
96 if( writePointer == readPointer ){
97
98 return -1;
99
100 }
101
102 else{
103
104 ret = (uint8_t)buffer[ readPointer ];
105 readPointer++;
106
107 if( readPointer >= SHELLMINATOR_WEBSOCKET_BUFFER_LEN ){
108 readPointer = 0;
109 }
110
111 }
112
113 return ret;
114
115}
116
117int shellminatorWebSocketChannel::peek(){
118
119 if( writePointer == readPointer ){
120
121 return -1;
122
123 }
124
125 else{
126
127 return (uint8_t)buffer[ readPointer ];
128
129 }
130
131}
132
133void shellminatorWebSocketChannel::flush(){
134
135 // Todo Maybe clear the input buffer?
136
137}
138
139size_t shellminatorWebSocketChannel::write( uint8_t b ){
140
141 if( server ){
142 server -> sendTXT( clientID, &b, 1 );
143 return 1;
144 }
145 return 0;
146
147}
148
149size_t shellminatorWebSocketChannel::write( const uint8_t *buffer, size_t size ){
150
151 if( server ){
152 server -> sendTXT( clientID, buffer, size );
153 return 1;
154 }
155 return 0;
156
157}
158
159//---- print section ----//
160
161size_t shellminatorWebSocketChannel::print( char c ){
162
163 if( server ){
164 server -> sendTXT( clientID, (uint8_t*)&c, 1 );
165 return 1;
166 }
167 return 0;
168
169}
170
171size_t shellminatorWebSocketChannel::print( uint8_t b ){
172
173 char outBuff[10];
174 uint32_t dataSize;
175
176 snprintf( outBuff, 10, "%u", (int)b );
177
178 dataSize = strlen( outBuff );
179
180 if( server ){
181 server -> sendTXT( clientID, (uint8_t*)outBuff, dataSize );
182 return dataSize;
183 }
184 return 0;
185
186}
187
188size_t shellminatorWebSocketChannel::print( char *str ){
189
190 uint32_t dataSize;
191
192 dataSize = strlen( str );
193
194 if( server ){
195 server -> sendTXT( clientID, (uint8_t*)str, dataSize );
196 return dataSize;
197 }
198
199 return 0;
200
201}
202
203size_t shellminatorWebSocketChannel::print( const char *str ){
204
205 uint32_t dataSize;
206
207 dataSize = strlen( str );
208
209 if( server ){
210 server -> sendTXT( clientID, (uint8_t*)str, dataSize );
211 return dataSize;
212 }
213 return 0;
214
215}
216
217int8_t shellminatorWebSocketChannel::getClientID(){
218
219 return clientID;
220
221}
222
223#endif