QuakeGod
2023-02-01 c5764186c3ec23eb954463495b8fbd77e32b268c
提交 | 用户 | 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
85d591 27 unsigned char KLBCC(void const * pData, int nSize)
bfc108 28 {
Q 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 }
85d591 37 int KLMakeRplyPacket(void * pBuf, uchar dst, uchar Status, uchar nCmd, uchar DataLen,const void * pData )
bfc108 38 {
Q 39     pKLRplyPktHdr p1 = (pKLRplyPktHdr)pBuf;
40     int PacketLenth=0;
41     p1->RplyStSgn=KLSignReply;
85d591 42     p1->DstHost=dst;
bfc108 43     p1->nStatus=Status;
Q 44     p1->nRplyCMD=nCmd;
45     p1->nSize1=DataLen;
46     memcpy(p1->Datas,pData,DataLen);
7a54c0 47     p1->Datas[DataLen]=KLBCC(p1,sizeof(stKLRplyPktHdr)+DataLen-1);
bfc108 48     PacketLenth=sizeof(stKLRplyPktHdr)+DataLen;    
Q 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
85d591 79 int KLCheckPacket(int nChn, void * pBuf, int nLen1)
bfc108 80 {
Q 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 {
85d591 114     (*pW)|=1<<(bitAddr&0xf);
bfc108 115 }
Q 116
117 inline void ResetBit(unsigned short * pW, unsigned char bitAddr)
118 {
85d591 119     (*pW)&=~(1<<(bitAddr&0xf));
bfc108 120 }
Q 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 }
85d591 127
Q 128 int KLParseReqPacket(int nChn, void * pBuf, int Len1)
bfc108 129 {
Q 130     pKLReqPktHdr p1 = (pKLReqPktHdr)pBuf;
131
85d591 132     int nDstHost=p1->DstHost;
bfc108 133     KLRecvTimeuS=KLThisuS;
Q 134     int nDataType=p1->nType1;
135     int nAddr=0;
136     int DataLen=0;    //p1->LoadLen;
137 //int nSrcAddr=p1->SrcAddr;
138
139     nKLStatus.nSEQ = ((pKLStat)(&(p1->Stat)))->nSEQ;;
140     
141     void * pData=0;
142     pKLRplyPktHdr p2=(pKLRplyPktHdr)KLPacketBuf2;
143     char rData[4];
144
145     int PacketLen=0;
146     KLRecvTimeuS=KLThisuS;
85d591 147     if (nDstHost==nKLAddr || nDstHost==0xff)
bfc108 148     {
Q 149         switch (p1->nCMD)
150         {
151             case KLCmdNone:
152                 break;
153             case KLCmdPing:
154                 DataLen=nDataType;
155 //                KLBufferIn[0]=p1->Params[0];
156 //                p1->Params[0]=KLBufferOut[0];
85d591 157                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdPing,DataLen,p1->Params);
Q 158                 SendPacket(nChn, p2, PacketLen);
bfc108 159                 break;
Q 160 //            case KLCmdPingReply:
161 //                break;
162             case KLCmdInfo:
163                 //if (nDataType == KLDataTypeDT)
85d591 164                 DataLen= sizeof(KMInfoBlock);
Q 165                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,&KMInfoBlock);
166                 SendPacket(nChn, p2, PacketLen);                
167                 break;
168             case KLCmdGetUid:
169                 //if (nDataType == KLDataTypeDT)
170                 DataLen= 12;
171                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,(uint32_t *)UID_BASE);
172                 SendPacket(nChn, p2, PacketLen);                
173                 break;
174             case KLCmdGetSN:
175                 //if (nDataType == KLDataTypeDT)
bfc108 176                 DataLen= 4;
85d591 177                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,&((pFactoryData)FACTORY_DATA_BASE)->SN1);
Q 178                 SendPacket(nChn, p2, PacketLen);                
179                 break;
180             case KLCmdGetFactoryData:
181                 DataLen= sizeof(stFactoryData);
182                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,(pFactoryData)FACTORY_DATA_BASE);
183                 SendPacket(nChn, p2, PacketLen);                
184                 break;
185             case KLCmdWriteFactoryData:
186                 nAddr=p1->Params[0]+ (p1->Params[1]<<8);
187                 DataLen= p1->Params[2];
188                 //DataLen=16;
189                 for (int i=0;i<DataLen;i++)
190                 {KLPacketBuf2[i]=p1->Params[4+i];}
191                 WriteFactoryData(KLPacketBuf2, DataLen);
192                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
193                 SendPacket(nChn, p2, PacketLen);
194                 break;
195             
bfc108 196             case KLCmdRead:
85d591 197             case KLCmdReadDataWord:
bfc108 198             case KLCmdReadDataByte:                
Q 199                 nAddr=p1->Params[0]+ (p1->Params[1]<<8);
200                 DataLen= p1->Params[2];
201                 if (nDataType ==KLDataTypeDT)                {            pData=KMem.DT+nAddr;        }
202                 else if (nDataType == KLDataTypeSDT)    {        pData=KMem.SDT+nAddr;    }
85d591 203                 else if (nDataType == KLDataTypeWSR)    {        pData=KMem.WSR+nAddr;    }
bfc108 204                 else if (nDataType == KLDataTypeWX)        {        pData=KMem.WX+nAddr;        }
Q 205                 else if (nDataType == KLDataTypeWY)        {        pData=KMem.WY+nAddr;        }
206                 else if (nDataType == KLDataTypeWR)        {        pData=KMem.WR+nAddr;        }
207                 else if (nDataType == KLDataTypeWLX)        {        pData=KMem.WLX+nAddr;        }
208                 else if (nDataType == KLDataTypeWLY)        {        pData=KMem.WLY+nAddr;        }
85d591 209                 else if (nDataType == KLDataTypeSV)        {        pData=KMem.SV+nAddr;    }
Q 210                 else if (nDataType == KLDataTypeEV)        {        pData=KMem.EV+nAddr;    }
bfc108 211                 else if (nDataType == KLDataTypeTest)    {        pData=KMem.SDT+nAddr;    }
85d591 212                 else if (nDataType == KLDataSysCfg) { pData = (unsigned short *)&storedKMSysCfg + nAddr;}
bfc108 213                 else if (nDataType == KLDataTypeFlash) { pData = (unsigned short *)FLASH_BASE + nAddr;}
Q 214                 else                {                    pData=KLBufferOut+nAddr;                }
215                 
85d591 216                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
Q 217                 SendPacket(nChn, p2, PacketLen);
bfc108 218                 
Q 219                 break;
220 //            case KLCmdReadReply:
221 //                break;
222             case KLCmdWrite:
85d591 223             case KLCmdWriteDataWord:
bfc108 224             case KLCmdWriteDataByte:
Q 225                 
226                 //memcpy(DispBuf,p1->data,DataLen);
227                 nAddr=p1->Params[0]+ (p1->Params[1]<<8);
228                 DataLen= p1->Params[2];
229                     if (nDataType ==KLDataTypeDT)                {        pData=KMem.DT+nAddr;        }
230                 else if (nDataType == KLDataTypeSDT)    {        pData=KMem.SDT+nAddr;    }
85d591 231                 else if (nDataType == KLDataTypeWSR)    {        pData=KMem.WSR+nAddr;    }
bfc108 232                 else if (nDataType == KLDataTypeWX)        {        pData=KMem.WX+nAddr;        }
Q 233                 else if (nDataType == KLDataTypeWY)        {        pData=KMem.WY+nAddr;        }
234                 else if (nDataType == KLDataTypeWR)        {        pData=KMem.WR+nAddr;        }
235                 else if (nDataType == KLDataTypeWLX)        {        pData=KMem.WLX+nAddr;        }
236                 else if (nDataType == KLDataTypeWLY)        {        pData=KMem.WLY+nAddr;        }
85d591 237                 else if (nDataType == KLDataTypeSV)        {        pData=KMem.SV+nAddr;    DataLen=0;}
Q 238                 else if (nDataType == KLDataTypeEV)        {        pData=KMem.EV+nAddr;    DataLen=0;}
bfc108 239                 else if (nDataType == KLDataTypeTest)    {        pData=KMem.SDT+nAddr;    DataLen=0;}
85d591 240                 else if (nDataType == KLDataSysCfg)        { pData = (unsigned short *)&storedKMSysCfg + nAddr;}
bfc108 241                 else if (nDataType == KLDataTypeFlash) { pData = (unsigned short *)FLASH_BASE + nAddr;}
Q 242                 else                {                    pData=KLBufferOut+nAddr;    DataLen=0;            }
243                         
244                 memcpy(pData,p1->Params+4,DataLen);                    
85d591 245                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
Q 246                 SendPacket(nChn, p2, PacketLen);                    
bfc108 247                 break;
Q 248 //            case KLCmdWriteReply:
249 //                break;
250
251             case KLCmdRead1Bit:
252                 nAddr=p1->Params[0]+ (p1->Params[1]<<8);
253                 DataLen = 1;
85d591 254                 if (nDataType == KLCoilTypeX)             { rData[0] = ((KMem.WX[nAddr>>4]&(1<<(nAddr&0x0f)))>0);}
Q 255                 else if (nDataType == KLCoilTypeY) { rData[0] = ((KMem.WY[nAddr>>4]&(1<<(nAddr&0x0f)))>0);}
256                 else if (nDataType == KLCoilTypeR) { rData[0] = ((KMem.WR[nAddr>>4]&(1<<(nAddr&0x0f)))>0);}
257                 else if (nDataType == KLCoilTypeLX) { rData[0] = ((KMem.WLX[nAddr>>4]&(1<<(nAddr&0x0f)))>0);}
258                 else if (nDataType == KLCoilTypeLY) { rData[0] = ((KMem.WLY[nAddr>>4]&(1<<(nAddr&0x0f)))>0);}
259                 else if (nDataType == KLCoilTypeT) { rData[0] = KMem.Timers[nAddr].bTon;}
260                 else if (nDataType == KLCoilTypeC) { rData[0] = KMem.Timers[nAddr].bTon;}
261                 else if (nDataType == KLCoilTypeSR) {rData[0] = ((KMem.WSR[nAddr>>4]&(1<<(nAddr&0x0f)))>0);}
262                 
bfc108 263                 else if (nDataType == KLCoilTypeLR) { rData[0] = 0;}
Q 264                 else {rData[0]=0;}
85d591 265                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdRead1Bit,DataLen,rData);
Q 266                 SendPacket(nChn, p2, PacketLen);
bfc108 267                 break;
Q 268             case KLCmdWrite1Bit:
269                 nAddr=p1->Params[0]+ (p1->Params[1]<<8);
270                 if (nDataType == KLCoilTypeX)             { SetBitValue( &KMem.WX[nAddr>>4],nAddr&0x0f,p1->Params[2]);}
271                 else if (nDataType == KLCoilTypeY) { SetBitValue( &KMem.WY[nAddr>>4],nAddr&0x0f,p1->Params[2]);}
272                 else if (nDataType == KLCoilTypeR) { SetBitValue( &KMem.WR[nAddr>>4],nAddr&0x0f,p1->Params[2]);}
273                 else if (nDataType == KLCoilTypeLX) {SetBitValue( &KMem.WLX[nAddr>>4],nAddr&0x0f,p1->Params[2]);}
274                 else if (nDataType == KLCoilTypeLY) {SetBitValue( &KMem.WLY[nAddr>>4],nAddr&0x0f,p1->Params[2]);}
85d591 275                 else if (nDataType == KLCoilTypeT) { KMem.Timers[nAddr].bTon = p1->Params[2];}
Q 276                 else if (nDataType == KLCoilTypeC) { KMem.Timers[nAddr].bTon = p1->Params[2];}
277                 else if (nDataType == KLCoilTypeC) { KMem.Timers[nAddr].bTon = p1->Params[2];}
278                 else if (nDataType == KLCoilTypeLR) { SetBitValue( &KMem.WSR[nAddr>>4],nAddr&0x0f,p1->Params[2]);;}
bfc108 279                 else {rData[0]=0;}
Q 280                 DataLen=0;
85d591 281                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdWrite1Bit,DataLen,rData);
Q 282                 SendPacket(nChn, p2, PacketLen);
bfc108 283                 break;
Q 284             case KLCmdReadBits:
285                 break;
286             case KLCmdWriteBits:
287                 break;
85d591 288             case KLCmdChgMode:
Q 289                 
290                 break;
291             case KLCmdReadProgram:
292                 nAddr=p1->Params[0]+ (p1->Params[1]<<8);
293                 DataLen= p1->Params[2];
294                  if (nDataType==0){
295                         pData = (unsigned short *)STORE_PRG_BASE + nAddr;
296                  } else if (nDataType==1){
297                         pData = (unsigned short *)ALT_PRG_BASE + nAddr;
298                  } else if (KMRunStat.nBinProgBank == 0) {
299                         pData = (unsigned short *)STORE_PRG_BASE + nAddr;
300                  }else {
301                         pData = (unsigned short *)ALT_PRG_BASE + nAddr;
302                  }
303                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
304                 SendPacket(nChn, p2, PacketLen);
305                 
306                 break;
307             case KLCmdStartProgram:
308                 DataLen=nDataType;
309 //                KLBufferIn[0]=p1->Params[0];
310 //                p1->Params[0]=KLBufferOut[0];
311                 if (PLCMem.bPLCRunning) PLCMem.bPLCRunning=0;
312             
313                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdStartProgram,0,0);
314                 SendPacket(nChn, p2, PacketLen);
315                 
316                 break;
317             case KLCmdWriteProgram:
318                 if (PLCMem.bPLCRunning) PLCMem.bPLCRunning=0;
319                 nAddr=p1->Params[0]+ (p1->Params[1]<<8);
320                 DataLen= p1->Params[2];
321
322                 //DataLen=16;
323                 for (int i=0;i<DataLen;i++)
324                 {KLPacketBuf2[i]=p1->Params[4+i];}
325                 WriteProgram(nAddr, KLPacketBuf2, DataLen,nDataType);
326                 DataLen=4;
327                 *((int *)(&rData[0]))=(long)(p1->Params+4);
328                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdWriteProgram,0,0);
329                 SendPacket(nChn, p2, PacketLen);
330                 break;
331             case KLCmdFinishProgram:
332                 nAddr=p1->Params[0]+ (p1->Params[1]<<8);        //Program Size;
333                 DataLen=nDataType;
334                 KMRunStat.nBinProgSize=nAddr;
335                 if (KMRunStat.nBinProgBank ==0) {KMRunStat.nBinProgBank=1;}
336                 else {KMRunStat.nBinProgBank=0;}
337                 SaveRunStat(&KMRunStat);                
338                 PLCMem.bPLCRunning=1;
339 //                KLBufferIn[0]=p1->Params[0];
340 //                p1->Params[0]=KLBufferOut[0];
341                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdFinishProgram,0,0);
342                 SendPacket(nChn, p2, PacketLen);
343                 break;
344             case KLCmdReadRunStat:
345                 DataLen= sizeof(stRunStat);
346                 pData=&KMRunStat;
347                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
348                 SendPacket(nChn, p2, PacketLen);    
349                 break;
bfc108 350             case KLCmdSaveSysCfg:
85d591 351                 WriteSysCfgToFlash(&storedKMSysCfg);
Q 352                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdSaveSysCfg,0,0);
353                 SendPacket(nChn, p2, PacketLen);
bfc108 354                 break;
Q 355             case     KLCmdSaveRunStat:
356                 SaveRunStat(&KMRunStat);
85d591 357                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdSaveRunStat,0,0);
Q 358                 SendPacket(nChn, p2, PacketLen);
bfc108 359                 break;
Q 360             case KLCmdGetEventLogCount:
361                 DataLen= 4;
362                 pData=&KMem.nEventCount;
85d591 363                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
Q 364                 SendPacket(nChn, p2, PacketLen);
bfc108 365                 break;
Q 366             case KLCmdGetEventLog:
367                 nAddr=p1->Params[0]+ (p1->Params[1]<<8);
368                 DataLen= p1->Params[2] * sizeof(stEventLog);
369                 pData=GetEventLogAddr(nAddr);
85d591 370                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
Q 371                 SendPacket(nChn, p2, PacketLen);
bfc108 372                 break;
Q 373             case KLCmdClearEventLog:
374                 ClearEventLog();
85d591 375                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdClearEventLog,0,0);
Q 376                 SendPacket(nChn, p2, PacketLen);
bfc108 377                 break;            
Q 378             default:
379             DataLen=1;
380             rData[0]=KL_UNKNOWN;
381             PacketLen=KLMakeRplyPacket(p2,nKLAddr,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData);
85d591 382             SendPacket(nChn, p2, PacketLen);            
bfc108 383                 break;        
Q 384         }
385     }    
386     return 0;
387 }
85d591 388 int KLParsePacket(int nChn, void * pBuf, int Len1)
bfc108 389 {
85d591 390 //    pKLReqPktHdr p1 = (pKLReqPktHdr)pBuf;
Q 391 //    KLThisuS=GetuS();
392 //    pKLRplyPktHdr p2=(pKLRplyPktHdr)KLPacketBuf2;
bfc108 393     int PacketLen=Len1;
85d591 394     int Result=KLCheckPacket(nChn, pBuf, Len1);
bfc108 395     if (Result != KL_OK)
Q 396     {
397         int DataLen=4;
398         char rData[4];
399         rData[0]=Result;
400         
85d591 401         PacketLen=KLMakeRplyPacket(KLPacketBuf2,nKLAddr,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData);
bfc108 402 //        PacketLen=KLMakePacket(p2,0,nAddr,cmdKLPing,p1->nSEQ,DataLen,rData);
85d591 403         SendPacket(nChn, KLPacketBuf2, PacketLen);
bfc108 404         return Result;
Q 405     }
85d591 406 //    if (bKBusMaster) Result=ParseMasterPacket(p1,Len1);
bfc108 407 //    memcpy(KLPacketBuf2,pBuf,Len1);
85d591 408 //    SendPacket(nChn, KLPacketBuf2, PacketLen);
bfc108 409     
85d591 410     Result=KLParseReqPacket(nChn, pBuf, Len1);
bfc108 411     
Q 412     //int len1=p1->PacketLen;
85d591 413 //    if (p1->DstHost!=255&&p1->DstHost!=2) return -3;
bfc108 414 //    pPacket p2=(pPacket)PacketBuf2;
Q 415 //            Uart2Stat.OKPacket++;
416
417     return Result;
418 }
419