Merge "[CA Integration] Updated C sample apps to accept connectivity type param"...
[platform/upstream/iotivity.git] / resource / examples / groupclient.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 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
21 #include "OCPlatform.h"
22 #include "OCApi.h"
23
24 #include <functional>
25 #include <pthread.h>
26 #include <iostream>
27
28 using namespace std;
29 using namespace OC;
30 namespace PH = std::placeholders;
31
32 OCResourceHandle resourceHandle;
33
34 shared_ptr< OCResource > g_resource;
35 vector< string > lights;
36
37 bool isReady = false;
38
39 void onGet(const HeaderOptions& opt, const OCRepresentation &rep, const int eCode);
40
41 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
42
43 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
44
45 void foundResource(std::shared_ptr< OCResource > resource)
46 {
47     std::string resourceURI;
48     std::string hostAddress;
49
50     try
51     {
52         cout << "FOUND Resource" << endl;
53
54         if (resource)
55         {
56             string resourceURI = resource->uri();
57             cout << resourceURI << endl;
58             if (resourceURI == "/core/a/collection")
59             {
60                 g_resource = resource;
61             }
62
63             g_resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), onGet);
64             printf("HOST :: %s\n", resource->host().c_str());
65         }
66     }
67     catch (std::exception& e)
68     {
69         std::cout << "" << std::endl;
70     }
71 }
72
73 void onGet(const HeaderOptions& opt, const OCRepresentation &rep, const int eCode)
74 {
75     // printf("onGet\n");
76
77     std::vector< OCRepresentation > children = rep.getChildren();
78
79     cout << "\n\n\nCHILD RESOURCE OF GROUP" << endl;
80     for (auto iter = children.begin(); iter != children.end(); ++iter)
81     {
82         lights.push_back((*iter).getUri());
83         cout << "\tURI :: " << (*iter).getUri() << endl;
84     }
85
86     isReady = true;
87 }
88
89 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
90 {
91     printf("\nonPut\n");
92 }
93
94 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
95 {
96     printf("\nonPost\n");
97
98     std::vector< OCRepresentation > children = rep.getChildren();
99
100     cout << "\n\n\nCHILD RESOURCE OF GROUP" << endl;
101     for (auto iter = children.begin(); iter != children.end(); ++iter)
102     {
103         std::string power;
104         (*iter).getValue("power", power);
105
106         cout << "\tURI :: " << (*iter).getUri() << endl;
107         cout << "\t\tpower :: " << power << endl;
108     }
109
110     if (rep.hasAttribute("ActionSet"))
111     {
112         std::string plainText;
113
114         rep.getValue("ActionSet", plainText);
115
116         printf("\tPlain Text :: %s\n", plainText.c_str());
117     }
118     else
119     {
120         printf("Not found ActionSet\n");
121     }
122 }
123
124 int main()
125 {
126     PlatformConfig config
127     { OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos };
128
129     bool isRun = true;
130
131     try
132     {
133         OCPlatform::Configure(config);
134
135         string resourceTypeName = "a.collection";
136 #ifdef CA_INT
137         OCConnectivityType connectivityType = OC_WIFI;
138         OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=a.collection",
139                                  connectivityType, &foundResource);
140 #else
141         OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=a.collection", &foundResource);
142 #endif
143
144         isReady = false;
145
146         while (isRun)
147         {
148             usleep(100);
149             while (isReady)
150             {
151                 int n;
152
153                 cout << endl
154                         << "1 :: CREATE ACTIONSET 2 :: EXECUTE ACTION SET 3 :: GET ACTIONSET\n";
155                 cout << "4 :: DELETE ACTIONSET 5 :: Quit\n";
156
157                 cin >> n;
158                 if (n == 1)
159                 {
160                     string actionsetDesc;
161                     //"movieTime*uri=coap://10.251.44.228:49858/a/light|power=10*uri=coap://10.251.44.228:49858/a/light|power=10|";
162
163                     actionsetDesc = "allbulboff";
164                     actionsetDesc.append("*");
165                     for (auto iter = lights.begin(); iter != lights.end(); ++iter)
166                     {
167                         actionsetDesc.append("uri=").append((*iter));
168                         actionsetDesc.append("|");
169                         actionsetDesc.append("power=");
170                         actionsetDesc.append("off");
171                         if ((iter + 1) != lights.end())
172                         {
173                             actionsetDesc.append("*");
174                         }
175                     }
176
177                     cout << "ActionSet :: " << actionsetDesc << endl;
178
179                     OCRepresentation rep;
180                     rep.setValue("ActionSet", actionsetDesc);
181
182                     if (g_resource)
183                     {
184                         g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
185                                 &onPut);
186                     }
187                 }
188                 else if (n == 2)
189                 {
190                     OCRepresentation rep;
191
192                     rep.setValue("DoAction", std::string("allbulboff"));
193
194                     if (g_resource)
195                     {
196                         g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
197                                 &onPost);
198                     }
199                 }
200                 else if (n == 3)
201                 {
202                     OCRepresentation rep;
203
204                     rep.setValue("GetActionSet", std::string("allbulboff"));
205
206                     if (g_resource)
207                     {
208                         g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
209                                 &onPost);
210                     }
211                 }
212                 else if (n == 4)
213                 {
214                     OCRepresentation rep;
215
216                     rep.setValue("DelActionSet", std::string("allbulboff"));
217
218                     if (g_resource)
219                     {
220                         g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
221                                 &onPut);
222                     }
223                 }
224                 else if (n == 5)
225                 {
226                     isRun = false;
227                     break;
228                 }
229
230                 fflush(stdin);
231             }
232         }
233     }
234     catch (OCException& e)
235     {
236         cout << e.what() << endl;
237     }
238
239     return 0;
240 }