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

Commit 0e1add2

Browse files
author
Gabriel Schulhof
committed
Bindings: Add resource directory bindings
1 parent ee783f3 commit 0e1add2

File tree

14 files changed

+536
-29
lines changed

14 files changed

+536
-29
lines changed

binding.gyp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
# When building internally, we use pre-defined CFLAGS/LIBS, trusting that the CSDK
2828
# will be built successfully
2929

30-
"defines": [ "ROUTING_EP", "__WITH_DTLS__" ],
30+
"defines": [ "ROUTING_EP", "__WITH_DTLS__", "RD_CLIENT", "RD_SERVER" ],
3131
"include_dirs+": [ '<@(internalOCTBStack_include_dirs)' ],
3232
"conditions": [
3333

@@ -43,7 +43,8 @@
4343
"libraries": [
4444
"-L<(module_root_dir)/iotivity-installed/lib",
4545
"-Wl,-rpath <(module_root_dir)/iotivity-installed/lib",
46-
"-loctbstack"
46+
"-loctbstack",
47+
"-lresource_directory"
4748
]
4849
} ],
4950

@@ -109,6 +110,11 @@
109110
"files": [
110111
"<(module_root_dir)/iotivity-installed/lib/octbstack.dll"
111112
]
113+
}, {
114+
"destination": "<(PRODUCT_DIR)",
115+
"files": [
116+
"<(module_root_dir)/iotivity-installed/lib/resource_directory.dll"
117+
]
112118
} ]
113119
} ]
114120
]

