From 0d68f8a7ab6701c3a6b7938dc6e2d3ec18514337 Mon Sep 17 00:00:00 2001 From: "Valentin D. Pinkman" Date: Mon, 18 Jun 2018 17:43:34 +0200 Subject: [PATCH] add mcu flash call --- src/commands/installMcu.js | 24 ++++------ src/components/ManagerPage/FlashMcu.js | 48 ++++++++++++++++++++ src/components/ManagerPage/index.js | 7 +-- src/components/Workflow/index.js | 63 ++++++++++++++------------ src/helpers/firmware/installMcu.js | 17 +++++-- 5 files changed, 108 insertions(+), 51 deletions(-) diff --git a/src/commands/installMcu.js b/src/commands/installMcu.js index c16387dc..fcc70eb3 100644 --- a/src/commands/installMcu.js +++ b/src/commands/installMcu.js @@ -3,24 +3,20 @@ import { createCommand, Command } from 'helpers/ipc' import { fromPromise } from 'rxjs/observable/fromPromise' -// import { withDevice } from 'helpers/deviceAccess' +import { withDevice } from 'helpers/deviceAccess' import installMcu from 'helpers/firmware/installMcu' -// type Input = { -// devicePath: string, -// firmware: Object, -// } +type Input = { + devicePath: string, + targetId: string | number, +} -// type Result = { -// targetId: number | string, -// version: string, -// final: boolean, -// mcu: boolean, -// } - -type Input = * type Result = * -const cmd: Command = createCommand('installMcu', () => fromPromise(installMcu())) +const cmd: Command = createCommand('installMcu', ({ devicePath, targetId }) => + fromPromise( + withDevice(devicePath)(transport => installMcu(transport, { targetId, version: '1.5' })), + ), +) export default cmd diff --git a/src/components/ManagerPage/FlashMcu.js b/src/components/ManagerPage/FlashMcu.js index e69de29b..a9cd2d5a 100644 --- a/src/components/ManagerPage/FlashMcu.js +++ b/src/components/ManagerPage/FlashMcu.js @@ -0,0 +1,48 @@ +// @flow +import React, { PureComponent } from 'react' + +import type { Device } from 'types/common' +import installMcu from 'commands/installMcu' + +type DeviceInfo = { + targetId: number | string, + version: string, + final: boolean, + mcu: boolean, +} + +type Props = { + device: Device, + deviceInfo: DeviceInfo, +} + +type State = { + flashing: boolean, +} + +class FlashMcu extends PureComponent { + state = { + flashing: false, + } + + componentDidMount() { + this.flasMCU() + } + + flasMCU = async () => { + const { device, deviceInfo } = this.props + const { flashing } = this.state + + if (!flashing) { + this.setState(state => ({ ...state, flashing: true })) + await installMcu.send({ devicePath: device.path, targetId: deviceInfo.targetId }).toPromise() + this.setState(state => ({ ...state, flashing: false })) + } + } + + render() { + return
Flashing MCU
+ } +} + +export default FlashMcu diff --git a/src/components/ManagerPage/index.js b/src/components/ManagerPage/index.js index 6b1b48a7..11209e93 100644 --- a/src/components/ManagerPage/index.js +++ b/src/components/ManagerPage/index.js @@ -9,6 +9,7 @@ import type { Device } from 'types/common' import Workflow from 'components/Workflow' import WorkflowWithIcon from 'components/Workflow/WorkflowWithIcon' import Dashboard from './Dashboard' +import FlashMcu from './FlashMcu' type DeviceInfo = { targetId: number | string, @@ -25,11 +26,11 @@ type Error = { function ManagerPage(): Node { return ( ( + renderFinalUpdate={(device: Device, deviceInfo: DeviceInfo) => (

UPDATE FINAL FIRMARE (TEMPLATE + ACTION WIP) {deviceInfo.final}

)} - renderMcuUpdate={(deviceInfo: DeviceInfo) => ( -

FLASH MCU (TEMPLATE + ACTION WIP) {deviceInfo.mcu}

+ renderMcuUpdate={(device: Device, deviceInfo: DeviceInfo) => ( + )} renderDashboard={(device: Device, deviceInfo: DeviceInfo) => ( diff --git a/src/components/Workflow/index.js b/src/components/Workflow/index.js index 9c5edc0a..794fa7f6 100644 --- a/src/components/Workflow/index.js +++ b/src/components/Workflow/index.js @@ -31,8 +31,8 @@ type Props = { genuineError: ?Error, }, ) => Node, - renderMcuUpdate?: (deviceInfo: DeviceInfo) => Node, - renderFinalUpdate?: (deviceInfo: DeviceInfo) => Node, + renderMcuUpdate?: (device: Device, deviceInfo: DeviceInfo) => Node, + renderFinalUpdate?: (device: Device, deviceInfo: DeviceInfo) => Node, renderDashboard?: (device: Device, deviceInfo: DeviceInfo, isGenuine: boolean) => Node, renderError?: (dashboardError: ?Error, genuineError: ?Error) => Node, } @@ -52,37 +52,42 @@ class Workflow extends PureComponent { {(device: Device) => ( - {(deviceInfo: ?DeviceInfo, dashboardError: ?Error) => ( - - {(isGenuine: ?boolean, genuineError: ?Error) => { - if (dashboardError || genuineError) { - return renderError - ? renderError(dashboardError, genuineError) - : renderDefault(device, deviceInfo, isGenuine, { - genuineError, - dashboardError, - }) - } + {(deviceInfo: ?DeviceInfo, dashboardError: ?Error) => { - if (deviceInfo && deviceInfo.mcu && renderMcuUpdate) { - return renderMcuUpdate(deviceInfo) - } + if (deviceInfo && deviceInfo.mcu && renderMcuUpdate) { + return renderMcuUpdate(device, deviceInfo) + } - if (deviceInfo && deviceInfo.final && renderFinalUpdate) { - return renderFinalUpdate(deviceInfo) - } + if (deviceInfo && deviceInfo.final && renderFinalUpdate) { + return renderFinalUpdate(device, deviceInfo) + } - if (isGenuine && deviceInfo && device && !dashboardError && !genuineError) { - if (renderDashboard) return renderDashboard(device, deviceInfo, isGenuine) - } + return ( + + {(isGenuine: ?boolean, genuineError: ?Error) => { + if (dashboardError || genuineError) { + return renderError + ? renderError(dashboardError, genuineError) + : renderDefault(device, deviceInfo, isGenuine, { + genuineError, + dashboardError, + }) + } - return renderDefault(device, deviceInfo, isGenuine, { - genuineError, - dashboardError, - }) - }} - - )} + if (isGenuine && deviceInfo && device && !dashboardError && !genuineError) { + if (onGenuineCheck) onGenuineCheck(isGenuine) + + if (renderDashboard) return renderDashboard(device, deviceInfo, isGenuine) + } + + return renderDefault(device, deviceInfo, isGenuine, { + genuineError, + dashboardError, + }) + }} + + ) + }} )} diff --git a/src/helpers/firmware/installMcu.js b/src/helpers/firmware/installMcu.js index e44f4d47..3a0b6499 100644 --- a/src/helpers/firmware/installMcu.js +++ b/src/helpers/firmware/installMcu.js @@ -1,8 +1,15 @@ // @flow +import type Transport from '@ledgerhq/hw-transport' +import qs from 'qs' +import { MANAGER_API_URL } from 'helpers/constants' +import { createDeviceSocket } from 'helpers/socket' -type Result = Promise +type Result = Promise<*> -// TODO: IMPLEMENTATION FOR FLASHING FIRMWARE -// GETTING APDUS FROM SERVER -// SEND THE APDUS TO DEVICE -export default async (): Result => new Promise(resolve => resolve(true)) +export default async ( + transport: Transport<*>, + params: { targetId: string | number, version: string }, +): Result => { + const url = `${MANAGER_API_URL}/mcu?${qs.stringify(params)}` + return createDeviceSocket(transport, url).toPromise() +}