Skip to content

Commit 45c4987

Browse files
committed
Added doc
1 parent a4704af commit 45c4987

File tree

5 files changed

+53
-56
lines changed

5 files changed

+53
-56
lines changed

apps/my_sensor_app/src/ble.c

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,48 @@
1-
// Based on https://mynewt.apache.org/latest/tutorials/ble/ibeacon.html
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
// Bluetooth LE Functions. Based on https://mynewt.apache.org/latest/tutorials/ble/ibeacon.html
220
#include "sysinit/sysinit.h"
321
#include "os/os.h"
422
#include "console/console.h"
523
#include "host/ble_hs.h"
624

7-
static void
8-
ble_app_set_addr(void)
9-
{
25+
static void ble_app_on_sync(void);
26+
static void ble_app_set_addr(void);
27+
static void ble_app_advertise(void);
28+
29+
int start_ble(void) {
30+
// Set the callback for starting Bluetooth LE.
31+
ble_hs_cfg.sync_cb = ble_app_on_sync;
32+
return 0;
33+
}
34+
35+
static void ble_app_on_sync(void) {
36+
// Called upon starting Bluetooth LE.
37+
// Generate a non-resolvable private address.
38+
ble_app_set_addr();
39+
40+
// Advertise indefinitely as an iBeacon.
41+
ble_app_advertise();
42+
}
43+
44+
static void ble_app_set_addr(void) {
45+
// Generate a non-resolvable private address.
1046
ble_addr_t addr;
1147
int rc;
1248

@@ -17,41 +53,23 @@ ble_app_set_addr(void)
1753
assert(rc == 0);
1854
}
1955

20-
static void
21-
ble_app_advertise(void)
22-
{
56+
static void ble_app_advertise(void) {
57+
// Advertise indefinitely as an iBeacon.
2358
struct ble_gap_adv_params adv_params;
2459
uint8_t uuid128[16];
2560
int rc;
2661

27-
/* Arbitrarily set the UUID to a string of 0x11 bytes. */
62+
// Arbitrarily set the UUID to a string of 0x11 bytes.
2863
memset(uuid128, 0x11, sizeof uuid128);
2964

30-
/* Major version=2; minor version=10. */
31-
// Measured Power ranging data (Calibrated tx power at 1 meters). Must be > -126 and < 20
32-
rc = ble_ibeacon_set_adv_data(uuid128, 2, 10, -60); // TODO: Confirm RSSI
65+
// Set iBeacon parameters: Major version=2; minor version=10; RSSI=-60.
66+
// RSSI is the Measured Power ranging data (Calibrated tx power at 1 meters). Must be > -126 and < 20.
67+
rc = ble_ibeacon_set_adv_data(uuid128, 2, 10, -60); // TODO: Verify RSSI for your device.
3368
assert(rc == 0);
3469

35-
/* Begin advertising. */
70+
// Begin advertising as an iBeacon.
3671
adv_params = (struct ble_gap_adv_params){ 0 };
3772
rc = ble_gap_adv_start(BLE_OWN_ADDR_RANDOM, NULL, BLE_HS_FOREVER,
3873
&adv_params, NULL, NULL);
3974
assert(rc == 0);
4075
}
41-
42-
static void
43-
ble_app_on_sync(void)
44-
{
45-
/* Generate a non-resolvable private address. */
46-
ble_app_set_addr();
47-
48-
/* Advertise indefinitely. */
49-
ble_app_advertise();
50-
}
51-
52-
int
53-
start_ble(void)
54-
{
55-
ble_hs_cfg.sync_cb = ble_app_on_sync;
56-
return 0;
57-
}

rust/app/src/app_sensor.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* under the License.
1818
*/
1919
//! Poll the temperature sensor every 10 seconds. Transmit the sensor data to the CoAP server after polling.
20-
//! This is the Rust version of https://github.com/lupyuen/stm32bluepill-mynewt-sensor/blob/rust-nbiot/apps/my_sensor_app/OLDsrc/sensor.c
20+
//! This is the Rust version of https://github.com/lupyuen/stm32bluepill-mynewt-sensor/blob/nrf52/apps/my_sensor_app/OLDsrc/sensor.c
2121
2222
use mynewt::{
2323
result::*, // Import Mynewt API Result and Error types
@@ -35,8 +35,7 @@ use crate::app_network; // Import `app_network.rs` for send
3535
/// Sensor to be polled: `temp_stm32_0` is the internal temperature sensor
3636
//static SENSOR_DEVICE: Strn = init_strn!("temp_stm32_0");
3737
static SENSOR_DEVICE: Strn = init_strn!("temp_stub_0");
38-
/// Poll sensor every 19,000 milliseconds (19 seconds)
39-
//const SENSOR_POLL_TIME: u32 = (19 * 1000);
38+
/// Poll sensor every 10,000 milliseconds (10 seconds)
4039
const SENSOR_POLL_TIME: u32 = (10 * 1000);
4140
/// Use key (field name) `t` to transmit raw temperature to CoAP Server
4241
const TEMP_SENSOR_KEY: Strn = init_strn!("t");
@@ -53,7 +52,7 @@ pub fn start_sensor_listener() -> MynewtResult<()> { // Returns an error code
5352
.next() // Fetch the first sensor that matches
5453
.expect("no TMP"); // Stop if no sensor found
5554

56-
// At power on, we ask Mynewt to poll our temperature sensor every 19 seconds.
55+
// At power on, we ask Mynewt to poll our temperature sensor every 10 seconds.
5756
sensor::set_poll_rate_ms(&SENSOR_DEVICE, SENSOR_POLL_TIME) ? ;
5857

5958
// Create a sensor listener that will call function `aggregate_sensor_data` after polling the sensor data

rust/app/src/lib.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,25 @@ use mynewt::{
4848
/// main() will be called at Mynewt startup. It replaces the C version of the main() function.
4949
#[no_mangle] // Don't mangle the name "main"
5050
extern "C" fn main() -> ! { // Declare extern "C" because it will be called by Mynewt
51-
// Initialise the Mynewt packages and Blue Pill internal temperature sensor driver.
52-
// Start the CoAP / OIC Background Task to transmit CoAP messages. Any startup
51+
// Initialise the Mynewt packages and internal temperature sensor driver. Any startup
5352
// functions defined in pkg.yml of our custom drivers and libraries will be called by
5453
// sysinit(). Here are the startup functions consolidated by Mynewt:
55-
// bin/targets/bluepill_my_sensor/generated/src/bluepill_my_sensor-sysinit-app.c
54+
// bin/targets/nrf52_my_sensor/generated/src/nrf52_my_sensor-sysinit-app.c
5655
mynewt::sysinit();
5756

5857
// Start the Server Transport for sending sensor data to CoAP Server over NB-IoT.
5958
//sensor_network::start_server_transport()
6059
//.expect("NET fail");
6160

6261
// Start polling the temperature sensor every 10 seconds in the background.
63-
// If this is a standby wakeup, the server transport must already be started.
6462
app_sensor::start_sensor_listener()
6563
.expect("TMP fail");
6664

6765
// Start polling the GPS.
6866
//gps_sensor::start_gps_listener()
6967
//.expect("GPS fail");
7068

71-
// Start Bluetooth LE.
69+
// Start Bluetooth LE. TODO: Create a safe wrapper for starting BLE.
7270
extern { fn start_ble() -> i32; }
7371
let rc = unsafe { start_ble() };
7472
assert!(rc == 0, "BLE fail");

scripts/nrf52/build-app.cmd

Lines changed: 0 additions & 7 deletions
This file was deleted.

scripts/nrf52/build-app.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)