1 //******************************************************************
3 // Copyright 2015 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 #include "oic_string.h"
26 #include "rd_client.h"
27 #include "payload_logging.h"
29 #define TAG ("RD_PublishClient")
30 #define DEFAULT_CONTEXT_VALUE 0x99
32 OCResourceHandle handles[2];
33 std::ostringstream rdAddress;
35 OCStackResult registerLocalResources()
37 std::string resourceURI_thermostat = "/a/thermostat";
38 std::string resourceTypeName_thermostat = "core.thermostat";
39 std::string resourceURI_light = "/a/light";
40 std::string resourceTypeName_light = "core.light";
41 std::string resourceInterface = OC_RSRVD_INTERFACE_DEFAULT;
42 uint8_t resourceProperty = OC_DISCOVERABLE;
44 OCStackResult result = OCCreateResource(&handles[0],
45 resourceTypeName_thermostat.c_str(),
46 resourceInterface.c_str(),
47 resourceURI_thermostat.c_str(),
52 if (OC_STACK_OK != result)
57 result = OCCreateResource(&handles[1],
58 resourceTypeName_light.c_str(),
59 resourceInterface.c_str(),
60 resourceURI_light.c_str(),
70 std::cout << std::endl;
71 std::cout << "********************************************" << std::endl;
72 std::cout << "* method Type : 1 - Discover RD *" << std::endl;
73 std::cout << "* method Type : 2 - Publish *" << std::endl;
74 std::cout << "* method Type : 3 - Update *" << std::endl;
75 std::cout << "* method Type : 4 - Delete *" << std::endl;
76 std::cout << "* method Type : 5 - Status *" << std::endl;
77 std::cout << "********************************************" << std::endl;
78 std::cout << std::endl;
81 static OCStackApplicationResult handleDiscoveryCB(__attribute__((unused))void *ctx,
82 __attribute__((unused)) OCDoHandle handle,
83 __attribute__((unused))
84 OCClientResponse *clientResponse)
86 OIC_LOG(DEBUG, TAG, "Successfully found RD.");
87 rdAddress << clientResponse->devAddr.addr << ":" << clientResponse->devAddr.port;
88 std::cout << "RD Address is : " << rdAddress.str() << std::endl;
89 return OC_STACK_DELETE_TRANSACTION;
92 static OCStackApplicationResult handlePublishCB(__attribute__((unused))void *ctx,
93 __attribute__((unused)) OCDoHandle handle,
94 __attribute__((unused))
95 OCClientResponse *clientResponse)
97 OIC_LOG(DEBUG, TAG, "Successfully published resources.");
98 return OC_STACK_DELETE_TRANSACTION;
103 std::cout << "Initializing IoTivity Platform" << std::endl;
104 OCStackResult result = OCInit(NULL, 0, OC_CLIENT_SERVER);
105 if (OC_STACK_OK != result)
107 std::cout << "OCInit Failed" << result << std::endl;
111 std::cout << "Created Platform..." << std::endl;
112 result = registerLocalResources();
113 if (OC_STACK_OK != result)
115 std::cout << "Could not create the resource " << result << std::endl;
121 if (handles[0] == NULL || handles[1] == NULL)
133 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
134 std::cout << "Invalid input type, please try again" << std::endl;
142 OCCallbackData cbData;
143 cbData.cb = &handleDiscoveryCB;;
145 cbData.context = (void*) DEFAULT_CONTEXT_VALUE;
146 OCRDDiscover(CT_ADAPTER_IP, &cbData, OC_LOW_QOS);
151 OCCallbackData cbData;
152 cbData.cb = &handlePublishCB;
154 cbData.context = (void*) DEFAULT_CONTEXT_VALUE;
155 std::string address = rdAddress.str();
156 OCRDPublish(address.c_str(), CT_ADAPTER_IP, handles,
157 2, &cbData, OC_LOW_QOS);
163 std::cout << "Invalid input, please try again" << std::endl;