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://www.apache.org/licenses/LICENSE-2.0
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_DataRouter.cpp
19 * @brief This is the implementation file for %_DataRouter class.
25 #include <FBaseSysLog.h>
26 #include <FIo_DataRouter.h>
27 #include <FIo_IDataRouterEventListener.h>
29 using namespace Tizen::Base;
31 namespace Tizen { namespace Io
34 const char* _VCONF_KEY_DATA_ROUTER_READY_EVENT = "memory/data_router/osp_serial_open";
36 static serial_h gSerialHandle = null;
37 static _DataRouter* gp_DataRouter = null;
38 static int __refCount = 0;
41 SerialStateChangedCallback(serial_error_e result, serial_state_e state, void* userData)
43 SysLog(NID_IO, "Enter");
45 if (gp_DataRouter != null)
47 if (state == SERIAL_STATE_OPENED)
49 gp_DataRouter->StateChanged(_DATA_ROUTER_STATE_OPENED);
51 else if (state == SERIAL_STATE_CLOSED)
53 gp_DataRouter->StateChanged(_DATA_ROUTER_STATE_CLOSED);
54 ret = serial_unset_state_changed_cb(gSerialHandle);
55 ret = serial_unset_data_received_cb(gSerialHandle);
56 ret = serial_destroy(gSerialHandle);
60 SysLogException(NID_IO, E_SYSTEM, "Undefined state is required.");
66 SerialDataReceivedCallback(const char* data, int length, void* userData)
68 SysLog(NID_IO, "Enter");
69 if (gp_DataRouter != null)
71 gp_DataRouter->DataReceived(data, length);
78 SerialStateReadyCallback(keynode_t* node, void* userData)
80 SysLog(NID_IO, "Enter");
84 if (strcmp(_VCONF_KEY_DATA_ROUTER_READY_EVENT, vconf_keynode_get_name(node)) == 0)
86 int dataRouterStatus = 0;
87 ret = vconf_get_int(_VCONF_KEY_DATA_ROUTER_READY_EVENT, &dataRouterStatus);
88 SysTryCatch(NID_IO, ret == 0, r= E_SYSTEM, r, "[%s] Failed to get vconf status");
90 if(dataRouterStatus == 1)
92 SysLog(NID_IO, "Try to initialize serial port");
93 ret = serial_create(&gSerialHandle);
94 SysTryCatch(NID_IO, ret == SERIAL_ERROR_NONE, r = E_SYSTEM, r, "[%s] Failed to initialize data router module. Error:%d", ret);
96 ret = serial_set_state_changed_cb(gSerialHandle, SerialStateChangedCallback, null);
97 SysTryCatch(NID_IO, ret == SERIAL_ERROR_NONE, r = E_SYSTEM, r, "[%s] Failed to set callback function for state change", GetErrorMessage(r));
99 ret = serial_set_data_received_cb(gSerialHandle, SerialDataReceivedCallback, null);
100 SysTryCatch(NID_IO, ret == SERIAL_ERROR_NONE, r = E_SYSTEM, r, "[%s] Failed to set callback function for data receive", GetErrorMessage(r));
102 ret = serial_open(gSerialHandle);
103 SysTryCatch(NID_IO, ret == SERIAL_ERROR_NONE, r = E_SYSTEM, r, "[%s] Failed to set callback function for data receive", GetErrorMessage(r));
110 ret = serial_unset_state_changed_cb(gSerialHandle);
111 ret = serial_unset_data_received_cb(gSerialHandle);
112 ret = serial_close(gSerialHandle);
113 ret = serial_destroy(gSerialHandle);
117 _DataRouter::_DataRouter(void)
118 : __pDataRouterEventListener(null)
119 , __initialized(false)
122 _DataRouter::~_DataRouter(void)
125 ret = serial_close(gSerialHandle);
126 if (ret != SERIAL_ERROR_NONE)
128 SysLogException(NID_IO, E_SYSTEM, "Failed to close handle of serial. Error:%d", ret);
131 ret = serial_destroy(gSerialHandle);
132 if (ret != SERIAL_ERROR_NONE)
134 SysLogException(NID_IO, E_SYSTEM, "Failed to release handle of serial. Error:%d", ret);
137 gSerialHandle = null;
141 _DataRouter::Construct(_IDataRouterEventListener& listener)
145 __pDataRouterEventListener = &listener;
147 if(__initialized == false)
149 ret = vconf_notify_key_changed(_VCONF_KEY_DATA_ROUTER_READY_EVENT, SerialStateReadyCallback, null);
150 SysTryReturnResult(NID_IO, ret == 0, E_SYSTEM, "It is failed to register vconf event for data router ready.");
151 __initialized = true;
157 _DataRouter::Write(const char* data, int length)
160 SysTryReturnResult(NID_IO, gSerialHandle != null, E_SYSTEM, "DataRouter is not ready.");
162 ret = serial_write(gSerialHandle, data, length);
163 SysTryReturnResult(NID_IO, ret >= 0, E_SYSTEM, "Write is failed %d", ret);
169 _DataRouter::DataReceived(const char* data, int length)
171 if (__pDataRouterEventListener != null)
173 __pDataRouterEventListener->OnDataRouterDataReceivedN(data, length);
178 _DataRouter::StateChanged(_DataRouterState state)
180 if (__pDataRouterEventListener != null)
182 __pDataRouterEventListener->OnDataRouterStateChanged(state);
187 _DataRouter::GetInstance(void)
189 if (gp_DataRouter == null)
191 gp_DataRouter = new (std::nothrow) _DataRouter();
192 SysTryReturn(NID_IO, gp_DataRouter != null, null, E_OUT_OF_MEMORY, "The memory is insufficient.");
197 return gp_DataRouter;
201 _DataRouter::ReleaseInstance(void)
206 if (gp_DataRouter != null && __refCount == 0)
208 delete gp_DataRouter;
209 gp_DataRouter = null;