summaryrefslogtreecommitdiffstats
path: root/systemservice/interface_unified/library/src/ss_devicedetection_service_ifc.cpp
blob: 1f68df6c991f78cf47f1649bd2804ed4e283ee2d (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
/*
 * @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_SystemServices_DeviceDetection
/// \brief    This file contains service interface functions.
///
///////////////////////////////////////////////////////////////////////////////
#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_types.h>
#include <native_service/frameworkunified_framework_types.h>
#include <string.h>

#ifdef AGL_STUB
#else
#include "dcmd_sim_mmcsd.h"
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#ifdef AGL_STUB
#else
#include <sys/dcmd_cam.h>
#endif
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/cdrom.h>
#include <asm-generic/param.h>

#include "system_service/ss_services.h"
#include "system_service/ss_devicedetection_service_protocol.h"
#include "system_service/ss_devicedetection_service_protocol_local.h"
#include "system_service/ss_devicedetection_service_notifications.h"
#include "system_service/ss_devicedetection_service_local.h"
#include "system_service/ss_devicedetection_service_types_local.h"
#include "ss_devicedetection_service_if_interfaceunifiedlog.h"

DeviceDetectionServiceIf::DeviceDetectionServiceIf()
    : m_hApp(NULL),
      m_hService(NULL),
      m_hSession(NULL) {
}

DeviceDetectionServiceIf::~DeviceDetectionServiceIf() {
  CloseSessionRequest();  // if the session is still connected
  if ((NULL != m_hApp) && (NULL != m_hService)) {
    FrameworkunifiedCloseService(m_hApp, m_hService);
  }
  m_hService = NULL;
  m_hSession = NULL;
  m_hApp = NULL;
}  // LCOV_EXCL_BR_LINE 10:Because destructor

EFrameworkunifiedStatus DeviceDetectionServiceIf::NotifyOnDeviceDetectionAvailability(
    CbFuncPtr f_pCallBackFn) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;
  if (NULL != m_hApp && NULL != f_pCallBackFn) {
    // Subscriptions
    FrameworkunifiedNotificationCallbackHandler
    g_aryDeviceDetect_Notif_Cbs[] = {
      // Notifications name,                Call back function
      { NTFY_SS_Device_Detection_Service_Availability, f_pCallBackFn},
    };

    if (0 != strcmp(SS_DEV_DETECT_SRV, FrameworkunifiedGetAppName(m_hApp))) {
      // Subscribe and attach call backs to notifications
      if (eFrameworkunifiedStatusOK
          != (eStatus = FrameworkunifiedSubscribeNotificationsWithCallback(
              m_hApp, g_aryDeviceDetect_Notif_Cbs,
              _countof(g_aryDeviceDetect_Notif_Cbs)))) {
        FRAMEWORKUNIFIEDLOG(
            ZONE_ERR,
            __FUNCTION__,
            "Error: FrameworkunifiedAttachNotificationCallbacksToDispatcher Failed Status:0x%x ",
            eStatus);
      }
    }
  }
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return eStatus;
}

EFrameworkunifiedStatus DeviceDetectionServiceIf::DetachDeviceDetectionAvailabilityCb() {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;
  if (NULL != m_hApp) {
    if (0 != strcmp(SS_DEV_DETECT_SRV, FrameworkunifiedGetAppName(m_hApp))) {
      // Unsubscribe and detach call backs to notifications
      if (eFrameworkunifiedStatusOK != (eStatus =
          FrameworkunifiedUnsubscribeNotificationWithCallback(m_hApp, NTFY_SS_Device_Detection_Service_Availability))) {
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
               "Error: InterfaceunifiedDetachNotificationCallback Failed Status:0x%x ",
               eStatus);
      }
    }
  }
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return eStatus;
}

EFrameworkunifiedStatus DeviceDetectionServiceIf::NotifyOnOpenSessionAck(
    CbFuncPtr f_pCallBackFn) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;
  if (NULL != m_hApp && NULL != f_pCallBackFn) {
    if (eFrameworkunifiedStatusOK
        != (eStatus = FrameworkunifiedAttachCallbackToDispatcher(m_hApp,
                                                    SS_DEV_DETECT_SRV,
                                                    PROTOCOL_OPEN_SESSION_ACK,
                                                    f_pCallBackFn))) {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
             "Error: FrameworkunifiedAttachCallbackToDispatcher Failed Status:0x%x ",
             eStatus);
    }
  }
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return eStatus;
}

EFrameworkunifiedStatus DeviceDetectionServiceIf::DetachOpenSessionAckCb() {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;
  if (NULL != m_hApp) {
    if (eFrameworkunifiedStatusOK
        != (eStatus = FrameworkunifiedDetachCallbackFromDispatcher(m_hApp,
        SS_DEV_DETECT_SRV,
                                                      PROTOCOL_OPEN_SESSION_ACK))) {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
             "Error: FrameworkunifiedDetachCallbackFromDispatcher Failed Status:0x%x ",
             eStatus);
    }
  }
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return eStatus;
}

EFrameworkunifiedStatus DeviceDetectionServiceIf::NotifyOnCloseSessionAck(
    CbFuncPtr f_pCallBackFn) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;

  if (NULL != m_hApp && NULL != f_pCallBackFn) {
    if (eFrameworkunifiedStatusOK
        != (eStatus = FrameworkunifiedAttachCallbackToDispatcher(m_hApp,
        SS_DEV_DETECT_SRV,
                                                    PROTOCOL_CLOSE_SESSION_ACK,
                                                    f_pCallBackFn))) {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
             "Error: FrameworkunifiedAttachCallbackToDispatcher Failed Status:0x%x ",
             eStatus);
    }
  }

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return eStatus;
}

EFrameworkunifiedStatus DeviceDetectionServiceIf::DetachCloseSessionAckCb() {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;

  if (NULL != m_hApp) {
    if (eFrameworkunifiedStatusOK
        != (eStatus = FrameworkunifiedDetachCallbackFromDispatcher(
            m_hApp,
            SS_DEV_DETECT_SRV,
            PROTOCOL_CLOSE_SESSION_ACK))) {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
             "Error: FrameworkunifiedDetachCallbackFromDispatcher Failed Status:0x%x ",
             eStatus);
    }
  }

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return eStatus;
}

EFrameworkunifiedStatus DeviceDetectionServiceIf::RegisterForDeviceDetectionEvent(
    SS_DeviceDetectionServerEvents f_eDevDetectEvent, CbFuncPtr f_pCallBackFn,
    PCSTR pFilepath) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;
  char l_filepathname[FILEPATH_SIZE] = { '\0' };
  int len = 0;

  if (NULL != pFilepath) {
    len = strlen(pFilepath);
    strcpy(l_filepathname, pFilepath);  // NOLINT (runtime/printf)
  }

  if (NULL != m_hSession && NULL != f_pCallBackFn) {
    if (CheckDetectEvent(f_eDevDetectEvent)) {
      if (eFrameworkunifiedStatusOK
          != (eStatus = FrameworkunifiedAttachCallbackToDispatcher(m_hApp,
          SS_DEV_DETECT_SRV,
                                                      f_eDevDetectEvent,
                                                      f_pCallBackFn, m_hSession))) {
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
               "Error: FrameworkunifiedAttachCallbackToDispatcher Failed Status:0x%x ",
               eStatus);
      }

      eStatus = FrameworkunifiedSendMsg(m_hSession, SS_REGISTER_DETECT_EVENT,
                                         sizeof(SS_DeviceDetectionServerEvents),
                                         &f_eDevDetectEvent);
    } else {
      if (eFrameworkunifiedStatusOK
          != (eStatus = FrameworkunifiedAttachCallbackToDispatcher(m_hApp,
          SS_DEV_DETECT_SRV,
                                                      f_eDevDetectEvent,
                                                      f_pCallBackFn, m_hSession))) {
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
               "Error: FrameworkunifiedAttachCallbackToDispatcher Failed Status:0x%x ",
               eStatus);
      }

      // client registers for the event
      if (eFrameworkunifiedStatusOK
          != (eStatus = FrameworkunifiedRegisterEvent(m_hSession, f_eDevDetectEvent))) {
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
               "Error: FrameworkunifiedRegisterEvents Failed Status:0x%x", eStatus);
      }

      if (NULL != pFilepath) {
        eStatus = FrameworkunifiedSendMsg(m_hSession, SS_REGISTER_FILEPATHNAME, len,
                                             l_filepathname);
      }
    }
  }
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return eStatus;
}

EFrameworkunifiedStatus DeviceDetectionServiceIf::UnRegisterForDeviceDetectionEvent(
    SS_DeviceDetectionServerEvents f_eDevDetectEvent) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;

  if (NULL != m_hSession) {
    if (eFrameworkunifiedStatusOK
        != (eStatus = FrameworkunifiedDetachCallbackFromDispatcher(m_hApp,
                                                      SS_DEV_DETECT_SRV,
                                                      f_eDevDetectEvent,
                                                      m_hSession))) {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
             "Error: InterfaceunifiedDetachCallbackToDispatcher Failed Status:0x%x ", eStatus);
    }

    // client registers for the event
    if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedUnRegisterEvent(m_hSession, f_eDevDetectEvent))) {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
             "Error: FrameworkunifiedUnRegisterEvents Failed Status:0x%x", eStatus);
    }

    if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedSendMsg(m_hSession, SS_UNREGISTER_DETECT_EVENT,
                                              sizeof(SS_DeviceDetectionServerEvents),
                                              &f_eDevDetectEvent))) {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
             "UnRegister Detect Event[0x%x] Send Error[0x%x]", f_eDevDetectEvent, eStatus);
    }
  }

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return eStatus;
}

BOOL DeviceDetectionServiceIf::Initialize(HANDLE hApp) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  BOOL l_eStatus = TRUE;

  if (NULL != hApp) {
    m_hApp = hApp;
  } else {
    l_eStatus = FALSE;  // eFrameworkunifiedStatusInvldHandle;
  }
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_eStatus;
}

EFrameworkunifiedStatus DeviceDetectionServiceIf::OpenSessionRequest() {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusInvldHandle;
  if (NULL != m_hApp) {
    // if the session is there then we need to close it first!
    if (NULL != m_hService) {
      CloseSessionRequest();
      FrameworkunifiedCloseService(m_hApp, m_hService);
      m_hService = NULL;
    }
    BOOL l_bServiceAvailable = FrameworkunifiedIsServiceAvailable(m_hApp);
    if (FALSE == l_bServiceAvailable) {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __PRETTY_FUNCTION__,
             "DeviceError: Service is unavailable");
      l_eStatus = eFrameworkunifiedStatusFail;
    } else {
      // Now open the service.
      if (l_bServiceAvailable
          && (NULL != (m_hService = OpenService()))) {  // LCOV_EXCL_BR_LINE 8:Because l_bServiceAvailable is always TRUE
        // [DM: TODO] Check if we need to open a session with some data sent to Server.
        UI_32 l_pTestData = 1;
        if (eFrameworkunifiedStatusOK != (l_eStatus = FrameworkunifiedOpenSessionWithData(m_hService,
                                                   (PVOID) &l_pTestData,
                                                   sizeof(UI_32)))) {
          FRAMEWORKUNIFIEDLOG(ZONE_ERR, __PRETTY_FUNCTION__, "Error: FrameworkunifiedOpenSessionWithData Failed");
        }
      } else {
        l_eStatus = eFrameworkunifiedStatusFail;
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __PRETTY_FUNCTION__, "Error: FrameworkunifiedOpenService Failed");
      }
    }
  } else {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error: OpenSessionRequest Failed %X",
           l_eStatus);
  }
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_eStatus;
}

HANDLE DeviceDetectionServiceIf::OpenService() {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  HANDLE l_hService = NULL;

  if (NULL == (l_hService = FrameworkunifiedOpenService(m_hApp, SS_DEV_DETECT_SRV))) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __PRETTY_FUNCTION__, "Error: FrameworkunifiedOpenService : Failed to open Device Detection Service");
  }
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_hService;
}

EFrameworkunifiedStatus DeviceDetectionServiceIf::DecodeOpenSessionResponse() {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusInvldHandle;

  if (NULL != m_hService && NULL != m_hApp) {  // LCOV_EXCL_BR_LINE 8:Because when m_hApp is NULL, m_hService is NULL
    if (NULL == (m_hSession = FrameworkunifiedGetOpenSessionHandle(m_hApp))) {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __PRETTY_FUNCTION__, "Error: Error in extracting OpenSessionAck response.");
      l_eStatus = eFrameworkunifiedStatusFail;
    } else {
      l_eStatus = eFrameworkunifiedStatusOK;
      FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Logger Session Name: %s, Session Id: %d",
             FrameworkunifiedGetSessionName(m_hSession), FrameworkunifiedGetSessionId(m_hSession));
    }
  } else {
    FRAMEWORKUNIFIEDLOG(
        ZONE_ERR,
        __FUNCTION__,
        "Error. m_hService: %p, m_hApp: %p is NULL. Open session handle not acquired,"
        " l_eStatus: eFrameworkunifiedStatusInvldHandle.", m_hService, m_hApp);
  }

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_eStatus;
}

EFrameworkunifiedStatus DeviceDetectionServiceIf::CloseSessionRequest() {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusInvldHandle;

  if (NULL != m_hService && NULL != m_hSession) {
    if (eFrameworkunifiedStatusOK != (l_eStatus = FrameworkunifiedCloseSession(m_hService, m_hSession))) {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error: FrameworkunifiedCloseSession Failed");
    }
    m_hSession = NULL;  // clear our session handle
  }
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_eStatus;
}

BOOL DeviceDetectionServiceIf::CheckDetectEvent(
    SS_DeviceDetectionServerEvents f_eDevDetectEvent) {
  BOOL chkevt = FALSE;

  switch (f_eDevDetectEvent) {
    case SS_DEV_INFO_ANY_USB_EV:
    case SS_DEV_DETECT_ANY_USB_EV:
    case SS_DEV_DETECT_ANY_SD_EV:
    case SS_DEV_DETECT_ANY_DISC_EV:
    case SS_DEV_DETECT_ANY_USB_NCM_EV:

    case SS_DEV_DETECT_ANY_USB_DCM_NCM_EV:
    case SS_DEV_DETECT_ANY_USB__CWORD57__EV:
    case SS_DEV_DETECT_ANY_USB_ACM_EV:
    case SS_DEV_DETECT_ANY_USB_DEV_MNG_EV:
    case SS_DEV_DETECT_ANY_USB_VEHICLE_CTRL_EV:
    case SS_DEV_DETECT_ANY_USB_DSRC_APP_EV:
    case SS_DEV_DETECT_ANY_USB_DSRC_CTRL_EV:
    case SS_DEV_DETECT_ANY_USB_IR_VICS_DATA_EV:
    case SS_DEV_ERR_USB_OVER_CURRENT_EV:
    case SS_DEV_DETECT_ANY_USB_MTP_EV:
    case SS_DEV_INFO_ANY_USB_NOTIFY_EV:
    case SS_DEV_DETECT_ANY_USB__CWORD57__ROLE_EV:
    case SS_DEV_INFO_SD_FORMAT_COMP_EV:
    case SS_DEV_INFO_ANY_USB_DVDP_EV:
      chkevt = TRUE;
      break;
    default:
      break;
  }
  return chkevt;
}

static void ReadString(const char *file, char *buf, int len) {
  int fd;
  ssize_t r;
  char *tmp;

  if (file == NULL) {  // LCOV_EXCL_BR_LINE 8:Because file is not NULL
    // LCOV_EXCL_START  8:Because file is not NULL
    buf[0] = '\0';
    return;
    // LCOV_EXCL_STOP
  }

  fd = open(file, O_RDONLY);
  if (fd < 0) {  // LCOV_EXCL_BR_LINE 6:Because the sequence at the time of open failure cannot be passed
    // LCOV_EXCL_START 6:Because the sequence at the time of open failure cannot be passed
    buf[0] = '\0';
    return;
    // LCOV_EXCL_STOP
  }

  r = read(fd, buf, len);
  close(fd);
  if (r > 0 && r < len) {
    buf[r] = '\0';
    r--;
    /* If there is a line feed code'\n' at the end of the acquired data, replace it with '\0' */
    while (buf[r] == '\n') {
      buf[r] = '\0';
      if (r == 0)
        break;
      r--;
    }
    /* If there is a line feed code'\n' in the middle of the acquired data, replace with '\0' */
    tmp = buf;
    while ((tmp = strchr(tmp, '\n')) != NULL) {
      *tmp = ' ';
      tmp++;
    }
    return;
  } else {
    buf[0] = '\0';
  }
}

