summaryrefslogtreecommitdiffstats
path: root/components/pairing/proto_decoder.cc
blob: f8e42a4bd4d14861c16d90b2cac7c7a1059597ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/pairing/proto_decoder.h"

#include "components/pairing/pairing_api.pb.h"
#include "net/base/io_buffer.h"

namespace {
enum {
  MESSAGE_NONE,
  MESSAGE_HOST_STATUS,
  MESSAGE_CONFIGURE_HOST,
  MESSAGE_PAIR_DEVICES,
  MESSAGE_COMPLETE_SETUP,
  MESSAGE_ERROR,
  MESSAGE_ADD_NETWORK,
  NUM_MESSAGES,
};
}

namespace pairing_chromeos {

ProtoDecoder::ProtoDecoder(Observer* observer)
    : observer_(observer),
      next_message_type_(MESSAGE_NONE),
      next_message_size_(0) {
  DCHECK(observer_);
}

ProtoDecoder::~ProtoDecoder() {}

bool ProtoDecoder::DecodeIOBuffer(int size,
                                  ProtoDecoder::IOBufferRefPtr io_buffer) {
  // Update the message buffer.
  message_buffer_.AddIOBuffer(io_buffer, size);

  // If there is no current message, the next byte is the message type.
  if (next_message_type_ == MESSAGE_NONE) {
    if (message_buffer_.AvailableBytes() < static_cast<int>(sizeof(uint8_t)))
      return true;

    uint8_t message_type = MESSAGE_NONE;
    message_buffer_.ReadBytes(reinterpret_cast<char*>(&message_type),
                               sizeof(message_type));

    if (message_type == MESSAGE_NONE || message_type >= NUM_MESSAGES) {
      LOG(ERROR) << "Unknown message type received: " << message_type;
      return false;
    }
    next_message_type_ = message_type;
  }

  // If the message size isn't set, the next two bytes are the message size.
  if (next_message_size_ == 0) {
    if (message_buffer_.AvailableBytes() < static_cast<int>(sizeof(uint16_t)))
      return true;

    // The size is sent in network byte order.
    uint8_t high_byte = 0;
    message_buffer_.ReadBytes(reinterpret_cast<char*>(&high_byte),
                               sizeof(high_byte));
    uint8_t low_byte = 0;
    message_buffer_.ReadBytes(reinterpret_cast<char*>(&low_byte),
                               sizeof(low_byte));

    next_message_size_ = (high_byte << 8) + low_byte;
  }

  // If the whole proto buffer is not yet available, return early.
  if (message_buffer_.AvailableBytes() < next_message_size_)
    return true;

  std::vector<char> buffer(next_message_size_);
  message_buffer_.ReadBytes(&buffer[0], next_message_size_);

  switch (next_message_type_) {
    case MESSAGE_HOST_STATUS: {
        pairing_api::HostStatus message;
        message.ParseFromArray(&buffer[0], buffer.size());
        observer_->OnHostStatusMessage(message);
      }
      break;
    case MESSAGE_CONFIGURE_HOST: {
        pairing_api::ConfigureHost message;
        message.ParseFromArray(&buffer[0], buffer.size());
        observer_->OnConfigureHostMessage(message);
      }
      break;
    case MESSAGE_PAIR_DEVICES: {
        pairing_api::PairDevices message;
        message.ParseFromArray(&buffer[0], buffer.size());
        observer_->OnPairDevicesMessage(message);
      }
      break;
    case MESSAGE_COMPLETE_SETUP: {
        pairing_api::CompleteSetup message;
        message.ParseFromArray(&buffer[0], buffer.size());
        observer_->OnCompleteSetupMessage(message);
      }
      break;
    case MESSAGE_ERROR: {
        pairing_api::Error message;
        message.ParseFromArray(&buffer[0], buffer.size());
        observer_->OnErrorMessage(message);
      }
      break;
    case MESSAGE_ADD_NETWORK: {
        pairing_api::AddNetwork message;
        message.ParseFromArray(&buffer[0], buffer.size());
        observer_->OnAddNetworkMessage(message);
      }
      break;

    default:
      LOG(WARNING) << "Skipping unknown message type: " << next_message_type_;
      break;
  }

  // Reset the message data.
  next_message_type_ = MESSAGE_NONE;
  next_message_size_ = 0;

  return true;
}

ProtoDecoder::IOBufferRefPtr ProtoDecoder::SendHostStatus(
    const pairing_api::HostStatus& message, int* size) {
  std::string serialized_proto;
  if (!message.SerializeToString(&serialized_proto)) {
    NOTREACHED();
  }

  return SendMessage(MESSAGE_HOST_STATUS, serialized_proto, size);
}

ProtoDecoder::IOBufferRefPtr ProtoDecoder::SendConfigureHost(
    const pairing_api::ConfigureHost& message, int* size) {
  std::string serialized_proto;
  if (!message.SerializeToString(&serialized_proto)) {
    NOTREACHED();
  }

  return SendMessage(MESSAGE_CONFIGURE_HOST, serialized_proto, size);
}

ProtoDecoder::IOBufferRefPtr ProtoDecoder::SendPairDevices(
    const pairing_api::PairDevices& message, int* size) {
  std::string serialized_proto;
  if (!message.SerializeToString(&serialized_proto)) {
    NOTREACHED();
  }

  return SendMessage(MESSAGE_PAIR_DEVICES, serialized_proto, size);
}

ProtoDecoder::IOBufferRefPtr ProtoDecoder::SendCompleteSetup(
    const pairing_api::CompleteSetup& message, int* size) {
  std::string serialized_proto;
  if (!message.SerializeToString(&serialized_proto)) {
    NOTREACHED();
  }

  return SendMessage(MESSAGE_COMPLETE_SETUP, serialized_proto, size);
}

ProtoDecoder::IOBufferRefPtr ProtoDecoder::SendError(
    const pairing_api::Error& message, int* size) {
  std::string serialized_proto;
  if (!message.SerializeToString(&serialized_proto)) {
    NOTREACHED();
  }

  return SendMessage(MESSAGE_ERROR, serialized_proto, size);
}

ProtoDecoder::IOBufferRefPtr ProtoDecoder::SendMessage(
    uint8_t message_type,
    const std::string& message,
    int* size) {
  uint16_t message_size = message.size();

  *size = sizeof(message_type) + sizeof(message_size) + message.size();
  IOBufferRefPtr io_buffer(new net::IOBuffer(*size));

  // Write the message type.
  int offset = 0;
  memcpy(&io_buffer->data()[offset], &message_type, sizeof(message_type));
  offset += sizeof(message_type);

  // Network byte order.
  // Write the high byte of the size.
  uint8_t data = (message_size >> 8) & 0xFF;
  memcpy(&io_buffer->data()[offset], &data, sizeof(data));
  offset += sizeof(data);
  // Write the low byte of the size.
  data = message_size & 0xFF;
  memcpy(&io_buffer->data()[offset], &data, sizeof(data));
  offset += sizeof(data);

  // Write the actual message.
  memcpy(&io_buffer->data()[offset], message.data(), message.size());

  return io_buffer;
}

}  // namespace pairing_chromeos