 parse_control_point_response = (response_code, data, cb)->
     expected_size = (size)->
         if data.length != size
-            throw "while parsing response (code=#{response_code}), the response expected to be #{size} in size, but was #{data.length}"
+            raise "while parsing response (code=#{response_code}), the response expected to be #{size} in size, but was #{data.length}"
 
     switch response_code
         when OPC_GET_VERSION

    console.log "  --device <mac>, -d <mac>     48 bit MAC address of the device to be flashed"
    console.log "  --version, -v                request version string from device"
    console.log "  --list, -l                   scan for a list of bootloaders"

if options.help
    print_usage()
    process.exit 0

if options.list
    console.log " mac              | version              | addr. size | page size | buffers "
    console.log "----------------------------------------------------------------------------"

    print_line = (mac, version, address_size, page_size, page_buffer)->
        console.log " #{left mac, 16} | #{left version, 20} | #{right address_size, 10} | #{right page_size, 9} | #{right page_buffer, 7}"

    scan_devices null, (devices)->
        number_of_devices = Object.keys(devices).length

        if number_of_devices == 0
            console.log "no devices found."
            process.exit 0

        wait_for = number_of_devices
        stop_waiting = ->
            wait_for = wait_for - 1
            process.exit 0 if wait_for == 0

        for id, device of devices

            connect_device device, (peripheral, error)->
                if error
                    console.log "#{device.address}: Error: #{error}"
                    stop_waiting()
                else
                    execute OPC_GET_VERSION, (error, version)->
                        if error
                            console.log "#{device.address} Error: #{error}"
                            stop_waiting()
                        else
                            execute OPC_GET_SIZES, (error, address_size, page_size, page_buffer)->
                                if error
                                    console.log "#{device.address} #{version} Error: #{error}"
                                else
                                    print_line device.address, version, address_size, page_size, page_buffer
                                    peripheral.disconnect()
                                    stop_waiting()

else if options.version
    scan_devices device_address(), (device)->
        if device
            connect_device device, (peripheral, error)->
                if error
                    console.log "#{device.address}: Error: #{error}"
                    process.exit 1
                else
                    execute OPC_GET_VERSION, (error, version)->
                        if error
                            console.log "error requesting version string: #{error}"
                            process.exit 1
                        else
                            console.log version
                            process.exit 0
        else
            console.log "device not found!"
            process.exit 1

else
    console.log "Unrecognized command."
    print_usage()
    process.exit 1

# if options.version
#     connect ->


        process.exit 1

    options['device']

start_address = ->
    if !options['address']
        console.log "start address (--address) required!"
        process.exit 1

    result = Number.parseInt options['address']

    if isNaN result
        console.log "argument for start address (--address #{options['address']}) is not a number"
        process.exit 1

    if result < 0
        console.log "argument for start address (--address #{options['address']}) is a negatic number"
        process.exit 1

    result

print_usage = ->
    console.log "usage: ble_flash [options] <input-file>"
