Merge "Updated buildscript.mk to include build of TB unit tests and samples."
authorErich Keane <erich.keane@intel.com>
Thu, 16 Oct 2014 16:52:10 +0000 (09:52 -0700)
committerGerrit Code Review <gerrit@oicreview.vlan14.01.org>
Thu, 16 Oct 2014 16:52:10 +0000 (09:52 -0700)
14 files changed:
csdk/stack/include/ocstack.h
include/InitializeException.h
include/OCApi.h
include/OCResourceRequest.h
include/ResourceInitException.h
include/StringConstants.h [new file with mode: 0644]
include/WrapperFactory.h
src/InProcClientWrapper.cpp
src/InProcServerWrapper.cpp
src/OCException.cpp
src/OCResource.cpp
unittests/README [new file with mode: 0644]
unittests/makefile [new file with mode: 0644]
unittests/tests.cpp [new file with mode: 0644]

index af6f77e..f1a04fd 100644 (file)
@@ -347,6 +347,12 @@ OCStackResult OCInit(const char *ipAddr, uint16_t port, OCMode mode);
 
 /**
  * Stop the OC stack.  Use for a controlled shutdown.
+ *
+ * Note: OCStop() performs operations similar to OCStopPresence(), as well as OCDeleteResource() on
+ * all resources this server is hosting. OCDeleteResource() performs operations similar to
+ * OCNotifyAllObservers() to notify all client observers that the respective resource is being
+ * deleted.
+ *
  * @return
  *     OC_STACK_OK    - no errors
  *     OC_STACK_ERROR - stack not initialized
@@ -561,6 +567,9 @@ OCResourceHandle OCGetResourceHandle(uint8_t index);
  * Delete resource specified by handle.  Deletes resource and all resourcetype and resourceinterface
  * linked lists.
  *
+ * Note: OCDeleteResource() performs operations similar to OCNotifyAllObservers() to notify all
+ * client observers that "this" resource is being deleted.
+ *
  * @param handle - handle of resource to be deleted
  *
  * @return
index d189f38..c6eed56 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <stdexcept>
 #include <ocstack.h>
+#include "StringConstants.h"
 
 namespace OC
 {
@@ -48,21 +49,21 @@ namespace OC
             switch(m_reason)
             {
             case OC_STACK_OK:
-                return "No Error";
+                return OC::InitException::NO_ERROR;
             case OC_STACK_INVALID_URI:
-                return "Invalid URI";
+                return OC::InitException::INVALID_URI;
             case OC_STACK_INVALID_IP:
-                return "Invalid IP";
+                return OC::InitException::INVALID_IP;
             case OC_STACK_INVALID_PORT:
-                return "Invalid Port";
+                return OC::InitException::INVALID_PORT;
             case OC_STACK_INVALID_CALLBACK:
-                return "Invalid Callback";
+                return OC::InitException::INVALID_CB;
             case OC_STACK_INVALID_METHOD:
-                return "Invalid Method";
+                return OC::InitException::INVALID_METHOD;
             case OC_STACK_ERROR:
-                return "General Fault";
+                return OC::InitException::GENERAL_FAULT;
             default:
-                return "Unknown Error";
+                return OC::InitException::UNKNOWN_ERROR;
             }
         }
 
@@ -72,4 +73,4 @@ namespace OC
     };
 }
 
-#endif
\ No newline at end of file
+#endif
index c041be6..30f9e4a 100644 (file)
@@ -35,7 +35,7 @@
 #include "ocstack.h"
 #include "OCHeaderOption.h"
 #include <OCException.h>
-
+#include "StringConstants.h"
 #include "oc_logger.hpp"
 
 namespace OC
@@ -531,7 +531,7 @@ namespace OC
                 }
                 else
                 {
-                    throw OCException("Array type should have at least []");
+                    throw OCException(OC::Exception::INVALID_ARRAY);
                 }
 
             }
@@ -553,7 +553,7 @@ namespace OC
                 }
                 else
                 {
-                    throw OCException("Array type should have at least []");
+                    throw OCException(OC::Exception::INVALID_ARRAY);
                 }
             }
 
@@ -575,7 +575,7 @@ namespace OC
                 }
                 else
                 {
-                    throw OCException("Array type should have at least []");
+                    throw OCException(OC::Exception::INVALID_ARRAY);
                 }
 
             }
@@ -598,7 +598,7 @@ namespace OC
                 }
                 else
                 {
-                    throw OCException("Array type should have at least []");
+                    throw OCException(OC::Exception::INVALID_ARRAY);
                 }
             }
 
@@ -620,7 +620,7 @@ namespace OC
                 }
                 else
                 {
-                    throw OCException("Array type should have at least []");
+                    throw OCException(OC::Exception::INVALID_ARRAY);
                 }
             }
 
index dffcfd0..890a4a7 100644 (file)
@@ -150,7 +150,7 @@ namespace OC
 
             // TODO this expects the representation oc:{} and not oc:[{}]
             //      this representation is fine when setting for simple resource.
-            boost::property_tree::ptree payload = root.get_child("oc", boost::property_tree::ptree());
+            boost::property_tree::ptree payload = root.get_child(OC::Key::OCKEY, boost::property_tree::ptree());
 
             for(auto& item: payload)
             {
index 8cca8c2..a3ebea6 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <stdexcept>
 #include <ocstack.h>
+#include "StringConstants.h"
 
 namespace OC
 {
@@ -60,22 +61,22 @@ namespace OC
 
             if(isUriMissing())
             {
-                ret += "Missing URI;";
+                ret += OC::InitException::MISSING_URI;
             }
 
             if(isTypeMissing())
             {
-                ret += "Missing Resource Type;";
+                ret += OC::InitException::MISSING_TYPE;
             }
 
             if(isInterfaceMissing())
             {
-                ret += "Missing Interface;";
+                ret += OC::InitException::MISSING_INTERFACE;
             }
 
             if(isClientWrapperMissing())
             {
-                ret += "Missing ClientWrapper;";
+                ret += OC::InitException::MISSING_CLIENT_WRAPPER;
             }
 
             return ret.c_str();
diff --git a/include/StringConstants.h b/include/StringConstants.h
new file mode 100644 (file)
index 0000000..24c985f
--- /dev/null
@@ -0,0 +1,119 @@
+//******************************************************************
+//
+// Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#ifndef _STRING_CONSTANTS_H_
+#define _STRING_CONSTANTS_H_
+
+#include <string>
+
+using namespace std;
+
+namespace OC
+{
+    namespace InitException
+    {
+        static const std::string NO_ERROR                   = "No Error";
+        static const std::string INVALID_URI                = "Invalid URI";
+        static const std::string INVALID_PORT               = "Invalid Port";
+        static const std::string INVALID_IP                 = "Invalid IP";
+        static const std::string INVALID_CB                 = "Invalid Callback";
+        static const std::string INVALID_METHOD             = "Invalid Method";
+        static const std::string GENERAL_FAULT              = "General Fault";
+        static const std::string UNKNOWN_ERROR              = "Unknown Error";
+
+        static const std::string STACK_INIT_ERROR           = "Error Initializing Stack";
+        static const std::string NOT_CONFIGURED_AS_SERVER   =
+                          "Cannot static construct a Server when configured as a client";
+        static const std::string INVALID_PARAM              = "Invalid Param";
+        static const std::string MISSING_URI                = "Missing URI;";
+        static const std::string MISSING_TYPE               = "Missing Resource Type;";
+        static const std::string MISSING_INTERFACE          = "Missing Interface;";
+        static const std::string MISSING_CLIENT_WRAPPER     = "Missing ClientWrapper;";
+    }
+
+    namespace Exception // Not To Be Confused With 'InitException'
+    {
+        static const std::string SVCTYPE_OUTOFPROC          = "ServiceType::OutOfProc";
+        static const std::string BIND_TYPE_FAILED           = "Bind Type to resource failed";
+        static const std::string BIND_INTERFACE_FAILED      = "Bind Interface to resource failed";
+        static const std::string START_PRESENCE_FAILED      = "startPresence failed";
+        static const std::string END_PRESENCE_FAILED        = "stopPresence failed";
+        static const std::string INVALID_ARRAY              = "Array type should have at least []";
+        static const std::string STR_NULL_RESPONSE          = "Response is NULL";
+        static const std::string STR_PAYLOAD_OVERFLOW       = "Payload overflow";
+
+
+        static const std::string NO_ERROR                   = "No Error";
+        static const std::string RESOURCE_CREATED           = "Resource Created";
+        static const std::string RESOURCE_DELETED           = "Resource Deleted";
+        static const std::string INVALID_URI                = "Invalid URI";
+        static const std::string INVALID_IP                 = "Invalid IP";
+        static const std::string INVALID_PORT               = "Invalid Port";
+        static const std::string INVALID_CB                 = "Invalid Callback";
+        static const std::string INVALID_METHOD             = "Invalid Method";
+        static const std::string INVALID_QUERY              = "Invalid Query";
+        static const std::string INVALID_PARAM              = "Invalid Param";
+        static const std::string INVALID_OBESERVE           = "Invalid Observe Param";
+        static const std::string NO_MEMORY                  = "No Memory";
+        static const std::string COMM_ERROR                 = "Communication Error";
+        static const std::string NOT_IMPL                   = "Not Implemented";
+        static const std::string NOT_FOUND                  = "Resource Not Found";
+        static const std::string RESOURCE_ERROR             = "Resource Error";
+        static const std::string SLOW_RESOURCE              = "Slow Resource";
+        static const std::string NO_OBSERVERS               = "No Observers";
+        static const std::string OBSV_NO_FOUND              = "Stack observer not found";
+        static const std::string OBSV_NOT_ADDED             = "Stack observer not added";
+        static const std::string OBSV_NOT_REMOVED           = "Stack observer not removed";
+        static const std::string STACK_RESOURCE_DELETED     = "The specified resource has been deleted";
+        static const std::string PRESENCE_STOPPED           = "Stack presence stopped";
+        static const std::string PRESENCE_NOT_HANDLED       = "Stack presence should not be handled";
+        static const std::string INVALID_OPTION             = "Invalid option";
+        static const std::string GENERAL_FAULT              = "General Fault";
+        static const std::string MALFORMED_STACK_RESPONSE   = "Response from OC_STACK is malformed";
+        static const std::string UNKNOWN_ERROR              = "Unknown Error";
+    }
+
+    namespace Error
+    {
+        static const std::string INVALID_IP                 = "Invalid IP";
+    }
+
+    namespace PlatformCommands
+    {
+        static const std::string GET                        = "GET";
+        static const std::string PUT                        = "PUT";
+        static const std::string POST                       = "POST";
+        static const std::string DELETE                     = "DELETE";
+    }
+
+    namespace Key
+    {
+        static const std::string OCKEY                      = "oc";
+        static const std::string URIKEY                     = "href";
+        static const std::string OBSERVABLEKEY              = "obs";
+        static const std::string RESOURCETYPESKEY           = "rt";
+        static const std::string INTERFACESKEY              = "if";
+        static const std::string PROPERTYKEY                = "prop";
+        static const std::string REPKEY                     = "rep";
+    }
+
+}
+
+#endif // _STRING_CONSTANTS_H_
index 45e349c..c4923e4 100644 (file)
@@ -29,6 +29,7 @@
 #include <InProcClientWrapper.h>
 #include <OutOfProcServerWrapper.h>
 #include <InProcServerWrapper.h>
+#include "StringConstants.h"
 
 namespace OC
 {
@@ -75,7 +76,7 @@ namespace OC
                 return std::make_shared<InProcServerWrapper>(owner, csdkLock, cfg);
                 break;
             case ServiceType::OutOfProc:
-                throw OC::OCException("ServiceType::OutOfProc", OC_STACK_NOTIMPL);
+                throw OC::OCException(OC::Exception::SVCTYPE_OUTOFPROC, OC_STACK_NOTIMPL);
                 break;
             }
                        return nullptr;
index c875f63..e8a41f3 100644 (file)
@@ -45,7 +45,7 @@ namespace OC
 
             if(OC_STACK_OK != result)
             {
-                throw InitializeException("Error Initializing Stack", result);
+                throw InitializeException(OC::InitException::STACK_INIT_ERROR, result);
             }
 
             m_threadRun = true;
@@ -107,7 +107,7 @@ namespace OC
         }
         else
         {
-            return "INVALID IP";
+            return OC::Error::INVALID_IP;
         }
     }
 
@@ -119,34 +119,27 @@ namespace OC
     };
 
 
-    const std::string URIKEY = "href";
-    const std::string OBSERVABLEKEY = "obs";
-    const std::string RESOURCETYPESKEY= "rt";
-    const std::string INTERFACESKEY = "if";
-    const std::string PROPERTYKEY = "prop";
-    const std::string REPKEY = "rep";
-
     std::shared_ptr<OCResource> InProcClientWrapper::parseOCResource(
         IClientWrapper::Ptr clientWrapper, const std::string& host,
         const boost::property_tree::ptree resourceNode)
     {
-        std::string uri = resourceNode.get<std::string>(URIKEY, "");
-        bool obs = resourceNode.get<int>(OBSERVABLEKEY,0) == 1;
+        std::string uri = resourceNode.get<std::string>(OC::Key::URIKEY, "");
+        bool obs = resourceNode.get<int>(OC::Key::OBSERVABLEKEY,0) == 1;
         std::vector<std::string> rTs;
         std::vector<std::string> ifaces;
 
         boost::property_tree::ptree properties =
-            resourceNode.get_child(PROPERTYKEY, boost::property_tree::ptree());
+            resourceNode.get_child(OC::Key::INTERFACESKEY, boost::property_tree::ptree());
 
         boost::property_tree::ptree rT =
-            properties.get_child(RESOURCETYPESKEY, boost::property_tree::ptree());
+            properties.get_child(OC::Key::RESOURCETYPESKEY, boost::property_tree::ptree());
         for(auto itr : rT)
         {
             rTs.push_back(itr.second.data());
         }
 
         boost::property_tree::ptree iF =
-            properties.get_child(INTERFACESKEY, boost::property_tree::ptree());
+            properties.get_child(OC::Key::INTERFACESKEY, boost::property_tree::ptree());
         for(auto itr : iF)
         {
             ifaces.push_back(itr.second.data());
@@ -281,7 +274,7 @@ namespace OC
         {
             return OCRepresentation();
         }
-        boost::property_tree::ptree payload = root.get_child("oc", boost::property_tree::ptree());
+        boost::property_tree::ptree payload = root.get_child(OC::Key::OCKEY, boost::property_tree::ptree());
         OCRepresentation root_resource;
         std::vector<OCRepresentation> children;
         bool isRoot = true;
@@ -291,7 +284,7 @@ namespace OC
             try
             {
                 auto resourceNode = payloadItr.second;
-                std::string uri = resourceNode.get<std::string>(URIKEY, "");
+                std::string uri = resourceNode.get<std::string>(OC::Key::URIKEY, "");
 
                 if (isRoot)
                 {
@@ -302,22 +295,22 @@ namespace OC
                     child.setUri(uri);
                 }
 
-                if( resourceNode.count(PROPERTYKEY) != 0 )
+                if( resourceNode.count(OC::Key::INTERFACESKEY) != 0 )
                 {
                     std::vector<std::string> rTs;
                     std::vector<std::string> ifaces;
                     boost::property_tree::ptree properties =
-                        resourceNode.get_child(PROPERTYKEY, boost::property_tree::ptree());
+                        resourceNode.get_child(OC::Key::INTERFACESKEY, boost::property_tree::ptree());
 
                     boost::property_tree::ptree rT =
-                        properties.get_child(RESOURCETYPESKEY, boost::property_tree::ptree());
+                        properties.get_child(OC::Key::RESOURCETYPESKEY, boost::property_tree::ptree());
                     for(auto itr : rT)
                     {
                         rTs.push_back(itr.second.data());
                     }
 
                     boost::property_tree::ptree iF =
-                        properties.get_child(INTERFACESKEY, boost::property_tree::ptree());
+                        properties.get_child(OC::Key::INTERFACESKEY, boost::property_tree::ptree());
                     for(auto itr : iF)
                     {
                         ifaces.push_back(itr.second.data());
@@ -334,10 +327,10 @@ namespace OC
                     }
                 }
 
-                if( resourceNode.count(REPKEY) != 0 )
+                if( resourceNode.count(OC::Key::INTERFACESKEY) != 0 )
                 {
                     boost::property_tree::ptree rep =
-                        resourceNode.get_child(REPKEY, boost::property_tree::ptree());
+                        resourceNode.get_child(OC::Key::INTERFACESKEY, boost::property_tree::ptree());
                     AttributeMap attrs;
                     for( auto item : rep)
                     {
index 4af539d..43c9a77 100644 (file)
@@ -90,17 +90,17 @@ void formResourceRequest(OCEntityHandlerFlag flag,
             if(OC_REST_GET == entityHandlerRequest->method)
             {
                 // TODO Why strings : "GET"??
-                pRequest->setRequestType("GET");
+                pRequest->setRequestType(OC::PlatformCommands::GET);
             }
             else if(OC_REST_PUT == entityHandlerRequest->method)
             {
-                pRequest->setRequestType("PUT");
+                pRequest->setRequestType(OC::PlatformCommands::PUT);
                 pRequest->setPayload(std::string(reinterpret_cast<const char*>
                                             (entityHandlerRequest->reqJSONPayload)));
             }
             else if(OC_REST_POST == entityHandlerRequest->method)
             {
-                pRequest->setRequestType("POST");
+                pRequest->setRequestType(OC::PlatformCommands::POST);
                 pRequest->setPayload(std::string(reinterpret_cast<const char*>
                                             (entityHandlerRequest->reqJSONPayload)));
             }
@@ -142,7 +142,7 @@ void processResourceResponse(OCEntityHandlerFlag flag,
         }
         else
         {
-            throw OCException("Response is NULL", OC_STACK_MALFORMED_RESPONSE);
+            throw OCException(OC::Exception::STR_NULL_RESPONSE, OC_STACK_MALFORMED_RESPONSE);
         }
 
         if (payLoad.size() < entityHandlerRequest->resJSONPayloadLen)
@@ -169,7 +169,7 @@ void processResourceResponse(OCEntityHandlerFlag flag,
         }
         else
         {
-            throw OCException("Payload overflow", OC_STACK_MALFORMED_RESPONSE);
+            throw OCException(OC::Exception::STR_PAYLOAD_OVERFLOW, OC_STACK_MALFORMED_RESPONSE);
         }
     }
 
@@ -296,7 +296,7 @@ namespace OC
         }
         else
         {
-            throw InitializeException("Cannot construct a Server when configured as a client",
+            throw InitializeException(OC::InitException::NOT_CONFIGURED_AS_SERVER,
                                       OC_STACK_INVALID_PARAM);
         }
 
@@ -304,7 +304,7 @@ namespace OC
 
         if(OC_STACK_OK != result)
         {
-            throw InitializeException("Error Initializing Stack", result);
+            throw InitializeException(OC::InitException::STACK_INIT_ERROR, result);
         }
 
         m_threadRun = true;
@@ -451,7 +451,7 @@ namespace OC
 
         if (result != OC_STACK_OK)
         {
-            throw OCException("Bind Type to resource failed", result);
+            throw OCException(OC::Exception::BIND_TYPE_FAILED, result);
         }
         return result;
     }
@@ -475,7 +475,7 @@ namespace OC
 
         if (result != OC_STACK_OK)
         {
-            throw OCException("Bind Interface to resource failed", result);
+            throw OCException(OC::Exception::BIND_INTERFACE_FAILED, result);
         }
         return result;
     }
@@ -492,7 +492,7 @@ namespace OC
 
         if(result != OC_STACK_OK)
         {
-            throw OCException("startPresence failed", result);
+            throw OCException(OC::Exception::START_PRESENCE_FAILED, result);
         }
         return result;
     }
@@ -509,7 +509,7 @@ namespace OC
 
         if(result != OC_STACK_OK)
         {
-            throw OCException("stopPresence failed", result);
+            throw OCException(OC::Exception::END_PRESENCE_FAILED, result);
         }
         return result;
     }
index f50fa59..612dc1d 100644 (file)
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
 #include "OCException.h"
+#include "StringConstants.h"
 
 std::string OC::OCException::reason(const OCStackResult sr)
 {
     switch(sr)
     {
         case OC_STACK_OK:
-             return "No Error";
+             return OC::Exception::NO_ERROR;
         case OC_STACK_RESOURCE_CREATED:
-             return "Resource Created";
+             return OC::Exception::RESOURCE_CREATED;
         case OC_STACK_RESOURCE_DELETED:
-             return "Resource Deleted";
+             return OC::Exception::RESOURCE_DELETED;
         case OC_STACK_INVALID_URI:
-            return "Invalid URI";
+            return OC::Exception::INVALID_URI;
         case OC_STACK_INVALID_IP:
-            return "Invalid IP";
+            return OC::Exception::INVALID_IP;
         case OC_STACK_INVALID_PORT:
-            return "Invalid Port";
+            return OC::Exception::INVALID_PORT;
         case OC_STACK_INVALID_CALLBACK:
-            return "Invalid Callback";
+            return OC::Exception::INVALID_CB;
         case OC_STACK_INVALID_METHOD:
-            return "Invalid Method";
+            return OC::Exception::INVALID_METHOD;
         case OC_STACK_INVALID_QUERY:
-            return "Invalid Query";
+            return OC::Exception::INVALID_QUERY;
         case OC_STACK_INVALID_PARAM:
-            return "Invalid Parameter";
+            return OC::Exception::INVALID_PARAM;
         case OC_STACK_INVALID_OBSERVE_PARAM:
-            return "Invalid Observe Parameter";
+            return OC::Exception::INVALID_OBESERVE;
         case OC_STACK_NO_MEMORY:
-            return "No Memory";
+            return OC::Exception::NO_MEMORY;
         case OC_STACK_COMM_ERROR:
-            return "Communication Error";
+            return OC::Exception::COMM_ERROR;
         case OC_STACK_NOTIMPL:
-            return "Not Implemented";
+            return OC::Exception::NOT_IMPL;
         case OC_STACK_NO_RESOURCE:
-            return "Resource Not Found";
+            return OC::Exception::NOT_FOUND;
         case OC_STACK_RESOURCE_ERROR:
-            return "Resource Error";
+            return OC::Exception::RESOURCE_ERROR;
         case OC_STACK_SLOW_RESOURCE:
-            return "Slow Resource";
+            return OC::Exception::SLOW_RESOURCE;
         case OC_STACK_NO_OBSERVERS:
-            return "No Observers";
-       case OC_STACK_OBSERVER_NOT_FOUND:
-            return "Stack observer not found";
-       case OC_STACK_OBSERVER_NOT_ADDED:
-            return "Stack observer not added";
-       case OC_STACK_OBSERVER_NOT_REMOVED:
-            return "Stack observer not removed";
+            return OC::Exception::NO_OBSERVERS;
+        case OC_STACK_OBSERVER_NOT_FOUND:
+            return OC::Exception::OBSV_NO_FOUND;
+        case OC_STACK_OBSERVER_NOT_ADDED:
+            return OC::Exception::OBSV_NOT_ADDED;
+        case OC_STACK_OBSERVER_NOT_REMOVED:
+            return OC::Exception::OBSV_NOT_REMOVED;
 #ifdef WITH_PRESENCE
-       case OC_STACK_PRESENCE_STOPPED:
-            return "Stack presence stopped";
-       case OC_STACK_PRESENCE_DO_NOT_HANDLE:
-            return "Stack presence should not be handled";
+        case OC_STACK_PRESENCE_STOPPED:
+            return OC::Exception::PRESENCE_STOPPED;
+        case OC_STACK_PRESENCE_DO_NOT_HANDLE:
+            return OC::Exception::PRESENCE_NOT_HANDLED;
 #endif
-       case OC_STACK_INVALID_OPTION:
-            return "Invalid option";
-       case OC_STACK_MALFORMED_RESPONSE:
-            return "Malformed response";
-       case OC_STACK_ERROR:
-            return "General Fault";
+        case OC_STACK_INVALID_OPTION:
+            return OC::Exception::INVALID_OPTION;
+        case OC_STACK_MALFORMED_RESPONSE:
+            return OC::Exception::MALFORMED_STACK_RESPONSE;
+        case OC_STACK_ERROR:
+            return OC::Exception::GENERAL_FAULT;
     }
 
-    return "Unknown Error";
+    return OC::Exception::UNKNOWN_ERROR;
 }
 
index ee9cca7..b3062e9 100644 (file)
@@ -83,12 +83,12 @@ OCStackResult OCResource::get(const std::string& resourceType, const std::string
 
     if(!resourceType.empty())
     {
-        mapCpy["rt"]=resourceType;
+        mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
     }
 
     if(!resourceInterface.empty())
     {
-        mapCpy["if"]= resourceInterface;
+        mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
     }
 
     return result_guard(get(mapCpy, attributeHandler, QoS));
@@ -132,12 +132,12 @@ OCStackResult OCResource::put(const std::string& resourceType,
 
     if(!resourceType.empty())
     {
-        mapCpy["rt"]=resourceType;
+        mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
     }
 
     if(!resourceInterface.empty())
     {
-        mapCpy["if"]=resourceInterface;
+        mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
     }
 
     return result_guard(put(rep, mapCpy, attributeHandler, QoS));
@@ -181,12 +181,12 @@ OCStackResult OCResource::post(const std::string& resourceType,
 
     if(!resourceType.empty())
     {
-        mapCpy["rt"]=resourceType;
+        mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
     }
 
     if(!resourceInterface.empty())
     {
-        mapCpy["if"]=resourceInterface;
+        mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
     }
 
     return result_guard(post(rep, mapCpy, attributeHandler, QoS));
diff --git a/unittests/README b/unittests/README
new file mode 100644 (file)
index 0000000..28b4bd4
--- /dev/null
@@ -0,0 +1,25 @@
+
+tests.cpp contains unitests for C++ APIs.  It uses Google Test for the unit tests.
+
+Unit Test Requirements:
+
+1. Install Google Test on a Linux build machine:
+       http://code.google.com/p/googletest/downloads/list
+
+2. Create a file called "local.properties" in the
+   root/unittests.  local.properties is used
+   by the makefile and specifies the path to the
+   Google Test directory on the build machine.
+   Since local.properties is specific to each build
+   system, do not add local.properties to the code repo.
+
+   local.properties should contain GTEST_DIR.
+   For example:
+
+   GTEST_DIR := /home/johndoe/utils/gtest-1.7.0
+
+4. To run the unit test, first build the stack.
+
+5. Run the unit test by
+   root/unittests/release/tests
+
diff --git a/unittests/makefile b/unittests/makefile
new file mode 100644 (file)
index 0000000..f0f22ff
--- /dev/null
@@ -0,0 +1,81 @@
+# //******************************************************************
+# //
+# // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
+# //
+# //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+# //
+# // Licensed under the Apache License, Version 2.0 (the "License");
+# // you may not use this file except in compliance with the License.
+# // You may obtain a copy of the License at
+# //
+# //      http://www.apache.org/licenses/LICENSE-2.0
+# //
+# // Unless required by applicable law or agreed to in writing, software
+# // distributed under the License is distributed on an "AS IS" BASIS,
+# // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# // See the License for the specific language governing permissions and
+# // limitations under the License.
+# //
+# //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+#
+# override with `make BUILD=debug`
+# default to release build
+BUILD   := release
+PLATFORM := linux
+CXX     := g++
+
+ROOT_DIR       = $(PWD)
+
+# You must create the file "local.properties" on your local machine which contains any local paths, etc
+# local.properties should NOT be committed to repo
+include $(ROOT_DIR)/local.properties
+# GTEST_DIR contains the path to Google Test libs and must be defined in local.properties
+#  Example:
+#  GTEST_DIR := /home/johndoe/utils/gtest-1.7.0
+
+# NOTE:  to run app, make sure that LD_LIBRARY_PATH env variable
+#        contains $(GTEST_DIR)/lib/.libs
+
+OUT_DIR          := $(PWD)/$(BUILD)
+OBJ_DIR          := $(OUT_DIR)/obj
+
+OUT_DIR          := $(PWD)
+
+INC_DIRS  := -I../include/
+INC_DIRS  += -I../oc_logger/include
+INC_DIRS  += -I../csdk/stack/include
+INC_DIRS  += -I../csdk/ocsocket/include
+INC_DIRS  += -I../csdk/ocrandom/include
+INC_DIRS  += -I../csdk/logger/include
+INC_DIRS  += -I../csdk/libcoap
+INC_DIRS  += -I$(GTEST_DIR)/include
+
+LIB_OC_LOGGER := ../oc_logger/lib/oc_logger.a
+
+CXX_LIBS  := ../$(BUILD)/obj/liboc.a ../csdk/$(PLATFORM)/$(BUILD)/liboctbstack.a $(LIB_OC_LOGGER)
+CXX_LIBS  += $(GTEST_DIR)/lib/.libs/libgtest.a $(GTEST_DIR)/lib/.libs/libgtest_main.a
+
+CC_FLAGS.debug      := -g -O0 -g3 -Wall -ffunction-sections -fdata-sections \
+                        -std=c++0x -pedantic $(INC_DIRS) -L$(ROOT_DIR)/$(BUILD) -DTB_LOG
+CC_FLAGS.release    := -Os -Wall -fdata-sections -Wl,--gc-sections -Wl,-s \
+                        -std=c++0x $(INC_DIRS) -L$(ROOT_DIR)/$(BUILD)
+
+CPPFLAGS       += $(CC_FLAGS.$(BUILD)) $(CXX_LIBS) -lpthread
+
+all: prep_dirs tests
+
+prep_dirs:
+       -mkdir -p $(OUT_DIR)
+       -mkdir -p $(OBJ_DIR)
+
+tests: tests.cpp
+       $(CXX) $^ $(CPPFLAGS) -o $(OUT_DIR)/$(BUILD)/$@
+
+.PHONY: clean
+
+clean: legacy_clean
+       -rm -rf release
+       -rm -rf debug
+
+legacy_clean:
+       rm -f *.o $(PROGRAMS)
diff --git a/unittests/tests.cpp b/unittests/tests.cpp
new file mode 100644 (file)
index 0000000..9600374
--- /dev/null
@@ -0,0 +1,101 @@
+//******************************************************************
+//
+// Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+
+#include <iostream>
+#include <thread>
+#include <functional>
+#include <condition_variable>
+
+#include <OCPlatform.h>
+#include <OCApi.h>
+
+#include <gtest/gtest.h>
+
+namespace PH = std::placeholders;
+
+using namespace OC;
+using namespace std;
+
+// Entity handler used for register and find test
+void entityHandler_rf(std::shared_ptr<OCResourceRequest> request,
+                    std::shared_ptr<OCResourceResponse> response)
+{
+}
+
+// Condition variables used for register and find
+std::mutex mutex_rf;
+std::condition_variable cv_rf;
+std::shared_ptr <OCResource> res_rf;
+
+void foundResource_rf(std::shared_ptr<OCResource> resource)
+{
+    if(resource)
+    {
+        res_rf = resource;
+        cv_rf.notify_all();
+    }
+}
+
+// Resource : Register and find test
+TEST(Resource, rf) {
+    // Create PlatformConfig object
+    PlatformConfig cfg {
+        OC::ServiceType::InProc,
+        OC::ModeType::Both,
+        "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
+        0,         // Uses randomly available port
+        OC::QualityOfService::LowQoS
+    };
+    OCPlatform platform(cfg);
+
+    std::string resourceURI = "/a/res";
+    std::string resourceTypeName = "core.res";
+    std::string resourceInterface = DEFAULT_INTERFACE;
+
+    uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
+
+    OCResourceHandle resourceHandle;
+
+    // This will internally create and register the resource.
+    if(OC_STACK_OK == platform.registerResource(
+                                    resourceHandle, resourceURI, resourceTypeName,
+                                    resourceInterface, entityHandler_rf, resourceProperty))
+    {
+        platform.findResource("","coap://224.0.1.187/oc/core?rt=core.res", foundResource_rf);
+
+        {
+            std::unique_lock<std::mutex> lk(mutex_rf);
+            cv_rf.wait(lk);
+        }
+
+        if(res_rf)
+        {
+            EXPECT_EQ(res_rf->uri(), "/a/res");
+            vector<std::string> rts = res_rf->getResourceTypes();
+            EXPECT_EQ(rts.size(), (unsigned) 1);
+            EXPECT_EQ(rts[0], "core.res");
+            vector<std::string> ifs = res_rf->getResourceInterfaces();
+            EXPECT_EQ(ifs.size(), (unsigned) 1);
+            EXPECT_EQ(ifs[0], DEFAULT_INTERFACE);
+        }
+    }
+}
+