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 DramVoteClientSingleton::init();
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 // Never exits
81 chre::LogBufferManagerSingleton::get()->startSendLogsToHostLoop();
82 }
83 #endif
84
85 } // namespace
86
87 #ifdef CHRE_USE_BUFFERED_LOGGING
88 const char *getChreFlushTaskName();
89 #endif
90
init()91 BaseType_t init() {
92 BaseType_t rc = pdPASS;
93
94 #ifdef CHRE_USE_BUFFERED_LOGGING
95 chre::LogBufferManagerSingleton::init(gPrimaryLogBufferData,
96 gSecondaryLogBufferData,
97 sizeof(gPrimaryLogBufferData));
98
99 rc = xTaskCreate(chreFlushLogsToHostThreadEntry, getChreFlushTaskName(),
100 kChreTaskStackDepthWords, nullptr /* args */,
101 kChreTaskPriority, &gChreFlushTaskHandle);
102 #endif
103
104 if (rc == pdPASS) {
105 rc = xTaskCreate(chreThreadEntry, getChreTaskName(),
106 kChreTaskStackDepthWords, nullptr /* args */,
107 kChreTaskPriority, &gChreTaskHandle);
108 }
109
110 CHRE_ASSERT(rc == pdPASS);
111
112 chpp::init();
113
114 return rc;
115 }
116
deinit()117 void deinit() {
118 // On a deinit call, we just stop the CHRE event loop. This causes the 'run'
119 // method in the task function exit, and move on to handle task cleanup
120 if (gChreTaskHandle != nullptr) {
121 chre::EventLoopManagerSingleton::get()->getEventLoop().stop();
122 }
123
124 chpp::deinit();
125 }
126
getChreTaskName()127 const char *getChreTaskName() {
128 static constexpr char kChreTaskName[] = "CHRE";
129 return kChreTaskName;
130 }
131
132 #ifdef CHRE_USE_BUFFERED_LOGGING
getChreFlushTaskName()133 const char *getChreFlushTaskName() {
134 static constexpr char kChreFlushTaskName[] = "CHRELogs";
135 return kChreFlushTaskName;
136 }
137 #endif
138
139 } // namespace freertos
140
getChreTaskPriority()141 BaseType_t getChreTaskPriority() {
142 return freertos::kChreTaskPriority;
143 }
144
145 } // namespace chre
146