2 #include "nvitem_common.h"
\r
3 #include "nvitem_buf.h"
\r
4 #include "nvitem_sync.h"
\r
5 #include "nvitem_packet.h"
\r
7 #define SYNC_REQ_HEAD 0
\r
8 #define SYNC_REQ_BODY 1
\r
9 #define SYNC_REQ_TAIL 2
\r
13 uint32 packet_type; // headd,body,tail
\r
14 uint32 reqIdx; // ++operation
\r
15 uint32 partId; // parition Id
\r
16 uint32 sctSize; // size of sector
\r
21 uint32 packet_type; // headd,body,tail
\r
22 uint32 reqIdx; // ++operation
\r
23 uint32 partId; // parition Id
\r
24 uint32 start; // start position of dirty data
\r
25 uint32 num; // number of dirty data, unit is sector
\r
30 uint32 packet_type; // headd,body,tail
\r
31 uint32 reqIdx; // ++operation
\r
32 uint32 partId; // parition Id
\r
43 uint32 _syncState; // 0: analyzer head ,1 analyzer body, 2 annalyzer tail
\r
44 uint32 _syncBusy = 0; // 0: idle, 1: busy
\r
46 static uint32 syncReqFrame = 0;
\r
47 static uint8* syncReqBuf[sizeof(SYNC_REQ)];
\r
48 static uint32 _ofst = 0;
\r
49 static uint32 _id = (uint32)(-1);
\r
50 static uint32 _start = 0;
\r
51 static uint32 _len = 0;
\r
53 //----------------------------
\r
55 //----------------------------
\r
56 void _syncInit(void)
\r
67 //----------------------------
\r
68 // Reset Sync module
\r
69 //----------------------------
\r
71 #define SYNC_FAIL_RESET 0
\r
72 #define SYNC_SUC_RESET 1
\r
73 static BOOLEAN _syncReset(uint32 ifSuc)
\r
80 ret = backupData(_id);
\r
84 if(_syncState) // in this state , fromchannel buffer maybe changed. So we must restore it.
\r
93 //--------------------------------------------------
\r
94 // TRUE: have getted
\r
95 // FALSE: not getted, need more information
\r
97 static uint32 __getToken(uint8* *buf, uint32* size)
\r
99 if((sizeof(SYNC_REQ)-_ofst) > *size)
\r
105 memcpy(syncReqBuf+_ofst, *buf, *size);
\r
113 if(0 == (sizeof(SYNC_REQ)-_ofst))
\r
118 memcpy(syncReqBuf+_ofst, *buf,(sizeof(SYNC_REQ)-_ofst));
\r
119 *buf += (sizeof(SYNC_REQ)-_ofst);
\r
120 *size -= (sizeof(SYNC_REQ)-_ofst);
\r
127 #define SYNC_CONTINUE 0 // analyzer not complete, information is not enough, wait for continuous information
\r
128 #define SYNC_NEXT 1 // one token analyzer complete, should analyzer next token of request
\r
129 #define SYNC_DONE 2 // all token analyzer is all complete
\r
130 #define SYNC_FAIL 3 // analyzer fail, infomation error, should reset
\r
132 static uint32 _syncGetHead(uint8** buf, uint32 *size)
\r
134 SYNC_REQ* head = (SYNC_REQ*)syncReqBuf;
\r
136 if(!__getToken(buf,size))
\r
138 return SYNC_CONTINUE;
\r
140 if(SYNC_REQ_HEAD != head->head.packet_type)
\r
144 syncReqFrame = head->head.reqIdx;
\r
145 printf("NVITEM:_syncGetHead partId 0x%x\n",head->head.partId);
\r
146 _id = getCtlId(head->head.partId);
\r
147 if(((uint32)-1) == _id)
\r
155 static uint32 _syncGetBody(uint8** buf, uint32* size)
\r
157 SYNC_REQ* req = (SYNC_REQ*)syncReqBuf;
\r
159 if(!__getToken(buf,size))
\r
161 return SYNC_CONTINUE;
\r
163 if(SYNC_REQ_BODY == req->body.packet_type)
\r
165 if((syncReqFrame+1) != req->body.reqIdx)
\r
170 _markDirtyInfo(_id, req->body.start, req->body.num);
\r
171 _start = req->body.start*RAMNV_SECT_SIZE;
\r
172 _len = req->body.num*RAMNV_SECT_SIZE;
\r
175 else if(SYNC_REQ_TAIL == req->tail.packet_type)
\r
177 if((syncReqFrame+1) != req->tail.reqIdx)
\r
191 static uint32 _syncGetData(uint8** buf, uint32* size)
\r
195 writeData(_id, _start, _len, *buf);
\r
204 writeData(_id, _start, *size, *buf);
\r
209 return SYNC_CONTINUE;
\r
214 //----------------------------
\r
215 // analyzer packet buf
\r
216 //----------------------------
\r
218 void syncAnalyzer(void)
\r
221 uint32 packetState;
\r
227 packetState = _getPacket(&buf, &size);
\r
228 if(_PACKET_FAIL == packetState)
\r
230 //printf("NVITEM:packetState fail\n");
\r
231 // packet module connection fail. teminate current req process
\r
232 //_syncReset(SYNC_FAIL_RESET);
\r
235 else if((_PACKET_START == packetState)&&_syncBusy)
\r
237 printf("NVITEM:packetState no req tail\n");
\r
238 // 1 == _syncBusy, means current req not finish
\r
239 // _PACKET_START means next req has come. so teminate current req process, and start new process
\r
240 _syncReset(SYNC_FAIL_RESET);
\r
242 else if((_PACKET_CONTINUE == packetState)&&(!_syncBusy))
\r
244 printf("NVITEM:packetState no req head\n");
\r
245 // 0 == _syncBusy, means no req is process.
\r
246 // 1 _PACKET_CONTINUE means body of req. So has no head, skip it.
\r
250 else if(_PACKET_SKIP == packetState)
\r
252 printf("NVITEM:packetState skip\n");
\r
253 // invalid packet, skip it
\r
261 if(0 == _syncState) // head analyzer
\r
263 syncRet = _syncGetHead(&buf, &size);
\r
264 if(SYNC_FAIL == syncRet)
\r
266 printf("NVITEM:_syncGetHead fail\n");
\r
267 _syncReset(SYNC_FAIL_RESET);
\r
271 else if(SYNC_NEXT == syncRet)
\r
276 else if(SYNC_CONTINUE == syncRet)
\r
282 else if(1 == _syncState) // body and tail analyzer
\r
284 syncRet = _syncGetBody(&buf, &size);
\r
285 if(SYNC_FAIL == syncRet)
\r
287 printf("NVITEM:_syncGetBody fail\n");
\r
288 _syncReset(SYNC_FAIL_RESET);
\r
292 else if(SYNC_NEXT == syncRet)
\r
297 else if(SYNC_CONTINUE == syncRet)
\r
302 else if(SYNC_DONE == syncRet)
\r
304 printf("NVITEM:sync _syncGetTail success\n");
\r
305 _sendPacktRsp(_syncReset(SYNC_SUC_RESET));
\r
309 else // (2 == _syncState) // data analyzer
\r
311 syncRet = _syncGetData(&buf, &size);
\r
312 if(SYNC_CONTINUE == syncRet)
\r
317 else if(SYNC_NEXT == syncRet)
\r