1 //******************************************************************
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 /// This sample shows how one could create a resource (collection) with children.
28 #include "OCPlatform.h"
30 #include "ThingsManager.h"
36 typedef std::function< OCEntityHandlerResult(std::shared_ptr< OCResourceRequest > request) > ResourceEntityHandler;
38 static std::string defaultURIPrefix = "/oic/con";
39 static std::string defaultResourceTypePrefix = "oic.con";
41 extern std::string defaultTimeValue;
42 extern std::string defaultCurrentTimeValue;
48 // diagnostics members
49 std::string m_timeUri;
50 std::string m_timeValue;
51 std::vector< std::string > m_timeTypes;
52 std::vector< std::string > m_timeInterfaces;
53 OCResourceHandle m_timeHandle;
54 OCRepresentation m_timeRep;
56 // factory reset members
57 std::string m_currentTimeUri;
58 std::string m_currentTimeValue;
59 std::vector< std::string > m_currentTimeTypes;
60 std::vector< std::string > m_currentTimeInterfaces;
61 OCResourceHandle m_currentTimeHandle;
62 OCRepresentation m_currentTimeRep;
67 m_timeValue(defaultTimeValue), m_currentTimeValue(defaultCurrentTimeValue)
69 m_currentTimeUri = defaultURIPrefix + "/time/0/currentTime"; // URI of the resource
70 m_currentTimeTypes.push_back("oic.con.time.currentTime"); // resource type name.
71 m_currentTimeInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
73 m_currentTimeRep.setUri(m_currentTimeUri);
74 m_currentTimeRep.setResourceTypes(m_currentTimeTypes);
75 m_currentTimeRep.setResourceInterfaces(m_currentTimeInterfaces);
76 m_currentTimeRep.setValue("value", m_currentTimeValue);
77 m_currentTimeHandle = NULL;
79 m_timeUri = defaultURIPrefix + "/time"; // URI of the resource
80 m_timeTypes.push_back("oic.con.time"); // resource type name.
81 m_timeInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
82 m_timeInterfaces.push_back(BATCH_INTERFACE); // resource interface.
83 m_timeInterfaces.push_back(LINK_INTERFACE); // resource interface.
84 m_timeRep.setValue("value", m_timeValue);
85 m_timeRep.setUri(m_timeUri);
86 m_timeRep.setResourceTypes(m_timeTypes);
87 m_timeRep.setResourceInterfaces(m_timeInterfaces);
93 TimeCollection(std::string URIPrefix, std::string ResourceTypePrefix) :
94 m_timeValue(defaultTimeValue), m_currentTimeValue(defaultCurrentTimeValue)
96 m_currentTimeUri = URIPrefix + "/time/0/currentTime"; // URI of the resource
97 m_currentTimeTypes.push_back(ResourceTypePrefix + ".time.currentTime"); // type name.
98 m_currentTimeInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
100 m_currentTimeRep.setUri(m_currentTimeUri);
101 m_currentTimeRep.setResourceTypes(m_currentTimeTypes);
102 m_currentTimeRep.setResourceInterfaces(m_currentTimeInterfaces);
103 m_currentTimeRep.setValue("value", m_currentTimeValue);
104 m_currentTimeHandle = NULL;
106 m_timeUri = URIPrefix + "/time"; // URI of the resource
107 m_timeTypes.push_back(ResourceTypePrefix + ".time"); // resource type name.
108 m_timeInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
109 m_timeInterfaces.push_back(BATCH_INTERFACE); // resource interface.
110 m_timeInterfaces.push_back(LINK_INTERFACE); // resource interface.
111 m_timeRep.setValue("value", m_timeValue);
112 m_timeRep.setUri(m_timeUri);
113 m_timeRep.setResourceTypes(m_timeTypes);
114 m_timeRep.setResourceInterfaces(m_timeInterfaces);
119 /// This function internally calls registerResource API.
120 void createResources(ResourceEntityHandler callback);
122 void setTimeRepresentation(OCRepresentation& rep);
123 void setCurrentTimeRepresentation(OCRepresentation& rep);
125 OCRepresentation getTimeRepresentation();
126 OCRepresentation getCurrentTimeRepresentation();
128 std::string getTimeUri();
129 std::string getCurrentTimeUri();
135 extern std::string defaultNetworkValue;
136 extern std::string defaultIPAddressValue;
138 class NetworkCollection
142 // diagnostics members
143 std::string m_networkUri;
144 std::string m_networkValue;
145 std::vector< std::string > m_networkTypes;
146 std::vector< std::string > m_networkInterfaces;
147 OCResourceHandle m_networkHandle;
148 OCRepresentation m_networkRep;
150 // factory reset members
151 std::string m_IPAddressUri;
152 std::string m_IPAddressValue;
153 std::vector< std::string > m_IPAddressTypes;
154 std::vector< std::string > m_IPAddressInterfaces;
155 OCResourceHandle m_IPAddressHandle;
156 OCRepresentation m_IPAddressRep;
161 NetworkCollection() :
162 m_networkValue(defaultNetworkValue), m_IPAddressValue(defaultIPAddressValue)
164 m_IPAddressUri = defaultURIPrefix + "/network/0/IPAddress"; // URI of the resource
165 m_IPAddressTypes.push_back("oic.con.network.IPAddress"); // resource type name.
166 m_IPAddressInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
168 m_IPAddressRep.setUri(m_IPAddressUri);
169 m_IPAddressRep.setResourceTypes(m_IPAddressTypes);
170 m_IPAddressRep.setResourceInterfaces(m_IPAddressInterfaces);
171 m_IPAddressRep.setValue("value", m_IPAddressValue);
172 m_IPAddressHandle = NULL;
174 m_networkUri = defaultURIPrefix + "/network"; // URI of the resource
175 m_networkTypes.push_back("oic.con.network"); // resource type name.
176 m_networkInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
177 m_networkInterfaces.push_back(BATCH_INTERFACE); // resource interface.
178 m_networkInterfaces.push_back(LINK_INTERFACE); // resource interface.
179 m_networkRep.setValue("value", m_networkValue);
180 m_networkRep.setUri(m_networkUri);
181 m_networkRep.setResourceTypes(m_networkTypes);
182 m_networkRep.setResourceInterfaces(m_networkInterfaces);
183 m_networkHandle = NULL;
188 NetworkCollection(std::string URIPrefix, std::string ResourceTypePrefix) :
189 m_networkValue(defaultNetworkValue), m_IPAddressValue(defaultIPAddressValue)
191 m_IPAddressUri = URIPrefix + "/network/0/IPAddress"; // URI of the resource
192 m_IPAddressTypes.push_back(ResourceTypePrefix + "network.IPAddress"); // resource type name.
193 m_IPAddressInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
195 m_IPAddressRep.setUri(m_IPAddressUri);
196 m_IPAddressRep.setResourceTypes(m_IPAddressTypes);
197 m_IPAddressRep.setResourceInterfaces(m_IPAddressInterfaces);
198 m_IPAddressRep.setValue("value", m_IPAddressValue);
199 m_IPAddressHandle = NULL;
201 m_networkUri = URIPrefix + "/network"; // URI of the resource
202 m_networkTypes.push_back(ResourceTypePrefix + ".network"); // resource type name.
203 m_networkInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
204 m_networkInterfaces.push_back(BATCH_INTERFACE); // resource interface.
205 m_networkInterfaces.push_back(LINK_INTERFACE); // resource interface.
206 m_networkRep.setValue("value", m_networkValue);
207 m_networkRep.setUri(m_networkUri);
208 m_networkRep.setResourceTypes(m_networkTypes);
209 m_networkRep.setResourceInterfaces(m_networkInterfaces);
210 m_networkHandle = NULL;
214 /// This function internally calls registerResource API.
215 void createResources(ResourceEntityHandler callback);
217 void setNetworkRepresentation(OCRepresentation& rep);
218 void setIPAddressRepresentation(OCRepresentation& rep);
220 OCRepresentation getNetworkRepresentation();
221 OCRepresentation getIPAddressRepresentation();
223 std::string getNetworkUri();
224 std::string getIPAddressUri();
230 extern std::string defaultSecurityValue;
231 extern std::string defaultModeValue;
233 class SecurityCollection
237 // diagnostics members
238 std::string m_securityUri;
239 std::string m_securityValue;
240 std::vector< std::string > m_securityTypes;
241 std::vector< std::string > m_securityInterfaces;
242 OCResourceHandle m_securityHandle;
243 OCRepresentation m_securityRep;
245 // factory reset members
246 std::string m_modeUri;
247 std::string m_modeValue;
248 std::vector< std::string > m_modeTypes;
249 std::vector< std::string > m_modeInterfaces;
250 OCResourceHandle m_modeHandle;
251 OCRepresentation m_modeRep;
255 SecurityCollection() :
256 m_securityValue(defaultSecurityValue), m_modeValue(defaultModeValue)
258 m_modeUri = defaultURIPrefix + "/security/0/mode"; // URI of the resource
259 m_modeTypes.push_back("oic.con.security.mode"); // resource type name.
260 m_modeInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
262 m_modeRep.setUri(m_modeUri);
263 m_modeRep.setResourceTypes(m_modeTypes);
264 m_modeRep.setResourceInterfaces(m_modeInterfaces);
265 m_modeRep.setValue("value", m_modeValue);
268 m_securityUri = defaultURIPrefix + "/security"; // URI of the resource
269 m_securityTypes.push_back("oic.con.security"); // resource type name.
270 m_securityInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
271 m_securityInterfaces.push_back(BATCH_INTERFACE); // resource interface.
272 m_securityInterfaces.push_back(LINK_INTERFACE); // resource interface.
273 m_securityRep.setValue("value", m_securityValue);
274 m_securityRep.setUri(m_securityUri);
275 m_securityRep.setResourceTypes(m_securityTypes);
276 m_securityRep.setResourceInterfaces(m_securityInterfaces);
277 m_securityHandle = NULL;
282 SecurityCollection(std::string URIPrefix, std::string ResourceTypePrefix) :
283 m_securityValue(defaultSecurityValue), m_modeValue(defaultModeValue)
285 m_modeUri = URIPrefix + "/security/0/mode"; // URI of the resource
286 m_modeTypes.push_back(ResourceTypePrefix + ".security.mode"); // resource type name.
287 m_modeInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
289 m_modeRep.setUri(m_modeUri);
290 m_modeRep.setResourceTypes(m_modeTypes);
291 m_modeRep.setResourceInterfaces(m_modeInterfaces);
292 m_modeRep.setValue("value", m_modeValue);
295 m_securityUri = URIPrefix + "/security"; // URI of the resource
296 m_securityTypes.push_back(ResourceTypePrefix + ".security"); // resource type name.
297 m_securityInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
298 m_securityInterfaces.push_back(BATCH_INTERFACE); // resource interface.
299 m_securityInterfaces.push_back(LINK_INTERFACE); // resource interface.
300 m_securityRep.setValue("value", m_securityValue);
301 m_securityRep.setUri(m_securityUri);
302 m_securityRep.setResourceTypes(m_securityTypes);
303 m_securityRep.setResourceInterfaces(m_securityInterfaces);
304 m_securityHandle = NULL;
308 /// This function internally calls registerResource API.
309 void createResources(ResourceEntityHandler callback);
311 void setSecurityRepresentation(OCRepresentation& rep);
312 void setModeRepresentation(OCRepresentation& rep);
314 OCRepresentation getSecurityRepresentation();
315 OCRepresentation getModeRepresentation();
317 std::string getSecurityUri();
318 std::string getModeUri();
324 extern std::string defaultConfigurationValue;
325 extern std::string defaultRegionValue;
326 static std::string defaultTimeLink = "/con/con/0/time";
327 static std::string defaultNetworkLink = "/con/con/0/network";
328 static std::string defaultSecurityLink = "/con/con/0/security";
330 class ConfigurationCollection
333 TimeCollection *myTimeCollection;
334 NetworkCollection *myNetworkCollection;
335 SecurityCollection *mySecurityCollection;
338 // Configuration members
339 std::string m_configurationUri;
340 std::string m_configurationValue;
341 std::vector< std::string > m_configurationTypes;
342 std::vector< std::string > m_configurationInterfaces;
343 OCResourceHandle m_configurationHandle;
344 OCRepresentation m_configurationRep;
347 std::string m_regionUri;
348 std::string m_regionValue;
349 std::vector< std::string > m_regionTypes;
350 std::vector< std::string > m_regionInterfaces;
351 OCResourceHandle m_regionHandle;
352 OCRepresentation m_regionRep;
355 std::string m_timeUri;
356 std::string m_timeLink;
357 std::vector< std::string > m_timeTypes;
358 std::vector< std::string > m_timeInterfaces;
359 OCResourceHandle m_timeHandle;
360 OCRepresentation m_timeRep;
363 std::string m_networkUri;
364 std::string m_networkLink;
365 std::vector< std::string > m_networkTypes;
366 std::vector< std::string > m_networkInterfaces;
367 OCResourceHandle m_networkHandle;
368 OCRepresentation m_networkRep;
371 std::string m_securityUri;
372 std::string m_securityLink;
373 std::vector< std::string > m_securityTypes;
374 std::vector< std::string > m_securityInterfaces;
375 OCResourceHandle m_securityHandle;
376 OCRepresentation m_securityRep;
380 ConfigurationCollection() :
381 m_configurationValue(defaultConfigurationValue), m_regionValue(defaultRegionValue), m_timeLink(
382 defaultTimeLink), m_networkLink(defaultNetworkLink), m_securityLink(
385 m_regionUri = defaultURIPrefix + "/0/region"; // URI of the resource
386 m_regionTypes.push_back(defaultResourceTypePrefix + ".region"); // resource type name.
387 m_regionInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
389 m_regionRep.setUri(m_regionUri);
390 m_regionRep.setResourceTypes(m_regionTypes);
391 m_regionRep.setResourceInterfaces(m_regionInterfaces);
392 m_regionRep.setValue("value", m_regionValue);
393 m_regionHandle = NULL;
395 m_timeUri = defaultURIPrefix + "/0/time"; // URI of the resource
396 m_timeTypes.push_back(defaultResourceTypePrefix + ".time"); // resource type name.
397 m_timeInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
399 m_timeRep.setUri(m_timeUri);
400 m_timeRep.setResourceTypes(m_timeTypes);
401 m_timeRep.setResourceInterfaces(m_timeInterfaces);
402 m_timeRep.setValue("link", m_timeLink);
405 m_networkUri = defaultURIPrefix + "/0/net"; // URI of the resource
406 m_networkTypes.push_back(defaultResourceTypePrefix + ".net"); // resource type name.
407 m_networkInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
409 m_networkRep.setUri(m_networkUri);
410 m_networkRep.setResourceTypes(m_networkTypes);
411 m_networkRep.setResourceInterfaces(m_networkInterfaces);
412 m_networkRep.setValue("link", m_networkLink);
413 m_networkHandle = NULL;
415 m_securityUri = defaultURIPrefix + "/0/sec"; // URI of the resource
416 m_securityTypes.push_back(defaultResourceTypePrefix + ".sec"); // resource type name.
417 m_securityInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
419 m_securityRep.setUri(m_securityUri);
420 m_securityRep.setResourceTypes(m_securityTypes);
421 m_securityRep.setResourceInterfaces(m_securityInterfaces);
422 m_securityRep.setValue("link", m_securityLink);
423 m_securityHandle = NULL;
425 m_configurationUri = defaultURIPrefix + ""; // URI of the resource
426 m_configurationTypes.push_back(defaultResourceTypePrefix + ""); // resource type name.
427 m_configurationInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
428 m_configurationInterfaces.push_back(BATCH_INTERFACE); // resource interface.
429 m_configurationInterfaces.push_back(LINK_INTERFACE); // resource interface.
430 m_configurationRep.setValue("value", m_configurationValue);
431 m_configurationRep.setUri(m_configurationUri);
432 m_configurationRep.setResourceTypes(m_configurationTypes);
433 m_configurationRep.setResourceInterfaces(m_configurationInterfaces);
434 m_configurationHandle = NULL;
436 myTimeCollection = new TimeCollection();
437 myNetworkCollection = new NetworkCollection();
438 mySecurityCollection = new SecurityCollection();
443 ConfigurationCollection(std::string URIPrefix, std::string ResourceTypePrefix) :
444 m_configurationValue(defaultConfigurationValue), m_regionValue(defaultRegionValue), m_timeLink(
445 defaultTimeLink), m_networkLink(defaultNetworkLink), m_securityLink(
448 m_regionUri = URIPrefix + "/0/region"; // URI of the resource
449 m_regionTypes.push_back(ResourceTypePrefix + ".region"); // type name.
450 m_regionInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
452 m_regionRep.setUri(m_regionUri);
453 m_regionRep.setResourceTypes(m_regionTypes);
454 m_regionRep.setResourceInterfaces(m_regionInterfaces);
455 m_regionRep.setValue("value", m_regionValue);
456 m_regionHandle = NULL;
458 m_timeUri = URIPrefix + "/0/time"; // URI of the resource
459 m_timeTypes.push_back(ResourceTypePrefix + ".time"); // resource type name.
460 m_timeInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
462 m_timeRep.setUri(m_timeUri);
463 m_timeRep.setResourceTypes(m_timeTypes);
464 m_timeRep.setResourceInterfaces(m_timeInterfaces);
465 m_timeRep.setValue("link", m_timeLink);
468 m_networkUri = URIPrefix + "/0/net"; // URI of the resource
469 m_networkTypes.push_back(ResourceTypePrefix + ".net"); // resource type name.
470 m_networkInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
472 m_networkRep.setUri(m_networkUri);
473 m_networkRep.setResourceTypes(m_networkTypes);
474 m_networkRep.setResourceInterfaces(m_networkInterfaces);
475 m_networkRep.setValue("link", m_networkLink);
476 m_networkHandle = NULL;
478 m_securityUri = URIPrefix + "/0/sec"; // URI of the resource
479 m_securityTypes.push_back(ResourceTypePrefix + ".sec"); // resource type name.
480 m_securityInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
482 m_securityRep.setUri(m_securityUri);
483 m_securityRep.setResourceTypes(m_securityTypes);
484 m_securityRep.setResourceInterfaces(m_securityInterfaces);
485 m_securityRep.setValue("link", m_securityLink);
486 m_securityHandle = NULL;
488 m_configurationUri = URIPrefix + ""; // URI of the resource
489 m_configurationTypes.push_back(ResourceTypePrefix + ""); // resource type name.
490 m_configurationInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
491 m_configurationInterfaces.push_back(BATCH_INTERFACE); // resource interface.
492 m_configurationInterfaces.push_back(LINK_INTERFACE); // resource interface.
493 m_configurationRep.setValue("value", m_configurationValue);
494 m_configurationRep.setUri(m_configurationUri);
495 m_configurationRep.setResourceTypes(m_configurationTypes);
496 m_configurationRep.setResourceInterfaces(m_configurationInterfaces);
497 m_configurationHandle = NULL;
499 myTimeCollection = new TimeCollection(URIPrefix, ResourceTypePrefix);
500 myNetworkCollection = new NetworkCollection(URIPrefix, ResourceTypePrefix);
501 mySecurityCollection = new SecurityCollection(URIPrefix, ResourceTypePrefix);
505 ~ConfigurationCollection()
507 free(myTimeCollection);
508 free(myNetworkCollection);
509 free(mySecurityCollection);
512 /// This function internally calls registerResource API.
513 void createResources(ResourceEntityHandler callback);
515 void setConfigurationRepresentation(OCRepresentation& rep);
516 void setTimeRepresentation(OCRepresentation& rep);
517 void setNetworkRepresentation(OCRepresentation& rep);
518 void setSecurityRepresentation(OCRepresentation& rep);
519 void setRegionRepresentation(OCRepresentation& rep);
521 OCRepresentation getTimeRepresentation();
522 OCRepresentation getNetworkRepresentation();
523 OCRepresentation getSecurityRepresentation();
524 OCRepresentation getRegionRepresentation();
525 OCRepresentation getConfigurationRepresentation();
527 std::string getConfigurationUri();
528 std::string getTimeUri();
529 std::string getNetworkUri();
530 std::string getSecurityUri();
531 std::string getRegionUri();