/* Set the device into BROADCAST_MODE, which transmits the data automatically. The second argument is the samping rate. */ RValue CHR_6dm::gotoMeasurementMode(SensorData *sensor, int freq) { addHeader(_shared.data); _shared.data[3] = SET_BROADCAST_MODE; _shared.data[4] = 1; RValue ret_val; if (freq < 20 || freq > 300) { return CHR_ErrorCommand; } else { _shared.data[5] = ((freq - 20) * 255) / 280; printf("X = %X\n", _shared.data[5]); } addCheckSum(_shared.data); ret_val = _sending_command(_shared.data); if (ret_val != CHR_OK) return ret_val; serial.ClosePort(); if (pthread_create(&_comm_thread,NULL,CHR_6dm::_communicator,(void*)&_shared) < 0) { perror("Thread Creation"); return CHR_Error; } _shared.measurement_mode = true; return CHR_OK; }
/* This method simply set the device into SLIENT_MODE, not that there really exist a config mode in the device. However, if user wants to change any settings of the device, they have to make sure the device is in a so called "config mode". I think this is a better way to prevent accidentally option changing. */ RValue CHR_6dm::gotoConfigMode() { if (!_shared.measurement_mode) { addHeader(_shared.data); _shared.data[3] = SET_SILENT_MODE; _shared.data[4] = 0; addCheckSum(_shared.data); return _sending_command(_shared.data); } else { pthread_cancel(_comm_thread); return open(_shared.timeout); } }
/* Set active channels in the device. the class SensorData is used to preserve the settings, thus it's passed by pointer. */ RValue CHR_6dm::setActiveChannels(SensorData *sensor) { addHeader(_shared.data); _shared.data[3] = SET_ACTIVE_CHANNELS; _shared.data[4] = 2; _shared.data[5] = sensor->getFirstChannelMask(); _shared.data[6] = sensor->getSecondChannelMask(); addCheckSum(_shared.data); RValue ret_val = _sending_command(_shared.data); if (ret_val == CHR_OK) { _shared.dataNumber = (sensor->getNumberOfChannel() * 2) + 9; printf("Number of data : %d\n", _shared.dataNumber); } return ret_val; }
/*! \sa PacketProcessor */ Packet& PacketProcessor::buildIPPacket(string p_DestinationIP, string p_SourceIP, string p_Payload) { //store the arguments m_DestinationIP = p_DestinationIP; m_SourceIP = p_SourceIP; m_Payload = p_Payload; //set version m_PacketBuffer[0] = (unsigned char)setSubField((unsigned)m_PacketBuffer[0], VERSION, 7, 4); //set standard header length m_PacketBuffer[0] = (unsigned char)setSubField((unsigned)m_PacketBuffer[0], IHL, 3, 0); //set the second field to zero (DSCP[7-2] and ECN[1-0]) //set identification setMultipleFields(&m_PacketBuffer[5], m_Identification++, SHORT); //set flags clearBit(&m_PacketBuffer[6], 7); //reserved clearBit(&m_PacketBuffer[6], 6); //Don't fragment DF clearBit(&m_PacketBuffer[6], 5); //More fragments MF //set fragment offset //set TTL m_PacketBuffer[8] = TTL; //set Protocol m_PacketBuffer[9] = PROTOCOL; //set source IP m_Converter.ipToUChar(m_SourceIP, &m_PacketBuffer[12]); //set destination IP m_Converter.ipToUChar(m_DestinationIP, &m_PacketBuffer[16]); //set paylod and packet length setMultipleFields(&m_PacketBuffer[3], setPayload(&m_PacketBuffer[20]), SHORT); addCheckSum(m_PacketBuffer); //set the IP packet into the frame m_Frame.setPDU(m_PacketBuffer); resetPacketBuffer(); return m_Frame; }
RValue CHR_6dm::getData(SensorData *sensor, UpdateMode umode) { byte local_buffer[BUF_MAX]; RValue ret_val; if (umode == NONBLOCKING) { if (!_shared.measurement_mode) { addHeader(_shared.data); _shared.data[3] = GET_DATA; _shared.data[4] = 0; addCheckSum(_shared.data); ret_val = _sending_command(_shared.data); if (ret_val != CHR_OK) return ret_val; } pthread_mutex_lock(&_shared.mutex); memcpy(local_buffer, _shared.data, _shared.dataNumber); pthread_mutex_unlock(&_shared.mutex); sensor->resolvePacket(local_buffer); printf("mask = %X%X\n", local_buffer[5], local_buffer[6]); return CHR_OK; } else if (umode == BLOCKING) { if (!_shared.measurement_mode) return CHR_Error; pthread_mutex_lock(&_shared.mutex); pthread_cond_wait(&_shared.condvar, &_shared.mutex); //printf("Signal Received!\n"); memcpy(local_buffer, _shared.data, _shared.dataNumber); pthread_mutex_unlock(&_shared.mutex); sensor->resolvePacket(local_buffer); return CHR_OK; } else { return CHR_Error; } }
/* This is how you can get the data in SLIENT_MODE. */ RValue CHR_6dm::getData(SensorData *sensor) { byte local_buffer[BUF_MAX]; RValue ret_val; if (!_shared.measurement_mode) { addHeader(_shared.data); _shared.data[3] = GET_DATA; _shared.data[4] = 0; addCheckSum(_shared.data); ret_val = _sending_command(_shared.data); if (ret_val != CHR_OK) return ret_val; } pthread_mutex_lock(&_shared.mutex); memcpy(local_buffer, _shared.data, _shared.dataNumber); pthread_mutex_unlock(&_shared.mutex); sensor->resolvePacket(local_buffer); printf("mask = %X%X\n", local_buffer[5], local_buffer[6]); return CHR_OK; }