diff options
Diffstat (limited to 'mobile_src/NFC/ResponseDispatcher.cpp')
-rwxr-xr-x | mobile_src/NFC/ResponseDispatcher.cpp | 262 |
1 files changed, 262 insertions, 0 deletions
diff --git a/mobile_src/NFC/ResponseDispatcher.cpp b/mobile_src/NFC/ResponseDispatcher.cpp new file mode 100755 index 0000000..f89e30c --- /dev/null +++ b/mobile_src/NFC/ResponseDispatcher.cpp @@ -0,0 +1,262 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <vector> +#include <CommonsJavaScript/JSCallbackManager.h> +#include <JSWebAPIErrorFactory.h> +#include "ResponseDispatcher.h" +#include "NFCConverter.h" +#include "JSNdefMessage.h" +#include "NFCAsyncCallbackManager.h" +#include <Logger.h> + +using namespace DeviceAPI::Common; +using namespace WrtDeviceApis::Commons; +using namespace WrtDeviceApis::CommonsJavaScript; +namespace DeviceAPI { +namespace NFC { + +NFCResponseDispatcher& NFCResponseDispatcher::getInstance() +{ + static NFCResponseDispatcher instance; + return instance; +} + +NFCResponseDispatcher::NFCResponseDispatcher() : + readNDEFAnswerReceiver(ThreadEnum::NULL_THREAD), + writeNDEFAnswerReceiver(ThreadEnum::NULL_THREAD), + transceiveAnswerReceiver(ThreadEnum::NULL_THREAD), + formatAnswerReceiver(ThreadEnum::NULL_THREAD), + TargetSendNdefAnswerReceiver(ThreadEnum::NULL_THREAD), + TargetReceiveNdefListener(ThreadEnum::NULL_THREAD) +{ + LoggerD("entered"); +} + +NFCResponseDispatcher::~NFCResponseDispatcher() +{ + LoggerD("entered"); +} + +void NFCResponseDispatcher::OnAnswerReceived(const EventTagActionReadPtr &event) +{ + JSCallbackManagerPtr cbm = + DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData()); + + Try { + if (!cbm) { + LoggerD("no callback manager"); + return; + } + NFCAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(cbm); + + if (event->getResult()) { + LoggerD("result success"); + cbm->callOnSuccess(JSNdefMessage::createJSObject(cbm->getContext(), event->getReadNdefResult())); + return; + } + LoggerD("result fail"); + + std::string error = event->getError(); + std::string errorMessage = event->getErrorMessage(); + JSValueRef errorObject; + if (error != "") { + if (errorMessage != "") + errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), error, errorMessage); + else + errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), error, error); + } else + errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR,"Unknown Error"); + + cbm->callOnError(errorObject); + } Catch (PlatformException) { + LoggerE("Platform can't create NdefMessage" << _rethrown_exception.GetMessage()); + JSValueRef errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR,"Unknown Error"); + cbm->callOnError(errorObject); + } Catch (Exception) { + LoggerE("Exception: " << _rethrown_exception.GetMessage()); + JSValueRef errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR,"Unknown Error"); + cbm->callOnError(errorObject); + } +} + +void NFCResponseDispatcher::OnAnswerReceived(const EventTagActionWritePtr &event) +{ + JSCallbackManagerPtr cbm = + DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData()); + + if (!cbm) { + LoggerD("no callback manager"); + return; + } + NFCAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(cbm); + + if (event->getResult()) { + LoggerD("result success"); + cbm->callOnSuccess(); + return; + } + LoggerD("result fail"); + + std::string error = event->getError(); + std::string errorMessage = event->getErrorMessage(); + JSValueRef errorObject; + if (error != "") { + if (errorMessage != "") + errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), error, errorMessage); + else + errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), error, error); + } else + errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR,"Unknown Error"); + + cbm->callOnError(errorObject); +} + +void NFCResponseDispatcher::OnAnswerReceived(const EventTagActionTransceivePtr &event) +{ + JSCallbackManagerPtr cbm = + DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData()); + + Try { + if (!cbm) { + LoggerD("no callback manager"); + return; + } + NFCAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(cbm); + + JSContextRef context = cbm->getContext(); + + if (event->getResult()) { + NFCConverter convert(context); + LoggerD("result success"); + cbm->callOnSuccess(convert.toJSValueRef(event->getTransceiveBuffer())); + return; + } + LoggerD("result fail"); + + std::string error = event->getError(); + std::string errorMessage = event->getErrorMessage(); + JSValueRef errorObject; + if (error != "") { + if (errorMessage != "") + errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), error, errorMessage); + else + errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), error, error); + } else + errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR,"Unknown Error"); + + cbm->callOnError(errorObject); + } Catch (ConversionException) { + LoggerE("Conversion exception while processing EventTagActionTransceive"); + JSValueRef errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR,"Unknown Error"); + cbm->callOnError(errorObject); + } Catch (Exception) { + LoggerE("Exception: " << _rethrown_exception.GetMessage()); + JSValueRef errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR,"Unknown Error"); + cbm->callOnError(errorObject); + } +} + +void NFCResponseDispatcher::OnAnswerReceived(const EventTagActionFormatPtr &event) +{ + JSCallbackManagerPtr cbm = + DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData()); + + if (!cbm) { + LoggerD("no callback manager"); + return; + } + NFCAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(cbm); + + if (event->getResult()) { + LoggerD("result success"); + cbm->callOnSuccess(); + return; + } + LoggerD("result fail"); + + std::string error = event->getError(); + std::string errorMessage = event->getErrorMessage(); + JSValueRef errorObject; + if (error != "") { + if (errorMessage != "") + errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), error, errorMessage); + else + errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), error, error); + } else + errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR,"Unknown Error"); + + cbm->callOnError(errorObject); +} + +void NFCResponseDispatcher::OnAnswerReceived(const EventTargetActionSendPtr &event) +{ + JSCallbackManagerPtr cbm = + DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData()); + + if (!cbm) { + LoggerD("no callback manager"); + return; + } + NFCAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(cbm); + + if (event->getResult()) { + LoggerD("result success"); + cbm->callOnSuccess(); + return; + } + LoggerD("result fail"); + + std::string error = event->getError(); + std::string errorMessage = event->getErrorMessage(); + JSValueRef errorObject; + if (error != "") { + if (errorMessage != "") + errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), error, errorMessage); + else + errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), error, error); + } else + errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR,"Unknown Error"); + + cbm->callOnError(errorObject); +} + +void NFCResponseDispatcher::onAnswerReceived(const EventTargetActionReceivePtr &event) +{ + LoggerD("onAnswerReceived Enter"); + JSCallbackManagerPtr cbm = + DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData()); + + Try { + if (!cbm) { + LoggerD("no callback manager"); + return; + } + + JSContextRef context = cbm->getContext(); + + LoggerD("result success"); + cbm->callOnSuccess(JSNdefMessage::createJSObject(context, event->getReadNdefResult())); + } Catch (PlatformException) { + LoggerE("Platform can't create NdefMessage" << _rethrown_exception.GetMessage()); + } Catch (Exception) { + LoggerE("Exception: " << _rethrown_exception.GetMessage()); + } +} + +} +} |