Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / systemservice / logger_service / server / src / ss_logger_device_detection.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_NS_InterfaceunifiedLogCapture
19 /// \brief    This file supports USB logging threads.
20 ///
21 ///////////////////////////////////////////////////////////////////////////////
22
23 // System Headers
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stddef.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #ifdef AGL_STUB
31 #else
32 #include <sys/usbdi.h>
33 #endif
34 #include <limits.h>
35 #include <string.h>
36 #include <native_service/ns_message_center_if.h>
37 #include <ss_logger_fs_directory.h>
38 #include <boost/bind.hpp>
39
40 // Pasa Logs
41 #include <loggerservicedebug_loggerservicelog.h>
42 #include <ss_logger_device_detection.h>
43 #include <loggerservicedebug_thread_if.h>
44 #include <native_service/frameworkunified_framework_if.h>
45 #include <system_service/ss_logger_service.h>
46 #include <system_service/ss_devicedetection_service_notifications.h>
47 #include <system_service/ss_devicedetection_service_ifc.h>
48 #include <system_service/ss_sm_client_if.h>
49 #include <string>
50 #include "ss_logger_common.h"
51
52 //
53 const SI_32 INVALID_FD = -1;
54
55 // Max command line length
56 const UI_32 MAX_SYS_CMD_LN_LENGTH = 120;
57
58 CLoggerDeviceDetection::CLoggerDeviceDetection() :
59     m_pLoggerCfg(NULL),
60     m_loggerUtil(NULL) {
61 }
62
63 CLoggerDeviceDetection::~CLoggerDeviceDetection() {  // LCOV_EXCL_START 14:global instance
64   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
65 }
66 // LCOV_EXCL_STOP
67
68 EFrameworkunifiedStatus CLoggerDeviceDetection::Initialize(HANDLE f_hApp, CLoggerCfg * f_pLoggerCfg) {
69     FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
70     EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
71     m_pLoggerCfg = f_pLoggerCfg;
72     // Device Detection Obj initialization
73     if (m_devDetect.Initialize(f_hApp)) {  // LCOV_EXCL_BR_LINE 200:To ensure success
74             if (eFrameworkunifiedStatusOK != (l_eStatus = m_devDetect.NotifyOnDeviceDetectionAvailability(
75                     boost::bind(&CLoggerDeviceDetection::DD_ServiceAvailabilityCallBack, this, _1) ))) {
76                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "ServiceAvailabilityCallback registration failed");
77             }
78
79             if (eFrameworkunifiedStatusOK != (l_eStatus = m_devDetect.NotifyOnOpenSessionAck(
80                     boost::bind(&CLoggerDeviceDetection::DD_OpenSessionAckCallBack, this, _1) ))) {
81                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "OpenSessionAckCallback registration failed");
82             }
83
84             if (eFrameworkunifiedStatusOK != (l_eStatus = m_devDetect.NotifyOnCloseSessionAck(
85                     boost::bind(&CLoggerDeviceDetection::DD_CloseSessionAckCallBack, this, _1) ))) {
86                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "CloseSessionCallback registration failed");
87             }
88     } else {
89             // LCOV_EXCL_START 200:To ensure success
90             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
91             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Device Detection Object Initialization failed!");
92             // LCOV_EXCL_STOP
93     }
94
95     FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
96     return l_eStatus;
97 }
98
99 //////////////////////////////////////////
100 //  Function : ServiceAvailabilityCallBack
101 //////////////////////////////////////////
102 EFrameworkunifiedStatus CLoggerDeviceDetection::DD_ServiceAvailabilityCallBack(HANDLE hApp) {
103   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
104   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
105
106   if (FrameworkunifiedIsServiceAvailable(hApp)) {
107     if (eFrameworkunifiedStatusOK != (eStatus = m_devDetect.OpenSessionRequest())) {
108       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Open session request failed");
109     }
110   } else {
111     if (eFrameworkunifiedStatusOK != (eStatus = m_devDetect.CloseSessionRequest())) {
112       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Close session request failed");
113     }
114   }
115
116   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
117   return eStatus;
118 }
119
120 //////////////////////////////////////////
121 //  Function : OpenSessionAckCallBack
122 //////////////////////////////////////////
123 EFrameworkunifiedStatus CLoggerDeviceDetection::DD_OpenSessionAckCallBack(HANDLE hApp) {
124   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
125   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
126
127   if (eFrameworkunifiedStatusOK == (eStatus = m_devDetect.DecodeOpenSessionResponse())) {
128     if (eFrameworkunifiedStatusOK != (eStatus = m_devDetect.RegisterForDeviceDetectionEvent(SS_DEV_DETECT_ANY_USB_EV,
129                          boost::bind(&CLoggerDeviceDetection::DD_MediaDetectCallBack, this, _1)))) {
130       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Registration for SS_DEV_DETECT_ANY_USB_EV failed");
131     }
132   } else {
133     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Decode open session response failed");
134   }
135   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
136   return eStatus;
137 }
138 //////////////////////////////////////////
139 //  Function : OpenSessionAckCallBack
140 //////////////////////////////////////////
141 EFrameworkunifiedStatus CLoggerDeviceDetection::DD_CloseSessionAckCallBack(HANDLE hApp) {  // LCOV_EXCL_START 200: can not called from devicedetection_service   // NOLINT[whitespace/line_length]
142   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
143   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
144   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
145   CloseSessionAck tCloseSessionAck;
146
147   if (hApp) {
148     if (eFrameworkunifiedStatusOK == (eStatus = FrameworkunifiedGetMsgDataOfSize(hApp, &tCloseSessionAck, sizeof (tCloseSessionAck)))) {
149       if (eFrameworkunifiedStatusOK  == tCloseSessionAck.eStatus) {
150         UI_32 l_uiSessionId = tCloseSessionAck.sessionId;
151         FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "session  %d closed successfully", l_uiSessionId);
152       }
153     }
154   }
155   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
156   return eStatus;
157 }
158 // LCOV_EXCL_STOP
159
160 EFrameworkunifiedStatus CLoggerDeviceDetection::DD_CloseSessionWithDevDetectionSrv(HANDLE hApp) {  // LCOV_EXCL_START 200: can not called from devicedetection_service   // NOLINT[whitespace/line_length]
161   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
162   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
163   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
164
165   if (eFrameworkunifiedStatusOK == (eStatus = m_devDetect.
166       NotifyOnCloseSessionAck(boost::bind(&CLoggerDeviceDetection::DD_CloseSessionAckCallBack, this, _1)))) {
167     if (eFrameworkunifiedStatusOK != (eStatus = m_devDetect.CloseSessionRequest())) {
168       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to send CloseSessionRequest");
169     }
170   } else {
171     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to register closeSessionAckCallback");
172   }
173
174   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
175   return eStatus;
176 }
177 // LCOV_EXCL_STOP
178
179 EFrameworkunifiedStatus CLoggerDeviceDetection::DD_MediaDetectCallBack(HANDLE hApp) {  // LCOV_EXCL_START 200: can not called from devicedetection_service   // NOLINT[whitespace/line_length]
180   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
181   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
182   EFrameworkunifiedStatus         l_eStatus = eFrameworkunifiedStatusOK;
183   std::string        l_devstr;
184     std::string        l_filePathStr;
185     SS_MediaDetectInfo l_tMediaDetectInfo;
186
187
188   if (eFrameworkunifiedStatusOK != (l_eStatus = FrameworkunifiedGetMsgDataOfSize(hApp, &l_tMediaDetectInfo, sizeof (l_tMediaDetectInfo)))) {
189     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __PRETTY_FUNCTION__, "FrameworkunifiedGetMsgDataOfSize Failed Status:0x%x ", l_eStatus);
190   } else {
191         FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, " Device = %d",       l_tMediaDetectInfo.dev_type);
192         FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, " Availability = %d", l_tMediaDetectInfo.bIsDeviceAvailable);
193         FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, " device path = %s",  l_tMediaDetectInfo.deviceMountpath);
194         FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, " File path = %s",    l_tMediaDetectInfo.filepathName);
195
196         if (l_tMediaDetectInfo.dev_type == eUSB) {
197       if (l_tMediaDetectInfo.bIsDeviceAvailable == TRUE && m_pLoggerCfg != NULL) {
198         std::string l_usbMountPath;
199         l_usbMountPath = l_tMediaDetectInfo.deviceMountpath;
200         m_pLoggerCfg->setUsb0MountPath(l_usbMountPath);
201         m_loggerUtil->SetUsbMountPath(l_usbMountPath);
202         }
203     }
204   }
205
206   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
207   return (l_eStatus);
208 }
209 // LCOV_EXCL_STOP