#include "cw1310_comm.h" #include "cw1310.h" #include "cw1310_sm.h" #include "plt.h" void cw1310_comm_dac(int idx) { struct cw1310_t *dev = &cw1310[idx]; struct comm_t *comm = &dev->comm; int chanidx = comm->chanidx; int addr = comm->adr; int ret = 0; if (comm_get_state(comm) != COMMST_NORMAL) { return; } comm_start_cal_dac_timing(comm); chan_socketcan_ringbuffer_element_t e; static chan_socketcan_ringbuffer_element_t e_temp[128]; int tempQueueCount = 0; // e.frame.can_id = 0x18A201F6 & CAN_EFF_MASK; e.frame.can_id = 0x18A200F6 | (addr << 8); // e.frame.can_id = 0x18A200F6 | (0xFF << 8); //发送广播消息 e.frame.can_id |= CAN_EFF_FLAG; e.frame.can_dlc = 2; e.frame.data[0] = 0x5A; e.frame.data[1] = 0x4E; e.frame.data[2] = 0; e.frame.data[3] = 0; e.frame.data[4] = 0; e.frame.data[5] = 0; e.frame.data[6] = 0; e.frame.data[7] = 0; chan_lock(chanidx); chan_socketcan_txrb_queue(chanidx, e); usleep(100000); // 100ms while (chan_socketcan_rxrb_num_items(chanidx) > 0) { // printf("idx : %d items count : %d\n", addr, chan_socketcan_rxrb_num_items(chanidx)); if (chan_socketcan_rxrb_dequeue(chanidx, &e) > 0) { int fun = (e.frame.can_id >> 16) & 0xff; int msgAddr = (e.frame.can_id) & 0xff; if ((fun) == 0xB0) { if (msgAddr != addr) { // log_dbg("can msg on addr%d is received when receive addr%d", msgAddr, addr); e_temp[tempQueueCount] = e; tempQueueCount++; } else { struct firedetect_t *fd = &(dev->fa_pack); fd->t1 = e.frame.data[0] - 40; fd->t2 = e.frame.data[1] - 40; fd->co = e.frame.data[2] * 256 + e.frame.data[3]; fd->voc = e.frame.data[4] * 256 + e.frame.data[5]; fd->smoke_flag = e.frame.data[6]; if (fd->smoke_flag == 0xAA) { sprintf(fd->szSmokeFlags, "normal"); } else if (fd->smoke_flag == 0x55) { sprintf(fd->szSmokeFlags, "warning"); } else { sprintf(fd->szSmokeFlags, "unexpected"); } fd->fireLevel = (e.frame.data[7] & 0xf0) >> 4; fd->status_code = (e.frame.data[7] & 0x0f); if (fd->status_code == 0) { sprintf(fd->szStatus, "normal"); } else if (fd->status_code == 1) { sprintf(fd->szStatus, "fe on"); } else if (fd->status_code == 2) { sprintf(fd->szStatus, "sensor failure"); } else if (fd->status_code == 3) { sprintf(fd->szStatus, "hw failure"); } cw1310_check_warning(idx); } } } } if(tempQueueCount>0 && tempQueueCount<128) { chan_socketcan_rxrb_queue_arr(chanidx, e_temp, tempQueueCount); // 重新入队 } chan_unlock(chanidx); comm_stop_cal_dac_timing(comm); } int testCount = 0; int cw1310_check_warning(int idx) { struct cw1310_t *dev = &cw1310[idx]; struct firedetect_t *fd = &(dev->fa_pack); int level = 0; if(( (fd->co>=dev->level4_co || fd->voc>=dev->level4_voc) &&(fd->t1>=dev->level3_tmp || fd->t2>=dev->level3_tmp)) || fd->t1>=dev->level4_tmp || fd->t2>=dev->level4_tmp ) { level = 4; } else if(fd->co>=dev->level3_co || fd->voc>=dev->level3_voc || fd->t1>=dev->level3_tmp || fd->t2>=dev->level3_tmp) { level = 3; } else if(fd->co>=dev->level2_co || fd->voc>=dev->level2_voc || fd->fireLevel>=4 || fd->t1>=dev->level2_tmp || fd->t2>=dev->level2_tmp) { level = 2; } else if(fd->co>=dev->level1_co || fd->voc>=dev->level1_voc || fd->smoke_flag == 0x55 || (fd->fireLevel>1 && fd->fireLevel<4) ) { level = 1; } if (level != fd->lastCheck) { fd->lastCheck = level; testCount = 0; } else if (testCount++ > 15 && (fd->warningLevel < fd->lastCheck || fd->warningLevel < 3)) // 连续探测15个周期,确认状态 { fd->warningLevel = fd->lastCheck; } } int cw1310_comm_init(int idx) { struct cw1310_t *dev = &cw1310[idx]; struct comm_t *comm = &dev->comm; comm_set_state(comm, COMMST_ERR); } int cw1310_comm_reset(int idx) { struct cw1310_t *dev = &cw1310[idx]; struct comm_t *comm = &dev->comm; comm_set_state(comm, COMMST_NORMAL); }