build-scripts/build-csdk.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ if ( buildIotivity ) {
193193
.concat( process.env.npm_config_debug === "true" ?
194194
[ "RELEASE=False", "LOGGING=False" ] : [] )
195195
.concat( [ "logger", "octbstack", "connectivity_abstraction", "coap", "c_common", "ocsrm",
196-
"routingmanager", "resource/csdk/security/tool"
196+
"routingmanager", "resource/csdk/security/tool", "resource_directory"
197197
] );
198198

199199
// Node 5 on Windows seems unable to run batch files
@@ -229,6 +229,12 @@ shelljs.cp(
229229
path.join( repoPaths.iotivity, "resource", "csdk", "stack", "include", "ocstackconfig.h" ),
230230
path.join( repoPaths.iotivity, "resource", "csdk", "stack", "include", "ocstack.h" ),
231231
path.join( repoPaths.iotivity, "resource", "csdk", "include", "octypes.h" ),
232+
path.join( repoPaths.iotivity, "resource", "csdk", "resource-directory", "include",
233+
"rd_client.h" ),
234+
path.join( repoPaths.iotivity, "resource", "csdk", "resource-directory", "include",
235+
"rd_server.h" ),
236+
path.join( repoPaths.iotivity, "resource", "csdk", "resource-directory", "include",
237+
"rd_database.h" ),
232238
path.join( repoPaths.iotivity, "resource", "csdk", "security", "include", "experimental",
233239
"securevirtualresourcetypes.h" ),
234240
path.join( repoPaths.iotivity, "resource", "c_common", "platform_features.h" ),

build-scripts/generate-constants.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ fs.writeFileSync( constantsCC, [
7474
"std::string InitConstants(napi_env env, napi_value exports) {",
7575
" // ocstackconfig.h: Stack configuration",
7676
parseFileForConstants( includePaths[ "ocstackconfig.h" ] ),
77-
" // octypes.h: Definitions",
77+
" // octypes.h: Type definitions",
7878
parseFileForConstants( includePaths[ "octypes.h" ] ),
79+
" // octypes.h: Type definitions",
80+
parseFileForConstants( includePaths[ "rd_client.h" ] ),
7981
" return std::string();",
8082
"}"
8183
].join( "\n" ) );

build-scripts/helpers/header-paths.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var path = require( "path" );
1717

1818
var index, fileIndex, completeFileName;
1919

20-
var filesToFind = [ "octypes.h", "ocpresence.h", "ocstackconfig.h" ];
20+
var filesToFind = [ "octypes.h", "ocpresence.h", "ocstackconfig.h", "rd_client.h" ];
2121

2222
var paths = {};
2323

build-scripts/postinstall.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ if ( !isDependency ) {
4747
shelljs.mv( addonAbsoluteName, repoPaths.root );
4848
if ( os.platform() === "win32" ) {
4949
shelljs.mv( path.join( addonAbsolutePath, "octbstack.dll" ), repoPaths.root );
50+
shelljs.mv( path.join( addonAbsolutePath, "resource_directory.dll" ), repoPaths.root );
5051
}
5152
shelljs.rm( "-rf", path.join( repoPaths.root, "build" ) );
5253
shelljs.mkdir( "-p", addonAbsolutePath );
5354
shelljs.mv( path.join( repoPaths.root, addonName ), addonAbsolutePath );
5455
if ( os.platform() === "win32" ) {
5556
shelljs.mv( path.join( repoPaths.root, "octbstack.dll" ), addonAbsolutePath );
57+
shelljs.mv( path.join( repoPaths.root, "resource_directory.dll" ), addonAbsolutePath );
5658
}
5759

5860
// Purge any and all files not needed after building

patches/0005-remove-rd-cpp.patch

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
diff --git a/resource/csdk/resource-directory/SConscript b/resource/csdk/resource-directory/SConscript
2+
index f28a5f3db..7f7edcedb 100644
3+
--- a/resource/csdk/resource-directory/SConscript
4+
+++ b/resource/csdk/resource-directory/SConscript
5+
@@ -54,7 +54,7 @@ if 'SERVER' in rd_mode:
6+
rd_env.AppendUnique(CPPDEFINES=['RD_SERVER'])
7+
8+
rd_env.PrependUnique(
9+
- LIBS=['octbstack', 'oc', 'oc_logger', 'connectivity_abstraction'])
10+
+ LIBS=['octbstack', 'oc_logger', 'connectivity_abstraction'])
11+
12+
if target_os not in ['windows']:
13+
rd_env.AppendUnique(
14+
@@ -80,7 +80,6 @@ else:
15+
######################################################################
16+
RD_SRC_DIR = 'src/'
17+
rd_src_c = []
18+
-rd_src_cpp = []
19+
20+
if 'SERVER' in rd_mode:
21+
rd_src_c += [
22+
@@ -95,19 +94,13 @@ if 'CLIENT' in rd_mode:
23+
24+
rd_src_all = rd_src_c
25+
26+
-if target_os not in ['arduino', 'darwin', 'ios']:
27+
- rd_src_cpp += [RD_SRC_DIR + 'RDClient.cpp']
28+
- if 'CLIENT' in rd_mode:
29+
- rd_src_all += rd_src_cpp
30+
-
31+
if target_os not in ['arduino', 'darwin', 'ios', 'msys_nt', 'windows']:
32+
rdsdk_shared = rd_env.SharedLibrary('resource_directory', rd_src_all)
33+
rdsdk_static = rd_env.StaticLibrary('resource_directory', rd_src_all)
34+
rdsdk = Flatten([rdsdk_static, rdsdk_shared])
35+
elif target_os in ['msys_nt', 'windows']:
36+
rdsdk_c = rd_env.StaticLibrary('resource_directory_internal', rd_src_c)
37+
- rdsdk_cpp = rd_env.StaticLibrary('resource_directory', rd_src_cpp)
38+
- rdsdk = Flatten([rdsdk_c, rdsdk_cpp])
39+
+ rdsdk = Flatten([rdsdk_c])
40+
else:
41+
rdsdk = rd_env.StaticLibrary('resource_directory', rd_src_all)
42+

src/functions/oc-do-resource.cc

Lines changed: 116 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,19 @@
1414
* limitations under the License.
1515
*/
1616

17+
#include <vector>
18+
1719
#include "../common.h"
1820
#include "../structures/handles.h"
1921
#include "../structures/oc-client-response.h"
2022
#include "../structures/oc-dev-addr.h"
2123
#include "../structures/oc-payload.h"
24+
2225
extern "C" {
2326
#include <ocpayload.h>
2427
#include <ocstack.h>
28+
#include <rd_client.h>
29+
#include <stdint.h>
2530
}
2631

2732
static void deleteCallback(void *data) {
@@ -61,9 +66,41 @@ static OCStackApplicationResult defaultOCClientResponseHandler(
6166
return cResult;
6267
}
6368

69+
#define OC_DO_CALL(env, info, arguments, conTypeIndex, qosIndex, cbIndex, \
70+
doCall) \
71+
do { \
72+
J2C_VALIDATE_VALUE_TYPE_THROW(env, (arguments)[0], napi_object, "handle"); \
73+
J2C_VALIDATE_VALUE_TYPE_THROW(env, (arguments)[(cbIndex)], napi_function, \
74+
"callback"); \
75+
J2C_DECLARE_VALUE_JS_THROW(OCConnectivityType, connectivityType, env, \
76+
arguments[(conTypeIndex)], napi_number, \
77+
"connectivityType", uint32, uint32_t); \
78+
J2C_DECLARE_VALUE_JS_THROW(OCQualityOfService, qos, env, \
79+
arguments[(qosIndex)], napi_number, "qos", \
80+
uint32, uint32_t); \
81+
\
82+
JSOCDoHandle *cData; \
83+
napi_value jsHandle; \
84+
HELPER_CALL_THROW(env, JSOCDoHandle::New(env, &jsHandle, &cData)); \
85+
\
86+
OCCallbackData cbData; \
87+
cbData.context = cData; \
88+
cbData.cb = defaultOCClientResponseHandler; \
89+
cbData.cd = deleteCallback; \
90+
\
91+
OCStackResult result = doCall; \
92+
\
93+
if (result == OC_STACK_OK) { \
94+
HELPER_CALL_THROW(env, \
95+
cData->Init(env, (arguments)[(cbIndex)], jsHandle)); \
96+
NAPI_CALL_THROW(env, napi_set_named_property(env, arguments[0], \
97+
"handle", jsHandle)); \
98+
} \
99+
C2J_SET_RETURN_VALUE(env, info, double, ((double)result)); \
100+
} while (0)
101+
64102
napi_value bind_OCDoResource(napi_env env, napi_callback_info info) {
65103
J2C_DECLARE_ARGUMENTS(env, info, 9);
66-
J2C_VALIDATE_VALUE_TYPE_THROW(env, arguments[0], napi_object, "handle");
67104
J2C_DECLARE_VALUE_JS_THROW(OCMethod, method, env, arguments[1], napi_number,
68105
"method", uint32, uint32_t);
69106
J2C_GET_STRING_TRACKED_JS_THROW(requestUri, env, arguments[2], false,
@@ -82,34 +119,89 @@ napi_value bind_OCDoResource(napi_env env, napi_callback_info info) {
82119
OCPayload *payload = nullptr;
83120
HELPER_CALL_THROW(env, c_OCPayload(env, arguments[4], &payload));
84121

85-
J2C_DECLARE_VALUE_JS_THROW(OCConnectivityType, connectivityType, env,
86-
arguments[5], napi_number, "connectivityType",
87-
uint32, uint32_t);
88-
J2C_DECLARE_VALUE_JS_THROW(OCQualityOfService, qos, env, arguments[6],
89-
napi_number, "qos", uint32, uint32_t);
122+
J2C_VALIDATE_IS_ARRAY_THROW(env, arguments[8], true, "options");
90123

91-
J2C_VALIDATE_VALUE_TYPE_THROW(env, arguments[7], napi_function, "callback");
124+
OC_DO_CALL(env, info, arguments, 5, 6, 7,
125+
OCDoResource(&(cData->data), method, requestUri, destination,
126+
payload, connectivityType, qos, &cbData, nullptr, 0));
127+
}
92128

93-
J2C_VALIDATE_IS_ARRAY_THROW(env, arguments[8], true, "options");
129+
napi_value bind_OCRDDiscover(napi_env env, napi_callback_info info) {
130+
J2C_DECLARE_ARGUMENTS(env, info, 4);
131+
OC_DO_CALL(env, info, arguments, 1, 3, 2,
132+
OCRDDiscover(&(cData->data), connectivityType, &cbData, qos));
133+
}
94134

95-
JSOCDoHandle *cData;
135+
static std::string c_OCResourceArray(
136+
napi_env env, napi_value source,
137+
std::vector<OCResourceHandle> &destination) {
138+
uint32_t length, index;
139+
napi_handle_scope scope;
96140
napi_value jsHandle;
97-
HELPER_CALL_THROW(env, JSOCDoHandle::New(env, &jsHandle, &cData));
98-
99-
OCCallbackData cbData;
100-
cbData.context = cData;
101-
cbData.cb = defaultOCClientResponseHandler;
102-
cbData.cd = deleteCallback;
103-
OCStackResult result =
104-
OCDoResource(&(cData->data), method, requestUri, destination, payload,
105-
connectivityType, qos, &cbData, nullptr, 0);
106-
107-
if (result == OC_STACK_OK) {
108-
HELPER_CALL_THROW(env, cData->Init(env, arguments[7], jsHandle));
109-
NAPI_CALL_THROW(
110-
env, napi_set_named_property(env, arguments[0], "handle", jsHandle));
141+
JSOCResourceHandle *cResData;
142+
143+
J2C_VALIDATE_IS_ARRAY_RETURN(env, source, false, "resource array");
144+
145+
NAPI_CALL_RETURN(env, napi_get_array_length(env, source, &length));
146+
if (length > UINT8_MAX) {
147+
return LOCAL_MESSAGE("resource array length exceeds UINT8_MAX");
148+
}
149+
150+
destination.resize(length);
151+
for (index = 0; index < length; index++) {
152+
NAPI_CALL_RETURN(env, napi_open_handle_scope(env, &scope));
153+
NAPI_CALL_RETURN(env, napi_get_element(env, source, index, &jsHandle));
154+
HELPER_CALL_RETURN(JSOCResourceHandle::Get(env, jsHandle, &cResData));
155+
destination[index] = cResData->data;
156+
NAPI_CALL_RETURN(env, napi_close_handle_scope(env, scope));
111157
}
112-
C2J_SET_RETURN_VALUE(env, info, double, ((double)result));
158+
159+
return std::string();
160+
}
161+
162+
#define OC_DO_RESOURCE_LIST_CALL(env, info, arguments, hostIndex, \
163+
conTypeIndex, resourceListIndex, cbIndex, \
164+
qosIndex, doCall) \
165+
do { \
166+
J2C_GET_STRING_TRACKED_JS_THROW(host, env, (arguments)[(hostIndex)], \
167+
false, "host"); \
168+
std::vector<OCResourceHandle> handles; \
169+
HELPER_CALL_THROW( \
170+
env, \
171+
c_OCResourceArray(env, (arguments)[(resourceListIndex)], handles)); \
172+
\
173+
OC_DO_CALL((env), (info), (arguments), (conTypeIndex), (qosIndex), \
174+
(cbIndex), doCall); \
175+
} while (0)
176+
177+
napi_value bind_OCRDPublish(napi_env env, napi_callback_info info) {
178+
J2C_DECLARE_ARGUMENTS(env, info, 7);
179+
J2C_DECLARE_VALUE_JS_THROW(uint32_t, ttl, env, arguments[4], napi_number,
180+
"ttl", uint32, uint32_t);
181+
OC_DO_RESOURCE_LIST_CALL(
182+
env, info, arguments, 1, 2, 3, 5, 6,
183+
OCRDPublish(&(cData->data), host, connectivityType, handles.data(),
184+
handles.size(), ttl, &cbData, qos));
185+
}
186+
187+
napi_value bind_OCRDPublishWithDeviceId(napi_env env, napi_callback_info info) {
188+
J2C_DECLARE_ARGUMENTS(env, info, 8);
189+
J2C_GET_STRING_TRACKED_JS_THROW(id, env, arguments[2], false, "id");
190+
J2C_DECLARE_VALUE_JS_THROW(uint32_t, ttl, env, arguments[5], napi_number,
191+
"ttl", uint32, uint32_t);
192+
OC_DO_RESOURCE_LIST_CALL(
193+
env, info, arguments, 1, 3, 4, 6, 7,
194+
OCRDPublishWithDeviceId(&(cData->data), host, (const unsigned char *)id,
195+
connectivityType, handles.data(), handles.size(),
196+
ttl, &cbData, qos));
197+
}
198+
199+
napi_value bind_OCRDDelete(napi_env env, napi_callback_info info) {
200+
J2C_DECLARE_ARGUMENTS(env, info, 6);
201+
OC_DO_RESOURCE_LIST_CALL(
202+
env, info, arguments, 1, 2, 3, 4, 5,
203+
OCRDDelete(&(cData->data), host, connectivityType, handles.data(),
204+
handles.size(), &cbData, qos));
113205
}
114206

115207
napi_value bind_OCCancel(napi_env env, napi_callback_info info) {

src/functions/simple.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
extern "C" {
2222
#include <ocstack.h>
23+
#include <rd_server.h>
2324
}
2425

2526
std::string getDateCtor(napi_env env, napi_value *result) {
@@ -72,6 +73,14 @@ napi_value bind_OCStopPresence(napi_env env, napi_callback_info info) {
7273
C2J_SET_RETURN_VALUE(env, info, double, ((double)OCStopPresence()));
7374
}
7475

76+
napi_value bind_OCRDStart(napi_env env, napi_callback_info info) {
77+
C2J_SET_RETURN_VALUE(env, info, double, ((double)OCRDStart()));
78+
}
79+
80+
napi_value bind_OCRDStop(napi_env env, napi_callback_info info) {
81+
C2J_SET_RETURN_VALUE(env, info, double, ((double)OCRDStop()));
82+
}
83+
7584
napi_value bind_OCGetNumberOfResources(napi_env env, napi_callback_info info) {
7685
J2C_DECLARE_ARGUMENTS(env, info, 1);
7786
J2C_VALIDATE_VALUE_TYPE_THROW(env, arguments[0], napi_object, "receptacle");

tests/provision/produceClientContent.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ module.exports = function produceClientContent( item, creds ) {
2424
{ href: "/oic/sec/doxm" }
2525
],
2626
permission: 2
27+
},
28+
{
29+
aceid: 3,
30+
subject: { conntype: "anon-clear" },
31+
resources: [
32+
{ href: "/oic/rd" }
33+
],
34+
permission: 31
35+
},
36+
{
37+
aceid: 4,
38+
subject: { conntype: "auth-crypt" },
39+
resources: [
40+
{ href: "/oic/rd" }
41+
],
42+
permission: 31
2743
}
2844
],
2945
rowneruuid: item.deviceUuid

tests/security.boilerplate.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"subject": { "conntype": "anon-clear" },
66
"resources": [
77
{ "href": "/oic/res" },
8+
{ "href": "/oic/rd" },
89
{ "href": "/oic/d" },
910
{ "href": "/oic/p" }
1011
],
@@ -14,6 +15,7 @@
1415
"subject": { "conntype": "auth-crypt" },
1516
"resources": [
1617
{ "href": "/oic/res" },
18+
{ "href": "/oic/rd" },
1719
{ "href": "/oic/d" },
1820
{ "href": "/oic/p"}
1921
],

0 commit comments

Comments
 (0)