1*84e33947SAndroid Build Coastguard Worker /*
2*84e33947SAndroid Build Coastguard Worker * Copyright (C) 2017 The Android Open Source Project
3*84e33947SAndroid Build Coastguard Worker *
4*84e33947SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*84e33947SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*84e33947SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*84e33947SAndroid Build Coastguard Worker *
8*84e33947SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*84e33947SAndroid Build Coastguard Worker *
10*84e33947SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*84e33947SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*84e33947SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*84e33947SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*84e33947SAndroid Build Coastguard Worker * limitations under the License.
15*84e33947SAndroid Build Coastguard Worker */
16*84e33947SAndroid Build Coastguard Worker
17*84e33947SAndroid Build Coastguard Worker #include "chpp/pal_api.h"
18*84e33947SAndroid Build Coastguard Worker
19*84e33947SAndroid Build Coastguard Worker #include <stdarg.h>
20*84e33947SAndroid Build Coastguard Worker #include <stdio.h>
21*84e33947SAndroid Build Coastguard Worker
22*84e33947SAndroid Build Coastguard Worker #include "chpp/app.h"
23*84e33947SAndroid Build Coastguard Worker #include "chpp/log.h"
24*84e33947SAndroid Build Coastguard Worker #include "chpp/macros.h"
25*84e33947SAndroid Build Coastguard Worker #include "chpp/memory.h"
26*84e33947SAndroid Build Coastguard Worker #include "chpp/time.h"
27*84e33947SAndroid Build Coastguard Worker #include "chre/pal/system.h"
28*84e33947SAndroid Build Coastguard Worker #include "chre_api/chre/re.h"
29*84e33947SAndroid Build Coastguard Worker
30*84e33947SAndroid Build Coastguard Worker //! Define a format string for PAL logs. This is defined as a macro so that it
31*84e33947SAndroid Build Coastguard Worker //! can be used as a string literal by platform implementations of the logging
32*84e33947SAndroid Build Coastguard Worker //! macros.
33*84e33947SAndroid Build Coastguard Worker #define PAL_LOG_FORMAT_STR "PAL: %s"
34*84e33947SAndroid Build Coastguard Worker
palSystemApiGetCurrentTime(void)35*84e33947SAndroid Build Coastguard Worker static uint64_t palSystemApiGetCurrentTime(void) {
36*84e33947SAndroid Build Coastguard Worker return chppGetCurrentTimeNs();
37*84e33947SAndroid Build Coastguard Worker }
38*84e33947SAndroid Build Coastguard Worker
palSystemApiLog(enum chreLogLevel level,const char * formatStr,...)39*84e33947SAndroid Build Coastguard Worker static void palSystemApiLog(enum chreLogLevel level, const char *formatStr,
40*84e33947SAndroid Build Coastguard Worker ...) {
41*84e33947SAndroid Build Coastguard Worker char logBuf[512];
42*84e33947SAndroid Build Coastguard Worker va_list args;
43*84e33947SAndroid Build Coastguard Worker
44*84e33947SAndroid Build Coastguard Worker va_start(args, formatStr);
45*84e33947SAndroid Build Coastguard Worker vsnprintf(logBuf, sizeof(logBuf), formatStr, args);
46*84e33947SAndroid Build Coastguard Worker // TODO: not sure if vsnprintf will be well-supported on partner platforms (on
47*84e33947SAndroid Build Coastguard Worker // the CHRE side, we know it's the case for SLPI but even then it's not
48*84e33947SAndroid Build Coastguard Worker // supported in micro-image). We may instead want to expose a va_list version
49*84e33947SAndroid Build Coastguard Worker // of the platform log functionality, so we can just forward directly to the
50*84e33947SAndroid Build Coastguard Worker // underlying platform log functionality without handling format string
51*84e33947SAndroid Build Coastguard Worker // expansion in common code.
52*84e33947SAndroid Build Coastguard Worker va_end(args);
53*84e33947SAndroid Build Coastguard Worker
54*84e33947SAndroid Build Coastguard Worker switch (level) {
55*84e33947SAndroid Build Coastguard Worker case CHRE_LOG_ERROR:
56*84e33947SAndroid Build Coastguard Worker CHPP_LOGE(PAL_LOG_FORMAT_STR, logBuf);
57*84e33947SAndroid Build Coastguard Worker break;
58*84e33947SAndroid Build Coastguard Worker case CHRE_LOG_WARN:
59*84e33947SAndroid Build Coastguard Worker CHPP_LOGW(PAL_LOG_FORMAT_STR, logBuf);
60*84e33947SAndroid Build Coastguard Worker break;
61*84e33947SAndroid Build Coastguard Worker case CHRE_LOG_INFO:
62*84e33947SAndroid Build Coastguard Worker CHPP_LOGI(PAL_LOG_FORMAT_STR, logBuf);
63*84e33947SAndroid Build Coastguard Worker break;
64*84e33947SAndroid Build Coastguard Worker case CHRE_LOG_DEBUG:
65*84e33947SAndroid Build Coastguard Worker default:
66*84e33947SAndroid Build Coastguard Worker CHPP_LOGD(PAL_LOG_FORMAT_STR, logBuf);
67*84e33947SAndroid Build Coastguard Worker }
68*84e33947SAndroid Build Coastguard Worker }
69*84e33947SAndroid Build Coastguard Worker
chppPalSystemApiInit(struct ChppAppState * context)70*84e33947SAndroid Build Coastguard Worker void chppPalSystemApiInit(struct ChppAppState *context) {
71*84e33947SAndroid Build Coastguard Worker // Initialize the CHRE System API with function implementations provided
72*84e33947SAndroid Build Coastguard Worker // above.
73*84e33947SAndroid Build Coastguard Worker static const struct chrePalSystemApi chrePalSystemApi = {
74*84e33947SAndroid Build Coastguard Worker .version = CHRE_PAL_SYSTEM_API_CURRENT_VERSION,
75*84e33947SAndroid Build Coastguard Worker .getCurrentTime = palSystemApiGetCurrentTime,
76*84e33947SAndroid Build Coastguard Worker .log = palSystemApiLog,
77*84e33947SAndroid Build Coastguard Worker .memoryAlloc = chppMalloc,
78*84e33947SAndroid Build Coastguard Worker .memoryFree = chppFree,
79*84e33947SAndroid Build Coastguard Worker };
80*84e33947SAndroid Build Coastguard Worker
81*84e33947SAndroid Build Coastguard Worker context->systemApi = &chrePalSystemApi;
82*84e33947SAndroid Build Coastguard Worker }
83*84e33947SAndroid Build Coastguard Worker
chppPalSystemApiDeinit(struct ChppAppState * context)84*84e33947SAndroid Build Coastguard Worker void chppPalSystemApiDeinit(struct ChppAppState *context) {
85*84e33947SAndroid Build Coastguard Worker UNUSED_VAR(context);
86*84e33947SAndroid Build Coastguard Worker }
87