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