1 
2 /*
3  * Copyright (C) 2022 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <openthread/instance.h>
19 #include <openthread/logging.h>
20 #include <openthread/platform/alarm-milli.h>
21 #include <utils/Log.h>
22 
otLogPlatArgs(otLogLevel aLogLevel,const char * aPlatModuleName,const char * aFormat,va_list aArgs)23 void otLogPlatArgs(otLogLevel aLogLevel, const char* aPlatModuleName, const char* aFormat,
24                    va_list aArgs) {
25     OT_UNUSED_VARIABLE(aPlatModuleName);
26     static const android_LogPriority kLogPriorities[] = {ANDROID_LOG_SILENT, ANDROID_LOG_FATAL,
27                                                          ANDROID_LOG_WARN,   ANDROID_LOG_INFO,
28                                                          ANDROID_LOG_INFO,   ANDROID_LOG_DEBUG};
29 
30     if (aLogLevel >= sizeof(kLogPriorities) / sizeof(android_LogPriority)) {
31         return;
32     }
33 
34     __android_log_vprint(kLogPriorities[aLogLevel], LOG_TAG, aFormat, aArgs);
35 }
36 
otLogCritPlat(const char * format,...)37 void otLogCritPlat(const char* format, ...) {
38     va_list args;
39 
40     va_start(args, format);
41     __android_log_vprint(ANDROID_LOG_FATAL, LOG_TAG, format, args);
42     va_end(args);
43 }
44 
otDumpDebgPlat(const char * aText,const void * aData,uint16_t aDataLength)45 void otDumpDebgPlat(const char* aText, const void* aData, uint16_t aDataLength) {
46     constexpr uint16_t kBufSize = 512;
47     char buf[kBufSize];
48 
49     if ((aText != nullptr) && (aData != nullptr)) {
50         const uint8_t* data = reinterpret_cast<const uint8_t*>(aData);
51 
52         for (uint16_t i = 0; (i < aDataLength) && (i < (kBufSize - 1) / 3); i++) {
53             snprintf(buf + (i * 3), (kBufSize - 1) - (i * 3), "%02x ", data[i]);
54         }
55 
56         __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "%s: %s", aText, buf);
57     }
58 }
59 
otPlatAlarmMilliFired(otInstance * aInstance)60 OT_TOOL_WEAK void otPlatAlarmMilliFired(otInstance* aInstance) {
61     OT_UNUSED_VARIABLE(aInstance);
62 }
63