Skip to content

Commit aaf0c6e

Browse files
yperessMaureenHelm
authored andcommitted
modules: chre
Add initial build rules for CHRE. This change includes a Kconfig and CMakeLists.txt to begin compiling code from the CHRE module. Additional files are included to bridge the APIs from CHRE's to Zephyr's. These can be found in modules/chre/include and modules/chre/src. Additionally, add a sample to make sure that the module builds. It can be built via: ``` $ west build -b native_posix -p=always samples/application_development/chre ``` Signed-off-by: Yuval Peress <[email protected]>
1 parent 8da60c3 commit aaf0c6e

File tree

9 files changed

+223
-0
lines changed

9 files changed

+223
-0
lines changed

modules/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ comment "FFF module not available."
8484
comment "zcbor module not available."
8585
depends on !ZEPHYR_ZCBOR_MODULE
8686

87+
comment "CHRE module not available."
88+
depends on !ZEPHYR_CHRE_MODULE
89+
8790
# This ensures that symbols are available in Kconfig for dependency checking
8891
# and referencing, while keeping the settings themselves unavailable when the
8992
# modules are not present in the workspace

samples/modules/chre/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) Google LLC
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
6+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
7+
project(chre_sample)
8+
9+
FILE(GLOB app_sources src/*.cpp)
10+
target_sources(app PRIVATE ${app_sources})
11+
target_include_directories(app PRIVATE include)

samples/modules/chre/README.rst

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
Android's Context Hub Runtime Environment (CHRE)
2+
################################################
3+
4+
Android's context hub enables the use of nanoapps. A single nanoapp has 3 entry points seen in
5+
`chre_api/chre/nanoapp.h`_:
6+
7+
* A ``nanoappStart`` function used to notify the nanoapp that it is now active.
8+
* A ``nanoappHandleEvent`` function used to notify the nanoapp tha an event of interest took place.
9+
* A ``nanoappEnd`` function used to notify the nanoapp that it is now deactivated.
10+
11+
The CHRE connects to several frameworks called Platform Abstraction Layers (PAL)s. Note that
12+
currently, none of these are implemented for Zephyr, but they are scheduled to be added. These
13+
frameworks include:
14+
15+
#. *Audio* - a framework allowing nanoapps to get audio events. See `pal/audio.h`_ for API details.
16+
#. *GNSS* - a framework allowing nanoapps to manage location and measurement sessions. See
17+
`pal/gnss.h`_ for API details.
18+
#. *Sensor* - a framework allowing nanoapps to request changes to sensors configuration get
19+
data/bias events. See `pal/sensor.h`_ for API details.
20+
#. *System* - a framework allowing nanoapps to make common system requests such as logging, clock,
21+
and some basic memory allocation/deallocation. See `pal/system.h`_ for API details.
22+
#. *WiFi* - a framework allowing nanoapps to interact with the on board WiFi. See `pal/wifi.h`_ for
23+
API details.
24+
#. *WWAN* - a framework allowing nanoapps to interact with the WWAN module such as getting the
25+
current capabilities and info. See `pal/wwan.h`_ for API details.
26+
27+
Building and expectations
28+
=========================
29+
30+
To build the sample use the following west command:
31+
32+
.. zephyr-app-commands::
33+
:zephyr-app: samples/modules/chre
34+
:board: native_posix
35+
:goals: build
36+
37+
Once built and run, the sample application should:
38+
39+
#. Print a hello message
40+
#. Notify that the event loop started via an ``inf`` level log
41+
#. Notify that a nanoapp was started and assigned an instance/app ID of 1 via a ``dbg`` level log
42+
#. Print a message saying that the nanoapp's start callback was called
43+
#. Send an event of type ``1`` and no data to the nanoapp
44+
#. Notify that the event was processed
45+
#. Call the ``nanoappEnd`` function of the nanoapp
46+
#. Print a message notifying that it's not possible to remove a system level nanoapp
47+
#. Exit the event loop
48+
49+
Roadmap
50+
=======
51+
52+
#. Add an implementation of the `pal/sensor.h`_ and `pal/system.h`_ to Zephyr. These will be
53+
standalone modules that can be used independently of CHRE, but when ``CONFIG_CHRE`` is enabled
54+
will also provide an implementation of ``chrePalSensorGetApi()`` and ``struct chrePalSystemApi``.
55+
#. Add a directory ``chre/nanoapps`` which will host various nanoapps to be used by the Zephyr
56+
community. These should each have their own Kconfig to enable them and set the appropriate
57+
dependencies. The first nanoapp will be a lid angle calculator which will use 2 accelerometers.
58+
#. Update the ``overlay.dts`` of this sample application to include 2 emulated accelerometers and
59+
configure them to return scripted data.
60+
#. Run this sample application and watch the nanoapp provide lid angle calculations based on 2
61+
accelerometers provided by the sensors PAL framework.
62+
63+
.. _`chre_api/chre/nanoapp.h`: https://cs.android.com/android/platform/superproject/+/master:system/chre/chre_api/include/chre_api/chre/nanoapp.h;drc=7c60a553288d63e6e3370d679803da46dac723a4
64+
.. _`pal/audio.h`: https://cs.android.com/android/platform/superproject/+/master:system/chre/pal/include/chre/pal/audio.h;l=69;drc=6ca547ad175f80ce9f09f5b7b14fcc6f14565f5c
65+
.. _`pal/gnss.h`: https://cs.android.com/android/platform/superproject/+/master:system/chre/pal/include/chre/pal/gnss.h;l=152;drc=830255234157cc7afe5201dca19a7fb71ea850fe
66+
.. _`pal/sensor.h`: https://cs.android.com/android/platform/superproject/+/master:system/chre/pal/include/chre/pal/sensor.h;l=51;drc=23207906add05054a94dfab41b95bcfc39bedfe4
67+
.. _`pal/system.h`: https://cs.android.com/android/platform/superproject/+/master:system/chre/pal/include/chre/pal/system.h;l=49;drc=6ca547ad175f80ce9f09f5b7b14fcc6f14565f5c
68+
.. _`pal/wifi.h`: https://cs.android.com/android/platform/superproject/+/master:system/chre/pal/include/chre/pal/wifi.h;l=153;drc=b673b80ed98e1b6e6360bee4ba98e2cd8f4e0935
69+
.. _`pal/wwan.h`: https://cs.android.com/android/platform/superproject/+/master:system/chre/pal/include/chre/pal/wwan.h;l=69;drc=629361a803cb305c8575b41614e6e071e7141a03

samples/modules/chre/include/apps.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* Copyright (c) 2021 Google LLC
2+
* SPDX-License-Identifier: Apache-2.0
3+
*/
4+
5+
#ifndef SAMPLES_APPLICATION_DEVELOPMENT_CHRE_INCLUDE_APPS_H_
6+
#define SAMPLES_APPLICATION_DEVELOPMENT_CHRE_INCLUDE_APPS_H_
7+
8+
#include "chre/core/nanoapp.h"
9+
#include "chre/util/unique_ptr.h"
10+
11+
namespace chre {
12+
13+
UniquePtr<Nanoapp> initializeStaticNanoappEchoApp();
14+
15+
} /* namespace chre */
16+
17+
#endif /* SAMPLES_APPLICATION_DEVELOPMENT_CHRE_INCLUDE_APPS_H_ */

samples/modules/chre/prj.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CONFIG_CHRE=y
2+
CONFIG_LOG=y
3+
CONFIG_CHRE_LOG_LEVEL_DBG=y
4+
5+
CONFIG_CPLUSPLUS=y
6+
CONFIG_STD_CPP17=y
7+
CONFIG_LIB_CPLUSPLUS=y

samples/modules/chre/sample.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
sample:
2+
description: A simple echo app using Android's Context Hub Runtime
3+
Environment (CHRE).
4+
name: chre
5+
tests:
6+
sample.modules.chre:
7+
tags: introduction
8+
harness: console
9+
harness_config:
10+
type: multi_line
11+
ordered: false
12+
regex:
13+
- "Hello CHRE!"
14+
- "\\[.*\\] .*<inf> chre: EventLoop start.*"
15+
- "\\[.*\\] .*<dbg> chre: startNanoapp: Instance ID 1 assigned to app ID 0x0000000000000001.*"
16+
- "EchoApp::nanoappStart\\(\\)"
17+
- "EchoApp::nanoappHandleEvent\\(sender_instance_id=0, event_type=1, event_data@\\(nil\\)\\)"
18+
- "Event \\(1\\) complete!"
19+
- "EchoApp::nanoappEnd\\(\\)"
20+
- "\\[.*\\] .*<inf> chre: Exiting EventLoop.*"
21+
integration_platforms:
22+
- native_posix
23+
filter: not CONFIG_MINIMAL_LIBC

samples/modules/chre/src/echoapp.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* Copyright (c) 2021 Google LLC
2+
* SPDX-License-Identifier: Apache-2.0
3+
*/
4+
5+
#include <cinttypes>
6+
#include <sys/printk.h>
7+
8+
#include "chre_api/chre/event.h"
9+
#include "chre/core/event_loop_manager.h"
10+
#include "chre/platform/static_nanoapp_init.h"
11+
#include "chre/util/system/napp_permissions.h"
12+
13+
namespace
14+
{
15+
constexpr const uint64_t kAppId = 1;
16+
constexpr const uint32_t kAppVersion = 1;
17+
18+
chre::Nanoapp *nanoapp = nullptr;
19+
20+
bool nanoappStart(void)
21+
{
22+
printk("EchoApp::nanoappStart()\n");
23+
nanoapp = chre::EventLoopManagerSingleton ::get()->getEventLoop().getCurrentNanoapp();
24+
nanoapp->registerForBroadcastEvent(CHRE_EVENT_MESSAGE_FROM_HOST);
25+
return true;
26+
}
27+
28+
void nanoappHandleEvent(uint32_t sender_instance_id, uint16_t event_type, const void *event_data)
29+
{
30+
printk("EchoApp::nanoappHandleEvent(sender_instance_id=%u, event_type=%u, event_data@%p)\n",
31+
sender_instance_id, event_type, event_data);
32+
}
33+
34+
void nanoappEnd()
35+
{
36+
nanoapp->unregisterForBroadcastEvent(0);
37+
nanoapp = nullptr;
38+
printk("EchoApp::nanoappEnd()\n");
39+
}
40+
41+
} /* anonymous namespace */
42+
43+
CHRE_STATIC_NANOAPP_INIT(EchoApp, kAppId, kAppVersion, chre::NanoappPermissions::CHRE_PERMS_NONE);

samples/modules/chre/src/main.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* Copyright (c) 2021 Google LLC
2+
* SPDX-License-Identifier: Apache-2.0
3+
*/
4+
5+
#include <zephyr.h>
6+
#include <sys/printk.h>
7+
8+
#include "apps.hpp"
9+
#include "chre/core/event_loop_manager.h"
10+
#include "chre/target_platform/init.h"
11+
12+
inline const char *boolToString(bool cond)
13+
{
14+
return cond ? "SUCCESS" : "FAIL";
15+
}
16+
17+
void main(void)
18+
{
19+
auto echo_app = chre::initializeStaticNanoappEchoApp();
20+
auto& eventLoop = chre::EventLoopManagerSingleton::get()->getEventLoop();
21+
uint32_t instanceId;
22+
23+
if (chre::zephyr::init()) {
24+
printk("Failed to initialize!\n");
25+
return;
26+
}
27+
28+
printk("Hello CHRE!\n");
29+
30+
k_msleep(500);
31+
printk("Starting EchoApp... %s\n", boolToString(eventLoop.startNanoapp(echo_app)));
32+
printk("Nanoapp count=%u\n", eventLoop.getNanoappCount());
33+
printk("Finding instance ID... %s\n", boolToString(eventLoop.findNanoappInstanceIdByAppId(1, &instanceId)));
34+
printk("Nanoapp count=%u\n", eventLoop.getNanoappCount());
35+
printk("Instance ID: %u\n", instanceId);
36+
37+
printk("Sending event %u...\n", eventLoop.getNanoappCount());
38+
eventLoop.postEventOrDie(CHRE_EVENT_MESSAGE_FROM_HOST, nullptr, [](uint16_t eventType, void *eventData) {
39+
printk("Event (%u) complete!\n", eventType);
40+
});
41+
42+
k_sleep(K_MSEC(500));
43+
printk("Ending EchoApp... %s\n",
44+
boolToString(eventLoop.unloadNanoapp(instanceId, false)));
45+
chre::zephyr::deinit();
46+
printk("Goodbye!\n");
47+
}

west.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ manifest:
2828
- name: canopennode
2929
revision: 53d3415c14d60f8f4bfca54bfbc5d5a667d7e724
3030
path: modules/lib/canopennode
31+
- name: chre
32+
revision: 0edfe2c2ec656afb910cfab8ed59a5ffd59b87c8
33+
path: modules/lib/chre
3134
- name: civetweb
3235
revision: 094aeb41bb93e9199d24d665ee43e9e05d6d7b1c
3336
path: modules/lib/civetweb

0 commit comments

Comments
 (0)