Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / systemservice / interface_unified / library / src / ss_devicedetection_service_ifc.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 ///////////////////////////////////////////////////////////////////////////////
18 /// \ingroup  tag_SystemServices_DeviceDetection
19 /// \brief    This file contains service interface functions.
20 ///
21 ///////////////////////////////////////////////////////////////////////////////
22 #include "system_service/ss_devicedetection_service_ifc.h"
23 #include <native_service/frameworkunified_application.h>
24 #include <native_service/frameworkunified_framework_if.h>
25 #include <native_service/frameworkunified_types.h>
26 #include <native_service/frameworkunified_framework_types.h>
27 #include <string.h>
28
29 #ifdef AGL_STUB
30 #else
31 #include "dcmd_sim_mmcsd.h"
32 #endif
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <stdio.h>
38 #ifdef AGL_STUB
39 #else
40 #include <sys/dcmd_cam.h>
41 #endif
42 #include <errno.h>
43 #include <sys/ioctl.h>
44 #include <linux/cdrom.h>
45 #include <asm-generic/param.h>
46
47 #include "system_service/ss_services.h"
48 #include "system_service/ss_devicedetection_service_protocol.h"
49 #include "system_service/ss_devicedetection_service_protocol_local.h"
50 #include "system_service/ss_devicedetection_service_notifications.h"
51 #include "system_service/ss_devicedetection_service_local.h"
52 #include "system_service/ss_devicedetection_service_types_local.h"
53 #include "ss_devicedetection_service_if_interfaceunifiedlog.h"
54
55 DeviceDetectionServiceIf::DeviceDetectionServiceIf()
56     : m_hApp(NULL),
57       m_hService(NULL),
58       m_hSession(NULL) {
59 }
60
61 DeviceDetectionServiceIf::~DeviceDetectionServiceIf() {
62   CloseSessionRequest();  // if the session is still connected
63   if ((NULL != m_hApp) && (NULL != m_hService)) {
64     FrameworkunifiedCloseService(m_hApp, m_hService);
65   }
66   m_hService = NULL;
67   m_hSession = NULL;
68   m_hApp = NULL;
69 }  // LCOV_EXCL_BR_LINE 10:Because destructor
70
71 EFrameworkunifiedStatus DeviceDetectionServiceIf::NotifyOnDeviceDetectionAvailability(
72     CbFuncPtr f_pCallBackFn) {
73   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
74   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;
75   if (NULL != m_hApp && NULL != f_pCallBackFn) {
76     // Subscriptions
77     FrameworkunifiedNotificationCallbackHandler
78     g_aryDeviceDetect_Notif_Cbs[] = {
79       // Notifications name,                Call back function
80       { NTFY_SS_Device_Detection_Service_Availability, f_pCallBackFn},
81     };
82
83     if (0 != strcmp(SS_DEV_DETECT_SRV, FrameworkunifiedGetAppName(m_hApp))) {
84       // Subscribe and attach call backs to notifications
85       if (eFrameworkunifiedStatusOK
86           != (eStatus = FrameworkunifiedSubscribeNotificationsWithCallback(
87               m_hApp, g_aryDeviceDetect_Notif_Cbs,
88               _countof(g_aryDeviceDetect_Notif_Cbs)))) {
89         FRAMEWORKUNIFIEDLOG(
90             ZONE_ERR,
91             __FUNCTION__,
92             "Error: FrameworkunifiedAttachNotificationCallbacksToDispatcher Failed Status:0x%x ",
93             eStatus);
94       }
95     }
96   }
97   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
98   return eStatus;
99 }
100
101 EFrameworkunifiedStatus DeviceDetectionServiceIf::DetachDeviceDetectionAvailabilityCb() {
102   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
103   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;
104   if (NULL != m_hApp) {
105     if (0 != strcmp(SS_DEV_DETECT_SRV, FrameworkunifiedGetAppName(m_hApp))) {
106       // Unsubscribe and detach call backs to notifications
107       if (eFrameworkunifiedStatusOK != (eStatus =
108           FrameworkunifiedUnsubscribeNotificationWithCallback(m_hApp, NTFY_SS_Device_Detection_Service_Availability))) {
109         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
110                "Error: InterfaceunifiedDetachNotificationCallback Failed Status:0x%x ",
111                eStatus);
112       }
113     }
114   }
115   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
116   return eStatus;
117 }
118
119 EFrameworkunifiedStatus DeviceDetectionServiceIf::NotifyOnOpenSessionAck(
120     CbFuncPtr f_pCallBackFn) {
121   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
122   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;
123   if (NULL != m_hApp && NULL != f_pCallBackFn) {
124     if (eFrameworkunifiedStatusOK
125         != (eStatus = FrameworkunifiedAttachCallbackToDispatcher(m_hApp,
126                                                     SS_DEV_DETECT_SRV,
127                                                     PROTOCOL_OPEN_SESSION_ACK,
128                                                     f_pCallBackFn))) {
129       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
130              "Error: FrameworkunifiedAttachCallbackToDispatcher Failed Status:0x%x ",
131              eStatus);
132     }
133   }
134   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
135   return eStatus;
136 }
137
138 EFrameworkunifiedStatus DeviceDetectionServiceIf::DetachOpenSessionAckCb() {
139   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
140   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;
141   if (NULL != m_hApp) {
142     if (eFrameworkunifiedStatusOK
143         != (eStatus = FrameworkunifiedDetachCallbackFromDispatcher(m_hApp,
144         SS_DEV_DETECT_SRV,
145                                                       PROTOCOL_OPEN_SESSION_ACK))) {
146       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
147              "Error: FrameworkunifiedDetachCallbackFromDispatcher Failed Status:0x%x ",
148              eStatus);
149     }
150   }
151   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
152   return eStatus;
153 }
154
155 EFrameworkunifiedStatus DeviceDetectionServiceIf::NotifyOnCloseSessionAck(
156     CbFuncPtr f_pCallBackFn) {
157   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
158   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;
159
160   if (NULL != m_hApp && NULL != f_pCallBackFn) {
161     if (eFrameworkunifiedStatusOK
162         != (eStatus = FrameworkunifiedAttachCallbackToDispatcher(m_hApp,
163         SS_DEV_DETECT_SRV,
164                                                     PROTOCOL_CLOSE_SESSION_ACK,
165                                                     f_pCallBackFn))) {
166       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
167              "Error: FrameworkunifiedAttachCallbackToDispatcher Failed Status:0x%x ",
168              eStatus);
169     }
170   }
171
172   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
173   return eStatus;
174 }
175
176 EFrameworkunifiedStatus DeviceDetectionServiceIf::DetachCloseSessionAckCb() {
177   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
178   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;
179
180   if (NULL != m_hApp) {
181     if (eFrameworkunifiedStatusOK
182         != (eStatus = FrameworkunifiedDetachCallbackFromDispatcher(
183             m_hApp,
184             SS_DEV_DETECT_SRV,
185             PROTOCOL_CLOSE_SESSION_ACK))) {
186       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
187              "Error: FrameworkunifiedDetachCallbackFromDispatcher Failed Status:0x%x ",
188              eStatus);
189     }
190   }
191
192   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
193   return eStatus;
194 }
195
196 EFrameworkunifiedStatus DeviceDetectionServiceIf::RegisterForDeviceDetectionEvent(
197     SS_DeviceDetectionServerEvents f_eDevDetectEvent, CbFuncPtr f_pCallBackFn,
198     PCSTR pFilepath) {
199   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
200   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;
201   char l_filepathname[FILEPATH_SIZE] = { '\0' };
202   int len = 0;
203
204   if (NULL != pFilepath) {
205     len = strlen(pFilepath);
206     strcpy(l_filepathname, pFilepath);  // NOLINT (runtime/printf)
207   }
208
209   if (NULL != m_hSession && NULL != f_pCallBackFn) {
210     if (CheckDetectEvent(f_eDevDetectEvent)) {
211       if (eFrameworkunifiedStatusOK
212           != (eStatus = FrameworkunifiedAttachCallbackToDispatcher(m_hApp,
213           SS_DEV_DETECT_SRV,
214                                                       f_eDevDetectEvent,
215                                                       f_pCallBackFn, m_hSession))) {
216         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
217                "Error: FrameworkunifiedAttachCallbackToDispatcher Failed Status:0x%x ",
218                eStatus);
219       }
220
221       eStatus = FrameworkunifiedSendMsg(m_hSession, SS_REGISTER_DETECT_EVENT,
222                                          sizeof(SS_DeviceDetectionServerEvents),
223                                          &f_eDevDetectEvent);
224     } else {
225       if (eFrameworkunifiedStatusOK
226           != (eStatus = FrameworkunifiedAttachCallbackToDispatcher(m_hApp,
227           SS_DEV_DETECT_SRV,
228                                                       f_eDevDetectEvent,
229                                                       f_pCallBackFn, m_hSession))) {
230         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
231                "Error: FrameworkunifiedAttachCallbackToDispatcher Failed Status:0x%x ",
232                eStatus);
233       }
234
235       // client registers for the event
236       if (eFrameworkunifiedStatusOK
237           != (eStatus = FrameworkunifiedRegisterEvent(m_hSession, f_eDevDetectEvent))) {
238         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
239                "Error: FrameworkunifiedRegisterEvents Failed Status:0x%x", eStatus);
240       }
241
242       if (NULL != pFilepath) {
243         eStatus = FrameworkunifiedSendMsg(m_hSession, SS_REGISTER_FILEPATHNAME, len,
244                                              l_filepathname);
245       }
246     }
247   }
248   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
249   return eStatus;
250 }
251
252 EFrameworkunifiedStatus DeviceDetectionServiceIf::UnRegisterForDeviceDetectionEvent(
253     SS_DeviceDetectionServerEvents f_eDevDetectEvent) {
254   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
255   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;
256
257   if (NULL != m_hSession) {
258     if (eFrameworkunifiedStatusOK
259         != (eStatus = FrameworkunifiedDetachCallbackFromDispatcher(m_hApp,
260                                                       SS_DEV_DETECT_SRV,
261                                                       f_eDevDetectEvent,
262                                                       m_hSession))) {
263       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
264              "Error: InterfaceunifiedDetachCallbackToDispatcher Failed Status:0x%x ", eStatus);
265     }
266
267     // client registers for the event
268     if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedUnRegisterEvent(m_hSession, f_eDevDetectEvent))) {
269       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
270              "Error: FrameworkunifiedUnRegisterEvents Failed Status:0x%x", eStatus);
271     }
272
273     if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedSendMsg(m_hSession, SS_UNREGISTER_DETECT_EVENT,
274                                               sizeof(SS_DeviceDetectionServerEvents),
275                                               &f_eDevDetectEvent))) {
276       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
277              "UnRegister Detect Event[0x%x] Send Error[0x%x]", f_eDevDetectEvent, eStatus);
278     }
279   }
280
281   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
282   return eStatus;
283 }
284
285 BOOL DeviceDetectionServiceIf::Initialize(HANDLE hApp) {
286   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
287   BOOL l_eStatus = TRUE;
288
289   if (NULL != hApp) {
290     m_hApp = hApp;
291   } else {
292     l_eStatus = FALSE;  // eFrameworkunifiedStatusInvldHandle;
293   }
294   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
295   return l_eStatus;
296 }
297
298 EFrameworkunifiedStatus DeviceDetectionServiceIf::OpenSessionRequest() {
299   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
300   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusInvldHandle;
301   if (NULL != m_hApp) {
302     // if the session is there then we need to close it first!
303     if (NULL != m_hService) {
304       CloseSessionRequest();
305       FrameworkunifiedCloseService(m_hApp, m_hService);
306       m_hService = NULL;
307     }
308     BOOL l_bServiceAvailable = FrameworkunifiedIsServiceAvailable(m_hApp);
309     if (FALSE == l_bServiceAvailable) {
310       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __PRETTY_FUNCTION__,
311              "DeviceError: Service is unavailable");
312       l_eStatus = eFrameworkunifiedStatusFail;
313     } else {
314       // Now open the service.
315       if (l_bServiceAvailable
316           && (NULL != (m_hService = OpenService()))) {  // LCOV_EXCL_BR_LINE 8:Because l_bServiceAvailable is always TRUE
317         // [DM: TODO] Check if we need to open a session with some data sent to Server.
318         UI_32 l_pTestData = 1;
319         if (eFrameworkunifiedStatusOK != (l_eStatus = FrameworkunifiedOpenSessionWithData(m_hService,
320                                                    (PVOID) &l_pTestData,
321                                                    sizeof(UI_32)))) {
322           FRAMEWORKUNIFIEDLOG(ZONE_ERR, __PRETTY_FUNCTION__, "Error: FrameworkunifiedOpenSessionWithData Failed");
323         }
324       } else {
325         l_eStatus = eFrameworkunifiedStatusFail;
326         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __PRETTY_FUNCTION__, "Error: FrameworkunifiedOpenService Failed");
327       }
328     }
329   } else {
330     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error: OpenSessionRequest Failed %X",
331            l_eStatus);
332   }
333   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
334   return l_eStatus;
335 }
336
337 HANDLE DeviceDetectionServiceIf::OpenService() {
338   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
339   HANDLE l_hService = NULL;
340
341   if (NULL == (l_hService = FrameworkunifiedOpenService(m_hApp, SS_DEV_DETECT_SRV))) {
342     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __PRETTY_FUNCTION__, "Error: FrameworkunifiedOpenService : Failed to open Device Detection Service");
343   }
344   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
345   return l_hService;
346 }
347
348 EFrameworkunifiedStatus DeviceDetectionServiceIf::DecodeOpenSessionResponse() {
349   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
350   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusInvldHandle;
351
352   if (NULL != m_hService && NULL != m_hApp) {  // LCOV_EXCL_BR_LINE 8:Because when m_hApp is NULL, m_hService is NULL
353     if (NULL == (m_hSession = FrameworkunifiedGetOpenSessionHandle(m_hApp))) {
354       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __PRETTY_FUNCTION__, "Error: Error in extracting OpenSessionAck response.");
355       l_eStatus = eFrameworkunifiedStatusFail;
356     } else {
357       l_eStatus = eFrameworkunifiedStatusOK;
358       FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Logger Session Name: %s, Session Id: %d",
359              FrameworkunifiedGetSessionName(m_hSession), FrameworkunifiedGetSessionId(m_hSession));
360     }
361   } else {
362     FRAMEWORKUNIFIEDLOG(
363         ZONE_ERR,
364         __FUNCTION__,
365         "Error. m_hService: %p, m_hApp: %p is NULL. Open session handle not acquired,"
366         " l_eStatus: eFrameworkunifiedStatusInvldHandle.", m_hService, m_hApp);
367   }
368
369   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
370   return l_eStatus;
371 }
372
373 EFrameworkunifiedStatus DeviceDetectionServiceIf::CloseSessionRequest() {
374   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
375   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusInvldHandle;
376
377   if (NULL != m_hService && NULL != m_hSession) {
378     if (eFrameworkunifiedStatusOK != (l_eStatus = FrameworkunifiedCloseSession(m_hService, m_hSession))) {
379       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error: FrameworkunifiedCloseSession Failed");
380     }
381     m_hSession = NULL;  // clear our session handle
382   }
383   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
384   return l_eStatus;
385 }
386
387 BOOL DeviceDetectionServiceIf::CheckDetectEvent(
388     SS_DeviceDetectionServerEvents f_eDevDetectEvent) {
389   BOOL chkevt = FALSE;
390
391   switch (f_eDevDetectEvent) {
392     case SS_DEV_INFO_ANY_USB_EV:
393     case SS_DEV_DETECT_ANY_USB_EV:
394     case SS_DEV_DETECT_ANY_SD_EV:
395     case SS_DEV_DETECT_ANY_DISC_EV:
396     case SS_DEV_DETECT_ANY_USB_NCM_EV:
397
398     case SS_DEV_DETECT_ANY_USB_DCM_NCM_EV:
399     case SS_DEV_DETECT_ANY_USB__CWORD57__EV:
400     case SS_DEV_DETECT_ANY_USB_ACM_EV:
401     case SS_DEV_DETECT_ANY_USB_DEV_MNG_EV:
402     case SS_DEV_DETECT_ANY_USB_VEHICLE_CTRL_EV:
403     case SS_DEV_DETECT_ANY_USB_DSRC_APP_EV:
404     case SS_DEV_DETECT_ANY_USB_DSRC_CTRL_EV:
405     case SS_DEV_DETECT_ANY_USB_IR_VICS_DATA_EV:
406     case SS_DEV_ERR_USB_OVER_CURRENT_EV:
407     case SS_DEV_DETECT_ANY_USB_MTP_EV:
408     case SS_DEV_INFO_ANY_USB_NOTIFY_EV:
409     case SS_DEV_DETECT_ANY_USB__CWORD57__ROLE_EV:
410     case SS_DEV_INFO_SD_FORMAT_COMP_EV:
411     case SS_DEV_INFO_ANY_USB_DVDP_EV:
412       chkevt = TRUE;
413       break;
414     default:
415       break;
416   }
417   return chkevt;
418 }
419
420 static void ReadString(const char *file, char *buf, int len) {
421   int fd;
422   ssize_t r;
423   char *tmp;
424
425   if (file == NULL) {  // LCOV_EXCL_BR_LINE 8:Because file is not NULL
426     // LCOV_EXCL_START  8:Because file is not NULL
427     buf[0] = '\0';
428     return;
429     // LCOV_EXCL_STOP
430   }
431
432   fd = open(file, O_RDONLY);
433   if (fd < 0) {  // LCOV_EXCL_BR_LINE 6:Because the sequence at the time of open failure cannot be passed
434     // LCOV_EXCL_START 6:Because the sequence at the time of open failure cannot be passed
435     buf[0] = '\0';
436     return;
437     // LCOV_EXCL_STOP
438   }
439
440   r = read(fd, buf, len);
441   close(fd);
442   if (r > 0 && r < len) {
443     buf[r] = '\0';
444     r--;
445     /* If there is a line feed code'\n' at the end of the acquired data, replace it with '\0' */
446     while (buf[r] == '\n') {
447       buf[r] = '\0';
448       if (r == 0)
449         break;
450       r--;
451     }
452     /* If there is a line feed code'\n' in the middle of the acquired data, replace with '\0' */
453     tmp = buf;
454     while ((tmp = strchr(tmp, '\n')) != NULL) {
455       *tmp = ' ';
456       tmp++;
457     }
458     return;
459   } else {
460     buf[0] = '\0';
461   }
462 }
463
464 static bool isMounttedDir(char* pMountDir) {
465   char buf[4096];
466
467   if (!pMountDir) {  // LCOV_EXCL_BR_LINE 8:Because pMountDir is not NULL
468     // LCOV_EXCL_START 8:Because pMountDir is not NULL
469     return false;
470     // LCOV_EXCL_STOP
471   }
472
473   ReadString("/proc/mounts", buf, sizeof(buf));
474   if (strstr(buf, pMountDir)) {  // The mount folder is already in use as a mount point
475     return true;
476   }
477
478   return false;
479 }
480
481 EFrameworkunifiedStatus DeviceDetectionServiceIf::MountSD(uint8_t part_num,
482                                              BOOL *already_mounted) {
483   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
484   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;
485
486   FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "part_num %d", part_num);
487
488   if (NULL == m_hSession) {
489     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "m_hSession is NULL");
490     return eFrameworkunifiedStatusInvldHandle;
491   }
492
493   if (part_num > 3) {
494     return eFrameworkunifiedStatusInvldParam;
495   }
496
497   char mount_dir[256];
498   if (part_num != 0) {
499     sprintf(mount_dir, "/mnt/mmcblk1p%d", part_num);  // NOLINT (runtime/printf)
500   } else {
501     sprintf(mount_dir, "/mnt/mmcblk1");  // NOLINT (runtime/printf)
502   }
503   if (isMounttedDir(mount_dir)) {
504     *already_mounted = TRUE;
505     return eFrameworkunifiedStatusOK;
506   } else {
507     *already_mounted = FALSE;
508   }
509
510   char filename[256];
511   struct stat st;
512   if (part_num != 0) {
513     sprintf(filename, "/dev/mmcblk1p%d", part_num);  // NOLINT (runtime/printf)
514   } else {
515     sprintf(filename, "/dev/mmcblk1");  // NOLINT (runtime/printf)
516   }
517   if (stat(filename, &st) != 0) {
518     return eFrameworkunifiedStatusAccessError;
519   }
520
521   if (eFrameworkunifiedStatusOK
522       == (eStatus = FrameworkunifiedSendMsg(m_hSession, SS_DEV_MOUNT_SD, sizeof(uint8_t),
523                                &part_num))) {
524     FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Send MountSD msg Sucessfully");
525   }
526
527   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
528
529   return eStatus;
530 }
531
532 EFrameworkunifiedStatus DeviceDetectionServiceIf::UmountSD(uint8_t part_num,
533                                               BOOL *already_umounted) {
534   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
535   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;
536
537   FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "part_num %d", part_num);
538
539   if (NULL == m_hSession) {
540     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "m_hSession is NULL");
541     return eFrameworkunifiedStatusInvldHandle;
542   }
543
544   if (part_num > 3) {
545     return eFrameworkunifiedStatusInvldParam;
546   }
547
548   char mount_dir[256];
549   if (part_num != 0) {
550     sprintf(mount_dir, "/mnt/mmcblk1p%d", part_num);  // NOLINT (runtime/printf)
551   } else {
552     sprintf(mount_dir, "/mnt/mmcblk1");  // NOLINT (runtime/printf)
553   }
554
555   if (isMounttedDir(mount_dir)) {
556     *already_umounted = FALSE;
557   } else {
558     *already_umounted = TRUE;
559     return eFrameworkunifiedStatusOK;
560   }
561
562   char filename[256];
563   struct stat st;
564
565   if (part_num != 0) {
566     sprintf(filename, "/dev/mmcblk1p%d", part_num);  // NOLINT (runtime/printf)
567   } else {
568     sprintf(filename, "/dev/mmcblk1");  // NOLINT (runtime/printf)
569   }
570   if (stat(filename, &st) != 0) {
571     return eFrameworkunifiedStatusAccessError;
572   }
573
574   if (eFrameworkunifiedStatusOK
575       == (eStatus = FrameworkunifiedSendMsg(m_hSession, SS_DEV_UMOUNT_SD, sizeof(uint8_t),
576                                &part_num))) {
577     FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Send UmountSD msg Sucessfully");
578   }
579
580   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
581
582   return eStatus;
583 }
584
585 static const uint8_t VBUS_RESET_INTERVAL_MIN = 11;
586
587 static EFrameworkunifiedStatus SendUsbVBusResetMsg(HANDLE handle, int8_t port, uint8_t interval) {
588   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __func__, "+");
589   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
590
591   if (NULL == handle) {
592     FRAMEWORKUNIFIEDLOG(ZONE_WARN, __func__, "Invalid Handle.");
593     return eFrameworkunifiedStatusInvldHandle;
594   }
595
596   int16_t data[] = {port, interval};
597   eStatus = FrameworkunifiedSendMsg(handle, SS_USB_VBUS_RESET, sizeof(data), data);
598
599   if (eFrameworkunifiedStatusOK != eStatus) {
600     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedSendMsg Send Error[0x%x]", eStatus);
601     return eStatus;
602   }
603   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __func__, "-");
604   return eStatus;
605 }
606
607 EFrameworkunifiedStatus DeviceDetectionServiceIf::UsbVBusReset(int8_t port, uint8_t interval) {
608   return SendUsbVBusResetMsg(m_hSession, port, interval);
609 }
610
611 EFrameworkunifiedStatus DeviceDetectionServiceIf::UsbVBusReset(int8_t port) {
612   return SendUsbVBusResetMsg(m_hSession, port, VBUS_RESET_INTERVAL_MIN);
613 }
614
615 EFrameworkunifiedStatus DeviceDetectionServiceIf::RoleSwStateNotify(
616     SS_DEV_ROLE_SW_STATE state) {
617   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
618   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
619   EFrameworkunifiedStatus result = eFrameworkunifiedStatusFail;
620   UI_32 rcvlength = 0;
621   UI_32 bufflength;
622
623   if (NULL == m_hSession) {
624     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "No Session");
625     return eFrameworkunifiedStatusInvldHandle;
626   }
627
628   bufflength = sizeof(EFrameworkunifiedStatus);
629   eStatus = FrameworkunifiedInvokeSync(m_hSession, SS_DEV_ROLE_SW_STATE_NOTIFY,
630                           sizeof(SS_DEV_ROLE_SW_STATE), &state, bufflength,
631                           &result, &rcvlength);
632
633   if (eFrameworkunifiedStatusOK != eStatus) {
634     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "FrameworkunifiedSendMsg Send Error[0x%x]", eStatus);
635     return eStatus;
636   }
637
638   if (bufflength != rcvlength) {
639     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
640            "FrameworkunifiedInvokeSync Response Error. bufflen[%d], rcvlen[%d]", bufflength,
641            rcvlength);
642     return eStatus;
643   }
644
645   eStatus = result;
646
647   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
648   return eStatus;
649 }
650
651 EFrameworkunifiedStatus DeviceDetectionServiceIf::FormatSD(void) {
652   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
653   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
654
655   if (NULL == m_hSession) {
656     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Invalid Handle.");
657     return eFrameworkunifiedStatusInvldHandle;
658   }
659
660   if (access("/dev/mmcblk1", F_OK) != 0) {
661     return eFrameworkunifiedStatusAccessError;
662   }
663
664   eStatus = FrameworkunifiedSendMsg(m_hSession, SS_DEV_FORMAT_SD, 0, NULL);
665
666   if (eFrameworkunifiedStatusOK != eStatus) {
667     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "FrameworkunifiedSendMsg Send Error[0x%x]", eStatus);
668     return eStatus;
669   }
670   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
671   return eStatus;
672 }  // LCOV_EXCL_BR_LINE 10:Because the last line
673