summaryrefslogtreecommitdiffstats
path: root/logger_service/server/realtimeUsbLog/src/ss_logger_realtime_usb.cpp
blob: c318f1ac7ea99003e2555d372db5585144ea7a4d (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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
/*
 * @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_InterfaceunifiedLogCapture
/// \brief    This file contains the standard set functions called by the NS
//            dispatcher on application initialization, cleanup and wakeup.
///
///////////////////////////////////////////////////////////////////////////////
// System Headers
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <sys/time.h>
#include <mqueue.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include <time.h>
#include <loggerservicedebug_loggerservicelog.h>
#include <native_service/frameworkunified_multithreading.h>
#include <system_service/ss_system_manager_if.h>
#include <system_service/ss_system_manager_notifications.h>
#include <native_service/frameworkunified_timer.h>
#include <system_service/ss_devicedetection_service_notifications.h>
#include <system_service/ss_devicedetection_service.h>
#include <system_service/ss_devicedetection_service_protocol.h>
#include <system_service/ss_devicedetection_service_ifc.h>
#include <native_service/frameworkunified_application.h>
#include <native_service/frameworkunified_framework_if.h>
#include <native_service/frameworkunified_service_protocol.h>
#include <native_service/frameworkunified_types.h>
#include <queue>
#include <string>

// System definition
#ifndef TN_LOGGERRTIME
#define TN_LOGGERRTIME     "LoggerRtime"
#define PR_LOGGERRTIME_S   PR_TSS_S
#define PR_LOGGERRTIME     PR_TSS
#endif  // TN_LOGGERRTIME
// Queue resource
#define USB_REALTIME_MSGMAX 256
#define USB_REALTIME_SIZMAX 4096
#define USB_REALTIME_QNAME  "/PlRtLogger"
// Folder path
#define USB_RT_FOLDERPATH_ "/REALTIME_"

// older path
#define USB_RT_TMP_PATH "/tmp"
// File name
#define USB_RT_LOGNAME_TMP "usbrt_tmp.log"
#define ENC_BUF_MAX_SIZE   (1024*8)

#ifdef RELEASE_BUILD
// encrypt values
#define DEV_ENC_EXTENSION  ".enc1"
#define REL_ENC_EXTENSION  ".enc2"
#define ALIGNMENT_SIZE 4
#endif  // RELEASE_BUILD

// File path
#define USB_RT_LOGNAME_ "loggerservice_usb__realtime"
// Max size of each log file
#define USB_RT_FILE_SIZE (1024*1024)
// Num of logs to write in std::queue at one time
#define QUELOG_WRITE_BLOCK_NUM 300
// Max of logs to save in std::queue
#define QUELOG_SAVE_MAX   12000
#define QUELOG_DELNUM   (QUELOG_WRITE_BLOCK_NUM*2)  // num of deletion at one time
// TIMER ID
#define USB_RT_TIMER_EV  0x89
#define USB_RT_TIMER_SYNC 1   // kind
#define USB_RT_TIMER_CYCLE 10000  // cycle of timeout(msec)

static HANDLE g_rt_tid = NULL;
static int g_wfd = -1;
static mqd_t g_qid = (mqd_t) - 1;
static int g_num_of_file = 1;
static char g_mnt_path[MAXPATHLEN];
static char g_fld_path[MAXPATHLEN];
static char g_log_path[MAXPATHLEN];
static char *g_log_name;
static bool g_need_sync = false;
static size_t g_filesize = 0;

//static queue<string> g_saveq;
static std::queue<std::string> g_saveq;

static DeviceDetectionServiceIf g_devDetect;
static bool g_usb_available = false;

static char g_log_tmp_path[MAXPATHLEN];

EFrameworkunifiedStatus rtUsbLogThread(HANDLE hApp);
EFrameworkunifiedStatus rtUsbLogTShutdown(HANDLE hApp);
EFrameworkunifiedStatus OnPowerOnOffNotification(HANDLE hApp);

EFrameworkunifiedStatus StartTimer(HANDLE hApp, const UI_32 timeout);
EFrameworkunifiedStatus CancelTimer(void);

// DeviceDetection Related funcs
EFrameworkunifiedStatus OnDeviceDetectionAvailability(HANDLE hApp);
EFrameworkunifiedStatus OnDeviceDetectionOpenSessionACK(HANDLE hApp);
EFrameworkunifiedStatus onDeviceDetectionCloseSessionACK(HANDLE hApp);
EFrameworkunifiedStatus onUsbDetectCallback(HANDLE hApp);

EFrameworkunifiedStatus EncryptAndCopyFile(void);

EFrameworkunifiedStatus StartRtUsbLogThread(HANDLE hApp) {
  FrameworkunifiedChildThreadAttr attr;
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  if (g_rt_tid != NULL) {
    return eFrameworkunifiedStatusOK;
  }

  // Create Realtime USB Logging Thread and start
  memset(&attr, 0, sizeof(FrameworkunifiedChildThreadAttr));
  // One time while to break on Error
  if (eFrameworkunifiedStatusOK
      != (l_eStatus = FrameworkunifiedCreateChildThreadAttrSetSched(&attr,
                                                       eFrameworkunifiedSchedPolicyTSS,
                                                       PR_LOGGERRTIME))) {
    return l_eStatus;
  }
  g_rt_tid = FrameworkunifiedCreateChildThreadWithAttribute(hApp, TN_LOGGERRTIME,
                                               rtUsbLogThread,
                                               rtUsbLogTShutdown, &attr);
  if (g_rt_tid == NULL) {
    l_eStatus = eFrameworkunifiedStatusFail;
  } else {
    l_eStatus = FrameworkunifiedStartChildThread(hApp, g_rt_tid, 0, (PVOID) NULL);
  }

  return l_eStatus;
}

EFrameworkunifiedStatus StopRtUsbLogThread(HANDLE hApp) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  // Close fd to sync
  if (g_wfd != -1) {
    if (0 != close(g_wfd)) {
      l_eStatus = eFrameworkunifiedStatusFail;
    }
    EncryptAndCopyFile();
  }
  // Close message queue
  if (g_qid != (mqd_t) - 1) {
    if (0 != mq_close(g_qid)) {
      l_eStatus = eFrameworkunifiedStatusFail;
    }
  }
  // Terminate Thread
  if (eFrameworkunifiedStatusOK != FrameworkunifiedStopChildThread(hApp, g_rt_tid, 0, NULL)) {
    l_eStatus = eFrameworkunifiedStatusFail;
  }
  if (eFrameworkunifiedStatusOK != FrameworkunifiedDestroyChildThread(hApp, g_rt_tid)) {
    l_eStatus = eFrameworkunifiedStatusFail;
  }

  return l_eStatus;
}

EFrameworkunifiedStatus OnPowerOnOffNotification(HANDLE hApp) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  T_SS_SM_UserModeOnOffNotification_StructType onoff_mode;

  l_eStatus = FrameworkunifiedGetMsgDataOfSize(hApp, (PVOID) &onoff_mode,
                                  sizeof(onoff_mode));
  if (eFrameworkunifiedStatusOK != l_eStatus) {
    fprintf(stderr, "%s: Failed to fetch OnOff Mode\n", __FUNCTION__);
    return l_eStatus;
  }
  if (!onoff_mode.isUserModeOn) {  // ACC-OFF
    // Close fd to sync
    if (g_wfd >= 0) {
      syncfs(g_wfd);
      if (0 != close(g_wfd)) {
        l_eStatus = eFrameworkunifiedStatusFail;
      }
      EncryptAndCopyFile();
      g_wfd = -2;  // ACC-OFF detected
    }
  }
  return l_eStatus;
}

static int getStartFileNum(void) {
  int start = 0;
  int num;
  DIR *dirp;
  struct dirent entry;
  struct dirent *next;

  if ((dirp = opendir(g_fld_path)) == NULL) {
    fprintf(stderr, "%s: Failed in opendir, errno=%d\n", __FUNCTION__, errno);
    return -1;
  }
  for (;;) {
    if (readdir_r(dirp, &entry, &next) != 0) {
      fprintf(stderr, "%s: Failed in readdir_r, errno=%d\n", __FUNCTION__,
              errno);
      closedir(dirp);
      return -1;
    }
    if (next == NULL) {  // NO more
      break;
    }
    if (strncmp(entry.d_name, g_log_name, strlen(g_log_name)) != 0) {
      continue;
    }
    num = atoi(entry.d_name + strlen(g_log_name));
    if (num > start) {
      start = num;
    }
  }
  closedir(dirp);
  start++;

  return start;
}

EFrameworkunifiedStatus WriteAndSwitchLogFile(const char* data, ssize_t size) {
  ssize_t wrotelen;
  char logpath[MAXPATHLEN];

  wrotelen = write(g_wfd, data, size);
  if (wrotelen != size) {
    close(g_wfd);
    g_wfd = -1;
    g_filesize = 0;
    EncryptAndCopyFile();
    fprintf(stderr, "%s: Failed to write, size=%d, wrotelen=%d, errno=%d\n",

//            __FUNCTION__, static_cast<int32_t>size, static_cast<int32_t>wrotelen, errno);
            __FUNCTION__, static_cast<int32_t>(size), static_cast<int32_t>(wrotelen), errno);

    return eFrameworkunifiedStatusFail;
  } else {
    g_filesize += wrotelen;
    g_need_sync = true;
    if (static_cast<int>(g_filesize) >= USB_RT_FILE_SIZE) {
      g_filesize = 0;
      close(g_wfd);
      EncryptAndCopyFile();
      g_num_of_file++;


//      snprintf(logpath, "%s/%s", g_log_tmp_path, USB_RT_LOGNAME_TMP);
      snprintf(logpath, MAXPATHLEN, "%s/%s", g_log_tmp_path, USB_RT_LOGNAME_TMP);

      if ((g_wfd = open(logpath, O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0644))
          == -1) {
        fprintf(stderr, "%s: Failed to open file %s, errno=%d\n", __FUNCTION__,
                logpath, errno);
        return eFrameworkunifiedStatusFail;
      }
    }
  }
  return eFrameworkunifiedStatusOK;
}

EFrameworkunifiedStatus OnMessageQueueReceived(HANDLE hApp) {
  ssize_t len, len2;
  char msg[USB_REALTIME_SIZMAX], msg2[USB_REALTIME_SIZMAX];
  char logpath[MAXPATHLEN];
  unsigned int prior;
  struct stat stbuf;

//  string work_str;
  std::string work_str;

  bool pushed = false;
  const struct timespec tspec = { 0, 0 };

  // write loop
  while (1) {
    pushed = false;
    if (g_qid == (mqd_t) - 1) {  // Already ACC-OFFed
      return eFrameworkunifiedStatusOK;
    }
    len = mq_timedreceive(g_qid, msg, sizeof(msg), &prior, &tspec);
    if (len == (ssize_t) - 1) {  // NO more data
      if (errno != ETIMEDOUT) {
        fprintf(stderr, "%s: Unexpected error %d\n", __FUNCTION__, errno);
      }
      break;
    }
    // Remove overflowed log
    if (g_saveq.size() > QUELOG_SAVE_MAX) {
      int delnum = g_saveq.size() - QUELOG_SAVE_MAX + QUELOG_DELNUM;
      for (int i = 0; i < delnum; i++) {
        g_saveq.pop();
      }
    }
    // check if file is opened
    if (g_wfd == -1) {
      /*
       * Make folder and open log file
       */
      // save dequeued log

//      g_saveq.push(string((const char*) msg, len));
      g_saveq.push(std::string((const char*) msg, len));

      pushed = true;  // request pushed or NON-log message -> NO NEED to write this data to log file
      // Check USB mounted
      if (!g_usb_available) {
        // No mount event is received
        continue;
      }
      // Check USB mounted
      if (stat(g_mnt_path, &stbuf) != 0) {
        // No mount path found
        continue;
      }
      // Check and make subfolder
      int mkdres = mkdir(g_fld_path, 0666);
      if (mkdres == 0) {
        g_num_of_file = 1;
      } else if (mkdres == -1 && errno == EEXIST) {
        // Decide file number to start(Check current log files)
        g_num_of_file = getStartFileNum();
        if (g_num_of_file <= 0) {
          fprintf(stderr, "%s: Failed in getStartFileNum\n", __FUNCTION__);
          continue;
        }
      } else {
        fprintf(stderr, "%s: Failed to mkdir %s, errno=%d\n", __FUNCTION__,
                g_fld_path, errno);
        continue;
      }

      // Open File
      g_filesize = 0;


//      snprintf(logpath, "%s/%s", g_log_tmp_path, USB_RT_LOGNAME_TMP);
      snprintf(logpath, MAXPATHLEN, "%s/%s", g_log_tmp_path, USB_RT_LOGNAME_TMP);

      if ((g_wfd = open(logpath, O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0644))
          == -1) {
        fprintf(stderr, "%s: Failed to open file %s, errno=%d\n", __FUNCTION__,
                logpath, errno);
        continue;
      }
    } else if (g_wfd == -2) {
      // Already ACC-OFF, Just throw the logs away
      continue;
    }
    // Write out saved log if any
    int written_once = QUELOG_WRITE_BLOCK_NUM;  // num of written at once
    while (1) {
      if (g_saveq.empty()) {
        break;
      }
      if (written_once >= QUELOG_WRITE_BLOCK_NUM) {
        written_once = 0;
        while (1) {
          // read request queue
          len2 = mq_timedreceive(g_qid, msg2, sizeof(msg2), &prior, &tspec);
          if (len2 == (ssize_t) - 1) {  // NO MORE data
            if (errno != ETIMEDOUT) {
              fprintf(stderr, "%s: Unexpected error %d\n", __FUNCTION__, errno);
            }
            break;
          }

//          g_saveq.push(string((const char*) msg2, len2));
          g_saveq.push(std::string((const char*) msg2, len2));

        }
      }
      work_str = g_saveq.front();
      if (eFrameworkunifiedStatusOK
          != WriteAndSwitchLogFile(work_str.data(), work_str.length())) {
        fprintf(stderr, "%s: Failed in write Saved data, errno=%d\n",
                __FUNCTION__, errno);
        break;
      } else {
        written_once++;
        g_saveq.pop();
      }
    }
    if (pushed || g_wfd == -1) {
      continue;
    }
    // write data
    if (eFrameworkunifiedStatusOK != WriteAndSwitchLogFile(msg, len)) {
      fprintf(stderr, "%s: Failed in write Saved data-2, errno=%d\n",
              __FUNCTION__, errno);
    }
  }
  return eFrameworkunifiedStatusOK;
}

