Home | History | Annotate | Download | only in core
      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, void *eventData,
     42         chreEventCompleteFunction *freeCallback,
     43         uint32_t senderInstanceId = kSystemInstanceId,
     44         uint32_t targetInstanceId = kBroadcastInstanceId);
     45 
     46   void incrementRefCount() {
     47     mRefCount++;
     48     CHRE_ASSERT(mRefCount != 0);
     49   }
     50 
     51   void decrementRefCount() {
     52     CHRE_ASSERT(mRefCount > 0);
     53     mRefCount--;
     54   }
     55 
     56   bool isUnreferenced() const {
     57     return (mRefCount == 0);
     58   }
     59 
     60   const uint16_t eventType;
     61   void * const eventData;
     62   chreEventCompleteFunction * const freeCallback;
     63   const uint32_t senderInstanceId;
     64   const uint32_t targetInstanceId;
     65 
     66  private:
     67   size_t mRefCount = 0;
     68 };
     69 
     70 }
     71 
     72 #endif  // CHRE_CORE_EVENT_H_
     73