Imported Upstream version 1.2.0
[platform/upstream/iotivity.git] / cloud / samples / client / group_invite / group_invite.cpp
1 /* ****************************************************************
2 *
3 * Copyright 2016 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 <memory>
22 #include <iostream>
23 #include <stdexcept>
24 #include <condition_variable>
25 #include <map>
26 #include <vector>
27 #include <string>
28 #include <unistd.h>
29 #include <stdio.h>
30
31 #include "ocstack.h"
32 #include "ocpayload.h"
33
34 #include <OCApi.h>
35 #include <OCPlatform.h>
36
37 using namespace std;
38 using namespace OC;
39
40 void printRepresentation(OCRepresentation rep)
41 {
42     for (auto itr = rep.begin(); itr != rep.end(); ++itr)
43     {
44         cout << "\t" << itr->attrname() << ":\t" << itr->getValueToString() << endl;
45         if (itr->type() == AttributeType::Vector)
46         {
47             switch (itr->base_type())
48             {
49                 case AttributeType::OCRepresentation:
50                     for (auto itr2 : (*itr).getValue<vector<OCRepresentation> >())
51                     {
52                         printRepresentation(itr2);
53                     }
54                     break;
55
56                 case AttributeType::Integer:
57                     for (auto itr2 : (*itr).getValue<vector<int> >())
58                     {
59                         cout << "\t\t" << itr2 << endl;
60                     }
61                     break;
62
63                 case AttributeType::String:
64                     for (auto itr2 : (*itr).getValue<vector<string> >())
65                     {
66                         cout << "\t\t" << itr2 << endl;
67                     }
68                     break;
69
70                 default:
71                     cout << "Unhandled base type " << itr->base_type() << endl;
72                     break;
73             }
74         }
75         else if (itr->type() == AttributeType::OCRepresentation)
76         {
77             printRepresentation((*itr).getValue<OCRepresentation>());
78         }
79     }
80 }
81
82 //tmp callback
83 void ocPost(const HeaderOptions & /*headerOptions*/,
84             const OCRepresentation &rep, const int eCode)
85 {
86     if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CHANGED)
87     {
88         cout << "\tRequest was successful: " << eCode << endl;
89
90         printRepresentation(rep);
91     }
92     else
93     {
94         cout << "\tResponse error: " << eCode << endl;
95     }
96 }
97
98 void onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation &rep,
99                const int &eCode, const int &sequenceNumber)
100 {
101     try
102     {
103         if (eCode == OC_STACK_OK && sequenceNumber <= MAX_SEQUENCE_NUMBER)
104         {
105             if (sequenceNumber == OC_OBSERVE_REGISTER)
106             {
107                 cout << "Observe registration action is successful" << endl;
108             }
109
110             cout << "OBSERVE RESULT:" << endl;
111             printRepresentation(rep);
112         }
113         else
114         {
115             if (eCode == OC_STACK_OK)
116             {
117                 cout << "Observe registration failed or de-registration action failed/succeeded" << endl;
118             }
119             else
120             {
121                 cout << "onObserve Response error: " << eCode << endl;
122                 exit(-1);
123             }
124         }
125     }
126     catch (exception &e)
127     {
128         cout << "Exception: " << e.what() << " in onObserve" << endl;
129     }
130 }
131
132 void onDelete(const HeaderOptions & /*headerOptions*/,
133               const int eCode)
134 {
135     if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_DELETED)
136     {
137         cout << "\tDelete was successful" << endl;
138     }
139     else
140     {
141         cout << "\tDelete Response error: " << eCode << endl;
142     }
143 }
144
145 condition_variable g_callbackLock;
146 string             g_uid;
147 string             g_accesstoken;
148
149 void handleLoginoutCB(const HeaderOptions &,
150                       const OCRepresentation &rep, const int ecode)
151 {
152     cout << "Auth response received code: " << ecode << endl;
153
154     if (rep.getPayload() != NULL)
155     {
156         printRepresentation(rep);
157     }
158
159     if (ecode == 4)
160     {
161         g_accesstoken = rep.getValueToString("accesstoken");
162
163         g_uid = rep.getValueToString("uid");
164     }
165
166     g_callbackLock.notify_all();
167 }
168
169 int main(int argc, char *argv[])
170 {
171     if (argc != 4 && argc != 5)
172     {
173         cout << "Put \"[host-ipaddress:port] [authprovider] [authcode]\" for sign-up and sign-in"
174              << endl;
175         cout << "Put \"[host-ipaddress:port] [uid] [accessToken] 1\" for sign-in" <<
176              endl;
177         return 0;
178     }
179
180     PlatformConfig cfg
181     {
182         ServiceType::InProc,
183         ModeType::Both,
184         "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
185         0,         // Uses randomly available port
186         QualityOfService::LowQos
187     };
188
189     OCPlatform::Configure(cfg);
190
191     OCStackResult result = OC_STACK_ERROR;
192
193     string host = "coap+tcp://";
194     host += argv[1];
195
196     OCAccountManager::Ptr accountMgr = OCPlatform::constructAccountManagerObject(host,
197                                        CT_ADAPTER_TCP);
198
199     mutex blocker;
200     unique_lock<mutex> lock(blocker);
201
202     if (argc == 5)
203     {
204         accountMgr->signIn(argv[2], argv[3], &handleLoginoutCB);
205         g_callbackLock.wait(lock);
206     }
207     else
208     {
209         accountMgr->signUp(argv[2], argv[3], &handleLoginoutCB);
210         g_callbackLock.wait(lock);
211         accountMgr->signIn(g_uid, g_accesstoken, &handleLoginoutCB);
212         g_callbackLock.wait(lock);
213     }
214
215     cout << "---Group & Invite sample---" << endl;
216     cout << "     1 - searchUser using user UUID" << endl;
217     cout << "     2 - searchUser using email" << endl;
218     cout << "     3 - searchUser using phone" << endl;
219     cout << "     4 - deleteDevice" << endl;
220     cout << "     5 - createGroup" << endl;
221     cout << "     6 - observeGroup" << endl;
222     cout << "     7 - getGroupList" << endl;
223     cout << "     8 - deleteGroup" << endl;
224     cout << "     9 - joinGroup" << endl;
225     cout << "    10 - addDeviceToGroup" << endl;
226     cout << "    11 - getGroupInfo" << endl;
227     cout << "    12 - leaveGroup" << endl;
228     cout << "    13 - deleteDeviceFromGroup" << endl;
229     cout << "    14 - observeInvitation" << endl;
230     cout << "    15 - sendInvitation" << endl;
231     cout << "    16 - cancelInvitation" << endl;
232     cout << "    17 - deleteInvitation" << endl;
233     cout << "    18 - cancelObserveGroup" << endl;
234     cout << "    19 - cancelObserveInvitation" << endl;
235     cout << "    20 - exit" << endl;
236
237     string cmd;
238     string cmd2;
239
240     while (true)
241     {
242         cin >> cmd;
243
244         try
245         {
246             QueryParamsMap query;
247             OCRepresentation rep;
248
249             switch (atoi(cmd.c_str()))
250             {
251                 case 1:
252                     cout << "Put userUUID to search:" << endl;
253                     cin >> cmd;
254                     result = accountMgr->searchUser(cmd, &ocPost);
255                     break;
256
257                 case 2:
258                     cout << "Put email to search:" << endl;
259                     cin >> cmd;
260                     query["email"] = cmd;
261                     result = accountMgr->searchUser(query, &ocPost);
262                     break;
263
264                 case 3:
265                     cout << "Put phone number to search:" << endl;
266                     cin >> cmd;
267                     query["phone"] = cmd;
268                     result = accountMgr->searchUser(query, &ocPost);
269                     break;
270
271                 case 4:
272                     cout << "PUT deviceID to delete:";
273                     cin >> cmd;
274                     result = accountMgr->deleteDevice(cmd, &onDelete);
275                     break;
276
277                 case 5:
278                     result = accountMgr->createGroup(OC::AclGroupType::PUBLIC, &ocPost);
279                     break;
280
281                 case 6:
282                     cout << "PUT groupId to observe:";
283                     cin >> cmd;
284                     result = accountMgr->observeGroup(cmd, &onObserve);
285                     break;
286
287                 case 7:
288                     result = accountMgr->getGroupList(&ocPost);
289                     break;
290
291                 case 8:
292                     cout << "PUT groupId to delete:";
293                     cin >> cmd;
294                     result = accountMgr->deleteGroup(cmd, &onDelete);
295                     break;
296
297                 case 9:
298                     cout << "PUT groupId to join:";
299                     cin >> cmd;
300                     result = accountMgr->joinGroup(cmd, &ocPost);
301                     break;
302
303                 case 10:
304                     cout << "PUT groupId to add device:";
305                     cin >> cmd;
306                     cout << "PUT deviceId to add to group:";
307                     cin >> cmd2;
308                     {
309                         vector<string> deviceIds;
310                         deviceIds.push_back(cmd2);
311                         result = accountMgr->addDeviceToGroup(cmd, deviceIds, &ocPost);
312                     }
313                     break;
314
315                 case 11:
316                     cout << "PUT groupId to get info:";
317                     cin >> cmd;
318                     result = accountMgr->getGroupInfo(cmd, &ocPost);
319                     break;
320
321                 case 12:
322                     cout << "PUT groupId to leave:";
323                     cin >> cmd;
324                     result = accountMgr->leaveGroup(cmd, &onDelete);
325                     break;
326
327                 case 13:
328                     cout << "PUT groupId to remove device:";
329                     cin >> cmd;
330                     cout << "PUT deviceId to remove from group:";
331                     cin >> cmd2;
332                     {
333                         vector<string> deviceIds;
334                         deviceIds.push_back(cmd2);
335                         result = accountMgr->deleteDeviceFromGroup(cmd, deviceIds, &onDelete);
336                     }
337                     break;
338
339                 case 14:
340                     result = accountMgr->observeInvitation(&onObserve);
341                     break;
342
343                 case 15:
344                     cout << "PUT groupId to invite:";
345                     cin >> cmd;
346                     cout << "PUT userUUID to invite:";
347                     cin >> cmd2;
348                     result = accountMgr->sendInvitation(cmd, cmd2, &ocPost);
349                     break;
350
351                 case 16:
352                     cout << "PUT groupId to cancel invitation:";
353                     cin >> cmd;
354                     cout << "PUT userUUID to cancel invitation:";
355                     cin >> cmd2;
356                     result = accountMgr->cancelInvitation(cmd, cmd2, &onDelete);
357                     break;
358
359                 case 17:
360                     cout << "PUT groupId to delete invitation:";
361                     cin >> cmd;
362                     result = accountMgr->deleteInvitation(cmd, &onDelete);
363                     break;
364
365                 case 18:
366                     cout << "PUT groupId to cancel observe:";
367                     cin >> cmd;
368                     result = accountMgr->cancelObserveGroup(cmd);
369                     break;
370
371                 case 19:
372                     result = accountMgr->cancelObserveInvitation();
373                     break;
374
375                 case 20:
376                     goto exit;
377                     break;
378             }
379
380             if (result != OC_STACK_OK)
381             {
382                 cout << "Error, return code: " << result << endl;
383             }
384         }
385         catch (exception e)
386         {
387             cout << "Precondition failed." << endl;
388         }
389     }
390
391 exit:
392     return 0;
393 }