5af916a5d4be3a8d6a2a34d036fcc639ff9d79ac
[platform/upstream/iotivity.git] / src / OCPlatform.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 //******************************************************************
22 // File name:
23 //     OCPlatform.cpp
24 //
25 // Description: Implementation of the OCPlatform.
26 //
27 //
28 //
29 //*********************************************************************
30
31 #include <random>
32
33 #include "OCPlatform.h"
34 #include "OCApi.h"
35 #include "OCException.h"
36
37 namespace OC
38 {
39     // Constructor. Internally calls private init function
40     OCPlatform::OCPlatform(const PlatformConfig& config)
41      : m_cfg(config)
42     {
43         init(m_cfg);
44     }
45
46     OCPlatform::~OCPlatform(void)
47     {
48         std::cout << "platform destructor called" << std::endl;
49     }
50
51     OCStackResult OCPlatform::notifyAllObservers(OCResourceHandle resourceHandle)
52     {
53         return OCNotifyAllObservers(resourceHandle);
54     }
55
56     OCStackResult OCPlatform::notifyListOfObservers(
57                                           OCResourceHandle resourceHandle,
58                                           ObservationIds& observationIds,
59                                           const std::shared_ptr<OCResourceResponse> pResponse)
60     {
61         OCStackResult result = OC_STACK_ERROR;
62
63         if(pResponse)
64         {
65             try
66             {
67                 std::string payload = pResponse->getPayload();
68                 unsigned char *pBuffer = new unsigned char[payload.length()+1];
69                 strncpy((char*)pBuffer, payload.c_str(), payload.length() + 1);
70
71                 // TODO Logging
72                 printf("\tGoing from stack for List of Observers: Payload: %s\n", (char*)pBuffer);
73
74                 result = OCNotifyListOfObservers(resourceHandle, &observationIds[0], 
75                                                  observationIds.size(), pBuffer);
76
77                 delete(pBuffer);
78             }
79             catch(std::exception e) // TODO : define our own exception
80             {
81                 throw e;
82             }
83
84             return result;
85         }
86     }
87
88     void OCPlatform::init(const PlatformConfig& config)
89     {
90         m_csdkLock = make_shared<std::mutex>();
91         std::unique_ptr<WrapperFactory> wrapperInstance(new WrapperFactory());
92         m_WrapperInstance = std::move(wrapperInstance);
93
94         if(config.mode == ModeType::Server)
95         {
96             // Call server wrapper init
97             m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
98         }
99         else if(config.mode == ModeType::Client)
100         {
101             // Call client wrapper init
102             m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
103         }
104         else
105         {
106             // This must be both server and client
107             m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
108             m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
109         }
110     }
111
112     OCResource::Ptr OCPlatform::constructResourceObject(const std::string& host, const std::string& uri,
113         bool isObservable, const std::vector<std::string>& resourceTypes,
114         const std::vector<std::string>& interfaces)
115     {
116         if(m_client)
117         {
118             return std::shared_ptr<OCResource>(new OCResource(m_client, host, uri, isObservable, resourceTypes, interfaces));
119         }
120
121         else
122         {
123             return std::shared_ptr<OCResource>();
124         }
125     }
126
127     OCStackResult OCPlatform::findResource(const std::string& host, const std::string& resourceName,
128         FindCallback resourceHandler)
129     {
130         if(m_client)
131         {
132             return m_client->ListenForResource(host, resourceName, resourceHandler);
133         }
134         return OC_STACK_ERROR;
135     }
136
137
138     OCStackResult OCPlatform::registerResource(OCResourceHandle& resourceHandle,
139         std::string& resourceURI,
140         const std::string& resourceTypeName,
141         const std::string& resourceInterface,
142         RegisterCallback entityHandler,
143         uint8_t resourceProperty)
144     {
145         OCStackResult result = OC_STACK_OK;
146
147         if(m_server)
148         {
149             try{
150                 result = m_server->registerResource(resourceHandle, resourceURI, resourceTypeName, resourceInterface, entityHandler, resourceProperty);
151             }
152             catch(std::exception e) // TODO: define our own expception.
153             {
154                 throw e;
155             }
156         }
157
158         return result;
159     }
160
161
162     OCStackResult OCPlatform::unregisterResource(const OCResourceHandle& resourceHandle) const
163     {
164         OCStackResult result = OC_STACK_ERROR;
165
166         if(m_server)
167         {
168             result = m_server->unregisterResource(resourceHandle);
169         }
170         return result;
171     }
172
173
174
175     OCStackResult OCPlatform::unbindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle)
176     {
177         OCStackResult result = OC_STACK_OK;
178
179         try {
180             result = OCUnBindResource(collectionHandle, resourceHandle);
181         }
182         catch(std::exception e) // TODO: define our own expception.
183         {
184             throw e;
185         }
186
187         return result;
188     }
189
190     OCStackResult OCPlatform::unbindResources(const OCResourceHandle collectionHandle, const std::vector<OCResourceHandle>& resourceHandles)
191     {
192         OCStackResult result = OC_STACK_OK;
193
194         try {
195
196             for(const OCResourceHandle& handle : resourceHandles)
197             {
198                 result = OCUnBindResource(collectionHandle, handle);
199
200                 if(result != OC_STACK_OK)
201                 {
202                     // TODO Should we unbind the previous successful ones?
203                     // TODO should we return which are succesful
204                     // Currently just returns with any failure
205                     return result;
206                 }
207             }
208         }
209         catch(std::exception e) // TODO : define our own exception 
210         {
211             throw e;
212         }
213
214         return result;
215     }
216
217     OCStackResult OCPlatform::bindResource(const OCResourceHandle collectionHandle, const OCResourceHandle resourceHandle)
218     {
219         OCStackResult result = OC_STACK_OK;
220
221         try {
222             result = OCBindResource(collectionHandle, resourceHandle);
223         }
224         catch(std::exception e) // TODO : define our own exception
225         {
226             throw e;
227         }
228
229         return result;
230     }
231
232     OCStackResult OCPlatform::bindResources(const OCResourceHandle collectionHandle, const std::vector<OCResourceHandle>& resourceHandles)
233     {
234         OCStackResult result = OC_STACK_OK;
235
236         try {
237
238             for(const OCResourceHandle& handle : resourceHandles)
239             {
240                 result = OCBindResource(collectionHandle, handle);
241
242                 if(result != OC_STACK_OK)
243                 {
244                     // TODO Should we unbind the previous successful ones?
245                     // TODO should we return which are succesful
246                     // Currently just returns with any failure
247                     return result;
248                 }
249             }
250         }
251         catch(std::exception e) // TODO : define our own exception
252         {
253             throw e;
254         }
255
256         return result;
257     }
258
259     OCStackResult OCPlatform::bindTypeToResource(const OCResourceHandle& resourceHandle,
260         const std::string& resourceTypeName) const
261     {
262         OCStackResult result = OC_STACK_ERROR;
263         if(m_server)
264         {
265             try
266             {
267                 result = m_server->bindTypeToResource(resourceHandle, resourceTypeName);
268             }
269             catch (OCException& e)
270             {
271                 cout << "Caught an exception..." << endl;
272                 cout << "\tMessage: " << e.what()  << endl;
273                 cout << "\t Reason: " << e.reason() << endl;
274             }
275         }
276         return result;
277     }
278
279     OCStackResult OCPlatform::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
280         const std::string& resourceInterfaceName) const
281     {
282         OCStackResult result = OC_STACK_ERROR;
283         if(m_server)
284         {
285             try
286             {
287                 result = m_server->bindInterfaceToResource(resourceHandle, resourceInterfaceName);
288             }
289             catch (OCException& e)
290             {
291                 cout << "Caught an exception..." << endl;
292                 cout << "\tMessage: " << e.what()  << endl;
293                 cout << "\t Reason: " << e.reason() << endl;
294             }
295         }
296         return result;
297
298     }
299
300     OCStackResult OCPlatform::startPresence(const unsigned int announceDurationSeconds)            
301     { 
302         if(m_server)
303         {
304             return m_server->startPresence(announceDurationSeconds);
305         }
306         else
307         {
308             return OC_STACK_ERROR;
309         }
310     }
311
312     OCStackResult OCPlatform::stopPresence()
313     {
314         if(m_server)
315         {
316             return m_server->stopPresence();
317         }
318         else
319         {
320             return OC_STACK_ERROR;
321         }
322     }
323
324     OCStackResult OCPlatform::subscribePresence(OCPresenceHandle& presenceHandle, const std::string& host, 
325         SubscribeCallback presenceHandler)
326     {
327         if(m_client)
328         {
329             return m_client->SubscribePresence(&presenceHandle, host, presenceHandler);
330         }
331         else
332         {
333             return OC_STACK_ERROR;
334         }
335     }
336
337     OCStackResult OCPlatform::unsubscribePresence(OCPresenceHandle presenceHandle)
338     {
339         if(m_client)
340         {
341             return m_client->UnsubscribePresence(presenceHandle);
342         }
343         else
344         {
345             return OC_STACK_ERROR;
346         }
347     }
348
349 } //namespace OC