ERIS CORE
svcSerialCommandInterface.h
Go to the documentation of this file.
1 /**
2  * @file svcSerialCommandInterface.h
3  * @author Brian Monkaba (brian.monkaba@gmail.com)
4  * @brief
5  * @version 0.1
6  * @date 2021-08-24
7  *
8  * @copyright Copyright (c) 2021
9  *
10  */
11 #ifndef _AppSCI_
12 #define _AppSCI_
13 
14 #include "Eris.h"
15 #include <string.h>
16 #include "AppManager.h"
17 #include "AppBaseClass.h"
18 #include <SdFat.h>
19 #include <Print.h>
20 
21 /**
22  * @brief Serial communication service and interface
23  * Serial commands (rx): \n
24  *
25  * LS [PATH]
26  * GET [PATH] get file is responded with FS messages \n
27  * ACON request current audio block connections \n
28  * CONNECT make an audio block connection \n
29  * DISCONNECT break an audio block connection \n
30  * APC audio block parameter control message \n
31  * AA broadcast message to active app \n
32  * STATS \n
33  * CQT_CFG request a dump of the cqt bin configs \n
34  * GET_DD request a dump of the data dictionary \n
35  * UPDATE_DD key val set a key value \n
36  *\n
37  * Serial messages (tx):\n
38  *\n
39  * CQT_H (CQT BINS - High Range) \n
40  * CQT_L (CQT " Low Range") \n
41  * CQT_EOF (End of analysis frame) \n
42  * DIR (directory contents - aka the result of the ls command) \n
43  * DIR_EOF \n
44  * S (FFT window stream) \n
45  * FS (file stream) \n
46  * FS_END (file stream end, indicate to client eof) \n
47  * GET_ERR (file request error response ) \n
48  * ACON START (begin audio connection list) \n
49  * ACON END (end audio connection list) \n
50  * RAM1 JSON container \n
51  * RAM2 JSON container \n
52  */
54  public:
56  update_priority = 20;
57  et_since_poll = 0;
60  index_rx_buffer = 0;
61  index_tx_buffer = 0;
63  stream_pos = 0;
64  is_streaming_file = false;
65  is_capturing_bulk_data = false;
68 #ifdef USE_EXTMEM
69  p_capture_buffer = (char*)extmem_malloc(SERIAL_RX_CAPTURE_BUFFER_SIZE);
70  p_working_buffer = (char*)extmem_malloc(SERIAL_WORKING_BUFFER_SIZE);
71 
72  p_multipart_header = (char*)extmem_malloc(SERIAL_TX_HEADER_BUFFER_SIZE);
73  p_received_chars = (char*)extmem_malloc(SERIAL_RX_BUFFER_SIZE); // an array to store the received data
74  p_stream_path = (char*)extmem_malloc(SERIAL_PARAM_BUFFER_SIZE);
75  p_stream_file = (char*)extmem_malloc(SERIAL_PARAM_BUFFER_SIZE);
76  p_tx_Buffer = (char*)extmem_malloc(SERIAL_OUTPUT_BUFFER_SIZE);
77 #else
78  captureBuffer = 0;
79  workingBuffer = 0;
80 #endif
81  strcpy(p_multipart_header,"");
82  strcpy(p_stream_path,"");
83  strcpy(p_stream_file,"");
84  stream_pos = 0;
86  strcpy(name,"SCI");
87  memset(p_tx_Buffer,0,SERIAL_OUTPUT_BUFFER_SIZE);
88  memset(p_received_chars,0,SERIAL_RX_BUFFER_SIZE);
89  };
90 
91  /**
92  * @brief zero out the transmit buffer and reset the write index
93  *
94  */
95  void empty(){
96  memset(p_tx_Buffer,0,SERIAL_OUTPUT_BUFFER_SIZE);
97  index_tx_buffer = 0;
98  }
99 
100  /**
101  * @brief request to start a lz4 compressed message \n
102  * starts the message and returns true if available
103  * returns false if busy
104  * @return true
105  * @return false
106  */
108  if(is_streaming_file) return false;
109  if (Serial.availableForWrite() < 6000) return false;
110  startLZ4Message();
111  return true;
112  }
113 
114  void sendLZ4Message();
115  void send();
116  bool throttle();
117  protected:
118  SdFs *p_SD;
119  FsFile file;
120  uint16_t index_rx_buffer;
122  uint16_t index_tx_buffer;
124  char *p_received_chars; // an array to store the received data
127  char *p_tx_Buffer;
128  //used for lz4 tx compressor
131  uint64_t stream_pos;
135  volatile bool tx_buffer_overflow_flag;
136  elapsedMillis et_since_poll;
139  uint16_t checksum(const char *msg);
140  void streamTransmitHandler();
141  void streamReceiveHandler();
142  void messageHandler_LS();
143  void messageHandler_GET();
151  void render() override{}; //called only when the app is active
152  void update() override;
153  void txOverflowHandler();
155  empty();
156  }
157  size_t write(uint8_t c){
159  if (index_tx_buffer == SERIAL_OUTPUT_BUFFER_SIZE-10){
160  p_tx_Buffer[index_tx_buffer++] = 0; //make sure the buffer is null terminated
162  }
163  return 1;
164  };
165  void flush(){
166  while(throttle()){
167  delay(1);
168  }
169  if (strlen(p_tx_Buffer) > 0 ) Serial.print(p_tx_Buffer);
170  memset(p_tx_Buffer,0,SERIAL_OUTPUT_BUFFER_SIZE);
171  index_tx_buffer = 0;
172  tx_buffer_overflow_flag = false;
173  };
174 };
175 
176 #endif
char name[MAX_NAME_LENGTH]
Definition: AppBaseClass.h:74
uint16_t update_priority
Definition: AppBaseClass.h:52
base class definition / implementation from which all app classes will be derived and override
Definition: AppBaseClass.h:34
SdFs * getSD()
provides an interface for apps to request the SdFs object
Definition: AppManager.cpp:366
static AppManager * getInstance()
Definition: AppManager.h:66
uint16_t checksum(const char *msg)
bool requestStartLZ4Message()
request to start a lz4 compressed message starts the message and returns true if available returns f...
bool throttle()
returns true if the available serial buffer falls below SERIAL_THROTTLE_BUFFER_REMAINING_THRESHOLD if...
void send()
immediately transmit then clear the txBuffer
void render() override
render loop
void empty()
zero out the transmit buffer and reset the write index
void update() override
update loop
void sendLZ4Message()
Calling this function signals the end of a compressed message. The txBuffer contents are lz4 compres...
Serial communication service and interface Serial commands (rx): .