@@ -434,88 +434,85 @@ void GAuthManager::tokenProcessingTask()
434434
435435 time_t now = getTime ();
436436
437- while (!ret && config->signer .tokens .status != token_status_ready)
437+ Utils::idle ();
438+ // check time if clock synching once set in the JWT token generating process (during beginning step)
439+ // or valid time required for SSL handshake in ESP8266
440+ if (!config->internal .clock_rdy )
438441 {
439- Utils::idle ();
440- // check time if clock synching once set in the JWT token generating process (during beginning step)
441- // or valid time required for SSL handshake in ESP8266
442- if (!config->internal .clock_rdy )
442+ if (readyToSync ())
443443 {
444- if (readyToSync ())
444+ if (isSyncTimeOut ())
445445 {
446- if (isSyncTimeOut ())
447- {
448- config->signer .tokens .error .message .clear ();
449- if (_cli_type == esp_google_sheet_client_type_internal_basic_client)
450- setTokenError (ESP_GOOGLE_SHEET_CLIENT_ERROR_NTP_SYNC_TIMED_OUT);
451- else
452- setTokenError (ESP_GOOGLE_SHEET_CLIENT_ERROR_SYS_TIME_IS_NOT_READY);
453- sendTokenStatusCB ();
454- config->signer .tokens .status = token_status_on_initialize;
455- config->internal .last_jwt_generation_error_cb_millis = 0 ;
456- }
457-
458- // reset flag to allow clock synching execution again in ut->syncClock if clocck synching was timed out
459- config->internal .clock_synched = false ;
460- reconnect ();
446+ config->signer .tokens .error .message .clear ();
447+ if (_cli_type == esp_google_sheet_client_type_internal_basic_client)
448+ setTokenError (ESP_GOOGLE_SHEET_CLIENT_ERROR_NTP_SYNC_TIMED_OUT);
449+ else
450+ setTokenError (ESP_GOOGLE_SHEET_CLIENT_ERROR_SYS_TIME_IS_NOT_READY);
451+ sendTokenStatusCB ();
452+ config->signer .tokens .status = token_status_on_initialize;
453+ config->internal .last_jwt_generation_error_cb_millis = 0 ;
461454 }
462455
463- // check or set time again
464- tryGetTime ();
465-
466- // exit task immediately if time is not ready synched
467- // which handleToken function should run repeatedly to enter this function again.
468- if (!config->internal .clock_rdy )
469- {
470- config->signer .tokenTaskRunning = false ;
471- return ;
472- }
456+ // reset flag to allow clock synching execution again in ut->syncClock if clocck synching was timed out
457+ config->internal .clock_synched = false ;
458+ reconnect ();
473459 }
474460
475- // create signed JWT token and exchange with auth token
476- if (config->signer .step == gauth_jwt_generation_step_begin &&
477- (millis () - config->internal .last_jwt_begin_step_millis > config->timeout .tokenGenerationBeginStep ||
478- config->internal .last_jwt_begin_step_millis == 0 ))
479- {
480-
481- // time must be set first
482- tryGetTime ();
483- config->internal .last_jwt_begin_step_millis = millis ();
461+ // check or set time again
462+ tryGetTime ();
484463
485- if (config->internal .clock_rdy )
486- config->signer .step = gauth_jwt_generation_step_encode_header_payload;
487- }
488- // encode the JWT token
489- else if (config->signer .step == gauth_jwt_generation_step_encode_header_payload)
490- {
491- if (createJWT ())
492- config->signer .step = gauth_jwt_generation_step_sign;
493- }
494- // sign the JWT token
495- else if (config->signer .step == gauth_jwt_generation_step_sign)
464+ // exit task immediately if time is not ready synched
465+ // which handleToken function should run repeatedly to enter this function again.
466+ if (!config->internal .clock_rdy )
496467 {
497- if ( createJWT ())
498- config-> signer . step = gauth_jwt_generation_step_exchange ;
468+ config-> signer . tokenTaskRunning = false ;
469+ return ;
499470 }
500- // sending JWT token requst for auth token
501- else if (config->signer .step == gauth_jwt_generation_step_exchange)
502- {
471+ }
503472
504- if (readyToRefresh ())
505- {
506- // sending a new request
507- ret = requestTokens (false );
473+ // create signed JWT token and exchange with auth token
474+ if (config->signer .step == gauth_jwt_generation_step_begin &&
475+ (millis () - config->internal .last_jwt_begin_step_millis > config->timeout .tokenGenerationBeginStep ||
476+ config->internal .last_jwt_begin_step_millis == 0 ))
477+ {
508478
509- // send error cb
510- if (! reconnect ())
511- handleTaskError (ESP_GOOGLE_SHEET_CLIENT_ERROR_TCP_ERROR_CONNECTION_LOST );
479+ // time must be set first
480+ tryGetTime ();
481+ config-> internal . last_jwt_begin_step_millis = millis ( );
512482
513- // reset state and exit loop
514- config->signer .step = ret || getTime () - now > 3599 ? gauth_jwt_generation_step_begin : gauth_jwt_generation_step_exchange;
483+ if (config->internal .clock_rdy )
484+ config->signer .step = gauth_jwt_generation_step_encode_header_payload;
485+ }
486+ // encode the JWT token
487+ else if (config->signer .step == gauth_jwt_generation_step_encode_header_payload)
488+ {
489+ if (createJWT ())
490+ config->signer .step = gauth_jwt_generation_step_sign;
491+ }
492+ // sign the JWT token
493+ else if (config->signer .step == gauth_jwt_generation_step_sign)
494+ {
495+ if (createJWT ())
496+ config->signer .step = gauth_jwt_generation_step_exchange;
497+ }
498+ // sending JWT token requst for auth token
499+ else if (config->signer .step == gauth_jwt_generation_step_exchange)
500+ {
515501
516- _token_processing_task_enable = false ;
517- ret = true ;
518- }
502+ if (readyToRefresh ())
503+ {
504+ // sending a new request
505+ ret = requestTokens (false );
506+
507+ // send error cb
508+ if (!reconnect ())
509+ handleTaskError (ESP_GOOGLE_SHEET_CLIENT_ERROR_TCP_ERROR_CONNECTION_LOST);
510+
511+ // reset state and exit loop
512+ config->signer .step = ret || getTime () - now > 3599 ? gauth_jwt_generation_step_begin : gauth_jwt_generation_step_exchange;
513+
514+ _token_processing_task_enable = false ;
515+ ret = true ;
519516 }
520517 }
521518
0 commit comments