1 /*
2  * Copyright (C) 2016 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 <cstdarg>
18 #include <cstring>
19 
20 #include "chre_api/chre/event.h"
21 #include "chre_api/chre/re.h"
22 #include "chre/core/event_loop_manager.h"
23 #include "chre/core/host_comms_manager.h"
24 #include "chre/platform/context.h"
25 #include "chre/platform/log.h"
26 
27 using chre::EventLoopManager;
28 
chreSendEvent(uint16_t eventType,void * eventData,chreEventCompleteFunction * freeCallback,uint32_t targetInstanceId)29 bool chreSendEvent(uint16_t eventType, void *eventData,
30                    chreEventCompleteFunction *freeCallback,
31                    uint32_t targetInstanceId) {
32   chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
33   return chre::EventLoopManagerSingleton::get()->postEvent(
34       eventType, eventData, freeCallback, nanoapp->getInstanceId(),
35       targetInstanceId);
36 }
37 
chreSendMessageToHost(void * message,uint32_t messageSize,uint32_t messageType,chreMessageFreeFunction * freeCallback)38 bool chreSendMessageToHost(void *message, uint32_t messageSize,
39                            uint32_t messageType,
40                            chreMessageFreeFunction *freeCallback) {
41   return chreSendMessageToHostEndpoint(
42       message, static_cast<size_t>(messageSize), messageType,
43       CHRE_HOST_ENDPOINT_BROADCAST, freeCallback);
44 }
45 
chreSendMessageToHostEndpoint(void * message,size_t messageSize,uint32_t messageType,uint16_t hostEndpoint,chreMessageFreeFunction * freeCallback)46 bool chreSendMessageToHostEndpoint(void *message, size_t messageSize,
47                                    uint32_t messageType, uint16_t hostEndpoint,
48                                    chreMessageFreeFunction *freeCallback) {
49   auto& hostCommsManager =
50       chre::EventLoopManagerSingleton::get()->getHostCommsManager();
51   return hostCommsManager.sendMessageToHostFromCurrentNanoapp(
52       message, messageSize, messageType, hostEndpoint, freeCallback);
53 }
54 
chreLog(enum chreLogLevel level,const char * formatStr,...)55 void chreLog(enum chreLogLevel level, const char *formatStr, ...) {
56   char logBuf[512];
57   va_list args;
58 
59   va_start(args, formatStr);
60   vsnprintf(logBuf, sizeof(logBuf), formatStr, args);
61   va_end(args);
62 
63   switch (level) {
64     case CHRE_LOG_ERROR:
65       LOGE("%s", logBuf);
66       break;
67     case CHRE_LOG_WARN:
68       LOGW("%s", logBuf);
69       break;
70     case CHRE_LOG_INFO:
71       LOGI("%s", logBuf);
72       break;
73     case CHRE_LOG_DEBUG:
74     default:
75       LOGD("%s", logBuf);
76   }
77 }
78