]> rtime.felk.cvut.cz Git - arc.git/blob - system/kernel/application.c
Cleanup of makefiles. Cleanup of merge.
[arc.git] / system / kernel / application.c
1 /* -------------------------------- Arctic Core ------------------------------\r
2  * Arctic Core - the open source AUTOSAR platform http://arccore.com\r
3  *\r
4  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>\r
5  *\r
6  * This source code is free software; you can redistribute it and/or modify it\r
7  * under the terms of the GNU General Public License version 2 as published by the\r
8  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.\r
9  *\r
10  * This program is distributed in the hope that it will be useful, but\r
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\r
13  * for more details.\r
14  * -------------------------------- Arctic Core ------------------------------*/\r
15 \r
16 /* ----------------------------[includes]------------------------------------*/\r
17 \r
18 #include <stdlib.h>\r
19 #include <stdint.h>\r
20 #include "Os.h"\r
21 \r
22 #include "internal.h"\r
23 #include "arc.h"\r
24 #include "arch.h"\r
25 \r
26 /* ----------------------------[private define]------------------------------*/\r
27 /* ----------------------------[private macro]-------------------------------*/\r
28 #define APPL_ID_TO_MASK(_x)   (1<<(_x))\r
29 \r
30 /* ----------------------------[private typedef]-----------------------------*/\r
31 /* ----------------------------[private function prototypes]-----------------*/\r
32 /* ----------------------------[private variables]---------------------------*/\r
33 #if OS_APPLICATION_CNT!=0\r
34 OsAppVarType Os_AppVar[OS_APPLICATION_CNT];\r
35 #endif\r
36 \r
37 /* ----------------------------[private functions]---------------------------*/\r
38 /* ----------------------------[public functions]----------------------------*/\r
39 \r
40 \r
41 /* @req OS547\r
42  *   Availability of AllowAccess(): Available in Scalability Classes 3 and 4.\r
43  * @req OS536\r
44  *    Availability of TerminateApplication(): Available in Scalability Classes 3 and 4.\r
45  * @req OS520\r
46  *    Availability  of  CheckObjectOwnership():Available  in  Scalability  Classes 3 and 4.\r
47  *\r
48  */\r
49 \r
50 \r
51 #if     (OS_USE_APPLICATIONS == STD_ON)\r
52 \r
53 /**\r
54  * This service determines the currently running OS-Application (a unique\r
55  * identifier has to be allocated to each application).\r
56  *
57  * @return <identifier of running OS-Application> or INVALID_OSAPPLICATION
58  */\r
59 \r
60 ApplicationType GetApplicationID( void ) {\r
61         return Os_Sys.currApplId;\r
62 }\r
63 \r
64 \r
65 /**\r
66  * A (trusted or non-trusted) OS-Application uses this service to call a trusted\r
67  * function\r
68  *
69  * @param FunctionIndex         Index of the function to be called.
70  * @param FunctionParams    Pointer to the parameters for the function -\r
71  *                                                      specified by the FunctionIndex - to be called.\r
72  *                                                      If no parameters are provided, a NULL pointer has\r
73  *                                                      to be passed.
74  * @return
75  */\r
76 StatusType      CallTrustedFunction(    TrustedFunctionIndexType FunctionIndex,\r
77                                                                         TrustedFunctionParameterRefType FunctionParams ) {\r
78 \r
79 \r
80         return E_OK;\r
81 }\r
82 \r
83 \r
84 \r
85 /**\r
86  * This service checks if a memory region is write/read/execute accessible\r
87  * and also returns information if the memory region is part of the stack\r
88  * space.\r
89  *
90  * @param ISRID         ISR reference
91  * @param Address   Start of memory area
92  * @param Size      Size of memory area
93  * @return
94  */\r
95 AccessType CheckISRMemoryAccess( ISRType isrId,\r
96                                                                 MemoryStartAddressType address,\r
97                                                                 MemorySizeType size )\r
98 {\r
99         ptrdiff_t addr = (ptrdiff_t)address;\r
100         (void)addr;\r
101         (void)size;\r
102 \r
103         if( isrId > OS_TASK_CNT ) {\r
104                 return 0;\r
105         }\r
106         return 0;\r
107 }\r
108 /**\r
109  * This service checks if a memory region is write/read/execute accessible\r
110  * and also returns information if the memory region is part of the stack\r
111  * space.\r
112  *\r
113  * Check returned accesstype with:\r
114  *   OSMEMORY_IS_READABLE(<AccessType>)\r
115  *   OSMEMORY_IS_WRITEABLE(<AccessType>)\r
116  *   OSMEMORY_IS_EXECUTABLE(<AccessType>)\r
117  *   OSMEMORY_IS_STACKSPACE(<AccessType>)\r
118  *\r
119  * TODO: Not really sure what this function is actually good for? Add a use-case!\r
120  *
121  * @param TaskID   Task reference
122  * @param Address  Start of memory area
123  * @param Size     Size of memory area
124  * @return
125  */\r
126 AccessType CheckTaskMemoryAccess(       TaskType taskId,\r
127                                                                         MemoryStartAddressType address,\r
128                                                                         MemorySizeType size )\r
129 {\r
130         ptrdiff_t addr = (ptrdiff_t)address;\r
131         (void)addr;\r
132         (void)size;\r
133 \r
134         /* @req OS270:\r
135          * if the Task reference <TaskID> in a call of CheckTaskMemoryAccess() is\r
136          * not valid, CheckTaskMemoryAccess() shall yield no access rights.\r
137          */\r
138         if( taskId > OS_TASK_CNT ) {\r
139                 return 0;\r
140         }\r
141 \r
142         /* TODO: Add body :) */\r
143         return 0;\r
144 }\r
145 \r
146 \r
147 /**\r
148  * This service determines if the OS-Applications, given by ApplID,\r
149  * is allowed to use the IDs of a Task, ISR, Resource, Counter,\r
150  * Alarm or Schedule Table in API calls.\r
151  *\r
152  * @param ApplID      OS-Application identifier
153  * @param ObjectType  Type of the following parameter
154  * @param object      The object to be examined
155  * @return ACCESS if the ApplID has access to the object\r
156  * NO_ACCESS otherwise
157  */\r
158 ObjectAccessType CheckObjectAccess( ApplicationType ApplId,\r
159                                                                         ObjectTypeType ObjectType,\r
160                                                                         uint32_t objectId )\r
161 {\r
162         uint32 appMask = APPL_ID_TO_MASK(ApplId);\r
163         ObjectAccessType orv;\r
164         _Bool rv = 0;\r
165 \r
166 \r
167         /* @req OS423\r
168          * If in a call of CheckObjectAccess() the object to  be examined\r
169          * is not avalid object OR <ApplID> is invalid OR <ObjectType> is\r
170          * invalid THEN CheckObjectAccess() shall return NO_ACCESS.\r
171          */\r
172         if( ApplId > OS_APPLICATION_CNT ) {\r
173                 return NO_ACCESS;\r
174         }\r
175 \r
176         /* @req OS272\r
177          * If the OS-Application <ApplID> in a call of CheckObjectAccess() has no\r
178          * access to the queried object, CheckObjectAccess() shall return NO_ACCESS.\r
179          *\r
180          * TODO: Could be that OS450 comes into play here....and then this is wrong.\r
181          */\r
182         if( Os_AppVar[ApplId].state != APPLICATION_ACCESSIBLE ) {\r
183                 return NO_ACCESS;\r
184         }\r
185 \r
186         /* TODO: check id */\r
187         switch( ObjectType ) {\r
188         case OBJECT_ALARM:\r
189                 rv =  ((OsAlarmType *)objectId)->accessingApplMask & (appMask);\r
190                 break;\r
191         case OBJECT_COUNTER:\r
192                 rv =  ((OsCounterType *)objectId)->accessingApplMask & (appMask);\r
193                 break;\r
194         case OBJECT_ISR:\r
195                 /* TODO: Add more things here for missing objects */\r
196                 break;\r
197         case OBJECT_MESSAGE:\r
198         case OBJECT_RESOURCE:\r
199         case OBJECT_SCHEDULETABLE:\r
200                 break;\r
201         case OBJECT_TASK:\r
202                 rv = ((OsCounterType *)objectId)->accessingApplMask & (appMask);\r
203                 break;\r
204         default:\r
205                 /* @req OS423 */\r
206                 rv = NO_ACCESS;\r
207                 break;\r
208         }\r
209 \r
210         orv = rv ? ACCESS : NO_ACCESS;\r
211 \r
212         return orv;\r
213 }\r
214 \r
215 /**\r
216  * This service determines to which OS-Application a given Task, ISR, Resource,\r
217  * Counter, Alarm or Schedule Table belongs\r
218  *
219  * @param ObjectType Type of the following parameter
220  * @param object     The object to be examined\r
221  * @return The OS-Application to which the object ObjectType belongs or\r
222  * INVALID_OSAPPLICATION if the object does not exists
223  */\r
224 ApplicationType CheckObjectOwnership( ObjectTypeType ObjectType,\r
225                                                                         uint32_t objectId )\r
226 {\r
227         ApplicationType rv = INVALID_OSAPPLICATION;\r
228 \r
229         switch( ObjectType ) {\r
230         case OBJECT_ALARM:\r
231                 break;\r
232         case OBJECT_COUNTER:\r
233                 break;\r
234         case OBJECT_ISR:\r
235                 break;\r
236         case OBJECT_MESSAGE:\r
237                 break;\r
238         case OBJECT_RESOURCE:\r
239                 break;\r
240         case OBJECT_SCHEDULETABLE:\r
241                 break;\r
242         case OBJECT_TASK:\r
243 #if 0\r
244                 if( objectId < OS_TASK_CNT ) {\r
245                         rv = Os_TaskGet(objectId)\r
246                 }\r
247 \r
248 \r
249                 OsTaskVarTypeapplOwnerId\r
250                 if( objectId > OS_MAX )\r
251 #endif\r
252                 break;\r
253         default:\r
254                 break;\r
255         }\r
256 \r
257         return rv;\r
258 }\r
259 \r
260 \r
261 /**\r
262  * This service terminates the OS-Application to which the calling Task/Category 2\r
263  * ISR/application specific error hook belongs.\r
264  *
265  * @param       Application - The identifier of the OS-Application to be terminated.\r
266  *                      If the caller belongs to <Application> the call results in a\r
267  *                      self termination.\r
268  *\r
269  * @param       RestartOption - Either RESTART for doing a restart of the\r
270  *                      OS-Application or NO_RESTART if OS-Application shall not be restarted.\r
271  *
272  * @return  E_OK: No errors\r
273  *                      E_OS_ID: <Application> was not valid\r
274  *                      E_OS_VALUE: <RestartOption> was neither RESTART nor NO_RESTART\r
275  *                      E_OS_ACCESS: The caller does not have the right to terminate <Application>\r
276  *                      E_OS_STATE: The state of <Application> does not allow terminating <Application>
277  */\r
278 StatusType TerminateApplication(  ApplicationType applId, RestartType restartOption ) {\r
279         (void)applId;\r
280         (void)restartOption;\r
281         return E_OK;\r
282 }\r
283 \r
284 \r
285 /**\r
286  * This service sets the own state of an OS-Application from\r
287  * APPLICATION_RESTARTING to APPLICATION_ACCESSIBLE.\r
288  *
289  * @return  E_OK : No errors\r
290  *                      E_OS_STATE : The OS-Application of the caller is in the wrong\r
291 state
292  */\r
293 StatusType AllowAccess( void ) {\r
294         ApplicationType applId = Os_Sys.currApplId;\r
295 \r
296         /* @req OS497 */\r
297         if( Os_AppVar[applId].state != APPLICATION_RESTARTING ) {\r
298                 return E_OS_STATE;\r
299         }\r
300 \r
301         /* @req OS498 */\r
302         Os_AppVar[applId].state = APPLICATION_ACCESSIBLE;\r
303         return E_OK;\r
304 }\r
305 \r
306 /**\r
307  * This service returns the current state of an OS-Application.\r
308  * SC: SC3 and SC4\r
309  *
310  * @param ApplId                The OS-Application from which the state is requested
311  * @param Value                 The current state of the application
312  * @return  E_OK: No errors, E_OS_ID: <Application> is not valid
313  */\r
314 StatusType GetApplicationState(   ApplicationType applId,  ApplicationStateRefType value ) {\r
315 \r
316         if(applId > OS_APPLICATION_CNT ) {\r
317                 return E_OS_ID;\r
318         }\r
319 \r
320         *value = Os_AppVar[applId].state;\r
321 \r
322         return E_OK;\r
323 }\r
324 \r
325 \r
326 /**\r
327  * TODO: Move somewhere else
328  * @param mode
329  * @return
330  */\r
331 StatusType GetActiveApplicationMode( AppModeType* mode) {\r
332          *mode = Os_Sys.appMode;\r
333          return E_OK;\r
334 }\r
335 \r
336 /**\r
337  *
338  */\r
339 void Os_ApplStart( void ) {\r
340         uint16_t i;\r
341 \r
342         /* Call startuphooks for all applications */\r
343         for(i=0;i<OS_APPLICATION_CNT;i++) {\r
344 \r
345                 Os_AppVar[i].state = APPLICATION_ACCESSIBLE;\r
346 \r
347                 if( Os_AppConst[i].StartupHook != NULL ) {\r
348                         Os_AppConst[i].StartupHook();\r
349                 }\r
350         }\r
351 }\r
352 \r
353 #endif\r