Source Code Structure

The thing program is built from just two files: hellothingif.c and hellothingif.h.

Internal Structure

This figure shows the internal structure of hellothingif.c.

hellothingif.c consists of the below components based on the framework of Thing-IF SDK.

  • Action handler action_handler()

    When the thing receives a command from the mobile app, the Thing-IF SDK calls action_handler() for each action included in the command. The action handler parses actions and processes their results.

    The thing program of Hello Thing-IF outputs actions on the console (A thing program for an actual solution would operate the thing hardware). Also, it stores the state of the LED light obtained from the action parameters in global variables in order to send them to Thing Interaction Framework.

  • State handler state_handler()

    The Thing-IF SDK calls the state handler when the SDK needs to send state information. The handler reads the state of the LED light and the value of the motion sensor stored in the global variables, and then it formats and outputs the data in JSON.

  • main()

    main() initializes the Thing-IF SDK and onboards the thing. It does not have anything to do after initialization and just waits with an infinite loop of while.

  • Helper functions

    The above major functions call the helper functions, including processes to save and restore the latest state of the LED light and to read the value of the motion sensor.

Thread Structure

As shown in the above figure of the internal structure, the sample thing program uses the following three threads.

  • Main thread

    The main thread executes main(). This thread only initializes the program and enters into the infinite loop of while (1) {}. The following two threads are created in the Thing-IF SDK and call the action and state handlers specified in initialization.

  • MQTT reception thread

    This thread receives and processes MQTT push notifications.

    When the thing is initialized, the Thing-IF SDK internally creates the MQTT reception thread and establishes the MQTT connection with the server. Then, this thread waits for commands from the mobile app as PUBLISH commands of the MQTT protocol.

    When the Thing-IF SDK receives a PUBLISH command, it calls the action handler to process actions. In addition, it calls the state handler to send state information to Thing Interaction Framework after the command is processed.

  • Periodic state update thread

    This thread periodically updates state information.

    The Thing-IF SDK creates this dedicated thread to call the state handler with a certain interval in order to send state information to Thing Interaction Framework.

Note that the state handler is called from both the MQTT reception thread and the periodic state update thread. Both the threads may call the handler at the same time.

Hello Thing-IF uses the mutex of Pthreads for exclusive processing. It prevents inconsistencies which could be caused by concurrent execution of the handler from the MQTT reception thread and the periodic state update thread.

The reference implementation of the Thing-IF SDK for Linux implements threads with Pthreads, but the SDK is designed to let the user program manage OS dependent processes such as creating threads. You can make use of tasks and threads conforming to the target OS.

The SDK internally generates a task to send MQTT PINGREQ commands but the user program does not need to take it into account.


What's Next?

The subsequent topics explain the code in order of initialization, receiving commands, and sending state information. Let us start with initialization.

Go to Initialization.

If you want to learn more...

  • See Thing-IF SDK Structure for the overview of the internal structure and design of the Thing-IF SDK.
  • See Implementation Guidelines to learn more about considerations in using the Thing-IF SDK, such as threads, data types, and memory management.