[Iotcon] Fixes for native handlers to json conversion
authorPiotr Kosko <p.kosko@samsung.com>
Wed, 24 Feb 2016 07:13:52 +0000 (08:13 +0100)
committerPiotr Kosko <p.kosko@samsung.com>
Fri, 26 Feb 2016 09:15:05 +0000 (10:15 +0100)
Change-Id: I76ffcbfb9d1aa86a5969e746909fb35301482556
Signed-off-by: Piotr Kosko <p.kosko@samsung.com>
src/iotcon/iotcon_utils.cc

index 6647814c6d152a8c7472ee7531acf2dd452c96c0..0784204dfafa6ffe8ca476bc13ca50dd6ea8e39d 100644 (file)
@@ -222,21 +222,24 @@ TizenResult IotconUtils::ArrayToInterfaces(const picojson::array& i, iotcon_reso
 TizenResult IotconUtils::InterfacesToArray(iotcon_resource_interfaces_h interfaces, picojson::array* arr) {
   ScopeLogger();
 
-  auto result = ConvertIotconError(iotcon_resource_interfaces_foreach(interfaces, [](const char* iface, void* user_data) -> bool {
-    ScopeLogger("iotcon_resource_interfaces_foreach");
+  if (interfaces) {
+    auto result = ConvertIotconError(iotcon_resource_interfaces_foreach(interfaces, [](const char* iface, void* user_data) -> bool {
+      ScopeLogger("iotcon_resource_interfaces_foreach");
 
-    if (iface) {
-      auto arr = static_cast<picojson::array*>(user_data);
-      arr->push_back(picojson::value(iface));
-    }
+      if (iface) {
+        auto arr = static_cast<picojson::array*>(user_data);
+        arr->push_back(picojson::value(iface));
+      }
 
-    // always continue with iteration
-    return true;
-  }, arr));
-  if (!result) {
-    LogAndReturnTizenError(result, ("iotcon_resource_interfaces_foreach() failed"));
+      // always continue with iteration
+      return true;
+    }, arr));
+    if (!result) {
+      LogAndReturnTizenError(result, ("iotcon_resource_interfaces_foreach() failed"));
+    }
+  } else {
+    LoggerW("Interface handle is null, ignoring");
   }
-
   return TizenSuccess();
 }
 
@@ -536,105 +539,108 @@ common::TizenResult IotconUtils::RequestToJson(iotcon_request_h request,
                                                picojson::object* out) {
   ScopeLogger();
 
-  {
-    // hostAddress
-    char* host_address = nullptr;
-    auto result = ConvertIotconError(iotcon_request_get_host_address(request, &host_address));
-    if (!result || !host_address) {
-      LogAndReturnTizenError(result, ("iotcon_request_get_host_address() failed"));
+  if (request) {
+    {
+      // hostAddress
+      char* host_address = nullptr;
+      auto result = ConvertIotconError(iotcon_request_get_host_address(request, &host_address));
+      if (!result || !host_address) {
+        LogAndReturnTizenError(result, ("iotcon_request_get_host_address() failed"));
+      }
+      out->insert(std::make_pair(kHostAddress, picojson::value{host_address}));
     }
-    out->insert(std::make_pair(kHostAddress, picojson::value{host_address}));
-  }
 
-  {
-    // connectivityType
-    iotcon_connectivity_type_e connectivity_type = IOTCON_CONNECTIVITY_ALL;
-    auto result = ConvertIotconError(iotcon_request_get_connectivity_type(request, &connectivity_type));
-    if (!result) {
-      LogAndReturnTizenError(result, ("iotcon_request_get_connectivity_type() failed"));
+    {
+      // connectivityType
+      iotcon_connectivity_type_e connectivity_type = IOTCON_CONNECTIVITY_ALL;
+      auto result = ConvertIotconError(iotcon_request_get_connectivity_type(request, &connectivity_type));
+      if (!result) {
+        LogAndReturnTizenError(result, ("iotcon_request_get_connectivity_type() failed"));
+      }
+      out->insert(std::make_pair(kConnectivityType, picojson::value{FromConnectivityType(connectivity_type)}));
     }
-    out->insert(std::make_pair(kConnectivityType, picojson::value{FromConnectivityType(connectivity_type)}));
-  }
 
-  {
-    // representation
-    iotcon_representation_h representation = nullptr;
-    auto result = ConvertIotconError(iotcon_request_get_representation(request, &representation));
-    if (!result) {
-      LogAndReturnTizenError(result, ("iotcon_request_get_representation() failed"));
-    }
-    if (representation) {
-      picojson::value v{picojson::object{}};
-      result = RepresentationToJson(representation, &v.get<picojson::object>());
+    {
+      // representation
+      iotcon_representation_h representation = nullptr;
+      auto result = ConvertIotconError(iotcon_request_get_representation(request, &representation));
       if (!result) {
-        LogAndReturnTizenError(result, ("RepresentationToJson() failed"));
+        LogAndReturnTizenError(result, ("iotcon_request_get_representation() failed"));
+      }
+      if (representation) {
+        picojson::value v{picojson::object{}};
+        result = RepresentationToJson(representation, &v.get<picojson::object>());
+        if (!result) {
+          LogAndReturnTizenError(result, ("RepresentationToJson() failed"));
+        }
+        out->insert(std::make_pair(kRepresentation, v));
+      } else {
+        LoggerD("Request doesn't have representation.");
       }
-      out->insert(std::make_pair(kRepresentation, v));
-    } else {
-      LoggerD("Request doesn't have representation.");
     }
-  }
 
-  {
-    // requestType
-    iotcon_request_type_e request_type = IOTCON_REQUEST_UNKNOWN;
-    auto result = ConvertIotconError(iotcon_request_get_request_type(request, &request_type));
-    if (!result) {
-      LogAndReturnTizenError(result, ("iotcon_request_get_request_type() failed"));
+    {
+      // requestType
+      iotcon_request_type_e request_type = IOTCON_REQUEST_UNKNOWN;
+      auto result = ConvertIotconError(iotcon_request_get_request_type(request, &request_type));
+      if (!result) {
+        LogAndReturnTizenError(result, ("iotcon_request_get_request_type() failed"));
+      }
+      out->insert(std::make_pair(kRequestType, picojson::value{FromRequestType(request_type)}));
     }
-    out->insert(std::make_pair(kRequestType, picojson::value{FromRequestType(request_type)}));
-  }
 
-  {
-    // options
-    iotcon_options_h options = nullptr;
-    auto result = ConvertIotconError(iotcon_request_get_options(request, &options));
-    if (!result) {
-      LogAndReturnTizenError(result, ("iotcon_request_get_options() failed"));
-    }
-    picojson::value v{picojson::array{}};
-    result = OptionsToJson(options, &v.get<picojson::array>());
-    if (!result) {
-      LogAndReturnTizenError(result, ("OptionsToJson() failed"));
+    {
+      // options
+      iotcon_options_h options = nullptr;
+      auto result = ConvertIotconError(iotcon_request_get_options(request, &options));
+      if (!result) {
+        LogAndReturnTizenError(result, ("iotcon_request_get_options() failed"));
+      }
+      picojson::value v{picojson::array{}};
+      result = OptionsToJson(options, &v.get<picojson::array>());
+      if (!result) {
+        LogAndReturnTizenError(result, ("OptionsToJson() failed"));
+      }
+      out->insert(std::make_pair(kOptions, v));
     }
-    out->insert(std::make_pair(kOptions, v));
-  }
 
-  {
-    // query
-    iotcon_query_h query = nullptr;
-    auto result = ConvertIotconError(iotcon_request_get_query(request, &query));
-    if (!result) {
-      LogAndReturnTizenError(result, ("iotcon_request_get_query() failed"));
-    }
-    picojson::value v{picojson::object{}};
-    result = QueryToJson(query, &v.get<picojson::object>());
-    if (!result) {
-      LogAndReturnTizenError(result, ("QueryToJson() failed"));
+    {
+      // query
+      iotcon_query_h query = nullptr;
+      auto result = ConvertIotconError(iotcon_request_get_query(request, &query));
+      if (!result) {
+        LogAndReturnTizenError(result, ("iotcon_request_get_query() failed"));
+      }
+      picojson::value v{picojson::object{}};
+      result = QueryToJson(query, &v.get<picojson::object>());
+      if (!result) {
+        LogAndReturnTizenError(result, ("QueryToJson() failed"));
+      }
+      out->insert(std::make_pair(kQuery, v));
     }
-    out->insert(std::make_pair(kQuery, v));
-  }
 
-  {
-    // observerId
-    int observer_id = -1;
-    auto result = ConvertIotconError(iotcon_request_get_observe_id(request, &observer_id));
-    if (!result) {
-      LogAndReturnTizenError(result, ("iotcon_request_get_observe_id() failed"));
+    {
+      // observerId
+      int observer_id = -1;
+      auto result = ConvertIotconError(iotcon_request_get_observe_id(request, &observer_id));
+      if (!result) {
+        LogAndReturnTizenError(result, ("iotcon_request_get_observe_id() failed"));
+      }
+      out->insert(std::make_pair(kObserverId, picojson::value{static_cast<double>(observer_id)}));
     }
-    out->insert(std::make_pair(kObserverId, picojson::value{static_cast<double>(observer_id)}));
-  }
 
-  {
-    // observeType
-    iotcon_observe_type_e observe_type = IOTCON_OBSERVE_NO_TYPE;
-    auto result = ConvertIotconError(iotcon_request_get_observe_type(request, &observe_type));
-    if (!result) {
-      LogAndReturnTizenError(result, ("iotcon_request_get_observe_type() failed"));
+    {
+      // observeType
+      iotcon_observe_type_e observe_type = IOTCON_OBSERVE_NO_TYPE;
+      auto result = ConvertIotconError(iotcon_request_get_observe_type(request, &observe_type));
+      if (!result) {
+        LogAndReturnTizenError(result, ("iotcon_request_get_observe_type() failed"));
+      }
+      out->insert(std::make_pair(kRequestType, picojson::value{FromObserveType(observe_type)}));
     }
-    out->insert(std::make_pair(kRequestType, picojson::value{FromObserveType(observe_type)}));
+  } else {
+    LoggerW("Request handle is null, ignoring");
   }
-
   return TizenSuccess();
 }
 
@@ -642,75 +648,79 @@ common::TizenResult IotconUtils::RepresentationToJson(iotcon_representation_h re
                                                       picojson::object* out) {
   ScopeLogger();
 
-  {
-    // hostAddress
-    char* uri_path = nullptr;
-    auto result = ConvertIotconError(iotcon_representation_get_uri_path(representation, &uri_path));
-    if (!result || !uri_path) {
-      LogAndReturnTizenError(result, ("iotcon_representation_get_uri_path() failed"));
+  if (representation) {
+    {
+      // hostAddress
+      char* uri_path = nullptr;
+      auto result = ConvertIotconError(iotcon_representation_get_uri_path(representation, &uri_path));
+      if (!result || !uri_path) {
+        LogAndReturnTizenError(result, ("iotcon_representation_get_uri_path() failed"));
+      }
+      out->insert(std::make_pair(kUriPath, picojson::value{uri_path}));
     }
-    out->insert(std::make_pair(kUriPath, picojson::value{uri_path}));
-  }
 
-  {
-    // resourceTypes
-    iotcon_resource_types_h resource_types = nullptr;
-    auto result = ConvertIotconError(iotcon_representation_get_resource_types(representation, &resource_types));
-    if (!result) {
-      LogAndReturnTizenError(result, ("iotcon_representation_get_resource_types() failed"));
+    {
+      // resourceTypes
+      iotcon_resource_types_h resource_types = nullptr;
+      auto result = ConvertIotconError(iotcon_representation_get_resource_types(representation, &resource_types));
+      if (!result) {
+        LogAndReturnTizenError(result, ("iotcon_representation_get_resource_types() failed"));
+      }
+      picojson::value v{picojson::array{}};
+      iotcon_resource_types_foreach(resource_types, ResourceTypeIterator, &v.get<picojson::array>());
+      out->insert(std::make_pair(kResourceTypes, v));
     }
-    picojson::value v{picojson::array{}};
-    iotcon_resource_types_foreach(resource_types, ResourceTypeIterator, &v.get<picojson::array>());
-    out->insert(std::make_pair(kResourceTypes, v));
-  }
 
-  {
-    // resourceInterfaces
-    iotcon_resource_interfaces_h interfaces = nullptr;
-    auto result = ConvertIotconError(iotcon_representation_get_resource_interfaces(representation, &interfaces));
-    if (!result) {
-      LogAndReturnTizenError(result, ("iotcon_representation_get_resource_interfaces() failed"));
-    }
-    picojson::array js_interfaces;
-    result = InterfacesToArray(interfaces, &js_interfaces);
-    if (!result) {
-      LogAndReturnTizenError(result, ("InterfacesToArray() failed"));
+    {
+      // resourceInterfaces
+      iotcon_resource_interfaces_h interfaces = nullptr;
+      auto result = ConvertIotconError(iotcon_representation_get_resource_interfaces(representation, &interfaces));
+      if (!result) {
+        LogAndReturnTizenError(result, ("iotcon_representation_get_resource_interfaces() failed"));
+      }
+      picojson::array js_interfaces;
+      result = InterfacesToArray(interfaces, &js_interfaces);
+      if (!result) {
+        LogAndReturnTizenError(result, ("InterfacesToArray() failed"));
+      }
+      out->insert(std::make_pair(kResourceInterfaces, picojson::value{js_interfaces}));
     }
-    out->insert(std::make_pair(kResourceInterfaces, picojson::value{js_interfaces}));
-  }
 
-  {
-    // states
-    iotcon_state_h state = nullptr;
-    auto result = ConvertIotconError(iotcon_representation_get_state(representation, &state));
-    if (!result) {
-      LogAndReturnTizenError(result, ("iotcon_representation_get_state() failed"));
-    }
-    picojson::value v{picojson::object{}};
-    result = StateToJson(state, &v.get<picojson::object>());
-    if (!result) {
-      LogAndReturnTizenError(result, ("StateToJson() failed"));
+    {
+      // states
+      iotcon_state_h state = nullptr;
+      auto result = ConvertIotconError(iotcon_representation_get_state(representation, &state));
+      if (!result) {
+        LogAndReturnTizenError(result, ("iotcon_representation_get_state() failed"));
+      }
+      picojson::value v{picojson::object{}};
+      result = StateToJson(state, &v.get<picojson::object>());
+      if (!result) {
+        LogAndReturnTizenError(result, ("StateToJson() failed"));
+      }
+      out->insert(std::make_pair(kStates, v));
     }
-    out->insert(std::make_pair(kStates, v));
-  }
 
-  {
-    // representations
-    picojson::value v{picojson::array{}};
-    auto result = ConvertIotconError(iotcon_representation_foreach_children(representation, [](iotcon_representation_h child, void* user_data) -> bool {
-      auto arr = static_cast<picojson::array*>(user_data);
-      arr->push_back(picojson::value{picojson::object{}});
-      auto result = RepresentationToJson(child, &arr->back().get<picojson::object>());
+    {
+      // representations
+      picojson::value v{picojson::array{}};
+      auto result = ConvertIotconError(iotcon_representation_foreach_children(representation, [](iotcon_representation_h child, void* user_data) -> bool {
+        auto arr = static_cast<picojson::array*>(user_data);
+        arr->push_back(picojson::value{picojson::object{}});
+        auto result = RepresentationToJson(child, &arr->back().get<picojson::object>());
+        if (!result) {
+          LoggerE("Failed to convert child representation");
+        }
+        // always continue with iteration
+        return true;
+      }, &v.get<picojson::array>()));
       if (!result) {
-        LoggerE("Failed to convert child representation");
+        LogAndReturnTizenError(result, ("iotcon_representation_foreach_children() failed"));
       }
-      // always continue with iteration
-      return true;
-    }, &v.get<picojson::array>()));
-    if (!result) {
-      LogAndReturnTizenError(result, ("iotcon_representation_foreach_children() failed"));
+      out->insert(std::make_pair(kRepresentations, v));
     }
-    out->insert(std::make_pair(kRepresentations, v));
+  } else {
+    LoggerW("Representation handle is null, ignoring");
   }
 
   return TizenSuccess();
@@ -720,19 +730,20 @@ common::TizenResult IotconUtils::StateToJson(iotcon_state_h state,
                                              picojson::object* out) {
   ScopeLogger();
 
-  auto result = ConvertIotconError(iotcon_state_foreach(state, [](iotcon_state_h state, const char* key, void* user_data) -> bool {
-    iotcon_type_e type = IOTCON_TYPE_NONE;
-    auto result = ConvertIotconError(iotcon_state_get_type(state, key, &type));
+  if (state) {
+    auto result = ConvertIotconError(iotcon_state_foreach(state, [](iotcon_state_h state, const char* key, void* user_data) -> bool {
+      iotcon_type_e type = IOTCON_TYPE_NONE;
+      auto result = ConvertIotconError(iotcon_state_get_type(state, key, &type));
 
-    if (result) {
-      auto out = static_cast<picojson::object*>(user_data);
+      if (result) {
+        auto out = static_cast<picojson::object*>(user_data);
 
-      switch (type) {
-        case IOTCON_TYPE_NONE:
-          LoggerE("Key %s has type NONE", key);
-          break;
+        switch (type) {
+          case IOTCON_TYPE_NONE:
+            LoggerE("Key %s has type NONE", key);
+            break;
 
-        case IOTCON_TYPE_INT:
+          case IOTCON_TYPE_INT:
           {
             int value = 0;
             result = ConvertIotconError(iotcon_state_get_int(state, key, &value));
@@ -744,7 +755,7 @@ common::TizenResult IotconUtils::StateToJson(iotcon_state_h state,
           }
           break;
 
-        case IOTCON_TYPE_BOOL:
+          case IOTCON_TYPE_BOOL:
           {
             bool value = false;
             result = ConvertIotconError(iotcon_state_get_bool(state, key, &value));
@@ -756,7 +767,7 @@ common::TizenResult IotconUtils::StateToJson(iotcon_state_h state,
           }
           break;
 
-        case IOTCON_TYPE_DOUBLE:
+          case IOTCON_TYPE_DOUBLE:
           {
             double value = 0.0;
             result = ConvertIotconError(iotcon_state_get_double(state, key, &value));
@@ -768,7 +779,7 @@ common::TizenResult IotconUtils::StateToJson(iotcon_state_h state,
           }
           break;
 
-        case IOTCON_TYPE_STR:
+          case IOTCON_TYPE_STR:
           {
             char* value = nullptr;
             result = ConvertIotconError(iotcon_state_get_str(state, key, &value));
@@ -780,7 +791,7 @@ common::TizenResult IotconUtils::StateToJson(iotcon_state_h state,
           }
           break;
 
-        case IOTCON_TYPE_BYTE_STR:
+          case IOTCON_TYPE_BYTE_STR:
           {
             unsigned char* value = nullptr;
             int length = 0;
@@ -796,11 +807,11 @@ common::TizenResult IotconUtils::StateToJson(iotcon_state_h state,
           }
           break;
 
-        case IOTCON_TYPE_NULL:
-          out->insert(std::make_pair(key, picojson::value{}));
-          break;
+          case IOTCON_TYPE_NULL:
+            out->insert(std::make_pair(key, picojson::value{}));
+            break;
 
-        case IOTCON_TYPE_LIST:
+          case IOTCON_TYPE_LIST:
           {
             iotcon_list_h list = nullptr;
             result = ConvertIotconError(iotcon_state_get_list(state, key, &list));
@@ -819,7 +830,7 @@ common::TizenResult IotconUtils::StateToJson(iotcon_state_h state,
           }
           break;
 
-        case IOTCON_TYPE_STATE:
+          case IOTCON_TYPE_STATE:
           {
             iotcon_state_h child = nullptr;
             result = ConvertIotconError(iotcon_state_get_state(state, key, &child));
@@ -837,17 +848,20 @@ common::TizenResult IotconUtils::StateToJson(iotcon_state_h state,
             }
           }
           break;
+        }
+      } else {
+        LoggerE("iotcon_state_get_type() failed");
       }
-    } else {
-      LoggerE("iotcon_state_get_type() failed");
-    }
 
-    // always continue with iteration
-    return true;
-  }, out));
+      // always continue with iteration
+      return true;
+    }, out));
 
-  if (!result) {
-    LogAndReturnTizenError(result, ("iotcon_state_foreach() failed"));
+    if (!result) {
+      LogAndReturnTizenError(result, ("iotcon_state_foreach() failed"));
+    }
+  } else {
+    LoggerW("State handle is null, ignoring");
   }
 
   return TizenSuccess();
@@ -857,124 +871,128 @@ common::TizenResult IotconUtils::StateListToJson(iotcon_list_h list,
                                                  picojson::array* out) {
   ScopeLogger();
 
-  iotcon_type_e type = IOTCON_TYPE_NONE;
-  auto result = ConvertIotconError(iotcon_list_get_type(list, &type));
-
-  if (!result) {
-    LogAndReturnTizenError(result, ("iotcon_list_get_type() failed"));
-  }
+  if (list) {
+    iotcon_type_e type = IOTCON_TYPE_NONE;
+    auto result = ConvertIotconError(iotcon_list_get_type(list, &type));
 
-  switch (type) {
-    case IOTCON_TYPE_NONE:
-      LoggerE("List has type NONE");
-      break;
+    if (!result) {
+      LogAndReturnTizenError(result, ("iotcon_list_get_type() failed"));
+    }
 
-    case IOTCON_TYPE_INT:
-      result = ConvertIotconError(iotcon_list_foreach_int(list, [](int, int value, void* user_data) -> bool {
-        auto out = static_cast<picojson::array*>(user_data);
-        out->push_back(picojson::value{static_cast<double>(value)});
-        // always continue with iteration
-        return true;
-      }, out));
-      if (!result) {
-        LogAndReturnTizenError(result, ("iotcon_list_foreach_int() failed"));
-      }
-      break;
+    switch (type) {
+      case IOTCON_TYPE_NONE:
+        LoggerE("List has type NONE");
+        break;
 
-    case IOTCON_TYPE_BOOL:
-      result = ConvertIotconError(iotcon_list_foreach_bool(list, [](int, bool value, void* user_data) -> bool {
-        auto out = static_cast<picojson::array*>(user_data);
-        out->push_back(picojson::value{value});
-        // always continue with iteration
-        return true;
-      }, out));
-      if (!result) {
-        LogAndReturnTizenError(result, ("iotcon_list_foreach_bool() failed"));
-      }
-      break;
+      case IOTCON_TYPE_INT:
+        result = ConvertIotconError(iotcon_list_foreach_int(list, [](int, int value, void* user_data) -> bool {
+          auto out = static_cast<picojson::array*>(user_data);
+          out->push_back(picojson::value{static_cast<double>(value)});
+          // always continue with iteration
+          return true;
+        }, out));
+        if (!result) {
+          LogAndReturnTizenError(result, ("iotcon_list_foreach_int() failed"));
+        }
+        break;
 
-    case IOTCON_TYPE_DOUBLE:
-      result = ConvertIotconError(iotcon_list_foreach_double(list, [](int, double value, void* user_data) -> bool {
-        auto out = static_cast<picojson::array*>(user_data);
-        out->push_back(picojson::value{value});
-        // always continue with iteration
-        return true;
-      }, out));
-      if (!result) {
-        LogAndReturnTizenError(result, ("iotcon_list_foreach_double() failed"));
-      }
-      break;
+      case IOTCON_TYPE_BOOL:
+        result = ConvertIotconError(iotcon_list_foreach_bool(list, [](int, bool value, void* user_data) -> bool {
+          auto out = static_cast<picojson::array*>(user_data);
+          out->push_back(picojson::value{value});
+          // always continue with iteration
+          return true;
+        }, out));
+        if (!result) {
+          LogAndReturnTizenError(result, ("iotcon_list_foreach_bool() failed"));
+        }
+        break;
 
-    case IOTCON_TYPE_STR:
-      result = ConvertIotconError(iotcon_list_foreach_str(list, [](int, const char* value, void* user_data) -> bool {
-        if (value) {
+      case IOTCON_TYPE_DOUBLE:
+        result = ConvertIotconError(iotcon_list_foreach_double(list, [](int, double value, void* user_data) -> bool {
           auto out = static_cast<picojson::array*>(user_data);
           out->push_back(picojson::value{value});
+          // always continue with iteration
+          return true;
+        }, out));
+        if (!result) {
+          LogAndReturnTizenError(result, ("iotcon_list_foreach_double() failed"));
         }
-        // always continue with iteration
-        return true;
-      }, out));
-      if (!result) {
-        LogAndReturnTizenError(result, ("iotcon_list_foreach_str() failed"));
-      }
-      break;
+        break;
+
+      case IOTCON_TYPE_STR:
+        result = ConvertIotconError(iotcon_list_foreach_str(list, [](int, const char* value, void* user_data) -> bool {
+          if (value) {
+            auto out = static_cast<picojson::array*>(user_data);
+            out->push_back(picojson::value{value});
+          }
+          // always continue with iteration
+          return true;
+        }, out));
+        if (!result) {
+          LogAndReturnTizenError(result, ("iotcon_list_foreach_str() failed"));
+        }
+        break;
 
-    case IOTCON_TYPE_BYTE_STR:
-      result = ConvertIotconError(iotcon_list_foreach_byte_str(list, [](int, const unsigned char* value, int length, void* user_data) -> bool {
-        if (length) {
-          std::unique_ptr<char[]> data{new char[2 * length]};
-          common::tools::BinToHex(value, length, data.get(), 2 * length);
+      case IOTCON_TYPE_BYTE_STR:
+        result = ConvertIotconError(iotcon_list_foreach_byte_str(list, [](int, const unsigned char* value, int length, void* user_data) -> bool {
+          if (length) {
+            std::unique_ptr<char[]> data{new char[2 * length]};
+            common::tools::BinToHex(value, length, data.get(), 2 * length);
 
-          auto out = static_cast<picojson::array*>(user_data);
-          out->push_back(picojson::value{kHexPrefix + data.get()});
+            auto out = static_cast<picojson::array*>(user_data);
+            out->push_back(picojson::value{kHexPrefix + data.get()});
+          }
+          // always continue with iteration
+          return true;
+        }, out));
+        if (!result) {
+          LogAndReturnTizenError(result, ("iotcon_list_foreach_str() failed"));
         }
-        // always continue with iteration
-        return true;
-      }, out));
-      if (!result) {
-        LogAndReturnTizenError(result, ("iotcon_list_foreach_str() failed"));
-      }
-      break;
+        break;
 
-    case IOTCON_TYPE_NULL:
-      LoggerE("List has type NULL");
-      break;
+      case IOTCON_TYPE_NULL:
+        LoggerE("List has type NULL");
+        break;
 
-    case IOTCON_TYPE_LIST:
-      result = ConvertIotconError(iotcon_list_foreach_list(list, [](int, iotcon_list_h list, void* user_data) -> bool {
-        picojson::value value{picojson::array{}};
-        auto result = StateListToJson(list, &value.get<picojson::array>());
-        if (result) {
-          auto out = static_cast<picojson::array*>(user_data);
-          out->push_back(picojson::value{value});
-        } else {
-          LoggerE("StateListToJson() failed");
+      case IOTCON_TYPE_LIST:
+        result = ConvertIotconError(iotcon_list_foreach_list(list, [](int, iotcon_list_h list, void* user_data) -> bool {
+          picojson::value value{picojson::array{}};
+          auto result = StateListToJson(list, &value.get<picojson::array>());
+          if (result) {
+            auto out = static_cast<picojson::array*>(user_data);
+            out->push_back(picojson::value{value});
+          } else {
+            LoggerE("StateListToJson() failed");
+          }
+          // always continue with iteration
+          return true;
+        }, out));
+        if (!result) {
+          LogAndReturnTizenError(result, ("iotcon_list_foreach_list() failed"));
         }
-        // always continue with iteration
-        return true;
-      }, out));
-      if (!result) {
-        LogAndReturnTizenError(result, ("iotcon_list_foreach_list() failed"));
-      }
-      break;
+        break;
 
-    case IOTCON_TYPE_STATE:
-      result = ConvertIotconError(iotcon_list_foreach_state(list, [](int, iotcon_state_h state, void* user_data) -> bool {
-        picojson::value value{picojson::object{}};
-        auto result = StateToJson(state, &value.get<picojson::object>());
-        if (result) {
-          auto out = static_cast<picojson::array*>(user_data);
-          out->push_back(picojson::value{value});
-        } else {
-          LoggerE("StateToJson() failed");
+      case IOTCON_TYPE_STATE:
+        result = ConvertIotconError(iotcon_list_foreach_state(list, [](int, iotcon_state_h state, void* user_data) -> bool {
+          picojson::value value{picojson::object{}};
+          auto result = StateToJson(state, &value.get<picojson::object>());
+          if (result) {
+            auto out = static_cast<picojson::array*>(user_data);
+            out->push_back(picojson::value{value});
+          } else {
+            LoggerE("StateToJson() failed");
+          }
+          // always continue with iteration
+          return true;
+        }, out));
+        if (!result) {
+          LogAndReturnTizenError(result, ("iotcon_list_foreach_state() failed"));
         }
-        // always continue with iteration
-        return true;
-      }, out));
-      if (!result) {
-        LogAndReturnTizenError(result, ("iotcon_list_foreach_state() failed"));
-      }
-      break;
+        break;
+    }
+  } else {
+    LoggerW("List handle is null, ignoring");
   }
 
   return TizenSuccess();
@@ -984,23 +1002,27 @@ common::TizenResult IotconUtils::OptionsToJson(iotcon_options_h  options,
                                                picojson::array* out) {
   ScopeLogger();
 
-  auto result = ConvertIotconError(iotcon_options_foreach(options, [](unsigned short id, const char *data, void* user_data) -> bool {
-    if (data) {
-      picojson::value v{picojson::object{}};
-      auto& obj = v.get<picojson::object>();
+  if (options) {
+    auto result = ConvertIotconError(iotcon_options_foreach(options, [](unsigned short id, const char *data, void* user_data) -> bool {
+      if (data) {
+        picojson::value v{picojson::object{}};
+        auto& obj = v.get<picojson::object>();
 
-      obj.insert(std::make_pair(kOptionsId, picojson::value{static_cast<double>(id)}));
-      obj.insert(std::make_pair(kOptionsData, picojson::value{data}));
+        obj.insert(std::make_pair(kOptionsId, picojson::value{static_cast<double>(id)}));
+        obj.insert(std::make_pair(kOptionsData, picojson::value{data}));
 
-      auto out = static_cast<picojson::array*>(user_data);
-      out->push_back(v);
-    }
-    // always continue with iteration
-    return true;
-  }, out));
+        auto out = static_cast<picojson::array*>(user_data);
+        out->push_back(v);
+      }
+      // always continue with iteration
+      return true;
+    }, out));
 
-  if (!result) {
-    LogAndReturnTizenError(result, ("iotcon_options_foreach() failed"));
+    if (!result) {
+      LogAndReturnTizenError(result, ("iotcon_options_foreach() failed"));
+    }
+  } else {
+    LoggerW("Options handle is null, ignoring");
   }
 
   return TizenSuccess();
@@ -1010,43 +1032,46 @@ common::TizenResult IotconUtils::QueryToJson(iotcon_query_h query,
                                              picojson::object* out) {
   ScopeLogger();
 
-  {
-    // resourceType
-    char* resource_type = nullptr;
-    auto result = ConvertIotconError(iotcon_query_get_resource_type(query, &resource_type));
-    if (!result || !resource_type) {
-      LogAndReturnTizenError(result, ("iotcon_query_get_resource_type() failed"));
+  if (query) {
+    {
+      // resourceType
+      char* resource_type = nullptr;
+      auto result = ConvertIotconError(iotcon_query_get_resource_type(query, &resource_type));
+      if (!result || !resource_type) {
+        LogAndReturnTizenError(result, ("iotcon_query_get_resource_type() failed"));
+      }
+      out->insert(std::make_pair(kResourceType, picojson::value{resource_type}));
     }
-    out->insert(std::make_pair(kResourceType, picojson::value{resource_type}));
-  }
 
-  {
-    // resourceInterface
-    char* interface = nullptr;
-    auto result = ConvertIotconError(iotcon_query_get_interface(query, &interface));
-    if (!result || !interface) {
-      LogAndReturnTizenError(result, ("iotcon_query_get_interface() failed"));
+    {
+      // resourceInterface
+      char* interface = nullptr;
+      auto result = ConvertIotconError(iotcon_query_get_interface(query, &interface));
+      if (!result || !interface) {
+        LogAndReturnTizenError(result, ("iotcon_query_get_interface() failed"));
+      }
+      out->insert(std::make_pair(kResourceInterface, picojson::value{interface}));
     }
-    out->insert(std::make_pair(kResourceInterface, picojson::value{interface}));
-  }
 
-  {
-    // filter
-    picojson::value v{picojson::object{}};
-    auto result = ConvertIotconError(iotcon_query_foreach(query, [](const char* key, const char* value, void* user_data) -> bool {
-      if (key && value) {
-        auto obj = static_cast<picojson::object*>(user_data);
-        obj->insert(std::make_pair(key, picojson::value{value}));
+    {
+      // filter
+      picojson::value v{picojson::object{}};
+      auto result = ConvertIotconError(iotcon_query_foreach(query, [](const char* key, const char* value, void* user_data) -> bool {
+        if (key && value) {
+          auto obj = static_cast<picojson::object*>(user_data);
+          obj->insert(std::make_pair(key, picojson::value{value}));
+        }
+        // always continue with iteration
+        return true;
+      }, &v.get<picojson::object>()));
+      if (!result) {
+        LogAndReturnTizenError(result, ("iotcon_query_foreach() failed"));
       }
-      // always continue with iteration
-      return true;
-    }, &v.get<picojson::object>()));
-    if (!result) {
-      LogAndReturnTizenError(result, ("iotcon_query_foreach() failed"));
+      out->insert(std::make_pair(kFilter, v));
     }
-    out->insert(std::make_pair(kFilter, v));
+  } else {
+    LoggerW("Query handle is null, ignoring");
   }
-
   return TizenSuccess();
 }
 
@@ -1114,49 +1139,54 @@ common::TizenResult IotconUtils::ResponseToJson(iotcon_response_h handle,
                                                 picojson::object* res) {
   ScopeLogger();
 
-  {
-    // ResponseResult result
-    iotcon_response_result_e response = IOTCON_RESPONSE_ERROR;
-    auto result = ConvertIotconError(iotcon_response_get_result(handle, &response));
-    if (!result) {
-      LogAndReturnTizenError(result, ("iotcon_response_get_result() failed"));
+  if (handle) {
+    {
+      // ResponseResult result
+      iotcon_response_result_e response = IOTCON_RESPONSE_ERROR;
+      auto result = ConvertIotconError(iotcon_response_get_result(handle, &response));
+      if (!result) {
+        LogAndReturnTizenError(result, ("iotcon_response_get_result() failed"));
+      }
+      std::string result_str = FromResponseResultType(response);
+      res->insert(std::make_pair(kResultType, picojson::value{result_str}));
     }
-    std::string result_str = FromResponseResultType(response);
-    res->insert(std::make_pair(kResultType, picojson::value{result_str}));
-  }
 
-  {
-    // Representation representation
-    iotcon_representation_h repr = nullptr;
-    auto result = ConvertIotconError(iotcon_response_get_representation(handle, &repr));
-    if (!result) {
-      LogAndReturnTizenError(result, ("iotcon_response_get_representation() failed"));
-    }
-    picojson::value repr_json{picojson::object{}};
-    result = RepresentationToJson(repr, &repr_json.get<picojson::object>());
-    if (!result) {
-      LogAndReturnTizenError(result, ("RepresentationToJson() failed"));
+    {
+      // Representation representation
+      iotcon_representation_h repr = nullptr;
+      auto result = ConvertIotconError(iotcon_response_get_representation(handle, &repr));
+      if (!result) {
+        LogAndReturnTizenError(result, ("iotcon_response_get_representation() failed"));
+      }
+      picojson::value repr_json{picojson::object{}};
+      result = RepresentationToJson(repr, &repr_json.get<picojson::object>());
+      if (!result) {
+        LogAndReturnTizenError(result, ("RepresentationToJson() failed"));
+      }
+      res->insert(std::make_pair(kRepresentation, repr_json));
     }
-    res->insert(std::make_pair(kRepresentation, repr_json));
-  }
 
-  {
-    // IotconOption[]? options
-    iotcon_options_h options = nullptr;
-    auto result = ConvertIotconError(iotcon_response_get_options(handle, &options));
-    if (!result) {
-      LogAndReturnTizenError(result, ("iotcon_response_get_options() failed"));
-    }
-    if (options) {
-      picojson::value opt_json{picojson::array{}};
-      result = OptionsToJson(options, &opt_json.get<picojson::array>());
-      if (!result) {
-        LogAndReturnTizenError(result, ("OptionsToJson() failed"));
+    {
+      // IotconOption[]? options
+      iotcon_options_h options = nullptr;
+      int native_ret = iotcon_response_get_options(handle, &options);
+      auto result = ConvertIotconError(native_ret);
+      if (!result && IOTCON_ERROR_NO_DATA != native_ret) {
+        LogAndReturnTizenError(result, ("iotcon_response_get_options() failed"));
+      }
+      if (options) {
+        picojson::value opt_json{picojson::array{}};
+        result = OptionsToJson(options, &opt_json.get<picojson::array>());
+        if (!result) {
+          LogAndReturnTizenError(result, ("OptionsToJson() failed"));
+        }
+        res->insert(std::make_pair(kOptions, opt_json));
+      } else {
+        res->insert(std::make_pair(kOptions, picojson::value{}));
       }
-      res->insert(std::make_pair(kOptions, opt_json));
-    } else {
-      res->insert(std::make_pair(kOptions, picojson::value{}));
     }
+  } else {
+    LoggerW("Response handle is null, ignoring");
   }
 
   return TizenSuccess();
@@ -1490,72 +1520,75 @@ common::TizenResult IotconUtils::PresenceResponseToJson(
     iotcon_presence_response_h presence, picojson::object* out) {
   ScopeLogger();
 
-  {
-    // hostAddress
-    char* host = nullptr;
-    auto result = ConvertIotconError(iotcon_presence_response_get_host_address(presence,
-                                                                       &host));
-    if (!result || !host) {
-      LogAndReturnTizenError(result, ("iotcon_presence_response_get_host_address() failed"));
+  if (presence) {
+    {
+      // hostAddress
+      char* host = nullptr;
+      auto result = ConvertIotconError(iotcon_presence_response_get_host_address(presence,
+                                                                                 &host));
+      if (!result || !host) {
+        LogAndReturnTizenError(result, ("iotcon_presence_response_get_host_address() failed"));
+      }
+      out->insert(std::make_pair(kHostAddress, picojson::value{std::string(host)}));
     }
-    out->insert(std::make_pair(kHostAddress, picojson::value{std::string(host)}));
-  }
 
-  {
-    // connectivityType
-    iotcon_connectivity_type_e con_type = IOTCON_CONNECTIVITY_IPV4;
-    auto result = ConvertIotconError(iotcon_presence_response_get_connectivity_type(presence,
-                                                                       &con_type));
-    if (!result) {
-      LogAndReturnTizenError(result, ("iotcon_presence_response_get_connectivity_type() failed"));
+    {
+      // connectivityType
+      iotcon_connectivity_type_e con_type = IOTCON_CONNECTIVITY_IPV4;
+      auto result = ConvertIotconError(iotcon_presence_response_get_connectivity_type(presence,
+                                                                                      &con_type));
+      if (!result) {
+        LogAndReturnTizenError(result, ("iotcon_presence_response_get_connectivity_type() failed"));
+      }
+      out->insert(std::make_pair(kConnectivityType, picojson::value{
+        FromConnectivityType(con_type)}));
+    }
+
+    {
+      // resourceType
+      char* resource_type = nullptr;
+      auto result = ConvertIotconError(iotcon_presence_response_get_resource_type(presence,
+                                                                                  &resource_type));
+      if (!result || !resource_type) {
+        LoggerE("iotcon_presence_response_get_resource_type() failed");
+        out->insert(std::make_pair(kResourceType, picojson::value()));
+      } else {
+        out->insert(std::make_pair(kResourceType, picojson::value{std::string(resource_type)}));
+      }
     }
-    out->insert(std::make_pair(kConnectivityType, picojson::value{
-      FromConnectivityType(con_type)}));
-  }
 
-  {
-    // resourceType
-    char* resource_type = nullptr;
-    auto result = ConvertIotconError(iotcon_presence_response_get_resource_type(presence,
-                                                                       &resource_type));
-    if (!result || !resource_type) {
-      LoggerE("iotcon_presence_response_get_resource_type() failed");
-      out->insert(std::make_pair(kResourceType, picojson::value()));
-    } else {
-      out->insert(std::make_pair(kResourceType, picojson::value{std::string(resource_type)}));
-    }
-  }
+    // resultType
+    iotcon_presence_result_e result_type = IOTCON_PRESENCE_OK;
+    {
+      auto result = ConvertIotconError(iotcon_presence_response_get_result(presence,
+                                                                           &result_type));
+      if (!result) {
+        LogAndReturnTizenError(result, ("iotcon_presence_response_get_result() failed"));
+      }
 
-  // resultType
-  iotcon_presence_result_e result_type = IOTCON_PRESENCE_OK;
-  {
-    auto result = ConvertIotconError(iotcon_presence_response_get_result(presence,
-                                                                       &result_type));
-    if (!result) {
-      LogAndReturnTizenError(result, ("iotcon_presence_response_get_result() failed"));
+      out->insert(std::make_pair(kResultType, picojson::value{
+        FromPresenceResponseResultType(result_type)}));
     }
 
-    out->insert(std::make_pair(kResultType, picojson::value{
-      FromPresenceResponseResultType(result_type)}));
-  }
-
-  {
-    // triggerType
-    iotcon_presence_trigger_e trigger_type = IOTCON_PRESENCE_RESOURCE_CREATED;
-    if (IOTCON_PRESENCE_OK == result_type) {
-      auto result = ConvertIotconError(iotcon_presence_response_get_trigger(presence,
-                                                                         &trigger_type));
-      if (!result) {
-        LoggerE("iotcon_presence_response_get_trigger() failed");
-        out->insert(std::make_pair(kTriggerType, picojson::value()));
+    {
+      // triggerType
+      iotcon_presence_trigger_e trigger_type = IOTCON_PRESENCE_RESOURCE_CREATED;
+      if (IOTCON_PRESENCE_OK == result_type) {
+        auto result = ConvertIotconError(iotcon_presence_response_get_trigger(presence,
+                                                                              &trigger_type));
+        if (!result) {
+          LoggerE("iotcon_presence_response_get_trigger() failed");
+          out->insert(std::make_pair(kTriggerType, picojson::value()));
+        } else {
+          out->insert(std::make_pair(kTriggerType, picojson::value{FromPresenceTriggerType(
+              trigger_type)}));
+        }
       } else {
-        out->insert(std::make_pair(kTriggerType, picojson::value{FromPresenceTriggerType(
-            trigger_type)}));
+        out->insert(std::make_pair(kTriggerType, picojson::value()));
       }
-    } else {
-      out->insert(std::make_pair(kTriggerType, picojson::value()));
     }
-
+  } else {
+    LoggerW("Presence handle is null, ignoring");
   }
 
   return TizenSuccess();
@@ -1680,7 +1713,6 @@ common::TizenResult IotconUtils::RepresentationFromJson(const picojson::object&
       SCOPE_EXIT {
         iotcon_resource_interfaces_destroy(interfaces);
       };
-
       result = ConvertIotconError(iotcon_representation_set_resource_interfaces(representation, interfaces));
       if (!result) {
         LogAndReturnTizenError(result, ("iotcon_representation_set_resource_interfaces() failed"));