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://floralicense.org/license/
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_SerialPortImpl.cpp
20 * @brief This is the implementation file for _SerialPortImpl class.
23 #include <unique_ptr.h>
25 #include <FBaseErrors.h>
26 #include <FBaseSysLog.h>
27 #include <FBaseRtIEventArg.h>
29 #include <FIo_AppServiceIpcMessages.h>
30 #include <FIo_IpcClient.h>
31 #include "FIo_SerialPortImpl.h"
32 #include "FSys_CommunicationDispatcherClient.h"
35 using namespace Tizen::App;
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Runtime;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::System;
41 namespace Tizen { namespace Io
44 const wchar_t COMMUNICATION_DISPATCHER_IPC_ID[] = L"osp.app.ipcserver.communicationdispatcher";
45 const wchar_t ACCESSORY_MANAGER_SERVICE_ID[] = L"osp.accessorymanager.service";
46 const wchar_t SERIAL_COMMAND_OPEN[] = L"Open";
47 const wchar_t SERIAL_COMMAND_CLOSE[] = L"Close";
48 const wchar_t SERIAL_COMMAND_WRITE[] = L"Write";
49 const wchar_t SERIAL_COMMAND_DATA[] = L"Data";
50 const wchar_t SERIAL_COMMAND_ERROR[] = L"Error";
52 const int SERIAL_BUFFER_SIZE = 512 * 1024; //512KB
53 const int SERIAL_DATA_HEAD = 1;
54 const int SERIAL_DATA_BODY = 2;
56 class _SerialPortEventArg
60 _SerialPortEventArg(ByteBuffer* pBuffer)
64 ByteBuffer* __pBuffer;
67 class _SerialPortEvent
71 virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
73 ISerialPortEventListener* pListener = dynamic_cast<ISerialPortEventListener*> (&listener);
74 if (pListener != null)
76 const _SerialPortEventArg* pArg = dynamic_cast<const _SerialPortEventArg*>(&arg);
79 if (pArg->__pBuffer == null)
81 pListener->OnSerialPortErrorOccured(E_SYSTEM);
85 SysLog(NID_IO, "Forward byte data to application.");
86 ByteBuffer* pBuffer = pArg->__pBuffer;
87 pListener->OnSerialPortDataReceivedN(*pBuffer);
92 SysLog(NID_IO, "data is forwarded to application.");
96 _SerialPortImpl* _SerialPortImpl::__pSerialPortImpl = null;
99 _SerialPortImpl::GetInstance(void)
101 SysLog(NID_IO, "Requires SerialPort instance");
102 if(__pSerialPortImpl == null)
104 __pSerialPortImpl = new (std::nothrow) _SerialPortImpl();
106 return __pSerialPortImpl;
109 _SerialPortImpl::_SerialPortImpl()
114 SysLog(NID_IO, "Initialize SerialPort");
115 result r = E_SUCCESS;
116 _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
117 SysTryCatch(NID_IO, pCommunicationDispatcherClient != null, r = E_SYSTEM, r, "It is failed to get CommunicationDispatcherClient.");
119 r = pCommunicationDispatcherClient->RegisterCommunicationListener(ACCESSORY_MANAGER_SERVICE_ID, *this);
120 SysTryCatch(NID_IO, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to register on CommunicationDispatcherClient.");
122 __pIpcClient = pCommunicationDispatcherClient->GetIpcClient();
123 SysTryCatch(NID_IO, __pIpcClient != null, r = E_SYSTEM, r, "It is failed to get IpcClient from CommunicationDispatcherClient.");
125 __pEvent = new (std::nothrow) _SerialPortEvent();
126 SysTryCatch(NID_IO, __pEvent != null, r = E_OUT_OF_MEMORY, r, "[E_OUT_OF_MEMORY] The memory is insufficient.");
132 _SerialPortImpl::~_SerialPortImpl(void)
141 _SerialPortImpl::SerialOpen(void)
143 result r = E_SUCCESS;
146 String serviceId(ACCESSORY_MANAGER_SERVICE_ID);
147 String commandId(SERIAL_COMMAND_OPEN);
148 SysLog(NID_IO, "Open SerialPort");
149 SysTryReturnResult(NID_IO, __pIpcClient != null, E_SYSTEM, "IPC is not ready.");
151 r = request.Construct();
152 r = response.Construct();
154 r = request.Add(serviceId);
155 r = request.Add(commandId);
157 unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(request, &response));
158 SysTryReturnResult(NID_IO, pMsg != NULL, r = E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
160 SysLog(NID_IO, "Try to send IPC message");
161 r = __pIpcClient->SendRequest(*pMsg);
162 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "[%s] Propagated.", GetErrorMessage(r));
164 SysLog(NID_IO, "Sent IPC message");
171 _SerialPortImpl::SerialClose(void)
173 result r = E_SUCCESS;
176 String serviceId(ACCESSORY_MANAGER_SERVICE_ID);
177 String commandId(SERIAL_COMMAND_CLOSE);
179 SysLog(NID_IO, "Close SerialPort");
180 SysTryReturnResult(NID_IO, __pIpcClient != null, E_SYSTEM, "IPC client is not ready.");
182 r = request.Construct();
183 r = request.Add(serviceId);
184 r = request.Add(commandId);
185 r = response.Construct();
187 unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(request, &response));
188 SysTryReturnResult(NID_IO, pMsg != NULL, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
190 r = __pIpcClient->SendRequest(*pMsg);
191 SysTryReturnResult(NID_IO, r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r));
199 _SerialPortImpl::IsOpended(void)
205 _SerialPortImpl::SetSerialPortEventListener(ISerialPortEventListener& listener)
207 result r = E_SUCCESS;
208 SysTryReturnResult(NID_IO, __pEvent != null, E_SYSTEM, "Event is not ready.");
209 r = __pEvent->AddListener(listener);
215 _SerialPortImpl::Write(const Tizen::Base::ByteBuffer &byteBuffer)
217 result r = E_SUCCESS;
218 ByteBuffer* pByteBuffer = null;
221 String serviceId(ACCESSORY_MANAGER_SERVICE_ID);
222 String commandId(SERIAL_COMMAND_WRITE);
224 SysTryReturnResult(NID_IO, byteBuffer.GetCapacity() < SERIAL_BUFFER_SIZE, E_MAX_EXCEEDED, "[E_MAX_EXCEEDED] The buffer size exceeded a limit of the current device.");
226 pByteBuffer = const_cast <ByteBuffer *> (&byteBuffer);
228 SysTryReturnResult(NID_IO, __pIpcClient != null, E_SYSTEM, "[E_SYSTEM] IPC client is not ready");
230 r = request.Construct();
231 r = request.Add(serviceId);
232 r = request.Add(commandId);
233 r = request.Add(*pByteBuffer);
234 r = response.Construct();
236 unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(request, &response));
237 SysTryReturnResult(NID_IO, pMsg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
239 r = __pIpcClient->SendRequest(pMsg.get());
240 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] Propagated.", GetErrorMessage(r));
246 _SerialPortImpl::GetWriteBufferSize(void) const
248 return SERIAL_BUFFER_SIZE;
252 _SerialPortImpl::OnDataReceived(const ArrayList& data)
254 ByteBuffer* pBuffer = null;
256 String* pCommand = (String*)(data.GetAt(SERIAL_DATA_HEAD));
257 SysTryReturnVoidResult(NID_APP, pCommand != null, E_SYSTEM, "[E_SYSTEM] There is no command");
259 if (pCommand->Equals(SERIAL_COMMAND_DATA, true))
261 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(pBuffer);
277 if(pEventArg != null)
279 SysLog(NID_IO, "_SerialPortImpl::OnDataReceived Event Fire");
280 __pEvent->Fire(*pEventArg);