1 /* 2 * Copyright (C) 2021 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 #pragma once 18 #include "common/libs/utils/vsock_connection.h" 19 20 #include <atomic> 21 #include <mutex> 22 #include <thread> 23 #include <unordered_map> 24 25 #include <json/json.h> 26 27 namespace cuttlefish { 28 namespace webrtc_streaming { 29 30 struct Light { 31 enum class Type { 32 BACKLIGHT = 0, 33 KEYBOARD, 34 BUTTONS, 35 BATTERY, 36 NOTIFICATIONS, 37 ATTENTION, 38 BLUETOOTH, 39 WIFI, 40 MICROPHONE, 41 CAMERA, 42 }; 43 44 unsigned int id; 45 unsigned int color; 46 Type light_type; 47 }; 48 49 class LightsObserver { 50 public: 51 LightsObserver(unsigned int port, unsigned int cid, bool vhost_user_vsock); 52 ~LightsObserver(); 53 54 LightsObserver(const LightsObserver& other) = delete; 55 LightsObserver& operator=(const LightsObserver& other) = delete; 56 57 bool Start(); 58 int Subscribe(std::function<bool(const Json::Value&)> lights_message_sender); 59 void Unsubscribe(int lights_message_sender_id); 60 61 private: 62 void Stop(); 63 void ReadServerMessages(); 64 // TODO(b/295543722): Move to a virtio_console transport instead. 65 VsockClientConnection cvd_connection_; 66 unsigned int cid_; 67 unsigned int port_; 68 bool vhost_user_vsock_; 69 std::thread connection_thread_; 70 std::atomic<bool> is_running_; 71 std::atomic<bool> session_active_; 72 std::unordered_map<unsigned int, Light> lights_state_; 73 74 std::mutex clients_lock_; 75 Json::Value cached_latest_update_; 76 std::unordered_map<int, std::function<bool(const Json::Value&)>> 77 client_message_senders_; 78 int last_client_channel_id_; 79 }; 80 81 } // namespace webrtc_streaming 82 } // namespace cuttlefish 83