# 
# OEDL Script to call and execute rx_multi_receive application
# for OTA sample collection.
# 
# 


#
# Section name: defProperty
#
# Description
# defProperty is used to define experiment parameters that can be passed in from the command line.
# If the parameter is not specified on the command line then a default value is assigned.
#
# Format
# defProperty(parameter,  default value, Text description)
#  - parameter is a variable that can be used through out the experiement
#    It is like a global in this script.
#  - default value is assigned to the above paramter if the paramter is not
#    specified in the command line.
#  - In this field add a text description for the paramter.
#  
defProperty('duration_', 15,        "Application runtime in seconds")
defProperty('group', '--',              "your MERIF group color")

baseTopo = Topology['system:topo:group-' + property.group.to_s]


#
# Section name: defApplication
#
# Description
# defApplication defines the application and all the input arguments
# that we want to use in this experiment script.
#
# Format
# defApplication(app_reference, application name)
# - app_reference: symbolic name that can be used to reference
#   the application in other parts of the script
#
# Body contents (most important fields)
#
# a.path defines the complete pathname for the application.
#
# a.defProperty(var_reference, description, application input)
# - var_reference: the script's symbolic name give to the application's command line argument
# - description: is a description of the above command line arugment
# - application input: command line arguments / flags
#
defApplication('ref_rx_receive', 'rx_multi_receive') { |a|
  a.description = "UHD c++ application to read OTA samples from Software Defined Radio"
  a.path = "export LC_ALL=C;/root/RX_MULTI_RECEIVE/rx_multi_receive"

  a.defProperty('nsamps',   'Number of samples',                      '--nsamps')
  a.defProperty('freq',     'Center frequency',                       '--freq')
  a.defProperty('rate',     'Sample rate',                            '--rate')
  a.defProperty('gain',     'RX path gain',                           '--gain')
  a.defProperty('args',     'like this... addr0=10.10.23.5',          '--args')
  a.defProperty('subdev',   'sub device radio selection... A:0 B:0',  '--subdev')
  a.defProperty('channels', 'Channel list',                           '--channels')
  a.defProperty('prefix',   'File out prefix',                        '--prefix')
  a.defProperty('sync',     'sync',                                   '--sync')
  a.defProperty('secs',     'seconds into future',                    '--sec')

# additional properties added for OML collection
  a.defProperty('oml-id',      'sender id',         '--oml-id')
  a.defProperty('oml-domain',  'sq3 database file', '--oml-domain')
  a.defProperty('oml-collect', 'storage location',  '--oml-collect')
}


#
# Section name: defGroup
#
# Description
# defGroup defines the compute nodes that the above application will be running on.
#
# Format
# defGroup(node_reference, group of compute nodes)
# - node_reference: symbolic name to reference the group of nodes
#
# Body contents (most important fields)
#
# a.setProperty(var_reference, value)
# - var_reference: the script's symbolic name give to the application's command line argument
# - value:         assigns a value to command line argument.
#
defGroup('rx_sdr', baseTopo.getNodeByIndex(0).to_s) do |node|
  node.addApplication("ref_rx_receive") do |a|
    a.setProperty('nsamps', "1024000")   # Sticky note: no more than 3 zeros at the end
    a.setProperty('freq', "2440e6")
    a.setProperty('rate', "20e6")
    a.setProperty('gain', "22")
    a.setProperty('args', "addr0=192.168.10.2")
    a.setProperty('subdev', "A:0")
    a.setProperty('channels', "0")
    #a.setProperty('prefix', "/root/RX_MULTI_RECEIVE/x310_")
    a.setProperty('secs', "1.5")
    
    a.setProperty('oml-id', baseTopo.getNodeByIndex(0).to_s)
    a.setProperty('oml-domain', property.group.to_s)
    #a.setProperty('oml-domain', "rx_multi_samples")
    a.setProperty('oml-collect', "oml:3003")
  end
end

info "Using SDR on node: " + baseTopo.getNodeByIndex(0).to_s

onEvent(:ALL_UP_AND_INSTALLED) { |event|
  info "Preparing OML experiment"
  wait 1

  info "Running the experiment"
  group("rx_sdr").startApplications
  
  wait property.duration_

  #allGroups.exec("pkill rx_multi_receive")
  #allGroups.stopApplications

  info "Stopping the applications"
  Experiment.done
  exit
}
