1 /* ****************************************************************
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
24 #include <condition_variable>
32 #include "ocpayload.h"
35 #include <OCPlatform.h>
40 #define maxSequenceNumber 0xFFFFFF
42 void printRepresentation(OCRepresentation rep)
44 for (auto itr = rep.begin(); itr != rep.end(); ++itr)
46 cout << "\t" << itr->attrname() << ":\t" << itr->getValueToString() << endl;
47 if (itr->type() == AttributeType::Vector)
49 switch (itr->base_type())
51 case AttributeType::OCRepresentation:
52 for (auto itr2 : (*itr).getValue<vector<OCRepresentation> >())
54 printRepresentation(itr2);
58 case AttributeType::Integer:
59 for (auto itr2 : (*itr).getValue<vector<int> >())
61 cout << "\t\t" << itr2 << endl;
65 case AttributeType::String:
66 for (auto itr2 : (*itr).getValue<vector<string> >())
68 cout << "\t\t" << itr2 << endl;
73 cout << "Unhandled base type " << itr->base_type() << endl;
77 else if (itr->type() == AttributeType::OCRepresentation)
79 printRepresentation((*itr).getValue<OCRepresentation>());
85 void ocPost(const HeaderOptions & /*headerOptions*/,
86 const OCRepresentation &rep, const int eCode)
88 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CHANGED)
90 cout << "\tRequest was successful: " << eCode << endl;
92 printRepresentation(rep);
96 cout << "\tResponse error: " << eCode << endl;
100 void onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation &rep,
101 const int &eCode, const int &sequenceNumber)
105 if (eCode == OC_STACK_OK && sequenceNumber != maxSequenceNumber + 1)
107 if (sequenceNumber == OC_OBSERVE_REGISTER)
109 cout << "Observe registration action is successful" << endl;
112 cout << "OBSERVE RESULT:" << endl;
113 printRepresentation(rep);
117 if (eCode == OC_STACK_OK)
119 cout << "Observe registration failed or de-registration action failed/succeeded" << endl;
123 cout << "onObserve Response error: " << eCode << endl;
130 cout << "Exception: " << e.what() << " in onObserve" << endl;
134 void onDelete(const HeaderOptions & /*headerOptions*/,
137 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_DELETED)
139 cout << "\tDelete was successful" << endl;
143 cout << "\tDelete Response error: " << eCode << endl;
147 condition_variable g_callbackLock;
149 string g_accesstoken;
151 void handleLoginoutCB(const HeaderOptions &,
152 const OCRepresentation &rep, const int ecode)
154 cout << "Auth response received code: " << ecode << endl;
156 if (rep.getPayload() != NULL)
158 printRepresentation(rep);
163 g_accesstoken = rep.getValueToString("accesstoken");
165 g_uid = rep.getValueToString("uid");
168 g_callbackLock.notify_all();
171 int main(int argc, char *argv[])
173 if (argc != 4 && argc != 5)
175 cout << "Put \"[host-ipaddress:port] [authprovider] [authcode]\" for sign-up and sign-in"
177 cout << "Put \"[host-ipaddress:port] [uid] [accessToken] 1\" for sign-in" <<
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
191 OCPlatform::Configure(cfg);
193 OCStackResult result = OC_STACK_ERROR;
195 string host = "coap+tcp://";
198 OCAccountManager::Ptr accountMgr = OCPlatform::constructAccountManagerObject(host,
202 unique_lock<mutex> lock(blocker);
206 accountMgr->signIn(argv[2], argv[3], &handleLoginoutCB);
207 g_callbackLock.wait(lock);
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);
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;
248 QueryParamsMap query;
249 OCRepresentation rep;
251 switch (atoi(cmd.c_str()))
254 cout << "Put userUUID to search:" << endl;
256 result = accountMgr->searchUser(cmd, &ocPost);
260 cout << "Put email to search:" << endl;
262 query["email"] = cmd;
263 result = accountMgr->searchUser(query, &ocPost);
267 cout << "Put phone number to search:" << endl;
269 query["phone"] = cmd;
270 result = accountMgr->searchUser(query, &ocPost);
274 cout << "PUT deviceID to delete:";
276 result = accountMgr->deleteDevice(cmd, &onDelete);
280 result = accountMgr->createGroup(OC::AclGroupType::PUBLIC, &ocPost);
284 cout << "PUT groupId to observe:";
286 result = accountMgr->observeGroup(cmd, &onObserve);
290 result = accountMgr->getGroupList(&ocPost);
294 cout << "PUT groupId to delete:";
296 result = accountMgr->deleteGroup(cmd, &onDelete);
300 cout << "PUT groupId to join:";
302 result = accountMgr->joinGroup(cmd, &ocPost);
306 cout << "PUT groupId to add device:";
308 cout << "PUT deviceId to add to group:";
311 vector<string> deviceIds;
312 deviceIds.push_back(cmd2);
313 result = accountMgr->addDeviceToGroup(cmd, deviceIds, &ocPost);
318 cout << "PUT groupId to get info:";
320 result = accountMgr->getGroupInfo(cmd, &ocPost);
324 cout << "PUT groupId to leave:";
326 result = accountMgr->leaveGroup(cmd, &onDelete);
330 cout << "PUT groupId to remove device:";
332 cout << "PUT deviceId to remove from group:";
335 vector<string> deviceIds;
336 deviceIds.push_back(cmd2);
337 result = accountMgr->deleteDeviceFromGroup(cmd, deviceIds, &onDelete);
342 result = accountMgr->observeInvitation(&onObserve);
346 cout << "PUT groupId to invite:";
348 cout << "PUT userUUID to invite:";
350 result = accountMgr->sendInvitation(cmd, cmd2, &ocPost);
354 cout << "PUT groupId to cancel invitation:";
356 cout << "PUT userUUID to cancel invitation:";
358 result = accountMgr->cancelInvitation(cmd, cmd2, &onDelete);
362 cout << "PUT groupId to delete invitation:";
364 result = accountMgr->deleteInvitation(cmd, &onDelete);
368 cout << "PUT groupId to cancel observe:";
370 result = accountMgr->cancelObserveGroup(cmd);
374 result = accountMgr->cancelObserveInvitation();
382 if (result != OC_STACK_OK)
384 cout << "Error, return code: " << result << endl;
389 cout << "Precondition failed." << endl;