Index: extensions/browser/api/webcam_private/visca_webcam.cc |
diff --git a/extensions/browser/api/webcam_private/visca_webcam.cc b/extensions/browser/api/webcam_private/visca_webcam.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..52ba338059a95dcd50f1b625b79f1cc59a415ba0 |
--- /dev/null |
+++ b/extensions/browser/api/webcam_private/visca_webcam.cc |
@@ -0,0 +1,180 @@ |
+// Copyright 2015 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 "extensions/browser/api/webcam_private/visca_webcam.h" |
+ |
+#include "base/bind.h" |
+#include "base/message_loop/message_loop.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "extensions/browser/api/serial/serial_connection.h" |
+ |
+using content::BrowserThread; |
+ |
+namespace extensions { |
+ |
+ViscaWebcam::ViscaWebcam(const std::string& path, |
+ const std::string& extension_id) |
+ : path_(path), |
+ extension_id_(extension_id) {} |
+ |
+ViscaWebcam::~ViscaWebcam() {} |
+ |
+void ViscaWebcam::Open(const OpenCallback& callback) { |
+ BrowserThread::PostTask( |
+ BrowserThread::IO, |
+ FROM_HERE, |
+ base::Bind(&ViscaWebcam::OpenOnIOThread, AsWeakPtr(), callback)); |
+} |
+ |
+void ViscaWebcam::OpenOnIOThread(const OpenCallback& callback) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ |
+ core_api::serial::ConnectionOptions options; |
+ |
+ options.persistent.reset(new bool(false)); |
+ options.buffer_size.reset(new int(4096)); |
+ options.bitrate.reset(new int(9600)); |
+ options.cts_flow_control.reset(new bool(false)); |
+ options.receive_timeout.reset(new int(0)); |
+ options.send_timeout.reset(new int(0)); |
+ options.data_bits = core_api::serial::DATA_BITS_EIGHT; |
+ options.parity_bit = core_api::serial::PARITY_BIT_NO; |
+ options.stop_bits = core_api::serial::STOP_BITS_ONE; |
+ |
+ serial_connection_.reset(new SerialConnection(path_, extension_id_)); |
+ serial_connection_->Open(options, |
+ base::Bind(&ViscaWebcam::OnConnected, |
+ AsWeakPtr(), |
+ callback)); |
+} |
+ |
+void ViscaWebcam::OnConnected(const OpenCallback& callback, bool success) { |
+ if (!success) { |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, |
+ FROM_HERE, |
+ base::Bind(callback, false)); |
+ return; |
+ } |
+ |
+ std::vector<char> data; |
+ // Clear All command = 0x88 0x01 0x00 0x01 0xFF |
+ data.push_back(0x88); |
+ data.push_back(0x01); |
+ data.push_back(0x00); |
+ data.push_back(0x01); |
+ data.push_back(0xFF); |
+ |
+ Send(data, base::Bind(&ViscaWebcam::OnClearAllComplete, AsWeakPtr(), |
+ callback)); |
+ ReceiveLoop(); |
+ |
+ // TODO: Send an addressing request when Clear All is complete. |
+} |
+ |
+void ViscaWebcam::OnClearAllComplete(const OpenCallback& callback, |
+ bool success, |
+ const std::vector<char>& data) { |
+ /* TODO: Re-add this code to check response. |
+ LOG(ERROR) << "SUCCESS: " << success; |
+ if (success) { |
+ // Expected response = 0x88 0x01 0x00 0x01 0xFF |
+ LOG(ERROR) << "size: " << data.size(); |
+ if (data.size() == 5) { |
+ success = (data[0] == 0x88 && |
+ data[1] == 0x01 && |
+ data[2] == 0x00 && |
+ data[3] == 0x01 && |
+ data[4] == 0xFF); |
+ } else { |
+ success = false; |
+ } |
+ } |
+ */ |
+ |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, |
+ FROM_HERE, |
+ base::Bind(callback, success)); |
+} |
+ |
+void ViscaWebcam::Send(const std::vector<char>& data, |
+ const CommandCompleteCallback& callback) { |
+ serial_connection_->Send(data, base::Bind(&ViscaWebcam::OnSendComplete, |
+ AsWeakPtr(), callback)); |
+} |
+ |
+void ViscaWebcam::OnSendComplete(const CommandCompleteCallback& callback, |
+ int bytes_sent, |
+ core_api::serial::SendError error) { |
+ if (error == core_api::serial::SEND_ERROR_NONE) { |
+ // TODO: Pull bytes from receive buffer until a 0xFF is read. |
+ std::vector<char> data; |
+ callback.Run(true, data); |
+ } else { |
+ std::vector<char> data; |
+ callback.Run(false, data); |
+ } |
+} |
+ |
+void ViscaWebcam::ReceiveLoop() { |
+ serial_connection_->Receive(base::Bind(&ViscaWebcam::OnReceiveComplete, |
+ AsWeakPtr())); |
+} |
+ |
+void ViscaWebcam::OnReceiveComplete(const std::vector<char>& data, |
+ core_api::serial::ReceiveError error) { |
+ // TODO: Remove |
+ LOG(ERROR) << "size: " << data.size(); |
+ for (const auto& byte : data) { |
+ LOG(ERROR) << "Data: 0x" << std::hex << (int(byte) & 0xFF); |
+ } |
+ |
+ data_buffer_.insert(data_buffer_.end(), data.begin(), data.end()); |
+ |
+ if (error == core_api::serial::RECEIVE_ERROR_NONE) { |
+ base::MessageLoop::current()->PostTask( |
+ FROM_HERE, base::Bind(&ViscaWebcam::ReceiveLoop, AsWeakPtr())); |
+ } else { |
+ // TODO: Error handling. |
+ LOG(ERROR) << "ERROR: " << error; |
+ } |
+} |
+ |
+void ViscaWebcam::Reset(bool pan, bool tilt, bool zoom) { |
+} |
+ |
+bool ViscaWebcam::GetPan(int* value) { |
+ return false; |
+} |
+ |
+bool ViscaWebcam::GetTilt(int* value) { |
+ return false; |
+} |
+ |
+bool ViscaWebcam::GetZoom(int* value) { |
+ return false; |
+} |
+ |
+bool ViscaWebcam::SetPan(int value) { |
+ return false; |
+} |
+ |
+bool ViscaWebcam::SetTilt(int value) { |
+ return false; |
+} |
+ |
+bool ViscaWebcam::SetZoom(int value) { |
+ return false; |
+} |
+ |
+bool ViscaWebcam::SetPanDirection(PanDirection direction) { |
+ return false; |
+} |
+ |
+bool ViscaWebcam::SetTiltDirection(TiltDirection direction) { |
+ return false; |
+} |
+ |
+} // namespace extensions |