QuakeGod
2024-08-08 1fb934cda3067a81f82a7add0fa5b39f5ebf3094
提交 | 用户 | 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;
842bb6 160     unsigned short nValue=0;
483170 161 //int nSrcAddr=p1->SrcAddr;
Q 162
163     nKLStatus.nSEQ = ((pKLStat)(&(p1->Stat)))->nSEQ;;
164     
165     void * pData=0;
166     pKLRplyPktHdr p2=(pKLRplyPktHdr)KLPacketBuf2;
167     char rData[4];
5dd1b7 168     int res = 0 ;
483170 169     int PacketLen=0;
Q 170     KLRecvTimeuS=KLThisuS;
171     if (nDstHost==nKLStationId || nDstHost==0xff)
172     {
173         switch (p1->nCMD)
174         {
175             case KLCmdNone:
176                 break;
177             case KLCmdPing:
178                 DataLen=nDataType;
179 //                KLBufferIn[0]=p1->Params[0];
180 //                p1->Params[0]=KLBufferOut[0];
181                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdPing,DataLen,p1->Params);
182                 SendPacket(nChn, p2, PacketLen);
183                 break;
184 //            case KLCmdPingReply:
185 //                break;
186             case KLCmdInfo:
187                 //if (nDataType == KLDataTypeDT)
188                 DataLen= sizeof(KMInfoBlock);
189                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,&KMInfoBlock);
190                 SendPacket(nChn, p2, PacketLen);                
191                 break;
192             case KLCmdGetUid:
193                 //if (nDataType == KLDataTypeDT)
194                 DataLen= 12;
195                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,(uint32_t *)UID_BASE);
196                 SendPacket(nChn, p2, PacketLen);                
197                 break;
198             case KLCmdGetSN:
199                 //if (nDataType == KLDataTypeDT)
200                 DataLen= 4;
201                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,&((pFactoryData)FACTORY_DATA_BASE)->SN1);
202                 SendPacket(nChn, p2, PacketLen);                
203                 break;
204             case KLCmdGetFactoryData:
205                 DataLen= sizeof(stFactoryData);
206                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,(pFactoryData)FACTORY_DATA_BASE);
207                 SendPacket(nChn, p2, PacketLen);                
208                 break;
209             case KLCmdWriteFactoryData:
210                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
211                 DataLen= p1->Params[2];
212                 //DataLen=16;
213                 for (int i=0;i<DataLen;i++)
214                 {KLPacketBuf2[i]=p1->Params[4+i];}
215                 WriteFactoryData(KLPacketBuf2, DataLen);
216                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
217                 SendPacket(nChn, p2, PacketLen);
218                 break;
219
220             case KLCmdRead:
221             case KLCmdReadDataWord:
222             case KLCmdReadDataByte:                
223                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
224                 DataLen= p1->Params[2];
225                 if (nDataType ==KLDataTypeDT)                {            pData=KMem.DT+nWordAddr;        }
226                 else if (nDataType == KLDataTypeSDT)    {        pData=KMem.SDT+nWordAddr;    }
227                 else if (nDataType == KLDataTypeWSR)    {        pData=KMem.WSR+nWordAddr;    }
228                 else if (nDataType == KLDataTypeWX)        {        pData=KMem.WX+nWordAddr;        }
229                 else if (nDataType == KLDataTypeWY)        {        pData=KMem.WY+nWordAddr;        }
230                 else if (nDataType == KLDataTypeWR)        {        pData=KMem.WR+nWordAddr;        }
231                 else if (nDataType == KLDataTypeWLX)        {        pData=KMem.WLX+nWordAddr;        }
232                 else if (nDataType == KLDataTypeWLY)        {        pData=KMem.WLY+nWordAddr;        }
842bb6 233 #if (ENABLE_PLC)                
Q 234                 else if (nDataType == KLDataTypeSV)        {        pData=PLCMem.SV+nWordAddr;    }
235                 else if (nDataType == KLDataTypeEV)        {        pData=PLCMem.EV+nWordAddr;    }
236 #endif            
483170 237                 else if (nDataType == KLDataTypeTest)    {        pData=KMem.SDT+nWordAddr;    }
Q 238                 else if (nDataType == KLDataTypeWDT)    {        pData=KMem.WDT+nWordAddr;    }
5dd1b7 239                 else if (nDataType == KLDataTypeKBD)    {        pData=(unsigned short *)&KBus1.KBusChnStats  +nWordAddr;    }
8b51c7 240 #ifdef KWLESS
eaf5d5 241                 else if (nDataType == KLDataTypeKWLD)    {        pData=(unsigned short *)&KwRunStat  +nWordAddr;    }
8b51c7 242 #endif
483170 243                 else if (nDataType == KLDataSysCfg) { pData = (unsigned short *)&storedKMSysCfg.theKMSysCfg + nWordAddr;}
Q 244                 else if (nDataType == KLDataTypeFlash) { pData = (unsigned short *)FLASH_BASE + nWordAddr;}
245                 else                {                    pData=KLBufferOut+nWordAddr;                }
246                 
247                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
248                 SendPacket(nChn, p2, PacketLen);
249                 
250                 break;
251 //            case KLCmdReadReply:
252 //                break;
253             case KLCmdWrite:
254             case KLCmdWriteDataWord:
255             case KLCmdWriteDataByte:
256                 
257                 //memcpy(DispBuf,p1->data,DataLen);
258                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
259                 DataLen= p1->Params[2];
260                     if (nDataType ==KLDataTypeDT)                {        pData=KMem.DT+nWordAddr;        }
261                 else if (nDataType == KLDataTypeSDT)    {        pData=KMem.SDT+nWordAddr;    }
262                 else if (nDataType == KLDataTypeWSR)    {        pData=KMem.WSR+nWordAddr;    }
263                 else if (nDataType == KLDataTypeWX)        {        pData=KMem.WX+nWordAddr;        }
264                 else if (nDataType == KLDataTypeWY)        {        pData=KMem.WY+nWordAddr;        }
265                 else if (nDataType == KLDataTypeWR)        {        pData=KMem.WR+nWordAddr;        }
266                 else if (nDataType == KLDataTypeWLX)        {        pData=KMem.WLX+nWordAddr;        }
267                 else if (nDataType == KLDataTypeWLY)        {        pData=KMem.WLY+nWordAddr;        }
842bb6 268 #if (ENABLE_PLC)                
Q 269                 else if (nDataType == KLDataTypeSV)        {        pData=PLCMem.SV+nWordAddr;    DataLen=0;}
270                 else if (nDataType == KLDataTypeEV)        {        pData=PLCMem.EV+nWordAddr;    DataLen=0;}
271 #endif
483170 272                 else if (nDataType == KLDataTypeTest)    {        pData=KMem.SDT+nWordAddr;    DataLen=0;}
Q 273                 else if (nDataType == KLDataTypeWDT)    {        pData=KMem.WDT+nWordAddr;    DataLen=0;}
5dd1b7 274                 else if (nDataType == KLDataTypeKBD)    {        pData=(unsigned short *)KBus1.KBusChnStats +nWordAddr;    DataLen=0;}
8b51c7 275 #ifdef KWLESS                
eaf5d5 276                 else if (nDataType == KLDataTypeKWLD)    {        pData=(unsigned short *)&KwRunStat  +nWordAddr;    }
8b51c7 277 #endif
483170 278                 else if (nDataType == KLDataSysCfg)        { pData = (unsigned short *)&(storedKMSysCfg) + nWordAddr;}
Q 279                 else if (nDataType == KLDataTypeFlash) { pData = (unsigned short *)FLASH_BASE + nWordAddr;}
280                 else                {                    pData=KLBufferOut+nWordAddr;    DataLen=0;            }
281                         
282                 memcpy(pData,p1->Params+4,DataLen);                    
283                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
284                 SendPacket(nChn, p2, PacketLen);                    
285                 break;
286 //            case KLCmdWriteReply:
287 //                break;
288
289             case KLCmdRead1Bit:
290                 nBitAddr=p1->Params[0]+ (p1->Params[1]<<8);
291                 DataLen = 1;
292                 if (nDataType == KLCoilTypeX)             { rData[0] = ((KMem.WX[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
293                 else if (nDataType == KLCoilTypeY) { rData[0] = ((KMem.WY[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
294                 else if (nDataType == KLCoilTypeR) { rData[0] = ((KMem.WR[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
295                 else if (nDataType == KLCoilTypeLX) { rData[0] = ((KMem.WLX[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
296                 else if (nDataType == KLCoilTypeLY) { rData[0] = ((KMem.WLY[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
842bb6 297 #if (ENABLE_PLC)                
Q 298                 else if (nDataType == KLCoilTypeT) { rData[0] = PLCMem.Timers[nBitAddr].bTon;}
299                 else if (nDataType == KLCoilTypeC) { rData[0] = PLCMem.Timers[nBitAddr].bTon;}
300 #endif
483170 301                 else if (nDataType == KLCoilTypeSR) {rData[0] = ((KMem.WSR[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
Q 302                 
303                 else if (nDataType == KLCoilTypeLR) { rData[0] = 0;}
304                 else {rData[0]=0;}
305                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdRead1Bit,DataLen,rData);
306                 SendPacket(nChn, p2, PacketLen);
307                 break;
308             case KLCmdWrite1Bit:
309                 nBitAddr=p1->Params[0]+ (p1->Params[1]<<8);
310                 if (nDataType == KLCoilTypeX)             { SetBitValue( &KMem.WX[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
311                 else if (nDataType == KLCoilTypeY) { SetBitValue( &KMem.WY[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
312                 else if (nDataType == KLCoilTypeR) { SetBitValue( &KMem.WR[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
313                 else if (nDataType == KLCoilTypeLX) {SetBitValue( &KMem.WLX[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
314                 else if (nDataType == KLCoilTypeLY) {SetBitValue( &KMem.WLY[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
842bb6 315 #if (ENABLE_PLC)            
Q 316                 else if (nDataType == KLCoilTypeT) { PLCMem.Timers[nBitAddr].bTon = p1->Params[2];}
317                 else if (nDataType == KLCoilTypeC) { PLCMem.Timers[nBitAddr].bTon = p1->Params[2];}
318 #endif
483170 319                 else if (nDataType == KLCoilTypeLR) { SetBitValue( &KMem.WSR[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);;}
Q 320                 else {rData[0]=0;}
321                 DataLen=0;
322                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdWrite1Bit,DataLen,rData);
323                 SendPacket(nChn, p2, PacketLen);
324                 break;
325             case KLCmdReadBits:
326                 break;
327             case KLCmdWriteBits:
842bb6 328                 break;
Q 329 #if (ENABLE_PLC)
330             case KLCmdGetMode:
331                 rData[0]=    PLCMem.bPLCRunning;
332                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,1,rData);
333                 SendPacket(nChn, p2, PacketLen);            
483170 334                 break;
Q 335             case KLCmdChgMode:
336                 if (nDataType ==0) {StopPLC();}
337                 if (nDataType == 1) {StartPLC();}
338                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
339                 SendPacket(nChn, p2, PacketLen);                
340                 break;
5dd1b7 341             case KLCmdReadPLCProgram:
483170 342                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
Q 343                 DataLen= p1->Params[2];
344                  if (nDataType==0){
842bb6 345                         pData = (unsigned short *)(&((stStoredBinProgs *)STORE_PRG_BASE)->BinInstrcns) + nWordAddr;
483170 346                  } else if (nDataType==1){
842bb6 347                         pData = (unsigned short *)(&((stStoredBinProgs *)ALT_PRG_BASE)->BinInstrcns) + nWordAddr;
483170 348                  } else if (KMRunStat.nBinProgBank == 0) {
842bb6 349                         pData = (unsigned short *)(&((stStoredBinProgs *)STORE_PRG_BASE)->BinInstrcns) + nWordAddr;
483170 350                  }else {
842bb6 351                         pData = (unsigned short *)(&((stStoredBinProgs *)ALT_PRG_BASE)->BinInstrcns) + nWordAddr;
483170 352                  }
Q 353                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
354                 SendPacket(nChn, p2, PacketLen);
355                 
356                 break;
5dd1b7 357             case KLCmdStartPLCProgram:
842bb6 358                 DataLen=p1->Params[0] + (p1->Params[1] <<8);        //Program Size
Q 359                 nValue = p1->Params[2] + (p1->Params[3] <<8);   // CRC
360                 //if (PLCMem.bPLCRunning) StopPLC();
361                 StartPLCProgram(nDataType, DataLen, nValue);
5dd1b7 362                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdStartPLCProgram,0,0);
483170 363                 SendPacket(nChn, p2, PacketLen);
842bb6 364
483170 365                 break;
5dd1b7 366             case KLCmdWritePLCProgram:
483170 367                 if (PLCMem.bPLCRunning) PLCMem.bPLCRunning=0;
Q 368                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
369                 DataLen= p1->Params[2];
842bb6 370                 WritePLCProgram(nDataType, nWordAddr, &p1->Params[4], DataLen);
483170 371                 DataLen=4;
842bb6 372 //                *((int *)(&rData[0]))=(long)(p1->Params+4);
5dd1b7 373                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdWritePLCProgram,0,0);
483170 374                 SendPacket(nChn, p2, PacketLen);
Q 375                 break;
5dd1b7 376             case KLCmdFinishPLCProgram:
842bb6 377                 DataLen=p1->Params[0]+ (p1->Params[1]<<8);        //Program Size;
Q 378                 nValue = p1->Params[2] + (p1->Params[3] <<8);                //CRC
379                 FinishiPLCProgram(nDataType, DataLen, nValue);
5dd1b7 380                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdFinishPLCProgram,0,0);
483170 381                 SendPacket(nChn, p2, PacketLen);
Q 382                 break;
842bb6 383             case KLCmdReadPLCAnno:
Q 384                 nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);
385                 DataLen= p1->Params[2];
386                 pData = ((stStoredAnno *)STORE_PLC_ANNO_BASE)->Annos + nByteAddr;
387                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
388                 SendPacket(nChn, p2, PacketLen);
389                 break;
390             case KLCmdStartPLCAnno:
391                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
392                 DataLen= p1->Params[2];
393                 StartPLCAnno(nDataType, DataLen, nValue);
394                 DataLen=4;
395 //                *((int *)(&rData[0]))=(long)(p1->Params+4);
396                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdStartPLCAnno,0,0);
397                 SendPacket(nChn, p2, PacketLen);
398                 break;
399             case KLCmdWritePLCAnno:
400                 nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);        ;
401                 DataLen = p1->Params[2];                //CRC
402                 WritePLCAnno(nDataType, nByteAddr, &p1->Params[4], DataLen);
403                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdWritePLCAnno,0,0);
404                 SendPacket(nChn, p2, PacketLen);
405                 break;            
406             case KLCmdFinishPLCAnno:
407                 DataLen=p1->Params[0]+ (p1->Params[1]<<8);        //Program Size;
408                 nValue = p1->Params[2] + (p1->Params[3] <<8);                //CRC
409                 FinishiPLCAnno(nDataType, DataLen, nValue);
410                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdFinishPLCAnno,0,0);
411                 SendPacket(nChn, p2, PacketLen);
412                 break;            
413 #endif // ENABLE_PLC                
483170 414             case KLCmdBlinkLED:
Q 415                 DataLen=nDataType;
416                 KMRunStat.bLEDFlick=DataLen;
417                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
418                 SendPacket(nChn, p2, PacketLen);
419                 break;
420             
421             case KLCmdStopBlinkLED:
422                 DataLen=nDataType;
423                 KMRunStat.bLEDFlick=0;
424                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
425                 SendPacket(nChn, p2, PacketLen);
426                 break;
427             
428             case KLCmdReadRunStat:
429                 DataLen= sizeof(stRunStat);
430                 pData=&KMRunStat;
431                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
432                 SendPacket(nChn, p2, PacketLen);    
433                 break;
434             case KLCmdReadSysCfg:
435                 nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);
436                 pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
437                 DataLen = p1->Params[2];
438                 if (nByteAddr >= sizeof(stKMSysCfg)) {DataLen=0;}
439                 else {
440                     if (DataLen + nByteAddr >sizeof(stKMSysCfg))
441                         DataLen= sizeof(stKMSysCfg) - nByteAddr;
442                 }
443                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
444                 SendPacket(nChn, p2, PacketLen);    
445                 break;
446             case KLCmdWriteSysCfg:
447                 nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);
448                 pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
449                 DataLen = p1->Params[2];
450                 if (nByteAddr >= sizeof(stKMSysCfg)) {DataLen=0;}
451                 else {
452                     if (DataLen + nByteAddr >sizeof(stKMSysCfg))
453                         DataLen= sizeof(stKMSysCfg) - nByteAddr;
454                 }
455                 memcpy(pData,p1->Params+4,DataLen);                    
456                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
457                 SendPacket(nChn, p2, PacketLen);                
458                 WriteSysCfgToFlash(&storedKMSysCfg);
459                 break;
460             case KLCmdSaveSysCfg:
461                 WriteSysCfgToFlash(&storedKMSysCfg);
462                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdSaveSysCfg,0,0);
463                 SendPacket(nChn, p2, PacketLen);
464                 break;
465             case     KLCmdSaveRunStat:
466                 SaveRunStat(&KMRunStat);
467                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdSaveRunStat,0,0);
468                 SendPacket(nChn, p2, PacketLen);
469                 break;
470             case KLCmdClearRunStat:
5dd1b7 471                 memset(KBus1.KBusChnStats,0,sizeof(KBus1.KBusChnStats));
483170 472                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
Q 473                 SendPacket(nChn, p2, PacketLen);                    
474                 break;
475             case KLCmdGetEventLogCount:
476                 DataLen= 4;
477                 pData=&KMem.nEventCount;
478                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
479                 SendPacket(nChn, p2, PacketLen);
480                 break;
481             case KLCmdGetEventLog:
482                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
483                 DataLen= p1->Params[2] * sizeof(stEventLog);
484                 pData=GetEventLogAddr(nWordAddr);
485                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
486                 SendPacket(nChn, p2, PacketLen);
487                 break;
488             case KLCmdClearEventLog:
489                 ClearEventLog();
490                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdClearEventLog,0,0);
491                 SendPacket(nChn, p2, PacketLen);
492                 break;            
5dd1b7 493             case KLCmdResetMachine:
Q 494
495                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdClearEventLog,0,0);
496                 SendPacket(nChn, p2, PacketLen);
842bb6 497                 KMRunService(ReqReset,0,0,0,0);
5dd1b7 498                 break;                    
Q 499             case KLCmdWriteFirmware:
500                 nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);
501                 //pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
502                 DataLen = p1->Params[2];
842bb6 503                 pData = p1->Params + 4 ;
Q 504                 res = KMRunService(ReqUpdateFirm,nByteAddr,0,&pData,&DataLen);
505             
506 //                res = WriteNewApp(nByteAddr,p1->Params+4,DataLen);
507 //            res = 0;
5dd1b7 508                 if (res) {
Q 509                     PacketLen=KLMakeRplyPacket(p2,nKLStationId,nKLStatus.StatByte,KLCmdErrRply,res,rData);                    
510                 }else {                    
511                     PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
512                 }
513                 SendPacket(nChn, p2, PacketLen);                
514
842bb6 515                 
5dd1b7 516                 break;
Q 517             case KLCmdWriteFirmInfo:
518                 nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);
519                 //pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
520                 DataLen = p1->Params[2];
842bb6 521                 pData = p1->Params + 4 ;
Q 522                 res = KMRunService(ReqUpdateFirmInfo,nByteAddr,0,&pData,&DataLen);
5dd1b7 523                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
Q 524                 SendPacket(nChn, p2, PacketLen);        
842bb6 525 //                WriteNewAppInfo(nByteAddr,p1->Params+4,DataLen);            
Q 526                 break;
527             case KLCmdGetPortInfo:
528                 
529                 nByteAddr=p1->Params[0] + (p1->Params[1]<<8);
530                 //pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
531                 pData = KMem.pPorts[nDataType];
532                 DataLen = sizeof(stPortDef);
533                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
534                 SendPacket(nChn, p2, PacketLen);        
535                 break;            
536             case KLCmdGetPortChnInfo:
537                 
538                 nByteAddr=p1->Params[0] + (p1->Params[1]<<8);
539                 // nDataType --> port 
540                 // nByteAddr --> ChildId
541                 //pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
542               KMPortReqFunc(nDataType,ReqPortChnInfo,nByteAddr,0,&pData,&DataLen);
543                 
544                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
545                 SendPacket(nChn, p2, PacketLen);        
546                 break;            
547             
548             case KLCmdGetPortChildInfo:
549                 nByteAddr=p1->Params[0] + (p1->Params[1]<<8);
550                 // nDataType --> port 
551                 // nByteAddr --> ChildId
552                 //pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
553               KMPortReqFunc(nDataType,ReqPortChildInfo,nByteAddr,0,&pData,&DataLen);
554                 
555                 //DataLen = sizeof(stPortDef);
556                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
557                 SendPacket(nChn, p2, PacketLen);        
558                 break;            
559             
560             case KLCmdPortRemoteReq:        // 远程服务请求
561                 //p1->nType1 -> ReqId;
562                 //p1->Params[0] -> Port;
563                 //p1->Params[1] -> Child;
564                 //p1->params[2] -> Param;
565                 //p1->params[3] -> nAddr % 256;
566                 //p1->params[4] -> nAddr / 256;
567                 //p1->params[5] -> nCount;
568                 pData = &p1->Params[3];
569                 DataLen = p1->Params[5]+3;
570             
571                 KMPortReqFunc(p1->Params[0],nDataType,p1->Params[1],p1->Params[2],&pData,&DataLen);
572                 DataLen=0;
573                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
574                 SendPacket(nChn, p2, PacketLen);
5dd1b7 575                 break;
483170 576             default:
5dd1b7 577             //DataLen=1;
Q 578             DataLen=KL_UNKNOWN;            
483170 579             rData[0]=KL_UNKNOWN;
Q 580             PacketLen=KLMakeRplyPacket(p2,nKLStationId,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData);
581             SendPacket(nChn, p2, PacketLen);            
582                 break;        
583         }
584     }    
585     return 0;
586 }
587 int KLParsePacket(int nChn, void * pBuf, int Len1)
588 {
589 //    pKLReqPktHdr p1 = (pKLReqPktHdr)pBuf;
590 //    KLThisuS=GetuS();
591 //    pKLRplyPktHdr p2=(pKLRplyPktHdr)KLPacketBuf2;
592     int PacketLen=Len1;
593     int Result=KLCheckPacket(nChn, pBuf, Len1);
594     if (Result != KL_OK)
595     {
596         int DataLen=4;
597         char rData[4];
598         rData[0]=Result;
5dd1b7 599         DataLen = Result;
483170 600         PacketLen=KLMakeRplyPacket(KLPacketBuf2,nKLStationId,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData);
Q 601 //        PacketLen=KLMakePacket(p2,0,nAddr,cmdKLPing,p1->nSEQ,DataLen,rData);
602         SendPacket(nChn, KLPacketBuf2, PacketLen);
603         return Result;
604     }
605 //    if (bKBusMaster) Result=ParseMasterPacket(p1,Len1);
606 //    memcpy(KLPacketBuf2,pBuf,Len1);
607 //    SendPacket(nChn, KLPacketBuf2, PacketLen);
608     
609     Result=KLParseReqPacket(nChn, pBuf, Len1);
610     
611     //int len1=p1->PacketLen;
612 //    if (p1->DstHost!=255&&p1->DstHost!=2) return -3;
613 //    pKBPacket p2=(pKBPacket)PacketBuf2;
614 //            Uart2Stat.OKPacket++;
615
616     return Result;
617 }
618