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 #ifndef __SEOS_PRIV_H__
18 #define __SEOS_PRIV_H__
19 
20 #include <inttypes.h>
21 #include <seos.h>
22 #include <chre.h>
23 
24 #define NO_NODE (TaskIndex)(-1)
25 #define for_each_task(listHead, task) for (task = osTaskByIdx((listHead)->next); task; task = osTaskByIdx(task->list.next))
26 
27 #define TID_TO_TASK_IDX(tid) (tid & TASK_TID_IDX_MASK)
28 
29 #define FL_TASK_STOPPED 1
30 #define FL_TASK_ABORTED 2
31 
32 #define EVT_SUBSCRIBE_TO_EVT         0x00000000
33 #define EVT_UNSUBSCRIBE_TO_EVT       0x00000001
34 #define EVT_DEFERRED_CALLBACK        0x00000002
35 #define EVT_PRIVATE_EVT              0x00000003
36 
37 #define EVT_PRIVATE_CLASS_CHRE       0x00000001
38 
39 #define EVENT_WITH_ORIGIN(evt, origin)       (((evt) & EVT_MASK) | ((origin) << (32 - TASK_TID_BITS)))
40 #define EVENT_GET_ORIGIN(evt) ((evt) >> (32 - TASK_TID_BITS))
41 #define EVENT_GET_EVENT(evt) ((evt) & (EVT_MASK & ~EVENT_TYPE_BIT_DISCARDABLE))
42 
43 #define MAX_EVT_SUB_CNT              6
44 
45 SET_PACKED_STRUCT_MODE_ON
46 struct TaskList {
47     TaskIndex prev;
48     TaskIndex next;
49 } ATTRIBUTE_PACKED;
50 SET_PACKED_STRUCT_MODE_OFF
51 
52 struct Task {
53     /* App entry points */
54     const struct AppHdr *app;
55 
56     /* per-platform app info */
57     struct PlatAppInfo platInfo;
58 
59     /* for some basic number of subbed events, the array is stored directly here. after that, a heap chunk is used */
60     uint32_t subbedEventsInt[MAX_EMBEDDED_EVT_SUBS];
61     uint32_t *subbedEvents; /* NULL for invalid tasks */
62 
63     struct TaskList list;
64 
65     /* task pointer will not change throughout task lifetime,
66      * however same task pointer may be reused for a new task; to eliminate the ambiguity,
67      * TID is maintained for each task such that new tasks will be guaranteed to receive different TID */
68     uint16_t tid;
69 
70     uint8_t  subbedEvtCount;
71     uint8_t  subbedEvtListSz;
72     uint8_t  flags;
73     uint8_t  ioCount;
74 
75 };
76 
77 struct I2cEventData {
78     void *cookie;
79     uint32_t tx;
80     uint32_t rx;
81     int err;
82 };
83 
84 union OsApiSlabItem {
85     struct I2cEventData i2cAppCbkEvt;
86     struct {
87         uint32_t toTid;
88         void *cookie;
89     } i2cAppCbkInfo;
90 };
91 
92 /* this is a system slab allocator internal data type */
93 union SeosInternalSlabData {
94     struct {
95         uint16_t tid;
96         uint8_t numEvts;
97         uint16_t evts[MAX_EVT_SUB_CNT];
98     } evtSub;
99     struct {
100         OsDeferCbkF callback;
101         void *cookie;
102     } deferred;
103     struct {
104         uint32_t evtType;
105         void *evtData;
106         TaggedPtr evtFreeInfo;
107         uint16_t fromTid;
108         uint16_t toTid;
109     } privateEvt;
110     union OsApiSlabItem osApiItem;
111 };
112 
113 typedef bool (*appMatchFunc)(const void *cookie, const struct AppHdr *);
114 
115 uint8_t osTaskIndex(struct Task *task);
116 struct Task *osGetCurrentTask();
117 struct Task *osSetCurrentTask(struct Task *task);
118 struct Task *osTaskFindByTid(uint32_t tid);
119 void osTaskAbort(struct Task *task);
120 void osTaskInvokeMessageFreeCallback(struct Task *task, void (*freeCallback)(void *, size_t), void *message, uint32_t messageSize);
121 void osTaskInvokeEventFreeCallback(struct Task *task, void (*freeCallback)(uint16_t, void *), uint16_t event, void *data);
122 void osChreTaskHandle(struct Task *task, uint32_t evtType, const void *evtData);
123 
osTaskIsChre(const struct Task * task)124 static inline bool osTaskIsChre(const struct Task *task)
125 {
126     return task->app && (task->app->hdr.fwFlags & FL_APP_HDR_CHRE) != 0;
127 }
128 
osTaskChreVersion(const struct Task * task)129 static inline uint32_t osTaskChreVersion(const struct Task *task)
130 {
131     if (osTaskIsChre(task)) {
132         // Apps loaded on 1.0 stored 0xFF in both rfu bytes
133         if (task->app->hdr.chreApiMajor == 0xFF && task->app->hdr.chreApiMinor == 0xFF)
134             return CHRE_API_VERSION_1_0;
135         else
136             return task->app->hdr.chreApiMajor << 24 | task->app->hdr.chreApiMinor << 16;
137     } else {
138         return 0;
139     }
140 }
141 
osTaskMakeNewTid(struct Task * task)142 static inline void osTaskMakeNewTid(struct Task *task)
143 {
144     task->tid = ((task->tid + TASK_TID_INCREMENT) & TASK_TID_COUNTER_MASK) |
145                 (osTaskIndex(task) & TASK_TID_IDX_MASK);
146 }
147 
148 #endif // __SEOS_PRIV_H__
149