1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include "host/libs/usbip/messages.h"
17
18#include <netinet/in.h>
19#include <iostream>
20
21#include <glog/logging.h>
22
23namespace vadb {
24namespace usbip {
25namespace {
26// Basic sanity checking.
27// We're using CmdHeader + CmdReq/Rep in case any of the fields is moved between
28// structures.
29constexpr int kUsbIpCmdLength = 48;
30
31static_assert(sizeof(CmdHeader) + sizeof(CmdReqSubmit) == kUsbIpCmdLength,
32              "USB/IP command + header must be exactly 48 bytes.");
33static_assert(sizeof(CmdHeader) + sizeof(CmdRepSubmit) == kUsbIpCmdLength,
34              "USB/IP command + header must be exactly 48 bytes.");
35static_assert(sizeof(CmdHeader) + sizeof(CmdReqUnlink) == kUsbIpCmdLength,
36              "USB/IP command + header must be exactly 48 bytes.");
37static_assert(sizeof(CmdHeader) + sizeof(CmdRepUnlink) == kUsbIpCmdLength,
38              "USB/IP command + header must be exactly 48 bytes.");
39}  // namespace
40
41std::ostream& operator<<(std::ostream& out, const CmdHeader& header) {
42  out << "CmdHeader\n";
43  out << "\t\tcmd:\t" << header.command << '\n';
44  out << "\t\tseq#:\t" << header.seq_num << '\n';
45  out << "\t\tbus#:\t0x" << header.bus_num << '\n';
46  out << "\t\tdev#:\t0x" << header.dev_num << '\n';
47  out << "\t\tdir:\t" << (header.direction ? "in" : "out") << '\n';
48  out << "\t\tendpt:\t" << header.endpoint << "\n";
49  return out;
50}
51
52std::ostream& operator<<(std::ostream& out, const CmdRequest& setup) {
53  out << "Request\n";
54  out << "\t\t\ttype:\t" << std::hex << int(setup.type) << '\n';
55  out << "\t\t\treq:\t" << int(setup.cmd) << std::dec << '\n';
56  out << "\t\t\tval:\t" << setup.value << '\n';
57  out << "\t\t\tidx:\t" << setup.index << '\n';
58  out << "\t\t\tlen:\t" << setup.length << '\n';
59  return out;
60}
61
62std::ostream& operator<<(std::ostream& out, const CmdReqSubmit& submit) {
63  out << "CmdReqSubmit\n";
64  out << "\t\ttr_flg:\t" << std::hex << submit.transfer_flags << std::dec
65      << '\n';
66  out << "\t\ttr_len:\t" << submit.transfer_buffer_length << '\n';
67  out << "\t\tstart:\t" << submit.start_frame << '\n';
68  out << "\t\tpktcnt:\t" << submit.number_of_packets << '\n';
69  out << "\t\tttl:\t" << submit.deadline_interval << '\n';
70  out << "\t\tsetup:\t" << submit.setup << '\n';
71  return out;
72}
73
74std::ostream& operator<<(std::ostream& out, const CmdRepSubmit& submit) {
75  out << "CmdRepSubmit\n";
76  out << "\t\tstatus:\t" << submit.status << '\n';
77  out << "\t\tlen:\t" << submit.actual_length << '\n';
78  out << "\t\tstart:\t" << submit.start_frame << '\n';
79  out << "\t\tpktcnt:\t" << submit.number_of_packets << '\n';
80  out << "\t\terrors:\t" << submit.error_count << '\n';
81  out << "\t\tsetup:\t" << submit.setup << '\n';
82  return out;
83}
84
85std::ostream& operator<<(std::ostream& out, const CmdReqUnlink& unlink) {
86  out << "CmdReqUnlink\n";
87  out << "\t\tseq#:\t" << unlink.seq_num << '\n';
88  return out;
89}
90
91std::ostream& operator<<(std::ostream& out, const CmdRepUnlink& unlink) {
92  out << "CmdRepUnlink\n";
93  out << "\t\tstatus:\t" << unlink.status << '\n';
94  return out;
95}
96
97}  // namespace usbip
98}  // namespace vadb
99