[NFC] Remove try/catch to adjust to google coding style - part 3
authorLukasz Bardeli <l.bardeli@samsung.com>
Wed, 4 Mar 2015 11:05:33 +0000 (12:05 +0100)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Wed, 4 Mar 2015 12:23:43 +0000 (21:23 +0900)
[Verification] Code compiles without error.

Change-Id: I8cd2134cfe0b1ce87bc2c3f5e21a38e2a0230e65
Signed-off-by: Lukasz Bardeli <l.bardeli@samsung.com>
src/nfc/nfc_adapter.cc
src/nfc/nfc_adapter.h
src/nfc/nfc_instance.cc
src/nfc/nfc_util.cc
src/nfc/nfc_util.h

index f1c6f3893352e7062ad9238d974035e0c4e1a4b9..09989f17058919eaed2a41e79538acd363542a65 100644 (file)
@@ -725,50 +725,48 @@ bool NFCAdapter::IsNDEFListenerSet() {
 
 
 // NFCTag related functions
-std::string NFCAdapter::TagTypeGetter(int tag_id) {
+PlatformResult NFCAdapter::TagTypeGetter(int tag_id, std::string* type) {
 
   LoggerD("Entered");
 
-  nfc_tag_type_e type = NFC_UNKNOWN_TARGET;
+  nfc_tag_type_e nfc_type = NFC_UNKNOWN_TARGET;
 
-  int err = nfc_tag_get_type(m_last_tag_handle, &type);
+  int err = nfc_tag_get_type(m_last_tag_handle, &nfc_type);
   if(NFC_ERROR_NONE != err) {
     LoggerE("Failed to get tag type: %d", err);
-    NFCUtil::throwNFCException(err, "Failed to get tag type");
+    return NFCUtil::CodeToResult(err, "Failed to get tag type");
   }
 
-  return NFCUtil::toStringNFCTag(type);
+  *type = NFCUtil::ToStringNFCTag(nfc_type);
+
+  return PlatformResult(ErrorCode::NO_ERROR);
 }
 
-bool NFCAdapter::TagIsSupportedNDEFGetter(int tag_id) {
+PlatformResult NFCAdapter::TagIsSupportedNDEFGetter(int tag_id, bool *is_supported) {
 
   LoggerD("Entered");
 
-  bool result = false;
-
-  int err = nfc_tag_is_support_ndef(m_last_tag_handle, &result);
+  int err = nfc_tag_is_support_ndef(m_last_tag_handle, is_supported);
   if(NFC_ERROR_NONE != err) {
     LoggerE("Failed to check if NDEF is supported %d", err);
-    NFCUtil::throwNFCException(err,
+    return NFCUtil::CodeToResult(err,
                                "Failed to check if NDEF is supported");
   }
 
-  return result;
+  return PlatformResult(ErrorCode::NO_ERROR);
 }
 
-unsigned int NFCAdapter::TagNDEFSizeGetter(int tag_id) {
+PlatformResult NFCAdapter::TagNDEFSizeGetter(int tag_id, unsigned int *size) {
 
   LoggerD("Entered");
 
-  unsigned int result = 0;
-
-  int err = nfc_tag_get_ndef_size(m_last_tag_handle, &result);
+  int err = nfc_tag_get_ndef_size(m_last_tag_handle, size);
   if(NFC_ERROR_NONE != err) {
     LoggerE("Failed to get tag NDEF size: %d, %s", err);
-    NFCUtil::throwNFCException(err,
+    return NFCUtil::CodeToResult(err,
                                "Failed to get tag NDEF size");
   }
-  return result;
+  return PlatformResult(ErrorCode::NO_ERROR);
 }
 
 static bool tagPropertiesGetterCb(const char *key,
@@ -785,27 +783,59 @@ static bool tagPropertiesGetterCb(const char *key,
   return false;
 }
 
-NFCTagPropertiesT NFCAdapter::TagPropertiesGetter(int tag_id) {
+PlatformResult NFCAdapter::TagPropertiesGetter(int tag_id, NFCTagPropertiesT *properties) {
 
   LoggerD("Entered");
 
-  NFCTagPropertiesT result;
-
   int err = nfc_tag_foreach_information(m_last_tag_handle,
-                                        tagPropertiesGetterCb, (void*)&result);
+                                        tagPropertiesGetterCb, (void*)properties);
   if(NFC_ERROR_NONE != err) {
     LoggerE("Error occured while getting NFC properties: %d", err);
-    NFCUtil::throwNFCException(err,
+    return NFCUtil::CodeToResult(err,
                                "Error occured while getting NFC properties");
   }
 
+  return PlatformResult(ErrorCode::NO_ERROR);
+}
+
+PlatformResult NFCAdapter::TagIsConnectedGetter(int tag_id, bool *state) {
+
+  LoggerD("Entered");
+
+  PlatformResult result = PlatformResult(ErrorCode::NO_ERROR);
+
+  if(tag_id != m_latest_tag_id || NULL == m_last_tag_handle) {
+    // internaly stored tag id changed -> new tag has been already connected
+    // internaly stored tag handle NULL -> tag has been disconnected
+    LoggerD("NFCTag () not connected (id differs or invalid handle)");
+    *state = false;
+    return result;
+  }
+
+  nfc_tag_h handle = NULL;
+  int ret = nfc_manager_get_connected_tag(&handle);
+  if(NFC_ERROR_NONE != ret) {
+    LoggerE("Failed to get connected tag: %s",
+            NFCUtil::getNFCErrorMessage(ret).c_str());
+    // exception is thrown here to return undefined in JS layer
+    // instead of false
+    return NFCUtil::CodeToResult(ret, "Failed to get connected tag");
+  }
+
+  if(m_last_tag_handle != handle) {
+    LoggerD("Last known handle and current handle differs");
+    *state = false;
+  } else {
+    *state = true;
+  }
+
   return result;
 }
 
+// TODO remove after clean code from try/catch
 bool NFCAdapter::TagIsConnectedGetter(int tag_id) {
 
   LoggerD("Entered");
-
   if(tag_id != m_latest_tag_id || NULL == m_last_tag_handle) {
     // internaly stored tag id changed -> new tag has been already connected
     // internaly stored tag handle NULL -> tag has been disconnected
@@ -831,7 +861,6 @@ bool NFCAdapter::TagIsConnectedGetter(int tag_id) {
   return true;
 }
 
-
 int NFCAdapter::GetNextTagId() {
 
   LoggerD("Entered");
@@ -864,7 +893,7 @@ static void tagEventCallback(nfc_discovered_type_e type, nfc_tag_h tag, void *da
 
     obj.insert(make_pair("action", "onattach"));
     obj.insert(make_pair("id", static_cast<double>(generated_id)));
-    obj.insert(make_pair("type", NFCUtil::toStringNFCTag(tag_type)));
+    obj.insert(make_pair("type", NFCUtil::ToStringNFCTag(tag_type)));
 
     NFCInstance::getInstance().PostMessage(event.serialize().c_str());
   }
@@ -964,44 +993,52 @@ static void tagReadNDEFCb(nfc_error_e result , nfc_ndef_message_h message , void
   free(raw_data);
 }
 
-void NFCAdapter::TagReadNDEF(int tag_id, const picojson::value& args) {
+PlatformResult NFCAdapter::TagReadNDEF(int tag_id, const picojson::value& args) {
 
   LoggerD("Entered");
+
+  bool is_connected = false;
+  PlatformResult result = TagIsConnectedGetter(tag_id, &is_connected);
+  if (result.IsError()) {
+    return result;
+  }
+
   double callbackId = args.get(CALLBACK_ID).get<double>();
   LoggerD("Received callback id: %f", callbackId);
 
-  if(!TagIsConnectedGetter(tag_id)) {
-    UnknownException ex("Tag is no more connected");
-
-    picojson::value event = createEventError(callbackId, ex);
+  if(!is_connected) {
+    picojson::value event = CreateEventError(callbackId,
+                                             PlatformResult(ErrorCode::UNKNOWN_ERR,
+                                                            "Tag is no more connected."));
     NFCInstance::getInstance().PostMessage(event.serialize().c_str());
-    return;
+    return PlatformResult(ErrorCode::NO_ERROR);;
   }
 
   double* callbackIdPointer = new double(callbackId);
-  int result = nfc_tag_read_ndef(m_last_tag_handle, tagReadNDEFCb,
+  int ret = nfc_tag_read_ndef(m_last_tag_handle, tagReadNDEFCb,
                                  (void*)(callbackIdPointer));
-  if(NFC_ERROR_NONE != result) {
-    LoggerE("Failed to read NDEF message from tag: %d", result);
+  if(NFC_ERROR_NONE != ret) {
+    LoggerE("Failed to read NDEF message from tag: %d", ret);
     delete callbackIdPointer;
     callbackIdPointer = NULL;
 
     // for permission related error throw exception ...
-    if(NFC_ERROR_SECURITY_RESTRICTED == result ||
-        NFC_ERROR_PERMISSION_DENIED == result) {
-      throw SecurityException("Failed to read NDEF - permission denied");
+    if(NFC_ERROR_SECURITY_RESTRICTED == ret ||
+        NFC_ERROR_PERMISSION_DENIED == ret) {
+      return PlatformResult(ErrorCode::SECURITY_ERR, "Failed to read NDEF - permission denied");
     }
 
     LoggerE("Preparing error callback to call");
     // ... otherwise call error callback
-    std::string errName = NFCUtil::getNFCErrorString(result);
-    std::string errMessage = NFCUtil::getNFCErrorMessage(result);
-    PlatformException ex(errName, errMessage);
+    std::string errMessage = NFCUtil::getNFCErrorMessage(ret);
 
-    picojson::value event = createEventError(callbackId, ex);
+    result = NFCUtil::CodeToResult(ret, errMessage.c_str());
+
+    picojson::value event = CreateEventError(callbackId, result);
     NFCInstance::getInstance().PostMessage(event.serialize().c_str());
   }
 
+  return PlatformResult(ErrorCode::NO_ERROR);
 }
 
 void NFCAdapter::TagWriteNDEF(int tag_id, const picojson::value& args) {
index 5c2fb117dce07b4d2a848ff36769ae3c476f15fe..a27ffc32d4747bc2671605fbade2fde5bb64d350 100644 (file)
@@ -62,13 +62,15 @@ class NFCAdapter {
 
   // NFCTag related methods
   // attributes
-  std::string TagTypeGetter(int tag_id);
-  bool TagIsSupportedNDEFGetter(int tag_id);
-  unsigned int TagNDEFSizeGetter(int tag_id);
-  NFCTagPropertiesT TagPropertiesGetter(int tag_id);
+  common::PlatformResult TagTypeGetter(int tag_id, std::string *type);
+  common::PlatformResult TagIsSupportedNDEFGetter(int tag_id, bool *is_supported);
+  common::PlatformResult TagNDEFSizeGetter(int tag_id, unsigned int *size);
+  common::PlatformResult TagPropertiesGetter(int tag_id, NFCTagPropertiesT *properties);
+  common::PlatformResult TagIsConnectedGetter(int tag_id, bool *state);
+  // TODO remove after clean code from try/catch
   bool TagIsConnectedGetter(int tag_id);
   // methods
-  void TagReadNDEF(int tag_id, const picojson::value& args);
+  common::PlatformResult TagReadNDEF(int tag_id, const picojson::value& args);
   void TagWriteNDEF(int tag_id, const picojson::value& args);
   void TagTransceive(int tag_id, const picojson::value& args);
   // listeners
index f42591445b42b79e46e46299b6e5f90c473e51d5..e4a9ed7532f572cf3b6db081ce48857d0eccf40a 100644 (file)
@@ -353,15 +353,16 @@ void NFCInstance::SetExclusiveModeForTransaction(
 void NFCInstance::ReadNDEF(
     const picojson::value& args, picojson::object& out) {
 
+  CHECK_EXIST(args, "id", out);
+
   int tag_id = static_cast<int>(args.get("id").get<double>());
   LoggerD("Tag id: %d", tag_id);
 
-  try {
-    NFCAdapter::GetInstance()->TagReadNDEF(tag_id, args);
+  PlatformResult result =NFCAdapter::GetInstance()->TagReadNDEF(tag_id, args);
+  if (result.IsSuccess()) {
     ReportSuccess(out);
-  }
-  catch(const common::PlatformException& ex) {
-    ReportError(ex, out);
+  } else {
+    ReportError(result, &out);
   }
 }
 
@@ -527,27 +528,34 @@ void NFCInstance::TagTypeGetter(
 
   LoggerD("Entered");
 
+  CHECK_EXIST(args, "id", out);
   int tag_id = (int)args.get("id").get<double>();
   LoggerD("Tag id: %d", tag_id);
 
-  try {
-    // Function below throws exception if core API call fails
-    if (!NFCAdapter::GetInstance()->TagIsConnectedGetter(tag_id)) {
-      LoggerE("Tag with id %d is not connected anymore", tag_id);
-      // If tag is not connected then attribute's value
-      // should be undefined
-      ReportError(out);
-      return;
-    }
+  // Function below throws exception if core API call fails
+  bool is_connected = false;
+  PlatformResult result = NFCAdapter::GetInstance()->TagIsConnectedGetter(tag_id, &is_connected);
 
-    std::string tag_type =
-        NFCAdapter::GetInstance()->TagTypeGetter(tag_id);
+  if (result.IsError()) {
+    ReportError(result, &out);
+    return;
+  }
 
-    ReportSuccess(picojson::value(tag_type), out);
+  if (!is_connected) {
+    LoggerE("Tag with id %d is not connected anymore", tag_id);
+    // If tag is not connected then attribute's value
+    // should be undefined
+    ReportError(out);
+    return;
   }
-  catch(const PlatformException& ex) {
-    LoggerE("Failed to check tag connection");
-    ReportError(ex, out);
+
+  std::string tag_type = "";
+  result = NFCAdapter::GetInstance()->TagTypeGetter(tag_id, &tag_type);
+
+  if (result.IsSuccess()) {
+    ReportSuccess(picojson::value(tag_type), out);
+  } else {
+    ReportError(result, &out);
   }
 }
 
@@ -559,26 +567,30 @@ void NFCInstance::TagIsSupportedNDEFGetter(
   int tag_id = (int)args.get("id").get<double>();
   LoggerD("Tag id: %d", tag_id);
 
-  try {
-    // Function below throws exception if core API call fails
-    if (!NFCAdapter::GetInstance()->TagIsConnectedGetter(tag_id)) {
-      LoggerE("Tag with id %d is not connected anymore", tag_id);
-      // If tag is not connected then attribute's value
-      // should be undefined
-      ReportError(out);
-      return;
-    }
-
-    bool is_supported =
-        NFCAdapter::GetInstance()->TagIsSupportedNDEFGetter(tag_id);
+  // Function below throws exception if core API call fails
+  bool is_connected = false;
+  PlatformResult result = NFCAdapter::GetInstance()->TagIsConnectedGetter(tag_id, &is_connected);
 
-    ReportSuccess(picojson::value(is_supported), out);
+  if (result.IsError()) {
+    ReportError(result, &out);
+    return;
   }
-  catch(const PlatformException& ex) {
-    LoggerE("Failed to check is NDEF supported");
-    ReportError(ex, out);
+
+  if (!is_connected) {
+    LoggerE("Tag with id %d is not connected anymore", tag_id);
+    // If tag is not connected then attribute's value
+    // should be undefined
+    ReportError(out);
+    return;
   }
 
+  bool is_supported = false;
+  result = NFCAdapter::GetInstance()->TagIsSupportedNDEFGetter(tag_id, &is_supported);
+  if (result.IsSuccess()) {
+    ReportSuccess(picojson::value(is_supported), out);
+  } else {
+    ReportError(result, &out);
+  }
 }
 
 void NFCInstance::TagNDEFSizeGetter(
@@ -589,26 +601,31 @@ void NFCInstance::TagNDEFSizeGetter(
   int tag_id = (int)args.get("id").get<double>();
   LoggerD("Tag id: %d", tag_id);
 
-  try {
-    // Function below throws exception if core API call fails
-    if (!NFCAdapter::GetInstance()->TagIsConnectedGetter(tag_id)) {
-      LoggerE("Tag with id %d is not connected anymore", tag_id);
-      // If tag is not connected then attribute's value
-      // should be undefined
-      ReportError(out);
-      return;
-    }
-
-    unsigned int ndef_size =
-        NFCAdapter::GetInstance()->TagNDEFSizeGetter(tag_id);
+  // Function below throws exception if core API call fails
+  bool is_connected = false;
+  PlatformResult result = NFCAdapter::GetInstance()->TagIsConnectedGetter(tag_id, &is_connected);
 
-    ReportSuccess(picojson::value((double)ndef_size), out);
+  if (result.IsError()) {
+    ReportError(result, &out);
+    return;
   }
-  catch(const PlatformException& ex) {
-    LoggerE("Failed to get tag NDEF size");
-    ReportError(ex, out);
+
+  if (!is_connected) {
+    LoggerE("Tag with id %d is not connected anymore", tag_id);
+    // If tag is not connected then attribute's value
+    // should be undefined
+    ReportError(out);
+    return;
   }
 
+  unsigned int ndef_size;
+  result = NFCAdapter::GetInstance()->TagNDEFSizeGetter(tag_id, &ndef_size);
+
+  if (result.IsSuccess()) {
+    ReportSuccess(picojson::value((double)ndef_size), out);
+  } else {
+    ReportError(result, &out);
+  }
 }
 
 void NFCInstance::TagPropertiesGetter(
@@ -618,22 +635,31 @@ void NFCInstance::TagPropertiesGetter(
 
   int tag_id = (int)args.get("id").get<double>();
   LoggerD("Tag id: %d", tag_id);
-  try {
-    // Function below throws exception if core API call fails
-    if (!NFCAdapter::GetInstance()->TagIsConnectedGetter(tag_id)) {
-      LoggerE("Tag with id %d is not connected anymore", tag_id);
-      // If tag is not connected then attribute's value
-      // should be undefined
-      ReportError(out);
-      return;
-    }
 
-    NFCTagPropertiesT result =
-        NFCAdapter::GetInstance()->TagPropertiesGetter(tag_id);
+  // Function below throws exception if core API call fails
+  bool is_connected = false;
+  PlatformResult result = NFCAdapter::GetInstance()->TagIsConnectedGetter(tag_id, &is_connected);
+
+  if (result.IsError()) {
+    ReportError(result, &out);
+    return;
+  }
+
+  if (!is_connected) {
+    LoggerE("Tag with id %d is not connected anymore", tag_id);
+    // If tag is not connected then attribute's value
+    // should be undefined
+    ReportError(out);
+    return;
+  }
+
+  NFCTagPropertiesT prop;
 
+  result = NFCAdapter::GetInstance()->TagPropertiesGetter(tag_id, &prop);
+  if (result.IsSuccess()) {
     picojson::value properties = picojson::value(picojson::array());
     picojson::array& properties_array = properties.get<picojson::array>();
-    for (auto it = result.begin() ; it != result.end(); it++) {
+    for (auto it = prop.begin() ; it != prop.end(); it++) {
       picojson::value val = picojson::value(picojson::object());
       picojson::object& obj = val.get<picojson::object>();
 
@@ -649,11 +675,10 @@ void NFCInstance::TagPropertiesGetter(
       properties_array.push_back(val);
     }
     ReportSuccess(properties, out);
+  } else {
+    ReportError(result, &out);
   }
-  catch(const PlatformException& ex) {
-    LoggerE("Failed to tag properties");
-    ReportError(ex, out);
-  }
+
 }
 
 void NFCInstance::TagIsConnectedGetter(
@@ -661,18 +686,19 @@ void NFCInstance::TagIsConnectedGetter(
 
   LoggerD("Entered");
 
+  CHECK_EXIST(args, "id", out);
   int tag_id = (int)args.get("id").get<double>();
   LoggerD("Tag id: %d", tag_id);
-  try {
-    bool connected = NFCAdapter::GetInstance()->TagIsConnectedGetter(tag_id);
-    ReportSuccess(picojson::value(connected), out);
-  }
-  catch(const PlatformException& ex) {
-    LoggerE("Failed to check tag connection");
-    ReportError(ex, out);
+
+  bool connected = false;
+  PlatformResult result = NFCAdapter::GetInstance()->TagIsConnectedGetter(tag_id, &connected);
+
+  if (result.IsSuccess()) {
+    ReportSuccess(out);
+  } else {
+    ReportError(result, &out);
   }
 }
 
-
 } // namespace nfc
 } // namespace extension
index 0ce73bd0ea0a7639f446c6c2ffcd3c26ddc32cbb..d7b65d877d39ad49564843278064047e697dd851 100644 (file)
@@ -140,7 +140,7 @@ std::string NFCUtil::getNFCErrorMessage(const int error_code)
   return "UnknownError";
 }
 
-std::string NFCUtil::toStringNFCTag(nfc_tag_type_e tag_type)
+std::string NFCUtil::ToStringNFCTag(nfc_tag_type_e tag_type)
 {
   switch (tag_type) {
     case NFC_GENERIC_PICC:
index 590c58f131ba0620c29560404cfe7dc41494d1dd..a4fb4e9fed52e5b4f265c6f671587bca3d068f69 100644 (file)
@@ -57,7 +57,7 @@ class NFCUtil
   static void throwNFCException(const int errorCode, const char * message);
   static std::string getNFCErrorString(const int error_code);
   static std::string getNFCErrorMessage(const int error_code);
-  static std::string toStringNFCTag(const nfc_tag_type_e tag_type);
+  static std::string ToStringNFCTag(const nfc_tag_type_e tag_type);
   static nfc_tag_type_e toNfcTagString(const std::string& type_string);
   static common::PlatformResult ToStringCardEmulationMode(
       const nfc_se_card_emulation_mode_type_e card_mode, std::string *mode);