summaryrefslogtreecommitdiffstats
path: root/device/serial/buffer.cc
blob: b5bf8883b970b4b9c5adf59ffbc103ce9fd78e28 (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
// 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 "base/numerics/safe_conversions.h"
#include "base/stl_util.h"
#include "device/serial/buffer.h"
#include "net/base/io_buffer.h"

namespace device {

ReadOnlyBuffer::~ReadOnlyBuffer() {
}

WritableBuffer::~WritableBuffer() {
}

SendBuffer::SendBuffer(
    const std::vector<char>& data,
    const base::Callback<void(int, device::serial::SendError)>& callback)
    : data_(data), callback_(callback) {}

SendBuffer::~SendBuffer() {}

const char* SendBuffer::GetData() {
  return vector_as_array(&data_);
}

uint32_t SendBuffer::GetSize() {
  return base::checked_cast<uint32_t>(data_.size());
}

void SendBuffer::Done(uint32_t bytes_read) {
  callback_.Run(bytes_read, device::serial::SEND_ERROR_NONE);
}

void SendBuffer::DoneWithError(uint32_t bytes_read, int32_t error) {
  callback_.Run(bytes_read, static_cast<device::serial::SendError>(error));
}

ReceiveBuffer::ReceiveBuffer(
    scoped_refptr<net::IOBuffer> buffer,
    uint32_t size,
    const base::Callback<void(int, device::serial::ReceiveError)>& callback)
    : buffer_(buffer), size_(size), callback_(callback) {}

ReceiveBuffer::~ReceiveBuffer() {}

char* ReceiveBuffer::GetData() {
  return buffer_->data();
}

uint32_t ReceiveBuffer::GetSize() {
  return size_;
}

void ReceiveBuffer::Done(uint32_t bytes_written) {
  callback_.Run(bytes_written, device::serial::RECEIVE_ERROR_NONE);
}

void ReceiveBuffer::DoneWithError(uint32_t bytes_written, int32_t error) {
  callback_.Run(bytes_written,
                static_cast<device::serial::ReceiveError>(error));
}

}  // namespace device