[Iotcon] implement add/removeGeneratedPinListener 92/97592/1
authorJakub Skowron <j.skowron@samsung.com>
Mon, 14 Nov 2016 18:37:44 +0000 (19:37 +0100)
committerJakub Skowron <j.skowron@samsung.com>
Mon, 14 Nov 2016 18:37:44 +0000 (19:37 +0100)
Added iotcon_manager and implemented methods
tizen.iotcon.add/removeGeneratedPinListener
using native iotcon_add/remove_generated_pin_cb()

Change-Id: I60767a8f0ed7ef8fb5378e2ad77d8059be45f18c
Signed-off-by: Jakub Skowron <j.skowron@samsung.com>
src/iotcon/iotcon.gyp
src/iotcon/iotcon_api.js
src/iotcon/iotcon_instance.cc
src/iotcon/iotcon_instance.h
src/iotcon/iotcon_manager.cc [new file with mode: 0644]
src/iotcon/iotcon_manager.h [new file with mode: 0644]

index c45581fa8b087ba1e40cd6f561d79ce0e5e04b6c..0fa85ed401c60bc50318fccf90ea80a23ae47eea 100644 (file)
@@ -15,6 +15,8 @@
         'iotcon_extension.h',
         'iotcon_instance.cc',
         'iotcon_instance.h',
+        'iotcon_manager.cc',
+        'iotcon_manager.h',
         'iotcon_server_manager.cc',
         'iotcon_server_manager.h',
         'iotcon_client_manager.cc',
index c8ea786290ceece0c6d124eb16b1f77508f0355c..1afe4a0ae1c4a0d6a39fb4ef45ad1207d70c9f09 100644 (file)
@@ -1384,6 +1384,42 @@ Iotcon.prototype.setTimeout = function() {
   }
 };
 
+var generatedPinListener = createListener( 'GeneratedPinListener' /* kGeneratedPinToken in iotcon_instance.cc */,
+                                           function(response) { return response.pin; } );
+
+Iotcon.prototype.addGeneratedPinListener  = function() {
+  var args = validator.validateMethod(arguments, [{
+    name: 'successCallback',
+    type: types.FUNCTION
+  }]);
+
+  var result = native.callSync('Iotcon_addGeneratedPinListener', {});
+
+  if (native.isFailure(result)) {
+    throw native.getErrorObject(result);
+  } else {
+    var watchId = native.getResultObject(result);
+    generatedPinListener.addListener(watchId, args.successCallback);
+    return watchId;
+  }
+};
+
+Iotcon.prototype.removeGeneratedPinListener  = function() {
+  var args = validator.validateMethod(arguments, [{
+    name: 'watchId',
+    type: types.LONG
+  }]);
+
+  var result = native.callSync('Iotcon_removeGeneratedPinListener', { watchId: args.watchId });
+
+  if (native.isFailure(result)) {
+    throw native.getErrorObject(result);
+  } else {
+    generatedPinListener.removeListener(args.watchId);
+  }
+};
+
+
 // Exports
 tizen.IotconOption = IotconOption;
 tizen.Representation = Representation;
index 41aa17e0bdfc0557a14dd0a7d47ac8470c2d7620..73bdb1e4b5d4c7bc58badefa8cc381f22fb4aa83 100644 (file)
@@ -23,6 +23,7 @@
 #include "common/tools.h"
 
 #include "iotcon/iotcon_utils.h"
+#include "iotcon/iotcon_manager.h"
 #include "iotcon/iotcon_server_manager.h"
 #include "iotcon/iotcon_client_manager.h"
 
@@ -70,6 +71,7 @@ void RemoteResourceResponseCallback(iotcon_remote_resource_h resource,
   }
 }
 
+const common::ListenerToken kGeneratedPinToken{"GeneratedPinListener"};
 const common::ListenerToken kResourceRequestListenerToken{"ResourceRequestListener"};
 const common::ListenerToken kFindResourceListenerToken{"FindResourceListener"};
 const common::ListenerToken kPresenceEventListenerToken{"PresenceEventListener"};
@@ -131,6 +133,8 @@ IotconInstance::IotconInstance() : initialized_(false), presence_started_(false)
   REGISTER_SYNC("Iotcon_initialize", Initialize);
   REGISTER_SYNC("Iotcon_getTimeout", GetTimeout);
   REGISTER_SYNC("Iotcon_setTimeout", SetTimeout);
+  REGISTER_SYNC("Iotcon_addGeneratedPinListener", AddGeneratedPinListener);
+  REGISTER_SYNC("Iotcon_removeGeneratedPinListener", RemoveGeneratedPinListener);
   REGISTER_SYNC("IotconServer_createResource", ServerCreateResource);
   REGISTER_SYNC("IotconServer_removeResource", ServerRemoveResource);
   REGISTER_SYNC("IotconServer_startPresence", ServerStartPresence);
@@ -1624,6 +1628,28 @@ common::TizenResult IotconInstance::SetTimeout(const picojson::object& args) {
   return common::TizenSuccess();
 }
 
