Kopīgot, izmantojot


Initialization and termination

At start-up, every Azure Sphere application should perform some initialization:

  • Register a SIGTERM handler for termination requests. The Azure Sphere device OS sends the SIGTERM termination signal to indicate that that application must exit, most frequently when an update is pending, but also in response to a device power down request. As part of its initialization code, the application should register a handler for such requests. For example:

      #include <signal.h>
      ...
      // Register a SIGTERM handler for termination requests
      struct sigaction action;
      memset(&action, 0, sizeof(struct sigaction));
      action.sa_handler = TerminationHandler;
      sigaction(SIGTERM, &action, NULL);
    

    In the termination handler, the application can perform whatever shutdown tasks it requires. Termination handlers must be POSIX async-signal-safe. In particular, they must not contain calls to Log_Debug(). The sample programs exit on error as well as on receipt of the termination signal. Therefore, these programs simply set a Boolean in the termination handler and then perform cleanup and shutdown tasks after exiting the main loop.

  • Initialize handles for GPIO peripherals.

  • If the application uses Azure IoT Hub, connect to the IoT client and register callback functions for IoT features such as cloud-to-device messages, device twin status, and direct method calls.

At termination, the application should close peripherals, destroy handles, and free allocated memory. The application has only two seconds to exit upon receipt of the SIGTERM signal; if the application has not exited by then the Azure Sphere OS sends a SIGKILL signal which immediately terminates the application. The SIGKILL signal should always be avoided. If the application routinely performs actions that might take longer than two seconds to complete, consider adding a deferred update loop to the application. Applications that use the Powerdown feature should do any necessary cleanup before calling the power-down APIs.