Implemented Secure resource support in C++ layer
authorSashi Penta <sashi.kumar.penta@intel.com>
Fri, 14 Nov 2014 05:32:27 +0000 (21:32 -0800)
committerErich Keane <erich.keane@intel.com>
Fri, 14 Nov 2014 05:35:28 +0000 (21:35 -0800)
Added ability for the C++ stack to properly parse/support the secure protocol URIs.

Change-Id: I800d29598854339a2d30ae5b4ef89c2d0973dfe7
Signed-off-by: Sakthivel Samidurai <sakthivel.samidurai@intel.com>
resource/examples/simpleclientserver.cpp
resource/examples/simpleserver.cpp
resource/include/IClientWrapper.h
resource/include/InProcClientWrapper.h
resource/include/OCApi.h
resource/include/OutOfProcClientWrapper.h
resource/include/StringConstants.h
resource/src/InProcClientWrapper.cpp

index 444c4dc..659a2b7 100644 (file)
@@ -183,7 +183,7 @@ struct FooResource
         std::string resourceTypeName = "core.foo";
         std::string resourceInterface = DEFAULT_INTERFACE;
 
-        uint8_t resourceProperty = OC_DISCOVERABLE;
+        uint8_t resourceProperty = OC_DISCOVERABLE | OC_SECURE ;
 
         EntityHandler eh(std::bind(&FooResource::entityHandler,
                     this, std::placeholders::_1, std::placeholders::_2));
index a8138f0..492fc80 100644 (file)
@@ -83,7 +83,7 @@ public:
         std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
 
         // OCResourceProperty is defined ocstack.h
-        uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
+        uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE;
 
         EntityHandler cb = std::bind(&LightResource::entityHandler, this,PH::_1, PH::_2);
 
@@ -105,7 +105,7 @@ public:
         std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
 
         // OCResourceProperty is defined ocstack.h
-        uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
+        uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE;
 
         EntityHandler cb = std::bind(&LightResource::entityHandler, this,PH::_1, PH::_2);
 
index e898a8d..164684d 100644 (file)
@@ -86,7 +86,7 @@ namespace OC
         // 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
         virtual std::shared_ptr<OCResource> parseOCResource(IClientWrapper::Ptr clientWrapper,
-                        const std::string& host, const boost::property_tree::ptree resourceNode)=0;
+                        OCDevAddr& addr, const boost::property_tree::ptree resourceNode)=0;
     private:
     };
 }
index b0af439..bc0a9d5 100644 (file)
@@ -39,7 +39,14 @@ namespace OC
 {
     class InProcClientWrapper : public IClientWrapper
     {
+
     public:
+        enum OCSecureType
+        {
+            IPV4Secure,
+            IPV4
+        };
+
         InProcClientWrapper(OC::OCPlatform_impl& owner, std::weak_ptr<std::recursive_mutex> csdkLock,
                             PlatformConfig cfg);
         virtual ~InProcClientWrapper();
@@ -80,10 +87,12 @@ namespace OC
         // 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
         virtual std::shared_ptr<OCResource> parseOCResource(IClientWrapper::Ptr clientWrapper,
-            const std::string& host, const boost::property_tree::ptree resourceNode);
+            OCDevAddr& addr, const boost::property_tree::ptree resourceNode);
 
         OCStackResult GetDefaultQos(QualityOfService& QoS);
     private:
+        std::string convertOCAddrToString(OCDevAddr& addr,
+        OCSecureType type, const std::string &portStr = std::string());
         void listeningFunc();
         std::string assembleSetResourceUri(std::string uri, const QueryParamsMap& queryParams);
         std::string assembleSetResourcePayload(const OCRepresentation& attributes);
index fd988af..ec49526 100644 (file)
@@ -72,6 +72,7 @@ namespace OC
 
 namespace OC
 {
+
     enum class OCPlatformStatus
     {
         PlatformUp,
index 4f88207..69b4e78 100644 (file)
@@ -70,7 +70,7 @@ namespace OC
             const HeaderOptions& headerOptions, QualityOfService QoS){return OC_STACK_NOTIMPL;}
 
         virtual std::shared_ptr<OCResource> parseOCResource(IClientWrapper::Ptr clientWrapper,
-            const std::string& host, const boost::property_tree::ptree resourceNode)
+            OCDevAddr& addr, const boost::property_tree::ptree resourceNode)
         {
             return nullptr;
         }
index 1892efd..e8b4b5a 100644 (file)
@@ -117,7 +117,9 @@ namespace OC
         static const std::string INTERFACESKEY              = "if";
         static const std::string PROPERTYKEY                = "prop";
         static const std::string REPKEY                     = "rep";
-    }
+        static const std::string SECUREKEY                  = "sec";
+        static const std::string PORTKEY                    = "port";
+  }
 
 }
 
