| #include "sp_data.h" |
| #include "sp_util.h" |
| #include "sp_flash.h" |
| #include "sp_constant.h" |
| #include "sp_display.h" |
| |
| #define record_behalf_len 19 |
| #define record_below_len 13 |
| |
| //Ñ»·¼Ç¼ÿһ±ÊÁ÷Ë®¼Ç¼µÄµØÖ· |
| static uint16 sp_write_last_record(sp_pos_t* pos) |
| { |
| uint8 crc[2]; |
| uint8 buff[sizeof(sp_last_transdtl_t)]; |
| MEMCLEAR(buff, sizeof(buff)); |
| |
| pos->last_transdtl.this_offset += sizeof(sp_last_transdtl_t); |
| if(pos->last_transdtl.this_offset >= DEF_FLASH_PageSize) |
| { |
| sp_flash_erase(ADDR_LAST_TRANSNO); |
| pos->last_transdtl.this_offset = 0; |
| } |
| sp_protocol_crc((uint8*)&pos->last_transdtl, sizeof(buff)-2, pos->last_transdtl.crc); |
| sp_flash_write(pos->last_transdtl.this_offset+ADDR_LAST_TRANSNO, |
| (uint8*)&pos->last_transdtl, sizeof(buff)); |
| |
| sp_flash_read(pos->last_transdtl.this_offset+ADDR_LAST_TRANSNO, buff, sizeof(buff)); |
| sp_protocol_crc(buff, sizeof(buff)-2, crc); |
| if(MEMCMP(pos->last_transdtl.crc,crc,2)!=0) |
| { |
| pos->load_para_status = RC_FLASH_ERR; |
| return RC_FLASH_ERR; |
| } |
| return 0; |
| } |
| |
| static uint16 sp_write_behalf_record(sp_pos_t* pos, sp_transdtl_t* record) |
| { |
| uint8 buf[record_behalf_len]; |
| MEMCLEAR(buf, sizeof(buf)); |
| |
| pos->last_transdtl.transaddr += sizeof(sp_transdtl_t); |
| if(pos->last_transdtl.transaddr % DEF_FLASH_PageSize == 0) |
| { |
| if(pos->last_transdtl.transaddr >= ADDR_TRANSDTL_END) |
| { |
| pos->last_transdtl.transaddr = ADDR_TRANSDTL_BEGIN; |
| } |
| sp_flash_erase(pos->last_transdtl.transaddr); |
| } |
| sp_flash_write(pos->last_transdtl.transaddr, (uint8*)record, sizeof(buf)); |
| return 0; |
| } |
| |
| static uint16 sp_write_below_record(sp_pos_t* pos, sp_transdtl_t* record) |
| { |
| uint8 crc[2]; |
| uint8 buff[sizeof(sp_transdtl_t)]; |
| MEMCLEAR(crc, sizeof(crc)); |
| MEMCLEAR(buff, sizeof(buff)); |
| |
| sp_protocol_crc((uint8*)record, sizeof(buff)-2, record->crc); |
| sp_flash_write(pos->last_transdtl.transaddr+record_behalf_len, |
| (uint8*)record+record_behalf_len, record_below_len); |
| sp_flash_read(pos->last_transdtl.transaddr, buff, sizeof(buff)); |
| sp_protocol_crc(buff, sizeof(buff)-2, crc); |
| if(MEMCMP(record->crc, crc, 2) != 0) |
| { |
| pos->load_para_status = RC_FLASH_ERR; |
| return RC_FLASH_ERR; |
| } |
| return 0; |
| } |
| |
| //Ïû·Ñ³õʼ»¯ºó´æ´¢Éϰ벿·ÖÁ÷Ë®£¬·ÀÖ¹Ïû·Ñʧ°ÜÎÞÁ÷Ë®ÐÅÏ¢ |
| uint16 sp_prepare_behalf_transdtl(sp_pos_t* pos, sp_card_t* card, sp_transdtl_t* record) |
| { |
| uint8 ctime[6]; |
| MEMCLEAR(ctime, sizeof(ctime)); |
| |
| sp_get_bcdtime(ctime); |
| memcpy(record->devphyid, pos->devphyid, sizeof(pos->devphyid)); |
| memcpy(record->transdate, ctime, 3); |
| memcpy(record->transtime, ctime+3, 3); |
| record->cobillno = card->cobillno; |
| memcpy(record->cardphyid, card->cardphyid, sizeof(card->cardphyid)); |
| if(pos->paymode == PAYMODE_QRCODE) |
| { |
| record->transway = 2; |
| } |
| else |
| { |
| record->transway = 1; |
| } |
| sp_write_behalf_record(pos, record); |
| return sp_write_last_record(pos); |
| } |
| |
| //ÈôÏû·Ñ³É¹¦£¬´æ´¢Ï°벿·ÖÁ÷Ë®ÐÅÏ¢ |
| uint16 sp_prepare_below_transdtl(sp_pos_t* pos, sp_card_t* card, sp_transdtl_t* record) |
| { |
| uint8 buff[record_behalf_len]; |
| MEMCLEAR(buff, sizeof(buff)); |
| |
| record->amount = pos->purchase.paid_sum; |
| if(pos->paymode == PAYMODE_QRCODE) |
| { |
| record->paidAmount = card->qrcode.paidAmount; |
| } |
| else |
| { |
| record->paidAmount = 0; |
| } |
| record->flowsensors = pos->purchase.paid_num; |
| record->transtatus = 1; |
| sp_flash_read(pos->last_transdtl.transaddr, buff, sizeof(buff)); |
| MEMCPY(record, buff, sizeof(buff)); |
| return sp_write_below_record(pos, record); |
| } |
| |
| //Ñ»·´æ´¢Ã¿Ò»±ÊδÉÏ´«Á÷Ë®¼Ç¼µÄµØÖ· |
| uint16 sp_write_unconfirm_record(sp_pos_t* pos) |
| { |
| uint8 crc[2]; |
| uint8 buff[sizeof(sp_unconfirm_transdtl_t)]; |
| MEMCLEAR(buff, sizeof(buff)); |
| |
| pos->unconfirm_transdtl.this_offset += sizeof(buff); |
| if(pos->unconfirm_transdtl.this_offset >= DEF_FLASH_PageSize) |
| { |
| sp_flash_erase(ADDR_UNCONFIRM_TRANSNO); |
| pos->unconfirm_transdtl.this_offset = 0; |
| } |
| sp_protocol_crc((uint8*)&pos->unconfirm_transdtl, sizeof(buff)-2, |
| pos->unconfirm_transdtl.crc); |
| sp_flash_write(pos->unconfirm_transdtl.this_offset+ADDR_UNCONFIRM_TRANSNO, |
| (uint8*)&pos->unconfirm_transdtl, sizeof(buff)); |
| |
| sp_flash_read(pos->unconfirm_transdtl.this_offset+ADDR_UNCONFIRM_TRANSNO, buff, |
| sizeof(buff)); |
| sp_protocol_crc(buff, sizeof(buff)-2, crc); |
| if(MEMCMP(pos->unconfirm_transdtl.crc, crc, 2) !=0) |
| { |
| pos->load_para_status = RC_FLASH_ERR; |
| return RC_FLASH_ERR; |
| } |
| return 0; |
| } |
| |
| static uint16 sp_init_last_transdtl_ptr(sp_last_transdtl_t* record) |
| { |
| record->transaddr = ADDR_TRANSDTL_BEGIN -sizeof(sp_transdtl_t); |
| record->this_offset = 0; |
| sp_protocol_crc((uint8*)record,sizeof(sp_last_transdtl_t) -2,record->crc); |
| return sp_flash_page_write(ADDR_LAST_TRANSNO,(uint8*)record,sizeof(sp_last_transdtl_t)); |
| } |
| |
| static uint16 sp_init_unconfirm_transdtl_ptr(sp_unconfirm_transdtl_t* record) |
| { |
| record->transaddr = ADDR_TRANSDTL_BEGIN; |
| record->this_offset = 0; |
| sp_protocol_crc((uint8*)record,sizeof(sp_unconfirm_transdtl_t) -2,record->crc); |
| return sp_flash_page_write(ADDR_UNCONFIRM_TRANSNO,(uint8*)record, |
| sizeof(sp_unconfirm_transdtl_t)); |
| } |
| |
| static uint8 sp_load_last_transdtl_ptr(sp_pos_t* pos) |
| { |
| uint8 crc[2]; |
| sp_flash_page_read(ADDR_LAST_TRANSNO,(uint8*)&pos->last_transdtl, |
| sizeof(sp_last_transdtl_t)); |
| sp_protocol_crc((uint8*)&pos->last_transdtl,sizeof(sp_last_transdtl_t) -2,crc); |
| if(MEMCMP(pos->last_transdtl.crc,crc,2) != 0) |
| { |
| sp_init_last_transdtl_ptr(&pos->last_transdtl); |
| } |
| return 0; |
| } |
| static uint8 sp_load_unconfirm_transdtl_ptr(sp_pos_t* pos) |
| { |
| uint8 crc[2]; |
| sp_flash_page_read(ADDR_UNCONFIRM_TRANSNO,(uint8*)&pos->unconfirm_transdtl, |
| sizeof(sp_unconfirm_transdtl_t)); |
| sp_protocol_crc((uint8*)&pos->unconfirm_transdtl,sizeof(sp_unconfirm_transdtl_t) -2,crc); |
| if(MEMCMP(pos->unconfirm_transdtl.crc,crc,2) != 0) |
| { |
| sp_init_unconfirm_transdtl_ptr(&pos->unconfirm_transdtl); |
| } |
| return 0; |
| } |
| |
| static uint16 sp_parse_config(sp_pos_t* pos, sp_config_t* config) |
| { |
| pos->sysconf.dev_offline_maxhour = config->offline_work_hour; |
| if(pos->sysconf.dev_offline_maxhour == 0) |
| { |
| pos->sysconf.dev_offline_maxhour = DEV_OFFLINE_DEFAULT_HOUR; |
| } |
| pos->sysconf.flowsensor_unit = config->flowsensor_unit; |
| pos->deviceno = config->deviceno; |
| pos->devlogin.login_flag = config->login_flag; |
| MEMCPY(pos->devphyid,config->devphyid,4); |
| return 0; |
| } |
| static uint16 sp_read_config(sp_pos_t* pos, sp_config_t* config) |
| { |
| uint8 crc[2]; |
| sp_flash_read(ADDR_CONFIG_PARA,(uint8*)config,sizeof(sp_config_t)); |
| sp_protocol_crc((uint8*)config,sizeof(sp_config_t) -2,crc); |
| if(memcmp(config->crc,crc,2) != 0) |
| { |
| return RC_CONFPARA_CRC_ERR; |
| } |
| sp_parse_config(pos, config); |
| return 0; |
| } |
| |
| uint16 sp_clear_transdtl(sp_pos_t* pos) |
| { |
| uint32 i; |
| uint16 ret = sp_flash_erase(ADDR_LAST_TRANSNO); |
| ret |= sp_flash_erase(ADDR_UNCONFIRM_TRANSNO); |
| for(i = ADDR_TRANSDTL_BEGIN; i < ADDR_TRANSDTL_END; i += DEF_FLASH_PageSize) |
| { |
| ret |= sp_flash_erase(i); |
| } |
| return ret; |
| } |
| |
| static uint16 sp_init_device(sp_pos_t* pos) |
| { |
| uint16 ret; |
| disp_hint_info_two(pos,"É豸³õʼ»¯","Çå¿ÕÁ÷Ë®",0); |
| ret |= sp_clear_transdtl(pos); |
| return ret; |
| } |
| |
| void sp_reset_factory(sp_pos_t* pos) |
| { |
| uint16 ret; |
| disp_hint_info_two(pos,"»Ö¸´³ö³§ÖÐ","ÇëÉÔµÈ...",0); |
| ret = sp_flash_erase(ADDR_CONFIG_PARA); |
| ret |=sp_clear_transdtl(pos); |
| if(!ret) |
| { |
| disp_hint_info_two(pos,"³õʼ»¯³É¹¦","µÈ´ýÖØÆô",0); |
| } |
| else |
| { |
| disp_hint_info_two(pos,"³õʼ»¯Ê§°Ü","µÈ´ýÖØÆô",0); |
| } |
| sp_reset(); |
| } |
| |
| uint16 sp_load_config(sp_pos_t* pos) |
| { |
| uint16 ret; |
| sp_config_t config; |
| MEMCLEAR(&config,sizeof config); |
| ret |= sp_read_config(pos, &config); |
| if(ret) |
| { |
| sp_config_init(pos); |
| sp_init_device(pos); |
| } |
| ret |= sp_load_last_transdtl_ptr(pos); |
| ret |= sp_load_unconfirm_transdtl_ptr(pos); |
| return ret; |
| } |
| |
| uint16 sp_save_config(sp_pos_t* pos, sp_config_t* config) |
| { |
| uint8 buffer[sizeof(sp_config_t)]; |
| uint8 crc[2]; |
| sp_protocol_crc((uint8*)config,sizeof(sp_config_t)-2,config->crc); |
| sp_flash_erase(ADDR_CONFIG_PARA); |
| sp_flash_write(ADDR_CONFIG_PARA,(uint8*)config,sizeof(sp_config_t)); |
| sp_flash_read(ADDR_CONFIG_PARA,buffer,sizeof buffer); |
| sp_protocol_crc(buffer,sizeof(buffer)-2,crc); |
| if(memcmp(config->crc,crc,2) != 0) |
| { |
| return RC_CONFPARA_CRC_ERR; |
| } |
| sp_parse_config(pos, config); |
| return 0; |
| } |
| |
| uint16 sp_save_deviceno(sp_pos_t* pos, uint8 deviceno) |
| { |
| uint16 ret; |
| sp_config_t config; |
| MEMCLEAR(&config, sizeof config); |
| ret = sp_read_config(pos, &config); |
| if(ret) |
| { |
| return ret; |
| } |
| config.deviceno = deviceno; |
| return sp_save_config(pos, &config); |
| } |
| uint16 sp_save_devphyid(sp_pos_t* pos, uint8 devphyid[4]) |
| { |
| uint16 ret; |
| sp_config_t config; |
| MEMCLEAR(&config, sizeof config); |
| ret = sp_read_config(pos, &config); |
| if(ret) |
| { |
| return ret; |
| } |
| MEMCPY(config.devphyid,devphyid,4); |
| return sp_save_config(pos, &config); |
| } |
| uint16 sp_save_login_info(sp_pos_t* pos, uint8 flag, uint8 unit, uint8 offline_maxhour) |
| { |
| uint16 ret; |
| sp_config_t config; |
| MEMCLEAR(&config, sizeof config); |
| ret = sp_read_config(pos, &config); |
| if(ret) |
| { |
| return ret; |
| } |
| config.login_flag = flag; |
| config.flowsensor_unit = unit; |
| config.offline_work_hour = offline_maxhour; |
| return sp_save_config(pos, &config); |
| } |
| |
| uint16 sp_save_heartbeat_info(sp_pos_t* pos, uint8 flag) |
| { |
| uint16 ret; |
| sp_config_t config; |
| MEMCLEAR(&config, sizeof config); |
| ret = sp_read_config(pos, &config); |
| if(ret) |
| { |
| return ret; |
| } |
| config.login_flag = flag; |
| return sp_save_config(pos, &config); |
| |
| } |
| |
| uint16 sp_config_init(sp_pos_t* pos) |
| { |
| uint8 crc[2]; |
| sp_config_t config; |
| disp_hint_info_two(pos,"É豸²ÎÊý³õʼ»¯", "ÇëÉÔµÈ...",0); |
| MEMCLEAR(&config, sizeof(config)); |
| config.deviceno = 1; |
| MEMCPY(config.devphyid,"\x40\x12\x01\x00",4); |
| sp_protocol_crc((uint8*)&config,sizeof(sp_config_t)-2,config.crc); |
| sp_flash_erase(ADDR_CONFIG_PARA); |
| sp_flash_write(ADDR_CONFIG_PARA,(uint8*)&config,sizeof(sp_config_t)); |
| sp_flash_read(ADDR_CONFIG_PARA,(uint8*)&config,sizeof(config)); |
| sp_protocol_crc((uint8*)&config,sizeof(sp_config_t)-2,crc); |
| if(memcmp(config.crc,crc,2) != 0) |
| { |
| return RC_CONFPARA_CRC_ERR; |
| } |
| return sp_parse_config(pos,&config); |
| } |