...
void diagPoll(int appID, int state, void *appContext);
Code Block language c // Load Application Context applicationContext *ctx = appContext;
This line casts the context, so it can be used in subsequent operations.
Code Block language c APP_PRINTF("diag: Entered application callback function: diagPoll\r\n\tappId: %d\tstate: %s\r\n", appID, diagStateName(state));
This line logs the entry into the function along with the calling parameters.
Code Block language c case STATE_ACTIVATED: if (!ctx->templateRegistered) { registerNotefileTemplate(); schedSetCompletionState(appID, STATE_DIAG_CHECK, STATE_DIAG_ABORT); } else { schedSetState(appID, STATE_DIAG_CHECK, "diag: process diagnostics"); } break;
This case handles the newly activated state. It will first check to see if a template has been registered in a previous invocation of the polling callback.
If a template has not been registered, then it will attempt to do so. If the request is delivered successfully, then the state will be changed to the next state in the progression, to process the diagnostic values. Otherwise, if the message fails to be delivered, then the state will be set to the ABORT state.
If a template has already been registered on a previous invocation, then the state will be changed to the next state, to process diagnostic values.Code Block language c case STATE_DIAG_ISR_XFER: APP_PRINTF("diag: Transfered from application ISR callback function.\r\n"); APP_PRINTF("diag: ISR callback function called %s <%d> times.\r\n", (ctx->isrOverflow ? "more than" : ""), (ctx->isrOverflow ? ISR_MAX_CALL_RETENTION : ctx->isrCount)); if (ctx->isrOverflow) { ctx->isrCount = ISR_MAX_CALL_RETENTION; } for (size_t i = 0 ; i < ctx->isrCount ; ++i) { APP_PRINTF("diag: call %d:\tappId: %d\tpins: %d\r\n", i, ctx->isrParams[i].appID, ctx->isrParams[i].pins); } resetIsrValues(ctx); // fall through and report diagnostics
This case handles a transfer from the interrupt routine. It will print the invocation, along with the parameters, of of each recorded ISR, up to
ISR_MAX_CALL_RETENTION
times. Once it has exhausted it’s cache, it will reset all values related to the ISR. Instead of exiting, it will fall through into the next case…Code Block language c case STATE_DIAG_CHECK: if (ctx->done) { schedSetState(appID, STATE_DEACTIVATED, "diag: completed successfully"); break; } schedSetCompletionState(appID, STATE_DIAG_CHECK, STATE_DIAG_ABORT); addNote(true); break;
This case handles the main objective of the application, the collection of diagnostics and sending a Note to the Notehub. It will check for the
done
flag, which is only set once a Note has been successfully received by the Notecard. Ifdone
is set, then the application will log a success message and request to be deactivated. If thedone
flag has not been set, thenschedSetCompletionState
is called to instruct the scheduler what to do when the request to add a Note either succeeds or fails. As you can see above, if the request is successful, then it will return to this state, but thedone
flag will be true, and the app will request deactivation. However, if it were to fail, then the application will proceed to the ABORT state. Finally, the request to add a Note is made.Code Block language c case STATE_DIAG_ABORT: schedSetState(appID, STATE_DEACTIVATED, "diag: aborted due to failure!"); break;
This case is reserved for fail cases encountered during the application processing. It request for the application to be deactivated and generates a log indicating the failure.
void diagResponse(int appID, J *rsp, void *appContext);
Code Block language c // Load Application Context applicationContext *ctx = appContext;
This line casts the context, so it can be used in subsequent operations.
Code Block language c APP_PRINTF("diag: Entered application callback function: diagResponse\r\n\tappId: %d", appID); char *json_string = JConvertToJSONString(rsp); APP_PRINTF("\trsp: %s\r\n", json_string); free(json_string);
These lines log the entry into the function along with the calling parameters. Special accommodation is made for the JSON response.
APP_PRINTF
is limited to 90 characters, so two calls are required to reliably log the parameters.Code Block language c // If this is a response timeout, indicate as suchSee if there's an error char *err = JGetString(rsp, "err"); if (rsperr[0] !== NULL'\0') { APP_PRINTF("diag: app error response: timeout%d\r\n", err); schedSetState(appID, STATE_DIAG_ABORT, "diag: aborting..."); return; }
lorem ipsum
Code Block language c // See if there's an error char *err = JGetString(rsp, "err"); if (err[0] != '\0') { APP_PRINTF("diag: app error response: %d\r\n", err); schedSetState(appID, STATE_DIAG_ABORT, "diag: aborting..."); return; }
These lines will peek inside the response JSON to see These lines will peek inside the response JSON to see if an error message is present. If so, the error message will be logged and the application will proceed to the ABORT state.
Code Block language c // Flash the LED if this is a response to this specific ping request switch (JGetInt(rsp, "id")) { case REQUESTID_TEMPLATEDIAGNOTE: ctx->templateRegistered>done = true; APP_PRINTF("diag: SUCCESSFUL templateNote registrationsubmission\r\n"); break; default:case REQUESTID_TEMPLATE: ctx->templateRegistered = true; APP_PRINTF("diag: SUCCESSFUL Notetemplate submissionregistration\r\n"); break; default: APP_PRINTF("diag: received ctx->done = true;unexpected response\r\n"); }
This switch statement is designed to handle the responses to successful messagesrequests. The switch operates on the unique identifiers given to requestseach request. If the ID of a diagnostic Note is encountered, the
done
flag is set and a success log is generated. If the ID associated with a template request is foundencountered, then the flag indicating the template was successfully recorded the flag is set and a log is generated. The only other request sent by this application is the diagnostic Note, so If neither of these IDs are present, then a log is generated indicating the response was not recognized.
Other Functions
External
bool diagInit(void);
...