index a56833a..e9af394 100644 (file)
@@ -39,6 +39,7 @@ namespace OC
     {
         // if the config type is server, we ought to never get called.  If the config type
         // is both, we count on the server to run the thread and do the initialize
+
         if(m_cfg.mode == ModeType::Client)
         {
             OCStackResult result = OCInit(m_cfg.ipAddress.c_str(), m_cfg.port, OC_CLIENT);
@@ -90,9 +91,8 @@ namespace OC
         }
     }
 
-
-
-    std::string convertOCAddrToString(OCDevAddr& addr)
+    std::string InProcClientWrapper::convertOCAddrToString(OCDevAddr& addr,
+        OCSecureType type, const std::string &portStr)
     {
         // TODO: we currently assume this is a IPV4 address, need to figure out the actual value
 
@@ -102,7 +102,18 @@ namespace OC
         if(OCDevAddrToIPv4Addr(&addr, &a, &b, &c, &d) ==0 && OCDevAddrToPort(&addr, &port)==0)
         {
             ostringstream os;
-            os << "coap://"<<(int)a<<'.'<<(int)b<<'.'<<(int)c<<'.'<<(int)d<<':'<<(int)port;
+            if(type == OCSecureType::IPV4)
+            {
+                os << "coap://" << static_cast<int>(a) << '.' <<
+                    static_cast<int>(b) << '.' << static_cast<int>(c) <<
+                    '.' << static_cast<int>(d) << ':' <<static_cast<int>(port);
+            }
+            else if(type == OCSecureType::IPV4Secure)
+            {
+                 os << "coaps://" << static_cast<int>(a) <<'.' <<
+                    static_cast<int>(b) <<'.' << static_cast<int>(c) <<
+                    '.' << static_cast<int>(d) << ':' << portStr;
+            }
             return os.str();
         }
         else
@@ -119,7 +130,7 @@ namespace OC
 
 
     std::shared_ptr<OCResource> InProcClientWrapper::parseOCResource(
-        IClientWrapper::Ptr clientWrapper, const std::string& host,
+        IClientWrapper::Ptr clientWrapper, OCDevAddr& addr,
         const boost::property_tree::ptree resourceNode)
     {
         std::string uri = resourceNode.get<std::string>(OC::Key::URIKEY, "");
@@ -136,6 +147,7 @@ namespace OC
         {
             rTs.push_back(itr.second.data());
         }
+        bool secure = properties.get<int>(OC::Key::SECUREKEY,0) == 1;
 
         boost::property_tree::ptree iF =
             properties.get_child(OC::Key::INTERFACESKEY, boost::property_tree::ptree());
@@ -144,6 +156,17 @@ namespace OC
             ifaces.push_back(itr.second.data());
         }
 
+        std::string host;
+        if(secure)
+        {
+            string port = properties.get<string>(OC::Key::PORTKEY,"");
+            host= convertOCAddrToString(addr, OCSecureType::IPV4Secure, port);
+        }
+        else
+        {
+            host= convertOCAddrToString(addr, OCSecureType::IPV4);
+        }
+
         return std::shared_ptr<OCResource>(
             new OCResource(clientWrapper, host, uri, obs, rTs, ifaces));
     }
@@ -186,9 +209,9 @@ namespace OC
         {
                 try
                 {
-                    std::string host = convertOCAddrToString(*clientResponse->addr);
                     std::shared_ptr<OCResource> resource =
-                        context->clientWrapper->parseOCResource(context->clientWrapper, host,
+                        context->clientWrapper->parseOCResource(context->clientWrapper,
+                        *clientResponse->addr,
                         payloadItr.second);
 
                     // Note: the call to detach allows the underlying thread to continue until