## @file  Multiple_Connection_Slave_Role.py
## @brief This script implements a multiple connection scenario: one Bluetooth Low Energy (BLE) Master connected to up 8 BLE Slaves devices. This script implements the Slave role which configures a BLE device as Peripheral, Mode 2. Then it adds a service with a characteristic and it enters in discoverable mode. When Master connect to it, the characteristic notification is enabled and the Slave device continuously notifies its characteristic value.
## @li The script is implemented using the new BlueNRG GUI language scripts based on the BLE ACI APIs. 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 S4 Multiple_Connection_Slave_Role.py 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 Multiple_Connection_Slave_Role.py script location and select  it
##  @li On BlueNRG GUI, click on  Run Script tab for running the selected script
##  @li BLE Slave device adds a service with a characteristic and it enters in discoverable mode. When connected and after characteristic notification is enabled, the Slave device continuously notifies its characteristic value to the Master device.  
##
##  @li NOTE: Repeat previous steps in order to configure another BLE device as Slave (up to 8 connections are supported from a BLE Master device, Mode 3). 
##  @li NOTE: For configuring a BLE device as Master, Mode 3 (handling up to 8 connections), run the Multiple_Connection_Master_Role.py script (refer to related script documentation for running steps).
##
##  @section S41 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 S42 Supported BLE devices and stack versions
##
##  @li BlueNRG-1 stack v1.x
##  @li BlueNRG-MS stack v7.2x
##  @li BlueNRG stack v6.4x
##

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

## @cond DOXYGEN_SHOULD_SKIP_THIS

RESET()
SET_PUBLIC_ADDRESS()
ACI_GATT_INIT()
ACI_GAP_INIT(Role=PERIPHERAL)  #Peripheral Role
ACI_HAL_SET_TX_POWER_LEVEL(En_High_Power=1,PA_Level = 4)
 

#Add a Services
Status,ServHandle=ACI_GATT_ADD_SERVICE(Service_UUID_Type=0x01, Service_UUID_16=0xA001,Service_Type=1,Max_Attribute_Records=5)      
if Status !=0x00:
    ERROR('ACI_GATT_ADD_SERVICE CALL FAILED')    

#Add a Characteristic (Notify properties)
Status,CharHandle=ACI_GATT_ADD_CHAR(Service_Handle =ServHandle, Char_UUID_Type=0x01,Char_UUID_16= 0xA002,Char_Value_Length=20, Char_Properties=0x10, #Only Notification
                     Security_Permissions=0x00,   
                     GATT_Evt_Mask=0x01, Enc_Key_Size=0x07,Is_Variable=0x01)      
if Status !=0x00:
    ERROR('ACI_GATT_ADD_CHAR CALL FAILED')  

Status = HCI_LE_SET_SCAN_RESPONSE_DATA(Scan_Response_Data_Length = 0,Scan_Response_Data = 0) ## Set Scan Response to 0  
if Status!=0x00:
    ERROR('HCI_LE_SET_SCAN_RESPONSE_DATA CALL FAILED')

#Put in Discoverable Mode with Name = 'Test' = [0x08,0x74,0x65,0x73,0x74](Add prefix 0x08 to indicate the AD type Name)
Status =ACI_GAP_SET_DISCOVERABLE(Advertising_Type= 0x00,
                                 Advertising_Interval_Min=0x20,
                                 Advertising_Interval_Max=0x100,
                                 Local_Name_Length=0x05,
                                 Local_Name=[0x08,0x74,0x65,0x73,0x74],
                                 Slave_Conn_Interval_Min = 0x0006,
                                 Slave_Conn_Interval_Max = 0x0008) 
if Status !=0x00:
    ERROR('ACI_GAP_SET_DISCOVERABLE CALL FAILED')

#Wait Connection Complete event
event = WAIT_EVENT(HCI_LE_META_EVENT,Subevent_Code=HCI_LE_CONNECTION_COMPLETE_EVENT )
if event.get_param('Status').val==0x00:
    conn_handle= event.get_param('Connection_Handle').val

event = WAIT_EVENT(HCI_VENDOR_EVENT ) 

while(True):
   
   # Characteristic notification
   ACI_GATT_UPDATE_CHAR_VALUE(Service_Handle=ServHandle, Char_Handle=CharHandle, Val_Offset=0x00, Char_Value_Length = 0x01, Char_Value=0xaa )
   SLEEP(1) 
