Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 69 additions & 16 deletions Firmware/LoRaSerial/Commands.ino
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,14 @@ bool sendRemoteCommand(const char * commandString)
commandTXBuffer[commandTXHead++] = commandString[x];
commandTXHead %= sizeof(commandTXBuffer);
}
if (settings.debugSerial)
{
systemPrint("sendRemoteCommand moved ");
systemPrint(commandLength);
systemPrintln(" from commandBuffer into commandTXBuffer");
}
remoteCommandResponse = false;
waitRemoteCommandResponse = true;
return true;
}

Expand Down Expand Up @@ -812,11 +819,26 @@ void checkCommand()
char * commandString;
int index;
int prefixLength;
PrinterEndpoints responseDestination;
uint16_t responseLength;
bool success;

//Verify the command length
//Save previous index
if (settings.debugSerial)
{
responseDestination = printerEndpoint;
if (responseDestination == PRINT_TO_SERIAL)
responseLength = txHead;
else
responseLength = commandTXHead;
}

//Zero terminate the string
success = false;
commandString = trimCommand(); //Remove any leading or trailing whitespace
commandBuffer[commandLength] = 0;

//Remove any whitespace
commandString = trimCommand();

//Upper case the command
for (index = 0; index < commandLength; index++)
Expand Down Expand Up @@ -845,6 +867,8 @@ void checkCommand()
break;
}
}
else if (!commandLength)
success = true;

//Print the command failure
petWDT();
Expand All @@ -855,6 +879,28 @@ void checkCommand()
systemPrintln("ERROR");
commandComplete(false);
}

//Display the response
printerEndpoint = PRINT_TO_SERIAL;
if (settings.debugSerial)
{
uint16_t tail;
tail = responseLength;
if (responseDestination == PRINT_TO_SERIAL)
{
responseLength = (txHead + sizeof(serialTransmitBuffer) - responseLength) % sizeof(serialTransmitBuffer);
systemPrint("checkCommand placed ");
systemPrint(responseLength);
systemPrintln(" command response bytes into serialTransmitBuffer");
}
else
{
responseLength = (commandTXHead + sizeof(commandTXBuffer) - responseLength) % sizeof(commandTXBuffer);
systemPrint("checkCommand placed ");
systemPrint(responseLength);
systemPrintln(" command response bytes into commandTXBuffer");
}
}
outputSerialData(true);
petWDT();

Expand Down Expand Up @@ -903,24 +949,31 @@ void commandReset()
//Remove any preceeding or following whitespace chars
char * trimCommand()
{
char * commandString = commandBuffer;
int index;
int j;

//Remove the leading white space
while (isspace(*commandString))
//Remove the comment
for (index = 0; index < commandLength; index++)
{
commandString++;
--commandLength;
if (commandBuffer[index] == '#')
{
commandBuffer[index] = 0;
commandLength = index;
break;
}
}

//Zero terminate the string
commandString[commandLength] = 0;

//Remove the trailing white space
while (commandLength && isspace(commandString[commandLength - 1]))
commandString[--commandLength] = 0;

//Return the remainder as the command
return commandString;
//Remove the white space
for (index = 0; index < commandLength; index++)
{
while (isspace(commandBuffer[index]))
{
for (j = index + 1; j < commandLength; j++)
commandBuffer[j - 1] = commandBuffer[j];
commandBuffer[--commandLength] = 0;
}
}
return commandBuffer;
}

//Display all of the commands
Expand Down
1 change: 1 addition & 0 deletions Firmware/LoRaSerial/LoRaSerial.ino
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ const long minEscapeTime_ms = 2000; //Serial traffic must stop this amount befor
bool inCommandMode = false; //Normal data is prevented from entering serial output when in command mode
uint8_t commandLength = 0;
bool remoteCommandResponse;
bool waitRemoteCommandResponse;

bool rtsAsserted; //When RTS is asserted, host says it's ok to send data
bool forceRadioReset = false; //Goes true when a setting requires a link/radio reset to work
Expand Down
5 changes: 4 additions & 1 deletion Firmware/LoRaSerial/Radio.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2393,7 +2393,10 @@ PacketType validateDatagram(VIRTUAL_CIRCUIT * vc, PacketType datagramType, uint8
}

