#include #include #include #include SyrlinksHkHandler::SyrlinksHkHandler(object_id_t objectId, object_id_t comIF, CookieIF * comCookie) : DeviceHandlerBase(objectId, comIF, comCookie), rxDataset(this), txDataset(this) { if (comCookie == NULL) { sif::error << "SyrlinksHkHandler: Invalid com cookie" << std::endl; } } SyrlinksHkHandler::~SyrlinksHkHandler() { } void SyrlinksHkHandler::doStartUp(){ if(mode == _MODE_START_UP){ setMode(MODE_ON); } } void SyrlinksHkHandler::doShutDown(){ } ReturnValue_t SyrlinksHkHandler::buildNormalDeviceCommand( DeviceCommandId_t * id) { *id = SYRLINKS::READ_RX_STATUS_REGISTERS; return buildCommandFromCommand(*id, NULL, 0); } ReturnValue_t SyrlinksHkHandler::buildTransitionDeviceCommand( DeviceCommandId_t * id){ return HasReturnvaluesIF::RETURN_OK; } ReturnValue_t SyrlinksHkHandler::buildCommandFromCommand( DeviceCommandId_t deviceCommand, const uint8_t * commandData, size_t commandDataLen) { switch(deviceCommand) { case(SYRLINKS::RESET_UNIT): { resetCommand.copy(reinterpret_cast(commandBuffer), resetCommand.size(), 0); rawPacketLen = resetCommand.size(); rawPacket = commandBuffer; return RETURN_OK; } case(SYRLINKS::SET_TX_MODE_STANDBY): { setTxModeStandby.copy(reinterpret_cast(commandBuffer), setTxModeStandby.size(), 0); rawPacketLen = setTxModeStandby.size(); rawPacket = commandBuffer; return RETURN_OK; } case(SYRLINKS::SET_TX_MODE_MODULATION): { setTxModeModulation.copy(reinterpret_cast(commandBuffer), setTxModeModulation.size(), 0); rawPacketLen = setTxModeModulation.size(); rawPacket = commandBuffer; return RETURN_OK; } case(SYRLINKS::SET_TX_MODE_CW): { setTxModeCw.copy(reinterpret_cast(commandBuffer), setTxModeCw.size(), 0); rawPacketLen = setTxModeCw.size(); rawPacket = commandBuffer; return RETURN_OK; } case(SYRLINKS::READ_RX_STATUS_REGISTERS): { readRxStatusRegCommand.copy(reinterpret_cast(commandBuffer), readRxStatusRegCommand.size(), 0); rawPacketLen = readRxStatusRegCommand.size(); rawPacket = commandBuffer; return RETURN_OK; } case(SYRLINKS::READ_TX_STATUS): { readTxStatus.copy(reinterpret_cast(commandBuffer), readTxStatus.size(), 0); rawPacketLen = readTxStatus.size(); rememberCommandId = SYRLINKS::READ_TX_STATUS; rawPacket = commandBuffer; return RETURN_OK; } default: return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED; } return HasReturnvaluesIF::RETURN_FAILED; } void SyrlinksHkHandler::fillCommandAndReplyMap() { this->insertInCommandAndReplyMap(SYRLINKS::RESET_UNIT, 1, nullptr, SYRLINKS::ACK_SIZE, false, true, SYRLINKS::ACK_REPLY); this->insertInCommandAndReplyMap(SYRLINKS::SET_TX_MODE_STANDBY, 1, nullptr, SYRLINKS::ACK_SIZE, false, true, SYRLINKS::ACK_REPLY); this->insertInCommandAndReplyMap(SYRLINKS::SET_TX_MODE_MODULATION, 1, nullptr, SYRLINKS::ACK_SIZE, false, true, SYRLINKS::ACK_REPLY); this->insertInCommandAndReplyMap(SYRLINKS::SET_TX_MODE_CW, 1, nullptr, SYRLINKS::ACK_SIZE, false, true, SYRLINKS::ACK_REPLY); this->insertInCommandAndReplyMap(SYRLINKS::READ_TX_STATUS, 1, &txDataset, SYRLINKS::READ_TX_STATUS); this->insertInCommandAndReplyMap(SYRLINKS::READ_RX_STATUS_REGISTERS, 1, &rxDataset, SYRLINKS::RX_STATUS_REGISTERS_REPLY_SIZE); } ReturnValue_t SyrlinksHkHandler::scanForReply(const uint8_t *start, size_t remainingSize, DeviceCommandId_t *foundId, size_t *foundLen) { ReturnValue_t result = RETURN_OK; if(*start != '<') { sif::error << "SyrlinksHkHandler::scanForReply: Missing start frame character" << std::endl; return MISSING_START_FRAME_CHARACTER; } switch(*(start + 1)) { case('A'): *foundLen = SYRLINKS::ACK_SIZE; *foundId = SYRLINKS::ACK_REPLY; break; case('E'): *foundLen = SYRLINKS::RX_STATUS_REGISTERS_REPLY_SIZE; *foundId = SYRLINKS::READ_RX_STATUS_REGISTERS; break; case('R'): if (rememberCommandId == SYRLINKS::READ_TX_STATUS) { *foundLen = SYRLINKS::READ_TX_STATUS_REPLY_SIZE; *foundId = SYRLINKS::READ_TX_STATUS; rememberCommandId = SYRLINKS::NONE; } break; default: sif::error << "SyrlinksHkHandler::scanForReply: Unknown reply identifier" << std::endl; result = IGNORE_REPLY_DATA; break; } return result; } ReturnValue_t SyrlinksHkHandler::interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) { ReturnValue_t result; switch (id) { case (SYRLINKS::ACK_REPLY): result = verifyReply(packet, SYRLINKS::ACK_SIZE); if (result != RETURN_OK) { sif::error << "SyrlinksHkHandler::interpretDeviceReply: Acknowledgement reply has " "invalid crc" << std::endl; return CRC_FAILURE; } result = parseReplyStatus(reinterpret_cast(packet + SYRLINKS::MESSAGE_HEADER_SIZE)); if (result != RETURN_OK) { return result; } break; case(SYRLINKS::READ_RX_STATUS_REGISTERS): result = verifyReply(packet, SYRLINKS::RX_STATUS_REGISTERS_REPLY_SIZE); if (result != RETURN_OK) { sif::error << "SyrlinksHkHandler::interpretDeviceReply: Read rx status registers reply " << "has invalid crc" << std::endl; return CRC_FAILURE; } parseRxStatusRegistersReply(packet); break; case(SYRLINKS::READ_TX_STATUS): result = verifyReply(packet, SYRLINKS::READ_TX_STATUS_REPLY_SIZE); if (result != RETURN_OK) { sif::error << "SyrlinksHkHandler::interpretDeviceReply: Read tx status reply " << "has invalid crc" << std::endl; return CRC_FAILURE; } parseTxStatusReply(packet); break; default: { sif::debug << "SyrlinksHkHandler::interpretDeviceReply: Unknown device reply id" << std::endl; return DeviceHandlerIF::UNKNOWN_DEVICE_REPLY; } } return RETURN_OK; } std::string SyrlinksHkHandler::convertIntToHexString(uint16_t intValue) { std::stringstream stream; stream << std::hex << intValue; return stream.str(); } uint8_t SyrlinksHkHandler::convertHexStringToUint8(char* twoChars) { uint8_t value; std::string hexString(twoChars, 2); std::stringstream stream; stream << std::hex << hexString; stream >> value; return value; } uint16_t convertHexStringToUint16(char* fourChars) { } ReturnValue_t SyrlinksHkHandler::parseReplyStatus(const char* status) { switch (*status) { case '0': return RETURN_OK; case '1': sif::debug << "SyrlinksHkHandler::parseReplyStatus: Uart faming or parity error" << std::endl; return UART_FRAMIN_OR_PARITY_ERROR_ACK; case '2': sif::debug << "SyrlinksHkHandler::parseReplyStatus: Bad character detected" << std::endl; return BAD_CHARACTER_ACK; case '3': sif::debug << "SyrlinksHkHandler::parseReplyStatus: Bad parameter value (unexpected value " << "detected" << std::endl; return BAD_PARAMETER_VALUE_ACK; case '4': sif::debug << "SyrlinksHkHandler::parseReplyStatus: Bad end of frame" << std::endl; return BAD_END_OF_FRAME_ACK; case '5': sif::debug << "SyrlinksHkHandler::parseReplyStatus: Unknown command id or attempt to access" << " a protected register" << std::endl; return UNKNOWN_COMMAND_ID_ACK; case '6': sif::debug << "SyrlinksHkHandler::parseReplyStatus: Bad CRC" << std::endl; return BAD_CRC_ACK; default: sif::debug << "SyrlinksHkHandler::parseReplyStatus: Status reply contains an invalid " << "status id" << std::endl; return RETURN_FAILED; } } ReturnValue_t SyrlinksHkHandler::verifyReply(const uint8_t* packet, uint8_t size) { int result = 0; /* Calculate crc of received packet */ uint16_t crc = CRC::crc16ccitt(packet, size - SYRLINKS::SIZE_CRC_AND_TERMINATION, CRC_INITIAL_VALUE); std::string recalculatedCrc = convertIntToHexString(crc); const char* replyCrc = reinterpret_cast(packet) + size - SYRLINKS::SIZE_CRC_AND_TERMINATION; size_t startPosition = 0; result = recalculatedCrc.compare(startPosition, SYRLINKS::CRC_FIELD_SIZE, replyCrc); if (result != 0) { return RETURN_FAILED; } return RETURN_OK; } void SyrlinksHkHandler::parseRxStatusRegistersReply(const uint8_t* packet) { PoolReadHelper readHelper(&rxDataset); uint16_t offset = SYRLINKS::MESSAGE_HEADER_SIZE; rxDataset.rxStatus = *(packet + offset); packet += 1; rxDataset.rxSensitivity = *(packet + offset) << 16 | *(packet + offset + 1) << 8 | *(packet + offset + 2); packet += 3; rxDataset.rxFrequencyShift = *(packet + offset) << 16 | *(packet + offset + 1) << 8 | *(packet + offset + 2); packet += 3; rxDataset.rxIqPower = *(packet + offset) << 8 | *(packet + offset + 1); packet += 2; rxDataset.rxAgcValue = *(packet + offset + 1) << 8 | *(packet + offset); packet += 2; packet += 1; // reserved register rxDataset.rxDemodEb= *(packet + offset) << 16 | *(packet + offset + 1) << 8 | *(packet + offset + 2); packet += 3; rxDataset.rxDemodN0= *(packet + offset) << 16 | *(packet + offset + 1) << 8 | *(packet + offset + 2); packet += 3; rxDataset.rxDataRate = *(packet + offset); } void SyrlinksHkHandler::parseTxStatusReply(const uint8_t* packet) { PoolReadHelper readHelper(&txDataset); uint16_t offset = SYRLINKS::MESSAGE_HEADER_SIZE; rxDataset.rxStatus = *(packet + offset); } void SyrlinksHkHandler::setNormalDatapoolEntriesInvalid(){ } uint32_t SyrlinksHkHandler::getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo){ return 500; } ReturnValue_t SyrlinksHkHandler::initializeLocalDataPool(localpool::DataPool& localDataPoolMap, LocalDataPoolManager& poolManager) { localDataPoolMap.emplace(SYRLINKS::RX_STATUS, new PoolEntry( { 0 })); localDataPoolMap.emplace(SYRLINKS::RX_SENSITIVITY, new PoolEntry( { 0 })); localDataPoolMap.emplace(SYRLINKS::RX_FREQUENCY_SHIFT, new PoolEntry( { 0 })); localDataPoolMap.emplace(SYRLINKS::RX_IQ_POWER, new PoolEntry( { 0 })); localDataPoolMap.emplace(SYRLINKS::RX_AGC_VALUE, new PoolEntry( { 0 })); localDataPoolMap.emplace(SYRLINKS::RX_DEMOD_EB, new PoolEntry( { 0 })); localDataPoolMap.emplace(SYRLINKS::RX_DEMOD_N0, new PoolEntry( { 0 })); localDataPoolMap.emplace(SYRLINKS::RX_DATA_RATE, new PoolEntry( { 0 })); return HasReturnvaluesIF::RETURN_OK; } void SyrlinksHkHandler::setModeNormal() { mode = MODE_NORMAL; }