EFrameworkunifiedStatus OnSyncTimeout(HANDLE hApp) {
  if (g_need_sync) {
    if (g_wfd != -1 && g_wfd != -2) {
      char logpath[MAXPATHLEN];
      fsync(g_wfd);
      close(g_wfd);
      g_wfd = -1;
      EncryptAndCopyFile();

//      snprintf(logpath, "%s/%s", g_log_tmp_path, USB_RT_LOGNAME_TMP);
      snprintf(logpath, MAXPATHLEN, "%s/%s", g_log_tmp_path, USB_RT_LOGNAME_TMP);

      if ((g_wfd = open(logpath, O_RDWR | O_CREAT | O_APPEND | O_CLOEXEC, 0644))
          == -1) {
        fprintf(stderr, "%s: Failed to open file %s, errno=%d (%s)\n",
                __FUNCTION__, logpath, errno, strerror(errno));
        // because in order to continue to obtain logs.
        return eFrameworkunifiedStatusOK;
      }
      g_need_sync = false;
    }
  }
  return eFrameworkunifiedStatusOK;
}

EFrameworkunifiedStatus rtUsbLogThread(HANDLE hApp) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  struct mq_attr qattr;
  const struct timespec idle_time = { 1, 0 };  // 1sec
  HANDLE timer_handle = NULL;

  // Subscribe Boot Mode Notifcation to SysMgr
  l_eStatus = FrameworkunifiedSubscribeNotificationWithCallback(hApp,
  NTFY_SSSystemMgrPowerOnOff,
                                                   OnPowerOnOffNotification);
  if (eFrameworkunifiedStatusOK != l_eStatus) {
    fprintf(stderr, "%s: Subscribe PowerOnOff failed, status=%d\n",
            __FUNCTION__, l_eStatus);
  }
  // Subscribe Device Detection Availability
  while (1) {
    if (g_devDetect.Initialize(hApp)) {
      if (eFrameworkunifiedStatusOK
          != (l_eStatus = g_devDetect.NotifyOnDeviceDetectionAvailability(
              OnDeviceDetectionAvailability))) {
        fprintf(stderr,
                "%s: DevDetec AvailabilityCallback registration failed\n",
                __FUNCTION__);
      }
      if (eFrameworkunifiedStatusOK
          != (l_eStatus = g_devDetect.NotifyOnOpenSessionAck(
              OnDeviceDetectionOpenSessionACK))) {
        fprintf(stderr, "%s: DevDetec OpenSession ACK registration failed\n",
                __FUNCTION__);
      }
      if (eFrameworkunifiedStatusOK
          != (l_eStatus = g_devDetect.NotifyOnCloseSessionAck(
              onDeviceDetectionCloseSessionACK))) {
        fprintf(stderr, "%s: DevDetec CloseSession ACK registration failed\n",
                __FUNCTION__);
      }
      break;
    } else {
      fprintf(stderr, "%s: Device Detection Object Initialization failed\n",
              __FUNCTION__);
      nanosleep(&idle_time, NULL);
    }
  }

  // open message queue
  while (1) {
    qattr.mq_flags = 0;
    qattr.mq_maxmsg = USB_REALTIME_MSGMAX;
    qattr.mq_msgsize = USB_REALTIME_SIZMAX;
    qattr.mq_curmsgs = 0;
    if ((g_qid = mq_open(USB_REALTIME_QNAME, O_RDWR | O_CREAT, 0666, &qattr))
        >= (mqd_t) 0) {
      break;
    }
    fprintf(stderr, "%s: Failed in mq_open, errno=%d\n", __FUNCTION__, errno);
    nanosleep(&idle_time, NULL);
  }

  // Attach callback func to message queue fd
  l_eStatus = FrameworkunifiedAttachCallbackToDispatcherWithFd(hApp, static_cast<int>(g_qid),
                                                  OnMessageQueueReceived);
  if (eFrameworkunifiedStatusOK != l_eStatus) {
    fprintf(
        stderr,
        "%s: Error in FrameworkunifiedAttachCallbackToDispatcherWithFd with mq_fd, status=%d\n",
        __FUNCTION__, l_eStatus);
    return l_eStatus;
  }
  // Start Cyclic Timer
  timer_handle = FrameworkunifiedAttachTimerCallback(hApp, USB_RT_TIMER_CYCLE,
  USB_RT_TIMER_CYCLE,
                                        OnSyncTimeout);
  if (timer_handle == NULL) {
    fprintf(stderr, "%s: Error in FrameworkunifiedAttachTimerCallback\n", __FUNCTION__);
    return eFrameworkunifiedStatusFail;
  }

  // Enter dispatcher ==> Just return
  return eFrameworkunifiedStatusOK;
}

