Exemplo n.º 1
0
func newConn(cp config.NewtmgrConnProfile, readTimeout time.Duration) (Conn, error) {
	// Based on ConnProfile, instantiate the right type of conn object, that
	// implements the conn interface.
	var c Conn
	switch cp.Type() {
	case "serial":
		c = &ConnSerial{}
		c.SetOICEncoded(false)
	case "oic_serial":
		c = &ConnSerial{}
		c.SetOICEncoded(true)
	case "ble":
		c = &ConnBLE{}
		c.SetOICEncoded(false)
	case "oic_ble":
		c = &ConnBLE{}
		c.SetOICEncoded(true)
	case "udp":
		c = &ConnUDP{}
		c.SetOICEncoded(false)
	case "oic_udp":
		c = &ConnUDP{}
		c.SetOICEncoded(true)
	default:
		return nil, util.NewNewtError("Invalid conn profile " + cp.Type() +
			" not implemented")
	}

	if err := c.Open(cp, readTimeout); err != nil {
		return nil, err
	}
	return c, nil
}
Exemplo n.º 2
0
func (cs *ConnUDP) Open(cp config.NewtmgrConnProfile, readTimeout time.Duration) error {
	addr, err := net.ResolveUDPAddr("udp", cp.ConnString())
	if err != nil {
		return util.NewNewtError(fmt.Sprintf("Name not resolving: %s",
			err.Error()))
	}
	cs.dst = addr

	// bind local endpoint to wait for response afterwards
	s, err := net.ListenUDP("udp", nil)
	if err != nil {
		return util.NewNewtError(fmt.Sprintf("UDP conn failed: %s\n",
			err.Error()))
	}
	cs.conn = s
	return nil
}
Exemplo n.º 3
0
func (cs *ConnSerial) Open(cp config.NewtmgrConnProfile, readTimeout time.Duration) error {

	c, err := newSerialConfig(cp.ConnString(), readTimeout)
	if err != nil {
		return err
	}

	cs.serialChannel, err = serial.OpenPort(c)
	if err != nil {
		return util.ChildNewtError(err)
	}
	//defer cs.serialChannel.Close()

	// Most of the reading will be done line by line, use the
	// bufio.Scanner to do this
	cs.scanner = bufio.NewScanner(cs.serialChannel)

	return nil
}
Exemplo n.º 4
0
func (cb *ConnBLE) Open(cp config.NewtmgrConnProfile, readTimeout time.Duration) error {
	var err error

	var DefaultClientOptions = BleOptions

	deviceName = cp.ConnString()
	deviceAddress = reverseBytes(cp.DeviceAddress())
	log.Debugf("BLE Connection devaddr:%+v", deviceAddress)
	deviceAddressType = cp.DeviceAddressType()
	cb.bleDevice, err = gatt.NewDevice(DefaultClientOptions...)
	if err != nil {
		return util.NewNewtError(err.Error())
	}

	cb.bleDevice.Handle(
		gatt.PeripheralDiscovered(onPeriphDiscovered),
		gatt.PeripheralConnected(onPeriphConnected),
		gatt.PeripheralDisconnected(onPeriphDisconnected),
	)
	cb.bleDevice.Init(onStateChanged)
	CharDisc <- true

	return nil
}