Add gitlab issue/merge request templates
[staging/basesystem.git] / service / system / task_manager / server / src / tskm_shutdown.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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 #include "tskm_shutdown.h"
18 #include "tskm_util.h"
19 #include "tskm_debug.h"
20 #include "tskm_state.h"
21
22 #include "tskm_port_pf.h"
23 #include "tskm_port_subsys.h"
24 #include "tskm_gstep.h"
25
26
27 /*********************************************************
28  *        Get context of gradual startup
29  *********************************************************/
30 TSKM_STATIC TSKM_GSTEP_CTX_t*
31 gstepGetDownCtx(TSKM_MAIN_CTX_t* p_main) {
32   return &p_main->down;
33 }
34
35 /*********************************************************
36  *        System shutdown completed
37  *********************************************************/
38 TSKM_STATIC void downFinish(TSKM_MAIN_CTX_t* p_main) {
39   int ret;
40
41   // Send FrameworkunifiedOnStop response
42   ret = tskm_pf_sendStopCompResp();
43   if (ret != 0) {  // LCOV_EXCL_BR_LINE 8: As ret never becomes non-zero
44     // LCOV_EXCL_START 8: As ret never becomes non-zero
45     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
46     TSKM_ASSERT(0);
47     // LCOV_EXCL_STOP
48   }
49
50   // Terminate subsystems
51   tskm_sub_term();
52 }
53 /*********************************************************
54  *        Challenge for a transition to the next state
55  *********************************************************/
56 TSKM_STATIC void tryTransNextState(TSKM_MAIN_CTX_t* p_main) {
57   TSKM_GSTEP_CTX_t* p_down;
58   TSKM_GSTEP_t* p_current;
59
60   p_down = gstepGetDownCtx(p_main);
61   p_current = gstepGetCurrent(p_down);
62
63   if (gstepIsLast(p_down)) {
64     TSKM_SVC_WAIT_STATE_t waitState;
65
66     // Checking the ShutdownWaitSVC termination status
67     waitState = tskm_svcsGetSvcTermWaitState(&p_main->svcs);
68     switch (waitState) {
69       case TSKM_SVC_WAIT_BOTH:
70         break;
71       case TSKM_SVC_WAIT_TRANSIENT:
72         if ((p_down->compState & INI_TERMCOMP_RESIDENT) != INI_TERMCOMP_RESIDENT) {
73           // Nonresident service termination processing
74           if (TSKM_E_OK != tskm_svcsAvtiveSvcTerm(&p_main->svcs)) {  // LCOV_EXCL_BR_LINE 5: tskm_svcsAvtiveSvcTerm is always return TSKM_E_OK
75             // LCOV_EXCL_START 5: tskm_svcsAvtiveSvcTerm is always return TSKM_E_OK
76             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
77             TSKM_ASSERT(0);
78             // LCOV_EXCL_STOP
79           }
80           // Nonresident SVC terminated and started
81           p_down->compState |= INI_TERMCOMP_RESIDENT;
82           TSKM_PRINTF(TSKM_LOG_SVCSTATE, "TERM COMP :%s(%#llx)",
83                       tskm_convTermCompId2Str(INI_TERMCOMP_RESIDENT),
84                       INI_TERMCOMP_RESIDENT);
85         }
86         break;
87       case TSKM_SVC_WAIT_NONE:
88         // Complete ShutdownWaitSVC
89         // Nonresident SVC terminated and started
90         p_down->compState |= INI_TERMCOMP_TRANSIENT;
91         TSKM_PRINTF(TSKM_LOG_SVCSTATE, "TERM COMP :%s(%#llx)",
92                     tskm_convTermCompId2Str(INI_TERMCOMP_TRANSIENT),
93                     INI_TERMCOMP_TRANSIENT);
94         break;
95       default:
96         // LCOV_EXCL_START 8: As it does not meet the default requirements
97         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
98         TSKM_ASSERT(0);
99         break;
100         // LCOV_EXCL_STOP
101     }
102   }
103
104   if (tskm_svcsIsWaiting(&p_main->svcs) == TSKM_FALSE &&  // No waiting services
105       ((p_down->compState & p_current->nextTransCond)
106           == p_current->nextTransCond)) {  // Event completion condition
107     if (gstepIsLast(p_down)) {
108       tskm_stateTransit(p_main, TSKM_ST_ACCON, TSKM_ST_ACCOFF);
109     } else {
110       tskm_stateTransit(p_main, TSKM_ST_DOWN, TSKM_ST_DOWN);
111     }
112   }
113 }
114
115 /*********************************************************
116  *        Check and process gradual termination request
117  *********************************************************/
118 TSKM_ERR_t tskm_entryDown_Check_Req(TSKM_MAIN_CTX_t* p_main,
119                                     TSKM_GSTEP_t* p_current) {
120   uint32_t ii;
121
122   if (0 == p_current->reqNum) {
123     EFrameworkunifiedStatus l_eStatus;
124
125     // If there is no gradual termination request, check the transition to the next step.
126     l_eStatus = FrameworkunifiedSendSelf(p_main->hApp, TSKM_TRANS_STEP_REQ, 0, NULL);
127     if (eFrameworkunifiedStatusOK != l_eStatus) {  // LCOV_EXCL_BR_LINE 5: Checked in death testing, but it is not reflected in the coverage and excluded
128       // LCOV_EXCL_START 5: Checked in death testing, but it is not reflected in the coverage and excluded
129       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
130       TSKM_ASSERT(0);
131       return TSKM_E_NG;
132       // LCOV_EXCL_STOP
133     }
134
135   } else {
136     // Gradual termination request
137     for (ii = 0; ii < p_current->reqNum; ii++) {
138       TSKM_ERR_t tskmRet;
139       TSKM_GSTEP_REQ_INFO_t* p_req = &p_current->reqList[ii];
140       TSKM_SVC_CTX_t* p_svc = tskm_svcsGetSvcBySvcId(&p_main->svcs,
141                                                      p_req->svcId);
142
143       tskmRet = tskm_svcDownRequest(p_svc, p_req);
144       if (tskmRet != TSKM_E_OK) {
145         TSKM_ASSERT(0);
146       }
147     }
148   }
149   return TSKM_E_OK;
150 }
151
152 /*********************************************************
153  *        Gradual termination entry process
154  *********************************************************/
155 TSKM_ERR_t tskm_entryDown(TSKM_MAIN_CTX_t* p_main) {
156   TSKM_FUNC_IN();
157
158   TSKM_GSTEP_CTX_t* p_down;
159   TSKM_GSTEP_t* p_current;
160
161   p_main->state = TSKM_ST_DOWN;
162   p_down = gstepGetDownCtx(p_main);
163   p_current = gstepGetCurrent(p_down);
164   if (p_current == NULL) {  // LCOV_EXCL_BR_LINE 200:p_current can not be null
165     // LCOV_EXCL_START 200:p_current can not be null
166     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
167     TSKM_ASSERT(0);
168     goto ERROR;
169     // LCOV_EXCL_STOP
170   }
171   TSKM_PRINTF(TSKM_LOG_SVCSTATE, "DOWN GSTEP:%d", p_down->gstepIdx);
172
173   if (tskm_entryDown_Check_Req(p_main, p_current) == TSKM_E_NG) {  // LCOV_EXCL_BR_LINE 200:the function of tskm_entryDown_Check_Req can not be TSKM_E_NG at this case // NOLINT(whitespace/line_length)
174     // LCOV_EXCL_START 200:tskm_entryDown_Check_Req can not be TSKM_E_NG at this case
175     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
176     goto ERROR;
177     // LCOV_EXCL_STOP
178   }
179
180   if (p_current->nextTransCond) {
181     TSKM_PRINTF(TSKM_LOG_STATE, "WAIT COMP:%s(%llx)",
182                 tskm_convTermCompId2Str(p_current->nextTransCond),
183                 p_current->nextTransCond);
184   }
185
186   TSKM_FUNC_OUT();
187   return TSKM_E_OK;
188
189   ERROR: tskm_pf_abort();
190   return TSKM_E_NG;
191 }
192
193 /*********************************************************
194  *        Gradual termination exit process
195  *********************************************************/
196 TSKM_ERR_t tskm_exitDown(TSKM_MAIN_CTX_t* p_main) {
197   TSKM_FUNC_IN();
198   TSKM_GSTEP_CTX_t* p_down;
199
200   p_down = gstepGetDownCtx(p_main);
201
202   if (gstepIsLast(p_down)) {
203     TSKM_PRINTF(TSKM_LOG_STATE, "DOWN FINISH");
204     downFinish(p_main);    // Final process of termination
205   }
206
207   changeNextStep(p_down);  // Proceed to the next step
208
209   TSKM_FUNC_OUT();
210   return TSKM_E_OK;
211 }
212 /*********************************************************
213  *        Hander during gradual termination
214  *********************************************************/
215 TSKM_ERR_t tskm_handleDown(TSKM_MAIN_CTX_t* p_main, TSKM_EVENT_INFO_t* p_ev) {
216   TSKM_FUNC_IN();
217
218   switch (p_ev->event) {
219     case TSKM_EV_PRI_REP_DOWN_COMP:  // Notificaton for event completion in termination
220     {
221       TSKM_PRINTF(TSKM_LOG_SVCSTATE, "TERM COMP :%s(%#llx) from:%d",
222                   tskm_convTermCompId2Str(p_ev->prm.repDownComp.compId),
223                   p_ev->prm.repDownComp.compId, p_ev->fromPid);
224       TSKM_GSTEP_CTX_t* p_down = gstepGetDownCtx(p_main);
225       p_down->compState |= p_ev->prm.repDownComp.compId;
226       tryTransNextState(p_main);
227     }
228       break;
229     case TSKM_EV_LCL_CHG_SVC_STATE:  // Service state change
230       tryTransNextState(p_main);
231       break;
232     case TSKM_EV_LCL_REQ_TRANS_STEP:  //Step transition request
233       tryTransNextState(p_main);
234       break;
235     default:
236       tskm_handleAccon(p_main, p_ev);
237       break;
238   }
239   TSKM_FUNC_OUT();
240   return TSKM_E_OK;
241 }  // LCOV_EXCL_BR_LINE 10: Final line
242