QuakeGod
2024-09-02 7eb19e6024af7f05cf94c66fb843439a3509147e
提交 | 用户 | age
483170 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"
5dd1b7 12 #include "main.h"
Q 13
8b51c7 14 #ifdef KWLESS
eaf5d5 15 #include "KWireless.h"
8b51c7 16 #endif
eaf5d5 17 //extern stWLRunStat KwRunStat;
483170 18
842bb6 19 //unsigned char KLPacketBuf1[256];
483170 20 unsigned char KLPacketBuf2[256];
Q 21
22 unsigned char KLBufferIn[16]={0};
23 unsigned char KLBufferOut[16]={0};
24
25 unsigned  char nKLStationId=1;
26 unsigned  char nKLSeq=0;
27
28 unKLStat nKLStatus ={0};
29
30 int KLThisuS=0;
31 int KLRecvTimeuS=0;
32
842bb6 33 stPortDef KLinkPortReg1 = {.nPortHardType = 3,.nPortUseType = 2, .bEnable = 1, .bRunning =1, .StationId = 1, .bMaster = 0};
Q 34
35 int KLinkInit(int n)
36 {
37         
38         KMRegisterPort(0,&KLinkPortReg1);
39     return 0;
40 }
483170 41 unsigned char KLBCC(void const * pData, int nSize)
Q 42 {
43     unsigned char k;
44     k=0;
45     for (int i=0;i<nSize;i++)
46     {
47         k^=((unsigned char *)pData)[i];
48     }
49     return k;    
50 }
51 int KLMakeRplyPacket(void * pBuf, uchar dst, uchar Status, uchar nCmd, uchar DataLen,const void * pData )
52 {
53     pKLRplyPktHdr p1 = (pKLRplyPktHdr)pBuf;
54     int PacketLenth=0;
55     p1->RplyStSgn=KLSignReply;
56     p1->DstHost=dst;
57     p1->nStatus=Status;
58     p1->nRplyCMD=nCmd;
59     p1->nSize1=DataLen;
5dd1b7 60     if (nCmd == KLCmdErrRply) {
Q 61         DataLen = 0;
62         p1->Datas[DataLen]=KLBCC(p1,sizeof(stKLRplyPktHdr)+DataLen-1);
63         PacketLenth=sizeof(stKLRplyPktHdr)+DataLen;    
64         
65     } else {
66         memcpy(p1->Datas,pData,DataLen);
67         p1->Datas[DataLen]=KLBCC(p1,sizeof(stKLRplyPktHdr)+DataLen-1);
68         PacketLenth=sizeof(stKLRplyPktHdr)+DataLen;    
69     }
483170 70
Q 71     switch (nCmd)
72     {
73         case KLCmdNone:
74             PacketLenth=0;
75             break;
76         case KLCmdPing:
77 //        case KLCmdPingReply:
78             //p1->data[DataLen+1]=KLEndSign;        
79             //PacketLenth=sizeof(stKLPacket)+DataLen+1;        
80             break;
81         case KLCmdRead:
82 //        case KLCmdReadReply:
83             //p1->data[DataLen+1]=KLEndSign;
84             //PacketLenth=sizeof(stKBPacket)+DataLen+1;
85             break;
86         case KLCmdWrite:
87 //        case KLCmdWriteReply:
88         break;
89         case KLCmdErrRply:
90         
91         break;
92         default:
5dd1b7 93             
483170 94             break;
Q 95     }
96     
97     return PacketLenth;
98 }
99
100 int KLCheckPacket(int nChn, void * pBuf, int nLen1)
101 {
102     pKLReqPktHdr p1 = (pKLReqPktHdr) pBuf;
103     if (p1->ReqStSgn != KLSignStart)
104     {
105         return 1;
106     }
107     
108     int DataLen=0;    //p1->LoadLen;
109     if (DataLen>KLMaxPacketLength) 
110     {
111         return 2;
112     }
113     if (nLen1<DataLen+sizeof(stKLReqPktHdr))
114     {
115         //len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stKBPacket));
116         //PutStr(str3,len4);
117         return 3;    //not long enough                    
118     }
119 //    if (p1->data[DataLen+1] != EndSign)
120 //    {
121 //        KBusChnStats[nCurPollId].NoEndErr++;
122 //        Uart2Stat.LengthErr++;        
123 //        return -2;
124 //    }
125     unsigned char thisBCC=KLBCC(p1,nLen1-1);
126     if (thisBCC != ((uchar *)pBuf)[nLen1-1]) 
127     {//BCC Error;
128         return 4;
129     }         
130     return 0;
131 }
132
133 inline void SetAddrBit(unsigned short * pW, unsigned char bitAddr)
134 {
135     (*pW)|=1<<(bitAddr&0xf);
136 }
137
138 inline void ResetBit(unsigned short * pW, unsigned char bitAddr)
139 {
140     (*pW)&=~(1<<(bitAddr&0xf));
141 }
142
143 static inline void SetBitValue(unsigned short * pW, unsigned char bitAddr, unsigned char Value)
144 {
145     if (Value)    {    SetAddrBit(pW, bitAddr);}
146     else {ResetBit(pW, bitAddr);}
147 }
148
149 int KLParseReqPacket(int nChn, void * pBuf, int Len1)
150 {
151     pKLReqPktHdr p1 = (pKLReqPktHdr)pBuf;
152
153     unsigned char nDstHost=p1->DstHost;
154     KLRecvTimeuS=KLThisuS;
155     unsigned char nDataType=p1->nType1;
156     unsigned short nWordAddr=0;
157     unsigned short nByteAddr=0;
158     unsigned short nBitAddr=0;
159     unsigned short DataLen=0;    //p1->LoadLen;
7eb19e 160 #if (ENABLE_PLC)    
842bb6 161     unsigned short nValue=0;
7eb19e 162 #endif
483170 163 //int nSrcAddr=p1->SrcAddr;
Q 164
165     nKLStatus.nSEQ = ((pKLStat)(&(p1->Stat)))->nSEQ;;
166     
167     void * pData=0;
168     pKLRplyPktHdr p2=(pKLRplyPktHdr)KLPacketBuf2;
169     char rData[4];
5dd1b7 170     int res = 0 ;
483170 171     int PacketLen=0;
Q 172     KLRecvTimeuS=KLThisuS;
173     if (nDstHost==nKLStationId || nDstHost==0xff)
174     {
175         switch (p1->nCMD)
176         {
177             case KLCmdNone:
178                 break;
179             case KLCmdPing:
180                 DataLen=nDataType;
181 //                KLBufferIn[0]=p1->Params[0];
182 //                p1->Params[0]=KLBufferOut[0];
183                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdPing,DataLen,p1->Params);
184                 SendPacket(nChn, p2, PacketLen);
185                 break;
186 //            case KLCmdPingReply:
187 //                break;
188             case KLCmdInfo:
189                 //if (nDataType == KLDataTypeDT)
190                 DataLen= sizeof(KMInfoBlock);
191                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,&KMInfoBlock);
192                 SendPacket(nChn, p2, PacketLen);                
193                 break;
194             case KLCmdGetUid:
195                 //if (nDataType == KLDataTypeDT)
196                 DataLen= 12;
197                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,(uint32_t *)UID_BASE);
198                 SendPacket(nChn, p2, PacketLen);                
199                 break;
200             case KLCmdGetSN:
201                 //if (nDataType == KLDataTypeDT)
202                 DataLen= 4;
203                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,&((pFactoryData)FACTORY_DATA_BASE)->SN1);
204                 SendPacket(nChn, p2, PacketLen);                
205                 break;
206             case KLCmdGetFactoryData:
207                 DataLen= sizeof(stFactoryData);
208                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,(pFactoryData)FACTORY_DATA_BASE);
209                 SendPacket(nChn, p2, PacketLen);                
210                 break;
211             case KLCmdWriteFactoryData:
212                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
213                 DataLen= p1->Params[2];
214                 //DataLen=16;
215                 for (int i=0;i<DataLen;i++)
216                 {KLPacketBuf2[i]=p1->Params[4+i];}
217                 WriteFactoryData(KLPacketBuf2, DataLen);
218                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
219                 SendPacket(nChn, p2, PacketLen);
220                 break;
221
222             case KLCmdRead:
223             case KLCmdReadDataWord:
224             case KLCmdReadDataByte:                
225                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
226                 DataLen= p1->Params[2];
227                 if (nDataType ==KLDataTypeDT)                {            pData=KMem.DT+nWordAddr;        }
228                 else if (nDataType == KLDataTypeSDT)    {        pData=KMem.SDT+nWordAddr;    }
229                 else if (nDataType == KLDataTypeWSR)    {        pData=KMem.WSR+nWordAddr;    }
230                 else if (nDataType == KLDataTypeWX)        {        pData=KMem.WX+nWordAddr;        }
231                 else if (nDataType == KLDataTypeWY)        {        pData=KMem.WY+nWordAddr;        }
232                 else if (nDataType == KLDataTypeWR)        {        pData=KMem.WR+nWordAddr;        }
233                 else if (nDataType == KLDataTypeWLX)        {        pData=KMem.WLX+nWordAddr;        }
234                 else if (nDataType == KLDataTypeWLY)        {        pData=KMem.WLY+nWordAddr;        }
842bb6 235 #if (ENABLE_PLC)                
Q 236                 else if (nDataType == KLDataTypeSV)        {        pData=PLCMem.SV+nWordAddr;    }
237                 else if (nDataType == KLDataTypeEV)        {        pData=PLCMem.EV+nWordAddr;    }
238 #endif            
483170 239                 else if (nDataType == KLDataTypeTest)    {        pData=KMem.SDT+nWordAddr;    }
Q 240                 else if (nDataType == KLDataTypeWDT)    {        pData=KMem.WDT+nWordAddr;    }
5dd1b7 241                 else if (nDataType == KLDataTypeKBD)    {        pData=(unsigned short *)&KBus1.KBusChnStats  +nWordAddr;    }
8b51c7 242 #ifdef KWLESS
eaf5d5 243                 else if (nDataType == KLDataTypeKWLD)    {        pData=(unsigned short *)&KwRunStat  +nWordAddr;    }
8b51c7 244 #endif
483170 245                 else if (nDataType == KLDataSysCfg) { pData = (unsigned short *)&storedKMSysCfg.theKMSysCfg + nWordAddr;}
Q 246                 else if (nDataType == KLDataTypeFlash) { pData = (unsigned short *)FLASH_BASE + nWordAddr;}
247                 else                {                    pData=KLBufferOut+nWordAddr;                }
248                 
249                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
250                 SendPacket(nChn, p2, PacketLen);
251                 
252                 break;
253 //            case KLCmdReadReply:
254 //                break;
255             case KLCmdWrite:
256             case KLCmdWriteDataWord:
257             case KLCmdWriteDataByte:
258                 
259                 //memcpy(DispBuf,p1->data,DataLen);
260                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
261                 DataLen= p1->Params[2];
262                     if (nDataType ==KLDataTypeDT)                {        pData=KMem.DT+nWordAddr;        }
263                 else if (nDataType == KLDataTypeSDT)    {        pData=KMem.SDT+nWordAddr;    }
264                 else if (nDataType == KLDataTypeWSR)    {        pData=KMem.WSR+nWordAddr;    }
265                 else if (nDataType == KLDataTypeWX)        {        pData=KMem.WX+nWordAddr;        }
266                 else if (nDataType == KLDataTypeWY)        {        pData=KMem.WY+nWordAddr;        }
267                 else if (nDataType == KLDataTypeWR)        {        pData=KMem.WR+nWordAddr;        }
268                 else if (nDataType == KLDataTypeWLX)        {        pData=KMem.WLX+nWordAddr;        }
269                 else if (nDataType == KLDataTypeWLY)        {        pData=KMem.WLY+nWordAddr;        }
842bb6 270 #if (ENABLE_PLC)                
Q 271                 else if (nDataType == KLDataTypeSV)        {        pData=PLCMem.SV+nWordAddr;    DataLen=0;}
272                 else if (nDataType == KLDataTypeEV)        {        pData=PLCMem.EV+nWordAddr;    DataLen=0;}
273 #endif
483170 274                 else if (nDataType == KLDataTypeTest)    {        pData=KMem.SDT+nWordAddr;    DataLen=0;}
Q 275                 else if (nDataType == KLDataTypeWDT)    {        pData=KMem.WDT+nWordAddr;    DataLen=0;}
5dd1b7 276                 else if (nDataType == KLDataTypeKBD)    {        pData=(unsigned short *)KBus1.KBusChnStats +nWordAddr;    DataLen=0;}
8b51c7 277 #ifdef KWLESS                
eaf5d5 278                 else if (nDataType == KLDataTypeKWLD)    {        pData=(unsigned short *)&KwRunStat  +nWordAddr;    }
8b51c7 279 #endif
483170 280                 else if (nDataType == KLDataSysCfg)        { pData = (unsigned short *)&(storedKMSysCfg) + nWordAddr;}
Q 281                 else if (nDataType == KLDataTypeFlash) { pData = (unsigned short *)FLASH_BASE + nWordAddr;}
282                 else                {                    pData=KLBufferOut+nWordAddr;    DataLen=0;            }
283                         
284                 memcpy(pData,p1->Params+4,DataLen);                    
285                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
286                 SendPacket(nChn, p2, PacketLen);                    
287                 break;
288 //            case KLCmdWriteReply:
289 //                break;
290
291             case KLCmdRead1Bit:
292                 nBitAddr=p1->Params[0]+ (p1->Params[1]<<8);
293                 DataLen = 1;
294                 if (nDataType == KLCoilTypeX)             { rData[0] = ((KMem.WX[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
295                 else if (nDataType == KLCoilTypeY) { rData[0] = ((KMem.WY[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
296                 else if (nDataType == KLCoilTypeR) { rData[0] = ((KMem.WR[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
297                 else if (nDataType == KLCoilTypeLX) { rData[0] = ((KMem.WLX[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
298                 else if (nDataType == KLCoilTypeLY) { rData[0] = ((KMem.WLY[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
842bb6 299 #if (ENABLE_PLC)                
Q 300                 else if (nDataType == KLCoilTypeT) { rData[0] = PLCMem.Timers[nBitAddr].bTon;}
301                 else if (nDataType == KLCoilTypeC) { rData[0] = PLCMem.Timers[nBitAddr].bTon;}
302 #endif
483170 303                 else if (nDataType == KLCoilTypeSR) {rData[0] = ((KMem.WSR[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
Q 304                 
305                 else if (nDataType == KLCoilTypeLR) { rData[0] = 0;}
306                 else {rData[0]=0;}
307                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdRead1Bit,DataLen,rData);
308                 SendPacket(nChn, p2, PacketLen);
309                 break;
310             case KLCmdWrite1Bit:
311                 nBitAddr=p1->Params[0]+ (p1->Params[1]<<8);
312                 if (nDataType == KLCoilTypeX)             { SetBitValue( &KMem.WX[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
313                 else if (nDataType == KLCoilTypeY) { SetBitValue( &KMem.WY[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
314                 else if (nDataType == KLCoilTypeR) { SetBitValue( &KMem.WR[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
315                 else if (nDataType == KLCoilTypeLX) {SetBitValue( &KMem.WLX[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
316                 else if (nDataType == KLCoilTypeLY) {SetBitValue( &KMem.WLY[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
842bb6 317 #if (ENABLE_PLC)            
Q 318                 else if (nDataType == KLCoilTypeT) { PLCMem.Timers[nBitAddr].bTon = p1->Params[2];}
319                 else if (nDataType == KLCoilTypeC) { PLCMem.Timers[nBitAddr].bTon = p1->Params[2];}
320 #endif
483170 321                 else if (nDataType == KLCoilTypeLR) { SetBitValue( &KMem.WSR[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);;}
Q 322                 else {rData[0]=0;}
323                 DataLen=0;
324                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdWrite1Bit,DataLen,rData);
325                 SendPacket(nChn, p2, PacketLen);
326                 break;
327             case KLCmdReadBits:
328                 break;
329             case KLCmdWriteBits:
842bb6 330                 break;
Q 331 #if (ENABLE_PLC)
332             case KLCmdGetMode:
333                 rData[0]=    PLCMem.bPLCRunning;
334                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,1,rData);
335                 SendPacket(nChn, p2, PacketLen);            
483170 336                 break;
Q 337             case KLCmdChgMode:
338                 if (nDataType ==0) {StopPLC();}
339                 if (nDataType == 1) {StartPLC();}
340                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
341                 SendPacket(nChn, p2, PacketLen);                
342                 break;
5dd1b7 343             case KLCmdReadPLCProgram:
483170 344                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
Q 345                 DataLen= p1->Params[2];
346                  if (nDataType==0){
842bb6 347                         pData = (unsigned short *)(&((stStoredBinProgs *)STORE_PRG_BASE)->BinInstrcns) + nWordAddr;
483170 348                  } else if (nDataType==1){
842bb6 349                         pData = (unsigned short *)(&((stStoredBinProgs *)ALT_PRG_BASE)->BinInstrcns) + nWordAddr;
483170 350                  } else if (KMRunStat.nBinProgBank == 0) {
842bb6 351                         pData = (unsigned short *)(&((stStoredBinProgs *)STORE_PRG_BASE)->BinInstrcns) + nWordAddr;
483170 352                  }else {
842bb6 353                         pData = (unsigned short *)(&((stStoredBinProgs *)ALT_PRG_BASE)->BinInstrcns) + nWordAddr;
483170 354                  }
Q 355                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
356                 SendPacket(nChn, p2, PacketLen);
357                 
358                 break;
5dd1b7 359             case KLCmdStartPLCProgram:
842bb6 360                 DataLen=p1->Params[0] + (p1->Params[1] <<8);        //Program Size
Q 361                 nValue = p1->Params[2] + (p1->Params[3] <<8);   // CRC
362                 //if (PLCMem.bPLCRunning) StopPLC();
363                 StartPLCProgram(nDataType, DataLen, nValue);
5dd1b7 364                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdStartPLCProgram,0,0);
483170 365                 SendPacket(nChn, p2, PacketLen);
842bb6 366
483170 367                 break;
5dd1b7 368             case KLCmdWritePLCProgram:
483170 369                 if (PLCMem.bPLCRunning) PLCMem.bPLCRunning=0;
Q 370                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
371                 DataLen= p1->Params[2];
842bb6 372                 WritePLCProgram(nDataType, nWordAddr, &p1->Params[4], DataLen);
483170 373                 DataLen=4;
842bb6 374 //                *((int *)(&rData[0]))=(long)(p1->Params+4);
5dd1b7 375                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdWritePLCProgram,0,0);
483170 376                 SendPacket(nChn, p2, PacketLen);
Q 377                 break;
5dd1b7 378             case KLCmdFinishPLCProgram:
842bb6 379                 DataLen=p1->Params[0]+ (p1->Params[1]<<8);        //Program Size;
Q 380                 nValue = p1->Params[2] + (p1->Params[3] <<8);                //CRC
381                 FinishiPLCProgram(nDataType, DataLen, nValue);
5dd1b7 382                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdFinishPLCProgram,0,0);
483170 383                 SendPacket(nChn, p2, PacketLen);
Q 384                 break;
842bb6 385             case KLCmdReadPLCAnno:
Q 386                 nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);
387                 DataLen= p1->Params[2];
388                 pData = ((stStoredAnno *)STORE_PLC_ANNO_BASE)->Annos + nByteAddr;
389                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
390                 SendPacket(nChn, p2, PacketLen);
391                 break;
392             case KLCmdStartPLCAnno:
393                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
394                 DataLen= p1->Params[2];
395                 StartPLCAnno(nDataType, DataLen, nValue);
396                 DataLen=4;
397 //                *((int *)(&rData[0]))=(long)(p1->Params+4);
398                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdStartPLCAnno,0,0);
399                 SendPacket(nChn, p2, PacketLen);
400                 break;
401             case KLCmdWritePLCAnno:
402                 nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);        ;
403                 DataLen = p1->Params[2];                //CRC
404                 WritePLCAnno(nDataType, nByteAddr, &p1->Params[4], DataLen);
405                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdWritePLCAnno,0,0);
406                 SendPacket(nChn, p2, PacketLen);
407                 break;            
408             case KLCmdFinishPLCAnno:
409                 DataLen=p1->Params[0]+ (p1->Params[1]<<8);        //Program Size;
410                 nValue = p1->Params[2] + (p1->Params[3] <<8);                //CRC
411                 FinishiPLCAnno(nDataType, DataLen, nValue);
412                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdFinishPLCAnno,0,0);
413                 SendPacket(nChn, p2, PacketLen);
414                 break;            
415 #endif // ENABLE_PLC                
483170 416             case KLCmdBlinkLED:
Q 417                 DataLen=nDataType;
418                 KMRunStat.bLEDFlick=DataLen;
419                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
420                 SendPacket(nChn, p2, PacketLen);
421                 break;
422             
423             case KLCmdStopBlinkLED:
424                 DataLen=nDataType;
425                 KMRunStat.bLEDFlick=0;
426                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
427                 SendPacket(nChn, p2, PacketLen);
428                 break;
429             
430             case KLCmdReadRunStat:
431                 DataLen= sizeof(stRunStat);
432                 pData=&KMRunStat;
433                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
434                 SendPacket(nChn, p2, PacketLen);    
435                 break;
436             case KLCmdReadSysCfg:
437                 nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);
438                 pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
439                 DataLen = p1->Params[2];
440                 if (nByteAddr >= sizeof(stKMSysCfg)) {DataLen=0;}
441                 else {
442                     if (DataLen + nByteAddr >sizeof(stKMSysCfg))
443                         DataLen= sizeof(stKMSysCfg) - nByteAddr;
444                 }
445                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
446                 SendPacket(nChn, p2, PacketLen);    
447                 break;
448             case KLCmdWriteSysCfg:
449                 nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);
450                 pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
451                 DataLen = p1->Params[2];
452                 if (nByteAddr >= sizeof(stKMSysCfg)) {DataLen=0;}
453                 else {
454                     if (DataLen + nByteAddr >sizeof(stKMSysCfg))
455                         DataLen= sizeof(stKMSysCfg) - nByteAddr;
456                 }
457                 memcpy(pData,p1->Params+4,DataLen);                    
458                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
459                 SendPacket(nChn, p2, PacketLen);                
460                 WriteSysCfgToFlash(&storedKMSysCfg);
461                 break;
462             case KLCmdSaveSysCfg:
463                 WriteSysCfgToFlash(&storedKMSysCfg);
464                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdSaveSysCfg,0,0);
465                 SendPacket(nChn, p2, PacketLen);
466                 break;
467             case     KLCmdSaveRunStat:
468                 SaveRunStat(&KMRunStat);
469                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdSaveRunStat,0,0);
470                 SendPacket(nChn, p2, PacketLen);
471                 break;
472             case KLCmdClearRunStat:
5dd1b7 473                 memset(KBus1.KBusChnStats,0,sizeof(KBus1.KBusChnStats));
483170 474                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
Q 475                 SendPacket(nChn, p2, PacketLen);                    
476                 break;
477             case KLCmdGetEventLogCount:
478                 DataLen= 4;
479                 pData=&KMem.nEventCount;
480                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
481                 SendPacket(nChn, p2, PacketLen);
482                 break;
483             case KLCmdGetEventLog:
484                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
485                 DataLen= p1->Params[2] * sizeof(stEventLog);
486                 pData=GetEventLogAddr(nWordAddr);
487                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
488                 SendPacket(nChn, p2, PacketLen);
489                 break;
490             case KLCmdClearEventLog:
491                 ClearEventLog();
492                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdClearEventLog,0,0);
493                 SendPacket(nChn, p2, PacketLen);
494                 break;            
5dd1b7 495             case KLCmdResetMachine:
Q 496
497                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdClearEventLog,0,0);
498                 SendPacket(nChn, p2, PacketLen);
842bb6 499                 KMRunService(ReqReset,0,0,0,0);
5dd1b7 500                 break;                    
Q 501             case KLCmdWriteFirmware:
502                 nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);
503                 //pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
504                 DataLen = p1->Params[2];
842bb6 505                 pData = p1->Params + 4 ;
Q 506                 res = KMRunService(ReqUpdateFirm,nByteAddr,0,&pData,&DataLen);
507             
508 //                res = WriteNewApp(nByteAddr,p1->Params+4,DataLen);
509 //            res = 0;
5dd1b7 510                 if (res) {
Q 511                     PacketLen=KLMakeRplyPacket(p2,nKLStationId,nKLStatus.StatByte,KLCmdErrRply,res,rData);                    
512                 }else {                    
513                     PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
514                 }
515                 SendPacket(nChn, p2, PacketLen);                
516
842bb6 517                 
5dd1b7 518                 break;
Q 519             case KLCmdWriteFirmInfo:
520                 nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);
521                 //pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
522                 DataLen = p1->Params[2];
842bb6 523                 pData = p1->Params + 4 ;
Q 524                 res = KMRunService(ReqUpdateFirmInfo,nByteAddr,0,&pData,&DataLen);
5dd1b7 525                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
Q 526                 SendPacket(nChn, p2, PacketLen);        
842bb6 527 //                WriteNewAppInfo(nByteAddr,p1->Params+4,DataLen);            
Q 528                 break;
529             case KLCmdGetPortInfo:
530                 
531                 nByteAddr=p1->Params[0] + (p1->Params[1]<<8);
532                 //pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
533                 pData = KMem.pPorts[nDataType];
7eb19e 534                 DataLen = (uint32_t)&(((pstPortDef)(0))->pInstance);
842bb6 535                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
Q 536                 SendPacket(nChn, p2, PacketLen);        
537                 break;            
538             case KLCmdGetPortChnInfo:
539                 
540                 nByteAddr=p1->Params[0] + (p1->Params[1]<<8);
541                 // nDataType --> port 
542                 // nByteAddr --> ChildId
543                 //pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
544               KMPortReqFunc(nDataType,ReqPortChnInfo,nByteAddr,0,&pData,&DataLen);
545                 
546                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
547                 SendPacket(nChn, p2, PacketLen);        
548                 break;            
549             
550             case KLCmdGetPortChildInfo:
551                 nByteAddr=p1->Params[0] + (p1->Params[1]<<8);
552                 // nDataType --> port 
553                 // nByteAddr --> ChildId
554                 //pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
555               KMPortReqFunc(nDataType,ReqPortChildInfo,nByteAddr,0,&pData,&DataLen);
556                 
557                 //DataLen = sizeof(stPortDef);
558                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
559                 SendPacket(nChn, p2, PacketLen);        
560                 break;            
561             
562             case KLCmdPortRemoteReq:        // 远程服务请求
563                 //p1->nType1 -> ReqId;
564                 //p1->Params[0] -> Port;
565                 //p1->Params[1] -> Child;
566                 //p1->params[2] -> Param;
567                 //p1->params[3] -> nAddr % 256;
568                 //p1->params[4] -> nAddr / 256;
569                 //p1->params[5] -> nCount;
570                 pData = &p1->Params[3];
571                 DataLen = p1->Params[5]+3;
572             
573                 KMPortReqFunc(p1->Params[0],nDataType,p1->Params[1],p1->Params[2],&pData,&DataLen);
574                 DataLen=0;
575                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
576                 SendPacket(nChn, p2, PacketLen);
5dd1b7 577                 break;
483170 578             default:
5dd1b7 579             //DataLen=1;
Q 580             DataLen=KL_UNKNOWN;            
483170 581             rData[0]=KL_UNKNOWN;
Q 582             PacketLen=KLMakeRplyPacket(p2,nKLStationId,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData);
583             SendPacket(nChn, p2, PacketLen);            
584                 break;        
585         }
586     }    
587     return 0;
588 }
589 int KLParsePacket(int nChn, void * pBuf, int Len1)
590 {
591 //    pKLReqPktHdr p1 = (pKLReqPktHdr)pBuf;
592 //    KLThisuS=GetuS();
593 //    pKLRplyPktHdr p2=(pKLRplyPktHdr)KLPacketBuf2;
594     int PacketLen=Len1;
595     int Result=KLCheckPacket(nChn, pBuf, Len1);
596     if (Result != KL_OK)
597     {
598         int DataLen=4;
599         char rData[4];
600         rData[0]=Result;
5dd1b7 601         DataLen = Result;
483170 602         PacketLen=KLMakeRplyPacket(KLPacketBuf2,nKLStationId,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData);
Q 603 //        PacketLen=KLMakePacket(p2,0,nAddr,cmdKLPing,p1->nSEQ,DataLen,rData);
604         SendPacket(nChn, KLPacketBuf2, PacketLen);
605         return Result;
606     }
607 //    if (bKBusMaster) Result=ParseMasterPacket(p1,Len1);
608 //    memcpy(KLPacketBuf2,pBuf,Len1);
609 //    SendPacket(nChn, KLPacketBuf2, PacketLen);
610     
611     Result=KLParseReqPacket(nChn, pBuf, Len1);
612     
613     //int len1=p1->PacketLen;
614 //    if (p1->DstHost!=255&&p1->DstHost!=2) return -3;
615 //    pKBPacket p2=(pKBPacket)PacketBuf2;
616 //            Uart2Stat.OKPacket++;
617
618     return Result;
619 }
620