QuakeGod
2022-10-17 f04e2b1dac8f950376652c5a1dd8df6055af8fd0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
  ******************************************************************************
  * @file           : KLink.h
  * @brief          : Header for KLink.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
    */
#ifndef __KLINK_H__
#define __KLINK_H__
typedef unsigned char uchar;
typedef unsigned char UCHAR;
typedef unsigned short USHORT;
typedef unsigned int UINT;
 
enum eKLResult
{    
        KL_ERR = -1,
        KL_OK = 0,
        KL_NG = 1,
        KL_UNKNOWN =2,
        KL_TIMEOUT = 3,
        KL_BCC_ERR = 4,
        KL_PKG_ERR = 5,
        KL_SEQ_ERR = 6,
        KL_LENTH_EXCEED,
        KL_NEED_PASS,
        KL_NOT_SUPPORT,
    
}    ;
 
enum {KLSignStart='%',    //0x25
            KLSignReply='U',    //0x55
            KLSignEnd=0x0D,
            KLMaxPacketLength=64,
};
    enum enKLCMDs
    {
        KLCmdNone = 0x00,        //Nothing
        KLCmdPing = 0x01,            //Ping
//        KLCmdPingReply = 0x02,        //PingReply
        KLCmdInfo = 0x03,        //GetInfo
 
        KLCmdRead = 0x05,                //Read
//        KLCmdReadReply = 0x06,        //ReadReply
        KLCmdWrite = 0x07,                //Write
//        KLCmdWriteReply = 0x08,    //WriteReply
        KLCmdSaveToFlash = 0x09,        //
        
        KLCmdExChgData=0x0A,                //ExChangeDate, = Wirte + Read
//        KLCmdExChgDataReply,            //ExchangeData Reply
        
        KLCmdSyncRead=0x0d,                //SyncRead
        KLCmdSyncWrite,                        //SyncWrite
        KLCmdSequenRead,                    //Sequence Read
        KLCmdSyncTime,                        //SyncTime
        
        KLCmdChgMode = 0x11,
        KLCmdGetMode = 0x12,
 
        KLCmdChkPass,
        KLCmdSetPass,
        KLCmdEraseAll,
        KLCmdReadProgram,
        KLCmdStartProgram,
        KLCmdWriteProgram,
        KLCmdFinishProgram,
 
        KLCmdRead1Bit = 0x21,                //ReadSingleBit
        KLCmdWrite1Bit = 0x22,                //WriteSingleBit
        KLCmdReadBits = 0x23,                //ReadBits        n = 1 - 8
        KLCmdWriteBits = 0x24,                //ReadBits        n = 1 - 8
 
        KLCmdReadBitsByByte = 0x25,            //ReadBitsByByte
        KLCmdWriteBitsByByte = 0x26,        //ReadBitsByByte
 
        KLCmdReadBitsByWord = 0x27,            //ReadBitsByWord
        KLCmdWriteBitsByWord = 0x28,        //ReadBitsByWord
        
        KLCmdReadDataByte = 0x31,
        KLCmdWriteDataByte = 0x32 ,
 
        KLCmdReadDataWord = 0x33,
        KLCmdWriteDataWord = 0x34,
 
        KLCmdReadData = 0x35,
        KLCmdWriteData = 0x36 ,
        
        
        KLCmdReadProg = 0x51,
        KLCmdWriteProg,
        KLCmdReadSysCfg,
        KLCmdWriteSysCfg,
        KLCmdSaveSysCfg,
        KLCmdSaveRunStat,
        KLCmdReadRunStat,
        
        KLCmdEraseFlashPage = 0x61,
        KLCmdWriteToFlash = 0x62,
        
        KLCmdGetEventLogCount,
        KLCmdGetEventLog,
        KLCmdClearEventLog,
        KLCmdGetFactoryData,
        KLCmdWriteFactoryData,
        KLCmdResetMachine,
        KLCmdGetUid,
        KLCmdSetTime,
        KLCmdGetSN,
        KLCmdReadFactData,
        KLCmdWriteFactData,
 
        KLCmdMC = 0x70,         
        KLCmdMD,
        KLCmdMG,
        
        
        KLCmdErrRply=0xEE,                //ERRORReply
    };
 
    enum enKLInfoType
    {
        KLInfoTypeInfo = 0,
        KLInfoTypeUID = 1,
        KLInfoTypeSN = 2,
        KLInfoTypeEventLogCount = 3,
    };
    