+common::TizenResult IotconInstance::AddGeneratedPinListener(const picojson::object& args) {
+  ScopeLogger();
+
+  auto listener = [this](const char* pin, long watchId) {
+    picojson::object obj;
+    obj[kId] = picojson::value{(double)watchId};
+    obj["pin"] = picojson::value{pin};
+    this->Post(kGeneratedPinToken, common::TizenSuccess{ picojson::value{obj} });
+  };
+
+  return IotconManager::GetInstance().addGeneratedPinListener(listener);
+}
+
+common::TizenResult IotconInstance::RemoveGeneratedPinListener(const picojson::object& args) {
+  ScopeLogger();
+
+  CHECK_EXIST(args, "watchId");
+  long watchId = args.find("watchId")->second.get<double>();
+
+  return IotconManager::GetInstance().removeGeneratedPinListener(watchId);
+}
+
 common::PostCallback IotconInstance::PostForMethodCall(const common::AsyncToken& token, const FoundRemoteInfoPtr& resource) {
   ScopeLogger();
 
index 4a0201b9120b3722c4dd23c5faac12eaa5151909..2d44f5c9145aff8dc16bb3b18034dabcc7e99a24 100644 (file)
@@ -78,6 +78,8 @@ class IotconInstance : public common::TizenInstance {
   common::TizenResult Initialize(const picojson::object& args);
   common::TizenResult GetTimeout(const picojson::object& args);
   common::TizenResult SetTimeout(const picojson::object& args);
+  common::TizenResult AddGeneratedPinListener(const picojson::object& args);
+  common::TizenResult RemoveGeneratedPinListener(const picojson::object& args);
 
   common::PostCallback PostForMethodCall(const common::AsyncToken& token, const FoundRemoteInfoPtr& resource);
 
diff --git a/src/iotcon/iotcon_manager.cc b/src/iotcon/iotcon_manager.cc
new file mode 100644 (file)
index 0000000..d0f6c49
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd 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 <functional>
+
+#include <iotcon.h>
+
+#include "iotcon/iotcon_manager.h"
+#include "iotcon/iotcon_utils.h"
+#include "common/logger.h"
+
+namespace extension {
+namespace iotcon {
+
+IotconManager::IotconManager() : nextWatchId(1) {
+}
+
+IotconManager& IotconManager::GetInstance() {
+  static IotconManager instance;
+  return instance;
+}
+
+common::TizenResult IotconManager::addGeneratedPinListener(std::function<void(const char*, long)> listener) {
+  ScopeLogger();
+  using namespace std::placeholders;
+
+  //generate id
+  long watchId = nextWatchId++;
+
+  std::function<void(const char*)> listener_with_id = std::bind(listener, _1, watchId);
+
+  iotcon_generated_pin_cb callback = IotconManager::ListenerHandler;
+
+  {
+    std::lock_guard<std::mutex> mutex_lock{listeners_mutex};
+    if( listeners.empty() ) {
+      /* user_data is not used */
+      auto result = IotconUtils::ConvertIotconError( iotcon_add_generated_pin_cb(callback, nullptr) );
+      if( !result ) {
+        return result;
+      }
+    }
+
+    //store listener in map
+    this->listeners[watchId] = listener_with_id;
+  }
+
+  return common::TizenSuccess{ picojson::value{(double)watchId} };
+}
+
+common::TizenResult IotconManager::removeGeneratedPinListener(long watchId) {
+  std::lock_guard<std::mutex> mutex_guard{listeners_mutex};
+
+  auto it = listeners.find(watchId);
+  if( it == listeners.end() ) {
+    return LogAndCreateTizenError(AbortError, "Listener with specified ID does not exist");
+  }
+
+  listeners.erase(it);
+
+  // If we deleted last listener, unregister our handler
+  if( listeners.empty() ) {
+    auto result = IotconUtils::ConvertIotconError( iotcon_remove_generated_pin_cb(IotconManager::ListenerHandler) );
+    if (!result) {
+      return result;
+    }
+  }
+
+  return common::TizenSuccess{};
+}
+
+void IotconManager::ListenerHandler(const char* pin, void *user_data) {
+  ScopeLogger();
+  /* user_data is not used */
+
+  auto& instance = IotconManager::GetInstance();
+  std::unique_lock<std::mutex> mutex_guard{instance.listeners_mutex};
+  for( auto& listener : instance.listeners ) {
+    listener.second(pin);
+  }
+};
+
+} // namespace iotcon
+} // namespace extension
diff --git a/src/iotcon/iotcon_manager.h b/src/iotcon/iotcon_manager.h
new file mode 100644 (file)
index 0000000..2d9876e
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd 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 IOTCON_MANAGER_H_
+#define IOTCON_MANAGER_H_
+
+#include <map>
+#include <functional>
+#include <mutex>
+
+#include "common/tizen_result.h"
+
+namespace extension {
+namespace iotcon {
+
+class IotconManager {
+ public:
+  static IotconManager& GetInstance();
+
+  common::TizenResult addGeneratedPinListener(std::function<void(const char*,long)> listener);
+  common::TizenResult removeGeneratedPinListener(long watchId);
+
+ private:
+  static void ListenerHandler(const char* pin, void *user_data);
+
+  IotconManager();
+  IotconManager(const IotconManager&) = delete;
+  IotconManager(IotconManager&&) = delete;
+  IotconManager& operator=(const IotconManager&) = delete;
+  IotconManager& operator=(IotconManager&&) = delete;
+
+  long nextWatchId;
+  std::map<long, std::function<void(const char*)> > listeners;
+  std::mutex listeners_mutex;
+};
+
+} // namespace iotcon
+} // namespace extension
+
+
+#endif /* IOTCON_MANAGER_H_ */