OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "extensions/browser/api/webcam_private/visca_webcam.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" |
| 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "extensions/browser/api/serial/serial_connection.h" |
| 11 |
| 12 using content::BrowserThread; |
| 13 |
| 14 namespace extensions { |
| 15 |
| 16 ViscaWebcam::ViscaWebcam(const std::string& path, |
| 17 const std::string& extension_id) |
| 18 : path_(path), |
| 19 extension_id_(extension_id) {} |
| 20 |
| 21 ViscaWebcam::~ViscaWebcam() {} |
| 22 |
| 23 void ViscaWebcam::Open(const OpenCallback& callback) { |
| 24 BrowserThread::PostTask( |
| 25 BrowserThread::IO, |
| 26 FROM_HERE, |
| 27 base::Bind(&ViscaWebcam::OpenOnIOThread, AsWeakPtr(), callback)); |
| 28 } |
| 29 |
| 30 void ViscaWebcam::OpenOnIOThread(const OpenCallback& callback) { |
| 31 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 32 |
| 33 core_api::serial::ConnectionOptions options; |
| 34 |
| 35 options.persistent.reset(new bool(false)); |
| 36 options.buffer_size.reset(new int(4096)); |
| 37 options.bitrate.reset(new int(9600)); |
| 38 options.cts_flow_control.reset(new bool(false)); |
| 39 options.receive_timeout.reset(new int(0)); |
| 40 options.send_timeout.reset(new int(0)); |
| 41 options.data_bits = core_api::serial::DATA_BITS_EIGHT; |
| 42 options.parity_bit = core_api::serial::PARITY_BIT_NO; |
| 43 options.stop_bits = core_api::serial::STOP_BITS_ONE; |
| 44 |
| 45 serial_connection_.reset(new SerialConnection(path_, extension_id_)); |
| 46 serial_connection_->Open(options, |
| 47 base::Bind(&ViscaWebcam::OnConnected, |
| 48 AsWeakPtr(), |
| 49 callback)); |
| 50 } |
| 51 |
| 52 void ViscaWebcam::OnConnected(const OpenCallback& callback, bool success) { |
| 53 if (!success) { |
| 54 BrowserThread::PostTask( |
| 55 BrowserThread::UI, |
| 56 FROM_HERE, |
| 57 base::Bind(callback, false)); |
| 58 return; |
| 59 } |
| 60 |
| 61 std::vector<char> data; |
| 62 // Clear All command = 0x88 0x01 0x00 0x01 0xFF |
| 63 data.push_back(0x88); |
| 64 data.push_back(0x01); |
| 65 data.push_back(0x00); |
| 66 data.push_back(0x01); |
| 67 data.push_back(0xFF); |
| 68 |
| 69 Send(data, base::Bind(&ViscaWebcam::OnClearAllComplete, AsWeakPtr(), |
| 70 callback)); |
| 71 ReceiveLoop(); |
| 72 |
| 73 // TODO: Send an addressing request when Clear All is complete. |
| 74 } |
| 75 |
| 76 void ViscaWebcam::OnClearAllComplete(const OpenCallback& callback, |
| 77 bool success, |
| 78 const std::vector<char>& data) { |
| 79 /* TODO: Re-add this code to check response. |
| 80 LOG(ERROR) << "SUCCESS: " << success; |
| 81 if (success) { |
| 82 // Expected response = 0x88 0x01 0x00 0x01 0xFF |
| 83 LOG(ERROR) << "size: " << data.size(); |
| 84 if (data.size() == 5) { |
| 85 success = (data[0] == 0x88 && |
| 86 data[1] == 0x01 && |
| 87 data[2] == 0x00 && |
| 88 data[3] == 0x01 && |
| 89 data[4] == 0xFF); |
| 90 } else { |
| 91 success = false; |
| 92 } |
| 93 } |
| 94 */ |
| 95 |
| 96 BrowserThread::PostTask( |
| 97 BrowserThread::UI, |
| 98 FROM_HERE, |
| 99 base::Bind(callback, success)); |
| 100 } |
| 101 |
| 102 void ViscaWebcam::Send(const std::vector<char>& data, |
| 103 const CommandCompleteCallback& callback) { |
| 104 serial_connection_->Send(data, base::Bind(&ViscaWebcam::OnSendComplete, |
| 105 AsWeakPtr(), callback)); |
| 106 } |
| 107 |
| 108 void ViscaWebcam::OnSendComplete(const CommandCompleteCallback& callback, |
| 109 int bytes_sent, |
| 110 core_api::serial::SendError error) { |
| 111 if (error == core_api::serial::SEND_ERROR_NONE) { |
| 112 // TODO: Pull bytes from receive buffer until a 0xFF is read. |
| 113 std::vector<char> data; |
| 114 callback.Run(true, data); |
| 115 } else { |
| 116 std::vector<char> data; |
| 117 callback.Run(false, data); |
| 118 } |
| 119 } |
| 120 |
| 121 void ViscaWebcam::ReceiveLoop() { |
| 122 serial_connection_->Receive(base::Bind(&ViscaWebcam::OnReceiveComplete, |
| 123 AsWeakPtr())); |
| 124 } |
| 125 |
| 126 void ViscaWebcam::OnReceiveComplete(const std::vector<char>& data, |
| 127 core_api::serial::ReceiveError error) { |
| 128 // TODO: Remove |
| 129 LOG(ERROR) << "size: " << data.size(); |
| 130 for (const auto& byte : data) { |
| 131 LOG(ERROR) << "Data: 0x" << std::hex << (int(byte) & 0xFF); |
| 132 } |
| 133 |
| 134 data_buffer_.insert(data_buffer_.end(), data.begin(), data.end()); |
| 135 |
| 136 if (error == core_api::serial::RECEIVE_ERROR_NONE) { |
| 137 base::MessageLoop::current()->PostTask( |
| 138 FROM_HERE, base::Bind(&ViscaWebcam::ReceiveLoop, AsWeakPtr())); |
| 139 } else { |
| 140 // TODO: Error handling. |
| 141 LOG(ERROR) << "ERROR: " << error; |
| 142 } |
| 143 } |
| 144 |
| 145 void ViscaWebcam::Reset(bool pan, bool tilt, bool zoom) { |
| 146 } |
| 147 |
| 148 bool ViscaWebcam::GetPan(int* value) { |
| 149 return false; |
| 150 } |
| 151 |
| 152 bool ViscaWebcam::GetTilt(int* value) { |
| 153 return false; |
| 154 } |
| 155 |
| 156 bool ViscaWebcam::GetZoom(int* value) { |
| 157 return false; |
| 158 } |
| 159 |
| 160 bool ViscaWebcam::SetPan(int value) { |
| 161 return false; |
| 162 } |
| 163 |
| 164 bool ViscaWebcam::SetTilt(int value) { |
| 165 return false; |
| 166 } |
| 167 |
| 168 bool ViscaWebcam::SetZoom(int value) { |
| 169 return false; |
| 170 } |
| 171 |
| 172 bool ViscaWebcam::SetPanDirection(PanDirection direction) { |
| 173 return false; |
| 174 } |
| 175 |
| 176 bool ViscaWebcam::SetTiltDirection(TiltDirection direction) { |
| 177 return false; |
| 178 } |
| 179 |
| 180 } // namespace extensions |
OLD | NEW |