Registering State Information

The thing program registers state information to Thing Interaction Framework. It occurs at intervals specified in onboarding or just after all the actions in a received command are processed.

The Thing-IF SDK calls the state handler when the registration should occur. The state handler uses one of its arguments, KII_THING_IF_WRITER, to write a string of state information in JSON format. The Thing-IF SDK sends state information to Thing Interaction Framework if the state handler returns successfully.

Getting State Information

The state information in Hello Thing-IF consists of the state of the LED light and the value of the motion sensor. The program below gets those pieces of information in smartlight and motion, respectively.

static kii_bool_t state_handler(
        kii_t* kii,
        KII_THING_IF_WRITER writer)
{
  ......

  int motion;
  prv_smartlight_t smartlight;
  memset(&smartlight, 0x00, sizeof(smartlight));
  if (prv_get_smartlight_info(&smartlight) == KII_FALSE) {
    printf("Failed to lock/unlock mutex.\n");
    return KII_FALSE;
  }
  if (prv_get_motion_sensor(&motion) == KII_FALSE) {
    printf("Failed to lock/unlock mutex.\n");
    return KII_FALSE;
  }

  ......

  return KII_TRUE;
}

prv_get_motion_sensor functions in the same way as those in the action handler as described in Data Sharing between the Handers. It uses the mutex to synchronize and return the values in the prv_smartlight_t structure stored in the global variable.

prg_get_motion_sensor() gets the value in the global variable m_motion and increments it.

static pthread_mutex_t m_mutex;
static int m_motion = 0;

static kii_bool_t prv_get_motion_sensor(int *motion)
{
  if (pthread_mutex_lock(&m_mutex) != 0) {
    return KII_FALSE;
  }
  m_motion = (m_motion + 1) % 11;
  *motion = m_motion;
  if (pthread_mutex_unlock(&m_mutex) != 0) {
    return KII_FALSE;
  }
  return KII_TRUE;
}

Note that the MQTT reception thread and the periodic state update thread may execute the line of m_motion = m_motion + 1 in prv_get_motion_sensor() at the same time. If the synchronization process was not implemented in Hello Thing-IF, the sensor value would incorrectly increment although it would not have much impact in Hello Thing-IF, which just simulates changes in the sensor value. However, in general, it is appropriate to synchronize threads with the mutex or anything as in the above code.

Writing out State Information

When state information is ready, the thing program writes out a JSON string of state information to KII_THING_IF_WRITER.

The first argument of KII_THING_IF_WRITER is kii_t without changes after it is passed to the state handler. The second argument is the string to output. KII_TRUE is returned if the string is successfully written out.

Hello Thing-IF outputs a JSON string like the following.

{"power":true,"brightness":100,"motion":10}

The code to output a JSON string like the above is as below.

static kii_bool_t state_handler(
        kii_t* kii,
        KII_THING_IF_WRITER writer)
{
  char buf[256];

  ......

  if ((*writer)(kii, "{\"power\":") == KII_FALSE) {
    return KII_FALSE;
  }
  if ((*writer)(kii, smartlight.power == KII_JSON_TRUE
                  ? "true," : "false,") == KII_FALSE) {
    return KII_FALSE;
  }

  if ((*writer)(kii, "\"brightness\":") == KII_FALSE) {
    return KII_FALSE;
  }
  sprintf(buf, "%d,", smartlight.brightness);
  if ((*writer)(kii, buf) == KII_FALSE) {
    return KII_FALSE;
  }

  if ((*writer)(kii, "\"motion\":") == KII_FALSE) {
    return KII_FALSE;
  }
  sprintf(buf, "%d}", motion);
  if ((*writer)(kii, buf) == KII_FALSE) {
    return KII_FALSE;
  }

  printf("Sending the state\n");

  return KII_TRUE;
}

The sample code writes out a string piece by piece to explain how KII_THING_IF_WRITER works. You can make it simpler by writing out a entire string to a buffer and then passing it to KII_THING_WRITER.


What's Next?

Now you have walked through the implementation of Hello Thing-IF. Next, let us review troubleshooting tips for the thing implementation.

Go to Troubleshooting.

If you want to learn more...

  • See Updating State for more information about sending state information.