Commit 4cdd52ff authored by Sachin Agrawal's avatar Sachin Agrawal Committed by Gerrit Code Review

Merge "Merge 'security-basecamp' branch into master with CBOR"

parents dc3ad3c6 5afbbb1a
......@@ -58,7 +58,8 @@ LOCAL_SRC_FILES := JniOcStack.cpp \
JniOcResourceResponse.cpp \
JniOcPlatform.cpp \
JniOcResource.cpp \
JniOcResourceIdentifier.cpp
JniOcResourceIdentifier.cpp \
JniOcSecurity.cpp
LOCAL_LDLIBS := -llog
LOCAL_STATIC_LIBRARIES := android-oc
......
/******************************************************************
*
* 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 "JniOcSecurity.h"
#include "JniOcStack.h"
/*
* TODO: Persistant Storage Handling should be done by App.
* For 0.9.2 , Handling is done at JNI. As of now Plaform Config only
* SVR Database fileName(fullpath) is passed.
*/
using namespace std;
namespace PH = std::placeholders;
namespace OC {
string& JniOcSecurity::store_path()
{
static string s_dbPath;
return s_dbPath;
}
void JniOcSecurity::StoreDbPath(const string &path)
{
store_path() = path;
}
OCPersistentStorage* JniOcSecurity::getOCPersistentStorage()
{
if (store_path().empty())
{
return nullptr;
}
static OCPersistentStorage s_ps { &JniOcSecurity::client_open, fread,
fwrite, fclose, unlink };
return &s_ps;
}
FILE* JniOcSecurity::client_open(const char *path, const char *mode)
{
LOGI("Opening SVR Database file '%s' with mode '%s'\n", store_path().c_str(), mode);
return fopen(store_path().c_str(), mode);
}
}
/******************************************************************
*
* 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 __JNIOCSECURITY_H
#define __JNIOCSECURITY_H
#include <stdint.h>
#include <stdio.h>
#include <string>
#include "octypes.h"
#include <unistd.h>
namespace OC
{
class JniOcSecurity
{
private:
static FILE* client_open(const char*, const char*);
public:
static std::string& store_path(void);
static void StoreDbPath(const std::string&);
static OCPersistentStorage* getOCPersistentStorage(void);
};
}
#endif //__JNIOCSECURITY_H
......@@ -35,6 +35,8 @@ public class PlatformConfig {
private String mIpAddress;
private int mPort;
private QualityOfService mQualityOfService;
private String mSvrDbPath; //TODO: Instead of SVRDB file, it should be Persistent Storage.
//this is only for 0.9.2
/**
* @param context app context
......@@ -47,19 +49,44 @@ public class PlatformConfig {
* if you specify 5683 : client discovery can work even if they don't
* specify port
* @param qualityOfService quality of service
* @param dbPath Persistant storage file for SVR Database.
*/
public PlatformConfig(Context context,
ServiceType serviceType,
ModeType modeType,
String ipAddress,
int port,
QualityOfService qualityOfService) {
QualityOfService qualityOfService,
String dbPath) {
this.mContext = context;
this.mServiceType = serviceType;
this.mModeType = modeType;
this.mIpAddress = ipAddress;
this.mPort = port;
this.mQualityOfService = qualityOfService;
this.mSvrDbPath = dbPath;
}
/**
* @param context app context
* @param serviceType indicate IN_PROC or OUT_OF_PROC
* @param modeType indicate whether we want to do server, client or both
* @param ipAddress ip address of server
* if you specify 0.0.0.0 : it listens on any interface
* @param port port of server
* if you specifiy 0 : next available random port is used
* if you specify 5683 : client discovery can work even if they don't
* specify port
* @param qualityOfService quality of service
*/
//Avoid breaking building java samples due to persistent storage SVR DB changes.
public PlatformConfig(Context context,
ServiceType serviceType,
ModeType modeType,
String ipAddress,
int port,
QualityOfService qualityOfService) {
this(context,serviceType,modeType,ipAddress,port,qualityOfService, "");
}
public Context getContext() {
......@@ -84,5 +111,9 @@ public class PlatformConfig {
public QualityOfService getQualityOfService() {
return mQualityOfService;
}
public String getSvrDbPath() {
return mSvrDbPath;
}
}
......@@ -4,6 +4,8 @@
xmlns:tools="http://schemas.android.com/tools">
<uses-sdk tools:overrideLibrary="org.iotivity.base"></uses-sdk>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
......
{
"acl": [
{
"sub": "Kg==",
"rsrc": [
"/oic/res",
"/oic/d",
"/oic/p",
"/oic/res/types/d",
"/oic/ad",
"/oic/sec/acl"
],
"perms": 2,
"ownrs" : ["MjIyMjIyMjIyMjIyMjIyMg=="]
},
{
"sub": "Kg==",
"rsrc": [
"/oic/sec/doxm",
"/oic/sec/pstat"
],
"perms": 2,
"ownrs" : ["MjIyMjIyMjIyMjIyMjIyMg=="]
}
],
"pstat": {
"isop": true,
"deviceid": "ZGV2aWNlaWQAAAAAABhanw==",
"ch": 0,
"cm": 0,
"tm": 0,
"om": 3,
"sm": [3]
},
"doxm": {
"oxm": [0],
"oxmsel": 0,
"owned": true,
"deviceid": "MjIyMjIyMjIyMjIyMjIyMg==",
"ownr": "MjIyMjIyMjIyMjIyMjIyMg=="
},
"cred": [{
"credid": 1,
"sub": "MTExMTExMTExMTExMTExMQ==",
"credtyp": 1,
"pvdata": "QUFBQUFBQUFBQUFBQUFBQQ==",
"ownrs" : ["MjIyMjIyMjIyMjIyMjIyMg=="]
}]
}
......@@ -26,8 +26,11 @@ import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Message;
import android.preference.PreferenceManager;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.widget.LinearLayout;
......@@ -46,6 +49,12 @@ import org.iotivity.base.PlatformConfig;
import org.iotivity.base.QualityOfService;
import org.iotivity.base.ServiceType;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
......@@ -61,13 +70,15 @@ import base.iotivity.org.examples.message.IMessageLogger;
*/
public class SimpleClient extends Activity implements OcPlatform.OnResourceFoundListener,
IMessageLogger {
private static final String TAG = "SimpleClient: ";
private static final String TAG = "SimpleClient: ";
private static final int BUFFER_SIZE = 1024;
private String filePath = "";
private Light myLight;
private OcResource curResource;
private OcResource curResource;
//for display
private TextView mEventsTextView;
private TextView mEventsTextView;
private static boolean printOnce = true;
/**
......@@ -78,10 +89,10 @@ public class SimpleClient extends Activity implements OcPlatform.OnResourceFound
PlatformConfig cfg = new PlatformConfig(
this,
ServiceType.IN_PROC,
ModeType.CLIENT,
ModeType.CLIENT_SERVER,
"0.0.0.0", // bind to all available interfaces
0,
QualityOfService.LOW);
QualityOfService.LOW, filePath + StringConstants.OIC_CLIENT_JSON_DB_FILE);
OcPlatform.Configure(cfg);
try {
/**
......@@ -111,7 +122,7 @@ public class SimpleClient extends Activity implements OcPlatform.OnResourceFound
hostAddress = ocResource.getHost();
logMessage(TAG + "Discovered Resource\nUri: " + resourceUri + " \n Host: " + hostAddress);
// get the resource types
if (resourceUri.equals(StringConstants.RESOURCE_URI0)) {
if (resourceUri.contains("light")) {
curResource = ocResource;
doGetLightRepresentation();
}
......@@ -416,10 +427,70 @@ public class SimpleClient extends Activity implements OcPlatform.OnResourceFound
LinearLayout.LayoutParams.MATCH_PARENT, 0, 1f)
);
myLight = new Light();
filePath = getFilesDir().getPath() + "/"; // data/data/<package>/files/
//copy json when application runs first time
SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(this);
boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUN", true);
if (isFirstRun) {
copyJsonFromAsset();
SharedPreferences.Editor editor = wmbPreference.edit();
editor.putBoolean("FIRSTRUN", false);
editor.commit();
}
initOICStack();
}
/**
* Copy svr db json file from assets folder to app data files dir
*/
private void copyJsonFromAsset() {
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(StringConstants.OIC_CLIENT_JSON_DB_FILE);
File file = new File(filePath);
//check files directory exists
if (!(file.exists() && file.isDirectory())) {
file.mkdirs();
}
out = new FileOutputStream(filePath + StringConstants.OIC_CLIENT_JSON_DB_FILE);
copyFile(in, out);
} catch (NullPointerException e) {
logMessage(TAG + "Null pointer exception " + e.getMessage());
Log.e(TAG, e.getMessage());
} catch (FileNotFoundException e) {
logMessage(TAG + "Json svr db file not found " + e.getMessage());
Log.e(TAG, e.getMessage());
} catch (IOException e) {
logMessage(TAG + StringConstants.OIC_CLIENT_JSON_DB_FILE+ " file copy failed");
Log.e(TAG, e.getMessage());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[BUFFER_SIZE];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}
@Override
public void logMessage(String text) {
logMsg(text);
......
package org.iotivity.base.examples.simpleclient;
/**
* StringConstant contains the simpleclient specific constant values. To add another supported
* Resource or Interface type to this app, begin by adding the new strings here, and then
......@@ -9,6 +8,7 @@ package org.iotivity.base.examples.simpleclient;
public interface StringConstants {
public static final String RESOURCE_URI0 = "/light0";
public static final String RESOURCE_URI1 = "/light1";
public static final String OIC_CLIENT_JSON_DB_FILE = "oic_svr_db_client.json";
public static final String CREATED_URI = "createduri";
public static final String STATE = "state";
public static final String NAME = "name";
......
......@@ -4,6 +4,8 @@
xmlns:tools="http://schemas.android.com/tools">
<uses-sdk tools:overrideLibrary="org.iotivity.base"></uses-sdk>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
......
{
"acl": [
{
"sub": "Kg==",
"rsrc": [
"/oic/res",
"/oic/d",
"/oic/p",
"/oic/res/types/d",
"/oic/ad",
"/oic/sec/acl"
],
"perms": 2,
"ownrs" : ["MTExMTExMTExMTExMTExMQ=="]
},
{
"sub": "Kg==",
"rsrc": [
"/oic/sec/doxm",
"/oic/sec/pstat"
],
"perms": 2,
"ownrs" : ["MTExMTExMTExMTExMTExMQ=="]
},
{
"sub": "Kg==",
"rsrc": ["/light0", "/light1", "/a/light"],
"perms": 6,
"ownrs" : ["MjIyMjIyMjIyMjIyMjIyMg=="]
}
],
"pstat": {
"isop": true,
"deviceid": "ZGV2aWNlaWQAAAAAABhanw==",
"ch": 0,
"cm": 0,
"tm": 0,
"om": 3,
"sm": [3]
},
"doxm": {
"oxm": [0],
"oxmsel": 0,
"owned": true,
"deviceid": "MTExMTExMTExMTExMTExMQ==",
"ownr": "MjIyMjIyMjIyMjIyMjIyMg=="
},
"cred": [{
"credid": 1,
"sub": "MjIyMjIyMjIyMjIyMjIyMg==",
"credtyp": 1,
"pvdata": "QUFBQUFBQUFBQUFBQUFBQQ==",
"ownrs" : ["MjIyMjIyMjIyMjIyMjIyMg=="]
}]
}
......@@ -305,4 +305,4 @@ public class LightResource implements IMessageLogger {
intent.putExtra(StringConstants.MESSAGE, text);
mContext.sendBroadcast(intent);
}
}
\ No newline at end of file
}
......@@ -36,6 +36,9 @@ import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.preference.PreferenceManager;
import org.iotivity.base.ModeType;
import org.iotivity.base.OcPlatform;
......@@ -44,6 +47,13 @@ import org.iotivity.base.PlatformConfig;
import org.iotivity.base.QualityOfService;
import org.iotivity.base.ServiceType;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import base.iotivity.org.examples.message.IMessageLogger;
/**
......@@ -56,6 +66,8 @@ import base.iotivity.org.examples.message.IMessageLogger;
public class SimpleServer extends Activity implements IMessageLogger {
private final static String TAG = "SimpleServer: ";
private static final int BUFFER_SIZE = 1024;
private String filePath = "";
private TextView mEventsTextView;
private MessageReceiver mMessageReceiver = new MessageReceiver();
......@@ -73,10 +85,72 @@ public class SimpleServer extends Activity implements IMessageLogger {
OcRepresentation rep = new OcRepresentation();
rep.setValueBool("test", false);
boolean result = rep.getValueBool("test");
filePath = getFilesDir().getPath() + "/";// data/data/<package>/files/
//copy json when application runs first time
SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(this);
boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUN", true);
if (isFirstRun) {
copyJsonFromAsset();
SharedPreferences.Editor editor = wmbPreference.edit();
editor.putBoolean("FIRSTRUN", false);
editor.commit();
}
initOICStack();
}
/**
* Copy svr db json file from assets folder to app data files dir
*/
private void copyJsonFromAsset() {
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(StringConstants.OIC_SERVER_JSON_DB_FILE);
File file = new File(filePath);
//check files directory exists
if (!(file.exists() && file.isDirectory())) {
file.mkdirs();
}
out = new FileOutputStream(filePath + StringConstants.OIC_SERVER_JSON_DB_FILE);
copyFile(in, out);
} catch (NullPointerException e) {
logMessage(TAG + "Null pointer exception " + e.getMessage());
Log.e(TAG, e.getMessage());
} catch (FileNotFoundException e) {
logMessage(TAG + "Json svr db file not found " + e.getMessage());
Log.e(TAG, e.getMessage());
} catch (IOException e) {
logMessage(TAG + StringConstants.OIC_SERVER_JSON_DB_FILE + " file copy failed");
Log.e(TAG, e.getMessage());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[BUFFER_SIZE];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}
/**
* configure OIC platform and call findResource
*/
......@@ -88,14 +162,15 @@ public class SimpleServer extends Activity implements IMessageLogger {
ModeType.SERVER,
"0.0.0.0", // bind to all available interfaces
0,
QualityOfService.LOW);
QualityOfService.LOW,
filePath + StringConstants.OIC_SERVER_JSON_DB_FILE);
OcPlatform.Configure(cfg);
// Create instance of lightResource
LightResource myLight = new LightResource(this);
// create and register a resource
myLight.createResource0();
}
public class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
......
......@@ -11,6 +11,7 @@ public interface StringConstants {
public static final String RESOURCE_URI0 = "/light0";
public static final String RESOURCE_URI1 = "/light1";
public static final String RESOURCE_TYPENAME = "core.light";