Commit a568102c authored by George Nash's avatar George Nash

Merge branch 'origin/master' into swig

Change-Id: Ia4b7b622ff06ded0ffdf29cabab915d50d3ab660
Signed-off-by: George Nash's avatarGeorge Nash <george.nash@intel.com>
parents 46e9e48e 4a84ea85
......@@ -166,6 +166,8 @@ oc_core_introspection_wk_handler(oc_request_t *request,
int interface_index =
(request->origin) ? request->origin->interface_index : -1;
enum transport_flags conn =
(request->origin && (request->origin->flags & IPV6)) ? IPV6 : IPV4;
/* We are interested in only a single coap:// endpoint on this logical device.
*/
oc_endpoint_t *eps = oc_connectivity_get_endpoints(request->resource->device);
......@@ -173,7 +175,7 @@ oc_core_introspection_wk_handler(oc_request_t *request,
memset(&uri, 0, sizeof(oc_string_t));
while (eps != NULL) {
if ((interface_index == -1 || eps->interface_index == interface_index) &&
!(eps->flags & SECURED)) {
!(eps->flags & SECURED) && (eps->flags == conn)) {
if (oc_endpoint_to_string(eps, &ep) == 0) {
oc_concat_strings(&uri, oc_string(ep), "/oc/introspection");
oc_free_string(&ep);
......
......@@ -46,8 +46,24 @@
#include "util/oc_mem_trace.h"
#endif /* OC_MEMORY_TRACE */
#include "oc_main.h"
static bool initialized = false;
static const oc_handler_t *app_callbacks;
static oc_factory_presets_t factory_presets;
void
oc_set_factory_presets_cb(oc_factory_presets_cb_t cb, void *data)
{
factory_presets.cb = cb;
factory_presets.data = data;
}
oc_factory_presets_t *
oc_get_factory_presets_cb(void)
{
return &factory_presets;
}
#ifdef OC_DYNAMIC_ALLOCATION
#include "oc_buffer_settings.h"
......@@ -204,6 +220,9 @@ oc_main_init(const oc_handler_t *handler)
oc_sec_load_ecdsa_keypair(device);
#endif /* OC_PKI */
oc_sec_load_unique_ids(device);
if (factory_presets.cb != NULL) {
factory_presets.cb(device, factory_presets.data);
}
}
#endif
......
/*
// 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_MAIN_H
#define OC_MAIN_H
#include "oc_api.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct oc_factory_presets_t
{
oc_factory_presets_cb_t cb;
void *data;
} oc_factory_presets_t;
oc_factory_presets_t *oc_get_factory_presets_cb(void);
typedef struct oc_random_pin_t
{
oc_random_pin_cb_t cb;
void *data;
} oc_random_pin_t;
#ifdef __cplusplus
}
#endif
#endif /* OC_MAIN_H */
......@@ -1102,10 +1102,10 @@ oc_ri_invoke_coap_entity_handler(void *request, void *response, uint8_t *buffer,
static void
free_client_cb(oc_client_cb_t *cb)
{
oc_list_remove(client_cbs, cb);
#ifdef OC_BLOCK_WISE
oc_blockwise_scrub_buffers_for_client_cb(cb);
#endif /* OC_BLOCK_WISE */
oc_list_remove(client_cbs, cb);
oc_free_string(&cb->uri);
if (oc_string_len(cb->query)) {
oc_free_string(&cb->query);
......
......@@ -140,36 +140,11 @@ handle_signal(int signal)
quit = 1;
}
int
main(void)
void
factory_presets_cb(size_t device, void *data)
{
int init;
struct sigaction sa;
sigfillset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = handle_signal;
sigaction(SIGINT, &sa, NULL);
static const oc_handler_t handler = {.init = app_init,
.signal_event_loop = signal_event_loop,
.register_resources =
register_resources };
oc_clock_time_t next_event;
oc_set_con_res_announced(false);
oc_set_mtu_size(16384);
oc_set_max_app_data_size(16384);
#ifdef OC_SECURITY
oc_storage_config("./simpleserver_pki_creds");
#endif /* OC_SECURITY */
init = oc_main_init(&handler);
if (init < 0) {
return init;
}
(void)device;
(void)data;
#if defined(OC_SECURITY) && defined(OC_PKI)
const unsigned char my_crt[] = {
0x30, 0x82, 0x03, 0xf8, 0x30, 0x82, 0x03, 0x9e, 0xa0, 0x03, 0x02, 0x01,
......@@ -393,6 +368,39 @@ main(void)
oc_pki_set_security_profile(0, OC_SP_BLACK, OC_SP_BLACK, credid);
#endif /* OC_SECURITY && OC_PKI */
}
int
main(void)
{
int init;
struct sigaction sa;
sigfillset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = handle_signal;
sigaction(SIGINT, &sa, NULL);
static const oc_handler_t handler = {.init = app_init,
.signal_event_loop = signal_event_loop,
.register_resources =
register_resources };
oc_clock_time_t next_event;
oc_set_con_res_announced(false);
oc_set_mtu_size(16384);
oc_set_max_app_data_size(16384);
#ifdef OC_SECURITY
oc_storage_config("./simpleserver_pki_creds");
#endif /* OC_SECURITY */
oc_set_factory_presets_cb(factory_presets_cb, NULL);
init = oc_main_init(&handler);
if (init < 0) {
return init;
}
while (quit != 1) {
next_event = oc_main_poll();
......
......@@ -353,34 +353,20 @@ handle_signal(int signal)
quit = 1;
}
int
main(void)
{
int init;
struct sigaction sa;
sigfillset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = handle_signal;
sigaction(SIGINT, &sa, NULL);
static const oc_handler_t handler = {.init = app_init,
.signal_event_loop = signal_event_loop,
.register_resources =
register_resources };
oc_clock_time_t next_event;
oc_set_con_res_announced(false);
oc_set_mtu_size(16384);
oc_set_max_app_data_size(16384);
#ifdef OC_SECURITY
oc_storage_config("./smart_home_server_linux_creds");
void
random_pin_cb(const unsigned char *pin, size_t pin_len, void *data)
{
(void)data;
PRINT("\n\nRandom PIN: %.*s\n\n", pin_len, pin);
}
#endif /* OC_SECURITY */
init = oc_main_init(&handler);
if (init < 0)
return init;
void
factory_presets_cb(size_t device, void *data)
{
(void)device;
(void)data;
#if defined(OC_SECURITY) && defined(OC_PKI)
const unsigned char my_crt[] = {
0x30, 0x82, 0x03, 0xf8, 0x30, 0x82, 0x03, 0x9e, 0xa0, 0x03, 0x02, 0x01,
......@@ -604,6 +590,40 @@ main(void)
oc_pki_set_security_profile(0, OC_SP_BLACK, OC_SP_BLACK, credid);
#endif /* OC_SECURITY && OC_PKI */
}
int
main(void)
{
int init;
struct sigaction sa;
sigfillset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = handle_signal;
sigaction(SIGINT, &sa, NULL);
static const oc_handler_t handler = {.init = app_init,
.signal_event_loop = signal_event_loop,
.register_resources =
register_resources };
oc_clock_time_t next_event;
oc_set_con_res_announced(false);
oc_set_mtu_size(16384);
oc_set_max_app_data_size(16384);
#ifdef OC_SECURITY
oc_storage_config("./smart_home_server_linux_creds");
#endif /* OC_SECURITY */
oc_set_factory_presets_cb(factory_presets_cb, NULL);
#ifdef OC_SECURITY
oc_set_random_pin_callback(random_pin_cb, NULL);
#endif /* OC_SECURITY */
init = oc_main_init(&handler);
if (init < 0)
return init;
while (quit != 1) {
next_event = oc_main_poll();
......
......@@ -240,11 +240,15 @@ get_lock_state(bool observe)
PRINT("\nERROR: Invalid selection.. Try again..\n");
} else {
if (observe) {
oc_do_observe(locks[c]->uri, locks[c]->endpoint, NULL, GET_handler,
HIGH_QOS, locks[c]);
if (!oc_do_observe(locks[c]->uri, locks[c]->endpoint, NULL, GET_handler,
HIGH_QOS, locks[c])) {
PRINT("\nERROR: Could not issue Observe request to lock\n");
}
} else {
oc_do_get(locks[c]->uri, locks[c]->endpoint, NULL, GET_handler,
HIGH_QOS, locks[c]);
if (!oc_do_get(locks[c]->uri, locks[c]->endpoint, NULL, GET_handler,
HIGH_QOS, locks[c])) {
PRINT("\nERROR Could not issue GET request to lock\n");
}
}
}
} else {
......@@ -369,7 +373,9 @@ null_discovery(const char *di, const char *uri, oc_string_array_t types,
static void
issue_requests(void)
{
oc_do_ip_discovery(NULL, &null_discovery, NULL);
if (!oc_do_ip_discovery(NULL, &null_discovery, NULL)) {
PRINT("\nERROR: Could not issue discovery request\n");
}
}
static void
......@@ -377,12 +383,22 @@ discover_smart_locks(void)
{
pthread_mutex_lock(&app_sync_lock);
free_all_known_locks();
oc_do_ip_discovery("oic.r.lock.status", &discovery, NULL);
oc_set_delayed_callback(NULL, show_discovered_locks, 5);
if (oc_do_ip_discovery("oic.r.lock.status", &discovery, NULL)) {
oc_set_delayed_callback(NULL, show_discovered_locks, 5);
}
pthread_mutex_unlock(&app_sync_lock);
signal_event_loop();
}
#ifdef OC_SECURITY
void
random_pin_cb(const unsigned char *pin, size_t pin_len, void *data)
{
(void)data;
PRINT("\n\nRandom PIN: %.*s\n\n", pin_len, pin);
}
#endif /* OC_SECURITY */
int
main(void)
{
......@@ -400,6 +416,7 @@ main(void)
#ifdef OC_SECURITY
oc_storage_config("./smart_lock_creds");
oc_set_random_pin_callback(random_pin_cb, NULL);
#endif /* OC_SECURITY */
oc_set_con_res_announced(false);
......
......@@ -61,6 +61,9 @@ int oc_main_init(const oc_handler_t *handler);
oc_clock_time_t oc_main_poll(void);
void oc_main_shutdown(void);
typedef void (*oc_factory_presets_cb_t)(size_t device, void *data);
void oc_set_factory_presets_cb(oc_factory_presets_cb_t cb, void *data);
int oc_add_device(const char *uri, const char *rt, const char *name,
const char *spec_version, const char *data_model_version,
oc_add_device_cb_t add_device_cb, void *data);
......@@ -74,6 +77,10 @@ int oc_init_platform(const char *mfg_name,
#define oc_set_custom_platform_property(prop, value) \
oc_rep_set_text_string(root, prop, value)
typedef void (*oc_random_pin_cb_t)(const unsigned char *pin, size_t pin_len,
void *data);
void oc_set_random_pin_callback(oc_random_pin_cb_t cb, void *data);
/**
@brief Returns whether the oic.wk.con res is announed.
@return true if announced (default) or false if not
......
/*
// Copyright (c) 2017 Intel Corporation
// Copyright (c) 2017-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.
......@@ -34,6 +34,8 @@ typedef void (*oc_obt_status_cb_t)(int, void *);
/* Call once at startup for OBT initialization */
void oc_obt_init(void);
/* Called when the OBT terminates to free all resources */
void oc_obt_shutdown(void);
/* Device discovery */
int oc_obt_discover_unowned_devices(oc_obt_discovery_cb_t cb, void *data);
......@@ -42,6 +44,11 @@ int oc_obt_discover_owned_devices(oc_obt_discovery_cb_t cb, void *data);
/* Perform ownership transfer */
int oc_obt_perform_just_works_otm(oc_uuid_t *uuid, oc_obt_device_status_cb_t cb,
void *data);
int oc_obt_request_random_pin(oc_uuid_t *uuid, oc_obt_device_status_cb_t cb,
void *data);
int oc_obt_perform_random_pin_otm(oc_uuid_t *uuid, const unsigned char *pin,
size_t pin_len, oc_obt_device_status_cb_t cb,
void *data);
/* RESET device state */
int oc_obt_device_hard_reset(oc_uuid_t *uuid, oc_obt_device_status_cb_t cb,
......
......@@ -155,19 +155,11 @@ coap_send_transaction(coap_transaction_t *t)
} else {
/* timed out */
OC_WRN("Timeout");
#ifdef OC_SERVER
OC_WRN("timeout.. so removing observers");
/* handle observers */
/* remove observers */
coap_remove_observer_by_client(&t->message->endpoint);
#endif /* OC_SERVER */
#ifdef OC_SECURITY
if (t->message->endpoint.flags & SECURED) {
oc_tls_close_connection(&t->message->endpoint);
}
#endif /* OC_SECURITY */
#ifdef OC_CLIENT
oc_ri_remove_client_cb_by_mid(t->mid);
#endif /* OC_CLIENT */
......@@ -175,8 +167,14 @@ coap_send_transaction(coap_transaction_t *t)
#ifdef OC_BLOCK_WISE
oc_blockwise_scrub_buffers();
#endif /* OC_BLOCK_WISE */
coap_clear_transaction(t);
#ifdef OC_SECURITY
if (t->message->endpoint.flags & SECURED) {
oc_tls_close_connection(&t->message->endpoint);
} else
#endif /* OC_SECURITY */
{
coap_clear_transaction(t);
}
}
} else {
oc_message_add_ref(t->message);
......@@ -225,7 +223,12 @@ coap_check_transactions(void)
if (oc_etimer_expired(&t->retrans_timer)) {
++(t->retrans_counter);
OC_DBG("Retransmitting %u (%u)", t->mid, t->retrans_counter);
int removed = oc_list_length(transactions_list);
coap_send_transaction(t);
if ((removed - oc_list_length(transactions_list)) > 1) {
t = (coap_transaction_t *)oc_list_head(transactions_list);
continue;
}
}
t = next;
}
......@@ -242,3 +245,25 @@ coap_free_all_transactions(void)
t = next;
}
}
void
coap_free_transaction_by_endpoint(oc_endpoint_t *endpoint)
{
#ifdef OC_SERVER
/* remove all observations by this peer */
coap_remove_observer_by_client(endpoint);
#endif /* OC_SERVER */
coap_transaction_t *t = (coap_transaction_t *)oc_list_head(transactions_list),
*next;
while (t != NULL) {
next = t->next;
if (oc_endpoint_compare(&t->message->endpoint, endpoint) == 0) {
#ifdef OC_CLIENT
/* Remove the client callback tied to this transaction */
oc_ri_remove_client_cb_by_mid(t->mid);
#endif /* OC_CLIENT */
coap_clear_transaction(t);
}
t = next;
}
}
......@@ -91,6 +91,7 @@ coap_transaction_t *coap_get_transaction_by_mid(uint16_t mid);
void coap_check_transactions(void);
void coap_free_all_transactions(void);
void coap_free_transaction_by_endpoint(oc_endpoint_t *endpoint);
#ifdef __cplusplus
}
......
......@@ -46,7 +46,6 @@ OC_LIST(unowned_devices);
#if defined (_WIN32)
static HANDLE event_thread;
//static HANDLE app_sync_lock;
static CRITICAL_SECTION app_sync_lock;
static CONDITION_VARIABLE cv;
static CRITICAL_SECTION cs;
......@@ -79,11 +78,13 @@ display_menu(void)
PRINT("[1] Discover un-owned devices\n");
PRINT("[2] Discover owned devices\n");
PRINT("-----------------------------------------------\n");
PRINT("[3] Take ownership of device (Just-works)\n");
PRINT("[4] Provision pair-wise credentials\n");
PRINT("[5] Provision ACE2\n");
PRINT("[3] Just-Works Ownership Transfer Method\n");
PRINT("[4] Request Random PIN from device for OTM\n");
PRINT("[5] Random PIN Ownership Transfer Method\n");
PRINT("[6] Provision pair-wise credentials\n");
PRINT("[7] Provision ACE2\n");
PRINT("-----------------------------------------------\n");
PRINT("[6] RESET device\n");
PRINT("[8] RESET device\n");
PRINT("-----------------------------------------------\n");
PRINT("[9] Exit\n");
PRINT("################################################\n");
......@@ -155,6 +156,7 @@ ocf_event_thread(LPVOID lpParam)
}
oc_main_shutdown();
oc_obt_shutdown();
return TRUE;
}
#elif defined(__linux__)
......@@ -179,6 +181,7 @@ ocf_event_thread(void *data)
otb_mutex_unlock(mutex);
}
oc_main_shutdown();
oc_obt_shutdown();
return NULL;
}
#endif
......@@ -273,6 +276,7 @@ discover_owned_devices(void)
otb_mutex_lock(app_sync_lock);
oc_obt_discover_owned_devices(owned_device_cb, NULL);
otb_mutex_unlock(app_sync_lock);
signal_event_loop();
}
static void
......@@ -284,6 +288,124 @@ discover_unowned_devices(void)
signal_event_loop();
}
static void
otm_rdp_cb(oc_uuid_t *uuid, int status, void *data)
{
(void)data;
char di[37];
oc_uuid_to_str(uuid, di, 37);
if (status >= 0) {
PRINT("\nSuccessfully performed OTM on device %s\n", di);
add_device_to_list(uuid, owned_devices);
} else {
PRINT("\nERROR performing ownership transfer on device %s\n", di);
}
}
static void
otm_rdp(void)
{
if (oc_list_length(unowned_devices) == 0) {
PRINT("\nPlease Re-discover Unowned devices\n");
return;
}
device_handle_t *device = (device_handle_t *)oc_list_head(unowned_devices);
device_handle_t *devices[MAX_NUM_DEVICES];
int i = 0, c;
PRINT("\nUnowned Devices:\n");
while (device != NULL) {
char di[OC_UUID_LEN];
oc_uuid_to_str(&device->uuid, di, OC_UUID_LEN);
PRINT("[%d]: %s\n", i, di);
devices[i] = device;
i++;
device = device->next;
}
PRINT("\n\nSelect device: ");
SCANF("%d", &c);
if (c < 0 || c >= i) {
PRINT("ERROR: Invalid selection\n");
return;
}
unsigned char pin[24];
PRINT("\nEnter Random PIN: ");
SCANF("%10s", pin);
otb_mutex_lock(app_sync_lock);
int ret = oc_obt_perform_random_pin_otm(
&devices[c]->uuid, pin, strlen((const char *)pin), otm_rdp_cb, NULL);
if (ret >= 0) {
PRINT("\nSuccessfully issued request to perform Random PIN OTM\n");
} else {
PRINT("\nERROR issuing request to perform Random PIN OTM\n");
}
/* Having issued an OTM request, remove this item from the unowned device list
*/
remove_device_from_list(&devices[c]->uuid, unowned_devices);
otb_mutex_unlock(app_sync_lock);
}
static void
random_pin_cb(oc_uuid_t *uuid, int status, void *data)
{
(void)data;
char di[37];
oc_uuid_to_str(uuid, di, 37);
if (status >= 0) {
PRINT("\nSuccessfully requested device %s to generate a Random PIN\n", di);
} else {
PRINT("\nERROR requesting device %s to generate a Random PIN\n", di);
}
}
static void
request_random_pin(void)
{
if (oc_list_length(unowned_devices) == 0) {
PRINT("\nPlease Re-discover Unowned devices\n");
return;
}
device_handle_t *device = (device_handle_t *)oc_list_head(unowned_devices);
device_handle_t *devices[MAX_NUM_DEVICES];
int i = 0, c;
PRINT("\nUnowned Devices:\n");
while (device != NULL) {
char di[OC_UUID_LEN];
oc_uuid_to_str(&device->uuid, di, OC_UUID_LEN);
PRINT("[%d]: %s\n", i, di);
devices[i] = device;
i++;
device = device->next;
}
PRINT("\n\nSelect device: ");
SCANF("%d", &c);
if (c < 0 || c >= i) {
PRINT("ERROR: Invalid selection\n");
return;
}
otb_mutex_lock(app_sync_lock);
int ret = oc_obt_request_random_pin(&devices[c]->uuid, random_pin_cb, NULL);
if (ret >= 0) {
PRINT("\nSuccessfully issued request to generate a random PIN\n");
} else {
PRINT("\nERROR issuing request to generate random PIN\n");
}
otb_mutex_unlock(app_sync_lock);