static bool isMounttedDir(char* pMountDir) {
  char buf[4096];

  if (!pMountDir) {  // LCOV_EXCL_BR_LINE 8:Because pMountDir is not NULL
    // LCOV_EXCL_START 8:Because pMountDir is not NULL
    return false;
    // LCOV_EXCL_STOP
  }

  ReadString("/proc/mounts", buf, sizeof(buf));
  if (strstr(buf, pMountDir)) {  // The mount folder is already in use as a mount point
    return true;
  }

  return false;
}

EFrameworkunifiedStatus DeviceDetectionServiceIf::MountSD(uint8_t part_num,
                                             BOOL *already_mounted) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;

  FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "part_num %d", part_num);

  if (NULL == m_hSession) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "m_hSession is NULL");
    return eFrameworkunifiedStatusInvldHandle;
  }

  if (part_num > 3) {
    return eFrameworkunifiedStatusInvldParam;
  }

  char mount_dir[256];
  if (part_num != 0) {
    sprintf(mount_dir, "/mnt/mmcblk1p%d", part_num);  // NOLINT (runtime/printf)
  } else {
    sprintf(mount_dir, "/mnt/mmcblk1");  // NOLINT (runtime/printf)
  }
  if (isMounttedDir(mount_dir)) {
    *already_mounted = TRUE;
    return eFrameworkunifiedStatusOK;
  } else {
    *already_mounted = FALSE;
  }

  char filename[256];
  struct stat st;
  if (part_num != 0) {
    sprintf(filename, "/dev/mmcblk1p%d", part_num);  // NOLINT (runtime/printf)
  } else {
    sprintf(filename, "/dev/mmcblk1");  // NOLINT (runtime/printf)
  }
  if (stat(filename, &st) != 0) {
    return eFrameworkunifiedStatusAccessError;
  }

  if (eFrameworkunifiedStatusOK
      == (eStatus = FrameworkunifiedSendMsg(m_hSession, SS_DEV_MOUNT_SD, sizeof(uint8_t),
                               &part_num))) {
    FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Send MountSD msg Sucessfully");
  }

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");

  return eStatus;
}

