/**
 * This file is part of Traintastic,
 * see <https://github.com/traintastic/traintastic>.
 *
 * Copyright (C) 2025-2026 Reinder Feenstra
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#ifndef TRAINTASTIC_SERVER_HARDWARE_PROTOCOL_LOCONET_LNCV_LNCVUTILS_HPP
#define TRAINTASTIC_SERVER_HARDWARE_PROTOCOL_LOCONET_LNCV_LNCVUTILS_HPP

#include <cstdint>
#include "lncverror.hpp"

namespace LocoNet::LNCV {

/**
 * @brief Interpret OPC_LONG_ACK status byte for LNCV operations.
 *
 * @note The status values used here are based on monitoring LocoNet traffic
 *       generated by a Uhlenbrock Intellibox performing LNCV operations against
 *       Uhlenbrock devices.
 *
 * @note LNCV was introduced by Uhlenbrock, but no public specification for
 *       OPC_LONG_ACK status values have been found. These mappings therefore
 *       reflect observed, de-facto behavior and may vary for non-Uhlenbrock
 *       devices.
 */
[[nodiscard]] inline std::error_code parseLongAck(uint8_t status) noexcept
{
  switch(status)
  {
    case 0x00:
      return Error::rejected();

    case 0x01:
      return Error::notFound();

    case 0x03:
      return Error::readOnly();

    case 0x7F:
      return {}; // no error

    default:
      return Error::unexpectedResponse();
  }
}

}

#endif
