1 /*
2  * Copyright (C) 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 #ifndef CHRE_CORE_EVENT_H_
18 #define CHRE_CORE_EVENT_H_
19 
20 #include "chre_api/chre/event.h"
21 #include "chre/platform/assert.h"
22 #include "chre/util/non_copyable.h"
23 
24 #include <cstdint>
25 
26 namespace chre {
27 
28 //! Instance ID used for events sent by the system
29 constexpr uint32_t kSystemInstanceId = 0;
30 
31 //! Target instance ID used to deliver a message to all nanoapps registered for
32 //! the event
33 constexpr uint32_t kBroadcastInstanceId = UINT32_MAX;
34 
35 //! This value can be used in a nanoapp's own instance ID to indicate that the
36 //! ID is invalid/not assigned yet
37 constexpr uint32_t kInvalidInstanceId = kBroadcastInstanceId;
38 
39 class Event : public NonCopyable {
40  public:
41   Event(uint16_t eventType_, uint16_t receivedTimeMillis_, void *eventData_,
42         chreEventCompleteFunction *freeCallback_,
43         uint32_t senderInstanceId = kSystemInstanceId,
44         uint32_t targetInstanceId = kBroadcastInstanceId);
45 
incrementRefCount()46   void incrementRefCount() {
47     mRefCount++;
48     CHRE_ASSERT(mRefCount != 0);
49   }
50 
decrementRefCount()51   void decrementRefCount() {
52     CHRE_ASSERT(mRefCount > 0);
53     mRefCount--;
54   }
55 
isUnreferenced()56   bool isUnreferenced() const {
57     return (mRefCount == 0);
58   }
59 
60   const uint16_t eventType;
61   //! This value can serve as a proxy for how fast CHRE is processing events
62   //! in its queue by substracting the newest event timestamp by the oldest one.
63   const uint16_t receivedTimeMillis;
64   void * const eventData;
65   chreEventCompleteFunction * const freeCallback;
66   const uint32_t senderInstanceId;
67   const uint32_t targetInstanceId;
68 
69  private:
70   size_t mRefCount = 0;
71 };
72 
73 }
74 
75 #endif  // CHRE_CORE_EVENT_H_
76