...
Code Block | ||
---|---|---|
| ||
#define REQUESTID_DIAGNOTE 09171979
#define REQUESTID_TEMPLATE 19790917 |
REQUESTID_TEMPLATEDIAGNOTE
- An arbitrary number allowing you to match a diagnostic Note response and request. In order to keep track of a response to specific Note, you need to supply an “id” tag to the Note. When an “id” is supplied to a Note, then the response parameter provided todiagResponse()
will contain the matching “id”.
...
language | c |
---|
...
REQUESTID_TEMPLATE
- An arbitrary number to match the template response and request.
Code Block | ||
---|---|---|
| ||
#define APPLICATION_NOTEFILE "*#diag.qo" |
APPLICATION_NOTEFILE
- The dynamic filename of the application specific queue.NOTE: The Gateway will replace
*
with the originating node's ID. The resulting transformation will resemble012345678901234567890123#diag.qo
.
...
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 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 move on to the abort 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 such if (rsp == NULL) { APP_PRINTF("diag: response timeout\r\n"); 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 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_TEMPLATE: ctx->templateRegistered = true; APP_PRINTF("diag: SUCCESSFUL template registration\r\n"); break; default: APP_PRINTF("diag: SUCCESSFUL Note submission\r\n"); ctx->done = true;
This switch statement is designed to handle the successful messages. The switch operates on the unique identifiers given to requests. If the ID associated with a template request is found, then 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
Other Functions
External
bool diagInit(void);
...