PolicyManager can judge the current car state
[apps/agl-service-windowmanager.git] / src / policy_manager / zipc / dummy_stm.c
1 #include <string.h>
2 #include "dummy_stm.h"
3
4 const char* gStmEventName[] = {
5     "activate",
6     "deactivate",
7     "parking_brake_off",
8     "parking_brake_on",
9     "accel_pedal_off",
10     "accel_pedal_on",
11     "timer_expired",
12     "lamp_off",
13     "lamp_on"
14 };
15
16 const int gStmEventNo[] = {
17     STM_EVT_NO_ACTIVATE,
18     STM_EVT_NO_DEACTIVATE,
19     STM_EVT_NO_PARKING_BRAKE_OFF,
20     STM_EVT_NO_PARKING_BRAKE_ON,
21     STM_EVT_NO_ACCEL_PEDAL_OFF,
22     STM_EVT_NO_ACCEL_PEDAL_ON,
23     STM_EVT_NO_TIMER_EXPIRED,
24     STM_EVT_NO_LAMP_OFF,
25     STM_EVT_NO_LAMP_ON
26 };
27
28 const char* gStmCategoryName[] = {
29     "homescreen",
30     "map",
31     "general",
32     "splitable",
33     "popup",
34     "system_alert",
35     "restriction",
36 };
37
38 const int gStmCategoryNo[] = {
39     STM_CTG_NO_HOMESCREEN,
40     STM_CTG_NO_MAP,
41     STM_CTG_NO_GENERAL,
42     STM_CTG_NO_SPLITABLE,
43     STM_CTG_NO_POPUP,
44     STM_CTG_NO_SYSTEM_ALERT,
45     STM_CTG_NO_RESTRICTION,
46 };
47
48 const char* gStmAreaName[] = {
49     "full",
50     "normal",
51     "split.main",
52     "split.sub",
53     "onscreen",
54     "restriction.normal",
55     "restriction.split.main",
56     "restriction.split.sub",
57 };
58
59 const int gStmAreaNo[] = {
60     STM_ARA_NO_FULL,
61     STM_ARA_NO_NORMAL,
62     STM_ARA_NO_SPLIT_MAIN,
63     STM_ARA_NO_SPLIT_SUB,
64     STM_ARA_NO_ON_SCREEN,
65     STM_ARA_NO_RESTRICTION_NORMAL,
66     STM_ARA_NO_RESTRICTION_SPLIT_MAIN,
67     STM_ARA_NO_RESTRICTION_SPLIT_SUB,
68 };
69
70 // String for state
71 const char* gStmParkingBrakeStateNo2Name[] = {
72     "parking_brake_off",
73     "parking_brake_on"
74 };
75
76 const char* gStmAccelPedalStateNo2Name[] = {
77     "accel_pedal_off",
78     "accel_pedal_on"
79 };
80
81 const char* gStmCarStateNo2Name[] = {
82     "car_stop",
83     "car_run"
84 };
85
86 const char* gStmLampStateNo2Name[] = {
87     "lamp_off",
88     "lamp_on"
89 };
90
91 const char* gStmLayoutNo2Name[] = {
92     "none",
93     "pu",
94     "sa",
95     "m1",
96     "m2",
97     "mf",
98     "s1",
99     "s2",
100     "g",
101     "hs",
102     "restriction",
103     "restriction.split.main",
104     "restriction.split.sub",
105 };
106
107 stm_state_t g_crr_state;
108 stm_state_t g_prv_state;
109 int g_prv_restriction_state_car_stop = 0;
110 int g_prv_apps_state_car_stop = 0;
111
112 void stmInitialize() {
113     // Initialize previous state
114     memset(&g_prv_state, 0, sizeof(g_prv_state));
115
116     g_prv_state.layer.on_screen.state   = gStmLayoutNoNone;
117     g_prv_state.layer.restriction.state = gStmLayoutNoNone;
118     g_prv_state.layer.apps.state        = gStmLayoutNoNone;
119     g_prv_state.layer.homescreen.state  = gStmLayoutNoNone;
120     g_prv_state.parking_brake.state = gStmParkingBrakeStateNoOn;
121     g_prv_state.accel_pedal.state   = gStmAccelPedalStateNoOff;
122     g_prv_state.car.state           = gStmCarStateNoStop;
123     g_prv_state.lamp.state          = gStmLampStateNoOff;
124
125     // Initialize current state
126     g_crr_state = g_prv_state;
127 }
128
129 int stmTransitionState(int event, stm_state_t* state) {
130     int event_no, category_no, area_no;
131     int restriction_state, apps_state;
132     int parking_brake_state, accel_pedal_state, car_state, lamp_state;
133
134     event_no    = event & STM_MSK_EVT_NO;
135     category_no = event & STM_MSK_CTG_NO;
136     area_no     = event & STM_MSK_ARA_NO;
137
138     // Backup previous state
139     g_prv_state = g_crr_state;
140
141     // Get previous state
142     restriction_state = g_prv_state.layer.restriction.state;
143     apps_state = g_prv_state.layer.apps.state;
144     parking_brake_state  = g_prv_state.parking_brake.state;
145     accel_pedal_state = g_prv_state.accel_pedal.state;
146     car_state  = g_prv_state.car.state;
147     lamp_state = g_prv_state.lamp.state;
148
149     // Clear flags
150     g_crr_state.layer.on_screen.is_changed = STM_FALSE;
151     g_crr_state.layer.restriction.is_changed = STM_FALSE;
152     g_crr_state.layer.apps.is_changed = STM_FALSE;
153     g_crr_state.layer.homescreen.is_changed = STM_FALSE;
154     g_crr_state.parking_brake.is_changed = STM_FALSE;
155     g_crr_state.accel_pedal.is_changed = STM_FALSE;
156     g_crr_state.car.is_changed = STM_FALSE;
157     g_crr_state.lamp.is_changed = STM_FALSE;
158
159     switch (event_no) {
160     case STM_EVT_NO_ACTIVATE:
161         switch (category_no) {
162         case STM_CTG_NO_HOMESCREEN:
163             // Apps layer
164             g_crr_state.layer.apps.state = gStmLayoutNoNone;
165             g_crr_state.layer.apps.is_changed = STM_TRUE;
166
167             // Homescreen layer
168             g_crr_state.layer.homescreen.state = gStmLayoutNoHs;
169             g_crr_state.layer.homescreen.is_changed = STM_TRUE;
170             break;
171         case STM_CTG_NO_MAP:
172             switch (area_no) {
173             case STM_ARA_NO_FULL:
174                 // Apps layer
175                 switch (apps_state) {
176                 case gStmLayoutNoMf:
177                     // nop
178                     break;
179                 default:
180                     g_crr_state.layer.apps.state = gStmLayoutNoMf;
181                     g_crr_state.layer.apps.is_changed = STM_TRUE;
182                     break;
183                 }
184                 break;
185             case STM_ARA_NO_NORMAL:
186                 // Apps layer
187                 switch (apps_state) {
188                 case gStmLayoutNoM1:
189                     // nop
190                     break;
191                 case gStmLayoutNoS1:
192                     g_crr_state.layer.apps.state = gStmLayoutNoM2;
193                     g_crr_state.layer.apps.is_changed = STM_TRUE;
194                     break;
195                 default:
196                     g_crr_state.layer.apps.state = gStmLayoutNoM1;
197                     g_crr_state.layer.apps.is_changed = STM_TRUE;
198                 }
199                 break;
200             case STM_ARA_NO_SPLIT_MAIN:
201                 // Apps layer
202                 switch (apps_state) {
203                 case gStmLayoutNoS1:
204                 case gStmLayoutNoS2:
205                     g_crr_state.layer.apps.state = gStmLayoutNoS2;
206                     g_crr_state.layer.apps.is_changed = STM_TRUE;
207                     break;
208                 default:
209                     // nop
210                     break;
211                 }
212                 break;
213             }
214             break;
215         case STM_CTG_NO_GENERAL:
216             switch (area_no) {
217             case STM_ARA_NO_NORMAL:
218                 // Apps layer
219                 switch (apps_state) {
220                 case gStmLayoutNoMf:
221                     // nop
222                     break;
223                 default:
224                     g_crr_state.layer.apps.state = gStmLayoutNoG;
225                     g_crr_state.layer.apps.is_changed = STM_TRUE;
226                     break;
227                 }
228                 break;
229             default:
230                 // nop
231                 break;
232             }
233             break;
234         case STM_CTG_NO_SPLITABLE:
235             switch (area_no) {
236             case STM_ARA_NO_NORMAL:
237                 // Apps layer
238                 switch (apps_state) {
239                 case gStmLayoutNoM1:
240                     g_crr_state.layer.apps.state = gStmLayoutNoM2;
241                     g_crr_state.layer.apps.is_changed = STM_TRUE;
242                     break;
243                 case gStmLayoutNoMf:
244                 case gStmLayoutNoS1:
245                     // nop
246                     break;
247                 default:
248                     g_crr_state.layer.apps.state = gStmLayoutNoS1;
249                     g_crr_state.layer.apps.is_changed = STM_TRUE;
250                     break;
251                 }
252                 break;
253             case STM_ARA_NO_SPLIT_MAIN:
254                 // Apps layer
255                 switch (apps_state) {
256                 case gStmLayoutNoS1:
257                     g_crr_state.layer.apps.state = gStmLayoutNoS2;
258                     g_crr_state.layer.apps.is_changed = STM_TRUE;
259                     break;
260                 case gStmLayoutNoS2:
261                     g_crr_state.layer.apps.state = gStmLayoutNoS2;
262                     g_crr_state.layer.apps.is_changed = STM_TRUE;
263                     break;
264                 default:
265                     // nop
266                     break;
267                 }
268                 break;
269             case STM_ARA_NO_SPLIT_SUB:
270                 // Apps layer
271                 switch (apps_state) {
272                 case gStmLayoutNoM1:
273                     g_crr_state.layer.apps.state = gStmLayoutNoM2;
274                     g_crr_state.layer.apps.is_changed = STM_TRUE;
275                     break;
276                 case gStmLayoutNoM2:
277                     g_crr_state.layer.apps.state = gStmLayoutNoM2;
278                     g_crr_state.layer.apps.is_changed = STM_TRUE;
279                     break;
280                 case gStmLayoutNoS1:
281                     g_crr_state.layer.apps.state = gStmLayoutNoS2;
282                     g_crr_state.layer.apps.is_changed = STM_TRUE;
283                     break;
284                 case gStmLayoutNoS2:
285                     g_crr_state.layer.apps.state = gStmLayoutNoS2;
286                     g_crr_state.layer.apps.is_changed = STM_TRUE;
287                     break;
288                 default:
289                     // nop
290                     break;
291                 }
292                 break;
293             default:
294                 // nop
295                 break;
296             }
297             break;
298         case STM_CTG_NO_RESTRICTION:
299             switch (area_no) {
300             case STM_ARA_NO_RESTRICTION_NORMAL:
301                 // restriction Layer
302                 switch (restriction_state) {
303                 case gStmLayoutNoNone:
304                     g_crr_state.layer.restriction.state = gStmLayoutNoRestriction;
305                     g_crr_state.layer.restriction.is_changed = STM_TRUE;
306                     break;
307                 default:
308                     // nop
309                     break;
310                 }
311                 break;
312             case STM_ARA_NO_RESTRICTION_SPLIT_MAIN:
313                 // restriction Layer
314                 switch (restriction_state) {
315                 case gStmLayoutNoNone:
316                     g_crr_state.layer.restriction.state = gStmLayoutNoRestrictionSplitMain;
317                     g_crr_state.layer.restriction.is_changed = STM_TRUE;
318                     break;
319                 default:
320                     // nop
321                     break;
322                 }
323                 break;
324             case STM_ARA_NO_RESTRICTION_SPLIT_SUB:
325                 // restriction Layer
326                 switch (restriction_state) {
327                 case gStmLayoutNoNone:
328                     g_crr_state.layer.restriction.state = gStmLayoutNoRestrictionSplitSub;
329                     g_crr_state.layer.restriction.is_changed = STM_TRUE;
330                     break;
331                 default:
332                     // nop
333                     break;
334                 }
335                 break;
336             default:
337                 // nop
338                 break;
339             }
340             break;
341         default:
342             // nop
343             break;
344         }
345     case STM_EVT_NO_DEACTIVATE:
346         switch (category_no) {
347         case STM_CTG_NO_RESTRICTION:
348             // restriction Layer
349             switch (restriction_state) {
350             case gStmLayoutNoRestriction:
351             case gStmLayoutNoRestrictionSplitMain:
352             case gStmLayoutNoRestrictionSplitSub:
353                 g_crr_state.layer.restriction.state = gStmLayoutNoNone;
354                 g_crr_state.layer.restriction.is_changed = STM_TRUE;
355                 break;
356             default:
357                 // nop
358                 break;
359             }
360         default:
361             // nop
362             break;
363         }
364         break;
365     case STM_EVT_NO_PARKING_BRAKE_OFF:
366         if (gStmParkingBrakeStateNoOff != parking_brake_state) {
367             g_crr_state.parking_brake.state = gStmParkingBrakeStateNoOff;
368             g_crr_state.parking_brake.is_changed = STM_TRUE;
369         }
370         break;
371     case STM_EVT_NO_PARKING_BRAKE_ON:
372         if (gStmParkingBrakeStateNoOn != parking_brake_state) {
373             g_crr_state.parking_brake.state = gStmParkingBrakeStateNoOn;
374             g_crr_state.parking_brake.is_changed = STM_TRUE;
375         }
376         break;
377     case STM_EVT_NO_ACCEL_PEDAL_OFF:
378         if (gStmAccelPedalStateNoOff != accel_pedal_state) {
379             g_crr_state.accel_pedal.state = gStmAccelPedalStateNoOff;
380             g_crr_state.accel_pedal.is_changed = STM_TRUE;
381         }
382         break;
383     case STM_EVT_NO_ACCEL_PEDAL_ON:
384         if (gStmAccelPedalStateNoOn != accel_pedal_state) {
385             g_crr_state.accel_pedal.state = gStmAccelPedalStateNoOn;
386             g_crr_state.accel_pedal.is_changed = STM_TRUE;
387         }
388         break;
389     case STM_EVT_NO_LAMP_OFF:
390         if (gStmLampStateNoOff != lamp_state) {
391             g_crr_state.lamp.state = gStmLampStateNoOff;
392             g_crr_state.lamp.is_changed = STM_TRUE;
393         }
394         break;
395     case STM_EVT_NO_LAMP_ON:
396         if (gStmLampStateNoOn != lamp_state) {
397             g_crr_state.lamp.state = gStmLampStateNoOn;
398             g_crr_state.lamp.is_changed = STM_TRUE;
399         }
400         break;
401     default:
402         // nop
403         break;
404     }
405
406     // Set car state
407     if (g_crr_state.parking_brake.is_changed
408         || g_crr_state.accel_pedal.is_changed) {
409         if ((gStmParkingBrakeStateNoOff == g_crr_state.parking_brake.state)
410             && (gStmAccelPedalStateNoOn == g_crr_state.accel_pedal.state)){
411             if (gStmCarStateNoRun != car_state) {
412                 // Car state is changed stop -> run
413                 g_crr_state.car.state = gStmCarStateNoRun;
414                 g_crr_state.car.is_changed = STM_TRUE;
415
416                 // Update restriction layer
417                 g_prv_restriction_state_car_stop = restriction_state;
418                 g_crr_state.layer.restriction.state = gStmLayoutNoNone;
419                 g_crr_state.layer.restriction.is_changed = STM_TRUE;
420
421                 // Update apps layer
422                 g_prv_apps_state_car_stop = apps_state;
423                 g_crr_state.layer.apps.state = gStmLayoutNoM1;
424                 g_crr_state.layer.apps.is_changed = STM_TRUE;
425             }
426         }
427         else {
428             if (gStmCarStateNoStop != car_state) {
429                 // Car state is changed run -> stop
430                 g_crr_state.car.state = gStmCarStateNoStop;
431                 g_crr_state.car.is_changed = STM_TRUE;
432
433                 // Update restriction layer
434                 g_crr_state.layer.restriction.state = g_prv_restriction_state_car_stop;
435                 g_crr_state.layer.restriction.is_changed = STM_TRUE;
436
437                 // Update apps layer
438                 g_crr_state.layer.apps.state = g_prv_apps_state_car_stop;
439                 g_crr_state.layer.apps.is_changed = STM_TRUE;
440             }
441         }
442     }
443
444     // Copy current state for return
445     memcpy(state, &g_crr_state, sizeof(g_crr_state));
446
447     return 0;
448 }