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