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