1 /*
2 * Copyright (C) 2020 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 #include "chre/core/init.h"
18
19 #ifdef CHRE_ENABLE_CHPP
20 #include "chpp/platform/chpp_init.h"
21 #endif
22 #include "chre/core/event_loop_manager.h"
23 #include "chre/core/static_nanoapps.h"
24 #include "chre/platform/shared/dram_vote_client.h"
25 #include "chre/target_platform/init.h"
26
27 #ifdef CHRE_USE_BUFFERED_LOGGING
28 #include "chre/platform/shared/log_buffer_manager.h"
29 #include "chre/target_platform/macros.h"
30 #endif
31
32 #include "task.h"
33
34 namespace chre {
35 namespace freertos {
36 namespace {
37
38 #ifdef CHRE_FREERTOS_TASK_PRIORITY
39 constexpr UBaseType_t kChreTaskPriority =
40 tskIDLE_PRIORITY + CHRE_FREERTOS_TASK_PRIORITY;
41 #else
42 constexpr UBaseType_t kChreTaskPriority = tskIDLE_PRIORITY + 1;
43 #endif
44
45 #ifdef CHRE_FREERTOS_STACK_DEPTH_IN_WORDS
46 constexpr configSTACK_DEPTH_TYPE kChreTaskStackDepthWords =
47 CHRE_FREERTOS_STACK_DEPTH_IN_WORDS;
48 #else
49 constexpr configSTACK_DEPTH_TYPE kChreTaskStackDepthWords = 0x800;
50 #endif
51
52 TaskHandle_t gChreTaskHandle;
53
54 #ifdef CHRE_USE_BUFFERED_LOGGING
55
56 TaskHandle_t gChreFlushTaskHandle;
57
58 #ifdef CHRE_HIGH_POWER_TEXT_ATTRIBUTE
59 CHRE_HIGH_POWER_TEXT_ATTRIBUTE
60 #endif
61 uint8_t gSecondaryLogBufferData[CHRE_LOG_BUFFER_DATA_SIZE];
62
63 uint8_t gPrimaryLogBufferData[CHRE_LOG_BUFFER_DATA_SIZE];
64
65 #endif
66
67 // This function is intended to be the task action function for FreeRTOS.
68 // It Initializes CHRE, runs the event loop, and only exits if it receives
69 // a message to shutdown. Note that depending on the hardware platform this
70 // runs on, CHRE might create additional threads, which are cleaned up when
71 // CHRE exits.
chreThreadEntry(void * context)72 void chreThreadEntry(void *context) {
73 UNUSED_VAR(context);
74
75 chre::init();
76 chre::EventLoopManagerSingleton::get()->lateInit();
77 chre::loadStaticNanoapps();
78
79 chre::EventLoopManagerSingleton::get()->getEventLoop().run();
80
81 // we only get here if the CHRE EventLoop exited
82 chre::deinit();
83
84 DramVoteClientSingleton::deinit();
85
86 vTaskDelete(nullptr);
87 gChreTaskHandle = nullptr;
88 }
89
90 #ifdef CHRE_USE_BUFFERED_LOGGING
chreFlushLogsToHostThreadEntry(void * context)91 void chreFlushLogsToHostThreadEntry(void *context) {
92 UNUSED_VAR(context);
93
94 // Never exits
95 chre::LogBufferManagerSingleton::get()->startSendLogsToHostLoop();
96 }
97 #endif
98
99 } // namespace
100
101 #ifdef CHRE_USE_BUFFERED_LOGGING
102 const char *getChreFlushTaskName();
103 #endif
104
init()105 BaseType_t init() {
106 BaseType_t rc =
107 xTaskCreate(chreThreadEntry, getChreTaskName(), kChreTaskStackDepthWords,
108 nullptr /* args */, kChreTaskPriority, &gChreTaskHandle);
109 CHRE_ASSERT(rc == pdPASS);
110
111 #ifdef CHRE_ENABLE_CHPP
112 chpp::init();
113 #endif
114
115 return rc;
116 }
117
initLogger()118 BaseType_t initLogger() {
119 BaseType_t rc = pdPASS;
120 #ifdef CHRE_USE_BUFFERED_LOGGING
121 if (!chre::LogBufferManagerSingleton::isInitialized()) {
122 chre::LogBufferManagerSingleton::init(gPrimaryLogBufferData,
123 gSecondaryLogBufferData,
124 sizeof(gPrimaryLogBufferData));
125
126 rc = xTaskCreate(chreFlushLogsToHostThreadEntry, getChreFlushTaskName(),
127 kChreTaskStackDepthWords, nullptr /* args */,
128 kChreTaskPriority, &gChreFlushTaskHandle);
129 }
130 #endif
131 return rc;
132 }
133
deinit()134 void deinit() {
135 // On a deinit call, we just stop the CHRE event loop. This causes the 'run'
136 // method in the task function exit, and move on to handle task cleanup
137 if (gChreTaskHandle != nullptr) {
138 chre::EventLoopManagerSingleton::get()->getEventLoop().stop();
139 }
140
141 #ifdef CHRE_ENABLE_CHPP
142 chpp::deinit();
143 #endif
144 }
145
getChreTaskName()146 const char *getChreTaskName() {
147 static constexpr char kChreTaskName[] = "CHRE";
148 return kChreTaskName;
149 }
150
151 #ifdef CHRE_USE_BUFFERED_LOGGING
getChreFlushTaskName()152 const char *getChreFlushTaskName() {
153 static constexpr char kChreFlushTaskName[] = "CHRELogs";
154 return kChreFlushTaskName;
155 }
156 #endif
157
158 } // namespace freertos
159
getChreTaskPriority()160 BaseType_t getChreTaskPriority() {
161 return freertos::kChreTaskPriority;
162 }
163
164 } // namespace chre
165