Tizen 2.1 base
[platform/framework/native/app-service.git] / src / FIo_MmcStorageManagerStub.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file         FIo_MmcStorageManagerStub.cpp
20  * @brief       This is the implementation for the _MmcStorageManagerStub class.
21  */
22
23 #include <cstdio>
24 #include <dlfcn.h>
25
26 #include <FBaseSysLog.h>
27 #include <FSystem.h>
28
29 #include <FBase_StringConverter.h>
30 #include <FIo_IpcServer.h>
31 #include <FSec_AccessController.h>
32
33 #include "FIo_MmcStorageManagerIpcMessages.h"
34 #include "FIo_MmcStorageManagerService.h"
35 #include "FIo_MmcStorageManagerStub.h"
36
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Security;
40 using namespace Tizen::System;
41
42 namespace Tizen { namespace Io {
43
44 ///////////////////////////////////////////
45 // _MmcStorageManagerStub
46 ///////////////////////////////////////////
47
48 _MmcStorageManagerStub::_MmcStorageManagerStub()
49         : __pIpcServer(null)
50         , __pMmcStorageManagerService(null)
51         , __operationInProgress(false)
52 {
53         SysLog(NID_IO, "_MmcStorageManagerStub - Enter\n");
54 }
55
56 _MmcStorageManagerStub::~_MmcStorageManagerStub()
57 {
58         if ( __pIpcServer != null)
59         {
60                 __pIpcServer->Stop();
61                 delete __pIpcServer;
62         }
63
64         SysLog(NID_IO, "_MmcStorageManagerStub - Exit\n");
65 }
66
67 result
68 _MmcStorageManagerStub::Construct(void)
69 {
70         __pMmcStorageManagerService = _MmcStorageManagerService::GetInstance();
71         SysTryReturnResult(NID_IO, __pMmcStorageManagerService != null, E_OUT_OF_MEMORY, "Not enough memory.");
72
73         result r = E_SUCCESS;
74         r = __pMmcStorageManagerService->Construct(this);
75         SysTryReturn(NID_IO, !IsFailed(r), r, r, "failed to __pMmcStorageManagerService->Construct.(%s)", GetErrorMessage(r) );
76
77         r = StartIpcServer();
78         SysTryReturn(NID_IO, !IsFailed(r), r, r, "failed to StartIpcServer.(%s)", GetErrorMessage(r) );
79
80         return E_SUCCESS;
81 }
82
83 result
84 _MmcStorageManagerStub::StartIpcServer(void)
85 {
86         __pIpcServer = new (std::nothrow) _IpcServer();
87         SysTryReturnResult(NID_IO, __pIpcServer != null, E_OUT_OF_MEMORY, "Not enough memory.");
88
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");
91
92         r = __pIpcServer->Start();
93         SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Failed to Start IPC server(%s)", GetErrorMessage(r), "osp.io.ipcserver.mmcstoragemanager");
94
95         return E_SUCCESS;
96
97 CATCH:
98         delete __pIpcServer;
99         __pIpcServer = null;
100         return r;
101 }
102
103 void
104 _MmcStorageManagerStub::OnMount(result* pRes)
105 {
106         SysTryReturnVoidResult(NID_IO, __pMmcStorageManagerService != null, E_INVALID_STATE, "Invalid Mmc storage manager state.");
107
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.");
110
111         if (__operationInProgress == true)
112         {
113                 SysLog(NID_IO, "Operation is in-progress.\n");
114                 *pRes = E_SERVICE_BUSY;
115                 return;
116         }
117
118         String state;
119
120         result r = DeviceManager::GetState(DEVICE_TYPE_STORAGE_CARD, state);
121         SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
122
123         if (state == String(L"Mounted"))
124         {
125                 SysLog(NID_IO, "Already Mounted.\n");
126                 *pRes = E_INVALID_OPERATION;
127                 return;
128         }
129         __operationInProgress = true;
130
131         *pRes = __pMmcStorageManagerService->Mount();
132 }
133
134 void
135 _MmcStorageManagerStub::OnUnmount(result* pRes)
136 {
137         SysTryReturnVoidResult(NID_IO, __pMmcStorageManagerService != null, E_INVALID_STATE, "Invalid Mmc storage manager state.");
138
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.");
141
142         if (__operationInProgress == true)
143         {
144                 SysLog(NID_IO, "Operation is in-progress.\n");
145                 *pRes = E_SERVICE_BUSY;
146                 return;
147         }
148         
149         String state;
150
151         result r = DeviceManager::GetState(DEVICE_TYPE_STORAGE_CARD, state);
152         SysTryReturnVoidResult(NID_IO, !IsFailed(r), *pRes = r, "[%s] Propagated.", GetErrorMessage(r));
153
154         if (state != String(L"Mounted"))
155         {
156                 SysLog(NID_IO, "Already Unmounted.\n");
157                 *pRes = E_INVALID_OPERATION;
158                 return;
159         }
160         __operationInProgress = true;
161
162         *pRes = __pMmcStorageManagerService->Unmount();
163 }
164
165 void
166 _MmcStorageManagerStub::OnFormat(result* pRes)
167 {
168         SysTryReturnVoidResult(NID_IO, __pMmcStorageManagerService != null, E_INVALID_STATE, "Invalid Mmc storage manager state.");
169
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.");
172
173         if (__operationInProgress == true)
174         {
175                 SysLog(NID_IO, "Operation is in-progress.\n");
176                 *pRes = E_SERVICE_BUSY;
177                 return;
178         }
179
180         String state;
181
182         result r = DeviceManager::GetState(DEVICE_TYPE_STORAGE_CARD, state);
183         SysTryReturnVoidResult(NID_IO, !IsFailed(r), *pRes = r, "[%s] Propagated.", GetErrorMessage(r));
184
185         if (state != String(L"Unmounted"))
186         {
187                 SysLog(NID_IO, "MMC is not Unmounted. Unmount it first.");
188                 *pRes = E_INVALID_OPERATION;
189                 return;
190         }
191         __operationInProgress = true;
192
193         *pRes = __pMmcStorageManagerService->Format();
194 }
195
196 ///////////////////////////////////////////
197 // ipc handlers
198 ///////////////////////////////////////////
199 void
200 _MmcStorageManagerStub::OnIpcRequestReceived(_IpcServer& server, const IPC::Message& message)
201 {
202         SysLog(NID_IO, "(appId:%ls, pid:%d, clientId:%d)\n", server.GetClientAppId().GetPointer(), server.GetClientProcessId(), server.GetClientId());
203
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()
209 }
210
211 void
212 _MmcStorageManagerStub::OnIpcServerStarted(const _IpcServer& server)
213 {
214         SysLog(NID_IO, "_MmcStorageManagerStub::OnIpcServerStarted \n");
215 }
216
217 void
218 _MmcStorageManagerStub::OnIpcServerStopped(const _IpcServer& server)
219 {
220         SysLog(NID_IO, "_MmcStorageManagerStub::OnIpcServerStopped \n");
221 }
222
223 void
224 _MmcStorageManagerStub::OnIpcClientConnected(const _IpcServer& server, int clientId)
225 {
226         __clientId = clientId;
227         SysLog(NID_IO, "_MmcStorageManagerStub::OnIpcClientConnected (clientId:%d)\n", clientId);
228 }
229
230 void
231 _MmcStorageManagerStub::OnIpcClientDisconnected(const _IpcServer&server, int clientId)
232 {
233         SysLog(NID_IO, "(appId:%ls, pid:%d, clientId:%d)\n", server.GetClientAppId().GetPointer(), server.GetClientProcessId(), clientId);
234 }
235
236 //Server to Client message
237 result
238 _MmcStorageManagerStub::OnMmcMountResponseReceived(result response)
239 {
240         SysLog(NID_IO, "Response msg is : [%s]\n", GetErrorMessage(response));
241
242         __operationInProgress = false;
243
244         SysTryReturnResult(NID_IO, __pIpcServer != null, E_INVALID_STATE, "__pIpcServer is null.");
245
246         result r = __pIpcServer->SendResponse(__clientId, new MmcStorageManager_MountReceived(response));
247         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
248
249         return r;
250 }
251
252 result
253 _MmcStorageManagerStub::OnMmcUnmountResponseReceived(result response)
254 {
255         SysLog(NID_IO, "Response msg is : [%s]\n", GetErrorMessage(response));
256
257         __operationInProgress = false;
258
259         SysTryReturnResult(NID_IO, __pIpcServer != null, E_INVALID_STATE, "__pIpcServer is null.");
260
261         result r = __pIpcServer->SendResponse(__clientId, new MmcStorageManager_UnmountReceived(response));
262         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
263
264         return r;
265 }
266
267 result
268 _MmcStorageManagerStub::OnMmcFormatResponseReceived(result response)
269 {
270         SysLog(NID_IO, "Response msg is : [%s]\n", GetErrorMessage(response));
271
272         __operationInProgress = false;
273
274         SysTryReturnResult(NID_IO, __pIpcServer != null, E_INVALID_STATE, "__pIpcServer is null.");
275
276         result r = __pIpcServer->SendResponse(__clientId, new MmcStorageManager_FormatReceived(response));
277         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
278
279         return r;
280 }
281
282 }} //namespace Tizen { namespace Io {