You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2016/10/25 01:53:35 UTC

[1/2] incubator-mynewt-core git commit: ocf_sample - BLE server support.

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop c28b9b17d -> ac3c314ce


ocf_sample - BLE server support.


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/94f0a7f9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/94f0a7f9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/94f0a7f9

Branch: refs/heads/develop
Commit: 94f0a7f97fe290b99a9747e83576f0299732d71d
Parents: c28b9b1
Author: Christopher Collins <cc...@apache.org>
Authored: Mon Oct 24 18:53:00 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Mon Oct 24 18:53:27 2016 -0700

----------------------------------------------------------------------
 apps/ocf_sample/pkg.yml          |   3 +-
 apps/ocf_sample/src/main.c       |  36 +----
 apps/ocf_sample/src/ocf_ble.c    | 284 ++++++++++++++++++++++++++++++++++
 apps/ocf_sample/src/ocf_sample.h |  35 +++++
 4 files changed, 326 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/94f0a7f9/apps/ocf_sample/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/ocf_sample/pkg.yml b/apps/ocf_sample/pkg.yml
index b865ea3..5e37779 100644
--- a/apps/ocf_sample/pkg.yml
+++ b/apps/ocf_sample/pkg.yml
@@ -35,5 +35,6 @@ pkg.deps.OC_TRANSPORT_SERIAL:
 
 pkg.deps.OC_TRANSPORT_GATT:
     - net/nimble/controller
-    - net/nimble/transport/ram
+    - net/nimble/host
     - net/nimble/host/store/ram
+    - net/nimble/transport/ram

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/94f0a7f9/apps/ocf_sample/src/main.c
----------------------------------------------------------------------
diff --git a/apps/ocf_sample/src/main.c b/apps/ocf_sample/src/main.c
index 01257c6..b174fd5 100644
--- a/apps/ocf_sample/src/main.c
+++ b/apps/ocf_sample/src/main.c
@@ -36,12 +36,7 @@
 #endif
 
 #if (MYNEWT_VAL(OC_TRANSPORT_GATT) == 1)
-#include <oic/oc_gatt.h>
-#include "nimble/ble.h"
-#include "host/ble_hs.h"
-#include "controller/ble_ll.h"
-#include "services/gap/ble_svc_gap.h"
-#include "services/gatt/ble_svc_gatt.h"
+#include "ocf_sample.h"
 #endif
 
 #define OCF_TASK_PRIO      (8)
@@ -266,44 +261,23 @@ ocf_task_init(void) {
     oc_main_init(&ocf_handler);
 }
 
