Registering and Getting Things

In the first place, you need to register a thing to use it with Kii Cloud. Kii Cloud recognizes things through registration.

At the second and subsequent boots, the registered thing will get authenticated. You can save the obtained access token and thingID in non-volatile memory on the thing hardware if available.

After registration or authentication of the thing, the kii_t structure for API call keeps the thing access token. You can access Kii Cloud with the privilege of the thing by using the thing access token.

Registering Things

To register a thing to Kii Cloud, call the kii_thing_register function.

/* Set thing information. */
#define VENDOR_THING_ID "rBnvSPOXBDF9r29GJeGS"
#define THING_TYPE "sensor"
#define PASSWORD "123ABC"

int ret;

/* Register the thing. */
ret = kii_thing_register(&kii, VENDOR_THING_ID, THING_TYPE, PASSWORD);
if (ret != 0) {
  /* Handle the error. */
  return;
}

printf("thingID:%s\n", kii.kii_core.author.author_id);
printf("access token:%s\n", kii.kii_core.author.access_token);

In the above example, a thing is registered with vendorThingID "rBnvSPOXBDF9r29GJeGS", thing type "sensor", and password "123ABC". Also, the kii points to the kii_t structure initialized in Initializing the SDK.

If the registration succeeds, control is returned to the caller with the specified kii_t structure containing the thingID and access token. This is equivalent to logged-in status and you can call the subsequent API calls with the thing's privilege.

You can get the thingID and access token of the registered thing from the kii_core.author member of the kii_t structure.

Authenticating Things

To use registered things again after the information in the kii_t structure is lost because of an event such as shutdown, authenticate the thing with the kii_thing_authenticate function.

/* Set thing information. */
#define VENDOR_THING_ID "rBnvSPOXBDF9r29GJeGS"
#define PASSWORD "123ABC"

int ret;

/* Authenticate the thing. */
ret = kii_thing_authenticate(&kii, VENDOR_THING_ID, PASSWORD);
if (ret != 0) {
  /* Handle the error. */
  return;
}

printf("thingID:%s\n", kii.kii_core.author.author_id);
printf("access token:%s\n", kii.kii_core.author.access_token);

In the above example, the thing is authenticated with vendorThingID "rBnvSPOXBDF9r29GJeGS" and password "123ABC". These are the credentials used in the registration example.

As with the registration, if the authentication succeeds, the kii_t structure has the thingID and access token. You can get these data from the kii_core.author structure.

Setting the Access Token

At the second and subsequent boots, you can get the same result as authenticating the thing with the kii_thing_authenticate function by restoring the saved access token and thingID from non-volatile memory.

At the successful authentication, save the kii.kii_core.author.author_id and kii.kii_core.author.access_token as a string with a method you prepare in the user program. Note that the author_id and access_token are declared as a char array of 128 bytes.

/* Securely store the thing ID and the access token in the persistent storage with your own function. */
storeToken(kii.kii_core.author.author_id, kii.kii_core.author.access_token);

At the subsequent boot, restore the saved data to the initialized kii_t structure as below.

int ret;

/* Get the thing ID and the access token from the persistent storage with your own function. */
ret = getStoredToken(&kii.kii_core.author.author_id, &kii.kii_core.author.access_token);
if (ret != 0) {
  /* Handle the error. */
  return;
}

/* Do something. */

The sample program built in Building Sample Programs restores the credentials with this method. Instead of saving them in non-volatile memory, it directly restores the hard-coded author_id and access_token with #define. Update the values defined with #define to the values that are assigned at the thing registration or authentication.