| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/serial/serial_api.h" | 5 #include "chrome/browser/extensions/api/serial/serial_api.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/extensions/api/api_resource_controller.h" | 10 #include "chrome/browser/extensions/api/api_resource_controller.h" |
| 11 #include "chrome/browser/extensions/api/serial/serial_connection.h" | 11 #include "chrome/browser/extensions/api/serial/serial_connection.h" |
| 12 | 12 |
| 13 namespace extensions { | 13 namespace extensions { |
| 14 | 14 |
| 15 const char kConnectionIdKey[] = "connectionId"; | 15 const char kConnectionIdKey[] = "connectionId"; |
| 16 const char kMessageKey[] = "message"; | 16 const char kDataKey[] = "data"; |
| 17 const char kBytesReadKey[] = "bytesRead"; | 17 const char kBytesReadKey[] = "bytesRead"; |
| 18 const char kBytesWrittenKey[] = "bytesWritten"; | 18 const char kBytesWrittenKey[] = "bytesWritten"; |
| 19 | 19 |
| 20 SerialOpenFunction::SerialOpenFunction() | 20 SerialOpenFunction::SerialOpenFunction() |
| 21 : src_id_(-1) { | 21 : src_id_(-1) { |
| 22 } | 22 } |
| 23 | 23 |
| 24 bool SerialOpenFunction::Prepare() { | 24 bool SerialOpenFunction::Prepare() { |
| 25 size_t argument_position = 0; | 25 size_t argument_position = 0; |
| 26 EXTENSION_FUNCTION_VALIDATE(args_->GetString(argument_position++, &port_)); | 26 EXTENSION_FUNCTION_VALIDATE(args_->GetString(argument_position++, &port_)); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 return true; | 74 return true; |
| 75 } | 75 } |
| 76 | 76 |
| 77 bool SerialReadFunction::Prepare() { | 77 bool SerialReadFunction::Prepare() { |
| 78 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &connection_id_)); | 78 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &connection_id_)); |
| 79 return true; | 79 return true; |
| 80 } | 80 } |
| 81 | 81 |
| 82 void SerialReadFunction::Work() { | 82 void SerialReadFunction::Work() { |
| 83 int bytes_read = -1; | 83 int bytes_read = -1; |
| 84 std::string message; | 84 std::string data; |
| 85 SerialConnection* serial_connection = | 85 SerialConnection* serial_connection = |
| 86 controller()->GetSerialConnection(connection_id_); | 86 controller()->GetSerialConnection(connection_id_); |
| 87 if (serial_connection) { | 87 if (serial_connection) { |
| 88 unsigned char byte = '\0'; | 88 unsigned char byte = '\0'; |
| 89 bytes_read = serial_connection->Read(&byte); | 89 bytes_read = serial_connection->Read(&byte); |
| 90 if (bytes_read == 1) | 90 if (bytes_read == 1) |
| 91 message = byte; | 91 data = byte; |
| 92 } | 92 } |
| 93 | 93 |
| 94 DictionaryValue* result = new DictionaryValue(); | 94 DictionaryValue* result = new DictionaryValue(); |
| 95 result->SetInteger(kBytesReadKey, bytes_read); | 95 result->SetInteger(kBytesReadKey, bytes_read); |
| 96 result->SetString(kMessageKey, message); | 96 result->SetString(kDataKey, data); |
| 97 result_.reset(result); | 97 result_.reset(result); |
| 98 } | 98 } |
| 99 | 99 |
| 100 bool SerialReadFunction::Respond() { | 100 bool SerialReadFunction::Respond() { |
| 101 return true; | 101 return true; |
| 102 } | 102 } |
| 103 | 103 |
| 104 bool SerialWriteFunction::Prepare() { | 104 bool SerialWriteFunction::Prepare() { |
| 105 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &connection_id_)); | 105 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &connection_id_)); |
| 106 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &data_)); | 106 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &data_)); |
| 107 return true; | 107 return true; |
| 108 } | 108 } |
| 109 | 109 |
| 110 void SerialWriteFunction::Work() { | 110 void SerialWriteFunction::Work() { |
| 111 int bytes_written = -1; | 111 int bytes_written = -1; |
| 112 SerialConnection* serial_connection = | 112 SerialConnection* serial_connection = |
| 113 controller()->GetSerialConnection(connection_id_); | 113 controller()->GetSerialConnection(connection_id_); |
| 114 if (serial_connection) | 114 if (serial_connection) |
| 115 bytes_written = serial_connection->Write(data_); | 115 bytes_written = serial_connection->Write(data_); |
| 116 DictionaryValue* result = new DictionaryValue(); | 116 DictionaryValue* result = new DictionaryValue(); |
| 117 result->SetInteger(kBytesWrittenKey, bytes_written); | 117 result->SetInteger(kBytesWrittenKey, bytes_written); |
| 118 result_.reset(result); | 118 result_.reset(result); |
| 119 } | 119 } |
| 120 | 120 |
| 121 bool SerialWriteFunction::Respond() { | 121 bool SerialWriteFunction::Respond() { |
| 122 return true; | 122 return true; |
| 123 } | 123 } |
| 124 | 124 |
| 125 } // namespace extensions | 125 } // namespace extensions |
| OLD | NEW |