Add tinycbor and iotivity constrained to the repo. These are not working yet. Need to add basic test cases for cbor Need to add the OS support for iotivity
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/ece29bb3 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/ece29bb3 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/ece29bb3 Branch: refs/heads/develop Commit: ece29bb340b4ec83b635e3a13cdca8c21d458d19 Parents: 0b82d07 Author: Paul Dietrich <paulfdietr...@yahoo.com> Authored: Wed Sep 14 13:18:31 2016 -0700 Committer: Paul Dietrich <paulfdietr...@yahoo.com> Committed: Wed Sep 14 13:32:32 2016 -0700 ---------------------------------------------------------------------- libs/iotivity/include/iotivity/oc_api.h | 165 +++ libs/iotivity/include/iotivity/oc_buffer.h | 32 + .../iotivity/include/iotivity/oc_client_state.h | 88 ++ libs/iotivity/include/iotivity/oc_constants.h | 43 + libs/iotivity/include/iotivity/oc_core_res.h | 52 + libs/iotivity/include/iotivity/oc_discovery.h | 22 + libs/iotivity/include/iotivity/oc_helpers.h | 78 ++ .../include/iotivity/oc_network_events.h | 29 + libs/iotivity/include/iotivity/oc_rep.h | 228 +++ libs/iotivity/include/iotivity/oc_ri.h | 176 +++ libs/iotivity/include/iotivity/oc_uuid.h | 31 + libs/iotivity/pkg.yml | 37 + libs/iotivity/src/api/oc_buffer.c | 138 ++ libs/iotivity/src/api/oc_client_api.c | 287 ++++ libs/iotivity/src/api/oc_core_res.c | 280 ++++ libs/iotivity/src/api/oc_discovery.c | 298 ++++ libs/iotivity/src/api/oc_events.h | 34 + libs/iotivity/src/api/oc_helpers.c | 164 +++ libs/iotivity/src/api/oc_main.c | 119 ++ libs/iotivity/src/api/oc_network_events.c | 57 + libs/iotivity/src/api/oc_rep.c | 301 ++++ libs/iotivity/src/api/oc_ri.c | 1016 ++++++++++++++ libs/iotivity/src/api/oc_server_api.c | 291 ++++ libs/iotivity/src/api/oc_uuid.c | 119 ++ libs/iotivity/src/messaging/coap/coap.c | 1186 ++++++++++++++++ libs/iotivity/src/messaging/coap/coap.h | 297 ++++ libs/iotivity/src/messaging/coap/conf.h | 71 + libs/iotivity/src/messaging/coap/constants.h | 158 +++ libs/iotivity/src/messaging/coap/engine.c | 333 +++++ libs/iotivity/src/messaging/coap/engine.h | 48 + libs/iotivity/src/messaging/coap/observe.c | 319 +++++ libs/iotivity/src/messaging/coap/observe.h | 80 ++ libs/iotivity/src/messaging/coap/oc_coap.h | 39 + libs/iotivity/src/messaging/coap/separate.c | 152 ++ libs/iotivity/src/messaging/coap/separate.h | 71 + libs/iotivity/src/messaging/coap/transactions.c | 202 +++ libs/iotivity/src/messaging/coap/transactions.h | 75 + libs/iotivity/src/port/mynewt/config.h | 50 + libs/iotivity/src/port/oc_assert.h | 42 + libs/iotivity/src/port/oc_clock.h | 102 ++ libs/iotivity/src/port/oc_connectivity.h | 83 ++ libs/iotivity/src/port/oc_log.h | 46 + .../iotivity/src/port/oc_network_events_mutex.h | 26 + libs/iotivity/src/port/oc_random.h | 56 + libs/iotivity/src/port/oc_signal_main_loop.h | 22 + libs/iotivity/src/port/oc_storage.h | 27 + libs/iotivity/src/security/oc_acl.c | 412 ++++++ libs/iotivity/src/security/oc_acl.h | 63 + libs/iotivity/src/security/oc_cred.c | 200 +++ libs/iotivity/src/security/oc_cred.h | 40 + libs/iotivity/src/security/oc_doxm.c | 128 ++ libs/iotivity/src/security/oc_doxm.h | 45 + libs/iotivity/src/security/oc_dtls.c | 372 +++++ libs/iotivity/src/security/oc_dtls.h | 50 + libs/iotivity/src/security/oc_pstat.c | 119 ++ libs/iotivity/src/security/oc_pstat.h | 39 + libs/iotivity/src/security/oc_store.c | 158 +++ libs/iotivity/src/security/oc_store.h | 26 + libs/iotivity/src/security/oc_svr.c | 51 + libs/iotivity/src/security/oc_svr.h | 22 + libs/iotivity/src/util/oc_etimer.c | 263 ++++ libs/iotivity/src/util/oc_etimer.h | 247 ++++ libs/iotivity/src/util/oc_list.c | 317 +++++ libs/iotivity/src/util/oc_list.h | 144 ++ libs/iotivity/src/util/oc_memb.c | 111 ++ libs/iotivity/src/util/oc_memb.h | 127 ++ libs/iotivity/src/util/oc_mmem.c | 154 +++ libs/iotivity/src/util/oc_mmem.h | 53 + libs/iotivity/src/util/oc_process.c | 346 +++++ libs/iotivity/src/util/oc_process.h | 527 +++++++ libs/iotivity/src/util/oc_timer.c | 131 ++ libs/iotivity/src/util/oc_timer.h | 88 ++ libs/iotivity/src/util/pt/lc-addrlabels.h | 86 ++ libs/iotivity/src/util/pt/lc-switch.h | 79 ++ libs/iotivity/src/util/pt/lc.h | 130 ++ libs/iotivity/src/util/pt/pt-sem.h | 233 ++++ libs/iotivity/src/util/pt/pt.h | 333 +++++ libs/tinycbor/include/tinycbor/assert_p.h | 29 + libs/tinycbor/include/tinycbor/cbor.h | 479 +++++++ .../tinycbor/include/tinycbor/cborconstants_p.h | 52 + libs/tinycbor/include/tinycbor/cborjson.h | 62 + .../include/tinycbor/compilersupport_p.h | 218 +++ .../include/tinycbor/extract_number_p.h | 78 ++ libs/tinycbor/include/tinycbor/math_support_p.h | 47 + libs/tinycbor/pkg.yml | 26 + libs/tinycbor/src/cborencoder.c | 629 +++++++++ .../src/cborencoder_close_container_checked.c | 82 ++ libs/tinycbor/src/cborerrorstrings.c | 165 +++ libs/tinycbor/src/cborparser.c | 1293 ++++++++++++++++++ libs/tinycbor/src/cborparser_dup_string.c | 113 ++ libs/tinycbor/src/cborpretty.c | 470 +++++++ libs/tinycbor/src/cbortojson.c | 686 ++++++++++ libs/tinycbor/src/open_memstream.c | 117 ++ 93 files changed, 17210 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ece29bb3/libs/iotivity/include/iotivity/oc_api.h ---------------------------------------------------------------------- diff --git a/libs/iotivity/include/iotivity/oc_api.h b/libs/iotivity/include/iotivity/oc_api.h new file mode 100644 index 0000000..e607668 --- /dev/null +++ b/libs/iotivity/include/iotivity/oc_api.h @@ -0,0 +1,165 @@ +/* +// Copyright (c) 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. +*/ + +#ifndef OC_API_H +#define OC_API_H + +#include "messaging/coap/oc_coap.h" +#include "oc_ri.h" +#include "port/oc_signal_main_loop.h" +#include "port/oc_storage.h" + +typedef struct +{ + void (*init)(void); + +#ifdef OC_SECURITY + void (*get_credentials)(void); +#endif /* OC_SECURITY */ + +#ifdef OC_SERVER + void (*register_resources)(void); +#endif /* OC_SERVER */ + +#ifdef OC_CLIENT + void (*requests_entry)(void); +#endif /* OC_CLIENT */ +} oc_handler_t; + +typedef void (*oc_init_platform_cb_t)(void *data); +typedef void (*oc_add_device_cb_t)(void *data); + +int oc_main_init(oc_handler_t *handler); + +oc_clock_time_t oc_main_poll(void); + +void oc_main_shutdown(void); + +void 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); + +#define oc_set_custom_device_property(prop, value) \ + oc_rep_set_text_string(root, prop, value) + +void oc_init_platform(const char *mfg_name, + oc_init_platform_cb_t init_platform_cb, void *data); + +#define oc_set_custom_platform_property(prop, value) \ + oc_rep_set_text_string(root, prop, value) + +/** Server side */ +oc_resource_t *oc_new_resource(const char *uri, uint8_t num_resource_types, + int device); +void oc_resource_bind_resource_interface(oc_resource_t *resource, + uint8_t interface); +void oc_resource_set_default_interface(oc_resource_t *resource, + oc_interface_mask_t interface); +void oc_resource_bind_resource_type(oc_resource_t *resource, const char *type); + +void oc_process_baseline_interface(oc_resource_t *resource); + +#ifdef OC_SECURITY +void oc_resource_make_secure(oc_resource_t *resource); +#endif /* OC_SECURITY */ + +void oc_resource_set_discoverable(oc_resource_t *resource); +void oc_resource_set_observable(oc_resource_t *resource); +void oc_resource_set_periodic_observable(oc_resource_t *resource, + uint16_t seconds); +void oc_resource_set_request_handler(oc_resource_t *resource, + oc_method_t method, + oc_request_handler_t handler); +bool oc_add_resource(oc_resource_t *resource); +void oc_delete_resource(oc_resource_t *resource); +void oc_deactivate_resource(oc_resource_t *resource); + +void oc_init_query_iterator(oc_request_t *request); +int oc_interate_query(oc_request_t *request, char **key, int *key_len, + char **value, int *value_len); +int oc_get_query_value(oc_request_t *request, const char *key, char **value); + +void oc_send_response(oc_request_t *request, oc_status_t response_code); +void oc_ignore_request(oc_request_t *request); + +void oc_indicate_separate_response(oc_request_t *request, + oc_separate_response_t *response); +void oc_set_separate_response_buffer(oc_separate_response_t *handle); +void oc_send_separate_response(oc_separate_response_t *handle, + oc_status_t response_code); + +int oc_notify_observers(oc_resource_t *resource); + +/** Client side */ +#include "oc_client_state.h" + +bool oc_do_ip_discovery(const char *rt, oc_discovery_cb_t handler); + +bool oc_do_get(const char *uri, oc_server_handle_t *server, const char *query, + oc_response_handler_t handler, oc_qos_t qos); + +bool oc_do_delete(const char *uri, oc_server_handle_t *server, + oc_response_handler_t handler, oc_qos_t qos); + +bool oc_init_put(const char *uri, oc_server_handle_t *server, const char *query, + oc_response_handler_t handler, oc_qos_t qos); + +bool oc_do_put(void); + +bool oc_init_post(const char *uri, oc_server_handle_t *server, + const char *query, oc_response_handler_t handler, + oc_qos_t qos); + +bool oc_do_post(void); + +bool oc_do_observe(const char *uri, oc_server_handle_t *server, + const char *query, oc_response_handler_t handler, + oc_qos_t qos); + +bool oc_stop_observe(const char *uri, oc_server_handle_t *server); + +/** Common operations */ + +void oc_set_delayed_callback(void *cb_data, oc_trigger_t callback, + uint16_t seconds); +void oc_remove_delayed_callback(void *cb_data, oc_trigger_t callback); + +/** API for setting handlers for interrupts */ + +#define oc_signal_interrupt_handler(name) \ + do { \ + oc_process_poll(&(name##_interrupt_x)); \ + oc_signal_main_loop(); \ + } while (0) + +#define oc_activate_interrupt_handler(name) \ + (oc_process_start(&(name##_interrupt_x), 0)) + +#define oc_define_interrupt_handler(name) \ + void name##_interrupt_x_handler(void); \ + OC_PROCESS(name##_interrupt_x, ""); \ + OC_PROCESS_THREAD(name##_interrupt_x, ev, data) \ + { \ + OC_PROCESS_POLLHANDLER(name##_interrupt_x_handler()); \ + OC_PROCESS_BEGIN(); \ + while (oc_process_is_running(&(name##_interrupt_x))) { \ + OC_PROCESS_YIELD(); \ + } \ + OC_PROCESS_END(); \ + } \ + void name##_interrupt_x_handler(void) + +#endif /* OC_API_H */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ece29bb3/libs/iotivity/include/iotivity/oc_buffer.h ---------------------------------------------------------------------- diff --git a/libs/iotivity/include/iotivity/oc_buffer.h b/libs/iotivity/include/iotivity/oc_buffer.h new file mode 100644 index 0000000..55373e6 --- /dev/null +++ b/libs/iotivity/include/iotivity/oc_buffer.h @@ -0,0 +1,32 @@ +/* +// Copyright (c) 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. +*/ + +#ifndef OC_BUFFER_H +#define OC_BUFFER_H + +#include "port/oc_connectivity.h" +#include "util/oc_process.h" +#include <stdbool.h> + +OC_PROCESS_NAME(message_buffer_handler); +oc_message_t *oc_allocate_message(void); +void oc_message_add_ref(oc_message_t *message); +void oc_message_unref(oc_message_t *message); + +void oc_recv_message(oc_message_t *message); +void oc_send_message(oc_message_t *message); + +#endif /* OC_BUFFER_H */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ece29bb3/libs/iotivity/include/iotivity/oc_client_state.h ---------------------------------------------------------------------- diff --git a/libs/iotivity/include/iotivity/oc_client_state.h b/libs/iotivity/include/iotivity/oc_client_state.h new file mode 100644 index 0000000..7049c32 --- /dev/null +++ b/libs/iotivity/include/iotivity/oc_client_state.h @@ -0,0 +1,88 @@ +/* +// Copyright (c) 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. +*/ + +#ifndef OC_CLIENT_STATE_H +#define OC_CLIENT_STATE_H + +#include "messaging/coap/constants.h" +#include "oc_ri.h" +#include <stdbool.h> + +typedef enum { HIGH_QOS = 0, LOW_QOS } oc_qos_t; + +typedef struct +{ + oc_rep_t *payload; + oc_status_t code; + int observe_option; +} oc_client_response_t; + +typedef struct +{ + oc_endpoint_t endpoint; +} oc_server_handle_t; + +typedef enum { + OC_STOP_DISCOVERY = 0, + OC_CONTINUE_DISCOVERY +} oc_discovery_flags_t; + +typedef oc_discovery_flags_t(oc_discovery_cb_t)(const char *, const char *, + oc_string_array_t, + oc_interface_mask_t, + oc_server_handle_t *); + +typedef void (*oc_response_handler_t)(oc_client_response_t *); + +typedef struct oc_client_cb_s +{ + struct oc_client_cb_s *next; + oc_string_t uri; + uint8_t token[COAP_TOKEN_LEN]; + uint8_t token_len; + uint16_t mid; + + oc_server_handle_t server; + + void *handler; + + bool discovery; + int32_t observe_seq; + oc_clock_time_t timestamp; + oc_qos_t qos; + oc_method_t method; +} oc_client_cb_t; + +bool oc_ri_invoke_client_cb(void *response, oc_endpoint_t *endpoint); + +oc_client_cb_t *oc_ri_alloc_client_cb(const char *uri, + oc_server_handle_t *server, + oc_method_t method, void *handler, + oc_qos_t qos); + +oc_client_cb_t *oc_ri_get_client_cb(const char *uri, oc_server_handle_t *server, + oc_method_t method); + +void oc_ri_remove_client_cb_by_mid(uint16_t mid); + +oc_discovery_flags_t oc_ri_process_discovery_payload(uint8_t *payload, int len, + oc_discovery_cb_t *handler, + oc_endpoint_t *endpoint); + +bool oc_ri_send_rst(oc_endpoint_t *endpoint, uint8_t *token, uint8_t token_len, + uint16_t mid); + +#endif /* OC_CLIENT_STATE_H */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ece29bb3/libs/iotivity/include/iotivity/oc_constants.h ---------------------------------------------------------------------- diff --git a/libs/iotivity/include/iotivity/oc_constants.h b/libs/iotivity/include/iotivity/oc_constants.h new file mode 100644 index 0000000..0413ddc --- /dev/null +++ b/libs/iotivity/include/iotivity/oc_constants.h @@ -0,0 +1,43 @@ +/* +// Copyright (c) 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. +*/ + +#ifndef OC_CONSTANTS_H +#define OC_CONSTANTS_H + +/* OCF standard resource interfaces */ +#define OC_NUM_STD_INTERFACES (7) +#define OC_RSRVD_IF_BASELINE "oic.if.baseline" +#define OC_BASELINE_IF_LEN (15) +#define OC_RSRVD_IF_LL "oic.if.ll" +#define OC_LL_IF_LEN (9) +#define OC_RSRVD_IF_B "oic.if.b" +#define OC_B_IF_LEN (8) +#define OC_RSRVD_IF_R "oic.if.r" +#define OC_R_IF_LEN (8) +#define OC_RSRVD_IF_RW "oic.if.rw" +#define OC_RW_IF_LEN (9) +#define OC_RSRVD_IF_A "oic.if.a" +#define OC_A_IF_LEN (8) +#define OC_RSRVD_IF_S "oic.if.s" +#define OC_S_IF_LEN (8) + +/* OCF Core resource URIs */ +#define OC_RSRVD_WELL_KNOWN_URI "/oic/res" +#define OC_MULTICAST_DISCOVERY_URI "/oic/res" +#define OC_RSRVD_DEVICE_URI "/oic/d" +#define OC_RSRVD_PLATFORM_URI "/oic/p" + +#endif /* OC_CONSTANTS_H */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ece29bb3/libs/iotivity/include/iotivity/oc_core_res.h ---------------------------------------------------------------------- diff --git a/libs/iotivity/include/iotivity/oc_core_res.h b/libs/iotivity/include/iotivity/oc_core_res.h new file mode 100644 index 0000000..69e5879 --- /dev/null +++ b/libs/iotivity/include/iotivity/oc_core_res.h @@ -0,0 +1,52 @@ +/* +// Copyright (c) 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. +*/ + +#ifndef OC_CORE_RES_H +#define OC_CORE_RES_H + +#include "oc_ri.h" + +typedef void (*oc_core_init_platform_cb_t)(void *data); +typedef void (*oc_core_add_device_cb_t)(void *data); + +oc_string_t *oc_core_init_platform(const char *mfg_name, + oc_core_init_platform_cb_t init_cb, + void *data); + +oc_string_t *oc_core_add_new_device(const char *uri, const char *rt, + const char *name, const char *spec_version, + const char *data_model_version, + oc_core_add_device_cb_t add_device_cb, + void *data); + +int oc_core_get_num_devices(void); + +oc_uuid_t *oc_core_get_device_id(int device); + +void oc_core_encode_interfaces_mask(CborEncoder *parent, + oc_interface_mask_t interface); + +oc_resource_t *oc_core_get_resource_by_index(int type); + +oc_resource_t *oc_core_get_resource_by_uri(const char *uri); + +void oc_core_populate_resource( + int type, const char *uri, const char *rt, oc_interface_mask_t interfaces, + oc_interface_mask_t default_interface, oc_resource_properties_t properties, + oc_request_handler_t get, oc_request_handler_t put, oc_request_handler_t post, + oc_request_handler_t delete, int device); + +#endif /* OC_CORE_RES_H */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ece29bb3/libs/iotivity/include/iotivity/oc_discovery.h ---------------------------------------------------------------------- diff --git a/libs/iotivity/include/iotivity/oc_discovery.h b/libs/iotivity/include/iotivity/oc_discovery.h new file mode 100644 index 0000000..6fd3f29 --- /dev/null +++ b/libs/iotivity/include/iotivity/oc_discovery.h @@ -0,0 +1,22 @@ +/* +// Copyright (c) 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. +*/ + +#ifndef OC_DISCOVERY_H +#define OC_DISCOVERY_H + +void oc_create_discovery_resource(void); + +#endif /* OC_DISCOVERY_H */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ece29bb3/libs/iotivity/include/iotivity/oc_helpers.h ---------------------------------------------------------------------- diff --git a/libs/iotivity/include/iotivity/oc_helpers.h b/libs/iotivity/include/iotivity/oc_helpers.h new file mode 100644 index 0000000..4a8cd1e --- /dev/null +++ b/libs/iotivity/include/iotivity/oc_helpers.h @@ -0,0 +1,78 @@ +/* +// Copyright (c) 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. +*/ + +#ifndef OC_HELPERS_H +#define OC_HELPERS_H + +#include "util/oc_list.h" +#include "util/oc_mmem.h" +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> + +typedef struct oc_mmem oc_handle_t, oc_string_t, oc_array_t, oc_string_array_t; + +#define oc_cast(block, type) ((type *)(OC_MMEM_PTR(&(block)))) +#define oc_string(ocstring) (oc_cast(ocstring, char)) + +void oc_new_string(oc_string_t *ocstring, const char str[]); +void oc_alloc_string(oc_string_t *ocstring, int size); +void oc_free_string(oc_string_t *ocstring); +void oc_concat_strings(oc_string_t *concat, const char *str1, const char *str2); +#define oc_string_len(ocstring) ((ocstring).size ? (ocstring).size - 1 : 0) + +void _oc_new_array(oc_array_t *ocarray, uint8_t size, pool type); +void _oc_free_array(oc_array_t *ocarray, pool type); +#define oc_new_int_array(ocarray, size) (_oc_new_array(ocarray, size, INT_POOL)) +#define oc_new_bool_array(ocarray, size) \ + (_oc_new_array(ocarray, size, BYTE_POOL)) +#define oc_new_double_array(ocarray, size) \ + (_oc_new_array(ocarray, size, DOUBLE_POOL)) +#define oc_free_int_array(ocarray) (_oc_free_array(ocarray, INT_POOL)) +#define oc_free_bool_array(ocarray) (_oc_free_array(ocarray, BYTE_POOL)) +#define oc_free_double_array(ocarray) (_oc_free_array(ocarray, DOUBLE_POOL)) +#define oc_int_array_size(ocintarray) ((ocintarray).size / sizeof(int64_t)) +#define oc_bool_array_size(ocboolarray) ((ocboolarray).size / sizeof(bool)) +#define oc_double_array_size(ocdoublearray) \ + ((ocdoublearray).size / sizeof(double)) +#define oc_int_array(ocintarray) (oc_cast(ocintarray, int64_t)) +#define oc_bool_array(ocboolarray) (oc_cast(ocboolarray, bool)) +#define oc_double_array(ocdoublearray) (oc_cast(ocdoublearray, double)) + +#define STRING_ARRAY_ITEM_MAX_LEN 24 +void _oc_alloc_string_array(oc_string_array_t *ocstringarray, uint8_t size); +bool _oc_copy_string_to_string_array(oc_string_array_t *ocstringarray, + const char str[], uint8_t index); +bool _oc_string_array_add_item(oc_string_array_t *ocstringarray, + const char str[]); +void oc_join_string_array(oc_string_array_t *ocstringarray, + oc_string_t *ocstring); +#define oc_new_string_array(ocstringarray, size) \ + (_oc_alloc_string_array(ocstringarray, size)) +#define oc_free_string_array(ocstringarray) (oc_free_string(ocstringarray)) +#define oc_string_array_add_item(ocstringarray, str) \ + (_oc_string_array_add_item(&(ocstringarray), str)) +#define oc_string_array_get_item(ocstringarray, index) \ + (oc_string(ocstringarray) + index * STRING_ARRAY_ITEM_MAX_LEN) +#define oc_string_array_set_item(ocstringarray, str, index) \ + (_oc_copy_string_to_string_array(&(ocstringarray), str, index)) +#define oc_string_array_get_item_size(ocstringarray, index) \ + (strlen((const char *)oc_string_array_get_item(ocstringarray, index))) +#define oc_string_array_get_allocated_size(ocstringarray) \ + ((ocstringarray).size / STRING_ARRAY_ITEM_MAX_LEN) + +#endif /* OC_HELPERS_H */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ece29bb3/libs/iotivity/include/iotivity/oc_network_events.h ---------------------------------------------------------------------- diff --git a/libs/iotivity/include/iotivity/oc_network_events.h b/libs/iotivity/include/iotivity/oc_network_events.h new file mode 100644 index 0000000..a678666 --- /dev/null +++ b/libs/iotivity/include/iotivity/oc_network_events.h @@ -0,0 +1,29 @@ +/* +// Copyright (c) 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. +*/ + +#ifndef OC_NETWORK_EVENTS_H +#define OC_NETWORK_EVENTS_H + +#include "port/oc_network_events_mutex.h" +#include "util/oc_process.h" + +OC_PROCESS_NAME(oc_network_events); + +typedef struct oc_message_s oc_message_t; + +void oc_network_event(oc_message_t *message); + +#endif /* OC_NETWORK_EVENTS_H */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ece29bb3/libs/iotivity/include/iotivity/oc_rep.h ---------------------------------------------------------------------- diff --git a/libs/iotivity/include/iotivity/oc_rep.h b/libs/iotivity/include/iotivity/oc_rep.h new file mode 100644 index 0000000..b7a88be --- /dev/null +++ b/libs/iotivity/include/iotivity/oc_rep.h @@ -0,0 +1,228 @@ +/* +// Copyright (c) 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. +*/ + +#ifndef OC_REP_H +#define OC_REP_H + +#include <tinycbor/cbor.h> +#include "oc_constants.h" +#include "oc_helpers.h" +#include <config.h> +#include <stdbool.h> +#include <stdint.h> + +extern CborEncoder g_encoder, root_map, links_array; +extern CborError g_err; + +void oc_rep_new(uint8_t *payload, int size); +void oc_rep_reset(void); +int oc_rep_finalize(void); + +#define oc_rep_object(name) &name##_map +#define oc_rep_array(name) &name##_array + +#define oc_rep_set_double(object, key, value) \ + do { \ + g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \ + g_err |= cbor_encode_double(&object##_map, value); \ + } while (0) + +#define oc_rep_set_int(object, key, value) \ + do { \ + g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \ + g_err |= cbor_encode_int(&object##_map, value); \ + } while (0) + +#define oc_rep_set_uint(object, key, value) \ + do { \ + g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \ + g_err |= cbor_encode_uint(&object##_map, value); \ + } while (0) + +#define oc_rep_set_boolean(object, key, value) \ + do { \ + g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \ + g_err |= cbor_encode_boolean(&object##_map, value); \ + } while (0) + +#define oc_rep_set_text_string(object, key, value) \ + do { \ + g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \ + g_err |= cbor_encode_text_string(&object##_map, value, strlen(value)); \ + } while (0) + +#define oc_rep_set_byte_string(object, key, value) \ + do { \ + g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \ + g_err |= cbor_encode_byte_string(&object##_map, value, strlen(value)); \ + } while (0) + +#define oc_rep_start_array(parent, key) \ + do { \ + CborEncoder key##_array; \ + g_err |= \ + cbor_encoder_create_array(&parent, &key##_array, CborIndefiniteLength) + +#define oc_rep_end_array(parent, key) \ + g_err |= cbor_encoder_close_container(&parent, &key##_array); \ + } \ + while (0) + +#define oc_rep_start_links_array() \ + g_err |= \ + cbor_encoder_create_array(&g_encoder, &links_array, CborIndefiniteLength) + +#define oc_rep_end_links_array() \ + g_err |= cbor_encoder_close_container(&g_encoder, &links_array) + +#define oc_rep_start_root_object() \ + g_err |= cbor_encoder_create_map(&g_encoder, &root_map, CborIndefiniteLength) + +#define oc_rep_end_root_object() \ + g_err |= cbor_encoder_close_container(&g_encoder, &root_map) + +#define oc_rep_add_byte_string(parent, value) \ + g_err |= cbor_encode_byte_string(&parent##_array, value, strlen(value)) + +#define oc_rep_add_text_string(parent, value) \ + g_err |= cbor_encode_text_string(&parent##_array, value, strlen(value)) + +#define oc_rep_set_key(parent, key) \ + g_err |= cbor_encode_text_string(&parent, key, strlen(key)) + +#define oc_rep_set_array(object, key) \ + g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \ + oc_rep_start_array(object##_map, key) + +#define oc_rep_close_array(object, key) oc_rep_end_array(object##_map, key) + +#define oc_rep_start_object(parent, key) \ + do { \ + CborEncoder key##_map; \ + g_err |= cbor_encoder_create_map(&parent, &key##_map, CborIndefiniteLength) + +#define oc_rep_end_object(parent, key) \ + g_err |= cbor_encoder_close_container(&parent, &key##_map); \ + } \ + while (0) + +#define oc_rep_object_array_start_item(key) \ + oc_rep_start_object(key##_array, key) + +#define oc_rep_object_array_end_item(key) oc_rep_end_object(key##_array, key) + +#define oc_rep_set_object(object, key) \ + g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \ + oc_rep_start_object(object##_map, key) + +#define oc_rep_close_object(object, key) oc_rep_end_object(object##_map, key) + +#define oc_rep_set_int_array(object, key, values, length) \ + do { \ + g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \ + CborEncoder key##_value_array; \ + g_err |= \ + cbor_encoder_create_array(&object##_map, &key##_value_array, length); \ + int i; \ + for (i = 0; i < length; i++) { \ + g_err |= cbor_encode_int(&key##_value_array, values[i]); \ + } \ + g_err |= cbor_encoder_close_container(&object##_map, &key##_value_array); \ + } while (0) + +#define oc_rep_set_bool_array(object, key, values, length) \ + do { \ + g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \ + CborEncoder key##_value_array; \ + g_err |= \ + cbor_encoder_create_array(&object##_map, &key##_value_array, length); \ + int i; \ + for (i = 0; i < length; i++) { \ + g_err |= cbor_encode_boolean(&key##_value_array, values[i]); \ + } \ + g_err |= cbor_encoder_close_container(&object##_map, &key##_value_array); \ + } while (0) + +#define oc_rep_set_double_array(object, key, values, length) \ + do { \ + g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \ + CborEncoder key##_value_array; \ + g_err |= \ + cbor_encoder_create_array(&object##_map, &key##_value_array, length); \ + int i; \ + for (i = 0; i < length; i++) { \ + g_err |= cbor_encode_floating_point(&key##_value_array, CborDoubleType, \ + &values[i]); \ + } \ + g_err |= cbor_encoder_close_container(&object##_map, &key##_value_array); \ + } while (0) + +#define oc_rep_set_string_array(object, key, values) \ + do { \ + g_err |= cbor_encode_text_string(&object##_map, #key, strlen(#key)); \ + CborEncoder key##_value_array; \ + g_err |= \ + cbor_encoder_create_array(&object##_map, &key##_value_array, \ + oc_string_array_get_allocated_size(values)); \ + int i; \ + for (i = 0; i < oc_string_array_get_allocated_size(values); i++) { \ + g_err |= cbor_encode_text_string( \ + &key##_value_array, oc_string_array_get_item(values, i), \ + oc_string_array_get_item_size(values, i)); \ + } \ + g_err |= cbor_encoder_close_container(&object##_map, &key##_value_array); \ + } while (0) + +typedef enum { + NIL = 0, + INT = 0x01, + DOUBLE = 0x02, + BOOL = 0x03, + BYTE_STRING = 0x04, + STRING = 0x05, + OBJECT = 0x06, + ARRAY = 0x08, + INT_ARRAY = 0x09, + DOUBLE_ARRAY = 0x0A, + BOOL_ARRAY = 0x0B, + BYTE_STRING_ARRAY = 0x0C, + STRING_ARRAY = 0x0D, + OBJECT_ARRAY = 0x0E +} oc_rep_value_type_t; + +typedef struct oc_rep_s +{ + oc_rep_value_type_t type; + struct oc_rep_s *next; + oc_string_t name; + union + { + int64_t value_int; + bool value_boolean; + double value_double; + oc_string_t value_string; + oc_array_t value_array; + struct oc_rep_s *value_object; + struct oc_rep_s *value_object_array; + }; +} oc_rep_t; + +uint16_t oc_parse_rep(const uint8_t *payload, uint16_t payload_size, + oc_rep_t **value_list); + +void oc_free_rep(oc_rep_t *rep); + +#endif /* OC_REP_H */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ece29bb3/libs/iotivity/include/iotivity/oc_ri.h ---------------------------------------------------------------------- diff --git a/libs/iotivity/include/iotivity/oc_ri.h b/libs/iotivity/include/iotivity/oc_ri.h new file mode 100644 index 0000000..5e96656 --- /dev/null +++ b/libs/iotivity/include/iotivity/oc_ri.h @@ -0,0 +1,176 @@ +/* +// Copyright (c) 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. +*/ + +#ifndef OC_RI_H +#define OC_RI_H + +#include "config.h" +#include "oc_rep.h" +#include "oc_uuid.h" +#include "port/oc_connectivity.h" +#include "util/oc_etimer.h" + +typedef enum { OC_GET = 1, OC_POST, OC_PUT, OC_DELETE } oc_method_t; + +typedef enum { + OC_DISCOVERABLE = (1 << 0), + OC_OBSERVABLE = (1 << 1), + OC_ACTIVE = (1 << 2), + OC_SECURE = (1 << 4), + OC_PERIODIC = (1 << 6), +} oc_resource_properties_t; + +typedef enum { + OC_STATUS_OK = 0, + OC_STATUS_CREATED, + OC_STATUS_CHANGED, + OC_STATUS_DELETED, + OC_STATUS_NOT_MODIFIED, + OC_STATUS_BAD_REQUEST, + OC_STATUS_UNAUTHORIZED, + OC_STATUS_BAD_OPTION, + OC_STATUS_FORBIDDEN, + OC_STATUS_NOT_FOUND, + OC_STATUS_METHOD_NOT_ALLOWED, + OC_STATUS_NOT_ACCEPTABLE, + OC_STATUS_REQUEST_ENTITY_TOO_LARGE, + OC_STATUS_UNSUPPORTED_MEDIA_TYPE, + OC_STATUS_INTERNAL_SERVER_ERROR, + OC_STATUS_NOT_IMPLEMENTED, + OC_STATUS_BAD_GATEWAY, + OC_STATUS_SERVICE_UNAVAILABLE, + OC_STATUS_GATEWAY_TIMEOUT, + OC_STATUS_PROXYING_NOT_SUPPORTED, + __NUM_OC_STATUS_CODES__, + OC_IGNORE +} oc_status_t; + +typedef struct oc_separate_response_s oc_separate_response_t; + +typedef struct oc_response_buffer_s oc_response_buffer_t; + +typedef struct +{ + oc_separate_response_t *separate_response; + oc_response_buffer_t *response_buffer; +} oc_response_t; + +typedef enum { + OC_IF_BASELINE = 1 << 1, + OC_IF_LL = 1 << 2, + OC_IF_B = 1 << 3, + OC_IF_R = 1 << 4, + OC_IF_RW = 1 << 5, + OC_IF_A = 1 << 6, + OC_IF_S = 1 << 7, +} oc_interface_mask_t; + +typedef enum { + OCF_RES = 0, + OCF_P, +#ifdef OC_SECURITY + OCF_SEC_DOXM, + OCF_SEC_PSTAT, + OCF_SEC_ACL, + OCF_SEC_CRED, +#endif + __NUM_OC_CORE_RESOURCES__ +} oc_core_resource_t; + +#define NUM_OC_CORE_RESOURCES (__NUM_OC_CORE_RESOURCES__ + MAX_NUM_DEVICES) + +typedef struct oc_resource_s oc_resource_t; + +typedef struct +{ + oc_endpoint_t *origin; + oc_resource_t *resource; + const char *query; + int query_len; + oc_rep_t *request_payload; + oc_response_t *response; +} oc_request_t; + +typedef void (*oc_request_handler_t)(oc_request_t *, oc_interface_mask_t); + +typedef struct oc_resource_s +{ + struct oc_resource_s *next; + int device; + oc_string_t uri; + oc_string_array_t types; + oc_interface_mask_t interfaces; + oc_interface_mask_t default_interface; + oc_resource_properties_t properties; + oc_request_handler_t get_handler; + oc_request_handler_t put_handler; + oc_request_handler_t post_handler; + oc_request_handler_t delete_handler; + uint16_t observe_period_seconds; + uint8_t num_observers; +} oc_resource_t; + +typedef enum { DONE = 0, CONTINUE } oc_event_callback_retval_t; + +typedef oc_event_callback_retval_t (*oc_trigger_t)(void *); + +typedef struct oc_event_callback_s +{ + struct oc_event_callback_s *next; + struct oc_etimer timer; + oc_trigger_t callback; + void *data; +} oc_event_callback_t; + +void oc_ri_init(void); + +void oc_ri_shutdown(void); + +void oc_ri_add_timed_event_callback_ticks(void *cb_data, + oc_trigger_t event_callback, + oc_clock_time_t ticks); + +#define oc_ri_add_timed_event_callback_seconds(cb_data, event_callback, \ + seconds) \ + do { \ + oc_ri_add_timed_event_callback_ticks( \ + cb_data, event_callback, (oc_clock_time_t)(seconds * OC_CLOCK_SECOND)); \ + } while (0) + +void oc_ri_remove_timed_event_callback(void *cb_data, + oc_trigger_t event_callback); + +int oc_status_code(oc_status_t key); + +oc_resource_t *oc_ri_get_app_resource_by_uri(const char *uri); + +oc_resource_t *oc_ri_get_app_resources(void); + +#ifdef OC_SERVER +oc_resource_t *oc_ri_alloc_resource(void); +bool oc_ri_add_resource(oc_resource_t *resource); +void oc_ri_delete_resource(oc_resource_t *resource); +#endif + +int oc_ri_get_query_nth_key_value(const char *query, int query_len, char **key, + int *key_len, char **value, int *value_len, + int n); +int oc_ri_get_query_value(const char *query, int query_len, const char *key, + char **value); + +oc_interface_mask_t oc_ri_get_interface_mask(char *iface, int if_len); + +#endif /* OC_RI_H */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ece29bb3/libs/iotivity/include/iotivity/oc_uuid.h ---------------------------------------------------------------------- diff --git a/libs/iotivity/include/iotivity/oc_uuid.h b/libs/iotivity/include/iotivity/oc_uuid.h new file mode 100644 index 0000000..b1e5b26 --- /dev/null +++ b/libs/iotivity/include/iotivity/oc_uuid.h @@ -0,0 +1,31 @@ +/* +// Copyright (c) 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. +*/ + +#ifndef OC_UUID_H +#define OC_UUID_H + +#include <stdint.h> + +typedef struct +{ + uint8_t id[16]; +} oc_uuid_t; + +void oc_str_to_uuid(const char *str, oc_uuid_t *uuid); +void oc_uuid_to_str(const oc_uuid_t *uuid, char *buffer, int buflen); +void oc_gen_uuid(oc_uuid_t *uuid); + +#endif /* OC_UUID_H */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ece29bb3/libs/iotivity/pkg.yml ---------------------------------------------------------------------- diff --git a/libs/iotivity/pkg.yml b/libs/iotivity/pkg.yml new file mode 100644 index 0000000..f8f9f03 --- /dev/null +++ b/libs/iotivity/pkg.yml @@ -0,0 +1,37 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# + +pkg.name: libs/iotivity +pkg.description: OCF contstrained iotivity stack +pkg.author: "https://github.com/iotivity/iotivity-constrained" +pkg.homepage: "https://www.iotivity.org/" +pkg.keywords: + +pkg.deps: + - "@apache-mynewt-core/libs/tinycbor" + - "@apache-mynewt-core/libs/os" + +pkg.cflags: -DDEBUG=1 + -DSECURE=0 + -I./port/mynewt + -I../port/mynewt + -I../../port/mynewt + -I../include/iotivity + -I../../include/iotivity + -I../../../include/iotivity \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ece29bb3/libs/iotivity/src/api/oc_buffer.c ---------------------------------------------------------------------- diff --git a/libs/iotivity/src/api/oc_buffer.c b/libs/iotivity/src/api/oc_buffer.c new file mode 100644 index 0000000..e623221 --- /dev/null +++ b/libs/iotivity/src/api/oc_buffer.c @@ -0,0 +1,138 @@ +/* +// Copyright (c) 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. +*/ + +#include "messaging/coap/engine.h" +#include "port/oc_signal_main_loop.h" +#include "util/oc_memb.h" +#include <stdint.h> +#include <stdio.h> + +#ifdef OC_SECURITY +#include "security/oc_dtls.h" +#endif + +#include "config.h" +#include "oc_buffer.h" +#include "oc_events.h" + +OC_PROCESS(message_buffer_handler, "OC Message Buffer Handler"); +OC_MEMB(oc_buffers_s, oc_message_t, (MAX_NUM_CONCURRENT_REQUESTS * 2)); + +oc_message_t * +oc_allocate_message(void) +{ + oc_message_t *message = (oc_message_t *)oc_memb_alloc(&oc_buffers_s); + if (message) { + message->length = 0; + message->next = 0; + message->ref_count = 1; + LOG("buffer: Allocated TX/RX buffer; num free: %d\n", + oc_memb_numfree(&oc_buffers_s)); + } else + LOG("buffer: No free TX/RX buffers!\n"); + return message; +} + +void +oc_message_add_ref(oc_message_t *message) +{ + if (message) + message->ref_count++; +} + +void +oc_message_unref(oc_message_t *message) +{ + if (message) { + message->ref_count--; + if (message->ref_count == 0) { + oc_memb_free(&oc_buffers_s, message); + LOG("buffer: freed TX/RX buffer; num free: %d\n", + oc_memb_numfree(&oc_buffers_s)); + } + } +} + +void +oc_recv_message(oc_message_t *message) +{ + oc_process_post(&message_buffer_handler, oc_events[INBOUND_NETWORK_EVENT], + message); +} + +void +oc_send_message(oc_message_t *message) +{ + oc_process_post(&message_buffer_handler, oc_events[OUTBOUND_NETWORK_EVENT], + message); + + oc_signal_main_loop(); +} + +OC_PROCESS_THREAD(message_buffer_handler, ev, data) +{ + OC_PROCESS_BEGIN(); + LOG("Started buffer handler process\n"); + while (1) { + OC_PROCESS_YIELD(); + + if (ev == oc_events[INBOUND_NETWORK_EVENT]) { +#ifdef OC_SECURITY + uint8_t b = (uint8_t)((oc_message_t *)data)->data[0]; + if (b > 19 && b < 64) { + LOG("Inbound network event: encrypted request\n"); + oc_process_post(&oc_dtls_handler, oc_events[UDP_TO_DTLS_EVENT], data); + } else { + LOG("Inbound network event: decrypted request\n"); + oc_process_post(&coap_engine, oc_events[INBOUND_RI_EVENT], data); + } +#else + LOG("Inbound network event: decrypted request\n"); + oc_process_post(&coap_engine, oc_events[INBOUND_RI_EVENT], data); +#endif + } else if (ev == oc_events[OUTBOUND_NETWORK_EVENT]) { + oc_message_t *message = (oc_message_t *)data; + +#ifdef OC_CLIENT + if (message->endpoint.flags & MULTICAST) { + LOG("Outbound network event: multicast request\n"); + oc_send_multicast_message(message); + oc_message_unref(message); + } else +#endif +#ifdef OC_SECURITY + if (message->endpoint.flags & SECURED) { + LOG("Outbound network event: forwarding to DTLS\n"); + + if (!oc_sec_dtls_connected(&message->endpoint)) { + LOG("Posting INIT_DTLS_CONN_EVENT\n"); + oc_process_post(&oc_dtls_handler, oc_events[INIT_DTLS_CONN_EVENT], + data); + } else { + LOG("Posting RI_TO_DTLS_EVENT\n"); + oc_process_post(&oc_dtls_handler, oc_events[RI_TO_DTLS_EVENT], data); + } + } else +#endif + { + LOG("Outbound network event: unicast message\n"); + oc_send_buffer(message); + oc_message_unref(message); + } + } + } + OC_PROCESS_END(); +} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ece29bb3/libs/iotivity/src/api/oc_client_api.c ---------------------------------------------------------------------- diff --git a/libs/iotivity/src/api/oc_client_api.c b/libs/iotivity/src/api/oc_client_api.c new file mode 100644 index 0000000..bd73da3 --- /dev/null +++ b/libs/iotivity/src/api/oc_client_api.c @@ -0,0 +1,287 @@ +/* +// Copyright (c) 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. +*/ + +#include "messaging/coap/coap.h" +#include "messaging/coap/transactions.h" +#include "oc_api.h" + +#ifdef OC_CLIENT +#define OC_CLIENT_CB_TIMEOUT_SECS COAP_RESPONSE_TIMEOUT + +static oc_message_t *message; +static coap_transaction_t *transaction; +coap_packet_t request[1]; + +static bool +dispatch_coap_request(void) +{ + int response_length = oc_rep_finalize(); + if (!transaction) { + if (message) { + if (response_length) { + coap_set_payload(request, message->data + COAP_MAX_HEADER_SIZE, + response_length); + coap_set_header_content_format(request, APPLICATION_CBOR); + } + message->length = coap_serialize_message(request, message->data); + coap_send_message(message); + message = 0; + return true; + } + } else { + if (response_length) { + coap_set_payload(request, + transaction->message->data + COAP_MAX_HEADER_SIZE, + response_length); + coap_set_header_content_format(request, APPLICATION_CBOR); + } + transaction->message->length = + coap_serialize_message(request, transaction->message->data); + coap_send_transaction(transaction); + transaction = 0; + return true; + } + return false; +} + +static bool +prepare_coap_request(oc_client_cb_t *cb, oc_string_t *query) +{ + coap_message_type_t type = COAP_TYPE_NON; + + if (cb->qos == HIGH_QOS) { + type = COAP_TYPE_CON; + transaction = coap_new_transaction(cb->mid, &cb->server.endpoint); + if (!transaction) + return false; + oc_rep_new(transaction->message->data + COAP_MAX_HEADER_SIZE, + COAP_MAX_BLOCK_SIZE); + } else { + message = oc_allocate_message(); + if (!message) + return false; + memcpy(&message->endpoint, &cb->server.endpoint, sizeof(oc_endpoint_t)); + oc_rep_new(message->data + COAP_MAX_HEADER_SIZE, COAP_MAX_BLOCK_SIZE); + } + + coap_init_message(request, type, cb->method, cb->mid); + + coap_set_header_accept(request, APPLICATION_CBOR); + + coap_set_token(request, cb->token, cb->token_len); + + coap_set_header_uri_path(request, oc_string(cb->uri)); + + if (cb->observe_seq != -1) + coap_set_header_observe(request, cb->observe_seq); + + if (query && oc_string_len(*query)) + coap_set_header_uri_query(request, oc_string(*query)); + + if (cb->observe_seq == -1 && cb->qos == LOW_QOS) { + extern oc_event_callback_retval_t oc_ri_remove_client_cb(void *data); + + oc_set_delayed_callback(cb, &oc_ri_remove_client_cb, + OC_CLIENT_CB_TIMEOUT_SECS); + } + + return true; +} + +bool +oc_do_delete(const char *uri, oc_server_handle_t *server, + oc_response_handler_t handler, oc_qos_t qos) +{ + oc_client_cb_t *cb = + oc_ri_alloc_client_cb(uri, server, OC_DELETE, handler, qos); + if (!cb) + return false; + + bool status = false; + + status = prepare_coap_request(cb, NULL); + + if (status) + status = dispatch_coap_request(); + + return status; +} + +bool +oc_do_get(const char *uri, oc_server_handle_t *server, const char *query, + oc_response_handler_t handler, oc_qos_t qos) +{ + oc_client_cb_t *cb = oc_ri_alloc_client_cb(uri, server, OC_GET, handler, qos); + if (!cb) + return false; + + bool status = false; + + if (query && strlen(query)) { + oc_string_t q; + oc_concat_strings(&q, "?", query); + status = prepare_coap_request(cb, &q); + oc_free_string(&q); + } else { + status = prepare_coap_request(cb, NULL); + } + + if (status) + status = dispatch_coap_request(); + + return status; +} + +bool +oc_init_put(const char *uri, oc_server_handle_t *server, const char *query, + oc_response_handler_t handler, oc_qos_t qos) +{ + oc_client_cb_t *cb = oc_ri_alloc_client_cb(uri, server, OC_PUT, handler, qos); + if (!cb) + return false; + + bool status = false; + + if (query && strlen(query)) { + oc_string_t q; + oc_concat_strings(&q, "?", query); + status = prepare_coap_request(cb, &q); + oc_free_string(&q); + } else { + status = prepare_coap_request(cb, NULL); + } + + return status; +} + +bool +oc_init_post(const char *uri, oc_server_handle_t *server, const char *query, + oc_response_handler_t handler, oc_qos_t qos) +{ + oc_client_cb_t *cb = + oc_ri_alloc_client_cb(uri, server, OC_POST, handler, qos); + if (!cb) + return false; + + bool status = false; + + if (query && strlen(query)) { + oc_string_t q; + oc_concat_strings(&q, "?", query); + status = prepare_coap_request(cb, &q); + oc_free_string(&q); + } else { + status = prepare_coap_request(cb, NULL); + } + + return status; +} + +bool +oc_do_put(void) +{ + return dispatch_coap_request(); +} + +bool +oc_do_post(void) +{ + return dispatch_coap_request(); +} + +bool +oc_do_observe(const char *uri, oc_server_handle_t *server, const char *query, + oc_response_handler_t handler, oc_qos_t qos) +{ + oc_client_cb_t *cb = oc_ri_alloc_client_cb(uri, server, OC_GET, handler, qos); + if (!cb) + return false; + + cb->observe_seq = 0; + + bool status = false; + + if (query && strlen(query)) { + oc_string_t q; + oc_concat_strings(&q, "?", query); + status = prepare_coap_request(cb, &q); + oc_free_string(&q); + } else { + status = prepare_coap_request(cb, NULL); + } + + if (status) + status = dispatch_coap_request(); + + return status; +} + +bool +oc_stop_observe(const char *uri, oc_server_handle_t *server) +{ + oc_client_cb_t *cb = oc_ri_get_client_cb(uri, server, OC_GET); + + if (!cb) + return false; + + cb->observe_seq = 1; + + bool status = false; + + status = prepare_coap_request(cb, NULL); + + if (status) + status = dispatch_coap_request(); + + return status; +} + +bool +oc_do_ip_discovery(const char *rt, oc_discovery_cb_t handler) +{ + oc_make_ip_endpoint(mcast, IP | MULTICAST, 5683, 0xff, 0x02, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0xfd); + mcast.ipv6_addr.scope = 0; + + oc_server_handle_t handle; + memcpy(&handle.endpoint, &mcast, sizeof(oc_endpoint_t)); + + oc_client_cb_t *cb = + oc_ri_alloc_client_cb("/oic/res", &handle, OC_GET, handler, LOW_QOS); + + if (!cb) + return false; + + cb->discovery = true; + + bool status = false; + + oc_string_t query; + + if (rt && strlen(rt) > 0) { + oc_concat_strings(&query, "if=oic.if.ll&rt=", rt); + } else { + oc_new_string(&query, "if=oic.if.ll"); + } + status = prepare_coap_request(cb, &query); + oc_free_string(&query); + + if (status) + status = dispatch_coap_request(); + + return status; +} +#endif /* OC_CLIENT */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ece29bb3/libs/iotivity/src/api/oc_core_res.c ---------------------------------------------------------------------- diff --git a/libs/iotivity/src/api/oc_core_res.c b/libs/iotivity/src/api/oc_core_res.c new file mode 100644 index 0000000..ced0f88 --- /dev/null +++ b/libs/iotivity/src/api/oc_core_res.c @@ -0,0 +1,280 @@ +/* + // Copyright (c) 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. + */ + +#include "oc_core_res.h" +#include "messaging/coap/oc_coap.h" +#include "oc_rep.h" +#include "oc_ri.h" + +#ifdef OC_SECURITY +#include "security/oc_pstat.h" +#endif /* OC_SECURITY */ + +static oc_resource_t core_resources[NUM_OC_CORE_RESOURCES]; +struct oc_device_info_t +{ + oc_uuid_t uuid; + oc_string_t payload; +} oc_device_info[MAX_NUM_DEVICES]; +static int device_count; +static oc_string_t oc_platform_payload; + +void +oc_core_encode_interfaces_mask(CborEncoder *parent, + oc_interface_mask_t interface) +{ + oc_rep_set_key((*parent), "if"); + oc_rep_start_array((*parent), if); + if (interface & OC_IF_LL) { + oc_rep_add_text_string(if, OC_RSRVD_IF_LL); + } + if (interface & OC_IF_B) { + oc_rep_add_text_string(if, OC_RSRVD_IF_B); + } + if (interface & OC_IF_R) { + oc_rep_add_text_string(if, OC_RSRVD_IF_R); + } + if (interface & OC_IF_RW) { + oc_rep_add_text_string(if, OC_RSRVD_IF_RW); + } + if (interface & OC_IF_A) { + oc_rep_add_text_string(if, OC_RSRVD_IF_A); + } + if (interface & OC_IF_S) { + oc_rep_add_text_string(if, OC_RSRVD_IF_S); + } + oc_rep_add_text_string(if, OC_RSRVD_IF_BASELINE); + oc_rep_end_array((*parent), if); +} + +static void +oc_core_device_handler(oc_request_t *request, oc_interface_mask_t interface) +{ + uint8_t *buffer = request->response->response_buffer->buffer; + uint16_t buffer_size = request->response->response_buffer->buffer_size; + int payload_size = oc_device_info[request->resource->device].payload.size; + + if (buffer_size < payload_size) { + request->response->response_buffer->response_length = 0; + request->response->response_buffer->code = + oc_status_code(OC_STATUS_INTERNAL_SERVER_ERROR); + return; + } + + switch (interface) { + case OC_IF_R: + case OC_IF_BASELINE: + memcpy(buffer, + oc_cast(oc_device_info[request->resource->device].payload, uint8_t), + payload_size); + request->response->response_buffer->response_length = payload_size; + request->response->response_buffer->code = oc_status_code(OC_STATUS_OK); + break; + default: + break; + } +} + +int +oc_core_get_num_devices(void) +{ + return device_count; +} + +static int +finalize_payload(oc_string_t *temp_buffer, oc_string_t *payload) +{ + oc_rep_end_root_object(); + int size = oc_rep_finalize(); + if (size != -1) { + oc_alloc_string(payload, size); + memcpy(oc_cast(*payload, uint8_t), oc_cast(*temp_buffer, uint8_t), size); + oc_free_string(temp_buffer); + return 1; + } + + oc_free_string(temp_buffer); + return -1; +} + +oc_string_t * +oc_core_add_new_device(const char *uri, const char *rt, const char *name, + const char *spec_version, const char *data_model_version, + oc_core_add_device_cb_t add_device_cb, void *data) +{ + if (device_count == MAX_NUM_DEVICES) + return false; + + oc_string_t temp_buffer; +/* Once provisioned, UUID is retrieved from the credential store. + If not yet provisioned, a default is generated in the security + layer. +*/ +#ifdef OC_SECURITY /*fix if add new devices after provisioning, need to reset \ + or it will generate non-standard uuid */ + /* where are secondary device ids persisted? */ + if (!oc_sec_provisioned() && device_count > 0) + oc_gen_uuid(&oc_device_info[device_count].uuid); +#else + oc_gen_uuid(&oc_device_info[device_count].uuid); +#endif + + int ocf_d = NUM_OC_CORE_RESOURCES - 1 - device_count; + + /* Construct device resource */ + oc_core_populate_resource(ocf_d, uri, rt, OC_IF_R | OC_IF_BASELINE, + OC_IF_BASELINE, OC_ACTIVE | OC_DISCOVERABLE, + oc_core_device_handler, 0, 0, 0, device_count); + + /* Encoding device resource payload */ + oc_alloc_string(&temp_buffer, MAX_DEVICE_PAYLOAD_SIZE); + oc_rep_new(oc_cast(temp_buffer, uint8_t), MAX_DEVICE_PAYLOAD_SIZE); + + oc_rep_start_root_object(); + + oc_rep_set_string_array(root, rt, core_resources[ocf_d].types); + oc_core_encode_interfaces_mask(oc_rep_object(root), + core_resources[ocf_d].interfaces); + oc_rep_set_uint(root, p, core_resources[ocf_d].properties); + + char uuid[37]; + oc_uuid_to_str(&oc_device_info[device_count].uuid, uuid, 37); + oc_rep_set_text_string(root, di, uuid); + oc_rep_set_text_string(root, n, name); + oc_rep_set_text_string(root, icv, spec_version); + oc_rep_set_text_string(root, dmv, data_model_version); + + if (add_device_cb) + add_device_cb(data); + if (!finalize_payload(&temp_buffer, &oc_device_info[device_count].payload)) + return NULL; + + return &oc_device_info[device_count++].payload; +} + +void +oc_core_platform_handler(oc_request_t *request, oc_interface_mask_t interface) +{ + uint8_t *buffer = request->response->response_buffer->buffer; + uint16_t buffer_size = request->response->response_buffer->buffer_size; + int payload_size = oc_platform_payload.size; + + if (buffer_size < payload_size) { + request->response->response_buffer->response_length = 0; + request->response->response_buffer->code = + oc_status_code(OC_STATUS_INTERNAL_SERVER_ERROR); + return; + } + + switch (interface) { + case OC_IF_R: + case OC_IF_BASELINE: + memcpy(buffer, oc_cast(oc_platform_payload, uint8_t), payload_size); + request->response->response_buffer->response_length = payload_size; + request->response->response_buffer->code = oc_status_code(OC_STATUS_OK); + break; + default: + break; + } +} + +oc_string_t * +oc_core_init_platform(const char *mfg_name, oc_core_init_platform_cb_t init_cb, + void *data) +{ + if (oc_platform_payload.size > 0) + return NULL; + + oc_string_t temp_buffer; + /* Populating resource obuject */ + oc_core_populate_resource(OCF_P, OC_RSRVD_PLATFORM_URI, "oic.wk.p", + OC_IF_R | OC_IF_BASELINE, OC_IF_BASELINE, + OC_ACTIVE | OC_DISCOVERABLE, + oc_core_platform_handler, 0, 0, 0, 0); + + /* Encoding platform resource payload */ + oc_alloc_string(&temp_buffer, MAX_PLATFORM_PAYLOAD_SIZE); + oc_rep_new(oc_cast(temp_buffer, uint8_t), MAX_PLATFORM_PAYLOAD_SIZE); + oc_rep_start_root_object(); + oc_rep_set_string_array(root, rt, core_resources[OCF_P].types); + + oc_core_encode_interfaces_mask(oc_rep_object(root), + core_resources[OCF_P].interfaces); + oc_rep_set_uint(root, p, core_resources[OCF_P].properties & ~OC_PERIODIC); + + oc_uuid_t uuid; /*fix uniqueness of platform id?? */ + oc_gen_uuid(&uuid); + char uuid_str[37]; + + oc_uuid_to_str(&uuid, uuid_str, 37); + oc_rep_set_text_string(root, pi, uuid_str); + oc_rep_set_text_string(root, mnmn, mfg_name); + + if (init_cb) + init_cb(data); + + if (!finalize_payload(&temp_buffer, &oc_platform_payload)) + return NULL; + + return &oc_platform_payload; +} + +void +oc_core_populate_resource(int type, const char *uri, const char *rt, + oc_interface_mask_t interfaces, + oc_interface_mask_t default_interface, + oc_resource_properties_t properties, + oc_request_handler_t get, oc_request_handler_t put, + oc_request_handler_t post, + oc_request_handler_t delete, int device) +{ + oc_resource_t *r = &core_resources[type]; + r->device = device; + oc_new_string(&r->uri, uri); + r->properties = properties; + oc_new_string_array(&r->types, 1); + oc_string_array_add_item(r->types, rt); + r->interfaces = interfaces; + r->default_interface = default_interface; + r->get_handler = get; + r->put_handler = put; + r->post_handler = post; + r->delete_handler = delete; +} + +oc_uuid_t * +oc_core_get_device_id(int device) +{ + return &oc_device_info[device].uuid; +} + +oc_resource_t * +oc_core_get_resource_by_index(int type) +{ + return &core_resources[type]; +} + +oc_resource_t * +oc_core_get_resource_by_uri(const char *uri) +{ + int i; + for (i = 0; i < NUM_OC_CORE_RESOURCES; i++) { + if (oc_string_len(core_resources[i].uri) == strlen(uri) && + strncmp(uri, oc_string(core_resources[i].uri), strlen(uri)) == 0) + return &core_resources[i]; + } + return NULL; +} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ece29bb3/libs/iotivity/src/api/oc_discovery.c ---------------------------------------------------------------------- diff --git a/libs/iotivity/src/api/oc_discovery.c b/libs/iotivity/src/api/oc_discovery.c new file mode 100644 index 0000000..34d34b8 --- /dev/null +++ b/libs/iotivity/src/api/oc_discovery.c @@ -0,0 +1,298 @@ +/* +// Copyright (c) 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. +*/ + +#ifdef OC_CLIENT +#include "oc_client_state.h" +#endif /* OC_CLIENT */ + +#include "messaging/coap/oc_coap.h" +#include "oc_api.h" +#include "oc_core_res.h" + +static bool +filter_resource(oc_resource_t *resource, const char *rt, int rt_len, + CborEncoder *links) +{ + int i; + bool match = true; + if (rt_len > 0) { + match = false; + for (i = 0; i < oc_string_array_get_allocated_size(resource->types); i++) { + int size = oc_string_array_get_item_size(resource->types, i); + const char *t = + (const char *)oc_string_array_get_item(resource->types, i); + if (rt_len == size && strncmp(rt, t, rt_len) == 0) { + match = true; + break; + } + } + } + + if (!match) { + return false; + } + + oc_rep_start_object(*links, res); + + // uri + oc_rep_set_text_string(res, href, oc_string(resource->uri)); + + // rt + oc_rep_set_array(res, rt); + for (i = 0; i < oc_string_array_get_allocated_size(resource->types); i++) { + int size = oc_string_array_get_item_size(resource->types, i); + const char *t = (const char *)oc_string_array_get_item(resource->types, i); + if (size > 0) + oc_rep_add_text_string(rt, t); + } + oc_rep_close_array(res, rt); + + // if + oc_core_encode_interfaces_mask(oc_rep_object(res), resource->interfaces); + + // p + oc_rep_set_object(res, p); + oc_rep_set_uint(p, bm, resource->properties & ~OC_PERIODIC); +#ifdef OC_SECURITY + if (resource->properties & OC_SECURE) { + oc_rep_set_boolean(p, sec, true); + oc_rep_set_uint(p, port, oc_connectivity_get_dtls_port()); + } +#endif /* OC_SECURITY */ + + oc_rep_close_object(res, p); + + oc_rep_end_object(*links, res); + return true; +} + +static int +process_device_object(CborEncoder *device, const char *uuid, const char *rt, + int rt_len) +{ + int dev, matches = 0; + oc_rep_start_object(*device, links); + oc_rep_set_text_string(links, di, uuid); + oc_rep_set_array(links, links); + + if (filter_resource(oc_core_get_resource_by_index(OCF_P), rt, rt_len, + oc_rep_array(links))) + matches++; + + for (dev = 0; dev < oc_core_get_num_devices(); dev++) { + if (filter_resource( + oc_core_get_resource_by_index(NUM_OC_CORE_RESOURCES - 1 - dev), rt, + rt_len, oc_rep_array(links))) + matches++; + } + +#ifdef OC_SERVER + oc_resource_t *resource = oc_ri_get_app_resources(); + for (; resource; resource = resource->next) { + + if (!(resource->properties & OC_DISCOVERABLE)) + continue; + + if (filter_resource(resource, rt, rt_len, oc_rep_array(links))) + matches++; + } +#endif + +#ifdef OC_SECURITY + if (filter_resource(oc_core_get_resource_by_index(OCF_SEC_DOXM), rt, rt_len, + oc_rep_array(links))) + matches++; +#endif + + oc_rep_close_array(links, links); + oc_rep_end_object(*device, links); + + return matches; +} + +static void +oc_core_discovery_handler(oc_request_t *request, oc_interface_mask_t interface) +{ + char *rt = NULL; + int rt_len = 0, matches = 0; + if (request->query_len) { + rt_len = + oc_ri_get_query_value(request->query, request->query_len, "rt", &rt); + } + + char uuid[37]; + oc_uuid_to_str(oc_core_get_device_id(0), uuid, 37); + + switch (interface) { + case OC_IF_LL: { + oc_rep_start_links_array(); + matches = process_device_object(oc_rep_array(links), uuid, rt, rt_len); + oc_rep_end_links_array(); + } break; + case OC_IF_BASELINE: { + oc_rep_start_root_object(); + oc_process_baseline_interface(request->resource); + oc_rep_set_array(root, links); + matches = process_device_object(oc_rep_array(links), uuid, rt, rt_len); + oc_rep_close_array(root, links); + oc_rep_end_root_object(); + } break; + default: + break; + } + + int response_length = oc_rep_finalize(); + + if (matches && response_length) { + request->response->response_buffer->response_length = response_length; + request->response->response_buffer->code = oc_status_code(OC_STATUS_OK); + } else { + /* There were rt/if selections and there were no matches, so ignore */ + request->response->response_buffer->code = OC_IGNORE; + } +} + +void +oc_create_discovery_resource(void) +{ + oc_core_populate_resource(OCF_RES, "/oic/res", "oic.wk.res", + OC_IF_LL | OC_IF_BASELINE, OC_IF_LL, OC_ACTIVE, + oc_core_discovery_handler, 0, 0, 0, 0); +} + +#ifdef OC_CLIENT +oc_discovery_flags_t +oc_ri_process_discovery_payload(uint8_t *payload, int len, + oc_discovery_cb_t *handler, + oc_endpoint_t *endpoint) +{ + oc_discovery_flags_t ret = OC_CONTINUE_DISCOVERY; + oc_string_t uri; + uri.ptr = 0; + oc_string_t di; + di.ptr = 0; + bool secure = false; + uint16_t dtls_port = 0, default_port = endpoint->ipv6_addr.port; + oc_string_array_t types = {}; + oc_interface_mask_t interfaces = 0; + oc_server_handle_t handle; + memcpy(&handle.endpoint, endpoint, sizeof(oc_endpoint_t)); + + oc_rep_t *array = 0, *rep; + int s = oc_parse_rep(payload, len, &rep); + if (s == 0) + array = rep; + while (array != NULL) { + oc_rep_t *device_map = array->value_object; + while (device_map != NULL) { + switch (device_map->type) { + case STRING: + if (oc_string_len(device_map->name) == 2 && + strncmp(oc_string(device_map->name), "di", 2) == 0) + di = device_map->value_string; + break; + default: + break; + } + device_map = device_map->next; + } + device_map = array->value_object; + while (device_map != NULL) { + switch (device_map->type) { + case OBJECT_ARRAY: { + oc_rep_t *links = device_map->value_object_array; + while (links != NULL) { + switch (links->type) { + case OBJECT: { + oc_rep_t *resource_info = links->value_object; + while (resource_info != NULL) { + switch (resource_info->type) { + case STRING: + uri = resource_info->value_string; + break; + case STRING_ARRAY: + if (oc_string_len(resource_info->name) == 2 && + strncmp(oc_string(resource_info->name), "rt", 2) == 0) + types = resource_info->value_array; + else { + interfaces = 0; + int i; + for (i = 0; i < oc_string_array_get_allocated_size( + resource_info->value_array); + i++) { + interfaces |= oc_ri_get_interface_mask( + oc_string_array_get_item(resource_info->value_array, i), + oc_string_array_get_item_size(resource_info->value_array, + i)); + } + } + break; + case OBJECT: { + oc_rep_t *policy_info = resource_info->value_object; + while (policy_info != NULL) { + if (policy_info->type == INT && + oc_string_len(policy_info->name) == 4 && + strncmp(oc_string(policy_info->name), "port", 4) == 0) { + dtls_port = policy_info->value_int; + } + if (policy_info->type == BOOL && + oc_string_len(policy_info->name) == 3 && + strncmp(oc_string(policy_info->name), "sec", 3) == 0 && + policy_info->value_boolean == true) { + secure = true; + } + policy_info = policy_info->next; + } + } break; + default: + break; + } + resource_info = resource_info->next; + } + if (secure) { + handle.endpoint.ipv6_addr.port = dtls_port; + handle.endpoint.flags |= SECURED; + } else { + handle.endpoint.ipv6_addr.port = default_port; + handle.endpoint.flags &= ~SECURED; + } + + if (handler(oc_string(di), oc_string(uri), types, interfaces, + &handle) == OC_STOP_DISCOVERY) { + ret = OC_STOP_DISCOVERY; + goto done; + } + dtls_port = 0; + secure = false; + } break; + default: + break; + } + links = links->next; + } + } break; + default: + break; + } + device_map = device_map->next; + } + array = array->next; + } +done: + oc_free_rep(rep); + return ret; +} +#endif /* OC_CLIENT */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ece29bb3/libs/iotivity/src/api/oc_events.h ---------------------------------------------------------------------- diff --git a/libs/iotivity/src/api/oc_events.h b/libs/iotivity/src/api/oc_events.h new file mode 100644 index 0000000..d79c22b --- /dev/null +++ b/libs/iotivity/src/api/oc_events.h @@ -0,0 +1,34 @@ +/* + // Copyright (c) 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. + */ + +#ifndef OC_EVENTS_H +#define OC_EVENTS_H + +#include "util/oc_process.h" + +typedef enum { + INBOUND_NETWORK_EVENT, + UDP_TO_DTLS_EVENT, + INIT_DTLS_CONN_EVENT, + RI_TO_DTLS_EVENT, + INBOUND_RI_EVENT, + OUTBOUND_NETWORK_EVENT, + __NUM_OC_EVENT_TYPES__ +} oc_events_t; + +oc_process_event_t oc_events[__NUM_OC_EVENT_TYPES__]; + +#endif /* OC_EVENTS_H */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ece29bb3/libs/iotivity/src/api/oc_helpers.c ---------------------------------------------------------------------- diff --git a/libs/iotivity/src/api/oc_helpers.c b/libs/iotivity/src/api/oc_helpers.c new file mode 100644 index 0000000..4af8eeb --- /dev/null +++ b/libs/iotivity/src/api/oc_helpers.c @@ -0,0 +1,164 @@ +/* +// Copyright (c) 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. +*/ + +#include "oc_helpers.h" +#include "port/oc_assert.h" +#include "port/oc_log.h" +#include <stdbool.h> + +static bool mmem_initialized = false; + +static void +oc_malloc(oc_handle_t *block, uint16_t num_bytes, pool pool_type) +{ + if (!mmem_initialized) { + oc_mmem_init(); + mmem_initialized = true; + } + oc_assert(oc_mmem_alloc(block, num_bytes, pool_type) > 0); +} + +static void +oc_free(oc_handle_t *block, pool pool_type) +{ + oc_mmem_free(block, pool_type); + block->next = 0; + block->ptr = 0; + block->size = 0; +} + +void +oc_new_string(oc_string_t *ocstring, const char str[]) +{ + oc_malloc(ocstring, strlen(str) + 1, BYTE_POOL); + memcpy(oc_string(*ocstring), (const uint8_t *)str, strlen(str)); + memcpy(oc_string(*ocstring) + strlen(str), (const uint8_t *)"", 1); +} + +void +oc_alloc_string(oc_string_t *ocstring, int size) +{ + oc_malloc(ocstring, size, BYTE_POOL); +} + +void +oc_free_string(oc_string_t *ocstring) +{ + oc_free(ocstring, BYTE_POOL); +} + +void +oc_concat_strings(oc_string_t *concat, const char *str1, const char *str2) +{ + size_t len1 = strlen(str1), len2 = strlen(str2); + oc_alloc_string(concat, len1 + len2 + 1); + memcpy(oc_string(*concat), str1, len1); + memcpy(oc_string(*concat) + len1, str2, len2); + memcpy(oc_string(*concat) + len1 + len2, (const char *)"", 1); +} + +void +_oc_new_array(oc_array_t *ocarray, uint8_t size, pool type) +{ + switch (type) { + case INT_POOL: + oc_malloc(ocarray, size * sizeof(int64_t), INT_POOL); + break; + case BYTE_POOL: + oc_malloc(ocarray, size * sizeof(bool), BYTE_POOL); + break; + case DOUBLE_POOL: + oc_malloc(ocarray, size * sizeof(double), DOUBLE_POOL); + break; + default: + break; + } +} + +void +_oc_free_array(oc_array_t *ocarray, pool type) +{ + oc_free(ocarray, type); +} + +void +_oc_alloc_string_array(oc_string_array_t *ocstringarray, uint8_t size) +{ + oc_alloc_string(ocstringarray, size * STRING_ARRAY_ITEM_MAX_LEN); + int i, pos; + for (i = 0; i < size; i++) { + pos = i * STRING_ARRAY_ITEM_MAX_LEN; + memcpy((char *)oc_string(*ocstringarray) + pos, (const char *)"", 1); + } +} + +bool +_oc_copy_string_to_string_array(oc_string_array_t *ocstringarray, + const char str[], uint8_t index) +{ + if (strlen(str) >= STRING_ARRAY_ITEM_MAX_LEN) { + return false; + } + uint8_t pos = index * STRING_ARRAY_ITEM_MAX_LEN; + memcpy(oc_string(*ocstringarray) + pos, (const uint8_t *)str, strlen(str)); + memcpy(oc_string(*ocstringarray) + pos + strlen(str), (const uint8_t *)"", 1); + return true; +} + +bool +_oc_string_array_add_item(oc_string_array_t *ocstringarray, const char str[]) +{ + bool success = false; + int i; + for (i = 0; i < oc_string_array_get_allocated_size(*ocstringarray); i++) { + if (strlen((const char *)oc_string_array_get_item(*ocstringarray, i)) == + 0) { + success = oc_string_array_set_item(*ocstringarray, str, i); + break; + } + } + return success; +} + +void +oc_join_string_array(oc_string_array_t *ocstringarray, oc_string_t *ocstring) +{ + size_t len = 0; + uint8_t i; + for (i = 0; i < oc_string_array_get_allocated_size(*ocstringarray); i++) { + const char *item = + (const char *)oc_string_array_get_item(*ocstringarray, i); + if (strlen(item)) { + len += strlen(item); + len++; + } + } + oc_alloc_string(ocstring, len); + len = 0; + for (i = 0; i < oc_string_array_get_allocated_size(*ocstringarray); i++) { + const char *item = + (const char *)oc_string_array_get_item(*ocstringarray, i); + if (strlen(item)) { + if (len > 0) { + oc_string(*ocstring)[len] = ' '; + len++; + } + strncpy((char *)oc_string(*ocstring) + len, item, strlen(item)); + len += strlen(item); + } + } + strcpy((char *)oc_string(*ocstring) + len, ""); +} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ece29bb3/libs/iotivity/src/api/oc_main.c ---------------------------------------------------------------------- diff --git a/libs/iotivity/src/api/oc_main.c b/libs/iotivity/src/api/oc_main.c new file mode 100644 index 0000000..9086b4e --- /dev/null +++ b/libs/iotivity/src/api/oc_main.c @@ -0,0 +1,119 @@ +/* +// Copyright (c) 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. +*/ + +#include <stdint.h> +#include <stdio.h> + +#include "port/oc_assert.h" +#include "port/oc_clock.h" +#include "port/oc_connectivity.h" + +#include "util/oc_etimer.h" +#include "util/oc_process.h" + +#include "oc_api.h" + +#ifdef OC_SECURITY +#include "security/oc_dtls.h" +#include "security/oc_store.h" +#include "security/oc_svr.h" +#endif /* OC_SECURITY */ + +static bool initialized = false; + +int +oc_main_init(oc_handler_t *handler) +{ + int ret; + extern int oc_stack_errno; + + if (initialized == true) + return 0; + + oc_ri_init(); + +#ifdef OC_SECURITY + handler->get_credentials(); + + oc_sec_load_pstat(); + oc_sec_load_doxm(); + oc_sec_load_cred(); + + oc_sec_dtls_init_context(); +#endif + + oc_network_event_handler_mutex_init(); + ret = oc_connectivity_init(); + if (ret < 0) + goto err; + + handler->init(); + +#ifdef OC_SERVER + handler->register_resources(); +#endif + +#ifdef OC_SECURITY + oc_sec_create_svr(); + oc_sec_load_acl(); +#endif + + if (oc_stack_errno != 0) { + ret = -oc_stack_errno; + goto err; + } + + PRINT("oc_main: Stack successfully initialized\n"); + +#ifdef OC_CLIENT + handler->requests_entry(); +#endif + + initialized = true; + return 0; + +err: + oc_abort("oc_main: Error in stack initialization\n"); + return ret; +} + +oc_clock_time_t +oc_main_poll(void) +{ + oc_clock_time_t ticks_until_next_event = oc_etimer_request_poll(); + while (oc_process_run()) { + ticks_until_next_event = oc_etimer_request_poll(); + } + return ticks_until_next_event; +} + +void +oc_main_shutdown(void) +{ + if (initialized == false) { + PRINT("tiny_ocf is not initialized\n"); + return; + } + + oc_connectivity_shutdown(); + oc_ri_shutdown(); + +#ifdef OC_SECURITY /* fix ensure this gets executed on constraied platforms */ + oc_sec_dump_state(); +#endif + + initialized = false; +}