#include "app_common.h" #include "../clib/md5.h" #include "../msp/uart0.h" #include "../msp/uart2.h" #include "../app/app_task_gm35.h" #include "../app/app_paraid.h" #include "../app/app_task_relay.h" #include "../app/app_task_wdt.h" //0xFF+0xFE+2+20+1+21 `0xFF``0xFE`00``2A``simno,salt #define LEN_GPRS_TCP_PKG_AT_SIGNIN 46 #define LEN_GPRS_TCP_DATA_ACK 8 vU8 CCID[5] = {0xFF,0xFE,0x00,0x2A,0x38}; struct printer_maintenance_global G; TS_PH3_ccmodbus_ack ccmodbus_ack; TS_PH3_ccmodbus_ack ccmodbus_ack_Ascii; U8 ccmodbus_len = 0; vU8 gprsTcpPkg_Signin[LEN_GPRS_TCP_PKG_AT_SIGNIN] = {0xFF,0xFE,0x0,0x2A}; vU8 gprsTcpPkg_SigninAscii[LEN_GPRS_TCP_PKG_AT_SIGNIN * 2]; vU8 CCID_ascii[40] = {0}; vU8 gprsdataack[LEN_GPRS_TCP_DATA_ACK] = {0xFF,0xFE,0x0,0x4,0x11,0x95,0x8F,0xCD}; vU8 gprsdataack_Ascii[LEN_GPRS_TCP_DATA_ACK * 2]; TS_PH3_ccmodbus_ack g_pkg_report; TS_Paper_Upper_calib_ack g_upper_calib_ack; TS_Paper_Count_Ack g_paper_count_ack; void L2_register_init() { U8 i = 0; for(i = 0; i < PAPER_BOX_NUM; i++) { G.paperCalibs[i].num0 = 0; G.paperCalibs[i].angle0 = 0x23F0; G.paperCalibs[i].num1 = 400; G.paperCalibs[i].angle1 = 0x20C4; G.currentSensorVal[i] = 0; G.papers[i].version = APP_VERSION_HEX; G.papers[i].LowPower = 0x00; G.papers[i].cameraNotWorking = 0x00; G.papers[i].cameraIndex = 0x00; G.papers[i].paperNum = 0x00; } G.boardstatus.a9RecvDataInterval = 0; G.boardstatus.a9Status = 0; G.boardstatus.wdtStatus = 0; G.lastResetToISPCmdTimeInSeconds = 0; //G.irthreshold[0] = 7; //G.irthreshold[1] = 5; G.runmode = MODE_RUN; //G.runmode = DEBUG_IR; //G.runmode = DEBUG_A9; //G.runmode = DEBUG_CAMER; G.reset = 0; G.md_slaver_info[0].slaver = SLAVER_ASGA03_01; G.md_slaver_info[0].oper = MODBUS_OPER_READ; G.md_slaver_info[0].reg = 0x04; G.md_slaver_info[0].regnum = 0x03; G.md_slaver_info[0].mask = 2; G.md_slaver_info[1].slaver = SLAVER_ASGA03_02; G.md_slaver_info[1].oper = MODBUS_OPER_READ; G.md_slaver_info[1].reg = 0x04; G.md_slaver_info[1].regnum = 0x03; G.md_slaver_info[1].mask = 2; G.modbusstmp = 0; } U16 L2_paper_count(U8 index, U32 angle) { S32 x1,y1,x2,y2,x,paper; //y = (x-x1) * (y2-y1) / (x2-x1) + y1 x1 = G.paperCalibs[index].angle0; y1 = G.paperCalibs[index].num0; x2 = G.paperCalibs[index].angle1; y2 = G.paperCalibs[index].num1; x = angle; paper = (S16)(1.0 * (x-x1) * (y2-y1) / (x2-x1) + y1); if(paper > 0x1FF) { paper = 0x1FF; } if(paper < 0) { paper = ~paper + 1; paper |= 1<<9; } else { paper &= ~(1<<9); } paper &= 0x3FF; return paper; } static void resetToIsp() { if(s_nos_tick.t_1s < G.lastResetToISPCmdTimeInSeconds) { //isp IAP_CONTR = 0x60; //disable wdt } else { G.lastResetToISPCmdTimeInSeconds = s_nos_tick.t_1s + 2; //2s(1s-2s) } } U8 L2_constructor_ccmodbus_ack(TS_PH3_ccmodbus_ack *pccmodbus, U8 addr, U8 oper, U8 *buf, U8 len) { pccmodbus->filter1 = 0xFF; pccmodbus->filter2 = 0xFE; pccmodbus->num0 = (len + 4) >> 8 & 0xFF; pccmodbus->num1 = (len + 4) >> 0 & 0xFF; pccmodbus->addr = addr; pccmodbus->oper = oper; byte_copy_uc((U8*)&pccmodbus->buf, buf, len); crc16(pccmodbus->crc, &pccmodbus->addr, len + 2); pccmodbus->buf[len] = pccmodbus->crc[1]; pccmodbus->buf[len+1] = pccmodbus->crc[0]; return len + 6 + 2; } int L2_ph3_common_send(U8 dst, void *buf, U8 len) { switch(dst) { case ADDR_UPPER: L0_uart0_sendArray(buf,len); break; case ADDR_SERVER: L3_gm35_send_data(buf, len); //L0_uart0_uc('S'); break; default: break; } return 0; } #if 1 U8 ascTable[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; #if 0 U8 *hexBytes2AsciiBytes(U8 asciiBytes[],U8 hexBytes[],U8 hexBytesLen) { U8 i = 0; L0_uart0_uchex(hexBytesLen); for(i=0;i> 4 & 0x0F]; asciiBytes[i*2 + 1] = ascTable[hexBytes[i] >> 0 & 0x0F]; //L0_uart0_uchex(i); //L0_uart0_uc(asciiBytes[i*2]); //L0_uart0_uc(asciiBytes[i*2+1]); } return asciiBytes; } #endif U8 *hexBytes2AsciiBytes(U8 *asciiBytes,U8 *hexBytes,U8 hexBytesLen) { U8 i = 0; L0_uart0_uchex(hexBytesLen); for(i=0;i> 4 & 0x0F]; asciiBytes[i*2 + 1] = ascTable[hexBytes[i] >> 0 & 0x0F]; //L0_uart0_uchex(i); //L0_uart0_uc(asciiBytes[i*2]); //L0_uart0_uc(asciiBytes[i*2+1]); } return asciiBytes; } void L2_gprs_send_signin_pkg(void) { libc_memcpy(gprsTcpPkg_Signin+4,G.ccid,20); gprsTcpPkg_Signin[24] = ','; libc_memcpy(gprsTcpPkg_Signin+25,G.ccid_passwd,21); //L0_uart0_sendArray(gprsTcpPkg_Signin,46); hexBytes2AsciiBytes(gprsTcpPkg_SigninAscii,gprsTcpPkg_Signin,LEN_GPRS_TCP_PKG_AT_SIGNIN); //L0_uart0_0d0a(); //L0_uart0_sendArray(gprsTcpPkg_SigninAscii,46*2); L2_ph3_common_send(ADDR_SERVER,gprsTcpPkg_SigninAscii,LEN_GPRS_TCP_PKG_AT_SIGNIN * 2); } #endif void L2_gprs_send_ack_pkg(void) { hexBytes2AsciiBytes((U8 *)&gprsdataack_Ascii,(U8 *)&gprsdataack,LEN_GPRS_TCP_DATA_ACK); L2_ph3_common_send(ADDR_SERVER,(U8 *)&gprsdataack_Ascii,LEN_GPRS_TCP_DATA_ACK * 2); } void L2_gprs_send_heart_pkg(void) { TS_gprs_heart gprs_heart; gprs_heart.version = APP_VERSION_HEX; ccmodbus_len = L2_constructor_ccmodbus_ack(&ccmodbus_ack, ADDR_SERVER, OPER_GPRS_HEART, (U8 *)&gprs_heart, sizeof(gprs_heart)); hexBytes2AsciiBytes((U8 *)&ccmodbus_ack_Ascii,(U8 *)&ccmodbus_ack,ccmodbus_len); L2_ph3_common_send(ADDR_SERVER,(U8 *)&ccmodbus_ack_Ascii,ccmodbus_len * 2); //L2_ph3_common_send(ADDR_SERVER,(U8 *)&ccmodbus_ack,ccmodbus_len); } void L2_gprs_send_report_pkg(void) { TS_Printer_Report report_ack; byte_copy_uc((U8*)&report_ack.papers[0],(U8*)&G.papers[0],3); byte_copy_uc((U8*)&report_ack.papers[1],(U8*)&G.papers[1],3); byte_copy_uc((U8*)&report_ack.boardstatus,(U8*)&G.boardstatus,3); report_ack.ir = 0; ccmodbus_len = L2_constructor_ccmodbus_ack(&ccmodbus_ack, ADDR_SERVER, OPER_GPRS_REPORT, (U8 *)&report_ack, sizeof(report_ack)); hexBytes2AsciiBytes((U8 *)&ccmodbus_ack_Ascii,(U8 *)&ccmodbus_ack,ccmodbus_len); L2_ph3_common_send(ADDR_SERVER,(U8 *)&ccmodbus_ack_Ascii,ccmodbus_len * 2); } #if 0 void L2_gprs_send_report_pkg(void) { TS_Printer_Report report_ack; byte_copy_uc((U8*)&report_ack.papers[0],(U8*)&G.papers[0],3); byte_copy_uc((U8*)&report_ack.papers[1],(U8*)&G.papers[1],3); byte_copy_uc((U8*)&report_ack.boardstatus,(U8*)&G.boardstatus,3); report_ack.ir = 0; ccmodbus_len = L2_constructor_ccmodbus_ack(&ccmodbus_ack, ADDR_SERVER, OPER_GPRS_REPORT, (U8 *)&report_ack, sizeof(report_ack)); L2_ph3_common_send(ADDR_SERVER,(U8 *)&ccmodbus_ack,ccmodbus_len); } #endif void L2_do_angle_setid(U8 *buf) { TS_Angle_SetId *ts_angle_setid = (TS_Angle_SetId*)buf; TS_PH1A_DAT ph1a; ph1a.filter = D_HETU_FXA; ph1a.id = SLAVER_ID_N; ph1a.oper = OPER_ANGLE_SET_ID; ph1a.xaxis[0] = ts_angle_setid->id; ph1a.xaxis[1] = ts_angle_setid->id; ph1a.yaxis[0] = ts_angle_setid->id; ph1a.yaxis[1] = ts_angle_setid->id; ph1a.zaxis[0] = ts_angle_setid->id; ph1a.zaxis[1] = ts_angle_setid->id; L0_uart2_sendArray(&ph1a,sizeof(TS_PH1A_DAT)); } void L2_do_angle_setid_ack(U8 id) { TS_Angle_SetId ts_setid_ack; ts_setid_ack.id = id; ccmodbus_len = L2_constructor_ccmodbus_ack(&ccmodbus_ack, ADDR_UPPER, OPER_SET_485_ID, (U8 *)&ts_setid_ack, sizeof(ts_setid_ack)); L0_uart0_sendArray((U8 *)&ccmodbus_ack,ccmodbus_len); } void L2_do_paper_count(U8 *buf) { TS_Paper_Count *ts_paper = (TS_Paper_Count*)buf; U16 num = 0; if(ts_paper->no >= PAPER_BOX_NUM) { L0_uart0_uc('x'); return; } num = L2_paper_count(ts_paper->no,G.currentSensorVal[ts_paper->no]); g_paper_count_ack.no = ts_paper->no; g_paper_count_ack.num[0] = num >> 8 & 0xFF; g_paper_count_ack.num[1] = num >> 0 & 0xFF; ccmodbus_len = L2_constructor_ccmodbus_ack(&ccmodbus_ack, ADDR_UPPER, OPER_PAPER_COUNT, (U8 *)&g_paper_count_ack, sizeof(g_paper_count_ack)); L0_uart0_sendArray((U8 *)&ccmodbus_ack,ccmodbus_len); } void L2_do_calib_read(U8 *buf) { TS_Paper_Upper_Calib_Read *ts_calib = (TS_Paper_Upper_Calib_Read*)buf; if(ts_calib->no >= PAPER_BOX_NUM) { L0_uart0_uc('x'); return; } g_upper_calib_ack.num0 = G.paperCalibs[ts_calib->no].num0; g_upper_calib_ack.angle0 = G.paperCalibs[ts_calib->no].angle0; g_upper_calib_ack.num1 = G.paperCalibs[ts_calib->no].num1; g_upper_calib_ack.angle1 = G.paperCalibs[ts_calib->no].angle1; ccmodbus_len = L2_constructor_ccmodbus_ack(&ccmodbus_ack, ADDR_UPPER, OPER_CALIB_READ, (U8 *)&g_upper_calib_ack, sizeof(g_upper_calib_ack)); L0_uart0_sendArray((U8 *)&ccmodbus_ack,ccmodbus_len); } void L2_do_calib(U8 *buf) { TS_Paper_Upper_Calib *ts_calib = (TS_Paper_Upper_Calib*)buf; if(ts_calib->no >= PAPER_BOX_NUM || ts_calib->point >= 2) { L0_uart0_uc('x'); return; } switch(ts_calib->point) { case 0: { G.paperCalibs[ts_calib->no].num0 = (U16)(ts_calib->paperNum0) << 8 | ts_calib->paperNum1; G.paperCalibs[ts_calib->no].angle0 = G.currentSensorVal[ts_calib->no]; break; } case 1: { G.paperCalibs[ts_calib->no].num1 = (U16)(ts_calib->paperNum0) << 8 | ts_calib->paperNum1; G.paperCalibs[ts_calib->no].angle1 = G.currentSensorVal[ts_calib->no]; break; } } L3_flash_write_page0(); //??Flash g_upper_calib_ack.num0 = G.paperCalibs[ts_calib->no].num0; g_upper_calib_ack.angle0 = G.paperCalibs[ts_calib->no].angle0; g_upper_calib_ack.num1 = G.paperCalibs[ts_calib->no].num1; g_upper_calib_ack.angle1 = G.paperCalibs[ts_calib->no].angle1; ccmodbus_len = L2_constructor_ccmodbus_ack(&ccmodbus_ack, ADDR_UPPER, OPER_CALIB, (U8 *)&g_upper_calib_ack, sizeof(g_upper_calib_ack)); L0_uart0_sendArray((U8 *)&ccmodbus_ack,ccmodbus_len); } void L2_do_calib_enter(void) { G.runmode = MODE_PAPER_CALIB; ccmodbus_len = L2_constructor_ccmodbus_ack(&ccmodbus_ack, ADDR_UPPER, OPER_CALIB_MODE, NULL, 0 ); L0_uart0_sendArray((U8 *)&ccmodbus_ack,ccmodbus_len); } void L2_do_calib_leave(void) { G.runmode = MODE_RUN; ccmodbus_len = L2_constructor_ccmodbus_ack(&ccmodbus_ack, ADDR_UPPER, OPER_CALIB_LEAVE, NULL, 0 ); L0_uart0_sendArray((U8 *)&ccmodbus_ack,ccmodbus_len); } void L2_do_board_reset(void) { G.reset = 1; //wdt stop feed L2_do_gm35_reset(); //GM35_DISABLE(); //gm35 poweroff } void L2_do_gm35_reset(void) { //GM35_DISABLE(); //gm35 poweroff //Lc_delay_ms(200); //GM35_ENABLE(); NB_key = 1; Lc_delay_ms(8000); NB_key = 0; Lc_delay_ms(15000); L3_task_NB_open(&s_task_nb_open); } void L3_protocol_handler_uart0_fun(TS_PH3_ccmodbus *p,U8 from) //from:0代表上位机,1代表服务器 { switch(p->oper) { case OPER_RESET: L2_do_board_reset(); break; case OPER_CALIB_MODE: L2_do_calib_enter(); break; case OPER_CALIB_LEAVE: L2_do_calib_leave(); break; case OPER_CALIB: L2_do_calib(p->buf); break; case OPER_CALIB_READ: L2_do_calib_read(p->buf); break; case OPER_PAPER_COUNT: L2_do_paper_count(p->buf); break; case OPER_SET_485_ID: //L2_do_angle_setid(p->buf); break; case OPER_RELAY: L2_do_relay(p->buf); if(from == 1) { L0_uart0_uc('R'); L0_uart0_uchex(p->buf[0]); L0_uart0_uchex(p->buf[1]); g_at_send_status.ack_flag = 1; //如果是服务器则发送ack包 } break; case OPER_ISP: //ResetToISP L0_uart0_uc('P'); resetToIsp(); break; case 0x00: //心跳 L0_uart0_uc('H'); break; case 0x99: //修改运行模式 G.runmode = p->buf[1] & 0x0F; Lc_print_buf("Version: %s\r\n",APP_VERSION); Lc_delay_ms(700);// 2000--7s break; default: break; }; } void L3_protocol_handler_uart4_fun(U8 *pPara) { #if 0 if(StrStartsWith(pPara,"OK")) //module ok { g_at_send_status.module_ok = 1; } else if(StrStartsWith(pPara,"+CCID")) //sim ok { L0_uart0_uc('s'); libc_memcpy(G.ccid,pPara+7,20); getPwd(G.ccid, G.ccid_passwd); g_at_send_status.sim_ok = 1; } else if(StrStartsWith(pPara,"+CREG: 1,1")) //net ok { L0_uart0_uc('n'); g_at_send_status.net_ok = 1; } else if(StrStartsWith(pPara,"+CGATT:1")) //gprs ok { L0_uart0_uc('g'); g_at_send_status.gprs_ok = 1; } else if(StrStartsWith(pPara,"CONNECT OK") || StrStartsWith(pPara,"STATE:CONNECT OK")) //conn ok { //ALREAY CONNECT????????????????????????????????????????????????????????????????? if(g_at_send_status.conn_ok == 0) { L0_uart0_uc('c'); g_at_send_status.conn_ok = 1; } } else if(StrStartsWith(pPara,"CLOSE") || StrStartsWith(pPara,"STATE:CLOSED")) //conn close //CLOSE OK / CLOSED { if(g_at_send_status.conn_ok == 1) { L0_uart0_uc('d'); g_at_send_status.conn_ok = 0; } } if(g_at_send_status.conn_ok == 0) { if(StrStartsWith(pPara,"CONNECT OK") || StrStartsWith(pPara,"STATE:CONNECT OK") /*|| StrStartsWith(pPara,"ALREAY")*/ ) //conn ok { //ALREAY CONNECT不需要判断了,使用AT+CTPSTATUS指令进行查询连接状态 L0_uart0_uc('c'); g_at_send_status.conn_ok = 1; } else if(g_at_send_status.module_ok == 0) { if(StrStartsWith(pPara,"OK")) //module ok { g_at_send_status.module_ok = 1; } } else if(g_at_send_status.sim_ok == 0) { if(StrStartsWith(pPara,"+CCID")) //sim ok { L0_uart0_uc('s'); libc_memcpy(G.ccid,pPara+7,20); getPwd(G.ccid, G.ccid_passwd); g_at_send_status.sim_ok = 1; } } else if(g_at_send_status.net_ok == 0) { if(StrStartsWith(pPara,"+CREG: 1,1")) //net ok { L0_uart0_uc('n'); g_at_send_status.net_ok = 1; } } else if(g_at_send_status.gprs_ok == 0) { if(StrStartsWith(pPara,"+CGATT:1")) //gprs ok { L0_uart0_uc('g'); g_at_send_status.gprs_ok = 1; } } } #else #if 1 //NB模块 if(g_at_send_status.conn_ok == 0) { if(StrStartsWith(pPara,"CONNECT OK") || StrStartsWith(pPara,"+IPSTATUS: 0,\"TCP\"")/*StrStartsWith(pPara,"STATE:CONNECT OK")*/ /*|| StrStartsWith(pPara,"ALREAY")*/ ) //conn ok { //ALREAY CONNECT不需要判断了,使用AT+CTPSTATUS指令进行查询连接状态 L0_uart0_uc('c'); g_at_send_status.conn_ok = 1; } else if(g_at_send_status.module_ok == 0) { if(StrStartsWith(pPara,"OK")) //module ok { g_at_send_status.module_ok = 1; } } else if(g_at_send_status.sim_ok == 0) { if(StrStartsWith(pPara,"+ICCID:")) //sim ok { L0_uart0_uc('s'); libc_memcpy(G.ccid,pPara+8,20);//返回的+ICCID后面有一位空格 getPwd(G.ccid, G.ccid_passwd); g_at_send_status.sim_ok = 1; } } else if(g_at_send_status.net_ok == 0) { if(StrStartsWith(pPara,"+CEREG: 0,1")) //net ok { L0_uart0_uc('n'); g_at_send_status.net_ok = 1; } } else if(g_at_send_status.gprs_ok == 0) { if(StrStartsWith(pPara,"+CGATT: 1")) //gprs ok { L0_uart0_uc('g'); g_at_send_status.gprs_ok = 1; } } /*else if(g_at_send_status.return_off_ok == 0) { if(StrStartsWith(pPara,"ATE0")) //return_off_ok { //L0_uart0_uc('g'); g_at_send_status.return_off_ok = 1; } } else if(g_at_send_status.print_ok == 0) { if(StrStartsWith(pPara,"OK")) //print_ok { //L0_uart0_uc('g'); g_at_send_status.print_ok = 1; } }*/ } #endif else { if(/*StrStartsWith(pPara,"CLOSE") || */StrStartsWith(pPara,"+IPSTATUS: 0,\"\",\"\",0,\"\"") || StrStartsWith(pPara,"+IPCLOSE: 0")/*StrStartsWith(pPara,"STATE:CLOSED")*/) //conn close //CLOSE OK / CLOSED { L0_uart0_uc('d'); g_at_send_status.conn_ok = 0; } } #endif }