-static const uint8_t addr[6] = {1,2,3,4,5,6};
-
 int
 main(int argc, char **argv)
 {
     int rc;
-#if (MYNEWT_VAL(OC_TRANSPORT_GATT) == 1)
-    struct os_eventq *ev;
-#endif
+
+    (void)rc;
+
     /* Initialize OS */
     sysinit();
 
-    /* Set cputime to count at 1 usec increments */
-    rc = os_cputime_init(1000000);
-    assert(rc == 0);
-
 #if (MYNEWT_VAL(OC_TRANSPORT_IP) == 1)
     rc = native_sock_init();
     assert(rc == 0);
 #endif
 
 #if (MYNEWT_VAL(OC_TRANSPORT_GATT) == 1)
-    memcpy(g_dev_addr, addr, sizeof(g_dev_addr));
-
-    /* COAP Gatt server initialization */
-    rc = ble_coap_gatt_srv_init(&ev);
-    assert(rc == 0);
-
-#if (MYNEWT_VAL(OC_CLIENT) == 1)
-    /* TODO INIT CLIENT */
-#endif
-    /* Set the default device name. */
-    rc = ble_svc_gap_device_name_set("Mynewt_OCF");
-    assert(rc == 0);
-
-    /* Initialize the BLE host. */
-    log_register("ble_hs", &ble_hs_log, &log_console_handler, NULL, LOG_SYSLEVEL);
-    ble_hs_cfg.parent_evq = ev;
+    ocf_ble_init();
 #endif
 
     ocf_task_init();

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/94f0a7f9/apps/ocf_sample/src/ocf_ble.c
----------------------------------------------------------------------
diff --git a/apps/ocf_sample/src/ocf_ble.c b/apps/ocf_sample/src/ocf_ble.c
new file mode 100644
index 0000000..8d02d2f
--- /dev/null
+++ b/apps/ocf_sample/src/ocf_ble.c
@@ -0,0 +1,284 @@
+/**
+ * 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.
+ */
+
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(OC_TRANSPORT_GATT)
+
+#include <assert.h>
+#include <string.h>
+
+#include "log/log.h"
+
+#include "oic/oc_gatt.h"
+
+/* BLE */
+#include "nimble/ble.h"
+#include "host/ble_hs.h"
+#include "services/gap/ble_svc_gap.h"
+
+struct log ocf_ble_log;
+
+/* ocf_ble uses the first "peruser" log module. */
+#define OCF_BLE_LOG_MODULE  (LOG_MODULE_PERUSER + 0)
+
+/* Convenience macro for logging to the ocf_ble module. */
+#define OCF_BLE_LOG(lvl, ...) \
+    LOG_ ## lvl(&ocf_ble_log, OCF_BLE_LOG_MODULE, __VA_ARGS__)
+
+static int ocf_ble_gap_event(struct ble_gap_event *event, void *arg);
+
+/**
+ * Utility function to log an array of bytes.
+ */
+void
+print_bytes(const uint8_t *bytes, int len)
+{
+    int i;
+
+    for (i = 0; i < len; i++) {
+        OCF_BLE_LOG(INFO, "%s0x%02x", i != 0 ? ":" : "", bytes[i]);
+    }
+}
+
+void
+print_addr(const void *addr)
+{
+    const uint8_t *u8p;
+
+    u8p = addr;
+    OCF_BLE_LOG(INFO, "%02x:%02x:%02x:%02x:%02x:%02x",
+                u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]);
+}
+
+/**
+ * Logs information about a connection to the console.
+ */
+static void
+ocf_ble_print_conn_desc(struct ble_gap_conn_desc *desc)
+{
+    OCF_BLE_LOG(INFO, "handle=%d our_ota_addr_type=%d our_ota_addr=",
+                desc->conn_handle, desc->our_ota_addr_type);
+    print_addr(desc->our_ota_addr);
+    OCF_BLE_LOG(INFO, " our_id_addr_type=%d our_id_addr=",
+                desc->our_id_addr_type);
+    print_addr(desc->our_id_addr);
+    OCF_BLE_LOG(INFO, " peer_ota_addr_type=%d peer_ota_addr=",
+                desc->peer_ota_addr_type);
+    print_addr(desc->peer_ota_addr);
+    OCF_BLE_LOG(INFO, " peer_id_addr_type=%d peer_id_addr=",
+                desc->peer_id_addr_type);
+    print_addr(desc->peer_id_addr);
+    OCF_BLE_LOG(INFO, " conn_itvl=%d conn_latency=%d supervision_timeout=%d "
+                "encrypted=%d authenticated=%d bonded=%d\n",
+                desc->conn_itvl, desc->conn_latency,
+                desc->supervision_timeout,
+                desc->sec_state.encrypted,
+                desc->sec_state.authenticated,
+                desc->sec_state.bonded);
+}
+
+/**
+ * Enables advertising with the following parameters:
+ *     o General discoverable mode.
+ *     o Undirected connectable mode.
+ */
+static void
+ocf_ble_advertise(void)
+{
+    struct ble_gap_adv_params adv_params;
+    struct ble_hs_adv_fields fields;
+    const char *name;
+    int rc;
+
+    /**
+     *  Set the advertisement data included in our advertisements:
+     *     o Flags (indicates advertisement type and other general info).
+     *     o Advertising tx power.
+     *     o Device name.
+     */
+
+    memset(&fields, 0, sizeof fields);
+
+    /* Indicate that the flags field should be included; specify a value of 0
+     * to instruct the stack to fill the value in for us.
+     */
+    fields.flags_is_present = 1;
+    fields.flags = 0;
+
+    /* Indicate that the TX power level field should be included; have the
+     * stack fill this one automatically as well.  This is done by assiging the
+     * special value BLE_HS_ADV_TX_PWR_LVL_AUTO.
+     */
+    fields.tx_pwr_lvl_is_present = 1;
+    fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
+
+    name = ble_svc_gap_device_name();
+    fields.name = (uint8_t *)name;
+    fields.name_len = strlen(name);
+    fields.name_is_complete = 1;
+
+    rc = ble_gap_adv_set_fields(&fields);
+    if (rc != 0) {
+        OCF_BLE_LOG(ERROR, "error setting advertisement data; rc=%d\n", rc);
+        return;
+    }
+
+    /* Begin advertising. */
+    memset(&adv_params, 0, sizeof adv_params);
+    adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
+    adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
+    rc = ble_gap_adv_start(BLE_ADDR_TYPE_PUBLIC, 0, NULL, BLE_HS_FOREVER,
+                           &adv_params, ocf_ble_gap_event, NULL);
+    if (rc != 0) {
+        OCF_BLE_LOG(ERROR, "error enabling advertisement; rc=%d\n", rc);
+        return;
+    }
+}
+
+static void
+ocf_ble_on_sync(void)
+{
+    /* Begin advertising. */
+    ocf_ble_advertise();
+}
+
+/**
+ * The nimble host executes this callback when a GAP event occurs.  The
+ * application associates a GAP event callback with each connection that forms.
+ * ocf_ble uses the same callback for all connections.
+ *
+ * @param event                 The type of event being signalled.
+ * @param ctxt                  Various information pertaining to the event.
+ * @param arg                   Application-specified argument; unuesd by
+ *                                  ocf_ble.
+ *
+ * @return                      0 if the application successfully handled the
+ *                                  event; nonzero on failure.  The semantics
+ *                                  of the return code is specific to the
+ *                                  particular GAP event being signalled.
+ */
+static int
+ocf_ble_gap_event(struct ble_gap_event *event, void *arg)
+{
+    struct ble_gap_conn_desc desc;
+    int rc;
+
+    switch (event->type) {
+    case BLE_GAP_EVENT_CONNECT:
+        /* A new connection was established or a connection attempt failed. */
+        OCF_BLE_LOG(INFO, "connection %s; status=%d ",
+                       event->connect.status == 0 ? "established" : "failed",
+                       event->connect.status);
+        if (event->connect.status == 0) {
+            rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
+            assert(rc == 0);
+            ocf_ble_print_conn_desc(&desc);
+        }
+        OCF_BLE_LOG(INFO, "\n");
+
+        if (event->connect.status != 0) {
+            /* Connection failed; resume advertising. */
+            ocf_ble_advertise();
+        }
+        return 0;
+
+    case BLE_GAP_EVENT_DISCONNECT:
+        OCF_BLE_LOG(INFO, "disconnect; reason=%d ", event->disconnect.reason);
+        ocf_ble_print_conn_desc(&event->disconnect.conn);
+        OCF_BLE_LOG(INFO, "\n");
+
+        /* Connection terminated; resume advertising. */
+        ocf_ble_advertise();
+        return 0;
+
+    case BLE_GAP_EVENT_CONN_UPDATE:
+        /* The central has updated the connection parameters. */
+        OCF_BLE_LOG(INFO, "connection updated; status=%d ",
+                    event->conn_update.status);
+        rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
+        assert(rc == 0);
+        ocf_ble_print_conn_desc(&desc);
+        OCF_BLE_LOG(INFO, "\n");
+        return 0;
+
+    case BLE_GAP_EVENT_ENC_CHANGE:
+        /* Encryption has been enabled or disabled for this connection. */
+        OCF_BLE_LOG(INFO, "encryption change event; status=%d ",
+                    event->enc_change.status);
+        rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
+        assert(rc == 0);
+        ocf_ble_print_conn_desc(&desc);
+        OCF_BLE_LOG(INFO, "\n");
+        return 0;
+
+    case BLE_GAP_EVENT_SUBSCRIBE:
+        OCF_BLE_LOG(INFO, "subscribe event; conn_handle=%d attr_handle=%d "
+                          "reason=%d prevn=%d curn=%d previ=%d curi=%d\n",
+                    event->subscribe.conn_handle,
+                    event->subscribe.attr_handle,
+                    event->subscribe.reason,
+                    event->subscribe.prev_notify,
+                    event->subscribe.cur_notify,
+                    event->subscribe.prev_indicate,
+                    event->subscribe.cur_indicate);
+        return 0;
+
+    case BLE_GAP_EVENT_MTU:
+        OCF_BLE_LOG(INFO, "mtu update event; conn_handle=%d cid=%d mtu=%d\n",
+                    event->mtu.conn_handle,
+                    event->mtu.channel_id,
+                    event->mtu.value);
+        return 0;
+    }
+
+    return 0;
+}
+
+static const uint8_t ocf_ble_addr[6] = {1,2,3,4,5,6};
+
+void
+ocf_ble_init(void)
+{
+    struct os_eventq *evq;
+    int rc;
+
+    /* Initialize the ocf_ble log. */
+    log_register("ocf_ble", &ocf_ble_log, &log_console_handler, NULL,
+                 LOG_SYSLEVEL);
+
+    memcpy(g_dev_addr, ocf_ble_addr, sizeof(g_dev_addr));
+
+    /* COAP Gatt server initialization */
+    rc = ble_coap_gatt_srv_init(&evq);
+    assert(rc == 0);
+
+    /* Set the default device name. */
+    rc = ble_svc_gap_device_name_set("Mynewt_OCF");
+    assert(rc == 0);
+
+    /* Initialize the BLE host. */
+    log_register("ble_hs", &ble_hs_log, &log_console_handler, NULL,
+                 LOG_SYSLEVEL);
+
+    ble_hs_cfg.parent_evq = evq;
+    ble_hs_cfg.sync_cb = ocf_ble_on_sync;
+}
+
+#endif /* MYNEWT_VAL(OC_TRANSPORT_GATT) */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/94f0a7f9/apps/ocf_sample/src/ocf_sample.h
----------------------------------------------------------------------
diff --git a/apps/ocf_sample/src/ocf_sample.h b/apps/ocf_sample/src/ocf_sample.h
new file mode 100644
index 0000000..b2327f1
--- /dev/null
+++ b/apps/ocf_sample/src/ocf_sample.h
@@ -0,0 +1,35 @@
+/**
+ * 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 H_OCF_BLE_
+#define H_OCF_BLE_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if MYNEWT_VAL(OC_TRANSPORT_GATT)
+void ocf_ble_init(void);
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif


[2/2] incubator-mynewt-core git commit: ocf_sample - whitespace cleanup.

Posted by cc...@apache.org.
ocf_sample - whitespace cleanup.


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/ac3c314c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/ac3c314c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/ac3c314c

Branch: refs/heads/develop
Commit: ac3c314cefeca7ca79564294107202cf28c20d4f
Parents: 94f0a7f97
Author: Christopher Collins <cc...@apache.org>
Authored: Mon Oct 24 18:53:15 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Mon Oct 24 18:53:28 2016 -0700

----------------------------------------------------------------------
 apps/ocf_sample/src/main.c | 196 +++++++++++++++++++++-------------------
 1 file changed, 101 insertions(+), 95 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ac3c314c/apps/ocf_sample/src/main.c
----------------------------------------------------------------------
diff --git a/apps/ocf_sample/src/main.c b/apps/ocf_sample/src/main.c
index b174fd5..c657592 100644
--- a/apps/ocf_sample/src/main.c
+++ b/apps/ocf_sample/src/main.c
@@ -54,44 +54,44 @@ static bool light_state = false;
 static void
 get_light(oc_request_t *request, oc_interface_mask_t interface)
 {
-  PRINT("GET_light:\n");
-  oc_rep_start_root_object();
-  switch (interface) {
-  case OC_IF_BASELINE:
-    oc_process_baseline_interface(request->resource);
-  case OC_IF_RW:
-    oc_rep_set_boolean(root, state, light_state);
-    break;
-  default:
-    break;
-  }
-  oc_rep_end_root_object();
-  oc_send_response(request, OC_STATUS_OK);
-  PRINT("Light state %d\n", light_state);
+    PRINT("GET_light:\n");
+    oc_rep_start_root_object();
+    switch (interface) {
+        case OC_IF_BASELINE:
+            oc_process_baseline_interface(request->resource);
+        case OC_IF_RW:
+            oc_rep_set_boolean(root, state, light_state);
+            break;
+        default:
+            break;
+    }
+    oc_rep_end_root_object();
+    oc_send_response(request, OC_STATUS_OK);
+    PRINT("Light state %d\n", light_state);
 }
 
 static void
 put_light(oc_request_t *request, oc_interface_mask_t interface)
 {
-  PRINT("PUT_light:\n");
-  bool state = false;
-  oc_rep_t *rep = request->request_payload;
-  while (rep != NULL) {
-    PRINT("key: %s ", oc_string(rep->name));
-    switch (rep->type) {
-    case BOOL:
-      state = rep->value_boolean;
-      PRINT("value: %d\n", state);
-      break;
-    default:
-      oc_send_response(request, OC_STATUS_BAD_REQUEST);
-      return;
-      break;
+    PRINT("PUT_light:\n");
+    bool state = false;
+    oc_rep_t *rep = request->request_payload;
+    while (rep != NULL) {
+        PRINT("key: %s ", oc_string(rep->name));
+        switch (rep->type) {
+            case BOOL:
+                state = rep->value_boolean;
+                PRINT("value: %d\n", state);
+                break;
+            default:
+                oc_send_response(request, OC_STATUS_BAD_REQUEST);
+                return;
+                break;
+        }
+        rep = rep->next;
     }
-    rep = rep->next;
-  }
-  oc_send_response(request, OC_STATUS_CHANGED);
-  light_state = state;
+    oc_send_response(request, OC_STATUS_CHANGED);
+    light_state = state;
 }
 
 static void
@@ -119,120 +119,125 @@ static bool light_state = false;
 static void
 set_device_custom_property(void *data)
 {
-  oc_set_custom_device_property(purpose, "operate mynewt-light");
+    oc_set_custom_device_property(purpose, "operate mynewt-light");
 }
 
 static oc_event_callback_retval_t
 stop_observe(void *data)
 {
-  PRINT("Stopping OBSERVE\n");
-  oc_stop_observe(light_1, &light_server);
-  return DONE;
+    PRINT("Stopping OBSERVE\n");
+    oc_stop_observe(light_1, &light_server);
+    return DONE;
 }
 
 static void
 put_light(oc_client_response_t *data)
 {
-  PRINT("PUT_light:\n");
-  if (data->code == OC_STATUS_CHANGED)
-    PRINT("PUT response OK\n");
-  else
-    PRINT("PUT response code %d\n", data->code);
+    PRINT("PUT_light:\n");
+    if (data->code == OC_STATUS_CHANGED)
+        PRINT("PUT response OK\n");
+    else
+        PRINT("PUT response code %d\n", data->code);
 }
 
 static void
 observe_light(oc_client_response_t *data)
 {
-  PRINT("OBSERVE_light:\n");
-  oc_rep_t *rep = data->payload;
-  while (rep != NULL) {
-    PRINT("key %s, value ", oc_string(rep->name));
-    switch (rep->type) {
-    case BOOL:
-      PRINT("%d\n", rep->value_boolean);
-      light_state = rep->value_boolean;
-      break;
-    default:
-      break;
+    PRINT("OBSERVE_light:\n");
+    oc_rep_t *rep = data->payload;
+    while (rep != NULL) {
+        PRINT("key %s, value ", oc_string(rep->name));
+        switch (rep->type) {
+            case BOOL:
+                PRINT("%d\n", rep->value_boolean);
+                light_state = rep->value_boolean;
+                break;
+            default:
+                break;
+        }
+        rep = rep->next;
     }
-    rep = rep->next;
-  }
 
-  if (oc_init_put(light_1, &light_server, NULL, &put_light, LOW_QOS)) {
-    oc_rep_start_root_object();
-    oc_rep_set_boolean(root, state, !light_state);
-    oc_rep_end_root_object();
-    if (oc_do_put())
-      PRINT("Sent PUT request\n");
-    else
-      PRINT("Could not send PUT\n");
-  } else
-    PRINT("Could not init PUT\n");
+    if (oc_init_put(light_1, &light_server, NULL, &put_light, LOW_QOS)) {
+        oc_rep_start_root_object();
+        oc_rep_set_boolean(root, state, !light_state);
+        oc_rep_end_root_object();
+        if (oc_do_put())
+            PRINT("Sent PUT request\n");
+        else
+            PRINT("Could not send PUT\n");
+    } else
+        PRINT("Could not init PUT\n");
 }
 
 static oc_discovery_flags_t
 discovery(const char *di, const char *uri, oc_string_array_t types,
           oc_interface_mask_t interfaces, oc_server_handle_t *server)
 {
-  int i;
-  int uri_len = strlen(uri);
-  uri_len = (uri_len >= MAX_URI_LENGTH) ? MAX_URI_LENGTH - 1 : uri_len;
-
-  for (i = 0; i < oc_string_array_get_allocated_size(types); i++) {
-    char *t = oc_string_array_get_item(types, i);
-    if (strlen(t) == 11 && strncmp(t, "oic.r.light", 11) == 0) {
-      memcpy(&light_server, server, sizeof(oc_server_handle_t));
-
-      strncpy(light_1, uri, uri_len);
-      light_1[uri_len] = '\0';
-
-      oc_do_observe(light_1, &light_server, NULL, &observe_light, LOW_QOS);
-      oc_set_delayed_callback(NULL, &stop_observe, 30);
-      return OC_STOP_DISCOVERY;
+    int i;
+    int uri_len = strlen(uri);
+    uri_len = (uri_len >= MAX_URI_LENGTH) ? MAX_URI_LENGTH - 1 : uri_len;
+
+    for (i = 0; i < oc_string_array_get_allocated_size(types); i++) {
+        char *t = oc_string_array_get_item(types, i);
+        if (strlen(t) == 11 && strncmp(t, "oic.r.light", 11) == 0) {
+            memcpy(&light_server, server, sizeof(oc_server_handle_t));
+
+            strncpy(light_1, uri, uri_len);
+            light_1[uri_len] = '\0';
+
+            oc_do_observe(light_1, &light_server, NULL, &observe_light,
+                          LOW_QOS);
+            oc_set_delayed_callback(NULL, &stop_observe, 30);
+            return OC_STOP_DISCOVERY;
+        }
     }
-  }
-  return OC_CONTINUE_DISCOVERY;
+    return OC_CONTINUE_DISCOVERY;
 }
 
 static void
 issue_requests(void)
 {
-  oc_do_ip_discovery("oic.r.light", &discovery);
+    oc_do_ip_discovery("oic.r.light", &discovery);
 }
 #endif
 
 static void
 app_init(void)
 {
-  oc_init_platform("Mynewt", NULL, NULL);
+    oc_init_platform("Mynewt", NULL, NULL);
 #if (MYNEWT_VAL(OC_CLIENT) == 1)
-  oc_add_device("/oic/d", "oic.d.light", "MynewtClient", "1.0", "1.0",
-                set_device_custom_property, NULL);
+    oc_add_device("/oic/d", "oic.d.light", "MynewtClient", "1.0", "1.0",
+                  set_device_custom_property, NULL);
 #endif
 
 #if (MYNEWT_VAL(OC_SERVER) == 1)
-    oc_add_device("/oic/d", "oic.d.light", "MynewtServer", "1.0", "1.0", NULL, NULL);
+    oc_add_device("/oic/d", "oic.d.light", "MynewtServer", "1.0", "1.0", NULL,
+                  NULL);
 #endif
 }
 
- oc_handler_t ocf_handler = {.init = app_init,
+oc_handler_t ocf_handler = {
+    .init = app_init,
 #if (MYNEWT_VAL(OC_SERVER) == 1)
-                          .register_resources = register_resources,
+    .register_resources = register_resources,
 #endif
 #if (MYNEWT_VAL(OC_CLIENT) == 1)
-                          .requests_entry = issue_requests,
+    .requests_entry = issue_requests,
 #endif
  };
 
 struct os_sem ocf_main_loop_sem;
 
- void
-oc_signal_main_loop(void) {
+void
+oc_signal_main_loop(void)
+{
      os_sem_release(&ocf_main_loop_sem);
 }
 
-void ocf_task_handler(void *arg) {
-
+void
+ocf_task_handler(void *arg)
+{
     os_sem_init(&ocf_main_loop_sem, 1);
 
     while (1) {
@@ -251,9 +256,10 @@ void ocf_task_handler(void *arg) {
 }
 
 void
-ocf_task_init(void) {
-
+ocf_task_init(void)
+{
     int rc;
+
     rc = os_task_init(&ocf_task, "ocf", ocf_task_handler, NULL,
             OCF_TASK_PRIO, OS_WAIT_FOREVER, ocf_stack, OCF_TASK_STACK_SIZE);
     assert(rc == 0);