Home | History | Annotate | Download | only in inc
      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_H_
     18 #define _CHRE_H_
     19 
     20 /**
     21  * This header file includes all the headers which combine to fully defined
     22  * the interface for the Context Hub Runtime Environment (CHRE).  This is the
     23  * environment in which a nanoapp runs.
     24  *
     25  * This interface is of interest to both implementors of CHREs and
     26  * authors of nanoapps.  The API documentation attempts to address concerns
     27  * of both.
     28  *
     29  * See individual header files for API specific, and general comments below
     30  * for overall platform information.
     31  */
     32 
     33 #include "chre_event.h"
     34 #include "chre_nanoapp.h"
     35 #include "chre_re.h"
     36 #include "chre_sensor.h"
     37 #include "chre_version.h"
     38 
     39 
     40 /**
     41  * Entry points.
     42  *
     43  * The following entry points are required to be handled by the CHRE
     44  * implementation, and the functions must all be implemented by nanoapps.
     45  * o nanoappStart function (see chre_nanoapp.h)
     46  * o nanoappHandleEvent function (see chre_nanoapp.h)
     47  * o nanoappEnd function (see chre_nanoapp.h)
     48  * o bss section zeroed out (prior to nanoappStart)
     49  * o static variables initialized (prior to nanoappStart)
     50  * o global C++ constructors called (prior to nanoappStart)
     51  * o global C++ destructors called (after nanoappEnd)
     52  */
     53 
     54 
     55 /**
     56  * Threading model.
     57  *
     58  * A CHRE implementation is free to chose among many different
     59  * threading models, including a single threaded system or a multi-threaded
     60  * system with preemption.  The current platform definition is agnostic to this
     61  * underlying choice [1].
     62  *
     63  * However, the Platform does require that all nanoapps are treated as
     64  * non-reentrant.  That is, any of the functions of the nanoapp, including
     65  * the entry points defined above and the memory freeing callbacks defined
     66  * below, cannot be invoked by the CHRE if a previous invocation
     67  * hasn't completed.  Note this means no nanoapp function can be invoked
     68  * from an interrupt context.
     69  *
     70  * For example, if a nanoapp is currently in nanoappHandleEvent(), the CHRE is
     71  * not allowed to call nanoappHandleEvent() again, or to call a memory freeing
     72  * callback.  Similarly, if a nanoapp is currently in a memory freeing
     73  * callback, the CHRE is not allowed to call nanoappHandleEvent(), or invoke
     74  * another memory freeing callback.
     75  *
     76  * For a nanoapp author, this means no thought needs to be given to
     77  * synchronization issues with global objects, as they will, by definition,
     78  * only be accessed by a single thread at once.
     79  *
     80  *
     81  * [1] Note to CHRE implementors: A future version of the CHRE platform may
     82  * require multi-threading with preemption.  This is mentioned as a heads up,
     83  * and to allow implementors deciding between implementation approaches to
     84  * make the most informed choice.
     85  */
     86 
     87 /**
     88  * Notes on timing.
     89  *
     90  * Nanoapps should expect to be running on a highly constrained system, with
     91  * little memory and little CPU.  Any single nanoapp should expect to
     92  * be one of several nanoapps on the system, which also share the CPU with the
     93  * CHRE and possibly other services as well.
     94  *
     95  * Thus, a nanoapp needs to be efficient in its memory and CPU usage.
     96  * Also, as noted in the Threading Model section, a CHRE implementation may
     97  * be single threaded.  As a result, all methods invoked in a nanoapp
     98  * (like nanoappStart, nanoappHandleEvent, memory free callbacks, etc.)
     99  * must run "quickly".  "Quickly" is difficult to define, as there is a
    100  * diversity of Context Hub hardware.  For Android N, there is no firm
    101  * definition of "quickly", but expect this term to gain definition in
    102  * future releases as we get feedback from partners.
    103  *
    104  * In order to write a nanoapp that will be able to adopt to future
    105  * stricter notions of "quickly", all nanoapp methods should be written so
    106  * they execute in a small amount of time.  Some nanoapps may have the need
    107  * to occasionally perform a large block of calculations, which may seem
    108  * to violate this.  The recommended approach in this case is to
    109  * split up the large block of calculations into smaller batches.  In one
    110  * call into the nanoapp, the nanoapp can perform the first batch, and then
    111  * send an event (chreSendEvent()) to itself indicating which batch should be
    112  * done next.  This will allow the nanoapp to perform the entire calculation
    113  * over time, without monopolizing system resources.
    114  */
    115 
    116 /**
    117  * CHRE and Nanoapp compatibility.
    118  *
    119  * The Android N release introduces the first version of this API.
    120  * It is anticipated that there will be a lot of feedback from
    121  * Android partners on this initial API.  To allow more flexibility
    122  * in addressing that feedback, there is no plan to assure
    123  * binary compatibility between the Android N and Android O CHRE
    124  * implementations and nanoapps.
    125  *
    126  * That is, a nanoapp built with the Android O version of this
    127  * API should not expect to run on a CHRE built with
    128  * the Android N API.  Similarly, a nanoapp build with the
    129  * Android N API should not expect to run on a CHRE
    130  * build with the Android O API.  Such a nanoapp will need to
    131  * recompiled with the appropriate API in order to work.
    132  */
    133 
    134 #endif  /* _CHRE_H_ */
    135 
    136