extern unsigned char KLPacketBuf1[256];
extern unsigned char KLPacketBuf2[256];
 
extern unsigned char KLBufferIn[16];
extern unsigned char KLBufferOut[16];
 
extern unsigned char nKLAddr;
extern unsigned char nKLSeq;
 
extern int KLThisuS;
extern int KLRecvTimeuS;
 
#pragma anon_unions
typedef union tagKLStatDef
{
    UCHAR StatByte;
    struct {
        UCHAR nSEQ : 4;
        UCHAR HasExt : 1;
        UCHAR HasErr : 1;
    };
}unKLStat, *pKLStat;
/*
typedef struct tagKLStatDef
{
    UCHAR Confirm : 2;
    UCHAR HasExt : 1;
    UCHAR HasErr : 1;
}stKLStat,*pKLStat;
*/
extern unKLStat nKLStatus;
 
typedef struct tagKLReqPktHdr
{
    unsigned char ReqStSgn;        //
    unsigned char DstHost;        //
    unsigned char Stat;                //
    unsigned char nCMD;                //
    unsigned char nType1;            //
    unsigned char Params[1];        //
}stKLReqPktHdr,* pKLReqPktHdr;
/*
typedef struct tagKLPingReqPkt
{
    unsigned char ReqStSgn;        //
    unsigned char DstHost;        //
    unsigned char nCMD;                //
    unsigned char nSize;            //
    
}stKLPingReqPkt,* pKLPingReqPkt;
*/
typedef struct tagKLRplyPktHdr
{
    unsigned char RplyStSgn;    //
    unsigned char DstHost;        //
    unsigned char nStatus;        //    
    unsigned char nRplyCMD;        //
    unsigned char nSize1;            //
    unsigned char Datas[1];        //
}stKLRplyPktHdr,* pKLRplyPktHdr;
 
/*
typedef struct tagKLPktHdr
{
        unsigned char StSign;        //Æðʼ±ê¼Ç
        unsigned char SrcAddr;    //Ô´µØÖ·
        unsigned char DstHost;    //Ä¿±êµØÖ·
        unsigned char nCMD;        //ÃüÁî
    
}stKLPtHdr, * pKLPktHdr;
*/
/*
typedef struct tagKLRdPkt
{
        unsigned char StSign;        //Æðʼ±ê¼Ç        
        unsigned char SrcAddr;    //Ô´µØÖ·
        unsigned char DstHost;    //Ä¿±êµØÖ·
        unsigned char nCMD;        //ÃüÁî
        unsigned char nType;
        unsigned char nAddr;    //
}stKLRdPkt,* pKLRdKpt;
*/
/*
typedef struct tagKLPacket
    {
        unsigned char StSign;        //Æðʼ±ê¼Ç        
        unsigned char SrcAddr;    //Ô´µØÖ·
        unsigned char DstHost;    //Ä¿±êµØÖ·
        unsigned char nCMD;        //ÃüÁî
        unsigned char nSEQ;        //ÐòÁкÅ
        unsigned char LoadLen;    //Êý¾ÝÔغɳ¤¶È    ²»°üÀ¨Í·²¿5¸ö×Ö½Ú£¬²»°üÀ¨Î²²¿BCC¡£
        unsigned char data[1];        //Êý¾ÝÔغÉ,×îĩβÊÇBCC£¬Êý¾Ý³¤¶ÈΪ0ʱ£¬Êµ¼ÊÒ²ÓÐÒ»¸öÊý¾Ý¡£
    }stKLPacket,* pKLPacket;    
*/
 
unsigned char KLBCC(const void * pData, int nSize);
//
 
    /* Make a Packet and return Packet Length */
int KLMakeReqPacket(void * pBuf1,uchar Src, uchar Dst, uchar nType, uchar nSEQ, uchar DataLen, void *pData );
int KLMakeRplyPacket(void * pBuf1, uchar Dst, uchar Status, uchar nCmd, uchar DataLen, const void *pData );
 
/*   */
int KLCheckPacket(int nChn, void * pBuf1, int len1);
 
/* */
int KLParsePacket(int nChn, void * pBuf1, int Len1);
 
//int ReadData(void);
 
//int WriteData(void);
 
//int GetStat(void);
 
 
#endif    /* __KLINK_H__ */