struct ObserveContext
{
- std::function<void(const AttributeMap&, const int&)> callback;
+ std::function<void(const AttributeMap&, const int&, const int&)> callback;
};
OCStackApplicationResult observeResourceCallback(void* ctx, OCDoHandle handle, OCClientResponse* clientResponse)
{
ObserveContext* context = static_cast<ObserveContext*>(ctx);
AttributeMap attrs;
+ uint32_t sequenceNumber = clientResponse->sequenceNumber;
if(clientResponse->result == OC_STACK_OK)
{
attrs = parseGetSetCallback(clientResponse);
}
- std::thread exec(context->callback, attrs, clientResponse->result);
+ std::thread exec(context->callback, attrs, clientResponse->result, sequenceNumber);
exec.detach();
return OC_STACK_KEEP_TRANSACTION;
}
- OCStackResult InProcClientWrapper::ObserveResource(OCDoHandle* handle, const std::string& host, const std::string& uri, std::function<void(const AttributeMap, const int)>& callback)
+
+ OCStackResult InProcClientWrapper::ObserveResource(ObserveType observeType, OCDoHandle* handle, const std::string& host, const std::string& uri, std::function<void(const AttributeMap&, const int&, const int&)>& callback)
{
OCStackResult result;
OCCallbackData* cbdata = new OCCallbackData();
cbdata->context = static_cast<void*>(ctx);
cbdata->cb = &observeResourceCallback;
+ OCMethod method;
+ if (observeType == ObserveType::Observe)
+ {
+ method = OC_REST_OBSERVE;
+ }
+ else if (observeType == ObserveType::ObserveAll)
+ {
+ method = OC_REST_OBSERVE_ALL;
+ }
+ else
+ {
+ method = OC_REST_OBSERVE_ALL;
+ }
+
ostringstream os;
os << host<< uri;
{
std::lock_guard<std::mutex> lock(m_csdkLock);
//result = OCDoResource(handle, OC_REST_OBSERVE, uri.c_str(), host.c_str(), nullptr, OC_CONFIRMABLE, cbdata);
- result = OCDoResource(handle, OC_REST_OBSERVE, os.str().c_str(), nullptr, nullptr, OC_NON_CONFIRMABLE, cbdata);
+ result = OCDoResource(handle, method, os.str().c_str(), nullptr, nullptr, OC_NON_CONFIRMABLE, cbdata);
}
return result;
}
return m_clientWrapper->SetResourceAttributes(m_host, m_uri, attributeMap, queryParametersMap, attributeHandler);
}
- OCStackResult OCResource::observe(std::function<void(const AttributeMap, const int)> observeHandler)
+ OCStackResult OCResource::observe(ObserveType observeType, std::function<void(const AttributeMap&, const int&, const int&)> observeHandler)
{
if(m_observeHandle != nullptr)
{
}
else
{
- return m_clientWrapper->ObserveResource(&m_observeHandle, m_host, m_uri, observeHandler);
+ return m_clientWrapper->ObserveResource(observeType, &m_observeHandle, m_host, m_uri, observeHandler);
}
}
{
if(clientResponse->sequenceNumber != 0)
{
- if(clientResponse->sequenceNumber <= cbNode->sequenceNumber)
+ if(cbNode->method == OC_REST_OBSERVE && (clientResponse->sequenceNumber <= cbNode->sequenceNumber))
{
OC_LOG_V(DEBUG, TAG, "Observe notification came out of order. \
Ignoring Incoming:%d Against Current:%d.",
#include <cstdlib>
#include <pthread.h>
#include "OCPlatform.h"
+#include "OCApi.h"
using namespace OC;
const int SUCCESS_RESPONSE = 0;
std::shared_ptr<OCResource> curResource;
+static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
int observe_count()
{
return ++oc;
}
-void onObserve(const AttributeMap attributeMap, const int eCode)
+void onObserve(const AttributeMap& attributeMap, const int& eCode, const int& sequenceNumber)
{
if(eCode == SUCCESS_RESPONSE)
{
std::cout << "OBSERVE RESULT:"<<std::endl;
+ std::cout << "\tSequenceNumber: "<< sequenceNumber << endl;
for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
{
std::cout << "\tAttribute name: "<< it->first << " value: ";
std::cout << std::endl;
}
-
- if(observe_count() > 3)
+
+ if(observe_count() > 30)
{
std::cout<<"Cancelling Observe..."<<std::endl;
OCStackResult result = curResource->cancelObserve();
std::cout << std::endl;
}
- curResource->observe(&onObserve);
+
+ if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
+ std::cout << endl << "Observe is used." << endl << endl;
+ else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
+ std::cout << endl << "ObserveAll is used." << endl << endl;
+
+ curResource->observe(OBSERVE_TYPE_TO_USE, &onObserve);
+
}
else
{
}
}
-
-int main()
+void PrintUsage()
{
- // Create PlatformConfig object
+ std::cout << endl;
+ std::cout << "Usage : simpleclient <ObserveType>" << endl;
+ std::cout << " ObserveType : 1 - Observe" << endl;
+ std::cout << " ObserveType : 2 - ObserveAll" << endl;
+}
+int main(int argc, char* argv[]) {
+ if (argc == 1)
+ {
+ OBSERVE_TYPE_TO_USE = ObserveType::Observe;
+ }
+ else if (argc == 2)
+ {
+ int value = atoi(argv[1]);
+ if (value == 1)
+ OBSERVE_TYPE_TO_USE = ObserveType::Observe;
+ else if (value == 2)
+ OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
+ else
+ OBSERVE_TYPE_TO_USE = ObserveType::Observe;
+ }
+ else
+ {
+ PrintUsage();
+ return -1;
+ }
+
+ // Create PlatformConfig object
PlatformConfig cfg;
cfg.ipAddress = "134.134.161.33";
cfg.port = 5683;
std::function<void(std::shared_ptr<OCResource>)>& callback) = 0;
virtual OCStackResult GetResourceAttributes(const std::string& host, const std::string& uri, std::function<void(const AttributeMap, const int)>& callback)=0;
virtual OCStackResult SetResourceAttributes(const std::string& host, const std::string& uri, const AttributeMap& attributes, const QueryParamsMap& queryParams, std::function<void(const AttributeMap,const int)>& callback)=0;
- virtual OCStackResult ObserveResource(OCDoHandle* handle, const std::string& host, const std::string& uri, std::function<void(const AttributeMap, const int)>& callback)=0;
+ virtual OCStackResult ObserveResource(ObserveType observeType, OCDoHandle* handle, const std::string& host, const std::string& uri, std::function<void(const AttributeMap&, const int&, const int&)>& callback)=0;
virtual OCStackResult CancelObserveResource(OCDoHandle handle, const std::string& host, const std::string& uri)=0;
virtual ~IClientWrapper(){}
virtual OCStackResult ListenForResource(const std::string& serviceUrl, const std::string& resourceType, std::function<void(std::shared_ptr<OCResource>)>& callback);
virtual OCStackResult GetResourceAttributes(const std::string& host, const std::string& uri, std::function<void(const AttributeMap, const int)>& callback);
virtual OCStackResult SetResourceAttributes(const std::string& host, const std::string& uri, const AttributeMap& attributes, const QueryParamsMap& queryParams, std::function<void(const AttributeMap,const int)>& callback);
- virtual OCStackResult ObserveResource(OCDoHandle* handle, const std::string& host, const std::string& uri, std::function<void(const AttributeMap, const int)>& callback);
+ virtual OCStackResult ObserveResource(ObserveType observeType, OCDoHandle* handle, const std::string& host, const std::string& uri, std::function<void(const AttributeMap&, const int&, const int&)>& callback);
virtual OCStackResult CancelObserveResource(OCDoHandle handle, const std::string& host, const std::string& uri);
// Note: this should never be called by anyone but the handler for the listen command. It is public becuase that needs to be a non-instance callback
ObserverFlag
};
+ enum class ObserveType
+ {
+ Observe,
+ ObserveAll
+ };
+
// TODO: To find the complete JSon data structure and modify map value type
// Typedef for attribute values and attribute map.
typedef std::vector<std::string> AttributeValues;
// Default interface
const std::string DEFAULT_INTERFACE = "oc.mi.def";
- // Used in discovering (GET) links to other resources of a collection.
+ // Used in discovering (GET) links to other resources of a collection.
const std::string LINK_INTERFACE = "oc.mi.ll";
- // Used in GET, PUT, POST, DELETE methods on links to other resources of a collection.
+ // Used in GET, PUT, POST, DELETE methods on links to other resources of a collection.
const std::string BATCH_INTERFACE = "oc.mi.b";
} // namespace OC
* @param AttributeMap Map which can either have all the attribute names and values
* (which will represent entire state of the resource) or a
* set of attribute names and values which needs to be modified
- * @param QueryParamsMap Map which can have the query parameter name and value
+ * @param QueryParamsMap Map which can have the query parameter name and value
* @param attributeHandler
* The callback function will be invoked with a map of attribute name and values.
* The callback function will also have the result from this Put operation
/**
* Function to set observation on the resource
+ * @param observeType allows the client to specify how it wants to observe.
* @param observeHandler handles callback
* The callback function will be invoked with a map of attribute name and values.
* The callback function will also have the result from this observe operation
* @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
* NOTE: OCStackResult is defined in ocstack.h.
*/
- OCStackResult observe(std::function<void(const AttributeMap, const int)> observeHandler);
+ OCStackResult observe(ObserveType observeType, std::function<void(const AttributeMap&, const int&, const int&)> observeHandler);
/**
* Function to cancel the observation on the resource
virtual OCStackResult GetResourceAttributes(const std::string& host, const std::string& uri, std::function<void(const AttributeMap, const int)>& callback){return OC_STACK_NOTIMPL;}
virtual OCStackResult SetResourceAttributes(const std::string& host, const std::string& uri, const AttributeMap& attributes, const QueryParamsMap& queryParams, std::function<void(const AttributeMap,const int)>& callback){return OC_STACK_NOTIMPL;}
- virtual OCStackResult ObserveResource(OCDoHandle* handle, const std::string& host, const std::string& uri, std::function<void(const AttributeMap, const int)>& callback){return OC_STACK_NOTIMPL;}
+ virtual OCStackResult ObserveResource(ObserveType observeType, OCDoHandle* handle, const std::string& host, const std::string& uri, std::function<void(const AttributeMap&, const int&, const int&)>& callback){return OC_STACK_NOTIMPL;}
virtual OCStackResult CancelObserveResource(OCDoHandle handle, const std::string& host, const std::string& uri){return OC_STACK_NOTIMPL;}
virtual std::shared_ptr<OCResource> parseOCResource(IClientWrapper::Ptr clientWrapper, const std::string& host, const boost::property_tree::ptree resourceNode) {return nullptr;}