1 /*
2  * Copyright (C) 2017 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 "chpp/pal_api.h"
18 
19 #include <stdarg.h>
20 #include <stdio.h>
21 
22 #include "chpp/app.h"
23 #include "chpp/log.h"
24 #include "chpp/macros.h"
25 #include "chpp/memory.h"
26 #include "chpp/time.h"
27 #include "chre/pal/system.h"
28 #include "chre_api/chre/re.h"
29 
30 //! Define a format string for PAL logs. This is defined as a macro so that it
31 //! can be used as a string literal by platform implementations of the logging
32 //! macros.
33 #define PAL_LOG_FORMAT_STR "PAL: %s"
34 
palSystemApiGetCurrentTime(void)35 static uint64_t palSystemApiGetCurrentTime(void) {
36   return chppGetCurrentTimeNs();
37 }
38 
palSystemApiLog(enum chreLogLevel level,const char * formatStr,...)39 static void palSystemApiLog(enum chreLogLevel level, const char *formatStr,
40                             ...) {
41   char logBuf[512];
42   va_list args;
43 
44   va_start(args, formatStr);
45   vsnprintf(logBuf, sizeof(logBuf), formatStr, args);
46   // TODO: not sure if vsnprintf will be well-supported on partner platforms (on
47   // the CHRE side, we know it's the case for SLPI but even then it's not
48   // supported in micro-image). We may instead want to expose a va_list version
49   // of the platform log functionality, so we can just forward directly to the
50   // underlying platform log functionality without handling format string
51   // expansion in common code.
52   va_end(args);
53 
54   switch (level) {
55     case CHRE_LOG_ERROR:
56       CHPP_LOGE(PAL_LOG_FORMAT_STR, logBuf);
57       break;
58     case CHRE_LOG_WARN:
59       CHPP_LOGW(PAL_LOG_FORMAT_STR, logBuf);
60       break;
61     case CHRE_LOG_INFO:
62       CHPP_LOGI(PAL_LOG_FORMAT_STR, logBuf);
63       break;
64     case CHRE_LOG_DEBUG:
65     default:
66       CHPP_LOGD(PAL_LOG_FORMAT_STR, logBuf);
67   }
68 }
69 
chppPalSystemApiInit(struct ChppAppState * context)70 void chppPalSystemApiInit(struct ChppAppState *context) {
71   // Initialize the CHRE System API with function implementations provided
72   // above.
73   static const struct chrePalSystemApi chrePalSystemApi = {
74       .version = CHRE_PAL_SYSTEM_API_CURRENT_VERSION,
75       .getCurrentTime = palSystemApiGetCurrentTime,
76       .log = palSystemApiLog,
77       .memoryAlloc = chppMalloc,
78       .memoryFree = chppFree,
79   };
80 
81   context->systemApi = &chrePalSystemApi;
82 }
83 
chppPalSystemApiDeinit(struct ChppAppState * context)84 void chppPalSystemApiDeinit(struct ChppAppState *context) {
85   UNUSED_VAR(context);
86 }
87