46
65
HAPTIC_DBUS_IFACE = "com.canonical.usensord.haptic"
47
66
HAPTIC_DEVICE = "/sys/class/timed_output/vibrator/enable"
67
PROP_DBUS_IFACE = "org.freedesktop.DBus.Properties"
68
OSK_PROCESS_NAME = "/usr/bin/maliit-server"
69
UNCONFINED_PROFILE = "unconfined"
50
72
func watchDBusMethodCalls(msgChan <-chan *dbus.Message) {
51
73
for msg := range msgChan {
74
logger.Println("msg sender", msg.Sender)
52
75
var reply *dbus.Message
54
77
if msg.Interface == HAPTIC_DBUS_IFACE {
55
78
reply = handleHapticInterface(msg)
79
} else if msg.Interface == PROP_DBUS_IFACE {
80
reply = handlePropInterface(msg)
57
82
reply = dbus.NewErrorMessage(
59
84
"org.freedesktop.DBus.Error.UnknownInterface",
94
func handlePropInterface(msg *dbus.Message) (reply *dbus.Message) {
97
var iname, pname string
98
msg.Args(&iname, &pname)
99
if iname == HAPTIC_DBUS_IFACE && pname == "OtherVibrate" {
100
reply = dbus.NewMethodReturnMessage(msg)
101
reply.AppendArgs(dbus.Variant{uint32(pvalue)})
103
reply = dbus.NewErrorMessage(msg, "com.canonical.usensord.Error", "interface or property not correct")
108
if iname == HAPTIC_DBUS_IFACE {
109
reply = dbus.NewMethodReturnMessage(msg)
110
reply.AppendArgs(dbus.Variant{uint32(pvalue)})
112
reply = dbus.NewErrorMessage(msg, "com.canonical.usensord.Error", "interface or property not correct")
115
var iname, pname string
116
msg.Args(&iname, &pname, &pvalue)
117
if iname == HAPTIC_DBUS_IFACE && pname == "OtherVibrate" && (pvalue == 1 || pvalue == 0) {
118
//save the property value
119
prop := Prop{OtherVibrate: pvalue,}
120
propJson, _ := json.Marshal(prop)
121
errwrite := ioutil.WriteFile(configFile, propJson, 0644)
123
logger.Println("WriteFile error:", errwrite)
126
//reply = dbus.NewSignalMessage("/com/canonical/usensord/haptic", HAPTIC_DBUS_IFACE, "Set")
127
reply = dbus.NewMethodReturnMessage(msg)
128
reply.AppendArgs(dbus.Variant{uint32(pvalue)})
129
logger.Println("Set property to be ", pvalue)
131
reply = dbus.NewErrorMessage(msg, "com.canonical.usensord.Error", "interface or property not correct")
134
logger.Println("Received unknown method call on", msg.Interface, msg.Member)
135
reply = dbus.NewErrorMessage(msg, "org.freedesktop.DBus.Error.UnknownMethod", "Unknown method")
69
140
func handleHapticInterface(msg *dbus.Message) (reply *dbus.Message) {
141
messageBus := conn.Object("org.freedesktop.DBus", "/org/freedesktop/DBus")
142
processreply, err := messageBus.Call("org.freedesktop.DBus", "GetConnectionCredentials", msg.Sender)
144
reply = dbus.NewErrorMessage(msg, "com.canonical.usensord.Error", err.Error())
147
var credentials map[string]dbus.Variant
148
if err := processreply.Args(&credentials); err != nil {
149
reply = dbus.NewErrorMessage(msg, "com.canonical.usensord.Error", err.Error())
152
pid := credentials["ProcessID"].Value.(uint32)
153
logger.Printf("caller process id: %d", pid)
155
ret, error := C.aa_is_enabled()
157
logger.Println("aa_is_enabled")
158
label := credentials["LinuxSecurityLabel"].Value.([]interface{})
160
for _, f := range label {
161
bb = append(bb, f.(uint8))
163
profile = strings.TrimSpace(string(bb))
164
//LinuxSecurityLabel ends with null
165
profile = profile[:len(profile)-1]
166
logger.Println("caller process label:", profile)
168
logger.Println("aa_is_enabled failed:", error)
169
profile = UNCONFINED_PROFILE
172
if profile == UNCONFINED_PROFILE {
173
file := "/proc/" + strconv.FormatUint(uint64(pid), 10) + "/exe"
174
_, err := os.Lstat(file)
176
logger.Println("error while calling os.Lstat", err)
178
exe, erreadexe := os.Readlink(file)
179
if erreadexe != nil {
180
logger.Printf("fail to read %s with error:", file, erreadexe.Error())
182
pname := strings.TrimSpace(string(exe))
183
logger.Println("process name:", pname)
184
if pname == OSK_PROCESS_NAME {
186
logger.Println("OSK calling")
190
if !isOSK && pvalue == 0 {
191
logger.Println("not vibrate since not osk and pvalue is 0")
192
reply = dbus.NewMethodReturnMessage(msg)
70
195
switch msg.Member {
72
197
var duration uint32
181
306
powerd = sysbus.Object("com.canonical.powerd", "/com/canonical/powerd")
182
307
mutex = &sync.Mutex{}
308
//save and load the property value
309
u, err := user.Current()
310
configPath := path.Join(u.HomeDir, ".config", "usensord")
311
configFile = path.Join(u.HomeDir, ".config", "usensord", "prop.json")
312
log.Println("configFile:", configFile)
313
os.MkdirAll(configPath, 0755)
314
b, errread := ioutil.ReadFile(configFile)
317
prop := Prop{OtherVibrate: pvalue,}
318
propJson, _ := json.Marshal(prop)
319
errwrite := ioutil.WriteFile(configFile, propJson, 0644)
321
logger.Fatal("WriteFile error:", errwrite)
326
err := json.Unmarshal(b, &prop)
328
pvalue = prop.OtherVibrate
329
log.Println("pvalueb is", pvalue)
331
log.Println("err is", err)
184
336
ch := make(chan *dbus.Message)
185
337
go watchDBusMethodCalls(ch)