1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-05 10:39:25 +02:00

Update STFService. Now both the agent and the service use the same wire proto.

This commit is contained in:
Simo Kinnunen 2014-04-17 02:20:16 +09:00
parent d5c96dc50d
commit ce9733f5da
5 changed files with 152 additions and 106 deletions

View file

@ -161,18 +161,60 @@ module.exports = syrup.serial()
return callService(util.format("-a '%s'", apk.stopAction)) return callService(util.format("-a '%s'", apk.stopAction))
} }
function sendInputEvent(event) { function keyEvent(data) {
agent.writer.write(new apk.agentProto.InputEvent(event).encodeNB()) return runAgentCommand(
apk.wire.RequestType.KEYEVENT
, new apk.wire.KeyEventRequest(data)
)
}
function type(text) {
return runAgentCommand(
apk.wire.RequestType.TYPE
, new apk.wire.TypeRequest(text)
)
}
function paste(text) {
return setClipboard(text)
.then(function() {
keyEvent({
event: apk.wire.KeyEvent.PRESS
, keyCode: adb.Keycode.KEYCODE_V
, ctrlKey: true
})
})
}
function wake() {
return runAgentCommand(
apk.wire.RequestType.WAKE
, new apk.wire.WakeRequest()
)
}
function freezeRotation(rotation) {
return runAgentCommand(
apk.wire.RequestType.SET_ROTATION
, new apk.wire.SetRotationRequest(rotation, true)
)
}
function thawRotation() {
return runAgentCommand(
apk.wire.RequestType.SET_ROTATION
, new apk.wire.SetRotationRequest(0, false)
)
} }
function version() { function version() {
return runServiceCommand( return runServiceCommand(
apk.serviceProto.RequestType.VERSION apk.wire.RequestType.VERSION
, new apk.serviceProto.VersionRequest() , new apk.wire.VersionRequest()
) )
.timeout(10000) .timeout(10000)
.then(function(data) { .then(function(data) {
var response = apk.serviceProto.VersionResponse.decode(data) var response = apk.wire.VersionResponse.decode(data)
if (response.success) { if (response.success) {
return response.version return response.version
} }
@ -182,12 +224,12 @@ module.exports = syrup.serial()
function unlock() { function unlock() {
return runServiceCommand( return runServiceCommand(
apk.serviceProto.RequestType.SET_KEYGUARD_STATE apk.wire.RequestType.SET_KEYGUARD_STATE
, new apk.serviceProto.SetKeyguardStateRequest(false) , new apk.wire.SetKeyguardStateRequest(false)
) )
.timeout(10000) .timeout(10000)
.then(function(data) { .then(function(data) {
var response = apk.serviceProto.SetKeyguardStateResponse.decode(data) var response = apk.wire.SetKeyguardStateResponse.decode(data)
if (!response.success) { if (!response.success) {
throw new Error('Unable to unlock device') throw new Error('Unable to unlock device')
} }
@ -196,12 +238,12 @@ module.exports = syrup.serial()
function lock() { function lock() {
return runServiceCommand( return runServiceCommand(
apk.serviceProto.RequestType.SET_KEYGUARD_STATE apk.wire.RequestType.SET_KEYGUARD_STATE
, new apk.serviceProto.SetKeyguardStateRequest(true) , new apk.wire.SetKeyguardStateRequest(true)
) )
.timeout(10000) .timeout(10000)
.then(function(data) { .then(function(data) {
var response = apk.serviceProto.SetKeyguardStateResponse.decode(data) var response = apk.wire.SetKeyguardStateResponse.decode(data)
if (!response.success) { if (!response.success) {
throw new Error('Unable to lock device') throw new Error('Unable to lock device')
} }
@ -210,12 +252,12 @@ module.exports = syrup.serial()
function acquireWakeLock() { function acquireWakeLock() {
return runServiceCommand( return runServiceCommand(
apk.serviceProto.RequestType.SET_WAKE_LOCK apk.wire.RequestType.SET_WAKE_LOCK
, new apk.serviceProto.SetWakeLockRequest(true) , new apk.wire.SetWakeLockRequest(true)
) )
.timeout(10000) .timeout(10000)
.then(function(data) { .then(function(data) {
var response = apk.serviceProto.SetWakeLockResponse.decode(data) var response = apk.wire.SetWakeLockResponse.decode(data)
if (!response.success) { if (!response.success) {
throw new Error('Unable to acquire WakeLock') throw new Error('Unable to acquire WakeLock')
} }
@ -224,12 +266,12 @@ module.exports = syrup.serial()
function releaseWakeLock() { function releaseWakeLock() {
return runServiceCommand( return runServiceCommand(
apk.serviceProto.RequestType.SET_WAKE_LOCK apk.wire.RequestType.SET_WAKE_LOCK
, new apk.serviceProto.SetWakeLockRequest(false) , new apk.wire.SetWakeLockRequest(false)
) )
.timeout(10000) .timeout(10000)
.then(function(data) { .then(function(data) {
var response = apk.serviceProto.SetWakeLockResponse.decode(data) var response = apk.wire.SetWakeLockResponse.decode(data)
if (!response.success) { if (!response.success) {
throw new Error('Unable to release WakeLock') throw new Error('Unable to release WakeLock')
} }
@ -238,12 +280,12 @@ module.exports = syrup.serial()
function identity() { function identity() {
return runServiceCommand( return runServiceCommand(
apk.serviceProto.RequestType.IDENTIFY apk.wire.RequestType.IDENTIFY
, new apk.serviceProto.IdentifyRequest(options.serial) , new apk.wire.IdentifyRequest(options.serial)
) )
.timeout(10000) .timeout(10000)
.then(function(data) { .then(function(data) {
var response = apk.serviceProto.IdentifyResponse.decode(data) var response = apk.wire.IdentifyResponse.decode(data)
if (!response.success) { if (!response.success) {
throw new Error('Unable to identify device') throw new Error('Unable to identify device')
} }
@ -252,15 +294,15 @@ module.exports = syrup.serial()
function setClipboard(text) { function setClipboard(text) {
return runServiceCommand( return runServiceCommand(
apk.serviceProto.RequestType.SET_CLIPBOARD apk.wire.RequestType.SET_CLIPBOARD
, new apk.serviceProto.SetClipboardRequest( , new apk.wire.SetClipboardRequest(
apk.serviceProto.ClipboardType.TEXT apk.wire.ClipboardType.TEXT
, text , text
) )
) )
.timeout(10000) .timeout(10000)
.then(function(data) { .then(function(data) {
var response = apk.serviceProto.SetClipboardResponse.decode(data) var response = apk.wire.SetClipboardResponse.decode(data)
if (!response.success) { if (!response.success) {
throw new Error('Unable to set clipboard') throw new Error('Unable to set clipboard')
} }
@ -269,17 +311,17 @@ module.exports = syrup.serial()
function getClipboard() { function getClipboard() {
return runServiceCommand( return runServiceCommand(
apk.serviceProto.RequestType.GET_CLIPBOARD apk.wire.RequestType.GET_CLIPBOARD
, new apk.serviceProto.GetClipboardRequest( , new apk.wire.GetClipboardRequest(
apk.serviceProto.ClipboardType.TEXT apk.wire.ClipboardType.TEXT
) )
) )
.timeout(10000) .timeout(10000)
.then(function(data) { .then(function(data) {
var response = apk.serviceProto.GetClipboardResponse.decode(data) var response = apk.wire.GetClipboardResponse.decode(data)
if (response.success) { if (response.success) {
switch (response.type) { switch (response.type) {
case apk.serviceProto.ClipboardType.TEXT: case apk.wire.ClipboardType.TEXT:
return response.text return response.text
} }
} }
@ -289,12 +331,12 @@ module.exports = syrup.serial()
function getBrowsers() { function getBrowsers() {
return runServiceCommand( return runServiceCommand(
apk.serviceProto.RequestType.GET_BROWSERS apk.wire.RequestType.GET_BROWSERS
, new apk.serviceProto.GetBrowsersRequest() , new apk.wire.GetBrowsersRequest()
) )
.timeout(15000) .timeout(15000)
.then(function(data) { .then(function(data) {
var response = apk.serviceProto.GetBrowsersResponse.decode(data) var response = apk.wire.GetBrowsersResponse.decode(data)
if (response.success) { if (response.success) {
delete response.success delete response.success
return response return response
@ -305,12 +347,12 @@ module.exports = syrup.serial()
function getProperties(properties) { function getProperties(properties) {
return runServiceCommand( return runServiceCommand(
apk.serviceProto.RequestType.GET_PROPERTIES apk.wire.RequestType.GET_PROPERTIES
, new apk.serviceProto.GetPropertiesRequest(properties) , new apk.wire.GetPropertiesRequest(properties)
) )
.timeout(15000) .timeout(15000)
.then(function(data) { .then(function(data) {
var response = apk.serviceProto.GetPropertiesResponse.decode(data) var response = apk.wire.GetPropertiesResponse.decode(data)
if (response.success) { if (response.success) {
var mapped = Object.create(null) var mapped = Object.create(null)
response.properties.forEach(function(property) { response.properties.forEach(function(property) {
@ -324,7 +366,7 @@ module.exports = syrup.serial()
function runServiceCommand(type, cmd) { function runServiceCommand(type, cmd) {
var resolver = Promise.defer() var resolver = Promise.defer()
service.writer.write(new apk.serviceProto.RequestEnvelope( service.writer.write(new apk.wire.RequestEnvelope(
type type
, cmd.encodeNB() , cmd.encodeNB()
).encodeNB()) ).encodeNB())
@ -332,6 +374,13 @@ module.exports = syrup.serial()
return resolver.promise return resolver.promise
} }
function runAgentCommand(type, cmd) {
agent.writer.write(new apk.wire.RequestEnvelope(
type
, cmd.encodeNB()
).encodeNB())
}
return openAgent() return openAgent()
.then(openService) .then(openService)
.then(function() { .then(function() {
@ -345,58 +394,47 @@ module.exports = syrup.serial()
]) ])
}) })
.on(wire.KeyDownMessage, function(channel, message) { .on(wire.KeyDownMessage, function(channel, message) {
sendInputEvent({ keyEvent({
action: apk.agentProto.InputAction.KEYDOWN event: apk.wire.KeyEvent.DOWN
, keyCode: keyutil.unwire(message.keyCode) , keyCode: keyutil.unwire(message.keyCode)
}) })
}) })
.on(wire.KeyUpMessage, function(channel, message) { .on(wire.KeyUpMessage, function(channel, message) {
sendInputEvent({ keyEvent({
action: apk.agentProto.InputAction.KEYUP event: apk.wire.KeyEvent.UP
, keyCode: keyutil.unwire(message.keyCode) , keyCode: keyutil.unwire(message.keyCode)
}) })
}) })
.on(wire.KeyPressMessage, function(channel, message) { .on(wire.KeyPressMessage, function(channel, message) {
sendInputEvent({ keyEvent({
action: apk.agentProto.InputAction.KEYPRESS event: apk.wire.KeyEvent.PRESS
, keyCode: keyutil.unwire(message.keyCode) , keyCode: keyutil.unwire(message.keyCode)
}) })
}) })
.on(wire.TypeMessage, function(channel, message) { .on(wire.TypeMessage, function(channel, message) {
sendInputEvent({ type(message.text)
action: apk.agentProto.InputAction.TYPE
, keyCode: 0
, text: message.text
})
}) })
.on(wire.RotateMessage, function(channel, message) { .on(wire.RotateMessage, function(channel, message) {
sendInputEvent({ if (message.rotation >= 0) {
action: message.rotation < 0 freezeRotation(message.rotation)
? apk.agentProto.InputAction.THAW_ROTATION }
: apk.agentProto.InputAction.FREEZE_ROTATION else {
, rotation: message.rotation thawRotation()
}) }
}) })
return { return {
unlock: unlock acquireWakeLock: acquireWakeLock
, lock: lock
, acquireWakeLock: acquireWakeLock
, releaseWakeLock: releaseWakeLock
, identity: identity
, paste: function(text) {
return setClipboard(text)
.then(function() {
sendInputEvent({
action: apk.agentProto.InputAction.KEYPRESS
, keyCode: adb.Keycode.KEYCODE_V
, ctrlKey: true
})
})
}
, copy: getClipboard , copy: getClipboard
, getBrowsers: getBrowsers , getBrowsers: getBrowsers
, getProperties: getProperties , getProperties: getProperties
, identity: identity
, lock: lock
, paste: paste
, releaseWakeLock: releaseWakeLock
, unlock: unlock
, version: version
, wake: wake
} }
}) })
}) })

View file

@ -15,16 +15,13 @@ module.exports = syrup.serial()
var log = logger.createLogger('device:resources:service') var log = logger.createLogger('device:resources:service')
var resource = { var resource = {
requiredVersion: '0.4.1' requiredVersion: '0.5.0'
, pkg: 'jp.co.cyberagent.stf' , pkg: 'jp.co.cyberagent.stf'
, main: 'jp.co.cyberagent.stf.Agent' , main: 'jp.co.cyberagent.stf.Agent'
, apk: pathutil.vendor('STFService/STFService.apk') , apk: pathutil.vendor('STFService/STFService.apk')
, agentProto: ProtoBuf.loadProtoFile( , wire: ProtoBuf.loadProtoFile(
pathutil.vendor('STFService/proto/agent.proto') pathutil.vendor('STFService/wire.proto')
).build().jp.co.cyberagent.stf.proto ).build().jp.co.cyberagent.stf
, serviceProto: ProtoBuf.loadProtoFile(
pathutil.vendor('STFService/proto/service.proto')
).build().jp.co.cyberagent.stf.proto
, startAction: 'jp.co.cyberagent.stf.ACTION_START' , startAction: 'jp.co.cyberagent.stf.ACTION_START'
, stopAction: 'jp.co.cyberagent.stf.ACTION_STOP' , stopAction: 'jp.co.cyberagent.stf.ACTION_STOP'
} }

Binary file not shown.

View file

@ -1,29 +0,0 @@
package jp.co.cyberagent.stf.proto;
option java_outer_classname = "AgentProto";
enum InputAction {
KEYDOWN = 0;
KEYUP = 1;
KEYPRESS = 2;
TYPE = 3;
WAKE = 4;
FREEZE_ROTATION = 5;
THAW_ROTATION = 6;
}
message InputEvent {
required InputAction action = 1;
optional int32 keyCode = 2;
optional bool shiftKey = 3;
optional bool ctrlKey = 4;
optional bool altKey = 5;
optional bool metaKey = 6;
optional bool symKey = 7;
optional bool functionKey = 8;
optional bool capsLockKey = 9;
optional bool scrollLockKey = 10;
optional bool numLockKey = 11;
optional string text = 12;
optional int32 rotation = 13;
}

View file

@ -1,6 +1,6 @@
package jp.co.cyberagent.stf.proto; package jp.co.cyberagent.stf;
option java_outer_classname = "ServiceProto"; option java_outer_classname = "Wire";
enum RequestType { enum RequestType {
VERSION = 0; VERSION = 0;
@ -11,6 +11,10 @@ enum RequestType {
GET_BROWSERS = 5; GET_BROWSERS = 5;
GET_PROPERTIES = 6; GET_PROPERTIES = 6;
IDENTIFY = 7; IDENTIFY = 7;
KEYEVENT = 8;
TYPE = 9;
WAKE = 10;
SET_ROTATION = 11;
} }
message RequestEnvelope { message RequestEnvelope {
@ -18,6 +22,8 @@ message RequestEnvelope {
required bytes request = 2; required bytes request = 2;
} }
// Service
message VersionRequest { message VersionRequest {
} }
@ -102,3 +108,37 @@ message IdentifyRequest {
message IdentifyResponse { message IdentifyResponse {
required bool success = 1; required bool success = 1;
} }
// Agent
enum KeyEvent {
DOWN = 0;
UP = 1;
PRESS = 2;
}
message KeyEventRequest {
required KeyEvent event = 1;
required int32 keyCode = 2;
optional bool shiftKey = 3;
optional bool ctrlKey = 4;
optional bool altKey = 5;
optional bool metaKey = 6;
optional bool symKey = 7;
optional bool functionKey = 8;
optional bool capsLockKey = 9;
optional bool scrollLockKey = 10;
optional bool numLockKey = 11;
}
message TypeRequest {
required string text = 1;
}
message SetRotationRequest {
required int32 rotation = 1;
required bool lock = 2;
}
message WakeRequest {
}