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 <cstdio>
18 
19 #include "chre/core/event_loop_manager.h"
20 #include "chre/platform/system_time.h"
21 #include "chre/util/nested_data_ptr.h"
22 
23 #ifdef CHRE_USE_TOKENIZED_LOGGING
24 namespace {
25 
26 static constexpr size_t kLogBufferSize = 60;
27 
28 }  // anonymous namespace
29 
30 // The callback function that must be defined to handle an encoded
31 // tokenizer message.
pw_TokenizerHandleEncodedMessageWithPayload(void * userPayload,const uint8_t encodedMsg[],size_t encodedMsgSize)32 void pw_TokenizerHandleEncodedMessageWithPayload(void *userPayload,
33                                                  const uint8_t encodedMsg[],
34                                                  size_t encodedMsgSize) {
35   // The header encoding here follows the message definition in the
36   // host_messages.fbs flatbuffers file.
37   uint8_t logBuffer[kLogBufferSize];
38   constexpr size_t kLogMessageHeaderSizeBytes = 1 + sizeof(uint64_t);
39   uint8_t *pLogBuffer = &logBuffer[0];
40 
41   chre::NestedDataPtr<uint8_t> nestedLevel(userPayload);
42   *pLogBuffer = nestedLevel.data;
43   ++pLogBuffer;
44 
45   uint64_t timestampNanos =
46       chre::SystemTime::getMonotonicTime().toRawNanoseconds();
47   memcpy(pLogBuffer, &timestampNanos, sizeof(uint64_t));
48   pLogBuffer += sizeof(uint64_t);
49   memcpy(pLogBuffer, encodedMsg, encodedMsgSize);
50 
51   // TODO (b/148873804): buffer log messages generated while the AP is asleep
52   auto &hostCommsMgr =
53       chre::EventLoopManagerSingleton::get()->getHostCommsManager();
54   hostCommsMgr.sendLogMessage(logBuffer,
55                               encodedMsgSize + kLogMessageHeaderSizeBytes);
56 }
57 #endif
58