blob: 575cb84c92d8b4bc457862ba9acdaff40f8625ea [file] [log] [blame]
// Copyright 2024 Google LLC
//
// Licensed 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.
#pragma once
#include "libusb.hpp"
#include "message_intf.hpp"
#include <boost/endian/arithmetic.hpp>
#include <stdplus/handle/managed.hpp>
#include <array>
#include <string_view>
namespace google
{
namespace hoth
{
using boost::endian::little_uint32_t;
using boost::endian::little_uint8_t;
class MessageUSB : public MessageIntf
{
public:
explicit MessageUSB(std::string_view usb_id, LibusbIntf* libusb,
bool unit_test = false);
void send(const uint8_t* buf, size_t size, size_t seek) override;
void recv(uint8_t* buf, size_t size, size_t seek) override;
static constexpr uint8_t kMailboxClass = 0xff;
static constexpr std::array<const uint8_t, 2> kMailboxSubClasses{
0x58, // New USB protocol
0x71 // Old USB protocol
};
static constexpr uint8_t kMailboxProtocol = 0x01;
// Hoths can only be full speed which limits buffers to 64B
static constexpr uint16_t kMaxBulkTransferSize = 64;
struct ReqHeader
{
static constexpr uint8_t kWrite = 0x02;
static constexpr uint8_t kRead = 0x00;
little_uint8_t type;
little_uint32_t offset;
little_uint8_t length;
};
struct RspHeader
{
little_uint8_t status;
little_uint8_t rsvd;
};
private:
libusb::Context ctx;
libusb::Device dev;
const libusb_interface_descriptor& interface;
const libusb_endpoint_descriptor &in, &out;
uint16_t maxInSize, maxOutSize;
libusb::DeviceHandle handle;
libusb::InterfaceClaim claim;
};
} // namespace hoth
} // namespace google