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 <stdlib.h>
18 #include <string.h>
19 
20 #include <seos.h>
21 #include <timer.h>
22 #include <syscallDo.h>
23 
24 static uint32_t mMyTid;
25 static int cnt;
26 
27 struct ExtMsg
28 {
29     struct HostHubRawPacket hdr;
30     uint8_t msg;
31     uint32_t val;
32 } __attribute__((packed));
33 
start_task(uint32_t myTid)34 static bool start_task(uint32_t myTid)
35 {
36     mMyTid = myTid;
37     cnt = 100;
38 
39     return eOsEventSubscribe(myTid, EVT_APP_START);
40 }
41 
end_task(void)42 static void end_task(void)
43 {
44     eOsLog(LOG_DEBUG, "App 0 terminating");
45 }
46 
handle_event(uint32_t evtType,const void * evtData)47 static void handle_event(uint32_t evtType, const void* evtData)
48 {
49     const struct TimerEvent *te;
50     const struct AppEventFreeData *aefd;
51     uint32_t timerId;
52     struct ExtMsg *extMsg;
53 
54     if (evtType == EVT_APP_START) {
55         timerId = eOsTimTimerSet(1000000000ULL, 50, 50, mMyTid, (void *)&cnt, false);
56         eOsLog(LOG_INFO, "App 0 started with tid %u timerid %u\n", mMyTid, timerId);
57     } else if (evtType == EVT_APP_TIMER) {
58         te = evtData;
59         eOsLog(LOG_INFO, "App 0 received timer %u callback: %d (TIM: %lld, RTC: %lld, SENSOR: %lld, HOST: %lld)\n", te->timerId, *(int *)te->data, eOsTimGetTime(), eOsRtcGetTime(), eOsSensorGetTime(), eOsHostGetTime());
60         extMsg = eOsHeapAlloc(sizeof(*extMsg));
61         extMsg->hdr.appId = APP_ID_MAKE(NANOHUB_VENDOR_GOOGLE, 0x548000);
62         extMsg->hdr.dataLen = 5;
63         extMsg->msg = 0x01;
64         extMsg->val = *(int *)te->data;
65         if (!(eOsEnqueueEvt(EVT_APP_TO_HOST, extMsg, mMyTid)))
66             eOsHeapFree(extMsg);
67         if (cnt-- <= 0)
68             eOsTimTimerCancel(te->timerId);
69     } else if (evtType == EVT_APP_FREE_EVT_DATA) {
70         aefd = evtData;
71         if (aefd->evtType == EVT_APP_TO_HOST)
72             eOsHeapFree(aefd->evtData);
73     }
74 }
75 
76 APP_INIT(0, start_task, end_task, handle_event);
77