2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 #include <Commons/Exception.h>
21 #include <app_manager.h>
22 #include <package-manager.h>
25 #include "DataControlCallback.h"
26 #include "SqlDataControlConsumer.h"
27 #include "MappedDataControlConsumer.h"
29 #include "DataControlAsyncCallbackManager.h"
31 #include "EventDelete.h"
32 #include "EventInsert.h"
33 #include "EventSelect.h"
34 #include "EventUpdate.h"
35 #include "EventRemoveValue.h"
36 #include "EventAddValue.h"
37 #include "EventGetValue.h"
38 #include "EventUpdateValue.h"
41 using namespace WrtDeviceApis::Commons;
44 namespace DataControl {
46 // utils for common usage
47 std::vector<std::string> getDataArrayFromBundle(bundle *b, std::string key)
49 std::vector<std::string> result;
54 array = appsvc_get_data_array(b, key.c_str(), &length);
56 if (array == NULL || length == 0)
58 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result data fail from datacontrol provider");
61 for (index = 0; index < length; index++)
63 result.push_back(array[index]);
69 std::string getProviderApplicationId(const std::string type, const std::string& provId)
71 std::string appIdStr = "";
75 const char *passId = provId.c_str();
77 if( pkgmgr_datacontrol_get_info(passId, type.c_str(), &appId, &access) < 0)
79 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "get appId error");
96 std::string getCurrentApplicationId()
99 std::string appId = "";
100 int parent_pid = getppid();
101 LoggerD("parent pid : " << parent_pid);
102 int ret = app_manager_get_app_id(parent_pid, &app_id);
104 if((ret != APP_ERROR_NONE) || (app_id == NULL))
106 LoggerE("Can not get app id from current pid (" << ret << ")");
107 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "could not get information about application");
114 std::string getProviderPkgId(const std::string& appId)
117 std::string pkgIdStr = "";
118 app_info_h handle = NULL;
120 int ret = app_manager_get_app_info(appId.c_str(), &handle);
122 if (ret != APP_ERROR_NONE)
124 LoggerD("Fail to get appinfo");
125 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "get appId error");
128 ret = app_info_get_package(handle, &pkgId);
130 if ((ret != APP_ERROR_NONE) || (pkgId == NULL))
132 LoggerD("Fail to get pkg_name");
133 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "get pkdid error");
136 app_info_destroy(handle);
141 std::string convertIntToString(unsigned int data)
143 std::stringstream ssbuffer;
147 return ssbuffer.str();
150 std::string generateFileName(unsigned int reqId, const std::string currentAppId)
152 std::stringstream ssdata;
155 std::string parent = DATACONTROL_PROTOCOL_DIR;
156 std::string filename = parent + "/" + currentAppId + ssdata.str();
160 void addArrayToBundle(bundle* passData, std::vector<std::string>& array)
162 size_t arraySize = array.size();
166 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "size = 0");
169 const char** arr = NULL;
170 arr = (const char**)calloc(sizeof(char*), arraySize);
174 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "null");
177 for (size_t index = 0; index < arraySize; index++)
179 arr[index] = array[index].c_str();
183 bundle_add_str_array(passData, OSP_K_ARG, arr, arraySize);
189 void handleRemainingPendingEvent()
191 CommonPendingEvent* userData = NULL;
192 unsigned short currentJob = 0;
193 SQLDataControlConsumer *sqlConsumer = NULL;
194 MappedDataControlConsumer *mapConsumer = NULL;
198 userData = (CommonPendingEvent*)DataControlAsyncCallbackManagerSingleton::Instance().getRemainingIpcOperation();
202 SendAppControlLaunchToProvider(userData, currentJob);
205 catch (const WrtDeviceApis::Commons::Exception& ex)
211 case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
212 case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
213 case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
214 case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
216 sqlConsumer = (SQLDataControlConsumer *)userData->getThisObject();
217 sqlConsumer->handleCommonErrorEvent(userData, ex.getCode(), ex.GetMessage());
220 case _DATACONTROL_REQUEST_TYPE_MAP_QUERY:
221 case _DATACONTROL_REQUEST_TYPE_MAP_INSERT:
222 case _DATACONTROL_REQUEST_TYPE_MAP_UPDATE:
223 case _DATACONTROL_REQUEST_TYPE_MAP_DELETE:
225 mapConsumer = (MappedDataControlConsumer *)userData->getThisObject();
226 mapConsumer->handleCommonErrorEvent(userData, ex.getCode(), ex.GetMessage());
231 LoggerD("Unknown Request");
238 void SendAppControlLaunchToProvider(void* event, unsigned short& currentJob)
241 bundle* passData = NULL;
242 std::vector<std::string> queryItem;
243 std::stringstream ssBuffer;
244 std::string stringBuffer;
245 CommonPendingEvent* pEvent = (CommonPendingEvent*)(event);
246 std::string ipcFilename;
248 unsigned int reqId = 0;
249 unsigned int columnSize = 0;
250 currentJob = _DATACONTROL_REQUEST_TYPE_UNDEFINED;
254 passData = bundle_create();
256 if (passData == NULL)
258 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "ipc memory allocation fail");
263 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "event null");
266 appsvc_set_operation(passData, APPSVC_OPERATION_DEFAULT);
267 appsvc_set_appid(passData, pEvent->getProviderAppId().c_str());
269 bundle_add(passData, OSP_K_CALLER_TYPE, OSP_V_CALLER_TYPE_OSP);
270 bundle_add(passData, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_DATACONTROL);
271 bundle_add(passData, OSP_K_DATACONTROL_PROVIDER, pEvent->getProviderId().c_str());
272 bundle_add(passData, OSP_K_DATACONTROL_PROTOCOL_VERSION, OSP_K_DATACONTROL_PROTOCOL_VERSION_VALUE); // version
273 bundle_add(passData, AUL_K_CALLER_APPID, pEvent->getCurrentAppId().c_str());
275 if (dynamic_cast<EventInsertPendingEvent*>(pEvent) != NULL)
278 currentJob = _DATACONTROL_REQUEST_TYPE_SQL_INSERT;
280 EventInsertPendingEvent* pendingInsertEvent = (EventInsertPendingEvent*)pEvent;
281 EventInsertPtr insertEvent = pendingInsertEvent->getEvent();
283 reqId = insertEvent->getReqId();
284 columnSize = insertEvent->getColumnSize();
286 stringBuffer = convertIntToString(reqId);
287 bundle_add(passData, OSP_K_REQUEST_ID, stringBuffer.c_str());
289 ipcFilename = generateFileName(reqId, pendingInsertEvent->getCurrentAppId().c_str());
290 stringBuffer = convertIntToString(columnSize);
292 queryItem.push_back(pendingInsertEvent->getDataId());
293 queryItem.push_back(stringBuffer);
294 queryItem.push_back(ipcFilename);
296 stringBuffer = convertIntToString(_DATACONTROL_REQUEST_TYPE_SQL_INSERT);
297 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, stringBuffer.c_str());
299 addArrayToBundle(passData, queryItem);
301 else if (dynamic_cast<EventUpdatePendingEvent*>(pEvent) != NULL)
304 currentJob = _DATACONTROL_REQUEST_TYPE_SQL_UPDATE;
306 EventUpdatePendingEvent* pendingUpdateEvent = (EventUpdatePendingEvent*)pEvent;
307 EventUpdatePtr updateEvent = pendingUpdateEvent->getEvent();
309 reqId = updateEvent->getReqId();
310 columnSize = updateEvent->getColumnSize();
311 where = updateEvent->getWhere();
313 stringBuffer = convertIntToString(reqId);
314 bundle_add(passData, OSP_K_REQUEST_ID, stringBuffer.c_str());
316 stringBuffer = convertIntToString(_DATACONTROL_REQUEST_TYPE_SQL_UPDATE);
317 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, stringBuffer.c_str());
319 ipcFilename = generateFileName(reqId, pendingUpdateEvent->getCurrentAppId().c_str());
320 stringBuffer = convertIntToString(columnSize);
322 queryItem.push_back(pendingUpdateEvent->getDataId());
323 queryItem.push_back(stringBuffer);
324 queryItem.push_back(ipcFilename);
326 if (where.size() == 0) // where
328 queryItem.push_back("NULL");
332 queryItem.push_back(where);
335 addArrayToBundle(passData, queryItem);
338 else if (dynamic_cast<EventSelectPendingEvent*>(pEvent) != NULL)
341 currentJob = _DATACONTROL_REQUEST_TYPE_SQL_QUERY;
343 EventSelectPendingEvent* pendingSelectEvent = (EventSelectPendingEvent*)pEvent;
344 EventSelectPtr selectEvent = pendingSelectEvent->getEvent();
346 reqId = selectEvent->getReqId();
347 std::vector<std::string> columns = selectEvent->getColumns();
348 columnSize = columns.size();
349 where = selectEvent->getWhere();
350 std::string order = selectEvent->getOrder();
351 std::string page = selectEvent->getPage();
352 std::string numberPerPage = selectEvent->getNumerPerPage();
354 stringBuffer = convertIntToString(reqId);
355 bundle_add(passData, OSP_K_REQUEST_ID, stringBuffer.c_str());
357 stringBuffer = convertIntToString(_DATACONTROL_REQUEST_TYPE_SQL_QUERY);
358 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, stringBuffer.c_str());
360 queryItem.push_back(pendingSelectEvent->getDataId());
364 queryItem.push_back("NULL");
368 stringBuffer = convertIntToString(columnSize);
369 queryItem.push_back(stringBuffer);
371 for (size_t index = 0; index < columnSize; index++)
373 queryItem.push_back(columns[index]);
377 if (where.size() == 0)
379 queryItem.push_back("NULL");
383 queryItem.push_back(where);
386 if (order.size() == 0)
388 queryItem.push_back("NULL");
392 queryItem.push_back(order);
395 if (page.size() == 0) // page
397 queryItem.push_back("1");
401 queryItem.push_back(page);
404 if (numberPerPage.size() == 0) // numberOfPage
406 queryItem.push_back("20");
410 queryItem.push_back(numberPerPage);
413 addArrayToBundle(passData, queryItem);
415 else if (dynamic_cast<EventDeletePendingEvent*>(pEvent) != NULL)
417 currentJob = _DATACONTROL_REQUEST_TYPE_SQL_DELETE;
419 EventDeletePendingEvent* pendingDeleteEvent = (EventDeletePendingEvent*)pEvent;
420 EventDeletePtr deleteEvent = pendingDeleteEvent->getEvent();
421 reqId = deleteEvent->getReqId();
422 stringBuffer = convertIntToString(reqId);
423 bundle_add(passData, OSP_K_REQUEST_ID, stringBuffer.c_str());
425 stringBuffer = convertIntToString(_DATACONTROL_REQUEST_TYPE_SQL_DELETE);
426 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, stringBuffer.c_str());
428 queryItem.push_back(pendingDeleteEvent->getDataId()); // dataid
429 where = deleteEvent->getWhere();
431 if (where.size() == 0) // where
433 queryItem.push_back("NULL");
437 queryItem.push_back(where);
440 addArrayToBundle(passData, queryItem);
442 else if (dynamic_cast<EventAddValuePendingEvent*>(pEvent) != NULL)
445 currentJob = _DATACONTROL_REQUEST_TYPE_MAP_INSERT;
447 EventAddValuePendingEvent* pendingAddValueEvent = (EventAddValuePendingEvent*)pEvent;
448 EventAddValuePtr addValueEvent = pendingAddValueEvent->getEvent();
449 reqId = addValueEvent->getReqId();
450 stringBuffer = convertIntToString(reqId);
451 bundle_add(passData, OSP_K_REQUEST_ID, stringBuffer.c_str());
453 stringBuffer = convertIntToString(_DATACONTROL_REQUEST_TYPE_MAP_INSERT);
454 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, stringBuffer.c_str());
456 queryItem.push_back(pendingAddValueEvent->getDataId());
457 queryItem.push_back(addValueEvent->getKey());
458 queryItem.push_back(addValueEvent->getValue());
460 addArrayToBundle(passData, queryItem);
462 else if (dynamic_cast<EventUpdateValuePendingEvent*>(pEvent) != NULL)
465 currentJob = _DATACONTROL_REQUEST_TYPE_MAP_UPDATE;
467 EventUpdateValuePendingEvent* pendingUpdateValueEvent = (EventUpdateValuePendingEvent*)pEvent;
468 EventUpdateValuePtr updateValueEvent = pendingUpdateValueEvent->getEvent();
469 reqId = updateValueEvent->getReqId();
470 stringBuffer = convertIntToString(reqId);
471 bundle_add(passData, OSP_K_REQUEST_ID, stringBuffer.c_str());
473 stringBuffer = convertIntToString(_DATACONTROL_REQUEST_TYPE_MAP_UPDATE);
474 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, stringBuffer.c_str());
476 queryItem.push_back(pendingUpdateValueEvent->getDataId());
477 queryItem.push_back(updateValueEvent->getKey());
478 queryItem.push_back(updateValueEvent->getOldValue());
479 queryItem.push_back(updateValueEvent->getNewValue());
482 addArrayToBundle(passData, queryItem);
484 else if (dynamic_cast<EventGetValuePendingEvent*>(pEvent) != NULL)
487 currentJob = _DATACONTROL_REQUEST_TYPE_MAP_QUERY;
489 EventGetValuePendingEvent* pendingGetValueEvent = (EventGetValuePendingEvent*)pEvent;
490 EventGetValuePtr getValueEvent = pendingGetValueEvent->getEvent();
491 reqId = getValueEvent->getReqId();
492 stringBuffer = convertIntToString(reqId);
493 bundle_add(passData, OSP_K_REQUEST_ID, stringBuffer.c_str());
495 stringBuffer = convertIntToString(_DATACONTROL_REQUEST_TYPE_MAP_QUERY);
496 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, stringBuffer.c_str());
498 queryItem.push_back(pendingGetValueEvent->getDataId());
499 queryItem.push_back(getValueEvent->getKey());
500 queryItem.push_back("1");
501 queryItem.push_back("100"); //default num per a page // consider this in 3.0 as a spec
503 addArrayToBundle(passData, queryItem);
506 else if (dynamic_cast<EventRemoveValuePendingEvent*>(pEvent) != NULL)
509 currentJob = _DATACONTROL_REQUEST_TYPE_MAP_DELETE;
511 EventRemoveValuePendingEvent* pendingRemoveValueEvent = (EventRemoveValuePendingEvent*)pEvent;
512 EventRemoveValuePtr removeValueEvent = pendingRemoveValueEvent->getEvent();
513 reqId = removeValueEvent->getReqId();
514 stringBuffer = convertIntToString(reqId);
515 bundle_add(passData, OSP_K_REQUEST_ID, stringBuffer.c_str());
517 stringBuffer = convertIntToString(_DATACONTROL_REQUEST_TYPE_MAP_DELETE);
518 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, stringBuffer.c_str());
520 queryItem.push_back(pendingRemoveValueEvent->getDataId());
521 queryItem.push_back(removeValueEvent->getKey());
522 queryItem.push_back(removeValueEvent->getValue());
524 addArrayToBundle(passData, queryItem);
529 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "no type request type");
534 for (int index = 0; index < 3; index++)
536 pid = appsvc_run_service(passData, reqId, dataControlCommonCallback, (void*)NULL);
542 LoggerD("Launch Retry" << (index + 1));
548 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Launch Error");
551 LoggerD("Launch OK : pid(" << pid << "), reqid : (" << reqId << ")");
553 catch (const WrtDeviceApis::Commons::Exception& ex)
555 LoggerE("Exception: " << ex.GetMessage());
558 bundle_free(passData);
561 ThrowMsg(WrtDeviceApis::Commons::PlatformException, ex.GetMessage());
567 bundle_free(passData);
574 // callbacks for sqldatacontrol
575 void sqldataControlSelectCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
579 EventSelectPendingEvent *pendingEvent = NULL;
580 SQLDataControlConsumer *consumer = NULL;
581 EventSelectPtr event;
582 const char *bundleKey = NULL;
588 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
591 bundleKey = appsvc_get_data(b, OSP_K_REQUEST_ID);
593 if (bundleKey == NULL)
595 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get key fail from datacontrol provider");
598 pendingEvent = (EventSelectPendingEvent *)DataControlAsyncCallbackManagerSingleton::Instance().removeSQLUserData(bundleKey);
599 consumer = (SQLDataControlConsumer*)pendingEvent->getThisObject();
601 if (DataControlAsyncCallbackManagerSingleton::Instance().isDataControlGC((void*)consumer))
603 LoggerD("private object is garbage collected");
607 event = pendingEvent->getEvent();
609 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
611 for (size_t index = 0; index < result.size(); index++)
613 LoggerD(result[index]);
616 if (result.size() < 3)
618 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
621 // 0 : result true or false??
622 if (RESULT_TRUE_FROM_OSP != result[0])
625 ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
628 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
629 // 2 : result set file path
630 event->setResultSetPath(result[2]);
633 catch (const WrtDeviceApis::Commons::Exception& ex)
635 LoggerE("Exception: " << ex.GetMessage());
637 if (event.Get() == NULL)
639 LoggerD("event removed, invalid cb");
643 event->setExceptionCode(ex.getCode());
644 event->setErrorMsg(ex.GetMessage());
646 consumer->handlePendingEvent(event);
655 void sqldataControlInsertCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
660 EventInsertPendingEvent *pendingEvent = NULL;
661 SQLDataControlConsumer *consumer = NULL;
662 EventInsertPtr event;
663 const char *bundleKey = NULL;
669 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
672 bundleKey = appsvc_get_data(b, OSP_K_REQUEST_ID);
674 if (bundleKey == NULL)
676 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get key fail from datacontrol provider");
679 pendingEvent = (EventInsertPendingEvent *)DataControlAsyncCallbackManagerSingleton::Instance().removeSQLUserData(bundleKey);
680 consumer = (SQLDataControlConsumer*)pendingEvent->getThisObject();
682 if (DataControlAsyncCallbackManagerSingleton::Instance().isDataControlGC((void*)consumer))
684 LoggerD("private object is garbage collected");
689 event = pendingEvent->getEvent();
691 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
693 for (size_t index = 0; index < result.size(); index++)
695 LoggerD(result[index]);
698 if (result.size() < 2)
700 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
703 // 0 : result true or false??
704 if (RESULT_TRUE_FROM_OSP != result[0])
707 ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
711 std::stringstream sstr(result[2]);
712 long insertRowid = 0;
714 LoggerD(result[2] << insertRowid);
715 event->setRowId(insertRowid);
716 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
719 catch (const WrtDeviceApis::Commons::Exception& ex)
721 LoggerE("Exception: " << ex.GetMessage());
723 if (event.Get() == NULL)
725 LoggerD("event removed, invalid cb");
729 event->setExceptionCode(ex.getCode());
730 event->setErrorMsg(ex.GetMessage());
732 consumer->handlePendingEvent(event);
741 void sqldataControlDeleteCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
745 EventDeletePendingEvent* pendingEvent = NULL;
746 SQLDataControlConsumer *consumer = NULL;
747 EventDeletePtr event;
748 const char* bundleKey = NULL;
754 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
757 bundleKey = appsvc_get_data(b, OSP_K_REQUEST_ID);
759 if (bundleKey == NULL)
761 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get key fail from datacontrol provider");
764 pendingEvent = (EventDeletePendingEvent *)DataControlAsyncCallbackManagerSingleton::Instance().removeSQLUserData(bundleKey);
765 consumer = (SQLDataControlConsumer*)pendingEvent->getThisObject();
767 if (DataControlAsyncCallbackManagerSingleton::Instance().isDataControlGC((void*)consumer))
769 LoggerD("private object is garbage collected");
774 event = pendingEvent->getEvent();
776 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
778 for (size_t index = 0; index < result.size(); index++)
780 LoggerD(result[index]);
783 if (result.size() < 2)
785 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
788 // 0 : result true or false??
789 if (RESULT_TRUE_FROM_OSP != result[0])
792 ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
795 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
799 catch (const WrtDeviceApis::Commons::Exception& ex)
801 LoggerE("Exception: " << ex.GetMessage());
803 if (event.Get() == NULL)
805 LoggerD("event removed, invalid cb");
809 event->setExceptionCode(ex.getCode());
810 event->setErrorMsg(ex.GetMessage());
812 consumer->handlePendingEvent(event);
820 void sqldataControlUpdateCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
824 EventUpdatePendingEvent* pendingEvent = NULL;
825 SQLDataControlConsumer *consumer = NULL;
826 EventUpdatePtr event;
827 const char *bundleKey = NULL;
833 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
836 bundleKey = appsvc_get_data(b, OSP_K_REQUEST_ID);
838 if (bundleKey == NULL)
840 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get key fail from datacontrol provider");
843 pendingEvent = (EventUpdatePendingEvent *)DataControlAsyncCallbackManagerSingleton::Instance().removeSQLUserData(bundleKey);
844 consumer = (SQLDataControlConsumer*)pendingEvent->getThisObject();
846 if (DataControlAsyncCallbackManagerSingleton::Instance().isDataControlGC((void*)consumer))
848 LoggerD("private object is garbage collected");
853 event = pendingEvent->getEvent();
855 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
857 for (size_t index = 0; index < result.size(); index++)
859 LoggerD(result[index]);
862 if (result.size() < 2)
864 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
867 // 0 : result true or false??
868 if (RESULT_TRUE_FROM_OSP != result[0])
871 ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
874 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
877 catch (const WrtDeviceApis::Commons::Exception& ex)
879 LoggerE("Exception: " << ex.GetMessage());
881 if (event.Get() == NULL)
883 LoggerD("event removed, invalid cb");
887 event->setExceptionCode(ex.getCode());
888 event->setErrorMsg(ex.GetMessage());
890 consumer->handlePendingEvent(event);
898 void dataControlCommonCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
904 LoggerD("Bundle null, Error");
908 const char *reqType = appsvc_get_data(b, OSP_K_DATACONTROL_REQUEST_TYPE);
912 LoggerD("UnkownRequest");
916 std::istringstream buffer(reqType);
919 buffer >> reqTypeInt;
926 case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
927 sqldataControlSelectCallback(b, request_code, res, data);
929 case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
930 sqldataControlInsertCallback(b, request_code, res, data);
932 case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
933 sqldataControlDeleteCallback(b, request_code, res, data);
935 case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
936 sqldataControlUpdateCallback(b, request_code, res, data);
938 case _DATACONTROL_REQUEST_TYPE_MAP_QUERY:
939 MappedDataControlGetValueCallback(b, request_code, res, data);
941 case _DATACONTROL_REQUEST_TYPE_MAP_INSERT:
942 MappedDataControlAddValueCallback(b, request_code, res, data);
944 case _DATACONTROL_REQUEST_TYPE_MAP_UPDATE:
945 MappedDataControlUpdateValueCallback(b, request_code, res, data);
947 case _DATACONTROL_REQUEST_TYPE_MAP_DELETE:
948 MappedDataControlRemoveValueCallback(b, request_code, res, data);
951 LoggerD("Unknown Request");
955 // callbacks for mapped datacontrol
956 void MappedDataControlGetValueCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
960 EventGetValuePendingEvent *pendingEvent = NULL;
961 MappedDataControlConsumer *consumer = NULL;
962 EventGetValuePtr event;
965 std::ifstream getValueStream;
966 std::string getValueFilePath;
967 const char *bundleKey = NULL;
973 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
976 bundleKey = appsvc_get_data(b, OSP_K_REQUEST_ID);
978 if (bundleKey == NULL)
980 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get key fail from datacontrol provider");
983 pendingEvent = (EventGetValuePendingEvent *)DataControlAsyncCallbackManagerSingleton::Instance().removeSQLUserData(bundleKey);
984 consumer = (MappedDataControlConsumer*)pendingEvent->getThisObject();
987 if (DataControlAsyncCallbackManagerSingleton::Instance().isDataControlGC((void*)consumer))
989 LoggerD("private object is garbage collected");
994 event = pendingEvent->getEvent();
998 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
1000 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
1002 for (index = 0; index < result.size(); index++)
1004 LoggerD(result[index]);
1007 if (result.size() < 2)
1009 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
1012 // 0 : result true or false??
1013 if (RESULT_TRUE_FROM_OSP != result[0])
1016 if (result[1].find("E_KEY_NOT_FOUND") != std::string::npos)
1018 ThrowMsg(WrtDeviceApis::Commons::NotFoundException, result[1]);
1022 ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
1026 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
1029 std::istringstream str(result[2]);
1035 getValueFilePath = result[3];
1036 getValueStream.open(getValueFilePath.c_str(), std::ios::binary);
1037 int memorizedSize = 255;
1040 buf = new char[memorizedSize + 1];
1042 if (getValueStream.is_open())
1048 LoggerD("fail open" << getValueFilePath);
1049 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "get value error");
1052 for (index = 0; index < count; index++)
1054 getValueStream.read((char*)&size, sizeof(int)); // read size
1057 if (memorizedSize < size)
1059 memorizedSize = size;
1066 buf = new char[memorizedSize + 1];
1071 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Out of memory");
1073 memset(buf, 0, memorizedSize + 1);
1074 getValueStream.read((char*)buf, size);
1077 event->addResultValue(buf);
1083 catch (const WrtDeviceApis::Commons::Exception& ex)
1085 LoggerE("Exception: " << ex.GetMessage());
1087 if (event.Get() == NULL)
1089 LoggerD("event removed, invalid cb");
1093 event->setExceptionCode(ex.getCode());
1094 event->setErrorMsg(ex.GetMessage());
1096 getValueStream.close();
1097 if (unlink(getValueFilePath.c_str()) != 0)
1099 LoggerE("Error while removing SelectDataObject.");
1102 consumer->handlePendingEvent(event);
1106 delete pendingEvent;
1108 handleRemainingPendingEvent();
1111 void MappedDataControlAddValueCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
1116 EventAddValuePendingEvent *pendingEvent = NULL;
1117 MappedDataControlConsumer *consumer = NULL;
1118 EventAddValuePtr event;
1119 const char *bundleKey = NULL;
1125 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
1128 bundleKey = appsvc_get_data(b, OSP_K_REQUEST_ID);
1130 if (bundleKey == NULL)
1132 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get key fail from datacontrol provider");
1135 pendingEvent = (EventAddValuePendingEvent *)DataControlAsyncCallbackManagerSingleton::Instance().removeSQLUserData(bundleKey);
1136 consumer = (MappedDataControlConsumer*)pendingEvent->getThisObject();
1139 if (DataControlAsyncCallbackManagerSingleton::Instance().isDataControlGC((void*)consumer))
1141 LoggerD("private object is garbage collected");
1142 delete pendingEvent;
1146 event = pendingEvent->getEvent();
1148 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
1150 for (size_t index = 0; index < result.size(); index++)
1152 LoggerD(result[index]);
1155 if (result.size() < 2)
1157 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
1160 // 0 : result true or false??
1161 if (RESULT_TRUE_FROM_OSP != result[0])
1164 ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
1167 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
1170 catch (const WrtDeviceApis::Commons::Exception& ex)
1172 LoggerE("Exception: " << ex.GetMessage());
1174 if (event.Get() == NULL)
1176 LoggerD("event removed, invalid cb");
1180 LoggerE("Exception: " << ex.GetMessage());
1181 event->setExceptionCode(ex.getCode());
1182 event->setErrorMsg(ex.GetMessage());
1184 consumer->handlePendingEvent(event);
1188 delete pendingEvent;
1190 handleRemainingPendingEvent();
1194 void MappedDataControlRemoveValueCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
1198 EventRemoveValuePendingEvent* pendingEvent = NULL;
1199 MappedDataControlConsumer *consumer = NULL;
1200 EventRemoveValuePtr event;
1201 const char *bundleKey = NULL;
1207 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
1210 bundleKey = appsvc_get_data(b, OSP_K_REQUEST_ID);
1212 if (bundleKey == NULL)
1214 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get key fail from datacontrol provider");
1217 pendingEvent = (EventRemoveValuePendingEvent *)DataControlAsyncCallbackManagerSingleton::Instance().removeSQLUserData(bundleKey);
1218 consumer = (MappedDataControlConsumer*)pendingEvent->getThisObject();
1221 if (DataControlAsyncCallbackManagerSingleton::Instance().isDataControlGC((void*)consumer))
1223 LoggerD("private object is garbage collected");
1224 delete pendingEvent;
1228 event = pendingEvent->getEvent();
1232 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
1234 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
1236 for (size_t index = 0; index < result.size(); index++)
1238 LoggerD(result[index]);
1241 if (result.size() < 2)
1243 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
1246 // 0 : result true or false??
1247 if (RESULT_TRUE_FROM_OSP != result[0])
1250 if (result[1].find("E_KEY_NOT_FOUND") != std::string::npos)
1252 ThrowMsg(WrtDeviceApis::Commons::NotFoundException, result[1]);
1256 ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
1260 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
1264 catch (const WrtDeviceApis::Commons::Exception& ex)
1266 LoggerE("Exception: " << ex.GetMessage());
1268 if (event.Get() == NULL)
1270 LoggerD("event removed, invalid cb");
1274 event->setExceptionCode(ex.getCode());
1275 event->setErrorMsg(ex.GetMessage());
1277 consumer->handlePendingEvent(event);
1281 delete pendingEvent;
1283 handleRemainingPendingEvent();
1286 void MappedDataControlUpdateValueCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
1290 EventUpdateValuePendingEvent* pendingEvent = NULL;
1291 MappedDataControlConsumer *consumer = NULL;
1292 EventUpdateValuePtr event;
1293 const char *bundleKey = NULL;
1299 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
1302 bundleKey = appsvc_get_data(b, OSP_K_REQUEST_ID);
1304 if (bundleKey == NULL)
1306 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get key fail from datacontrol provider");
1309 pendingEvent = (EventUpdateValuePendingEvent *)DataControlAsyncCallbackManagerSingleton::Instance().removeSQLUserData(bundleKey);
1310 consumer = (MappedDataControlConsumer*)pendingEvent->getThisObject();
1313 if (DataControlAsyncCallbackManagerSingleton::Instance().isDataControlGC((void*)consumer))
1315 LoggerD("private object is garbage collected");
1316 delete pendingEvent;
1320 event = pendingEvent->getEvent();
1324 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
1326 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
1328 for (size_t index = 0; index < result.size(); index++)
1330 LoggerD(result[index]);
1333 if (result.size() < 2)
1335 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
1338 // 0 : result true or false??
1339 if (RESULT_TRUE_FROM_OSP != result[0])
1342 if (result[1].find("E_KEY_NOT_FOUND") != std::string::npos)
1344 ThrowMsg(WrtDeviceApis::Commons::NotFoundException, result[1]);
1348 ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
1352 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
1355 catch (const WrtDeviceApis::Commons::Exception& ex)
1358 LoggerE("Exception: " << ex.GetMessage());
1360 if (event.Get() == NULL)
1362 LoggerD("event removed, invalid cb");
1367 event->setExceptionCode(ex.getCode());
1368 event->setErrorMsg(ex.GetMessage());
1370 consumer->handlePendingEvent(event);
1374 delete pendingEvent;
1376 handleRemainingPendingEvent();