Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / peripheralservice / communication / server / src / main / communication_main.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 <errno.h>
18 #include <sys/timerfd.h>
19 #include <unistd.h>
20 #include <poll.h>
21
22 #include <native_service/frameworkunified_dispatcher.h>
23 #include <native_service/frameworkunified_application.h>
24 #include <native_service/frameworkunified_multithreading.h>
25
26 #include <native_service/ns_version_if.h>
27 #include <system_service/ss_system_if.h>
28 #include <system_service/ss_sm_client_if.h>
29 #include "communication_communicationlog.h"
30 #include "communication_version.h"
31 #include "communication_cid.h"
32
33 #include "CAN_Thread.h"
34 #include "Thread_Common.h"
35
36 CFrameworkunifiedVersion g_FrameworkunifiedVersion(MAJORNO, MINORNO, REVISION);
37
38 FRAMEWORKUNIFIEDLOGPARAM g_FrameworkunifiedLogParams = {
39   FRAMEWORKUNIFIEDLOGOPTIONS,
40   {ZONE_TEXT_10, ZONE_TEXT_11, ZONE_TEXT_12,
41    ZONE_TEXT_13, ZONE_TEXT_14, ZONE_TEXT_15,
42    ZONE_TEXT_16, ZONE_TEXT_17, ZONE_TEXT_18,
43    ZONE_TEXT_19, ZONE_TEXT_20, ZONE_TEXT_21,
44    ZONE_TEXT_22, ZONE_TEXT_23, ZONE_TEXT_24,
45    ZONE_TEXT_25, ZONE_TEXT_26, ZONE_TEXT_27,
46    ZONE_TEXT_28, ZONE_TEXT_29, ZONE_TEXT_30,
47    ZONE_TEXT_31},
48   FRAMEWORKUNIFIEDLOGZONES};
49
50 extern HANDLE g_can_thread;
51 static bool capture_log_flg = FALSE;
52
53 #define COMMSYS_SND_ERR_CNT_MAX 10
54
55 static void MessageHandler(HANDLE h_app) {
56   EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
57   e_status = FrameworkunifiedDispatchProcessWithoutLoop(h_app);
58   if (e_status != eFrameworkunifiedStatusOK) {
59     // Ignore Error. Logging only.
60     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedDispatchProcessWithoutLoop: %d", e_status);
61   }
62 }
63
64 static void TimeoutHandlerCAN(HANDLE h_app) {
65   EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
66   uint32_t dummy = 0;
67   static uint32_t snd_err_cnt_can = 0;
68
69   if (CommGetAvailabilityCurrent(CAN_AVAILABILITY)) {
70     e_status = FrameworkunifiedSendChild(h_app, g_can_thread,
71                             CID_COMMSYS_TIMEOUT, 0, &dummy);
72     if (e_status != eFrameworkunifiedStatusOK) {
73       snd_err_cnt_can++;
74       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__,
75                        "FrameworkunifiedSendChild(timeout for can) faild: %d", e_status);
76
77       if (capture_log_flg)
78         return;
79
80       if (snd_err_cnt_can < COMMSYS_SND_ERR_CNT_MAX)
81         return;
82
83       SendUserInvokedLoggingRequestToSystemManager(
84                      e_SS_SM_CAPTURE_DTC_LOGS, "Send CAN TimeoutNtfy Error");
85       capture_log_flg = true;
86     } else {
87       snd_err_cnt_can = 0;
88     }
89   }
90 }
91
92 int main(int argc, char *argv[]) {
93   EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
94   FrameworkunifiedDefaultCallbackHandler cb_funcs;
95   FRAMEWORKUNIFIED_MAKE_DEFAULT_CALLBACK(cb_funcs);
96   HANDLE h_app;
97   FRAMEWORKUNIFIED_SET_ZONES();
98   struct pollfd fds[2];
99   int ret;
100
101   e_status = FrameworkunifiedCreateDispatcherWithoutLoop(LAN_SERVICE_MAIN, 
102                                         h_app, argc, argv, &cb_funcs, TRUE);
103   if (e_status != eFrameworkunifiedStatusOK) {
104     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedCreateDispatcherWithoutLoop: %d", e_status);
105     return EXIT_FAILURE;
106   }
107
108   e_status = FrameworkunifiedGetDispatcherFD(h_app, &fds[0].fd);
109   if (e_status != eFrameworkunifiedStatusOK) {
110     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedGetDispatcherFD: %d", e_status);
111     return EXIT_FAILURE;
112   }
113
114   if ((fds[1].fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC)) == -1) {
115     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "timerfd_create: %d", errno);
116     return EXIT_FAILURE;
117   }
118
119   struct itimerspec tm;
120   tm.it_value.tv_sec = 0;
121   tm.it_value.tv_nsec = 100 * 1000 * 1000;
122   tm.it_interval.tv_sec = 0;
123   tm.it_interval.tv_nsec = 100 * 1000 * 1000;
124   if (timerfd_settime(fds[1].fd, 0, &tm, NULL) == -1) {
125     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "timerfd_settime: %d", errno);
126     return EXIT_FAILURE;
127   }
128   fds[0].events = POLLIN;
129   fds[1].events = POLLIN;
130
131   while (1) {
132     ret = poll(fds, sizeof(fds) / sizeof(struct pollfd), -1);
133     if (ret < 0) {
134       if (errno == EINTR) {
135         continue;
136       }
137       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "poll errno:%d", errno);
138       continue;
139     }
140
141     if ((fds[0].revents & POLLIN) != 0) {
142       MessageHandler(h_app);
143       continue;
144     }
145
146     if ((fds[1].revents & POLLIN) != 0) {
147       uint64_t exp;
148       read(fds[1].fd, &exp, sizeof(uint64_t));
149       TimeoutHandlerCAN(h_app);
150       continue;
151     }
152   }
153   return EXIT_SUCCESS;
154 }