QuakeGod
2021-06-20 bfc108e6097eff2bec73050e261f3b9e5db447b7
提交 | 用户 | age
bfc108 1 /**
Q 2   ******************************************************************************
3   * @file           : KLink.c
4   * @brief          : K-Link Protocol program body
5   ******************************************************************************
6     */
7 #include "KLink.h"
8 #include "functions.h"
9 #include "string.h"
10 #include "PLCFunctions.h"
11 #include "stm32f0xx_hal.h"
12
13 unsigned char KLPacketBuf1[256];
14 unsigned char KLPacketBuf2[256];
15
16 unsigned char KLBufferIn[16]={0};
17 unsigned char KLBufferOut[16]={0};
18
19 unsigned  char nKLAddr=1;
20 unsigned  char nKLSeq=0;
21
22 unKLStat nKLStatus ={0};
23
24 int KLThisuS=0;
25 int KLRecvTimeuS=0;
26
27 unsigned char KLBCC(void * pData, int nSize)
28 {
29     unsigned char k;
30     k=0;
31     for (int i=0;i<nSize;i++)
32     {
33         k^=((unsigned char *)pData)[i];
34     }
35     return k;    
36 }
37 int KLMakeRplyPacket(void * pBuf, uchar dst, uchar Status, uchar nCmd, uchar DataLen,void * pData )
38 {
39     pKLRplyPktHdr p1 = (pKLRplyPktHdr)pBuf;
40     int PacketLenth=0;
41     p1->RplyStSgn=KLSignReply;
42     p1->DstAddr=dst;
43     p1->nStatus=Status;
44     p1->nRplyCMD=nCmd;
45     p1->nSize1=DataLen;
46     memcpy(p1->Datas,pData,DataLen);
47     p1->Datas[DataLen]=BCC(p1,sizeof(stKLRplyPktHdr)+DataLen-1);
48     PacketLenth=sizeof(stKLRplyPktHdr)+DataLen;    
49     
50
51     switch (nCmd)
52     {
53         case KLCmdNone:
54             PacketLenth=0;
55             break;
56         case KLCmdPing:
57 //        case KLCmdPingReply:
58             //p1->data[DataLen+1]=KLEndSign;        
59             //PacketLenth=sizeof(stKLPacket)+DataLen+1;        
60             break;
61         case KLCmdRead:
62 //        case KLCmdReadReply:
63             //p1->data[DataLen+1]=KLEndSign;
64             //PacketLenth=sizeof(stPacket)+DataLen+1;
65             break;
66         case KLCmdWrite:
67 //        case KLCmdWriteReply:
68         break;
69         case KLCmdErrRply:
70         
71         break;
72         default:
73             break;
74     }
75     
76     return PacketLenth;
77 }
78
79 int KLCheckPacket(void * pBuf,int nLen1)
80 {
81     pKLReqPktHdr p1 = (pKLReqPktHdr) pBuf;
82     if (p1->ReqStSgn != KLSignStart)
83     {
84         return 1;
85     }
86     
87     int DataLen=0;    //p1->LoadLen;
88     if (DataLen>KLMaxPacketLength) 
89     {
90         return 2;
91     }
92     if (nLen1<DataLen+sizeof(stKLReqPktHdr))
93     {
94         //len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stPacket));
95         //PutStr(str3,len4);
96         return 3;    //not long enough                    
97     }
98 //    if (p1->data[DataLen+1] != EndSign)
99 //    {
100 //        ChnStats[nCurPollId].NoEndErr++;
101 //        Uart2Stat.LengthErr++;        
102 //        return -2;
103 //    }
104     unsigned char thisBCC=KLBCC(p1,nLen1-1);
105     if (thisBCC != ((uchar *)pBuf)[nLen1-1]) 
106     {//BCC Error;
107         return 4;
108     }         
109     return 0;
110 }
111
112 inline void SetAddrBit(unsigned short * pW, unsigned char bitAddr)
113 {
114     (*pW)|=bitMasks[bitAddr&0xf];
115 }
116
117 inline void ResetBit(unsigned short * pW, unsigned char bitAddr)
118 {
119     (*pW)&=~bitMasks[bitAddr&0xf];
120 }
121
122 static inline void SetBitValue(unsigned short * pW, unsigned char bitAddr, unsigned char Value)
123 {
124     if (Value)    {    SetAddrBit(pW, bitAddr);}
125     else {ResetBit(pW, bitAddr);}
126 }
127 int KLParseReqPacket(void * pBuf,int Len1)
128 {
129     pKLReqPktHdr p1 = (pKLReqPktHdr)pBuf;
130
131     int nDstAddr=p1->DstAddr;
132     KLRecvTimeuS=KLThisuS;
133     int nDataType=p1->nType1;
134     int nAddr=0;
135     int DataLen=0;    //p1->LoadLen;
136 //int nSrcAddr=p1->SrcAddr;
137
138     nKLStatus.nSEQ = ((pKLStat)(&(p1->Stat)))->nSEQ;;
139     
140     void * pData=0;
141     pKLRplyPktHdr p2=(pKLRplyPktHdr)KLPacketBuf2;
142     char rData[4];
143
144     int PacketLen=0;
145     KLRecvTimeuS=KLThisuS;
146     if (nDstAddr==nKLAddr || nDstAddr==0xff)
147     {
148         switch (p1->nCMD)
149         {
150             case KLCmdNone:
151                 break;
152             case KLCmdPing:
153                 DataLen=nDataType;
154 //                KLBufferIn[0]=p1->Params[0];
155 //                p1->Params[0]=KLBufferOut[0];
156                 PacketLen=KLMakeRplyPacket(p2,nDstAddr,nKLStatus.StatByte,KLCmdPing,DataLen,p1->Params);
157                 SendPacket1(p2,PacketLen);
158                 break;
159 //            case KLCmdPingReply:
160 //                break;
161             case KLCmdInfo:
162                 //if (nDataType == KLDataTypeDT)
163                 DataLen= 4;
164                 pData=&KMem.nEventCount;
165                 PacketLen=KLMakeRplyPacket(p2,nDstAddr,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
166                 SendPacket1(p2,PacketLen);                
167                 break;            
168             case KLCmdRead:
169             case KLCmdReadDataByte:                
170                 nAddr=p1->Params[0]+ (p1->Params[1]<<8);
171                 DataLen= p1->Params[2];
172                 if (nDataType ==KLDataTypeDT)                {            pData=KMem.DT+nAddr;        }
173                 else if (nDataType == KLDataTypeSDT)    {        pData=KMem.SDT+nAddr;    }
174                 else if (nDataType == KLDataTypeWX)        {        pData=KMem.WX+nAddr;        }
175                 else if (nDataType == KLDataTypeWY)        {        pData=KMem.WY+nAddr;        }
176                 else if (nDataType == KLDataTypeWR)        {        pData=KMem.WR+nAddr;        }
177                 else if (nDataType == KLDataTypeWLX)        {        pData=KMem.WLX+nAddr;        }
178                 else if (nDataType == KLDataTypeWLY)        {        pData=KMem.WLY+nAddr;        }
179                 else if (nDataType == KLDataTypeSV)        {        pData=KMem.SDT+nAddr;    }
180                 else if (nDataType == KLDataTypeEV)        {        pData=KMem.SDT+nAddr;    }
181                 else if (nDataType == KLDataTypeTest)    {        pData=KMem.SDT+nAddr;    }
182                 else if (nDataType == KLDataSysCfg) { pData = (unsigned short *)&KMSysCfg + nAddr;}
183                 else if (nDataType == KLDataTypeFlash) { pData = (unsigned short *)FLASH_BASE + nAddr;}
184                 else                {                    pData=KLBufferOut+nAddr;                }
185                 
186                 PacketLen=KLMakeRplyPacket(p2,nDstAddr,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
187                 SendPacket1(p2,PacketLen);
188                 
189                 break;
190 //            case KLCmdReadReply:
191 //                break;
192             case KLCmdWrite:
193             case KLCmdWriteDataByte:
194                 
195                 //memcpy(DispBuf,p1->data,DataLen);
196                 nAddr=p1->Params[0]+ (p1->Params[1]<<8);
197                 DataLen= p1->Params[2];
198                     if (nDataType ==KLDataTypeDT)                {        pData=KMem.DT+nAddr;        }
199                 else if (nDataType == KLDataTypeSDT)    {        pData=KMem.SDT+nAddr;    }
200                 else if (nDataType == KLDataTypeWX)        {        pData=KMem.WX+nAddr;        }
201                 else if (nDataType == KLDataTypeWY)        {        pData=KMem.WY+nAddr;        }
202                 else if (nDataType == KLDataTypeWR)        {        pData=KMem.WR+nAddr;        }
203                 else if (nDataType == KLDataTypeWLX)        {        pData=KMem.WLX+nAddr;        }
204                 else if (nDataType == KLDataTypeWLY)        {        pData=KMem.WLY+nAddr;        }
205                 else if (nDataType == KLDataTypeSV)        {        pData=KMem.SDT+nAddr;    DataLen=0;}
206                 else if (nDataType == KLDataTypeEV)        {        pData=KMem.SDT+nAddr;    DataLen=0;}
207                 else if (nDataType == KLDataTypeTest)    {        pData=KMem.SDT+nAddr;    DataLen=0;}
208                 else if (nDataType == KLDataSysCfg) { pData = (unsigned short *)&KMSysCfg + nAddr;}
209                 else if (nDataType == KLDataTypeFlash) { pData = (unsigned short *)FLASH_BASE + nAddr;}
210                 else                {                    pData=KLBufferOut+nAddr;    DataLen=0;            }
211                         
212                 memcpy(pData,p1->Params+4,DataLen);                    
213                 PacketLen=KLMakeRplyPacket(p2,nDstAddr,nKLStatus.StatByte,p1->nCMD,0,0);
214                 SendPacket1(p2,PacketLen);                    
215                 break;
216 //            case KLCmdWriteReply:
217 //                break;
218
219             case KLCmdRead1Bit:
220                 nAddr=p1->Params[0]+ (p1->Params[1]<<8);
221                 DataLen = 1;
222                 if (nDataType == KLCoilTypeX)             { rData[0] = ((KMem.WX[nAddr>>4]&bitMasks[nAddr&0x0f])>0);}
223                 else if (nDataType == KLCoilTypeY) { rData[0] = ((KMem.WY[nAddr>>4]&bitMasks[nAddr&0x0f])>0);}
224                 else if (nDataType == KLCoilTypeR) { rData[0] = ((KMem.WR[nAddr>>4]&bitMasks[nAddr&0x0f])>0);}
225                 else if (nDataType == KLCoilTypeLX) { rData[0] = ((KMem.WLX[nAddr>>4]&bitMasks[nAddr&0x0f])>0);}
226                 else if (nDataType == KLCoilTypeLY) { rData[0] = ((KMem.WLY[nAddr>>4]&bitMasks[nAddr&0x0f])>0);}
227                 else if (nDataType == KLCoilTypeT) { rData[0] = PLCMem.Timers[nAddr].bTon;}
228                 else if (nDataType == KLCoilTypeC) { rData[0] = PLCMem.Timers[nAddr].bTon;}
229                 else if (nDataType == KLCoilTypeLR) { rData[0] = 0;}
230                 else {rData[0]=0;}
231                 PacketLen=KLMakeRplyPacket(p2,nDstAddr,nKLStatus.StatByte,KLCmdRead1Bit,DataLen,rData);
232                 SendPacket1(p2,PacketLen);
233                 break;
234             case KLCmdWrite1Bit:
235                 nAddr=p1->Params[0]+ (p1->Params[1]<<8);
236                 if (nDataType == KLCoilTypeX)             { SetBitValue( &KMem.WX[nAddr>>4],nAddr&0x0f,p1->Params[2]);}
237                 else if (nDataType == KLCoilTypeY) { SetBitValue( &KMem.WY[nAddr>>4],nAddr&0x0f,p1->Params[2]);}
238                 else if (nDataType == KLCoilTypeR) { SetBitValue( &KMem.WR[nAddr>>4],nAddr&0x0f,p1->Params[2]);}
239                 else if (nDataType == KLCoilTypeLX) {SetBitValue( &KMem.WLX[nAddr>>4],nAddr&0x0f,p1->Params[2]);}
240                 else if (nDataType == KLCoilTypeLY) {SetBitValue( &KMem.WLY[nAddr>>4],nAddr&0x0f,p1->Params[2]);}
241                 else if (nDataType == KLCoilTypeT) { PLCMem.Timers[nAddr].bTon = p1->Params[2];}
242                 else if (nDataType == KLCoilTypeC) { PLCMem.Timers[nAddr].bTon = p1->Params[2];}
243                 else if (nDataType == KLCoilTypeLR) { ;}
244                 else {rData[0]=0;}
245                 DataLen=0;
246                 PacketLen=KLMakeRplyPacket(p2,nDstAddr,nKLStatus.StatByte,KLCmdWrite1Bit,DataLen,rData);
247                 SendPacket1(p2,PacketLen);
248                 break;
249             case KLCmdReadBits:
250                 break;
251             case KLCmdWriteBits:
252                 break;
253             case KLCmdSaveSysCfg:
254                 WriteSysCfgToFlash(&KMSysCfg);
255                 PacketLen=KLMakeRplyPacket(p2,nDstAddr,nKLStatus.StatByte,KLCmdSaveSysCfg,0,0);
256                 SendPacket1(p2,PacketLen);
257                 break;
258             case     KLCmdSaveRunStat:
259                 SaveRunStat(&KMRunStat);
260                 PacketLen=KLMakeRplyPacket(p2,nDstAddr,nKLStatus.StatByte,KLCmdSaveRunStat,0,0);
261                 SendPacket1(p2,PacketLen);
262                 break;
263             case KLCmdGetEventLogCount:
264                 DataLen= 4;
265                 pData=&KMem.nEventCount;
266                 PacketLen=KLMakeRplyPacket(p2,nDstAddr,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
267                 SendPacket1(p2,PacketLen);
268                 break;
269             case KLCmdGetEventLog:
270                 nAddr=p1->Params[0]+ (p1->Params[1]<<8);
271                 DataLen= p1->Params[2] * sizeof(stEventLog);
272                 pData=GetEventLogAddr(nAddr);
273                 PacketLen=KLMakeRplyPacket(p2,nDstAddr,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
274                 SendPacket1(p2,PacketLen);
275                 break;
276             case KLCmdClearEventLog:
277                 ClearEventLog();
278                 PacketLen=KLMakeRplyPacket(p2,nDstAddr,nKLStatus.StatByte,KLCmdClearEventLog,0,0);
279                 SendPacket1(p2,PacketLen);
280                 break;            
281             default:
282             DataLen=1;
283             rData[0]=KL_UNKNOWN;
284             PacketLen=KLMakeRplyPacket(p2,nKLAddr,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData);
285             SendPacket1(p2,PacketLen);            
286                 break;        
287         }
288     }    
289     return 0;
290 }
291 int KLParsePacket(void * pBuf,int Len1)
292 {
293     pKLReqPktHdr p1 = (pKLReqPktHdr)pBuf;
294     KLThisuS=GetuS();
295     pKLRplyPktHdr p2=(pKLRplyPktHdr)KLPacketBuf2;
296     int PacketLen=Len1;
297     int Result=KLCheckPacket(p1,Len1);
298     if (Result != KL_OK)
299     {
300         int DataLen=4;
301         char rData[4];
302         rData[0]=Result;
303         
304         PacketLen=KLMakeRplyPacket(p2,nKLAddr,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData);
305 //        PacketLen=KLMakePacket(p2,0,nAddr,cmdKLPing,p1->nSEQ,DataLen,rData);
306         SendPacket1(p2,PacketLen);
307         return Result;
308     }
309 //    if (bMaster) Result=ParseMasterPacket(p1,Len1);
310 //    memcpy(KLPacketBuf2,pBuf,Len1);
311 //    SendPacket1(p2,PacketLen);
312     
313     Result=KLParseReqPacket(p1,Len1);
314     
315     //int len1=p1->PacketLen;
316 //    if (p1->DstAddr!=255&&p1->DstAddr!=2) return -3;
317 //    pPacket p2=(pPacket)PacketBuf2;
318 //            Uart2Stat.OKPacket++;
319
320     return Result;
321 }
322