fgerlits commented on code in PR #1779: URL: https://github.com/apache/nifi-minifi-cpp/pull/1779#discussion_r1668369783
########## extensions/standard-processors/modbus/ReadModbusFunctions.cpp: ########## @@ -0,0 +1,235 @@ +/** +* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ReadModbusFunctions.h" + +namespace org::apache::nifi::minifi::modbus { +std::vector<std::byte> ReadModbusFunction::requestBytes() const { + constexpr std::array modbus_service_protocol_identifier = {std::byte{0}, std::byte{0}}; + const auto pdu = rawPdu(); + const uint16_t length = pdu.size() + 1; + + std::vector<std::byte> request; + ranges::copy(toBytes(transaction_id_), std::back_inserter(request)); + ranges::copy(modbus_service_protocol_identifier, std::back_inserter(request)); + ranges::copy(toBytes(length), std::back_inserter(request)); + request.push_back(std::byte{unit_id_}); + ranges::copy(pdu, std::back_inserter(request)); + return request; +} + +[[nodiscard]] auto ReadModbusFunction::getRespBytes(std::span<const std::byte> resp_pdu) const -> nonstd::expected<std::span<const std::byte>, std::error_code> { + if (resp_pdu.size() < 2) { + return nonstd::make_unexpected(ModbusExceptionCode::InvalidResponse); + } + + if (const auto resp_function_code = resp_pdu.front(); resp_function_code != getFunctionCode()) { + return nonstd::make_unexpected(ModbusExceptionCode::InvalidResponse); + } + + const auto resp_byte_count = static_cast<uint8_t>(resp_pdu[1]); + constexpr uint8_t function_code_length = 1; + constexpr uint8_t unit_id_length = 1; + const uint8_t expected_resp_pdu_size = resp_byte_count + function_code_length + unit_id_length; + if (resp_pdu.size() != expected_resp_pdu_size) { + return nonstd::make_unexpected(ModbusExceptionCode::InvalidResponse); + } + + if (resp_byte_count != expectedByteCount()) { + return nonstd::make_unexpected(ModbusExceptionCode::InvalidResponse); + } Review Comment: nice! better than my suggestion -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
