#include "protocol.h" #include "process.h" #include "cfg.h" #include "uart.h" #include "Motor.h" #include "AngleSensor.h" #include "Rtcx.h" #include "storage.h" static DataItem tmp_record; static uint8_t tmp_i=0; #ifdef IS_BOOTLOADER uint32_t Firmware_Version[4] = {1, 0, 0, 20240802}; #else uint32_t Firmware_Version[4] = {1, 1, 1, 20240803}; #endif uint16_t Read_FirmwareVersion(uint8_t *pBuf, uint16_t buf_len) { int i; if( buf_len < 16){ return 0; } for (i = 0; i < 4; ++i) { pBuf[i * 4] = (Firmware_Version[i] >> 24) & 0xff; pBuf[i * 4 + 1] = (Firmware_Version[i] >> 16) & 0xff; pBuf[i * 4 + 2] = (Firmware_Version[i] >> 8) & 0xff; pBuf[i * 4 + 3] = (Firmware_Version[i] >> 0) & 0xff; } return 16; } uint16_t Read_HardwareVersion(uint8_t *pBuf, uint16_t buf_len) { if( buf_len < 2){ return 0; } pBuf[0] = (config->hw_version >> 8)&0xff; pBuf[1] = (config->hw_version >> 0)&0xff; return 2; } uint16_t Read_Deviceid(uint8_t *pBuf, uint16_t buf_len) { if( buf_len < 4){ return 0; } pBuf[0] = (config->deviceid >> 24)&0xff; pBuf[1] = (config->deviceid >> 16)&0xff; pBuf[2] = (config->deviceid >> 8)&0xff; pBuf[3] = (config->deviceid >> 0)&0xff; return 4; } uint16_t Read_Devicetype(uint8_t *pBuf, uint16_t buf_len) { if( buf_len < 2){ return 0; } pBuf[0] = (config->devicetype >> 8)&0xff; pBuf[1] = (config->devicetype >> 0)&0xff; return 2; } uint16_t Read_Addr(uint8_t *pBuf, uint16_t buf_len) { if( buf_len < 2){ return 0; } pBuf[0] = 0x00; pBuf[1] = config->addr; return 2; } uint16_t Read_Baudrate(uint8_t *pBuf, uint16_t buf_len) { if( buf_len < 2){ return 0; } pBuf[0] = 0x00; pBuf[1] = config->br_index; return 2; } //读取锁状态 uint16_t Read_LockStatus(uint8_t *pBuf, uint16_t buf_len) { if( buf_len < 2){ return 0; } pBuf[0] = Process_GetLockStatus(); //锁状态 pBuf[1] = Process_GetCoverStatus(); //上盖状态 return 2; } //读取开锁、上锁阀值 uint16_t Read_Threshold(uint8_t *pBuf, uint16_t buf_len) { if( buf_len < 8){ return 0; } pBuf[0] = ((uint8_t*)(&config->unlock_threshold))[0]; pBuf[1] = ((uint8_t*)(&config->unlock_threshold))[1]; pBuf[2] = ((uint8_t*)(&config->unlock_threshold))[2]; pBuf[3] = ((uint8_t*)(&config->unlock_threshold))[3]; pBuf[4] = ((uint8_t*)(&config->lock_threshold))[0]; pBuf[5] = ((uint8_t*)(&config->lock_threshold))[1]; pBuf[6] = ((uint8_t*)(&config->lock_threshold))[2]; pBuf[7] = ((uint8_t*)(&config->lock_threshold))[3]; return 8; } //读取编码器角度值 uint16_t Read_Angle(uint8_t *pBuf, uint16_t buf_len) { if( buf_len < 4){ return 0; } float angle = AngleSensor_GetAngle(); pBuf[0] = ((uint8_t*)(&angle))[0]; pBuf[1] = ((uint8_t*)(&angle))[1]; pBuf[2] = ((uint8_t*)(&angle))[2]; pBuf[3] = ((uint8_t*)(&angle))[3]; return 4; } //读取设备日期时间 uint16_t Read_DateTime(uint8_t *pBuf, uint16_t buf_len) { if( buf_len < 8){ return 0; } RTCx_UpdateTime(); pBuf[0] = g_dateTime.year-2000; pBuf[1] = g_dateTime.month; pBuf[2] = g_dateTime.day; pBuf[3] = g_dateTime.hour; pBuf[4] = g_dateTime.minute; pBuf[5] = g_dateTime.second; pBuf[6] = g_dateTime.week; pBuf[7] = 0x00; return 8; } //读取设备历史记录条数 uint16_t Read_RecordsNum(uint8_t *pBuf, uint16_t buf_len) { if( buf_len < 2){ return 0; } uint16_t num = Storage_GetItemNum(); pBuf[0] = (num>>8)&0xFF; pBuf[1] = num&0xFF; return 2; } //读取历史记录 uint16_t Read_Records(uint8_t num, uint8_t *pBuf, uint16_t buf_len) { if((num > MAX_RECORDS_PERTIME) ||(buf_len < SIZE_RECORD*num)){ return 0; } if(num > Storage_GetItemNum()){ //return 0; num = Storage_GetItemNum(); } for(tmp_i=0; tmp_i < num; tmp_i++){ if(0 == Storage_GetItemFromOldest2(tmp_i, &tmp_record)) { pBuf[tmp_i*SIZE_RECORD+0] = tmp_record.itemtype; pBuf[tmp_i*SIZE_RECORD+1] = tmp_record.eventtype; pBuf[tmp_i*SIZE_RECORD+2] = tmp_record.timestamp[0]; pBuf[tmp_i*SIZE_RECORD+3] = tmp_record.timestamp[1]; pBuf[tmp_i*SIZE_RECORD+4] = tmp_record.timestamp[2]; pBuf[tmp_i*SIZE_RECORD+5] = tmp_record.timestamp[3]; pBuf[tmp_i*SIZE_RECORD+6] = tmp_record.timestamp[4]; pBuf[tmp_i*SIZE_RECORD+7] = tmp_record.timestamp[5]; }else{ break; } } Storage_WaitDelete(tmp_i); return tmp_i*SIZE_RECORD; } /*=======================================================================================*/ uint8_t Write_Addr(uint8_t *pdata, uint8_t len) { if(len == 2){ config->addr = pdata[1]; return RET_OK|RET_NEED_SAVE; }else{ return RET_DATAINVALID; } } uint8_t Write_Baudrate(uint8_t *pdata, uint8_t len) { if(len == 2){ if(pdata[1] >= BaudRate_4800 && pdata[1] <= BaudRate_230400){ config->br_index = pdata[1]; return RET_OK|RET_NEED_SAVE; }else{ return RET_DATAINVALID; } }else{ return RET_DATAINVALID; } } uint8_t Write_HardwareVersion(uint8_t *pdata, uint8_t len) { if(len == 2){ config->hw_version = ((uint16_t)pdata[0]<<8) | pdata[1]; return RET_OK|RET_NEED_SAVE; }else{ return RET_DATAINVALID; } } uint8_t Write_Deviceid(uint8_t *pdata, uint8_t len) { if(len == 4){ config->deviceid = ((uint32_t)pdata[0]<<24) | ((uint32_t)pdata[1]<<16)| ((uint32_t)pdata[2]<<8)| pdata[3]; return RET_OK|RET_NEED_SAVE; }else{ return RET_DATAINVALID; } } uint8_t Write_Devicetype(uint8_t *pdata, uint8_t len) { if(len == 2){ config->devicetype = ((uint16_t)pdata[0]<<8) | pdata[1]; return RET_OK|RET_NEED_SAVE; }else{ return RET_DATAINVALID; } } //开锁、上锁操作 uint8_t Write_LockOp(uint8_t *pdata, uint8_t len) { if(len == 2){ if(0x01 == pdata[0]){ //开锁 Process_OpUnlock(pdata[1]); }else if(0x02 == pdata[0]){ //上锁 Process_OpLock(pdata[1]); }else if(0x03 == pdata[0]){ //正转测试 Motor_Positive(pdata[1]); }else if(0x04 == pdata[0]){ //反转测试 Motor_Negative(pdata[1]); } return RET_OK; }else{ return RET_DATAINVALID; } } //开锁阀值、上锁阀值标定 uint8_t Write_ThresholdCalibration(uint8_t *pdata, uint8_t len) { if(len == 2){ if(0x01 == pdata[1]){ //开锁 Process_CalibrationThreshold(0); }else if(0x02 == pdata[1]){ //上锁 Process_CalibrationThreshold(1); } return RET_OK|RET_NEED_SAVE; }else{ return RET_DATAINVALID; } } //标定设备日期时间 uint8_t Write_DateTime(uint8_t *pdata, uint8_t len) { static DateTime dateTime; if(len == 8){ dateTime.year = pdata[0]; dateTime.year += 2000; dateTime.month = pdata[1]; dateTime.day = pdata[2]; dateTime.hour = pdata[3]; dateTime.minute = pdata[4]; dateTime.second = pdata[5]; dateTime.week = pdata[6]; RTCx_SetTime(&dateTime); return RET_OK; }else{ return RET_DATAINVALID; } } //删除已读取设备历史记录 uint8_t Write_RecordsDelete(uint8_t *pdata, uint8_t len) { if(len == 2){ Storage_DeleteConfirm(); return RET_OK; }else{ return RET_DATAINVALID; } }