| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device/serial/serial_io_handler.h" | 5 #include "device/serial/serial_io_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 | 11 |
| 12 namespace device { | 12 namespace device { |
| 13 | 13 |
| 14 SerialIoHandler::SerialIoHandler( | 14 SerialIoHandler::SerialIoHandler( |
| 15 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop) | 15 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop) |
| 16 : pending_read_buffer_len_(0), | 16 : file_thread_message_loop_(file_thread_message_loop) { |
| 17 pending_write_buffer_len_(0), | |
| 18 file_thread_message_loop_(file_thread_message_loop) { | |
| 19 } | 17 } |
| 20 | 18 |
| 21 SerialIoHandler::~SerialIoHandler() { | 19 SerialIoHandler::~SerialIoHandler() { |
| 22 DCHECK(CalledOnValidThread()); | 20 DCHECK(CalledOnValidThread()); |
| 23 Close(); | 21 Close(); |
| 24 } | 22 } |
| 25 | 23 |
| 26 void SerialIoHandler::Initialize(const ReadCompleteCallback& read_callback, | |
| 27 const WriteCompleteCallback& write_callback) { | |
| 28 DCHECK(CalledOnValidThread()); | |
| 29 read_complete_ = read_callback; | |
| 30 write_complete_ = write_callback; | |
| 31 } | |
| 32 | |
| 33 void SerialIoHandler::Open(const std::string& port, | 24 void SerialIoHandler::Open(const std::string& port, |
| 34 const OpenCompleteCallback& callback) { | 25 const OpenCompleteCallback& callback) { |
| 35 DCHECK(CalledOnValidThread()); | 26 DCHECK(CalledOnValidThread()); |
| 36 DCHECK(open_complete_.is_null()); | 27 DCHECK(open_complete_.is_null()); |
| 37 open_complete_ = callback; | 28 open_complete_ = callback; |
| 38 DCHECK(file_thread_message_loop_); | 29 DCHECK(file_thread_message_loop_); |
| 39 file_thread_message_loop_->PostTask( | 30 file_thread_message_loop_->PostTask( |
| 40 FROM_HERE, | 31 FROM_HERE, |
| 41 base::Bind(&SerialIoHandler::StartOpen, | 32 base::Bind(&SerialIoHandler::StartOpen, |
| 42 this, | 33 this, |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 file_thread_message_loop_->PostTask( | 85 file_thread_message_loop_->PostTask( |
| 95 FROM_HERE, base::Bind(&SerialIoHandler::DoClose, Passed(file_.Pass()))); | 86 FROM_HERE, base::Bind(&SerialIoHandler::DoClose, Passed(file_.Pass()))); |
| 96 } | 87 } |
| 97 } | 88 } |
| 98 | 89 |
| 99 // static | 90 // static |
| 100 void SerialIoHandler::DoClose(base::File port) { | 91 void SerialIoHandler::DoClose(base::File port) { |
| 101 // port closed by destructor. | 92 // port closed by destructor. |
| 102 } | 93 } |
| 103 | 94 |
| 104 void SerialIoHandler::Read(int max_bytes) { | 95 void SerialIoHandler::Read(scoped_ptr<WritableBuffer> buffer) { |
| 105 DCHECK(CalledOnValidThread()); | 96 DCHECK(CalledOnValidThread()); |
| 106 DCHECK(!IsReadPending()); | 97 DCHECK(!IsReadPending()); |
| 107 pending_read_buffer_ = new net::IOBuffer(max_bytes); | 98 pending_read_buffer_ = buffer.Pass(); |
| 108 pending_read_buffer_len_ = max_bytes; | |
| 109 read_canceled_ = false; | 99 read_canceled_ = false; |
| 110 AddRef(); | 100 AddRef(); |
| 111 ReadImpl(); | 101 ReadImpl(); |
| 112 } | 102 } |
| 113 | 103 |
| 114 void SerialIoHandler::Write(const std::string& data) { | 104 void SerialIoHandler::Write(scoped_ptr<ReadOnlyBuffer> buffer) { |
| 115 DCHECK(CalledOnValidThread()); | 105 DCHECK(CalledOnValidThread()); |
| 116 DCHECK(!IsWritePending()); | 106 DCHECK(!IsWritePending()); |
| 117 int length = static_cast<int>(data.length()); | 107 pending_write_buffer_ = buffer.Pass(); |
| 118 pending_write_buffer_ = new net::IOBuffer(length); | |
| 119 pending_write_buffer_len_ = length; | |
| 120 memcpy(pending_write_buffer_->data(), data.data(), pending_write_buffer_len_); | |
| 121 write_canceled_ = false; | 108 write_canceled_ = false; |
| 122 AddRef(); | 109 AddRef(); |
| 123 WriteImpl(); | 110 WriteImpl(); |
| 124 } | 111 } |
| 125 | 112 |
| 126 void SerialIoHandler::ReadCompleted(int bytes_read, | 113 void SerialIoHandler::ReadCompleted(int bytes_read, |
| 127 serial::ReceiveError error) { | 114 serial::ReceiveError error) { |
| 128 DCHECK(CalledOnValidThread()); | 115 DCHECK(CalledOnValidThread()); |
| 129 DCHECK(IsReadPending()); | 116 DCHECK(IsReadPending()); |
| 130 read_complete_.Run(std::string(pending_read_buffer_->data(), bytes_read), | 117 if (error == serial::RECEIVE_ERROR_NONE) { |
| 131 error); | 118 pending_read_buffer_->Done(bytes_read); |
| 132 pending_read_buffer_ = NULL; | 119 } else { |
| 133 pending_read_buffer_len_ = 0; | 120 pending_read_buffer_->DoneWithError(bytes_read, error); |
| 121 } |
| 122 pending_read_buffer_.reset(); |
| 134 Release(); | 123 Release(); |
| 135 } | 124 } |
| 136 | 125 |
| 137 void SerialIoHandler::WriteCompleted(int bytes_written, | 126 void SerialIoHandler::WriteCompleted(int bytes_written, |
| 138 serial::SendError error) { | 127 serial::SendError error) { |
| 139 DCHECK(CalledOnValidThread()); | 128 DCHECK(CalledOnValidThread()); |
| 140 DCHECK(IsWritePending()); | 129 DCHECK(IsWritePending()); |
| 141 write_complete_.Run(bytes_written, error); | 130 if (error == serial::SEND_ERROR_NONE) { |
| 142 pending_write_buffer_ = NULL; | 131 pending_write_buffer_->Done(bytes_written); |
| 143 pending_write_buffer_len_ = 0; | 132 } else { |
| 133 pending_write_buffer_->DoneWithError(bytes_written, error); |
| 134 } |
| 135 pending_write_buffer_.reset(); |
| 144 Release(); | 136 Release(); |
| 145 } | 137 } |
| 146 | 138 |
| 147 bool SerialIoHandler::IsReadPending() const { | 139 bool SerialIoHandler::IsReadPending() const { |
| 148 DCHECK(CalledOnValidThread()); | 140 DCHECK(CalledOnValidThread()); |
| 149 return pending_read_buffer_ != NULL; | 141 return pending_read_buffer_ != NULL; |
| 150 } | 142 } |
| 151 | 143 |
| 152 bool SerialIoHandler::IsWritePending() const { | 144 bool SerialIoHandler::IsWritePending() const { |
| 153 DCHECK(CalledOnValidThread()); | 145 DCHECK(CalledOnValidThread()); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 180 } | 172 } |
| 181 | 173 |
| 182 void SerialIoHandler::QueueWriteCompleted(int bytes_written, | 174 void SerialIoHandler::QueueWriteCompleted(int bytes_written, |
| 183 serial::SendError error) { | 175 serial::SendError error) { |
| 184 base::MessageLoop::current()->PostTask( | 176 base::MessageLoop::current()->PostTask( |
| 185 FROM_HERE, | 177 FROM_HERE, |
| 186 base::Bind(&SerialIoHandler::WriteCompleted, this, bytes_written, error)); | 178 base::Bind(&SerialIoHandler::WriteCompleted, this, bytes_written, error)); |
| 187 } | 179 } |
| 188 | 180 |
| 189 } // namespace device | 181 } // namespace device |
| OLD | NEW |