You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ma...@apache.org on 2016/12/15 00:47:26 UTC
[3/4] incubator-mynewt-core git commit: bleprph_oic;
add an example resource, which controls BSP's LED pin.
bleprph_oic; add an example resource, which controls BSP's LED pin.
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/edbdda6f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/edbdda6f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/edbdda6f
Branch: refs/heads/develop
Commit: edbdda6f41a98ff07398c05ba6daf5503a766b44
Parents: 0ea2aca
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Wed Dec 14 16:18:30 2016 -0800
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Wed Dec 14 16:45:51 2016 -0800
----------------------------------------------------------------------
apps/bleprph_oic/src/main.c | 98 ++++++++++++++++++++++++++++++++++------
1 file changed, 84 insertions(+), 14 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/edbdda6f/apps/bleprph_oic/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bleprph_oic/src/main.c b/apps/bleprph_oic/src/main.c
index 203b1d4..22b4b81 100755
--- a/apps/bleprph_oic/src/main.c
+++ b/apps/bleprph_oic/src/main.c
@@ -21,26 +21,28 @@
#include <string.h>
#include <stdio.h>
#include <errno.h>
-#include "sysinit/sysinit.h"
-#include "bsp/bsp.h"
-#include "os/os.h"
-#include "bsp/bsp.h"
-#include "hal/hal_gpio.h"
-#include "console/console.h"
-#include "imgmgr/imgmgr.h"
-#include "mgmt/mgmt.h"
-#include "oic/oc_api.h"
+#include <sysinit/sysinit.h>
+#include <bsp/bsp.h>
+#include <os/os.h>
+#include <bsp/bsp.h>
+#include <hal/hal_gpio.h>
+#include <console/console.h>
+#include <imgmgr/imgmgr.h>
+#include <mgmt/mgmt.h>
+
+#include <oic/oc_api.h>
+#include <oic/oc_gatt.h>
+#include <oic/oc_log.h>
+#include <cborattr/cborattr.h>
/* BLE */
-#include "nimble/ble.h"
-#include "host/ble_hs.h"
-#include "services/gap/ble_svc_gap.h"
+#include <nimble/ble.h>
+#include <host/ble_hs.h>
+#include <services/gap/ble_svc_gap.h>
/* Application-specified header. */
#include "bleprph.h"
-#include <oic/oc_gatt.h>
-#include <oic/oc_log.h>
/** Log data. */
struct log bleprph_log;
@@ -255,11 +257,76 @@ bleprph_on_sync(void)
}
static void
+app_get_light(oc_request_t *request, oc_interface_mask_t interface)
+{
+ bool state;
+
+ if (hal_gpio_read(LED_BLINK_PIN)) {
+ state = true;
+ } else {
+ state = false;
+ }
+ 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, state);
+ break;
+ default:
+ break;
+ }
+ oc_rep_end_root_object();
+ oc_send_response(request, OC_STATUS_OK);
+}
+
+static void
+app_set_light(oc_request_t *request, oc_interface_mask_t interface)
+{
+ bool state;
+ int len;
+ const uint8_t *data;
+ struct cbor_attr_t attrs[] = {
+ [0] = {
+ .attribute = "state",
+ .type = CborAttrBooleanType,
+ .addr.boolean = &state,
+ .dflt.boolean = false
+ },
+ [1] = {
+ }
+ };
+
+ len = coap_get_payload(request->packet, &data);
+ if (cbor_read_flat_attrs(data, len, attrs)) {
+ oc_send_response(request, OC_STATUS_BAD_REQUEST);
+ } else {
+ hal_gpio_write(LED_BLINK_PIN, state == true);
+ oc_send_response(request, OC_STATUS_CHANGED);
+ }
+
+}
+
+static void
omgr_app_init(void)
{
+ oc_resource_t *res;
+
oc_init_platform("MyNewt", NULL, NULL);
oc_add_device("/oic/d", "oic.d.light", "MynewtLed", "1.0", "1.0", NULL,
NULL);
+
+ res = oc_new_resource("/light/1", 1, 0);
+ oc_resource_bind_resource_type(res, "oic.r.light");
+ oc_resource_bind_resource_interface(res, OC_IF_RW);
+ oc_resource_set_default_interface(res, OC_IF_RW);
+
+ oc_resource_set_discoverable(res);
+ oc_resource_set_periodic_observable(res, 1);
+ oc_resource_set_request_handler(res, OC_GET, app_get_light);
+ oc_resource_set_request_handler(res, OC_PUT, app_set_light);
+ oc_add_resource(res);
+
}
static const oc_handler_t omgr_oc_handler = {
@@ -333,6 +400,9 @@ main(void)
rc = ble_svc_gap_device_name_set("pi");
assert(rc == 0);
+ /* Our light resource */
+ hal_gpio_init_out(LED_BLINK_PIN, 1);
+
/* Start the OS */
os_start();