EFrameworkunifiedStatus rtUsbLogTShutdown(HANDLE hApp) {
  return eFrameworkunifiedStatusOK;
}

EFrameworkunifiedStatus OnDeviceDetectionAvailability(HANDLE hApp) {
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;

  if (FrameworkunifiedIsServiceAvailable(hApp)) {
    if (eFrameworkunifiedStatusOK != (eStatus = g_devDetect.OpenSessionRequest())) {
      fprintf(stderr, "%s: Open session request failed\n", __FUNCTION__);
    }
  } else {
    if (eFrameworkunifiedStatusOK != (eStatus = g_devDetect.CloseSessionRequest())) {
      fprintf(stderr, "%s: Close session request failed\n", __FUNCTION__);
    }
  }
  return eStatus;
}

EFrameworkunifiedStatus OnDeviceDetectionOpenSessionACK(HANDLE hApp) {
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;

  if (eFrameworkunifiedStatusOK == (eStatus = g_devDetect.DecodeOpenSessionResponse())) {
    if (eFrameworkunifiedStatusOK
        != (eStatus = g_devDetect.RegisterForDeviceDetectionEvent(
            SS_DEV_DETECT_ANY_USB_EV, onUsbDetectCallback))) {
      fprintf(stderr, "%s: Registration for SS_DEV_DETECT_ANY_USB_EV failed\n",
              __FUNCTION__);
    }
  } else {
    fprintf(stderr, "%s: Decode open session response failed\n", __FUNCTION__);
  }
  return eStatus;
}

