replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / resource-directory / samples / rd_publishingClient.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics 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 #include <iostream>
21 #include <sstream>
22 #include <limits>
23
24 #include "oic_string.h"
25
26 #include "rd_client.h"
27 #include "payload_logging.h"
28
29 #define TAG ("RD_PublishClient")
30 #define DEFAULT_CONTEXT_VALUE 0x99
31
32 OCResourceHandle handles[2];
33 std::ostringstream rdAddress;
34
35 OCStackResult registerLocalResources()
36 {
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;
43
44     OCStackResult result = OCCreateResource(&handles[0],
45                                            resourceTypeName_thermostat.c_str(),
46                                            resourceInterface.c_str(),
47                                            resourceURI_thermostat.c_str(),
48                                            NULL,
49                                            NULL,
50                                            resourceProperty);
51
52     if (OC_STACK_OK != result)
53     {
54         return result;
55     }
56
57     result = OCCreateResource(&handles[1],
58                               resourceTypeName_light.c_str(),
59                               resourceInterface.c_str(),
60                               resourceURI_light.c_str(),
61                               NULL,
62                               NULL,
63                               resourceProperty);
64
65     return result;
66 }
67
68 void printHelp()
69 {
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;
79 }
80
81 static OCStackApplicationResult handleDiscoveryCB(__attribute__((unused))void *ctx,
82                                                   __attribute__((unused)) OCDoHandle handle,
83                                                   __attribute__((unused))
84                                                   OCClientResponse *clientResponse)
85 {
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;
90 }
91
92 static OCStackApplicationResult handlePublishCB(__attribute__((unused))void *ctx,
93                                                   __attribute__((unused)) OCDoHandle handle,
94                                                   __attribute__((unused))
95                                                   OCClientResponse *clientResponse)
96 {
97     OIC_LOG(DEBUG, TAG, "Successfully published resources.");
98     return OC_STACK_DELETE_TRANSACTION;
99 }
100
101 int main()
102 {
103     std::cout << "Initializing IoTivity Platform" << std::endl;
104     OCStackResult result = OCInit(NULL, 0, OC_CLIENT_SERVER);
105     if (OC_STACK_OK != result)
106     {
107         std::cout << "OCInit Failed" << result << std::endl;
108         return -1;
109     }
110
111     std::cout << "Created Platform..." << std::endl;
112     result = registerLocalResources();
113     if (OC_STACK_OK != result)
114     {
115         std::cout << "Could not create the resource " << result << std::endl;
116         return -1;
117     }
118
119     while (1)
120     {
121         if (handles[0] == NULL || handles[1] == NULL)
122         {
123             continue;
124         }
125         printHelp();
126
127         int in = 0;
128         std::cin >> in;
129
130         if (std::cin.fail())
131         {
132             std::cin.clear();
133             std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
134             std::cout << "Invalid input type, please try again" << std::endl;
135             continue;
136         }
137
138         switch ((int)in)
139         {
140             case 1:
141             {
142                 OCCallbackData cbData;
143                 cbData.cb = &handleDiscoveryCB;;
144                 cbData.cd = NULL;
145                 cbData.context = (void*) DEFAULT_CONTEXT_VALUE;
146                 OCRDDiscover(nullptr, CT_ADAPTER_IP, &cbData, OC_LOW_QOS);
147                 break;
148             }
149             case 2:
150             {
151                 OCCallbackData cbData;
152                 cbData.cb = &handlePublishCB;
153                 cbData.cd = NULL;
154                 cbData.context = (void*) DEFAULT_CONTEXT_VALUE;
155                 std::string address = rdAddress.str();
156                 OCRDPublish(nullptr, address.c_str(), CT_ADAPTER_IP, handles,
157                             2, &cbData, OC_LOW_QOS);
158                 break;
159             }
160             case 3:
161                 break;
162             default:
163                 std::cout << "Invalid input, please try again" << std::endl;
164                 break;
165         }
166     }
167     return 0;
168 }