EFrameworkunifiedStatus DeviceDetectionServiceIf::UmountSD(uint8_t part_num,
                                              BOOL *already_umounted) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;

  FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "part_num %d", part_num);

  if (NULL == m_hSession) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "m_hSession is NULL");
    return eFrameworkunifiedStatusInvldHandle;
  }

  if (part_num > 3) {
    return eFrameworkunifiedStatusInvldParam;
  }

  char mount_dir[256];
  if (part_num != 0) {
    sprintf(mount_dir, "/mnt/mmcblk1p%d", part_num);  // NOLINT (runtime/printf)
  } else {
    sprintf(mount_dir, "/mnt/mmcblk1");  // NOLINT (runtime/printf)
  }

  if (isMounttedDir(mount_dir)) {
    *already_umounted = FALSE;
  } else {
    *already_umounted = TRUE;
    return eFrameworkunifiedStatusOK;
  }

  char filename[256];
  struct stat st;

  if (part_num != 0) {
    sprintf(filename, "/dev/mmcblk1p%d", part_num);  // NOLINT (runtime/printf)
  } else {
    sprintf(filename, "/dev/mmcblk1");  // NOLINT (runtime/printf)
  }
  if (stat(filename, &st) != 0) {
    return eFrameworkunifiedStatusAccessError;
  }

  if (eFrameworkunifiedStatusOK
      == (eStatus = FrameworkunifiedSendMsg(m_hSession, SS_DEV_UMOUNT_SD, sizeof(uint8_t),
                               &part_num))) {
    FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Send UmountSD msg Sucessfully");
  }

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");

  return eStatus;
}

