Change the header install path for c_common and logger
[platform/upstream/iotivity.git] / resource / src / InProcServerWrapper.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <random>
22 #include <cstring>
23 #include <cstdlib>
24 #include <iostream>
25 #include <algorithm>
26 #include <map>
27 #include <sstream>
28 #include <string>
29
30 #include <InProcServerWrapper.h>
31 #include <InitializeException.h>
32 #include <OCResourceRequest.h>
33 #include <OCResourceResponse.h>
34 #include <ocstack.h>
35 #include <ocpayload.h>
36
37 #include <OCApi.h>
38 #include <oic_malloc.h>
39 #include <OCPlatform.h>
40 #include <OCUtilities.h>
41 #include "logger.h"
42
43 #define TAG "OIC_SERVER_WRAPPER"
44
45 using namespace std;
46 using namespace OC;
47
48 namespace OC
49 {
50     namespace details
51     {
52         std::mutex serverWrapperLock;
53         std::map <OCResourceHandle, OC::EntityHandler>  entityHandlerMap;
54         std::map <OCResourceHandle, std::string> resourceUriMap;
55         EntityHandler defaultDeviceEntityHandler;
56     }
57 }
58
59 void formResourceRequest(OCEntityHandlerFlag flag,
60                          OCEntityHandlerRequest * entityHandlerRequest,
61                          std::shared_ptr<OCResourceRequest> pRequest)
62 {
63     if(pRequest && entityHandlerRequest)
64     {
65         pRequest->setRequestHandle(entityHandlerRequest->requestHandle);
66         pRequest->setResourceHandle(entityHandlerRequest->resource);
67         pRequest->setMessageID(entityHandlerRequest->messageID);
68     }
69
70     if(flag & OC_REQUEST_FLAG)
71     {
72         pRequest->setRequestHandlerFlag(OC::RequestHandlerFlag::RequestFlag);
73
74         if(entityHandlerRequest)
75         {
76             if(entityHandlerRequest->query)
77             {
78                 OC::Utilities::QueryParamsKeyVal qp = OC::Utilities::getQueryParams(
79                         entityHandlerRequest->query);
80
81                 if(qp.size() > 0)
82                 {
83                     pRequest->setQueryParams(qp);
84                 }
85             }
86             if(entityHandlerRequest->numRcvdVendorSpecificHeaderOptions != 0)
87             {
88                 //Set the header options here.
89                 uint16_t optionID;
90                 char optionData[MAX_HEADER_OPTION_DATA_LENGTH + 1];
91                 HeaderOptions headerOptions;
92
93                 optionData[MAX_HEADER_OPTION_DATA_LENGTH] = '\0';
94                 for(int i = 0;
95                     i < entityHandlerRequest->numRcvdVendorSpecificHeaderOptions;
96                     i++)
97                 {
98                     optionID = entityHandlerRequest->rcvdVendorSpecificHeaderOptions[i].optionID;
99                     memcpy(optionData, entityHandlerRequest->rcvdVendorSpecificHeaderOptions[i].optionData,
100                         MAX_HEADER_OPTION_DATA_LENGTH);
101                     HeaderOption::OCHeaderOption headerOption(optionID, optionData);
102                     headerOptions.push_back(headerOption);
103                 }
104                 pRequest->setHeaderOptions(headerOptions);
105             }
106
107             if(OC_REST_GET == entityHandlerRequest->method)
108             {
109                 pRequest->setRequestType(OC::PlatformCommands::GET);
110             }
111             else if(OC_REST_PUT == entityHandlerRequest->method)
112             {
113                 pRequest->setRequestType(OC::PlatformCommands::PUT);
114                 pRequest->setPayload(entityHandlerRequest->payload);
115             }
116             else if(OC_REST_POST == entityHandlerRequest->method)
117             {
118                 pRequest->setRequestType(OC::PlatformCommands::POST);
119                 pRequest->setPayload(entityHandlerRequest->payload);
120             }
121             else if(OC_REST_DELETE == entityHandlerRequest->method)
122             {
123                 pRequest->setRequestType(OC::PlatformCommands::DELETE);
124             }
125         }
126     }
127
128     if(flag & OC_OBSERVE_FLAG)
129     {
130         pRequest->setRequestHandlerFlag(
131                    OC::RequestHandlerFlag::RequestFlag | OC::RequestHandlerFlag::ObserverFlag);
132
133         if(entityHandlerRequest)
134         {
135             OC::ObservationInfo observationInfo;
136             observationInfo.action = (OC::ObserveAction) entityHandlerRequest->obsInfo.action;
137             observationInfo.obsId = entityHandlerRequest->obsInfo.obsId;
138
139             observationInfo.connectivityType = static_cast<OCConnectivityType>(
140                     (entityHandlerRequest->devAddr.adapter << CT_ADAPTER_SHIFT) |
141                     (entityHandlerRequest->devAddr.flags & CT_MASK_FLAGS));
142             observationInfo.address = entityHandlerRequest->devAddr.addr;
143             observationInfo.port = entityHandlerRequest->devAddr.port;
144             pRequest->setObservationInfo(observationInfo);
145         }
146     }
147 }
148
149 OCEntityHandlerResult DefaultEntityHandlerWrapper(OCEntityHandlerFlag flag,
150                                                   OCEntityHandlerRequest * entityHandlerRequest,
151                                                   char* uri,
152                                                   void * /*callbackParam*/)
153 {
154     OCEntityHandlerResult result = OC_EH_ERROR;
155
156     OC::oclog() << "In Default device entity handler wrapper";
157
158     if(NULL == entityHandlerRequest)
159     {
160         oclog() << "Entity handler request is NULL.";
161         return OC_EH_ERROR;
162     }
163
164     auto pRequest = std::make_shared<OC::OCResourceRequest>();
165
166     formResourceRequest(flag, entityHandlerRequest, pRequest);
167
168     pRequest->setResourceUri(std::string(uri));
169
170     EntityHandler defHandler;
171     {
172         std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
173         defHandler = OC::details::defaultDeviceEntityHandler;
174     }
175
176     if(defHandler)
177     {
178         result = defHandler(pRequest);
179     }
180     else
181     {
182         oclog() << "Default device entity handler was not set.";
183         return OC_EH_ERROR;
184     }
185
186     return result;
187 }
188
189
190 OCEntityHandlerResult EntityHandlerWrapper(OCEntityHandlerFlag flag,
191                                            OCEntityHandlerRequest * entityHandlerRequest,
192                                            void* /*callbackParam*/)
193 {
194     OCEntityHandlerResult result = OC_EH_ERROR;
195
196     oclog() << "\nIn entity handler wrapper: " << endl;
197
198     if(NULL == entityHandlerRequest)
199     {
200         oclog() << "Entity handler request is NULL."  << endl;
201         return OC_EH_ERROR;
202     }
203
204     auto pRequest = std::make_shared<OC::OCResourceRequest>();
205
206     formResourceRequest(flag, entityHandlerRequest, pRequest);
207
208     std::map <OCResourceHandle, std::string>::iterator resourceUriEntry;
209     std::map <OCResourceHandle, std::string>::iterator resourceUriEnd;
210     {
211         std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
212         resourceUriEntry = OC::details::resourceUriMap.find(entityHandlerRequest->resource);
213         resourceUriEnd = OC::details::resourceUriMap.end();
214     }
215     // Finding the corresponding URI for a resource handle and set the URI in the request
216     if(resourceUriEntry != resourceUriEnd)
217     {
218         pRequest->setResourceUri(resourceUriEntry->second);
219     }
220     else
221     {
222         oclog() << "Resource handle not found; Resource URI not set in request";
223         return OC_EH_ERROR;
224     }
225
226     std::map <OCResourceHandle, OC::EntityHandler>::iterator entityHandlerEntry;
227     std::map <OCResourceHandle, OC::EntityHandler>::iterator entityHandlerEnd;
228     {
229         // Finding the corresponding CPP Application entityHandler for a given resource
230         std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
231         entityHandlerEntry = OC::details::entityHandlerMap.find(entityHandlerRequest->resource);
232         entityHandlerEnd = OC::details::entityHandlerMap.end();
233     }
234
235     if(entityHandlerEntry != entityHandlerEnd)
236     {
237         // Call CPP Application Entity Handler
238         if(entityHandlerEntry->second)
239         {
240             result = entityHandlerEntry->second(pRequest);
241         }
242         else
243         {
244             oclog() << "C stack should not call again for parent resource\n";
245             return OC_EH_ERROR;
246         }
247     }
248     else
249     {
250         oclog() << "No entity handler found."  << endl;
251         return OC_EH_ERROR;
252     }
253
254     return result;
255 }
256
257 namespace OC
258 {
259     InProcServerWrapper::InProcServerWrapper(
260         std::weak_ptr<std::recursive_mutex> csdkLock, PlatformConfig cfg)
261      : m_threadRun(false),
262        m_csdkLock(csdkLock),
263        m_cfg { cfg }
264     {
265         start();
266     }
267
268     OCStackResult InProcServerWrapper::start()
269     {
270         OIC_LOG_V(INFO, TAG, "start ocplatform for server : %d", m_cfg.transportType);
271
272         OCMode initType;
273         if(m_cfg.mode == ModeType::Server)
274         {
275             initType = OC_SERVER;
276         }
277         else if (m_cfg.mode == ModeType::Both)
278         {
279             initType = OC_CLIENT_SERVER;
280         }
281         else if (m_cfg.mode == ModeType::Gateway)
282         {
283             initType = OC_GATEWAY;
284         }
285         else
286         {
287             throw InitializeException(OC::InitException::NOT_CONFIGURED_AS_SERVER,
288                                          OC_STACK_INVALID_PARAM);
289         }
290
291         OCTransportFlags serverFlags =
292                             static_cast<OCTransportFlags>(m_cfg.serverConnectivity & CT_MASK_FLAGS);
293         OCTransportFlags clientFlags =
294                             static_cast<OCTransportFlags>(m_cfg.clientConnectivity & CT_MASK_FLAGS);
295         OCStackResult result = OCInit2(initType, serverFlags, clientFlags,
296                                        m_cfg.transportType);
297
298         if(OC_STACK_OK != result)
299         {
300             throw InitializeException(OC::InitException::STACK_INIT_ERROR, result);
301         }
302
303         if (false == m_threadRun)
304         {
305             m_threadRun = true;
306 #ifdef WITH_PROCESS_EVENT
307             m_processEvent = oc_event_new();
308             if (!m_processEvent)
309             {
310                 OIC_LOG(INFO, TAG, "oc_event_new failed!");
311                 return OC_STACK_ERROR;
312             }
313             OCRegisterProcessEvent(m_processEvent);
314 #endif
315             m_processThread = std::thread(&InProcServerWrapper::processFunc, this);
316         }
317         return OC_STACK_OK;
318     }
319
320     OCStackResult InProcServerWrapper::stop()
321     {
322         OIC_LOG(INFO, TAG, "stop");
323
324         if(m_processThread.joinable())
325         {
326             m_threadRun = false;
327 #ifdef WITH_PROCESS_EVENT
328             if (m_processEvent)
329             {
330                 oc_event_signal(m_processEvent);
331             }
332 #endif
333             m_processThread.join();
334         }
335
336 #ifdef WITH_PROCESS_EVENT
337         if (m_processEvent)
338         {
339             oc_event_free(m_processEvent);
340             m_processEvent = NULL;
341         }
342 #endif
343
344         OCStackResult res = OCStop();
345
346         if (OC_STACK_OK != res)
347         {
348            throw InitializeException(OC::InitException::STACK_TERMINATE_ERROR, res);
349         }
350
351         return OC_STACK_OK;
352     }
353
354     void InProcServerWrapper::processFunc()
355     {
356 #ifdef WITH_PROCESS_EVENT
357         uint32_t nextEventTime;
358 #endif
359         auto cLock = m_csdkLock.lock();
360         while(cLock && m_threadRun)
361         {
362             OCStackResult result;
363
364             {
365                 std::lock_guard<std::recursive_mutex> lock(*cLock);
366 #ifdef WITH_PROCESS_EVENT
367                 result = OCProcessEvent(&nextEventTime);
368 #else
369                 result = OCProcess();
370 #endif
371             }
372
373             if(OC_STACK_ERROR == result)
374             {
375                 oclog() << "OCProcess failed with result " << result <<std::flush;
376                 // ...the value of variable result is simply ignored for now.
377             }
378
379  #ifdef WITH_PROCESS_EVENT
380             oc_event_wait_for(m_processEvent, nextEventTime);
381 #else
382             std::this_thread::sleep_for(std::chrono::milliseconds(10));
383 #endif
384         }
385     }
386
387     OCStackResult InProcServerWrapper::registerDeviceInfo(const OCDeviceInfo deviceInfo)
388     {
389         auto cLock = m_csdkLock.lock();
390         OCStackResult result = OC_STACK_ERROR;
391         if(cLock)
392         {
393             std::lock_guard<std::recursive_mutex> lock(*cLock);
394             result = OCSetDeviceInfo(deviceInfo);
395         }
396         return result;
397     }
398
399     OCStackResult InProcServerWrapper::registerPlatformInfo(const OCPlatformInfo platformInfo)
400     {
401         auto cLock = m_csdkLock.lock();
402         OCStackResult result = OC_STACK_ERROR;
403         if(cLock)
404         {
405             std::lock_guard<std::recursive_mutex> lock(*cLock);
406             result = OCSetPlatformInfo(platformInfo);
407         }
408         return result;
409     }
410
411     OCStackResult InProcServerWrapper::setPropertyValue(OCPayloadType type, const std::string& propName,
412         const std::string& propValue)
413     {
414         auto cLock = m_csdkLock.lock();
415         OCStackResult result = OC_STACK_ERROR;
416         if (cLock)
417         {
418             std::lock_guard<std::recursive_mutex> lock(*cLock);
419             result = OCSetPropertyValue(type, propName.c_str(), (void *)propValue.c_str());
420         }
421         return result;
422     }
423
424     OCStackResult InProcServerWrapper::getPropertyValue(OCPayloadType type, const std::string& propName,
425         std::string& propValue)
426     {
427         auto cLock = m_csdkLock.lock();
428         OCStackResult result = OC_STACK_ERROR;
429         if (cLock)
430         {
431             std::lock_guard<std::recursive_mutex> lock(*cLock);
432             result = OCGetPropertyValue(type, propName.c_str(), (void **)propValue.c_str());
433         }
434         return result;
435     }
436
437     OCStackResult InProcServerWrapper::registerResource(
438                     OCResourceHandle& resourceHandle,
439                     std::string& resourceURI,
440                     const std::string& resourceTypeName,
441                     const std::string& resourceInterface,
442                     EntityHandler& eHandler,
443                     uint8_t resourceProperties)
444
445     {
446         OCStackResult result = OC_STACK_ERROR;
447
448         auto cLock = m_csdkLock.lock();
449
450         if(cLock)
451         {
452             std::lock_guard<std::recursive_mutex> lock(*cLock);
453
454             if(NULL != eHandler)
455             {
456                 result = OCCreateResource(&resourceHandle, // OCResourceHandle *handle
457                             resourceTypeName.c_str(), // const char * resourceTypeName
458                             resourceInterface.c_str(), //const char * resourceInterfaceName //TODO fix this
459                             resourceURI.c_str(), // const char * uri
460                             EntityHandlerWrapper, // OCEntityHandler entityHandler
461                             NULL,
462                             resourceProperties // uint8_t resourceProperties
463                             );
464             }
465             else
466             {
467                 result = OCCreateResource(&resourceHandle, // OCResourceHandle *handle
468                             resourceTypeName.c_str(), // const char * resourceTypeName
469                             resourceInterface.c_str(), //const char * resourceInterfaceName //TODO fix this
470                             resourceURI.c_str(), // const char * uri
471                             NULL, // OCEntityHandler entityHandler
472                             NULL,
473                             resourceProperties // uint8_t resourceProperties
474                             );
475             }
476
477             if(result != OC_STACK_OK)
478             {
479                 resourceHandle = (OCResourceHandle) 0;
480             }
481             else
482             {
483                 std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
484                 OC::details::entityHandlerMap[resourceHandle] = eHandler;
485                 OC::details::resourceUriMap[resourceHandle] = resourceURI;
486             }
487         }
488         else
489         {
490             result = OC_STACK_ERROR;
491         }
492
493         return result;
494     }
495
496
497
498     OCStackResult InProcServerWrapper::setDefaultDeviceEntityHandler
499                                         (EntityHandler entityHandler)
500     {
501         OCStackResult result = OC_STACK_ERROR;
502
503         {
504             std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
505             OC::details::defaultDeviceEntityHandler = entityHandler;
506         }
507
508         if(entityHandler)
509         {
510             result = OCSetDefaultDeviceEntityHandler(DefaultEntityHandlerWrapper, NULL);
511         }
512         else
513         {
514             // If Null passed we unset
515             result = OCSetDefaultDeviceEntityHandler(NULL, NULL);
516         }
517
518         return result;
519     }
520
521     OCStackResult InProcServerWrapper::unregisterResource(const OCResourceHandle& resourceHandle)
522     {
523         auto cLock = m_csdkLock.lock();
524         OCStackResult result = OC_STACK_ERROR;
525
526         if(cLock)
527         {
528             std::lock_guard<std::recursive_mutex> lock(*cLock);
529             result = OCDeleteResource(resourceHandle);
530
531             if(result == OC_STACK_OK)
532             {
533                 std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
534                 OC::details::resourceUriMap.erase(resourceHandle);
535             }
536             else
537             {
538                 throw OCException(OC::Exception::RESOURCE_UNREG_FAILED, result);
539             }
540         }
541         else
542         {
543             result = OC_STACK_ERROR;
544         }
545
546         return result;
547     }
548
549     OCStackResult InProcServerWrapper::bindTypeToResource(const OCResourceHandle& resourceHandle,
550                      const std::string& resourceTypeName)
551     {
552         auto cLock = m_csdkLock.lock();
553         OCStackResult result;
554         if(cLock)
555         {
556             std::lock_guard<std::recursive_mutex> lock(*cLock);
557             result = OCBindResourceTypeToResource(resourceHandle, resourceTypeName.c_str());
558         }
559         else
560         {
561             result = OC_STACK_ERROR;
562         }
563
564         if (result != OC_STACK_OK)
565         {
566             throw OCException(OC::Exception::BIND_TYPE_FAILED, result);
567         }
568         return result;
569     }
570
571     OCStackResult InProcServerWrapper::resetResourceTypes(const OCResourceHandle& resourceHandle,
572                      const std::string& newResourceType)
573     {
574         auto cLock = m_csdkLock.lock();
575         OCStackResult result;
576         if(cLock)
577         {
578             std::lock_guard<std::recursive_mutex> lock(*cLock);
579             result = OCResetResourceTypes(resourceHandle, newResourceType.c_str());
580         }
581         else
582         {
583             result = OC_STACK_ERROR;
584         }
585
586         if (result != OC_STACK_OK)
587         {
588             throw OCException(OC::Exception::BIND_TYPE_FAILED, result);
589         }
590         return result;
591     }
592
593     OCStackResult InProcServerWrapper::bindInterfaceToResource(
594                      const OCResourceHandle& resourceHandle,
595                      const std::string& resourceInterfaceName)
596     {
597         auto cLock = m_csdkLock.lock();
598         OCStackResult result;
599         if(cLock)
600         {
601             std::lock_guard<std::recursive_mutex> lock(*cLock);
602             result = OCBindResourceInterfaceToResource(resourceHandle,
603                         resourceInterfaceName.c_str());
604         }
605         else
606         {
607             result = OC_STACK_ERROR;
608         }
609
610         if (result != OC_STACK_OK)
611         {
612             throw OCException(OC::Exception::BIND_INTERFACE_FAILED, result);
613         }
614         return result;
615     }
616
617     OCStackResult InProcServerWrapper::resetResourceInterfaces(
618                      const OCResourceHandle& resourceHandle,
619                      const std::string& newResourceInterface)
620     {
621         auto cLock = m_csdkLock.lock();
622         OCStackResult result;
623         if(cLock)
624         {
625             std::lock_guard<std::recursive_mutex> lock(*cLock);
626             result = OCResetResourceInterfaces(resourceHandle,
627                         newResourceInterface.c_str());
628         }
629         else
630         {
631             result = OC_STACK_ERROR;
632         }
633
634         if (result != OC_STACK_OK)
635         {
636             throw OCException(OC::Exception::BIND_INTERFACE_FAILED, result);
637         }
638         return result;
639     }
640
641     OCStackResult InProcServerWrapper::startPresence(const unsigned int seconds)
642     {
643 #ifdef WITH_PRESENCE
644         auto cLock = m_csdkLock.lock();
645         OCStackResult result = OC_STACK_ERROR;
646         if(cLock)
647         {
648             std::lock_guard<std::recursive_mutex> lock(*cLock);
649             result = OCStartPresence(seconds);
650         }
651
652         if(result != OC_STACK_OK)
653         {
654             throw OCException(OC::Exception::START_PRESENCE_FAILED, result);
655         }
656         return result;
657 #else
658         return OC_STACK_NOT_IMPLEMENTED;
659 #endif
660     }
661
662     OCStackResult InProcServerWrapper::stopPresence()
663     {
664 #ifdef WITH_PRESENCE
665         auto cLock = m_csdkLock.lock();
666         OCStackResult result = OC_STACK_ERROR;
667         if(cLock)
668         {
669             std::lock_guard<std::recursive_mutex> lock(*cLock);
670             result = OCStopPresence();
671         }
672
673         if(result != OC_STACK_OK)
674         {
675             throw OCException(OC::Exception::END_PRESENCE_FAILED, result);
676         }
677         return result;
678 #else
679         return OC_STACK_NOT_IMPLEMENTED;
680 #endif
681     }
682
683     OCStackResult InProcServerWrapper::sendResponse(
684         const std::shared_ptr<OCResourceResponse> pResponse)
685     {
686         auto cLock = m_csdkLock.lock();
687         OCStackResult result = OC_STACK_ERROR;
688
689         if(!pResponse)
690         {
691             result = OC_STACK_MALFORMED_RESPONSE;
692             throw OCException(OC::Exception::STR_NULL_RESPONSE, OC_STACK_MALFORMED_RESPONSE);
693         }
694         else
695         {
696             OCEntityHandlerResponse response;
697 //            OCRepPayload* payLoad = pResponse->getPayload();
698             HeaderOptions serverHeaderOptions = pResponse->getHeaderOptions();
699
700             response.requestHandle = pResponse->getRequestHandle();
701             response.resourceHandle = pResponse->getResourceHandle();
702             response.ehResult = pResponse->getResponseResult();
703
704             response.payload = reinterpret_cast<OCPayload*>(pResponse->getPayload());
705
706             response.persistentBufferFlag = 0;
707
708             response.numSendVendorSpecificHeaderOptions = serverHeaderOptions.size();
709             int i = 0;
710             for (auto it=serverHeaderOptions.begin(); it != serverHeaderOptions.end(); ++it)
711             {
712                 response.sendVendorSpecificHeaderOptions[i].protocolID = OC_COAP_ID;
713                 response.sendVendorSpecificHeaderOptions[i].optionID =
714                     static_cast<uint16_t>(it->getOptionID());
715                 response.sendVendorSpecificHeaderOptions[i].optionLength =
716                     (it->getOptionData()).length() + 1;
717                 std::string optionData = it->getOptionData();
718                 std::copy(optionData.begin(),
719                          optionData.end(),
720                          response.sendVendorSpecificHeaderOptions[i].optionData);
721                 response.sendVendorSpecificHeaderOptions[i].optionData[it->getOptionData().length()]
722                     = '\0';
723                 i++;
724             }
725
726             if(OC_EH_RESOURCE_CREATED == response.ehResult)
727             {
728                 pResponse->getNewResourceUri().copy(response.resourceUri,
729                         sizeof (response.resourceUri) - 1);
730                 response.resourceUri[pResponse->getNewResourceUri().length()] = '\0';
731             }
732
733             if(cLock)
734             {
735                 std::lock_guard<std::recursive_mutex> lock(*cLock);
736                 result = OCDoResponse(&response);
737             }
738             else
739             {
740                 result = OC_STACK_ERROR;
741             }
742
743             if(result != OC_STACK_OK)
744             {
745                 oclog() << "Error sending response\n";
746             }
747
748             OCPayloadDestroy(response.payload);
749             return result;
750         }
751     }
752
753     OCStackResult InProcServerWrapper::notifyAllObservers(OCResourceHandle resourceHandle,
754                                                           QualityOfService QoS)
755     {
756         auto cLock = m_csdkLock.lock();
757         OCStackResult result = OC_STACK_ERROR;
758         if (cLock)
759         {
760             std::lock_guard<std::recursive_mutex> lock(*cLock);
761             result = OCNotifyAllObservers(resourceHandle, static_cast<OCQualityOfService>(QoS));
762         }
763
764         return result;
765     }
766
767     OCStackResult InProcServerWrapper::notifyListOfObservers(OCResourceHandle resourceHandle,
768                                                ObservationIds& observationIds,
769                                                const std::shared_ptr<OCResourceResponse> pResponse,
770                                                QualityOfService QoS)
771     {
772         if (!pResponse)
773         {
774             return OC_STACK_ERROR;
775         }
776
777         auto cLock = m_csdkLock.lock();
778         OCStackResult result = OC_STACK_ERROR;
779         if (cLock)
780         {
781             std::lock_guard<std::recursive_mutex> lock(*cLock);
782
783             OCRepPayload* pl = pResponse->getResourceRepresentation().getPayload();
784             result = OCNotifyListOfObservers(resourceHandle,
785                                              &observationIds[0],
786                                              observationIds.size(),
787                                              pl,
788                                              static_cast<OCQualityOfService>(QoS));
789             OCRepPayloadDestroy(pl);
790         }
791
792         if (result != OC_STACK_OK)
793         {
794             throw OCException(OC::Exception::NOTIFY_LIST_OBSERVERS_FAILED, result);
795         }
796         return result;
797     }
798
799     InProcServerWrapper::~InProcServerWrapper()
800     {
801         try
802         {
803             stop();
804         }
805         catch (InitializeException &e)
806         {
807             OIC_LOG_V(INFO, TAG, "Exception in stop (%s)", e.what());
808         }
809     }
810 }