aboutsummaryrefslogtreecommitdiff
path: root/mavigator-uav/src/main/scala/mavigator/uav/MavlinkUtil.scala
blob: 0fb56b06d721dcd97f4bbf21a3e2e6bf7673a6b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package mavigator.uav

import org.mavlink.Assembler
import org.mavlink.Packet
import org.mavlink.Parser
import org.mavlink.messages.Message

import akka.actor.Actor
import akka.actor.ActorLogging
import akka.util.ByteString

/** Provides utilities for actors representing a mavlink connection. */
trait MavlinkUtil { myself: Actor with ActorLogging =>

  /** Mavlink system ID of this connection. */
  val systemId: Byte

  /** Mavlink component ID of this connection. */
  val componentId: Byte

  /** Assembler for creating packets originating from this connection. */
  private lazy val assembler = new Assembler(systemId, componentId)

  /** Assembles a message into a bytestring representing a packet sent from this connection. */
  protected def assemble(message: Message): ByteString = {
    val (messageId, payload) = Message.pack(message)
    val packet: Packet = assembler.assemble(messageId, payload)
    ByteString(packet.toArray)
  }

  /** Parser for messages being sent to the uav. */
  protected val outgoing: Parser = new Parser(packet => Message.unpack(packet.messageId, packet.payload) match {
    //TODO handle ping
    /*
    case Ping(`systemId`, `componentId`) =>
      val message = Ack(packet.systemId, packet.componentId)
      val data = assemble(message)
      self ! Connection.Received(data)*/
    case _ => ()
  })

  /** Parser for messages coming from the uav. */
  protected val incoming: Parser = new Parser(pckt =>
    log.debug("incoming message: " + Message.unpack(pckt.messageId, pckt.payload)))

}