Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion packages/hap-debugger/src/adb/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,50 @@ class ADBModule {
this.currentDeviceMap.set(sn, currentDevice)
this.cachedDeviceMap.set(sn, currentDevice)
// 记录发送update http请求需要的ip和端口
const remote2local = {}
remote2local[currentDevice.upForwardPortPair[1]] = currentDevice.upForwardPortPair[0]
await this._writeClientLogFile({
sn,
ip: `127.0.0.1`,
port: currentDevice.upForwardPortPair[0]
port: currentDevice.upForwardPortPair[0],
remote2local
})
debuglog(`onDeviceAdded():(${sn}) end`)
// 增加设备连接检测
this.onCheckDeviceReverse(event)
}

async getForwardPort(client, remotePort) {
let remote2local = client.remote2local
if (!remote2local) return client.port

let port = remote2local[remotePort]
if (!port) {
// addForwardPort
port = await this.addForwardPort(client, remotePort)
}
return port
}

async addForwardPort(client, remotePort) {
const port = this._getNextLocalForwardPort()
debuglog(`addForwardPort():(${client.sn}) (local port: ${port}, remote port: ${remotePort}) start`)
const upForwardResult = await this.establishADBProxyLink(
'forward',
[client.sn].concat([port, remotePort])
)
if (upForwardResult.err) {
colorconsole.error(
`### App Server ### addForwardPort(): (${client.sn})建立adb forward失败(local port: ${port}, remote port: ${remotePort}) `
)
return
}
client.remote2local[remotePort] = port
await this._writeClientLogFile(client)
debuglog(`addForwardPort():(${client.sn}) (local port: ${port}, remote port: ${remotePort}) end`)
return port
}

/**
* 设备连接检测,用于检查当前窗口端口是否连接手机调试器
*/
Expand Down
25 changes: 16 additions & 9 deletions packages/hap-debugger/src/router/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,25 @@ export async function searchSn(context, next) {
const recordData = getRecords(clientRecordPath)
const clients = getProjectClients(recordData)
if (clients.length > 0) {
const remotePort = context.request.query.port
colorconsole.log('### App Loader ### 通知设备开始下发SN')
clients.forEach(function (client) {
for (const client of clients) {
// 匹配hap-toolkit-client-records.json表里已存的通过ADB连接的设备信息
if (client.ip === '127.0.0.1') {
// 仅向ADB现在连接的且没有SN的设备下发SN请求
getDeviceInfo(client, (err, data) => {
if (err || data.sn) {
return
}
callDeviceWithOwnSn(client)
})
if (remotePort) {
const port = await context.adbDebugger.getForwardPort(client, remotePort)
callDeviceWithOwnSn(client, port)
} else {
// 仅向ADB现在连接的且没有SN的设备下发SN请求
getDeviceInfo(client, (err, data) => {
if (err || data.sn) {
return
}
callDeviceWithOwnSn(client)
})
}
}
})
}
}
} else {
await next()
Expand All @@ -154,6 +160,7 @@ export async function startDebug(context, next) {
// ADB调试模式
if (linkMode === LINK_MODE.ADB) {
const { localWsPort, err } = await context.adbDebugger.forwardForWsChannel(sn, devicePort)
console.error(`startDebug(): adb forward 端口映射: sn=${sn},devicePort=${devicePort}`)
if (err) {
console.error(`startDebug(): adb forward 端口映射失败: ${err.message}`)
trackDebug(eventAlias.h_forward_err)
Expand Down
9 changes: 5 additions & 4 deletions packages/hap-debugger/src/router/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ export function getDebugInfoFromRequest(request) {
* 迫于device目前不支持POST,暂时把sn放在header中
* @param client 本地存储的设备信息(ip、port、sn)
*/
export function callDeviceWithOwnSn(client) {
export function callDeviceWithOwnSn(client, port) {
if (!port) port = client.port
const options = {
host: client.ip,
port: client.port,
port,
path: '/reportsn',
headers: {
'device-serial-number': client.sn
Expand All @@ -78,11 +79,11 @@ export function callDeviceWithOwnSn(client) {
}
const req = http
.request(options, () => {
colorconsole.log(`### App Server ### 通知手机设备(${client.sn})下发SN成功`)
colorconsole.log(`### App Server ### 通知手机设备(${client.sn}) port ${port} 下发SN成功`)
})
.on('error', (err) => {
colorconsole.warn(
`### App Server ### 通知手机设备(${client.sn})下发SN失败 错误信息: ${err.message}`
`### App Server ### 通知手机设备(${client.sn}) port ${port} 下发SN失败 错误信息: ${err.message}`
)
})
.on('timeout', function () {
Expand Down
Loading