Android-cuttlefish cvd tool
lights_observer.h
Go to the documentation of this file.
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
19
20#include <atomic>
21#include <mutex>
22#include <thread>
23#include <unordered_map>
24
25#include <json/json.h>
26
27namespace cuttlefish {
28namespace webrtc_streaming {
29
30struct 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;
47};
48
50 public:
51 LightsObserver(unsigned int port, unsigned int cid, bool vhost_user_vsock);
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.
66 unsigned int cid_;
67 unsigned int port_;
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_;
76 std::unordered_map<int, std::function<bool(const Json::Value&)>>
79};
80
81} // namespace webrtc_streaming
82} // namespace cuttlefish
Definition: vsock_connection.h:72
Definition: lights_observer.h:49
LightsObserver & operator=(const LightsObserver &other)=delete
void ReadServerMessages()
Definition: lights_observer.cpp:81
void Unsubscribe(int lights_message_sender_id)
Definition: lights_observer.cpp:122
std::thread connection_thread_
Definition: lights_observer.h:69
int last_client_channel_id_
Definition: lights_observer.h:78
unsigned int cid_
Definition: lights_observer.h:66
LightsObserver(const LightsObserver &other)=delete
VsockClientConnection cvd_connection_
Definition: lights_observer.h:65
std::atomic< bool > session_active_
Definition: lights_observer.h:71
void Stop()
Definition: lights_observer.cpp:71
int Subscribe(std::function< bool(const Json::Value &)> lights_message_sender)
Definition: lights_observer.cpp:106
std::mutex clients_lock_
Definition: lights_observer.h:74
LightsObserver(unsigned int port, unsigned int cid, bool vhost_user_vsock)
Definition: lights_observer.cpp:27
std::unordered_map< int, std::function< bool(const Json::Value &)> > client_message_senders_
Definition: lights_observer.h:77
Json::Value cached_latest_update_
Definition: lights_observer.h:75
std::atomic< bool > is_running_
Definition: lights_observer.h:70
~LightsObserver()
Definition: lights_observer.cpp:36
bool Start()
Definition: lights_observer.cpp:38
bool vhost_user_vsock_
Definition: lights_observer.h:68
unsigned int port_
Definition: lights_observer.h:67
std::unordered_map< unsigned int, Light > lights_state_
Definition: lights_observer.h:72
Definition: alloc_utils.cpp:23
Definition: lights_observer.h:30
unsigned int color
Definition: lights_observer.h:45
Type light_type
Definition: lights_observer.h:46
unsigned int id
Definition: lights_observer.h:44
Type
Definition: lights_observer.h:31