- US1321 - Now, C++ API users will be able to register multiple types and interfaces
authorclairiky <chanchala.roy.lairikyengbam@intel.com>
Fri, 8 Aug 2014 21:23:49 +0000 (14:23 -0700)
committerclairiky <chanchala.roy.lairikyengbam@intel.com>
Fri, 8 Aug 2014 21:23:49 +0000 (14:23 -0700)
  to a resource. The API names may change after review.
- Removed unwanted trailing whitespaces at the end of lines.
- Updated according to review comments.

Change-Id: I13f7fcd07386d4a13d42f00254f870cfbd8ca4b4

OCLib/InProcServerWrapper.cpp
OCLib/OCPlatform.cpp
OCLib/OCReflect.cpp
examples/simpleclient.cpp
examples/simpleserver.cpp
include/IServerWrapper.h
include/InProcServerWrapper.h
include/OCException.h
include/OCPlatform.h
include/OutOfProcServerWrapper.h

index df67b06..7f8c07d 100644 (file)
@@ -48,8 +48,8 @@ void defaultEntityHandler(const OC::OCResourceRequest::Ptr request, const OC::OC
 }
 
 OCStackResult entityHandler(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest ) {
-   
-    // TODO @SASHI we need to have a better way of logging (with various levels of logging) 
+
+    // TODO @SASHI we need to have a better way of logging (with various levels of logging)
     cout << "\nIn C entity handler: " << endl;
 
     // TODO @SASHI dow we need shared pointer?
@@ -73,7 +73,7 @@ OCStackResult entityHandler(OCEntityHandlerFlag flag, OCEntityHandlerRequest * e
                     // TODO @SASHI Why strings : "GET"??
                     pRequest->setRequestType("GET");
                 }
-                
+
                 if(OC_REST_PUT == entityHandlerRequest->method)
                 {
                     pRequest->setRequestType("PUT");
@@ -105,25 +105,29 @@ OCStackResult entityHandler(OCEntityHandlerFlag flag, OCEntityHandlerRequest * e
         // TODO @SASHI we could use const reference
         std::string payLoad = pResponse->getPayload();
 
-        if(OC_REST_GET == entityHandlerRequest->method) 
+        if(OC_REST_GET == entityHandlerRequest->method)
         {
-            cout << "\t\t\tGoing from stack for GET: " << payLoad << endl;    
+            cout << "\t\t\tGoing from stack for GET: " << payLoad << endl;
         }
         else if (OC_REST_PUT == entityHandlerRequest->method)
         {
-            cout << "\t\t\tGoing from stack for PUT: " << payLoad << endl;    
+            cout << "\t\t\tGoing from stack for PUT: " << payLoad << endl;
         }
 
+        else
+        {
+            cout << "\t\t\t Unknown method...!!!" << endl;
+        }
         // TODO @SASHI Now there is memory that needs to be freed.
         entityHandlerRequest->resJSONPayload = reinterpret_cast<unsigned char *>(OC::OCReflect::OCStack::strdup(payLoad.c_str()));
 
         if(nullptr == entityHandlerRequest->resJSONPayload)
         {
             // TODO @SASHI throw std::runtime_error("out of memory");
-            cout << "Out of memory in copying to resJSONPayload" << endl; 
-        } 
+            cout << "Out of memory in copying to resJSONPayload" << endl;
+        }
     }
-      
+
     return OC_STACK_OK;
 }
 
@@ -172,7 +176,7 @@ namespace OC
                     OCResourceHandle& resourceHandle,
                     std::string& resourceURI,
                     const std::string& resourceTypeName,
-                    const std::string& resourceInterface, 
+                    const std::string& resourceInterface,
                     std::function<void(const OCResourceRequest::Ptr, const OCResourceResponse::Ptr)> eHandler,
                     uint8_t resourceProperties)
 
@@ -208,10 +212,39 @@ namespace OC
                 entityHandlerMap[resourceHandle] = eHandler;
             }
         }
-    
+
+        return result;
+    }
+
+    OCStackResult InProcServerWrapper::bindTypeToResource(const OCResourceHandle& resourceHandle,
+                     const std::string& resourceTypeName)
+    {
+        cout << "Binding Type to Resource: \n";
+        cout << "\tTypeName: " << resourceTypeName  << endl;
+
+        OCStackResult result = OCBindResourceTypeToResource(resourceHandle, resourceTypeName.c_str());
+        if (result != OC_STACK_OK)
+        {
+            throw OCException("Bind Type to resource failed", result);
+        }
         return result;
     }
 
