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 - observeGroup" << endl;
221 cout << " 6 - createGroup" << endl;
222 cout << " 7 - deleteGroup" << endl;
223 cout << " 8 - getGroupInfoAll" << endl;
224 cout << " 9 - getGroupInfo" << endl;
225 cout << " 10 - addPropertyValueToGroup" << endl;
226 cout << " 11 - deletePropertyValueFromGroup" << endl;
227 cout << " 12 - updatePropertyValueOnGroup" << endl;
228 cout << " 13 - observeInvitation" << endl;
229 cout << " 14 - sendInvitation" << endl;
230 cout << " 15 - cancelInvitation" << endl;
231 cout << " 16 - deleteInvitation" << endl;
232 cout << " 17 - cancelObserveGroup" << endl;
233 cout << " 18 - cancelObserveInvitation" << endl;
234 cout << " 20 - exit" << endl;
245 QueryParamsMap query;
246 OCRepresentation rep;
248 switch (atoi(cmd.c_str()))
251 cout << "Put userUUID to search:" << endl;
254 result = accountMgr->searchUser(query, &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);
273 string accessToken, deviceId;
275 cout << "PUT accessToken:";
278 cout << "PUT deviceID to delete:";
281 result = accountMgr->deleteDevice(accessToken, deviceId, &onDelete);
286 result = accountMgr->observeGroup(&onObserve);
290 result = accountMgr->createGroup(&ocPost);
294 cout << "PUT groupId to delete:";
296 result = accountMgr->deleteGroup(cmd, &onDelete);
300 result = accountMgr->getGroupInfoAll(&ocPost);
304 cout << "PUT groupId to get info:";
306 result = accountMgr->getGroupInfo(cmd, &ocPost);
311 string groupId, property, value;
312 vector<string> values;
313 OCRepresentation propertyValue;
315 cout << "PUT groupId to add property values:";
318 cout << "PUT property name:";
321 cout << "PUT value:";
324 values.push_back(value);
325 propertyValue.setValue<vector<string>>(property, values);
327 accountMgr->addPropertyValueToGroup(groupId, propertyValue, &ocPost);
333 string groupId, property, value;
334 vector<string> values;
335 OCRepresentation propertyValue;
337 cout << "PUT groupId to delete property values:";
340 cout << "PUT property name:";
343 cout << "PUT value:";
346 values.push_back(value);
347 propertyValue.setValue<vector<string>>(property, values);
349 accountMgr->deletePropertyValueFromGroup(groupId, propertyValue, &ocPost);
355 string groupId, property, value;
356 OCRepresentation propertyValue;
358 cout << "PUT groupId to update property values:";
361 cout << "PUT property name:";
365 cout << "PUT value type(1:string / 2:array):";
368 cout << "PUT value:";
373 propertyValue.setValue<string>(property, value);
377 vector<string> values;
378 values.push_back(value);
379 propertyValue.setValue<vector<string>>(property, values);
386 accountMgr->updatePropertyValueOnGroup(groupId, propertyValue, &ocPost);
391 result = accountMgr->observeInvitation(&onObserve);
395 cout << "PUT groupId to invite:";
397 cout << "PUT userUUID to invite:";
399 result = accountMgr->sendInvitation(cmd, cmd2, &ocPost);
403 cout << "PUT groupId to cancel invitation:";
405 cout << "PUT userUUID to cancel invitation:";
407 result = accountMgr->cancelInvitation(cmd, cmd2, &onDelete);
411 cout << "PUT groupId to reply to invitation:";
413 cout << "accept to invitation? (1:yes)";
418 result = accountMgr->replyToInvitation(cmd, true, &onDelete);
422 result = accountMgr->replyToInvitation(cmd, false, &onDelete);
427 result = accountMgr->cancelObserveGroup();
431 result = accountMgr->cancelObserveInvitation();
442 if (result != OC_STACK_OK)
444 cout << "Error, return code: " << result << endl;
449 cout << "Precondition failed." << endl;