******************************************************************/
#include "RCSResourceObject.h"
+#include "RCSRequest.h"
#include "OCPlatform.h"
using namespace OC::OCPlatform;
constexpr int RESOURCE_TEMP = 1;
constexpr int RESOURCE_LIGHT = 2;
-constexpr int DEFALUT_SERVER = 1;
+constexpr int DEFAULT_SERVER = 1;
constexpr int CUSTOM_SERVER = 2;
constexpr int INCREASE = 1;
constexpr int DECREASE = 2;
+const std::string BASELINE_INTERFACE = "oic.if.baseline";
+const std::string ACTUATOR_INTERFACE = "oic.if.a";
+const std::string SENSOR_INTERFACE = "oic.if.s";
+const std::string CUSTOM_INTERFACE = "test.custom";
+
typedef void (*DisplayControlMenuFunc)();
typedef std::function<void()> Run;
{
assert(min <= max);
- int input;
+ int input = 0;
std::cin >> input;
if (!std::cin.fail())
{
- if(input == max + 1) throw CloseApp();
- if(min <= input && input <= max) return input;
+ if (input == max + 1)
+ {
+ throw CloseApp();
+ }
+ if (min <= input && input <= max)
+ {
+ return input;
+ }
}
std::cin.clear();
void printAttributes(const RCSResourceAttributes& attrs)
{
- for(const auto& attr : attrs)
+ for (const auto& attr : attrs)
{
std::cout << "\tkey : " << attr.key() << "\n\tvalue : "
<< attr.value().toString() << std::endl;
}
}
-RCSGetResponse requestHandlerForGet(const RCSRequest&, RCSResourceAttributes& attrs)
+RCSGetResponse requestHandlerForGet(const RCSRequest & req, RCSResourceAttributes& attrs)
{
std::cout << "Received a Get request from Client" << std::endl;
printAttributes(attrs);
{
RCSResourceObject::LockGuard lock(g_resource);
std::cout << "\nSending response to Client : " << std::endl;
- printAttributes(g_resource->getAttributes());
+ if (req.getInterface() == CUSTOM_INTERFACE)
+ {
+ auto attr = g_resource->getAttributes();
+ static RCSByteString::DataType binval {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8,
+ 0x9, 0x0, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF};
+ attr["blob"] = RCSByteString {binval};
+ printAttributes(attr);
+ return RCSGetResponse::create(attr);
+ }
+ else
+ {
+ printAttributes(g_resource->getAttributes());
+ return RCSGetResponse::defaultAction();
+ }
}
-
- return RCSGetResponse::defaultAction();
}
RCSSetResponse requestHandlerForSet(const RCSRequest&, RCSResourceAttributes& attrs)
void initServer(const std::string& resourceUri, const std::string& resourceType,
const std::string& attrKey)
{
- g_resource = RCSResourceObject::Builder(resourceUri, resourceType, "oic.if.").
- setDiscoverable(true).setObservable(true).build();
+ g_resource = RCSResourceObject::Builder(resourceUri, resourceType, ACTUATOR_INTERFACE)
+ .addInterface(CUSTOM_INTERFACE)
+ .addInterface(SENSOR_INTERFACE)
+ .setDefaultInterface(BASELINE_INTERFACE)
+ .setDiscoverable(true)
+ .setObservable(true)
+ .build();
g_resource->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
g_resource->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::NEVER);
attrs[attrKey] = attrs[attrKey].get<int>() + diff;
}
- if(control == INCREASE)
+ if (control == INCREASE)
{
std::cout << attrKey << " increased." << std::endl;
}
std::cout << "========================================================\n";
int resourceType = processUserInput(RESOURCE_TEMP, RESOURCE_LIGHT);
- DisplayControlMenuFunc displayMenuFunc;
+ DisplayControlMenuFunc displayMenuFunc = nullptr;
std::string attrKey;
switch (resourceType)
void runResourceModeSelection()
{
std::cout << "======================================================== \n";
- std::cout << DEFALUT_SERVER << ". Creation of Simple Resource Without Handlers \n";
+ std::cout << DEFAULT_SERVER << ". Creation of Simple Resource Without Handlers \n";
std::cout << CUSTOM_SERVER << ". Creation of Resource With Set and Get Handlers \n";
std::cout << CUSTOM_SERVER + 1 << ". Quit \n";
std::cout << "======================================================== \n";
g_currentRun = std::bind(runResourceTypeSelection,
- processUserInput(DEFALUT_SERVER, CUSTOM_SERVER));
+ processUserInput(DEFAULT_SERVER, CUSTOM_SERVER));
}
void runPresenceSelection()
{
g_currentRun = runPresenceSelection;
- while(true)
+ while (true)
{
try
{
g_currentRun();
}
- catch(const std::exception& e)
+ catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
- catch(const CloseApp&)
+ catch (const CloseApp&)
{
break;
}
g_resource.reset();
- if(g_isPresenceStarted)
+ if (g_isPresenceStarted)
{
- stopPresence();
+ try
+ {
+ stopPresence();
+ }
+ catch(...)
+ {
+ std::cout << "presence stop fail" << std::endl;
+ }
}
}