- Clean up of the JNI wrapper layer: unnecessary files are removed.
- The Android bundle sample contains an example that uses the light sensor as an input for a resource.
- Notification mechanism for android based bundle resources for CoAP observeA
- SoftSensor support for Android
Simplified JNI wrapper and removed unnecessary classes.
Change-Id: I0fa6d06e35851218011f2db0ca263e506a0987f4
Signed-off-by: Markus Jung <markus.jung@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/4353
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
protected RcsResourceAttributes m_attributes = new RcsResourceAttributes();
protected Context m_context;
+
+ long mNativeHandle;
+
+ protected native void updateNativeInstance(RcsResourceAttributes update);
public AndroidBundleResource() {
initAttributes();
* @param value
* new value of the attribute
*/
- protected final void setAttribute(String key, RcsValue value) {
+ protected final void setAttribute(String key, RcsValue value, boolean notify) {
m_attributes.put(key, value);
+
+ if(notify){
+ updateNativeInstance(m_attributes);
+ }
+ }
+
+ /**
+ * Set the attribute (map to a send command for the according protocol)
+ *
+ * @param key
+ * name of the attribute to be set
+ * @param value
+ * new value of the attribute
+ */
+ protected final void setAttribute(String key, RcsValue value) {
+ setAttribute(key, value, false);
}
/**
* @param value
* new value of the attribute
*/
- protected final void setAttributes(RcsResourceAttributes value) {
+ protected final void setAttributes(RcsResourceAttributes value, boolean notify) {
m_attributes.put(value);
+
+ if(notify){
+ updateNativeInstance(m_attributes);
+ }
+ }
+
+ protected final void setAttributes(RcsResourceAttributes value) {
+ setAttributes(value, false);
}
/**
public String getName() {
return m_name;
}
-
}
--- /dev/null
+package org.iotivity.service.resourcecontainer;
+
+import java.util.HashMap;
+import java.util.Set;
+import java.util.Vector;
+import android.content.Context;
+import android.util.Log;
+
+
+public abstract class AndroidBundleSoftSensorResource extends AndroidBundleResource {
+ protected HashMap<String, RcsValue> m_mapInputData;
+
+ public AndroidBundleSoftSensorResource(Context context) {
+ super(context);
+ }
+ /**
+ * Initialize the internal attribute structure.
+ */
+ protected abstract void onUpdatedInputResource(String attributeName, Vector<RcsValue> values);
+
+ /**
+ * SoftSensor logic. Has to be provided by the soft sensor developer.
+ * This function will be executed if an input attribute is updated.
+ *
+ * @return void
+ */
+ protected abstract void executeLogic();
+}
\ No newline at end of file
+++ /dev/null
-package org.iotivity.service.resourcecontainer;
-
-/**
- * Thrown when trying to access a destroyed resource object.
- *
- */
-public class RcsDestroyedObjectException extends RcsException {
-
- private static final long serialVersionUID = -4107062696447237658L;
-
- public RcsDestroyedObjectException(String message) {
- super(message);
- }
-
-}
+++ /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.service.resourcecontainer;
-
-/**
- * An exception that indicates there was an error with execution of Rcs APIs.
- */
-public class RcsException extends Exception {
-
- private static final long serialVersionUID = 7044421943523001940L;
-
- public RcsException(String message) {
- super(message);
- }
-}
+++ /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.service.resourcecontainer;
-
-/**
- * Thrown when an action is attempted at a time when not in the correct state.
- *
- */
-public class RcsIllegalStateException extends RcsException {
-
- private static final long serialVersionUID = 6142669404885957616L;
-
- public RcsIllegalStateException(String message) {
- super(message);
- }
-
-}
+++ /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.service.resourcecontainer;
-
-public class RcsObject {
- /*static {
- System.loadLibrary("gnustl_shared");
- System.loadLibrary("oc_logger");
- System.loadLibrary("connectivity_abstraction");
- System.loadLibrary("ca-interface");
- System.loadLibrary("octbstack");
- System.loadLibrary("oc");
- System.loadLibrary("rcs_client");
- System.loadLibrary("rcs_server");
- System.loadLibrary("rcs_common");
- System.loadLibrary("rcs_jni");
- }*/
-
- private long mNativeHandle;
-
- private native void nativeDispose();
-
- protected RcsObject() {
- }
-
- @Override
- protected void finalize() throws Throwable {
- super.finalize();
-
- dispose();
- }
-
- protected void dispose() {
- if (mNativeHandle != 0L) nativeDispose();
- }
-
- protected boolean hasHandle() {
- return mNativeHandle != 0L;
- }
-}
+++ /dev/null
-package org.iotivity.service.resourcecontainer;
-
-/**
- * Thrown when an operation that has base-layer dependency is failed.
- *
- */
-public class RcsPlatformException extends RcsException {
-
- private static final long serialVersionUID = -6093438347973754721L;
-
- private final int mReasonCode;
-
- public RcsPlatformException(String message, int reasonCode) {
- super(message);
-
- mReasonCode = reasonCode;
- }
-
- public int getReasonCode() {
- return mReasonCode;
- }
-}
import java.util.Map;
import java.util.Set;
-import org.iotivity.service.resourcecontainer.server.RcsLockedAttributes;
+//import org.iotivity.service.resourcecontainer.server.RcsLockedAttributes;
/**
*
*
* @see RcsValue
*/
-public final class RcsResourceAttributes extends RcsObject {
-
- private native boolean nativeIsEmpty();
-
- private native int nativeSize();
-
- private native boolean nativeRemove(String key);
-
- private native void nativeClear();
-
- private native boolean nativeContains(String key);
-
- private native void nativeAddKeys(Set<String> set);
-
- private native RcsValue nativeExtract(String key);
-
- private native void nativeExtractAll(Map<String, RcsValue> map);
+public final class RcsResourceAttributes
+{
private final Map<String, RcsValue> mCache = new HashMap<>();
public RcsResourceAttributes() {
}
- public RcsResourceAttributes(RcsLockedAttributes lockedAttrs)
- throws RcsException {
- for (final String key : lockedAttrs.keySet()) {
- mCache.put(key, lockedAttrs.get(key));
- }
- }
public RcsResourceAttributes(RcsResourceAttributes attrs){
for (final String key : attrs.keySet()) {
* @return an unmodifiable set view of the keys in this attributes
*/
public Set<String> keySet() {
- if (hasHandle()) {
- final Set<String> keySet = new HashSet<>(mCache.keySet());
-
- nativeAddKeys(keySet);
-
- return Collections.unmodifiableSet(keySet);
- }
-
return Collections.unmodifiableSet(mCache.keySet());
}
*/
public RcsValue get(String key) {
if (key == null) throw new NullPointerException("key is null");
-
- if (!mCache.containsKey(key) && hasHandle() && nativeContains(key)) {
- mCache.put(key, nativeExtract(key));
- }
+
return mCache.get(key);
}
if (value == null) throw new NullPointerException("value is null");
mCache.put(key, value);
- if (hasHandle()) nativeRemove(key);
}
/**
* @return true if this contains no key-value mappings
*/
public boolean isEmpty() {
- return mCache.isEmpty() && (!hasHandle() || nativeIsEmpty());
+ return mCache.isEmpty();
}
/**
* @return the number of key-value mappings
*/
public int size() {
- if (hasHandle()) return mCache.size() + nativeSize();
return mCache.size();
}
*/
public boolean remove(String key) {
if (key == null) throw new NullPointerException("key is null");
+
+ final boolean cacheRemove = mCache.remove(key) != null;
- // XXX make sure both cache and native values to be removed.
- final boolean cacheRemove = mCache.remove(key) != null;
- final boolean nativeRemove = hasHandle() && nativeRemove(key);
-
- return cacheRemove || nativeRemove;
+ return cacheRemove;
}
/**
* Removes all of the mappings.
*/
- public void clear() {
+ public void clear(){
mCache.clear();
- nativeClear();
}
/**
public boolean contains(String key) {
if (key == null) throw new NullPointerException("key is null");
- return mCache.containsKey(key) || nativeContains(key);
- }
-
- private void esnureAllExtracted() {
- if (hasHandle()) nativeExtractAll(mCache);
+ return mCache.containsKey(key);
}
+
@Override
public boolean equals(Object o) {
if (o == this) return true;
- if (!(o instanceof RcsResourceAttributes)) return false;
-
- final RcsResourceAttributes rhs = (RcsResourceAttributes) o;
-
- esnureAllExtracted();
- rhs.esnureAllExtracted();
-
+ if (!(o instanceof RcsResourceAttributes)) return false;
+ RcsResourceAttributes rhs = (RcsResourceAttributes) o;
return mCache.equals(rhs.mCache);
}
@Override
public int hashCode() {
- esnureAllExtracted();
return mCache.hashCode();
}
+++ /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.service.resourcecontainer.server;
-
-import org.iotivity.service.resourcecontainer.RcsResourceAttributes;
-
-/**
- * This class provides methods to create the response for a get request.
- *
- * @see RcsResourceObject
- * @see RcsSetResponse
- */
-public class RcsGetResponse extends RcsResponse {
-
- /**
- * Creates a default RCcsGetResponse. The response will have
- * {@link #DEFAULT_ERROR_CODE} for the errorCode. The attributes of
- * {@link RcsResourceObject} will be set as the result attributes.
- *
- */
- public static RcsGetResponse defaultAction() {
- return new RcsGetResponse();
- }
-
- /**
- * Creates a RcsGetResponse with error code passed. The
- * attributes of the {@link RcsResourceObject} will be set as the result
- * attributes.
- *
- * @param errorCode
- * error code to be set in response
- *
- */
- public static RcsGetResponse create(int errorCode) {
- return new RcsGetResponse(errorCode);
-
- }
-
- /**
- * Creates a RcsGetResponse with custom attributes and
- * {@link #DEFAULT_ERROR_CODE} for the errorCode. This sends the passed
- * attributes as the result attributes instead of the one the
- * {@link RcsResourceObject} holds.
- *
- * @param attributes
- * attributes to be sent as the result
- *
- */
- public static RcsGetResponse create(RcsResourceAttributes attributes) {
- return new RcsGetResponse(attributes);
- }
-
- /**
- * Creates a RcsGetResponse with error code passed. This sends
- * the passed attributes as the result attributes instead of one the
- * {@link RcsResourceObject} holds.
- *
- * @param attributes
- * attributes to be sent as the result
- * @param errorCode
- * error code for response
- *
- */
- public static RcsGetResponse create(RcsResourceAttributes attributes,
- int errorCode) {
- return new RcsGetResponse(attributes, errorCode);
- }
-
- private RcsGetResponse() {
- super();
- }
-
- private RcsGetResponse(int errorCode) {
- super(errorCode);
- }
-
- private RcsGetResponse(RcsResourceAttributes attrs) {
- super(attrs);
- }
-
- private RcsGetResponse(RcsResourceAttributes attrs, int errorCode) {
- super(attrs, errorCode);
- }
-}
+++ /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.service.resourcecontainer.server;
-
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-
-import org.iotivity.service.resourcecontainer.RcsException;
-import org.iotivity.service.resourcecontainer.RcsObject;
-import org.iotivity.service.resourcecontainer.RcsResourceAttributes;
-import org.iotivity.service.resourcecontainer.RcsValue;
-
-public final class RcsLockedAttributes extends RcsObject {
-
- private static native boolean nativeIsEmpty(RcsResourceObject resourceObj);
-
- private static native int nativeSize(RcsResourceObject resourceObj);
-
- private static native boolean nativeRemove(RcsResourceObject resourceObj,
- String key);
-
- private static native boolean nativeClear(RcsResourceObject resourceObj);
-
- private static native boolean nativeContains(RcsResourceObject resourceObj,
- String key);
-
- private static native void nativeAddKeys(RcsResourceObject resourceObj,
- Set<String> set);
-
- private static native RcsValue nativeAsJavaObject(
- RcsResourceObject resourceObj, String key);
-
- private static native void nativeApply(RcsResourceObject resourceObj,
- Map<String, RcsValue> cache);
-
- private native void nativeLock(RcsResourceObject resourceObj);
-
- private native void nativeUnlock();
-
- private final RcsResourceObject mResourceObject;
-
- private boolean mIsUnlocked;
-
- private Map<String, RcsValue> mCache = new HashMap<>();
-
- RcsLockedAttributes(RcsResourceObject resourceObject) throws RcsException {
- if (resourceObject == null) {
- throw new RcsException("Illegal opertaion!");
- }
-
- mResourceObject = resourceObject;
-
- nativeLock(resourceObject);
- }
-
- void setUnlockState() {
- mIsUnlocked = true;
- mCache = null;
-
- nativeUnlock();
- }
-
- void apply() {
- nativeApply(mResourceObject, mCache);
- mCache.clear();
- }
-
- private void ensureLocked() throws RcsException {
- if (mIsUnlocked) {
- throw new RcsUnlockedException("This attributes is unlocked!");
- }
- }
-
- /**
- * Returns a unmodifiable Set view of the keys contained in this attributes.
- *
- * @return an unmodifiable set view of the keys in this attributes
- *
- * @throws RcsUnlockedException
- * if the {@link RcsResourceObject.AttributesLock} for this
- * object is unlocked
- */
- public Set<String> keySet() throws RcsException {
- ensureLocked();
-
- final Set<String> keySet = new HashSet<>(mCache.keySet());
-
- nativeAddKeys(mResourceObject, keySet);
-
- return Collections.unmodifiableSet(keySet);
- }
-
- /**
- * Returns the value to which the specified key is mapped, or null if this
- * contains no mapping for the key.
- *
- * @param key
- * the key whose associated value is to be returned
- *
- * @return the value to which the specified key is mapped, or null if this
- * contains no mapping for the key
- *
- * @throws NullPointerException
- * if key is null
- * @throws RcsUnlockedException
- * if the {@link RcsResourceObject.AttributesLock} for this
- * object is unlocked
- */
- public RcsValue get(String key) throws RcsException {
- ensureLocked();
-
- if (key == null) throw new NullPointerException("key is null");
-
- if (!mCache.containsKey(key) && nativeContains(mResourceObject, key)) {
- mCache.put(key, nativeAsJavaObject(mResourceObject, key));
- }
- return mCache.get(key);
- }
-
- /**
- * Copies all of the mappings from the specified to this
- *
- * @param attributes
- * attributes to be copied
- *
- * @throws RcsUnlockedException
- * if the {@link RcsResourceObject.AttributesLock} for this
- * object is unlocked
- *
- */
- public RcsLockedAttributes putAll(RcsResourceAttributes attributes)
- throws RcsException {
- ensureLocked();
-
- final Set<String> keys = attributes.keySet();
-
- for (final String k : keys) {
- mCache.put(k, attributes.get(k));
- }
- return this;
- }
-
- /**
- * Sets the specified value with the specified key.
- * If the object previously contained a mapping for the key, the old value
- * is replaced by the specified value.
- *
- * @param key
- * key with which the specified value is to be associated
- *
- * @param value
- * value to be associated with the specified key
- *
- * @throws NullPointerException
- * if key or value is null
- * @throws RcsUnlockedException
- * if the {@link RcsResourceObject.AttributesLock} for this
- * object is unlocked
- *
- */
- public RcsLockedAttributes put(String key, RcsValue value)
- throws RcsException {
- ensureLocked();
-
- if (key == null) throw new NullPointerException("key is null");
- if (value == null) throw new NullPointerException("value is null");
-
- mCache.put(key, value);
-
- return this;
- }
-
- /**
- * Sets the specified value with the specified key.
- * If the object previously contained a mapping for the key, the old value
- * is replaced by the specified value.
- *
- * @param key
- * key with which the specified value is to be associated
- *
- * @param value
- * value to be associated with the specified key
- *
- * @throws NullPointerException
- * if key or value is null
- * @throws IllegalArgumentException
- * if object is not supported type by {@link RcsValue}
- * @throws RcsUnlockedException
- * if the {@link RcsResourceObject.AttributesLock} for this
- * object is unlocked
- */
- public void put(String key, Object value) throws RcsException {
- if (key == null) throw new NullPointerException("key is null");
-
- put(key, new RcsValue(value));
- }
-
- /**
- * Returns whether attribute is empty.
- *
- * @throws RcsUnlockedException
- * if the {@link RcsResourceObject.AttributesLock} for this
- * object is unlocked
- */
- public boolean isEmpty() throws RcsException {
- ensureLocked();
-
- return mCache.isEmpty() && nativeIsEmpty(mResourceObject);
- }
-
- /**
- * Returns the number of key-value mappings.
- *
- * @throws RcsUnlockedException
- * if the {@link RcsResourceObject.AttributesLock} for this
- * object is unlocked
- */
- public int size() throws RcsException {
- ensureLocked();
-
- return mCache.size() + nativeSize(mResourceObject);
- }
-
- /**
- * Removes the mapping for a key from this attributes if it is present.
- *
- * @param key
- * key whose mapping is to be removed
- *
- * @return true if the key is present and the the value mapped is removed.
- *
- * @throws RcsUnlockedException
- * if the {@link RcsResourceObject.AttributesLock} for this
- * object is unlocked
- */
- public boolean remove(String key) throws RcsException {
- ensureLocked();
-
- if (key == null) throw new NullPointerException("key is null");
-
- // XXX make sure both cache and native values to be removed.
- final boolean cacheRemove = mCache.remove(key) != null;
- final boolean nativeRemove = nativeRemove(mResourceObject, key);
-
- return cacheRemove || nativeRemove;
- }
-
- /**
- * Removes all elements.
- *
- * @throws RcsUnlockedException
- * if the {@link RcsResourceObject.AttributesLock} for this
- * object is unlocked
- */
- public void clear() throws RcsException {
- ensureLocked();
-
- nativeClear(mResourceObject);
- }
-
- /**
- * Returns true if this contains a mapping for the specified key.
- *
- * @param key
- * key whose presence is to be tested
- *
- * @return true if this contains a mapping for the specified key.
- *
- * @throws NullPointerException
- * if key is null
- * @throws RcsUnlockedException
- * if the {@link RcsResourceObject.AttributesLock} for this
- * object is unlocked
- */
- public boolean contains(String key) throws RcsException {
- ensureLocked();
-
- if (key == null) throw new NullPointerException("key is null");
-
- return mCache.containsKey(key) || nativeContains(mResourceObject, key);
- }
-}
+++ /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.service.resourcecontainer.server;
-
-/**
- * This class describes the request.
- */
-public class RcsRequest {
-
- private final String mUri;
-
- private RcsRequest(String resourceUri) {
- mUri = resourceUri;
- }
-
- /**
- * Returns the URI of the request.
- *
- * @return Uri of the request in string form
- */
- public String getResourceUri() {
- return mUri;
- }
-}
+++ /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 RCSResourceObject.java
- *
- * This file contains the resource object APIs provided to the developers.
- * RCSResourceObject is a part of the server builder module.
- *
- */
-
-package org.iotivity.service.resourcecontainer.server;
-
-import java.lang.ref.WeakReference;
-
-import org.iotivity.service.resourcecontainer.RcsDestroyedObjectException;
-import org.iotivity.service.resourcecontainer.RcsException;
-import org.iotivity.service.resourcecontainer.RcsIllegalStateException;
-import org.iotivity.service.resourcecontainer.RcsObject;
-import org.iotivity.service.resourcecontainer.RcsPlatformException;
-import org.iotivity.service.resourcecontainer.RcsResourceAttributes;
-import org.iotivity.service.resourcecontainer.RcsValue;
-
-/**
- * RCSResourceObject represents a resource. It handles any requests from clients
- * automatically with attributes.
- * <p>
- * It also provides an auto notification mechanism that notifies to the
- * observers. Requests are handled automatically by defaultAction of
- * {@link RcsGetResponse} and {@link RcsSetResponse}. You can override them and
- * send your own response with {@link GetRequestHandler} and
- * {@link SetRequestHandler}.
- * <p>
- * For simple resources, they are simply required to notify whenever attributes
- * are changed by a set request. In this case, add an
- * {@link OnAttributeUpdatedListener} with a key interested in instead of
- * overriding {@link SetRequestHandler}.
- *
- * @see Builder
- */
-public final class RcsResourceObject extends RcsObject {
- /**
- * This is a builder to create resource with properties and attributes.
- *
- * The resource will be observable and discoverable by default, to make them
- * disable
- * set these properties explicitly with setDiscoverable and setObservable.
- *
- */
- public static class Builder {
- private final String mUri;
- private final String mType;
- private final String mInterface;
- private boolean mIsObservable = true;
- private boolean mIsDiscovervable = true;
- private RcsResourceAttributes mAttributes;
-
- /**
- * Constructs a Builder.
- *
- * @param uri
- * resource uri
- * @param resourceType
- * resource type
- * @param resourceInterface
- * resource interface
- *
- * @throws NullPointerException
- * if any parameter is null
- */
- public Builder(String uri, String resourceType,
- String resourceInterface) {
- if (uri == null) {
- throw new NullPointerException("uri is null.");
- }
- if (resourceType == null) {
- throw new NullPointerException("resourceType is null.");
- }
- if (resourceInterface == null) {
- throw new NullPointerException("resourceInterface is null.");
- }
-
- mUri = uri;
- mType = resourceType;
- mInterface = resourceInterface;
- }
-
- /**
- * Sets whether the resource is discoverable.
- *
- * @param isDiscoverable
- * whether to be discoverable or not
- *
- */
- public Builder setDiscoverable(boolean isDiscoverable) {
- mIsDiscovervable = isDiscoverable;
- return this;
- }
-
- /**
- * Sets the observable(OC_OBSERVABLE) property of the resource.
- *
- * @param isObservable
- * whether to be observable or not
- *
- */
- public Builder setObservable(boolean isObservable) {
- mIsObservable = isObservable;
- return this;
- }
-
- /**
- * Sets attributes foe the resource.
- *
- */
- public Builder setAttributes(RcsResourceAttributes attributes) {
- mAttributes = attributes;
- return this;
- }
-
- /**
- * Register a resource and returns a RCSResourceObject.
- *
- * @throws RcsPlatformException
- * If registering a resource is failed.
- *
- */
- public RcsResourceObject build() {
- return nativeBuild(mUri, mType, mInterface, mIsObservable,
- mIsDiscovervable, mAttributes);
- }
- }
-
- /**
- * This provides the way to get the attributes of RcsResourceObject with
- * lock.
- * When a thread holds the lock, the other threads will be pending until the
- * lock is released by unlock.
- *
- * Here is the standard idiom for AttributesLock:
- *
- * <pre>
- * {@code
- * AttributesLock lock = rcsResourceObject.getAttributesLock();
- *
- * try {
- * lock.lock();
- *
- * ....
- *
- * lock.apply();
- * } finally {
- * lock.unlock();
- * }
- * }
- * </pre>
- */
- public static class AttributesLock {
-
- private final WeakReference<RcsResourceObject> mResourceObjectRef;
-
- private RcsLockedAttributes mCurrentAttributes;
-
- private AttributesLock(RcsResourceObject resourceObj) {
- mResourceObjectRef = new WeakReference<RcsResourceObject>(
- resourceObj);
- }
-
- private RcsResourceObject ensureResourceObject() throws RcsException {
- final RcsResourceObject object = mResourceObjectRef.get();
-
- if (object == null || object.isDestroyed()) {
- throw new RcsDestroyedObjectException(
- "The object is already destroyed!");
- }
-
- return object;
- }
-
- /**
- * Locks the attributes of the RcsResourceObject and returns locked
- * attributes that can be modified until unlocked.
- *
- * @return Locked attributes.
- *
- * @throws RcsException
- * if the RcsResourceObject is destroyed
- */
- public RcsLockedAttributes lock() throws RcsException {
- return mCurrentAttributes = new RcsLockedAttributes(
- ensureResourceObject());
- }
-
- /**
- * Changes the state to unlock of the attributes of the
- * RcsResourceObject.
- *
- */
- public void unlock() {
- if (mCurrentAttributes == null) return;
-
- mCurrentAttributes.setUnlockState();
- mCurrentAttributes = null;
- }
-
- /**
- * Applies the modified attributes to the RcsResourceObject.
- *
- * @throws RcsIllegalStateException
- * if not in locked state
- */
- public void apply() throws RcsIllegalStateException {
- if (mCurrentAttributes == null) {
- throw new RcsIllegalStateException("it is not locked state.");
- }
- mCurrentAttributes.apply();
- }
- }
-
- private static native RcsResourceObject nativeBuild(String uri,
- String resourceType, String resourceInterface, boolean isObservable,
- boolean isDiscoverable, RcsResourceAttributes attributes);
-
- private native void nativeSetAttribute(String key, RcsValue value);
-
- private native RcsValue nativeGetAttributeValue(String key);
-
- private native boolean nativeRemoveAttribute(String key);
-
- private native boolean nativeContainsAttribute(String key);
-
- private native RcsResourceAttributes nativeGetAttributes();
-
- private native boolean nativeIsObservable();
-
- private native boolean nativeIsDiscoverable();
-
- private native void nativeNotify();
-
- private native void nativeSetAutoNotifyPolicy(AutoNotifyPolicy policy);
-
- private native AutoNotifyPolicy nativeGetAutoNotifyPolicy();
-
- private native void nativeSetSetRequestHandlerPolicy(
- SetRequestHandlerPolicy policy);
-
- private native SetRequestHandlerPolicy nativeGetSetRequestHandlerPolicy();
-
- private native void nativeSetGetRequestHandler(GetRequestHandler handler);
-
- private native void nativeSetSetRequestHandler(SetRequestHandler handler);
-
- private native void nativeAddAttributeUpdatedListener(String key,
- OnAttributeUpdatedListener listener);
-
- private native boolean nativeRemoveAttributeUpdatedListener(String key);
-
- private RcsResourceObject() {
- }
-
- /**
- * Represents the policy of AutoNotify function of RCSResourceObject class
- * In accord with this, observers are notified of attributes that are
- * changed or updated.
- *
- * <p>
- * Attributes are changed or updated according to execution of some
- * functions which modify attributes or receipt of set requests.
- *
- * @see setAttribute
- * @see removeAttribute
- * @see getAttributesLock
- *
- */
- public enum AutoNotifyPolicy {
- /** Never */
- NEVER,
-
- /** Always */
- ALWAYS,
-
- /** When attributes are changed */
- UPDATED
- }
-
- /**
- * Represents the policy of set-request handler.
- * In accord with this, the RCSResourceObject decides whether a set-request
- * is
- * acceptable or not.
- */
- public enum SetRequestHandlerPolicy {
- /**
- * Requests will be ignored if attributes of the request contain
- * a new key or a value that has different type from the current
- * value of the key.
- */
- NEVER,
-
- /**
- * The attributes of the request will be applied unconditionally
- * even if there are new name or type conflicts.
- */
- ACCEPT
- }
-
- /**
- * Interface definition for a handler to be invoked when a get request is
- * received.
- * <p>
- * The handler will be called first when a get request is received, before
- * the RCSResourceObject handles.
- *
- * @see setGetRequestHandler
- */
- public interface GetRequestHandler {
-
- /**
- * Called when received a get request from the client.
- *
- * @param request
- * Request information.
- * @param attributes
- * The attributes of the request.
- *
- * @return A response to be sent.
- *
- * @see RcsGetResponse
- */
- RcsGetResponse onGetRequested(RcsRequest request,
- RcsResourceAttributes attributes);
-
- }
-
- /**
- * Interface definition for a handler to be invoked when a set request is
- * received.
- * <p>
- * The handler will be called first when a get request is received, before
- * the RCSResourceObject handles. If the attributes are modified in the
- * callback, the modified attributes will be set in the RCSResourceObject if
- * the request is not ignored.
- *
- * @see setGetRequestHandler
- */
- public interface SetRequestHandler {
-
- /**
- * Called when received a set request from the client.
- *
- * @param request
- * request information
- * @param attributes
- * the attributes of the request.
- * it will be applied to the RcsResourceObject
- *
- * @return A response indicating how to handle this request.
- *
- * @see RcsSetResponse
- */
- RcsSetResponse onSetRequested(RcsRequest request,
- RcsResourceAttributes attributes);
-
- }
-
- /**
- * Interface definition for a callback to be invoked when an attribute is
- * updated.
- */
- public interface OnAttributeUpdatedListener {
-
- /**
- * Called when an attribute value is updated.
- *
- * @param oldValue
- * the attribute value before updated
- * @param newValue
- * the current resource attribute value
- */
- void onAttributeUpdated(RcsValue oldValue, RcsValue newValue);
- }
-
- private void assertAlive() throws RcsException {
- if (!hasHandle()) {
- throw new RcsDestroyedObjectException(
- "The object is already destroyed!");
- }
- }
-
- /**
- * Sets a particular attribute value.
- *
- * @param key
- * key with which the specified value is to be associated
- * @param value
- * value to be associated with the specified key
- *
- * @throws RcsDestroyedObjectException
- * if the object is destroyed
- * @throws NullPointerException
- * if key or value is null
- *
- */
- public void setAttribute(String key, RcsValue value) throws RcsException {
- assertAlive();
-
- if (key == null) throw new NullPointerException("key is null");
- if (value == null) throw new NullPointerException("value is null");
-
- nativeSetAttribute(key, value);
- }
-
- /**
- * Returns a copied attribute value associated with the supplied key.
- *
- * @param key
- * the key whose associated value is to be returned
- *
- * @return the value to which the specified key is mapped, or null if no
- * attribute for the key
- *
- * @throws RcsDestroyedObjectException
- * if the object is destroyed
- * @throws NullPointerException
- * if key is null
- */
- public RcsValue getAttributeValue(String key) throws RcsException {
- assertAlive();
-
- if (key == null) throw new NullPointerException("key is null");
- return nativeGetAttributeValue(key);
- }
-
- /**
- * Removes the mapping for a key from the attributes if it is present.
- *
- * @param key
- * key whose mapping is to be removed
- *
- * @return true if the key is present and the the value mapped is removed.
- *
- * @throws RcsDestroyedObjectException
- * if the object is destroyed
- * @throws NullPointerException
- * if key is null
- */
- public boolean removeAttribute(String key) throws RcsException {
- assertAlive();
-
- if (key == null) throw new NullPointerException("key is null");
- return nativeRemoveAttribute(key);
- }
-
- /**
- * Returns true if the attributes contains a mapping for the specified key.
- *
- * @param key
- * key whose presence is to be tested
- *
- * @return true if the attributes contains a mapping for the specified key.
- *
- * @throws RcsDestroyedObjectException
- * if the object is destroyed
- * @throws NullPointerException
- * if key is null
- */
- public boolean containsAttribute(String key) throws RcsException {
- assertAlive();
-
- if (key == null) throw new NullPointerException("key is null");
- return nativeContainsAttribute(key);
- }
-
- /**
- * Returns a copied attributes of the RCSResourceObject.
- * To modify the attributes, use {@link AttributesLock}.
- *
- * @throws RcsDestroyedObjectException
- * if the object is destroyed
- *
- * @see getAttributesLock
- */
- public RcsResourceAttributes getAttributes() throws RcsException {
- assertAlive();
-
- return nativeGetAttributes();
- }
-
- /**
- * Returns an AttributesLock for this RcsResourceObject.
- *
- * @throws RcsDestroyedObjectException
- * if the object is destroyed
- */
- public AttributesLock getAttributesLock() throws RcsException {
- assertAlive();
-
- return new AttributesLock(this);
- }
-
- /**
- * Checks whether the resource is observable or not.
- *
- * @throws RcsDestroyedObjectException
- * if the object is destroyed
- */
- public boolean isObservable() throws RcsException {
- assertAlive();
-
- return nativeIsObservable();
- }
-
- /**
- * Checks whether the resource is discoverable or not.
- *
- * @throws RcsDestroyedObjectException
- * if the object is destroyed
- */
- public boolean isDiscoverable() throws RcsException {
- assertAlive();
-
- return nativeIsDiscoverable();
- }
-
- /**
- * Sets the get request handler. To remove handler, pass null.
- *
- * Default behavior is {@link RcsGetResponse#defaultAction()}.
- *
- * @throws RcsDestroyedObjectException
- * if the object is destroyed
- */
- public void setGetRequestHandler(GetRequestHandler handler)
- throws RcsException {
- assertAlive();
-
- nativeSetGetRequestHandler(handler);
- }
-
- /**
- * Sets the set request handler. To remove handler, pass null.
- *
- * Default behavior is {@link RcsSetResponse#defaultAction()}.
- *
- * @throws RcsDestroyedObjectException
- * if the object is destroyed
- *
- */
- public void setSetRequestHandler(SetRequestHandler handler)
- throws RcsException {
- assertAlive();
-
- nativeSetSetRequestHandler(handler);
- }
-
- /**
- * Adds a listener for a particular attribute updated.
- *
- * @param key
- * the interested attribute's key
- * @param listener
- * listener to be invoked
- *
- * @throws NullPointerException
- * if key or listener is null
- * @throws RcsDestroyedObjectException
- * if the object is destroyed
- */
- public void addAttributeUpdatedListener(String key,
- OnAttributeUpdatedListener listener) throws RcsException {
- assertAlive();
-
- if (key == null) {
- throw new NullPointerException("key is null.");
- }
- if (listener == null) {
- throw new NullPointerException("listener is null.");
- }
-
- nativeAddAttributeUpdatedListener(key, listener);
- }
-
- /**
- * Removes a listener for a particular attribute updated.
- *
- * @param key
- * key the key associated with the listener to be removed
- *
- * @return true if the listener added with same key exists and is removed.
- *
- * @throws RcsDestroyedObjectException
- * if the object is destroyed
- * @throws NullPointerException
- * if key is null
- */
- public boolean removeAttributeUpdatedListener(String key)
- throws RcsException {
- assertAlive();
-
- if (key == null) throw new NullPointerException("key is null");
- return nativeRemoveAttributeUpdatedListener(key);
- }
-
- /**
- * Notifies all observers of the current attributes.
- *
- * @throws RcsDestroyedObjectException
- * if the object is destroyed
- * @throws RcsPlatformException
- * if the operation failed
- */
- public void notifyObservers() throws RcsException {
- assertAlive();
-
- nativeNotify();
- }
-
- /**
- * Sets auto notify policy
- *
- * @param policy
- * policy to be set
- *
- * @throws RcsDestroyedObjectException
- * if the object is destroyed
- *
- */
- public void setAutoNotifyPolicy(AutoNotifyPolicy policy)
- throws RcsException {
- assertAlive();
-
- if (policy == null) throw new NullPointerException("policy is null");
- nativeSetAutoNotifyPolicy(policy);
- }
-
- /**
- * Returns the current policy
- *
- * @throws RcsDestroyedObjectException
- * if the object is destroyed
- *
- */
- public AutoNotifyPolicy getAutoNotifyPolicy() throws RcsException {
- assertAlive();
-
- return nativeGetAutoNotifyPolicy();
- }
-
- /**
- * Sets the policy for handling a set request.
- *
- * @param policy
- * policy to be set
- *
- * @throws RcsDestroyedObjectException
- * if the object is destroyed
- *
- */
- public void setSetRequestHandlerPolicy(SetRequestHandlerPolicy policy)
- throws RcsException {
- assertAlive();
-
- if (policy == null) throw new NullPointerException("policy is null");
- nativeSetSetRequestHandlerPolicy(policy);
- }
-
- /**
- * Returns the current policy.
- *
- * @throws RcsDestroyedObjectException
- * if the object is destroyed
- */
- public SetRequestHandlerPolicy getSetRequestHandlerPolicy()
- throws RcsException {
- assertAlive();
-
- return nativeGetSetRequestHandlerPolicy();
- }
-
- private boolean isDestroyed() {
- return !hasHandle();
- }
-
- /**
- * Unregister the resource and reclaims all resources used by this object.
- * This must be called if the resource is not used any longer.
- *
- */
- public void destroy() {
- super.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.
- *
- ******************************************************************/
-
-package org.iotivity.service.resourcecontainer.server;
-
-import org.iotivity.service.resourcecontainer.RcsResourceAttributes;
-
-class RcsResponse {
- private native static int nativeGetDefaultErrorCode();
-
- public static final int DEFAULT_ERROR_CODE;
-
- static {
- DEFAULT_ERROR_CODE = nativeGetDefaultErrorCode();
- }
-
- private final int mErrorCode;
- private final RcsResourceAttributes mAttrs;
-
- RcsResponse() {
- this(DEFAULT_ERROR_CODE);
- }
-
- RcsResponse(RcsResourceAttributes attrs) {
- this(attrs, DEFAULT_ERROR_CODE);
- }
-
- RcsResponse(int errorCode) {
- this(null, errorCode);
- }
-
- RcsResponse(RcsResourceAttributes attrs, int errorCode) {
- mErrorCode = errorCode;
- mAttrs = attrs;
- }
-}
+++ /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.service.resourcecontainer.server;
-
-import org.iotivity.service.resourcecontainer.RcsResourceAttributes;
-
-/**
- * This class provides methods to create the response for a received set
- * request.
- *
- * @see RcsResourceObject
- * @see RcsSetResponse
- */
-public final class RcsSetResponse extends RcsResponse {
- /**
- * Options for handling a set request.
- *
- * This overrides {@link RcsResourceObject.SetRequestHandlerPolicy}.
- *
- */
- public enum AcceptanceMethod {
- /**
- * Follow {@link RcsResourceObject.SetRequestHandlerPolicy}.
- */
- DEFAULT,
-
- /**
- * Accept the request attributes even if there is an unknown key or
- * mismatched type.
- */
- ACCEPT,
-
- /**
- * Ignore the request attributes.
- */
- IGNORE
- };
-
- private AcceptanceMethod mAcceptanceMethod = AcceptanceMethod.DEFAULT;
-
- /**
- * Creates a default RcsSetResponse with {@link AcceptanceMethod#DEFAULT}.
- * The response will have {@link #DEFAULT_ERROR_CODE} for the errorCode. The
- * attributes of {@link RcsResourceObject} will be set as the result
- * attributes.
- *
- */
- public static RcsSetResponse defaultAction() {
- return new RcsSetResponse();
- }
-
- /**
- * Creates a default RcsSetResponse with {@link AcceptanceMethod#ACCEPT}
- * The response will have {@link #DEFAULT_ERROR_CODE} for the errorCode. The
- * attributes of {@link RcsResourceObject} will be set as the result
- * attributes.
- *
- */
- public static RcsSetResponse accept() {
- return new RcsSetResponse()
- .setAcceptanceMethod(AcceptanceMethod.ACCEPT);
- }
-
- /**
- * Creates a RcsSetResponse with {@link AcceptanceMethod#ACCEPT} and error
- * code passed.
- * The attributes of the {@link RcsResourceObject} will be set as the result
- * attributes.
- *
- * @param errorCode
- * error code to be set in response
- *
- */
- public static RcsSetResponse accept(int errorCode) {
- return new RcsSetResponse(errorCode)
- .setAcceptanceMethod(AcceptanceMethod.ACCEPT);
- }
-
- /**
- * Creates a default RcsSetResponse with {@link AcceptanceMethod#IGNORE}.
- * The response will have {@link #DEFAULT_ERROR_CODE} for the errorCode. The
- * attributes of {@link RcsResourceObject} will be set as the result
- * attributes.
- *
- */
- public static RcsSetResponse ignore() {
- return new RcsSetResponse()
- .setAcceptanceMethod(AcceptanceMethod.IGNORE);
- }
-
- /**
- * Creates a RcsSetResponse with {@link AcceptanceMethod#IGNORE} and error
- * code passed. The attributes of the {@link RcsResourceObject} will be set
- * as the result attributes.
- *
- * @param errorCode
- * error code to be set in response
- *
- */
- public static RcsSetResponse ignore(int errorCode) {
- return new RcsSetResponse(errorCode)
- .setAcceptanceMethod(AcceptanceMethod.IGNORE);
- }
-
- /**
- * Creates a RcsSetResponse with error code passed and
- * {@link AcceptanceMethod#DEFAULT}. The attributes of the
- * {@link RcsResourceObject} will be set as the result attributes.
- *
- * @param errorCode
- * error code to be set in response
- *
- */
- public static RcsSetResponse create(int errorCode) {
- return new RcsSetResponse(errorCode);
- }
-
- /**
- * Creates a RcsSetResponse with custom attributes and
- * {@link AcceptanceMethod#DEFAULT}. This sends the passed attributes as the
- * result attributes instead of one the {@link RcsResourceObject} holds.
- *
- * @param attributes
- * attributes to be sent as the result
- *
- */
- public static RcsSetResponse create(RcsResourceAttributes attributes) {
- return new RcsSetResponse(attributes);
- }
-
- /**
- * Creates a RcsSetResponse with error code passed and
- * {@link AcceptanceMethod#DEFAULT}. This sends the passed attributes as the
- * result attributes instead of one the {@link RcsResourceObject} holds.
- *
- * @param attributes
- * attributes to be sent as the result
- * @param errorCode
- * error code for response
- *
- */
- public static RcsSetResponse create(RcsResourceAttributes attributes,
- int errorCode) {
- return new RcsSetResponse(attributes, errorCode);
- }
-
- /**
- * Returns the acceptance method.
- *
- */
- public AcceptanceMethod getAcceptanceMethod() {
- return mAcceptanceMethod;
- }
-
- /**
- * Sets the acceptance method.
- *
- * @param method
- * method to be set
- *
- * @return The reference to this RcsSetResponse
- *
- */
- public RcsSetResponse setAcceptanceMethod(AcceptanceMethod method) {
- mAcceptanceMethod = method;
- return this;
- }
-
- private RcsSetResponse() {
- super();
- }
-
- private RcsSetResponse(int errorCode) {
- super(errorCode);
- }
-
- private RcsSetResponse(RcsResourceAttributes attrs) {
- super(attrs);
- }
-
- private RcsSetResponse(RcsResourceAttributes attrs, int errorCode) {
- super(attrs, errorCode);
- }
-
-}
+++ /dev/null
-package org.iotivity.service.resourcecontainer.server;
-
-import org.iotivity.service.resourcecontainer.RcsException;
-
-/**
- * Thrown when trying to access a unlocked {@link RcsLockedAttributes}.
- *
- */
-public class RcsUnlockedException extends RcsException {
-
- private static final long serialVersionUID = 4292243643497860992L;
-
- public RcsUnlockedException(String message) {
- super(message);
- }
-
-}
#include "AndroidResource.h"
#include "JniRcsResourceAttributes.h"
+#include "JavaClasses.h"
+#include "JavaLocalRef.h"
+#include "JNIEnvWrapper.h"
+#include "JniRcsValue.h"
#include <jni.h>
#include <string.h>
using namespace OIC::Service;
using namespace std;
+namespace
+{
+ jclass g_cls_RCSBundleInfo;
+ jfieldID g_field_mNativeHandle;
+}
+
+void initRCSAndroidResource(JNIEnvWrapper *env)
+{
+ auto clsAndroidBundleResource = env->FindClass(PACKAGE_NAME "/AndroidBundleResource");
+
+ g_field_mNativeHandle = env->GetFieldID(clsAndroidBundleResource, "mNativeHandle", "J");
+}
AndroidResource::AndroidResource()
{
LOGD("Looking for getter.");
m_attributeGetRequestHandler = m_env->GetMethodID(m_bundleResourceClass,
"handleGetAttributesRequest", "()Lorg/iotivity/service/resourcecontainer/RcsResourceAttributes;");
+ jclass androidBundleSoftSensorResourceClass = m_env->FindClass("org/iotivity/service/resourcecontainer/AndroidBundleSoftSensorResource");
+ m_onUpdatedInputResource = m_env->GetMethodID(androidBundleSoftSensorResourceClass, "onUpdatedInputResource", "(java/lang/String,java/util/Vector)V");
+ m_vectorClazz = m_env->FindClass("java/util/Vector");
+ m_vectorAddMethod = m_env->GetMethodID(m_vectorClazz, "add", "(java/lang/Object)V");
LOGD("Get java vm.");
int jvmAccess = m_env->GetJavaVM(&m_jvm);
LOGD("JVM: %s", (jvmAccess ? "false" : "true") );
RCSResourceAttributes attrs = toNativeAttributes(m_env, responseObj);
LOGD("Received attributes %d", attrs.size());
- BundleResource::setAttributes(attrs);
+ BundleResource::setAttributes(attrs, false);
}
m_jvm->DetachCurrentThread();
LOGD("BundleResource::getAttributes().size() %d", BundleResource::getAttributes().size());
return BundleResource::getAttributes();
}
+
+void AndroidResource::executeLogic(){
+ LOGD("executeLogic");
+
+}
+
+void AndroidResource::onUpdatedInputResource(const std::string attributeName,
+ std::vector<RCSResourceAttributes::Value> values){
+ LOGD("onUpdatedInputResource");
+
+ int attached = m_jvm->AttachCurrentThread(&m_env, NULL);
+ jobject valueObj;
+ if(attached>0)
+ {
+ LOGE("Failed to attach thread to JavaVM");
+ }
+ else{
+ jobject obj = m_env->NewObject(m_vectorClazz, m_env->GetMethodID(m_vectorClazz, "<init>", "()V"));
+
+ for (int n=0;n<values.size();n++)
+ {
+ valueObj = newRCSValueObject(m_env, values[n]);
+ m_env->CallVoidMethod(obj, m_vectorAddMethod, valueObj);
+ }
+ m_env->CallObjectMethod(m_bundleResource,
+ m_onUpdatedInputResource, obj);
+ m_jvm->DetachCurrentThread();
+ }
+ LOGD("BundleResource::getAttributes().size() %d", BundleResource::getAttributes().size());
+}
+
+JNIEXPORT void JNICALL Java_org_iotivity_service_resourcecontainer_AndroidBundleResource_updateNativeInstance
+(JNIEnv* env, jobject obj, jobject updates)
+{
+ LOGD("updateNativeInstance");
+ BundleResource* androidResource = reinterpret_cast<BundleResource*>(env->GetLongField(obj, g_field_mNativeHandle));
+ RCSResourceAttributes attrs = toNativeAttributes(env, updates);
+ LOGD("Received attributes %d", attrs.size());
+ androidResource->setAttributes(attrs, true);
+}
#include <string>
#include <jni.h>
#include "BundleResource.h"
+#include "SoftSensorResource.h"
#include "ResourceContainerImpl.h"
+#include <jni.h>
+
+class JNIEnvWrapper;
+
+void initRCSAndroidResource(JNIEnvWrapper *);
+
using namespace std;
namespace OIC
{
namespace Service
{
- class AndroidResource: public BundleResource
+ class AndroidResource: public SoftSensorResource
{
public:
AndroidResource();
virtual RCSResourceAttributes& handleGetAttributesRequest();
+ /**
+ * SoftSensor logic. Has to be provided by the soft sensor developer.
+ * This function will be executed if an input attribute is updated.
+ *
+ * @return void
+ */
+ virtual void executeLogic();
+
+ /**
+ * Callback from the client module in the container.
+ * This function will be called if input data from remote resources are updated.
+ * SoftSensor resource can get a vector of input data from multiple input resources
+ * which have attributeName that softsensor needs to execute its logic.
+ *
+ * @param attributeName Attribute key of input data
+ *
+ * @param values Vector of input data value
+ *
+ * @return void
+ */
+ virtual void onUpdatedInputResource(const std::string attributeName,
+ std::vector<RCSResourceAttributes::Value> values);
+
virtual void initAttributes();
private:
// needs to be a GlobalRef
jclass m_bundleResourceClass;
jmethodID m_attributeSetRequestHandler;
jmethodID m_attributeGetRequestHandler;
+ jmethodID m_onUpdatedInputResource;
+ jclass m_vectorClazz;
+ jmethodID m_vectorAddMethod;
string m_bundleId;
JNIEnv *m_env;
JavaVM *m_jvm;
+ jfieldID g_field_mNativeHandle;
};
}
}
+#ifdef __cplusplus
+extern "C" {
#endif
+/*
+ * Class: org_iotivity_service_resourcecontainer_AndroidBundleResource
+ * Method: updateNativeInstance
+ * Signature: (Lorg/iotivity/service/resourcecontainer/RcsResourceAttributes;)V
+ */
+JNIEXPORT void JNICALL Java_org_iotivity_service_resourcecontainer_AndroidBundleResource_updateNativeInstance
+ (JNIEnv *, jobject, jobject);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
+
#include "JavaClasses.h"
#include "JNIEnvWrapper.h"
#include "Log.h"
-#include "JniRcsObject.h"
#include "JniRcsValue.h"
#include "JavaExceptions.h"
#include "JniRcsValue.h"
+#include "AndroidResource.h"
#define LOG_TAG "JNI-Main"
initRCSValue(&envWrapper);
initRCSResourceContainer(&envWrapper);
initRCSResourceAttributes(&envWrapper);
- initRCSObject(&envWrapper);
+ initRCSAndroidResource(&envWrapper);
}
catch (const JavaException &)
{
+++ /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 "JniRcsObject.h"
-
-#include "JavaClasses.h"
-#include "JNIEnvWrapper.h"
-#include "Log.h"
-#include "Verify.h"
-
-#define LOG_TAG "JNI-RCSObject"
-
-jfieldID g_field_mNativeHandle;
-
-void initRCSObject(JNIEnvWrapper* env)
-{
- auto clsRCSObject = env->FindClass(PACKAGE_NAME "/RcsObject");
-
- g_field_mNativeHandle = env->GetFieldID(clsRCSObject, "mNativeHandle", "J");
-}
-
-void clearRCSObject(JNIEnvWrapper* env)
-{
-}
-
-JNIEXPORT void JNICALL
-Java_org_iotivity_service_RcsObject_nativeDispose(JNIEnv* env, jobject obj)
-{
- LOGD("release nativeHandle!");
- releaseNativeHandle(env, obj);
-}
+++ /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 JNI_RCS_OBJECT_H_
-#define JNI_RCS_OBJECT_H_
-
-#include <jni.h>
-
-#include <memory>
-
-#include "JavaClasses.h"
-#include "JNIEnvWrapper.h"
-
-extern jfieldID g_field_mNativeHandle;
-
-void initRCSObject(JNIEnvWrapper*);
-void clearRCSObject(JNIEnvWrapper*);
-
-namespace Detail
-{
- struct BaseHandleHolder
- {
- virtual ~BaseHandleHolder() {}
- };
-
- template< typename T >
- struct HandleHolder: public BaseHandleHolder
- {
- HandleHolder(T* ptr) : m_ptr { ptr } {}
-
- virtual ~HandleHolder() { delete m_ptr; }
-
- T* m_ptr;
- };
-
- template< typename ENV >
- void* getNativeHandle(ENV* env, jobject obj)
- {
- return reinterpret_cast< void* >(env->GetLongField(obj, g_field_mNativeHandle));
- }
-}
-
-template< typename ENV >
-bool hasNativeHandle(ENV* env, jobject obj)
-{
- return Detail::getNativeHandle(env, obj) != nullptr;
-}
-
-template< typename T, typename ENV, typename ...PARAMS >
-inline void setSafeNativeHandle(ENV* env, jobject obj, PARAMS&&... params)
-{
- static_assert(!std::is_array< T >::value, "Array is not supported!");
-
- std::unique_ptr< Detail::HandleHolder< T > > p(
- new Detail::HandleHolder< T >{ new T{ std::forward< PARAMS >(params)... } });
-
- env->SetLongField(obj, g_field_mNativeHandle, reinterpret_cast< jlong >(p.get()));
-
- if (env->ExceptionCheck()) return;
-
- p.release();
-}
-
-template< typename ENV >
-void releaseNativeHandle(ENV* env, jobject obj)
-{
- auto handleHolder = reinterpret_cast< Detail::BaseHandleHolder* >(
- env->GetLongField(obj, g_field_mNativeHandle));
-
- delete handleHolder;
-
- env->SetLongField(obj, g_field_mNativeHandle, 0);
-}
-
-
-template< typename T >
-inline T& getNativeHandleAs(JNIEnv* env, jobject obj)
-{
- auto handleHolder = static_cast< Detail::HandleHolder< T >* >(Detail::getNativeHandle(env, obj));
-
- if (!handleHolder)
- {
- env->ThrowNew(env->FindClass(EXC_NAME_ILLEGAL_STATE), "Internal handle is null!");
- }
-
- return *handleHolder->m_ptr;
-}
-
-template< typename T >
-inline T& getNativeHandleAs(JNIEnvWrapper* env, jobject obj)
-{
- getNativeHandleAs< T >(env->get(), obj);
- if (env->ExceptionCheck()) throw JavaException();
-}
-
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-JNIEXPORT void JNICALL
-Java_org_iotivity_service_RcsObject_nativeDispose(JNIEnv*, jobject);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif // JNI_RCS_OBJECT_H_
#include "JniRcsResourceAttributes.h"
-#include "JniRcsObject.h"
+#include "JNIEnvWrapper.h"
+#include "JavaClasses.h"
#include "JavaLocalRef.h"
#include "JniRcsValue.h"
#include "Log.h"
jobject obj = env->NewObject(g_cls_RCSResourceAttributes, g_ctor_RCSResourceAttributes);
VERIFY_NO_EXC_RET_DEF(env);
- setSafeNativeHandle< RCSResourceAttributes >(env, obj, attrs);
+ jobject mapObj = env->GetObjectField(obj, g_field_mCache);
+
+ //EXPECT(mapObj, "Map is null.");
+ for (const auto& p : attrs) {
+ JavaLocalObject keyObj{ env, newStringObject(env, p.key()) };
+ //VERIFY_NO_EXC(env);
+
+ JavaLocalObject valueObj{ env, newRCSValueObject(env, p.value()) };
+ //VERIFY_NO_EXC(env);
+
+ invoke_Map_put(env, mapObj, keyObj, valueObj);
+ //VERIFY_NO_EXC(env);
+ }
+
+ // setSafeNativeHandle< RCSResourceAttributes >(env, obj, attrs);
return obj;
}
{
jobject obj = env->NewObject(g_cls_RCSResourceAttributes, g_ctor_RCSResourceAttributes);
- setSafeNativeHandle< RCSResourceAttributes >(env, obj, attrs);
+ jobject mapObj = env->GetObjectField(obj, g_field_mCache);
+
+ //EXPECT(mapObj, "Map is null.");
+ for (const auto& p : attrs) {
+ JavaLocalObject keyObj{ env, newStringObject(env, p.key()) };
+ //VERIFY_NO_EXC(env);
+
+ JavaLocalObject valueObj{ env, newRCSValueObject(env, p.value()) };
+ //VERIFY_NO_EXC(env);
+
+ invoke_Map_put(env, mapObj, keyObj, valueObj);
+ //VERIFY_NO_EXC(env);
+ }
+
+// setSafeNativeHandle< RCSResourceAttributes >(env, obj, attrs);
return obj;
}
LOGD("in write native attributes from map finished");
}
-JNIEXPORT jboolean JNICALL Java_org_iotivity_service_resourcecontainer_RcsResourceAttributes_nativeIsEmpty
-(JNIEnv* env, jobject obj)
-{
- LOGD("isEmpty");
- EXPECT_RET(hasNativeHandle(env, obj), "no native handle.", true);
-
- auto& attrs = getNativeHandleAs< RCSResourceAttributes >(env, obj);
- VERIFY_NO_EXC_RET_DEF(env);
-
- return attrs.empty();
-}
-
-JNIEXPORT jint JNICALL Java_org_iotivity_service_resourcecontainer_RcsResourceAttributes_nativeSize
-(JNIEnv* env, jobject obj)
-{
- LOGD("size");
- EXPECT_RET(hasNativeHandle(env, obj), "no native handle.", 0);
-
- auto& attrs = getNativeHandleAs< RCSResourceAttributes >(env, obj);
- VERIFY_NO_EXC_RET_DEF(env);
-
- return attrs.size();
-}
-
-JNIEXPORT jboolean JNICALL Java_org_iotivity_service_resourcecontainer_RcsResourceAttributes_nativeRemove
-(JNIEnv* env, jobject obj, jstring keyObj)
-{
- LOGD("remove");
- EXPECT_RET_DEF(keyObj, "Key is null.");
- EXPECT_RET_DEF(hasNativeHandle(env, obj), "no native handle.");
-
- auto& attrs = getNativeHandleAs< RCSResourceAttributes >(env, obj);
- VERIFY_NO_EXC_RET_DEF(env);
- const auto& key = toStdString(env, keyObj);
- VERIFY_NO_EXC_RET_DEF(env);
-
- auto ret = attrs.erase(key);
-
- if (attrs.empty()) releaseNativeHandle(env, obj);
-
- return ret;
-}
-
-JNIEXPORT void JNICALL
-Java_org_iotivity_service_resourcecontainer_RcsResourceAttributes_nativeClear
-(JNIEnv* env, jobject obj)
-{
- LOGD("clear");
-
- releaseNativeHandle(env, obj);
-}
-
-JNIEXPORT jboolean JNICALL Java_org_iotivity_service_resourcecontainer_RcsResourceAttributes_nativeContains
-(JNIEnv *env, jobject obj, jstring keyObj)
-{
- LOGD("contains");
- EXPECT_RET(keyObj, "Key is null.", false);
- EXPECT_RET(hasNativeHandle(env, obj), "no native handle.", false);
-
- auto& attrs = getNativeHandleAs< RCSResourceAttributes >(env, obj);
- VERIFY_NO_EXC_RET_DEF(env);
- const auto& key = toStdString(env, keyObj);
- VERIFY_NO_EXC_RET_DEF(env);
- return attrs.contains(key);
-}
-
-JNIEXPORT void JNICALL Java_org_iotivity_service_resourcecontainer_RcsResourceAttributes_nativeAddKeys
-(JNIEnv *env, jobject obj, jstring setObj)
-{
- LOGD("addKeys");
- EXPECT(setObj, "set is null.");
- EXPECT(hasNativeHandle(env, obj), "no native handle.");
-
- auto& attrs = getNativeHandleAs< RCSResourceAttributes >(env, obj);
- VERIFY_NO_EXC(env);
-
- for (const auto& keyValue : attrs)
- {
- JavaLocalString localObj{ env, env->NewStringUTF(keyValue.key().c_str()) };
- VERIFY_NO_EXC(env);
-
- invoke_Collection_add(env, setObj, localObj);
- VERIFY_NO_EXC(env);
- }
-}
-
-JNIEXPORT jobject JNICALL Java_org_iotivity_service_resourcecontainer_RcsResourceAttributes_nativeExtract
-(JNIEnv* env, jobject obj, jstring keyObj)
-{
- LOGD("extract");
- EXPECT_RET_DEF(keyObj, "Key is null.");
- EXPECT_RET_DEF(hasNativeHandle(env, obj), "no native handle.");
-
- auto& attrs = getNativeHandleAs< RCSResourceAttributes >(env, obj);
- VERIFY_NO_EXC_RET_DEF(env);
-
- const auto& key = toStdString(env, keyObj);
- VERIFY_NO_EXC_RET_DEF(env);
-
- EXPECT_RET_DEF(attrs.contains(key), "no matched value");
-
- jobject valueObj = newRCSValueObject(env, attrs[key]);
- VERIFY_NO_EXC_RET_DEF(env);
-
- attrs.erase(key);
- if (attrs.empty()) releaseNativeHandle(env, obj);
- return valueObj;
-}
-
-JNIEXPORT void JNICALL
-Java_org_iotivity_service_resourcecontainer_RcsResourceAttributes_nativeExtractAll
-(JNIEnv* env, jobject obj, jobject mapObj)
-{
- LOGD("extractAll");
- EXPECT(mapObj, "Map is null.");
- EXPECT(hasNativeHandle(env, obj), "no native handle.");
-
- auto& attrs = getNativeHandleAs< RCSResourceAttributes >(env, obj);
- VERIFY_NO_EXC(env);
-
- for (const auto& p : attrs) {
- JavaLocalObject keyObj{ env, newStringObject(env, p.key()) };
- VERIFY_NO_EXC(env);
-
- JavaLocalObject valueObj{ env, newRCSValueObject(env, p.value()) };
- VERIFY_NO_EXC(env);
-
- invoke_Map_put(env, mapObj, keyObj, valueObj);
- VERIFY_NO_EXC(env);
- }
-
- attrs.clear();
- releaseNativeHandle(env, obj);
-}
-
void writeNativeAttributesFromMap(JNIEnvWrapper*, jobject mapObj,
OIC::Service::RCSResourceAttributes& targetAttrs);
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-JNIEXPORT jboolean JNICALL
-Java_org_iotivity_service_resourcecontainer_RcsResourceAttributes_nativeIsEmpty(JNIEnv*, jobject);
-
-JNIEXPORT jint JNICALL
-Java_org_iotivity_service_resourcecontainer_RcsResourceAttributes_nativeSize(JNIEnv*, jobject);
-
-JNIEXPORT jboolean JNICALL
-Java_org_iotivity_service_resourcecontainer_RcsResourceAttributes_nativeRemove(JNIEnv*, jobject, jstring keyObj);
-
-JNIEXPORT void JNICALL
-Java_org_iotivity_service_resourcecontainer_RcsResourceAttributes_nativeClear(JNIEnv*, jobject);
-
-JNIEXPORT jboolean JNICALL
-Java_org_iotivity_service_resourcecontainer_RcsResourceAttributes_nativeContains(JNIEnv*, jobject, jstring keyObj);
-
-JNIEXPORT void JNICALL
-Java_org_iotivity_service_resourcecontainer_RcsResourceAttributes_nativeAddKeys(JNIEnv*, jobject, jstring setObj);
-
-JNIEXPORT jobject JNICALL
-Java_org_iotivity_service_resourcecontainer_RcsResourceAttributes_nativeExtract(JNIEnv*, jobject, jstring keyObj);
-
-JNIEXPORT void JNICALL
-Java_org_iotivity_service_resourcecontainer_RcsResourceAttributes_nativeExtractAll(JNIEnv*, jobject, jobject);
-
-#ifdef __cplusplus
-}
-#endif
-
#endif //JNI_RCS_RESOURCE_ATTRIBUTES_H_
namespace
{
jclass g_cls_RCSBundleInfo;
+ jfieldID g_field_mNativeHandle;
jmethodID g_ctor_RCSBundleInfo;
AS_SIG(CLS_NAME_STRING)
AS_SIG(CLS_NAME_STRING)
")V");
+
+ auto clsAndroidBundleResource = env->FindClass(PACKAGE_NAME "/AndroidBundleResource");
+
+ g_field_mNativeHandle = env->GetFieldID(clsAndroidBundleResource, "mNativeHandle", "J");
}
void clearRCSResourceContainer(JNIEnvWrapper *env)
androidResource->m_uri = string(str_uri, strlen(str_uri));
androidResource->m_resourceType = string(str_resourceType, strlen(str_resourceType));
androidResource->m_name = string(str_res_name, strlen(str_res_name));
+
+ // link java resource instance to c++ resource instance
+ env->SetLongField(bundleResource, g_field_mNativeHandle, reinterpret_cast< jlong >(androidResource.get()));
+
container->registerResource(androidResource);
android_resources[str_uri] = androidResource;
#include "JniRcsValue.h"
-#include "JniRcsObject.h"
+#include "JNIEnvWrapper.h"
#include "JniRcsResourceAttributes.h"
#include "JavaClasses.h"
#include "JavaExceptions.h"
#define CLS_NAME_VALUE PACKAGE_NAME "/RcsValue"
#define CLS_NAME_RESOURCEATTRIBUTES PACKAGE_NAME "/RcsResourceAttributes"
-#define CLS_NAME_REMOTERESOURCEOBJECT PACKAGE_NAME "/client/RcsRemoteResourceObject"
-
-#define EXC_NAME_RCS PACKAGE_NAME "/RcsException"
-#define EXC_NAME_PLATFORM PACKAGE_NAME "/RcsPlatformException"
-#define EXC_NAME_ILLEGAL_STATE PACKAGE_NAME "/RcsIllegalStateException"
#define CLS_NAME_OBJECT "java/lang/Object"
#define CLS_NAME_STRING "java/lang/String"
void initJavaExceptions(JNIEnvWrapper* env)
{
- g_cls_PlatformException = env->FindClassAsGlobalRef(EXC_NAME_PLATFORM);
+ /*g_cls_PlatformException = env->FindClassAsGlobalRef(EXC_NAME_PLATFORM);
g_ctor_PlatformException = env->GetConstructorID(g_cls_PlatformException,
- "(" AS_SIG(CLS_NAME_STRING) "I)V");
+ "(" AS_SIG(CLS_NAME_STRING) "I)V");*/
}
void clearJavaExceptions(JNIEnvWrapper* env)
{
- env->DeleteGlobalRef(g_cls_PlatformException);
+ //env->DeleteGlobalRef(g_cls_PlatformException);
}
void throwPlatformException(JNIEnv* env, const OIC::Service::RCSPlatformException& e)
{
- auto msg = newStringObject(env, e.getReason());
+ /*auto msg = newStringObject(env, e.getReason());
VERIFY_NO_EXC(env);
auto exObj = env->NewObject(g_cls_PlatformException, g_ctor_PlatformException,
msg, e.getReasonCode());
VERIFY_NO_EXC(env);
- env->Throw(static_cast< jthrowable >(exObj));
+ env->Throw(static_cast< jthrowable >(exObj));*/
}
template < typename ENV >
void throwRCSException(ENV* env, const char* msg)
{
- env->ThrowNew(env->FindClass(EXC_NAME_RCS), msg);
+ //env->ThrowNew(env->FindClass(EXC_NAME_RCS), msg);
}
*/
void setAttributes(RCSResourceAttributes &attrs);
+ void setAttributes(RCSResourceAttributes &attrs, bool notify);
+
/**
* Return the value of an attribute
*
}
};
+class LightSensor
+{
+public:
+ int m_intensity;
+
+ std::string m_name;
+
+ LightSensor() : m_intensity(0), m_name("")
+ {
+ }
+};
+
Light mylight;
int observe_count()
}
}
+
+void onGetForLightIntensitySensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
+ const int eCode)
+{
+ (void)headerOptions;
+ try
+ {
+ if (eCode == OC_STACK_OK)
+ {
+ std::cout << "GET request was successful" << std::endl;
+ //std::cout << "Resource URI: " << DISensorResource->uri() << std::endl;
+
+ std::cout << "Payload: " << rep.getPayload() << std::endl;
+
+ std::cout << "\lightIntensity: " << rep.getValue<int>("lightintensity") << std::endl;
+ }
+ else
+ {
+ std::cout << "onGET Response error: " << eCode << std::endl;
+ }
+ }
+ catch (std::exception &e)
+ {
+ std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
+ }
+}
+
// Local function to get representation of light resource
void getLightRepresentation(std::shared_ptr<OCResource> resource)
{
}
}
+// Local function to get representation of light resource
+void getLightIntensityRepresentation(std::shared_ptr<OCResource> resource)
+{
+ if (resource)
+ {
+ std::cout << "Getting Light Representation..." << std::endl;
+ // Invoke resource's get API with the callback parameter
+
+ QueryParamsMap test;
+ std::cout << "Sending request to: " << resource->uri() << std::endl;
+ resource->get(test, &onGetForLightIntensitySensor);
+ }
+}
+
// Callback to found resources
void foundResource(std::shared_ptr<OCResource> resource)
{
for (auto &resourceTypes : resource->getResourceTypes())
{
std::cout << "\t\t" << resourceTypes << std::endl;
- if (resourceTypes == "oic.r.light")
+ /*if (resourceTypes == "oic.r.light")
{
curResource = resource;
// Call a local function which will internally invoke get API on the resource pointer
getLightRepresentation(resource);
+ }*/
+ if (resourceTypes == "oic.r.lightintensity")
+ {
+ curResource = resource;
+ // Call a local function which will internally invoke get API on the resource pointer
+ getLightIntensityRepresentation(resource);
}
}
* Created by markus.jung on 11/8/2015.\r
*/\r
public class AndroidLightResource extends AndroidBundleResource {\r
- private static final String LOG_TAG = AndroidBundleResource.class.getSimpleName();\r
+ private static final String LOG_TAG = AndroidLightResource.class.getSimpleName();\r
public AndroidLightResource(Context context){\r
super(context);\r
this.setResourceType("oic.r.light");\r
}\r
@Override\r
protected void initAttributes() {\r
- this.m_attributes.put("on-off", true);\r
+ this.m_attributes.put("on-off", false);\r
}\r
\r
@Override\r
public void handleSetAttributesRequest(RcsResourceAttributes attrs) {\r
+\r
+ for(String key: attrs.keySet()){\r
+ Log.i(LOG_TAG, " " + key + ": " + attrs.get(key));\r
+ }\r
+\r
Log.i(LOG_TAG, "Set Attributes called with " + attrs);\r
\r
Log.i(LOG_TAG, "On-off value: " + attrs.get("on-off"));\r
\r
@Override\r
public RcsResourceAttributes handleGetAttributesRequest() {\r
- return null;\r
+ Log.i(LOG_TAG, "Get Attributes called");\r
+ Log.i(LOG_TAG, "Returning: ");\r
+ for(String key: m_attributes.keySet()){\r
+ Log.i(LOG_TAG, " " + key + ": " + m_attributes.get(key));\r
+ }\r
+ return this.m_attributes;\r
}\r
}\r
AndroidLightResource lightRes = new AndroidLightResource(this.appContext);\r
lightRes.setURI("/android/light/1");\r
bundleAPI.registerResource("oic.android.sample", lightRes);\r
+\r
+ LightSensor lightSensor = new LightSensor(this.appContext);\r
+ lightSensor.setURI("/android/lightsensor/1");\r
+ bundleAPI.registerResource("oic.android.sample", lightSensor);\r
+\r
+ DiscomfortIndexSensorResource diResource = new DiscomfortIndexSensorResource(this.appContext);\r
+ diResource.setURI("/android/discomfortindex/1");\r
+ bundleAPI.registerResource("oic.android.sample", diResource);\r
}\r
\r
@Override\r
--- /dev/null
+package org.iotivity.service.sample.androidbundle;\r
+\r
+import android.content.Context;\r
+import android.hardware.Sensor;\r
+import android.hardware.SensorManager;\r
+import android.util.Log;\r
+\r
+import org.iotivity.service.resourcecontainer.AndroidBundleSoftSensorResource;\r
+import org.iotivity.service.resourcecontainer.RcsResourceAttributes;\r
+import org.iotivity.service.resourcecontainer.RcsValue;\r
+\r
+import java.util.Vector;\r
+\r
+/**\r
+ * Created by markus.jung on 11/27/2015.\r
+ */\r
+public class DiscomfortIndexSensorResource extends AndroidBundleSoftSensorResource {\r
+ private static final String LOG_TAG = DiscomfortIndexSensorResource.class.getSimpleName();\r
+\r
+ public DiscomfortIndexSensorResource(Context context){\r
+ super(context);\r
+ this.setResourceType("oic.r.discomfortindex");\r
+ this.setName("discomfortIndex");\r
+ }\r
+\r
+ @Override\r
+ protected void initAttributes() {\r
+ this.m_attributes.put("discomfortIndex", 0);\r
+ this.m_attributes.put("humidity", 0);\r
+ this.m_attributes.put("temperature",0);\r
+ }\r
+\r
+ @Override\r
+ protected void onUpdatedInputResource(String attributeName, Vector<RcsValue> values) {\r
+ m_mapInputData.put(attributeName, values.get(0));\r
+ }\r
+\r
+ @Override\r
+ protected void executeLogic() {\r
+ if(m_mapInputData.get("humidity") != null && m_mapInputData.get("temperature") != null){\r
+ double dDI = 0.0;\r
+\r
+ int t = m_mapInputData.get("temperature").asInt();\r
+ int h = m_mapInputData.get("humidity").asInt();\r
+ double F = (9.0 * (double) t) / 5.0 + 32.0;\r
+\r
+ // calculation of discomfortIndex\r
+ dDI = F - (F - 58.0) * (double)((100 - h) * 55) / 10000.0;\r
+\r
+ this.setAttribute("temperature", new RcsValue(t));\r
+ this.setAttribute("humidity", new RcsValue(h));\r
+ this.setAttribute("discomfortIndex", new RcsValue(dDI));\r
+\r
+ Log.i(LOG_TAG, "Discomfort Index" + dDI);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public void handleSetAttributesRequest(RcsResourceAttributes rcsResourceAttributes) {\r
+ this.setAttributes(rcsResourceAttributes);\r
+ executeLogic();\r
+ }\r
+\r
+ @Override\r
+ public RcsResourceAttributes handleGetAttributesRequest() {\r
+ return this.getAttributes();\r
+ }\r
+}\r
--- /dev/null
+package org.iotivity.service.sample.androidbundle;\r
+\r
+import android.content.Context;\r
+import android.hardware.Sensor;\r
+import android.hardware.SensorEvent;\r
+import android.hardware.SensorEventListener;\r
+import android.hardware.SensorManager;\r
+import android.util.Log;\r
+\r
+import org.iotivity.service.resourcecontainer.AndroidBundleResource;\r
+import org.iotivity.service.resourcecontainer.RcsResourceAttributes;\r
+import org.iotivity.service.resourcecontainer.RcsValue;\r
+\r
+/**\r
+ * Created by markus.jung on 11/26/2015.\r
+ */\r
+public class LightSensor extends AndroidBundleResource implements SensorEventListener {\r
+ private static final String LOG_TAG = AndroidLightResource.class.getSimpleName();\r
+ private final SensorManager mSensorManager;\r
+ private final Sensor lightSensor;\r
+\r
+ public LightSensor(Context context){\r
+ super(context);\r
+ this.setResourceType("oic.r.lightsensor");\r
+ this.setName("lightSensor");\r
+ mSensorManager = (SensorManager) context.getApplicationContext().getSystemService(Context.SENSOR_SERVICE);\r
+ lightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);\r
+ mSensorManager.registerListener(this, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);\r
+ }\r
+\r
+ @Override\r
+ protected void initAttributes() {\r
+ this.m_attributes.put("intensity", 0);\r
+ }\r
+\r
+ @Override\r
+ public void handleSetAttributesRequest(RcsResourceAttributes attrs) {\r
+ Log.i(LOG_TAG, "Set Attributes called with ");\r
+ for(String key: attrs.keySet()){\r
+ Log.i(LOG_TAG, " " + key + ": " + attrs.get(key));\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public RcsResourceAttributes handleGetAttributesRequest() {\r
+ Log.i(LOG_TAG, "Get Attributes called");\r
+ Log.i(LOG_TAG, "Returning: ");\r
+ for(String key: m_attributes.keySet()){\r
+ Log.i(LOG_TAG, " " + key + ": " + m_attributes.get(key));\r
+ }\r
+ return this.m_attributes;\r
+ }\r
+\r
+ @Override\r
+ public void onSensorChanged(SensorEvent sensorEvent) {\r
+ Log.i(LOG_TAG, "Sensor event " + sensorEvent.values[0]);\r
+\r
+ setAttribute("intensity", new RcsValue( (int) (sensorEvent.values[0]) ) , true);\r
+ }\r
+\r
+ @Override\r
+ public void onAccuracyChanged(Sensor sensor, int i) {\r
+\r
+ }\r
+}\r
<path>org.iotivity.service.sample.androidbundle.apk</path>
<activator>org.iotivity.service.sample.androidbundle.AndroidSampleActivator</activator>
<version>1.0.0</version>
+ <resources>
+ <resourceInfo>
+ <name>androidLIghtResource</name>
+ <resourceType>oic.r.light</resourceType>
+ <resourceUri>/android/light/1</resourceUri>
+ </resourceInfo>
+ <resourceInfo>
+ <name>androidLightSensor</name>
+ <resourceType>oic.r.lightsensor</resourceType>
+ <resourceUri>/android/lightsensor/1</resourceUri>
+ </resourceInfo>
+ <resourceInfo>
+ <name>DiscomfortIndexSensor1</name>
+ <resourceType>oic.r.discomfortindex</resourceType>
+ <resourceUri>/android/discomfortindex/1</resourceUri>
+ <outputs>
+ <output>
+ <name>discomfortIndex</name>
+ <type>int</type>
+ </output>
+ <output>
+ <name>humidity</name>
+ <type>double</type>
+ </output>
+ <output>
+ <name>temperature</name>
+ <type>double</type>
+ </output>
+ </outputs>
+ <inputs>
+ <input>
+ <name>humidity</name>
+ <type>double</type>
+ <resourceType>oic.r.humidity</resourceType>
+ </input>
+ <input>
+ <name>temperature</name>
+ <type>double</type>
+ <resourceType>oic.r.temperature</resourceType>
+ </input>
+ </inputs>
+ </resourceInfo>
+ </resources>
</bundle>
</container>
\ No newline at end of file
\r
@Override\r
protected void initAttributes() {\r
- this.m_attributes.put("on-off", true);\r
+ this.m_attributes.put("on-off", false);\r
}\r
\r
@Override\r
return m_resourceAttributes;
}
- void BundleResource::setAttributes(RCSResourceAttributes &attrs)
+ void BundleResource::setAttributes(RCSResourceAttributes &attrs, bool notify)
{
for (RCSResourceAttributes::iterator it = attrs.begin(); it != attrs.end(); ++it)
{
m_resourceAttributes[it->key()] = it->value();
}
+
+ if (notify && m_pNotiReceiver)
+ m_pNotiReceiver->onNotificationReceived(m_uri);
+ }
+
+ void BundleResource::setAttributes(RCSResourceAttributes &attrs)
+ {
+ setAttributes(attrs, false);
}
void BundleResource::setAttribute(const std::string &key,