/**************************************************************** FunctionName : GAgent_Local_RecAll Description : receive all of data form local io one time. and put data int local cycle buf.This func will change data 0xff55 isn't SYNC HEAD to 0xff pgc : gagent global struct. return : void. ****************************************************************/ int32 GAgent_Local_RecAll(pgcontext pgc) { int32 fd; int32 available_len =0; int32 read_count = 0; uint32 offRec; fd = pgc->rtinfo.local.uart_fd; if(fd < 0) { return RET_FAILED; } if(RET_SUCCESS == GAgent_Local_IsDataValid(pgc)) { /* step 1.read data into loopbuf */ available_len = get_available_buf_space( pos_current, pos_start ); read_count = serial_read( fd, &hal_RxBuffer[pos_current & HAL_BUF_MASK],available_len ); offRec = pos_current; if(read_count <= 0) { return read_count; } pos_current += read_count; /* step 2. transfer 0xff55 to 0xff */ hal_decode_data(offRec, read_count); read_count = get_data_len(offRec, pos_current); } return read_count; }
/**************************************************************** FunctionName : find_packet_backward Description : 在串口缓冲区中,从指定位置往回查找包含该数据的一帧数据, 并返回帧头所在偏移。没有完整一帧数据则返回已接收数据的起始位置。 pos : 指定位置. return : 帧头所在偏移 Add by will --2015-05-21 ****************************************************************/ static int32 find_packet_backward(uint32 pos) { uint32 offset; uint8 data1; uint8 data2; uint32 datalen; uint16 packetlen; // 缓冲区中已知数据长度 datalen = get_data_len(pos_start, pos); if(datalen < MCU_SYNC_HEAD_LEN) { return pos_start; } if(datalen <= MCU_SYNC_HEAD_LEN + sizeof(packetlen)) { return pos_start; } offset = pos - MCU_SYNC_HEAD_LEN; while(offset != pos_start) { data1 = __halbuf_read(offset); data2 = __halbuf_read(offset + 1); if(MCU_HDR_FF == data1 && MCU_HDR_FF == data2) { datalen = get_data_len(offset, pos + 1); packetlen = __halbuf_read(offset + MCU_LEN_POS); packetlen = (packetlen << 8) | __halbuf_read(offset + MCU_LEN_POS + 1); packetlen += MCU_SYNC_HEAD_LEN + sizeof(packetlen); if(datalen <= packetlen) { return offset; } else { return offset + packetlen; } break; } offset--; } return pos_start; }
// 向前(左)移动数据,被移动的数据为从p_start_pos开始,到p_end_pos,移动长度为move_len void move_data_backward( uint8 *buf, int32 pos_start, int32 pos_end, int32 move_len) { int32 k; int32 pos_new_start; int32 move_data_len = get_data_len(pos_start, pos_end); pos_new_start = pos_start + (-1)*move_len; for(k=0; k < move_data_len; k++) { __halbuf_write(pos_new_start + k, __halbuf_read(pos_start + k)); } }
ValueBuffer* UnqliteCursor::get_data( bool as_binary, sxi64 value_len, pyunqlite::UserCallback* callback, pyunqlite::ValueBuffer* direct_buffer ) { ValueBuffer* value = 0; // setup the buffer for retrieving data if (direct_buffer) { if (value_len < 0) value_len = direct_buffer->get_data_len(); else if (direct_buffer->get_data_len() < value_len) throw UnqliteException(UNQLITE_INVALID); value = new ValueBuffer(*direct_buffer); } else if (!callback) { // determine the size of the stored data if it is unknown if (value_len < 0) value_len = get_data_len(); // create a new buffer value = new ValueBuffer(as_binary, value_len); if (!value) throw UnqliteException(UNQLITE_NOMEM); } int rc; if (callback) { rc = unqlite_kv_cursor_data_callback( this->_cursor, callback->get_unqlite_callback_function(), callback->get_unqlite_callback_data() ); } else { rc = unqlite_kv_cursor_data(this->_cursor, value->get_data(), &value_len); } if (callback && (rc == UNQLITE_ABORT)) callback->process_exception(); else if (rc != UNQLITE_OK) throw UnqliteException(rc, this->_db); return value; }
/**************************************************************** FunctionName : find_packet_forward Description : 在串口缓冲区中,从指定位置往前查找最近一帧完整数据, 返回该帧数据头所在位置。 pos : 指定位置. packetLen : 输出参数,该帧数据的长度 return : 失败--RET_FAILED 成功--帧头所在偏移 Add by will --2015-05-21 ****************************************************************/ static int32 find_packet_forward(uint32 pos, uint16 *packetLen) { uint8 data1; uint8 data2; uint32 datalen; uint16 packetlen; uint16 packetLenMin; uint32 start, end; start = pos; end = pos_current; datalen = get_data_len(start, end); /* A packet len must larger than the member sync head + len */ packetLenMin = MCU_SYNC_HEAD_LEN + sizeof(packetlen); while( datalen > packetLenMin ) { data1 = __halbuf_read(start); data2 = __halbuf_read(start + 1); if(MCU_HDR_FF == data1 && MCU_HDR_FF == data2) { packetlen = __halbuf_read(start + MCU_LEN_POS); packetlen = (packetlen << 8) | __halbuf_read(start + MCU_LEN_POS + 1); packetlen += 4; if(datalen < packetlen) { return RET_FAILED; } else { *packetLen = packetlen; return start; } } start++; datalen--; } return RET_FAILED; }
int32 check_isInPacket(uint32 pos) { uint32 packetStart; uint8 data1, data2; uint32 datalen; uint32 packetLen; packetStart = find_packet_backward(pos); data1 = __halbuf_read(packetStart); data2 = __halbuf_read(packetStart + 1); if(MCU_HDR_FF == data1 && MCU_HDR_FF == data2) { datalen = get_data_len(packetStart, pos) + 1; if(datalen <= MCU_SYNC_HEAD_LEN + sizeof(uint16)) { return 0; } packetLen = __halbuf_read(packetStart + MCU_LEN_POS); packetLen = (packetLen << 8) | __halbuf_read(packetStart + MCU_LEN_POS + 1); packetLen += MCU_SYNC_HEAD_LEN + sizeof(uint16); if(datalen > packetLen) { return 0; } else { return 1; } } else { return 0; } }
data *alloc_data(char *s) { int len = strlen(s); data *ret = malloc(sizeof(data)); int data_len = get_data_len(len); int i; ret->len = len; ret->l_len = data_len; ret->l1 = malloc(sizeof(long long) * data_len); ret->l2 = malloc(sizeof(long long) * data_len); memset(ret->l1, 0, sizeof(long long) * data_len); memset(ret->l2, 0, sizeof(long long) * data_len); char *head = &s[0], *tail = &s[len - 1]; char *l1 = (char *)ret->l1; char *l2 = (char *)ret->l2; int j; l1 += 7; l2 += 7; for (i = 0, j = 0; i < len; i++, j++) { if (j == 8) { j = 0; l1 += 15; l2 += 15; } *l1 = *head; *l2 = *tail; ++head; --tail; --l1; --l2; } return ret; }
/**************************************************************** FunctionName : Local_decode_data Description : decode local data in local cycle buf.transfer 0xff55 to 0xff. start : the posation in local cycle buf where will be decode length : decode data length will change the global pointer "put"--pos_current ****************************************************************/ void hal_decode_data(uint32 start, uint32 length) { static uint8 isSyncHead = 0; uint8 data; uint32 i; uint32 offRec; uint32 invalidLen; int32 ret; if(0 == length) { return ; } offRec = start; for(i = 0; i < length; i++) { data = __halbuf_read(offRec); if(isSyncHead) { isSyncHead = 0; /* check if is a new packet */ if(0xFF == data) { /* check if pre byte is in one packet */ ret = check_isInPacket(offRec - 1); if(0 == ret) { /* new packet */ offRec++; continue; } else { ret = find_packet_backward(offRec); invalidLen = get_data_len(ret, offRec + 1); move_data_backward(hal_RxBuffer, offRec + 1, pos_current, invalidLen); offRec -= invalidLen; pos_current = pos_current - invalidLen; } } /* check if is 0xff 0x55 */ else if(0x55 == data) { if((offRec & HAL_BUF_MASK) == ((pos_current - 1) & HAL_BUF_MASK)) { /* the last byte has received */ __halbuf_write(offRec, 0x00); } else { move_data_backward(hal_RxBuffer, offRec + 1, pos_current, 1); } offRec -= 1; pos_current -= 1; } /* invalid packet */ else { /* 找到当前字节处于哪一个packet */ ret = find_packet_backward(offRec); invalidLen = get_data_len(ret, offRec + 1); move_data_backward(hal_RxBuffer, offRec + 1, pos_current, invalidLen); offRec -= invalidLen; pos_current = pos_current - invalidLen; } } else if(MCU_HDR_FF == data) { isSyncHead = 1; } offRec++; } }