1 //
2 // Copyright (C) 2022 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 #include <android-base/logging.h>
17 
18 #include "host/commands/metrics/events.h"
19 #include "host/commands/metrics/host_receiver.h"
20 #include "host/commands/metrics/metrics_configs.h"
21 #include "host/commands/metrics/metrics_defs.h"
22 #include "host/commands/metrics/proto/cf_metrics_protos.h"
23 #include "host/libs/metrics/metrics_receiver.h"
24 #include "host/libs/msg_queue/msg_queue.h"
25 
26 using cuttlefish::MetricsExitCodes;
27 
28 namespace cuttlefish {
29 
MetricsHostReceiver(bool is_metrics_enabled)30 MetricsHostReceiver::MetricsHostReceiver(bool is_metrics_enabled)
31     : is_metrics_enabled_(is_metrics_enabled) {}
32 
~MetricsHostReceiver()33 MetricsHostReceiver::~MetricsHostReceiver() {}
34 
ServerLoop()35 void MetricsHostReceiver::ServerLoop() {
36   auto msg_queue = SysVMessageQueue::Create(metrics_queue_name_);
37   if (msg_queue == NULL) {
38     LOG(FATAL) << "create: failed to create" << metrics_queue_name_;
39   }
40 
41   struct msg_buffer msg = {0, {0}};
42   while (true) {
43     int rc = msg_queue->Receive(&msg, MAX_MSG_SIZE, 1, true);
44     if (rc == -1) {
45       LOG(FATAL) << "receive: failed to receive any messages";
46     }
47 
48     std::string text(msg.mesg_text);
49     LOG(INFO) << "Metrics host received: " << text;
50 
51     // Process the received message
52     ProcessMessage(text);
53 
54     sleep(1);
55   }
56 }
57 
Join()58 void MetricsHostReceiver::Join() { thread_.join(); }
59 
Initialize(const std::string & metrics_queue_name)60 bool MetricsHostReceiver::Initialize(const std::string& metrics_queue_name) {
61   metrics_queue_name_ = metrics_queue_name;
62   if (!is_metrics_enabled_) {
63     LOG(ERROR) << "init: metrics not enabled";
64     return false;
65   }
66 
67   // Start the server loop in a separate thread
68   thread_ = std::thread(&MetricsHostReceiver::ServerLoop, this);
69   return true;
70 }
71 
ProcessMessage(const std::string & text)72 void MetricsHostReceiver::ProcessMessage(const std::string& text) {
73   auto hostDev = cuttlefish::CuttlefishLogEvent::CUTTLEFISH_DEVICE_TYPE_HOST;
74 
75   int rc = MetricsExitCodes::kSuccess;
76 
77   if (text == "VMStart") {
78     rc = Clearcut::SendVMStart(hostDev);
79   } else if (text == "VMStop") {
80     rc = Clearcut::SendVMStop(hostDev);
81   } else if (text == "DeviceBoot") {
82     rc = Clearcut::SendDeviceBoot(hostDev);
83   } else if (text == "LockScreen") {
84     rc = Clearcut::SendLockScreen(hostDev);
85   } else {
86     LOG(ERROR) << "Message not recognized: " << text;
87     rc = MetricsExitCodes::kMetricsError;
88   }
89 
90   if (rc != MetricsExitCodes::kSuccess) {
91     LOG(ERROR) << "Message failed to send to ClearCut: " << text;
92   }
93 }
94 
95 }  // namespace cuttlefish
96