// Set the CTS pin behaviour in a port configuration. The port must be
// opened for this operation. Call p.ApplyConfig() to apply the
// change.
func (p *Port) SetCTS(cts int) error {
	ccts := cts2c(cts)
	if err := errmsg(C.sp_set_config_cts(p.c, ccts)); err != nil {
		return err
	}
	return p.getConf()
}
// Apply port options.
func (p *Port) Apply(o *Options) (err error) {
	// get port config
	var conf *C.struct_sp_port_config
	if err = errmsg(C.sp_new_config(&conf)); err != nil {
		return
	}
	defer C.sp_free_config(conf)

	// set bit rate
	if o.BitRate != 0 {
		err = errmsg(C.sp_set_config_baudrate(conf, C.int(o.BitRate)))
		if err != nil {
			return
		}
	}

	// set data bits
	if o.DataBits != 0 {
		err = errmsg(C.sp_set_config_bits(conf, C.int(o.DataBits)))
		if err != nil {
			return
		}
	}

	// set stop bits
	if o.StopBits != 0 {
		err = errmsg(C.sp_set_config_stopbits(conf, C.int(o.StopBits)))
		if err != nil {
			return
		}
	}

	// set parity
	if o.Parity != 0 {
		cparity := parity2c(o.Parity)
		if err = errmsg(C.sp_set_config_parity(conf, cparity)); err != nil {
			return
		}
	}

	// set flow control
	if o.FlowControl != 0 {
		cfc, err := flow2c(o.FlowControl)
		if err != nil {
			return err
		}
		if err = errmsg(C.sp_set_config_flowcontrol(conf, cfc)); err != nil {
			return err
		}
	}

	// set RTS
	if o.RTS != 0 {
		crts := rts2c(o.RTS)
		if err = errmsg(C.sp_set_config_rts(conf, crts)); err != nil {
			return
		}
	}

	// set CTS
	if o.CTS != 0 {
		ccts := cts2c(o.CTS)
		if err = errmsg(C.sp_set_config_cts(conf, ccts)); err != nil {
			return
		}
	}

	// set DTR
	if o.DTR != 0 {
		cdtr := dtr2c(o.DTR)
		if err = errmsg(C.sp_set_config_dtr(conf, cdtr)); err != nil {
			return
		}
	}

	// set DSR
	if o.DSR != 0 {
		cdsr := dsr2c(o.DSR)
		if err = errmsg(C.sp_set_config_dsr(conf, cdsr)); err != nil {
			return
		}
	}

	// apply config
	if err = errmsg(C.sp_set_config(p.p, conf)); err != nil {
		return
	}

	// update local config
	return p.getConf()
}