Merge "added check for listener validation" into tizen_2.2
[platform/framework/native/appfw.git] / src / io / FIoSerialPort.cpp
index e818997..49bcf71 100644 (file)
@@ -1,5 +1,4 @@
 //
-// Open Service Platform
 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
 //
 // Licensed under the Apache License, Version 2.0 (the License);
@@ -42,23 +41,62 @@ SerialPort::SerialPort(void)
 
 SerialPort::~SerialPort(void)
 {
+       result r = E_SUCCESS;
+       if(__pSerialPortImpl != null)
+       {
+               if(__pSerialPortImpl->IsOpended() == true)
+               {
+                       r = __pSerialPortImpl->SerialClose();
+               }
+               else
+               {
+                       SysLogException(NID_SYS, E_SYSTEM, "SerialPort is not open.");
+               }
+       }
+       else
+       {
+               SysLogException(NID_SYS, E_SYSTEM, "SerialPort instance is not ready.");
+       }
 }
 
 result
 SerialPort::Construct(ISerialPortEventListener& listener)
 {
-       SysAssertf(__pSerialPortImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class\n");
+       result r = E_SUCCESS;
+       SysLog(NID_IO, "SerialPort Construct");
 
-       __pSerialPortImpl = new (std::nothrow) _SerialPortImpl(listener);
+       __pSerialPortImpl = _SerialPortImpl::GetInstance();
        SysTryReturnResult(NID_IO, __pSerialPortImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
-
-       return E_SUCCESS;
+       SysTryCatch(NID_IO, __pSerialPortImpl->IsOpended() == false, r = E_SYSTEM, r, "SerialPort is already opened.");
+
+       SysLog(NID_IO, "Try to open serialport");
+       r = __pSerialPortImpl->SerialOpen();
+       SysTryCatch(NID_IO, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to open serial port.");
+
+       SysLog(NID_IO, "Try to register event");
+       r = __pSerialPortImpl->SetSerialPortEventListener(listener);
+       if(r != E_SUCCESS)
+       {
+               SysLogException(NID_IO, E_SYSTEM, "It is failed to register event listener.");
+               r = __pSerialPortImpl->SerialClose();
+               SysTryCatch(NID_IO, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to close serial port.");
+
+               r = E_SYSTEM;
+       }
+CATCH:
+       if(r != E_SUCCESS)
+       {
+               __pSerialPortImpl = null;
+       }
+
+       return r;
 }
 
 result
 SerialPort::Write(const ByteBuffer& byteBuffer)
 {
        SysAssertf(__pSerialPortImpl != null, "Not yet constructed. Construct() should be called before use.\n");
+       SysTryReturnResult(NID_IO, __pSerialPortImpl->IsOpended() == true, E_SYSTEM, "SerialPort is not open.");
 
        return __pSerialPortImpl->Write(byteBuffer);
 }
@@ -67,7 +105,6 @@ int
 SerialPort::GetWriteBufferSize(void) const
 {
        SysAssertf(__pSerialPortImpl != null, "Not yet constructed. Construct() should be called before use.\n");
-
        return __pSerialPortImpl->GetWriteBufferSize();
 }