Commit 7f850270 by Priit Ruberg

Upload New File

parent 3491700e
Showing with 582 additions and 0 deletions
/**
* Copyright (c) 2014 - 2021, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/** @file
*
* @defgroup ble_sdk_app_beacon_main main.c
* @{
* @ingroup ble_sdk_app_beacon
* @brief Beacon Transmitter Sample Application main file.
*
* This file contains the source code for an Beacon transmitter sample application.
*/
#include <stdbool.h>
#include <stdint.h>
#include "nordic_common.h"
#include "bsp.h"
#include "nrf_soc.h"
#include "nrf_sdh.h"
#include "nrf_sdh_ble.h"
#include "ble_advdata.h"
#include "app_timer.h"
#include "nrf_pwr_mgmt.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
//************ For UART functionality
#include <string.h>
#include "app_uart.h"
#include "app_fifo.h"
#include "app_util_platform.h"
#include "app_error.h"
#include "nrf_delay.h"
#if defined (UART_PRESENT)
#include "nrf_uart.h"
#endif
#if defined (UARTE_PRESENT)
#include "nrf_uarte.h"
#endif
#define DEVICE_NAME "Ruberg_BT_GPS_beacon" // How cool is that :D
//*****************************
#define APP_BLE_CONN_CFG_TAG 1 /**< A tag identifying the SoftDevice BLE configuration. */
#define NON_CONNECTABLE_ADV_INTERVAL MSEC_TO_UNITS(100, UNIT_0_625_MS) /**< The advertising interval for non-connectable advertisement (100 ms). This value can vary between 100ms to 10.24s). */
#define APP_BEACON_INFO_LENGTH 0x17 /**< Total length of information advertised by the Beacon. */
#define APP_ADV_DATA_LENGTH 0x15 /**< Length of manufacturer specific data in the advertisement. */
#define APP_DEVICE_TYPE 0x02 /**< 0x02 refers to Beacon. */
#define APP_MEASURED_RSSI 0xC3 /**< The Beacon's measured RSSI at 1 meter distance in dBm. */
#define APP_COMPANY_IDENTIFIER 0x0059 /**< Company identifier for Nordic Semiconductor ASA. as per www.bluetooth.org. */
#define APP_MAJOR_VALUE 0x01, 0x02 /**< Major value used to identify Beacons. */
#define APP_MINOR_VALUE 0x03, 0x04 /**< Minor value used to identify Beacons. */
#define APP_BEACON_UUID 0x01, 0x12, 0x23, 0x34, \
0x45, 0x56, 0x67, 0x78, \
0x89, 0x9a, 0xab, 0xbc, \
0xcd, 0xde, 0xef, 0xf0 /**< Proprietary UUID for Beacon. */
//********* Changed beacon info
#define APP_BEACON_INFO_LENGTH_GPS 0x17 //Change caused halt /**< Total length of information advertised by the Beacon. */
#define APP_ADV_DATA_LENGTH_GPS 0x25 //Defined in so many places.../**< Length of manufacturer specific data in the advertisement. */
#define APP_DEVICE_TYPE_GPS 0x02 /**< 0x02 refers to Beacon. */
//***********************
#define DEAD_BEEF 0xDEADBEEF /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */
#if defined(USE_UICR_FOR_MAJ_MIN_VALUES)
#define MAJ_VAL_OFFSET_IN_BEACON_INFO 18 /**< Position of the MSB of the Major Value in m_beacon_info array. */
#define UICR_ADDRESS 0x10001080 /**< Address of the UICR register used by this example. The major and minor versions to be encoded into the advertising data will be picked up from this location. */
#endif
//************ For UART functionality
#define UART_TX_BUF_SIZE 256 /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 256 /**< UART RX buffer size. */
//*****************************
static ble_gap_adv_params_t m_adv_params; /**< Parameters to be passed to the stack when starting advertising. */
static ble_gap_adv_params_t m_adv_params_init_gps; /**< Parameters to be passed to the stack when starting *****GPS****** advertising. */
static uint8_t m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET; /**< Advertising handle used to identify an advertising set. */
static uint8_t m_adv_handle_gps = BLE_GAP_ADV_SET_HANDLE_NOT_SET; /**< Advertising handle used to identify an ******GPS****** advertising set. */
static uint8_t m_enc_advdata[BLE_GAP_ADV_SET_DATA_SIZE_MAX]; /**< Buffer for storing an encoded advertising set. */
static uint8_t m_enc_srdata[BLE_GAP_ADV_SET_DATA_SIZE_MAX]; /**< Buffer for storing an encoded scan set. */
/**@brief Struct that contains pointers to the encoded ****GPS**** advertising data. */
static ble_gap_adv_data_t m_adv_data_gps = //Actually nothing changed in here, but let's keep it
{
.adv_data = //Advertisement data
{
.p_data = m_enc_advdata,
.len = BLE_GAP_ADV_SET_DATA_SIZE_MAX
},
.scan_rsp_data = //Response data
{
.p_data = m_enc_srdata,
.len = BLE_GAP_ADV_SET_DATA_SIZE_MAX
}
};
static uint8_t m_beacon_info_gps[APP_BEACON_INFO_LENGTH_GPS] =/**< Information advertised by the Beacon. */
{
APP_DEVICE_TYPE_GPS, // Manufacturer specific information. Specifies the device type in this
// implementation.
APP_ADV_DATA_LENGTH_GPS, // Manufacturer specific information. Specifies the length of the
// manufacturer specific data in this implementation.
};
/**@brief Callback function for asserts in the SoftDevice.
*
* @details This function will be called in case of an assert in the SoftDevice.
*
* @warning This handler is an example only and does not fit a final product. You need to analyze
* how your product is supposed to react in case of Assert.
* @warning On assert from the SoftDevice, the system can only recover on reset.
*
* @param[in] line_num Line number of the failing ASSERT call.
* @param[in] file_name File name of the failing ASSERT call.
*/
void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
{
app_error_handler(DEAD_BEEF, line_num, p_file_name);
}
/**@brief Function for initializing the Advertising the ****GPS****** functionality.
*
* @details Encodes the required advertising data and passes it to the stack.
* Also builds a structure to be passed to the stack when starting advertising.
*/
static void advertising_init_gps()
{
uint32_t err_code;
ble_advdata_t advdata_init_gps;
ble_advdata_t srdata_init_gps; // Scan respose to show meaningful name -> scanning is allowed
uint8_t flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
ble_advdata_manuf_data_t manuf_specific_data_init_gps;
manuf_specific_data_init_gps.company_identifier = 0x0000;
manuf_specific_data_init_gps.data.p_data = (uint8_t *) m_beacon_info_gps;
manuf_specific_data_init_gps.data.size = APP_BEACON_INFO_LENGTH_GPS;
// Build and set advertising data.
memset(&advdata_init_gps, 0, sizeof(advdata_init_gps));
advdata_init_gps.name_type = BLE_ADVDATA_NO_NAME;
advdata_init_gps.flags = flags;
advdata_init_gps.p_manuf_specific_data = &manuf_specific_data_init_gps;
// Build and set scan response data.
memset(&srdata_init_gps, 0, sizeof(srdata_init_gps));
srdata_init_gps.name_type = BLE_ADVDATA_FULL_NAME;
srdata_init_gps.p_manuf_specific_data = &manuf_specific_data_init_gps; //Let's use the response as well
// Initialize advertising parameters (used when starting advertising).
memset(&m_adv_params_init_gps, 0, sizeof(m_adv_params_init_gps));
// Alternative to below if more data is needed: BLE_GAP_ADV_TYPE_EXTENDED_NONCONNECTABLE_SCANNABLE_UNDIRECTED or not?
//or the original: BLE_GAP_ADV_TYPE_NONCONNECTABLE_SCANNABLE_UNDIRECTED
m_adv_params_init_gps.properties.type = BLE_GAP_ADV_TYPE_NONCONNECTABLE_SCANNABLE_UNDIRECTED; //Changed to scannable so that the name would be visible as a response
m_adv_params_init_gps.p_peer_addr = NULL; // Undirected advertisement.
m_adv_params_init_gps.filter_policy = BLE_GAP_ADV_FP_ANY;
m_adv_params_init_gps.interval = NON_CONNECTABLE_ADV_INTERVAL;
m_adv_params_init_gps.duration = 0; // Never time out.
err_code = ble_advdata_encode(&advdata_init_gps, m_adv_data_gps.adv_data.p_data, &m_adv_data_gps.adv_data.len);
APP_ERROR_CHECK(err_code);
err_code = ble_advdata_encode(&srdata_init_gps, m_adv_data_gps.scan_rsp_data.p_data, &m_adv_data_gps.scan_rsp_data.len);
APP_ERROR_CHECK(err_code);
err_code = sd_ble_gap_adv_set_configure(&m_adv_handle_gps, &m_adv_data_gps, &m_adv_params_init_gps);
APP_ERROR_CHECK(err_code);
}
/**@brief Function for starting advertising.
*/
static void advertising_start_gps(void)
{
ret_code_t err_code;
err_code = sd_ble_gap_adv_start(m_adv_handle_gps, APP_BLE_CONN_CFG_TAG);
APP_ERROR_CHECK(err_code);
err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
APP_ERROR_CHECK(err_code);
}
/**@brief Function for initializing the BLE stack.
*
* @details Initializes the SoftDevice and the BLE event interrupt.
*/
static void ble_stack_init(void)
{
ret_code_t err_code;
err_code = nrf_sdh_enable_request();
APP_ERROR_CHECK(err_code);
// Configure the BLE stack using the default settings.
// Fetch the start address of the application RAM.
uint32_t ram_start = 0;
err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
APP_ERROR_CHECK(err_code);
// Enable BLE stack.
err_code = nrf_sdh_ble_enable(&ram_start);
APP_ERROR_CHECK(err_code);
}
/**@brief Function for initializing logging. */
static void log_init(void)
{
ret_code_t err_code = NRF_LOG_INIT(NULL);
APP_ERROR_CHECK(err_code);
NRF_LOG_DEFAULT_BACKENDS_INIT();
}
/**@brief Function for initializing LEDs. */
static void leds_init(void)
{
ret_code_t err_code = bsp_init(BSP_INIT_LEDS, NULL);
APP_ERROR_CHECK(err_code);
}
/**@brief Function for initializing timers. */
static void timers_init(void)
{
ret_code_t err_code = app_timer_init();
APP_ERROR_CHECK(err_code);
}
/**@brief Function for initializing power management.
*/
static void power_management_init(void)
{
ret_code_t err_code;
err_code = nrf_pwr_mgmt_init();
APP_ERROR_CHECK(err_code);
}
/**@brief Function for handling the idle state (main loop).
*
* @details If there is no pending log operation, then sleep until next the next event occurs.
*/
static void idle_state_handle(void)
{
if (NRF_LOG_PROCESS() == false)
{
nrf_pwr_mgmt_run();
}
}
/**@brief Function for handling app_uart events.
*
* @details This function will receive a single character from the app_uart module and append it to
* a string. The string will be be sent over BLE when the last character received was a
* 'new line' '\n' (hex 0x0A) or if the string has reached the maximum data length.
*/
/**@snippet [Handling the data received over UART] */
void uart_event_handle(app_uart_evt_t * p_event)
{
//static uint8_t data_array[BLE_NUS_MAX_DATA_LEN]; //Original?
//static uint8_t data_array[BLE_GAP_ADV_SET_DATA_SIZE_MAX];
int max_array_size = 16;
uint8_t data_array[max_array_size];
static uint8_t index = 0;
uint32_t err_code;
switch (p_event->evt_type)
{
//case APP_UART_DATA_READY:
case APP_UART_COMMUNICATION_ERROR:
APP_ERROR_HANDLER(p_event->data.error_communication);
break;
case APP_UART_FIFO_ERROR:
APP_ERROR_HANDLER(p_event->data.error_code);
break;
default:
break;
}
}
/**@brief Function for initializing the UART module.
*/
/**@snippet [UART Initialization] */
static void uart_init(void)
{
uint32_t err_code;
NRF_LOG_INFO("UART init phase 1.");
app_uart_comm_params_t const comm_params =
{
.rx_pin_no = RX_PIN_NUMBER,
.tx_pin_no = TX_PIN_NUMBER,
//.rts_pin_no = RTS_PIN_NUMBER,
//.cts_pin_no = CTS_PIN_NUMBER,
.flow_control = APP_UART_FLOW_CONTROL_DISABLED, //was disabled
.use_parity = false,
#if defined (UART_PRESENT)
.baud_rate = NRF_UART_BAUDRATE_115200
#else
.baud_rate = NRF_UARTE_BAUDRATE_115200
#endif
};
NRF_LOG_INFO("UART init phase 2.");
APP_UART_FIFO_INIT(&comm_params,
UART_RX_BUF_SIZE,
UART_TX_BUF_SIZE,
uart_event_handle,
APP_IRQ_PRIORITY_LOWEST,
err_code);
NRF_LOG_INFO("UART init phase 3.");
APP_ERROR_CHECK(err_code);
}
/**@snippet [UART Initialization] */
/**@brief Function for initializing the Advertising functionality.
*
* @details Encodes the required advertising data and passes it to the stack.
* Also builds a structure to be passed to the stack when starting advertising.
*/
static void advertising_update(gps_longitude_decimal,gps_latitude_decimal,gps_h_long_decimal,gps_l_long_decimal,gps_h_lat_decimal,gps_l_lat_decimal)
{
uint32_t err_code;
uint8_t flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
uint32_t gps_longitude_decimal_copy;
gps_longitude_decimal_copy = gps_longitude_decimal;
//*********** Packet parameters for advertisement
// Convert GPS data to manufacturer array
const uint8_t number_of_decimals_gps = 8;
uint8_t longitude_array[number_of_decimals_gps];
uint8_t latitude_array[number_of_decimals_gps];
memset(longitude_array, 0, sizeof(longitude_array));
memset(latitude_array, 0, sizeof(latitude_array));
uint8_t gps_data_array[number_of_decimals_gps];
uint8_t gps_data_array_2[16];
//Yet another approach to skip the bit splitting
// I'll leave it for the future when decimals are needed on the output and not
/*
for (int a = 0; a < 8; a++){
gps_data_array_2[a] = (gps_longitude_decimal>>(a*4));
}
for (int a = 8; a < 15; a++){
gps_data_array_2[a] = (gps_latitude_decimal>>(a*4));
}
for (int a = 0; a <= 16; a++){
printf("\n pos %d data d %d\n",a, gps_data_array_2[a]);
}
*/
// This option works okay
/*
printf("\n %d\n", (gps_h_long_decimal));
printf("\n %d\n", (gps_l_long_decimal));
printf("\n %d\n", (gps_h_long_decimal&0xFF00)>>8);
printf("\n %d\n", (gps_h_long_decimal&0x00FF));
printf("\n %d\n", (gps_l_long_decimal&0xFF00)>>8);
printf("\n %d\n", (gps_l_long_decimal&0x00FF));
*/
// It works, a lot of bit splitting
gps_data_array[0] = (gps_h_long_decimal&0xFF00)>>8;
gps_data_array[1] = gps_h_long_decimal&0x00FF;
gps_data_array[2] = (gps_l_long_decimal&0xFF00)>>8;
gps_data_array[3] = gps_l_long_decimal&0x00FF;
gps_data_array[4] = (gps_h_lat_decimal&0xFF00)>>8;
gps_data_array[5] = gps_h_lat_decimal&0x00FF;
gps_data_array[6] = (gps_l_lat_decimal&0xFF00)>>8;
gps_data_array[7] = gps_l_lat_decimal&0x00FF;
/*
for (int a = 0; a <= number_of_decimals_gps; a++){
printf("\n pos %d data d %d\n",a, gps_data_array[a]);
}
*/
//gps_h_long_decimal,gps_l_long_decimal,gps_h_lat_decimal,gps_l_lat_decimal
/*
//Kinda works, extra zeroes in the output, cannot explain yet
for (int i = number_of_decimals_gps-1; i >= 0; i--) { // That poor -1 took a while...
longitude_array[i] = (uint8_t)gps_longitude_decimal_copy % 10;
latitude_array[i] = (uint8_t) gps_latitude_decimal % 10;
gps_data_array[1][i] = (uint8_t)gps_longitude_decimal_copy % 10;
gps_data_array[2][i] = (uint8_t) gps_latitude_decimal % 10;
gps_longitude_decimal_copy /= 10;
gps_latitude_decimal /= 10;
printf("The value of longitude %d\n", gps_longitude_decimal_copy);
printf("The value of longitude %d array %d\n", i, longitude_array[i]);
nrf_delay_ms(10) ;
}*/
ble_advdata_manuf_data_t longitude_specific_data;
longitude_specific_data.company_identifier = 0x0059;
longitude_specific_data.data.size = APP_BEACON_INFO_LENGTH_GPS; //original, somethings wrong with that!
longitude_specific_data.data.p_data = gps_data_array;
longitude_specific_data.data.size = sizeof(gps_data_array);
/*
printf("\nDebug1\n");
nrf_delay_ms(100) ;
*/
// ************* Build and set advertising data
ble_advdata_t advdata;
memset(&advdata, 0, sizeof(advdata));
advdata.name_type = BLE_ADVDATA_NO_NAME;
advdata.flags = flags;
advdata.p_manuf_specific_data = &longitude_specific_data;
err_code = ble_advdata_encode(&advdata, m_adv_data_gps.adv_data.p_data, &m_adv_data_gps.adv_data.len);
APP_ERROR_CHECK(err_code);
// *******Build and set scan response data.
ble_advdata_t srdata; // Scan respose to show meaningful name -> scanning is allowed
memset(&srdata, 0, sizeof(srdata));
srdata.name_type = BLE_ADVDATA_FULL_NAME;
err_code = ble_advdata_encode(&srdata, m_adv_data_gps.scan_rsp_data.p_data, &m_adv_data_gps.scan_rsp_data.len);
APP_ERROR_CHECK(err_code);
}
/**
* @brief Function for application main entry.
*/
int main(void)
{
// Initialize.
uart_init();
timers_init();
leds_init();
power_management_init();
ble_stack_init();
// For having a meaningful name from the response packet
ret_code_t err_code;
ble_gap_conn_sec_mode_t sec_mode;
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
err_code = sd_ble_gap_device_name_set(&sec_mode,
(const uint8_t *)DEVICE_NAME,
strlen(DEVICE_NAME));
APP_ERROR_CHECK(err_code);
advertising_init_gps();
advertising_start_gps();
nrf_delay_ms(100) ;
// Enter main loop.
for (;; )
{
//int value;
double gps_value_longitude, gps_value_latitude;
// Quick check for boundaries, let's hope the input is numeric and not strings or chards
do {
// Longitude - getting the value and splitting data to higher and lower parts by masking and shifting
printf("\nEnter longitude (-180 < x < 180):");
scanf("%lf", &gps_value_longitude);
printf("%lf", gps_value_longitude );
if (gps_value_longitude < -180 || gps_value_longitude > 180){
printf("\nValue out of bounds!");
nrf_delay_ms(10);
}
}while (!(gps_value_longitude > -180) || !(gps_value_longitude < 180));
int32_t gps_longitude_decimal = gps_value_longitude*100000; // Conversion to integer
int16_t gps_h_longitude_decimal = (gps_longitude_decimal & 0xFFFF0000)>>16;
int16_t gps_l_longitude_decimal = gps_longitude_decimal&0x0000FFFF;
// Quick check for boundaries, let's hope the input is numeric and not strings or chards
do {
// Latitude - getting the value and splitting data to higher and lower parts by masking and shifting
printf("\nEnter latitude (90 > y > -90): ");
scanf("%lf", &gps_value_latitude);
printf("%lf", gps_value_latitude );
if (gps_value_latitude < -90 || gps_value_latitude > 90){
printf("\nValue out of bounds!");
nrf_delay_ms(10);
}
} while (!(gps_value_latitude > -90) || !(gps_value_latitude < 90));
int32_t gps_latitude_decimal = gps_value_latitude*100000; // Conversion to integer
int16_t gps_h_latitude_decimal = (gps_latitude_decimal & 0xFFFF0000)>>16;
int16_t gps_l_latitude_decimal = gps_latitude_decimal&0x0000FFFF;
// What we got?
printf("\nYou entered %d, %d\n", gps_longitude_decimal, gps_latitude_decimal);
//printf("Let's find h bits %d\n", gps_h_longitude_decimal);
//printf("Let's find l bits %d\n", gps_l_longitude_decimal);
//printf("Testing if I can get the coordinate back %ld, %d\n", (gps_h_long_decimal<<16)&0xFFFF0000, gps_l_long_decimal);
//printf("Testing if I can get the coordinate back %ld\n", (gps_h_long_decimal<<16) +gps_l_long_decimal);
printf("Advertising update\n");
advertising_update(gps_longitude_decimal,gps_latitude_decimal,gps_h_longitude_decimal,gps_l_longitude_decimal,gps_h_latitude_decimal,gps_l_latitude_decimal);
}
}
/**
* @}
*/
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment