## @file Simultaneous_Mode_Slave.py
## @brief This script is used as component for testing a simultaneously Master & Slave scenario on a BLE device supporting such feature. One Bluetooth Low Energy (BLE) device (Master&Slave) is configured as Central & Peripheral, with a service and one characteristic and it performs two connections procedures (as Central) for connecting, respectively, to two BLE Peripheral devices (Slave_A, Slave_B) which have defined the same service and characteristic. Then Master&Slave device  enables the characteristics notification on both of them. At this stage, Master&Slave device enters in discovery mode (acting as Peripheral)  and it waits for connection request coming from another  device configured as Central (Master). Once this connection is performed,  Master&Slave device receives characteristics notifications from both  Slave_A, Slave_B devices and it notifies these characteristics (as Peripheral)  to the  Master device which displays the related values.
## @li The script is implemented using the new BlueNRG GUI language scripts based on the  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 S9 Simultaneous_Mode_Slave.py script execution steps through BlueNRG GUI Script Engine
##
##  @li Open BlueNRG GUI
##  @li <b>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 Simultaneous_Mode_Slave.py script location and select it
##  @li On BlueNRG GUI, click on  Run Script tab for running the selected script
##  @li Slave_A device enters in discovery mode.
##  @li Repeat same steps for configuring a Slave_B device using same Simultaneous_Mode_Slave.py script
##
##  @section S91 Simultaneous_Mode_Master&Slave.py script execution steps through BlueNRG GUI Script Engine
##
##  @li Open BlueNRG GUI
##  @li  <b>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 Simultaneous_Mode_Master&Slave.py script location and select  it
##  @li On BlueNRG GUI, click on  Run Script tab for running the selected script
##  @li Master&Slave device is configured as Central & Peripheral, with a service and one characteristic and it performs two connections procedures (as Central) for connecting, respectively, to two  Peripheral devices (Slave_A, Slave_B)
## @li Then  Master&Slave device  enables the characteristics notification on both of Slave_A, Slave_B devices.
## @li Then  Master&Slave device  enables the characteristics notification on both of them. At this stage,  Master&Slave device enters in discovery mode (acting as Peripheral)  and it waits for connection request coming from another  device configured as Central (Master).
## @li At this stage,  Master&Slave device enters in discovery mode (acting as Peripheral)  and it waits for connection request coming from another  device configured as Central (Master) using the Simultaneous_Mode_Master.py
## @li Once this connection is performed,  Master&Slave receives characteristics notifications from both  Slave_A, Slave_B devices (as Central) and it notifies these characteristics (as Peripheral)  to the  Master device which displays the related values.
##
##  @section S92 Simultaneous_Mode_Master.py script execution steps through BlueNRG GUI Script Engine
##
##  @li Open BlueNRG GUI 
##  @li <b>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 Simultaneous_Mode_Master.py script location and select  it
##  @li On BlueNRG GUI, click on  Run Script tab for running the selected script
##  @li  Master device is configured as Central and it performs a connection procedure for connecting to the  Master&Slave device. 
##  @li Then  Master  device  enables the characteristic notification on  Master&Slave device.
##  @li At this stage,  Master receives and displays the received characteristics values with information sender identity (Slave_A or Slave_B).
##
##  @section S93 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)
## 
##  @section S94 Supported BLE devices and stack versions
##
##  @li BlueNRG-1 stack v1.x
##  @li BlueNRG-MS stack v7.2x
##

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

## @cond DOXYGEN_SHOULD_SKIP_THIS


## Slave device

RESET()
if (IS_BlueNRG()):
  ERROR('BlueNRG device is not supported from this script')

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

 
#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:
    ERROR('CONNECTION FAILED')


while(True):
   # Characteristic notification
   v=GET_RAND_KEY()
   PRINT ('Write Value: ' + str(v))
   ACI_GATT_UPDATE_CHAR_VALUE(Service_Handle=ServHandle, Char_Handle=CharHandle, Val_Offset=0x00, Char_Value_Length = 19, Char_Value=v)
   SLEEP(1) 

## @endcond
