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