+    OCStackResult InProcServerWrapper::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
+                     const std::string& resourceInterfaceName)
+    {
+        cout << "Binding Interface to Resource: \n";
+        cout << "\tInterfaceName: " << resourceInterfaceName  << endl;
+
+        OCStackResult result = OCBindResourceInterfaceToResource(resourceHandle, resourceInterfaceName.c_str());
+        if (result != OC_STACK_OK)
+        {
+            throw OCException("Bind Interface to resource failed", result);
+        }
+        return result;
+    }
+
+
     InProcServerWrapper::~InProcServerWrapper()
     {
         if(m_processThread.joinable())
index 0251b9d..c33b760 100644 (file)
@@ -32,8 +32,9 @@
 
 #include "OCPlatform.h"
 #include "OCApi.h"
+#include "OCException.h"
 
-namespace OC 
+namespace OC
 {
     // Constructor. Internally calls private init function
     OCPlatform::OCPlatform(const PlatformConfig& config)
@@ -60,7 +61,7 @@ namespace OC
 
         if(config.mode == ModeType::Server)
         {
-            // Call server wrapper init 
+            // Call server wrapper init
             m_server = m_WrapperInstance->CreateServerWrapper(config);
         }
         else if(config.mode == ModeType::Client)
@@ -68,7 +69,7 @@ namespace OC
             // Call client wrapper init
             m_client = m_WrapperInstance->CreateClientWrapper(config);
         }
-        else 
+        else
         {
             // This must be both server and client
             m_server = m_WrapperInstance->CreateServerWrapper(config);
@@ -89,24 +90,23 @@ namespace OC
         }
     }
 
-    
-    OCStackResult OCPlatform::findResource(const std::string& host, const std::string& resourceName, 
+
+    OCStackResult OCPlatform::findResource(const std::string& host, const std::string& resourceName,
         std::function<void(OCResource::Ptr)> resourceHandler)
     {
         if(m_client)
         {
             return m_client->ListenForResource(host, resourceName, resourceHandler);
         }
-        
         return OC_STACK_ERROR;
     }
-    
 
-    OCStackResult OCPlatform::registerResource(OCResourceHandle& resourceHandle, 
-                std::string& resourceURI, 
-                const std::string& resourceTypeName, 
-                const std::string& resourceInterface, 
-                std::function<void(const OCResourceRequest::Ptr, const OCResourceResponse::Ptr)> entityHandler, 
+
+    OCStackResult OCPlatform::registerResource(OCResourceHandle& resourceHandle,
+                std::string& resourceURI,
+                const std::string& resourceTypeName,
+                const std::string& resourceInterface,
+                std::function<void(const OCResourceRequest::Ptr, const OCResourceResponse::Ptr)> entityHandler,
                 uint8_t resourceProperty)
     {
         OCStackResult result = OC_STACK_OK;
@@ -114,9 +114,9 @@ namespace OC
         if(m_server)
         {
             try{
-                result = m_server->registerResource(resourceHandle, resourceURI, resourceTypeName, resourceInterface, entityHandler, resourceProperty); 
+                result = m_server->registerResource(resourceHandle, resourceURI, resourceTypeName, resourceInterface, entityHandler, resourceProperty);
             }
-            catch(std::exception e) // define our own expception. 
+            catch(std::exception e) // define our own expception.
             {
                 throw e;
             }
@@ -152,4 +152,46 @@ namespace OC
         OCStackResult result = OC_STACK_OK;
         return result;
     }
+
+    OCStackResult OCPlatform::bindTypeToResource(const OCResourceHandle& resourceHandle,
+                     const std::string& resourceTypeName) const
+    {
+        OCStackResult result = OC_STACK_ERROR;
+        if(m_server)
+        {
+            try
+            {
+                result = m_server->bindTypeToResource(resourceHandle, resourceTypeName);
+            }
+            catch (OCException& e)
+            {
+                cout << "Caught an exception..." << endl;
+                cout << "\tMessage: " << e.what()  << endl;
+                cout << "\t Reason: " << e.reason() << endl;
+            }
+        }
+        return result;
+    }
+
+    OCStackResult OCPlatform::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
+                     const std::string& resourceInterfaceName) const
+    {
+        OCStackResult result = OC_STACK_ERROR;
+        if(m_server)
+        {
+            try
+            {
+                result = m_server->bindInterfaceToResource(resourceHandle, resourceInterfaceName);
+            }
+            catch (OCException& e)
+            {
+                cout << "Caught an exception..." << endl;
+                cout << "\tMessage: " << e.what()  << endl;
+                cout << "\t Reason: " << e.reason() << endl;
+            }
+        }
+        return result;
+
+    }
+
 } //namespace OC
index 5a98283..2bf87af 100644 (file)
@@ -25,7 +25,7 @@
 #include <iterator>
 
 #include <boost/algorithm/string/classification.hpp>
-#include <boost/algorithm/string/split.hpp>  
+#include <boost/algorithm/string/split.hpp>
 
 #include "OCReflect.h"
 
@@ -67,7 +67,7 @@ char *strdup(const char *s)
  if(nullptr == ret)
   return nullptr;
 
- return strcpy(ret, s); 
+ return strcpy(ret, s);
 }
 
 char *strdup(const std::string& s)
