Skip to content
This repository has been archived by the owner on Jun 13, 2019. It is now read-only.

Commit

Permalink
JS API,Examples: Fix coaps
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Schulhof committed Nov 2, 2017
1 parent cda1ee2 commit 44f7944
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 180 deletions.
88 changes: 88 additions & 0 deletions js/README.md
@@ -0,0 +1,88 @@
# `iotivity-node` Examples

## COAPS

The files `high-level-client.coaps.js` and `high-level-server.coaps.js` are for
a client/server pair illustrating DTLS access to a resource. To run the example,
please follow the steps below:

0. In addition to `iotivity-node`, build `iotivity` with `SECURED=1` and
`RD_MODE=all`. Let the root of the `iotivity` repository be `$iotivityRoot`.

0. Calculate the sha256 sum of the server. The example below assumes the server
is located in `/home/user/iotivity-node/js/high-level-server.coaps.js`.
Replace the path in the example below with the actual absolute path to the
file `high-level-server.coaps.js`.

```sh
echo -n '/home/user/iotivity-node/js/high-level-server.coaps.js' | sha256sum
```

Copy the resulting value to the clipboard. Let the value be `$serverHash`.

0. Create the directory `${HOME}/.iotivity-node/$serverHash`.

```sh
mkdir -p ~/.iotivity-node/$serverHash
```

0. Copy the file
`$iotivityRoot/resource/csdk/stack/security/provisioning/sample/oic_svr_db_server_justworks.dat`
to `${HOME}/.iotivity-node/$serverHash/oic_svr_db.dat`

0. Start the server

```sh
node high-level-server.coaps.js
```

0. Switch to the directory where iotivity built the provisioning client, and run
it from there. Let `$platform` be the platform for which you build iotivity,
`$architecture` be the architecture for which you built iotivity, and
`$buildType` be the build type - i.e., `release` for a production build, and
`debug` for a development build.

```sh
cd $iotivityRoot/out/$platform/$architecture/$buildType/resource/csdk/security/provisioning/sample
./provisioningclient
```

0. Choose 11 from the menu by typing `11` and pressing `Enter` to discover
unowned devices. The server should be listed under "Discovered Unowned
Devices" with uuid `12345678-1234-1234-1234-123456789012`.

0. Choose 20 from the menu by typing `20` and pressing `Enter` to assume
ownership of the devices discovered in the previous step.

0. Choose 99 to exit the client.

0. Calculate the sha256 sum of the client. The example below assumes the client
is located in `/home/user/iotivity-node/js/high-level-client.coaps.js`.
Replace the path in the example below with the actual absolute path to the
file `high-level-client.coaps.js`.

```sh
echo -n '/home/user/iotivity-node/js/high-level-client.coaps.js' | sha256sum
```

Copy the resulting value to the clipboard. Let the value be `$clientHash`.

0. Create the directory `${HOME}/.iotivity-node/$clientHash`.

```sh
mkdir -p ~/.iotivity-node/$clientHash
```

0. Copy the file `oic_svr_db_client.dat` from the current directory (in which
the `provisioningclient` program was built) to the newly created directory
and rename it to `oic_svr_db.dat`.

```sh
cp ./oic_svr_db_client.dat ${HOME}/.iotivity-node/$clientHash/oic_svr_db.dat
```

0. Launch the client.

```sh
node high-level-client.coaps.js
```
49 changes: 0 additions & 49 deletions js/high-level-client-example.js

This file was deleted.

43 changes: 43 additions & 0 deletions js/high-level-client.coaps.js
@@ -0,0 +1,43 @@
// Copyright 2016 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.

console.log( "Acquiring OCF device" );

var client = require( "iotivity-node" ).client;

function errorHandler( error ) {
console.log( error.stack + ": " + JSON.stringify( error, null, 4 ) );
process.exit( 1 );
}

function observer( resource ) {
console.log( "Observation: " + JSON.stringify( resource, null, 4 ) );
}

client
.on( "resourcefound", function( resource ) {
console.log( "Found resource: " + JSON.stringify( resource, null, 4 ) );
resource.on( "error", errorHandler );

client.retrieve( resource, observer )
.then( function( resultingResource ) {
console.log( "Resulting resource: " +
JSON.stringify( resultingResource, null, 4 ) );
} )
.catch( errorHandler );
} )
.findResources( { resourcePath: "/a/light" } )
.catch( errorHandler );

console.log( "Started looking for resources" );
109 changes: 0 additions & 109 deletions js/high-level-server-example.js

This file was deleted.

87 changes: 87 additions & 0 deletions js/high-level-server.coaps.js
@@ -0,0 +1,87 @@
// Copyright 2016 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.

function ObservationWrapper( resource ) {
if ( !( this instanceof ObservationWrapper ) ) {
return new ObservationWrapper( resource );
}
this.resource = resource;
this.observerCount = 0;
}

ObservationWrapper.prototype.addObserver = function() {
if ( this.observerCount === 0 ) {
console.log( "Firing up interval" );
var resource = this.resource;
this.intervalId = setInterval( function() {
resource.properties.outputValue = Math.round( Math.random() * 42 ) + 1;
resource.notify();
}, 1000 );
}
this.observerCount++;
};

ObservationWrapper.prototype.removeObserver = function() {
this.observerCount--;
if ( this.observerCount <= 0 ) {
console.log( "Turning off interval" );
clearInterval( this.intervalId );
this.observerCount = 0;
this.intervalId = 0;
}
};

var observationWrapper;

function errorHandler( error ) {
console.log( error.stack + ": " + JSON.stringify( error, null, 4 ) );
process.exit( 1 );
}

console.log( "Acquiring OCF device" );

require( "iotivity-node" ).server
.register( {
resourcePath: "/a/light",
resourceTypes: [ "core.light" ],
interfaces: [ "oic.if.baseline" ],
discoverable: true,
observable: true,
secure: true
} )
.then( function( resource ) {
console.log( "Resource registered" );
observationWrapper = ObservationWrapper( resource );
resource
.onretrieve( function( request ) {
if ( "observe" in request ) {
var observationRequest = request.observe ? "add" : "remove";
console.log( "Observation request: " + observationRequest );
observationWrapper[ observationRequest + "Observer" ]();
}
request.target.properties.outputValue =
Math.round( Math.random() * 42 ) + 1;
console.log( "Retrieve request received. Responding with " +
JSON.stringify( request.target, null, 4 ) );
request
.respond()
.catch( errorHandler );
} )
.onupdate( function( request ) {
console.log( "Update request: " + JSON.stringify( request, null, 4 ) );
resource.properties = request.data;
request.respond();
} );
} )
.catch( errorHandler );

0 comments on commit 44f7944

Please sign in to comment.