aboutsummaryrefslogtreecommitdiff
path: root/platform/freertos/init.cc
blob: 1cab708ea21774f1cc2141fc984d4d4e20c544a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "chre/core/init.h"

#include "chpp/platform/chpp_init.h"
#include "chre/core/event_loop_manager.h"
#include "chre/core/static_nanoapps.h"
#include "chre/platform/shared/dram_vote_client.h"
#include "chre/target_platform/init.h"

#ifdef CHRE_USE_BUFFERED_LOGGING
#include "chre/platform/shared/log_buffer_manager.h"
#include "chre/target_platform/macros.h"
#endif

#include "task.h"

namespace chre {
namespace freertos {
namespace {

constexpr configSTACK_DEPTH_TYPE kChreTaskStackDepthWords = 0x800;

constexpr UBaseType_t kChreTaskPriority = tskIDLE_PRIORITY + 1;

TaskHandle_t gChreTaskHandle;

#ifdef CHRE_USE_BUFFERED_LOGGING

TaskHandle_t gChreFlushTaskHandle;

#ifdef CHRE_HIGH_POWER_TEXT_ATTRIBUTE
CHRE_HIGH_POWER_TEXT_ATTRIBUTE
#endif
uint8_t gSecondaryLogBufferData[CHRE_LOG_BUFFER_DATA_SIZE];

uint8_t gPrimaryLogBufferData[CHRE_LOG_BUFFER_DATA_SIZE];

#endif

// This function is intended to be the task action function for FreeRTOS.
// It Initializes CHRE, runs the event loop, and only exits if it receives
// a message to shutdown. Note that depending on the hardware platform this
// runs on, CHRE might create additional threads, which are cleaned up when
// CHRE exits.
void chreThreadEntry(void *context) {
  DramVoteClientSingleton::init();

  chre::init();
  chre::EventLoopManagerSingleton::get()->lateInit();
  chre::loadStaticNanoapps();

  chre::EventLoopManagerSingleton::get()->getEventLoop().run();

  // we only get here if the CHRE EventLoop exited
  chre::deinit();

  DramVoteClientSingleton::deinit();

  vTaskDelete(nullptr);
  gChreTaskHandle = nullptr;
}

#ifdef CHRE_USE_BUFFERED_LOGGING
void chreFlushLogsToHostThreadEntry(void *context) {
  // Never exits
  chre::LogBufferManagerSingleton::get()->startSendLogsToHostLoop();
}
#endif

}  // namespace

#ifdef CHRE_USE_BUFFERED_LOGGING
const char *getChreFlushTaskName();
#endif

BaseType_t init() {
  BaseType_t rc = pdPASS;

#ifdef CHRE_USE_BUFFERED_LOGGING
  chre::LogBufferManagerSingleton::init(gPrimaryLogBufferData,
                                        gSecondaryLogBufferData,
                                        sizeof(gPrimaryLogBufferData));

  rc = xTaskCreate(chreFlushLogsToHostThreadEntry, getChreFlushTaskName(),
                   kChreTaskStackDepthWords, nullptr /* args */,
                   kChreTaskPriority, &gChreFlushTaskHandle);
#endif

  if (rc == pdPASS) {
    rc = xTaskCreate(chreThreadEntry, getChreTaskName(),
                     kChreTaskStackDepthWords, nullptr /* args */,
                     kChreTaskPriority, &gChreTaskHandle);
  }

  CHRE_ASSERT(rc == pdPASS);

  chpp::init();

  return rc;
}

void deinit() {
  // On a deinit call, we just stop the CHRE event loop. This causes the 'run'
  // method in the task function exit, and move on to handle task cleanup
  if (gChreTaskHandle != nullptr) {
    chre::EventLoopManagerSingleton::get()->getEventLoop().stop();
  }

  chpp::deinit();
}

const char *getChreTaskName() {
  static constexpr char kChreTaskName[] = "CHRE";
  return kChreTaskName;
}

#ifdef CHRE_USE_BUFFERED_LOGGING
const char *getChreFlushTaskName() {
  static constexpr char kChreFlushTaskName[] = "CHRELogs";
  return kChreFlushTaskName;
}
#endif

}  // namespace freertos

BaseType_t getChreTaskPriority() {
  return freertos::kChreTaskPriority;
}

}  // namespace chre