@@ -77,7 +77,7 @@ char *strdup(const std::string& s)
  if(nullptr == ret)
   return nullptr;
 
- return strcpy(ret, s.c_str()); 
+ return strcpy(ret, s.c_str());
 }
 
 // Count the number of elements in a NULL-terminated C array of char*:
@@ -89,9 +89,9 @@ size_t length(char **in)
  size_t ret = 0;
 
  for(char **cursor = in; nullptr != *cursor; ++cursor)
-  ++ret; 
+  ++ret;
 
- return ret; 
+ return ret;
 }
 
 // Note: caller is responsible for the returned memory:
@@ -143,21 +143,21 @@ std::string convert(const OC::OCReflect::property_binding& npb)
   {
        case property_type::nil:
        case property_type::rational:
-       case property_type::list:       
-       case property_type::string:             
+       case property_type::list:
+       case property_type::string:
                                     // Sashi commented this line to fix the build error
                                                                        //os << "convert(): request to convert valid, but unimplemented type: " << ps.type;
                                                                        throw std::runtime_error(os.str());
                                                                        break;
 
-       case property_type::INVALID:    
+       case property_type::INVALID:
                                                                        throw std::runtime_error("convert(): request to convert invalid type");
                                                                        break;
 
-       case property_type::boolean:    os << "bt." << 'b';     
+       case property_type::boolean:    os << "bt." << 'b';
                                                                        break;
 
-       case property_type::integer:    os << "bt." << 'i';     
+       case property_type::integer:    os << "bt." << 'i';
                                                                        break;
   }
 
@@ -168,7 +168,7 @@ OC::OCReflect::property_type as_property_type(const std::string& pb_code)
 {
  // We expect: "oc.bt.<char typecode>":
  const size_t code_length   = 7,
-              code_pos      = 6; 
+              code_pos      = 6;
 
  if(code_length != pb_code.length())
   return OC::OCReflect::property_type::INVALID;
@@ -183,8 +183,8 @@ OC::OCReflect::property_type as_property_type(const std::string& pb_code)
 /* These don't have codes yet:
        case property_type::nil:
        case property_type::rational:
-       case property_type::string:             
-       case property_type::list:       
+       case property_type::string:
+       case property_type::list:
 */
  }
 
@@ -196,14 +196,14 @@ OC::OCReflect::property_binding as_property_binding(const std::string& pb_rep)
  auto delim_pos = pb_rep.find_first_of(':');
 
  if(pb_rep.length() == delim_pos)
-  throw OC::OCReflect::reflection_exception("convert(): invalid property string (missing delimiter)");
-  
+  throw OC::reflection_exception("convert(): invalid property string (missing delimiter)");
+
  std::string pname { pb_rep.substr(0, delim_pos) };
 
  auto ptype = as_property_type(pb_rep.substr(1 + delim_pos));
 
  auto pattr = OC::OCReflect::property_attribute::rw;    // We aren't handling attributes right now...
+
  return OC::OCReflect::property_binding { pname, { ptype, pattr } };
 }
 
@@ -214,7 +214,7 @@ OC::OCReflect::property_binding_vector as_property_binding_vector(const std::vec
  for(const auto& s : pb_reps)
   pbv.emplace_back(as_property_binding(s));
 
- return pbv;   
+ return pbv;
 }
 
 std::vector<std::string> convert(const OC::OCReflect::property_binding_vector& psv)
