#else:
# SConscript('notification-manager/SampleApp/arduino/SConscript')
+if target_os in ['linux']:
+ # Build simulator module
+ SConscript('simulator/SConscript')
+
--- /dev/null
+#******************************************************************
+#
+# Copyright 2014 Samsung Electronics 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.
+#
+#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+##
+# Simulator build script
+##
+
+import os
+Import('env')
+
+lib_env = env.Clone()
+SConscript(env.get('SRC_DIR') + '/service/third_party_libs.scons', 'lib_env')
+simulator_env = lib_env.Clone()
+
+target_os = env.get('TARGET_OS')
+######################################################################
+# Build flags
+######################################################################
+simulator_env.AppendUnique(CPPPATH = ['inc', 'src'])
+simulator_env.AppendUnique(CPPPATH = [
+ '../../resource/include/',
+ '../../resource/csdk/stack/include',
+ '../../resource/csdk/ocrandom/include',
+ '../../resource/csdk/logger/include',
+ '../../resource/oc_logger/include',
+ ])
+
+# Including Java path for building JNI files
+java_home = ARGUMENTS.get('JAVA_HOME', 0)
+if not java_home:
+ print '''
+ *************************************** Error *********************************
+ * JAVA_HOME variable not set in scons command *
+ *******************************************************************************
+ '''
+else:
+ java_headers = [os.path.join(java_home, 'include')]
+ java_headers.append(os.path.join(java_headers[0], 'win32'))
+ java_headers.append(os.path.join(java_headers[0], 'linux'))
+ java_headers.append(os.path.join(java_headers[0], 'solaris'))
+ simulator_env.AppendUnique(CPPPATH = java_headers)
+
+simulator_env.AppendUnique(CPPPATH = ['../../extlibs/cjson'])
+simulator_env.PrependUnique(LIBS = ['oc', 'octbstack', 'oc_logger', 'connectivity_abstraction', 'libcoap'])
+simulator_env.AppendUnique(LIBS = ['pthread'])
+
+simulator_env.AppendUnique(CXXFLAGS = ['-O2', '-g', '-Wall', '-fmessage-length=0', '-std=c++0x'])
+
+######################################################################
+# Source files and Targets
+######################################################################
+simulator_src = [env.Glob('src/*.cpp'), env.Glob('java/jni/*.cpp')]
+simulatorsdk = simulator_env.SharedLibrary('SimulatorManager', simulator_src)
+
+simulator_env.InstallTarget(simulatorsdk, 'libSimulator')
+
+#Build sample application
+SConscript('examples/server/SConscript')
--- /dev/null
+Import('env')
+lib_env = env.Clone()
+SConscript(env.get('SRC_DIR') + '/service/third_party_libs.scons', 'lib_env')
+sim_env = lib_env.Clone()
+
+######################################################################
+# Build flags
+######################################################################
+sim_env.AppendUnique(CPPPATH = ['../../../../extlibs/timer'])
+sim_env.AppendUnique(CPPPATH = ['../../inc'])
+sim_env.AppendUnique(CPPPATH = ['../../src'])
+sim_env.AppendUnique(CXXFLAGS = ['-std=c++0x', '-Wall', '-pthread'])
+sim_env.AppendUnique(CPPDEFINES = ['LINUX'])
+sim_env.AppendUnique(LIBS = ['SimulatorManager'])
+
+sim_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
+sim_env.AppendUnique(RPATH = [env.get('BUILD_DIR')])
+sim_env.PrependUnique(LIBS = ['SimulatorManager'])
+
+if sim_env.get('SECURED') == '1':
+ sim_env.AppendUnique(LIBS = ['tinydtls'])
+
+######################################################################
+# Source files and Targets
+######################################################################
+simulatorserver = sim_env.Program('simulator-server', 'service_provider.cpp')
+
+Alias("simulatorserver", simulatorserver)
+env.AppendTarget('simulatorserver')
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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 "simulator_manager.h"
+
+class SimLightResource
+{
+ public:
+ void startTest()
+ {
+ printMenu();
+ bool cont = true;
+ while (cont)
+ {
+ int choice = -1;
+ std::cout << "Enter your choice: ";
+ std::cin >> choice;
+ if (choice < 0 || choice > 9)
+ {
+ std::cout << "Invaild choice !" << std::endl; continue;
+ }
+
+ switch (choice)
+ {
+ case 1: simulateResource(); break;
+ case 2: displayResource(); break;
+ case 3: deleteResource(); break;
+ case 4: updateAttributePower(); break;
+ case 5: updateAttributeIntensity(); break;
+ case 6: automateResourceUpdate(); break;
+ case 7: automateAttributeUpdate(); break;
+ case 8: stopAutomation(); break;
+ case 9: printMenu(); break;
+ case 0: cont = false;
+ }
+ }
+ }
+
+ private:
+ void printMenu()
+ {
+ std::cout << "########### LIGHT RESOURCE TESTING ###########" << std::endl;
+ std::cout << "1. Simulate resource" << std::endl;
+ std::cout << "2. Display resource information" << std::endl;
+ std::cout << "3. Delete resource" << std::endl;
+ std::cout << "4. Update attribute \"power\"" << std::endl;
+ std::cout << "5. Update attribute \"intensity\"" << std::endl;
+ std::cout << "6. Automate resource update" << std::endl;
+ std::cout << "7. Automate attributes update" << std::endl;
+ std::cout << "8. Stop Automation" << std::endl;
+ std::cout << "9: Help" << std::endl;
+ std::cout << "0. Exit" << std::endl;
+ std::cout << "#######################################" << std::endl;
+ }
+
+ int selectResource()
+ {
+ int index = 1;
+ for (auto & resource : m_resources)
+ {
+ std::cout << index++ << ": " << resource->getURI().c_str() << std::endl;
+ }
+
+ int choice = -1;
+ std::cout << "Choose the resource: ";
+ std::cin >> choice;
+
+ if (choice < 1 || choice > index - 1)
+ {
+ std::cout << "Invalid choice !" << std::endl;
+ choice = -1;
+ }
+
+ return choice;
+ }
+
+ void onResourceModelChanged(const std::string &uri,
+ const std::vector<SimulatorResourceModel::Attribute> &attributes)
+ {
+ std::cout << "[callback] Resource model is changed URI: " << uri.c_str() << "Count : " <<
+ attributes.size() << std::endl;
+ std::cout << "#### Modified attributes are ####" << std::endl;
+ for (auto & attribute : attributes)
+ {
+ std::cout << attribute.getName() << " : " << attribute.valueToString().c_str() << std::endl;
+ }
+ std::cout << "########################" << std::endl;
+ }
+
+ void simulateResource()
+ {
+ SimulatorResource::ResourceModelChangedCB callback = std::bind(
+ &SimLightResource::onResourceModelChanged, this, std::placeholders::_1, std::placeholders::_2);
+ SimulatorResourcePtr resource = SimulatorManager::getInstance()->createResource("", callback);
+ if (NULL == resource.get())
+ std::cout << "Failed to create resource" << std::endl;
+
+ m_resources.push_back(resource);
+ std::cout << "Resource created successfully! URI= " << resource->getURI().c_str() << std::endl;
+ }
+
+ void deleteResource()
+ {
+ int index = selectResource();
+ if (-1 == index)
+ return;
+
+ SimulatorManager::getInstance()->deleteResource(m_resources[index - 1]);
+ std::cout << "Resource deleted successfully! " << std::endl;
+ }
+
+ void updateAttributePower()
+ {
+ int index = selectResource();
+ if (-1 == index)
+ return;
+
+ SimulatorResourcePtr resource = m_resources[index - 1];
+ SimulatorResourceModel resModel = resource->getModel();
+ SimulatorResourceModel::Attribute powerAttribute;
+ resModel.getAttribute("power", powerAttribute);
+
+ int allowedValuesSize = powerAttribute.getAllowedValuesSize();
+ if (0 == allowedValuesSize)
+ {
+ std::cout << "This attribute does not have allowed values!" << std::endl;
+ return;
+ }
+
+ std::cout << "Setting the new values from allowed values list to power attribute" << std::endl;
+ // Update all possible values from allowed values
+ for (int index = 0; index < allowedValuesSize; index++)
+ {
+ // Update the new value and display the resource model after modifying
+ resource->updateAttributeFromAllowedValues("power", index);
+ std::cout << "Attribute value is modified ####" << std::endl;
+
+ // Display the resource to user to verify the changed attribute value
+ displayResource(resource);
+ std::cout << std::endl << std::endl;
+
+ // Get user input for continuing this operation
+ if ((index + 1) < allowedValuesSize)
+ {
+ int choice;
+ std::cout << "Would you like to continue the attribute values changing process? (1/0): ";
+ std::cin >> choice;
+ if (0 == choice)
+ break;
+ }
+ }
+
+ std::cout << "All the allowed values are tried!" << std::endl;
+ }
+
+ void updateAttributeIntensity()
+ {
+ int index = selectResource();
+ if (-1 == index)
+ return;
+
+ SimulatorResourcePtr resource = m_resources[index - 1];
+ SimulatorResourceModel resModel = resource->getModel();
+ SimulatorResourceModel::Attribute intensityAttribute;
+ resModel.getAttribute("intensity", intensityAttribute);
+
+ int allowedValuesSize = intensityAttribute.getAllowedValuesSize();
+ if (0 == allowedValuesSize)
+ {
+ std::cout << "This attribute does not have allowed values!" << std::endl;
+ return;
+ }
+
+ std::cout << "Setting the new values from allowed values list to intensity attribute" << std::endl;
+ // Update all possible values from allowed values
+ for (int index = 0; index < allowedValuesSize; index++)
+ {
+ // Update the new value and display the resource model after modifying
+ resource->updateAttributeFromAllowedValues("intensity", index);
+ std::cout << "Attribute value is modified ####" << std::endl;
+
+ // Display the resource to user to verify the changed attribute value
+ displayResource(resource);
+ std::cout << std::endl << std::endl;
+
+ // Get user input for continuing this operation
+ if ((index + 1) < allowedValuesSize)
+ {
+ int choice;
+ std::cout << "Would you like to continue the attribute values changing process? (1/0): ";
+ std::cin >> choice;
+ if (0 == choice)
+ break;
+ }
+ }
+
+ std::cout << "All the allowed values are tried!" << std::endl;
+ }
+
+ void displayResource()
+ {
+ int index = selectResource();
+ if (-1 == index)
+ return;
+
+ SimulatorResourcePtr resource = m_resources[index - 1];
+ displayResource(resource);
+ }
+
+ void displayResource(SimulatorResourcePtr resource)
+ {
+ std::cout << "#############################" << std::endl;
+ std::cout << "Name: " << resource->getName().c_str() << std::endl;
+ std::cout << "URI: " << resource->getURI().c_str() << std::endl;
+ std::cout << "R. Type: " << resource->getResourceType().c_str() << std::endl;
+ std::cout << "I. Type: " << resource->getInterfaceType().c_str() << std::endl;
+
+ // Attributes
+ SimulatorResourceModel resModel = resource->getModel();
+ std::map<std::string, SimulatorResourceModel::Attribute> attributes = resModel.getAttributes();
+ std::cout << "##### Attributes [" << attributes.size() << "]" << std::endl;
+ for (auto & attribute : attributes)
+ {
+ std::cout << (attribute.second).getName() << " : {" << std::endl;
+ std::cout << "value: " << (attribute.second).valueToString().c_str() << std::endl;
+ int min, max;
+ (attribute.second).getRange(min, max);
+ std::cout << "min: " << min << std::endl;
+ std::cout << "max: " << max << std::endl;
+ std::cout << "allowed values : " << (attribute.second).allowedValuesToString() << std::endl;
+ std::cout << "}" << std::endl << std::endl;
+ }
+ std::cout << "#############################" << std::endl;
+ }
+
+ void automateResourceUpdate()
+ {
+ int index = selectResource();
+ if (-1 == index)
+ return;
+
+ int id;
+ if (SIMULATOR_SUCCESS != m_resources[index - 1]->startUpdateAutomation(AutomationType::NORMAL, id))
+ std::cout << "startUpdateAutomation() returned error!" << std::endl;
+ else
+ std::cout << "startUpdateAutomation() returned succces : " << id << std::endl;
+ }
+
+ void automateAttributeUpdate()
+ {
+ int index = selectResource();
+ if (-1 == index)
+ return;
+
+ SimulatorResourcePtr resource = m_resources[index - 1];
+ SimulatorResourceModel resModel = resource->getModel();
+ std::map<std::string, SimulatorResourceModel::Attribute> attributes = resModel.getAttributes();
+ int size = 0;
+ for (auto & attribute : attributes)
+ {
+ std::cout << ++size << ": " << attribute.first.c_str() << std::endl;
+ }
+
+ if (0 == size)
+ {
+ std::cout << "This resource doest not contain any attributes!" << std::endl;
+ return;
+ }
+
+ int choice = -1;
+ std::cout << "Select the attribute which you want to automate for updation: " << std::endl;
+ std::cin >> choice;
+ if (choice == -1 || choice > size)
+ {
+ std::cout << "Invalid selection!" << std::endl;
+ return;
+ }
+
+ int count = 0;
+ std::string attributeName;
+ for (auto & attribute : attributes)
+ {
+ if (count == choice - 1)
+ {
+ attributeName = attribute.first;
+ break;
+ }
+
+ count++;
+ }
+
+ std::cout << "Requesting attribute automation for " << attributeName.c_str() << std::endl;
+ int id;
+ if (SIMULATOR_SUCCESS != resource->startUpdateAutomation(attributeName, AutomationType::NORMAL, id))
+ std::cout << "startUpdateAutomation() returned error!" << std::endl;
+ else
+ std::cout << "startUpdateAutomation() returned succces : " << id << std::endl;
+ }
+
+ void stopAutomation()
+ {
+ int index = selectResource();
+ if (-1 == index)
+ return;
+
+ SimulatorResourcePtr resource = m_resources[index - 1];
+ int automationid;
+ std::cout << "Enter automation id: " << std::endl;
+ std::cin >> automationid;
+ resource->stopUpdateAutomation(automationid);
+ }
+
+ private:
+ std::vector<SimulatorResourcePtr> m_resources;
+};
+
+void printMainMenu()
+{
+ std::cout << "############### MAIN MENU###############" << std::endl;
+ std::cout << "1. Test simulation of light resource" << std::endl;
+ std::cout << "2. Help" << std::endl;
+ std::cout << "0. Exit" << std::endl;
+ std::cout << "######################################" << std::endl;
+}
+
+int main(void)
+{
+ SimLightResource lightResource;
+
+ printMainMenu();
+ bool cont = true;
+ while (cont == true)
+ {
+ int choice = -1;
+ std::cout << "Enter your choice: ";
+ std::cin >> choice;
+ if (choice < 0 || choice > 2)
+ {
+ std::cout << "Invaild choice !" << std::endl; continue;
+ }
+
+ switch (choice)
+ {
+ case 1: lightResource.startTest();
+ std::cout << "Welcome back to main menu !" << std::endl;
+ break;
+ case 2: printMainMenu(); break;
+ case 0: cont = false;
+ }
+ }
+
+ std::cout << "Terminating test !!!" << std::endl;
+}
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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.
+ *
+ ******************************************************************/
+
+/**
+ * @file simulator_manager.h
+ *
+ * @brief This file contains the declaration of SimulatorManager class which has the methods
+ * for configuring the platform and creation/deletion of resources.
+ */
+
+#ifndef SIMULATOR_MANAGER_H_
+#define SIMULATOR_MANAGER_H_
+
+#include <vector>
+#include "simulator_error_codes.h"
+#include "simulator_resource.h"
+
+/**
+ * @class SimulatorManager
+ *
+ * @brief This class provides a set of methods for platform configuration,
+ * and creation/deletion of resources.
+ *
+ */
+class SimulatorManager
+{
+ public:
+ static SimulatorManager *getInstance();
+
+ /**
+ * This method is called for creating a single resource from RAML configuration file.
+ *
+ * @param configPath - RAML configuration file path.
+ * @param callback - Callback method for receive notifications when resource model changes.
+ *
+ * @return SimulatorResourcePtr - Shared pointer of SimulatorResource on success, otherwise NULL.
+ */
+ SimulatorResourcePtr createResource(const std::string &configPath,
+ SimulatorResource::ResourceModelChangedCB callback);
+
+ /**
+ * This method is called for creating a collection of resources from RAML configuration file.
+ *
+ * @param configPath - RAML configuration file path.
+ * @param count - Number of resource to be created.
+ * @param callback - Callback method for receive notifications when resource model changes.
+ *
+ * @return SimulatorResourcePtr - A vector of Shared pointers of SimulatorResource Objects.
+ */
+ std::vector<SimulatorResourcePtr> createResource(const std::string &configPath,
+ const int count,
+ SimulatorResource::ResourceModelChangedCB callback);
+
+ /**
+ * This method is called for obtaining a list of created resources.
+ *
+ * @return SimulatorResourcePtr - A vector of Shared pointers of SimulatorResource Objects.
+ */
+ std::vector<SimulatorResourcePtr> getResources(void);
+
+ /**
+ * This method is called for deleting a single resource.
+ *
+ * @param resource - Shared pointer of the SimulatorResource to be deleted.
+ *
+ * @return SimulatorResult
+ */
+ SimulatorResult deleteResource(std::shared_ptr<SimulatorResource> &resource);
+
+ /**
+ * This method is called for deleting multiple resources.
+ * If this method is called without any parameter, then all resources will be deleted.
+ * If thie method is called with a specific resourcetype as a parameter, then all the resources
+ * of that particular type will be deleted.
+ *
+ * @param resourceType - Resource type of the resource
+ *
+ * @return SimulatorResult
+ */
+ SimulatorResult deleteResources(const std::string &resourceType = "");
+
+ private:
+ SimulatorManager();
+};
+
+#endif
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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 SIMULATOR_COMMON_JNI_H_
+#define SIMULATOR_COMMON_JNI_H_
+
+#include <jni.h>
+
+static jfieldID GetHandleField(JNIEnv *env, jobject jobj)
+{
+ jclass cls = env->GetObjectClass(jobj);
+ return env->GetFieldID(cls, "nativeHandle", "J");
+}
+
+template <typename T>
+static T *GetHandle(JNIEnv *env, jobject jobj)
+{
+ jlong handle = env->GetLongField(jobj, GetHandleField(env, jobj));
+ return reinterpret_cast<T *>(handle);
+}
+
+template <typename T>
+static void SetHandle(JNIEnv *env, jobject jobj, T *type)
+{
+ jlong handle = reinterpret_cast<jlong>(type);
+
+ env->SetLongField(jobj, GetHandleField(env, jobj), handle);
+}
+
+typedef struct
+{
+ jclass classInteger;
+ jclass classDouble;
+ jclass classString;
+ jclass classHashMap;
+ jclass classSimulatorResource;
+ jclass classSimulatorResourceModel;
+ jclass classSimulatorResourceAttribute;
+
+ jmethodID classIntegerCtor;
+ jmethodID classDoubleCtor;
+ jmethodID classHashMapCtor;
+ jmethodID classHashMapPut;
+ jmethodID classSimulatorResourceCtor;
+ jmethodID classSimulatorResourceSetURI;
+ jmethodID classSimulatorResourceSetResourceType;
+ jmethodID classSimulatorResourceSetInterfaceType;
+ jmethodID classSimulatorResourceSetName;
+ jmethodID classSimulatorResourceModelCtor;
+ jmethodID classSimulatorResourceAttributeCtor;
+} SimulatorClassRefs;
+
+#endif
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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 "simulator_manager_jni.h"
+#include "simulator_resource_jni.h"
+#include "simulator_common_jni.h"
+#include "simulator_manager.h"
+
+#define SIMULATOR_RESOURCE_PATH "org/iotivity/simulator/SimulatorResourceServer"
+#define SIMULATOR_RESOURCE_TYPE "Lorg/iotivity/simulator/SimulatorResourceServer;"
+
+#define SIMULATOR_RESOURCE_MODEL_PATH "org/iotivity/simulator/SimulatorResourceModel"
+#define SIMULATOR_RESOURCE_MODEL_TYPE "Lorg/iotivity/simulator/SimulatorResourceModel;"
+
+#define SIMULATOR_RESOURCE_ATTRIBUTE_PATH "org/iotivity/simulator/SimulatorResourceAttribute"
+#define SIMULATOR_RESOURCE_ATTRIBUTE_TYPE "Lorg/iotivity/simulator/SimulatorResourceAttribute;"
+
+SimulatorClassRefs gSimulatorClassRefs;
+
+JNIEXPORT jobject JNICALL
+Java_org_iotivity_simulator_SimulatorManagerNativeInterface_createResource
+(JNIEnv *env, jclass object, jstring jConfigPath)
+{
+ if (!jConfigPath)
+ return NULL;
+
+ const char *configPath = env->GetStringUTFChars(jConfigPath, NULL);
+ if (!configPath)
+ return NULL;
+
+ std::string config(configPath);
+ std::shared_ptr<SimulatorResource> resource = SimulatorManager::getInstance()->createResource(
+ config, nullptr);
+ if (NULL == resource.get())
+ return NULL;
+
+ JniSimulatorResource *jniSimResource = new JniSimulatorResource(resource);
+ jobject jSimulatorResource = JniSimulatorResource::toJava(env,
+ reinterpret_cast<jlong>(jniSimResource));
+
+ // Setting the uri and resourceType
+ std::string uri = resource->getURI();
+ std::string resourceType = resource->getResourceType();
+ std::string name = resource->getName();
+
+ JniSimulatorResource::setUri(env, jSimulatorResource, uri);
+ JniSimulatorResource::setResourceType(env, jSimulatorResource, resourceType);
+ JniSimulatorResource::setResourceName(env, jSimulatorResource, name);
+
+ return jSimulatorResource;
+}
+
+JNIEXPORT jobjectArray JNICALL
+Java_org_iotivity_simulator_SimulatorManagerNativeInterface_createResources
+(JNIEnv *env, jclass object, jstring jConfigPath, jint count)
+{
+ //TODO: Need to implement this method
+ return nullptr;
+}
+
+JNIEXPORT jobjectArray JNICALL
+Java_org_iotivity_simulator_SimulatorManagerNativeInterface_getResources
+(JNIEnv *env, jclass object)
+{
+ //TODO: Need to implement this method
+ return nullptr;
+}
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorManagerNativeInterface_deleteResource
+(JNIEnv *env, jclass object, jobject jResource)
+{
+ if (!jResource)
+ return;
+
+ std::shared_ptr<SimulatorResource> resource =
+ JniSimulatorResource::getJniSimulatorResourcePtr(env, jResource);
+ if (!resource)
+ return;
+
+ SimulatorManager::getInstance()->deleteResource(resource);
+}
+
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorManagerNativeInterface_deleteResources
+(JNIEnv *env, jclass object, jstring resourceType)
+{
+ if (!resourceType)
+ return;
+
+ const char *type = env->GetStringUTFChars(resourceType, NULL);
+ if (!type)
+ return;
+
+ SimulatorManager::getInstance()->deleteResources(type);
+}
+
+
+static bool getClassRef(JNIEnv *env, const char *className, jclass &classRef)
+{
+ jclass localClassRef = nullptr;
+ localClassRef = env->FindClass(className);
+ if (!localClassRef)
+ return false;
+
+ classRef = (jclass)env->NewGlobalRef(localClassRef);
+ env->DeleteLocalRef(localClassRef);
+ return true;
+}
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
+{
+ if (!vm)
+ {
+ return JNI_ERR;
+ }
+
+ JNIEnv *env = NULL;
+ if (JNI_OK != vm->GetEnv((void **) &env, JNI_VERSION_1_6))
+ {
+ return JNI_ERR;
+ }
+
+ // Get the class references and constructor methods
+ if (false == getClassRef(env, "java/lang/Integer", gSimulatorClassRefs.classInteger))
+ {
+ return JNI_ERR;
+ }
+
+ if (false == getClassRef(env, "java/lang/Double", gSimulatorClassRefs.classDouble))
+ {
+ return JNI_ERR;
+ }
+
+ if (false == getClassRef(env, "java/lang/String", gSimulatorClassRefs.classString))
+ {
+ return JNI_ERR;
+ }
+
+ if (false == getClassRef(env, "java/util/HashMap", gSimulatorClassRefs.classHashMap))
+ {
+ return JNI_ERR;
+ }
+
+ if (false == getClassRef(env, SIMULATOR_RESOURCE_PATH, gSimulatorClassRefs.classSimulatorResource))
+ {
+ return JNI_ERR;
+ }
+
+ if (false == getClassRef(env, SIMULATOR_RESOURCE_MODEL_PATH,
+ gSimulatorClassRefs.classSimulatorResourceModel))
+ {
+ return JNI_ERR;
+ }
+
+ if (false == getClassRef(env, SIMULATOR_RESOURCE_ATTRIBUTE_PATH,
+ gSimulatorClassRefs.classSimulatorResourceAttribute))
+ {
+ return JNI_ERR;
+ }
+
+ // Get the constructor methods
+ gSimulatorClassRefs.classIntegerCtor = env->GetMethodID(gSimulatorClassRefs.classInteger, "<init>",
+ "(I)V");
+ if (!gSimulatorClassRefs.classIntegerCtor)
+ return JNI_ERR;
+
+ gSimulatorClassRefs.classDoubleCtor = env->GetMethodID(gSimulatorClassRefs.classDouble, "<init>",
+ "(D)V");
+ if (!gSimulatorClassRefs.classDoubleCtor)
+ return JNI_ERR;
+
+ gSimulatorClassRefs.classHashMapCtor = env->GetMethodID(gSimulatorClassRefs.classHashMap, "<init>",
+ "()V");
+ if (!gSimulatorClassRefs.classHashMapCtor)
+ return JNI_ERR;
+
+ gSimulatorClassRefs.classHashMapPut = env->GetMethodID(gSimulatorClassRefs.classHashMap, "put",
+ "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
+ if (!gSimulatorClassRefs.classHashMapPut)
+ return JNI_ERR;
+
+ gSimulatorClassRefs.classSimulatorResourceCtor = env->GetMethodID(
+ gSimulatorClassRefs.classSimulatorResource, "<init>", "(J)V");
+ if (!gSimulatorClassRefs.classSimulatorResourceCtor)
+ return JNI_ERR;
+
+ gSimulatorClassRefs.classSimulatorResourceSetURI = env->GetMethodID(
+ gSimulatorClassRefs.classSimulatorResource, "setURI", "(Ljava/lang/String;)V");
+ if (!gSimulatorClassRefs.classSimulatorResourceSetURI)
+ return JNI_ERR;
+
+ gSimulatorClassRefs.classSimulatorResourceSetResourceType = env->GetMethodID(
+ gSimulatorClassRefs.classSimulatorResource, "setResourceType", "(Ljava/lang/String;)V");
+ if (!gSimulatorClassRefs.classSimulatorResourceSetResourceType)
+ return JNI_ERR;
+
+ gSimulatorClassRefs.classSimulatorResourceSetInterfaceType = env->GetMethodID(
+ gSimulatorClassRefs.classSimulatorResource, "setInterfaceType", "(Ljava/lang/String;)V");
+ if (!gSimulatorClassRefs.classSimulatorResourceSetInterfaceType)
+ return JNI_ERR;
+
+ gSimulatorClassRefs.classSimulatorResourceSetName = env->GetMethodID(
+ gSimulatorClassRefs.classSimulatorResource, "setName", "(Ljava/lang/String;)V");
+ if (!gSimulatorClassRefs.classSimulatorResourceSetName)
+ return JNI_ERR;
+
+ gSimulatorClassRefs.classSimulatorResourceModelCtor = env->GetMethodID(
+ gSimulatorClassRefs.classSimulatorResourceModel, "<init>", "(J)V");
+ if (!gSimulatorClassRefs.classSimulatorResourceModelCtor)
+ return JNI_ERR;
+
+ gSimulatorClassRefs.classSimulatorResourceAttributeCtor = env->GetMethodID(
+ gSimulatorClassRefs.classSimulatorResourceAttribute, "<init>", "(J)V");
+ if (!gSimulatorClassRefs.classSimulatorResourceAttributeCtor)
+ return JNI_ERR;
+
+ return JNI_VERSION_1_6;
+}
+
+JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
+{
+ JNIEnv *env = NULL;
+ if (JNI_OK != vm->GetEnv((void **)&env, JNI_VERSION_1_6))
+ {
+ return;
+ }
+}
+
+#ifdef __cplusplus
+}
+#endif
\ No newline at end of file
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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 SIMULATOR_MANAGER_JNI_H_
+#define SIMULATOR_MANAGER_JNI_H_
+
+#include <jni.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorManagerNativeInterface_initialize
+(JNIEnv *env, jclass object);
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorManagerNativeInterface_terminate
+(JNIEnv *env, jclass object);
+
+JNIEXPORT jobject JNICALL
+Java_org_iotivity_simulator_SimulatorManagerNativeInterface_createResource
+(JNIEnv *env, jclass object, jstring jConfigPath);
+
+JNIEXPORT jobjectArray JNICALL
+Java_org_iotivity_simulator_SimulatorManagerNativeInterface_createResources
+(JNIEnv *env, jclass object, jstring jConfigPath, jint count);
+
+JNIEXPORT jobjectArray JNICALL
+Java_org_iotivity_simulator_SimulatorManageNativeInterfacer_getResources
+(JNIEnv *env, jclass object);
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorManagerNativeInterface_deleteResource
+(JNIEnv *env, jclass object, jobject jResource);
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorManagerNativeInterface_deleteResources
+(JNIEnv *env, jclass object, jstring resourceType);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif //SIMULATOR_MANAGER_JNI_H_
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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 "simulator_resource_attributes_jni.h"
+#include "simulator_resource_model.h"
+#include "simulator_common_jni.h"
+#include "simulator_error_codes.h"
+
+extern SimulatorClassRefs gSimulatorClassRefs;
+
+jobject JniSimulatorResourceAttribute::toJava(JNIEnv *env, jlong resource)
+{
+ jobject resourceObj = (jobject) env->NewObject(gSimulatorClassRefs.classSimulatorResourceAttribute,
+ gSimulatorClassRefs.classSimulatorResourceAttributeCtor, resource);
+ if (NULL == resourceObj)
+ {
+ return NULL;
+ }
+
+ return resourceObj;
+}
+
+class attribute_value_visitor : public boost::static_visitor<jobject>
+{
+ public:
+ attribute_value_visitor(JNIEnv *env) : m_Env(env) {}
+
+ jobject operator ()(const int &value) const
+ {
+ jobject result = m_Env->NewObject(gSimulatorClassRefs.classInteger,
+ gSimulatorClassRefs.classIntegerCtor, value);
+ return result;
+ }
+
+ jobject operator ()(const double &value) const
+ {
+ jobject result = m_Env->NewObject(gSimulatorClassRefs.classDouble,
+ gSimulatorClassRefs.classDoubleCtor, value);
+ return result;
+ }
+
+ jobject operator ()(const std::string &value) const
+ {
+ jstring str = m_Env->NewStringUTF(value.c_str());
+ return static_cast<jobject>(str);
+ }
+
+ private:
+ JNIEnv *m_Env;
+};
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceAttribute_create
+(JNIEnv *env, jobject object, jstring attrName)
+{
+ if (!attrName)
+ {
+ std::cout << "SimulatorResourceAttribute_create: AttributeName is Empty";
+ return;
+ }
+
+ const char *attrNamePtr = env->GetStringUTFChars(attrName, NULL);
+ if (!attrNamePtr)
+ return;
+
+ std::string attrNameStr(attrNamePtr);
+ SimulatorResourceModel::Attribute *attribute = new SimulatorResourceModel::Attribute(attrNameStr);
+ SetHandle<SimulatorResourceModel::Attribute>(env, object, attribute);
+ if (env->ExceptionCheck())
+ {
+ delete attribute;
+ }
+
+ env->ReleaseStringUTFChars(attrName, attrNamePtr);
+}
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceAttribute_dispose
+(JNIEnv *env, jobject object)
+{
+ SimulatorResourceModel::Attribute *attribute = GetHandle<SimulatorResourceModel::Attribute>(env,
+ object);
+ delete attribute;
+}
+
+JNIEXPORT int JNICALL
+Java_org_iotivity_simulator_SimulatorResourceAttribute_allowedValuesSize
+(JNIEnv *env, jobject object)
+{
+ SimulatorResourceModel::Attribute *attribute = GetHandle<SimulatorResourceModel::Attribute>(env,
+ object);
+ if (env->ExceptionCheck() || !attribute)
+ {
+ return SIMULATOR_ERROR;
+ }
+
+ return attribute->getAllowedValuesSize();
+}
+
+JNIEXPORT jstring JNICALL
+Java_org_iotivity_simulator_SimulatorResourceAttribute_valueToString
+(JNIEnv *env, jobject object)
+{
+ SimulatorResourceModel::Attribute *attribute = GetHandle<SimulatorResourceModel::Attribute>(env,
+ object);
+ if (env->ExceptionCheck() || !attribute)
+ {
+ return NULL;
+ }
+
+ std::string str = attribute->valueToString();
+ return env->NewStringUTF(str.c_str());
+}
+
+JNIEXPORT jstring JNICALL
+Java_org_iotivity_simulator_SimulatorResourceAttribute_allowedValuesToString
+(JNIEnv *env, jobject object)
+{
+ SimulatorResourceModel::Attribute *attribute = GetHandle<SimulatorResourceModel::Attribute>(env,
+ object);
+ if (env->ExceptionCheck() || !attribute)
+ {
+ return NULL;
+ }
+
+ std::string str = attribute->allowedValuesToString();
+ return env->NewStringUTF(str.c_str());
+}
+
+JNIEXPORT jstring JNICALL
+Java_org_iotivity_simulator_SimulatorResourceAttribute_getName
+(JNIEnv *env, jobject object)
+{
+ SimulatorResourceModel::Attribute *attribute = GetHandle<SimulatorResourceModel::Attribute>(env,
+ object);
+ if (env->ExceptionCheck() || !attribute)
+ {
+ return NULL;
+ }
+
+ std::string str = attribute->getName();
+ return env->NewStringUTF(str.c_str());
+}
+
+JNIEXPORT jobject JNICALL
+Java_org_iotivity_simulator_SimulatorResourceAttribute_getValue
+(JNIEnv *env, jobject object)
+{
+ SimulatorResourceModel::Attribute *attribute = GetHandle<SimulatorResourceModel::Attribute>(env,
+ object);
+ if (env->ExceptionCheck() || !attribute)
+ {
+ return NULL;
+ }
+
+ return boost::apply_visitor(attribute_value_visitor(env), attribute->getValue());
+}
\ No newline at end of file
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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 SIMULATOR_RESOURCE_ATTRIBUTE_JNI_H_
+#define SIMULATOR_RESOURCE_ATTRIBUTE_JNI_H_
+
+#include <jni.h>
+#include "simulator_resource_model.h"
+
+class JniSimulatorResourceAttribute
+{
+ public:
+ static jobject toJava(JNIEnv *env, jlong resource);
+};
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceAttribute_create
+(JNIEnv *, jobject, jstring);
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceAttribute_dispose
+(JNIEnv *, jobject);
+
+JNIEXPORT int JNICALL
+Java_org_iotivity_simulator_SimulatorResourceAttribute_allowedValuesSize
+(JNIEnv *, jobject);
+
+JNIEXPORT jstring JNICALL
+Java_org_iotivity_simulator_SimulatorResourceAttribute_valueToString
+(JNIEnv *, jobject);
+
+JNIEXPORT jstring JNICALL
+Java_org_iotivity_simulator_SimulatorResourceAttribute_allowedValuesToString
+(JNIEnv *, jobject);
+
+JNIEXPORT jstring JNICALL
+Java_org_iotivity_simulator_SimulatorResourceAttribute_getName
+(JNIEnv *, jobject);
+
+JNIEXPORT jobject JNICALL
+Java_org_iotivity_simulator_SimulatorResourceAttribute_getValue
+(JNIEnv *, jobject);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif //SIMULATOR_RESOURCE_ATTRIBUTE_JNI_H_
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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 "simulator_resource_jni.h"
+#include "simulator_resource_jni_util.h"
+#include "simulator_common_jni.h"
+#include "simulator_resource_model_jni.h"
+
+extern SimulatorClassRefs gSimulatorClassRefs;
+
+JniSimulatorResource::JniSimulatorResource(SimulatorResourcePtr &resource)
+ : m_sharedResource(resource) {}
+
+SimulatorResourcePtr JniSimulatorResource::getJniSimulatorResourcePtr(JNIEnv *env, jobject thiz)
+{
+ JniSimulatorResource *resource = GetHandle<JniSimulatorResource>(env, thiz);
+ if (env->ExceptionCheck())
+ {
+ return NULL;
+ }
+ return resource->m_sharedResource;
+}
+
+jobject JniSimulatorResource::toJava(JNIEnv *env, jlong resource)
+{
+ jobject resourceObj = (jobject) env->NewObject(gSimulatorClassRefs.classSimulatorResource,
+ gSimulatorClassRefs.classSimulatorResourceCtor, resource);
+ if (NULL == resourceObj)
+ {
+ return NULL;
+ }
+ return resourceObj;
+}
+
+void JniSimulatorResource::setUri(JNIEnv *env, jobject jobj, const std::string &uri)
+{
+ if (!env || !jobj)
+ return;
+
+ jstring jURI = env->NewStringUTF(uri.c_str());
+ if (!jURI)
+ return;
+
+ env->CallVoidMethod(jobj, gSimulatorClassRefs.classSimulatorResourceSetURI, jURI);
+ env->DeleteLocalRef(jURI);
+}
+
+void JniSimulatorResource::setResourceType(JNIEnv *env, jobject jobj,
+ const std::string &resourceType)
+{
+ if (!env || !jobj)
+ return;
+
+ jstring jResourceType = env->NewStringUTF(resourceType.c_str());
+ if (!jResourceType)
+ return;
+
+ env->CallVoidMethod(jobj, gSimulatorClassRefs.classSimulatorResourceSetResourceType, jResourceType);
+ env->DeleteLocalRef(jResourceType);
+}
+
+void JniSimulatorResource::setResourceName(JNIEnv *env, jobject jobj, const std::string &name)
+{
+ if (!env || !jobj)
+ return;
+
+ jstring jName = env->NewStringUTF(name.c_str());
+ if (!jName)
+ return;
+
+ env->CallVoidMethod(jobj, gSimulatorClassRefs.classSimulatorResourceSetName, jName);
+ env->DeleteLocalRef(jName);
+}
+
+JNIEXPORT jobject JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_getModel
+(JNIEnv *env, jobject object)
+{
+ SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
+ if (nullptr == resource.get())
+ {
+ std::cout << "getModel: Resource is NULL";
+ return nullptr;
+ }
+
+ SimulatorResourceModel resModel = resource->getModel();
+ JniSimulatorResourceModel *model = new JniSimulatorResourceModel(resModel);
+ jobject jModel = JniSimulatorResourceModel::toJava(env, reinterpret_cast<jlong>(model));
+ return jModel;
+}
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_updateAttributeFromAllowedValues
+(JNIEnv *env, jobject object, jstring attrName, jint index)
+{
+ SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
+ if (nullptr == resource.get())
+ {
+ std::cout << "updateAttributeFromAllowedValues: Resource is NULL";
+ return;
+ }
+
+ const char *attrNamePtr = env->GetStringUTFChars(attrName, NULL);
+ if (!attrNamePtr)
+ {
+ std::cout << "updateAttributeFromAllowedValues: Failed to convert jstring to char string!";
+ return;
+ }
+
+ resource->updateAttributeFromAllowedValues(attrNamePtr, static_cast<int>(index));
+ env->ReleaseStringUTFChars(attrName, attrNamePtr);
+}
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_setRange
+(JNIEnv *env, jobject object, jstring attrName, jint min, jint max)
+{
+ SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
+ if (nullptr == resource.get())
+ {
+ std::cout << "setRange: Resource is NULL";
+ return;
+ }
+
+ const char *attrNamePtr = env->GetStringUTFChars(attrName, NULL);
+ if (!attrNamePtr)
+ {
+ std::cout << "setRange: Failed to convert jstring to char string!";
+ return;
+ }
+
+ resource->setRange(attrNamePtr, static_cast<int>(min), static_cast<int>(max));
+ env->ReleaseStringUTFChars(attrName, attrNamePtr);
+}
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_setInterfaceType
+(JNIEnv *env, jobject jobject, const std::string &interfaceType)
+{
+ jstring jInterfaceType = env->NewStringUTF(interfaceType.c_str());
+ if (!jInterfaceType)
+ {
+ std::cout << "setInterfaceType: InterfaceType is NULL";
+ return;
+ }
+
+ env->CallVoidMethod(jobject, gSimulatorClassRefs.classSimulatorResourceSetInterfaceType,
+ jInterfaceType);
+ env->DeleteLocalRef(jInterfaceType);
+}
+
+JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_addAttributeInteger
+(JNIEnv *env, jobject jobject, jstring jKey, jint jValue)
+{
+ if (!jKey)
+ {
+ std::cout << "addAttributeInteger: AttributeName is Empty";
+ return;
+ }
+
+ SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
+ if (nullptr == resource.get())
+ {
+ std::cout << "addAttributeInteger: Resource is NULL";
+ return;
+ }
+
+ std::string str = env->GetStringUTFChars(jKey, NULL);
+ resource->addAttribute(str, static_cast<int>(jValue));
+}
+
+JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_addAttributeDouble
+(JNIEnv *env, jobject jobject, jstring jKey, jdouble jValue)
+{
+ if (!jKey)
+ {
+ std::cout << "addAttributeDouble: AttributeName is Empty";
+ return;
+ }
+
+ SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
+ if (nullptr == resource.get())
+ {
+ std::cout << "addAttributeDouble: Resource is NULL";
+ return;
+ }
+
+ std::string str = env->GetStringUTFChars(jKey, NULL);
+ resource->addAttribute(str, static_cast<double>(jValue));
+}
+
+JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_addAttributeBoolean
+(JNIEnv *env, jobject jobject, jstring jKey, jboolean jValue)
+{
+ if (!jKey)
+ {
+ std::cout << "addAttributeBoolean: AttributeName is Empty";
+ return;
+ }
+
+ SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
+ if (nullptr == resource.get())
+ {
+ std::cout << "addAttributeBoolean: Resource is NULL";
+ return;
+ }
+
+ std::string str = env->GetStringUTFChars(jKey, NULL);
+ resource->addAttribute(str, static_cast<bool>(jValue));
+}
+
+JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_addAttributeStringN
+(JNIEnv *env, jobject jobject, jstring jKey, jstring jValue)
+{
+ if (!jKey)
+ {
+ std::cout << "addAttributeStringN: AttributeName is Empty";
+ return;
+ }
+
+ SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
+ if (nullptr == resource.get())
+ {
+ std::cout << "addAttributeStringN: Resource is NULL";
+ return;
+ }
+
+ std::string key = env->GetStringUTFChars(jKey, NULL);
+ std::string value = env->GetStringUTFChars(jValue, NULL);
+
+ resource->addAttribute(key, value);
+}
+
+JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_updateAttributeInteger
+(JNIEnv *env, jobject jobject, jstring jKey, jint jValue)
+{
+ if (!jKey)
+ {
+ std::cout << "updateAttributeInteger: AttributeName is Empty";
+ return;
+ }
+
+ SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
+ if (nullptr == resource.get())
+ {
+ std::cout << "updateAttributeInteger: Resource is NULL";
+ return;
+ }
+
+ std::string str = env->GetStringUTFChars(jKey, NULL);
+ resource->updateAttribute(str, static_cast<int>(jValue));
+}
+
+JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_updateAttributeDouble
+(JNIEnv *env, jobject jobject, jstring jKey, jdouble jValue)
+{
+ if (!jKey)
+ {
+ std::cout << "updateAttributeDouble: AttributeName is Empty";
+ return;
+ }
+
+ SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
+ if (nullptr == resource.get())
+ {
+ std::cout << "updateAttributeDouble: Resource is NULL";
+ return;
+ }
+
+ std::string str = env->GetStringUTFChars(jKey, NULL);
+ resource->updateAttribute(str, static_cast<double>(jValue));
+}
+
+JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_updateAttributeBoolean
+(JNIEnv *env, jobject jobject, jstring jKey, jboolean jValue)
+{
+ if (!jKey)
+ {
+ std::cout << "updateAttributeBoolean: AttributeName is Empty";
+ return;
+ }
+
+ SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
+ if (nullptr == resource.get())
+ {
+ std::cout << "updateAttributeBoolean: Resource is NULL";
+ return;
+ }
+
+ std::string str = env->GetStringUTFChars(jKey, NULL);
+ resource->updateAttribute(str, static_cast<bool>(jValue));
+}
+
+JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_updateAttributeStringN
+(JNIEnv *env, jobject jobject, jstring jKey, jstring jValue)
+{
+ if (!jKey)
+ {
+ std::cout << "updateAttributeStringN: AttributeName is Empty";
+ return;
+ }
+
+ SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
+ if (nullptr == resource.get())
+ {
+ std::cout << "updateAttributeStringN: Resource is NULL";
+ return;
+ }
+
+ std::string key = env->GetStringUTFChars(jKey, NULL);
+ std::string value = env->GetStringUTFChars(jValue, NULL);
+
+ resource->updateAttribute(key, value);
+}
+
+JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_setAllowedValuesInteger
+(JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
+{
+ if (!jKey)
+ {
+ std::cout << "setAllowedValuesInteger: AttributeName is Empty";
+ return;
+ }
+
+ SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
+ if (nullptr == resource.get())
+ {
+ std::cout << "setAllowedValuesInteger: Resource is NULL";
+ return;
+ }
+
+ std::string str = env->GetStringUTFChars(jKey, NULL);
+ resource->setAllowedValues(str, convertIntegerVector(env, jAllowedValues));
+}
+
+JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_setAllowedValuesDouble
+(JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
+{
+ if (!jKey)
+ {
+ std::cout << "setAllowedValuesDouble: AttributeName is Empty";
+ return;
+ }
+
+ SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
+ if (nullptr == resource.get())
+ {
+ std::cout << "setAllowedValuesDouble: Resource is NULL";
+ return;
+ }
+
+ std::string str = env->GetStringUTFChars(jKey, NULL);
+ resource->setAllowedValues(str, convertDoubleVector(env, jAllowedValues));
+}
+
+JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_setAllowedValuesStringN
+(JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
+{
+ if (!jKey)
+ {
+ std::cout << "setAllowedValuesStringN: AttributeName is Empty";
+ return;
+ }
+
+ SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
+ if (nullptr == resource.get())
+ {
+ std::cout << "setAllowedValuesStringN: Resource is NULL";
+ return;
+ }
+
+ std::string str = env->GetStringUTFChars(jKey, NULL);
+ resource->setAllowedValues(str, convertStringVector(env, jAllowedValues));
+}
+
+JNIEXPORT jint JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_startResourceAutomation
+(JNIEnv *env, jobject object)
+{
+ SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
+ if (nullptr == resource.get())
+ {
+ return -1;
+ }
+
+ int automationId;
+ if (SIMULATOR_SUCCESS != resource->startUpdateAutomation(AutomationType::NORMAL, automationId))
+ return -1;
+
+ return automationId;
+}
+
+JNIEXPORT jint JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_startAttributeAutomation
+(JNIEnv *env, jobject object, jstring attrName)
+{
+ SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
+ if (nullptr == resource.get())
+ {
+ return -1;
+ }
+
+ const char *attrNamePtr = env->GetStringUTFChars(attrName, NULL);
+
+ int automationId = -1;
+ resource->startUpdateAutomation(AutomationType::NORMAL, automationId);
+
+ env->ReleaseStringUTFChars(attrName, attrNamePtr);
+ return automationId;
+}
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_startAutomation
+(JNIEnv *env, jobject object, jint automationId)
+{
+ SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, object);
+ if (nullptr == resource.get())
+ {
+ return;
+ }
+
+ resource->stopUpdateAutomation(automationId);
+}
+
+JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_removeAttribute
+(JNIEnv *env, jobject jobject, jstring jKey)
+{
+ if (!jKey)
+ {
+ std::cout << "removeAttribute: AttributeName is Empty";
+ return;
+ }
+
+ SimulatorResourcePtr resource = JniSimulatorResource::getJniSimulatorResourcePtr(env, jobject);
+ if (nullptr == resource.get())
+ {
+ std::cout << "removeAttribute: Resource is NULL";
+ return;
+ }
+
+ std::string str = env->GetStringUTFChars(jKey, NULL);
+ resource->removeAttribute(str);
+}
+
+JNIEXPORT void JNICALL Java_org_iotivity_simulator_SimulatorResourceServer_dispose
+(JNIEnv *env, jobject thiz)
+{
+ JniSimulatorResource *resource = GetHandle<JniSimulatorResource>(env, thiz);
+ delete resource;
+}
+
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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 SIMULATOR_RESOURCE_JNI_H_
+#define SIMULATOR_RESOURCE_JNI_H_
+
+#include <jni.h>
+#include "simulator_resource.h"
+
+class JniSimulatorResource
+{
+ public:
+ JniSimulatorResource(SimulatorResourcePtr &resource);
+
+ static jobject toJava(JNIEnv *env, jlong resource);
+ static void setUri(JNIEnv *env, jobject jobj, const std::string &uri);
+ static void setResourceType(JNIEnv *env, jobject jobj, const std::string &resourceType);
+ static void setResourceName(JNIEnv *env, jobject jobj, const std::string &name);
+ static void setInterfaceType(JNIEnv *env, jobject jobject, const std::string &interfaceType);
+ static SimulatorResourcePtr getJniSimulatorResourcePtr(JNIEnv *env, jobject thiz);
+ private:
+ SimulatorResourcePtr m_sharedResource;
+};
+
+
+#ifdef __cplusplus
+extern "C" {
+
+JNIEXPORT jobject JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_getModel
+(JNIEnv *, jobject);
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_updateAttributeFromAllowedValues
+(JNIEnv *, jobject, jstring, jint);
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_setRange
+(JNIEnv *, jobject, jstring, jint, jint);
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_addAttributeInteger
+(JNIEnv *, jobject, jstring, jint);
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_addAttributeDouble
+(JNIEnv *, jobject, jstring, jdouble);
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_addAttributeBoolean
+(JNIEnv *, jobject, jstring, jboolean);
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_addAttributeStringN
+(JNIEnv *, jobject, jstring, jstring);
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_updateAttributeInteger
+(JNIEnv *, jobject, jstring, jint);
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_updateAttributeDouble
+(JNIEnv *, jobject, jstring, jdouble);
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_updateAttributeBoolean
+(JNIEnv *, jobject, jstring, jboolean);
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_updateAttributeStringN
+(JNIEnv *, jobject, jstring, jstring);
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_setAllowedValuesInteger
+(JNIEnv *, jobject, jstring, jobject);
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_setAllowedValuesDouble
+(JNIEnv *, jobject, jstring, jobject);
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_setAllowedValuesStringN
+(JNIEnv *, jobject, jstring, jobject);
+
+JNIEXPORT jint JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_startResourceAutomation
+(JNIEnv *, jobject);
+
+JNIEXPORT jint JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_startAttributeAutomation
+(JNIEnv *, jobject, jstring);
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_startAutomation
+(JNIEnv *, jobject, jint);
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_removeAttribute
+(JNIEnv *, jobject, jstring);
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceServer_dispose
+(JNIEnv *, jobject);
+
+}
+#endif
+#endif //SIMULATOR_RESOURCE_JNI_H_
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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 "simulator_resource_jni_util.h"
+
+std::vector<int> convertIntegerVector(JNIEnv *env, jobject jVectorInt)
+{
+ std::vector<int> vectorInt;
+
+ jclass vectorClass = env->FindClass("java/util/Vector");
+ if (!vectorClass)
+ {
+ return vectorInt;
+ }
+
+ jmethodID size = env->GetMethodID(vectorClass, "size", "()I");
+ if (NULL == size)
+ {
+ return vectorInt;
+ }
+
+ jmethodID get = env->GetMethodID(vectorClass, "get", "(I)I");
+ if (NULL == get)
+ {
+ return vectorInt;
+ }
+
+ jint jSize = env->CallIntMethod(jVectorInt, size);
+ int sizeOfVector = jSize;
+
+ for (int index = 0; index < sizeOfVector; index++)
+ {
+ jint jIndex = index;
+ jint jValue = env->CallIntMethod(jVectorInt, get, jIndex);
+ vectorInt.push_back((int)jValue);
+ }
+
+ return vectorInt;
+}
+
+std::vector<double> convertDoubleVector(JNIEnv *env, jobject jVectorDouble)
+{
+ std::vector<double> vectorDouble;
+
+ jclass vectorClass = env->FindClass("java/util/Vector");
+ if (!vectorClass)
+ {
+ return vectorDouble;
+ }
+
+ jmethodID size = env->GetMethodID(vectorClass, "size", "()I");
+ if (NULL == size)
+ {
+ return vectorDouble;
+ }
+
+ jmethodID get = env->GetMethodID(vectorClass, "get", "(I)D");
+ if (NULL == get)
+ {
+ return vectorDouble;
+ }
+
+ jint jSize = env->CallIntMethod(jVectorDouble, size);
+ int sizeOfVector = jSize;
+
+ for (int index = 0; index < sizeOfVector; index++)
+ {
+ jint jIndex = index;
+ jdouble jValue = env->CallDoubleMethod(jVectorDouble, get, jIndex);
+ vectorDouble.push_back((double)jValue);
+ }
+
+ return vectorDouble;
+}
+
+std::vector<std::string> convertStringVector(JNIEnv *env, jobject jVectorString)
+{
+ std::vector<std::string> vectorString;
+
+ jclass vectorClass = env->FindClass("java/util/Vector");
+ if (!vectorClass)
+ {
+ return vectorString;
+ }
+
+ jmethodID size = env->GetMethodID(vectorClass, "size", "()I");
+ if (NULL == size)
+ {
+ return vectorString;
+ }
+
+ jmethodID get = env->GetMethodID(vectorClass, "get", "(I)""Ljava/lang/Object;");
+ if (NULL == get)
+ {
+ return vectorString;
+ }
+
+ jint jSize = env->CallIntMethod(jVectorString, size);
+ int sizeOfVector = jSize;
+
+ for (int index = 0; index < sizeOfVector; index++)
+ {
+ jint jIndex = index;
+ jstring jContactInfoObj = (jstring)env->CallObjectMethod(jVectorString, get, jIndex);
+ if (jContactInfoObj == NULL)
+ {
+ return vectorString;
+ }
+ const char *buff = env->GetStringUTFChars(jContactInfoObj, 0);
+ if (NULL != buff)
+ {
+ std::string tempString = buff;
+ vectorString.push_back(tempString);
+ }
+
+ env->ReleaseStringUTFChars(jContactInfoObj, buff);
+ }
+
+ return vectorString;
+}
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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.
+ *
+ ******************************************************************/
+
+/**
+ * @file simulator_resource_jni_util.h
+ *
+ * @brief This file contains the utility functions for conversions from java to CPP
+ * and viceversa
+ */
+
+#ifndef __SIMULATOR_RESOURCE_JNI_UTIL_H_
+#define __SIMULATOR_RESOURCE_JNI_UTIL_H_
+
+#include <jni.h>
+#include <iostream>
+#include <vector>
+#include <map>
+
+/**
+ * Utility function for converting a Java Vector of Strings to CPP Vector of Strings
+ *
+ * @param env - Default JNI Environment pointer
+ * @param jVectorString - Java Vector of Strings
+ *
+ * @return void
+ */
+std::vector<int> convertIntegerVector(JNIEnv *env, jobject jVectorInt);
+
+/**
+ * Utility function for converting a Java Vector of Strings to CPP Vector of Strings
+ *
+ * @param env - Default JNI Environment pointer
+ * @param jVectorString - Java Vector of Strings
+ *
+ * @return void
+ */
+std::vector<double> convertDoubleVector(JNIEnv *env, jobject jVectorDouble);
+
+/**
+ * Utility function for converting a Java Vector of Strings to CPP Vector of Strings
+ *
+ * @param env - Default JNI Environment pointer
+ * @param jVectorString - Java Vector of Strings
+ *
+ * @return void
+ */
+std::vector<std::string> convertStringVector(JNIEnv *env, jobject jVectorString);
+
+#endif //__SIMULATOR_RESOURCE_JNI_UTIL_H_
+
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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 "simulator_resource_model_jni.h"
+#include "simulator_common_jni.h"
+#include "simulator_resource_attributes_jni.h"
+#include "simulator_error_codes.h"
+
+using namespace std;
+
+extern SimulatorClassRefs gSimulatorClassRefs;
+
+JniSimulatorResourceModel::JniSimulatorResourceModel(SimulatorResourceModel resourceModel)
+ : m_resourceModel(resourceModel)
+{}
+
+bool JniSimulatorResourceModel::getResourceModel(JNIEnv *env, jobject thiz,
+ SimulatorResourceModel &resourceModel)
+{
+ JniSimulatorResourceModel *resource = GetHandle<JniSimulatorResourceModel>(env, thiz);
+ if (env->ExceptionCheck())
+ {
+ cout << "Exception while converting the nativeHandle to JniSimulatorResourceModel" << endl;
+ return false;
+ }
+ resourceModel = resource->m_resourceModel;
+ return true;
+}
+
+jobject JniSimulatorResourceModel::toJava(JNIEnv *env, jlong resource)
+{
+ jobject resourceObj = (jobject) env->NewObject(gSimulatorClassRefs.classSimulatorResourceModel,
+ gSimulatorClassRefs.classSimulatorResourceModelCtor, resource);
+ if (!resourceObj)
+ {
+ return NULL;
+ }
+ return resourceObj;
+}
+
+static jobject createHashMap(JNIEnv *env)
+{
+ jobject mapobj = env->NewObject(gSimulatorClassRefs.classHashMap,
+ gSimulatorClassRefs.classHashMapCtor);
+ return mapobj;
+}
+
+static void addEntryToHashMap(JNIEnv *env, jobject mapobj, jobject key, jobject value)
+{
+ if (!mapobj || !key || !value)
+ {
+ return;
+ }
+
+ env->CallObjectMethod(mapobj, gSimulatorClassRefs.classHashMapPut, key, value);
+}
+
+JNIEXPORT jint JNICALL
+Java_org_iotivity_simulator_SimulatorResourceModel_size
+(JNIEnv *env, jobject thiz)
+{
+ SimulatorResourceModel resourceModel;
+ bool result = JniSimulatorResourceModel::getResourceModel(env, thiz, resourceModel);
+ if (!result)
+ {
+ return SIMULATOR_ERROR;
+ }
+
+ return resourceModel.size();
+}
+
+JNIEXPORT jobject JNICALL
+Java_org_iotivity_simulator_SimulatorResourceModel_getAttributes
+(JNIEnv *env, jobject thiz)
+{
+ SimulatorResourceModel resourceModel;
+ bool result = JniSimulatorResourceModel::getResourceModel(env, thiz, resourceModel);
+ if (!result)
+ {
+ return NULL;
+ }
+
+ map<string, SimulatorResourceModel::Attribute> attributesMap = resourceModel.getAttributes();
+
+ // Create Java HashMap object
+ jobject jHashMap = NULL;
+ jHashMap = createHashMap(env);
+ if (!jHashMap)
+ {
+ return NULL;
+ }
+
+ for (auto & attributeEntry : attributesMap)
+ {
+
+ // Create JniSimulatorResourceAttribute object and put the attribute.second into it
+ SimulatorResourceModel::Attribute *attribute = new SimulatorResourceModel::Attribute(
+ attributeEntry.second);
+
+ // Create a java object for SimulatorResourceAttribute
+ jobject jAttribute = JniSimulatorResourceAttribute::toJava(env, reinterpret_cast<jlong>(attribute));
+
+ // Add an entry with attribute.first and javaSimualatorResourceAttribute to the HashMap
+ jstring jAttrName = env->NewStringUTF((attributeEntry.first).c_str());
+ addEntryToHashMap(env, jHashMap, static_cast<jobject>(jAttrName), jAttribute);
+ env->DeleteLocalRef(jAttrName);
+ }
+ return jHashMap;
+}
+
+JNIEXPORT jobject JNICALL
+Java_org_iotivity_simulator_SimulatorResourceModel_getAttribute
+(JNIEnv *env, jobject thiz, jstring jAttrName)
+{
+ if (!jAttrName)
+ {
+ std::cout << "getAttribute: AttributeName is Empty";
+ return NULL;
+ }
+
+ const char *attrName = env->GetStringUTFChars(jAttrName, NULL);
+ if (!attrName)
+ {
+ std::cout << "getAttribute: Failed to convert jstring to char string!";
+ return NULL;
+ }
+
+ SimulatorResourceModel resourceModel;
+ bool result = JniSimulatorResourceModel::getResourceModel(env, thiz, resourceModel);
+ if (!result)
+ {
+ std::cout << "getAttribute: getResourceModel failed!";
+ env->ReleaseStringUTFChars(jAttrName, attrName);
+ return NULL;
+ }
+
+ SimulatorResourceModel::Attribute *attribute = new SimulatorResourceModel::Attribute();
+ bool found = resourceModel.getAttribute(attrName, *attribute);
+ if (!found)
+ {
+ std::cout << "getAttribute: Attribute not found in ResourceModel!";
+ env->ReleaseStringUTFChars(jAttrName, attrName);
+ delete attribute;
+ return NULL;
+ }
+
+ env->ReleaseStringUTFChars(jAttrName, attrName);
+
+ // Create a java object for SimulatorResourceAttribute
+ jobject jsimulatorResourceAttribute = JniSimulatorResourceAttribute::toJava(env,
+ reinterpret_cast<jlong>(attribute));
+ return jsimulatorResourceAttribute;
+}
+
+JNIEXPORT jobjectArray JNICALL
+Java_org_iotivity_simulator_SimulatorResourceModel_getAllowedValues
+(JNIEnv *env, jobject thiz, jstring jAttrName)
+{
+ if (!jAttrName)
+ {
+ std::cout << "getAllowedValues: AttributeName is Empty";
+ return NULL;
+ }
+
+ const char *attrName = env->GetStringUTFChars(jAttrName, NULL);
+ if (!attrName)
+ {
+ std::cout << "getAllowedValues: Failed to convert jstring to char string!";
+ env->ReleaseStringUTFChars(jAttrName, attrName);
+ return NULL;
+ }
+
+ SimulatorResourceModel resourceModel;
+ bool result = JniSimulatorResourceModel::getResourceModel(env, thiz, resourceModel);
+ if (!result)
+ {
+ std::cout << "getAllowedValues: getResourceModel failed!";
+ env->ReleaseStringUTFChars(jAttrName, attrName);
+ return NULL;
+ }
+
+ SimulatorResourceModel::Attribute *attribute = new SimulatorResourceModel::Attribute();
+ bool found = resourceModel.getAttribute(attrName, *attribute);
+ if (!found)
+ {
+ std::cout << "getAllowedValues: Attribute not found in ResourceModel!";
+ env->ReleaseStringUTFChars(jAttrName, attrName);
+ delete attribute;
+ return NULL;
+ }
+
+ env->ReleaseStringUTFChars(jAttrName, attrName);
+
+ std::vector<std::string> values = attribute->allowedValuesToVectorString();
+
+ int size = attribute->getAllowedValuesSize();
+
+ // Create a jObjectArray for AllowedValues vector.
+ jobjectArray allowedValuesArr = env->NewObjectArray(size, env->FindClass("java/lang/String"),
+ env->NewStringUTF(""));
+
+ int i = 0;
+ for (std::vector<std::string>::iterator it = values.begin(); it != values.end(); ++it, i++)
+ {
+ env->SetObjectArrayElement(allowedValuesArr, i, env->NewStringUTF((*it).c_str()));
+ }
+ return allowedValuesArr;
+}
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceModel_dispose
+(JNIEnv *env, jobject thiz)
+{
+ JniSimulatorResourceModel *resourceModel = GetHandle<JniSimulatorResourceModel>(env, thiz);
+ delete resourceModel;
+}
+
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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 SIMULATOR_RESOURCE_MODEL_JNI_H_
+#define SIMULATOR_RESOURCE_MODEL_JNI_H_
+
+#include <jni.h>
+#include "simulator_resource_model.h"
+
+class JniSimulatorResourceModel
+{
+
+ public:
+ JniSimulatorResourceModel(SimulatorResourceModel);
+
+ static jobject toJava(JNIEnv *, jlong);
+ static bool getResourceModel(JNIEnv *env, jobject thiz, SimulatorResourceModel &resourceModel);
+ private:
+ SimulatorResourceModel m_resourceModel;
+};
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+JNIEXPORT jint JNICALL
+Java_org_iotivity_simulator_SimulatorResourceModel_size
+(JNIEnv *, jobject);
+
+JNIEXPORT jobject JNICALL
+Java_org_iotivity_simulator_SimulatorResourceModel_getAttributes
+(JNIEnv *, jobject);
+
+JNIEXPORT jobject JNICALL
+Java_org_iotivity_simulator_SimulatorResourceModel_getAttribute
+(JNIEnv *, jobject, jstring);
+
+JNIEXPORT jobjectArray JNICALL
+Java_org_iotivity_simulator_SimulatorResourceModel_getAllowedValues
+(JNIEnv *, jobject, jstring);
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_simulator_SimulatorResourceModel_dispose
+(JNIEnv *, jobject);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>ServiceProvider</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
--- /dev/null
+/*
+ * Copyright 2015 Samsung Electronics 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.
+ */
+
+package org.iotivity.simulator;
+
+public interface ILogMessageListener {
+ public void onLogMessageHandler(int level, String tag, String location,
+ int lineNumber, String log);
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright 2015 Samsung Electronics 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.
+ */
+
+package org.iotivity.simulator;
+
+import java.util.Vector;
+
+public interface IResourceModelChangedListener {
+ public void onResourceModelChanged(String resourceURI,
+ Vector<SimulatorResourceAttribute> attributes);
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright 2015 Samsung Electronics 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.
+ */
+
+package org.iotivity.simulator;
+
+/**
+ * This Enum contains Status codes for Success and Errors
+ */
+public enum OCSimulatorResult {
+ OC_STACK_OK,
+ OC_STACK_RESOURCE_CREATED,
+ OC_STACK_RESOURCE_DELETED,
+ OC_STACK_CONTINUE,
+ OC_STACK_INVALID_URI,
+ OC_STACK_INVALID_QUERY,
+ OC_STACK_INVALID_IP,
+ OC_STACK_INVALID_PORT,
+ OC_STACK_INVALID_CALLBACK,
+ OC_STACK_INVALID_METHOD,
+ OC_STACK_INVALID_PARAM,
+ OC_STACK_INVALID_OBSERVE_PARAM,
+ OC_STACK_NO_MEMORY,
+ OC_STACK_COMM_ERROR,
+ OC_STACK_NOTIMPL,
+ OC_STACK_NO_RESOURCE,
+ OC_STACK_RESOURCE_ERROR,
+ OC_STACK_SLOW_RESOURCE,
+ OC_STACK_REPEATED_REQUEST,
+ OC_STACK_NO_OBSERVERS,
+ OC_STACK_OBSERVER_NOT_FOUND,
+ OC_STACK_VIRTUAL_DO_NOT_HANDLE,
+ OC_STACK_INVALID_OPTION,
+ OC_STACK_MALFORMED_RESPONSE,
+ OC_STACK_PERSISTENT_BUFFER_REQUIRED,
+ OC_STACK_INVALID_REQUEST_HANDLE,
+ OC_STACK_INVALID_DEVICE_INFO,
+ OC_STACK_INVALID_JSON,
+ OC_STACK_PRESENCE_STOPPED,
+ OC_STACK_PRESENCE_TIMEOUT,
+ OC_STACK_PRESENCE_DO_NOT_HANDLE,
+ OC_STACK_ERROR,
+ OC_STACK_LISTENER_NOT_SET;
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright 2015 Samsung Electronics 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.
+ */
+
+package org.iotivity.simulator;
+
+import java.util.Vector;
+
+public class SimulatorManager {
+
+ static {
+ // TODO: Load all native libraries here
+ }
+
+ public static void setLogMessageHandler(ILogMessageListener listener) {
+ SimulatorManagerCallback.getInstance().registerLogMessageListener(
+ listener);
+ }
+
+ public static void setResourceModelChangeHandler(
+ IResourceModelChangedListener listener) {
+ SimulatorManagerCallback.getInstance()
+ .registerResourceModelChangeListener(listener);
+ }
+
+ public static void initialize() {
+ SimulatorManagerNativeInterface.initialize();
+ }
+
+ public static void terminate() {
+ SimulatorManagerNativeInterface.terminate();
+
+ // Unregister the listeners
+ SimulatorManagerCallback.getInstance().unregisterLogMessageListener();
+ SimulatorManagerCallback.getInstance()
+ .unregisterResourceModelChangeListener();
+ }
+
+ public static SimulatorResourceServer createResource(String configPath) {
+ SimulatorResourceServer simulatorResourceServerObj;
+ simulatorResourceServerObj = SimulatorManagerNativeInterface
+ .createResource(configPath);
+ return simulatorResourceServerObj;
+ }
+
+ public static Vector<SimulatorResourceServer> createResource(
+ String configPath, int count) {
+ Vector<SimulatorResourceServer> simulatorResourceServerVector;
+ simulatorResourceServerVector = SimulatorManagerNativeInterface
+ .createResources(configPath, count);
+ return simulatorResourceServerVector;
+ }
+
+ public static Vector<SimulatorResourceServer> getLocalResources() {
+ Vector<SimulatorResourceServer> simulatorResourceServerVector = null;
+ simulatorResourceServerVector = SimulatorManagerNativeInterface
+ .getResources();
+ return simulatorResourceServerVector;
+ }
+
+ public static void deleteResource(SimulatorResourceServer resource) {
+ SimulatorManagerNativeInterface.deleteResource(resource);
+ }
+
+ public static void deleteAllResources(String resourceType) {
+ SimulatorManagerNativeInterface.deleteAllResources(resourceType);
+ }
+
+ public static void deleteAllResources() {
+ SimulatorManagerNativeInterface.deleteAllResources();
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright 2015 Samsung Electronics 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.
+ */
+
+package org.iotivity.simulator;
+
+import java.util.Vector;
+
+class SimulatorManagerCallback {
+
+ private IResourceModelChangedListener resourceModelChangeListener;
+ private ILogMessageListener logMessageListener;
+ private static SimulatorManagerCallback simulatorManagerCallbackObj;
+
+ private SimulatorManagerCallback() {
+ }
+
+ public static synchronized SimulatorManagerCallback getInstance() {
+ if (null == simulatorManagerCallbackObj) {
+ simulatorManagerCallbackObj = new SimulatorManagerCallback();
+ }
+ return simulatorManagerCallbackObj;
+ }
+
+ public void registerResourceModelChangeListener(
+ IResourceModelChangedListener listener) {
+ resourceModelChangeListener = listener;
+ }
+
+ public void registerLogMessageListener(ILogMessageListener listener) {
+ logMessageListener = listener;
+ }
+
+ public void unregisterResourceModelChangeListener() {
+ resourceModelChangeListener = null;
+ }
+
+ public void unregisterLogMessageListener() {
+ logMessageListener = null;
+ }
+
+ public void onResourceModelChanged(String resourceURI,
+ Vector<SimulatorResourceAttribute> attributes) {
+ if (null != resourceModelChangeListener) {
+ resourceModelChangeListener.onResourceModelChanged(resourceURI,
+ attributes);
+ }
+ }
+
+ public void onLogMessageHandler(int level, String tag, String location,
+ int lineNumber, String log) {
+ if (null != logMessageListener) {
+ logMessageListener.onLogMessageHandler(level, tag, location,
+ lineNumber, log);
+ }
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright 2015 Samsung Electronics 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.
+ */
+
+package org.iotivity.simulator;
+
+import java.util.Vector;
+
+class SimulatorManagerNativeInterface {
+
+ public static native void initialize();
+
+ public static native void terminate();
+
+ public static native SimulatorResourceServer createResource(
+ String configPath);
+
+ public static native Vector<SimulatorResourceServer> createResources(
+ String configPath, int count);
+
+ public static native Vector<SimulatorResourceServer> getResources();
+
+ public static native void deleteResource(SimulatorResourceServer resource);
+
+ public static native void deleteAllResources(String resourceType);
+
+ public static native void deleteAllResources();
+
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright 2015 Samsung Electronics 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.
+ */
+
+package org.iotivity.simulator;
+
+public class SimulatorResourceAttribute {
+
+ private long nativeHandle;
+
+ public SimulatorResourceAttribute() {
+ }
+
+ public SimulatorResourceAttribute(String attrName) {
+ create(attrName);
+ }
+
+ private SimulatorResourceAttribute(long handle) {
+ nativeHandle = handle;
+ }
+
+ @Override
+ protected void finalize() throws Throwable {
+ dispose();
+ }
+
+ public <T> T getAttributeValue() {
+ Object obj = getValue();
+ @SuppressWarnings("unchecked")
+ T t = (T) obj;
+ return t;
+ }
+
+ private native void dispose();
+
+ public native void create(String attrName);
+
+ public native String getName();
+
+ public native Object getValue();
+
+ public native int allowedValuesSize();
+
+ public native String valueToString();
+
+ public native String allowedValuesToString();
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright 2015 Samsung Electronics 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.
+ */
+
+package org.iotivity.simulator;
+
+import java.util.Map;
+
+public class SimulatorResourceModel {
+
+ private long nativeHandle;
+
+ private SimulatorResourceModel(long nativeHandle) {
+ this.nativeHandle = nativeHandle;
+ }
+
+ @Override
+ protected void finalize() throws Throwable {
+ dispose();
+ }
+
+ public native int size();
+
+ public native Map<String, SimulatorResourceAttribute> getAttributes();
+
+ public native SimulatorResourceAttribute getAttribute();
+
+ public native String[] getAllowedValues(String key);
+
+ public native void dispose();
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright 2015 Samsung Electronics 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.
+ */
+
+package org.iotivity.simulator;
+
+import java.util.Vector;
+
+public class SimulatorResourceServer {
+
+ private String resourceName;
+ private String resourceURI;
+ private String resourceType;
+ private String interfaceType;
+
+ private long nativeHandle;
+
+ private SimulatorResourceServer(long nativeHandle) {
+ this.nativeHandle = nativeHandle;
+ }
+
+ public void setName(String resourceName) {
+ this.resourceName = resourceName;
+ }
+
+ private void setURI(String resourceURI) {
+ this.resourceURI = resourceURI;
+ }
+
+ private void setResourceType(String resourceType) {
+ this.resourceType = resourceType;
+ }
+
+ private void setInterfaceType(String interfaceType) {
+ this.interfaceType = interfaceType;
+ }
+
+ public String getName() {
+ return resourceName;
+ }
+
+ public String getURI() {
+ return resourceURI;
+ }
+
+ public String getResourceType() {
+ return resourceType;
+ }
+
+ public String getInterfaceType() {
+ return interfaceType;
+ }
+
+ @Override
+ protected void finalize() throws Throwable {
+ dispose();
+ }
+
+ public native SimulatorResourceModel getModel();
+
+ public native void updateAttributeFromAllowedValues(String attrName,
+ int index);
+
+ public native void setRange(String attrName, int min, int max);
+
+ public native void addAttributeInteger(String key, int value);
+
+ public native void addAttributeDouble(String key, double value);
+
+ public native void addAttributeBoolean(String key, Boolean value);
+
+ public native void addAttributeStringN(String key, String value);
+
+ public native void updateAttributeInteger(String key, int value);
+
+ public native void updateAttributeDouble(String key, double value);
+
+ public native void updateAttributeBoolean(String key, Boolean value);
+
+ public native void updateAttributeStringN(String key, String value);
+
+ public native void setAllowedValuesInteger(String key, int[] allowedValues);
+
+ public native void setAllowedValuesDouble(String key, double[] allowedValues);
+
+ public native void setAllowedValuesStringN(String key,
+ String[] allowedValues);
+
+ public native int startResourceAutomation();
+
+ public native int startAttributeAutomation(String attrName);
+
+ public native void startAutomation(int automationId);
+
+ public native void removeAttribute(String key);
+
+ private native void dispose();
+}
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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 "resource_manager.h"
+
+int ResourceManager::id;
+ResourceManager *ResourceManager::getInstance()
+{
+ static ResourceManager s_instance;
+ return &s_instance;
+}
+
+ResourceManager::ResourceManager()
+ : m_resourceCreator(new SimulatorResourceCreator()) {}
+
+ResourceManager::~ResourceManager()
+{
+ delete m_resourceCreator;
+}
+
+SimulatorResourcePtr ResourceManager::createResource(const std::string &configPath,
+ SimulatorResource::ResourceModelChangedCB callback)
+{
+ /**
+ * TODO: Temporarily creating the light resource for testing the basic flow
+ * Once the config parser is included this method will simulate the resource based on the config file
+ */
+ SimulatorResourcePtr simulatorResource = m_resourceCreator->createLightResoure();
+ simulatorResource->setModelChangeCallback(callback);
+
+ simulatorResource->setURI(getURI(simulatorResource->getURI()));
+ OC::EntityHandler entityHandler = std::bind(&SimulatorResource::entityHandler,
+ simulatorResource.get(), std::placeholders::_1);
+ std::string uri = simulatorResource->getURI();
+ OCStackResult result = OC::OCPlatform::registerResource(simulatorResource->m_resourceHandle,
+ uri,
+ simulatorResource->getResourceType(),
+ simulatorResource->getInterfaceType(),
+ entityHandler,
+ OC_DISCOVERABLE | OC_OBSERVABLE);
+ if (OC_STACK_OK != result)
+ {
+ return NULL;
+ }
+
+ // Add the resource to resource list table
+ m_resourceList[simulatorResource->getResourceType()].insert(
+ std::pair<std::string, SimulatorResourcePtr>(simulatorResource->getURI(), simulatorResource));
+ return simulatorResource;
+}
+
+std::vector<SimulatorResourcePtr> ResourceManager::createResource(const std::string &configPath,
+ const int count, SimulatorResource::ResourceModelChangedCB callback)
+{
+ std::vector<SimulatorResourcePtr> list;
+ return list;
+}
+
+std::vector<SimulatorResourcePtr> ResourceManager::getResources(void) const
+{
+ std::vector<SimulatorResourcePtr> list;
+ return list;
+}
+
+SimulatorResult ResourceManager::deleteResource(SimulatorResourcePtr &resource)
+{
+ if (!resource.get())
+ return SIMULATOR_RESOURCE_NOT_FOUND;
+
+ auto resourceTableEntry = m_resourceList.find(resource->getResourceType());
+ if (m_resourceList.end() != resourceTableEntry)
+ {
+ auto resourceEntry = resourceTableEntry->second.find(resource->getURI());
+ if (resourceTableEntry->second.end() != resourceEntry)
+ {
+ if (OC_STACK_OK == OC::OCPlatform::unregisterResource(resource->getHandle()))
+ resourceTableEntry->second.erase(resourceEntry);
+ }
+ }
+
+ return SIMULATOR_SUCCESS;
+}
+
+SimulatorResult ResourceManager::deleteResources(const std::string &resourceType)
+{
+ auto resourceTableEntry = m_resourceList.find(resourceType);
+ if (m_resourceList.end() != resourceTableEntry)
+ {
+ return SIMULATOR_RESOURCE_NOT_FOUND;
+ }
+
+ for (auto resourceEntry : resourceTableEntry->second)
+ {
+ SimulatorResourcePtr resource = resourceEntry.second;
+ if (OC_STACK_OK == OC::OCPlatform::unregisterResource(resource->getHandle()))
+ resourceTableEntry->second.erase(resourceTableEntry->second.find(resource->getURI()));
+ }
+
+ return SIMULATOR_SUCCESS;
+}
+
+std::string ResourceManager::getURI(std::string uri)
+{
+ std::ostringstream os;
+ os << uri;
+ if (!uri.empty() && '/' != uri[uri.length() - 1])
+ os << '/';
+ os << "simulator/" << id++;
+ return os.str();
+}
+
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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.
+ *
+ ******************************************************************/
+
+/**
+ * @file resource_manager.h
+ *
+ * @brief This file provides a class for a set of APIs relating to resource management
+ * in the Service Provider side.
+ */
+
+#ifndef RESOURCE_MANAGER_H_
+#define RESOURCE_MANAGER_H_
+
+#include <string>
+#include <vector>
+#include <map>
+#include "simulator_resource.h"
+#include "simulator_resource_creator.h"
+#include "simulator_error_codes.h"
+
+/**
+ * @class ResourceManager
+ * @brief This class provides a set of functions for managing the resource(s) in the Service Provider Module.
+ */
+class ResourceManager
+{
+ public:
+ /**
+ * This method is used to create/obtain the singleton instance of ResourceManager.
+ *
+ * @return ResourceManager - Singleton instance of ResourceManager.
+ */
+ static ResourceManager *getInstance(void);
+
+ /**
+ * This method is called for creating a single resource from the configuration file.
+ *
+ * @param configPath - RAML configuration file path.
+ * @param callback - Callback method for receive notifications when resource model changes.
+ *
+ * @return SimulatorResourcePtr - Shared pointer of SimulatorResource on success, otherwise NULL.
+ */
+ SimulatorResourcePtr createResource(const std::string &configPath,
+ SimulatorResource::ResourceModelChangedCB callback);
+
+ /**
+ * This method is called for creating a collection of resources from the configuration file.
+ *
+ * @param configPath - RAML configuration file path.
+ * @param count - Number of resource to be created.
+ * @param callback - Callback method for receive notifications when resource model changes.
+ *
+ * @return SimulatorResourcePtr - A vector of Shared pointers of SimulatorResource Objects.
+ */
+ std::vector<SimulatorResourcePtr> createResource(const std::string &configPath, const int count,
+ SimulatorResource::ResourceModelChangedCB callback);
+
+ /**
+ * This method is called for obtaining a list of created resources.
+ *
+ * @return SimulatorResourcePtr - A vector of Shared pointers of SimulatorResource Objects.
+ */
+ std::vector<SimulatorResourcePtr> getResources(void) const;
+
+ /**
+ * This method is called for deleting a single resource.
+ *
+ * @param resource - Shared pointer of the SimulatorResource to be deleted.
+ *
+ * @return SimulatorResult
+ */
+ SimulatorResult deleteResource(SimulatorResourcePtr &resource);
+
+ /**
+ * This method is called for deleting multiple resources.
+ * If this method is called without any parameter, then all resources will be deleted.
+ * If thie method is called with a specific resourcetype as a parameter, then all the resources
+ * of that particular type will be deleted.
+ *
+ * @param resourceType - Resource type of the resource
+ *
+ * @return SimulatorResult
+ */
+ SimulatorResult deleteResources(const std::string &resourceType = "");
+
+ private:
+ /**
+ * This method is called for obtaining a unique URI when creating multiple resources using createResource API.
+ * It appends a unique key to the given URI.
+ * Example: If input is "/a/light", then the output will be "/a/light/simulator/0" for the first resource
+ * and "/a/light/simulator/1" for the second resource and so on.
+ *
+ * @param uri - URI of the resource
+ *
+ * @return Unique URI for the resource
+ */
+ std::string getURI(std::string uri);
+
+ ResourceManager();
+ ~ResourceManager();
+
+ SimulatorResourceCreator *m_resourceCreator;
+ static int id;
+
+ /**
+ * This multi-level map organizes the resources in the form of ResourceType as the key
+ * and a set of resources of that resourceType as the value.
+ * The value is another map which has the ResourceURI as the key and the shared pointer
+ * of the SimulatorResource object as the value.
+ */
+ std::map<std::string, std::map<std::string, SimulatorResourcePtr>> m_resourceList;
+};
+
+#endif
+
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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 "simulator_attribute_automation.h"
+#include "simulator_resource.h"
+#include <thread>
+
+#define SLEEP_FOR(X) if (X > 0) std::this_thread::sleep_for(std::chrono::milliseconds(X));
+
+AttributeUpdateAutomation::AttributeUpdateAutomation(
+ SimulatorResource *resource, const std::string &attrName, AutomationType type, int interval)
+ : m_resource(resource),
+ m_attrName(attrName),
+ m_type(type),
+ m_status(false),
+ m_stopRequested(false),
+ m_updateInterval(interval) {}
+
+SimulatorResult AttributeUpdateAutomation::start()
+{
+ if (true == m_status)
+ return SIMULATOR_AUTOMATION_ALREADY_STARTED;
+
+ // Check the validity of attribute
+ SimulatorResourceModel resModel = m_resource->getModel();
+ if (false == resModel.getAttribute(m_attrName, m_attribute))
+ return SIMULATOR_ERROR;
+
+ if (m_updateInterval < 0)
+ {
+ m_updateInterval = m_attribute.getUpdateFrequencyTime();
+ if (0 > m_updateInterval)
+ m_updateInterval = 0;
+ }
+
+ m_thread = new std::thread(&AttributeUpdateAutomation::updateAttribute, this);
+ m_status = true;
+
+ return SIMULATOR_SUCCESS;
+}
+
+void AttributeUpdateAutomation::stop()
+{
+ m_stopRequested = true;
+ m_thread->join();
+ m_status = false;
+}
+
+void AttributeUpdateAutomation::updateAttribute()
+{
+ do
+ {
+ setAttributeValue();
+ if (m_stopRequested)
+ break;
+ }
+ while (AutomationType::RECURRENT == m_type);
+
+ m_status = false;
+}
+
+void AttributeUpdateAutomation::setAttributeValue()
+{
+ if (0 == m_attribute.getValueType()) // For integer type values
+ {
+ int min;
+ int max;
+ m_attribute.getRange(min, max);
+ for (int value = min; value <= max; value++)
+ {
+ m_resource->updateAttribute(m_attribute.getName(), value);
+ SLEEP_FOR(m_updateInterval);
+ }
+ }
+ else
+ {
+ for (int index = 0; index < m_attribute.getAllowedValuesSize(); index++)
+ {
+ m_resource->updateAttributeFromAllowedValues(m_attribute.getName(), index);
+ SLEEP_FOR(m_updateInterval);
+ }
+ }
+}
+
+
+ResourceUpdateAutomation::ResourceUpdateAutomation(
+ SimulatorResource *resource, AutomationType type, int interval)
+ : m_resource(resource),
+ m_type(type),
+ m_status(false),
+ m_updateInterval(interval) {}
+
+SimulatorResult ResourceUpdateAutomation::start()
+{
+ if (true == m_status)
+ return SIMULATOR_AUTOMATION_ALREADY_STARTED;
+
+ m_resModel = m_resource->getModel();
+ std::map<std::string, SimulatorResourceModel::Attribute> attributes = m_resModel.getAttributes();
+ if (0 == attributes.size())
+ {
+ m_status = false;
+ return SIMULATOR_ERROR;
+ }
+
+ for (auto & attribute : attributes)
+ {
+ AttributeUpdateAutomationPtr attributeAutomation = std::make_shared<AttributeUpdateAutomation>
+ (m_resource, attribute.first, m_type, m_updateInterval);
+ m_attrUpdationList.push_back(attributeAutomation);
+ if (SIMULATOR_SUCCESS != attributeAutomation->start())
+ {
+ m_status = false;
+ stop();
+ return SIMULATOR_ERROR;
+ }
+ }
+
+ m_status = true;
+ return SIMULATOR_SUCCESS;
+}
+
+void ResourceUpdateAutomation::stop()
+{
+ // Stop all the attributes updation
+ for (auto & attrAutomation : m_attrUpdationList)
+ {
+ attrAutomation->stop();
+ }
+
+ m_attrUpdationList.clear();
+ m_status = false;
+}
+
+UpdateAutomationManager::UpdateAutomationManager()
+ : m_automationId(0) {}
+
+SimulatorResult UpdateAutomationManager::startResourceAutomation(SimulatorResource *resource,
+ int &id, AutomationType type, int interval)
+{
+ ResourceUpdateAutomationPtr resoureceAutomation(new ResourceUpdateAutomation(resource, type,
+ interval));
+ SimulatorResult result = resoureceAutomation->start();
+ if (SIMULATOR_SUCCESS != result)
+ {
+ id = -1;
+ return result;
+ }
+
+ std::lock_guard<std::mutex> lock(m_mutex);
+ m_resourceUpdationList[m_automationId++] = resoureceAutomation;
+ id = m_automationId - 1;
+ return result;
+}
+
+SimulatorResult UpdateAutomationManager::startAttributeAutomation(SimulatorResource *resource,
+ const std::string &attrName, int &id, AutomationType type, int interval)
+{
+ AttributeUpdateAutomationPtr attributeAutomation(new AttributeUpdateAutomation(resource, attrName,
+ type, interval));
+ SimulatorResult result = attributeAutomation->start();
+ if (SIMULATOR_SUCCESS != result)
+ {
+ id = -1;
+ return result;
+ }
+
+ std::lock_guard<std::mutex> lock(m_mutex);
+ m_attrUpdationList[m_automationId++] = attributeAutomation;
+ id = m_automationId - 1;
+ return result;
+}
+
+void UpdateAutomationManager::stop(int automationId)
+{
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_resourceUpdationList.end() != m_resourceUpdationList.find(automationId))
+ {
+ m_resourceUpdationList[automationId]->stop();
+ m_resourceUpdationList.erase(m_resourceUpdationList.find(automationId));
+ }
+ else if (m_attrUpdationList.end() != m_attrUpdationList.find(automationId))
+ {
+ m_attrUpdationList[automationId]->stop();
+ m_attrUpdationList.erase(m_attrUpdationList.find(automationId));
+ }
+}
+
+void UpdateAutomationManager::stopAll()
+{
+ std::lock_guard<std::mutex> lock(m_mutex);
+ std::for_each(m_resourceUpdationList.begin(),
+ m_resourceUpdationList.end(), [] (std::pair<int, ResourceUpdateAutomationPtr> element)
+ {
+ element.second->stop();
+ });
+ m_resourceUpdationList.clear();
+
+ std::for_each(m_attrUpdationList.begin(),
+ m_attrUpdationList.end(), [] (std::pair<int, AttributeUpdateAutomationPtr> element)
+ {
+ element.second->stop();
+ });
+ m_attrUpdationList.clear();
+}
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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 SIMULATOR_RESOURCE_UPDATE_AUTOMATION_H_
+#define SIMULATOR_RESOURCE_UPDATE_AUTOMATION_H_
+
+#include "simulator_resource_model.h"
+#include "simulator_error_codes.h"
+
+class SimulatorResource;
+
+typedef enum class
+{
+ NORMAL,
+ RECURRENT
+} AutomationType;
+
+class AttributeUpdateAutomation
+{
+ public:
+ AttributeUpdateAutomation(SimulatorResource *resource,
+ const std::string &attrName, AutomationType type = AutomationType::NORMAL, int interval = -1);
+ SimulatorResult start();
+ void stop();
+
+ private:
+ void updateAttribute();
+ void setAttributeValue();
+
+ SimulatorResource *m_resource;
+ std::string m_attrName;
+ AutomationType m_type;
+ bool m_status;
+ std::thread *m_thread;
+ bool m_stopRequested;
+ int m_updateInterval;
+ SimulatorResourceModel::Attribute m_attribute;
+};
+
+typedef std::shared_ptr<AttributeUpdateAutomation> AttributeUpdateAutomationPtr;
+
+class ResourceUpdateAutomation
+{
+ public:
+ ResourceUpdateAutomation(SimulatorResource *resource,
+ AutomationType type = AutomationType::NORMAL, int interval = -1);
+ SimulatorResult start();
+ void stop();
+
+ private:
+ void updateAttribute();
+ void setAttributeValue();
+
+ SimulatorResource *m_resource;
+ AutomationType m_type;
+ bool m_status;
+ std::thread *m_thread;
+ int m_updateInterval;
+ SimulatorResourceModel m_resModel;
+ std::vector<AttributeUpdateAutomationPtr> m_attrUpdationList;
+};
+
+typedef std::shared_ptr<ResourceUpdateAutomation> ResourceUpdateAutomationPtr;
+
+class UpdateAutomationManager
+{
+ public:
+ UpdateAutomationManager();
+ SimulatorResult startResourceAutomation(SimulatorResource *resource,
+ int &id, AutomationType type = AutomationType::NORMAL, int interval = -1);
+ SimulatorResult startAttributeAutomation(SimulatorResource *resource,
+ const std::string &attrName, int &id, AutomationType type = AutomationType::NORMAL,
+ int interval = -1);
+ void stop(int automationId);
+ void stopAll();
+
+ private:
+ std::map<int, ResourceUpdateAutomationPtr> m_resourceUpdationList;
+ std::map<int, AttributeUpdateAutomationPtr> m_attrUpdationList;
+ int m_automationId;
+ std::mutex m_mutex;
+};
+
+typedef std::shared_ptr<UpdateAutomationManager> UpdateAutomationManagerPtr;
+
+#endif //SIMULATOR_RESOURCE_UPDATE_AUTOMATION_H_
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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 SIMULATOR_ERROR_CODES_H_
+#define SIMULATOR_ERROR_CODES_H_
+
+#include <iostream>
+
+typedef enum
+{
+ SIMULATOR_SUCCESS = 0,
+ SIMULATOR_BAD_INPUT,
+ SIMULATOR_RESOURCE_NOT_FOUND,
+ SIMULATOR_RESOURCE_BUSY,
+
+ // Attribute udpate automation related
+ SIMULATOR_AUTOMATION_ALREADY_STARTED,
+
+ SIMULATOR_ERROR = 255
+} SimulatorResult;
+#endif //SIMULATOR_ERROR_CODES_H_
\ No newline at end of file
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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 "simulator_manager.h"
+#include "resource_manager.h"
+
+using namespace OC;
+
+SimulatorManager *SimulatorManager::getInstance()
+{
+ static SimulatorManager s_instance;
+ return &s_instance;
+}
+
+SimulatorManager::SimulatorManager()
+{
+ OC::PlatformConfig conf
+ {
+ 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
+ };
+
+ OC::OCPlatform::Configure(conf);
+}
+
+SimulatorResourcePtr SimulatorManager::createResource(const std::string &configPath,
+ SimulatorResource::ResourceModelChangedCB callback)
+{
+ return ResourceManager::getInstance()->createResource(configPath, callback);
+}
+
+std::vector<SimulatorResourcePtr> SimulatorManager::createResource(const std::string &configPath,
+ const int count, SimulatorResource::ResourceModelChangedCB callback)
+{
+ return ResourceManager::getInstance()->createResource(configPath, count, callback);
+}
+
+std::vector<SimulatorResourcePtr> SimulatorManager::getResources(void)
+{
+ return ResourceManager::getInstance()->getResources();
+}
+
+SimulatorResult SimulatorManager::deleteResource(SimulatorResourcePtr &resource)
+{
+ return ResourceManager::getInstance()->deleteResource(resource);
+}
+
+SimulatorResult SimulatorManager::deleteResources(const std::string &resourceType)
+{
+ return ResourceManager::getInstance()->deleteResources(resourceType);
+}
\ No newline at end of file
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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 "simulator_resource.h"
+#include "simulator_attribute_automation.h"
+
+void SimulatorResource::setRange(const std::string &attrName, const int min, const int max)
+{
+ m_resModel.setRange(attrName, min, max);
+}
+
+void SimulatorResource::setUpdateInterval(const std::string &attrName, int interval)
+{
+ m_resModel.setUpdateInterval(attrName, interval);
+}
+
+void SimulatorResource::removeAttribute(const std::string &attrName)
+{
+ m_resModel.removeAttribute(attrName);
+ notifyListOfObservers();
+}
+
+void SimulatorResource::setURI(const std::string &uri)
+{
+ m_uri = uri;
+}
+
+std::string SimulatorResource::getURI() const
+{
+ return m_uri;
+}
+
+void SimulatorResource::setResourceType(const std::string &resourceType)
+{
+ m_resourceType = resourceType;
+}
+
+std::string SimulatorResource::getResourceType() const
+{
+ return m_resourceType;
+}
+
+void SimulatorResource::setInterfaceType(const std::string &interfaceType)
+{
+ m_interfaceType = interfaceType;
+}
+
+std::string SimulatorResource::getInterfaceType() const
+{
+ return m_interfaceType;
+}
+
+OCResourceHandle SimulatorResource::getHandle() const
+{
+ return m_resourceHandle;
+}
+
+void SimulatorResource::setName(const std::string &name)
+{
+ m_name = name;
+}
+
+std::string SimulatorResource::getName() const
+{
+ return m_name;
+}
+
+void SimulatorResource::updateAttributeFromAllowedValues(const std::string &attrName,
+ const int allowedValueIndex)
+{
+ m_resModel.updateAttributeFromAllowedValues(attrName, allowedValueIndex);
+}
+
+SimulatorResourceModel SimulatorResource::getModel() const
+{
+ return m_resModel;
+}
+
+void SimulatorResource::setModelChangeCallback(ResourceModelChangedCB callback)
+{
+ m_callback = callback;
+}
+
+SimulatorResult SimulatorResource::startUpdateAutomation(AutomationType type, int &id)
+{
+ return m_updateAutomationMgr.startResourceAutomation(this, id, type);
+}
+
+SimulatorResult SimulatorResource::startUpdateAutomation(const std::string &attrName,
+ AutomationType type, int &id)
+{
+ return m_updateAutomationMgr.startAttributeAutomation(this, attrName, id, type);
+}
+
+void SimulatorResource::stopUpdateAutomation(const int id)
+{
+ m_updateAutomationMgr.stop(id);
+}
+
+OC::OCRepresentation SimulatorResource::getOCRepresentation()
+{
+ return m_resModel.getOCRepresentation();
+}
+
+bool SimulatorResource::modifyResourceModel(OC::OCRepresentation &ocRep,
+ SimulatorResourceModel::UpdateType type)
+{
+ return m_resModel.update(ocRep, type);
+}
+
+OCEntityHandlerResult SimulatorResource::entityHandler(std::shared_ptr<OC::OCResourceRequest>
+ request)
+{
+ OCEntityHandlerResult errCode = OC_EH_ERROR;
+ if (!request)
+ {
+ return OC_EH_ERROR;
+ }
+
+ if (OC::RequestHandlerFlag::RequestFlag & request->getRequestHandlerFlag())
+ {
+ auto response = std::make_shared<OC::OCResourceResponse>();
+ response->setRequestHandle(request->getRequestHandle());
+ response->setResourceHandle(request->getResourceHandle());
+
+ if ("GET" == request->getRequestType())
+ {
+ response->setErrorCode(200);
+ response->setResponseResult(OC_EH_OK);
+ response->setResourceRepresentation(getOCRepresentation());
+
+ if (OC_STACK_OK == OC::OCPlatform::sendResponse(response))
+ {
+ errCode = OC_EH_OK;
+ }
+ }
+ else if ("PUT" == request->getRequestType())
+ {
+ OC::OCRepresentation rep = request->getResourceRepresentation();
+ if (true == modifyResourceModel(rep, SimulatorResourceModel::UpdateType::PUT))
+ {
+ response->setErrorCode(200);
+ response->setResponseResult(OC_EH_OK);
+ response->setResourceRepresentation(getOCRepresentation());
+ }
+ else
+ {
+ response->setErrorCode(400);
+ response->setResponseResult(OC_EH_ERROR);
+ }
+
+ if (OC_STACK_OK == OC::OCPlatform::sendResponse(response))
+ {
+ errCode = OC_EH_OK;
+ }
+ }
+ else if ("POST" == request->getRequestType())
+ {
+ OC::OCRepresentation rep = request->getResourceRepresentation();
+ if (true == modifyResourceModel(rep, SimulatorResourceModel::UpdateType::POST))
+ {
+ response->setErrorCode(200);
+ response->setResponseResult(OC_EH_OK);
+ response->setResourceRepresentation(getOCRepresentation());
+ }
+ else
+ {
+ response->setErrorCode(400);
+ response->setResponseResult(OC_EH_ERROR);
+ }
+
+ if (OC_STACK_OK == OC::OCPlatform::sendResponse(response))
+ {
+ errCode = OC_EH_OK;
+ }
+ }
+ else if ("DELETE" == request->getRequestType())
+ {
+ OC::OCRepresentation rep = request->getResourceRepresentation();
+ if (true == modifyResourceModel(rep, SimulatorResourceModel::UpdateType::DELETE))
+ {
+ response->setErrorCode(200);
+ response->setResponseResult(OC_EH_OK);
+ response->setResourceRepresentation(getOCRepresentation());
+ }
+ else
+ {
+ response->setErrorCode(400);
+ response->setResponseResult(OC_EH_ERROR);
+ }
+
+ if (OC_STACK_OK == OC::OCPlatform::sendResponse(response))
+ {
+ errCode = OC_EH_OK;
+ }
+ }
+ else
+ {
+ response->setResponseResult(OC_EH_ERROR);
+ OC::OCPlatform::sendResponse(response);
+ errCode = OC_EH_ERROR;
+ }
+ }
+ else if (OC::RequestHandlerFlag::ObserverFlag & request->getRequestHandlerFlag())
+ {
+ OC::ObservationInfo observationInfo = request->getObservationInfo();
+ if (OC::ObserveAction::ObserveRegister == observationInfo.action)
+ {
+ m_interestedObservers.push_back(observationInfo.obsId);
+ }
+ else if (OC::ObserveAction::ObserveUnregister == observationInfo.action)
+ {
+ m_interestedObservers.erase(std::remove(m_interestedObservers.begin(),
+ m_interestedObservers.end(),
+ observationInfo.obsId),
+ m_interestedObservers.end());
+ }
+ errCode = OC_EH_OK;
+ }
+
+ return errCode;
+}
+
+void SimulatorResource::notifyListOfObservers ()
+{
+ if (0 == m_interestedObservers.size())
+ return;
+
+ std::shared_ptr<OC::OCResourceResponse> resourceResponse =
+ {std::make_shared<OC::OCResourceResponse>()};
+
+ resourceResponse->setErrorCode(200);
+ resourceResponse->setResponseResult(OC_EH_OK);
+ resourceResponse->setResourceRepresentation(getOCRepresentation(), OC::DEFAULT_INTERFACE);
+
+ OC::OCPlatform::notifyListOfObservers(getHandle(), m_interestedObservers, resourceResponse);
+}
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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.
+ *
+ ******************************************************************/
+
+/**
+ * @file simulator_resource.h
+ *
+ * @brief This file contains a class which represents a simulator resource that provides a set
+ * of functions for operating a resource and performing automation on attribute values.
+ */
+
+#ifndef SIMULATOR_RESOURCE_H_
+#define SIMULATOR_RESOURCE_H_
+
+#include "simulator_resource_model.h"
+#include "simulator_attribute_automation.h"
+#include "simulator_error_codes.h"
+
+class ResourceManager;
+/**
+ * @class SimulatorResource
+ * @brief This class provides a set of functions for operating and automating a resource.
+ */
+class SimulatorResource
+{
+ friend class ResourceManager;
+
+ public:
+ typedef std::function<void (const std::string &uri, const std::vector<SimulatorResourceModel::Attribute> &attributes)>
+ ResourceModelChangedCB;
+
+ /**
+ * This method is used to set the resource URI.
+ *
+ * @param uri - Resource URI
+ *
+ * @return void
+ */
+ void setURI(const std::string &uri);
+
+ /**
+ * This method is used to get the resource URI.
+ *
+ * @return Resource URI
+ */
+ std::string getURI() const;
+
+ /**
+ * This method is used to set the resource type.
+ *
+ * @param resourceType - Resource Type
+ *
+ * @return void
+ */
+ void setResourceType(const std::string &resourceType);
+
+ /**
+ * This method is used to get the resource URI.
+ *
+ * @return Resource Type
+ */
+ std::string getResourceType() const;
+
+ /**
+ * This method is used to set the interface type of the resource.
+ *
+ * @param interfaceType - Interface Type of the resource
+ *
+ * @return void
+ */
+ void setInterfaceType(const std::string &interfaceType);
+
+ /**
+ * This method is used to get the interface type of the resource.
+ *
+ * @return Interface type of the resource
+ */
+ std::string getInterfaceType() const;
+
+ /**
+ * This method is used to set the name of the resource.
+ *
+ * @param name - Name of the resource
+ *
+ * @return void
+ */
+ void setName(const std::string &name);
+
+ /**
+ * This method is used to get the name of the resource.
+ *
+ * @return Resource name
+ */
+ std::string getName() const;
+
+ /**
+ * This method is used to start the attribute value automation for all attributes.
+ * Once started, values for the attributes will be selected randomly from their allowed range
+ * and the updated values will be notified to all the observers of the resource.
+ *
+ * @param id - Identifier for automation will be returned
+ *
+ * @return SimulatorResult
+ */
+ SimulatorResult startUpdateAutomation(AutomationType type, int &id);
+
+ /**
+ * This method is used to start the attribute value automation for a specific attribute.
+ * Once started, values for the attribute will be selected randomly from its allowed range
+ * and the updated value will be notified to all the observers of the resource.
+ *
+ * @param attrName - Name of the attribute to be automated
+ * @param id - Identifier for automation will be returned
+ *
+ * @return SimulatorResult
+ */
+ SimulatorResult startUpdateAutomation(const std::string &attrName, AutomationType type, int &id);
+
+ /**
+ * This method is used to stop the automation.
+ *
+ * @param id - Identifier for automation
+ */
+ void stopUpdateAutomation(const int id);
+
+ /**
+ * This method is used to add a new attribute to the resource model.
+ *
+ * @param attrName - Name of the attribute
+ * @param attrValue - Value of the attribute
+ *
+ * @return void
+ */
+ template <typename T>
+ void addAttribute(const std::string &attrName, const T &attrValue)
+ {
+ m_resModel.addAttribute(attrName, attrValue);
+ notifyListOfObservers();
+ }
+
+ /**
+ * This method is used to set the value range of an attribute.
+ * This method is intended to be used for attributes whose values are numbers only.
+ *
+ * @param attrName - Name of the attribute
+ * @param min - Minimum value of the range
+ * @param max - Maximum value of the range
+ *
+ * @return void
+ */
+ void setRange(const std::string &attrName, const int min, const int max);
+
+ /**
+ * This method is used to set the allowed values of an attribute.
+ *
+ * @param attrName - Name of the attribute
+ * @param values - Allowed values
+ *
+ * @return void
+ */
+ template <typename T>
+ void setAllowedValues(const std::string &attrName, const std::vector<T> &values)
+ {
+ m_resModel.setAllowedValues(attrName, values);
+ }
+
+ /**
+ * This method is used to set the update interval time for automation.
+ *
+ * @param attrName - Name of the attribute
+ * @param interval - Interval time in miliseconds for attribute value update automation
+ *
+ * @return void
+ */
+ void setUpdateInterval(const std::string &attrName, int interval);
+
+ /**
+ * This method is used to update the value of an attribute.
+ *
+ * @param attrName - Name of the attribute
+ * @param value - Value of the attribute
+ *
+ * @return void
+ */
+ template <typename T>
+ void updateAttribute(const std::string &attrName, const T &value)
+ {
+ m_resModel.updateAttribute(attrName, value);
+ notifyListOfObservers();
+ }
+
+ /**
+ * This method is used to update the attribute's value by taking the index of the value
+ * in the allowed values range.
+ *
+ * @param attrName - Name of the attribute
+ * @param allowedValueIndex - Index of the value in the allowed values range
+ *
+ * @return void
+ */
+ void updateAttributeFromAllowedValues(const std::string &attrName, const int allowedValueIndex);
+
+ /**
+ * This method is used to remove an attribute from the resource model.
+ *
+ * @param attName - Name of the attribute to be removed
+ *
+ * @return void
+ */
+ void removeAttribute(const std::string &attName);
+
+ /**
+ * This method is used to get the object of SimulatorResourceModel.
+ * Attributes of the resource are accessed using this object.
+ *
+ * @return SimulatorResourceModel - Resource model of the resource
+ */
+ SimulatorResourceModel getModel() const;
+
+ /**
+ * This method is used to set the callback for receiving the notifications when the
+ * resource model changes.
+ *
+ * @param callback - Callback to be set for receiving the notifications.
+ *
+ * @return SimulatorResourceModel - Resource model of the resource
+ */
+ void setModelChangeCallback(ResourceModelChangedCB callback);
+
+ private:
+ OCResourceHandle getHandle() const;
+ OC::OCRepresentation getOCRepresentation();
+ bool modifyResourceModel(OC::OCRepresentation &ocRep, SimulatorResourceModel::UpdateType type);
+ OCEntityHandlerResult entityHandler(std::shared_ptr<OC::OCResourceRequest> request);
+ void notifyListOfObservers ();
+
+ SimulatorResourceModel m_resModel;
+ OCResourceHandle m_resourceHandle;
+ std::string m_uri;
+ std::string m_resourceType;
+ std::string m_interfaceType;
+ std::string m_name;
+ OC::ObservationIds m_interestedObservers;
+ ResourceModelChangedCB m_callback;
+ UpdateAutomationManager m_updateAutomationMgr;
+};
+
+typedef std::shared_ptr<SimulatorResource> SimulatorResourcePtr;
+
+#endif
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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 "simulator_resource_creator.h"
+
+SimulatorResourcePtr SimulatorResourceCreator::createLightResoure()
+{
+ std::shared_ptr<SimulatorResource> lightResource(new SimulatorResource);
+
+ // set power attribute with its properties
+ {
+ lightResource->addAttribute("power", std::string("on"));
+ std::vector<std::string> values {"on", "off"};
+ lightResource->setAllowedValues("power", values);
+ lightResource->setUpdateInterval("power", 1000);
+ }
+
+ // set intensity attributes with its properties
+ {
+ lightResource->addAttribute("intensity", int(1));
+ lightResource->setRange("intensity", 1, 10);
+ lightResource->setUpdateInterval("intensity", 2000);
+ }
+
+ // set other properties
+ lightResource->setName("Light");
+ lightResource->setURI("oic/light");
+ lightResource->setResourceType("oic.light");
+ lightResource->setInterfaceType(OC::DEFAULT_INTERFACE);
+
+ return lightResource;
+}
+
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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 SIMULATOR_RESOURCE_CREATOR_H_
+#define SIMULATOR_RESOURCE_CREATOR_H_
+
+#include "simulator_resource.h"
+
+class SimulatorResourceCreator
+{
+ public:
+ /**
+ * This is temporary method to get the light resource as parser
+ * needs to implemented/integrated
+ */
+ SimulatorResourcePtr createLightResoure();
+};
+
+#endif
\ No newline at end of file
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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 "simulator_resource_model.h"
+#include <sstream>
+#include <boost/lexical_cast.hpp>
+
+class to_string_visitor : public boost::static_visitor<std::string>
+{
+ public:
+ template <typename T>
+ result_type operator ()(const T &value)
+ {
+ return boost::lexical_cast<std::string>(value);
+ }
+};
+
+class add_to_representation : public boost::static_visitor<>
+{
+ public:
+ add_to_representation(OC::OCRepresentation &rep, const std::string &key)
+ : m_rep(rep), m_key(key) {}
+
+ template <typename T>
+ void operator ()(const T &value)
+ {
+ m_rep.setValue(m_key, value);
+ }
+
+ OC::OCRepresentation &&getRep()
+ {
+ return std::move(m_rep);
+ }
+
+ private:
+ OC::OCRepresentation m_rep;
+ std::string m_key;
+};
+
+class range_validation : public boost::static_visitor<bool>
+{
+ public:
+ range_validation (SimulatorResourceModel::Attribute &attrItem)
+ : m_attrItem(attrItem) {}
+
+ bool operator ()(int &value)
+ {
+ int min, max;
+ m_attrItem.getRange(min, max);
+ if (value >= min && value <= max)
+ return true;
+ return false;
+ }
+
+ bool operator ()(double &value)
+ {
+ std::vector<SimulatorResourceModel::Attribute::ValueVariant> values
+ = m_attrItem.getAllowedValues();
+ for (SimulatorResourceModel::Attribute::ValueVariant & val : values)
+ {
+ SimulatorResourceModel::Attribute::ValueVariant vVal = value;
+ if (val == vVal)
+ return true;
+ }
+ return false;
+ }
+
+ bool operator ()(std::string &value)
+ {
+ std::vector<SimulatorResourceModel::Attribute::ValueVariant> values
+ = m_attrItem.getAllowedValues();
+ for (SimulatorResourceModel::Attribute::ValueVariant & vVal : values)
+ {
+ std::string val = boost::get<std::string>(vVal);
+ if (val == value)
+ return true;
+ }
+
+ return false;
+ }
+
+ private:
+ SimulatorResourceModel::Attribute &m_attrItem;
+};
+
+class Converter
+{
+ public:
+ void convert(const OC::OCRepresentation::AttributeItem &attrItem)
+ {
+ if (attrItem.type() == OC::AttributeType::Integer)
+ m_attribute.setValue(attrItem.getValue<int>());
+ if (attrItem.type() == OC::AttributeType::Double)
+ m_attribute.setValue(attrItem.getValue<double>());
+ if (attrItem.type() == OC::AttributeType::String)
+ m_attribute.setValue(attrItem.getValue<std::string>());
+ }
+
+ SimulatorResourceModel::Attribute &&get()
+ {
+ return std::move(m_attribute);
+ }
+
+ private:
+ SimulatorResourceModel::Attribute m_attribute;
+};
+
+SimulatorResourceModel::Attribute::ValueVariant
+&SimulatorResourceModel::Attribute::AllowedValues::at(int index)
+{
+ return m_values.at(index);
+}
+
+int SimulatorResourceModel::Attribute::AllowedValues::size() const
+{
+ return m_values.size();
+}
+
+std::string SimulatorResourceModel::Attribute::AllowedValues::toString() const
+{
+ std::ostringstream stream;
+ stream << "[ ";
+ for (auto & value : m_values)
+ {
+ to_string_visitor visitor;
+ stream << boost::apply_visitor(visitor, value) << " ";
+ }
+ stream << "]";
+ return stream.str();
+}
+
+std::vector<std::string> SimulatorResourceModel::Attribute::AllowedValues::toVectorString() const
+{
+ std::vector<std::string> values;
+
+ for (auto & value : m_values)
+ {
+ to_string_visitor visitor;
+ values.push_back(boost::apply_visitor(visitor, value));
+ }
+ return values;
+}
+
+std::vector<SimulatorResourceModel::Attribute::ValueVariant>
+SimulatorResourceModel::Attribute::AllowedValues::getValues()
+{
+ return m_values;
+}
+
+std::string SimulatorResourceModel::Attribute::getName(void) const
+{
+ return m_name;
+}
+
+void SimulatorResourceModel::Attribute::setName(const std::string &name)
+{
+ m_name = name;
+}
+
+void SimulatorResourceModel::Attribute::getRange(int &min, int &max) const
+{
+ min = m_min;
+ max = m_max;
+}
+
+void SimulatorResourceModel::Attribute::setRange(const int &min, const int &max)
+{
+ m_min = min;
+ m_max = max;
+}
+
+int SimulatorResourceModel::Attribute::getAllowedValuesSize() const
+{
+ return m_allowedValues.size();
+}
+
+void SimulatorResourceModel::Attribute::setFromAllowedValue(const int allowedValueIndex)
+{
+ m_value = m_allowedValues.at(allowedValueIndex);
+}
+
+std::string SimulatorResourceModel::Attribute::valueToString() const
+{
+ to_string_visitor visitor;
+ return boost::apply_visitor(visitor, m_value);
+}
+
+std::string SimulatorResourceModel::Attribute::allowedValuesToString() const
+{
+ return m_allowedValues.toString();
+}
+
+std::vector<std::string> SimulatorResourceModel::Attribute::allowedValuesToVectorString() const
+{
+ return m_allowedValues.toVectorString();
+}
+
+void SimulatorResourceModel::Attribute::addValuetoRepresentation(OC::OCRepresentation &rep,
+ const std::string &key)
+{
+ add_to_representation visitor(rep, key);
+ boost::apply_visitor(visitor, m_value);
+ rep = visitor.getRep();
+}
+
+bool SimulatorResourceModel::Attribute::compare(SimulatorResourceModel::Attribute &attribute)
+{
+ // Check the value types
+ if (m_value.which() != attribute.getValue().which())
+ {
+ return false;
+ }
+
+ // Check the value in allowed range
+ range_validation visitor(*this);
+ return boost::apply_visitor(visitor, attribute.getValue());
+}
+
+std::vector<SimulatorResourceModel::Attribute::ValueVariant>
+SimulatorResourceModel::Attribute::getAllowedValues()
+{
+ return m_allowedValues.getValues();
+}
+
+bool SimulatorResourceModel::getAttribute(const std::string &attrName, Attribute &value)
+{
+ if (m_attributes.end() != m_attributes.find(attrName))
+ {
+ value = m_attributes[attrName];
+ return true;
+ }
+
+ return false;
+}
+
+std::map<std::string, SimulatorResourceModel::Attribute> SimulatorResourceModel::getAttributes()
+const
+{
+ return m_attributes;
+}
+
+void SimulatorResourceModel::setRange(const std::string &attrName, const int min, const int max)
+{
+ if (m_attributes.end() != m_attributes.find(attrName))
+ m_attributes[attrName].setRange(min, max);
+}
+
+void SimulatorResourceModel::setUpdateInterval(const std::string &attrName, int interval)
+{
+ if (m_attributes.end() != m_attributes.find(attrName))
+ m_attributes[attrName].setUpdateFrequencyTime(interval);
+}
+
+void SimulatorResourceModel::updateAttributeFromAllowedValues(const std::string &attrName,
+ const int allowedValueIndex)
+{
+ if (m_attributes.end() != m_attributes.find(attrName))
+ m_attributes[attrName].setFromAllowedValue(allowedValueIndex);
+}
+
+void SimulatorResourceModel::removeAttribute(const std::string &attrName)
+{
+ m_attributes.erase(attrName);
+ return;
+}
+
+OC::OCRepresentation SimulatorResourceModel::getOCRepresentation()
+{
+ OC::OCRepresentation rep;
+ for (auto & attribute : m_attributes)
+ {
+ (attribute.second).addValuetoRepresentation(rep, attribute.first);
+ }
+
+ return rep;
+}
+
+bool SimulatorResourceModel::update(OC::OCRepresentation &ocRep, UpdateType type)
+{
+ if (0 == ocRep.size())
+ return true;
+
+ // Convert OCRepresentation to SimulatorResourceModel
+ SimulatorResourceModel resModel;
+ for (auto & attributeItem : ocRep)
+ {
+ Converter converter;
+ converter.convert(attributeItem);
+ SimulatorResourceModel::Attribute attribute = converter.get();
+ attribute.setName(attributeItem.attrname());
+ resModel.m_attributes[attributeItem.attrname()] = attribute;
+ }
+
+ return update(resModel, type);
+}
+
+bool SimulatorResourceModel::update(SimulatorResourceModel &repModel, UpdateType type)
+{
+ std::map<std::string, SimulatorResourceModel::Attribute> attributes = repModel.getAttributes();
+ for (auto & attributeItem : attributes)
+ {
+ // Check the attribute presence
+ SimulatorResourceModel::Attribute attribute;
+ if (type == PUT)
+ {
+ if (false == getAttribute((attributeItem.second).getName(), attribute))
+ {
+ return false;
+ }
+
+ // Check the validity of the value to be set
+ if (false == attribute.compare(attributeItem.second))
+ {
+ return false;
+ }
+ m_attributes[(attributeItem.second).getName()].setValue((attributeItem.second).getValue());
+ }
+ else if (type == POST)
+ {
+ if (false == getAttribute((attributeItem.second).getName(), attribute))
+ {
+ addAttribute(((attributeItem.second).getName()) , ((attributeItem.second).getValue()));
+ continue;
+ }
+ else
+ {
+ // Check the validity of the value to be set
+ if (false == attribute.compare(attributeItem.second))
+ {
+ return false;
+ }
+ m_attributes[(attributeItem.second).getName()].setValue((attributeItem.second).getValue());
+ }
+ }
+ else if (type == DELETE )
+ {
+ if (true == getAttribute((attributeItem.second).getName(), attribute))
+ {
+ removeAttribute((attributeItem.second).getName());
+ }
+ }
+ }
+ return true;
+}
\ No newline at end of file
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics 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.
+ *
+ ******************************************************************/
+
+/**
+ * @file simulator_resource_model.h
+ *
+ * @brief This file contains a class which represents the resource model for simulator
+ * resources and provides a set of functions for updating the model.
+ */
+
+#ifndef SIMULATOR_RESOURCE_ATTRIBUTE_H_
+#define SIMULATOR_RESOURCE_ATTRIBUTE_H_
+
+#include <string>
+#include <vector>
+#include "OCPlatform.h"
+#include "OCApi.h"
+
+class SimulatorResource;
+/**
+ * @class SimulatorResourceModel
+ * @brief This class provides a set of functions for accessing and manipulating the resource model.
+ */
+class SimulatorResourceModel
+{
+ friend class SimulatorResource;
+
+ public:
+ /**
+ * @class Attribute
+ * @brief This class represents a resource attribute whose values can be generic.
+ */
+ class Attribute
+ {
+ public:
+ typedef boost::variant <
+ int,
+ double,
+ std::string
+ > ValueVariant;
+
+ Attribute() = default;
+ Attribute(const std::string &attrName) : m_name(attrName) {}
+
+ std::string getName(void) const;
+ void setName(const std::string &name);
+
+ template <typename T>
+ T getValue() const
+ {
+ T val = T();
+ return boost::get<T>(m_value);
+ }
+
+ ValueVariant &getValue()
+ {
+ return m_value;
+ }
+
+ int getValueType() const
+ {
+ return m_value.which();
+ }
+
+ template <typename T>
+ void setValue(const T &value)
+ {
+ m_value = value;
+ }
+
+ void setFromAllowedValue(const int allowedValueIndex);
+
+ void getRange(int &min, int &max) const;
+
+ void setRange(const int &min, const int &max);
+
+ template <typename T>
+ bool setAllowedValues(const std::vector<T> &values)
+ {
+ ValueVariant temp = values.at(0);
+ if (temp.which() != m_value.which())
+ {
+ return false;
+ }
+
+ m_allowedValues.addValues(values);
+ return true;
+ }
+
+ /**
+ * This method is used to get the size of the allowed values.
+ *
+ * @return Size of the allowed values
+ */
+ int getAllowedValuesSize() const;
+
+ /**
+ * This method is used to get the string representation of the value.
+ *
+ * @return Attribute's value as a string
+ */
+ std::string valueToString() const;
+
+ /**
+ * This method is used to get the string representation of all the allowed values.
+ *
+ * @return All allowed values as a string
+ */
+ std::string allowedValuesToString() const;
+ std::vector<std::string> allowedValuesToVectorString() const;
+
+ void addValuetoRepresentation(OC::OCRepresentation &rep, const std::string &key);
+
+ bool compare(Attribute &attribute);
+
+ std::vector<ValueVariant> getAllowedValues();
+
+ int getUpdateFrequencyTime() {return m_updateInterval;}
+ void setUpdateFrequencyTime(int interval) {m_updateInterval = interval;}
+
+ private:
+ class AllowedValues
+ {
+ public:
+ template <typename T>
+ void addValue(const T &value)
+ {
+ ValueVariant temp = value;
+ m_values.push_back(temp);
+ }
+
+ template <typename T>
+ void addValues(const std::vector<T> &values)
+ {
+ for (auto value : values)
+ {
+ ValueVariant vValue = value;
+ m_values.push_back(vValue);
+ }
+ }
+
+ ValueVariant &at(int index);
+ int size() const;
+ std::string toString() const;
+ std::vector<std::string> toVectorString() const;
+ std::vector<ValueVariant> getValues();
+ private:
+ std::vector<ValueVariant> m_values;
+ };
+
+ std::string m_name;
+ ValueVariant m_value;
+ int m_max;
+ int m_min;
+ AllowedValues m_allowedValues;
+ int m_updateInterval;
+ };
+
+ typedef enum { POST, PUT, DELETE } UpdateType;
+
+ /**
+ * This method is used to get the number of attributes in the resource.
+ *
+ * @return Count of attributes
+ */
+ int size() const { return m_attributes.size(); }
+
+ /**
+ * This method is used to get the value of an attribute.
+ *
+ * @param attrName - Attribute name
+ * @param value - Attribute value
+ *
+ * @return Boolean, true if attribute exists, otherwise false.
+ */
+ bool getAttribute(const std::string &attrName, Attribute &value);
+
+ /**
+ * This method is used to get the entire list of attributes in the form of key-value pair.
+ * Attribute name is the key and an instance of Attribute is the value.
+ *
+ * @return A map of all the attributes
+ */
+ std::map<std::string, Attribute> getAttributes() const;
+
+ private:
+ template <typename T>
+ void addAttribute(const std::string &attrName, const T &attrValue)
+ {
+ if (m_attributes.end() == m_attributes.find(attrName))
+ {
+ m_attributes[attrName] = Attribute(attrName);
+ m_attributes[attrName].setValue(attrValue);
+ }
+ }
+
+ void setRange(const std::string &attrName, const int min, const int max);
+
+ template <typename T>
+ void setAllowedValues(const std::string &attrName, const std::vector<T> &values)
+ {
+ if (m_attributes.end() != m_attributes.find(attrName))
+ m_attributes[attrName].setAllowedValues(values);
+ }
+
+ void setUpdateInterval(const std::string &attrName, int interval);
+
+ template <typename T>
+ void updateAttribute(const std::string &attrName, const T &value)
+ {
+ if (m_attributes.end() != m_attributes.find(attrName))
+ m_attributes[attrName].setValue(value);
+ }
+
+ void updateAttributeFromAllowedValues(const std::string &attrName, const int allowedValueIndex);
+
+ void removeAttribute(const std::string &attrName);
+
+ OC::OCRepresentation getOCRepresentation();
+
+ bool update(OC::OCRepresentation &ocRep, UpdateType type);
+
+ bool update(SimulatorResourceModel &repModel, UpdateType type);
+
+ std::map<std::string, Attribute> m_attributes;
+};
+
+#endif