summaryrefslogtreecommitdiffstats
path: root/logger_service/server/include/loggerservicedebug_thread_if.h
blob: 8a43511473f6656b38997ecb93beb82d7d1faf43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

///////////////////////////////////////////////////////////////////////////////
/// \ingroup  tag_NS_LogTraceSender
/// \brief    This file supports the logging thread interface.
///
///////////////////////////////////////////////////////////////////////////////
#ifndef LOGGER_SERVICE_SERVER_INCLUDE_LOGGERSERVICEDEBUG_THREAD_IF_H_
#define LOGGER_SERVICE_SERVER_INCLUDE_LOGGERSERVICEDEBUG_THREAD_IF_H_

#include <pthread.h>
#ifdef AGL_STUB
#else
#include <sys/neutrino.h>
#include <sys/dispatch.h>
#endif
#include <errno.h>
#include <sched.h>
#include <stdio.h>
#include <native_service/frameworkunified_types.h>
#include <native_service/frameworkunified_thread_priority.h>
#include <system_service/ss_logger_service.h>
#include <system_service/ss_logger_service_local.h>
#include <string>
#include "loggerservicedebug_loggerservicelog.h"

typedef std::string ThreadName;    //< service name

typedef enum _EThreadCommandIds {
  eThrdCmdUsbEject = 5463,        ///< USB eject --> frameworkunifieddebug
  eThrdCmdUsbCaptureLogs,       ///< USB Capture log --> frameworkunifieddebug
  eThrdCmdUsbStoreLogs,               ///< USB Store log --> frameworkunifieddebug
  eThrdCmdWriteFilesToUsb,      ///< frameworkunifieddebug --> Writer
  eThrdCmdWriteDataToFile,      ///< Reader --> Writer
  eThrdCmdWriterStop,        ///< frameworkunifieddebug --> Writer (Stop writing to the file)
  eThrdCmdWriterResume,  ///< frameworkunifieddebug --> Writer (Continue writing to the file)
  eThrdCmdCaptureLogScriptExit,    ///< LogCaptureScript - > frameworkunifieddebug
  eThrdCmdWriteEvntFilesToUsb,         ///< Wreite Event files to USB
  eThrdCmdcopytofile,
  eThrdCmdClearEvntLogs,        ///< Clear event logs
  eThrdCmdStatisticalCounter,      ///< Read statistical counter
  eThrdCmdStatisticalCounterSuccessResp,  ///< Statistical counter positiv Response from child thread
  eThrdCmdStatisticalCounterErrorResp,  ///< Statistical counter Negative Response from child thread
  eThrdCmdResetStatisticalCntrs,
  eThrdCmdCopyEventLogUSBResponse,   ///< Copy event logs to usb response from child thread
  eThrdCmdClearEventLogResponse,  ///< Clear event logs response fromchild thread
  eThrdCmdPersistLogs,         ///< Statistical counter Response fromchild thread
  eThrdCmdSetVINnumber,
  eThrdCmdImmPersistEvtLog,
  eThrdCmdMileageData,
  eThrdCmdGetNumberOfEventsLogged,
  eThrdCmdNumberOfEventsLoggedResponse,
  eThrdCmdUploadEventLog,
  eThrdCmdUploadEventLogResponse,
  eThrdCmdCopyEvntLogToTmp,
  eThrdCmdCopyEvntLogToTmpResponse
} EThreadCommandIds;

typedef struct _TThrdWriteDataToFileEvt {
  static const UI_32 LN_LEN = 1024;
  CHAR line[LN_LEN];
  UI_32 length;
} TThrdWriteLogDataToFileEvt, TThrdWriteTransmitDataToFileEvt;

typedef struct _TThrdWriteFilesToUsbEvt {
  static const UI_32 LOC_PATH_LEN = 256;
  CHAR location[LOC_PATH_LEN];  ///< full path where files should be copied too
  UI_32 loc_len;
} TThrdWriteLogFilesToUsbEvt, TThrdWriteTransmitFilesToUsbEvt;

