1 /* 2 * Copyright 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #define LOG_TAG "connection" 18 19 #include "connection.h" 20 21 #include "base/logging.h" 22 23 #include "osi/include/log.h" 24 25 using std::vector; 26 27 namespace test_vendor_lib { 28 29 // Add an action from the controller. AddAction(const TaskCallback & task)30void Connection::AddAction(const TaskCallback& task) { actions_.push(task); } 31 32 // Model the quality of the downstream connection SendToDevice()33void Connection::SendToDevice() { 34 while (actions_.size() > 0) { 35 // Execute the first action 36 actions_.front()(); 37 actions_.pop(); 38 } 39 } 40 41 // Add a message from the device to the controller. AddMessage(const vector<uint8_t> & data)42void Connection::AddMessage(const vector<uint8_t>& data) { 43 messages_.push(data); 44 } 45 46 // Model the quality of the upstream connection ReceiveFromDevice(vector<uint8_t> & data)47bool Connection::ReceiveFromDevice(vector<uint8_t>& data) { 48 if (messages_.size() > 0) { 49 data = messages_.front(); 50 messages_.pop(); 51 52 return true; 53 } 54 return false; 55 } 56 ToString()57const std::string Connection::ToString() { 58 return "connection " + std::to_string(handle_) + " to " + dev_->ToString(); 59 } 60 61 } // namespace test_vendor_lib 62