📄 common.c
字号:
return SendUartRStr(s, ConsoleUart);
}
bool SendDebugData(char *d, u16 len)
{ // send data from ram
if (!(Flags1 & (1<<Flags1_Debug))) return true; // don't bother with debug unless the debug flag is set
return SendUartData(d, len, ConsoleUart);
}
bool SendDebugState(void)
{
if (!SendConsoleRStr(DebugMessagesStr1)) return false;
if (Flags1 & (1<<Flags1_Debug))
return SendConsoleRStr(DebugMessagesStr3);
else
return SendConsoleRStr(DebugMessagesStr2);
}
#endif
bool SendModemByte(char c)
{
return SendUartByte(c, ModemUart);
}
bool SendModemRStr(RomChar *s)
{
return SendUartRStr(s, ModemUart);
}
bool SendModemStr(char *s)
{
return SendUartStr(s, ModemUart);
}
#ifdef Debug
void SendDebugDataDump(u8 *Buf, int len)
{ // send a hex dump
int i; //
u8 b; //
char buffer[8]; //
//
if (!Buf) return; //
//
for (i = 0; i < len; i++) //
{ //
b = *Buf++; //
sprintf(buffer, " %02X", b); //
if (!SendDebugStr(buffer)) return; //
} //
SendDebugStr("\n"); //
}
void SendDebugAsciiDump(u8 *Buf, int len)
{ // send a text dump
int i; //
u8 b; //
char buffer[8]; //
//
if (!Buf) return; //
//
for (i = 0; i < len; i++) //
{ //
b = *Buf++; //
if ((b < 32) || (b > 127)) //
{ //
sprintf(buffer, " %02X", b); //
if (!SendDebugStr(buffer)) return; //
} //
else //
{ //
if (!SendDebugByte(b)) return; //
} //
} //
SendDebugStr("\n"); //
}
bool SendADCInputs(void)
{
int i; //
s16 j; //
for (i = 0; i < 8; i++) //
{ //
j = s16_Get((s16*)&ADC_Input[i]); //
sprintf((char*)ScratchPad, "ADC-%d: %u\n", i, j); //
if (!SendDebugStr((char*)ScratchPad)) return false; //
} //
return true; //
}
#endif
bool SendHelp(void)
{
if (!SendConsoleRStr(HelpStr)) return false;
#ifdef Debug
if (!SendDebugState()) return false;
if (!SendADCInputs()) return false;
if (!AT_DisplayStage()) return false;
if (!PPP_DisplayStage()) return false;
sprintf((char*)ScratchPad, "\nMainBufferWr_Rx: %d\n", MainBufferWr_Rx);
SendDebugStr((char*)ScratchPad);
sprintf((char*)ScratchPad, "MainBufferWr_Tx: %d\n", MainBufferWr_Tx);
SendDebugStr((char*)ScratchPad);
#ifdef IncludeTCP
TCP_DisplaySocketState(TCP_Socket);
#endif
#endif
return true;
}
// **************************************************************************
int bin2bstr(u8 *Buffer, u32 num, int digits)
{
register u8 *p = Buffer; // buffer to save the text into
register u32 i; // bit mask
//
if (digits-- <= 0) return 0; // no digits
if (!p) return 0; // no buffer
//
i = 1; //
i <<= digits; // bit mask
while (i) //
{ //
if (num & i) //
*p++ = '1'; //
else //
*p++ = '0'; //
i >>= 1; //
} //
*p = 0; //
//
return strlen(Buffer); // return number of characters
}
// **************************************************************************
// trim a string of it's leading and trailing characters
void strtrim(u8 *s, u8 c)
{
u8 i, j, *p1, *p2;
if (s == 0) return;
// delete the trailing characters
if (*s == 0) return;
j = strlen(s);
p1 = s + j;
for (i = 0; i < j; i++)
{
p1--;
if (*p1 != c) break;
}
if (i < j) p1++;
*p1 = 0; // null terminate the undesired trailing characters
// delete the leading characters
p1 = s;
if (*p1 == 0) return;
for (i = 0; *p1++ == c; i++);
if (i > 0)
{
p2 = s;
p1--;
for (; *p1 != 0;) *p2++ = *p1++;
*p2 = 0;
}
}
// **************************************************************************
/*
u8 *GetNextParam(u8 *out, u8 *in, u8 delimiter)
{
if (in == 0) return 0;
if (out == 0) return 0;
*out = 0;
if (*in == 0) return 0;
// extract the parameter
for (; ((*in != delimiter) && *in);) *out++ = *in++;
*out = 0;
strtrim(out, ' '); // delete leading and trailing spaces
if ((*in == delimiter) && (*in != 0)) *in++ = 0; // null term current param and skip the delimiter
strtrim(out, '('); // delete leading and trailing '('
strtrim(out, ')'); // delete leading and trailing ')'
strtrim(out, '\"'); // delete leading and trailing '"'
return in;
}
*/
// **************************************************************************
// convert a hex character into a 4-bit binary value
/*
u8 HexChar2Bin(u8 ascii)
{
if ((ascii >= '0') && (ascii <= '9')) return (ascii - '0');
if ((ascii >= 'A') && (ascii <= 'F')) return ((ascii - 'A') + 10);
if ((ascii >= 'a') && (ascii <= 'f')) return ((ascii - 'a') + 10);
return 0;
}
*/
// **************************************************************************
int str2ipport(char *buf, u8 *ip, u16 *port)
{ // convert an ip:port string into a binary values
int i;
u16 _ip[4], _port;
_port = 0;
memset(_ip, 0, sizeof(_ip));
strtrim((u8*)buf, ' ');
i = sscanf(buf, "%u.%u.%u.%u:%u", &_ip[0], &_ip[1], &_ip[2], &_ip[3], &_port);
*(u8*)(ip + 0) = (u8)_ip[0];
*(u8*)(ip + 1) = (u8)_ip[1];
*(u8*)(ip + 2) = (u8)_ip[2];
*(u8*)(ip + 3) = (u8)_ip[3];
*port = _port;
return i;
}
void ProcessUART0(void)
{ // process received uart0 data (console text commands)
u8 ip[4];
u16 w;
char c;
int i;
#ifdef IncludeTCP
T_TCP_Socket *Socket = NULL;
#endif
if (UART0_RxBufferRd == UART0_RxBufferWr) return; // no new bytes rx'ed
c = UART0_RxBuffer[UART0_RxBufferRd++]; // get byte
if (UART0_RxBufferRd >= sizeof(UART0_RxBuffer)) UART0_RxBufferRd = 0; // wap awound
#ifdef ConsoleHandShaking
HardwareFlowControl(ConsoleUart); // tell em to stop sending data if our buffer is full
#endif
i = strlen((char*)CommandBuffer);
if (i >= (sizeof(CommandBuffer) - 1)) goto end; // drop the buffer contents - buffer overflow
if (c != 13)
{ // add new byte into command buffer
if (c >= 32)
{ // ascii character
CommandBuffer[i++] = tolower(c);
CommandBuffer[i] = 0;
}
return;
}
// process the new command
strtrim(CommandBuffer, ' ');
if (CommandBuffer[0] == 0) goto end;
if ((strcmp((char*)CommandBuffer, "help") == 0) || (strcmp((char*)CommandBuffer, "?") == 0))
{ // send them help
SendHelp();
goto end;
}
if (strcmp((char*)CommandBuffer, "ipconfig") == 0)
{ // send them all the ip addresses
PPP_DisplayIP();
goto end;
}
if (strcmp((char*)CommandBuffer, "dial") == 0)
{ // connect
if (AT.Stage != AT_Idle) goto end;
AT_Start();
goto end;
}
if (strcmp((char*)CommandBuffer, "disc") == 0)
{ // disconnect
AT_End();
goto end;
}
#ifdef Debug
if (strcmp((char*)CommandBuffer, "debug off") == 0)
{ // disable the debug messages
Flags1 &= ~(1<<Flags1_Debug); // clear flag
SendDebugState();
goto end;
}
if (strcmp((char*)CommandBuffer, "debug on") == 0)
{ // enable the debug messages
Flags1 |= (1<<Flags1_Debug); // set flag
SendDebugState();
goto end;
}
#endif
#ifdef IncludeTCP
if (strcmp((char*)CommandBuffer, "tcp down") == 0)
{ // disconnect the tcp link
TCP_CloseSocket(TCP_Socket);
goto end;
}
#endif
if (strcmp((char*)CommandBuffer, "reboot") == 0)
{
AT_End(); // close the network links
while (AT.Stage != AT_Idle) //
{ //
ProcessMainLoop(); // let the tcp/ppp link close gracefully
} //
//
SendConsoleRStr(RebootingStr); //
Disable_Ints(); // disable global interrupts
RLed_On; // Red led on
YLed_On; // Yellow led on
GLed_On; // Green led on
#ifdef CPU_eZ8
WDTCTL = 0x55; // unock the watchdog - so we set it's time out
WDTCTL = 0xAA; // " " "
WDTU = 0; // Set the timeout to a very short value
WDTH = 0; // " " "
WDTL = 10; // " " "
Reset_WD(); //
#endif
for (;;); // let the watchdog reset us
}
#ifdef IncludeICMP
if ((strlen((char*)CommandBuffer) >= 4) && (strncmp((char*)CommandBuffer, "ping", 4) == 0))
{
i = str2ipport((char*)CommandBuffer + 4, ip, &w);
if (i < 4) goto end;
ICMP_Out(*(u32*)ip);
goto end;
}
#endif
#ifdef IncludeNTP
if ((strlen((char*)CommandBuffer) >= 3) && (strncmp((char*)CommandBuffer, "ntp", 3) == 0))
{
i = str2ipport((char*)CommandBuffer + 3, ip, &w);
if (i < 4) goto end;
NTP_RequestSNTP(*(u32*)ip);
goto end;
}
#endif
#ifdef IncludeTCP
if ((strlen((char*)CommandBuffer) >= 3) && (strncmp((char*)CommandBuffer, "tcp", 3) == 0))
{
i = str2ipport((char*)CommandBuffer + 3, ip, &w);
if (i < 4) goto end;
if (i <= 4) w = TCP_Port;
if (TCP_Socket != NULL)
{
if ((TCP_Socket->Stage != TCP_LISTEN) && (TCP_Socket->Stage != TCP_CLOSED)) goto end; // socket is already in use
}
TCP_Socket = TCP_OpenSocket(*(u32*)ip, w);
goto end;
}
#endif
#ifndef WindowsPPP
if ((strlen((char*)CommandBuffer) >= 2) && (strncmp((char*)CommandBuffer, "at", 2) == 0))
{ // modem command
strcat((char*)CommandBuffer, "\n"); // add a CR
SendModemStr(CommandBuffer);
goto end;
}
#endif
end:
memset(CommandBuffer, 0, sizeof(CommandBuffer));
}
// **************************************************************************
int CopyToRingBuffer(u8 *Dest, u8 *Src, int idx, int BufSize, int Bytes)
{ // move data from a linear buffer into a ring buffer
int i, j, k;
if ((!Dest) || (!Src) || (Bytes <= 0) || (BufSize <= 0)) return 0; //
//
j = Bytes; //
for (; Bytes > 0; ) //
{ //
i = Bytes; //
k = BufSize - idx; //
if (i > k) i = k; //
memcpy(Dest + idx, Src, i); // add the bytes to the ring buffer
Src += i; //
idx += i; //
while (idx >= BufSize) idx -= BufSize; // rap around
Bytes -= i; //
} //
//
return j; //
}
int CopyFromRingBuffer(u8 *Dest, u8 *Src, int idx, int BufSize, int Bytes)
{ // move data from a ring buffer into a linear buffer
int i, j, k;
if ((!Dest) || (!Src) || (Bytes <= 0) || (BufSize <= 0)) return 0; //
//
j = Bytes; //
for (; Bytes > 0; ) //
{ //
i = Bytes; //
k = BufSize - idx; //
if (i > k) i = k; //
memcpy(Dest, Src + idx, i); // add the bytes to the linear buffer
Dest += i; //
idx += i; //
while (idx >= BufSize) idx -= BufSize; // rap around
Bytes -= i; //
} //
//
return j; //
}
// *********************************************************************************
void ProcessMainLoop(void)
{
ProcessUART0(); //
AT_Process(); //
PPP_Process(); //
}
// **************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -