replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / resource-directory / samples / rd_queryClient.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 "octypes.h"
22 #include "ocstack.h"
23 #include "payload_logging.h"
24
25 bool g_foundResource = true;
26 static int count = 0;
27 #define DEFAULT_CONTEXT_VALUE       (0x99)
28
29 OCStackApplicationResult foundResource(void* ctx,
30                 OCDoHandle handle,
31                 OCClientResponse *clientResponse)
32 {
33     (void)handle;
34     (void) ctx;
35     if (clientResponse == NULL)
36     {
37         std::cout << "foundResource received NULL clientResponse" << std::endl;
38         return   OC_STACK_DELETE_TRANSACTION;
39     }
40
41     std::cout << "Found resource response." << std::endl;
42     OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
43     count++;
44
45     if (count == 3)
46     {
47         g_foundResource = false;
48         exit(0);
49     }
50     return OC_STACK_KEEP_TRANSACTION;
51 }
52
53 OCStackResult findResource(std::string str, OCClientResponseHandler cb)
54 {
55     OCCallbackData cbData;
56     cbData.context = (void *)DEFAULT_CONTEXT_VALUE;
57     cbData.cb = cb;
58
59     return OCDoResource(nullptr, OC_REST_DISCOVER,
60              str.c_str(), nullptr, nullptr, CT_DEFAULT,
61              static_cast<OCQualityOfService>(OC_LOW_QOS),
62              &cbData, nullptr, 0);
63 }
64
65 int main()
66 {
67     bool sendRequest = true;
68     std::cout << "Initializing IoTivity Platform" << std::endl;
69     OCStackResult result = OCInit(NULL, 0, OC_CLIENT_SERVER);
70     if (result != OC_STACK_OK)
71     {
72         std::cout << "OCInit Failed" << result << std::endl;
73         return -1;
74     }
75
76     std::cout << "Created Platform..." << std::endl;
77
78     while (g_foundResource)
79     {
80         if (sendRequest)
81         {
82             sendRequest = false;
83             std::cout << "Finding Resource light" << std::endl;
84             if (OC_STACK_OK != findResource("/oic/res?rt=core.light", foundResource))
85             {
86                 std::cout << "Exception finding resources : " << std::endl;
87                 sendRequest = true;
88             }
89             if (OC_STACK_OK != findResource("/oic/res?if=oic.if.baseline", foundResource))
90             {
91                 std::cout << "Exception finding resources : " << std::endl;
92                 sendRequest = true;
93             }
94             if (OC_STACK_OK != findResource("/oic/res", foundResource))
95             {
96                 std::cout << "Exception finding resources : " << std::endl;
97                 sendRequest = true;
98             }
99         }
100     }
101 }