Fail setup/start if device already registered

Previously device didn't check condition until wifi is switched.
Bug we can detect the issue and reply to client in the same request.

BUG: 27432528
Change-Id: I6564c47fba86671dbd59dc0ff70cfba3a25d60dc
Reviewed-on: https://weave-review.googlesource.com/2890
Reviewed-by: Alex Vakulenko <avakulenko@google.com>
diff --git a/examples/daemon/common/daemon.h b/examples/daemon/common/daemon.h
index 22591a2..23a08d2 100644
--- a/examples/daemon/common/daemon.h
+++ b/examples/daemon/common/daemon.h
@@ -114,10 +114,8 @@
  private:
   static void OnRegisterDeviceDone(weave::Device* device,
                                    weave::ErrorPtr error) {
-    if (error)
-      LOG(ERROR) << "Fail to register device: " << error->GetMessage();
-    else
-      LOG(INFO) << "Device registered: " << device->GetSettings().cloud_id;
+    CHECK(!error) << "Registration failed device: " << error->GetMessage();
+    LOG(INFO) << "Device registered: " << device->GetSettings().cloud_id;
   }
 
   std::unique_ptr<weave::examples::EventTaskRunner> task_runner_;
diff --git a/src/device_registration_info.h b/src/device_registration_info.h
index a488bae..db08ef9 100644
--- a/src/device_registration_info.h
+++ b/src/device_registration_info.h
@@ -115,6 +115,9 @@
 
   GcdState GetGcdState() const { return gcd_state_; }
 
+  // Checks whether we have credentials generated during registration.
+  bool HaveRegistrationCredentials() const;
+
  private:
   friend class DeviceRegistrationInfoTest;
 
@@ -124,8 +127,6 @@
     return weak_factory_.GetWeakPtr();
   }
 
-  // Checks whether we have credentials generated during registration.
-  bool HaveRegistrationCredentials() const;
   // Calls HaveRegistrationCredentials() and logs an error if no credentials
   // are available.
   bool VerifyRegistrationCredentials(ErrorPtr* error) const;
diff --git a/src/privet/cloud_delegate.cc b/src/privet/cloud_delegate.cc
index ef9c59e..d7e9bef 100644
--- a/src/privet/cloud_delegate.cc
+++ b/src/privet/cloud_delegate.cc
@@ -26,6 +26,8 @@
 
 namespace {
 
+const char kErrorAlreayRegistered[] = "already_registered";
+
 const BackoffEntry::Policy register_backoff_policy = {0,    1000, 2.0,  0.2,
                                                       5000, -1,   false};
 
@@ -103,6 +105,12 @@
   bool Setup(const RegistrationData& registration_data,
              ErrorPtr* error) override {
     VLOG(1) << "GCD Setup started. ";
+    if (device_->HaveRegistrationCredentials()) {
+      Error::AddTo(error, FROM_HERE, kErrorAlreayRegistered,
+                   "Unable to register already registered device");
+      return false;
+    }
+
     // Set (or reset) the retry counter, since we are starting a new
     // registration process.
     registation_retry_count_ = kMaxDeviceRegistrationRetries;
diff --git a/src/privet/privet_handler.cc b/src/privet/privet_handler.cc
index 97cacc5..41936b6 100644
--- a/src/privet/privet_handler.cc
+++ b/src/privet/privet_handler.cc
@@ -837,8 +837,9 @@
     return ReturnError(*error, callback);
 
   if (!registration_data.ticket_id.empty() &&
-      !cloud_->Setup(registration_data, &error))
+      !cloud_->Setup(registration_data, &error)) {
     return ReturnError(*error, callback);
+  }
 
   ReplyWithSetupStatus(callback);
 }