/* ****************************************************************
-*
-* Copyright 2016 Samsung Electronics All Rights Reserved.
-*
-*
-*
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*
-******************************************************************/
-
-#include <memory>
-#include <iostream>
-#include <stdexcept>
+ *
+ * Copyright 2016 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************/
+
+#include "OCPlatform.h"
+#include "OCApi.h"
+#include "RDClient.h"
#include <condition_variable>
-#include <map>
-#include <vector>
-#include <string>
-#include <unistd.h>
-#include <stdio.h>
-
-#include "ocstack.h"
-#include "ocpayload.h"
-
-#include <OCApi.h>
-#include <OCPlatform.h>
using namespace std;
using namespace OC;
+condition_variable g_callbackLock;
+string g_accesstoken, g_refreshtoken, g_tokentype, g_uid, g_redirectUri, g_certificate, g_serverId;
+int g_expiresin;
+
+class Resource
+{
+public:
+ OCResourceHandle m_handle;
+ Resource(string uri, vector< string > rt, vector< string > itf)
+ {
+ m_representation.setUri(uri);
+ m_representation.setResourceTypes(rt);
+ m_representation.setResourceInterfaces(itf);
+ }
+
+ string getResourceUri()
+ {
+ return m_representation.getUri();
+ }
+
+ vector< string > getResourceType()
+ {
+ return m_representation.getResourceTypes();
+ }
+
+ vector< string > getInterfaces()
+ {
+ return m_representation.getResourceInterfaces();
+ }
+
+ OCRepresentation getRepresentation(void)
+ {
+ m_representation.clearChildren();
+ for (auto it = m_childResources.begin(); it != m_childResources.end(); it++)
+ {
+ m_representation.addChild((*it)->getRepresentation());
+ }
+ return m_representation;
+ }
+
+ OCStackResult addChildResource(Resource *childResource)
+ {
+ m_childResources.push_back(childResource);
+ return OCPlatform::bindResource(m_handle, childResource->m_handle);
+ }
+
+ OCStackResult sendRepresentation(shared_ptr< OCResourceRequest > pRequest)
+ {
+ auto pResponse = make_shared< OC::OCResourceResponse >();
+ pResponse->setRequestHandle(pRequest->getRequestHandle());
+ pResponse->setResourceHandle(pRequest->getResourceHandle());
+
+ // Check for query params (if any)
+ QueryParamsMap queryParamsMap = pRequest->getQueryParameters();
+
+ cout << "\t\t\tquery params: \n";
+ for (auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
+ {
+ cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
+ }
+
+ auto findRes = queryParamsMap.find("if");
+
+ if (findRes != queryParamsMap.end())
+ {
+ pResponse->setResourceRepresentation(getRepresentation(), findRes->second);
+ }
+ else
+ {
+ pResponse->setResourceRepresentation(getRepresentation(), DEFAULT_INTERFACE);
+ }
+
+ pResponse->setErrorCode(200);
+ pResponse->setResponseResult(OC_EH_OK);
+
+ return OCPlatform::sendResponse(pResponse);
+ }
+
+ OCStackResult propagate()
+ {
+ if (m_interestedObservers.size() > 0)
+ {
+ shared_ptr<OCResourceResponse> resourceResponse =
+ { make_shared<OCResourceResponse>()};
+
+ resourceResponse->setErrorCode(200);
+ resourceResponse->setResourceRepresentation(getRepresentation(), DEFAULT_INTERFACE);
+
+ return OCPlatform::notifyListOfObservers(m_handle,
+ m_interestedObservers,
+ resourceResponse);
+ }
+
+ return OC_STACK_OK;
+ }
+
+ virtual OCEntityHandlerResult entityHandler(shared_ptr<OCResourceRequest> request) = 0;
+
+protected:
+ OCRepresentation m_representation;
+ vector<Resource *> m_childResources;
+ ObservationIds m_interestedObservers;
+};
+
+class BinarySwitchResource: public Resource //oic.r.switch.binary
+{
+private:
+ bool m_value;
+
+public:
+ BinarySwitchResource(string uri, vector< string > rt, vector< string > itf) :
+ Resource(uri, rt, itf)
+ {
+ m_value = false;
+ m_representation.setValue("value", m_value);
+ }
+
+ void setBinarySwitchRepresentation(OCRepresentation &rep)
+ {
+ bool value;
+ if (rep.getValue("value", value))
+ {
+ m_value = value;
+ m_representation.setValue("value", m_value);
+ cout << "\t\t\t\t" << "value: " << m_value << endl;
+
+ propagate();
+ }
+ }
+
+ OCEntityHandlerResult entityHandler(shared_ptr< OCResourceRequest > request)
+ {
+ cout << "\tIn Server Binaryswitch entity handler:\n";
+ OCEntityHandlerResult ehResult = OC_EH_ERROR;
+
+ if (request)
+ {
+ // Get the request type and request flag
+ string requestType = request->getRequestType();
+ int requestFlag = request->getRequestHandlerFlag();
+
+ if (requestFlag & RequestHandlerFlag::RequestFlag)
+ {
+ cout << "\t\trequestFlag : Request\n";
+
+ // If the request type is GET
+ if (requestType == "GET")
+ {
+ cout << "\t\t\trequestType : GET\n";
+ if (OC_STACK_OK == sendRepresentation(request))
+ {
+ ehResult = OC_EH_OK;
+ }
+ }
+ else if (requestType == "PUT")
+ {
+ cout << "\t\t\trequestType : PUT\n";
+ // PUT request operations
+ }
+ else if (requestType == "POST")
+ {
+ cout << "\t\t\trequestType : POST\n";
+ // POST request operations
+ OCRepresentation rep = request->getResourceRepresentation();
+ setBinarySwitchRepresentation(rep);
+
+ if (OC_STACK_OK == sendRepresentation(request))
+ {
+ ehResult = OC_EH_OK;
+ }
+ }
+ else if (requestType == "DELETE")
+ {
+ cout << "\t\t\trequestType : DELETE\n";
+ // DELETE request operations
+ }
+ }
+
+ if (requestFlag & RequestHandlerFlag::ObserverFlag)
+ {
+ cout << "\t\trequestFlag : Observer\n";
+
+ ObservationInfo observationInfo = request->getObservationInfo();
+ if (ObserveAction::ObserveRegister == observationInfo.action)
+ {
+ m_interestedObservers.push_back(observationInfo.obsId);
+ }
+ else if (ObserveAction::ObserveUnregister == observationInfo.action)
+ {
+ m_interestedObservers.erase(
+ remove(m_interestedObservers.begin(), m_interestedObservers.end(),
+ observationInfo.obsId), m_interestedObservers.end());
+ }
+ }
+ }
+ else
+ {
+ cout << "Request invalid" << endl;
+ }
+
+ return ehResult;
+ }
+};
+
+OCAccountManager::Ptr accountMgr;
+
+void printUsage()
+{
+ cout << endl << "---Group & Invite sample---" << endl;
+ cout << " 1 - searchUser using user UUID" << endl;
+ cout << " 2 - searchUser using email" << endl;
+ cout << " 3 - searchUser using phone" << endl;
+ cout << " 4 - deleteDevice" << endl;
+ cout << " 5 - observeGroup" << endl;
+ cout << " 6 - createGroup" << endl;
+ cout << " 7 - deleteGroup" << endl;
+ cout << " 8 - getGroupInfoAll" << endl;
+ cout << " 9 - getGroupInfo" << endl;
+ cout << " 10 - addPropertyValueToGroup" << endl;
+ cout << " 11 - deletePropertyValueFromGroup" << endl;
+ cout << " 12 - updatePropertyValueOnGroup" << endl;
+ cout << " 13 - observeInvitation" << endl;
+ cout << " 14 - sendInvitation" << endl;
+ cout << " 15 - cancelInvitation" << endl;
+ cout << " 16 - deleteInvitation" << endl;
+ cout << " 17 - cancelObserveGroup" << endl;
+ cout << " 18 - cancelObserveInvitation" << endl;
+ cout << " 30 - resource discover" << endl;
+ cout << " 31 - example resource publish" << endl;
+ cout << " 41 - get my info (device Id, accesstoken, user uuid)" << endl;
+ cout << " 50 - exit" << endl;
+}
+
void printRepresentation(OCRepresentation rep)
{
for (auto itr = rep.begin(); itr != rep.end(); ++itr)
switch (itr->base_type())
{
case AttributeType::OCRepresentation:
- for (auto itr2 : (*itr).getValue<vector<OCRepresentation> >())
+ for (auto itr2 : (*itr).getValue< vector< OCRepresentation > >())
{
printRepresentation(itr2);
}
break;
case AttributeType::Integer:
- for (auto itr2 : (*itr).getValue<vector<int> >())
+ for (auto itr2 : (*itr).getValue< vector< int > >())
{
cout << "\t\t" << itr2 << endl;
}
break;
case AttributeType::String:
- for (auto itr2 : (*itr).getValue<vector<string> >())
+ for (auto itr2 : (*itr).getValue< vector< string > >())
{
cout << "\t\t" << itr2 << endl;
}
}
else if (itr->type() == AttributeType::OCRepresentation)
{
- printRepresentation((*itr).getValue<OCRepresentation>());
+ printRepresentation((*itr).getValue< OCRepresentation >());
}
}
}
//tmp callback
-void ocPost(const HeaderOptions & /*headerOptions*/,
- const OCRepresentation &rep, const int eCode)
+void onPost(const HeaderOptions & /*headerOptions*/, const OCRepresentation &rep, const int eCode)
{
if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CHANGED)
{
{
cout << "\tResponse error: " << eCode << endl;
}
+ printUsage();
}
-void onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation &rep,
- const int &eCode, const int &sequenceNumber)
+void onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation &rep, const int &eCode,
+ const int &sequenceNumber)
{
try
{
{
if (eCode == OC_STACK_OK)
{
- cout << "Observe registration failed or de-registration action failed/succeeded" << endl;
+ cout << "Observe registration failed or de-registration action failed/succeeded"
+ << endl;
}
else
{
{
cout << "Exception: " << e.what() << " in onObserve" << endl;
}
+ printUsage();
+}
+
+void onPublish(const OCRepresentation &, const int &eCode)
+{
+ cout << "Publish resource response received, code: " << eCode << endl;
+
+ g_callbackLock.notify_all();
+ printUsage();
+}
+
+shared_ptr< OCResource > g_Resource;
+
+void onFoundResource(shared_ptr< OCResource > resource)
+{
+ cout << "In foundResource\n";
+ string resourceURI;
+ string hostAddress;
+
+ try
+ {
+ // Do some operations with resource object.
+ if (resource)
+ {
+ g_Resource = resource;
+ cout << "DISCOVERED Resource:" << endl;
+ // Get the resource URI
+ resourceURI = resource->uri();
+ cout << "\tURI of the resource: " << resourceURI << endl;
+
+ // Get the resource host address
+ hostAddress = resource->host();
+ cout << "\tHost address of the resource: " << hostAddress << endl;
+
+ // Get the resource types
+ cout << "\tList of resource types: " << endl;
+ for (auto &resourceTypes : resource->getResourceTypes())
+ {
+ cout << "\t\t" << resourceTypes << endl;
+ }
+
+ // Get the resource interfaces
+ cout << "\tList of resource interfaces: " << endl;
+ for (auto &resourceInterfaces : resource->getResourceInterfaces())
+ {
+ cout << "\t\t" << resourceInterfaces << endl;
+ }
+ }
+ else
+ {
+ // Resource is invalid
+ cout << "Resource is invalid" << endl;
+ }
+
+ }
+ catch (exception& e)
+ {
+ cerr << "Exception in foundResource: " << e.what() << endl;
+ }
}
-void onDelete(const HeaderOptions & /*headerOptions*/,
- const int eCode)
+void printResource(const OCRepresentation &rep)
+{
+ cout << "URI: " << rep.getUri() << endl;
+
+ vector< string > rt = rep.getResourceTypes();
+ for (auto it = rt.begin(); it != rt.end(); it++)
+ {
+ cout << "RT: " << (*it) << endl;
+ }
+
+ for (auto it = rep.begin(); it != rep.end(); it++)
+ {
+ cout << it->attrname() << " : " << it->getValueToString() << endl;
+ }
+
+ vector< OCRepresentation > children = rep.getChildren();
+
+ for (auto it = children.begin(); it != children.end(); it++)
+ {
+ printResource(*it);
+ }
+ printUsage();
+}
+
+void getCollectionResource(const HeaderOptions &, const OCRepresentation &rep, const int ecode)
+{
+ cout << "Resource get: " << ecode << endl;
+
+ printResource(rep);
+}
+
+void onDelete(const HeaderOptions & /*headerOptions*/, const int eCode)
{
if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_DELETED)
{
{
cout << "\tDelete Response error: " << eCode << endl;
}
+ printUsage();
}
-condition_variable g_callbackLock;
-string g_uid;
-string g_accesstoken;
+void onSignUp(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
+{
+ if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CHANGED)
+ {
+ cout << "\tSign-up request was successful" << endl;
+
+ printRepresentation(rep);
+
+ // Mandatory field
+ g_accesstoken = rep.getValue < string > ("accesstoken");
+ g_refreshtoken = rep.getValue < string > ("refreshtoken");
+ g_tokentype = rep.getValue < string > ("tokentype");
+ g_uid = rep.getValue < string > ("uid");
+
+ // Optional field
+ if (rep.hasAttribute("expiresin"))
+ {
+ g_expiresin = rep.getValue< int >("expiresin");
+ }
+ if (rep.hasAttribute("redirecturi"))
+ {
+ g_redirectUri = rep.getValue < string > ("redirecturi");
+ }
+ if (rep.hasAttribute("certificate"))
+ {
+ g_certificate = rep.getValue < string > ("certificate");
+ }
+ if (rep.hasAttribute("sid"))
+ {
+ g_serverId = rep.getValue < string > ("sid");
+ }
+ }
+ else
+ {
+ cout << "\tSign-up Response error: " << eCode << endl;
+ }
+ g_callbackLock.notify_all();
+}
-void handleLoginoutCB(const HeaderOptions &,
- const OCRepresentation &rep, const int ecode)
+void handleLoginoutCB(const HeaderOptions &, const OCRepresentation &rep, const int ecode)
{
cout << "Auth response received code: " << ecode << endl;
printRepresentation(rep);
}
- if (ecode == 4)
- {
- g_accesstoken = rep.getValueToString("accesstoken");
+ g_callbackLock.notify_all();
+}
- g_uid = rep.getValueToString("uid");
+int insertNumber()
+{
+ int var;
+ while (1)
+ {
+ cin >> var;
+ if (cin.fail() == 1)
+ {
+ cin.clear();
+ cin.ignore(100, '\n');
+ }
+ else
+ {
+ return var;
+ }
}
-
- g_callbackLock.notify_all();
}
int main(int argc, char *argv[])
if (argc != 4 && argc != 5)
{
cout << "Put \"[host-ipaddress:port] [authprovider] [authcode]\" for sign-up and sign-in"
- << endl;
- cout << "Put \"[host-ipaddress:port] [uid] [accessToken] 1\" for sign-in" <<
- endl;
+ << endl;
+ cout << "Put \"[host-ipaddress:port] [uid] [accessToken] 1\" for sign-in" << endl;
return 0;
}
-
PlatformConfig cfg
- {
- ServiceType::InProc,
- ModeType::Both,
- "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
- 0, // Uses randomly available port
- QualityOfService::LowQos
- };
-
+ { ServiceType::InProc, ModeType::Both, "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
+ 0, // Uses randomly available port
+ QualityOfService::LowQos };
OCPlatform::Configure(cfg);
-
OCStackResult result = OC_STACK_ERROR;
string host = "coap+tcp://";
host += argv[1];
- OCAccountManager::Ptr accountMgr = OCPlatform::constructAccountManagerObject(host,
- CT_ADAPTER_TCP);
+ accountMgr = OCPlatform::constructAccountManagerObject(host, CT_ADAPTER_TCP);
mutex blocker;
- unique_lock<mutex> lock(blocker);
+ unique_lock < mutex > lock(blocker);
if (argc == 5)
{
}
else
{
- accountMgr->signUp(argv[2], argv[3], &handleLoginoutCB);
+ accountMgr->signUp(argv[2], argv[3], &onSignUp);
g_callbackLock.wait(lock);
accountMgr->signIn(g_uid, g_accesstoken, &handleLoginoutCB);
g_callbackLock.wait(lock);
}
- cout << "---Group & Invite sample---" << endl;
- cout << " 1 - searchUser using user UUID" << endl;
- cout << " 2 - searchUser using email" << endl;
- cout << " 3 - searchUser using phone" << endl;
- cout << " 4 - deleteDevice" << endl;
- cout << " 5 - observeGroup" << endl;
- cout << " 6 - createGroup" << endl;
- cout << " 7 - deleteGroup" << endl;
- cout << " 8 - getGroupInfoAll" << endl;
- cout << " 9 - getGroupInfo" << endl;
- cout << " 10 - addPropertyValueToGroup" << endl;
- cout << " 11 - deletePropertyValueFromGroup" << endl;
- cout << " 12 - updatePropertyValueOnGroup" << endl;
- cout << " 13 - observeInvitation" << endl;
- cout << " 14 - sendInvitation" << endl;
- cout << " 15 - cancelInvitation" << endl;
- cout << " 16 - deleteInvitation" << endl;
- cout << " 17 - cancelObserveGroup" << endl;
- cout << " 18 - cancelObserveInvitation" << endl;
- cout << " 20 - exit" << endl;
+ cout << "Registering resources to platform..." << endl;
+
+ BinarySwitchResource binarySwitch("/power/0",
+ { "oic.r.switch.binary" },
+ { DEFAULT_INTERFACE });
+
+ printUsage();
string cmd;
+ int intCmd;
string cmd2;
+ string uri;
+ string itf;
+ string rt;
while (true)
{
- cin >> cmd;
-
+ intCmd = insertNumber();
try
{
QueryParamsMap query;
OCRepresentation rep;
- switch (atoi(cmd.c_str()))
- {
- case 1:
- cout << "Put userUUID to search:" << endl;
- cin >> cmd;
- query["uid"] = cmd;
- result = accountMgr->searchUser(query, &ocPost);
- break;
-
- case 2:
- cout << "Put email to search:" << endl;
- cin >> cmd;
- query["email"] = cmd;
- result = accountMgr->searchUser(query, &ocPost);
- break;
-
- case 3:
- cout << "Put phone number to search:" << endl;
- cin >> cmd;
- query["phone"] = cmd;
- result = accountMgr->searchUser(query, &ocPost);
- break;
-
- case 4:
+ switch (intCmd)
{
- string accessToken, deviceId;
+ case 1:
+ cout << "Put userUUID to search:" << endl;
+ cin >> cmd;
+ query["uid"] = cmd;
+ result = accountMgr->searchUser(query, &onPost);
+ break;
- cout << "PUT accessToken:";
- cin >> accessToken;
+ case 2:
+ cout << "Put email to search:" << endl;
+ cin >> cmd;
+ query["email"] = cmd;
+ result = accountMgr->searchUser(query, &onPost);
+ break;
- cout << "PUT deviceID to delete:";
- cin >> deviceId;
+ case 3:
+ cout << "Put phone number to search:" << endl;
+ cin >> cmd;
+ query["phone"] = cmd;
+ result = accountMgr->searchUser(query, &onPost);
+ break;
- result = accountMgr->deleteDevice(accessToken, deviceId, &onDelete);
- break;
- }
+ case 4:
+ {
+ string accessToken, deviceId;
+
+ cout << "PUT accessToken:";
+ cin >> accessToken;
+
+ cout << "PUT deviceID to delete:";
+ cin >> deviceId;
- case 5:
- result = accountMgr->observeGroup(&onObserve);
- break;
+ result = accountMgr->deleteDevice(accessToken, deviceId, &onDelete);
+ break;
+ }
- case 6:
- result = accountMgr->createGroup(&ocPost);
- break;
+ case 5:
+ result = accountMgr->observeGroup(&onObserve);
+ break;
- case 7:
- cout << "PUT groupId to delete:";
- cin >> cmd;
- result = accountMgr->deleteGroup(cmd, &onDelete);
- break;
+ case 6:
+ {
+ int opt;
+ cout
+ << "\n---------------------------------------------------------------------\n";
+ cout << " w/ optional field? (1:yes / 2:no)" << endl;
+ cout
+ << "---------------------------------------------------------------------\n\n";
+ cin >> opt;
+
+ if (opt == 1)
+ {
+ QueryParamsMap queryParam =
+ { };
+ string key, value;
+
+ int n;
+ cout << "\nnum of field : ";
+ n = insertNumber();
+
+ for (int i = 0; i < n; i++)
+ {
+ cout << "query key(ex: gname/parent): ";
+ cin >> key;
+ cout << "query value: ";
+ cin >> value;
+ queryParam.insert(pair< string, string >(key, value));
+ }
+ result = accountMgr->createGroup(queryParam, &onPost);
+ }
+ else if (opt == 2)
+ {
+ result = accountMgr->createGroup(&onPost);
+ }
+ else
+ {
+ cout << "invalid option" << endl;
+ }
- case 8:
- result = accountMgr->getGroupInfoAll(&ocPost);
- break;
+ break;
+ }
+ case 7:
+ cout << "PUT groupId to delete:";
+ cin >> cmd;
+ result = accountMgr->deleteGroup(cmd, &onDelete);
+ break;
- case 9:
- cout << "PUT groupId to get info:";
- cin >> cmd;
- result = accountMgr->getGroupInfo(cmd, &ocPost);
- break;
+ case 8:
+ result = accountMgr->getGroupInfoAll(&onPost);
+ break;
- case 10:
- {
- string groupId, property, value;
- vector<string> values;
- OCRepresentation propertyValue;
+ case 9:
+ cout << "PUT groupId to get info:";
+ cin >> cmd;
+ result = accountMgr->getGroupInfo(cmd, &onPost);
+ break;
- cout << "PUT groupId to add property values:";
- cin >> groupId;
+ case 10:
+ {
+ string groupId;
+ cout << "group ID: ";
+ cin >> groupId;
- cout << "PUT property name:";
- cin >> property;
+ int n;
+ cout << "num of property : ";
+ n = insertNumber();
- cout << "PUT value:";
- cin >> value;
+ OCRepresentation propertyValue;
- values.push_back(value);
- propertyValue.setValue<vector<string>>(property, values);
+ for (int i = 0; i < n; i++)
+ {
+ string key, value;
+ vector< string > values;
- accountMgr->addPropertyValueToGroup(groupId, propertyValue, &ocPost);
- break;
- }
+ cout << "property(ex: members/devices): ";
+ cin >> key;
- case 11:
- {
- string groupId, property, value;
- vector<string> values;
- OCRepresentation propertyValue;
+ int m;
+ cout << "\tnum of values : ";
+ m = insertNumber();
- cout << "PUT groupId to delete property values:";
- cin >> groupId;
+ for (int j = 0; j < m; j++)
+ {
+ cout << "\tvalue: ";
+ cin >> value;
+ values.push_back(value);
+ }
- cout << "PUT property name:";
- cin >> property;
+ propertyValue.setValue < vector < string >> (key, values);
+ }
- cout << "PUT value:";
- cin >> value;
+ result = accountMgr->addPropertyValueToGroup(groupId, propertyValue, &onPost);
+ break;
+ }
- values.push_back(value);
- propertyValue.setValue<vector<string>>(property, values);
+ case 11:
+ {
+ string groupId;
+ cout << "group ID: ";
+ cin >> groupId;
- accountMgr->deletePropertyValueFromGroup(groupId, propertyValue, &ocPost);
- break;
- }
+ int n;
+ cout << "number of properties : ";
+ n = insertNumber();
- case 12:
- {
- string groupId, property, value;
- OCRepresentation propertyValue;
+ OCRepresentation propertyValue;
- cout << "PUT groupId to update property values:";
- cin >> groupId;
+ for (int i = 0; i < n; i++)
+ {
+ string key, value;
+ vector< string > values;
- cout << "PUT property name:";
- cin >> property;
+ cout << "property(ex: members/devices): ";
+ cin >> key;
- int type;
- cout << "PUT value type(1:string / 2:array):";
- cin >> type;
+ int m;
+ cout << "\tnum of values : ";
+ m = insertNumber();
- cout << "PUT value:";
- cin >> value;
+ for (int j = 0; j < m; j++)
+ {
+ cout << "\tvalue: ";
+ cin >> value;
+ values.push_back(value);
+ }
- if (1 == type)
- {
- propertyValue.setValue<string>(property, value);
- }
- else if (2 == type)
- {
- vector<string> values;
- values.push_back(value);
- propertyValue.setValue<vector<string>>(property, values);
+ propertyValue.setValue < vector < string >> (key, values);
+ }
+
+ result = accountMgr->deletePropertyValueFromGroup(groupId, propertyValue,
+ &onPost);
}
- else
+
+ case 12:
{
+ string groupId;
+ cout << "group ID: ";
+ cin >> groupId;
+
+ int n;
+ cout << "num of property : ";
+ n = insertNumber();
+
+ OCRepresentation propertyValue;
+
+ for (int i = 0; i < n; i++)
+ {
+ string key, value;
+
+ cout << "property(ex: gname/devices): ";
+ cin >> key;
+
+ int opt;
+ cout << "\tvalue type?(1:string / 2:array) : ";
+ opt = insertNumber();
+
+ if (opt == 1)
+ {
+ cout << "\tvalue: ";
+ cin >> value;
+ propertyValue.setValue < string > (key, value);
+ }
+ else if (opt == 2)
+ {
+ vector< string > values;
+
+ int m;
+ cout << "\tnum of values : ";
+ m = insertNumber();
+
+ for (int j = 0; j < m; j++)
+ {
+ cout << "\tvalue: ";
+ cin >> value;
+ values.push_back(value);
+ }
+
+ propertyValue.setValue < vector < string >> (key, values);
+ }
+ else
+ {
+ cout << "invalid option" << endl;
+ break;
+ }
+ }
+
+ result = accountMgr->updatePropertyValueOnGroup(groupId, propertyValue,
+ &onPost);
break;
}
+ case 13:
+ result = accountMgr->observeInvitation(&onObserve);
+ break;
- accountMgr->updatePropertyValueOnGroup(groupId, propertyValue, &ocPost);
- break;
- }
+ case 14:
+ cout << "PUT groupId to invite:";
+ cin >> cmd;
+ cout << "PUT userUUID to invite:";
+ cin >> cmd2;
+ result = accountMgr->sendInvitation(cmd, cmd2, &onPost);
+ break;
- case 13:
- result = accountMgr->observeInvitation(&onObserve);
- break;
-
- case 14:
- cout << "PUT groupId to invite:";
- cin >> cmd;
- cout << "PUT userUUID to invite:";
- cin >> cmd2;
- result = accountMgr->sendInvitation(cmd, cmd2, &ocPost);
- break;
-
- case 15:
- cout << "PUT groupId to cancel invitation:";
- cin >> cmd;
- cout << "PUT userUUID to cancel invitation:";
- cin >> cmd2;
- result = accountMgr->cancelInvitation(cmd, cmd2, &onDelete);
- break;
-
- case 16:
- cout << "PUT groupId to reply to invitation:";
- cin >> cmd;
- cout << "accept to invitation? (1:yes)";
- cin >> cmd2;
-
- if (cmd2 == "1")
- {
- result = accountMgr->replyToInvitation(cmd, true, &onDelete);
- }
- else
+ case 15:
+ cout << "PUT groupId to cancel invitation:";
+ cin >> cmd;
+ cout << "PUT userUUID to cancel invitation:";
+ cin >> cmd2;
+ result = accountMgr->cancelInvitation(cmd, cmd2, &onDelete);
+ break;
+
+ case 16:
+ cout << "PUT groupId to reply to invitation:";
+ cin >> cmd;
+ cout << "accept to invitation? (1:yes)";
+ cin >> cmd2;
+
+ if (cmd2 == "1")
+ {
+ result = accountMgr->replyToInvitation(cmd, true, &onDelete);
+ }
+ else
+ {
+ result = accountMgr->replyToInvitation(cmd, false, &onDelete);
+ }
+ break;
+
+ case 17:
+ result = accountMgr->cancelObserveGroup();
+ break;
+
+ case 18:
+ result = accountMgr->cancelObserveInvitation();
+ break;
+ case 30:
+ int opt;
+ cout
+ << "\n---------------------------------------------------------------------\n";
+ cout << " w/ query? (1:yes / 2:no)" << endl;
+ cout
+ << "---------------------------------------------------------------------\n\n";
+ opt = insertNumber();
+ uri = OC_RSRVD_WELL_KNOWN_URI;
+ if (opt == 1)
+ {
+ string query;
+ cout << "\ninsert query : ";
+ cin >> query;
+ uri += "?";
+ uri += query;
+ }
+ result = OC::OCPlatform::findResource(accountMgr->host(), uri,
+ accountMgr->connectivityType(), &onFoundResource);
+ break;
+ case 31:
{
- result = accountMgr->replyToInvitation(cmd, false, &onDelete);
- }
- break;
+ uri = binarySwitch.getResourceUri();
+ rt = binarySwitch.getResourceType()[0];
+ itf = binarySwitch.getInterfaces()[0];
+ cout << " RESOURCE URI: " << uri << endl;
+ cout << " RESOURCE RT: " << rt << endl;
+ cout << " RESOURCE ITF: " << itf << endl;
+
+ result = OCPlatform::registerResource(binarySwitch.m_handle, uri, rt, itf,
+ bind(&BinarySwitchResource::entityHandler, &binarySwitch,
+ placeholders::_1), OC_OBSERVABLE);
+
+ if (result != OC_STACK_OK)
+ {
+ cout << "Resource registration was unsuccessful" << endl;
+ }
- case 17:
- result = accountMgr->cancelObserveGroup();
- break;
+ cout << "Publishing resources to cloud \n\n";
+ ResourceHandles resourceHandles;
+ OCDeviceInfo devInfoBinarySwitch;
+ OCStringLL deviceType;
- case 18:
- result = accountMgr->cancelObserveInvitation();
- break;
+ deviceType.value = "oic.d.binaryswitch";
+ deviceType.next = NULL;
+ devInfoBinarySwitch.deviceName = "FAC_2016";
+ devInfoBinarySwitch.types = &deviceType;
+ devInfoBinarySwitch.specVersion = NULL;
+ devInfoBinarySwitch.dataModelVersions = NULL;
- case 20:
- goto exit;
- break;
+ OCPlatform::registerDeviceInfo(devInfoBinarySwitch);
- default:
- break;
- }
+ resourceHandles.push_back(binarySwitch.m_handle);
+
+ result = RDClient::Instance().publishResourceToRD(host,
+ OCConnectivityType::CT_ADAPTER_TCP, resourceHandles, &onPublish);
+ cout << " result: " << result
+ << " Waiting Publish user resource response from cloud" << endl;
+ g_callbackLock.wait(lock);
+ break;
+ }
+ case 41:
+ {
+ cout << "my user uuid : " << g_uid << endl;
+ cout << "my accesstoken : " << g_accesstoken << endl;
+ cout << "my refreshtoken : " << g_refreshtoken << endl;
+ cout << "my tokentype : " << g_tokentype << endl;
+ cout << "my serverId : " << g_serverId << endl;
+
+ result = OC_STACK_OK;
+ break;
+ }
+ case 50:
+ goto exit;
+ break;
+
+ default:
+ break;
+ }
if (result != OC_STACK_OK)
{
cout << "Error, return code: " << result << endl;
}
}
-exit:
- return 0;
+ exit: return 0;
}
+
+++ /dev/null
-#include <memory>
-#include <iostream>
-#include <stdexcept>
-#include <condition_variable>
-#include <map>
-#include <vector>
-#include <string>
-#include <unistd.h>
-
-#include "ocstack.h"
-#include "ocpayload.h"
-#include "RDClient.h"
-
-#include <OCApi.h>
-#include <OCPlatform.h>
-
-#define maxSequenceNumber 0xFFFFFF
-
-using namespace OC;
-using namespace std;
-
-string g_host = "coap+tcp://";
-condition_variable g_callbackLock;
-
-class LightResource
-{
-public:
- /// Access this property from a TB client
- std::string m_power;
- std::string m_lightUri;
- OCResourceHandle m_resourceHandle;
- OCRepresentation m_lightRep;
-
- /// Constructor
- LightResource() :
- m_power(""), m_lightUri("/a/light")
- {
- // Initialize representation
- m_lightRep.setUri(m_lightUri);
- m_lightRep.setValue("power", m_power);
- }
-
- /// This function internally calls registerResource API.
- void createResource()
- {
- std::string resourceURI = m_lightUri; //URI of the resource
- std::string resourceTypeName = "core.light"; //resource type name. In this case, it is light
- std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
-
- EntityHandler cb = std::bind(&LightResource::entityHandler, this, std::placeholders::_1);
-
- // This will internally create and register the resource.
- OCStackResult result = OCPlatform::registerResource(m_resourceHandle, resourceURI,
- resourceTypeName, resourceInterface, cb, OC_DISCOVERABLE | OC_OBSERVABLE);
-
- if (OC_STACK_OK != result)
- {
- cout << "Resource creation was unsuccessful\n";
- }
- }
-
- OCRepresentation post(OCRepresentation &rep)
- {
- m_power = rep.getValueToString("power");
- return get();
- }
-
- // gets the updated representation.
- // Updates the representation with latest internal state before
- // sending out.
- OCRepresentation get()
- {
- m_lightRep.setValue("power", m_power);
-
- return m_lightRep;
- }
-
-private:
- // This is just a sample implementation of entity handler.
- // Entity handler can be implemented in several ways by the manufacturer
- OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request)
- {
- cout << "\tIn Server CPP entity handler:\n";
- OCEntityHandlerResult ehResult = OC_EH_ERROR;
- if (request)
- {
- // Get the request type and request flag
- std::string requestType = request->getRequestType();
- int requestFlag = request->getRequestHandlerFlag();
-
- if (requestFlag & RequestHandlerFlag::RequestFlag)
- {
- cout << "\t\trequestFlag : Request\n";
- auto pResponse = std::make_shared< OC::OCResourceResponse >();
- pResponse->setRequestHandle(request->getRequestHandle());
- pResponse->setResourceHandle(request->getResourceHandle());
-
- // If the request type is GET
- if (requestType == "GET")
- {
- cout << "\t\t\trequestType : GET\n";
- pResponse->setErrorCode(200);
- pResponse->setResponseResult(OC_EH_OK);
- pResponse->setResourceRepresentation(get());
- if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
- {
- ehResult = OC_EH_OK;
- }
- }
- else if (requestType == "POST")
- {
- cout << "\t\t\trequestType : POST\n";
-
- OCRepresentation rep = request->getResourceRepresentation();
-
- // Do related operations related to POST request
- OCRepresentation rep_post = post(rep);
- pResponse->setResourceRepresentation(rep_post);
- pResponse->setErrorCode(200);
-
- if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
- {
- ehResult = OC_EH_OK;
- }
- }
- else
- {
- std::cout << "Unhandled request type" << std::endl;
- }
- }
- }
- else
- {
- std::cout << "Request invalid" << std::endl;
- }
-
- return ehResult;
- }
-};
-
-void printRepresentation(OCRepresentation rep)
-{
- for (auto itr = rep.begin(); itr != rep.end(); ++itr)
- {
- cout << "\t" << itr->attrname() << ":\t" << itr->getValueToString() << endl;
- if (itr->type() == AttributeType::Vector)
- {
- switch (itr->base_type())
- {
- case AttributeType::OCRepresentation:
- for (auto itr2 : (*itr).getValue< vector< OCRepresentation > >())
- {
- printRepresentation(itr2);
- }
- break;
-
- case AttributeType::Integer:
- for (auto itr2 : (*itr).getValue< vector< int > >())
- {
- cout << "\t\t" << itr2 << endl;
- }
- break;
-
- case AttributeType::String:
- for (auto itr2 : (*itr).getValue< vector< string > >())
- {
- cout << "\t\t" << itr2 << endl;
- }
- break;
-
- default:
- cout << "Unhandled base type " << itr->base_type() << endl;
- break;
- }
- }
- else if (itr->type() == AttributeType::OCRepresentation)
- {
- printRepresentation((*itr).getValue< OCRepresentation >());
- }
- }
-}
-
-void getResource(const HeaderOptions &, const OCRepresentation &rep, const int ecode)
-{
- cout << "Resource get: " << ecode << endl;
-
- printRepresentation(rep);
-}
-
-void foundMyDevice(shared_ptr< OC::OCResource > resource)
-{
- cout << "Device found: " << resource->uri() << endl;
- cout << "DI: " << resource->sid() << endl;
-
- g_callbackLock.notify_all();
-}
-
-void foundDevice(shared_ptr< OC::OCResource > resource)
-{
- vector < string > rt = resource->getResourceTypes();
-
- cout << "Device found: " << resource->uri() << endl;
- cout << "DI: " << resource->sid() << endl;
-
- QueryParamsMap query;
- resource->get(query, &getResource);
-}
-
-void onObserveGroup(const HeaderOptions /*headerOptions*/, const OCRepresentation &rep,
- const int &eCode, const int /*&sequenceNumber*/)
-{
- cout << "onObserveGroup response received code: " << eCode << endl;
-
- if (eCode == OC_STACK_OK)
- {
- printRepresentation(rep);
-
- vector < string > dilist = rep.getValue < vector< string > > ("dilist");
-
- for (auto itr = dilist.begin(); itr != dilist.end(); ++itr)
- {
- cout << (*itr) << " discovered" << endl;
- if ((*itr) != OCGetServerInstanceIDString())
- {
- cout << "New device joined" << endl;
- string query = "/oic/res?di=";
- query += (*itr);
- OCStackResult result = OC_STACK_ERROR;
-
- cout << "find my resource : " << *itr << endl;
- result = OCPlatform::findResource(g_host, query,
- static_cast< OCConnectivityType >(CT_ADAPTER_TCP | CT_IP_USE_V4),
- &foundDevice);
- cout << " result: " << result << endl;
- break;
- }
- }
- }
- g_callbackLock.notify_all();
-}
-
-string g_invitedGroup;
-void onInvite(const HeaderOptions /*headerOptions*/, const OCRepresentation &rep, const int &eCode,
- const int &sequenceNumber)
-{
- cout << "onInvite response received code: " << eCode << endl;
-
- if (eCode == OC_STACK_OK)
- {
- printRepresentation(rep);
-
- if (sequenceNumber != OC_OBSERVE_REGISTER)
- {
- vector < OCRepresentation > invited = rep.getValue < vector< OCRepresentation >
- > ("invited");
-
- g_invitedGroup = invited[0].getValueToString("gid");
- }
- }
-
- g_callbackLock.notify_all();
-}
-
-string g_gid;
-void onCreateGroup(const HeaderOptions &, const OCRepresentation &rep, const int ecode)
-{
- cout << "onCreateGroup response received code: " << ecode << endl;
-
- if (ecode == 4)
- {
- printRepresentation(rep);
- g_gid = rep.getValueToString("gid");
- }
-
- g_callbackLock.notify_all();
-}
-
-void onPublish(const OCRepresentation &, const int &eCode)
-{
- cout << "Publish resource response received, code: " << eCode << endl;
- g_callbackLock.notify_all();
-}
-
-void onPost(const HeaderOptions & /*headerOptions*/, const OCRepresentation &rep, const int eCode)
-{
- if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CHANGED)
- {
- cout << "\tRequest was successful: " << eCode << endl;
-
- printRepresentation(rep);
- }
- else
- {
- cout << "\tResponse error: " << eCode << endl;
- }
-
- g_callbackLock.notify_all();
-}
-
-string g_uid;
-string g_accesstoken;
-
-void handleLoginoutCB(const HeaderOptions &, const OCRepresentation &rep, const int ecode)
-{
- cout << "Auth response received code: " << ecode << endl;
-
- if (rep.getPayload() != NULL)
- {
- printRepresentation(rep);
- }
-
- if (ecode == 4)
- {
- g_accesstoken = rep.getValueToString("accesstoken");
-
- g_uid = rep.getValueToString("uid");
- }
-
- g_callbackLock.notify_all();
-}
-
-string g_option;
-
-static FILE *client_open(const char * /*path*/, const char *mode)
-{
- string option = "./";
- option += g_option;
- option += ".dat";
- return fopen(option.c_str(), mode);
-}
-
-int main(int argc, char **argv)
-{
- if (argc != 5)
- {
- cout
- << "Put \"[host-ipaddress:port] [authprovider] [authcode] [\'owner\'|\'member\']\" for sign-up and sign-in"
- << endl;
- cout << "Put \"[host-ipaddress:port] [uid] [accessToken] 1\" for sign-in" << endl;
- return 0;
- }
-
- g_option = argv[4];
-
- OCPersistentStorage ps
- { client_open, fread, fwrite, fclose, unlink };
-
- PlatformConfig cfg
- { ServiceType::InProc, ModeType::Both, "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
- 0, // Uses randomly available port
- QualityOfService::LowQos, &ps };
-
- OCPlatform::Configure(cfg);
-
- OCStackResult result = OC_STACK_ERROR;
-
- g_host += argv[1];
-
- OCAccountManager::Ptr accountMgr = OCPlatform::constructAccountManagerObject(g_host,
- CT_ADAPTER_TCP);
-
- mutex blocker;
- unique_lock < mutex > lock(blocker);
-
- if (g_option == "1")
- {
- accountMgr->signIn(argv[2], argv[3], &handleLoginoutCB);
- g_callbackLock.wait(lock);
- }
- else
- {
- accountMgr->signUp(argv[2], argv[3], &handleLoginoutCB);
- g_callbackLock.wait(lock);
- accountMgr->signIn(g_uid, g_accesstoken, &handleLoginoutCB);
- g_callbackLock.wait(lock);
- }
-
- string cmd;
-
- LightResource lightResource;
- lightResource.createResource();
-
- ResourceHandles resourceHandles;
- resourceHandles.push_back(lightResource.m_resourceHandle);
-
- RDClient::Instance().publishResourceToRD(g_host, OCConnectivityType::CT_ADAPTER_TCP, resourceHandles,
- &onPublish);
- g_callbackLock.wait(lock);
-/* TODO: need to modify the below according to the OCAccountManager API changed.
- if (g_option == "owner")
- {
- cout << "Creating group" << endl;
- accountMgr->createGroup(AclGroupType::PUBLIC, &onCreateGroup);
- g_callbackLock.wait(lock);
- cout << "Adding device " << OCGetServerInstanceIDString() << " to group " << g_gid << endl;
- accountMgr->addDeviceToGroup(g_gid,
- { OCGetServerInstanceIDString() }, &onPost);
- g_callbackLock.wait(lock);
-
- accountMgr->observeGroup(g_gid, &onObserveGroup);
- g_callbackLock.wait(lock);
- cout << "Put userUUID to send invitation" << endl;
- cin >> cmd;
- cout << "Group id : " << g_gid << " send invitation to " << cmd << endl;
- accountMgr->sendInvitation(g_gid, cmd, &onPost);
- g_callbackLock.wait(lock);
-
- cin >> cmd;
- }
- else if (g_option == "member")
- {
- cout << "Observing invitation" << endl;
- accountMgr->observeInvitation(&onInvite);
- g_callbackLock.wait(lock);
- cout << "Waiting invitation" << endl;
- g_callbackLock.wait(lock);
- cout << "Joining group " << g_invitedGroup << endl;
- accountMgr->joinGroup(g_invitedGroup, &onPost);
- g_callbackLock.wait(lock);
-
- cout << "find my resource " << cmd << endl;
- result = OCPlatform::findResource(g_host, "/oic/res",
- static_cast< OCConnectivityType >(CT_ADAPTER_TCP | CT_IP_USE_V4), &foundMyDevice);
- g_callbackLock.wait(lock);
-
- accountMgr->observeGroup(g_invitedGroup, &onObserveGroup);
- g_callbackLock.wait(lock);
-
- cin >> cmd;
- }
-*/
- return 0;
-}