@@ -251,7 +251,7 @@ catch(...)
 std::vector<std::string> expand(const char *flattened_string, const std::string& delim)
 {
  if(nullptr == flattened_string)
-  throw OC::OCReflect::reflection_exception("nullptr passed to expand()");
+  throw OC::reflection_exception("nullptr passed to expand()");
 
   std::vector<std::string> ret;
   std::string flattened = flattened_string;
index 009f1b3..05bae3a 100644 (file)
@@ -27,7 +27,7 @@
 
 using namespace OC;
 
-const int SUCCESS_RESPONSE = 0; 
+const int SUCCESS_RESPONSE = 0;
 std::shared_ptr<OCResource> curResource;
 
 int observe_count()
@@ -51,7 +51,7 @@ void onObserve(const AttributeMap attributeMap, const int eCode)
 
             std::cout << std::endl;
         }
-        
+
         if(observe_count() > 3)
         {
             std::cout<<"Cancelling Observe..."<<std::endl;
@@ -76,7 +76,7 @@ void onPut(const AttributeMap attributeMap, const int eCode)
     if(eCode == SUCCESS_RESPONSE)
     {
         std::cout << "PUT request was successful" << std::endl;
-        std::cout << "Put count: "<< attributeMap.size()<<std::endl;
+
         for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
         {
             std::cout << "\tAttribute name: "<< it->first << " value: ";
@@ -105,18 +105,18 @@ void putLightRepresentation(std::shared_ptr<OCResource> resource)
         // Create AttributeMap
         AttributeMap attributeMap;
         // Add the attribute name and values in the attribute map
-        AttributeValues stateVal; 
+        AttributeValues stateVal;
         stateVal.push_back("true");
 
-        AttributeValues powerVal; 
+        AttributeValues powerVal;
         powerVal.push_back("10");
 
         attributeMap["state"] = stateVal;
-        attributeMap["power"] = powerVal; 
+        attributeMap["power"] = powerVal;
 
         // Create QueryParameters Map and add query params (if any)
-        QueryParamsMap queryParamsMap; 
-        
+        QueryParamsMap queryParamsMap;
+
         // Invoke resource's pit API with attribute map, query map and the callback parameter
         resource->put(attributeMap, queryParamsMap, &onPut);
     }
@@ -171,31 +171,31 @@ void foundResource(std::shared_ptr<OCResource> resource)
     std::string hostAddress;
     try
     {
-        // Do some operations with resource object. 
+        // Do some operations with resource object.
         if(resource)
         {
             std::cout<<"DISCOVERED Resource:"<<std::endl;
             // Get the resource URI
             resourceURI = resource->uri();
             std::cout << "\tURI of the resource: " << resourceURI << std::endl;
-            
+
             // Get the resource host address
             hostAddress = resource->host();
-            std::cout << "\tHost address of the resource: " << hostAddress << std::endl;      
+            std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
 
             if(resourceURI == "/a/light")
             {
                 curResource = resource;
                 // Call a local function which will internally invoke get API on the resource pointer
-                getLightRepresentation(resource); 
+                getLightRepresentation(resource);
             }
         }
         else
         {
             // Resource is invalid
             std::cout << "Resource is invalid" << std::endl;
-        }   
-        
+        }
+
     }
     catch(std::exception& e)
     {
@@ -214,8 +214,8 @@ int main()
     cfg.mode = ModeType::Client;
     cfg.serviceType = ServiceType::InProc;
 
-    // Create a OCPlatform instance. 
-    // Note: Platform creation is synchronous call. 
+    // Create a OCPlatform instance.
+    // Note: Platform creation is synchronous call.
 
     try
     {
@@ -229,7 +229,7 @@ int main()
             // some operations
         }
 
-    }catch(OCException e)
+    }catch(OCException& e)
     {
         //log(e.what());
     }
index dccbbed..0209bac 100644 (file)
@@ -20,7 +20,7 @@
 
 ///
 /// This sample provides steps to define an interface for a resource
-/// (properties and methods) and host this resource on the server. 
+/// (properties and methods) and host this resource on the server.
 ///
 
 #include <functional>
@@ -38,13 +38,13 @@ int gObservation = 0;
 // Forward declaring the entityHandler
 void entityHandler(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response);
 
