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