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 "chre/platform/shared/pal_system_api.h"
18
19#include <cstdarg>
20#include <cstdio>
21
22#include "chre/pal/system.h"
23#include "chre/platform/log.h"
24#include "chre/platform/memory.h"
25#include "chre/platform/system_time.h"
26
27//! Define a format string for PAL logs. This is defined as a macro so that it
28//! can be used as a string literal by platform implementations of the logging
29//! macros.
30#define PAL_LOG_FORMAT_STR "PAL: %s"
31
32namespace chre {
33
34uint64_t palSystemApiGetCurrentTime() {
35  return SystemTime::getMonotonicTime().toRawNanoseconds();
36}
37
38void palSystemApiLog(enum chreLogLevel level, const char *formatStr, ...) {
39  char logBuf[512];
40  va_list args;
41
42  va_start(args, formatStr);
43  vsnprintf(logBuf, sizeof(logBuf), formatStr, args);
44  va_end(args);
45
46  switch (level) {
47    case CHRE_LOG_ERROR:
48      LOGE(PAL_LOG_FORMAT_STR, logBuf);
49      break;
50    case CHRE_LOG_WARN:
51      LOGW(PAL_LOG_FORMAT_STR, logBuf);
52      break;
53    case CHRE_LOG_INFO:
54      LOGI(PAL_LOG_FORMAT_STR, logBuf);
55      break;
56    case CHRE_LOG_DEBUG:
57    default:
58      LOGD(PAL_LOG_FORMAT_STR, logBuf);
59  }
60}
61
62void *palSystemApiMemoryAlloc(size_t size) {
63  return memoryAlloc(size);
64}
65
66void palSystemApiMemoryFree(void *pointer) {
67  memoryFree(pointer);
68}
69
70// Initialize the CHRE System API with function implementations provided above.
71const chrePalSystemApi gChrePalSystemApi = {
72  CHRE_PAL_SYSTEM_API_CURRENT_VERSION, /* version */
73  palSystemApiGetCurrentTime,          /* getCurrentTime */
74  palSystemApiLog,                     /* log */
75  palSystemApiMemoryAlloc,             /* memoryAlloc */
76  palSystemApiMemoryFree,              /* memoryFree */
77};
78
79}  // namespace chre
80