-/// This class represents a single resource named 'lightResource'. This resource has 
-/// two simple properties named 'state' and 'power' 
+/// This class represents a single resource named 'lightResource'. This resource has
+/// two simple properties named 'state' and 'power'
 
 class LightResource
 {
 public:
-    /// Access this property from a TB client 
+    /// Access this property from a TB client
     bool m_state;
     int m_power;
     OCResourceHandle m_resourceHandle;
@@ -55,7 +55,7 @@ public:
 
     /* Note that this does not need to be a member function: for classes you do not have
     access to, you can accomplish this with a free function: */
-    
+
     /// This function internally calls registerResource API.
     void createResource(OC::OCPlatform& platform)
     {
@@ -66,10 +66,10 @@ public:
         // OCResourceProperty is defined ocstack.h
         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
 
-        // This will internally create and register the resource. 
+        // This will internally create and register the resource.
         OCStackResult result = platform.registerResource(
-                                    m_resourceHandle, resourceURI, resourceTypeName, 
-                                    resourceInterface, &entityHandler, resourceProperty); 
+                                    m_resourceHandle, resourceURI, resourceTypeName,
+                                    resourceInterface, &entityHandler, resourceProperty);
 
         if (OC_STACK_OK != result)
         {
@@ -110,9 +110,27 @@ public:
         attributeMap["state"] = stateVal;
         attributeMap["power"] = powerVal;
     }
+
+    void addType(const OC::OCPlatform& platform, const std::string& type) const
+    {
+        OCStackResult result = platform.bindTypeToResource(m_resourceHandle, type);
+        if (OC_STACK_OK != result)
+        {
+            cout << "Binding TypeName to Resource was unsuccessful\n";
+        }
+    }
+
+    void addInterface(const OC::OCPlatform& platform, const std::string& interface) const
+    {
+        OCStackResult result = platform.bindInterfaceToResource(m_resourceHandle, interface);
+        if (OC_STACK_OK != result)
+        {
+            cout << "Binding TypeName to Resource was unsuccessful\n";
+        }
+    }
 };
 
-// Create the instance of the resource class (in this case instance of class 'LightResource'). 
+// Create the instance of the resource class (in this case instance of class 'LightResource').
 LightResource myLightResource;
 
 // ChangeLightRepresentaion is an observation function,
@@ -124,18 +142,18 @@ void * ChangeLightRepresentation (void *param)
     while (1)
     {
         sleep (5);
-        
+
         if (gObservation)
         {
             // If under observation if there are any changes to the light resource
             // we call notifyObservors
             //
             // For demostration we are changing the power value and notifying.
-            myLightResource.m_power += 10; 
+            myLightResource.m_power += 10;
 
             cout << "\nPower updated to : " << myLightResource.m_power << endl;
             cout << "Notifying observers with resource handle: " << myLightResource.getHandle() << endl;
-            
+
             OCStackResult result = OCPlatform::notifyObservers(myLightResource.getHandle());
 
             if(OC_STACK_NO_OBSERVERS == result)
@@ -150,7 +168,7 @@ void * ChangeLightRepresentation (void *param)
 }
 
 
-// This is just a sample implementation of entity handler. 
+// This is just a sample implementation of entity handler.
 // Entity handler can be implemented in several ways by the manufacturer
 void entityHandler(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response)
 {
@@ -183,12 +201,12 @@ void entityHandler(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<O
                 // Process query params and do required operations ..
 
                 // Get the representation of this resource at this point and send it as response
-                AttributeMap attributeMap; 
+                AttributeMap attributeMap;
 
                 myLightResource.getRepresentation(attributeMap);
 
                 if(response)
-                {   
+                {
                     // TODO Error Code
                     response->setErrorCode(200);
                     response->setResourceRepresentation(attributeMap);
@@ -208,8 +226,8 @@ void entityHandler(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<O
 
                 myLightResource.setRepresentation(attributeMap);
 
-                // Do related operations related to PUT request 
-                
+                // Do related operations related to PUT request
+
                 myLightResource.getRepresentation(attributeMap);
 
                 if(response)
@@ -230,12 +248,12 @@ void entityHandler(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<O
         else if(requestFlag == RequestHandlerFlag::ObserverFlag)
         {
             pthread_t threadId;
-            
+
             cout << "\t\trequestFlag : Observer\n";
             gObservation = 1;
 
             static int startedThread = 0;
-            
+
             // Observation happens on a different thread in ChangeLightRepresentation function.
             // If we have not created the thread already, we will create one here.
             if(!startedThread)
@@ -243,7 +261,7 @@ void entityHandler(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<O
                 pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)NULL);
                 startedThread = 1;
             }
-            
+
         }
     }
     else
@@ -261,17 +279,18 @@ int main()
     cfg.port = 56832;
     cfg.mode = ModeType::Server;
     cfg.serviceType = ServiceType::InProc;
-    
-    // Create a OCPlatform instance. 
-    // Note: Platform creation is synchronous call. 
+
+    // Create a OCPlatform instance.
+    // Note: Platform creation is synchronous call.
     try
     {
         OCPlatform platform(cfg);
 
-        // Invoke createResource function of class light. 
+        // Invoke createResource function of class light.
 
         myLightResource.createResource(platform);
-
+        myLightResource.addType(platform, std::string("core.brightlight"));
+        myLightResource.addInterface(platform, std::string("oc.mi.ll"));
         // Perform app tasks
         while(true)
         {
@@ -283,7 +302,7 @@ int main()
         //log(e.what());
     }
 
-        
-    // No explicit call to stop the platform. 
+
+    // No explicit call to stop the platform.
     // When OCPlatform destructor is invoked, internally we do platform cleanup
 }
index 839eb81..e51db66 100644 (file)
@@ -42,12 +42,19 @@ namespace OC
 
         virtual OCStackResult registerResource(
                     OCResourceHandle& resourceHandle,
-                    std::string& resourceURI, 
-                    const std::string& resourceTypeName, 
+                    std::string& resourceURI,
+                    const std::string& resourceTypeName,
                     const std::string& resourceInterface,
                     std::function<void(const OCResourceRequest::Ptr, const OCResourceResponse::Ptr)> entityHandler,
                     uint8_t resourceProperty) = 0;
 
+        virtual OCStackResult bindTypeToResource(
+                    const OCResourceHandle& resourceHandle,
+                    const std::string& resourceTypeName) = 0;
+
+        virtual OCStackResult bindInterfaceToResource(
+                    const OCResourceHandle& resourceHandle,
+                    const std::string& resourceInterfaceName) = 0;
     };
 }
 
index de4627e..0f23bce 100644 (file)
@@ -45,11 +45,18 @@ namespace OC
                     std::function<void(const OCResourceRequest::Ptr, const OCResourceResponse::Ptr)> entityHandler,
                     uint8_t resourceProperty);
 
-       private:
-               void processFunc();
-               std::thread m_processThread;
+        virtual OCStackResult bindTypeToResource(
+                    const OCResourceHandle& resourceHandle,
+                    const std::string& resourceTypeName);
+
+        virtual OCStackResult bindInterfaceToResource(
+                    const OCResourceHandle& resourceHandle,
+                    const std::string& resourceInterface);
+    private:
+        void processFunc();
+        std::thread m_processThread;
         bool m_threadRun;
-               std::mutex m_csdkLock;
+        std::mutex m_csdkLock;
     };
 }
 
