Update change log and spec for wrt-plugins-tizen_0.4.22
[framework/web/wrt-plugins-tizen.git] / src / DataControl / MappedDataControlConsumer.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 #include <cassert>
19 #include <Commons/Exception.h>
20 #include "DataType.h"
21 #include "MappedDataControlConsumer.h"
22 #include <appsvc/appsvc.h>
23 #include <string>
24 #include <vector>
25 #include <aul/aul.h>
26 #include <sstream>
27 #include <iostream>
28 #include <package-manager.h>
29
30
31 namespace DeviceAPI {
32 namespace DataControl {
33
34
35 namespace {
36
37 static const char OSP_V_CALLER_TYPE_OSP[] = "osp";
38 static const char OSP_V_LAUNCH_TYPE_LAUNCH[] = "launch";
39 static const char OSP_V_LAUNCH_TYPE_APPCONTROL[] = "appcontrol";
40 static const char OSP_V_LAUNCH_TYPE_DATACONTROL[] = "datacontrol";
41 static const char OSP_V_LAUNCH_TYPE_CONDTION[] = "condition";
42 static const char OSP_V_REQUEST_TYPE_SQL_QUERY[] = "sql_query";
43 static const char OSP_V_REQUEST_TYPE_SQL_INSERT[] = "sql_insert";
44 static const char OSP_V_REQUEST_TYPE_SQL_UPDATE[] = "sql_update";
45 static const char OSP_V_REQUEST_TYPE_SQL_DELETE[] = "sql_delete";
46 static const char OSP_V_REQUEST_TYPE_MAP_QEURY[] = "map_query";
47 static const char OSP_V_REQUEST_TYPE_MAP_INSERT[] = "map_insert";
48 static const char OSP_V_REQUEST_TYPE_MAP_UPDATE[] = "map_update";
49 static const char OSP_V_REQUEST_TYPE_MAP_DELETE[] = "map_delete";
50 static const char BUNDLE_KEY_WINDOW[] = "__APP_SVC_K_WIN_ID__";
51
52
53 #define OSP_K_CALLER_TYPE   "__OSP_CALLER_TYPE__"
54 #define OSP_K_LAUNCH_TYPE   "__OSP_LAUNCH_TYPE__"
55 #define OSP_K_ARG           "__OSP_ARGS__"
56 #define OSP_K_COND          "__OSP_COND_NAME__"
57 #define OSP_K_APPID         "__OSP_APPID__"
58 #define OSP_K_REQUEST_ID    "__OSP_REQUEST_ID__"
59 #define OSP_K_APPCONTROL_PROVIDER   "__OSP_APPCONTROL_PROVIDER__"
60 #define OSP_K_APPCONTROL_OPERATION  "__OSP_APPCONTROL_OPERATION__"
61 #define OSP_K_APPCONTROL_CATEGORY   "__OSP_APPCONTROL_CATEGORY__"
62 #define OSP_K_APPCONTROL_MIME       "__OSP_APPCONTROL_MIME__"
63 #define OSP_K_APPCONTROL_URI        "__OSP_APPCONTROL_URI__"
64 #define OSP_K_DATACONTROL_PROVIDER      "__OSP_DATACONTROL_PROVIDER__"
65 #define OSP_K_DATACONTROL_REQUEST_TYPE  "__OSP_DATACONTROL_REQUEST_TYPE__"
66
67 #define RESULT_TRUE_FROM_OSP "1"
68 #define RESULT_FALSE_FROM_OSP "0"
69
70 static std::vector<std::string> getDataArrayFromBundle(bundle *b, std::string key)
71 {
72         std::vector<std::string> result;
73         const char **array;
74         int length = 0;
75         int index = 0;
76         
77         array = appsvc_get_data_array(b, key.c_str(), &length);
78
79         if (array == NULL || length == 0)
80         {
81                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result data fail from datacontrol provider");
82         }
83         
84         for (index = 0; index < length; index++)
85         {
86                 result.push_back(array[index]);
87         }
88
89         return result;
90 }
91
92
93 static void MappedDataControlGetValueCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
94 {
95         LogDebug("Enter");
96         
97         EventGetValuePendingEvent *pendingEvent = NULL;
98         MappedDataControlConsumer *consumer = NULL;
99         EventGetValuePtr event;
100         int count = 0;
101         size_t index = 0;
102
103         try 
104         {
105                 if (data == NULL)
106                 {
107                         LogDebug("data null, can not send result to JS Layer");
108                         return;
109                 }
110
111                 pendingEvent = (EventGetValuePendingEvent *)data;
112                 consumer = (MappedDataControlConsumer*)pendingEvent->getThisObject();
113                 event = pendingEvent->getEvent();
114
115                 if (b == NULL)
116                 {
117                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
118                 }
119                 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
120
121                 for (index = 0; index < result.size(); index++)
122                 {
123                         LogDebug(result[index]);
124                 }
125
126                 if (result.size() < 3)
127                 {
128                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
129                 }
130                 
131                 // 0 : result true or false??
132                 if (RESULT_TRUE_FROM_OSP != result[0])
133                 {
134                         // 1 : error msg 
135                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
136                 }
137
138                 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
139                 // 2 : value count
140                 
141                 std::istringstream str(result[2]);
142                 str >> count;
143
144                 // 3 ~ value...
145                 if (count > 0)
146                 {
147                         for (index = 3; index < result.size(); index++)
148                         {
149                                 event->addResultValue(result[index]);
150                         }
151
152                 }
153                 
154         }
155         catch (const WrtDeviceApis::Commons::Exception& ex) 
156         {
157                 LogError("Exception: " << ex.GetMessage());
158                 event->setExceptionCode(ex.getCode());
159                 event->setErrorMsg(ex.GetMessage());            
160         }
161         consumer->handlePendingEvent(event);
162
163         if (pendingEvent)
164         {
165                 delete pendingEvent;
166         }
167
168 }
169
170 static void MappedDataControlAddValueCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
171 {
172         LogDebug("Enter");
173
174         
175         EventAddValuePendingEvent *pendingEvent = NULL;
176         MappedDataControlConsumer *consumer = NULL;
177         EventAddValuePtr event;
178
179         try 
180         {
181                 if (data == NULL)
182                 {
183                         LogDebug("data null, can not send result to JS Layer");
184                         return;
185                 }
186
187                 pendingEvent = (EventAddValuePendingEvent *)data;
188                 consumer = (MappedDataControlConsumer*)pendingEvent->getThisObject();
189                 event = pendingEvent->getEvent();
190
191                 if (b == NULL)
192                 {
193                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
194                 }
195                 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
196
197                 for (size_t index = 0; index < result.size(); index++)
198                 {
199                         LogDebug(result[index]);
200                 }
201
202                 if (result.size() < 2)
203                 {
204                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
205                 }
206                 
207                 // 0 : result true or false??
208                 if (RESULT_TRUE_FROM_OSP != result[0])
209                 {
210                         // 1 : error msg 
211                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
212                 }
213
214                 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
215                 
216         }
217         catch (const WrtDeviceApis::Commons::Exception& ex) 
218         {
219                 LogError("Exception: " << ex.GetMessage());
220                 event->setExceptionCode(ex.getCode());
221                 event->setErrorMsg(ex.GetMessage());            
222         }
223         consumer->handlePendingEvent(event);
224
225         if (pendingEvent)
226         {
227                 delete pendingEvent;
228         }
229
230 }
231
232 static void MappedDataControlRemoveValueCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
233 {
234         LogDebug("Enter");
235
236         if (data == NULL)
237         {
238                 LogDebug("Data or Bundle error");
239                 return;
240         }
241
242         EventRemoveValuePendingEvent* pendingEvent = NULL;
243         MappedDataControlConsumer *consumer = NULL;
244         EventRemoveValuePtr event;
245         
246         try 
247         {
248                 if (data == NULL)
249                 {
250                         LogDebug("data null, can not send result to JS Layer");
251                         return;
252                 }
253
254                 pendingEvent = (EventRemoveValuePendingEvent *)data;
255                 consumer = (MappedDataControlConsumer*)pendingEvent->getThisObject();
256                 event = pendingEvent->getEvent();
257
258                 if (b == NULL)
259                 {
260                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
261                 }
262                 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
263
264                 for (size_t index = 0; index < result.size(); index++)
265                 {
266                         LogDebug(result[index]);
267                 }
268
269                 if (result.size() < 2)
270                 {
271                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
272                 }
273                 
274                 // 0 : result true or false??
275                 if (RESULT_TRUE_FROM_OSP != result[0])
276                 {
277                         // 1 : error msg 
278                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
279                 }
280
281                 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
282
283                 
284         }
285         catch (const WrtDeviceApis::Commons::Exception& ex) 
286         {
287                 LogError("Exception: " << ex.GetMessage());
288                 event->setExceptionCode(ex.getCode());
289                 event->setErrorMsg(ex.GetMessage());            
290         }
291         consumer->handlePendingEvent(event);
292
293         if (pendingEvent)
294         {
295                 delete pendingEvent;
296         }
297 }
298
299 static void MappedDataControlUpdateValueCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
300 {
301         LogDebug("Enter");
302
303         EventUpdateValuePendingEvent* pendingEvent = NULL;
304         MappedDataControlConsumer *consumer = NULL;
305         EventUpdateValuePtr event;
306         
307         try 
308         {
309                 if (data == NULL)
310                 {
311                         LogDebug("data null, can not send result to JS Layer");
312                         return;
313                 }
314
315                 pendingEvent = (EventUpdateValuePendingEvent *)data;
316                 consumer = (MappedDataControlConsumer*)pendingEvent->getThisObject();
317                 event = pendingEvent->getEvent();
318
319                 if (b == NULL)
320                 {
321                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
322                 }
323                 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
324
325                 for (size_t index = 0; index < result.size(); index++)
326                 {
327                         LogDebug(result[index]);
328                 }
329
330                 if (result.size() < 2)
331                 {
332                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
333                 }
334                 
335                 // 0 : result true or false??
336                 if (RESULT_TRUE_FROM_OSP != result[0])
337                 {
338                         // 1 : error msg 
339                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
340                 }
341
342                 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
343                 
344         }
345         catch (const WrtDeviceApis::Commons::Exception& ex) 
346         {
347                 LogError("Exception: " << ex.GetMessage());
348                 event->setExceptionCode(ex.getCode());
349                 event->setErrorMsg(ex.GetMessage());            
350         }
351         consumer->handlePendingEvent(event);
352
353         if (pendingEvent)
354         {
355                 delete pendingEvent;
356         }
357 }
358
359 }
360 MappedDataControlConsumer::MappedDataControlConsumer() 
361 {
362         LogDebug("Enter");
363
364         m_appId = "";
365         m_type = "";;
366         m_dataId = "";
367         m_providerId = "";
368
369 }
370
371 MappedDataControlConsumer::~MappedDataControlConsumer() 
372 {
373         LogDebug("Enter");
374 }
375
376 DPL::Mutex MappedDataControlConsumer::m_mutex;
377
378
379 void MappedDataControlConsumer::setType(std::string& type)
380 {
381         if (type != MAP_DATA_CONTROL && type != SQL_DATA_CONTROL)
382                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "type mismatch");
383         
384         m_type = type;
385 }
386
387 void MappedDataControlConsumer::setProviderId(const std::string& id )
388 {
389         m_providerId = id;
390 }
391
392 void MappedDataControlConsumer::setDataId(const std::string& id )
393 {
394         m_dataId = id;
395 }
396
397
398 std::string MappedDataControlConsumer::getDataId()
399 {
400         return m_dataId;
401 }
402
403 std::string MappedDataControlConsumer::getProviderId()
404 {
405         return m_providerId;
406 }
407
408 std::string MappedDataControlConsumer::getType()
409 {
410         return m_type;
411 }
412
413
414
415
416 std::string MappedDataControlConsumer::getApplicationId(const std::string& provId)
417 {
418         std::string appIdStr = "";
419
420         char* appId = NULL;
421         char* access = NULL;
422         const char *passId = provId.c_str();
423         
424         if (m_appId.length() == 0)
425         {
426         
427                 LogDebug("need " << passId);
428
429                 if(     pkgmgr_datacontrol_get_info(passId, OSP_PKGINFO_MAP_TYPE, &appId, &access) < 0)
430                 {
431                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "get appId error");
432                 }
433                 
434                 LogDebug("need2");
435
436                 if (appId)
437                 {
438                         LogDebug(appId);
439                         appIdStr = appId;
440                         free(appId);
441                 }
442
443                 if (access)
444                 {
445                         free(access);
446                 }
447                 
448                 m_appId = appIdStr;
449
450
451         }
452         return m_appId;
453
454 }
455
456 void MappedDataControlConsumer::addArrayToBundle(bundle* passData, std::vector<std::string>& array)
457 {
458         size_t arraySize = array.size();
459
460         if (arraySize == 0)
461         {
462                 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "size = 0");
463         }
464         
465         const char** arr = NULL;
466         arr = (const char**)calloc(sizeof(char*), arraySize);
467
468         if (arr == NULL)
469         {
470                 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "null");
471         }
472
473         for (size_t index = 0; index < arraySize; index++) 
474         {
475                 arr[index] = array[index].c_str();
476         }
477
478         
479         bundle_add_str_array(passData, OSP_K_ARG, arr, arraySize);
480
481         free(arr);
482         arr = NULL;
483         
484 }
485
486
487 void MappedDataControlConsumer::handlePendingEvent(const EventAddValuePtr& event)
488 {
489         WrtDeviceApis::Commons::EventRequestReceiver<EventAddValue>::ManualAnswer(event);       
490         removeReqId(event->getReqId());
491 }
492 void MappedDataControlConsumer::handlePendingEvent(const EventRemoveValuePtr& event)
493 {
494         WrtDeviceApis::Commons::EventRequestReceiver<EventRemoveValue>::ManualAnswer(event); 
495         removeReqId(event->getReqId());
496 }
497 void MappedDataControlConsumer::handlePendingEvent(const EventGetValuePtr& event)
498 {
499         WrtDeviceApis::Commons::EventRequestReceiver<EventGetValue>::ManualAnswer(event);
500         removeReqId(event->getReqId());
501 }
502
503 void MappedDataControlConsumer::handlePendingEvent(const EventUpdateValuePtr& event)
504 {
505         WrtDeviceApis::Commons::EventRequestReceiver<EventUpdateValue>::ManualAnswer(event); 
506         removeReqId(event->getReqId());
507 }
508
509
510 void MappedDataControlConsumer::addValue(const EventAddValuePtr& event)
511 {
512         WrtDeviceApis::Commons::EventRequestReceiver<EventAddValue>::PostRequest(event);
513 }
514
515 void MappedDataControlConsumer::removeValue(const EventRemoveValuePtr& event)
516 {
517         WrtDeviceApis::Commons::EventRequestReceiver<EventRemoveValue>::PostRequest(event);
518 }
519
520 void MappedDataControlConsumer::getValue(const EventGetValuePtr& event)
521 {
522         WrtDeviceApis::Commons::EventRequestReceiver<EventGetValue>::PostRequest(event);
523 }
524
525 void MappedDataControlConsumer::updateValue(const EventUpdateValuePtr& event)
526 {
527         WrtDeviceApis::Commons::EventRequestReceiver<EventUpdateValue>::PostRequest(event);
528 }
529
530 bool MappedDataControlConsumer::checkReqIdUniqueness(unsigned int reqId)
531 {
532         size_t index = 0; 
533
534         for (index = 0; index < m_currentReqIds.size(); index++)
535         {
536                 if (m_currentReqIds[index] == reqId)
537                         return false;
538         }
539
540         return true;
541 }
542
543 void MappedDataControlConsumer::removeReqId(unsigned int reqId)
544 {
545         bool remove = false;
546         
547         std::vector<unsigned int>::iterator it, found;
548         
549         for (it = m_currentReqIds.begin(); it != m_currentReqIds.end(); ++it)
550         {
551                 if (*it == reqId)
552                 {
553                         found = it;     
554                         remove = true;
555                 }
556         }
557
558         if (remove)
559         {
560                 DPL::Mutex::ScopedLock lock(&m_mutex);
561                 m_currentReqIds.erase(found);
562         }
563 }
564
565
566 void MappedDataControlConsumer::OnRequestReceived(const EventAddValuePtr& event)
567 {
568         LogDebug("Enter");
569         bundle* passData = NULL;
570
571         try 
572         {
573                 std::string dataId = getDataId();
574                 unsigned int reqId = event->getReqId();
575                 std::string key = event->getKey();
576                 std::string value = event->getValue();
577
578                 std::stringstream ss, ssreqtype;
579                 std::string reqIdStr, reqtypestr;
580                 std::vector<std::string> queryItem;
581
582                 if (checkReqIdUniqueness(reqId) == false)
583                 {
584                         ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
585                 }
586
587
588                 // conversion
589                 ss << reqId;
590                 reqIdStr = ss.str();
591
592                 ssreqtype << _DATACONTROL_REQUEST_TYPE_MAP_INSERT;
593                 reqtypestr = ssreqtype.str();
594
595                 // FIXME
596                 // qi993y8s4e.DataControlProviderService
597                 std::string appId = getApplicationId(m_providerId);
598                 //.DataControlProviderService";
599
600                 passData = bundle_create();
601                 
602                 if (passData == NULL)
603                 {
604                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "ipc memory allocation fail");
605                 }
606
607                 appsvc_set_operation(passData, APPSVC_OPERATION_DEFAULT);
608
609                 appsvc_set_appid(passData, appId.c_str());
610
611                 bundle_add(passData, OSP_K_REQUEST_ID, reqIdStr.c_str());
612                 bundle_add(passData, OSP_K_CALLER_TYPE, OSP_V_CALLER_TYPE_OSP);
613                 bundle_add(passData, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_DATACONTROL);
614                 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, reqtypestr.c_str());
615                 bundle_add(passData, OSP_K_DATACONTROL_PROVIDER, m_providerId.c_str());
616
617                 queryItem.push_back(dataId); // dataid
618                 queryItem.push_back(key); // key
619                 queryItem.push_back(value); // value
620
621                 addArrayToBundle(passData, queryItem);
622
623                 // FIXEME
624                 // reqid sholud be known
625                 EventAddValuePendingEvent* pendingEvent = new EventAddValuePendingEvent((void*)this, event);
626                 int pid = appsvc_run_service(passData, reqId, MappedDataControlAddValueCallback, (void*)pendingEvent);
627
628                 if (pid < 0) 
629                 {
630                         ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Error");
631                 }
632
633                 event->switchToManualAnswer();
634
635                 DPL::Mutex::ScopedLock lock(&m_mutex);
636                 m_currentReqIds.push_back(reqId);
637         }       
638         catch (const WrtDeviceApis::Commons::Exception& ex) 
639         {
640                 event->setExceptionCode(ex.getCode());
641                 event->setErrorMsg(ex.GetMessage());
642                 LogError("Exception: " << ex.GetMessage());
643                 
644                 
645         }
646         
647         if (passData)
648         {
649                 bundle_free(passData);
650                 passData = NULL;
651         }
652
653 }
654
655 void MappedDataControlConsumer::OnRequestReceived(const EventRemoveValuePtr& event)
656 {
657         LogDebug("Enter");
658         bundle* passData = NULL;
659         
660         try 
661         {
662                 std::string dataId = getDataId();
663                 std::vector<std::string> queryItem;
664                 std::string key = event->getKey();
665                 std::string value = event->getValue();
666                 
667                 unsigned int reqId = event->getReqId();
668                 std::stringstream ss, ssreqtype;
669                 std::string reqIdStr, reqtypestr;
670
671                 ss << reqId;
672                 reqIdStr = ss.str();
673
674                 ssreqtype << _DATACONTROL_REQUEST_TYPE_MAP_DELETE;
675                 reqtypestr = ssreqtype.str();
676
677                 if (checkReqIdUniqueness(reqId) == false)
678                 {
679                         ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
680                 }
681
682
683                 std::string appId = getApplicationId(m_providerId);
684                 passData = bundle_create();
685
686                 if (passData == NULL)
687                 {
688                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "ipc memory allocation fail");
689                 }
690
691                 appsvc_set_operation(passData, APPSVC_OPERATION_DEFAULT);
692                 appsvc_set_appid(passData, appId.c_str());
693
694                 bundle_add(passData, OSP_K_REQUEST_ID, reqIdStr.c_str());
695                 bundle_add(passData, OSP_K_CALLER_TYPE, OSP_V_CALLER_TYPE_OSP);
696                 bundle_add(passData, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_DATACONTROL);
697                 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, reqtypestr.c_str());
698                 bundle_add(passData, OSP_K_DATACONTROL_PROVIDER, m_providerId.c_str());
699
700                 queryItem.push_back(dataId); // dataid
701                 queryItem.push_back(key);
702                 queryItem.push_back(value);
703                 
704
705                 addArrayToBundle(passData, queryItem);
706                 EventRemoveValuePendingEvent* pendingEvent = new EventRemoveValuePendingEvent(this, event);
707
708                 int pid = appsvc_run_service(passData, reqId, MappedDataControlRemoveValueCallback, (void*)pendingEvent);
709
710                 if (pid < 0) 
711                 {
712                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "dataconstrol request fail.(can not launch)");
713                 }
714
715                 event->switchToManualAnswer();
716
717                 DPL::Mutex::ScopedLock lock(&m_mutex);
718                 m_currentReqIds.push_back(reqId);
719         }       
720         catch (const WrtDeviceApis::Commons::Exception& ex) 
721         {
722                 LogError("Exception: " << ex.GetMessage());
723                 event->setExceptionCode(ex.getCode());
724                 event->setErrorMsg(ex.GetMessage());
725         }
726         
727         if (passData)
728         {
729                 bundle_free(passData);
730                 passData = NULL;
731         }
732
733 }
734
735
736
737 void MappedDataControlConsumer::OnRequestReceived(const EventGetValuePtr& event)
738 {
739         LogDebug("Enter");
740         bundle* passData = NULL;
741         
742         try 
743         {
744                 std::string dataId = getDataId();
745                 std::vector<std::string> queryItem;
746                 
747                 unsigned int reqId = event->getReqId();
748                 std::stringstream ss, ssreqtype;
749                 std::string reqIdStr, reqtypestr;
750                 std::string key = event->getKey();
751
752                 if (checkReqIdUniqueness(reqId) == false)
753                 {
754                         ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
755                 }
756                 
757
758                 ss << reqId;
759                 reqIdStr = ss.str();
760
761                 ssreqtype << _DATACONTROL_REQUEST_TYPE_MAP_QUERY;
762                 reqtypestr = ssreqtype.str();
763
764                 std::string appId = getApplicationId(m_providerId);
765
766                 passData = bundle_create();
767
768                 if (passData == NULL)
769                 {
770                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "ipc memory allocation fail");
771                 }
772
773                 appsvc_set_operation(passData, APPSVC_OPERATION_DEFAULT);
774                 appsvc_set_appid(passData, appId.c_str());
775
776                 bundle_add(passData, OSP_K_REQUEST_ID, reqIdStr.c_str());
777                 bundle_add(passData, OSP_K_CALLER_TYPE, OSP_V_CALLER_TYPE_OSP);
778                 bundle_add(passData, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_DATACONTROL);
779                 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, reqtypestr.c_str());
780                 bundle_add(passData, OSP_K_DATACONTROL_PROVIDER, m_providerId.c_str());
781
782                 queryItem.push_back(dataId); // dataid
783                 queryItem.push_back(key);
784                 queryItem.push_back("1");
785                 queryItem.push_back("1");
786                 
787                 addArrayToBundle(passData, queryItem);
788                 EventGetValuePendingEvent* pendingEvent = new EventGetValuePendingEvent(this, event);
789                 
790                 int pid = appsvc_run_service(passData, reqId, MappedDataControlGetValueCallback, (void*)pendingEvent);
791
792                 if (pid < 0) 
793                 {
794                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "dataconstrol request fail.(can not launch)");
795                 }
796
797                 event->switchToManualAnswer();
798
799                 DPL::Mutex::ScopedLock lock(&m_mutex);          
800                 m_currentReqIds.push_back(reqId);
801         }       
802         catch (const WrtDeviceApis::Commons::Exception& ex) 
803         {
804                 LogError("Exception: " << ex.GetMessage());
805                 event->setExceptionCode(ex.getCode());
806                 event->setErrorMsg(ex.GetMessage());
807         }
808
809         if (passData)
810         {
811                 bundle_free(passData);
812                 passData = NULL;
813         }
814                 
815 }       
816
817 void MappedDataControlConsumer::OnRequestReceived(const EventUpdateValuePtr& event)
818 {
819         LogDebug("Enter");
820         bundle* passData = NULL;
821         
822         try 
823         {
824                 std::string dataId = getDataId();
825                 std::vector<std::string> queryItem;
826                 
827                 unsigned int reqId = event->getReqId();
828                 std::stringstream ss, ssreqtype;
829                 std::string reqIdStr, reqtypestr;
830                 std::string key = event->getKey();
831                 std::string oldValue = event->getOldValue();
832                 std::string newValue = event->getNewValue();
833
834                 if (checkReqIdUniqueness(reqId) == false)
835                 {
836                         ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
837                 }
838                                 
839                 std::string appId = getApplicationId(m_providerId);
840                 passData = bundle_create();
841
842                 if (passData == NULL)
843                 {
844                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "ipc memory allocation fail");
845                 }
846
847                 appsvc_set_operation(passData, APPSVC_OPERATION_DEFAULT);
848                 appsvc_set_appid(passData, appId.c_str());
849
850                 ss << reqId;
851                 reqIdStr = ss.str();
852
853                 ssreqtype << _DATACONTROL_REQUEST_TYPE_MAP_UPDATE;
854                 reqtypestr = ssreqtype.str();
855                 
856
857                 bundle_add(passData, OSP_K_REQUEST_ID, reqIdStr.c_str());
858                 bundle_add(passData, OSP_K_CALLER_TYPE, OSP_V_CALLER_TYPE_OSP);
859                 bundle_add(passData, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_DATACONTROL);
860                 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, reqtypestr.c_str());
861                 bundle_add(passData, OSP_K_DATACONTROL_PROVIDER, m_providerId.c_str());
862
863                 queryItem.push_back(dataId); // dataid
864                 queryItem.push_back(key);
865                 queryItem.push_back(oldValue);
866                 queryItem.push_back(newValue);
867
868                 addArrayToBundle(passData, queryItem);
869                 EventUpdateValuePendingEvent* pendingEvent = new EventUpdateValuePendingEvent(this, event);
870
871                 int pid = appsvc_run_service(passData, reqId, MappedDataControlUpdateValueCallback, (void*)pendingEvent);
872
873                 if (pid < 0) 
874                 {
875                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "dataconstrol request fail.(can not launch)");
876                 }
877
878                 event->switchToManualAnswer();
879
880                 DPL::Mutex::ScopedLock lock(&m_mutex);          
881                 m_currentReqIds.push_back(reqId);
882         }       
883         catch (const WrtDeviceApis::Commons::Exception& ex) 
884         {
885                 LogError("Exception: " << ex.GetMessage());
886                 event->setExceptionCode(ex.getCode());
887                 event->setErrorMsg(ex.GetMessage());
888         }
889
890         if (passData)
891         {
892                 bundle_free(passData);
893                 passData = NULL;
894         }
895
896
897 }
898
899
900
901 }
902 }
903
904