@@ -55,145 +55,136 @@ export class UsbPeripheral implements Peripheral {
5555 }
5656
5757 async sendCommand ( options : CommandOption ) : Promise < CommandResponse > {
58- return new Promise < CommandResponse > ( async ( resolve , reject ) => {
59- try {
60- await this . open ( ) ;
61- validateCommandParams ( options . params ) ;
62- const data = encodeCommandOption ( options ) ;
63- logger ( 'send data %o' , `<${ convertToHexString ( data ) } >` ) ;
64- await this . _device . write ( data ) ;
65- const receivedData = await this . _device . read ( options . timeout || 2000 ) ;
66- logger ( 'received data %o' , `<${ convertToHexString ( receivedData ) } >` ) ;
67- const commandResponse = decodeCommandResponse ( receivedData ) ;
68- logger ( 'command response: %o' , commandResponse ) ;
69- resolve ( commandResponse ) ;
70- } catch ( err ) {
71- logger ( 'USB send command communication error %O' , err ) ;
58+ try {
59+ await this . open ( ) ;
60+ validateCommandParams ( options . params ) ;
61+ const data = encodeCommandOption ( options ) ;
62+ logger ( 'send data %o' , `<${ convertToHexString ( data ) } >` ) ;
63+ await this . _device . write ( data ) ;
64+ const receivedData = await this . _device . read ( options . timeout || 2000 ) ;
65+ logger ( 'received data %o' , `<${ convertToHexString ( receivedData ) } >` ) ;
66+ const commandResponse = decodeCommandResponse ( receivedData ) ;
67+ logger ( 'command response: %o' , commandResponse ) ;
68+ return commandResponse ;
69+ } catch ( err ) {
70+ logger ( 'USB send command communication error %O' , err ) ;
71+
72+ throw err ;
73+ }
74+ }
7275
73- return reject ( err ) ;
76+ async writeMemory ( option : DataOption ) : Promise < void > {
77+ try {
78+ const command : CommandOption = {
79+ command : Commands . WriteMemory ,
80+ hasDataPhase : true ,
81+ params : [
82+ ...pack ( option . startAddress , { bits : 32 } ) ,
83+ ...pack ( option . data . length , { bits : 32 } )
84+ ]
85+ } ;
86+
87+ const firsCommandResponse = await this . sendCommand ( command ) ;
88+ if ( firsCommandResponse . tag !== ResponseTags . Generic ) {
89+ logger ( 'Invalid write memory response! %o' , firsCommandResponse ) ;
90+ throw new Error ( 'Invalid write memory response!' ) ;
91+ }
92+
93+ if ( firsCommandResponse . code !== 0 ) {
94+ logger ( 'Non zero write memory response! %o' , firsCommandResponse ) ;
95+ throw new Error ( `Non zero write memory response! Response code: ${ firsCommandResponse . code } ` ) ;
7496 }
75- } ) ;
76- }
7797
78- writeMemory ( option : DataOption ) : Promise < void > {
79- return new Promise < void > ( async ( resolve , reject ) => {
80- try {
81- const command : CommandOption = {
82- command : Commands . WriteMemory ,
83- hasDataPhase : true ,
84- params : [
85- ...pack ( option . startAddress , { bits : 32 } ) ,
86- ...pack ( option . data . length , { bits : 32 } )
87- ]
88- } ;
89-
90- const firsCommandResponse = await this . sendCommand ( command ) ;
91- if ( firsCommandResponse . tag !== ResponseTags . Generic ) {
92- logger ( 'Invalid write memory response! %o' , firsCommandResponse ) ;
93- return reject ( new Error ( 'Invalid write memory response!' ) ) ;
94- }
95-
96- if ( firsCommandResponse . code !== 0 ) {
97- logger ( 'Non zero write memory response! %o' , firsCommandResponse ) ;
98- return reject ( new Error ( `Non zero write memory response! Response code: ${ firsCommandResponse . code } ` ) ) ;
99- }
100-
101- for ( let i = 0 ; i < option . data . length ; i = i + WRITE_DATA_STREAM_PACKAGE_LENGTH ) {
102- const slice = option . data . slice ( i , i + WRITE_DATA_STREAM_PACKAGE_LENGTH ) ;
103-
104- const writeData = [
105- 2 , // USB channel
106- 0 ,
107- slice . length ,
108- 0 , // TODO: What is it?
109- ...slice
110- ] ;
111-
112- logger ( 'send data %o' , convertToHexString ( writeData ) ) ;
113- await this . _device . write ( writeData ) ;
114- // workaround to prevent main thread blocking
115- await snooze ( 1 ) ;
116- }
117-
118- const receivedData = await this . _device . read ( option . timeout || 2000 ) ;
119- logger ( 'write memory received data %o' , `<${ convertToHexString ( receivedData ) } >` ) ;
120- const secondCommandResponse = decodeCommandResponse ( receivedData ) ;
121- logger ( 'write memory response: %o' , secondCommandResponse ) ;
122-
123- if ( secondCommandResponse . tag !== ResponseTags . Generic ) {
124- logger ( 'Invalid write memory final response %o' , secondCommandResponse ) ;
125- return reject ( new Error ( 'Invalid write memory final response!' ) ) ;
126- }
127-
128- if ( secondCommandResponse . code !== 0 ) {
129- logger ( 'Non zero write memory final response %o' , secondCommandResponse ) ;
130- const msg = `Non zero write memory final response! Response code: ${ secondCommandResponse . code } ` ;
131- return reject ( new Error ( msg ) ) ;
132- }
133-
134- resolve ( ) ;
135- } catch ( err ) {
136- logger ( 'Can not write memory data %O' , err ) ;
137- reject ( err ) ;
98+ for ( let i = 0 ; i < option . data . length ; i = i + WRITE_DATA_STREAM_PACKAGE_LENGTH ) {
99+ const slice = option . data . slice ( i , i + WRITE_DATA_STREAM_PACKAGE_LENGTH ) ;
100+
101+ const writeData = [
102+ 2 , // USB channel
103+ 0 ,
104+ slice . length ,
105+ 0 , // TODO: What is it?
106+ ...slice
107+ ] ;
108+
109+ logger ( 'send data %o' , convertToHexString ( writeData ) ) ;
110+ await this . _device . write ( writeData ) ;
111+ // workaround to prevent main thread blocking
112+ await snooze ( 1 ) ;
138113 }
139114
140- } ) ;
115+ const receivedData = await this . _device . read ( option . timeout || 2000 ) ;
116+ logger ( 'write memory received data %o' , `<${ convertToHexString ( receivedData ) } >` ) ;
117+ const secondCommandResponse = decodeCommandResponse ( receivedData ) ;
118+ logger ( 'write memory response: %o' , secondCommandResponse ) ;
119+
120+ if ( secondCommandResponse . tag !== ResponseTags . Generic ) {
121+ logger ( 'Invalid write memory final response %o' , secondCommandResponse ) ;
122+ throw new Error ( 'Invalid write memory final response!' ) ;
123+ }
124+
125+ if ( secondCommandResponse . code !== 0 ) {
126+ logger ( 'Non zero write memory final response %o' , secondCommandResponse ) ;
127+ const msg = `Non zero write memory final response! Response code: ${ secondCommandResponse . code } ` ;
128+ throw new Error ( msg ) ;
129+ }
130+ } catch ( err ) {
131+ logger ( 'Can not write memory data %O' , err ) ;
132+ throw err ;
133+ }
141134 }
142135
143- readMemory ( startAddress : number , count : number ) : Promise < Buffer > {
144- return new Promise < Buffer > ( async ( resolve , reject ) => {
145- try {
146- const command : CommandOption = {
147- command : Commands . ReadMemory ,
148- params : [
149- ...pack ( startAddress , { bits : 32 } ) ,
150- ...pack ( count , { bits : 32 } )
151- ]
152- } ;
153-
154- const firsCommandResponse = await this . sendCommand ( command ) ;
155- if ( firsCommandResponse . tag !== ResponseTags . ReadMemory ) {
156- logger ( 'Invalid read memory response %o' , firsCommandResponse ) ;
157- return reject ( new Error ( 'Invalid read memory response!' ) ) ;
158- }
159-
160- if ( firsCommandResponse . code !== 0 ) {
161- logger ( 'Non zero read memory response %o' , firsCommandResponse ) ;
162- return reject ( new Error ( `Non zero read memory response! Response code: ${ firsCommandResponse . code } ` ) ) ;
163- }
164-
165- const byte4Number = firsCommandResponse . raw . slice ( 12 , 15 ) ;
166- const arrivingDataSize = convertLittleEndianNumber ( byte4Number ) ;
167- const memoryData : Array < number > = [ ] ;
168- while ( memoryData . length < arrivingDataSize ) {
169- const receivedData = await this . _device . read ( 2000 ) ;
170- logger ( 'received data %o' , `<${ convertToHexString ( receivedData ) } >` ) ;
171- memoryData . push ( ...receivedData ) ;
172- // workaround to prevent main thread blocking
173- await snooze ( 1 ) ;
174- }
175-
176- const responseData = await this . _device . read ( 2000 ) ;
177- logger ( 'received data %o' , `<${ convertToHexString ( responseData ) } >` ) ;
178-
179- const secondCommandResponse = decodeCommandResponse ( responseData ) ;
180- if ( secondCommandResponse . tag !== ResponseTags . Generic ) {
181- logger ( 'Invalid read memory final response %o' , secondCommandResponse ) ;
182- return reject ( new Error ( 'Invalid read memory final response!' ) ) ;
183- }
184-
185- if ( secondCommandResponse . code !== 0 ) {
186- logger ( 'Non zero read memory final response %o' , secondCommandResponse ) ;
187- const msg = `Non zero read memory final response! Response code: ${ secondCommandResponse . code } ` ;
188- return reject ( new Error ( msg ) ) ;
189- }
190-
191- resolve ( Buffer . from ( memoryData ) ) ;
192- } catch ( error ) {
193- logger ( 'Read memory error %O' , error ) ;
194-
195- reject ( error ) ;
136+ async readMemory ( startAddress : number , count : number ) : Promise < Buffer > {
137+ try {
138+ const command : CommandOption = {
139+ command : Commands . ReadMemory ,
140+ params : [
141+ ...pack ( startAddress , { bits : 32 } ) ,
142+ ...pack ( count , { bits : 32 } )
143+ ]
144+ } ;
145+
146+ const firsCommandResponse = await this . sendCommand ( command ) ;
147+ if ( firsCommandResponse . tag !== ResponseTags . ReadMemory ) {
148+ logger ( 'Invalid read memory response %o' , firsCommandResponse ) ;
149+ throw new Error ( 'Invalid read memory response!' ) ;
150+ }
151+
152+ if ( firsCommandResponse . code !== 0 ) {
153+ logger ( 'Non zero read memory response %o' , firsCommandResponse ) ;
154+ throw new Error ( `Non zero read memory response! Response code: ${ firsCommandResponse . code } ` ) ;
155+ }
156+
157+ const byte4Number = firsCommandResponse . raw . slice ( 12 , 15 ) ;
158+ const arrivingDataSize = convertLittleEndianNumber ( byte4Number ) ;
159+ const memoryData : Array < number > = [ ] ;
160+ while ( memoryData . length < arrivingDataSize ) {
161+ const receivedData = await this . _device . read ( 2000 ) ;
162+ logger ( 'received data %o' , `<${ convertToHexString ( receivedData ) } >` ) ;
163+ memoryData . push ( ...receivedData ) ;
164+ // workaround to prevent main thread blocking
165+ await snooze ( 1 ) ;
166+ }
167+
168+ const responseData = await this . _device . read ( 2000 ) ;
169+ logger ( 'received data %o' , `<${ convertToHexString ( responseData ) } >` ) ;
170+
171+ const secondCommandResponse = decodeCommandResponse ( responseData ) ;
172+ if ( secondCommandResponse . tag !== ResponseTags . Generic ) {
173+ logger ( 'Invalid read memory final response %o' , secondCommandResponse ) ;
174+ throw new Error ( 'Invalid read memory final response!' ) ;
175+ }
176+
177+ if ( secondCommandResponse . code !== 0 ) {
178+ logger ( 'Non zero read memory final response %o' , secondCommandResponse ) ;
179+ const msg = `Non zero read memory final response! Response code: ${ secondCommandResponse . code } ` ;
180+ throw new Error ( msg ) ;
196181 }
197- } ) ;
182+
183+ return Buffer . from ( memoryData ) ;
184+ } catch ( error ) {
185+ logger ( 'Read memory error %O' , error ) ;
186+
187+ throw error ;
188+ }
198189 }
199190}
0 commit comments