2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
18 * @file FIo_SerialPortImpl.cpp
19 * @brief This is the implementation file for _SerialPortImpl class.
22 #include <unique_ptr.h>
24 #include <FBaseErrors.h>
25 #include <FBaseSysLog.h>
26 #include <FBaseRtIEventArg.h>
28 #include <FIo_AppServiceIpcMessages.h>
29 #include <FIo_IpcClient.h>
30 #include "FIo_SerialPortImpl.h"
31 #include "FSys_CommunicationDispatcherClient.h"
34 using namespace Tizen::App;
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Runtime;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::System;
40 namespace Tizen { namespace Io
43 const wchar_t COMMUNICATION_DISPATCHER_IPC_ID[] = L"osp.app.ipcserver.communicationdispatcher";
44 const wchar_t ACCESSORY_MANAGER_SERVICE_ID[] = L"osp.accessorymanager.service";
45 const wchar_t SERIAL_COMMAND_OPEN[] = L"Open";
46 const wchar_t SERIAL_COMMAND_CLOSE[] = L"Close";
47 const wchar_t SERIAL_COMMAND_WRITE[] = L"Write";
48 const wchar_t SERIAL_COMMAND_DATA[] = L"Data";
49 const wchar_t SERIAL_COMMAND_ERROR[] = L"Error";
51 const int SERIAL_BUFFER_SIZE = 512 * 1024; //512KB
52 const int SERIAL_DATA_HEAD = 1;
53 const int SERIAL_DATA_BODY = 2;
55 class _SerialPortEventArg
59 _SerialPortEventArg(ByteBuffer* pBuffer)
63 ByteBuffer* __pBuffer;
66 class _SerialPortEvent
70 virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
72 ISerialPortEventListener* pListener = dynamic_cast<ISerialPortEventListener*> (&listener);
73 if (pListener != null)
75 const _SerialPortEventArg* pArg = dynamic_cast<const _SerialPortEventArg*>(&arg);
78 if (pArg->__pBuffer == null)
80 pListener->OnSerialPortErrorOccured(E_SYSTEM);
84 SysLog(NID_IO, "Forward byte data to application.");
85 ByteBuffer* pBuffer = pArg->__pBuffer;
86 pListener->OnSerialPortDataReceivedN(*pBuffer);
91 SysLog(NID_IO, "data is forwarded to application.");
95 _SerialPortImpl* _SerialPortImpl::__pSerialPortImpl = null;
98 _SerialPortImpl::GetInstance(void)
100 SysLog(NID_IO, "Requires SerialPort instance");
101 if(__pSerialPortImpl == null)
103 __pSerialPortImpl = new (std::nothrow) _SerialPortImpl();
105 return __pSerialPortImpl;
108 _SerialPortImpl::_SerialPortImpl()
113 SysLog(NID_IO, "Initialize SerialPort");
114 result r = E_SUCCESS;
115 _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
116 SysTryCatch(NID_IO, pCommunicationDispatcherClient != null, r = E_SYSTEM, r, "It is failed to get CommunicationDispatcherClient.");
118 r = pCommunicationDispatcherClient->RegisterCommunicationListener(ACCESSORY_MANAGER_SERVICE_ID, *this);
119 SysTryCatch(NID_IO, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to register on CommunicationDispatcherClient.");
121 __pIpcClient = pCommunicationDispatcherClient->GetIpcClient();
122 SysTryCatch(NID_IO, __pIpcClient != null, r = E_SYSTEM, r, "It is failed to get IpcClient from CommunicationDispatcherClient.");
124 __pEvent = new (std::nothrow) _SerialPortEvent();
125 SysTryCatch(NID_IO, __pEvent != null, r = E_OUT_OF_MEMORY, r, "[E_OUT_OF_MEMORY] The memory is insufficient.");
131 _SerialPortImpl::~_SerialPortImpl(void)
140 _SerialPortImpl::SerialOpen(void)
142 result r = E_SUCCESS;
145 String serviceId(ACCESSORY_MANAGER_SERVICE_ID);
146 String commandId(SERIAL_COMMAND_OPEN);
147 SysLog(NID_IO, "Open SerialPort");
148 SysTryReturnResult(NID_IO, __pIpcClient != null, E_SYSTEM, "IPC is not ready.");
150 r = request.Construct();
151 r = response.Construct();
153 r = request.Add(serviceId);
154 r = request.Add(commandId);
156 unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(request, &response));
157 SysTryReturnResult(NID_IO, pMsg != NULL, r = E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
159 SysLog(NID_IO, "Try to send IPC message");
160 r = __pIpcClient->SendRequest(*pMsg);
161 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "[%s] Propagated.", GetErrorMessage(r));
163 SysLog(NID_IO, "Sent IPC message");
170 _SerialPortImpl::SerialClose(void)
172 result r = E_SUCCESS;
175 String serviceId(ACCESSORY_MANAGER_SERVICE_ID);
176 String commandId(SERIAL_COMMAND_CLOSE);
178 SysLog(NID_IO, "Close SerialPort");
179 SysTryReturnResult(NID_IO, __pIpcClient != null, E_SYSTEM, "IPC client is not ready.");
181 r = request.Construct();
182 r = request.Add(serviceId);
183 r = request.Add(commandId);
184 r = response.Construct();
186 unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(request, &response));
187 SysTryReturnResult(NID_IO, pMsg != NULL, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
189 r = __pIpcClient->SendRequest(*pMsg);
190 SysTryReturnResult(NID_IO, r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r));
198 _SerialPortImpl::IsOpended(void)
204 _SerialPortImpl::SetSerialPortEventListener(ISerialPortEventListener& listener)
206 result r = E_SUCCESS;
207 SysTryReturnResult(NID_IO, __pEvent != null, E_SYSTEM, "Event is not ready.");
208 r = __pEvent->AddListener(listener);
214 _SerialPortImpl::Write(const Tizen::Base::ByteBuffer &byteBuffer)
216 result r = E_SUCCESS;
217 ByteBuffer* pByteBuffer = null;
220 String serviceId(ACCESSORY_MANAGER_SERVICE_ID);
221 String commandId(SERIAL_COMMAND_WRITE);
223 SysTryReturnResult(NID_IO, byteBuffer.GetCapacity() < SERIAL_BUFFER_SIZE, E_MAX_EXCEEDED, "[E_MAX_EXCEEDED] The buffer size exceeded a limit of the current device.");
225 pByteBuffer = const_cast <ByteBuffer *> (&byteBuffer);
227 SysTryReturnResult(NID_IO, __pIpcClient != null, E_SYSTEM, "[E_SYSTEM] IPC client is not ready");
229 r = request.Construct();
230 r = request.Add(serviceId);
231 r = request.Add(commandId);
232 r = request.Add(*pByteBuffer);
233 r = response.Construct();
235 unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(request, &response));
236 SysTryReturnResult(NID_IO, pMsg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
238 r = __pIpcClient->SendRequest(pMsg.get());
239 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] Propagated.", GetErrorMessage(r));
245 _SerialPortImpl::GetWriteBufferSize(void) const
247 return SERIAL_BUFFER_SIZE;
251 _SerialPortImpl::OnDataReceived(const ArrayList& data)
253 ByteBuffer* pBuffer = null;
255 String* pCommand = (String*)(data.GetAt(SERIAL_DATA_HEAD));
256 SysTryReturnVoidResult(NID_APP, pCommand != null, E_SYSTEM, "[E_SYSTEM] There is no command");
258 if (pCommand->Equals(SERIAL_COMMAND_DATA, true))
260 pBuffer = (ByteBuffer*)data.GetAt(SERIAL_DATA_BODY);
264 ArrayList* pData = const_cast<ArrayList*>(&data);
265 pData->Remove(*pBuffer);
266 _SerialPortEventArg* pEventArg= new (std::nothrow) _SerialPortEventArg(pBuffer);
267 if(pEventArg != null)
269 SysLog(NID_IO, "_SerialPortImpl::OnDataReceived Event Fire");
270 __pEvent->Fire(*pEventArg);
274 else if (pCommand->Equals(SERIAL_COMMAND_ERROR, true))
276 _SerialPortEventArg* pEventArg= new (std::nothrow) _SerialPortEventArg(null);
277 if(pEventArg != null)
279 SysLog(NID_IO, "_SerialPortImpl::OnDataReceived Event Fire");
280 __pEvent->Fire(*pEventArg);