typedef struct _TThrdCaptureLogsEvt {
  static const UI_32 FN_LEN = 256;
  CHAR location[FN_LEN];
  CHAR parse_fn[FN_LEN];  ///< full path an name of the file that should be parsed for event actions.
} TThrdCaptureLogsEvt;

// Structure to hold the USB device informaton
typedef struct _TThrdEvtLogStore {
  BOOL IsDeviceAvailable;
  static const UI_32 FN_LEN = 256;
  CHAR location[FN_LEN];
  CHAR parse_fn[FN_LEN];  ///< full path an name of the file that should be parsed for event actions.
} TThrdEvtLogStore;

typedef struct _TInitFlag {
  UI_8 mileage_flagbit :1;
  UI_8 statcounter_flagbit :1;
  UI_8 eventlog_flagbit :1;
  UI_8 reghandlers_flagbit :1;
} TInitFlag;

typedef struct _TSessionData {
  std::string strSrcName;
  UI_32 session_id;
} TSessionData;

typedef struct _TEvntNumber {
  TSessionData stSessiondata;
  UI_16 u16numberofeventslogged;
} TEvntsLogged;

typedef struct _TWriteFilesToUsbCmdData {
  TSessionData stSessiondata;
  EEvtLogUSBDevNumber eDevId;
} TWriteFilesToUsbCmdData;

typedef struct _TWriteFilesToUsbCmdResponse {
  TSessionData stSessiondata;
  UI_8 u8Response;
} TWriteFilesToUsbCmdResponse;

typedef struct _TClearEvntLogCmdResponse {
  TSessionData stSessiondata;
  UI_8 u8Response;
} TClearEvntLogCmdResponse;

typedef struct _TStatisticalCountersCmd {
  TSessionData stSessiondata;
  EStatCounterGroupID eGroupId;
} TStatisticalCountersCmd;

typedef struct _TStatisticalCntCmdSuccessResp {
  TSessionData stSessiondata;
  SStatisticalCounter stBuffer;
} TStatisticalCntCmdSuccessResp;

typedef struct _TUploadEventLogResp {
  TSessionData stSessiondata;
  STEventLogPersistBuffer stEventLogBuffer;
} TUploadEventLogResp;

typedef struct _TEventLogCopyStatus {
  static const UI_8 FilePathSize = 64;
  CHAR EventLogFilePath[FilePathSize];
  UI_8 status;
} TEventLogCopyStatus;

typedef enum _ECaptureLogScriptResp {
  ekCLSStatusCompleteSuccessful,
  ekCLSStatusInitError,
  ekCLSStatusExecuteError,
  ekCLSStatusCopyError,
  ekCLSStatusChmodError,
} ECaptureLogScriptResp;

class ThreadWrapper {
 public:

  ThreadWrapper(const char* t_name, void* (*routine)(void*), void* arg) {   // LCOV_EXCL_START 8:ThreadWrapper class is never used
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    pthread_attr_t attr;
    struct sched_param params;

    if (EOK != pthread_attr_init(&attr)) {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
             "Error: pthread_attr_init failed errno: %d", errno);
    }
    if (EOK != pthread_attr_getschedparam(&attr, &params)) {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
             "Error: pthread_attr_getschedparam failed errno: %d", errno);
    }
    SI_32 prio = frameworkunified::framework::CFrameworkunifiedThreadPriorities::GetPriority(
        std::string(t_name));
    if (-1 != prio) {
      params.sched_priority = prio;
      if (EOK != pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED)) {
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
               "Error: pthread_attr_setinheritsched failed errno: %d", errno);
      }
      if (EOK != pthread_attr_setschedparam(&attr, &params)) {
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
               "Error: pthread_attr_setschedparam failed errno: %d", errno);
      }
    }

    if (EOK != pthread_create(&m_thread, &attr, routine, arg)) {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error: pthread_create failed errno: %d",
             errno);
    }
  }
  // LCOV_EXCL_STOP


  ~ThreadWrapper() {  // LCOV_EXCL_START 8:ThreadWrapper class is never used
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
// todo: Need a clean way to cleanup thread in case class closes
  }
  // LCOV_EXCL_STOP


  operator pthread_t() const {  // LCOV_EXCL_START 8:ThreadWrapper class is never used
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    return m_thread;
  }
  // LCOV_EXCL_STOP

 private:
  pthread_t m_thread;
};


