chre_app.c revision b78969e7bce5b6d57a3e6b99315f44fe77b5d2b6
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 <eventnums.h> 18#include <seos.h> 19#include <timer.h> 20#include <toolchain.h> 21#include <crt_priv.h> 22 23#include <chre.h> 24 25/* 26 * Common CHRE App support code 27 */ 28 29static bool chreappStart(uint32_t tid) 30{ 31 __crt_init(); 32 return nanoappStart(); 33} 34 35static void chreappEnd(void) 36{ 37 nanoappEnd(); 38 __crt_exit(); 39} 40 41static void chreappHandle(uint32_t eventTypeAndTid, const void *eventData) 42{ 43 uint16_t evt = eventTypeAndTid; 44 uint16_t srcTid = eventTypeAndTid >> 16; 45 const void *data = eventData; 46 47 union EventLocalData { 48 struct chreMessageFromHostData msg; 49 } u; 50 51 switch(evt) { 52 case EVT_APP_TIMER: 53 evt = CHRE_EVENT_TIMER; 54 data = ((struct TimerEvent *)eventData)->data; 55 break; 56 case EVT_APP_FROM_HOST: 57 evt = CHRE_EVENT_MESSAGE_FROM_HOST; 58 data = &u.msg; 59 u.msg.message = (uint8_t*)eventData + 1; 60 u.msg.reservedMessageType = 0; 61 u.msg.messageSize = *(uint8_t*)eventData; 62 break; 63 case EVT_APP_FROM_HOST_CHRE: 64 { 65 const struct NanohubMsgChreHdr *hdr = eventData; 66 evt = CHRE_EVENT_MESSAGE_FROM_HOST; 67 data = &u.msg; 68 u.msg.message = hdr + 1; 69 u.msg.reservedMessageType = hdr->appEvent; 70 u.msg.messageSize = hdr->size; 71 break; 72 } 73 } 74 nanoappHandleEvent(srcTid, evt, data); 75} 76 77// Collect entry points 78const struct AppFuncs SET_EXTERNAL_APP_ATTRIBUTES(used, section (".app_init"),visibility("default")) _mAppFuncs = { 79 .init = chreappStart, 80 .end = chreappEnd, 81 .handle = chreappHandle, 82}; 83 84// declare version for compatibility with current runtime 85const uint32_t SET_EXTERNAL_APP_VERSION(used, section (".app_version"), visibility("default")) _mAppVer = 0; 86