Shellminator  V1.2.0
Simple Terminal
Loading...
Searching...
No Matches
Shellminator.hpp
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
35#ifndef SHELLMINATOR_HPP_
36#define SHELLMINATOR_HPP_
37
39#include "Shellminator-IO.hpp"
40
41#ifdef ARDUINO
42#include "Arduino.h"
43#endif
44
45#ifdef __AVR__
46
47 #include <avr/pgmspace.h>
48
49#endif
50
51#include "Stream.h"
52
53#ifdef SHELLMINATOR_USE_WIFI_CLIENT
54 #ifdef ESP8266
55 #include <ESP8266WiFi.h>
56 #endif
57
58 #ifdef ESP32
59 #include <WiFi.h>
60 #endif
61
62 #ifdef SHELLMINATOR_ENABLE_WEBSOCKET_MODULE
63 #include <WebSocketsServer.h>
64 #endif
65
66#endif
67
68#ifdef SHELLMINATOR_ENABLE_PASSWORD_MODULE
69
70 #include "external/sha256/terminal_sha256.h"
71
72#endif
73
74#ifdef __has_include
75 #if __has_include ("Commander-API.hpp")
76 #include "Commander-API.hpp"
77 #endif
78#endif
79
80#ifdef COMMANDER_API_VERSION
81#include "Commander-API.hpp"
82#endif
83
84#include <stdio.h>
85#include <stdint.h>
86#include <string.h>
87
94
95#ifdef SHELLMINATOR_ENABLE_QR_SUPPORT
96
98
99#endif
100
102#define SHELLMINATOR_VERSION "1.1.2"
103
111
112public:
113
117 enum {
118 BLACK = 30,
119 RED = 31,
120 GREEN = 32,
121 YELLOW = 33,
122 BLUE = 34,
124 CYAN = 36,
125 WHITE = 37
126 };
127
131 enum {
133 BOLD = 1,
140 INVISIBLE = 8
141 };
142
144 static const char *version;
145
146#ifdef SHELLMINATOR_USE_WIFI_CLIENT
147
148 Shellminator( WiFiServer *server_p );
149
150 Shellminator( WiFiServer *server_p, void( *execution_fn_p )( char* ) );
151
152 void beginServer();
153
154 void stopServer();
155
156 void setClientTimeout( uint16_t clientTimeout_p );
157
158#endif
159
160#ifdef SHELLMINATOR_ENABLE_WEBSOCKET_MODULE
161
162 Shellminator( WebSocketsServer *wsServer_p );
163
164 Shellminator( WebSocketsServer *wsServer_p, uint8_t serverID_p );
165
166 Shellminator( WebSocketsServer *wsServer_p, uint8_t serverID_p, void( *execution_fn_p )( char* ) );
167
168 void webSocketPush( uint8_t data );
169
170 void webSocketPush( uint8_t* data, size_t size );
171
172 void websocketDisconnect();
173
174#endif
175
176 Shellminator( Stream *stream_p );
177 Shellminator( Stream *stream_p, void( *execution_fn_p )( char* ) );
178
183 void addExecFunc( void( *execution_fn_p )( char* ) );
184
191 void begin( char* banner_p );
192
199 void begin( const char* banner_p );
200
205 void sendBackspace();
206
210 void clear();
211
217 void update();
218
225 void setTerminalCharacterColor( uint8_t style, uint8_t color );
226
235 void setTerminalCharacterColor( char* buff, uint8_t style, uint8_t color );
236
245 static void setTerminalCharacterColor( Stream *stream_p, uint8_t style, uint8_t color );
246
250 void drawLogo();
251
253 void printBanner();
254
255 void printHistory();
256
257 void printHelp();
258
263 void setBannerText( char* banner_p );
264
269 void setBannerText( const char* banner_p );
270
271 void setBannerPathText( char* bannerPath_p );
272 void setBannerPathText( const char* bannerPath_p );
273
281 void attachLogo( char* logo_p );
282
290 void attachLogo( const char* logo_p );
291
292 #ifdef __AVR__
300 void attachLogo( __FlashStringHelper * progmemLogo_p );
301 #endif
302
309 void overrideUpArrow( void( *func )( void ) );
310
317 void overrideDownArrow( void( *func )( void ) );
318
325 void overrideLeftArrow( void( *func )( void ) );
326
333 void overrideRightArrow( void( *func )( void ) );
334
342 void overrideAbortKey( void( *func )( void ) );
343
350 void overridePageUpKey( void( *func )( void ) );
351
358 void overridePageDownKey( void( *func )( void ) );
359
366 void overrideHomeKey( void( *func )( void ) );
367
374 void overrideEndKey( void( *func )( void ) );
375
383 void overrideLogoutKey( void( *func )( void ) );
384
392 void overrideSearchKey( void( *func )( void ) );
393
399 void freeUpArrow();
400
406 void freeDownArrow();
407
413 void freeLeftArrow();
414
420 void freeRightArrow();
421
427 void freeAbortKey();
428
434 void freePageUpKey();
435
441 void freePageDownKey();
442
448 void freeHomeKey();
449
455 void freeEndKey();
456
462 void freeLogoutKey();
463
469 void freeSearchKey();
470
471 #ifdef SHELLMINATOR_USE_WIFI_CLIENT
472
474 void clientDisconnect();
475
476 #endif
477
478 #ifdef SHELLMINATOR_ENABLE_PASSWORD_MODULE
479
480 void enablePasswordProtection( uint8_t* passwordHashAddress_p );
481 void enablePasswordProtection( const uint8_t* passwordHashAddress_p );
482 void enablePasswordProtection( char* passwordHashAddress_p );
483 void enablePasswordProtection( const char* passwordHashAddress_p );
484 void disablePasswordProtection();
485 bool checkPassword( uint8_t* pwStr );
486 bool checkPassword( const uint8_t* pwStr );
487 bool checkPassword( char* pwStr );
488 bool checkPassword( const char* pwStr );
489
490 #endif
491
493 void beep();
494
498 bool enableFormatting = true;
499
500 bool mute = false;
501
502 #ifdef COMMANDER_API_VERSION
503
504 void attachCommander( Commander* commander_p );
505
506 #endif
507
508 // Configuration for QR code specific parts.
509 #ifdef SHELLMINATOR_ENABLE_QR_SUPPORT
510
518 void generateQRText( char* text );
519
527 void generateQRText( const char* text );
528
536 void generateQRText( char* text, enum qrcodegen_Ecc ecc );
537
545 void generateQRText( const char* text, enum qrcodegen_Ecc ecc );
546
547 #endif
548
549private:
550
556 char *logo = NULL;
557
558 #ifdef __AVR__
559 __FlashStringHelper *progmemLogo = NULL;
560 #endif
561
564 void( *execution_fn )( char* );
565
577 char cmd_buff[ SHELLMINATOR_BUFF_DIM ][ SHELLMINATOR_BUFF_LEN + 1 ] = { { 0 } };
578
580 uint32_t cmd_buff_dim = 1;
581
583 uint32_t cmd_buff_cntr = 0;
584
586 uint32_t cursor = 0;
587
589 uint32_t escape_state = 0;
590
592 char banner[ SHELLMINATOR_BANNER_LEN ] = { '\0' };
593
594 char bannerPath[ SHELLMINATOR_BANNER_PATH_LEN ] = "$";
595
598 uint8_t lastBannerSize = 0;
599
601 void( *upArrowOverrideFunc )( void ) = NULL;
602
604 void( *downArrowOverrideFunc )( void ) = NULL;
605
607 void( *leftArrowOverrideFunc )( void ) = NULL;
608
610 void( *rightArrowOverrideFunc )( void ) = NULL;
611
613 void( *abortKeyFunc )( void ) = NULL;
614
616 void( *pageUpKeyFunc )( void ) = NULL;
617
619 void( *pageDownKeyFunc )( void ) = NULL;
620
622 void( *homeKeyFunc )( void ) = NULL;
623
625 void( *endKeyFunc )( void ) = NULL;
626
628 void( *logoutKeyFunc )( void ) = NULL;
629
631 void( *searchKeyFunc )( void ) = NULL;
632
639 void process( char new_char );
640
642 void redrawLine();
643
644 //---- Communication channels ----//
645
646 #ifdef SHELLMINATOR_ENABLE_HIGH_MEMORY_USAGE
647 // It is used for the ESP32 and ESP8266.
648 // They are very slow to send only one byte of data.
649 char acceleratorBuffer[ SHELLMINATOR_ACCELERATOR_BUFFER_LEN ];
650 char *acceleratorBufferPtr;
651 #endif
652
653 #ifdef SHELLMINATOR_USE_WIFI_CLIENT
654
655 WiFiServer *server = NULL;
656 WiFiClient client;
657 bool clientConnected = false;
658 uint8_t telnetNegotiationState = 0;
659 uint16_t clientTimeout = 1000;
660
661 // https://www.omnisecu.com/tcpip/telnet-commands-and-options.php
662 static const uint8_t TELNET_IAC_DONT_LINEMODE[ 3 ];
663 static const uint8_t TELNET_IAC_WILL_ECHO[ 3 ];
664 static const uint8_t TELNET_IAC_DONT_ECHO[ 3 ];
665 static const uint8_t TELNET_IAC_WILL_SUPRESS_GO_AHEAD[ 3 ];
666 static const uint8_t TELNET_IAC_DO_SUPRESS_GO_AHEAD[ 3 ];
667
668 #endif
669
670 static const char helpText[];
671
672 #ifdef SHELLMINATOR_ENABLE_WEBSOCKET_MODULE
673
674 WebSocketsServer *wsServer = NULL;
675 uint8_t serverID;
676 shellminatorWebSocketChannel webSocketChannel;
677
678 #endif
679
681 shellminatorDefaultChannel defaultChannel;
682
685 //shellminatorChannel *channel = &defaultChannel;
686
687 Stream *channel = &defaultChannel;
688
689 //---- Commander-API support specific part ----//
690 #ifdef COMMANDER_API_VERSION
691
693 Commander* commander = NULL;
694
696 uint32_t commandCheckTimerStart = 0;
697
699 bool commandChecked = false;
700
703 bool commandFound = false;
704
705 #endif
706
707 #ifdef SHELLMINATOR_ENABLE_SEARCH_MODULE
708
709 void historySearchBackward();
710 void historySearchForward();
711 void redrawHistorySearch();
712 int substring( char* str1, char* str2 );
713
714 bool inSearch = false;
715 int32_t searchMatch;
716
717 #endif
718
719 #ifdef SHELLMINATOR_ENABLE_PASSWORD_MODULE
720
721
722
723 SHA256_CTX passwordHashCtx;
724 uint8_t passwordHashBuffer[ SHA256_BLOCK_SIZE ];
725 uint8_t* passwordHashAddress = NULL;
726
727 #endif
728
729 // QR-code configuration specific parts.
730 #ifdef SHELLMINATOR_ENABLE_QR_SUPPORT
731
732 uint8_t qr_data[ qrcodegen_BUFFER_LEN_MAX ];
733 uint8_t qr_tempBuff[ qrcodegen_BUFFER_LEN_MAX ];
734
735 #endif
736
737};
738
739#endif
#define SHELLMINATOR_BANNER_PATH_LEN
#define SHELLMINATOR_BUFF_LEN
#define SHELLMINATOR_BANNER_LEN
#define SHELLMINATOR_ACCELERATOR_BUFFER_LEN
If defined, enables high memory mode.
#define SHELLMINATOR_BUFF_DIM
Shellminator object.
void begin(char *banner_p)
Shellminator initialization function.
void overrideHomeKey(void(*func)(void))
Override Home key behaviour.
void generateQRText(char *text)
This function generates a QR-code from text.
void overrideLogoutKey(void(*func)(void))
Override Logout key behaviour.
void clear()
Clear screen.
void freeLeftArrow()
Reset left arrow key functionality to default.
void overrideUpArrow(void(*func)(void))
Override up arrow key behaviour.
void printHistory()
void update()
Update function.
void addExecFunc(void(*execution_fn_p)(char *))
Execution function adder function.
bool enableFormatting
This flag enables or disables character formatting.
void overrideRightArrow(void(*func)(void))
Override right arrow key behaviour.
void freeAbortKey()
Reset abort key functionality to default.
void freePageUpKey()
Reset Page-Up key functionality to default.
void clientDisconnect()
Disconnect WiFiClient telnet client.
void freeLogoutKey()
Reset Logout key functionality to default.
void freeEndKey()
Reset End key functionality to default.
void sendBackspace()
Sends a backspace.
void overrideEndKey(void(*func)(void))
Override End key behaviour.
void beginServer()
void printBanner()
This function prints the banner text.
void freeUpArrow()
Reset up arrow key functionality to default.
void overrideDownArrow(void(*func)(void))
Override down arrow key behaviour.
void freeHomeKey()
Reset Home key functionality to default.
void beep()
Generate a beep sound on the terminal device.
void overridePageDownKey(void(*func)(void))
Override Page-Down key behaviour.
void setBannerPathText(char *bannerPath_p)
void overridePageUpKey(void(*func)(void))
Override Page-Up key behaviour.
void overrideSearchKey(void(*func)(void))
Override Search key behaviour.
void overrideAbortKey(void(*func)(void))
Override abort key behaviour.
void attachLogo(char *logo_p)
This function attaches a logo to the terminal.
void overrideLeftArrow(void(*func)(void))
Override left arrow key behaviour.
void setTerminalCharacterColor(uint8_t style, uint8_t color)
Bring some color into your code.
void freeRightArrow()
Reset right arrow key functionality to default.
void drawLogo()
Draws the startup logo.
void setBannerText(char *banner_p)
This function sets the banner text.
void setClientTimeout(uint16_t clientTimeout_p)
void freeSearchKey()
Reset Search key functionality to default.
void freeDownArrow()
Reset down arrow key functionality to default.
static const char * version
String that holds the version information.
void freePageDownKey()
Reset Page-Down key functionality to default.
Shellminator channel class.
qrcodegen_Ecc
Definition: qrcodegen.h:57
#define qrcodegen_BUFFER_LEN_MAX
Definition: qrcodegen.h:144