static const uint8_t VBUS_RESET_INTERVAL_MIN = 11;

static EFrameworkunifiedStatus SendUsbVBusResetMsg(HANDLE handle, int8_t port, uint8_t interval) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __func__, "+");
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;

  if (NULL == handle) {
    FRAMEWORKUNIFIEDLOG(ZONE_WARN, __func__, "Invalid Handle.");
    return eFrameworkunifiedStatusInvldHandle;
  }

  int16_t data[] = {port, interval};
  eStatus = FrameworkunifiedSendMsg(handle, SS_USB_VBUS_RESET, sizeof(data), data);

  if (eFrameworkunifiedStatusOK != eStatus) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedSendMsg Send Error[0x%x]", eStatus);
    return eStatus;
  }
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __func__, "-");
  return eStatus;
}

EFrameworkunifiedStatus DeviceDetectionServiceIf::UsbVBusReset(int8_t port, uint8_t interval) {
  return SendUsbVBusResetMsg(m_hSession, port, interval);
}

EFrameworkunifiedStatus DeviceDetectionServiceIf::UsbVBusReset(int8_t port) {
  return SendUsbVBusResetMsg(m_hSession, port, VBUS_RESET_INTERVAL_MIN);
}

EFrameworkunifiedStatus DeviceDetectionServiceIf::RoleSwStateNotify(
    SS_DEV_ROLE_SW_STATE state) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
  EFrameworkunifiedStatus result = eFrameworkunifiedStatusFail;
  UI_32 rcvlength = 0;
  UI_32 bufflength;

  if (NULL == m_hSession) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "No Session");
    return eFrameworkunifiedStatusInvldHandle;
  }

  bufflength = sizeof(EFrameworkunifiedStatus);
  eStatus = FrameworkunifiedInvokeSync(m_hSession, SS_DEV_ROLE_SW_STATE_NOTIFY,
                          sizeof(SS_DEV_ROLE_SW_STATE), &state, bufflength,
                          &result, &rcvlength);

  if (eFrameworkunifiedStatusOK != eStatus) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "FrameworkunifiedSendMsg Send Error[0x%x]", eStatus);
    return eStatus;
  }

  if (bufflength != rcvlength) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
           "FrameworkunifiedInvokeSync Response Error. bufflen[%d], rcvlen[%d]", bufflength,
           rcvlength);
    return eStatus;
  }

  eStatus = result;

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return eStatus;
}

EFrameworkunifiedStatus DeviceDetectionServiceIf::FormatSD(void) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;

  if (NULL == m_hSession) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Invalid Handle.");
    return eFrameworkunifiedStatusInvldHandle;
  }

  if (access("/dev/mmcblk1", F_OK) != 0) {
    return eFrameworkunifiedStatusAccessError;
  }

  eStatus = FrameworkunifiedSendMsg(m_hSession, SS_DEV_FORMAT_SD, 0, NULL);

  if (eFrameworkunifiedStatusOK != eStatus) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "FrameworkunifiedSendMsg Send Error[0x%x]", eStatus);
    return eStatus;
  }
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return eStatus;
}  // LCOV_EXCL_BR_LINE 10:Because the last line