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 #pragma once
17 
18 #include <aidl/android/hardware/contexthub/ContextHubMessage.h>
19 #include <aidl/android/hardware/contexthub/NanoappBinary.h>
20 #include <optional>
21 #include <string>
22 
23 #include "chre/util/array_queue.h"
24 #include "chre/util/non_copyable.h"
25 #include "chre_host/generated/host_messages_generated.h"
26 
27 namespace aidl::android::hardware::contexthub {
28 
29 /**
30  * Logs HAL events.
31  *
32  * The logged events are store in a fixed size queue. When the number of logged
33  * events exceed the size of the queue, older events are deleted.
34  */
35 class EventLogger {
36  public:
37   /** Maximum number of load and unload events to store. */
38   static constexpr int kMaxNanoappEvents = 20;
39   /** Maximum number of Context Hub restart events to store. */
40   static constexpr int kMaxRestartEvents = 20;
41   /** Maximum number of message events to store. */
42   static constexpr int kMaxMessageEvents = 20;
43 
44   void logNanoappLoad(uint64_t appId, size_t appSize, uint32_t appVersion,
45                       bool success);
46 
47   void logNanoappUnload(int64_t appId, bool success);
48 
49   void logContextHubRestart();
50 
51   void logMessageToNanoapp(const ContextHubMessage &message, bool success);
52 
53   void logMessageFromNanoapp(const ::chre::fbs::NanoappMessageT &message);
54 
55   void logMessageFromNanoapp(const ContextHubMessage &message);
56 
57   /** Returns a textual representation of the logged events. */
58   std::string dump() const;
59 
60  protected:
61   struct NanoappLoad {
62     int64_t timestampMs;
63     int64_t id;
64     int32_t version;
65     size_t sizeBytes;
66     bool success;
67   };
68 
69   struct NanoappUnload {
70     int64_t timestampMs;
71     int64_t id;
72     bool success;
73   };
74 
75   struct NanoappMessage {
76     int64_t timestampMs;
77     int64_t id;
78     size_t sizeBytes;
79     bool success;
80   };
81 
82   ::chre::ArrayQueue<NanoappLoad, kMaxNanoappEvents> mNanoappLoads;
83   ::chre::ArrayQueue<NanoappUnload, kMaxNanoappEvents> mNanoappUnloads;
84   ::chre::ArrayQueue<int64_t, kMaxRestartEvents> mContextHubRestarts;
85   ::chre::ArrayQueue<NanoappMessage, kMaxMessageEvents> mMsgToNanoapp;
86   ::chre::ArrayQueue<NanoappMessage, kMaxMessageEvents> mMsgFromNanoapp;
87 
88   /**
89    * Current time in milliseconds.
90    * Override the current time when a value is provided.
91    * Used for tests.
92    */
93   std::optional<int64_t> mNowMs;
94 
95  private:
96   /** Protects concurrent reads and writes to the queue. */
97   mutable std::mutex mQueuesMutex;
98 
99   /** Returns the current time in milliseconds */
100   int64_t getTimeMs() const;
101 };
102 
103 }  // namespace aidl::android::hardware::contexthub
104