EFrameworkunifiedStatus onDeviceDetectionCloseSessionACK(HANDLE hApp) {
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
  CloseSessionAck tCloseSessionAck;

  if (hApp) {
    if (eFrameworkunifiedStatusOK
        == (eStatus = FrameworkunifiedGetMsgDataOfSize(hApp, &tCloseSessionAck,
                                          sizeof(tCloseSessionAck)))) {
      if (eFrameworkunifiedStatusOK != tCloseSessionAck.eStatus) {
        eStatus = eFrameworkunifiedStatusFail;
      }
    }
  }
  return eStatus;
}

EFrameworkunifiedStatus onUsbDetectCallback(HANDLE hApp) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  SS_MediaDetectInfo l_tMediaDetectInfo;

  if (eFrameworkunifiedStatusOK
      != (l_eStatus = FrameworkunifiedGetMsgDataOfSize(hApp, &l_tMediaDetectInfo,
                                          sizeof(l_tMediaDetectInfo)))) {
    fprintf(stderr, "%s: FrameworkunifiedGetMsgDataOfSize Failed Status:0x%x\n",
            __FUNCTION__, l_eStatus);
  } else {
    if (l_tMediaDetectInfo.dev_type != eUSB) {  // NOT mass storage device
      return l_eStatus;
    }
    if (l_tMediaDetectInfo.bIsDeviceAvailable) {
      if (g_usb_available) {  // Already mounted
        return l_eStatus;
      }
      // set USB device available
      g_usb_available = true;
      // Organize paths to access
      strncpy(g_mnt_path, l_tMediaDetectInfo.deviceMountpath,
              sizeof(g_mnt_path));

      snprintf(g_fld_path, MAXPATHLEN, "%s%s", g_mnt_path, USB_RT_FOLDERPATH_);
      snprintf(g_log_path, MAXPATHLEN, "%s/%s", g_fld_path, USB_RT_LOGNAME_);
      g_log_name = const_cast<char*>(USB_RT_LOGNAME_);



//      snprintf(g_log_tmp_path, "%s", USB_RT_TMP_PATH);
      snprintf(g_log_tmp_path, MAXPATHLEN, "%s", USB_RT_TMP_PATH);


    } else {
      // unset USB device available
      if (strcmp(l_tMediaDetectInfo.deviceMountpath, g_mnt_path) == 0) {
        g_usb_available = false;
        g_mnt_path[0] = 0;
        if (g_wfd != -1 && g_wfd != -2) {
          close(g_wfd);
          g_wfd = -1;
          EncryptAndCopyFile();
        }
      }
    }
  }

  return (l_eStatus);
}

