1 /* Copyright (c) 2021 Google LLC
2  * SPDX-License-Identifier: Apache-2.0
3  */
4 
5 #include <zephyr/kernel.h>
6 #include <zephyr/sys/printk.h>
7 #include <zephyr/logging/log_ctrl.h>
8 
9 #include "apps.hpp"
10 #include "chre/core/event_loop_manager.h"
11 #include "chre/target_platform/init.h"
12 
boolToString(bool cond)13 inline const char *boolToString(bool cond)
14 {
15 	return cond ? "SUCCESS" : "FAIL";
16 }
17 
main(void)18 int main(void)
19 {
20 	auto echo_app = chre::initializeStaticNanoappEchoApp();
21 	auto& eventLoop = chre::EventLoopManagerSingleton::get()->getEventLoop();
22 	uint32_t instanceId;
23 
24 	if (chre::zephyr::init()) {
25 		printk("Failed to initialize!\n");
26 		return -1;
27 	}
28 
29 	printk("Hello CHRE!\n");
30 
31 	k_msleep(500);
32 	/*
33 	 * Flush all log messages that resulted from initialization to avoid
34 	 * getting them mingled with those printk messages below.
35 	 */
36 	while (IS_ENABLED(CONFIG_LOG_PROCESS_THREAD) && log_data_pending()) {
37 		k_msleep(100);
38 	}
39 	printk("Starting EchoApp... %s\n", boolToString(eventLoop.startNanoapp(echo_app)));
40 	printk("Nanoapp count=%u\n", eventLoop.getNanoappCount());
41 	printk("Finding instance ID... %s\n", boolToString(eventLoop.findNanoappInstanceIdByAppId(1, &instanceId)));
42 	printk("Nanoapp count=%u\n", eventLoop.getNanoappCount());
43 	printk("Instance ID: %u\n", instanceId);
44 
45 	printk("Sending event %u...\n", eventLoop.getNanoappCount());
46 	eventLoop.postEventOrDie(CHRE_EVENT_MESSAGE_FROM_HOST, nullptr, [](uint16_t eventType, void *eventData) {
47 		printk("Event (%u) complete!\n", eventType);
48 	});
49 
50 	k_sleep(K_MSEC(500));
51 	printk("Ending EchoApp... %s\n",
52 	       boolToString(eventLoop.unloadNanoapp(instanceId, false)));
53 	chre::zephyr::deinit();
54 	printk("Goodbye!\n");
55 	return 0;
56 }
57