//Verify that there is sufficient space in the serialTransmitBuffer
if (inCommandMode || (freeBytes < rxDataBytes))
//Apply back pressure if the remote system is trying to send data while
//the local system is in command mode. Remote command responses should
//be received.
if ((inCommandMode && (datagramType == DATAGRAM_DATA)) || (freeBytes < rxDataBytes))
{
if (settings.debugReceive || settings.debugDatagrams)
{
Expand Down
43 changes: 37 additions & 6 deletions Firmware/LoRaSerial/Serial.ino
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ void updateSerial()
{
commandLength = availableRXCommandBytes();

//Don't overflow the command buffer, save space for the zero termination
if (commandLength >= sizeof(commandBuffer))
commandLength = sizeof(commandBuffer) - 1;

for (x = 0 ; x < commandLength ; x++)
{
commandBuffer[x] = commandRXBuffer[commandRXTail++];
Expand All @@ -375,7 +379,6 @@ void updateSerial()
commandBuffer[0] = 'A'; //Convert this RT command to an AT command for local consumption
printerEndpoint = PRINT_TO_RF; //Send prints to RF link
checkCommand(); //Parse the command buffer
printerEndpoint = PRINT_TO_SERIAL;
remoteCommandResponse = true;
}
else
Expand All @@ -396,6 +399,7 @@ void processSerialInput()
radioHead = radioTxHead;
while (availableRXBytes()
&& (availableRadioTXBytes() < (sizeof(radioTxBuffer) - maxEscapeCharacters))
&& ((!inCommandMode) || (!waitRemoteCommandResponse))
&& (transactionComplete == false))
{
//Take a break if there are ISRs to attend to
Expand All @@ -414,11 +418,22 @@ void processSerialInput()
//Process serial into either rx buffer or command buffer
if (inCommandMode == true)
{
if (incoming == '\r' && commandLength > 0)
//Check for end of command
if ((incoming == '\r') || (incoming == ';'))
{
printerEndpoint = PRINT_TO_SERIAL;
systemPrintln();
checkCommand(); //Process command buffer
//Ignore end of command if no command in the buffer
if (commandLength > 0)
{
systemPrintln();
if (settings.debugSerial)
{
systemPrint("processSerialInput moved ");
systemPrint(commandLength);
systemPrintln(" from serialReceiveBuffer into commandBuffer");
}
checkCommand(); //Process command buffer
break;
}
}
else if (incoming == '\n')
; //Do nothing
Expand All @@ -443,7 +458,23 @@ void processSerialInput()
{
//Move this character into the command buffer
commandBuffer[commandLength++] = incoming;
commandLength %= sizeof(commandBuffer);

//Don't allow the command to overflow the command buffer
//Process the long command instead
//Save room for the zero termination
if (commandLength >= (sizeof(commandBuffer) - 1))
{
printerEndpoint = PRINT_TO_SERIAL;
systemPrintln();
if (settings.debugSerial)
{
systemPrint("processSerialInput moved ");
systemPrint(commandLength);
systemPrintln(" from serialReceiveBuffer into commandBuffer");
}
checkCommand(); //Process command buffer
break;
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions Firmware/LoRaSerial/States.ino
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,7 @@ void updateRadioState()
outputSerialData(true);
}
serialBufferOutput(rxData, rxDataBytes);
waitRemoteCommandResponse = false;

//Transmit ACK
P2P_SEND_ACK(TRIGGER_TX_ACK);
Expand Down Expand Up @@ -2982,6 +2983,7 @@ void discardPreviousData()
txTail = txHead;
commandRXTail = commandRXHead;
commandTXTail = commandTXHead;
waitRemoteCommandResponse = false;
}

//Output VC link status
Expand Down Expand Up @@ -3047,6 +3049,10 @@ void vcSendPcStateMessage(int8_t vcIndex, uint8_t state)
//Build the VC state message
VC_STATE_MESSAGE message;
message.vcState = state;
if (virtualCircuitList[vcIndex].flags.valid)
memcpy(&message.uniqueId[0], &virtualCircuitList[vcIndex].uniqueId[0], sizeof(message.uniqueId));
else
memset(message.uniqueId, UNIQUE_ID_ERASE_VALUE, sizeof(message.uniqueId));

//Build the message header
VC_SERIAL_MESSAGE_HEADER header;
Expand Down
7 changes: 6 additions & 1 deletion Firmware/LoRaSerial/Virtual_Circuit_Protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,15 @@ typedef struct _VC_SERIAL_MESSAGE_HEADER
} VC_SERIAL_MESSAGE_HEADER;

#define VC_SERIAL_HEADER_BYTES (sizeof(VC_SERIAL_MESSAGE_HEADER)) //Length of the serial VC header in bytes
#define UNIQUE_ID_ERASE_VALUE 0xff
#ifndef UNIQUE_ID_BYTES
#define UNIQUE_ID_BYTES 16
#endif //UNIQUE_ID_BYTES

typedef struct _VC_STATE_MESSAGE
{
uint8_t vcState; //VC state
uint8_t vcState; //VC state
uint8_t uniqueId[UNIQUE_ID_BYTES]; //Unique ID for the LoRaSerial radio, all 0xFF if unknown
} VC_STATE_MESSAGE;

typedef enum
Expand Down
Loading