2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FIo_MmcStorageManagerStub.cpp
20 * @brief This is the implementation for the _MmcStorageManagerStub class.
26 #include <FBaseSysLog.h>
29 #include <FBase_StringConverter.h>
30 #include <FIo_IpcServer.h>
31 #include <FSec_AccessController.h>
33 #include "FIo_MmcStorageManagerIpcMessages.h"
34 #include "FIo_MmcStorageManagerService.h"
35 #include "FIo_MmcStorageManagerStub.h"
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Security;
40 using namespace Tizen::System;
42 namespace Tizen { namespace Io {
44 ///////////////////////////////////////////
45 // _MmcStorageManagerStub
46 ///////////////////////////////////////////
48 _MmcStorageManagerStub::_MmcStorageManagerStub()
50 , __pMmcStorageManagerService(null)
51 , __operationInProgress(false)
53 SysLog(NID_IO, "_MmcStorageManagerStub - Enter\n");
56 _MmcStorageManagerStub::~_MmcStorageManagerStub()
58 if ( __pIpcServer != null)
64 SysLog(NID_IO, "_MmcStorageManagerStub - Exit\n");
68 _MmcStorageManagerStub::Construct(void)
70 __pMmcStorageManagerService = _MmcStorageManagerService::GetInstance();
71 SysTryReturnResult(NID_IO, __pMmcStorageManagerService != null, E_OUT_OF_MEMORY, "Not enough memory.");
74 r = __pMmcStorageManagerService->Construct(this);
75 SysTryReturn(NID_IO, !IsFailed(r), r, r, "failed to __pMmcStorageManagerService->Construct.(%s)", GetErrorMessage(r) );
78 SysTryReturn(NID_IO, !IsFailed(r), r, r, "failed to StartIpcServer.(%s)", GetErrorMessage(r) );
84 _MmcStorageManagerStub::StartIpcServer(void)
86 __pIpcServer = new (std::nothrow) _IpcServer();
87 SysTryReturnResult(NID_IO, __pIpcServer != null, E_OUT_OF_MEMORY, "Not enough memory.");
89 result r = __pIpcServer->Construct( "osp.io.ipcserver.mmcstoragemanager", *this);
90 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Failed to create IPC server(%s)", GetErrorMessage(r), "osp.io.ipcserver.mmcstoragemanager");
92 r = __pIpcServer->Start();
93 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Failed to Start IPC server(%s)", GetErrorMessage(r), "osp.io.ipcserver.mmcstoragemanager");
104 _MmcStorageManagerStub::OnMount(result* pRes)
106 SysTryReturnVoidResult(NID_IO, __pMmcStorageManagerService != null, E_INVALID_STATE, "Invalid Mmc storage manager state.");
108 *pRes = _AccessController::CheckSystemPrivilege(__pIpcServer->GetClientAppId(), _PRV_SYSTEMSETTING_READ);
109 SysTryReturnVoidResult(NID_IO, !IsFailed(*pRes), *pRes = E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
111 if (__operationInProgress == true)
113 SysLog(NID_IO, "Operation is in-progress.\n");
114 *pRes = E_SERVICE_BUSY;
120 result r = DeviceManager::GetState(DEVICE_TYPE_STORAGE_CARD, state);
121 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
123 if (state == String(L"Mounted"))
125 SysLog(NID_IO, "Already Mounted.\n");
126 *pRes = E_INVALID_OPERATION;
129 __operationInProgress = true;
131 *pRes = __pMmcStorageManagerService->Mount();
135 _MmcStorageManagerStub::OnUnmount(result* pRes)
137 SysTryReturnVoidResult(NID_IO, __pMmcStorageManagerService != null, E_INVALID_STATE, "Invalid Mmc storage manager state.");
139 *pRes = _AccessController::CheckSystemPrivilege(__pIpcServer->GetClientAppId(), _PRV_SYSTEMSETTING_READ);
140 SysTryReturnVoidResult(NID_IO, !IsFailed(*pRes), *pRes = E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
142 if (__operationInProgress == true)
144 SysLog(NID_IO, "Operation is in-progress.\n");
145 *pRes = E_SERVICE_BUSY;
151 result r = DeviceManager::GetState(DEVICE_TYPE_STORAGE_CARD, state);
152 SysTryReturnVoidResult(NID_IO, !IsFailed(r), *pRes = r, "[%s] Propagated.", GetErrorMessage(r));
154 if (state != String(L"Mounted"))
156 SysLog(NID_IO, "Already Unmounted.\n");
157 *pRes = E_INVALID_OPERATION;
160 __operationInProgress = true;
162 *pRes = __pMmcStorageManagerService->Unmount();
166 _MmcStorageManagerStub::OnFormat(result* pRes)
168 SysTryReturnVoidResult(NID_IO, __pMmcStorageManagerService != null, E_INVALID_STATE, "Invalid Mmc storage manager state.");
170 *pRes = _AccessController::CheckSystemPrivilege(__pIpcServer->GetClientAppId(), _PRV_SYSTEMSETTING_READ);
171 SysTryReturnVoidResult(NID_IO, !IsFailed(*pRes), *pRes = E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
173 if (__operationInProgress == true)
175 SysLog(NID_IO, "Operation is in-progress.\n");
176 *pRes = E_SERVICE_BUSY;
182 result r = DeviceManager::GetState(DEVICE_TYPE_STORAGE_CARD, state);
183 SysTryReturnVoidResult(NID_IO, !IsFailed(r), *pRes = r, "[%s] Propagated.", GetErrorMessage(r));
185 if (state != String(L"Unmounted"))
187 SysLog(NID_IO, "MMC is not Unmounted. Unmount it first.");
188 *pRes = E_INVALID_OPERATION;
191 __operationInProgress = true;
193 *pRes = __pMmcStorageManagerService->Format();
196 ///////////////////////////////////////////
198 ///////////////////////////////////////////
200 _MmcStorageManagerStub::OnIpcRequestReceived(_IpcServer& server, const IPC::Message& message)
202 SysLog(NID_IO, "(appId:%ls, pid:%d, clientId:%d)\n", server.GetClientAppId().GetPointer(), server.GetClientProcessId(), server.GetClientId());
204 IPC_BEGIN_MESSAGE_MAP(_MmcStorageManagerStub, message)
205 IPC_MESSAGE_HANDLER_EX(MmcStorageManager_Mount, &server, OnMount)
206 IPC_MESSAGE_HANDLER_EX(MmcStorageManager_Unmount, &server, OnUnmount)
207 IPC_MESSAGE_HANDLER_EX(MmcStorageManager_Format, &server, OnFormat)
208 IPC_END_MESSAGE_MAP()
212 _MmcStorageManagerStub::OnIpcServerStarted(const _IpcServer& server)
214 SysLog(NID_IO, "_MmcStorageManagerStub::OnIpcServerStarted \n");
218 _MmcStorageManagerStub::OnIpcServerStopped(const _IpcServer& server)
220 SysLog(NID_IO, "_MmcStorageManagerStub::OnIpcServerStopped \n");
224 _MmcStorageManagerStub::OnIpcClientConnected(const _IpcServer& server, int clientId)
226 __clientId = clientId;
227 SysLog(NID_IO, "_MmcStorageManagerStub::OnIpcClientConnected (clientId:%d)\n", clientId);
231 _MmcStorageManagerStub::OnIpcClientDisconnected(const _IpcServer&server, int clientId)
233 SysLog(NID_IO, "(appId:%ls, pid:%d, clientId:%d)\n", server.GetClientAppId().GetPointer(), server.GetClientProcessId(), clientId);
236 //Server to Client message
238 _MmcStorageManagerStub::OnMmcMountResponseReceived(result response)
240 SysLog(NID_IO, "Response msg is : [%s]\n", GetErrorMessage(response));
242 __operationInProgress = false;
244 SysTryReturnResult(NID_IO, __pIpcServer != null, E_INVALID_STATE, "__pIpcServer is null.");
246 result r = __pIpcServer->SendResponse(__clientId, new MmcStorageManager_MountReceived(response));
247 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
253 _MmcStorageManagerStub::OnMmcUnmountResponseReceived(result response)
255 SysLog(NID_IO, "Response msg is : [%s]\n", GetErrorMessage(response));
257 __operationInProgress = false;
259 SysTryReturnResult(NID_IO, __pIpcServer != null, E_INVALID_STATE, "__pIpcServer is null.");
261 result r = __pIpcServer->SendResponse(__clientId, new MmcStorageManager_UnmountReceived(response));
262 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
268 _MmcStorageManagerStub::OnMmcFormatResponseReceived(result response)
270 SysLog(NID_IO, "Response msg is : [%s]\n", GetErrorMessage(response));
272 __operationInProgress = false;
274 SysTryReturnResult(NID_IO, __pIpcServer != null, E_INVALID_STATE, "__pIpcServer is null.");
276 result r = __pIpcServer->SendResponse(__clientId, new MmcStorageManager_FormatReceived(response));
277 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
282 }} //namespace Tizen { namespace Io {