Skip to content

Commit

Permalink
Added doc
Browse files Browse the repository at this point in the history
  • Loading branch information
lupyuen committed Sep 30, 2019
1 parent a4704af commit 45c4987
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 56 deletions.
76 changes: 47 additions & 29 deletions apps/my_sensor_app/src/ble.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,48 @@
// Based on https://mynewt.apache.org/latest/tutorials/ble/ibeacon.html
/*
* 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.
*/
// Bluetooth LE Functions. Based on https://mynewt.apache.org/latest/tutorials/ble/ibeacon.html
#include "sysinit/sysinit.h"
#include "os/os.h"
#include "console/console.h"
#include "host/ble_hs.h"

static void
ble_app_set_addr(void)
{
static void ble_app_on_sync(void);
static void ble_app_set_addr(void);
static void ble_app_advertise(void);

int start_ble(void) {
// Set the callback for starting Bluetooth LE.
ble_hs_cfg.sync_cb = ble_app_on_sync;
return 0;
}

static void ble_app_on_sync(void) {
// Called upon starting Bluetooth LE.
// Generate a non-resolvable private address.
ble_app_set_addr();

// Advertise indefinitely as an iBeacon.
ble_app_advertise();
}

static void ble_app_set_addr(void) {
// Generate a non-resolvable private address.
ble_addr_t addr;
int rc;

Expand All @@ -17,41 +53,23 @@ ble_app_set_addr(void)
assert(rc == 0);
}

static void
ble_app_advertise(void)
{
static void ble_app_advertise(void) {
// Advertise indefinitely as an iBeacon.
struct ble_gap_adv_params adv_params;
uint8_t uuid128[16];
int rc;

/* Arbitrarily set the UUID to a string of 0x11 bytes. */
// Arbitrarily set the UUID to a string of 0x11 bytes.
memset(uuid128, 0x11, sizeof uuid128);

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

/* Begin advertising. */
// Begin advertising as an iBeacon.
adv_params = (struct ble_gap_adv_params){ 0 };
rc = ble_gap_adv_start(BLE_OWN_ADDR_RANDOM, NULL, BLE_HS_FOREVER,
&adv_params, NULL, NULL);
assert(rc == 0);
}

static void
ble_app_on_sync(void)
{
/* Generate a non-resolvable private address. */
ble_app_set_addr();

/* Advertise indefinitely. */
ble_app_advertise();
}

int
start_ble(void)
{
ble_hs_cfg.sync_cb = ble_app_on_sync;
return 0;
}
7 changes: 3 additions & 4 deletions rust/app/src/app_sensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/
//! Poll the temperature sensor every 10 seconds. Transmit the sensor data to the CoAP server after polling.
//! This is the Rust version of https://github.com/lupyuen/stm32bluepill-mynewt-sensor/blob/rust-nbiot/apps/my_sensor_app/OLDsrc/sensor.c
//! This is the Rust version of https://github.com/lupyuen/stm32bluepill-mynewt-sensor/blob/nrf52/apps/my_sensor_app/OLDsrc/sensor.c
use mynewt::{
result::*, // Import Mynewt API Result and Error types
Expand All @@ -35,8 +35,7 @@ use crate::app_network; // Import `app_network.rs` for send
/// Sensor to be polled: `temp_stm32_0` is the internal temperature sensor
//static SENSOR_DEVICE: Strn = init_strn!("temp_stm32_0");
static SENSOR_DEVICE: Strn = init_strn!("temp_stub_0");
/// Poll sensor every 19,000 milliseconds (19 seconds)
//const SENSOR_POLL_TIME: u32 = (19 * 1000);
/// Poll sensor every 10,000 milliseconds (10 seconds)
const SENSOR_POLL_TIME: u32 = (10 * 1000);
/// Use key (field name) `t` to transmit raw temperature to CoAP Server
const TEMP_SENSOR_KEY: Strn = init_strn!("t");
Expand All @@ -53,7 +52,7 @@ pub fn start_sensor_listener() -> MynewtResult<()> { // Returns an error code
.next() // Fetch the first sensor that matches
.expect("no TMP"); // Stop if no sensor found

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

// Create a sensor listener that will call function `aggregate_sensor_data` after polling the sensor data
Expand Down
8 changes: 3 additions & 5 deletions rust/app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,25 @@ use mynewt::{
/// main() will be called at Mynewt startup. It replaces the C version of the main() function.
#[no_mangle] // Don't mangle the name "main"
extern "C" fn main() -> ! { // Declare extern "C" because it will be called by Mynewt
// Initialise the Mynewt packages and Blue Pill internal temperature sensor driver.
// Start the CoAP / OIC Background Task to transmit CoAP messages. Any startup
// Initialise the Mynewt packages and internal temperature sensor driver. Any startup
// functions defined in pkg.yml of our custom drivers and libraries will be called by
// sysinit(). Here are the startup functions consolidated by Mynewt:
// bin/targets/bluepill_my_sensor/generated/src/bluepill_my_sensor-sysinit-app.c
// bin/targets/nrf52_my_sensor/generated/src/nrf52_my_sensor-sysinit-app.c
mynewt::sysinit();

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

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

// Start polling the GPS.
//gps_sensor::start_gps_listener()
//.expect("GPS fail");

// Start Bluetooth LE.
// Start Bluetooth LE. TODO: Create a safe wrapper for starting BLE.
extern { fn start_ble() -> i32; }
let rc = unsafe { start_ble() };
assert!(rc == 0, "BLE fail");
Expand Down
7 changes: 0 additions & 7 deletions scripts/nrf52/build-app.cmd

This file was deleted.

11 changes: 0 additions & 11 deletions scripts/nrf52/build-app.sh

This file was deleted.

0 comments on commit 45c4987

Please sign in to comment.