Blocking or non-blocking API calls

API functions often allow application to set blocking parameter indicating if function shall be blocking or non-blocking.

Blocking mode

When the function is called in blocking mode blocking = 1, application thread gets suspended until response from GSM device is received. If there is a queue of multiple commands, thread may wait a while before receiving data.

When API function returns, application has valid response data and can react immediately.

  • Linear programming model may be used

  • Application may use multiple threads for real-time execution to prevent system stalling when running function call

Warning

Due to internal architecture, it is not allowed to call API functions in blocking mode from events or callbacks. Any attempt not to do so will result in function returning error.

Example code:

Blocking command example
 1/* Somewhere in thread function */
 2
 3/* Get device hostname in blocking mode */
 4/* Function returns actual result */
 5if (lwcell_sms_send("+0123456789", "text", NULL, NULL, 1 /* 1 means blocking call */) == lwcellOK) {
 6    /* At this point we have valid result from device */
 7    printf("SMS sent successfully\r\n");
 8} else {
 9    printf("Error trying to send SMS..\r\n");
10}

Non-blocking mode

If the API function is called in non-blocking mode, function will return immediately with status indicating if command request has been successfully sent to internal command queue. Response has to be processed in event callback function.

Warning

Due to internal architecture, it is only allowed to call API functions in non-blocking mode from events or callbacks. Any attempt to do so will result in function returning error.

Example code:

Non-blocking command example
 1/* Hostname event function, called when lwcell_sms_send() function finishes */
 2void
 3sms_send_fn(lwcellr_t res, void* arg) {
 4    /* Check actual result from device */
 5    if (res == lwcellOK) {
 6        printf("SMS sent successfully\r\n");
 7    } else {
 8        printf("Error trying to send SMS\r\n");
 9    }
10}
11
12/* Somewhere in thread and/or other GSM event function */
13
14/* Send SMS in non-blocking mode */
15/* Function now returns if command has been sent to internal message queue */
16if (lwcell_sms_send("number", "text message", sms_send_fn, NULL, 0 /* 0 means non-blocking call */) == lwcellOK) {
17    /* At this point we only know that command has been sent to queue */
18    printf("SMS send message command sent to queue.\r\n");
19} else {
20    /* Error writing message to queue */
21    printf("Cannot send SMS send message command to queue. Maybe out of memory? Check result from function\r\n");
22}

Warning

When using non-blocking API calls, do not use local variables as parameter. This may introduce undefined behavior and memory corruption if application function returns before command is executed.

Example of a bad code:

Example of bad usage of non-blocking command
 1/* Hostname event function, called when lwcell_sms_send() function finishes */
 2void
 3sms_send_fn(lwcellr_t res, void* arg) {
 4    /* Check actual result from device */
 5    if (res == lwcellOK) {
 6        printf("SMS sent successfully\r\n");
 7    } else {
 8        printf("Error trying to send SMS\r\n");
 9    }
10}
11
12/* Check hostname */
13void
14check_hostname(void) {
15    char message[] = "text message";
16
17    /* Send SMS in non-blocking mode */
18    /* Function now returns if command has been sent to internal message queue */
19    /* It uses pointer to local data but w/o blocking command */
20    if (lwcell_sms_send("number", message, sms_send_fn, NULL, 0 /* 0 means non-blocking call */) == lwcellOK) {
21        /* At this point we only know that command has been sent to queue */
22        printf("SMS send message command sent to queue.\r\n");
23    } else {
24        /* Error writing message to queue */
25        printf("Cannot send SMS send message command to queue. Maybe out of memory? Check result from function\r\n");
26    }
27}