class CChild {
 public:
  CChild(const char* parentsName, const char * sendDataToQue,
         const char * clientsName, void* (*routine)(void*),
         const char * sendToUdpQue = "")
      : m_clientName(clientsName),
        m_parentName(parentsName),
        m_sendDataToQueuName(sendDataToQue),
        m_sendDataToUdpQueName(sendToUdpQue),
        m_barInit(InitBarrier()),
        m_thread(clientsName, routine, reinterpret_cast<void*>(this)) {  // LCOV_EXCL_START 7:debug code
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  }
  // LCOV_EXCL_STOP

  void Join() {  // LCOV_EXCL_START 7:debug code
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    if (EOK != pthread_join(m_thread, NULL)) {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error: pthread_join failed errno: %d",
             errno);
    }
  }
  // LCOV_EXCL_STOP

  const char * ParentName() {  // LCOV_EXCL_START 7:debug code
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    return m_parentName.c_str();
  }
  // LCOV_EXCL_STOP

  const char * ClientName() {  // LCOV_EXCL_START 7:debug code
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    return m_clientName.c_str();
  }
  // LCOV_EXCL_STOP

  const char * SendDataToQue() {  // LCOV_EXCL_START 7:debug code
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    return m_sendDataToQueuName.c_str();
  }
  // LCOV_EXCL_STOP

  const char * SendDataToUdpQueue() {  // LCOV_EXCL_START 7:debug code
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    return m_sendDataToUdpQueName.c_str();
  }
  // LCOV_EXCL_STOP

  bool IsValidWaitBarrier() {  // LCOV_EXCL_START 7:debug code
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    if (m_barInit) {
      int wbret = pthread_barrier_wait(&m_barrier);
      return ( PTHREAD_BARRIER_SERIAL_THREAD == wbret) || (0 == wbret);
    }

    return false;
  }
  // LCOV_EXCL_STOP

 private:
  bool InitBarrier() {  // LCOV_EXCL_START 7:debug code
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    /// Note: the count value should match the number of threads that are readers and usb notifiers
    return (EOK == pthread_barrier_init(&m_barrier, NULL, 2) ? true : false);
  }
  // LCOV_EXCL_STOP

 private:
  ThreadName m_clientName;
  ThreadName m_parentName;
  ThreadName m_sendDataToQueuName;
  ThreadName m_sendDataToUdpQueName;
  bool m_barInit;
  ThreadWrapper m_thread;
  pthread_barrier_t m_barrier;
};

class LoggerserviceDebugChildThread {
 public:
  typedef enum e {
    kLoggerserviceDebugLogQueReader,
    kLoggerserviceDebugTransmitQueReader,
    kLoggerservicePerformanceLogQueReader,
    kLoggerserviceDebugCaptureLogScript,
    kLoggerserviceDebugEventLogque,
    kLoggerserviceDebugMax
  } ELoggerserviceDebugThreads;

  LoggerserviceDebugChildThread();
  virtual ~LoggerserviceDebugChildThread();
  BOOL Start(ELoggerserviceDebugThreads id, const char* parentsName,
             const char * sendDataToQue, void* (*routine)(void*),
             const char * sendToUdpQue = "");
  void Stop(ELoggerserviceDebugThreads id);
  void StopAll();
  const char * Name(ELoggerserviceDebugThreads id);
  void LiftWaitBarrier(ELoggerserviceDebugThreads id);
  BOOL Running(ELoggerserviceDebugThreads id) const;
 private:
  ThreadName names[kLoggerserviceDebugMax];
  CChild * threads[kLoggerserviceDebugMax];
};

#endif  // LOGGER_SERVICE_SERVER_INCLUDE_LOGGERSERVICEDEBUG_THREAD_IF_H_