You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by an...@apache.org on 2020/03/17 16:10:37 UTC

[mynewt-nimble] branch master updated: apps: Add simple beacon sample

This is an automated email from the ASF dual-hosted git repository.

andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git


The following commit(s) were added to refs/heads/master by this push:
     new 969e896  apps: Add simple beacon sample
969e896 is described below

commit 969e896ec4f62b0fa54a904973a444777d0c38a8
Author: Krzysztof Kopyściński <kr...@codecoup.pl>
AuthorDate: Thu Mar 12 10:45:59 2020 +0100

    apps: Add simple beacon sample
    
    apps: Add simple beacon sample
    
    apps: Add simple advertiser sample
---
 apps/advertiser/pkg.yml    |  33 +++++++++++
 apps/advertiser/src/main.c | 136 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 169 insertions(+)

diff --git a/apps/advertiser/pkg.yml b/apps/advertiser/pkg.yml
new file mode 100644
index 0000000..662e282
--- /dev/null
+++ b/apps/advertiser/pkg.yml
@@ -0,0 +1,33 @@
+#
+# 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: "apps/advertiser"
+pkg.type: app
+pkg.description: "Basic advertiser application"
+pkg.author: "Krzysztof Kopyściński <kr...@codecoup.pl>"
+
+pkg.deps:
+    - "@apache-mynewt-core/kernel/os"
+    - "@apache-mynewt-core/sys/console/full"
+    - "@apache-mynewt-core/sys/log/full"
+    - "@apache-mynewt-core/sys/stats/full"
+    - "@apache-mynewt-core/sys/log/modlog"
+    - "@apache-mynewt-nimble/nimble/host/util"
+    - "@apache-mynewt-nimble/nimble/host/services/gap"
+    - "@apache-mynewt-nimble/nimble/transport"
diff --git a/apps/advertiser/src/main.c b/apps/advertiser/src/main.c
new file mode 100644
index 0000000..486d5c5
--- /dev/null
+++ b/apps/advertiser/src/main.c
@@ -0,0 +1,136 @@
+/*
+ * 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 <assert.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "os/os.h"
+#include "sysinit/sysinit.h"
+#include "log/log.h"
+#include "host/ble_hs.h"
+#include "host/util/util.h"
+#include "services/gap/ble_svc_gap.h"
+
+static const char *device_name = "Apache Mynewt";
+
+/* adv_event() calls advertise(), so forward declaration is required */
+static void advertise(void);
+
+static void
+set_ble_addr(void)
+{
+    int rc;
+    ble_addr_t addr;
+
+    /* generate new non-resolvable private address */
+    rc = ble_hs_id_gen_rnd(1, &addr);
+    assert(rc == 0);
+
+    /* set generated address */
+    rc = ble_hs_id_set_rnd(addr.val);
+    assert(rc == 0);
+}
+
+static int
+adv_event(struct ble_gap_event *event, void *arg)
+{
+    switch (event->type) {
+    case BLE_GAP_EVENT_ADV_COMPLETE:
+        MODLOG_DFLT(INFO, "Advertising completed, termination code: %d\n",
+                    event->adv_complete.reason);
+        advertise();
+        return 0;
+    default:
+        MODLOG_DFLT(ERROR, "Advertising event not handled\n");
+        return 0;
+    }
+}
+
+static void
+advertise(void)
+{
+    int rc;
+    struct ble_gap_adv_params adv_params;
+    struct ble_hs_adv_fields fields;
+
+    /* set adv parameters */
+    memset(&adv_params, 0, sizeof(adv_params));
+    adv_params.conn_mode = BLE_GAP_CONN_MODE_NON;
+    adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
+
+    memset(&fields, 0, sizeof(fields));
+
+    /* Fill the fields with advertising data - flags, tx power level, name */
+    fields.flags = BLE_HS_ADV_F_DISC_GEN;
+    fields.tx_pwr_lvl_is_present = 1;
+    fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
+    fields.name = (uint8_t *)device_name;
+    fields.name_len = strlen(device_name);
+    fields.name_is_complete = 1;
+
+    rc = ble_gap_adv_set_fields(&fields);
+    assert(rc == 0);
+
+    MODLOG_DFLT(INFO, "Starting advertising...\n");
+
+    /* As own address type we use hard-coded value, because we generate
+       NRPA and by definition it's random */
+    rc = ble_gap_adv_start(BLE_OWN_ADDR_RANDOM, NULL, 10000,
+                           &adv_params, adv_event, NULL);
+    assert(rc == 0);
+}
+
+static void
+on_sync(void)
+{
+    set_ble_addr();
+
+    /* begin advertising */
+    advertise();
+}
+
+static void
+on_reset(int reason)
+{
+    MODLOG_DFLT(INFO, "Resetting state; reason=%d\n", reason);
+}
+
+int
+main(int argc, char **argv)
+{
+    int rc;
+
+    /* Initialize all packages. */
+    sysinit();
+
+    ble_hs_cfg.sync_cb = on_sync;
+    ble_hs_cfg.reset_cb = on_reset;
+
+    rc = ble_svc_gap_device_name_set(device_name);
+    assert(rc == 0);
+
+    /* As the last thing, process events from default event queue. */
+    while (1) {
+        os_eventq_run(os_eventq_dflt_get());
+    }
+
+    return 0;
+}