Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / systemservice / logger_service / server / include / loggerservicedebug_thread_if.h
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 ///////////////////////////////////////////////////////////////////////////////
18 /// \ingroup  tag_NS_LogTraceSender
19 /// \brief    This file supports the logging thread interface.
20 ///
21 ///////////////////////////////////////////////////////////////////////////////
22 #ifndef LOGGER_SERVICE_SERVER_INCLUDE_LOGGERSERVICEDEBUG_THREAD_IF_H_
23 #define LOGGER_SERVICE_SERVER_INCLUDE_LOGGERSERVICEDEBUG_THREAD_IF_H_
24
25 #include <pthread.h>
26 #ifdef AGL_STUB
27 #else
28 #include <sys/neutrino.h>
29 #include <sys/dispatch.h>
30 #endif
31 #include <errno.h>
32 #include <sched.h>
33 #include <stdio.h>
34 #include <native_service/frameworkunified_types.h>
35 #include <native_service/frameworkunified_thread_priority.h>
36 #include <system_service/ss_logger_service.h>
37 #include <system_service/ss_logger_service_local.h>
38 #include <string>
39 #include "loggerservicedebug_loggerservicelog.h"
40
41 typedef std::string ThreadName;    //< service name
42
43 typedef enum _EThreadCommandIds {
44   eThrdCmdUsbEject = 5463,        ///< USB eject --> frameworkunifieddebug
45   eThrdCmdUsbCaptureLogs,       ///< USB Capture log --> frameworkunifieddebug
46   eThrdCmdUsbStoreLogs,               ///< USB Store log --> frameworkunifieddebug
47   eThrdCmdWriteFilesToUsb,      ///< frameworkunifieddebug --> Writer
48   eThrdCmdWriteDataToFile,      ///< Reader --> Writer
49   eThrdCmdWriterStop,        ///< frameworkunifieddebug --> Writer (Stop writing to the file)
50   eThrdCmdWriterResume,  ///< frameworkunifieddebug --> Writer (Continue writing to the file)
51   eThrdCmdCaptureLogScriptExit,    ///< LogCaptureScript - > frameworkunifieddebug
52   eThrdCmdWriteEvntFilesToUsb,         ///< Wreite Event files to USB
53   eThrdCmdcopytofile,
54   eThrdCmdClearEvntLogs,        ///< Clear event logs
55   eThrdCmdStatisticalCounter,      ///< Read statistical counter
56   eThrdCmdStatisticalCounterSuccessResp,  ///< Statistical counter positiv Response from child thread
57   eThrdCmdStatisticalCounterErrorResp,  ///< Statistical counter Negative Response from child thread
58   eThrdCmdResetStatisticalCntrs,
59   eThrdCmdCopyEventLogUSBResponse,   ///< Copy event logs to usb response from child thread
60   eThrdCmdClearEventLogResponse,  ///< Clear event logs response fromchild thread
61   eThrdCmdPersistLogs,         ///< Statistical counter Response fromchild thread
62   eThrdCmdSetVINnumber,
63   eThrdCmdImmPersistEvtLog,
64   eThrdCmdMileageData,
65   eThrdCmdGetNumberOfEventsLogged,
66   eThrdCmdNumberOfEventsLoggedResponse,
67   eThrdCmdUploadEventLog,
68   eThrdCmdUploadEventLogResponse,
69   eThrdCmdCopyEvntLogToTmp,
70   eThrdCmdCopyEvntLogToTmpResponse
71 } EThreadCommandIds;
72
73 typedef struct _TThrdWriteDataToFileEvt {
74   static const UI_32 LN_LEN = 1024;
75   CHAR line[LN_LEN];
76   UI_32 length;
77 } TThrdWriteLogDataToFileEvt, TThrdWriteTransmitDataToFileEvt;
78
79 typedef struct _TThrdWriteFilesToUsbEvt {
80   static const UI_32 LOC_PATH_LEN = 256;
81   CHAR location[LOC_PATH_LEN];  ///< full path where files should be copied too
82   UI_32 loc_len;
83 } TThrdWriteLogFilesToUsbEvt, TThrdWriteTransmitFilesToUsbEvt;
84
85 typedef struct _TThrdCaptureLogsEvt {
86   static const UI_32 FN_LEN = 256;
87   CHAR location[FN_LEN];
88   CHAR parse_fn[FN_LEN];  ///< full path an name of the file that should be parsed for event actions.
89 } TThrdCaptureLogsEvt;
90
91 // Structure to hold the USB device informaton
92 typedef struct _TThrdEvtLogStore {
93   BOOL IsDeviceAvailable;
94   static const UI_32 FN_LEN = 256;
95   CHAR location[FN_LEN];
96   CHAR parse_fn[FN_LEN];  ///< full path an name of the file that should be parsed for event actions.
97 } TThrdEvtLogStore;
98
99 typedef struct _TInitFlag {
100   UI_8 mileage_flagbit :1;
101   UI_8 statcounter_flagbit :1;
102   UI_8 eventlog_flagbit :1;
103   UI_8 reghandlers_flagbit :1;
104 } TInitFlag;
105
106 typedef struct _TSessionData {
107   std::string strSrcName;
108   UI_32 session_id;
109 } TSessionData;
110
111 typedef struct _TEvntNumber {
112   TSessionData stSessiondata;
113   UI_16 u16numberofeventslogged;
114 } TEvntsLogged;
115
116 typedef struct _TWriteFilesToUsbCmdData {
117   TSessionData stSessiondata;
118   EEvtLogUSBDevNumber eDevId;
119 } TWriteFilesToUsbCmdData;
120
121 typedef struct _TWriteFilesToUsbCmdResponse {
122   TSessionData stSessiondata;
123   UI_8 u8Response;
124 } TWriteFilesToUsbCmdResponse;
125
126 typedef struct _TClearEvntLogCmdResponse {
127   TSessionData stSessiondata;
128   UI_8 u8Response;
129 } TClearEvntLogCmdResponse;
130
131 typedef struct _TStatisticalCountersCmd {
132   TSessionData stSessiondata;
133   EStatCounterGroupID eGroupId;
134 } TStatisticalCountersCmd;
135
136 typedef struct _TStatisticalCntCmdSuccessResp {
137   TSessionData stSessiondata;
138   SStatisticalCounter stBuffer;
139 } TStatisticalCntCmdSuccessResp;
140
141 typedef struct _TUploadEventLogResp {
142   TSessionData stSessiondata;
143   STEventLogPersistBuffer stEventLogBuffer;
144 } TUploadEventLogResp;
145
146 typedef struct _TEventLogCopyStatus {
147   static const UI_8 FilePathSize = 64;
148   CHAR EventLogFilePath[FilePathSize];
149   UI_8 status;
150 } TEventLogCopyStatus;
151
152 typedef enum _ECaptureLogScriptResp {
153   ekCLSStatusCompleteSuccessful,
154   ekCLSStatusInitError,
155   ekCLSStatusExecuteError,
156   ekCLSStatusCopyError,
157   ekCLSStatusChmodError,
158 } ECaptureLogScriptResp;
159
160 class ThreadWrapper {
161  public:
162
163   ThreadWrapper(const char* t_name, void* (*routine)(void*), void* arg) {   // LCOV_EXCL_START 8:ThreadWrapper class is never used
164     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
165     pthread_attr_t attr;
166     struct sched_param params;
167
168     if (EOK != pthread_attr_init(&attr)) {
169       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
170              "Error: pthread_attr_init failed errno: %d", errno);
171     }
172     if (EOK != pthread_attr_getschedparam(&attr, &params)) {
173       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
174              "Error: pthread_attr_getschedparam failed errno: %d", errno);
175     }
176     SI_32 prio = frameworkunified::framework::CFrameworkunifiedThreadPriorities::GetPriority(
177         std::string(t_name));
178     if (-1 != prio) {
179       params.sched_priority = prio;
180       if (EOK != pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED)) {
181         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
182                "Error: pthread_attr_setinheritsched failed errno: %d", errno);
183       }
184       if (EOK != pthread_attr_setschedparam(&attr, &params)) {
185         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
186                "Error: pthread_attr_setschedparam failed errno: %d", errno);
187       }
188     }
189
190     if (EOK != pthread_create(&m_thread, &attr, routine, arg)) {
191       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error: pthread_create failed errno: %d",
192              errno);
193     }
194   }
195   // LCOV_EXCL_STOP
196
197
198   ~ThreadWrapper() {  // LCOV_EXCL_START 8:ThreadWrapper class is never used
199     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
200 // todo: Need a clean way to cleanup thread in case class closes
201   }
202   // LCOV_EXCL_STOP
203
204
205   operator pthread_t() const {  // LCOV_EXCL_START 8:ThreadWrapper class is never used
206     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
207     return m_thread;
208   }
209   // LCOV_EXCL_STOP
210
211  private:
212   pthread_t m_thread;
213 };
214
215
216 class CChild {
217  public:
218   CChild(const char* parentsName, const char * sendDataToQue,
219          const char * clientsName, void* (*routine)(void*),
220          const char * sendToUdpQue = "")
221       : m_clientName(clientsName),
222         m_parentName(parentsName),
223         m_sendDataToQueuName(sendDataToQue),
224         m_sendDataToUdpQueName(sendToUdpQue),
225         m_barInit(InitBarrier()),
226         m_thread(clientsName, routine, reinterpret_cast<void*>(this)) {  // LCOV_EXCL_START 7:debug code
227     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
228   }
229   // LCOV_EXCL_STOP
230
231   void Join() {  // LCOV_EXCL_START 7:debug code
232     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
233     if (EOK != pthread_join(m_thread, NULL)) {
234       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error: pthread_join failed errno: %d",
235              errno);
236     }
237   }
238   // LCOV_EXCL_STOP
239
240   const char * ParentName() {  // LCOV_EXCL_START 7:debug code
241     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
242     return m_parentName.c_str();
243   }
244   // LCOV_EXCL_STOP
245
246   const char * ClientName() {  // LCOV_EXCL_START 7:debug code
247     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
248     return m_clientName.c_str();
249   }
250   // LCOV_EXCL_STOP
251
252   const char * SendDataToQue() {  // LCOV_EXCL_START 7:debug code
253     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
254     return m_sendDataToQueuName.c_str();
255   }
256   // LCOV_EXCL_STOP
257
258   const char * SendDataToUdpQueue() {  // LCOV_EXCL_START 7:debug code
259     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
260     return m_sendDataToUdpQueName.c_str();
261   }
262   // LCOV_EXCL_STOP
263
264   bool IsValidWaitBarrier() {  // LCOV_EXCL_START 7:debug code
265     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
266     if (m_barInit) {
267       int wbret = pthread_barrier_wait(&m_barrier);
268       return ( PTHREAD_BARRIER_SERIAL_THREAD == wbret) || (0 == wbret);
269     }
270
271     return false;
272   }
273   // LCOV_EXCL_STOP
274
275  private:
276   bool InitBarrier() {  // LCOV_EXCL_START 7:debug code
277     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
278     /// Note: the count value should match the number of threads that are readers and usb notifiers
279     return (EOK == pthread_barrier_init(&m_barrier, NULL, 2) ? true : false);
280   }
281   // LCOV_EXCL_STOP
282
283  private:
284   ThreadName m_clientName;
285   ThreadName m_parentName;
286   ThreadName m_sendDataToQueuName;
287   ThreadName m_sendDataToUdpQueName;
288   bool m_barInit;
289   ThreadWrapper m_thread;
290   pthread_barrier_t m_barrier;
291 };
292
293 class LoggerserviceDebugChildThread {
294  public:
295   typedef enum e {
296     kLoggerserviceDebugLogQueReader,
297     kLoggerserviceDebugTransmitQueReader,
298     kLoggerservicePerformanceLogQueReader,
299     kLoggerserviceDebugCaptureLogScript,
300     kLoggerserviceDebugEventLogque,
301     kLoggerserviceDebugMax
302   } ELoggerserviceDebugThreads;
303
304   LoggerserviceDebugChildThread();
305   virtual ~LoggerserviceDebugChildThread();
306   BOOL Start(ELoggerserviceDebugThreads id, const char* parentsName,
307              const char * sendDataToQue, void* (*routine)(void*),
308              const char * sendToUdpQue = "");
309   void Stop(ELoggerserviceDebugThreads id);
310   void StopAll();
311   const char * Name(ELoggerserviceDebugThreads id);
312   void LiftWaitBarrier(ELoggerserviceDebugThreads id);
313   BOOL Running(ELoggerserviceDebugThreads id) const;
314  private:
315   ThreadName names[kLoggerserviceDebugMax];
316   CChild * threads[kLoggerserviceDebugMax];
317 };
318
319 #endif  // LOGGER_SERVICE_SERVER_INCLUDE_LOGGERSERVICEDEBUG_THREAD_IF_H_