## @file  BLE_Beacon.py
## @brief This script implements the BLE beacon feature that shows how to configure a Bluetooth Low Energy (BLE) device in order to advertise specific manufacturing data and allow another BLE device to know if it is in the range of the BLE beacon device. 
## @li The script is implemented using the BlueNRG GUI language scripts based on the BLE ACI APIs names. The BlueNRG GUI with script engine is delivered within BlueNRG GUI SW package.
## @li The script can also be run using the script launcher standalone utility available within BlueNRG GUI SW package.
## @li A PC running Windows Vista/7 operating systems (64 and 32 bit version) or Windows XP operating systems (32 bit edition), with available USB ports is required for installing the BlueNRG GUI SW package.
##
## <!-- Copyright 2016 by STMicroelectronics.  All rights reserved.       *80*-->
##
##  @section S1 BLE Beacon Manufacturing Payload fields used on advertising packets
##
##  @li SIG company identifier (default is 0x0030: STMicroelectronics. Refer to https://www.bluetooth.org/en-us/specification/assigned-numbers/company-identifiers)          
##  @li Beacon ID                         
##  @li Length of the remaining payload  
##  @li Location UUID (it is used to distinguish specific beacons from others)
##  @li Major number: Identifier for a group of beacons  (it is used to group a related set of beacons)                                             
##  @li Minor number: Identifier for a single beacon (it is used to identify a single beacon)                                      
##  @li Tx Power: 2's complement of the Tx power (it is used to establish how far you are from device) 
##
##  @section S11 BLE_Beacon script execution steps through BlueNRG GUI Script Engine
##
##  @li Open BlueNRG GUI 
##  @li <b>BlueNRG/BlueNRG-MS platform configuration steps</b>: <ol>
##  <li> Connect the selected platform to a PC USB port
##  <li> Put selected platform in DFU mode and download the "C:\Program Files (x86)\STMicroelectronics\BlueNRG GUI x.x.x\Firmware\STM32L1_prebuilt_images\BlueNRG_VCOM_x_x.hex" using the BlueNRG GUI, Tools, Flash Motherboard FW... utility </ol>
##  @li <b>BlueNRG-1 platform configuration steps</b>: <ol>
##  <li> Connect the selected platform to a PC USB port
##  <li> Download the "C:\Program Files (x86)\STMicroelectronics\BlueNRG GUI x.x.x\Firmware\BlueNRG1\DTM\DTM_UART_16MHz.hex or DTM_SPI.hex" file on the BlueNRG-1 platform using the BlueNRG Flasher utility available on  BlueNRG-1 DK SW package (STSW-BLUENRG1-DK) <ul>
##  <li> <b> NOTES</b>: SPI Network coprocessor configuration file (DTM_SPI.hex) requires some HW changes to be applied to the BlueNRG-1 development platform. Refer to STSW-BNRGUI user manual UM2058. </ul></ol>
##  @li On BlueNRG GUI, open the USB Port associated to the plugged platform
##  @li On BlueNRG GUI, select the Scripts window 
##  @li On BlueNRG GUI, Scripts window,  Scripts Engine section, click on ... tab and browse to the script location and select it
##  @li On BlueNRG GUI, click on  Run Script tab for running the selected script
##
##  @section S13 Supported platforms
##
##  @li BlueNRG-1 development platform (order code: STEVAL-IDB007V1)
##  @li BlueNRG-MS development platform (order code: STEVAL-IDB005V1) 
##  @li BlueNRG-MS Daughter Board (order code: STEVAL-IDB005V1D)
##  @li BlueNRG-MS USB dongle (order code: STEVAL-IDB006V1)
##  @li BlueNRG development platform (order code: STEVAL-IDB002V1)
##  @li BlueNRG USB dongle (order code: STEVAL-IDB003V1)
## 
##  @section S14 Supported BLE device and stack versions
##
##  @li BlueNRG-1 stack v1.x
##  @li BlueNRG-MS stack v7.2x
##  @li BlueNRG stack v6.4x
##

## \addtogroup BLE_scripts
## @{
## \see BLE_Beacon.py for documentation.
## 
## @} 

## @cond DOXYGEN_SHOULD_SKIP_THIS

# Set to 1 for enabling Flags AD Type position at the beginning of the advertising packet 
ENABLE_FLAGS_AD_TYPE_AT_BEGINNING = 1

# Reset BlueNRG/BlueNRG-MS 
RESET()
# Set Public Address
SET_PUBLIC_ADDRESS(0x123456789AAA)
# Init GATT 
if  ACI_GATT_INIT()!=0x00:
    ERROR('ACI_GATT_INIT FAILED')

# Init GAP as peripheral device
status,_,_,_=  ACI_GAP_INIT(Role=PERIPHERAL)
if status !=0x00:
    ERROR('ACI_GAP_INIT CALL FAILED')

#Set TX power level
ACI_HAL_SET_TX_POWER_LEVEL(En_High_Power=1,PA_Level = 4) 

# Put device in non-connectable mode
status = ACI_GAP_SET_DISCOVERABLE(Advertising_Type=0x03, Advertising_Interval_Min=160, Advertising_Interval_Max=160, Local_Name_Length = 0x0, Local_Name = 0x0)
if status !=0x00:
    ERROR('ACI_GAP_SET_DISCOVERABLE CALL FAILED')

# Define the beacon payload to be used within the advertising data
beaconPayload = [
        0x30, 0x00, # Company identifier code (Default is 0x0030 - STMicroelectronics: To be customized for specific identifier)
        0x02,       # ID
        0x15,       # Length of the remaining payload
        0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, # Location UUID
        0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61, ##  0x61
        0x00, 0x01, # Major number 
        0x00, 0x02, # Minor number
        0xC8        # 2's complement of the Tx power at 1 meter from device (0xC8=200, 2's complement 256-197=-56dBm)
    ]

lenData= len(beaconPayload)+1

        
#AD type 0xFF = Manufacturer Specific Data ->  2 or more octets
#Description = The first 2 octets contain the company Identifier Code followed by additional manufactures specific data 
data = [lenData , 0xFF ] + beaconPayload

if ENABLE_FLAGS_AD_TYPE_AT_BEGINNING:
    #Set the  ADV data with the Flags AD Type at beginning of the 
    #advertsing packet,  followed by the beacon manufacturer specific data */
    status =HCI_LE_SET_ADVERTISING_DATA(Advertising_Data_Length=len(data)+3,
                                    Advertising_Data= [0x02,0x01,0x06] + data)
                                    
    if status !=0x00:
        ERROR('HCI_LE_SET_ADVERTISING_DATA CALL FAILED')

else: 
    #Remove this AD type otherwise  beaconPayload exceed the maximum size
    status = ACI_GAP_DELETE_AD_TYPE (ADType=0x0A)
    if status !=0x00:
         ERROR('ACI_GAP_DELETE_AD_TYPE CALL FAILED')

     # Update advertising data 
    status = ACI_GAP_UPDATE_ADV_DATA( AdvDataLen=len(data), AdvData= data)
    if status !=0x00:
        ERROR('ACI_GAP_UPDATE_ADV_DATA CALL FAILED')
## @endcond





