//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+#include "iotivity_config.h"
#include "OCResource.h"
#include "OCUtilities.h"
#include <boost/lexical_cast.hpp>
#include <sstream>
+#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
+#endif
+#ifdef HAVE_WS2TCPIP_H
+#include <ws2tcpip.h>
+#endif
+#ifdef HAVE_IN6ADDR_H
+#include <in6addr.h>
+#endif
namespace OC {
static const char COAP[] = "coap://";
static const char COAPS[] = "coaps://";
static const char COAP_TCP[] = "coap+tcp://";
+static const char COAP_GATT[] = "coap+gatt://";
+static const char COAP_RFCOMM[] = "coap+rfcomm://";
using OC::nil_guard;
using OC::result_guard;
OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
const OCDevAddr& devAddr, const std::string& uri,
- const std::string& serverId, bool observable,
+ const std::string& serverId, uint8_t property,
const std::vector<std::string>& resourceTypes,
const std::vector<std::string>& interfaces)
: m_clientWrapper(clientWrapper), m_uri(uri),
m_resourceId(serverId, m_uri), m_devAddr(devAddr),
- m_isObservable(observable), m_isCollection(false),
+ m_isCollection(false), m_property(property),
m_resourceTypes(resourceTypes), m_interfaces(interfaces),
m_observeHandle(nullptr)
{
OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
const std::string& host, const std::string& uri,
const std::string& serverId,
- OCConnectivityType connectivityType, bool observable,
+ OCConnectivityType connectivityType, uint8_t property,
const std::vector<std::string>& resourceTypes,
const std::vector<std::string>& interfaces)
: m_clientWrapper(clientWrapper), m_uri(uri),
m_resourceId(serverId, m_uri),
- m_devAddr{ OC_DEFAULT_ADAPTER, OC_DEFAULT_FLAGS, 0, {0}, 0
-#if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
- , {0}
-#endif
- },
- m_isObservable(observable), m_isCollection(false),
+ m_isCollection(false), m_property(property),
m_resourceTypes(resourceTypes), m_interfaces(interfaces),
m_observeHandle(nullptr)
{
+ m_devAddr = OCDevAddr{OC_DEFAULT_ADAPTER, OC_DEFAULT_FLAGS, 0, {0}, 0,
+#if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
+ {0}
+#endif
+ };
m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
!= m_interfaces.end();
{
size_t prefix_len;
- if(host.compare(0, sizeof(COAP) - 1, COAP) == 0)
+ if (host.compare(0, sizeof(COAP) - 1, COAP) == 0)
{
prefix_len = sizeof(COAP) - 1;
}
- else if(host.compare(0, sizeof(COAPS) - 1, COAPS) == 0)
+ else if (host.compare(0, sizeof(COAPS) - 1, COAPS) == 0)
{
prefix_len = sizeof(COAPS) - 1;
m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags | OC_SECURE);
{
prefix_len = sizeof(COAP_TCP) - 1;
}
+ else if (host.compare(0, sizeof(COAP_GATT) - 1, COAP_GATT) == 0)
+ {
+ prefix_len = sizeof(COAP_GATT) - 1;
+ }
+ else if (host.compare(0, sizeof(COAP_RFCOMM) - 1, COAP_RFCOMM) == 0)
+ {
+ prefix_len = sizeof(COAP_RFCOMM) - 1;
+ }
else
{
throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
m_interfaces.empty(), m_clientWrapper.expired(), false, false);
}
- // remove 'coap://' or 'coaps://' or 'coap+tcp://'
+ // remove 'coap://' or 'coaps://' or 'coap+tcp://' or 'coap+gatt://' or 'coap+rfcomm://'
std::string host_token = host.substr(prefix_len);
- if(host_token[0] == '[') // IPv6
+ if (host_token[0] == '[') // IPv6
{
size_t bracket = host_token.find(']');
- if(bracket == std::string::npos || bracket == 0)
+ if (std::string::npos == bracket || 0 == bracket)
{
throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
m_interfaces.empty(), m_clientWrapper.expired(), false, false);
std::string ip6Addr = host_token.substr(1, bracket - 1);
// address validity check
+ std::string ip6AddrToValidityCheck(ip6Addr);
+ size_t percent = ip6AddrToValidityCheck.find('%');
+ if (std::string::npos != percent)
+ {
+ ip6AddrToValidityCheck.resize(percent);
+ }
struct in6_addr buf;
- const char *cAddr = ip6Addr.c_str();
- if(0 == inet_pton(AF_INET6, cAddr, &buf))
+ const char *cAddr = ip6AddrToValidityCheck.c_str();
+ if (0 == inet_pton(AF_INET6, cAddr, &buf))
{
throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
m_interfaces.empty(), m_clientWrapper.expired(), false, false);
// address validity check
struct in_addr buf;
const char *cAddr = ip4Addr.c_str();
- if(0 == inet_pton(AF_INET, cAddr, &buf))
+ if (0 == inet_pton(AF_INET, cAddr, &buf))
{
throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
m_interfaces.empty(), m_clientWrapper.expired(), false, false);
return checked_guard(m_clientWrapper.lock(),
&IClientWrapper::GetResourceRepresentation,
m_devAddr, m_uri,
- queryParametersMap, m_headerOptions,
+ queryParametersMap, m_headerOptions, CT_DEFAULT,
attributeHandler, QoS);
}
{
QueryParamsMap mapCpy(queryParametersMap);
- if(!resourceType.empty())
+ if (!resourceType.empty())
{
mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
}
- if(!resourceInterface.empty())
+ if (!resourceInterface.empty())
{
mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
}
{
QueryParamsMap mapCpy(queryParametersMap);
- if(!resourceType.empty())
+ if (!resourceType.empty())
{
mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
}
- if(!resourceInterface.empty())
+ if (!resourceInterface.empty())
{
mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
}
{
return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
m_devAddr, m_uri, rep, queryParametersMap,
- m_headerOptions, attributeHandler, QoS);
+ m_headerOptions, CT_DEFAULT, attributeHandler, QoS);
}
OCStackResult OCResource::post(const OCRepresentation& rep,
{
QueryParamsMap mapCpy(queryParametersMap);
- if(!resourceType.empty())
+ if (!resourceType.empty())
{
mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
}
- if(!resourceInterface.empty())
+ if (!resourceInterface.empty())
{
mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
}
OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
{
return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
- m_devAddr, m_uri, m_headerOptions, deleteHandler, QoS);
+ m_devAddr, m_uri, m_headerOptions, CT_DEFAULT, deleteHandler, QoS);
}
OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
QualityOfService QoS)
{
- if(m_observeHandle != nullptr)
- {
- return result_guard(OC_STACK_INVALID_PARAM);
- }
-
return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
observeType, &m_observeHandle, m_devAddr,
m_uri, queryParametersMap, m_headerOptions,
OCStackResult OCResource::cancelObserve(QualityOfService QoS)
{
- if(m_observeHandle == nullptr)
+ if (m_observeHandle == nullptr)
{
return result_guard(OC_STACK_INVALID_PARAM);
}
OCStackResult result = checked_guard(m_clientWrapper.lock(),
&IClientWrapper::CancelObserveResource,
- m_observeHandle, "", m_uri, m_headerOptions, QoS);
+ m_observeHandle, (const char*)"", m_uri, m_headerOptions, QoS);
- if(result == OC_STACK_OK)
+ if (result == OC_STACK_OK)
{
m_observeHandle = nullptr;
}
std::string OCResource::host() const
{
std::ostringstream ss;
- if (m_devAddr.flags & OC_SECURE)
+
+ if (m_devAddr.adapter & OC_ADAPTER_TCP)
+ {
+ ss << COAP_TCP;
+ }
+ else if (m_devAddr.adapter & OC_ADAPTER_GATT_BTLE)
{
- ss << COAPS;
+ ss << COAP_GATT;
}
- else if ((m_devAddr.adapter & OC_ADAPTER_TCP)
- || (m_devAddr.adapter & OC_ADAPTER_GATT_BTLE)
- || (m_devAddr.adapter & OC_ADAPTER_RFCOMM_BTEDR))
+ else if (m_devAddr.adapter & OC_ADAPTER_RFCOMM_BTEDR)
{
- ss << COAP_TCP;
+ ss << COAP_RFCOMM;
}
else
{
- ss << COAP;
+ if (m_devAddr.flags & OC_SECURE)
+ {
+ ss << COAPS;
+ }
+ else
+ {
+ ss << COAP;
+ }
}
+
if (m_devAddr.flags & OC_IP_USE_V6)
{
ss << '[' << m_devAddr.addr << ']';
bool OCResource::isObservable() const
{
- return m_isObservable;
+ return (m_property & OC_OBSERVABLE) == OC_OBSERVABLE;
}
+#ifdef WITH_MQ
+bool OCResource::isPublish() const
+{
+ return (m_property & OC_MQ_PUBLISHER) == OC_MQ_PUBLISHER;
+}
+#endif
+
std::vector<std::string> OCResource::getResourceTypes() const
{
return m_resourceTypes;
return this->uniqueIdentifier().m_representation;
}
+#ifdef WITH_MQ
+OCStackResult OCResource::discoveryMQTopics(const QueryParamsMap& queryParametersMap,
+ MQTopicCallback attributeHandler,
+ QualityOfService qos)
+{
+ return checked_guard(m_clientWrapper.lock(),
+ &IClientWrapper::ListenForMQTopic,
+ m_devAddr, m_uri,
+ queryParametersMap, m_headerOptions,
+ attributeHandler, qos);
+}
+
+OCStackResult OCResource::createMQTopic(const OCRepresentation& rep,
+ const std::string& topicUri,
+ const QueryParamsMap& queryParametersMap,
+ MQTopicCallback attributeHandler,
+ QualityOfService qos)
+{
+ return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutMQTopicRepresentation,
+ m_devAddr, topicUri, rep, queryParametersMap,
+ m_headerOptions, attributeHandler, qos);
+}
+#endif
+#ifdef MQ_SUBSCRIBER
+OCStackResult OCResource::subscribeMQTopic(ObserveType observeType,
+ const QueryParamsMap& queryParametersMap,
+ ObserveCallback observeHandler,
+ QualityOfService qos)
+{
+ return result_guard(observe(observeType, queryParametersMap, observeHandler, qos));
+}
+
+OCStackResult OCResource::unsubscribeMQTopic(QualityOfService qos)
+{
+ return result_guard(cancelObserve(qos));
+}
+
+OCStackResult OCResource::requestMQPublish(const QueryParamsMap& queryParametersMap,
+ PostCallback attributeHandler,
+ QualityOfService qos)
+{
+ OCRepresentation rep;
+ rep.setValue(std::string("req_pub"), std::string("true"));
+ return result_guard(post(rep, queryParametersMap, attributeHandler, qos));
+}
+#endif
+#ifdef MQ_PUBLISHER
+OCStackResult OCResource::publishMQTopic(const OCRepresentation& rep,
+ const QueryParamsMap& queryParametersMap,
+ PostCallback attributeHandler,
+ QualityOfService qos)
+{
+ return result_guard(post(rep, queryParametersMap, attributeHandler, qos));
+}
+#endif
+
bool OCResource::operator==(const OCResource &other) const
{
return m_resourceId == other.m_resourceId;