#include <map>
#include <vector>
#include <string>
-#include <pthread.h>
#include <unistd.h>
#include "ocstack.h"
#include <OCApi.h>
#include <OCPlatform.h>
-#define DEFAULT_CONTEXT_VALUE 0x99
-
-#define maxSequenceNumber 0xFFFFFF
-
using namespace OC;
using namespace std;
condition_variable g_callbackLock;
-std::string g_uid;
-std::string g_accesstoken;
+string g_uid;
+string g_accesstoken;
string g_host;
OC::OCResource::Ptr g_binaryswitchResource;
-void printRepresentation(OCRepPayloadValue *value)
+void printRepresentation(OCRepresentation rep)
{
- while (value)
+ for (auto itr = rep.begin(); itr != rep.end(); ++itr)
{
- std::cout << "Key: " << value->name;
- switch (value->type)
+ cout << "\t" << itr->attrname() << ":\t" << itr->getValueToString() << endl;
+ if (itr->type() == AttributeType::Vector)
{
- case OCREP_PROP_NULL:
- std::cout << " Value: None" << std::endl;
- break;
- case OCREP_PROP_INT:
- std::cout << " Value: " << value->i << std::endl;
- break;
- case OCREP_PROP_DOUBLE:
- std::cout << " Value: " << value->d << std::endl;
- break;
- case OCREP_PROP_BOOL:
- std::cout << " Value: " << value->b << std::endl;
- break;
- case OCREP_PROP_STRING:
- std::cout << " Value: " << value->str << std::endl;
- break;
- case OCREP_PROP_BYTE_STRING:
- std::cout << " Value: Byte String" << std::endl;
- break;
- case OCREP_PROP_OBJECT:
- std::cout << " Value: Object" << std::endl;
- break;
- case OCREP_PROP_ARRAY:
- std::cout << " Value: Array" << std::endl;
- break;
- }
-
- if (strcmp(value->name, "accesstoken") == 0)
- {
- g_accesstoken = value->str;
+ 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;
+ }
}
-
- if (strcmp(value->name, "uid") == 0)
+ else if (itr->type() == AttributeType::OCRepresentation)
{
- g_uid = value->str;
+ printRepresentation((*itr).getValue<OCRepresentation>());
}
-
- value = value->next;
}
}
if (rep.getPayload() != NULL)
{
- printRepresentation(rep.getPayload()->values);
+ printRepresentation(rep);
+ }
+
+ if (ecode == 4)
+ {
+ g_accesstoken = rep.getValueToString("accesstoken");
+
+ g_uid = rep.getValueToString("uid");
}
g_callbackLock.notify_all();
}
-void printRepresentation(const OCRepresentation &rep)
+void printResource(const OCRepresentation &rep)
{
cout << "URI: " << rep.getUri() << endl;
for (auto it = children.begin();
it != children.end(); it++)
{
- printRepresentation(*it);
+ printResource(*it);
}
}
{
try
{
- if (eCode == OC_STACK_OK && sequenceNumber != maxSequenceNumber + 1)
+ if (eCode == OC_STACK_OK && sequenceNumber <= MAX_SEQUENCE_NUMBER)
{
if (sequenceNumber == OC_OBSERVE_REGISTER)
{
{
if (eCode == OC_STACK_OK)
{
- std::cout << "Observe registration failed or de-registration action failed/succeeded" << std::endl;
+ cout << "Observe registration failed or de-registration action failed/succeeded" << endl;
}
else
{
}
}
-void onPut(const HeaderOptions & /*headerOptions*/, const OCRepresentation &rep, const int eCode)
+void onPost(const HeaderOptions & /*headerOptions*/, const OCRepresentation &rep, const int eCode)
{
- cout << "PUT response: " << eCode << endl;
+ cout << "POST response: " << eCode << endl;
printRepresentation(rep);
}
binarySwitch.setValue("value", toTurn);
QueryParamsMap query;
- g_binaryswitchResource->post("oic.r.switch.binary", DEFAULT_INTERFACE, binarySwitch, query, &onPut);
+ g_binaryswitchResource->post("oic.r.switch.binary", DEFAULT_INTERFACE, binarySwitch, query,
+ &onPost);
}
void getCollectionResource(const HeaderOptions &,
{
cout << "Resource get: " << ecode << endl;
- printRepresentation(rep);
+ printResource(rep);
vector<OCRepresentation> children = rep.getChildren();
void foundDevice(shared_ptr<OC::OCResource> resource)
{
+ cout << "Found device called!" << endl;
+
vector<string> rt = resource->getResourceTypes();
cout << "Device found: " << resource->uri() << endl;
OCPlatform::findResource(g_host, searchQuery,
static_cast<OCConnectivityType>(CT_ADAPTER_TCP | CT_IP_USE_V4),
&foundAirconditionerResource);
+
+ OCPlatform::OCPresenceHandle handle;
+ if (OCPlatform::subscribeDevicePresence(handle, g_host, { resource->sid() },
+ static_cast<OCConnectivityType>
+ (CT_ADAPTER_TCP | CT_IP_USE_V4), &onObserve) != OC_STACK_OK)
+ {
+ cout << "Device presence failed" << endl;
+ }
}
}
}
-void presenceDevice(OCStackResult , const unsigned int i, const std::string &str)
+void errorFoundDevice(const std::string &uri, const int ecode)
+{
+ cout << "Found device error on " << uri << " code " << ecode << endl;
+ g_callbackLock.notify_all();
+}
+
+void presenceDevice(OCStackResult , const unsigned int i, const string &str)
{
cout << "Presence received, i=" << i << " str=" << str << endl;
}
static FILE *client_open(const char * /*path*/, const char *mode)
{
- return fopen("./resource_controller.dat", mode);
+ return fopen("./aircon_controller.dat", mode);
}
int main(int argc, char *argv[])
mutex blocker;
- unique_lock<std::mutex> lock(blocker);
+ unique_lock<mutex> lock(blocker);
if (argc == 5)
{
result = OCPlatform::findResource(g_host, "/oic/res?rt=oic.wk.d",
static_cast<OCConnectivityType>(CT_ADAPTER_TCP | CT_IP_USE_V4),
- &foundDevice);
+ &foundDevice, &errorFoundDevice);
cout << " result: " << result << endl;