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 void printRepresentation(OCRepresentation rep)
42 for (auto itr = rep.begin(); itr != rep.end(); ++itr)
44 cout << "\t" << itr->attrname() << ":\t" << itr->getValueToString() << endl;
45 if (itr->type() == AttributeType::Vector)
47 switch (itr->base_type())
49 case AttributeType::OCRepresentation:
50 for (auto itr2 : (*itr).getValue<vector<OCRepresentation> >())
52 printRepresentation(itr2);
56 case AttributeType::Integer:
57 for (auto itr2 : (*itr).getValue<vector<int> >())
59 cout << "\t\t" << itr2 << endl;
63 case AttributeType::String:
64 for (auto itr2 : (*itr).getValue<vector<string> >())
66 cout << "\t\t" << itr2 << endl;
71 cout << "Unhandled base type " << itr->base_type() << endl;
75 else if (itr->type() == AttributeType::OCRepresentation)
77 printRepresentation((*itr).getValue<OCRepresentation>());
83 void ocPost(const HeaderOptions & /*headerOptions*/,
84 const OCRepresentation &rep, const int eCode)
86 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CHANGED)
88 cout << "\tRequest was successful: " << eCode << endl;
90 printRepresentation(rep);
94 cout << "\tResponse error: " << eCode << endl;
98 void onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation &rep,
99 const int &eCode, const int &sequenceNumber)
103 if (eCode == OC_STACK_OK && sequenceNumber <= MAX_SEQUENCE_NUMBER)
105 if (sequenceNumber == OC_OBSERVE_REGISTER)
107 cout << "Observe registration action is successful" << endl;
110 cout << "OBSERVE RESULT:" << endl;
111 printRepresentation(rep);
115 if (eCode == OC_STACK_OK)
117 cout << "Observe registration failed or de-registration action failed/succeeded" << endl;
121 cout << "onObserve Response error: " << eCode << endl;
128 cout << "Exception: " << e.what() << " in onObserve" << endl;
132 void onDelete(const HeaderOptions & /*headerOptions*/,
135 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_DELETED)
137 cout << "\tDelete was successful" << endl;
141 cout << "\tDelete Response error: " << eCode << endl;
145 condition_variable g_callbackLock;
147 string g_accesstoken;
149 void handleLoginoutCB(const HeaderOptions &,
150 const OCRepresentation &rep, const int ecode)
152 cout << "Auth response received code: " << ecode << endl;
154 if (rep.getPayload() != NULL)
156 printRepresentation(rep);
161 g_accesstoken = rep.getValueToString("accesstoken");
163 g_uid = rep.getValueToString("uid");
166 g_callbackLock.notify_all();
169 int main(int argc, char *argv[])
171 if (argc != 4 && argc != 5)
173 cout << "Put \"[host-ipaddress:port] [authprovider] [authcode]\" for sign-up and sign-in"
175 cout << "Put \"[host-ipaddress:port] [uid] [accessToken] 1\" for sign-in" <<
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
189 OCPlatform::Configure(cfg);
191 OCStackResult result = OC_STACK_ERROR;
193 string host = "coap+tcp://";
196 OCAccountManager::Ptr accountMgr = OCPlatform::constructAccountManagerObject(host,
200 unique_lock<mutex> lock(blocker);
204 accountMgr->signIn(argv[2], argv[3], &handleLoginoutCB);
205 g_callbackLock.wait(lock);
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);
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;
246 QueryParamsMap query;
247 OCRepresentation rep;
249 switch (atoi(cmd.c_str()))
252 cout << "Put userUUID to search:" << endl;
254 result = accountMgr->searchUser(cmd, &ocPost);
258 cout << "Put email to search:" << endl;
260 query["email"] = cmd;
261 result = accountMgr->searchUser(query, &ocPost);
265 cout << "Put phone number to search:" << endl;
267 query["phone"] = cmd;
268 result = accountMgr->searchUser(query, &ocPost);
272 cout << "PUT deviceID to delete:";
274 result = accountMgr->deleteDevice(cmd, &onDelete);
278 result = accountMgr->createGroup(OC::AclGroupType::PUBLIC, &ocPost);
282 cout << "PUT groupId to observe:";
284 result = accountMgr->observeGroup(cmd, &onObserve);
288 result = accountMgr->getGroupList(&ocPost);
292 cout << "PUT groupId to delete:";
294 result = accountMgr->deleteGroup(cmd, &onDelete);
298 cout << "PUT groupId to join:";
300 result = accountMgr->joinGroup(cmd, &ocPost);
304 cout << "PUT groupId to add device:";
306 cout << "PUT deviceId to add to group:";
309 vector<string> deviceIds;
310 deviceIds.push_back(cmd2);
311 result = accountMgr->addDeviceToGroup(cmd, deviceIds, &ocPost);
316 cout << "PUT groupId to get info:";
318 result = accountMgr->getGroupInfo(cmd, &ocPost);
322 cout << "PUT groupId to leave:";
324 result = accountMgr->leaveGroup(cmd, &onDelete);
328 cout << "PUT groupId to remove device:";
330 cout << "PUT deviceId to remove from group:";
333 vector<string> deviceIds;
334 deviceIds.push_back(cmd2);
335 result = accountMgr->deleteDeviceFromGroup(cmd, deviceIds, &onDelete);
340 result = accountMgr->observeInvitation(&onObserve);
344 cout << "PUT groupId to invite:";
346 cout << "PUT userUUID to invite:";
348 result = accountMgr->sendInvitation(cmd, cmd2, &ocPost);
352 cout << "PUT groupId to cancel invitation:";
354 cout << "PUT userUUID to cancel invitation:";
356 result = accountMgr->cancelInvitation(cmd, cmd2, &onDelete);
360 cout << "PUT groupId to delete invitation:";
362 result = accountMgr->deleteInvitation(cmd, &onDelete);
366 cout << "PUT groupId to cancel observe:";
368 result = accountMgr->cancelObserveGroup(cmd);
372 result = accountMgr->cancelObserveInvitation();
380 if (result != OC_STACK_OK)
382 cout << "Error, return code: " << result << endl;
387 cout << "Precondition failed." << endl;