index 52aa5aa..a0d2a08 100644 (file)
  #define __INTEL_OCEXCEPTION_H_2014_07_10
 
 #include <stdexcept>
-
-namespace OC { namespace OCReflect {
+#include <ocstack.h>
+namespace OC {
 
 typedef std::runtime_error reflection_exception;
 
-class OCException : public std::exception
+class OCException : public std::runtime_error
 {
+    public:
+        OCException(const std::string& msg, OCStackResult reason = OC_STACK_ERROR): std::runtime_error(msg),  m_reason(reason) {}
+        std::string reason()
+        {
+            switch(m_reason)
+            {
+                case OC_STACK_OK:
+                    return "No Error";
+                case OC_STACK_INVALID_URI:
+                    return "Invalid URI";
+                case OC_STACK_INVALID_IP:
+                    return "Invalid IP";
+                case OC_STACK_INVALID_PORT:
+                    return "Invalid Port";
+                case OC_STACK_INVALID_CALLBACK:
+                    return "Invalid Callback";
+                case OC_STACK_INVALID_METHOD:
+                    return "Invalid Method";
+                case OC_STACK_INVALID_QUERY:
+                    return "Invalid Query";
+                case OC_STACK_INVALID_PARAM:
+                    return "Invalid Param";
+                case OC_STACK_INVALID_OBSERVE_PARAM:
+                    return "Invalid Observe Param";
+                case OC_STACK_NO_MEMORY:
+                    return "No Memory";
+                case OC_STACK_COMM_ERROR:
+                    return "Communication Error";
+                case OC_STACK_NOTIMPL:
+                    return "Not Implemented";
+                case OC_STACK_NO_RESOURCE:
+                    return "Resource Not Found";
+                case OC_STACK_RESOURCE_ERROR:
+                    return "Resource Error";
+                case OC_STACK_SLOW_RESOURCE:
+                    return "Slow Resource";
+                case OC_STACK_NO_OBSERVERS:
+                    return "No Observers";
+                case OC_STACK_ERROR:
+                    return "General Fault";
+                default:
+                    return "Unknown Error";
+            }
+        }
 
+    private:
+        OCStackResult m_reason;
 };
 
-}} // namespace OC::OCReflect
+} // namespace OC
 
 #endif
