Merge "fixed the description" into tizen_2.1
[platform/framework/native/appfw.git] / src / io / FIoSerialPort.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://floralicense.org/license/
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        FIoSerialPort.cpp
20  * @brief       This is the implementation file for SerialPort class.
21  */
22
23 #include <serial.h>
24
25 #include <FBaseSysLog.h>
26 #include <FIoSerialPort.h>
27 #include <FIoISerialPortEventListener.h>
28
29 #include <FBase_NativeError.h>
30
31 #include "FIo_SerialPortImpl.h"
32
33 using namespace Tizen::Base;
34
35 namespace Tizen { namespace Io
36 {
37
38 SerialPort::SerialPort(void)
39         : __pSerialPortImpl(null)
40 {
41 }
42
43 SerialPort::~SerialPort(void)
44 {
45         result r = E_SUCCESS;
46         if(__pSerialPortImpl != null)
47         {
48                 if(__pSerialPortImpl->IsOpended() == true)
49                 {
50                         r = __pSerialPortImpl->SerialClose();
51                 }
52                 else
53                 {
54                         SysLogException(NID_SYS, E_SYSTEM, "SerialPort is not open.");
55                 }
56         }
57         else
58         {
59                 SysLogException(NID_SYS, E_SYSTEM, "SerialPort instance is not ready.");
60         }
61 }
62
63 result
64 SerialPort::Construct(ISerialPortEventListener& listener)
65 {
66         result r = E_SUCCESS;
67         SysLog(NID_IO, "SerialPort Construct");
68
69         __pSerialPortImpl = _SerialPortImpl::GetInstance();
70         SysTryReturnResult(NID_IO, __pSerialPortImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
71         SysTryCatch(NID_IO, __pSerialPortImpl->IsOpended() == false, r = E_SYSTEM, r, "SerialPort is already opened.");
72
73         SysLog(NID_IO, "Try to open serialport");
74         r = __pSerialPortImpl->SerialOpen();
75         SysTryCatch(NID_IO, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to open serial port.");
76
77         SysLog(NID_IO, "Try to register event");
78         r = __pSerialPortImpl->SetSerialPortEventListener(listener);
79         if(r != E_SUCCESS)
80         {
81                 SysLogException(NID_IO, E_SYSTEM, "It is failed to register event listener.");
82                 r = __pSerialPortImpl->SerialClose();
83                 SysTryCatch(NID_IO, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to close serial port.");
84
85                 r = E_SYSTEM;
86         }
87 CATCH:
88         if(r != E_SUCCESS)
89         {
90                 __pSerialPortImpl = null;
91         }
92
93         return r;
94 }
95
96 result
97 SerialPort::Write(const ByteBuffer& byteBuffer)
98 {
99         SysAssertf(__pSerialPortImpl != null, "Not yet constructed. Construct() should be called before use.\n");
100         SysTryReturnResult(NID_IO, __pSerialPortImpl->IsOpended() == true, E_SYSTEM, "SerialPort is not open.");
101
102         return __pSerialPortImpl->Write(byteBuffer);
103 }
104
105 int
106 SerialPort::GetWriteBufferSize(void) const
107 {
108         SysAssertf(__pSerialPortImpl != null, "Not yet constructed. Construct() should be called before use.\n");
109         return __pSerialPortImpl->GetWriteBufferSize();
110 }
111
112 } } // Tizen::Io