POK
/home/jaouen/pok_official/pok/trunk/libpok/arinc653/process.c
00001 /*
00002  *                               POK header
00003  *
00004  * The following file is a part of the POK project. Any modification should
00005  * made according to the POK licence. You CANNOT use this file or a part of
00006  * this file is this part of a file for your own project
00007  *
00008  * For more information on the POK licence, please see our LICENCE FILE
00009  *
00010  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
00011  *
00012  *                                      Copyright (c) 2007-2009 POK team
00013  *
00014  * Created by julien on Thu Jan 15 23:34:13 2009
00015  */
00016 
00017 
00018 #ifdef POK_NEEDS_ARINC653_PROCESS
00019 
00020 #include <core/dependencies.h>
00021 
00022 #include <core/thread.h>
00023 #include <arinc653/arincutils.h>
00024 #include <arinc653/types.h>
00025 #include <arinc653/process.h>
00026 #include <libc/string.h>
00027 
00028 void GET_PROCESS_ID (PROCESS_NAME_TYPE process_name[MAX_NAME_LENGTH],
00029                                                                                  PROCESS_ID_TYPE   *process_id,
00030                                                                                  RETURN_CODE_TYPE  *return_code )
00031 {
00032         int id;
00033 
00034         if ((id = process_name_exist(process_name)) == 0)
00035                 {
00036                         *process_id = id;
00037                         *return_code = INVALID_CONFIG;
00038                 }
00039         else
00040                 {
00041                         *process_id = id;
00042                         *return_code = NO_ERROR;
00043                 }
00044 }
00045 
00046 void GET_MY_ID (PROCESS_ID_TYPE   *process_id,
00047                 RETURN_CODE_TYPE  *return_code )
00048 {
00049         pok_ret_t         core_ret;
00050         uint32_t                        thread_id;
00051 
00052         core_ret = pok_thread_id (&thread_id);
00053         if (core_ret != 0)
00054                 *return_code = INVALID_MODE;
00055         *process_id = thread_id;
00056         *return_code = NO_ERROR;
00057 }
00058 
00059 void GET_PROCESS_STATUS (PROCESS_ID_TYPE     process_id,
00060                                                                                                  PROCESS_STATUS_TYPE *process_status,
00061                                                                                                  RETURN_CODE_TYPE    *return_code )
00062 {
00063         pok_thread_attr_t       attr;
00064         pok_ret_t               core_ret;
00065 
00066         core_ret = pok_thread_status (process_id, &attr);
00067         if (core_ret ==  POK_ERRNO_PARAM)
00068                 {
00069                         *return_code =  INVALID_PARAM;
00070                         return ;
00071                 }
00072         process_status->DEADLINE_TIME = attr.deadline;
00073         process_status->PROCESS_STATE = attr.state;
00074         strcpy(process_status->ATTRIBUTES.NAME, arinc_process_attribute[process_id].NAME);
00075         process_status->ATTRIBUTES.BASE_PRIORITY = arinc_process_attribute[process_id].BASE_PRIORITY;
00076         process_status->ATTRIBUTES.DEADLINE = HARD;
00077         process_status->CURRENT_PRIORITY = attr.priority;
00078         process_status->ATTRIBUTES.PERIOD = attr.period;
00079         process_status->ATTRIBUTES.TIME_CAPACITY = attr.time_capacity;
00080         process_status->ATTRIBUTES.ENTRY_POINT = attr.entry;
00081         process_status->ATTRIBUTES.STACK_SIZE = attr.stack_size;
00082         *return_code = NO_ERROR;
00083 }
00084 
00085 void CREATE_PROCESS (PROCESS_ATTRIBUTE_TYPE  *attributes,
00086                                                                                  PROCESS_ID_TYPE         *process_id,
00087                                                                                  RETURN_CODE_TYPE        *return_code )
00088 {
00089          pok_thread_attr_t core_attr;
00090          pok_ret_t         core_ret;
00091          uint32_t          core_process_id;
00092 
00093          if (process_name_exist(&attributes->NAME))
00094                  {
00095                          *return_code = NO_ACTION;
00096                          return;
00097                  }
00098          if (attributes->BASE_PRIORITY > MAX_PRIORITY_VALUE || attributes->BASE_PRIORITY < MIN_PRIORITY_VALUE)
00099                  {
00100                          *return_code = INVALID_PARAM;
00101                          return;
00102                  }
00103          core_attr.priority        = (uint8_t) attributes->BASE_PRIORITY;
00104          core_attr.entry           = attributes->ENTRY_POINT;
00105          core_attr.period          = attributes->PERIOD;
00106          core_attr.deadline        = attributes->DEADLINE;
00107          core_attr.time_capacity   = attributes->TIME_CAPACITY;
00108          core_attr.stack_size      = attributes->STACK_SIZE;
00109 
00110          core_ret = pok_thread_create (&core_process_id, &core_attr);
00111          arinc_process_attribute[core_process_id].BASE_PRIORITY = attributes->BASE_PRIORITY;
00112          strcpy(arinc_process_attribute[core_process_id].NAME, attributes->NAME);
00113          *process_id = core_process_id;
00114          *return_code = core_ret;
00115    if(core_ret != POK_ERRNO_OK) {
00116            return;
00117    }
00118    // ARINC specifies that threads shall be created in the DORMANT state
00119    core_ret = pok_thread_suspend_target(core_process_id);
00120    *return_code = core_ret;
00121 }
00122 
00123 void STOP_SELF ()
00124 {
00125          pok_thread_stop_self ();
00126 }
00127 
00128 
00129 void SET_PRIORITY (PROCESS_ID_TYPE  process_id,
00130                                                                          PRIORITY_TYPE    priority,
00131                                                                          RETURN_CODE_TYPE *return_code )
00132 {
00133         pok_thread_attr_t core_attr;
00134         pok_ret_t         core_ret;
00135 
00136         core_ret = pok_thread_status (process_id, &core_attr);
00137         if (core_ret != POK_ERRNO_OK)
00138                 {
00139                         *return_code =  INVALID_PARAM;
00140                         return;
00141                 }
00142         if (priority > MAX_PRIORITY_VALUE || priority < MIN_PRIORITY_VALUE)
00143                 {
00144                         *return_code = INVALID_PARAM;
00145                         return;
00146                 }
00147         if (core_attr.state == DORMANT)
00148                 {
00149                         *return_code = INVALID_MODE;
00150                         return;
00151                 }
00152         core_ret = pok_thread_set_priority(process_id, priority);
00153         *return_code = core_ret;
00154 }
00155 
00156 #ifndef POK_CONFIG_OPTIMIZE_FOR_GENERATED_CODE
00157 void SUSPEND_SELF (SYSTEM_TIME_TYPE time_out,
00158                                                                          RETURN_CODE_TYPE *return_code )
00159 {
00160          (void) time_out;
00161          *return_code = NOT_AVAILABLE;
00162 }
00163 
00164 #endif
00165 
00166 void SUSPEND (PROCESS_ID_TYPE    process_id,
00167                                                         RETURN_CODE_TYPE   *return_code )
00168 {
00169         pok_thread_attr_t  attr;
00170         pok_ret_t    core_ret;
00171 
00172         core_ret = pok_thread_status (process_id, &attr);
00173         if (attr.state == DORMANT)
00174                 {
00175                         *return_code = INVALID_MODE;
00176                         return ;
00177                 }
00178         if (attr.period == INFINITE_TIME_VALUE)
00179                 {
00180                         *return_code = INVALID_MODE;
00181                         return ;
00182                 }
00183         if (attr.state == WAITING)
00184                 {
00185                         *return_code = NO_ACTION;
00186                         return ;
00187                 }
00188         core_ret = pok_thread_suspend_target (process_id);
00189         *return_code = core_ret;
00190 }
00191 
00192 void RESUME (PROCESS_ID_TYPE     process_id,
00193                                                  RETURN_CODE_TYPE    *return_code )
00194 {
00195         pok_thread_attr_t  attr;
00196         pok_ret_t    core_ret;
00197 
00198         core_ret = pok_thread_status (process_id, &attr);
00199         if (core_ret != 0)
00200                 {
00201                         *return_code = INVALID_PARAM;
00202                         return ;
00203                 }
00204         if (attr.state == DORMANT)
00205                 {
00206                         *return_code = INVALID_MODE;
00207                         return ;
00208                 }
00209         if (attr.period == INFINITE_TIME_VALUE)
00210                 {
00211                         *return_code = INVALID_MODE;
00212                         return ;
00213                 }
00214         if (attr.state != WAITING)
00215                 {
00216                         *return_code = INVALID_MODE;
00217                         return ;
00218                 }
00219         core_ret = pok_thread_resume (process_id);
00220         *return_code = core_ret;
00221 }
00222 
00223 void START (PROCESS_ID_TYPE   process_id,
00224                                          RETURN_CODE_TYPE   *return_code )
00225 {
00226         DELAYED_START(process_id,0,return_code);
00227 }
00228 
00229 #ifndef POK_CONFIG_OPTIMIZE_FOR_GENERATED_CODE
00230 void STOP (PROCESS_ID_TYPE    process_id,
00231                                                 RETURN_CODE_TYPE *return_code )
00232 {
00233          (void) process_id;
00234          *return_code = NOT_AVAILABLE;
00235 }
00236 #endif
00237 
00238 void DELAYED_START (PROCESS_ID_TYPE   process_id,
00239                                 SYSTEM_TIME_TYPE  delay_time,
00240                                 RETURN_CODE_TYPE *return_code )
00241 {
00242   pok_thread_attr_t     attr;
00243   pok_ret_t             core_ret;
00244 
00245   core_ret = pok_thread_status (process_id, &attr);
00246   if (core_ret != POK_ERRNO_OK)
00247     {
00248       *return_code = INVALID_PARAM;
00249       return;
00250     }
00251     if (attr.state != DORMANT)
00252    {
00253      *return_code = NO_ACTION;
00254     return;
00255    }
00256   if (delay_time == INFINITE_TIME_VALUE)
00257     {
00258       *return_code = INVALID_PARAM;
00259       return;
00260     }
00261   /*if ((int)attr.period != INFINITE_TIME_VALUE && delay_time >= attr.period)
00262   {
00263     *return_code = INVALID_PARAM;
00264     return;
00265   }*/
00266   core_ret = pok_thread_delayed_start(process_id, delay_time);
00267   if (core_ret == POK_ERRNO_OK) {
00268     *return_code = NO_ERROR;
00269   }else {
00270     *return_code = INVALID_PARAM;
00271   }
00272 }
00273 
00274 #ifndef POK_CONFIG_OPTIMIZE_FOR_GENERATED_CODE
00275 void LOCK_PREEMPTION (LOCK_LEVEL_TYPE     *lock_level,
00276                                                                                         RETURN_CODE_TYPE    *return_code )
00277 {
00278          (void) lock_level;
00279          *return_code = NOT_AVAILABLE;
00280 }
00281 
00282 void UNLOCK_PREEMPTION (LOCK_LEVEL_TYPE   *lock_level,
00283                                                                                                 RETURN_CODE_TYPE  *return_code )
00284 
00285 {
00286          (void) lock_level;
00287          *return_code = NOT_AVAILABLE;
00288 }
00289 #endif
00290 
00291 #endif