index 481890d..ceebb52 100644 (file)
@@ -18,9 +18,9 @@
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
-/// @file OCPlatform.h 
+/// @file OCPlatform.h
 
-/// @brief  This file contains the declaration of classes and its members related to 
+/// @brief  This file contains the declaration of classes and its members related to
 ///         OCPlatform.
 
 #ifndef __OCPLATFORM_H
@@ -39,7 +39,7 @@ namespace OC
 {
     /**
     *   @brief  Both server and client must initialize the core platform by instantiating OCPlatform.
-    *           On successful initialization, an instance of the OCPlatform is returned.  
+    *           On successful initialization, an instance of the OCPlatform is returned.
     *           APIs in OCPlatform provide mechanism to register a resource and host the resource
     *           on the server, find resources on the network etc.
     */
@@ -47,10 +47,10 @@ namespace OC
     {
     public:
         /**
-        * Constructor for OCPlatform. Constructs a new OCPlatform from a given PlatformConfig with 
+        * Constructor for OCPlatform. Constructs a new OCPlatform from a given PlatformConfig with
         * appropriate fields
         * @param config PlatformConfig struct which has details such as modeType (server/client/both),
-        *               in-proc/out-of-proc etc. 
+        *               in-proc/out-of-proc etc.
         */
         OCPlatform(const PlatformConfig& config);
 
@@ -60,71 +60,71 @@ namespace OC
         virtual ~OCPlatform(void);
 
         /**
-        * API for notifying core that resource's attributes have changed. 
+        * API for notifying core that resource's attributes have changed.
         *
         * @param OCResourceHandle resource handle of the resource
         *
-        * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. 
-        * NOTE: This API is for server side only. 
-        * NOTE: OCResourceHandle is defined in ocstack.h. 
-        * NOTE: OCStackResult is defined in ocstack.h. 
+        * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
+        * NOTE: This API is for server side only.
+        * NOTE: OCResourceHandle is defined in ocstack.h.
+        * NOTE: OCStackResult is defined in ocstack.h.
         */
         static OCStackResult notifyObservers(OCResourceHandle resourceHandle);
 
         /**
-        * API for Service and Resource Discovery. 
+        * API for Service and Resource Discovery.
         * NOTE: This API applies to client side only.
-        * 
+        *
         * @param host - Host IP Address of a service to direct resource discovery query. If null or
-        *        empty, performs multicast resource discovery query  
+        *        empty, performs multicast resource discovery query
         * @param resourceURI - name of the resource. If null or empty, performs search for all resource names
         * @param handler - Handles callbacks, success states and failure states.
-        * 
-        *        Four modes of discovery defined as follows: 
-        *        (NULL/Empty, NULL/Empty) - Performs ALL service discovery AND ALL resource discovery. 
-        *        (NULL/Empty, Not Empty) - Performs query for a filtered/scoped/particular resource(s) 
-        *                                  from ALL services. 
-        *        (Not Empty, NULL/Empty) - Performs ALL resource discovery on a particular service. 
-        *        (Not Empty, Not Empty) - Performs query for a filtered/scoped/particular resource(s) 
+        *
+        *        Four modes of discovery defined as follows:
+        *        (NULL/Empty, NULL/Empty) - Performs ALL service discovery AND ALL resource discovery.
+        *        (NULL/Empty, Not Empty) - Performs query for a filtered/scoped/particular resource(s)
+        *                                  from ALL services.
+        *        (Not Empty, NULL/Empty) - Performs ALL resource discovery on a particular service.
+        *        (Not Empty, Not Empty) - Performs query for a filtered/scoped/particular resource(s)
         *                                  from a particular service.
-        * 
+        *
         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
         * NOTE: First parameter 'host' currently represents an IP address. This will change in future
         * and will refer to endpoint interface so that we can refer to other transports such as BTH etc.
-        * NOTE: OCStackResult is defined in ocstack.h. 
+        * NOTE: OCStackResult is defined in ocstack.h.
         */
-        OCStackResult findResource(const std::string& host, const std::string& resourceURI, 
+        OCStackResult findResource(const std::string& host, const std::string& resourceURI,
             std::function<void(OCResource::Ptr)> resourceHandler);
 
         /**
         * This API registers a resource with the server
         * NOTE: This API applies to server side only.
-        * 
+        *
         * @param resourceHandle - Upon successful registration, resourceHandle will be filled
         * @param resourceURI - The URI of the resource. Example: "a/light". See NOTE below
         * @param resourceTypeName - The resource type. Example: "light"
-        * @param resourceInterface - The resource interface (whether it is collection etc). 
-        * @param entityHandler - entity handler callback. 
-        * @param resourceProperty - indicates the property of the resource. Defined in ocstack.h. 
+        * @param resourceInterface - The resource interface (whether it is collection etc).
+        * @param entityHandler - entity handler callback.
+        * @param resourceProperty - indicates the property of the resource. Defined in ocstack.h.
         * setting resourceProperty as OC_DISCOVERABLE will allow Discovery of this resource
         * setting resourceProperty as OC_OBSERVABLE will allow observation
         * settings resourceProperty as OC_DISCOVERABLE | OC_OBSERVABLE will allow both discovery and observation
-        * 
+        *
         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
-        * NOTE: "a/light" is a relative URI. 
+        * NOTE: "a/light" is a relative URI.
         * Above relative URI will be prepended (by core) with a host IP + namespace "oc"
         * Therefore, fully qualified URI format would be //HostIP-Address/namespace/relativeURI"
         * Example, a relative URI: 'a/light' will result in a fully qualified URI: //134.134.161.33/oc/a/light"
-        * First parameter can take a relative URI and core will take care of preparing the fully qualified URI 
+        * First parameter can take a relative URI and core will take care of preparing the fully qualified URI
         * OR
         * first paramter can take fully qualified URI and core will take that as is for further operations
-        * NOTE: OCStackResult is defined in ocstack.h. 
+        * NOTE: OCStackResult is defined in ocstack.h.
         */
-        OCStackResult registerResource(OCResourceHandle& resourceHandle, 
-                        std::string& resourceURI, 
-                        const std::string& resourceTypeName, 
-                        const std::string& resourceInterface, 
-                        std::function<void(const OCResourceRequest::Ptr, const OCResourceResponse::Ptr)> entityHandler, 
+        OCStackResult registerResource(OCResourceHandle& resourceHandle,
+                        std::string& resourceURI,
+                        const std::string& resourceTypeName,
+                        const std::string& resourceInterface,
+                        std::function<void(const OCResourceRequest::Ptr, const OCResourceResponse::Ptr)> entityHandler,
                         uint8_t resourceProperty);
 
         /**
@@ -183,7 +183,7 @@ namespace OC
         * At the end of Step 4, resource "a/home" will no longer reference "a/kitchen". <br>
         */
         OCStackResult unbindResource(OCResourceHandle containerHandle, OCResourceHandle resourceHandle);
-     
+
         /**
         * Unbind resources from a container resource.
         *
@@ -206,13 +206,34 @@ namespace OC
         */
         OCStackResult unbindResources(OCResourceHandle containerHandle, std::vector<OCResourceHandle>& resourceHandleList);
 
+        /**
+        * Binds a type to a particular resource
+        * @param resourceHandle - handle to the resource
+        * @param resourceTypeName - new typename to bind to the resource
+
+        * @return OCStackResult - return value of the API. Returns OCSTACk_OK if success <br>
+        */
+        OCStackResult bindTypeToResource(const OCResourceHandle& resourceHandle,
+                        const std::string& resourceTypeName) const;
+
+        /**
+        * Binds an interface to a particular resource
+        * @param resourceHandle - handle to the resource
+        * @param resourceTypeName - new interface  to bind to the resource
+
+        * @return OCStackResult - return value of the API. Returns OCSTACk_OK if success <br>
+        */
+        OCStackResult bindInterfaceToResource(const OCResourceHandle& resourceHandle,
+                        const std::string& resourceInterfaceName) const;
+
+
     private:
         std::unique_ptr<WrapperFactory> m_WrapperInstance;
         IServerWrapper::Ptr m_server;
         IClientWrapper::Ptr m_client;
 
         /**
-        *  Private function to initalize the platfrom 
+        *  Private function to initalize the platfrom
         */
         void init(const PlatformConfig& config);
 
index dd1e0d9..1a4df82 100644 (file)
@@ -42,6 +42,19 @@ namespace OC
             // Not implemented
             return OC_STACK_ERROR;
         }
+        virtual OCStackResult bindTypeToResource(
+                     const OCResourceHandle& resourceHandle,
+                     const std::string& resourceTypeName)
+        {   //Not implemented yet
+            return OC_STACK_ERROR;
+        }
+
+        virtual OCStackResult bindInterfaceToResource(
+                     const OCResourceHandle& resourceHandle,
+                     const std::string& resourceInterfaceName)
+        {   //Not implemented yet
+            return OC_STACK_ERROR;
+        }
     };
 }