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)
44 if (gp_DataRouter != null)
46 if (state == SERIAL_STATE_OPENED)
48 gp_DataRouter->StateChanged(_DATA_ROUTER_STATE_OPENED);
50 else if (state == SERIAL_STATE_CLOSED)
52 gp_DataRouter->StateChanged(_DATA_ROUTER_STATE_CLOSED);
53 ret = serial_unset_state_changed_cb(gSerialHandle);
54 ret = serial_unset_data_received_cb(gSerialHandle);
55 ret = serial_destroy(gSerialHandle);
59 SysLogException(NID_IO, E_SYSTEM, "Undefined state is required.");
65 SerialDataReceivedCallback(const char* data, int length, void* userData)
68 if (gp_DataRouter != null)
70 gp_DataRouter->DataReceived(data, length);
77 SerialStateReadyCallback(keynode_t* node, void* userData)
82 if (strcmp(_VCONF_KEY_DATA_ROUTER_READY_EVENT, vconf_keynode_get_name(node)) == 0)
84 int dataRouterStatus = 0;
85 ret = vconf_get_int(_VCONF_KEY_DATA_ROUTER_READY_EVENT, &dataRouterStatus);
86 SysTryCatch(NID_IO, ret == 0, r= E_SYSTEM, r, "[%s] Failed to get vconf status");
88 if(dataRouterStatus == 1)
90 ret = serial_create(&gSerialHandle);
91 SysTryCatch(NID_IO, ret == SERIAL_ERROR_NONE, r = E_SYSTEM, r, "[%s] Failed to initialize data router module. Error:%d", ret);
93 ret = serial_set_state_changed_cb(gSerialHandle, SerialStateChangedCallback, null);
94 SysTryCatch(NID_IO, ret == SERIAL_ERROR_NONE, r = E_SYSTEM, r, "[%s] Failed to set callback function for state change", GetErrorMessage(r));
96 ret = serial_set_data_received_cb(gSerialHandle, SerialDataReceivedCallback, null);
97 SysTryCatch(NID_IO, ret == SERIAL_ERROR_NONE, r = E_SYSTEM, r, "[%s] Failed to set callback function for data receive", GetErrorMessage(r));
99 ret = serial_open(gSerialHandle);
100 SysTryCatch(NID_IO, ret == SERIAL_ERROR_NONE, r = E_SYSTEM, r, "[%s] Failed to set callback function for data receive", GetErrorMessage(r));
107 ret = serial_unset_state_changed_cb(gSerialHandle);
108 ret = serial_unset_data_received_cb(gSerialHandle);
109 ret = serial_close(gSerialHandle);
110 ret = serial_destroy(gSerialHandle);
114 _DataRouter::_DataRouter(void)
115 : __pDataRouterEventListener(null)
116 , __initialized(false)
119 _DataRouter::~_DataRouter(void)
122 ret = serial_close(gSerialHandle);
123 if (ret != SERIAL_ERROR_NONE)
125 SysLogException(NID_IO, E_SYSTEM, "Failed to close handle of serial. Error:%d", ret);
128 ret = serial_destroy(gSerialHandle);
129 if (ret != SERIAL_ERROR_NONE)
131 SysLogException(NID_IO, E_SYSTEM, "Failed to release handle of serial. Error:%d", ret);
134 gSerialHandle = null;
138 _DataRouter::Construct(_IDataRouterEventListener& listener)
142 __pDataRouterEventListener = &listener;
144 if(__initialized == false)
146 ret = vconf_notify_key_changed(_VCONF_KEY_DATA_ROUTER_READY_EVENT, SerialStateReadyCallback, null);
147 SysTryReturnResult(NID_IO, ret == 0, E_SYSTEM, "It is failed to register vconf event for data router ready.");
148 __initialized = true;
154 _DataRouter::Write(const char* data, int length)
157 SysTryReturnResult(NID_IO, gSerialHandle != null, E_SYSTEM, "DataRouter is not ready.");
159 ret = serial_write(gSerialHandle, data, length);
160 SysTryReturnResult(NID_IO, ret >= 0, E_SYSTEM, "Write is failed %d", ret);
166 _DataRouter::DataReceived(const char* data, int length)
168 if (__pDataRouterEventListener != null)
170 __pDataRouterEventListener->OnDataRouterDataReceivedN(data, length);
175 _DataRouter::StateChanged(_DataRouterState state)
177 if (__pDataRouterEventListener != null)
179 __pDataRouterEventListener->OnDataRouterStateChanged(state);
184 _DataRouter::GetInstance(void)
186 if (gp_DataRouter == null)
188 gp_DataRouter = new (std::nothrow) _DataRouter();
189 SysTryReturn(NID_IO, gp_DataRouter != null, null, E_OUT_OF_MEMORY, "The memory is insufficient.");
194 return gp_DataRouter;
198 _DataRouter::ReleaseInstance(void)
203 if (gp_DataRouter != null && __refCount == 0)
205 delete gp_DataRouter;
206 gp_DataRouter = null;