@@ -10,6 +10,7 @@ const SerialCommand = require('./serial');
1010const FlashCommand = require ( './flash' ) ;
1111const path = require ( 'path' ) ;
1212const _ = require ( 'lodash' ) ;
13+ const { log } = require ( 'console' ) ;
1314
1415// TODO: Get these from exports
1516const PROVISIONING_PROGRESS = 1 ;
@@ -18,7 +19,6 @@ const PROVISIONING_FAILURE = 3;
1819const CTRL_REQUEST_APP_CUSTOM = 10 ;
1920const GET_AT_COMMAND_STATUS = 4 ;
2021
21-
2222module . exports = class eSimCommands extends CLICommandBase {
2323 constructor ( ) { // TODO: Bring ui class
2424 super ( ) ;
@@ -45,7 +45,10 @@ module.exports = class eSimCommands extends CLICommandBase {
4545 : 'No devices found.' ;
4646 throw new Error ( errorMessage ) ;
4747 }
48- await this . doProvision ( devices [ 0 ] ) ;
48+ const device = devices [ 0 ] ;
49+ const resp = await this . doProvision ( device ) ;
50+ await this . _changeLed ( device , resp . success ? PROVISIONING_SUCCESS : PROVISIONING_FAILURE ) ;
51+ this . _addToJson ( this . outputJson , resp ) ;
4952 }
5053
5154 async bulkProvisionCommand ( args ) {
@@ -56,9 +59,12 @@ module.exports = class eSimCommands extends CLICommandBase {
5659 const devices = await this . serial . findDevices ( ) ;
5760 for ( const device of devices ) {
5861 if ( ! provisionedDevices . has ( device . deviceId ) ) {
59- provisionedDevices . add ( device . deviceId ) ;
60- console . log ( `Device ${ device . deviceId } connected` ) ;
61- await this . doProvision ( device , { verbose : true } ) ;
62+ const deviceId = device . deviceId ;
63+ provisionedDevices . add ( deviceId ) ;
64+ console . log ( `Device ${ deviceId } connected` ) ;
65+ const resp = await this . doProvision ( device , { verbose : true } ) ;
66+ await this . _changeLed ( device , resp . success ? PROVISIONING_SUCCESS : PROVISIONING_FAILURE ) ;
67+ this . _addToJson ( this . outputJson , resp ) ;
6268 }
6369 }
6470 } , 1000 ) ;
@@ -68,200 +74,143 @@ module.exports = class eSimCommands extends CLICommandBase {
6874
6975 async doProvision ( device ) {
7076 let provisionOutputLogs = [ ] ;
71- const timestamp = new Date ( ) . toISOString ( ) ;
72- const platform = platformForId ( device . specs . productId ) . name ;
73- const port = device . port ;
74-
75- provisionOutputLogs . push ( `${ os . EOL } Provisioning device ${ device . deviceId } with platform ${ platform } ` ) ;
76-
77- // Flash firmware and wait for AT to work
78- const flashResp = await this . _flashATPassThroughFirmware ( device , platform ) ;
79- provisionOutputLogs . push ( ...flashResp . output ) ;
80- if ( ! flashResp . success ) {
81- await this . _changeLed ( device , PROVISIONING_FAILURE ) ;
82- this . _addToJson ( this . outputJson , {
83- EID : null ,
77+ let eid = null ;
78+ let timestamp = null ;
79+ let expectedProfilesArray = [ ] ;
80+ let downloadedProfilesArray = [ ] ;
81+ let success = false ;
82+
83+ // Add the output logs to the output JSON file in one msg
84+ const outputMsg = ( ) => {
85+ return {
86+ esim_id : eid ,
8487 device_id : device . deviceId ,
85- success : false ,
88+ expectedProfiles : expectedProfilesArray ,
89+ downloadedProfiles : downloadedProfilesArray ,
90+ success : success ,
8691 timestamp : timestamp ,
8792 output : provisionOutputLogs
88- } ) ;
89- return ;
90- }
93+ }
94+ } ;
9195
92- // Get the EID
93- const eidResp = await this . _getEid ( port ) ;
94- provisionOutputLogs . push ( ...eidResp . output ) ;
95- if ( ! eidResp . success ) {
96- await this . _changeLed ( device , PROVISIONING_FAILURE ) ;
97- this . _addToJson ( this . outputJson , {
98- EID : null ,
99- device_id : device . deviceId ,
100- success : false ,
101- timestamp : timestamp ,
102- output : provisionOutputLogs ,
103- } ) ;
104- return ;
105- }
106- const eid = eidResp . eid ;
107- provisionOutputLogs . push ( `EID: ${ eid } ` ) ;
108-
109- // Get the profiles for this EID and compare them against the list in the input JSON under the same EID
110- const matchingEsim = this . inputJsonData . provisioning_data . find ( item => item . esim_id === eid ) ;
111- const iccidFromJson = matchingEsim . profiles . map ( ( profile ) => profile . iccid ) ;
112- const expectedProfilesArray = matchingEsim . profiles ;
113-
114- const profileCmdResp = await this . _checkForExistingProfiles ( port ) ;
115- provisionOutputLogs . push ( ...profileCmdResp . output ) ;
116- if ( ! profileCmdResp . success ) {
117- await this . _changeLed ( device , PROVISIONING_FAILURE ) ;
118- this . _addToJson ( this . outputJson , {
119- EID : eid ,
120- device_id : device . deviceId ,
121- expectedProfilesArray : expectedProfilesArray ,
122- downloadedProfiles : [ ] ,
123- success : false ,
124- timestamp : timestamp ,
125- output : provisionOutputLogs
126- } ) ;
127- return ;
128- }
96+ try {
97+ timestamp = new Date ( ) . toISOString ( ) ;
98+ const platform = platformForId ( device . specs . productId ) . name ;
99+ const port = device . port ;
129100
130- const profilesListOnDevice = profileCmdResp . profilesList ;
131- const existingIccids = profilesListOnDevice . map ( ( line ) => line . split ( '[' ) [ 1 ] . split ( ',' ) [ 0 ] . trim ( ) ) ;
101+ provisionOutputLogs . push ( `${ os . EOL } Provisioning device ${ device . deviceId } with platform ${ platform } ` ) ;
132102
133- console . log ( '[dbg] existingIccids: ' , existingIccids ) ;
134- console . log ( '[dbg] profilesListOnDevice: ' , profilesListOnDevice ) ;
103+ // Flash firmware and wait for AT to work
104+ const flashResp = await this . _flashATPassThroughFirmware ( device , platform ) ;
105+ provisionOutputLogs . push ( ...flashResp . output ) ;
106+ if ( ! flashResp . success ) {
107+ return outputMsg ( ) ;
108+ }
109+ provisionOutputLogs . push ( `${ os . EOL } Firmware flashed successfully` ) ;
135110
136- if ( profilesListOnDevice . length > 0 ) {
137- // extract the iccids that belong to this EID
138- const matchingEsim = this . inputJsonData . provisioning_data . find ( item => item . esim_id === eid ) ;
139- if ( ! matchingEsim ) {
140- provisionOutputLogs . push ( 'No profiles found for the given EID in the input JSON' ) ;
141- this . _addToJson ( this . outputJson , {
142- esim_id : eid ,
143- device_id : device . deviceId ,
144- expectedProfilesArray : expectedProfilesArray ,
145- downloadedProfiles : [ ] ,
146- success : false ,
147- timestamp : timestamp ,
148- output : provisionOutputLogs ,
149- } ) ;
150- return ;
111+ // Get the EID
112+ const eidResp = await this . _getEid ( port ) ;
113+ provisionOutputLogs . push ( ...eidResp . output ) ;
114+ if ( ! eidResp . success ) {
115+ return outputMsg ( ) ;
151116 }
117+ eid = ( eidResp . eid ) . trim ( ) ;
118+ provisionOutputLogs . push ( `EID: ${ eid } ` ) ;
119+
120+ // Get the profiles for this EID and compare them against the list in the input JSON under the same EID
121+ const matchingEsim = this . inputJsonData . provisioning_data . find ( item => item . esim_id === eid ) ;
152122 const iccidFromJson = matchingEsim . profiles . map ( ( profile ) => profile . iccid ) ;
153- const equal = _ . isEqual ( _ . sortBy ( existingIccids ) , _ . sortBy ( iccidFromJson ) ) ;
154- if ( equal ) {
155- this . _changeLed ( device , PROVISIONING_SUCCESS ) ;
156- provisionOutputLogs . push ( 'Profiles already provisioned correctly on the device for the given EID' ) ;
157- this . _addToJson ( this . outputJson , {
158- esim_id : eid ,
159- device_id : device . deviceId ,
160- expectedProfilesArray : expectedProfilesArray ,
161- success : true ,
162- timestamp : timestamp ,
163- output : provisionOutputLogs ,
164- } ) ;
165- return ;
166- } else {
167- provisionOutputLogs . push ( 'Profiles exist on the device but do not match the profiles in the input JSON' ) ;
168- await this . _changeLed ( device , PROVISIONING_FAILURE ) ;
169- this . _addToJson ( this . outputJson , {
170- esim_id : eid ,
171- device_id : device . deviceId ,
172- success : false ,
173- timestamp : timestamp ,
174- output : provisionOutputLogs ,
175- } ) ;
176- return ;
123+ expectedProfilesArray = matchingEsim . profiles ;
124+
125+ if ( ! matchingEsim || iccidFromJson ?. length === 0 || expectedProfilesArray ?. length === 0 ) {
126+ provisionOutputLogs . push ( 'No profiles found for the given EID in the input JSON' ) ;
127+ return outputMsg ( ) ;
177128 }
178- }
179129
180- // Get profiles for this EID from the input JSON
181- const profileResp = this . _getProfiles ( eid ) ;
182- provisionOutputLogs . push ( ...profileResp . output ) ;
183- if ( ! profileResp . success ) {
184- await this . _changeLed ( device , PROVISIONING_FAILURE ) ;
185- this . _addToJson ( this . outputJson , {
186- esim_id : eid ,
187- device_id : device . deviceId ,
188- success : false ,
189- timestamp : timestamp ,
190- output : provisionOutputLogs
191- } ) ;
192- return ;
193- }
130+ const profileCmdResp = await this . _checkForExistingProfiles ( port ) ;
131+ provisionOutputLogs . push ( ...profileCmdResp . output ) ;
132+ if ( ! profileCmdResp . success ) {
133+ return outputMsg ( ) ;
134+ }
194135
195- provisionOutputLogs . push ( `${ os . EOL } Provisioning the following profiles to EID ${ eid } :` ) ;
136+ const profilesListOnDevice = profileCmdResp . profilesList ;
137+ const existingIccids = profilesListOnDevice . map ( ( line ) => line . split ( '[' ) [ 1 ] . split ( ',' ) [ 0 ] . trim ( ) ) ;
138+ provisionOutputLogs . push ( `${ os . EOL } profilesListOnDevice: ${ profilesListOnDevice } ` ) ;
139+ provisionOutputLogs . push ( `${ os . EOL } existingIccids: ${ existingIccids } ` ) ;
140+
141+ if ( profilesListOnDevice . length > 0 ) {
142+ // extract the iccids that belong to this EID
143+ const matchingEsim = this . inputJsonData . provisioning_data . find ( item => item . esim_id === eid ) ;
144+ if ( ! matchingEsim ) {
145+ provisionOutputLogs . push ( 'No profiles found for the given EID in the input JSON' ) ;
146+ return outputMsg ( ) ;
147+ }
148+ const iccidFromJson = matchingEsim . profiles . map ( ( profile ) => profile . iccid ) ;
149+ const equal = _ . isEqual ( _ . sortBy ( existingIccids ) , _ . sortBy ( iccidFromJson ) ) ;
150+ if ( equal ) {
151+ success = true ;
152+ provisionOutputLogs . push ( 'Profiles already provisioned correctly on the device for the given EID' ) ;
153+ return outputMsg ( ) ;
154+ } else {
155+ provisionOutputLogs . push ( 'Profiles exist on the device but do not match the profiles in the input JSON' ) ;
156+ return outputMsg ( ) ;
157+ }
158+ }
196159
197- const profiles = profileResp . profiles ;
198- profiles . forEach ( ( profile , index ) => {
199- const rspUrl = `1\$${ profile . smdp } \$${ profile . matching_id } ` ;
200- provisionOutputLogs . push ( `\t${ index + 1 } . ${ profile . provider } (${ rspUrl } )` ) ;
201- } ) ;
160+ // Get profiles for this EID from the input JSON
161+ const profileResp = this . _getProfiles ( eid ) ;
162+ provisionOutputLogs . push ( ...profileResp . output ) ;
163+ if ( ! profileResp . success ) {
164+ return outputMsg ( ) ;
165+ }
202166
203- // Download each profile and update the JSON output
204- await this . _changeLed ( device , PROVISIONING_PROGRESS ) ;
167+ provisionOutputLogs . push ( `${ os . EOL } Provisioning the following profiles to EID ${ eid } :` ) ;
205168
206- const downloadResp = await this . _doDownload ( profiles , port ) ;
207- const downloadedProfiles = downloadResp . downloadedProfiles ;
208- const downloadedProfilesArray = downloadedProfiles . map ( ( profile ) => {
209- return {
210- status : profile . status ,
211- iccid : profile . iccid ,
212- provider : profile . provider ,
213- duration : profile . duration
214- } ;
215- } ) ;
216- provisionOutputLogs . push ( ...downloadResp . output ) ;
217-
218- if ( ! downloadResp . success ) {
219- await this . _changeLed ( device , PROVISIONING_FAILURE ) ;
220- this . _addToJson ( this . outputJson , {
221- esim_id : eid ,
222- device_id : device . deviceId ,
223- expectedProfiles : expectedProfilesArray ,
224- downloadedProfiles : downloadedProfilesArray ,
225- success : false ,
226- timestamp : timestamp ,
227- output : provisionOutputLogs ,
169+ const profiles = profileResp . profiles ;
170+ profiles . forEach ( ( profile , index ) => {
171+ const rspUrl = `1\$${ profile . smdp } \$${ profile . matching_id } ` ;
172+ provisionOutputLogs . push ( `\t${ index + 1 } . ${ profile . provider } (${ rspUrl } )` ) ;
228173 } ) ;
229- return ;
230- }
231- await this . _changeLed ( device , PROVISIONING_SUCCESS ) ;
232-
233- const profilesOnDeviceAfterDownload = await this . _listProfiles ( port ) ;
234- const iccidsOnDeviceAfterDownload = profilesOnDeviceAfterDownload . map ( ( line ) => line . split ( '[' ) [ 1 ] . split ( ',' ) [ 0 ] . trim ( ) ) ;
235- const equal = _ . isEqual ( _ . sortBy ( iccidsOnDeviceAfterDownload ) , _ . sortBy ( iccidFromJson ) ) ;
236- if ( ! equal ) {
237- provisionOutputLogs . push ( 'Profiles did not match after download' ) ;
238- await this . _changeLed ( device , PROVISIONING_FAILURE ) ;
239- this . _addToJson ( this . outputJson , {
240- esim_id : eid ,
241- device_id : device . deviceId ,
242- expectedProfiles : expectedProfilesArray ,
243- downloadedProfiles : downloadedProfilesArray ,
244- success : false ,
245- timestamp : timestamp ,
246- output : provisionOutputLogs ,
174+
175+ // Download each profile and update the JSON output
176+ await this . _changeLed ( device , PROVISIONING_PROGRESS ) ;
177+
178+ provisionOutputLogs . push ( `${ os . EOL } Downloading profiles...` ) ;
179+ const downloadResp = await this . _doDownload ( profiles , port ) ;
180+ const downloadedProfiles = downloadResp . downloadedProfiles ;
181+ downloadedProfilesArray = downloadedProfiles . map ( ( profile ) => {
182+ return {
183+ status : profile . status ,
184+ iccid : profile . iccid ,
185+ provider : profile . provider ,
186+ duration : profile . duration
187+ } ;
247188 } ) ;
248- return ;
249- }
189+ provisionOutputLogs . push ( ...downloadResp . output ) ;
190+
191+ if ( ! downloadResp . success ) {
192+ provisionOutputLogs . push ( 'Profile download failed' ) ;
193+ return outputMsg ( ) ;
194+ }
250195
251- // Update the JSON output with the downloaded profiles
252- // Success case
253- this . _addToJson ( this . outputJson , {
254- esim_id : eid ,
255- device_id : device . deviceId ,
256- expectedProfiles : expectedProfilesArray ,
257- downloadedProfiles : downloadedProfilesArray ,
258- success : true ,
259- timestamp : timestamp ,
260- output : provisionOutputLogs
261- } ) ;
262-
263- console . log ( `${ os . EOL } Provisioning complete for EID ${ eid } ` ) ;
264- provisionOutputLogs . push ( `${ os . EOL } Provisioning complete for EID ${ eid } ` ) ;
196+ const profilesOnDeviceAfterDownload = await this . _listProfiles ( port ) ;
197+ const iccidsOnDeviceAfterDownload = profilesOnDeviceAfterDownload . map ( ( line ) => line . split ( '[' ) [ 1 ] . split ( ',' ) [ 0 ] . trim ( ) ) ;
198+ const equal = _ . isEqual ( _ . sortBy ( iccidsOnDeviceAfterDownload ) , _ . sortBy ( iccidFromJson ) ) ;
199+ if ( ! equal ) {
200+ provisionOutputLogs . push ( 'Profiles did not match after download' ) ;
201+ return outputMsg ( ) ;
202+ }
203+
204+ // Update the JSON output with the downloaded profiles
205+ // Success case
206+ success = true ;
207+ console . log ( `${ os . EOL } Provisioning complete for EID ${ eid } ` ) ;
208+ provisionOutputLogs . push ( `${ os . EOL } Provisioning complete for EID ${ eid } ` ) ;
209+ return outputMsg ( ) ;
210+ } catch ( error ) {
211+ provisionOutputLogs . push ( `Error during provisioning: ${ error . message } ` ) ;
212+ return outputMsg ( ) ;
213+ }
265214 }
266215
267216 _validateArgs ( args ) {
@@ -318,6 +267,7 @@ module.exports = class eSimCommands extends CLICommandBase {
318267 // Flash the binary
319268 const flashCmdInstance = new FlashCommand ( ) ;
320269
270+ logAndPush ( `${ os . EOL } Flashing firmware...` ) ;
321271 await flashCmdInstance . flashLocal ( {
322272 files : [ device . deviceId , fwPath ] ,
323273 applicationOnly : true ,
@@ -338,7 +288,7 @@ module.exports = class eSimCommands extends CLICommandBase {
338288 while ( Date . now ( ) - start < timeout && ! atOkReceived ) {
339289 try {
340290 const resp = await usbDevice . sendControlRequest ( CTRL_REQUEST_APP_CUSTOM , JSON . stringify ( GET_AT_COMMAND_STATUS ) ) ;
341- console . log ( '[dbg] resp: ' , resp ) ;
291+ // console.log('[dbg] resp: ', resp);
342292 if ( resp ?. result === 0 && resp . data ?. [ 0 ] === '1' ) {
343293 logAndPush ( 'AT-OK received' ) ;
344294 atOkReceived = true ;
@@ -458,6 +408,7 @@ module.exports = class eSimCommands extends CLICommandBase {
458408 }
459409 } ) ;
460410 } ;
411+ logAndPush ( `${ os . EOL } Getting profiles for EID ${ eid } ...` ) ;
461412 const eidBlock = this . inputJsonData . provisioning_data . find ( ( block ) => block . esim_id === eid ) ;
462413
463414 if ( ! eidBlock || ! eidBlock . profiles || eidBlock . profiles . length === 0 ) {
0 commit comments