zongqiang.zhang | 0c6a088 | 2019-08-07 14:48:21 +0800 | [diff] [blame] | 1 | #include "sp_data.h" |
| 2 | #include "sp_util.h" |
| 3 | #include "sp_flash.h" |
| 4 | #include "sp_constant.h" |
| 5 | #include "sp_display.h" |
| 6 | |
| 7 | #define record_behalf_len 25 |
| 8 | #define record_below_len 7 |
| 9 | |
| 10 | //Ñ»·¼Ç¼ÿһ±ÊÁ÷Ë®¼Ç¼µÄµØÖ· |
| 11 | static uint16 sp_write_last_record(sp_pos_t* pos) |
| 12 | { |
| 13 | uint8 crc[2]; |
| 14 | uint8 buff[sizeof(sp_last_transdtl_t)]; |
| 15 | MEMCLEAR(buff, sizeof(buff)); |
| 16 | |
| 17 | pos->last_transdtl.this_offset += sizeof(sp_last_transdtl_t); |
| 18 | if(pos->last_transdtl.this_offset >= DEF_FLASH_PageSize) |
| 19 | { |
| 20 | sp_flash_erase(ADDR_LAST_TRANSNO); |
| 21 | pos->last_transdtl.this_offset = 0; |
| 22 | } |
| 23 | sp_protocol_crc((uint8*)&pos->last_transdtl, sizeof(buff)-2, pos->last_transdtl.crc); |
| 24 | sp_flash_write(pos->last_transdtl.this_offset+ADDR_LAST_TRANSNO, |
| 25 | (uint8*)&pos->last_transdtl, sizeof(buff)); |
| 26 | |
| 27 | sp_flash_read(pos->last_transdtl.this_offset+ADDR_LAST_TRANSNO, buff, sizeof(buff)); |
| 28 | sp_protocol_crc(buff, sizeof(buff)-2, crc); |
| 29 | if(MEMCMP(pos->last_transdtl.crc,crc,2)!=0) |
| 30 | { |
| 31 | pos->load_para_status = RC_FLASH_ERR; |
| 32 | return RC_FLASH_ERR; |
| 33 | } |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | static uint16 sp_write_behalf_record(sp_pos_t* pos, sp_transdtl_t* record) |
| 38 | { |
| 39 | uint8 buf[record_behalf_len]; |
| 40 | MEMCLEAR(buf, sizeof(buf)); |
| 41 | |
| 42 | pos->last_transdtl.transaddr += sizeof(sp_transdtl_t); |
| 43 | if(pos->last_transdtl.transaddr % DEF_FLASH_PageSize == 0) |
| 44 | { |
| 45 | if(pos->last_transdtl.transaddr >= ADDR_TRANSDTL_END) |
| 46 | { |
| 47 | pos->last_transdtl.transaddr = ADDR_TRANSDTL_BEGIN; |
| 48 | } |
| 49 | sp_flash_erase(pos->last_transdtl.transaddr); |
| 50 | } |
| 51 | sp_flash_write(pos->last_transdtl.transaddr, (uint8*)record, sizeof(buf)); |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static uint16 sp_write_below_record(sp_pos_t* pos, sp_transdtl_t* record) |
| 56 | { |
| 57 | uint8 crc[2]; |
| 58 | uint8 buff[sizeof(sp_transdtl_t)]; |
| 59 | MEMCLEAR(crc, sizeof(crc)); |
| 60 | MEMCLEAR(buff, sizeof(buff)); |
| 61 | |
| 62 | sp_protocol_crc((uint8*)record, sizeof(buff)-2, record->crc); |
| 63 | sp_flash_write(pos->last_transdtl.transaddr+record_behalf_len, |
| 64 | (uint8*)record+record_behalf_len, record_below_len); |
| 65 | sp_flash_read(pos->last_transdtl.transaddr, buff, sizeof(buff)); |
| 66 | sp_protocol_crc(buff, sizeof(buff)-2, crc); |
| 67 | if(MEMCMP(record->crc, crc, 2) != 0) |
| 68 | { |
| 69 | pos->load_para_status = RC_FLASH_ERR; |
| 70 | return RC_FLASH_ERR; |
| 71 | } |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | //Ïû·Ñ³õʼ»¯ºó´æ´¢Éϰ벿·ÖÁ÷Ë®£¬·ÀÖ¹Ïû·Ñʧ°ÜÎÞÁ÷Ë®ÐÅÏ¢ |
| 76 | uint16 sp_prepare_behalf_transdtl(sp_pos_t* pos, sp_card_t* card, sp_transdtl_t* record) |
| 77 | { |
| 78 | uint8 ret = 0; |
| 79 | uint8 ctime[6]; |
| 80 | MEMCLEAR(ctime, sizeof(ctime)); |
| 81 | |
| 82 | sp_get_bcdtime(ctime); |
| 83 | memcpy(record->devphyid, pos->devphyid, sizeof(pos->devphyid)); |
| 84 | memcpy(record->transdate, ctime, 3); |
| 85 | memcpy(record->transtime, ctime+3, 3); |
| 86 | memcpy(record->billno, card->billno, sizeof(card->billno)); |
| 87 | memcpy(record->cardphyid, card->cardphyid, sizeof(card->cardphyid)); |
| 88 | if(pos->paymode == PAYMODE_QRCODE) |
| 89 | { |
| 90 | record->transway = 2; |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | record->transway = 1; |
| 95 | } |
| 96 | ret = sp_write_behalf_record(pos, record); |
| 97 | if(ret) |
| 98 | return ret; |
| 99 | return sp_write_last_record(pos); |
| 100 | } |
| 101 | |
| 102 | //ÈôÏû·Ñ³É¹¦£¬´æ´¢Ï°벿·ÖÁ÷Ë®ÐÅÏ¢ |
| 103 | uint16 sp_prepare_below_transdtl(sp_pos_t* pos, sp_card_t* card, sp_transdtl_t* record) |
| 104 | { |
| 105 | uint8 buff[record_behalf_len]; |
| 106 | MEMCLEAR(buff, sizeof(buff)); |
| 107 | |
| 108 | record->amount = pos->purchase.paid_sum; |
| 109 | if(pos->paymode == PAYMODE_QRCODE) |
| 110 | { |
| 111 | record->paidAmount = card->qrcode.paidAmount; |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | record->paidAmount = 0; |
| 116 | } |
| 117 | record->flowsensors = pos->purchase.paid_num; |
| 118 | record->transtatus = 1; |
| 119 | sp_flash_read(pos->last_transdtl.transaddr, buff, sizeof(buff)); |
| 120 | MEMCPY(record, buff, sizeof(buff)); |
| 121 | return sp_write_below_record(pos, record); |
| 122 | } |
| 123 | |
| 124 | //Ñ»·´æ´¢Ã¿Ò»±ÊδÉÏ´«Á÷Ë®¼Ç¼µÄµØÖ· |
| 125 | uint16 sp_write_unconfirm_record(sp_pos_t* pos) |
| 126 | { |
| 127 | uint8 crc[2]; |
| 128 | uint8 buff[sizeof(sp_unconfirm_transdtl_t)]; |
| 129 | MEMCLEAR(buff, sizeof(buff)); |
| 130 | |
| 131 | pos->unconfirm_transdtl.this_offset += sizeof(buff); |
| 132 | if(pos->unconfirm_transdtl.this_offset >= DEF_FLASH_PageSize) |
| 133 | { |
| 134 | sp_flash_erase(ADDR_UNCONFIRM_TRANSNO); |
| 135 | pos->unconfirm_transdtl.this_offset = 0; |
| 136 | } |
| 137 | sp_protocol_crc((uint8*)&pos->unconfirm_transdtl, sizeof(buff)-2, |
| 138 | pos->unconfirm_transdtl.crc); |
| 139 | sp_flash_write(pos->unconfirm_transdtl.this_offset+ADDR_UNCONFIRM_TRANSNO, |
| 140 | (uint8*)&pos->unconfirm_transdtl, sizeof(buff)); |
| 141 | |
| 142 | sp_flash_read(pos->unconfirm_transdtl.this_offset+ADDR_UNCONFIRM_TRANSNO, buff, |
| 143 | sizeof(buff)); |
| 144 | sp_protocol_crc(buff, sizeof(buff)-2, crc); |
| 145 | if(MEMCMP(pos->unconfirm_transdtl.crc, crc, 2) !=0) |
| 146 | { |
| 147 | pos->load_para_status = RC_FLASH_ERR; |
| 148 | return RC_FLASH_ERR; |
| 149 | } |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static uint16 sp_init_last_transdtl_ptr(sp_last_transdtl_t* record) |
| 154 | { |
| 155 | record->transaddr = ADDR_TRANSDTL_BEGIN -sizeof(sp_transdtl_t); |
| 156 | record->this_offset = 0; |
| 157 | sp_protocol_crc((uint8*)record,sizeof(sp_last_transdtl_t) -2,record->crc); |
| 158 | return sp_flash_page_write(ADDR_LAST_TRANSNO,(uint8*)record,sizeof(sp_last_transdtl_t)); |
| 159 | } |
| 160 | |
| 161 | static uint16 sp_init_unconfirm_transdtl_ptr(sp_unconfirm_transdtl_t* record) |
| 162 | { |
| 163 | record->transaddr = ADDR_TRANSDTL_BEGIN; |
| 164 | record->this_offset = 0; |
| 165 | sp_protocol_crc((uint8*)record,sizeof(sp_unconfirm_transdtl_t) -2,record->crc); |
| 166 | return sp_flash_page_write(ADDR_UNCONFIRM_TRANSNO,(uint8*)record, |
| 167 | sizeof(sp_unconfirm_transdtl_t)); |
| 168 | } |
| 169 | |
| 170 | static uint8 sp_load_last_transdtl_ptr(sp_pos_t* pos) |
| 171 | { |
| 172 | uint8 crc[2]; |
| 173 | sp_flash_page_read(ADDR_LAST_TRANSNO,(uint8*)&pos->last_transdtl, |
| 174 | sizeof(sp_last_transdtl_t)); |
| 175 | sp_protocol_crc((uint8*)&pos->last_transdtl,sizeof(sp_last_transdtl_t) -2,crc); |
| 176 | if(MEMCMP(pos->last_transdtl.crc,crc,2) != 0) |
| 177 | { |
| 178 | sp_init_last_transdtl_ptr(&pos->last_transdtl); |
| 179 | } |
| 180 | return 0; |
| 181 | } |
| 182 | static uint8 sp_load_unconfirm_transdtl_ptr(sp_pos_t* pos) |
| 183 | { |
| 184 | uint8 crc[2]; |
| 185 | sp_flash_page_read(ADDR_UNCONFIRM_TRANSNO,(uint8*)&pos->unconfirm_transdtl, |
| 186 | sizeof(sp_unconfirm_transdtl_t)); |
| 187 | sp_protocol_crc((uint8*)&pos->unconfirm_transdtl,sizeof(sp_unconfirm_transdtl_t) -2,crc); |
| 188 | if(MEMCMP(pos->unconfirm_transdtl.crc,crc,2) != 0) |
| 189 | { |
| 190 | sp_init_unconfirm_transdtl_ptr(&pos->unconfirm_transdtl); |
| 191 | } |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static uint16 sp_parse_config(sp_pos_t* pos, sp_config_t* config) |
| 196 | { |
| 197 | pos->sysconf.dev_offline_maxhour = config->offline_work_hour; |
| 198 | if(pos->sysconf.dev_offline_maxhour == 0) |
| 199 | { |
| 200 | pos->sysconf.dev_offline_maxhour = DEV_OFFLINE_DEFAULT_HOUR; |
| 201 | } |
| 202 | pos->sysconf.flowsensor_unit = config->flowsensor_unit; |
| 203 | pos->deviceno = config->deviceno; |
| 204 | pos->devlogin.login_flag = config->login_flag; |
| 205 | MEMCPY(pos->devphyid,config->devphyid,4); |
| 206 | return 0; |
| 207 | } |
| 208 | static uint16 sp_read_config(sp_pos_t* pos, sp_config_t* config) |
| 209 | { |
| 210 | uint8 crc[2]; |
| 211 | sp_flash_read(ADDR_CONFIG_PARA,(uint8*)config,sizeof(sp_config_t)); |
| 212 | sp_protocol_crc((uint8*)config,sizeof(sp_config_t) -2,crc); |
| 213 | if(memcmp(config->crc,crc,2) != 0) |
| 214 | { |
| 215 | return RC_CONFPARA_CRC_ERR; |
| 216 | } |
| 217 | sp_parse_config(pos, config); |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | uint16 sp_clear_transdtl(sp_pos_t* pos) |
| 222 | { |
| 223 | uint32 i; |
| 224 | uint16 ret = sp_flash_erase(ADDR_LAST_TRANSNO); |
| 225 | ret |= sp_flash_erase(ADDR_UNCONFIRM_TRANSNO); |
| 226 | for(i = ADDR_TRANSDTL_BEGIN; i < ADDR_TRANSDTL_END; i += DEF_FLASH_PageSize) |
| 227 | { |
| 228 | ret |= sp_flash_erase(i); |
| 229 | } |
| 230 | return ret; |
| 231 | } |
| 232 | |
| 233 | static uint16 sp_init_device(sp_pos_t* pos) |
| 234 | { |
| 235 | uint16 ret; |
| 236 | disp_hint_info_two(pos,"É豸³õʼ»¯","Çå¿ÕÁ÷Ë®",0); |
| 237 | ret |= sp_clear_transdtl(pos); |
| 238 | return ret; |
| 239 | } |
| 240 | |
| 241 | void sp_reset_factory(sp_pos_t* pos) |
| 242 | { |
| 243 | uint16 ret; |
| 244 | disp_hint_info_two(pos,"»Ö¸´³ö³§ÖÐ","ÇëÉÔµÈ...",0); |
| 245 | ret = sp_flash_erase(ADDR_CONFIG_PARA); |
| 246 | ret |=sp_clear_transdtl(pos); |
| 247 | if(!ret) |
| 248 | { |
| 249 | disp_hint_info_two(pos,"³õʼ»¯³É¹¦","µÈ´ýÖØÆô",0); |
| 250 | } |
| 251 | else |
| 252 | { |
| 253 | disp_hint_info_two(pos,"³õʼ»¯Ê§°Ü","µÈ´ýÖØÆô",0); |
| 254 | } |
| 255 | sp_reset(); |
| 256 | } |
| 257 | |
| 258 | uint16 sp_load_config(sp_pos_t* pos) |
| 259 | { |
| 260 | uint16 ret; |
| 261 | sp_config_t config; |
| 262 | MEMCLEAR(&config,sizeof config); |
| 263 | ret |= sp_read_config(pos, &config); |
| 264 | if(ret) |
| 265 | { |
| 266 | sp_config_init(pos); |
| 267 | sp_init_device(pos); |
| 268 | } |
| 269 | ret |= sp_load_last_transdtl_ptr(pos); |
| 270 | ret |= sp_load_unconfirm_transdtl_ptr(pos); |
| 271 | return ret; |
| 272 | } |
| 273 | |
| 274 | uint16 sp_save_config(sp_pos_t* pos, sp_config_t* config) |
| 275 | { |
| 276 | uint8 buffer[sizeof(sp_config_t)]; |
| 277 | uint8 crc[2]; |
| 278 | sp_protocol_crc((uint8*)config,sizeof(sp_config_t)-2,config->crc); |
| 279 | sp_flash_erase(ADDR_CONFIG_PARA); |
| 280 | sp_flash_write(ADDR_CONFIG_PARA,(uint8*)config,sizeof(sp_config_t)); |
| 281 | sp_flash_read(ADDR_CONFIG_PARA,buffer,sizeof buffer); |
| 282 | sp_protocol_crc(buffer,sizeof(buffer)-2,crc); |
| 283 | if(memcmp(config->crc,crc,2) != 0) |
| 284 | { |
| 285 | return RC_CONFPARA_CRC_ERR; |
| 286 | } |
| 287 | sp_parse_config(pos, config); |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | uint16 sp_save_deviceno(sp_pos_t* pos, uint8 deviceno) |
| 292 | { |
| 293 | uint16 ret; |
| 294 | sp_config_t config; |
| 295 | MEMCLEAR(&config, sizeof config); |
| 296 | ret = sp_read_config(pos, &config); |
| 297 | if(ret) |
| 298 | { |
| 299 | return ret; |
| 300 | } |
| 301 | config.deviceno = deviceno; |
| 302 | return sp_save_config(pos, &config); |
| 303 | } |
| 304 | uint16 sp_save_devphyid(sp_pos_t* pos, uint8 devphyid[4]) |
| 305 | { |
| 306 | uint16 ret; |
| 307 | sp_config_t config; |
| 308 | MEMCLEAR(&config, sizeof config); |
| 309 | ret = sp_read_config(pos, &config); |
| 310 | if(ret) |
| 311 | { |
| 312 | return ret; |
| 313 | } |
| 314 | MEMCPY(config.devphyid,devphyid,4); |
| 315 | return sp_save_config(pos, &config); |
| 316 | } |
| 317 | uint16 sp_save_login_info(sp_pos_t* pos, uint8 flag, uint8 unit, uint8 offline_maxhour) |
| 318 | { |
| 319 | uint16 ret; |
| 320 | sp_config_t config; |
| 321 | MEMCLEAR(&config, sizeof config); |
| 322 | ret = sp_read_config(pos, &config); |
| 323 | if(ret) |
| 324 | { |
| 325 | return ret; |
| 326 | } |
| 327 | config.login_flag = flag; |
| 328 | config.flowsensor_unit = unit; |
| 329 | config.offline_work_hour = offline_maxhour; |
| 330 | return sp_save_config(pos, &config); |
| 331 | } |
| 332 | |
| 333 | uint16 sp_save_heartbeat_info(sp_pos_t* pos, uint8 flag) |
| 334 | { |
| 335 | uint16 ret; |
| 336 | sp_config_t config; |
| 337 | MEMCLEAR(&config, sizeof config); |
| 338 | ret = sp_read_config(pos, &config); |
| 339 | if(ret) |
| 340 | { |
| 341 | return ret; |
| 342 | } |
| 343 | config.login_flag = flag; |
| 344 | return sp_save_config(pos, &config); |
| 345 | |
| 346 | } |
| 347 | |
| 348 | uint16 sp_config_init(sp_pos_t* pos) |
| 349 | { |
| 350 | uint16 ret = 0; |
| 351 | uint8 crc[2]; |
| 352 | sp_config_t config; |
| 353 | MEMCLEAR(&config, sizeof(config)); |
| 354 | disp_hint_info(pos,"É豸²ÎÊý³õʼ»¯", DELAY_TIME2s); |
| 355 | sp_protocol_crc((uint8*)&config,sizeof(sp_config_t)-2,config.crc); |
| 356 | sp_flash_erase(ADDR_CONFIG_PARA); |
| 357 | sp_flash_write(ADDR_CONFIG_PARA,(uint8*)&config,sizeof(sp_config_t)); |
| 358 | sp_flash_read(ADDR_CONFIG_PARA,(uint8*)&config,sizeof(config)); |
| 359 | sp_protocol_crc((uint8*)&config,sizeof(sp_config_t)-2,crc); |
| 360 | if(memcmp(config.crc,crc,2) != 0) |
| 361 | { |
| 362 | return RC_CONFPARA_CRC_ERR; |
| 363 | } |
| 364 | ret = sp_flash_erase(ADDR_CONFIG_PARA); |
| 365 | return ret; |
| 366 | } |