http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f88168c2/libs/iotivity/src/util/pt/pt-sem.h ---------------------------------------------------------------------- diff --git a/libs/iotivity/src/util/pt/pt-sem.h b/libs/iotivity/src/util/pt/pt-sem.h deleted file mode 100644 index ad87c0d..0000000 --- a/libs/iotivity/src/util/pt/pt-sem.h +++ /dev/null @@ -1,241 +0,0 @@ -/* - * Copyright (c) 2004, Swedish Institute of Computer Science. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the Institute nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * This file is part of the Contiki operating system. - * - * Author: Adam Dunkels <a...@sics.se> - * - */ - -/** - * \addtogroup pt - * @{ - */ - -/** - * \defgroup ptsem Protothread semaphores - * @{ - * - * This module implements counting semaphores on top of - * protothreads. Semaphores are a synchronization primitive that - * provide two operations: "wait" and "signal". The "wait" operation - * checks the semaphore counter and blocks the thread if the counter - * is zero. The "signal" operation increases the semaphore counter but - * does not block. If another thread has blocked waiting for the - * semaphore that is signaled, the blocked thread will become - * runnable again. - * - * Semaphores can be used to implement other, more structured, - * synchronization primitives such as monitors and message - * queues/bounded buffers (see below). - * - * The following example shows how the producer-consumer problem, also - * known as the bounded buffer problem, can be solved using - * protothreads and semaphores. Notes on the program follow after the - * example. - * - \code -#include "pt-sem.h" - -#define NUM_ITEMS 32 -#ifdef __cplusplus -extern "C" { -#endif - -#define BUFSIZE 8 - -static struct pt_sem mutex, full, empty; - -PT_THREAD(producer(struct pt *pt)) -{ - static int produced; - - PT_BEGIN(pt); - - for(produced = 0; produced < NUM_ITEMS; ++produced) { - - PT_SEM_WAIT(pt, &full); - - PT_SEM_WAIT(pt, &mutex); - add_to_buffer(produce_item()); - PT_SEM_SIGNAL(pt, &mutex); - - PT_SEM_SIGNAL(pt, &empty); - } - - PT_END(pt); -} - -PT_THREAD(consumer(struct pt *pt)) -{ - static int consumed; - - PT_BEGIN(pt); - - for(consumed = 0; consumed < NUM_ITEMS; ++consumed) { - - PT_SEM_WAIT(pt, &empty); - - PT_SEM_WAIT(pt, &mutex); - consume_item(get_from_buffer()); - PT_SEM_SIGNAL(pt, &mutex); - - PT_SEM_SIGNAL(pt, &full); - } - - PT_END(pt); -} - -PT_THREAD(driver_thread(struct pt *pt)) -{ - static struct pt pt_producer, pt_consumer; - - PT_BEGIN(pt); - - PT_SEM_INIT(&empty, 0); - PT_SEM_INIT(&full, BUFSIZE); - PT_SEM_INIT(&mutex, 1); - - PT_INIT(&pt_producer); - PT_INIT(&pt_consumer); - - PT_WAIT_THREAD(pt, producer(&pt_producer) & - consumer(&pt_consumer)); - - PT_END(pt); -} - \endcode - * - * The program uses three protothreads: one protothread that - * implements the consumer, one thread that implements the producer, - * and one protothread that drives the two other protothreads. The - * program uses three semaphores: "full", "empty" and "mutex". The - * "mutex" semaphore is used to provide mutual exclusion for the - * buffer, the "empty" semaphore is used to block the consumer is the - * buffer is empty, and the "full" semaphore is used to block the - * producer is the buffer is full. - * - * The "driver_thread" holds two protothread state variables, - * "pt_producer" and "pt_consumer". It is important to note that both - * these variables are declared as <i>static</i>. If the static - * keyword is not used, both variables are stored on the stack. Since - * protothreads do not store the stack, these variables may be - * overwritten during a protothread wait operation. Similarly, both - * the "consumer" and "producer" protothreads declare their local - * variables as static, to avoid them being stored on the stack. - * - * - */ - -/** - * \file - * Counting semaphores implemented on protothreads - * \author - * Adam Dunkels <a...@sics.se> - * - */ - -#ifndef PT_SEM_H_ -#define PT_SEM_H_ - -#include "pt.h" - -struct pt_sem -{ - unsigned int head, tail; -}; - -#define PT_SEM_COUNT(s) ((s)->head - (s)->tail) - -/** - * Initialize a semaphore - * - * This macro initializes a semaphore with a value for the - * counter. Internally, the semaphores use an "unsigned int" to - * represent the counter, and therefore the "count" argument should be - * within range of an unsigned int. - * - * \param s (struct pt_sem *) A pointer to the pt_sem struct - * representing the semaphore - * - * \param c (unsigned int) The initial count of the semaphore. - * \hideinitializer - */ -#define PT_SEM_INIT(s, c) \ - do { \ - (s)->tail = 0; \ - (s)->head = (c); \ - } while (0) - -/** - * Wait for a semaphore - * - * This macro carries out the "wait" operation on the semaphore. The - * wait operation causes the protothread to block while the counter is - * zero. When the counter reaches a value larger than zero, the - * protothread will continue. - * - * \param pt (struct pt *) A pointer to the protothread (struct pt) in - * which the operation is executed. - * - * \param s (struct pt_sem *) A pointer to the pt_sem struct - * representing the semaphore - * - * \hideinitializer - */ -#define PT_SEM_WAIT(pt, s) \ - do { \ - PT_WAIT_UNTIL(pt, PT_SEM_COUNT(s) > 0); \ - ++(s)->tail; \ - } while (0) - -/** - * Signal a semaphore - * - * This macro carries out the "signal" operation on the semaphore. The - * signal operation increments the counter inside the semaphore, which - * eventually will cause waiting protothreads to continue executing. - * - * \param pt (struct pt *) A pointer to the protothread (struct pt) in - * which the operation is executed. - * - * \param s (struct pt_sem *) A pointer to the pt_sem struct - * representing the semaphore - * - * \hideinitializer - */ -#define PT_SEM_SIGNAL(pt, s) (++(s)->head) - -#ifdef __cplusplus -} -#endif - -#endif /* PT_SEM_H_ */ - -/** @} */ -/** @} */
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f88168c2/libs/iotivity/src/util/pt/pt.h ---------------------------------------------------------------------- diff --git a/libs/iotivity/src/util/pt/pt.h b/libs/iotivity/src/util/pt/pt.h deleted file mode 100644 index 43d1ab7..0000000 --- a/libs/iotivity/src/util/pt/pt.h +++ /dev/null @@ -1,341 +0,0 @@ -/* - * Copyright (c) 2004-2005, Swedish Institute of Computer Science. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the Institute nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * This file is part of the Contiki operating system. - * - * Author: Adam Dunkels <a...@sics.se> - * - */ - -/** - * \addtogroup pt - * @{ - */ - -/** - * \file - * Protothreads implementation. - * \author - * Adam Dunkels <a...@sics.se> - * - */ - -#ifndef PT_H_ -#define PT_H_ - -#include "lc.h" - -#ifdef __cplusplus -extern "C" { -#endif - -struct pt -{ - lc_t lc; -}; - -#define PT_WAITING 0 -#define PT_YIELDED 1 -#define PT_EXITED 2 -#define PT_ENDED 3 - -/** - * \name Initialization - * @{ - */ - -/** - * Initialize a protothread. - * - * Initializes a protothread. Initialization must be done prior to - * starting to execute the protothread. - * - * \param pt A pointer to the protothread control structure. - * - * \sa PT_SPAWN() - * - * \hideinitializer - */ -#define PT_INIT(pt) LC_INIT((pt)->lc) - -/** @} */ - -/** - * \name Declaration and definition - * @{ - */ - -/** - * Declaration of a protothread. - * - * This macro is used to declare a protothread. All protothreads must - * be declared with this macro. - * - * \param name_args The name and arguments of the C function - * implementing the protothread. - * - * \hideinitializer - */ -#define PT_THREAD(name_args) char name_args - -/** - * Declare the start of a protothread inside the C function - * implementing the protothread. - * - * This macro is used to declare the starting point of a - * protothread. It should be placed at the start of the function in - * which the protothread runs. All C statements above the PT_BEGIN() - * invokation will be executed each time the protothread is scheduled. - * - * \param pt A pointer to the protothread control structure. - * - * \hideinitializer - */ -#define PT_BEGIN(pt) \ - { \ - char PT_YIELD_FLAG = 1; \ - if (PT_YIELD_FLAG) { \ - ; \ - } \ - LC_RESUME((pt)->lc) - -/** - * Declare the end of a protothread. - * - * This macro is used for declaring that a protothread ends. It must - * always be used together with a matching PT_BEGIN() macro. - * - * \param pt A pointer to the protothread control structure. - * - * \hideinitializer - */ -#define PT_END(pt) \ - LC_END((pt)->lc); \ - PT_YIELD_FLAG = 0; \ - PT_INIT(pt); \ - return PT_ENDED; \ - } - -/** @} */ - -/** - * \name Blocked wait - * @{ - */ - -/** - * Block and wait until condition is true. - * - * This macro blocks the protothread until the specified condition is - * true. - * - * \param pt A pointer to the protothread control structure. - * \param condition The condition. - * - * \hideinitializer - */ -#define PT_WAIT_UNTIL(pt, condition) \ - do { \ - LC_SET((pt)->lc); \ - if (!(condition)) { \ - return PT_WAITING; \ - } \ - } while (0) - -/** - * Block and wait while condition is true. - * - * This function blocks and waits while condition is true. See - * PT_WAIT_UNTIL(). - * - * \param pt A pointer to the protothread control structure. - * \param cond The condition. - * - * \hideinitializer - */ -#define PT_WAIT_WHILE(pt, cond) PT_WAIT_UNTIL((pt), !(cond)) - -/** @} */ - -/** - * \name Hierarchical protothreads - * @{ - */ - -/** - * Block and wait until a child protothread completes. - * - * This macro schedules a child protothread. The current protothread - * will block until the child protothread completes. - * - * \note The child protothread must be manually initialized with the - * PT_INIT() function before this function is used. - * - * \param pt A pointer to the protothread control structure. - * \param thread The child protothread with arguments - * - * \sa PT_SPAWN() - * - * \hideinitializer - */ -#define PT_WAIT_THREAD(pt, thread) PT_WAIT_WHILE((pt), PT_SCHEDULE(thread)) - -/** - * Spawn a child protothread and wait until it exits. - * - * This macro spawns a child protothread and waits until it exits. The - * macro can only be used within a protothread. - * - * \param pt A pointer to the protothread control structure. - * \param child A pointer to the child protothread's control structure. - * \param thread The child protothread with arguments - * - * \hideinitializer - */ -#define PT_SPAWN(pt, child, thread) \ - do { \ - PT_INIT((child)); \ - PT_WAIT_THREAD((pt), (thread)); \ - } while (0) - -/** @} */ - -/** - * \name Exiting and restarting - * @{ - */ - -/** - * Restart the protothread. - * - * This macro will block and cause the running protothread to restart - * its execution at the place of the PT_BEGIN() call. - * - * \param pt A pointer to the protothread control structure. - * - * \hideinitializer - */ -#define PT_RESTART(pt) \ - do { \ - PT_INIT(pt); \ - return PT_WAITING; \ - } while (0) - -/** - * Exit the protothread. - * - * This macro causes the protothread to exit. If the protothread was - * spawned by another protothread, the parent protothread will become - * unblocked and can continue to run. - * - * \param pt A pointer to the protothread control structure. - * - * \hideinitializer - */ -#define PT_EXIT(pt) \ - do { \ - PT_INIT(pt); \ - return PT_EXITED; \ - } while (0) - -/** @} */ - -/** - * \name Calling a protothread - * @{ - */ - -/** - * Schedule a protothread. - * - * This function schedules a protothread. The return value of the - * function is non-zero if the protothread is running or zero if the - * protothread has exited. - * - * \param f The call to the C function implementing the protothread to - * be scheduled - * - * \hideinitializer - */ -#define PT_SCHEDULE(f) ((f) < PT_EXITED) - -/** @} */ - -/** - * \name Yielding from a protothread - * @{ - */ - -/** - * Yield from the current protothread. - * - * This function will yield the protothread, thereby allowing other - * processing to take place in the system. - * - * \param pt A pointer to the protothread control structure. - * - * \hideinitializer - */ -#define PT_YIELD(pt) \ - do { \ - PT_YIELD_FLAG = 0; \ - LC_SET((pt)->lc); \ - if (PT_YIELD_FLAG == 0) { \ - return PT_YIELDED; \ - } \ - } while (0) - -/** - * \brief Yield from the protothread until a condition occurs. - * \param pt A pointer to the protothread control structure. - * \param cond The condition. - * - * This function will yield the protothread, until the - * specified condition evaluates to true. - * - * - * \hideinitializer - */ -#define PT_YIELD_UNTIL(pt, cond) \ - do { \ - PT_YIELD_FLAG = 0; \ - LC_SET((pt)->lc); \ - if ((PT_YIELD_FLAG == 0) || !(cond)) { \ - return PT_YIELDED; \ - } \ - } while (0) - -/** @} */ - -#ifdef __cplusplus -} -#endif - -#endif /* PT_H_ */ - -/** @} */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f88168c2/libs/iotivity/syscfg.yml ---------------------------------------------------------------------- diff --git a/libs/iotivity/syscfg.yml b/libs/iotivity/syscfg.yml deleted file mode 100644 index 418aaa2..0000000 --- a/libs/iotivity/syscfg.yml +++ /dev/null @@ -1,27 +0,0 @@ -# Package: libs/iotivity - -syscfg.defs: - OC_TRANSPORT_SERIAL: - description: 'Enables OIC transport over nlip serial' - value: '0' - OC_TRANSPORT_GATT: - description: 'Enables OIC transport over BLE GATT' - value: '0' - OC_TRANSPORT_IP: - description: 'Enables OIC transport over IP UDP' - value: '0' - OC_CLIENT: - description: 'Enables OIC client support' - value: '0' - OC_SERVER: - description: 'Enables OIC server support' - value: '0' - OC_DEBUG: - description: 'Enables OIC debug logs' - value: '0' - OC_TASK_PRIORITY: - description: 'The task priority for an internal tasks that runs the OCF stack' - value: '4' - OC_TASK_STACK_SIZE: - description: 'The stack size of the OC task in words' - value: '300' http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f88168c2/libs/newtmgr_oic/pkg.yml ---------------------------------------------------------------------- diff --git a/libs/newtmgr_oic/pkg.yml b/libs/newtmgr_oic/pkg.yml index 10465f1..d2e4357 100644 --- a/libs/newtmgr_oic/pkg.yml +++ b/libs/newtmgr_oic/pkg.yml @@ -25,7 +25,7 @@ pkg.keywords: pkg.deps: - kernel/os - - libs/iotivity + - net/oic - encoding/json - libs/newtmgr/nmgr_os - libs/util http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f88168c2/libs/newtmgr_oic/src/newtmgr.c ---------------------------------------------------------------------- diff --git a/libs/newtmgr_oic/src/newtmgr.c b/libs/newtmgr_oic/src/newtmgr.c index 29a07de..136545f 100644 --- a/libs/newtmgr_oic/src/newtmgr.c +++ b/libs/newtmgr_oic/src/newtmgr.c @@ -26,7 +26,7 @@ #include <newtmgr/newtmgr.h> #include <nmgr_os/nmgr_os.h> -#include <iotivity/oc_api.h> +#include <oic/oc_api.h> #define NMGR_OC_EVENT (OS_EVENT_T_PERUSER) #define NMGR_OC_TIMER (OS_EVENT_T_PERUSER + 1) http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f88168c2/net/oic/include/oic/oc_api.h ---------------------------------------------------------------------- diff --git a/net/oic/include/oic/oc_api.h b/net/oic/include/oic/oc_api.h new file mode 100644 index 0000000..83e91f7 --- /dev/null +++ b/net/oic/include/oic/oc_api.h @@ -0,0 +1,174 @@ +/* +// 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 "../src/port/mynewt/config.h" +#include "../src/messaging/coap/oc_coap.h" +#include "oc_ri.h" +#include "../src/port/oc_signal_main_loop.h" +#include "../src/port/oc_storage.h" + +#ifdef __cplusplus +extern "C" { +#endif + +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) + +#ifdef __cplusplus +} +#endif + +#endif /* OC_API_H */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f88168c2/net/oic/include/oic/oc_buffer.h ---------------------------------------------------------------------- diff --git a/net/oic/include/oic/oc_buffer.h b/net/oic/include/oic/oc_buffer.h new file mode 100644 index 0000000..0bbf8a7 --- /dev/null +++ b/net/oic/include/oic/oc_buffer.h @@ -0,0 +1,40 @@ +/* +// 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 "../../src/port/oc_connectivity.h" +#include "../../src/util/oc_process.h" +#include <stdbool.h> + +#ifdef __cplusplus +extern "C" { +#endif + +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); + +#ifdef __cplusplus +} +#endif + +#endif /* OC_BUFFER_H */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f88168c2/net/oic/include/oic/oc_client_state.h ---------------------------------------------------------------------- diff --git a/net/oic/include/oic/oc_client_state.h b/net/oic/include/oic/oc_client_state.h new file mode 100644 index 0000000..43c25ac --- /dev/null +++ b/net/oic/include/oic/oc_client_state.h @@ -0,0 +1,96 @@ +/* +// 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 "../../src/messaging/coap/constants.h" +#include "oc_ri.h" +#include <stdbool.h> + +#ifdef __cplusplus +extern "C" { +#endif + +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); + +#ifdef __cplusplus +} +#endif + +#endif /* OC_CLIENT_STATE_H */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f88168c2/net/oic/include/oic/oc_constants.h ---------------------------------------------------------------------- diff --git a/net/oic/include/oic/oc_constants.h b/net/oic/include/oic/oc_constants.h new file mode 100644 index 0000000..cde1acf --- /dev/null +++ b/net/oic/include/oic/oc_constants.h @@ -0,0 +1,51 @@ +/* +// 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 + +#ifdef __cplusplus +extern "C" { +#endif + +/* 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" + +#ifdef __cplusplus +} +#endif + +#endif /* OC_CONSTANTS_H */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f88168c2/net/oic/include/oic/oc_core_res.h ---------------------------------------------------------------------- diff --git a/net/oic/include/oic/oc_core_res.h b/net/oic/include/oic/oc_core_res.h new file mode 100644 index 0000000..baf9fe8 --- /dev/null +++ b/net/oic/include/oic/oc_core_res.h @@ -0,0 +1,60 @@ +/* +// 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" + +#ifdef __cplusplus +extern "C" { +#endif + +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); + +#ifdef __cplusplus +} +#endif + +#endif /* OC_CORE_RES_H */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f88168c2/net/oic/include/oic/oc_discovery.h ---------------------------------------------------------------------- diff --git a/net/oic/include/oic/oc_discovery.h b/net/oic/include/oic/oc_discovery.h new file mode 100644 index 0000000..5b59a06 --- /dev/null +++ b/net/oic/include/oic/oc_discovery.h @@ -0,0 +1,30 @@ +/* +// 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 + +#ifdef __cplusplus +extern "C" { +#endif + +void oc_create_discovery_resource(void); + +#ifdef __cplusplus +} +#endif + +#endif /* OC_DISCOVERY_H */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f88168c2/net/oic/include/oic/oc_gatt.h ---------------------------------------------------------------------- diff --git a/net/oic/include/oic/oc_gatt.h b/net/oic/include/oic/oc_gatt.h new file mode 100644 index 0000000..ae6c59c --- /dev/null +++ b/net/oic/include/oic/oc_gatt.h @@ -0,0 +1,38 @@ +/** + * 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. + */ + +#ifndef OC_GATT_H +#define OC_GATT_H + +#ifdef __cplusplus +extern "C" { +#endif + +struct ble_hs_cfg; + +/* returns the event q for bluetooth to use */ +int +ble_coap_gatt_srv_init(struct os_eventq **out); + +#ifdef __cplusplus +} +#endif + +#endif /* OC_GATT_H */ + http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f88168c2/net/oic/include/oic/oc_helpers.h ---------------------------------------------------------------------- diff --git a/net/oic/include/oic/oc_helpers.h b/net/oic/include/oic/oc_helpers.h new file mode 100644 index 0000000..7520c7b --- /dev/null +++ b/net/oic/include/oic/oc_helpers.h @@ -0,0 +1,86 @@ +/* +// 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 "../../src/util/oc_list.h" +#include "../../src/util/oc_mmem.h" +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> + +#ifdef __cplusplus +extern "C" { +#endif + +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) + +#ifdef __cplusplus +} +#endif + +#endif /* OC_HELPERS_H */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f88168c2/net/oic/include/oic/oc_network_events.h ---------------------------------------------------------------------- diff --git a/net/oic/include/oic/oc_network_events.h b/net/oic/include/oic/oc_network_events.h new file mode 100644 index 0000000..d0f5d41 --- /dev/null +++ b/net/oic/include/oic/oc_network_events.h @@ -0,0 +1,37 @@ +/* +// 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 "../../src/port/oc_network_events_mutex.h" +#include "../../src/util/oc_process.h" + +#ifdef __cplusplus +extern "C" { +#endif + +OC_PROCESS_NAME(oc_network_events); + +typedef struct oc_message_s oc_message_t; + +void oc_network_event(oc_message_t *message); + +#ifdef __cplusplus +} +#endif + +#endif /* OC_NETWORK_EVENTS_H */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f88168c2/net/oic/include/oic/oc_rep.h ---------------------------------------------------------------------- diff --git a/net/oic/include/oic/oc_rep.h b/net/oic/include/oic/oc_rep.h new file mode 100644 index 0000000..f0f80a6 --- /dev/null +++ b/net/oic/include/oic/oc_rep.h @@ -0,0 +1,236 @@ +/* +// 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 "../src/port/mynewt/config.h" +#include <stdbool.h> +#include <stdint.h> + +#ifdef __cplusplus +extern "C" { +#endif + +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); + +#ifdef __cplusplus +} +#endif + +#endif /* OC_REP_H */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f88168c2/net/oic/include/oic/oc_ri.h ---------------------------------------------------------------------- diff --git a/net/oic/include/oic/oc_ri.h b/net/oic/include/oic/oc_ri.h new file mode 100644 index 0000000..a5bce1d --- /dev/null +++ b/net/oic/include/oic/oc_ri.h @@ -0,0 +1,184 @@ +/* +// 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 "../src/port/mynewt/config.h" +#include "oc_rep.h" +#include "oc_uuid.h" +#include "../../src/port/oc_connectivity.h" +#include "../../src/util/oc_etimer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +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); + +#ifdef __cplusplus +} +#endif + +#endif /* OC_RI_H */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f88168c2/net/oic/include/oic/oc_uuid.h ---------------------------------------------------------------------- diff --git a/net/oic/include/oic/oc_uuid.h b/net/oic/include/oic/oc_uuid.h new file mode 100644 index 0000000..03a8740 --- /dev/null +++ b/net/oic/include/oic/oc_uuid.h @@ -0,0 +1,39 @@ +/* +// 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> + +#ifdef __cplusplus +extern "C" { +#endif + +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); + +#ifdef __cplusplus +} +#endif + +#endif /* OC_UUID_H */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f88168c2/net/oic/pkg.yml ---------------------------------------------------------------------- diff --git a/net/oic/pkg.yml b/net/oic/pkg.yml new file mode 100644 index 0000000..0843be5 --- /dev/null +++ b/net/oic/pkg.yml @@ -0,0 +1,51 @@ +# +# 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: net/oic +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/encoding/tinycbor" + - "@apache-mynewt-core/kernel/os" + - "@apache-mynewt-core/sys/log" + +pkg.deps.OC_TRANSPORT_GATT: + - "@apache-mynewt-core/net/nimble/host" + - "@apache-mynewt-core/net/nimble/host/services/gap" + - "@apache-mynewt-core/net/nimble/host/services/gatt" + +pkg.deps.OC_TRANSPORT_IP: + - "@apache-mynewt-core/net/ip/mn_socket" + +pkg.deps.OC_TRANSPORT_SERIAL: + - "@apache-mynewt-core/sys/shell" + +# remove debug option to save logging +pkg.cflags: + - -std=c99 + - -DSECURE=0 + - -I./port/mynewt + - -I../port/mynewt + - -I../../port/mynewt + - -I../include/oic + - -I../../include/oic + - -I../../../include/oic http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f88168c2/net/oic/src/api/oc_buffer.c ---------------------------------------------------------------------- diff --git a/net/oic/src/api/oc_buffer.c b/net/oic/src/api/oc_buffer.c new file mode 100644 index 0000000..e623221 --- /dev/null +++ b/net/oic/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/f88168c2/net/oic/src/api/oc_client_api.c ---------------------------------------------------------------------- diff --git a/net/oic/src/api/oc_client_api.c b/net/oic/src/api/oc_client_api.c new file mode 100644 index 0000000..bd73da3 --- /dev/null +++ b/net/oic/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/f88168c2/net/oic/src/api/oc_core_res.c ---------------------------------------------------------------------- diff --git a/net/oic/src/api/oc_core_res.c b/net/oic/src/api/oc_core_res.c new file mode 100644 index 0000000..ced0f88 --- /dev/null +++ b/net/oic/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; +}