--- /dev/null
- response.setErrorCode(Resource.SUCCESS);
+/*
+ *******************************************************************
+ *
+ * Copyright 2015 Intel Corporation.
+ *
+ *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ *
+ * 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.base.examples;
+
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+
+import org.iotivity.base.EntityHandlerResult;
+import org.iotivity.base.OcException;
+import org.iotivity.base.OcPlatform;
+import org.iotivity.base.OcRepresentation;
+import org.iotivity.base.OcResourceHandle;
+import org.iotivity.base.OcResourceRequest;
+import org.iotivity.base.OcResourceResponse;
+import org.iotivity.base.RequestHandlerFlag;
+import org.iotivity.base.ResourceProperty;
+
+import java.util.EnumSet;
+
+/**
+ * DoorResource
+ * <p/>
+ * DoorResource is a sample OIC server resource created by the refrigerator.
+ */
+public class DoorResource extends Resource implements OcPlatform.EntityHandler {
+ DoorResource(String side, Context context) {
+ mContext = context;
+ mSide = side;
+
+ registerDoorResource();
+ }
+
+ private void registerDoorResource() {
+ String resourceURI = DOOR_URI + mSide;
+ logMessage(TAG + "RegisterDoorResource " + resourceURI + " : " + RESOURCE_TYPEDOOR);
+ try {
+ mResourceHandle = OcPlatform.registerResource(
+ resourceURI,
+ RESOURCE_TYPEDOOR,
+ OcPlatform.DEFAULT_INTERFACE,
+ this,
+ EnumSet.of(ResourceProperty.DISCOVERABLE));
+ } catch (OcException e) {
+ logMessage(TAG + "Failed to register DoorResource");
+ Log.e(TAG, e.getMessage());
+ }
+ logMessage("-----------------------------------------------------");
+ }
+
+ /**
+ * sample implementation of eventHandler for doorResource - this can be implemented in many
+ * different ways
+ *
+ * @param ocResourceRequest OcResourceRequest from the client
+ * @return EntityHandlerResult indicates whether the request was handled successfully or not
+ */
+ @Override
+ public synchronized EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
+ EntityHandlerResult result = EntityHandlerResult.ERROR;
+ if (null != ocResourceRequest) {
+ try {
+ if (ocResourceRequest.getRequestHandlerFlagSet().contains(
+ RequestHandlerFlag.REQUEST)) {
+ OcResourceResponse response = new OcResourceResponse();
+ response.setRequestHandle(ocResourceRequest.getRequestHandle());
+ response.setResourceHandle(ocResourceRequest.getResourceHandle());
+
+ switch (ocResourceRequest.getRequestType()) {
+ case GET:
- response.setErrorCode(Resource.SUCCESS);
+ updateRepresentationValues();
+ response.setResourceRepresentation(mRepresentation);
+ response.setResponseResult(EntityHandlerResult.OK);
+ OcPlatform.sendResponse(response);
+ break;
+ case PUT:
- response.setErrorCode(204);
+ put(ocResourceRequest.getResourceRepresentation());
+ updateRepresentationValues();
+ response.setResourceRepresentation(mRepresentation);
+ response.setResponseResult(EntityHandlerResult.OK);
+ OcPlatform.sendResponse(response);
+ break;
+ case DELETE:
+ response.setResponseResult(EntityHandlerResult.RESOURCE_DELETED);
+ OcPlatform.sendResponse(response);
+ break;
+ }
+ result = EntityHandlerResult.OK;
+ }
+ } catch (OcException e) {
+ logMessage("Error in handleEntity of DoorResource");
+ Log.e(TAG, e.getMessage());
+ return EntityHandlerResult.ERROR;
+ }
+ }
+ logMessage("-----------------------------------------------------");
+ return result;
+ }
+
+ public OcResourceHandle getHandle() {
+ return mResourceHandle;
+ }
+
+ /**
+ * helper function to update the current value of the door resource
+ */
+ private void updateRepresentationValues() {
+ try {
+ mRepresentation.setValue(DOOR_STATE_KEY, mDoorState);
+ mRepresentation.setValue(DOOR_SIDE_KEY, mSide);
+ logMessage(TAG + "door state is " + ((mDoorState == true) ? "open" : "close") +
+ " and door side is " + mSide);
+ } catch (OcException e) {
+ Log.e(TAG, e.getMessage());
+ }
+ }
+
+ /**
+ * update the value of doorResource, depending on if door is open/ closed
+ *
+ * @param representation new state of a door
+ */
+ private void put(OcRepresentation representation) {
+ try {
+ mDoorState = representation.getValue(DOOR_STATE_KEY);
+ } catch (OcException e) {
+ Log.e(TAG, e.getMessage());
+ }
+ // Note, we won't let the user change the door side!
+ }
+
+ //******************************************************************************
+ // End of the OIC specific code
+ //******************************************************************************
+ private Context mContext;
+ private String mSide;
+ private boolean mDoorState;
+ public static final String DOOR_URI = "/door/";
+ public static final String RESOURCE_TYPEDOOR = "intel.fridge.door";
+ private static String TAG = "DoorResource: ";
+ public static final String DOOR_STATE_KEY = "state";
+ public static final String DOOR_SIDE_KEY = "side";
+
+ private void logMessage(String msg) {
+ Intent intent = new Intent(Resource.INTENT);
+ intent.putExtra(Resource.MESSAGE, msg);
+ mContext.sendBroadcast(intent);
+ }
+}
--- /dev/null
- response.setErrorCode(Resource.SUCCESS);
+/*
+ *******************************************************************
+ *
+ * Copyright 2015 Intel Corporation.
+ *
+ *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ *
+ * 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.base.examples;
+
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+
+import org.iotivity.base.EntityHandlerResult;
+import org.iotivity.base.OcException;
+import org.iotivity.base.OcPlatform;
+import org.iotivity.base.OcRepresentation;
+import org.iotivity.base.OcResourceHandle;
+import org.iotivity.base.OcResourceRequest;
+import org.iotivity.base.OcResourceResponse;
+import org.iotivity.base.RequestHandlerFlag;
+import org.iotivity.base.ResourceProperty;
+
+import java.util.EnumSet;
+
+
+/**
+ * LightResource
+ * <p/>
+ * LightResource is a sample OIC server resource created by the refrigerator.
+ */
+public class LightResource extends Resource implements OcPlatform.EntityHandler {
+ LightResource(Context context) {
+ mContext = context;
+
+ registerLightResource();
+ }
+
+ private void registerLightResource() {
+ try {
+ logMessage(TAG + "RegisterLightResource " + LIGHT_URI + " : " + RESOURCE_TYPELIGHT);
+ mResourceHandle = OcPlatform.registerResource(
+ LIGHT_URI,
+ RESOURCE_TYPELIGHT,
+ OcPlatform.DEFAULT_INTERFACE,
+ this,
+ EnumSet.of(ResourceProperty.DISCOVERABLE));
+ } catch (OcException e) {
+ logMessage(TAG + "Failed to register LightResource");
+ Log.e(TAG, e.getMessage());
+ }
+ logMessage("-----------------------------------------------------");
+ }
+
+ /**
+ * sample implementation of eventHandler for lightResource - this can be implemented in many
+ * different ways
+ *
+ * @param ocResourceRequest OcResourceRequest from the client
+ * @return EntityHandlerResult indicates whether the request was handled successfully or not
+ */
+ @Override
+ public synchronized EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
+ EntityHandlerResult result = EntityHandlerResult.ERROR;
+ if (null != ocResourceRequest) {
+ try {
+ if (ocResourceRequest.getRequestHandlerFlagSet().contains(
+ RequestHandlerFlag.REQUEST)) {
+ OcResourceResponse response = new OcResourceResponse();
+ response.setRequestHandle(ocResourceRequest.getRequestHandle());
+ response.setResourceHandle(ocResourceRequest.getResourceHandle());
+ switch (ocResourceRequest.getRequestType()) {
+ case GET:
- response.setErrorCode(Resource.SUCCESS);
+ updateRepresentationValues();
+ response.setResourceRepresentation(mRepresentation);
+ response.setResponseResult(EntityHandlerResult.OK);
+ OcPlatform.sendResponse(response);
+ result = EntityHandlerResult.OK;
+ break;
+ case PUT:
+ put(ocResourceRequest.getResourceRepresentation());
+ updateRepresentationValues();
+ response.setResourceRepresentation(mRepresentation);
+ response.setResponseResult(EntityHandlerResult.OK);
+ OcPlatform.sendResponse(response);
+ result = EntityHandlerResult.OK;
+ break;
+ }
+ }
+ } catch (OcException e) {
+ logMessage("Error in handleEntity");
+ Log.e(TAG, e.getMessage());
+ return EntityHandlerResult.ERROR;
+ }
+ }
+ logMessage("-----------------------------------------------------");
+ return result;
+ }
+
+ public OcResourceHandle getHandle() {
+ return mResourceHandle;
+ }
+
+ /**
+ * update the value of light (ON/ OFF) from the representation
+ *
+ * @param representation new state of light
+ */
+ private void put(OcRepresentation representation) {
+ try {
+ mIsLightOn = representation.getValue(LIGHT_STATUS_KEY);
+ } catch (OcException e) {
+ Log.e(TAG, e.getMessage());
+ }
+ }
+
+ /**
+ * helper function to update the current state of the light
+ */
+ private void updateRepresentationValues() {
+ try {
+ mRepresentation.setValue(LIGHT_STATUS_KEY, mIsLightOn);
+ } catch (OcException e) {
+ Log.e(TAG, e.getMessage());
+ }
+ }
+
+ //******************************************************************************
+ // End of the OIC specific code
+ //******************************************************************************
+ private Context mContext;
+ private boolean mIsLightOn = false;
+
+ private static String TAG = "LightResource: ";
+ public static final String LIGHT_URI = "/light";
+ public static final String RESOURCE_TYPELIGHT = "intel.fridge.light";
+ public static final String LIGHT_STATUS_KEY = "light";
+
+ private void logMessage(String msg) {
+ Intent intent = new Intent(Resource.INTENT);
+ intent.putExtra(Resource.MESSAGE, msg);
+ mContext.sendBroadcast(intent);
+ }
+}
--- /dev/null
- response.setErrorCode(SUCCESS);
+/*
+ *******************************************************************
+ *
+ * Copyright 2015 Intel Corporation.
+ *
+ *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ *
+ * 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.base.examples.fridgeserver;
+
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+
+import org.iotivity.base.EntityHandlerResult;
+import org.iotivity.base.OcException;
+import org.iotivity.base.OcHeaderOption;
+import org.iotivity.base.OcPlatform;
+import org.iotivity.base.OcResourceRequest;
+import org.iotivity.base.OcResourceResponse;
+import org.iotivity.base.RequestHandlerFlag;
+import org.iotivity.base.ResourceProperty;
+
+import java.util.EnumSet;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * DeviceResource
+ * <p/>
+ * Creates a device resource and performs action based on client requests
+ */
+public class DeviceResource extends Resource implements OcPlatform.EntityHandler {
+ public static final String DEVICE_URI = "/device";
+ public static final String RESOURCE_TYPENAME = "intel.fridge";
+ public static final String API_VERSION = "v.1.0";
+ public static final String CLIENT_TOKEN = "21ae43gf";
+ public static final String DEVICE_NAME = "device_name";
+ private static String TAG = "DeviceResource: ";
+ public static final int SUCCESS = 200;
+ public static final int API_VERSION_KEY = 2048;
+ public static final int CLIENT_VERSION_KEY = 3000;
+
+ private Context mContext;
+
+ /**
+ * constructor
+ *
+ * @param context to enable sending of broadcast messages to be displayed on the user screen
+ */
+ DeviceResource(Context context) {
+ mContext = context;
+ registerDeviceResource();
+ }
+
+ private void registerDeviceResource() {
+ try {
+ logMessage("RegisterDeviceResource " + DEVICE_URI + " : " + RESOURCE_TYPENAME);
+ mResourceHandle = OcPlatform.registerResource(
+ DEVICE_URI,
+ RESOURCE_TYPENAME,
+ OcPlatform.DEFAULT_INTERFACE,
+ this,
+ EnumSet.of(ResourceProperty.DISCOVERABLE));
+ } catch (OcException e) {
+ logMessage(TAG + "Failed to register DeviceResource");
+ Log.e(TAG, e.getMessage());
+ }
+ }
+
+ /**
+ * this is the main method which handles different incoming requests appropriately.
+ *
+ * @param ocResourceRequest OcResourceRequest from the client
+ * @return EntityHandlerResult indicates whether the request was handled successfully or not
+ */
+ @Override
+ public synchronized EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
+ EntityHandlerResult result = EntityHandlerResult.ERROR;
+ if (null != ocResourceRequest) {
+ List<OcHeaderOption> headerOptions = ocResourceRequest.getHeaderOptions();
+ String clientAPIVersion = "";
+ String clientToken = "";
+ // search for header options map and look for API version and client token
+ for (OcHeaderOption headerOption : headerOptions) {
+ int optionId = headerOption.getOptionId();
+ if (API_VERSION_KEY == optionId) {
+ clientAPIVersion = headerOption.getOptionData();
+ logMessage(TAG + " Client API Version: " + clientAPIVersion);
+ } else if (CLIENT_VERSION_KEY == optionId) {
+ clientToken = headerOption.getOptionData();
+ logMessage(TAG + " Client Token: " + clientToken);
+ }
+ }
+ if (clientAPIVersion.equals(API_VERSION) &&
+ clientToken.equals(CLIENT_TOKEN)) {
+ List<OcHeaderOption> serverHeaderOptions = new LinkedList<>();
+ OcHeaderOption apiVersion = new OcHeaderOption(API_VERSION_KEY,
+ API_VERSION);
+ serverHeaderOptions.add(apiVersion);
+ try {
+ if (ocResourceRequest.getRequestHandlerFlagSet().contains(RequestHandlerFlag.REQUEST)) {
+ OcResourceResponse response = new OcResourceResponse();
+ response.setRequestHandle(ocResourceRequest.getRequestHandle());
+ response.setResourceHandle(ocResourceRequest.getResourceHandle());
+ response.setHeaderOptions(serverHeaderOptions);
+
+ switch (ocResourceRequest.getRequestType()) {
+ case GET:
+ response.setResponseResult(EntityHandlerResult.OK);
+ updateRepresentationValues();
+ response.setResourceRepresentation(mRepresentation);
+ OcPlatform.sendResponse(response);
+ break;
+ }
+ result = EntityHandlerResult.OK;
+ }
+ } catch (OcException e) {
+ logMessage("Error in handleEntity of DeviceResource");
+ Log.e(TAG, e.getMessage());
+ }
+ }
+ }
+ logMessage("-----------------------------------------------------");
+ return result;
+ }
+
+ /**
+ * update state of device
+ *
+ * @return device representation
+ */
+ private void updateRepresentationValues() {
+ try {
+ mRepresentation.setValue(DEVICE_NAME,
+ "Intel Powered 3 door, 1 light refrigerator");
+ } catch (OcException e) {
+ Log.e(TAG, e.getMessage());
+ }
+ }
+
+ //******************************************************************************
+ // End of the OIC specific code
+ //******************************************************************************
+ public void logMessage(String msg) {
+ Intent intent = new Intent(FridgeServer.INTENT);
+ intent.putExtra("message", msg);
+ mContext.sendBroadcast(intent);
+ }
+}
--- /dev/null
- response.setErrorCode(SUCCESS);
+/*
+ *******************************************************************
+ *
+ * Copyright 2015 Intel Corporation.
+ *
+ *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ *
+ * 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.base.examples.fridgeserver;
+
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+
+import org.iotivity.base.EntityHandlerResult;
+import org.iotivity.base.OcException;
+import org.iotivity.base.OcPlatform;
+import org.iotivity.base.OcRepresentation;
+import org.iotivity.base.OcResourceRequest;
+import org.iotivity.base.OcResourceResponse;
+import org.iotivity.base.RequestHandlerFlag;
+import org.iotivity.base.ResourceProperty;
+
+import java.util.EnumSet;
+
+/**
+ * DoorResource
+ * <p/>
+ * Creates a door resource and performs actions based on the client requests
+ */
+public class DoorResource extends Resource implements OcPlatform.EntityHandler {
+ public static final String DOOR_URI = "/door/";
+ public static final String RESOURCE_TYPEDOOR = "intel.fridge.door";
+ public static final String DOOR_STATE_KEY = "state";
+ public static final String DOOR_SIDE_KEY = "side";
+ private boolean mDoorState;
+ private String mSide;
+
+ /**
+ * Constructor
+ *
+ * @param side side of the door
+ * @param context to enable sending of broadcast messages to be displayed on the user screen
+ */
+ DoorResource(String side, Context context) {
+ mContext = context;
+ mSide = side;
+ registerDoorResource();
+ }
+
+ private void registerDoorResource() {
+ String resourceURI = DOOR_URI + mSide;
+ logMessage(TAG + "RegisterDoorResource " + resourceURI + " : " + RESOURCE_TYPEDOOR);
+ try {
+ mResourceHandle = OcPlatform.registerResource(resourceURI,
+ RESOURCE_TYPEDOOR,
+ OcPlatform.DEFAULT_INTERFACE,
+ this,
+ EnumSet.of(ResourceProperty.DISCOVERABLE));
+ } catch (OcException e) {
+ logMessage(TAG + "Failed to register DoorResource");
+ Log.e(TAG, e.getMessage());
+ }
+ }
+
+ /**
+ * this is the main method which handles different incoming requests appropriately.
+ *
+ * @param ocResourceRequest OcResourceRequest from the client
+ * @return EntityHandlerResult indicates whether the request was handled successfully or not
+ */
+ @Override
+ public synchronized EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
+ EntityHandlerResult result = EntityHandlerResult.ERROR;
+ if (null != ocResourceRequest) {
+ try {
+ if (ocResourceRequest.getRequestHandlerFlagSet().contains(RequestHandlerFlag.REQUEST)) {
+ OcResourceResponse response = new OcResourceResponse();
+ response.setRequestHandle(ocResourceRequest.getRequestHandle());
+ response.setResourceHandle(ocResourceRequest.getResourceHandle());
+
+ switch (ocResourceRequest.getRequestType()) {
+ case GET:
- response.setErrorCode(SUCCESS);
+ updateRepresentationValues();
+ response.setResourceRepresentation(mRepresentation);
+ response.setResponseResult(EntityHandlerResult.OK);
+ OcPlatform.sendResponse(response);
+ break;
+ case PUT:
- response.setErrorCode(204);
+ put(ocResourceRequest.getResourceRepresentation());
+ updateRepresentationValues();
+ response.setResourceRepresentation(mRepresentation);
+ response.setResponseResult(EntityHandlerResult.OK);
+ OcPlatform.sendResponse(response);
+ break;
+ case DELETE:
+ response.setResponseResult(EntityHandlerResult.RESOURCE_DELETED);
+ OcPlatform.sendResponse(response);
+ break;
+ }
+ result = EntityHandlerResult.OK;
+ }
+ } catch (OcException e) {
+ logMessage("Error in handleEntity of DoorResource");
+ Log.e(TAG, e.getMessage());
+ return EntityHandlerResult.ERROR;
+ }
+ }
+ logMessage("-----------------------------------------------------");
+ return result;
+ }
+
+ /**
+ * helper function to update the current value of the door resource
+ */
+ private void updateRepresentationValues() {
+ try {
+ mRepresentation.setValue(DOOR_STATE_KEY, mDoorState);
+ mRepresentation.setValue(DOOR_SIDE_KEY, mSide);
+ logMessage(TAG + "door state is " + ((mDoorState == true) ? "open" : "close") +
+ " and door side is " + mSide);
+ } catch (OcException e) {
+ Log.e(TAG, e.getMessage());
+ }
+ }
+
+ /**
+ * update the value of doorResource, depending on if door is open/ closed
+ *
+ * @param representation new state of a door
+ */
+ private void put(OcRepresentation representation) {
+ try {
+ mDoorState = representation.getValue(DOOR_STATE_KEY);
+ } catch (OcException e) {
+ Log.e(TAG, e.getMessage());
+ }
+ // Note, we won't let the user change the door side!
+ }
+
+ //******************************************************************************
+ // End of the OIC specific code
+ //******************************************************************************
+ private Context mContext;
+ private static String TAG = "DoorResource: ";
+
+ public void logMessage(String msg) {
+ Intent intent = new Intent(FridgeServer.INTENT);
+ intent.putExtra(FridgeServer.MESSAGE, msg);
+ mContext.sendBroadcast(intent);
+ }
+}
--- /dev/null
- response.setErrorCode(SUCCESS);
+/*
+ *******************************************************************
+ *
+ * Copyright 2015 Intel Corporation.
+ *
+ *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ *
+ * 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.base.examples.fridgeserver;
+
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+
+import org.iotivity.base.EntityHandlerResult;
+import org.iotivity.base.OcException;
+import org.iotivity.base.OcPlatform;
+import org.iotivity.base.OcRepresentation;
+import org.iotivity.base.OcResourceRequest;
+import org.iotivity.base.OcResourceResponse;
+import org.iotivity.base.RequestHandlerFlag;
+import org.iotivity.base.ResourceProperty;
+
+import java.util.EnumSet;
+
+/**
+ * LightResource
+ * <p/>
+ * Creates a light resource and performs actions based on the client requests
+ */
+public class LightResource extends Resource implements OcPlatform.EntityHandler {
+ public static final String LIGHT_STATUS_KEY = "light";
+ public static final String LIGHT_URI = "/light";
+ public static final String RESOURCE_TYPELIGHT = "intel.fridge.light";
+ private boolean mIsLightOn = false;
+
+ /**
+ * constructor
+ *
+ * @param context to enable sending of broadcast messages to be displayed on the user screen
+ */
+ LightResource(Context context) {
+ mContext = context;
+ registerLightResource();
+ }
+
+ private void registerLightResource() {
+ try {
+ logMessage(TAG + "RegisterLightResource " + LIGHT_URI + " : " + RESOURCE_TYPELIGHT);
+ mResourceHandle = OcPlatform.registerResource(LIGHT_URI,
+ RESOURCE_TYPELIGHT,
+ OcPlatform.DEFAULT_INTERFACE,
+ this,
+ EnumSet.of(ResourceProperty.DISCOVERABLE));
+ } catch (OcException e) {
+ logMessage(TAG + "Failed to register LightResource");
+ Log.e(TAG, e.getMessage());
+ }
+ logMessage("-----------------------------------------------------");
+ }
+
+ /**
+ * this is the main method which handles different incoming requests appropriately.
+ *
+ * @param ocResourceRequest OcResourceRequest from the client
+ * @return EntityHandlerResult indicates whether the request was handled successfully or not
+ */
+ @Override
+ public synchronized EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
+ EntityHandlerResult result = EntityHandlerResult.ERROR;
+ if (null != ocResourceRequest) {
+ try {
+ if (ocResourceRequest.getRequestHandlerFlagSet().contains(RequestHandlerFlag.REQUEST)) {
+ OcResourceResponse response = new OcResourceResponse();
+ response.setRequestHandle(ocResourceRequest.getRequestHandle());
+ response.setResourceHandle(ocResourceRequest.getResourceHandle());
+
+ switch (ocResourceRequest.getRequestType()) {
+ case GET:
- response.setErrorCode(SUCCESS);
+ updateRepresentationValues();
+ response.setResourceRepresentation(mRepresentation);
+ response.setResponseResult(EntityHandlerResult.OK);
+ OcPlatform.sendResponse(response);
+ result = EntityHandlerResult.OK;
+ break;
+ case PUT:
+ put(ocResourceRequest.getResourceRepresentation());
+ updateRepresentationValues();
+ response.setResourceRepresentation(mRepresentation);
+ response.setResponseResult(EntityHandlerResult.OK);
+ OcPlatform.sendResponse(response);
+ result = EntityHandlerResult.OK;
+ break;
+ }
+ }
+ } catch (OcException e) {
+ logMessage("Error in handleEntity of LightResource");
+ Log.e(TAG, e.getMessage());
+ return EntityHandlerResult.ERROR;
+ }
+ }
+ logMessage("-----------------------------------------------------");
+ return result;
+ }
+
+ /**
+ * updates the current state of the light (on/ off)
+ *
+ * @return light is on or off
+ */
+ private void updateRepresentationValues() {
+ try {
+ mRepresentation.setValue(LIGHT_STATUS_KEY, mIsLightOn);
+ } catch (OcException e) {
+ Log.e(TAG, e.getMessage());
+ }
+ }
+
+ /**
+ * update the value of mIsOn from the representation
+ *
+ * @param representation get current state of light
+ */
+ private void put(OcRepresentation representation) {
+ try {
+ mIsLightOn = representation.getValue(LIGHT_STATUS_KEY);
+ } catch (OcException e) {
+ Log.e(TAG, e.getMessage());
+ }
+ }
+
+ //******************************************************************************
+ // End of the OIC specific code
+ //******************************************************************************
+ private Context mContext;
+ private static String TAG = "LightResource: ";
+
+ public void logMessage(String msg) {
+ Intent intent = new Intent(FridgeServer.INTENT);
+ intent.putExtra(FridgeServer.MESSAGE, msg);
+ mContext.sendBroadcast(intent);
+ }
+}
--- /dev/null
- response.setErrorCode(SUCCESS);
+/*
+ *******************************************************************
+ *
+ * Copyright 2015 Intel Corporation.
+ *
+ *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ *
+ * 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.base.examples;
+
+import org.iotivity.base.EntityHandlerResult;
+import org.iotivity.base.ErrorCode;
+import org.iotivity.base.ObservationInfo;
+import org.iotivity.base.OcException;
+import org.iotivity.base.OcPlatform;
+import org.iotivity.base.OcRepresentation;
+import org.iotivity.base.OcResource;
+import org.iotivity.base.OcResourceHandle;
+import org.iotivity.base.OcResourceRequest;
+import org.iotivity.base.OcResourceResponse;
+import org.iotivity.base.RequestHandlerFlag;
+import org.iotivity.base.RequestType;
+import org.iotivity.base.ResourceProperty;
+
+import java.util.EnumSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Light
+ * <p/>
+ * This class represents a light resource
+ */
+public class Light implements OcPlatform.EntityHandler {
+ private static final String NAME_KEY = "name";
+ private static final String STATE_KEY = "state";
+ private static final String POWER_KEY = "power";
+
+ private String mResourceUri; //resource URI
+ private String mResourceTypeName; //resource type name.
+ private String mResourceInterface; //resource interface.
+ private OcResourceHandle mResourceHandle; //resource handle
+
+ private String mName; //light name
+ private boolean mState; //light state
+ private int mPower; //light power
+
+ public Light(String resourceUri, String name, boolean state, int power) {
+ mResourceUri = resourceUri;
+ mResourceTypeName = "core.light";
+ mResourceInterface = OcPlatform.DEFAULT_INTERFACE;
+ mResourceHandle = null; //this is set when resource is registered
+
+ mName = name;
+ mState = state;
+ mPower = power;
+ }
+
+ public synchronized void registerResource() throws OcException {
+ if (null == mResourceHandle) {
+ mResourceHandle = OcPlatform.registerResource(
+ mResourceUri,
+ mResourceTypeName,
+ mResourceInterface,
+ this,
+ EnumSet.of(ResourceProperty.DISCOVERABLE, ResourceProperty.OBSERVABLE)
+ );
+ }
+ }
+
+ /**
+ * NOTE: This is just a sample implementation of entity handler. Entity handler can be
+ * implemented in several ways by the manufacturer.
+ *
+ * @param request
+ * @return
+ */
+ @Override
+ public synchronized EntityHandlerResult handleEntity(final OcResourceRequest request) {
+ EntityHandlerResult ehResult = EntityHandlerResult.ERROR;
+ if (null == request) {
+ msg("Server request is invalid");
+ return ehResult;
+ }
+ // Get the request flags
+ EnumSet<RequestHandlerFlag> requestFlags = request.getRequestHandlerFlagSet();
+ if (requestFlags.contains(RequestHandlerFlag.INIT)) {
+ msg("\t\tRequest Flag: Init");
+ ehResult = EntityHandlerResult.OK;
+ }
+ if (requestFlags.contains(RequestHandlerFlag.REQUEST)) {
+ msg("\t\tRequest Flag: Request");
+ ehResult = handleRequest(request);
+ }
+ if (requestFlags.contains(RequestHandlerFlag.OBSERVER)) {
+ msg("\t\tRequest Flag: Observer");
+ ehResult = handleObserver(request);
+ }
+ return ehResult;
+ }
+
+ private EntityHandlerResult handleRequest(OcResourceRequest request) {
+ EntityHandlerResult ehResult = EntityHandlerResult.ERROR;
+ // Check for query params (if any)
+ Map<String, String> queries = request.getQueryParameters();
+ if (!queries.isEmpty()) {
+ msg("Query processing is up to entityHandler");
+ } else {
+ msg("No query parameters in this request");
+ }
+
+ for (Map.Entry<String, String> entry : queries.entrySet()) {
+ msg("Query key: " + entry.getKey() + " value: " + entry.getValue());
+ }
+
+ //Get the request type
+ RequestType requestType = request.getRequestType();
+ switch (requestType) {
+ case GET:
+ msg("\t\t\tRequest Type is GET");
+ ehResult = handleGetRequest(request);
+ break;
+ case PUT:
+ msg("\t\t\tRequest Type is PUT");
+ ehResult = handlePutRequest(request);
+ break;
+ case POST:
+ msg("\t\t\tRequest Type is POST");
+ ehResult = handlePostRequest(request);
+ break;
+ case DELETE:
+ msg("\t\t\tRequest Type is DELETE");
+ ehResult = handleDeleteRequest();
+ break;
+ }
+ return ehResult;
+ }
+
+ private EntityHandlerResult handleGetRequest(final OcResourceRequest request) {
+ EntityHandlerResult ehResult;
+ OcResourceResponse response = new OcResourceResponse();
+ response.setRequestHandle(request.getRequestHandle());
+ response.setResourceHandle(request.getResourceHandle());
+
+ if (mIsSlowResponse) { // Slow response case
+ new Thread(new Runnable() {
+ public void run() {
+ handleSlowResponse(request);
+ }
+ }).start();
+ ehResult = EntityHandlerResult.SLOW;
+ } else { // normal response case.
- response.setErrorCode(SUCCESS);
+ response.setResponseResult(EntityHandlerResult.OK);
+ response.setResourceRepresentation(getOcRepresentation());
+ ehResult = sendResponse(response);
+ }
+ return ehResult;
+ }
+
+ private EntityHandlerResult handlePutRequest(OcResourceRequest request) {
+ OcResourceResponse response = new OcResourceResponse();
+ response.setRequestHandle(request.getRequestHandle());
+ response.setResourceHandle(request.getResourceHandle());
+
+ setOcRepresentation(request.getResourceRepresentation());
+ response.setResourceRepresentation(getOcRepresentation());
+ response.setResponseResult(EntityHandlerResult.OK);
- response.setErrorCode(SUCCESS);
+ return sendResponse(response);
+ }
+
+ private static int sUriCounter = 1;
+ private EntityHandlerResult handlePostRequest(OcResourceRequest request) {
+ OcResourceResponse response = new OcResourceResponse();
+ response.setRequestHandle(request.getRequestHandle());
+ response.setResourceHandle(request.getResourceHandle());
+ String newUri = "/a/light" + (++sUriCounter);
+ SimpleServer.createNewLightResource(newUri, "John's light " + sUriCounter);
+ OcRepresentation rep_post = getOcRepresentation();
+ try {
+ rep_post.setValue(OcResource.CREATED_URI_KEY, newUri);
+ } catch (OcException e) {
+ msgError(TAG, e.toString());
+ }
+ response.setResourceRepresentation(rep_post);
- response.setErrorCode(SUCCESS);
+ response.setNewResourceUri(newUri);
+ response.setResponseResult(EntityHandlerResult.RESOURCE_CREATED);
+ return sendResponse(response);
+ }
+
+ private EntityHandlerResult handleDeleteRequest() {
+ try {
+ this.unregisterResource();
+ return EntityHandlerResult.RESOURCE_DELETED;
+ } catch (OcException e) {
+ msgError(TAG, e.toString());
+ msg("Failed to unregister a light resource");
+ return EntityHandlerResult.ERROR;
+ }
+ }
+
+ private void handleSlowResponse(OcResourceRequest request) {
+ sleep(10);
+ msg("Sending slow response...");
+ OcResourceResponse response = new OcResourceResponse();
+ response.setRequestHandle(request.getRequestHandle());
+ response.setResourceHandle(request.getResourceHandle());
+
- response.setErrorCode(SUCCESS);
+ response.setResponseResult(EntityHandlerResult.OK);
+ response.setResourceRepresentation(getOcRepresentation());
+ sendResponse(response);
+ }
+
+ private List<Byte> mObservationIds; //IDs of observes
+
+ private EntityHandlerResult handleObserver(final OcResourceRequest request) {
+ ObservationInfo observationInfo = request.getObservationInfo();
+ switch (observationInfo.getObserveAction()) {
+ case REGISTER:
+ if (null == mObservationIds) {
+ mObservationIds = new LinkedList<>();
+ }
+ mObservationIds.add(observationInfo.getOcObservationId());
+ break;
+ case UNREGISTER:
+ mObservationIds.remove((Byte)observationInfo.getOcObservationId());
+ break;
+ }
+ // Observation happens on a different thread in notifyObservers method.
+ // If we have not created the thread already, we will create one here.
+ if (null == mObserverNotifier) {
+ mObserverNotifier = new Thread(new Runnable() {
+ public void run() {
+ notifyObservers(request);
+ }
+ });
+ mObserverNotifier.start();
+ }
+ return EntityHandlerResult.OK;
+ }
+
+ private void notifyObservers(OcResourceRequest request) {
+ while (true) {
+ // increment current power value by 10 every 2 seconds
+ mPower += 10;
+ sleep(2);
+
+ msg("Notifying observers...");
+ msg(this.toString());
+ try {
+ if (mIsListOfObservers) {
+ OcResourceResponse response = new OcResourceResponse();
+ response.setResourceRepresentation(getOcRepresentation());
+ OcPlatform.notifyListOfObservers(
+ mResourceHandle,
+ mObservationIds,
+ response);
+ } else {
+ OcPlatform.notifyAllObservers(mResourceHandle);
+ }
+ } catch (OcException e) {
+ ErrorCode errorCode = e.getErrorCode();
+ if (ErrorCode.NO_OBSERVERS == errorCode) {
+ msg("No more observers, stopping notifications");
+ }
+ return;
+ }
+ }
+ }
+
+ private EntityHandlerResult sendResponse(OcResourceResponse response) {
+ try {
+ OcPlatform.sendResponse(response);
+ return EntityHandlerResult.OK;
+ } catch (OcException e) {
+ msgError(TAG, e.toString());
+ msg("Failed to send response");
+ return EntityHandlerResult.ERROR;
+ }
+ }
+
+ public synchronized void unregisterResource() throws OcException {
+ if (null != mResourceHandle) {
+ OcPlatform.unregisterResource(mResourceHandle);
+ }
+ }
+
+ public void setOcRepresentation(OcRepresentation rep) {
+ try {
+ if (rep.hasAttribute(NAME_KEY)) mName = rep.getValue(NAME_KEY);
+ if (rep.hasAttribute(STATE_KEY)) mState = rep.getValue(STATE_KEY);
+ if (rep.hasAttribute(POWER_KEY)) mPower = rep.getValue(POWER_KEY);
+ } catch (OcException e) {
+ msgError(TAG, e.toString());
+ msg("Failed to get representation values");
+ }
+ }
+
+ public OcRepresentation getOcRepresentation() {
+ OcRepresentation rep = new OcRepresentation();
+ try {
+ rep.setValue(NAME_KEY, mName);
+ rep.setValue(STATE_KEY, mState);
+ rep.setValue(POWER_KEY, mPower);
+ } catch (OcException e) {
+ msgError(TAG, e.toString());
+ msg("Failed to set representation values");
+ }
+ return rep;
+ }
+
+ //******************************************************************************
+ // End of the OIC specific code
+ //******************************************************************************
+
+ public void setSlowResponse(boolean isSlowResponse) {
+ mIsSlowResponse = isSlowResponse;
+ }
+
+ public void useListOfObservers(boolean isListOfObservers) {
+ mIsListOfObservers = isListOfObservers;
+ }
+
+ @Override
+ public String toString() {
+ return "\t" + "URI" + ": " + mResourceUri +
+ "\n\t" + NAME_KEY + ": " + mName +
+ "\n\t" + STATE_KEY + ": " + mState +
+ "\n\t" + POWER_KEY + ": " + mPower;
+ }
+
+ private void sleep(int seconds) {
+ try {
+ Thread.sleep(seconds * 1000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ msgError(TAG, e.toString());
+ }
+ }
+
+ private void msg(String text) {
+ SimpleServer.msg(text);
+ }
+
+ private void msgError(String tag, String text) {
+ SimpleServer.msgError(tag, text);
+ }
+
+ private final static String TAG = Light.class.getSimpleName();
+ private final static int SUCCESS = 200;
+ private boolean mIsSlowResponse = false;
+ private boolean mIsListOfObservers = false;
+ private Thread mObserverNotifier;
+}
#!/bin/bash
-BASE_PATH="../../android/android_api/base/src/main/java/"
+BASE_PATH="../../java/common/src/main/java/"
BASE_PKG="org.iotivity.base"
+ CA_SRCS="../../android/android_api/base/src/main/java/org/iotivity/ca/CaBtPairingInterface.java \
+ ../../android/android_api/base/src/main/java/org/iotivity/ca/CaInterface.java"
+
RE_PATH="../../service/resource-encapsulation/android/service/src/main/java/"
RE_COMMON_PKG=org.iotivity.service
RE_CLINET_PKG=org.iotivity.service.client