📄 protocal.cpp
字号:
#include <string.h>
#include <stdio.h>
#include "Sync.h"
#include "CommonDef.h"
#include "Protocal.h"
#include "Config.h"
#include "Net.h"
CiLink::CiLink(CCommunication* pCommunication, char* pBuffer, unsigned int unBufLen)
{
m_pCommunication = pCommunication;
m_pNetBuffer = pBuffer;
m_unBufLen = unBufLen;
return;
}
CiLink::~CiLink()
{
m_pCommunication = NULL;
m_pNetBuffer = NULL ;
return;
}
void CiLink::ParseProcess(int nCommuType)
{
COMU_HEAD* pch = (COMU_HEAD*)m_pNetBuffer;
if(nCommuType == COMMU_TCP)
{
if(LinkGetCmd() == 0)
{
if(pch->nCommandType == COMU_REQUEST)
LinkCommonParse((char*)pch);
}
}
else if(nCommuType == COMMU_RAW)
LinkIPParse();
return;
}
int CiLink::LinkCommonParse(char* pBuffer)
{
int rc = ENO;
int nType = COMU_REQUEST;
int nStart = 1;;
COMU_HEAD* pch = (COMU_HEAD*)pBuffer;
int nCmd = pch->nCommand;
MBX_MESSAGE msg;
msg.cSrc = TSK_NETNORMAL;
msg.nCommand = pch->nCommand;
msg.nDataLen = pch->nDataLen;
msg.nError = EINVREQUEST;
msg.nCommandType = COMU_RESPONSE;
msg.pData = (char*)(pch + 1);
msg.nDataType = pch->nDataType;
while(nType != COMU_RESPONSE)
{
//Task schedule according to command
if(nCmd < 1024)
{
msg.cDst = TSK_COG;
if(nStart == 1)
MBX_post(hMbxSchedule, &msg, 0);
MBX_pend(hMbxNetNormal, &msg, SYS_FOREVER);
}
else if(nCmd < 2048)
{
msg.cDst = TSK_TST;
if(nStart == 1)
MBX_post(hMbxSchedule, &msg, 0);
MBX_pend(hMbxNetNormal, &msg, SYS_FOREVER);
}
// else if(nCmd == 2071)
// {
// msg.cDst= TSK_STARTSERIAL;
// if(nStart == 1)
// MBX_post(hMbxSchedule, &msg, 0);
// MBX_pend(hMbxNetNormal, &msg, SYS_FOREVER);
// }
// else if(nCmd == 2072)
// {
// msg.cDst = TSK_STOPSERIAL;
// if(nStart == 1)
// MBX_post(hMbxSchedule, &msg, 0);
// MBX_pend(hMbxNetNormal, &msg, SYS_FOREVER);
// }
else if(nCmd < 3072)
{
msg.cDst = TSK_CFG;
if(nStart == 1)
MBX_post(hMbxSchedule, &msg, 0);
MBX_pend(hMbxNetNormal, &msg, SYS_FOREVER);
}
else if(nCmd < 4096)
{
msg.cDst = TSK_FILE;
if(nStart == 1)
MBX_post(hMbxSchedule, &msg, 0);
MBX_pend(hMbxNetNormal, &msg, SYS_FOREVER);
}
else if(nCmd < 5120)
{
msg.cDst = TSK_PRIOR;
if(nStart == 1)
MBX_post(hMbxSchedule, &msg, 0);
MBX_pend(hMbxNetPrior, &msg, SYS_FOREVER);
}
else
msg.nDataLen = 0;
nType = msg.nCommandType;
LinkReply((char*)&msg);
nStart = 0;
}
return rc;
}
void CiLink::LinkIPReply()
{
if(g_nNormStatus != 0 && g_nPrioStatus != 0 && g_nMakeCfgStatus != 0)
g_cfgIPHeader.c_mode = CONFIG_REPLY_OK;
g_cfgIPHeader.c_cksum = 0;
g_cfgIPHeader.c_cksum = LinkCheckSum((unsigned short*)&g_cfgIPHeader, sizeof(CONFIG_HDR));
m_pCommunication->SendData((char*)&g_cfgIPHeader, sizeof(CONFIG_HDR));
return;
}
/*int CiLink::LinkSerialReply(char* pBuffer)
{
MBX_MESSAGE* pMsg = (MBX_MESSAGE*)pBuffer;
COMU_HEAD ch;
LINKADDR linkAddr;
SOCKET m_nSerialSock;
struct sockaddr_in m_nSerialAddr;
memset((char*)&ch, 0, sizeof(COMU_HEAD));
//Make protocal header
ch.nCommand = pMsg->nCommand;
ch.nError = pMsg->nError;
ch.nDataLen = pMsg->nDataLen - sizeof(LINKADDR);
ch.nCheckSum = 0;
ch.nCommandType = pMsg->nType;
ch.nCheckSum = LinkCheckSum((unsigned short*)&ch, sizeof(ch));
linkAddr = *((LINKADDR *)pMsg->pData);
m_nSerialAddr.sin_addr.s_addr = linkAddr.nLinkIp;
m_nSerialAddr.sin_port=linkAddr.nLinkPortNum;
m_nSerialSock = socket(AF_INET, SOCK_STREAM, 0);
//Start send
((CiLinkCommu*)m_pCommunication)->SendSync();
if(connect(m_nSerialSock, (struct sockaddr *)&m_nSerialAddr, sizeof(sockaddr_in)) != -1)
{
if(sendto(m_nSerialSock, (char*)&ch, sizeof(COMU_HEAD), 0, &m_nSerialAddr, sizeof(sockaddr_in)) > 0)
{
if(sendto(m_nSerialSock, pMsg->pData + sizeof(LINKADDR), pMsg->nDataLen - sizeof(LINKADDR), 0, &m_nSerialAddr, sizeof(m_nSerialAddr)) > 0)
{
fdClose(m_nSerialSock);
}
}
}
return ENO;
}*/
void CiLink::LinkIPParse()
{
IP_HDR* pih = NULL;
CONFIG_HDR* pch = NULL;
int hdrlen = 0;
int i = 0;
CONFIG_HDR from;
//Reply ip address for last request
if(g_nNeedReply == 1)
{
g_nNeedReply = 0;
TSK_sleep(5000);
LinkIPReply();
}
if(m_pCommunication->ReceiveData(m_pNetBuffer, 128, 0) != 0)
return;
pih = (IP_HDR *)m_pNetBuffer;
hdrlen = (pih->h_len & 0x0f) * 4;
pch = (CONFIG_HDR*)(m_pNetBuffer + hdrlen);
if(LinkCheckSum((unsigned short*)pch, sizeof(CONFIG_HDR)) == 0)
{
memcpy(&from, (char*)pch, sizeof(CONFIG_HDR));
//Mac invalid
for(i = 0; i < 6; i++)
{
if(from.c_mac[i] != g_pEthInfo->bMacAddr[i])
return;
}
pch = (CONFIG_HDR*)m_pNetBuffer;
pch->c_id = from.c_id;
pch->c_mode = CONFIG_REPLY_FAIL;
//Static ip setup
if(from.c_mode == CONFIG_IP_REQ)
{
NtIPN2Str(from.c_ip, g_pEthInfo->strLocalIPAddr);
g_nNormStatus = 0;
g_nPrioStatus = 0;
g_nMakeCfgStatus = 0;
if(g_pConfigure != NULL)
g_pConfigure->SetEthernetInfo(g_pEthInfo, sizeof(ETH_INFO));
//Notify the connect server that net stack will be restart
g_nNetStackStoped = 1;
g_nNeedReply = 1;
//Save received config header
memcpy(&g_cfgIPHeader, pch, sizeof(g_cfgIPHeader));
NC_NetStop(1);
}
//Ip Search
else if(from.c_mode == CONFIG_GETIP_REQ)
{
pch->c_ip = inet_addr(g_pEthInfo->strLocalIPAddr);
pch->c_mask = inet_addr(g_pEthInfo->strLocalIPMask);
pch->c_gate = inet_addr(g_pEthInfo->strGatewayIP);
memcpy(pch->c_mac, g_pEthInfo->bMacAddr, sizeof(g_pEthInfo->bMacAddr));
pch->c_id = from.c_id;
pch->c_mode = CONFIG_REPLY_OK;
pch->c_reserve = 0xff;
pch->c_cksum = 0;
pch->c_cksum = LinkCheckSum((unsigned short*)pch, sizeof(CONFIG_HDR));
m_pCommunication->SendData((char*)pch, sizeof(CONFIG_HDR));
}
//dhcp ip setup
else if(from.c_mode == CONFIG_DHCP_REQ)
{
g_nDhcpEnable = 1;
g_nNormStatus = 0;
g_nPrioStatus = 0;
g_nMakeCfgStatus = 0;
// g_nDhcpStatus = 0;
//Notify the connect server that net stack will be restart
g_nNetStackStoped = 1;
g_nNeedReply = 1;
//Save received config header
memcpy(&g_cfgIPHeader, pch, sizeof(g_cfgIPHeader));
NC_NetStop(1);
}
//Invalid mode
else
return;
}
return;
}
int CiLink::LinkGetCmd()
{
int rc = ENO;
int len = sizeof(COMU_HEAD);
COMU_HEAD* pch = (COMU_HEAD*)m_pNetBuffer;
rc = m_pCommunication->ReceiveData((char*)pch, sizeof(COMU_HEAD), 0);
if(rc != 0)
return rc;
else
{
if(LinkCheckSum((unsigned short*)pch, len) != 0)
return -1;
else
{
if(pch->nDataLen > m_unBufLen - sizeof(COMU_HEAD))
{
return EINVPARAM;
}
rc = m_pCommunication->ReceiveData((char*)(pch + 1), pch->nDataLen, 0);
return rc;
}
}
}
int CiLink::LinkReply(char* pBuffer)
{
MBX_MESSAGE* pMsg = (MBX_MESSAGE*)pBuffer;
COMU_HEAD ch;
memset((char*)&ch, 0, sizeof(COMU_HEAD));
//Make protocal header
ch.nCommand = pMsg->nCommand;
ch.nError = pMsg->nError;
ch.nDataLen = pMsg->nDataLen;
ch.nDataType = pMsg->nDataType;
ch.nCheckSum = 0;
ch.nCommandType = pMsg->nCommandType;
ch.nCheckSum = LinkCheckSum((unsigned short*)&ch, sizeof(ch));
//Start send
((CiLinkCommu*)m_pCommunication)->SendSync();
m_pCommunication->SendData((char*)&ch, sizeof(COMU_HEAD));
if(ch.nDataLen > 0)
{
m_pCommunication->SendData(pMsg->pData, pMsg->nDataLen);
}
return ENO;
}
unsigned short CiLink::LinkCheckSum(unsigned short* pBuffer, int nLen)
{
unsigned int cksum = 0;
while(nLen > 1)
{
cksum += *pBuffer++;
nLen -= sizeof(unsigned short);
}
if(nLen)
cksum += *(unsigned char*)pBuffer;
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >>16);
return (unsigned short)(~cksum);
}
//CTerminal implement
CTerminal::CTerminal(CCommunication* pCommunication)
{
char strLable[] = "Welcome to Phocus1820";
m_pCommunication = pCommunication;
memset(m_strTerminalOutBuffer, 0, sizeof(m_strTerminalOutBuffer));
m_nCurrentToiPos = 0;
strcpy(m_strOutputEnd, "\r\n");
//Clear terminal and print log lable
m_pCommunication->SendData("\033[2J", 4);
m_pCommunication->SendData(strLable, sizeof(strLable));
m_pCommunication->SendData("\r\n>", 3);
}
CTerminal::~CTerminal()
{
}
int CTerminal::GetCmd()
{
int i = 0;
char c = 0;
memset(m_strCmd, 0, MAX_TERMINAL_CMD_LEN);
//Indicate cursor position in m_strCmd
m_nCurrentPos = 0;
m_nMaxPos = 0;
m_pCommunication->ReceiveData(&c, 1, -1);
//Protocal restart
// if(g_nRestart == 1)
// return -1;
//Get command from user
while(1)
{
if(c == 0x0d)
{
m_pCommunication->ReceiveData((char*)&c, 1, -1);
//For protocal restart
// if(g_nRestart == 1)
// return -1;
if(c == 0x0a)
return 0;
else
return -1;
}
else
{
//Key ctrl
if(c == 0x1b)
{
//Skip a char
m_pCommunication->ReceiveData(&c, 1, -1);
//Protocal restart
// if(g_nRestart == 1)
// return -1;
m_pCommunication->ReceiveData(&c, 1, -1);
//Protocal restart
// if(g_nRestart == 1)
// return -1;
//Right shift
if(c == 'C')
{
if(m_nMaxPos == 0)
//Rollback cursor
OutString("\033[D", 3);
if(m_nCurrentPos < m_nMaxPos)
m_nCurrentPos++;
}
//Left shift
else if(c == 'D')
{
if(m_nMaxPos == 0)
//Rollback cursor
OutString("\033[C", 3);
if(m_nCurrentPos > 0)
m_nCurrentPos--;
}
else if(c == 'A')
OutString("\033[B", 3);
else if(c == 'B')
OutString("\033[A", 3);
else
{
m_pCommunication->ReceiveData(&c, 1, -1);
//Protocal restart
// if(g_nRestart == 1)
// return -1;
continue;
}
}
else if(c == '\b')
{
if(m_nCurrentPos == 0)
OutString("\033[C", 3);
//Output a blank cursor left shift
else
{
ShrinkString(m_strCmd, m_nCurrentPos - 1);
OutString(" \033[D", 4);
m_nCurrentPos--;
m_nMaxPos--;
//Clear current cursor pos to line end
OutString("\033[K", 3);
//Output rest string
OutString(&m_strCmd[m_nCurrentPos], m_nMaxPos - m_nCurrentPos);
//Roll back the cursor to m_nCurrentPos
for(i = 0;i < m_nMaxPos - m_nCurrentPos;i++)
OutString("\033[D", 4);
}
}
else
{
m_strCmd[m_nMaxPos] = c;
m_nCurrentPos++;
m_nMaxPos++;
//Command line len reach max len, clear the command line
if(m_nMaxPos == MAX_TERMINAL_CMD_LEN)
{
memset(m_strCmd, 0, sizeof(m_strCmd));
m_nMaxPos = 0;
m_nCurrentPos = 0;
return EINVREQUEST;
}
}
}// end rc == 0
//Get next char
m_pCommunication->ReceiveData(&c, 1, -1);
//Protocal restart
// if(g_nRestart == 1)
// return -1;
} //end while
}
void CTerminal::OutString(char* pBuffer, int nLen)
{
m_pCommunication->SendData(pBuffer, nLen);
return;
}
void CTerminal::ParseProcess(int nCommuType)
{
char strMsg[32] = {0};
//Start get command and process it
if(GetCmd() == 0)
{
if(ParseCommand() != 0)
{
// if(g_nRestart == 0)
{
strcpy(strMsg, "Invalid command");
OutString(strMsg, strlen(strMsg));
OutString(m_strOutputEnd, strlen(m_strOutputEnd));
}
}
}
else
{
// if(g_nRestart == 0)
{
strcpy(strMsg, "Invalid command");
OutString(strMsg, strlen(strMsg));
OutString(m_strOutputEnd, strlen(m_strOutputEnd));
}
}
// if(g_nRestart == 0)
// OutString(">", 1);
return;
}
void CTerminal::ShrinkString(char* pBuffer, int nPos)
{
int i = 0;
if(nPos < 0)
return;
for(i = nPos;i < 30;i++)
pBuffer[i] = pBuffer[i + 1];
return;
}
int CTerminal::OnRead(char* pParam1, char* pParam2)
{
return 0;
}
int CTerminal::OnVersion()
{
char strTmp[64] = {0};
sprintf(strTmp, "MarkV IDReader%s", m_strOutputEnd);
OutString(strTmp, strlen(strTmp));
return 0;
}
int CTerminal::OnFile()
{
return 0;
}
int CTerminal::OnQuery(char* pParam1)
{
int offset = 0;
char* p = NULL;
char strMsg[32];
if(m_nCurrentToiPos == 0)
{
sprintf(strMsg, "No Message%s", m_strOutputEnd);
OutString(strMsg, strlen(strMsg));
return 0;
}
//There is message in buffer
while(offset < m_nCurrentToiPos)
{
p = m_strTerminalOutBuffer + offset;
OutString(p, strlen(p));
offset += MAX_TERMINAL_OUTITEM_SIZE;
}
//Clean the buffer?
if(strcmp(pParam1, "-d") == 0)
{
memset(m_strTerminalOutBuffer, 0, MAX_TERMINAL_OUTBUFFER_SIZE);
m_nCurrentToiPos = 0;
}
return 0;
}
int CTerminal::ParseCommand()
{
//Use for save command parameters, the max parameter number is 3 and each pamameters size is less than 16
char strTmp[3][16] = {0};
int i = 0;
int j = 0;
int k = 0;
if(strlen(m_strCmd) == 0)
return 0;
//Ship some charters
while(m_strCmd[j] == '\t' || m_strCmd[j] == ' ')
j++;
while(m_strCmd[j] != '\0')
{
if(m_strCmd[j] == ' ')
{
k = 0;
i++;
j++;
if(i > 2)
return -1;
while(m_strCmd[j] == ' ')
j++;
}
else
{
strTmp[i][k] = m_strCmd[j];
k++;
j++;
}
}
//Command parse
if((strcmp(strTmp[0], "version") == 0)
&& strlen(strTmp[1]) == 0
&& strlen(strTmp[2]) == 0)
OnVersion();
else if((strcmp(strTmp[0], "read") == 0)
&& strlen(strTmp[1]) != 0
&& strlen(strTmp[2]) != 0)
OnRead(strTmp[1], strTmp[2]);
else if((strcmp(strTmp[0], "file") == 0)
&& strlen(strTmp[1]) == 0
&& strlen(strTmp[2]) == 0)
OnFile();
else if((strcmp(strTmp[0], "query") == 0 && strlen(strTmp[1]) == 0 && strlen(strTmp[2]) == 0)
|| (strcmp(strTmp[0], "query") == 0 && strcmp(strTmp[1], "-d") == 0 && strlen(strTmp[2]) == 0))
OnQuery(strTmp[1]);
else
return -1;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -