Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed sending message from PC / Fix for ENABLE_MESSENGER_UART #120

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
40 changes: 21 additions & 19 deletions app/messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "helper/crypto.h"
#endif

#if defined(ENABLE_UART) && defined(ENABLE_UART_DEBUG)
#if (defined(ENABLE_UART) && defined(ENABLE_UART_DEBUG)) || (defined(ENABLE_MESSENGER_UART))
#include "driver/uart.h"
#endif

Expand Down Expand Up @@ -587,7 +587,7 @@ void MSG_SendPacket() {
FUNCTION_Select(FUNCTION_TRANSMIT);

SYSTEM_DelayMs(50);

MSG_FSKSendData();

SYSTEM_DelayMs(50);
Expand All @@ -607,7 +607,7 @@ void MSG_SendPacket() {

// clear packet buffer
MSG_ClearPacketBuffer();

msgStatus = READY;

} else {
Expand Down Expand Up @@ -728,12 +728,11 @@ void MSG_HandleReceive(){
#else
snprintf(rxMessage[3], PAYLOAD_LENGTH + 2, "< %s", dataPacket.data.payload);
#endif
#ifdef ENABLE_MESSENGER_UART
UART_printf("SMS<%s\r\n", dataPacket.data.payload);
#endif
}

#ifdef ENABLE_MESSENGER_UART
UART_printf("SMS<%s\r\n", dencryptedTxMessage);
#endif

if ( gScreenToDisplay != DISPLAY_MSG ) {
hasNewMessage = 1;
gUpdateStatus = true;
Expand All @@ -748,8 +747,8 @@ void MSG_HandleReceive(){
}

// Transmit a message to the sender that we have received the message
if (dataPacket.data.header == MESSAGE_PACKET ||
dataPacket.data.header == ENCRYPTED_MESSAGE_PACKET)
if (dataPacket.data.header == MESSAGE_PACKET ||
dataPacket.data.header == ENCRYPTED_MESSAGE_PACKET)
{
// wait so the correspondent radio can properly receive it
SYSTEM_DelayMs(700);
Expand Down Expand Up @@ -819,7 +818,7 @@ void processBackspace() {

void MSG_ProcessKeys(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) {
uint8_t state = bKeyPressed + 2 * bKeyHeld;

if (state == MSG_BUTTON_EVENT_SHORT) {

switch (Key)
Expand Down Expand Up @@ -856,14 +855,7 @@ void MSG_ProcessKeys(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) {
break;*/
case KEY_MENU:
// Send message
MSG_ClearPacketBuffer();
#ifdef ENABLE_ENCRYPTION
dataPacket.data.header=ENCRYPTED_MESSAGE_PACKET;
#else
dataPacket.data.header=MESSAGE_PACKET;
#endif
memcpy(dataPacket.data.payload, cMessage, sizeof(dataPacket.data.payload));
MSG_SendPacket();
MSG_Send(cMessage);
break;
case KEY_EXIT:
gRequestDisplayScreen = DISPLAY_MAIN;
Expand Down Expand Up @@ -894,5 +886,15 @@ void MSG_ClearPacketBuffer()
memset(dataPacket.serializedArray, 0, sizeof(dataPacket.serializedArray));
}

void MSG_Send(const char *cMessage){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this refactor. Good job!

MSG_ClearPacketBuffer();
#ifdef ENABLE_ENCRYPTION
dataPacket.data.header=ENCRYPTED_MESSAGE_PACKET;
#else
dataPacket.data.header=MESSAGE_PACKET;
#endif
memcpy(dataPacket.data.payload, cMessage, sizeof(dataPacket.data.payload));
MSG_SendPacket();
}

#endif
#endif
5 changes: 3 additions & 2 deletions app/messenger.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ typedef enum PacketType {

// Data Packet definition // 2024 kamilsss655
union DataPacket
{
{
struct{
uint8_t header;
uint8_t payload[PAYLOAD_LENGTH];
Expand All @@ -63,7 +63,8 @@ void MSG_FSKSendData();
void MSG_ClearPacketBuffer();
void MSG_SendAck();
void MSG_HandleReceive();
void MSG_Send(const char *cMessage);

#endif

#endif
#endif
54 changes: 43 additions & 11 deletions app/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
#include "sram-overlay.h"
#endif
#include "version.h"
#ifdef ENABLE_MESSENGER
#ifdef ENABLE_MESSENGER_UART
#include "app/messenger.h"
#include "external/printf/printf.h"
#endif
#endif


#define DMA_INDEX(x, y) (((x) + (y)) % sizeof(UART_DMA_Buffer))

Expand Down Expand Up @@ -212,7 +219,7 @@ static void CMD_0514(const uint8_t *pBuffer)
#endif

gSerialConfigCountDown_500ms = 12; // 6 sec

// turn the LCD backlight off
BACKLIGHT_TurnOff();

Expand Down Expand Up @@ -260,7 +267,7 @@ static void CMD_051D(const uint8_t *pBuffer)
return;

gSerialConfigCountDown_500ms = 12; // 6 sec

bReloadEeprom = false;

#ifdef ENABLE_FMRADIO
Expand Down Expand Up @@ -369,6 +376,31 @@ bool UART_IsCommandAvailable(void)
if (gUART_WriteIndex == DmaLength)
return false;

#ifdef ENABLE_MESSENGER
#ifdef ENABLE_MESSENGER_UART

if (UART_DMA_Buffer[gUART_WriteIndex] == 'S' && UART_DMA_Buffer[gUART_WriteIndex + 1] == 'M' && UART_DMA_Buffer[gUART_WriteIndex + 2] == 'S' && UART_DMA_Buffer[gUART_WriteIndex + 3] == ':')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use memcmp() here for cleaner code?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was also wondering if we need to require data to start with SMS, instead of sending any serial data that comes in?
So it could be a bit more universal. Are there any implications of this?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh nevermind I guess reading from CHIRP would then trigger message send. Perhaps in the future it should be limited to only when messenger screen is active.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe if (strncmp(((char*)UART_DMA_Buffer) + gUART_WriteIndex, "SMS:",4) == 0) ?

{

char txMessage[PAYLOAD_LENGTH + 4];
memset(txMessage, 0, sizeof(txMessage));
snprintf(txMessage, (PAYLOAD_LENGTH + 4), "%s", &UART_DMA_Buffer[gUART_WriteIndex + 4]);

for (int i = 0; txMessage[i] != '\0'; i++)
{
if (txMessage[i] == '\r' || txMessage[i] == '\n')
txMessage[i] = '\0';
}
if (strlen(txMessage) > 0)
{
MSG_Send(txMessage);
UART_printf("SMS>%s\r\n", txMessage);
gUpdateDisplay = true;
}
}

#endif
#endif
while (gUART_WriteIndex != DmaLength && UART_DMA_Buffer[gUART_WriteIndex] != 0xABU)
gUART_WriteIndex = DMA_INDEX(gUART_WriteIndex, 1);

Expand Down Expand Up @@ -442,7 +474,7 @@ bool UART_IsCommandAvailable(void)
for (i = 0; i < (Size + 2u); i++)
UART_Command.Buffer[i] ^= Obfuscation[i % 16];
}

CRC = UART_Command.Buffer[Size] | (UART_Command.Buffer[Size + 1] << 8);

return (CRC_Calculate(UART_Command.Buffer, Size) != CRC) ? false : true;
Expand All @@ -455,33 +487,33 @@ void UART_HandleCommand(void)
case 0x0514:
CMD_0514(UART_Command.Buffer);
break;

case 0x051B:
CMD_051B(UART_Command.Buffer);
break;

case 0x051D:
CMD_051D(UART_Command.Buffer);
break;

case 0x051F: // Not implementing non-authentic command
break;

case 0x0521: // Not implementing non-authentic command
break;

case 0x0527:
CMD_0527();
break;

case 0x0529:
CMD_0529();
break;

case 0x052F:
CMD_052F(UART_Command.Buffer);
break;

case 0x05DD:
#if defined(ENABLE_OVERLAY)
overlay_FLASH_RebootToBootloader();
Expand Down
14 changes: 14 additions & 0 deletions driver/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "bsp/dp32g030/syscon.h"
#include "bsp/dp32g030/uart.h"
#include "driver/uart.h"
#include "external/printf/printf.h"

static bool UART_IsLogEnabled;
uint8_t UART_DMA_Buffer[256];
Expand Down Expand Up @@ -102,3 +103,16 @@ void UART_LogSend(const void *pBuffer, uint32_t Size)
UART_Send(pBuffer, Size);
}
}

void UART_printf(const char *str, ...)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we reuse LogUart() from debugging.h ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not for debug, but for send/recv via serial port. I think it should not be mixed.

{
char text[256];
int len;

va_list va;
va_start(va, str);
len = vsnprintf(text, sizeof(text), str, va);
va_end(va);

UART_Send(text, len);
}
3 changes: 2 additions & 1 deletion driver/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ void UART_Init(void);
void UART_Send(const void *pBuffer, uint32_t Size);
void UART_LogSend(const void *pBuffer, uint32_t Size);

#endif
void UART_printf(const char *str, ...);

#endif