POK
semaphore.c
Go to the documentation of this file.
1 /*
2  * POK header
3  *
4  * The following file is a part of the POK project. Any modification should
5  * made according to the POK licence. You CANNOT use this file or a part of
6  * this file is this part of a file for your own project
7  *
8  * For more information on the POK licence, please see our LICENCE FILE
9  *
10  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
11  *
12  * Copyright (c) 2007-2009 POK team
13  *
14  * Created by julien on Thu Jan 15 23:34:13 2009
15  */
16 
22 #ifdef POK_NEEDS_ARINC653_SEMAPHORE
23 
24 #include <types.h>
25 #include <libc/string.h>
26 #include <arinc653/types.h>
27 #include <arinc653/semaphore.h>
28 #include <core/semaphore.h>
29 
30 #define CHECK_SEM_INIT if (pok_arinc653_semaphores_initialized == 0) \
31  { \
32  uint16_t bla; \
33  for (bla = 0 ; bla < POK_CONFIG_ARINC653_NB_SEMAPHORES ; bla++) \
34  {\
35  pok_arinc653_semaphores_layers[bla].ready = 0;\
36  }\
37  }\
38  pok_arinc653_semaphores_initialized = 1;
39 
40 pok_bool_t pok_arinc653_semaphores_initialized = 0;
41 
42 typedef struct
43 {
44  pok_bool_t ready;
45  pok_sem_id_t core_id;
47 
48 extern char* pok_arinc653_semaphores_names[POK_CONFIG_ARINC653_NB_SEMAPHORES];
49 pok_arinc653_semaphore_layer_t pok_arinc653_semaphores_layers[POK_CONFIG_ARINC653_NB_SEMAPHORES];
50 
51 void CREATE_SEMAPHORE (SEMAPHORE_NAME_TYPE SEMAPHORE_NAME,
52  SEMAPHORE_VALUE_TYPE CURRENT_VALUE,
53  SEMAPHORE_VALUE_TYPE MAXIMUM_VALUE,
54  QUEUING_DISCIPLINE_TYPE QUEUING_DISCIPLINE,
55  SEMAPHORE_ID_TYPE *SEMAPHORE_ID,
56  RETURN_CODE_TYPE *RETURN_CODE )
57 {
58 
59  RETURN_CODE_TYPE return_code_name;
60  pok_sem_id_t sem_id;
61  pok_ret_t core_ret;
62 
63  CHECK_SEM_INIT;
64 
65  *RETURN_CODE = INVALID_CONFIG;
66 
67  GET_SEMAPHORE_ID (SEMAPHORE_NAME, SEMAPHORE_ID, &return_code_name);
68 
69  if (return_code_name == INVALID_CONFIG)
70  {
71  *RETURN_CODE = INVALID_CONFIG;
72  return;
73  }
74 
75 
76  if (*SEMAPHORE_ID > POK_CONFIG_ARINC653_NB_SEMAPHORES)
77  {
78  *RETURN_CODE = INVALID_CONFIG;
79  return;
80  }
81 
82  if (pok_arinc653_semaphores_layers[*SEMAPHORE_ID].ready)
83  {
84  *RETURN_CODE = NO_ACTION;
85  return;
86  }
87 
88  core_ret = pok_sem_create (&sem_id, CURRENT_VALUE, MAXIMUM_VALUE, QUEUING_DISCIPLINE);
89 
90  if (core_ret != POK_ERRNO_OK)
91  {
92  *RETURN_CODE = INVALID_PARAM;
93  return;
94  }
95 
96  pok_arinc653_semaphores_layers[*SEMAPHORE_ID].ready = 1;
97  pok_arinc653_semaphores_layers[*SEMAPHORE_ID].core_id = sem_id;
98 
99  *RETURN_CODE = NO_ERROR;
100 }
101 
102 void WAIT_SEMAPHORE (SEMAPHORE_ID_TYPE SEMAPHORE_ID,
103  SYSTEM_TIME_TYPE TIME_OUT,
104  RETURN_CODE_TYPE *RETURN_CODE )
105 {
106  pok_ret_t core_ret;
107 
108  CHECK_SEM_INIT;
109 
110  if (SEMAPHORE_ID > POK_CONFIG_ARINC653_NB_SEMAPHORES)
111  {
112  *RETURN_CODE = INVALID_PARAM;
113  return;
114  }
115 
116  core_ret = pok_sem_wait (pok_arinc653_semaphores_layers[SEMAPHORE_ID].core_id, TIME_OUT);
117 
118  *RETURN_CODE = INVALID_PARAM;
119 
120  if (core_ret == POK_ERRNO_OK)
121  {
122  *RETURN_CODE = NO_ERROR;
123  }
124 
125  if (core_ret == POK_ERRNO_TIMEOUT)
126  {
127  *RETURN_CODE = TIMED_OUT;
128  }
129 }
130 
131 void SIGNAL_SEMAPHORE (SEMAPHORE_ID_TYPE SEMAPHORE_ID,
132  RETURN_CODE_TYPE *RETURN_CODE )
133 {
134  pok_ret_t core_ret;
135 
136  CHECK_SEM_INIT;
137 
138  if (SEMAPHORE_ID > POK_CONFIG_ARINC653_NB_SEMAPHORES)
139  {
140  *RETURN_CODE = INVALID_PARAM;
141  return;
142  }
143 
144  core_ret = pok_sem_signal (pok_arinc653_semaphores_layers[SEMAPHORE_ID].core_id);
145 
146  if (core_ret == POK_ERRNO_OK)
147  {
148  *RETURN_CODE = NO_ERROR;
149  }
150  else
151  {
152  *RETURN_CODE = INVALID_PARAM;
153  }
154 }
155 
156 void GET_SEMAPHORE_ID (SEMAPHORE_NAME_TYPE SEMAPHORE_NAME,
157  SEMAPHORE_ID_TYPE *SEMAPHORE_ID,
158  RETURN_CODE_TYPE *RETURN_CODE )
159 {
160  uint16_t i;
161  uint16_t len;
162 
163  CHECK_SEM_INIT;
164 
165  len = strlen (SEMAPHORE_NAME);
166 
167  *RETURN_CODE = INVALID_CONFIG;
168 
169  for (i = 0 ; i < POK_CONFIG_ARINC653_NB_SEMAPHORES ; i++)
170  {
171  if (strncmp (pok_arinc653_semaphores_names[i], SEMAPHORE_NAME, len) == 0)
172  {
173  *SEMAPHORE_ID = i;
174  *RETURN_CODE = NO_ERROR;
175  break;
176  }
177  }
178 }
179 
180 void GET_SEMAPHORE_STATUS (SEMAPHORE_ID_TYPE SEMAPHORE_ID,
181  SEMAPHORE_STATUS_TYPE *SEMAPHORE_STATUS,
182  RETURN_CODE_TYPE *RETURN_CODE )
183 {
184  (void) SEMAPHORE_ID;
185  (void) SEMAPHORE_STATUS;
186 
187  CHECK_SEM_INIT;
188 
189  *RETURN_CODE = NO_ERROR;
190 }
191 
192 #endif