Remove resource directory dependency in lib OC
[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
42 using namespace std;
43 using namespace OC;
44
45 namespace OC
46 {
47     namespace details
48     {
49         std::mutex serverWrapperLock;
50         std::map <OCResourceHandle, OC::EntityHandler>  entityHandlerMap;
51         std::map <OCResourceHandle, std::string> resourceUriMap;
52         EntityHandler defaultDeviceEntityHandler;
53     }
54 }
55
56 void formResourceRequest(OCEntityHandlerFlag flag,
57                          OCEntityHandlerRequest * entityHandlerRequest,
58                          std::shared_ptr<OCResourceRequest> pRequest)
59 {
60     if(pRequest && entityHandlerRequest)
61     {
62         pRequest->setRequestHandle(entityHandlerRequest->requestHandle);
63         pRequest->setResourceHandle(entityHandlerRequest->resource);
64         pRequest->setMessageID(entityHandlerRequest->messageID);
65     }
66
67     if(flag & OC_REQUEST_FLAG)
68     {
69         pRequest->setRequestHandlerFlag(OC::RequestHandlerFlag::RequestFlag);
70
71         if(entityHandlerRequest)
72         {
73             if(entityHandlerRequest->query)
74             {
75                 OC::Utilities::QueryParamsKeyVal qp = OC::Utilities::getQueryParams(
76                         entityHandlerRequest->query);
77
78                 if(qp.size() > 0)
79                 {
80                     pRequest->setQueryParams(qp);
81                 }
82             }
83             if(entityHandlerRequest->numRcvdVendorSpecificHeaderOptions != 0)
84             {
85                 //Set the header options here.
86                 uint16_t optionID;
87                 std::string optionData;
88                 HeaderOptions headerOptions;
89
90                 for(int i = 0;
91                     i < entityHandlerRequest->numRcvdVendorSpecificHeaderOptions;
92                     i++)
93                 {
94                     optionID = entityHandlerRequest->rcvdVendorSpecificHeaderOptions[i].optionID;
95                     optionData = reinterpret_cast<const char*>
96                              (entityHandlerRequest->rcvdVendorSpecificHeaderOptions[i].optionData);
97                     HeaderOption::OCHeaderOption headerOption(optionID, optionData);
98                     headerOptions.push_back(headerOption);
99                 }
100                 pRequest->setHeaderOptions(headerOptions);
101             }
102
103             if(OC_REST_GET == entityHandlerRequest->method)
104             {
105                 pRequest->setRequestType(OC::PlatformCommands::GET);
106             }
107             else if(OC_REST_PUT == entityHandlerRequest->method)
108             {
109                 pRequest->setRequestType(OC::PlatformCommands::PUT);
110                 pRequest->setPayload(entityHandlerRequest->payload);
111             }
112             else if(OC_REST_POST == entityHandlerRequest->method)
113             {
114                 pRequest->setRequestType(OC::PlatformCommands::POST);
115                 pRequest->setPayload(entityHandlerRequest->payload);
116             }
117             else if(OC_REST_DELETE == entityHandlerRequest->method)
118             {
119                 pRequest->setRequestType(OC::PlatformCommands::DELETE);
120             }
121         }
122     }
123
124     if(flag & OC_OBSERVE_FLAG)
125     {
126         pRequest->setRequestHandlerFlag(
127                    OC::RequestHandlerFlag::RequestFlag | OC::RequestHandlerFlag::ObserverFlag);
128
129         if(entityHandlerRequest)
130         {
131             OC::ObservationInfo observationInfo;
132             observationInfo.action = (OC::ObserveAction) entityHandlerRequest->obsInfo.action;
133             observationInfo.obsId = entityHandlerRequest->obsInfo.obsId;
134
135             observationInfo.connectivityType = static_cast<OCConnectivityType>(
136                     (entityHandlerRequest->devAddr.adapter << CT_ADAPTER_SHIFT) |
137                     (entityHandlerRequest->devAddr.flags & CT_MASK_FLAGS));
138             observationInfo.address = entityHandlerRequest->devAddr.addr;
139             observationInfo.port = entityHandlerRequest->devAddr.port;
140             pRequest->setObservationInfo(observationInfo);
141         }
142     }
143 }
144
145 OCEntityHandlerResult DefaultEntityHandlerWrapper(OCEntityHandlerFlag flag,
146                                                   OCEntityHandlerRequest * entityHandlerRequest,
147                                                   char* uri,
148                                                   void * /*callbackParam*/)
149 {
150     OCEntityHandlerResult result = OC_EH_ERROR;
151
152     OC::oclog() << "In Default device entity handler wrapper";
153
154     if(NULL == entityHandlerRequest)
155     {
156         oclog() << "Entity handler request is NULL.";
157         return OC_EH_ERROR;
158     }
159
160     auto pRequest = std::make_shared<OC::OCResourceRequest>();
161
162     formResourceRequest(flag, entityHandlerRequest, pRequest);
163
164     pRequest->setResourceUri(std::string(uri));
165
166     EntityHandler defHandler;
167     {
168         std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
169         defHandler = OC::details::defaultDeviceEntityHandler;
170     }
171
172     if(defHandler)
173     {
174         result = defHandler(pRequest);
175     }
176     else
177     {
178         oclog() << "Default device entity handler was not set.";
179         return OC_EH_ERROR;
180     }
181
182     return result;
183 }
184
185
186 OCEntityHandlerResult EntityHandlerWrapper(OCEntityHandlerFlag flag,
187                                            OCEntityHandlerRequest * entityHandlerRequest,
188                                            void* /*callbackParam*/)
189 {
190     OCEntityHandlerResult result = OC_EH_ERROR;
191
192     oclog() << "\nIn entity handler wrapper: " << endl;
193
194     if(NULL == entityHandlerRequest)
195     {
196         oclog() << "Entity handler request is NULL."  << endl;
197         return OC_EH_ERROR;
198     }
199
200     auto pRequest = std::make_shared<OC::OCResourceRequest>();
201
202     formResourceRequest(flag, entityHandlerRequest, pRequest);
203
204     std::map <OCResourceHandle, std::string>::iterator resourceUriEntry;
205     std::map <OCResourceHandle, std::string>::iterator resourceUriEnd;
206     {
207         std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
208         resourceUriEntry = OC::details::resourceUriMap.find(entityHandlerRequest->resource);
209         resourceUriEnd = OC::details::resourceUriMap.end();
210     }
211     // Finding the corresponding URI for a resource handle and set the URI in the request
212     if(resourceUriEntry != resourceUriEnd)
213     {
214         pRequest->setResourceUri(resourceUriEntry->second);
215     }
216     else
217     {
218         oclog() << "Resource handle not found; Resource URI not set in request";
219         return OC_EH_ERROR;
220     }
221
222     std::map <OCResourceHandle, OC::EntityHandler>::iterator entityHandlerEntry;
223     std::map <OCResourceHandle, OC::EntityHandler>::iterator entityHandlerEnd;
224     {
225         // Finding the corresponding CPP Application entityHandler for a given resource
226         std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
227         entityHandlerEntry = OC::details::entityHandlerMap.find(entityHandlerRequest->resource);
228         entityHandlerEnd = OC::details::entityHandlerMap.end();
229     }
230
231     if(entityHandlerEntry != entityHandlerEnd)
232     {
233         // Call CPP Application Entity Handler
234         if(entityHandlerEntry->second)
235         {
236             result = entityHandlerEntry->second(pRequest);
237         }
238         else
239         {
240             oclog() << "C stack should not call again for parent resource\n";
241             return OC_EH_ERROR;
242         }
243     }
244     else
245     {
246         oclog() << "No entity handler found."  << endl;
247         return OC_EH_ERROR;
248     }
249
250     return result;
251 }
252
253 namespace OC
254 {
255     InProcServerWrapper::InProcServerWrapper(
256         std::weak_ptr<std::recursive_mutex> csdkLock, PlatformConfig cfg)
257      : m_csdkLock(csdkLock)
258     {
259         OCMode initType;
260
261         if(cfg.mode == ModeType::Server)
262         {
263             initType = OC_SERVER;
264         }
265         else if (cfg.mode == ModeType::Both)
266         {
267             initType = OC_CLIENT_SERVER;
268         }
269         else if (cfg.mode == ModeType::Gateway)
270         {
271             initType = OC_GATEWAY;
272         }
273         else
274         {
275             throw InitializeException(OC::InitException::NOT_CONFIGURED_AS_SERVER,
276                                          OC_STACK_INVALID_PARAM);
277         }
278
279         OCTransportFlags serverFlags =
280                             static_cast<OCTransportFlags>(cfg.serverConnectivity & CT_MASK_FLAGS);
281         OCTransportFlags clientFlags =
282                             static_cast<OCTransportFlags>(cfg.clientConnectivity & CT_MASK_FLAGS);
283         OCStackResult result = OCInit1(initType, serverFlags, clientFlags);
284
285         if(OC_STACK_OK != result)
286         {
287             throw InitializeException(OC::InitException::STACK_INIT_ERROR, result);
288         }
289
290         m_threadRun = true;
291         m_processThread = std::thread(&InProcServerWrapper::processFunc, this);
292     }
293
294     void InProcServerWrapper::processFunc()
295     {
296         auto cLock = m_csdkLock.lock();
297         while(cLock && m_threadRun)
298         {
299             OCStackResult result;
300
301             {
302                 std::lock_guard<std::recursive_mutex> lock(*cLock);
303                 result = OCProcess();
304             }
305
306             if(OC_STACK_ERROR == result)
307             {
308                 oclog() << "OCProcess failed with result " << result <<std::flush;
309                 // ...the value of variable result is simply ignored for now.
310             }
311
312             std::this_thread::sleep_for(std::chrono::milliseconds(10));
313         }
314     }
315
316     OCStackResult InProcServerWrapper::registerDeviceInfo(const OCDeviceInfo deviceInfo)
317     {
318         auto cLock = m_csdkLock.lock();
319         OCStackResult result = OC_STACK_ERROR;
320         if(cLock)
321         {
322             std::lock_guard<std::recursive_mutex> lock(*cLock);
323             result = OCSetDeviceInfo(deviceInfo);
324         }
325         return result;
326     }
327
328     OCStackResult InProcServerWrapper::registerPlatformInfo(const OCPlatformInfo platformInfo)
329     {
330         auto cLock = m_csdkLock.lock();
331         OCStackResult result = OC_STACK_ERROR;
332         if(cLock)
333         {
334             std::lock_guard<std::recursive_mutex> lock(*cLock);
335             result = OCSetPlatformInfo(platformInfo);
336         }
337         return result;
338     }
339
340     OCStackResult InProcServerWrapper::registerResource(
341                     OCResourceHandle& resourceHandle,
342                     std::string& resourceURI,
343                     const std::string& resourceTypeName,
344                     const std::string& resourceInterface,
345                     EntityHandler& eHandler,
346                     uint8_t resourceProperties)
347
348     {
349         OCStackResult result = OC_STACK_ERROR;
350
351         auto cLock = m_csdkLock.lock();
352
353         if(cLock)
354         {
355             std::lock_guard<std::recursive_mutex> lock(*cLock);
356
357             if(NULL != eHandler)
358             {
359                 result = OCCreateResource(&resourceHandle, // OCResourceHandle *handle
360                             resourceTypeName.c_str(), // const char * resourceTypeName
361                             resourceInterface.c_str(), //const char * resourceInterfaceName //TODO fix this
362                             resourceURI.c_str(), // const char * uri
363                             EntityHandlerWrapper, // OCEntityHandler entityHandler
364                             NULL,
365                             resourceProperties // uint8_t resourceProperties
366                             );
367             }
368             else
369             {
370                 result = OCCreateResource(&resourceHandle, // OCResourceHandle *handle
371                             resourceTypeName.c_str(), // const char * resourceTypeName
372                             resourceInterface.c_str(), //const char * resourceInterfaceName //TODO fix this
373                             resourceURI.c_str(), // const char * uri
374                             NULL, // OCEntityHandler entityHandler
375                             NULL,
376                             resourceProperties // uint8_t resourceProperties
377                             );
378             }
379
380             if(result != OC_STACK_OK)
381             {
382                 resourceHandle = (OCResourceHandle) 0;
383             }
384             else
385             {
386                 std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
387                 OC::details::entityHandlerMap[resourceHandle] = eHandler;
388                 OC::details::resourceUriMap[resourceHandle] = resourceURI;
389             }
390         }
391         else
392         {
393             result = OC_STACK_ERROR;
394         }
395
396         return result;
397     }
398
399
400
401     OCStackResult InProcServerWrapper::setDefaultDeviceEntityHandler
402                                         (EntityHandler entityHandler)
403     {
404         OCStackResult result = OC_STACK_ERROR;
405
406         {
407             std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
408             OC::details::defaultDeviceEntityHandler = entityHandler;
409         }
410
411         if(entityHandler)
412         {
413             result = OCSetDefaultDeviceEntityHandler(DefaultEntityHandlerWrapper, NULL);
414         }
415         else
416         {
417             // If Null passed we unset
418             result = OCSetDefaultDeviceEntityHandler(NULL, NULL);
419         }
420
421         return result;
422     }
423
424     OCStackResult InProcServerWrapper::unregisterResource(const OCResourceHandle& resourceHandle)
425     {
426         auto cLock = m_csdkLock.lock();
427         OCStackResult result = OC_STACK_ERROR;
428
429         if(cLock)
430         {
431             std::lock_guard<std::recursive_mutex> lock(*cLock);
432             result = OCDeleteResource(resourceHandle);
433
434             if(result == OC_STACK_OK)
435             {
436                 std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
437                 OC::details::resourceUriMap.erase(resourceHandle);
438             }
439             else
440             {
441                 throw OCException(OC::Exception::RESOURCE_UNREG_FAILED, result);
442             }
443         }
444         else
445         {
446             result = OC_STACK_ERROR;
447         }
448
449         return result;
450     }
451
452     OCStackResult InProcServerWrapper::bindTypeToResource(const OCResourceHandle& resourceHandle,
453                      const std::string& resourceTypeName)
454     {
455         auto cLock = m_csdkLock.lock();
456         OCStackResult result;
457         if(cLock)
458         {
459             std::lock_guard<std::recursive_mutex> lock(*cLock);
460             result = OCBindResourceTypeToResource(resourceHandle, resourceTypeName.c_str());
461         }
462         else
463         {
464             result = OC_STACK_ERROR;
465         }
466
467         if (result != OC_STACK_OK)
468         {
469             throw OCException(OC::Exception::BIND_TYPE_FAILED, result);
470         }
471         return result;
472     }
473
474     OCStackResult InProcServerWrapper::bindInterfaceToResource(
475                      const OCResourceHandle& resourceHandle,
476                      const std::string& resourceInterfaceName)
477     {
478         auto cLock = m_csdkLock.lock();
479         OCStackResult result;
480         if(cLock)
481         {
482             std::lock_guard<std::recursive_mutex> lock(*cLock);
483             result = OCBindResourceInterfaceToResource(resourceHandle,
484                         resourceInterfaceName.c_str());
485         }
486         else
487         {
488             result = OC_STACK_ERROR;
489         }
490
491         if (result != OC_STACK_OK)
492         {
493             throw OCException(OC::Exception::BIND_INTERFACE_FAILED, result);
494         }
495         return result;
496     }
497
498     OCStackResult InProcServerWrapper::startPresence(const unsigned int seconds)
499     {
500         auto cLock = m_csdkLock.lock();
501         OCStackResult result = OC_STACK_ERROR;
502         if(cLock)
503         {
504             std::lock_guard<std::recursive_mutex> lock(*cLock);
505             result = OCStartPresence(seconds);
506         }
507
508         if(result != OC_STACK_OK)
509         {
510             throw OCException(OC::Exception::START_PRESENCE_FAILED, result);
511         }
512         return result;
513     }
514
515     OCStackResult InProcServerWrapper::stopPresence()
516     {
517         auto cLock = m_csdkLock.lock();
518         OCStackResult result = OC_STACK_ERROR;
519         if(cLock)
520         {
521             std::lock_guard<std::recursive_mutex> lock(*cLock);
522             result = OCStopPresence();
523         }
524
525         if(result != OC_STACK_OK)
526         {
527             throw OCException(OC::Exception::END_PRESENCE_FAILED, result);
528         }
529         return result;
530     }
531
532     OCStackResult InProcServerWrapper::sendResponse(
533         const std::shared_ptr<OCResourceResponse> pResponse)
534     {
535         auto cLock = m_csdkLock.lock();
536         OCStackResult result = OC_STACK_ERROR;
537
538         if(!pResponse)
539         {
540             result = OC_STACK_MALFORMED_RESPONSE;
541             throw OCException(OC::Exception::STR_NULL_RESPONSE, OC_STACK_MALFORMED_RESPONSE);
542         }
543         else
544         {
545             OCEntityHandlerResponse response;
546 //            OCRepPayload* payLoad = pResponse->getPayload();
547             HeaderOptions serverHeaderOptions = pResponse->getHeaderOptions();
548
549             response.requestHandle = pResponse->getRequestHandle();
550             response.resourceHandle = pResponse->getResourceHandle();
551             response.ehResult = pResponse->getResponseResult();
552
553             response.payload = reinterpret_cast<OCPayload*>(pResponse->getPayload());
554
555             response.persistentBufferFlag = 0;
556
557             response.numSendVendorSpecificHeaderOptions = serverHeaderOptions.size();
558             int i = 0;
559             for (auto it=serverHeaderOptions.begin(); it != serverHeaderOptions.end(); ++it)
560             {
561                 response.sendVendorSpecificHeaderOptions[i].protocolID = OC_COAP_ID;
562                 response.sendVendorSpecificHeaderOptions[i].optionID =
563                     static_cast<uint16_t>(it->getOptionID());
564                 response.sendVendorSpecificHeaderOptions[i].optionLength =
565                     (it->getOptionData()).length() + 1;
566                 std::string optionData = it->getOptionData();
567                 std::copy(optionData.begin(),
568                          optionData.end(),
569                          response.sendVendorSpecificHeaderOptions[i].optionData);
570                 response.sendVendorSpecificHeaderOptions[i].optionData[it->getOptionData().length()]
571                     = '\0';
572                 i++;
573             }
574
575             if(OC_EH_RESOURCE_CREATED == response.ehResult)
576             {
577                 pResponse->getNewResourceUri().copy(response.resourceUri,
578                         sizeof (response.resourceUri) - 1);
579                 response.resourceUri[pResponse->getNewResourceUri().length()] = '\0';
580             }
581
582             if(cLock)
583             {
584                 std::lock_guard<std::recursive_mutex> lock(*cLock);
585                 result = OCDoResponse(&response);
586             }
587             else
588             {
589                 OICFree(response.payload);
590                 result = OC_STACK_ERROR;
591             }
592
593             if(result != OC_STACK_OK)
594             {
595                 oclog() << "Error sending response\n";
596             }
597             return result;
598         }
599     }
600
601     InProcServerWrapper::~InProcServerWrapper()
602     {
603         if(m_processThread.joinable())
604         {
605             m_threadRun = false;
606             m_processThread.join();
607         }
608
609         OCStop();
610     }
611 }