Commit 55de9c39 authored by George Nash's avatar George Nash

Rework of oc_introspection.h

The oc_set_introspection_file is no long avalible. Now
it will default to the file IDD_<device_index> from oc_storage.

This adds the oc_set_introspection_data function to
the oc_introspection.h header. This enables setting
the introspection data by passing in an array of bytes via
the oc_set_introspection_data function.

The compiler define OC_IDD_FILE was change to OC_IDD_API since
we no longer specify file names.

As a side in the windows  build OC_IDD_API is set as the
default.

Users can get the same behavior as building the code without
OC_IDD_API by including server_introspection.dat.h in their
server program and calling
oc_set_interspection_data(0,
                          introspection_Data,
                          introspection_data_size);

The function oc_create_introspection_resourcewas moved
to a new internal header file oc_introspection_internal.h.
The function is only expected to be called by the framework
and there is no need to expose it to end users as public APIs.

Change-Id: I9ec452afbf96510c82557eeb1b8b79a8949eeb25
Signed-off-by: George Nash's avatarGeorge Nash <george.nash@intel.com>
parent 1d7cd051
......@@ -18,7 +18,7 @@
#include "messaging/coap/oc_coap.h"
#include "oc_api.h"
#include "oc_discovery.h"
#include "oc_introspection.h"
#include "oc_introspection_internal.h"
#include "oc_rep.h"
#ifdef OC_SECURITY
......
......@@ -19,113 +19,37 @@
#include "oc_api.h"
#include "oc_core_res.h"
#include "oc_endpoint.h"
#include "oc_introspection_internal.h"
#include <stdio.h>
#define MAX_FILENAME_LENGTH 128
#ifdef OC_IDD_FILE
#ifndef OC_IDD_API
#include "server_introspection.dat.h"
#else /* OC_IDD_API */
#define MAX_TAG_LENGTH 20
#ifdef OC_SECURITY
static void
gen_idd_tag(const char *name, size_t device_index, char *idd_tag)
{
int idd_tag_len =
snprintf(idd_tag, MAX_TAG_LENGTH, "%s_%d", name, device_index);
snprintf(idd_tag, MAX_TAG_LENGTH, "%s_%zd", name, device_index);
idd_tag_len =
(idd_tag_len < MAX_TAG_LENGTH) ? idd_tag_len + 1 : MAX_TAG_LENGTH;
idd_tag[idd_tag_len] = '\0';
}
static int
get_IDD_filename(size_t device_index, char *filename)
{
char idd_tag[MAX_TAG_LENGTH];
gen_idd_tag("IDD", device_index, idd_tag);
int ret = oc_storage_read(idd_tag, (uint8_t *)filename, MAX_FILENAME_LENGTH);
PRINT("get_IDD_filename: oc_storage_read %d\n", ret);
if (ret <= 0) {
strcpy(filename, "server_introspection.dat");
}
PRINT("get_IDD_filename: returning %s\n", filename);
return ret;
}
#endif /* OC_SECURITY */
void
oc_set_introspection_file(size_t device, const char *filename)
oc_set_introspection_data(size_t device, uint8_t *IDD, size_t IDD_size)
{
#ifdef OC_SECURITY
char idd_tag[MAX_TAG_LENGTH];
gen_idd_tag("IDD", device, idd_tag);
long ret =
oc_storage_write(idd_tag, (uint8_t *)filename, MAX_FILENAME_LENGTH);
if (ret == 0) {
OC_ERR("oc_set_introspection_file: could not set %s in store\n", filename);
}
}
static long
IDD_storage_size(const char *store)
{
FILE *fp;
long filesize;
fp = fopen(store, "rb");
if (!fp) {
OC_ERR("IDD_storage_size: ERROR file %s does not open\n", store);
return 0;
}
fseek(fp, 0, SEEK_END);
filesize = ftell(fp);
fclose(fp);
PRINT("IDD_storage_size %s size %d [bytes] \n", store, filesize);
return filesize;
}
static size_t
IDD_storage_read(const char *store, uint8_t *buf, size_t size)
{
FILE *fp = 0;
fp = fopen(store, "rb");
if (!fp) {
OC_ERR("IDD_storage_size file %s does not open\n", store);
return 0;
}
size = fread(buf, 1, size, fp);
fclose(fp);
return size;
}
#else /*OC_IDD_FILE*/
#include "server_introspection.dat.h"
static int
get_IDD_filename(size_t index, char *filename)
{
(void)index;
(void)filename;
return 0;
}
static long
IDD_storage_size(const char *store)
{
(void)store;
return introspection_data_size;
oc_storage_write(idd_tag, IDD, IDD_size);
#endif /* OC_SECURITY */
}
static size_t
IDD_storage_read(const char *store, uint8_t *buf, size_t size)
{
(void)store;
memcpy(buf, introspection_data, size);
return size;
}
#endif /*OC_IDD_FILE*/
#endif /*OC_IDD_API*/
static void
oc_core_introspection_data_handler(oc_request_t *request,
......@@ -136,22 +60,31 @@ oc_core_introspection_data_handler(oc_request_t *request,
OC_DBG("in oc_core_introspection_data_handler");
size_t index = request->resource->device;
char filename[MAX_FILENAME_LENGTH];
long filesize;
get_IDD_filename(index, filename);
filesize = IDD_storage_size(filename);
if (filesize < OC_MAX_APP_DATA_SIZE) {
IDD_storage_read(filename, request->response->response_buffer->buffer,
filesize);
request->response->response_buffer->response_length = (uint16_t)filesize;
long IDD_size = 0;
#ifndef OC_IDD_API
if (introspection_data_size < OC_MAX_APP_DATA_SIZE) {
memcpy(request->response->response_buffer->buffer, introspection_data,
introspection_data_size);
IDD_size = introspection_data_size;
} else {
IDD_size = -1;
}
#else /* OC_IDD_API */
#ifdef OC_SECURITY
char idd_tag[MAX_TAG_LENGTH];
gen_idd_tag("IDD", request->resource->device, idd_tag);
IDD_size = oc_storage_read(
idd_tag, request->response->response_buffer->buffer, OC_MAX_APP_DATA_SIZE);
#endif /* OC_SECURITY */
#endif /* OC_IDD_API */
if (IDD_size >= 0 && IDD_size < OC_MAX_APP_DATA_SIZE) {
request->response->response_buffer->response_length = (uint16_t)IDD_size;
request->response->response_buffer->code = oc_status_code(OC_STATUS_OK);
} else {
OC_ERR(
"oc_core_introspection_data_handler : %d is too big for buffer %d \n",
filesize, OC_MAX_APP_DATA_SIZE);
IDD_size, OC_MAX_APP_DATA_SIZE);
request->response->response_buffer->response_length = (uint16_t)0;
request->response->response_buffer->code =
oc_status_code(OC_STATUS_INTERNAL_SERVER_ERROR);
......
/*
// Copyright (c) 2019 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.
*/
#ifndef OC_INTROSPECTION_INTERNAL_H
#define OC_INTROSPECTION_INTERNAL_H
/**
@brief Creation of the oic.wk.introspection resource.
@param device index of the device to which the resource is to be created
*/
void oc_create_introspection_resource(size_t device);
#endif /* OC_INTROSPECTION_INTERNAL_H */
......@@ -26,6 +26,7 @@
#include "oc_api.h"
#include "oc_core_res.h"
#include "oc_introspection_internal.h"
#include "oc_signal_event_loop.h"
#ifdef OC_SECURITY
......
......@@ -20,6 +20,7 @@
#define OC_CORE_RES_H
#include "oc_ri.h"
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
......@@ -28,7 +29,7 @@ extern "C" {
typedef void (*oc_core_init_platform_cb_t)(void *data);
typedef void (*oc_core_add_device_cb_t)(void *data);
typedef struct
typedef struct oc_platform_info_t
{
oc_uuid_t pi;
oc_string_t mfg_name;
......
......@@ -14,54 +14,59 @@
// limitations under the License.
*/
/**
@file
*/
* @file
* @brief functions for introspection
*
* The IDD information is served up as encoded CBOR contents (read as is).
* If the size of the IDD data is to big for the buffer, then an internal error
* is returned. Note that some build options can only serve up introspection
* data for one device and can not be used if multiple devices are implemented.
*
* There are multiple mechanisms for adding introspection data to a server.
*
* Add OC_IDD_API (recomended) to the compilers preprocessor macro defines.
* Then set the introspection data for each device using
* oc_set_introspection_data function.
*
* If OC_IDD_API was added to the preprocessor and the server does not call
* oc_set_introspection_data the server will look for the file
* `IDD_<device_index>` in the location specified by oc_storage_config.
*
* The final option is to build without OC_IDD_API compiler preprocessor macro
* define. This is **not recomended** but is provided for backwards
* compatability. The instrospection data is read from the headerfile
* `server_introspection.dat.h`. Introspection data can only be added for a
* single device.
*
* When using the final option the `server_introspeciton.dat.h` must
* contain two items:
* 1. A macro `introspection_data_size` that specifies the size of the
* introspection data in bytes
* 2. An array `uint8_t introspection_data[]` containing the hex data
* representing the IDD. The header file must be placed in a folder that gets
* included in the build.
*/
#ifndef OC_INTROSPECTION_H
#define OC_INTROSPECTION_H
#include <stddef.h>
#include <inttypes.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief functions for introspection .
* The introspection mechanism is implemented by 2 different mechanisms
*
* The first mechanism is reading the IDD file from a location.
* The file is read with standard c library (fopen, fread, ..)
* The IDD information is served up as encoded CBOR contents (e.g. read as is).
* The file is read and passed to the requesting client on each call.
* if the file size is to big for the buffer, then an internal error is given
* back. note that this option can serve up more than 1 introspection file, if
* multiple devices are implemented. This feature is enabled when the compile
* switch OC_IDD_FILE is used.
* @brief Set the IDD by passing in an array containing the data
*
* The second option is to use an include file that contains the IDD
* information. This is the default mechanism. The include file is called
* "server_introspection.dat.h" and should contain introspection_data_size :
* size of the array introspection_data uint8_t introspection_data[] : hex data
* representing the IDD. one should place this file in a folder that gets
* included in the build for example the include directory.
* @param device index of the device to which the IDD describes
* @param IDD an array of CBOR encoded bytes containing the introspection device
* data
* @param IDD_size the size of the IDD array
*/
/**
@brief sets the filename of the introspection resource.
if not set the file is "server_introspection.dat" is read from the location
where the executable resides
@param device index of the device to which the resource is to be created
@param filename filename of the IDD file in cbor.
*/
void oc_set_introspection_file(size_t device, const char *filename);
/**
@brief Creation of the oic.wk.introspection resource.
@param device index of the device to which the resource is to be created
*/
void oc_create_introspection_resource(size_t device);
void oc_set_introspection_data(size_t device, uint8_t *IDD, size_t IDD_size);
#ifdef __cplusplus
}
......
......@@ -94,7 +94,7 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;OC_CLIENT;OC_SERVER;OC_IPV4;OC_SECURITY;OC_PKI;OC_DYNAMIC_ALLOCATION;_DEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;__OC_RANDOM;OC_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;OC_CLIENT;OC_SERVER;OC_IPV4;OC_SECURITY;OC_PKI;OC_DYNAMIC_ALLOCATION;OC_IDD_FILE;_DEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;__OC_RANDOM;OC_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MinimalRebuild>true</MinimalRebuild>
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
......@@ -128,7 +128,7 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;OC_CLIENT;OC_SERVER;OC_IPV4;OC_SECURITY;OC_PKI;OC_DYNAMIC_ALLOCATION;_DEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;__OC_RANDOM;OC_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;OC_CLIENT;OC_SERVER;OC_IPV4;OC_SECURITY;OC_PKI;OC_DYNAMIC_ALLOCATION;OC_IDD_FILE;_DEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;__OC_RANDOM;OC_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
......@@ -159,7 +159,7 @@
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<PreprocessorDefinitions>WIN32;OC_CLIENT;OC_SERVER;OC_IPV4;OC_SECURITY;OC_PKI;OC_DYNAMIC_ALLOCATION;NDEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;__OC_RANDOM;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;OC_CLIENT;OC_SERVER;OC_IPV4;OC_SECURITY;OC_PKI;OC_DYNAMIC_ALLOCATION;OC_IDD_FILE;NDEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;__OC_RANDOM;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
......@@ -186,7 +186,7 @@
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<PreprocessorDefinitions>WIN32;OC_CLIENT;OC_SERVER;OC_IPV4;OC_SECURITY;OC_PKI;OC_DYNAMIC_ALLOCATION;NDEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;__OC_RANDOM;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;OC_CLIENT;OC_SERVER;OC_IPV4;OC_SECURITY;OC_PKI;OC_DYNAMIC_ALLOCATION;OC_IDD_FILE;NDEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;__OC_RANDOM;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
......@@ -213,6 +213,7 @@
<ItemGroup>
<ClInclude Include="..\..\..\api\c-timestamp\timestamp.h" />
<ClInclude Include="..\..\..\api\oc_events.h" />
<ClInclude Include="..\..\..\api\oc_introspection_internal.h" />
<ClInclude Include="..\..\..\api\oc_main.h" />
<ClInclude Include="..\..\..\deps\tinycbor\src\cbor.h" />
<ClInclude Include="..\..\..\deps\tinycbor\src\cborjson.h" />
......
......@@ -588,6 +588,9 @@
<ClInclude Include="..\..\..\api\c-timestamp\timestamp.h">
<Filter>Core\c-timestamp</Filter>
</ClInclude>
<ClInclude Include="..\..\..\api\oc_introspection_internal.h">
<Filter>Core</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="mbedTLS">
......
......@@ -98,7 +98,7 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;OC_CLIENT;OC_SERVER;OC_IPV4;OC_SECURITY;OC_PKI;OC_DYNAMIC_ALLOCATION;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;__OC_RANDOM;OC_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;OC_CLIENT;OC_SERVER;OC_IPV4;OC_SECURITY;OC_PKI;OC_DYNAMIC_ALLOCATION;OC_IDD_FILE;OC_IDD_FILE_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;__OC_RANDOM;OC_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..\;..\..\..\include;..\..\..\;.\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
......@@ -118,7 +118,7 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;OC_CLIENT;OC_SERVER;OC_IPV4;OC_SECURITY;OC_PKI;OC_DYNAMIC_ALLOCATION;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;__OC_RANDOM;OC_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;OC_CLIENT;OC_SERVER;OC_IPV4;OC_SECURITY;OC_PKI;OC_DYNAMIC_ALLOCATION;OC_IDD_FILE;OC_IDD_FILE_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;__OC_RANDOM;OC_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..\;..\..\..\include;..\..\..\;.\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile>
......@@ -140,7 +140,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;OC_CLIENT;OC_SERVER;OC_IPV4;OC_SECURITY;OC_PKI;OC_DYNAMIC_ALLOCATION;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;__OC_RANDOM;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;OC_CLIENT;OC_SERVER;OC_IPV4;OC_SECURITY;OC_PKI;OC_DYNAMIC_ALLOCATION;OC_IDD_FILE;OC_IDD_FILENDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;__OC_RANDOM;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..\;..\..\..\include;..\..\..\;.\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
......@@ -165,7 +165,7 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>..\;..\..\..\include;..\..\..\;.\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;OC_CLIENT;OC_SERVER;OC_IPV4;OC_SECURITY;OC_PKI;OC_DYNAMIC_ALLOCATION;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;__OC_RANDOM;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;OC_CLIENT;OC_SERVER;OC_IPV4;OC_SECURITY;OC_PKI;OC_DYNAMIC_ALLOCATION;OC_IDD_FILE;OC_IDD_FILENDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;__OC_RANDOM;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment