~ci-train-bot/usensord/usensord-ubuntu-vivid-landing-017

« back to all changes in this revision

Viewing changes to haptic/haptic.go

  • Committer: CI bot
  • Author(s): Sergio Schvezov
  • Date: 2014-07-01 15:15:18 UTC
  • mfrom: (21.1.1 usensord)
  • Revision ID: ps-jenkins@lists.canonical.com-20140701151518-s0utxnwnu7aul4ec
Returning a UnknownInterface dbus error when recving a message not on the haptic interface. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
)
44
44
 
45
45
func watchDBusMethodCalls(msgChan <-chan *dbus.Message) {
46
 
        var reply *dbus.Message
47
 
 
48
46
        for msg := range msgChan {
49
 
                switch {
50
 
                case msg.Interface == HAPTIC_DBUS_IFACE && msg.Member == "Vibrate":
51
 
                        var duration uint32
52
 
                        msg.Args(&duration)
53
 
                        logger.Printf("Received Vibrate() method call %d", duration)
54
 
                        if err := Vibrate(duration); err != nil {
55
 
                                reply = dbus.NewErrorMessage(msg, "com.canonical.usensord.Error", err.Error())
56
 
                        } else {
57
 
                                reply = dbus.NewMethodReturnMessage(msg)
58
 
                        }
59
 
                case msg.Interface == HAPTIC_DBUS_IFACE && msg.Member == "VibratePattern":
60
 
                        var pattern []uint32
61
 
                        var repeat uint32
62
 
                        msg.Args(&pattern, &repeat)
63
 
                        logger.Print("Received VibratePattern() method call ", pattern, " ", repeat)
64
 
                        if err := VibratePattern(pattern, repeat); err != nil {
65
 
                                reply = dbus.NewErrorMessage(msg, "com.canonical.usensord.Error", err.Error())
66
 
                        } else {
67
 
                                reply = dbus.NewMethodReturnMessage(msg)
68
 
                        }
69
 
                default:
70
 
                        logger.Println("Received unkown method call on", msg.Interface, msg.Member)
71
 
                        reply = dbus.NewErrorMessage(msg, "org.freedesktop.DBus.Error.UnknownMethod", "Unknown method")
 
47
                var reply *dbus.Message
 
48
 
 
49
                if msg.Interface == HAPTIC_DBUS_IFACE {
 
50
                        reply = handleHapticInterface(msg)
 
51
                } else {
 
52
                        reply = dbus.NewErrorMessage(
 
53
                                msg,
 
54
                                "org.freedesktop.DBus.Error.UnknownInterface",
 
55
                                fmt.Sprintf("No such interface '%s' at object path '%s'", msg.Interface, msg.Path))
72
56
                }
 
57
 
73
58
                if err := conn.Send(reply); err != nil {
74
59
                        logger.Println("Could not send reply:", err)
75
60
                }
76
61
        }
77
62
}
78
63
 
 
64
func handleHapticInterface(msg *dbus.Message) (reply *dbus.Message) {
 
65
        switch msg.Member {
 
66
        case "Vibrate":
 
67
                var duration uint32
 
68
                msg.Args(&duration)
 
69
                logger.Printf("Received Vibrate() method call %d", duration)
 
70
                if err := Vibrate(duration); err != nil {
 
71
                        reply = dbus.NewErrorMessage(msg, "com.canonical.usensord.Error", err.Error())
 
72
                } else {
 
73
                        reply = dbus.NewMethodReturnMessage(msg)
 
74
                }
 
75
        case "VibratePattern":
 
76
                var pattern []uint32
 
77
                var repeat uint32
 
78
                msg.Args(&pattern, &repeat)
 
79
                logger.Print("Received VibratePattern() method call ", pattern, " ", repeat)
 
80
                if err := VibratePattern(pattern, repeat); err != nil {
 
81
                        reply = dbus.NewErrorMessage(msg, "com.canonical.usensord.Error", err.Error())
 
82
                } else {
 
83
                        reply = dbus.NewMethodReturnMessage(msg)
 
84
                }
 
85
        default:
 
86
                logger.Println("Received unkown method call on", msg.Interface, msg.Member)
 
87
                reply = dbus.NewErrorMessage(msg, "org.freedesktop.DBus.Error.UnknownMethod", "Unknown method")
 
88
        }
 
89
        return reply
 
90
}
 
91
 
79
92
// Vibrate generates a vibration with the specified duration
80
93
// If the haptic device used to generate the vibration cannot be opened
81
94
// an error is returned in err.