xref: /aosp_15_r20/system/chre/platform/zephyr/init.cc (revision 84e339476a462649f82315436d70fd732297a399)
1*84e33947SAndroid Build Coastguard Worker /*
2*84e33947SAndroid Build Coastguard Worker  * Copyright (C) 2022 The Android Open Source Project
3*84e33947SAndroid Build Coastguard Worker  *
4*84e33947SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*84e33947SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*84e33947SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*84e33947SAndroid Build Coastguard Worker  *
8*84e33947SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*84e33947SAndroid Build Coastguard Worker  *
10*84e33947SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*84e33947SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*84e33947SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*84e33947SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*84e33947SAndroid Build Coastguard Worker  * limitations under the License.
15*84e33947SAndroid Build Coastguard Worker  */
16*84e33947SAndroid Build Coastguard Worker #include "chre/core/init.h"
17*84e33947SAndroid Build Coastguard Worker 
18*84e33947SAndroid Build Coastguard Worker #include <errno.h>
19*84e33947SAndroid Build Coastguard Worker #include <zephyr/sys/printk.h>
20*84e33947SAndroid Build Coastguard Worker #include <zephyr/kernel.h>
21*84e33947SAndroid Build Coastguard Worker 
22*84e33947SAndroid Build Coastguard Worker #include "chre/core/event_loop_manager.h"
23*84e33947SAndroid Build Coastguard Worker #include "chre/core/static_nanoapps.h"
24*84e33947SAndroid Build Coastguard Worker #include "chre/target_platform/init.h"
25*84e33947SAndroid Build Coastguard Worker 
26*84e33947SAndroid Build Coastguard Worker namespace chre {
27*84e33947SAndroid Build Coastguard Worker namespace zephyr {
28*84e33947SAndroid Build Coastguard Worker namespace {
29*84e33947SAndroid Build Coastguard Worker 
30*84e33947SAndroid Build Coastguard Worker K_THREAD_STACK_DEFINE(chre_stack_area, CONFIG_CHRE_TASK_STACK_SIZE);
31*84e33947SAndroid Build Coastguard Worker struct k_thread chre_thread_data;
32*84e33947SAndroid Build Coastguard Worker k_tid_t chre_tid;
33*84e33947SAndroid Build Coastguard Worker 
chreThreadEntry(void *,void *,void *)34*84e33947SAndroid Build Coastguard Worker void chreThreadEntry(void *, void *, void *) {
35*84e33947SAndroid Build Coastguard Worker   chre::init();
36*84e33947SAndroid Build Coastguard Worker   chre::EventLoopManagerSingleton::get()->lateInit();
37*84e33947SAndroid Build Coastguard Worker   chre::loadStaticNanoapps();
38*84e33947SAndroid Build Coastguard Worker 
39*84e33947SAndroid Build Coastguard Worker   chre::EventLoopManagerSingleton::get()->getEventLoop().run();
40*84e33947SAndroid Build Coastguard Worker 
41*84e33947SAndroid Build Coastguard Worker   // we only get here if the CHRE EventLoop exited
42*84e33947SAndroid Build Coastguard Worker   chre::deinit();
43*84e33947SAndroid Build Coastguard Worker 
44*84e33947SAndroid Build Coastguard Worker   chre_tid = nullptr;
45*84e33947SAndroid Build Coastguard Worker }
46*84e33947SAndroid Build Coastguard Worker }  // namespace
47*84e33947SAndroid Build Coastguard Worker 
init()48*84e33947SAndroid Build Coastguard Worker int init() {
49*84e33947SAndroid Build Coastguard Worker   static const char *thread_name = CONFIG_CHRE_TASK_NAME;
50*84e33947SAndroid Build Coastguard Worker   chre_tid = k_thread_create(&chre_thread_data, chre_stack_area,
51*84e33947SAndroid Build Coastguard Worker                              K_THREAD_STACK_SIZEOF(chre_stack_area),
52*84e33947SAndroid Build Coastguard Worker                              chreThreadEntry, nullptr, nullptr, nullptr,
53*84e33947SAndroid Build Coastguard Worker                              CONFIG_CHRE_TASK_PRIORITY, 0, K_NO_WAIT);
54*84e33947SAndroid Build Coastguard Worker 
55*84e33947SAndroid Build Coastguard Worker   if (chre_tid == nullptr) {
56*84e33947SAndroid Build Coastguard Worker     printk("Failed to create thread\n");
57*84e33947SAndroid Build Coastguard Worker     return -EINVAL;
58*84e33947SAndroid Build Coastguard Worker   }
59*84e33947SAndroid Build Coastguard Worker 
60*84e33947SAndroid Build Coastguard Worker   if (int rc = k_thread_name_set(chre_tid, thread_name); rc != 0) {
61*84e33947SAndroid Build Coastguard Worker     printk("Failed to set thread name to \"%s\": %d\n", CONFIG_CHRE_TASK_NAME,
62*84e33947SAndroid Build Coastguard Worker            rc);
63*84e33947SAndroid Build Coastguard Worker   }
64*84e33947SAndroid Build Coastguard Worker 
65*84e33947SAndroid Build Coastguard Worker //  chpp::init();
66*84e33947SAndroid Build Coastguard Worker   return 0;
67*84e33947SAndroid Build Coastguard Worker }
68*84e33947SAndroid Build Coastguard Worker 
deinit()69*84e33947SAndroid Build Coastguard Worker void deinit() {
70*84e33947SAndroid Build Coastguard Worker   if (chre_tid != nullptr) {
71*84e33947SAndroid Build Coastguard Worker     chre::EventLoopManagerSingleton ::get()->getEventLoop().stop();
72*84e33947SAndroid Build Coastguard Worker   }
73*84e33947SAndroid Build Coastguard Worker //  chpp::deinit();
74*84e33947SAndroid Build Coastguard Worker }
75*84e33947SAndroid Build Coastguard Worker 
getChreTaskId()76*84e33947SAndroid Build Coastguard Worker k_tid_t getChreTaskId() {
77*84e33947SAndroid Build Coastguard Worker   return chre_tid;
78*84e33947SAndroid Build Coastguard Worker }
79*84e33947SAndroid Build Coastguard Worker 
80*84e33947SAndroid Build Coastguard Worker }  // namespace chre::zephyr
81*84e33947SAndroid Build Coastguard Worker 
82*84e33947SAndroid Build Coastguard Worker }  // namespace chre
83