EFrameworkunifiedStatus EncryptAndCopyFile(void) {
  char usblogpath[MAXPATHLEN];
  char logpath[MAXPATHLEN];
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  int rfd, wfd;
  int ret;
  struct stat istat_buf;


//  snprintf(usblogpath, "%s%05d.log", g_log_path, g_num_of_file);

//  snprintf(logpath, "%s/%s", g_log_tmp_path, USB_RT_LOGNAME_TMP);
  snprintf(usblogpath, MAXPATHLEN, "%s%05d.log", g_log_path, g_num_of_file);

  snprintf(logpath, MAXPATHLEN, "%s/%s", g_log_tmp_path, USB_RT_LOGNAME_TMP);


  rfd = open(logpath, O_RDWR | O_CLOEXEC);
  if (rfd == -1) {
    fprintf(stderr, "%s: Error. Failed to open file: %s for reading.\n",
            __FUNCTION__, logpath);
    return eFrameworkunifiedStatusFail;
  }

  ret = fstat(rfd, &istat_buf);
  if (ret < 0) {
    fprintf(stderr, "%s: Error. stat(%s). errno: %d, %s \n", __FUNCTION__,
            logpath, errno, strerror(errno));
    close(rfd);
    return eFrameworkunifiedStatusFail;
  } else {
    BYTE *pData;
    int total_len;
    int read_len;
    off_t total_size = istat_buf.st_size;

    wfd = open(usblogpath, O_RDWR | O_APPEND | O_TRUNC | O_CREAT | O_CLOEXEC,
               0644);
    if (wfd == -1) {
      fprintf(stderr, "%s: Error. Failed to open file: %s for reading.\n",
              __FUNCTION__, usblogpath);
      close(rfd);
      return eFrameworkunifiedStatusFail;
    }

    pData = reinterpret_cast<BYTE*>(malloc(ENC_BUF_MAX_SIZE));
    if (pData == NULL) {
      fprintf(stderr, "%s: Error. Failed to malloc %d byte for dst \n",
              __FUNCTION__, ENC_BUF_MAX_SIZE);
      close(rfd);
      close(wfd);
      return eFrameworkunifiedStatusFail;
    }

    total_len = static_cast<int>(total_size);
    do {
      read_len = read(rfd, pData, ENC_BUF_MAX_SIZE);
      if (read_len == -1) {
        fprintf(stderr, "%s: Error. File read failed. errno: %d, %s \n",
                __FUNCTION__, errno, strerror(errno));
        l_eStatus = eFrameworkunifiedStatusFail;
        break;
      }
      ret = write(wfd, pData, read_len);
      if (ret == -1) {
        fprintf(stderr, "%s: Error. length(%d) no space to write: %d, %s \n",
                __FUNCTION__, ret, errno, strerror(errno));
        l_eStatus = eFrameworkunifiedStatusFail;
        break;
      }
      total_len -= read_len;
    } while (total_len > 0);

    fsync(wfd);
    free(pData);
    close(wfd);
    close(rfd);

    if (l_eStatus == eFrameworkunifiedStatusFail) {
      return l_eStatus;
    }
    {
      // Synchronization security
      struct stat ostat_buf;
      while (1) {
        ret = stat(usblogpath, &ostat_buf);
        if (ret < 0) {
          fprintf(stderr, "%s: Error. stat(%s). errno: %d, %s", __FUNCTION__,
                  usblogpath, errno, strerror(errno));
          break;
        }
        if (ostat_buf.st_size >= total_size) {
          break;
        }
        usleep(1000);  // interval
      }
    }
  }
  return l_eStatus;
}