Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
I
iotivity-classic
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
3,289
Issues
3,289
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
IoTivity
iotivity-classic
Commits
4cdd52ff
Commit
4cdd52ff
authored
Jul 15, 2015
by
Sachin Agrawal
Committed by
Gerrit Code Review
Jul 15, 2015
Browse files
Options
Browse Files
Download
Plain Diff
Merge "Merge 'security-basecamp' branch into master with CBOR"
parents
dc3ad3c6
5afbbb1a
Changes
33
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
33 changed files
with
3424 additions
and
2763 deletions
+3424
-2763
android/android_api/base/jni/Android.mk
android/android_api/base/jni/Android.mk
+2
-1
android/android_api/base/jni/JniOcPlatform.cpp
android/android_api/base/jni/JniOcPlatform.cpp
+1623
-1614
android/android_api/base/jni/JniOcPlatform.h
android/android_api/base/jni/JniOcPlatform.h
+262
-262
android/android_api/base/jni/JniOcSecurity.cpp
android/android_api/base/jni/JniOcSecurity.cpp
+60
-0
android/android_api/base/jni/JniOcSecurity.h
android/android_api/base/jni/JniOcSecurity.h
+43
-0
android/android_api/base/src/main/java/org/iotivity/base/OcPlatform.java
..._api/base/src/main/java/org/iotivity/base/OcPlatform.java
+759
-757
android/android_api/base/src/main/java/org/iotivity/base/PlatformConfig.java
.../base/src/main/java/org/iotivity/base/PlatformConfig.java
+32
-1
android/examples/simpleclient/src/main/AndroidManifest.xml
android/examples/simpleclient/src/main/AndroidManifest.xml
+2
-0
android/examples/simpleclient/src/main/assets/oic_svr_db_client.json
...mples/simpleclient/src/main/assets/oic_svr_db_client.json
+49
-0
android/examples/simpleclient/src/main/java/org/iotivity/base/examples/simpleclient/SimpleClient.java
...org/iotivity/base/examples/simpleclient/SimpleClient.java
+77
-6
android/examples/simpleclient/src/main/java/org/iotivity/base/examples/simpleclient/StringConstants.java
.../iotivity/base/examples/simpleclient/StringConstants.java
+1
-1
android/examples/simpleserver/src/main/AndroidManifest.xml
android/examples/simpleserver/src/main/AndroidManifest.xml
+2
-0
android/examples/simpleserver/src/main/assets/oic_svr_db_server.json
...mples/simpleserver/src/main/assets/oic_svr_db_server.json
+55
-0
android/examples/simpleserver/src/main/java/org/iotivity/base/examples/simpleserver/LightResource.java
...rg/iotivity/base/examples/simpleserver/LightResource.java
+1
-1
android/examples/simpleserver/src/main/java/org/iotivity/base/examples/simpleserver/SimpleServer.java
...org/iotivity/base/examples/simpleserver/SimpleServer.java
+77
-2
android/examples/simpleserver/src/main/java/org/iotivity/base/examples/simpleserver/StringConstants.java
.../iotivity/base/examples/simpleserver/StringConstants.java
+1
-0
resource/csdk/security/include/internal/secureresourcemanager.h
...ce/csdk/security/include/internal/secureresourcemanager.h
+15
-0
resource/csdk/security/include/internal/srmresourcestrings.h
resource/csdk/security/include/internal/srmresourcestrings.h
+1
-0
resource/csdk/security/provisioning/SConscript
resource/csdk/security/provisioning/SConscript
+3
-1
resource/csdk/security/provisioning/src/provisioningmanager.c
...urce/csdk/security/provisioning/src/provisioningmanager.c
+105
-99
resource/csdk/security/src/policyengine.c
resource/csdk/security/src/policyengine.c
+2
-1
resource/csdk/security/src/secureresourcemanager.c
resource/csdk/security/src/secureresourcemanager.c
+23
-1
resource/csdk/security/src/srmresourcestrings.c
resource/csdk/security/src/srmresourcestrings.c
+2
-1
resource/examples/SConscript
resource/examples/SConscript
+7
-1
resource/examples/oic_svr_db_client.json
resource/examples/oic_svr_db_client.json
+49
-0
resource/examples/oic_svr_db_server.json
resource/examples/oic_svr_db_server.json
+55
-0
resource/examples/simpleclient.cpp
resource/examples/simpleclient.cpp
+9
-3
resource/examples/simpleserver.cpp
resource/examples/simpleserver.cpp
+7
-1
resource/include/OCApi.h
resource/include/OCApi.h
+13
-7
resource/include/OCSerialization.h
resource/include/OCSerialization.h
+13
-2
resource/src/OCPlatform_impl.cpp
resource/src/OCPlatform_impl.cpp
+1
-0
resource/unittests/OCPlatformTest.cpp
resource/unittests/OCPlatformTest.cpp
+67
-1
resource/unittests/SConscript
resource/unittests/SConscript
+6
-0
No files found.
android/android_api/base/jni/Android.mk
View file @
4cdd52ff
...
...
@@ -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
...
...
android/android_api/base/jni/JniOcPlatform.cpp
View file @
4cdd52ff
This diff is collapsed.
Click to expand it.
android/android_api/base/jni/JniOcPlatform.h
View file @
4cdd52ff
This diff is collapsed.
Click to expand it.
android/android_api/base/jni/JniOcSecurity.cpp
0 → 100644
View file @
4cdd52ff
/******************************************************************
*
* 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
);
}
}
android/android_api/base/jni/JniOcSecurity.h
0 → 100644
View file @
4cdd52ff
/******************************************************************
*
* 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
android/android_api/base/src/main/java/org/iotivity/base/OcPlatform.java
View file @
4cdd52ff
This diff is collapsed.
Click to expand it.
android/android_api/base/src/main/java/org/iotivity/base/PlatformConfig.java
View file @
4cdd52ff
...
...
@@ -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
;
}
}
android/examples/simpleclient/src/main/AndroidManifest.xml
View file @
4cdd52ff
...
...
@@ -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"
...
...
android/examples/simpleclient/src/main/assets/oic_svr_db_client.json
0 → 100755
View file @
4cdd52ff
{
"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=="
]
}]
}
android/examples/simpleclient/src/main/java/org/iotivity/base/examples/simpleclient/SimpleClient.java
View file @
4cdd52ff
...
...
@@ -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
,
1
f
)
);
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
);
...
...
android/examples/simpleclient/src/main/java/org/iotivity/base/examples/simpleclient/StringConstants.java
View file @
4cdd52ff
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"
;
...
...
android/examples/simpleserver/src/main/AndroidManifest.xml
View file @
4cdd52ff
...
...
@@ -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"
...
...
android/examples/simpleserver/src/main/assets/oic_svr_db_server.json
0 → 100755
View file @
4cdd52ff
{
"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=="
]
}]
}
android/examples/simpleserver/src/main/java/org/iotivity/base/examples/simpleserver/LightResource.java
View file @
4cdd52ff
...
...
@@ -305,4 +305,4 @@ public class LightResource implements IMessageLogger {
intent
.
putExtra
(
StringConstants
.
MESSAGE
,
text
);
mContext
.
sendBroadcast
(
intent
);
}
}
\ No newline at end of file
}
android/examples/simpleserver/src/main/java/org/iotivity/base/examples/simpleserver/SimpleServer.java
View file @
4cdd52ff
...
...
@@ -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
)
{
...
...
android/examples/simpleserver/src/main/java/org/iotivity/base/examples/simpleserver/StringConstants.java
View file @
4cdd52ff
...
...
@@ -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"
;