Init basesystem source codes.
[staging/basesystem.git] / nsframework / backup_manager / server / src / backupmanager_application.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 <native_service/ns_backup.h>
18 #include <agl_thread.h>
19 #include <system_service/ss_system_if.h>
20 #include <system_service/ss_system_manager_notifications.h>
21 #include <native_service/frameworkunified_multithreading.h>
22
23 #include <nv_hal.h>
24 #include <signal.h>
25 #include <cerrno>
26 #include <cstdio>
27 #include "bkup_api.h"
28 #include "bkup_param.h"
29 #include "bkup_backupmanagerlog.h"
30 #include "bkup_process.h"
31
32 pthread_t g_work_thread_id = 0;
33 pthread_t g_delay_thread_id = 0;
34 pthread_t g_nand_thread_id = 0;
35 HANDLE g_msg_handle_thread = NULL;
36
37 typedef void (* signal_handler)(int);
38
39 void SignalHandlerFuncForMsgHandleThread(int signum) {
40   pthread_exit(0);
41 }
42
43 EFrameworkunifiedStatus MsgHandleThreadStart(HANDLE h_app) {
44   signal_handler p_signal = SignalHandlerFuncForMsgHandleThread;
45   signal(SIGUSR1, p_signal);
46
47   g_work_thread_id = pthread_self();
48
49   EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
50   // LCOV_EXCL_BR_START 4: NSFW error case
51   if ((e_status = FrameworkunifiedAttachCallbackToDispatcher(h_app, FRAMEWORKUNIFIED_ANY_SOURCE, BACKUP_CID, BkupHandler)) != eFrameworkunifiedStatusOK) {
52   // LCOV_EXCL_BR_STOP 4: NSFW error case
53     // LCOV_EXCL_START 4: NSFW error case
54     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
55     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedAttachCallbacksToDispatcher e_status:%d\n", e_status);
56     exit(EXIT_FAILURE);
57     // LCOV_EXCL_STOP 4: NSFW error case
58   }
59
60   return e_status;
61 }
62
63 EFrameworkunifiedStatus MsgHandleThreadStop(HANDLE h_app) {
64   EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
65   return e_status;
66 }
67
68 EFrameworkunifiedStatus FrameworkunifiedOnInitialization(HANDLE h_app) {
69   EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
70   pthread_attr_t t_attr;
71   pthread_t t;
72
73   // LCOV_EXCL_BR_START 4: NSFW error case
74   if ((e_status = FrameworkunifiedRegisterServiceAvailabilityNotification(h_app, NTFY_BackupMgr_Availability)) != eFrameworkunifiedStatusOK) {
75   // LCOV_EXCL_BR_STOP 4: NSFW error case
76     // LCOV_EXCL_START 4: NSFW error case
77     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
78     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedRegisterServiceAvailabilityNotification e_status:%d\n", e_status);
79     exit(EXIT_FAILURE);
80     // LCOV_EXCL_STOP 4: NSFW error case
81   }
82
83   if (pthread_attr_init(&t_attr) < 0) {  // LCOV_EXCL_BR_LINE 5:pthread_attr_init's error case.
84     // LCOV_EXCL_START 5:pthread_attr_init's error case.
85     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
86     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "pthread_attr_init:%s", strerror(errno));
87     exit(EXIT_FAILURE);
88     // LCOV_EXCL_STOP 5:pthread_attr_init's error case.
89   }
90
91   // LCOV_EXCL_BR_START 5:pthread_attr_setinheritsched's error case.
92   if (pthread_attr_setinheritsched(&t_attr, PTHREAD_INHERIT_SCHED) < 0) {
93   // LCOV_EXCL_BR_STOP 5:pthread_attr_setinheritsched's error case.
94     // LCOV_EXCL_START 5:pthread_attr_setinheritsched's error case.
95     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
96     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "pthread_attr_setinheritsched:%s", strerror(errno));
97     exit(EXIT_FAILURE);
98     // LCOV_EXCL_STOP 5:pthread_attr_setinheritsched's error case.
99   }
100
101   if (pthread_create(&t, &t_attr, BkupNandThread, NULL) < 0) {  // LCOV_EXCL_BR_LINE 5:pthread_create's error case.
102     // LCOV_EXCL_START 5:pthread_create's error case.
103     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
104     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "pthread_create:%s", strerror(errno));
105     exit(EXIT_FAILURE);
106     // LCOV_EXCL_STOP 5:pthread_create's error case.
107   }
108
109   if (pthread_create(&t, &t_attr, BkupDelayThread, NULL) < 0) {  // LCOV_EXCL_BR_LINE 5:pthread_create's error case.
110     // LCOV_EXCL_START 5:pthread_create's error case.
111     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
112     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "pthread_create:%s", strerror(errno));
113     exit(EXIT_FAILURE);
114     // LCOV_EXCL_STOP 5:pthread_create's error case.
115   }
116
117   e_status = InitNv();
118   if (e_status != eFrameworkunifiedStatusOK) {
119     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "InitNv e_status:%d", e_status);
120     exit(EXIT_FAILURE);
121   }
122
123   if (BckupParamInit() < 0) {  // LCOV_EXCL_BR_LINE 5:C API's error case.
124     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
125     exit(EXIT_FAILURE);  // LCOV_EXCL_LINE 5:C API's error case.
126   }
127
128   // LCOV_EXCL_BR_START 4: NSFW error case
129   if ((e_status = FrameworkunifiedAttachCallbackToDispatcher(h_app, FRAMEWORKUNIFIED_ANY_SOURCE, BACKUP_CID, BkupHandler)) != eFrameworkunifiedStatusOK) {
130   // LCOV_EXCL_BR_STOP 4: NSFW error case
131     // LCOV_EXCL_START 4: NSFW error case
132     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
133     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedAttachCallbacksToDispatcher e_status:%d\n", e_status);
134     exit(EXIT_FAILURE);
135     // LCOV_EXCL_STOP 4: NSFW error case
136   }
137
138
139   g_msg_handle_thread = FrameworkunifiedCreateChildThread(h_app, SERVICE_BACKUP_MANAGER_MSG_HANDLE_THREAD, MsgHandleThreadStart, MsgHandleThreadStop);  // LCOV_EXCL_BR_LINE 11:Unexpected branch  // NOLINT (whitespace/line_length)
140   if (g_msg_handle_thread == NULL) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.
141     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
142     // LCOV_EXCL_START 4: NSFW error case.
143     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
144         "Fail to create SERVICE_BACKUP_MANAGER_MSG_HANDLE_THREAD");
145     // LCOV_EXCL_STOP 4: NSFW error case.
146   } else {
147     if (eFrameworkunifiedStatusOK != (e_status = FrameworkunifiedStartChildThread(h_app, g_msg_handle_thread, 0, NULL))) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.  // NOLINT (whitespace/line_length)
148       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
149       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Fail to Start SERVICE_BACKUP_MANAGER_MSG_HANDLE_THREAD. Status:%#x", e_status);  // LCOV_EXCL_LINE 4: NSFW error case.  // NOLINT (whitespace/line_length)
150     }
151   }
152
153   // LCOV_EXCL_BR_START 4: NSFW error case
154   if ((e_status = FrameworkunifiedSubscribeNotificationWithCallback(h_app,
155                                                        NTFY_SSSystemMgrPowerOnOff,
156                                                        bkup_power_handler)) != eFrameworkunifiedStatusOK) {
157   // LCOV_EXCL_BR_STOP 4: NSFW error case
158     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
159     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedSubscribeNotificationWithCallback eStatus:%d", e_status);  // LCOV_EXCL_LINE 4: NSFW error case.  // NOLINT (whitespace/line_length)
160   }
161
162   e_status = BkupInitHandler(h_app);  // LCOV_EXCL_BR_LINE 11:unexpected branch
163
164   // LCOV_EXCL_BR_START 4: NSFW error case
165   if ((e_status = FrameworkunifiedPublishServiceAvailability(h_app, TRUE)) != eFrameworkunifiedStatusOK) {
166   // LCOV_EXCL_BR_STOP 4: NSFW error case
167     // LCOV_EXCL_START 4: NSFW error case
168     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
169     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedPublishServiceAvailability e_status:%d\n", e_status);
170     exit(EXIT_FAILURE);
171     // LCOV_EXCL_STOP 4: NSFW error case
172   }
173
174   return e_status;
175 }
176
177 // LCOV_EXCL_START 14 Resident process, not called by NSFW
178 EFrameworkunifiedStatus FrameworkunifiedOnDestroy(HANDLE h_app) {
179   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
180   FRAMEWORKUNIFIEDLOG(ZONE_INFO, __func__, "called");
181   return eFrameworkunifiedStatusOK;
182 }
183 // LCOV_EXCL_STOP 14 Resident process, not called by NSFW
184
185 EFrameworkunifiedStatus FrameworkunifiedOnStart(HANDLE h_app) {
186   // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
187   FRAMEWORKUNIFIEDLOG(ZONE_INFO, __func__, "called");
188   // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
189   return eFrameworkunifiedStatusOK;
190 }
191
192 EFrameworkunifiedStatus FrameworkunifiedOnStop(HANDLE h_app) {
193   EFrameworkunifiedStatus e_status = BkupTerminateHandler(h_app);
194
195   if (FrameworkunifiedPublishServiceAvailability(h_app, FALSE) != eFrameworkunifiedStatusOK) {  // LCOV_EXCL_BR_LINE 4: NSFW error case
196     // LCOV_EXCL_START 15:marco defined in "native_service/ns_logger_if.h"
197     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
198     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedPublishServiceAvailability fail\n");
199     // LCOV_EXCL_STOP 15:marco defined in "native_service/ns_logger_if.h"
200   }
201
202   // LCOV_EXCL_BR_START 4: NSFW error case
203   if (eFrameworkunifiedStatusOK == (FrameworkunifiedStopChildThread(h_app, g_msg_handle_thread, 0, NULL))) {
204   // LCOV_EXCL_BR_STOP 4: NSFW error case
205     FrameworkunifiedDestroyChildThread(h_app, g_msg_handle_thread);
206     g_msg_handle_thread = NULL;
207   }
208
209   int ret2 = pthread_kill(g_work_thread_id, SIGUSR1);
210   if (ESRCH == ret2) {
211     FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __PRETTY_FUNCTION__,
212         "thread is already exit !");
213   } else if (EINVAL == ret2) {
214     FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __PRETTY_FUNCTION__,
215         "signal is invalid !");
216   } else {
217     pthread_join(g_work_thread_id, NULL);
218   }
219
220   ret2 = pthread_kill(g_nand_thread_id, SIGUSR1);
221   if (ESRCH == ret2) {
222     FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __PRETTY_FUNCTION__,
223         "thread is already exit !");
224   } else if (EINVAL == ret2) {
225     FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __PRETTY_FUNCTION__,
226         "signal is invalid !");
227   } else {
228     pthread_join(g_nand_thread_id, NULL);
229   }
230
231   ret2 = pthread_kill(g_delay_thread_id, SIGUSR1);
232   if (ESRCH == ret2) {
233     FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __PRETTY_FUNCTION__,
234         "thread is already exit !");
235   } else if (EINVAL == ret2) {
236     FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __PRETTY_FUNCTION__,
237         "signal is invalid !");
238   } else {
239     pthread_join(g_delay_thread_id, NULL);
240   }
241
242   return e_status;
243 }
244
245 EFrameworkunifiedStatus FrameworkunifiedOnPreStart(HANDLE hApp) {
246   FRAMEWORKUNIFIEDLOG(ZONE_INFO, __func__, "called");
247   return eFrameworkunifiedStatusOK;
248 }
249
250 EFrameworkunifiedStatus FrameworkunifiedOnPreStop(HANDLE hApp) {
251   FRAMEWORKUNIFIEDLOG(ZONE_INFO, __func__, "called");
252   return eFrameworkunifiedStatusOK;
253 }
254
255 EFrameworkunifiedStatus FrameworkunifiedOnBackgroundStart(HANDLE hApp) {
256   FRAMEWORKUNIFIEDLOG(ZONE_INFO, __func__, "called");
257   return eFrameworkunifiedStatusOK;
258 }
259
260 EFrameworkunifiedStatus FrameworkunifiedOnBackgroundStop(HANDLE hApp) {
261   FRAMEWORKUNIFIEDLOG(ZONE_INFO, __func__, "called");
262   return eFrameworkunifiedStatusOK;
263 }
264
265 EFrameworkunifiedStatus FrameworkunifiedOnDebugDump(HANDLE h_app) {  // LCOV_EXCL_START 7:debug code
266   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
267   FRAMEWORKUNIFIEDLOG(ZONE_INFO, __func__, "called");
268   return eFrameworkunifiedStatusOK;
269 }
270 // LCOV_EXCL_STOP 7:debug code
271
272 EFrameworkunifiedStatus FrameworkunifiedCreateStateMachine(HANDLE h_app) {  // LCOV_EXCL_START 7:debug code
273   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
274   FRAMEWORKUNIFIEDLOG(ZONE_INFO, __func__, "called");
275   return eFrameworkunifiedStatusOK;
276 }
277 // LCOV_EXCL_STOP 7:debug code