Пример #1
0
func (p *Mpris) SendRequest(device *network.Device, playerList bool, volume bool, nowPlaying bool) error {
	return device.Send(MprisType, &MprisBody{
		RequestPlayerList: playerList,
		RequestVolume:     volume,
		RequestNowPlaying: nowPlaying,
	})
}
Пример #2
0
func (e *Engine) sendIdentity(device *network.Device) error {
	return device.Send(protocol.IdentityType, &protocol.Identity{
		DeviceId:        e.config.DeviceId,
		DeviceName:      e.config.DeviceName,
		ProtocolVersion: protocol.Version,
		DeviceType:      e.config.DeviceType,
		TcpPort:         e.config.TcpPort,
	})
}
Пример #3
0
func (e *Engine) PairDevice(device *network.Device) error {
	if device.Paired {
		return nil
	}

	if !device.PairRequestSent {
		pair := &protocol.Pair{
			Pair: true,
		}

		pub, err := e.config.PrivateKey.PublicKey().Marshal()
		if err != nil {
			return err
		}
		pair.PublicKey = string(pub)

		err = device.Send(protocol.PairType, pair)
		if err != nil {
			return err
		}

		log.Println("Sent pairing request to", device.Name)

		device.PairRequestSent = true
	}

	if device.PairRequestReceived && device.PairRequestSent {
		device.Paired = true

		log.Println("Device", device.Name, "paired")

		if e.getKnownDevice(device) == -1 {
			// Add it to the list of known devices
			e.config.KnownDevices = append(e.config.KnownDevices, NewKnownDeviceFromDevice(device))
		}

		select {
		case e.Paired <- device:
		default:
		}
	}

	return nil
}
Пример #4
0
func (e *Engine) UnpairDevice(device *network.Device) error {
	if device.Paired {
		err := device.Send(protocol.PairType, &protocol.Pair{
			Pair: false,
		})
		if err != nil {
			return err
		}

		device.Paired = false
	}

	select {
	case e.Unpaired <- device:
	default:
	}

	return nil
}
Пример #5
0
func (p *Ping) SendPing(device *network.Device) error {
	return device.Send(PingType, nil)
}
Пример #6
0
func setDeviceIdentity(device *network.Device, identity *protocol.Identity) {
	device.Id = identity.DeviceId
	device.Name = identity.DeviceName
	device.Type = identity.DeviceType
	device.ProtocolVersion = identity.ProtocolVersion
}
Пример #7
0
func (e *Engine) handleDevice(device *network.Device) {
	e.devices[device.Id] = device

	if i := e.getKnownDevice(device); i != -1 {
		device.Paired = true
		//device.PublicKey = e.config.KnownDevices[i].PublicKey
	}

	select {
	case e.Joins <- device:
	default:
	}

	go device.Listen()
	go e.sendIdentity(device)

	for pkg := range device.Incoming {
		if pkg == nil {
			continue
		}

		// Decrypt package if encrypted
		if pkg.Type == protocol.EncryptedType {
			var err error
			pkg, err = pkg.Body.(*protocol.Encrypted).Decrypt(e.config.PrivateKey)
			if err != nil {
				log.Println("Cannot decrypt package:", err)
				continue
			}
		}

		if pkg.Type == protocol.PairType {
			pair := pkg.Body.(*protocol.Pair)

			if pair.Pair {
				// Remote asks pairing
				device.PairRequestReceived = true

				if len(pair.PublicKey) > 0 {
					// Remote sent its public key
					pub := &crypto.PublicKey{}

					if err := pub.Unmarshal([]byte(pair.PublicKey)); err != nil {
						log.Println("Cannot parse public key:", err)
						break
					}
					log.Println("Received public key")

					device.PublicKey = pub
				}

				if device.PairRequestSent {
					// We asked for pairing first, remote accepted
					log.Println("Device accepted pairing")

					e.PairDevice(device)
				} else {
					log.Println("Device requested pairing")

					select {
					case e.RequestsPairing <- device:
					default:
					}
				}
			} else {
				log.Println("Device requested unpairing")

				device.Paired = false
				e.UnpairDevice(device)
			}
		} else if pkg.Type == protocol.IdentityType {
			setDeviceIdentity(device, pkg.Body.(*protocol.Identity))
		} else {
			err := e.handler.Handle(device, pkg)
			if err != nil {
				log.Println("Error handling package:", err, pkg.Type, string(pkg.RawBody))
			}
		}
	}

	log.Println("Closed TCP connection")

	delete(e.devices, device.Id)

	select {
	case e.Leaves <- device:
	default:
	}
}
Пример #8
0
func (p *Mpris) SendSeek(device *network.Device, seek float64) error {
	return device.Send(MprisType, &MprisBody{
		Seek: seek,
	})
}
Пример #9
0
func (p *Mpris) SendSetPosition(device *network.Device, position float64) error {
	return device.Send(MprisType, &MprisBody{
		SetPosition: position,
	})
}
Пример #10
0
func (p *Mpris) SendSetVolume(device *network.Device, volume int) error {
	return device.Send(MprisType, &MprisBody{
		SetVolume: volume,
	})
}
Пример #11
0
func (p *Mpris) SendAction(device *network.Device, action string) error {
	return device.Send(MprisType, &MprisBody{Action: action})
}
Пример #12
0
func (p *Mpris) SendPlayerList(device *network.Device, playerList []string) error {
	return device.Send(MprisType, &MprisBody{PlayerList: playerList})
}
Пример #13
0
func (p *Battery) SendRequest(device *network.Device) error {
	return device.Send(BatteryType, &BatteryBody{Request: true})
}
Пример #14
0
func (p *Notification) SendRequest(device *network.Device) error {
	return device.Send(NotificationType, &NotificationBody{Request: true})
}
Пример #15
0
func (p *Sftp) SendStartBrowsing(device *network.Device) error {
	return device.Send(SftpType, &SftpBody{StartBrowsing: true})
}