-
Notifications
You must be signed in to change notification settings - Fork 15
/
sketch-uploader.js
253 lines (205 loc) · 6.6 KB
/
sketch-uploader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
const fsm = require('./util/fsm')
const logger = require('./util/logs').logger
const util = require('./util/util')
const commandIds = require('./command-definitions').commandIds
const buffer = require('buffer')
const timers = require('timers')
const BLOCK_SIZE = 64
// Sketch Uploader states
const STATE_INACTIVE = 'STATE_INACTIVE'
const STATE_AWAIT_READY = 'STATE_AWAIT_READY'
const STATE_BLOCK_TRANSFER = 'STATE_BLOCK_TRANSFER'
const STATE_COMPLETED = 'STATE_COMPLETED'
// Bean States
const BEAN_STATE_READY = 2
const BEAN_STATE_PROGRAMMING = 3
const BEAN_STATE_VERIFY = 4
const BEAN_STATE_COMPLETE = 5
const BEAN_STATE_ERROR = 6
// Bean Sub-state
const BEAN_SUBSTATE_INIT = 0
const BEAN_SUBSTATE_WRITE_ADDRESS = 1
const BEAN_SUBSTATE_WRITE_ADDRESS_ACK = 2
const BEAN_SUBSTATE_WRITE_CHUNK = 3
const BEAN_SUBSTATE_WRITE_CHUNK_ACK = 4
const BEAN_SUBSTATE_READ_ADDRESS = 5
const BEAN_SUBSTATE_READ_ADDRESS_ACK = 6
const BEAN_SUBSTATE_READ_CHUNK = 7
const BEAN_SUBSTATE_READ_CHUNK_ACK = 8
const BEAN_SUBSTATE_VERIFY = 9
const BEAN_SUBSTATE_DONE = 10
const BEAN_SUBSTATE_DONE_ACK = 11
const BEAN_SUBSTATE_START = 12
const BEAN_SUBSTATE_START_ACK = 13
const BEAN_SUBSTATE_HELLO = 14
const BEAN_SUBSTATE_HELLO_ACK = 15
const BEAN_SUBSTATE_START_RSTAGAIN = 16
const BEAN_SUBSTATE_DONE_RESET = 17
const BEAN_SUBSTATE_PROG_MODE = 18
const BEAN_SUBSTATE_PROG_MODE_ACK = 19
const BEAN_SUBSTATE_DEVICE_SIG = 20
const BEAN_SUBSTATE_DEVICE_SIG_ACK = 21
const BEAN_SUBSTATE_WRITE_CHUNK_TWO = 22
const BEAN_SUBSTATE_ERROR = 23
class SketchUploader {
constructor() {
this._process = null
}
beginUpload(device, sketchBuf, sketchName, promptUser, callback) {
this._process = new UploadProcess(device, sketchBuf, sketchName, promptUser, callback)
this._process.start()
}
}
class UploadProcess extends fsm.Context {
constructor(device, sketchBuf, sketchName, promptUser, callback) {
super()
this._device = device
this._sketchBuf = sketchBuf
this._sketchName = sketchName
this._promptUser = promptUser
this._callback = callback
this.initStates({
STATE_INACTIVE: StateInactive,
STATE_AWAIT_READY: StateAwaitReady,
STATE_BLOCK_TRANSFER: StateBlockTransfer,
STATE_COMPLETED: StateCompleted
})
this.setState(STATE_INACTIVE)
}
_statusCommandReceived(status) {
logger.debug(`New Bean State: ${status.state}`)
if (status.state === BEAN_STATE_ERROR) {
let out = `Bean sketch upload error!\n`
out += ` Sub state: ${status.substate}\n`
out += ` Blocks sent: ${status.blocks_sent}\n`
out += ` Bytes sent: ${status.bytes_sent}`
logger.error(out)
this.state.eventBeanError(status.substate)
} else {
this.state.eventBeanState(status.state)
}
}
start() {
logger.info(`Beginning sketch upload of sketch: ${this._sketchName}`)
let serialTransport = this._device.getSerialTransportService()
serialTransport.registerForCommandNotification(
commandIds.BL_STATUS,
(statusCommand) => this._statusCommandReceived(statusCommand))
this.setState(STATE_AWAIT_READY)
}
shouldPromptUser() {
return this._promptUser
}
getDevice() {
return this._device
}
getSketchBuffer() {
return this._sketchBuf
}
getSketchName() {
return this._sketchName
}
complete(error) {
this._callback(error)
}
}
class SketchUploadState extends fsm.State {
// Override any methods in subclasses
enterState(previousState) {}
exitState() {}
eventBeanState(state) {}
eventBeanError(substate) {}
}
class StateInactive extends SketchUploadState {
enterState(previousState) {
if (previousState) {
if (previousState === STATE_COMPLETED) {
// Sketch completed successfully
logger.info('Sketch completed successfully!')
this.ctx.complete(null)
} else {
// Sketch upload error
logger.error('Sketch upload error!')
this.ctx.complete('Sketch upload failed')
}
}
}
}
class StateAwaitReady extends SketchUploadState {
enterState(previousState) {
let serialTransport = this.ctx.getDevice().getSerialTransportService()
let sketchBuf = this.ctx.getSketchBuffer()
let sketchName = this.ctx.getSketchName()
let cmdArgs = [
sketchBuf.length, // hex size
util.crc16(sketchBuf), // hex crc
new Date().getTime() / 1000, // unix timestamp
sketchName.length, // sketch name size
sketchName // sketch name
]
if (this.ctx.shouldPromptUser()) {
util.userInput.question(`Press ENTER to begin upload:\n`, () => {
logger.info('Sketch upload started!')
serialTransport.sendCommand(commandIds.BL_CMD_START, cmdArgs)
})
} else {
logger.info('Sketch upload started!')
serialTransport.sendCommand(commandIds.BL_CMD_START, cmdArgs)
}
}
eventBeanState(state) {
if (state == BEAN_STATE_READY) {
this.ctx.setState(STATE_BLOCK_TRANSFER)
} else {
logger.error(`Unexpected Bean sketch state: ${state}`)
this.ctx.setState(STATE_INACTIVE)
}
}
}
class StateBlockTransfer extends SketchUploadState {
_sendBlock() {
logger.info(`Sending Bean sketch block: ${this._blocksSent}/${this._totalBlocks - 1}`)
let blockStart = this._blocksSent * BLOCK_SIZE
let blockEnd = blockStart + BLOCK_SIZE
if (blockEnd > this._sketchBuffer.length) {
blockEnd = this._sketchBuffer.length
}
let blockBuffer = this._sketchBuffer.slice(blockStart, blockEnd)
this._serialTransport.sendCommand(commandIds.BL_FW_BLOCK, [blockBuffer])
if (blockEnd == this._sketchBuffer.length) {
clearInterval(this._blockTimer)
} else {
this._blocksSent++
}
}
enterState(previousState) {
this._serialTransport = this.ctx.getDevice().getSerialTransportService()
this._sketchBuffer = this.ctx.getSketchBuffer()
this._totalBlocks = Math.ceil(this._sketchBuffer.length / BLOCK_SIZE)
this._blocksSent = 0
this._blockTimer = timers.setInterval(()=> {this._sendBlock()}, 200)
}
eventBeanState(state) {
if (state === BEAN_STATE_COMPLETE) {
this.ctx.setState(STATE_COMPLETED)
return
}
if (state !== BEAN_STATE_PROGRAMMING) {
logger.error(`Unexpected Bean sketch state: ${state}`)
this.ctx.setState(STATE_INACTIVE)
}
}
eventBeanError(substate) {
clearInterval(this._blockTimer)
this.ctx.setState(STATE_INACTIVE)
}
}
class StateCompleted extends SketchUploadState {
enterState(previousState) {
logger.info('Sketch upload complete!')
this.ctx.setState(STATE_INACTIVE)
}
}
module.exports = {
SketchUploader: SketchUploader
}