Initializing SDK

Initialize tio instance

We first initialize a tio instance. There are tio_updater_t and tio_handler_t in tio. Their initialization codes are as follows:

Initialize tio_updater_t instance

The tio_updater_t instance is used for updating states. The instance needs to be initialized as follows:

#define UPDATER_HTTP_BUFF_SIZE 1024
#define UPDATE_PERIOD_SEC 60

tio_updater_t updater;
char updater_buff[UPDATER_HTTP_BUFF_SIZE];
jkii_token_t updater_tokens[256];
jkii_resource_t updater_resource = {updater_tokens, 256};

tio_updater_init(&updater);

tio_updater_set_app(&updater, KII_APP_ID, KII_APP_HOST);

tio_updater_set_buff(&updater, updater_buff, UPDATER_HTTP_BUFF_SIZE);

tio_updater_set_interval(&updater, UPDATE_PERIOD_SEC);

tio_updater_set_json_parser_resource(&updater, updater_resource);

tio_updater_set_cb_sock_connect(&updater, sock_cb_connect, sock_ssl_ctx);
tio_updater_set_cb_sock_send(&updater, sock_cb_send, sock_ssl_ctx);
tio_updater_set_cb_sock_recv(&updater, sock_cb_recv, sock_ssl_ctx);
tio_updater_set_cb_sock_close(&updater, sock_cb_close, sock_ssl_ctx);

tio_updater_set_cb_task_create(&updater, task_create_cb_impl, NULL);

tio_updater_set_cb_delay_ms(&updater, delay_ms_cb_impl, NULL);
  • tio_updater_t

    A tio_updater_t instance that the SDK uses for storing the state updating information.

    In the subsequent function, we will pass the pointer to the initialized tio_updater_t instance. We need to preserve the initialized tio_updater_t instance while the program on the thing is running.

  • tio_updater_set_app

    Specify the AppID you've got on the developer portal in the KII_APP_ID. See Create an Application for the details.

    In the KII_APP_HOST, set the server location you've selected on the developer portal. Either specify the following URL directly, or specify the corresponding value ("JP","US","CN3", "SG", or "EU").

    • United States: https://api.kii.com/
    • Japan: https://api-jp.kii.com/
    • China: https://api-cn3.kii.com/
    • Singapore: https://api-sg.kii.com/
    • Europe: https://api-eu.kii.com/
  • tio_updater_set_buff

    Specify the buffer for HTTP communication during state update.

  • tio_updater_set_interval

    Specify the state update interval in seconds.

  • tio_updater_set_json_parser_resource

    Specify the resource for processing JSON during state update. The resource is set to a fixed length in the sample code. You can also allocate the resource dynamically. See tio_updater_set_cb_json_parser_resource method for more information.

  • tio_updater_set_cb_sock_*

    Specify callback functions for a socket communication during state update. You can pass any user data (e.g., sock_sll_xtc in the sample code) in each callback. You can get the user data in the callback functions.

  • tio_updater_set_cb_task_create

    Specify the callback function for task handling.

  • tio_updater_set_cb_delay_ms

    Specify the callback function for sleep operation.

Initialize tio_handler_t instance

The tio_handler_t instance is used for receiving commands. The instance needs to be initialized as follows:

#define HANDLER_HTTP_BUFF_SIZE 1024
#define HANDLER_MQTT_BUFF_SIZE 1024
#define HANDLER_KEEP_ALIVE_SEC 300

tio_handler_t handler;
char handler_buff[HANDLER_HTTP_BUFF_SIZE];
jkii_token_t handler_tokens[256];
jkii_resource_t handler_resource = {handler_tokens, 256};

tio_handler_init(&handler);

tio_handler_set_app(&handler, KII_APP_ID, KII_APP_HOST);

tio_handler_set_http_buff(&handler, handler_http_buff, HANDLER_HTTP_BUFF_SIZE);
tio_handler_set_mqtt_buff(&handler, handler_mqtt_buff, HANDLER_MQTT_BUFF_SIZE);

tio_handler_set_keep_alive_interval(&handler, HANDLER_KEEP_ALIVE_SEC);

tio_handler_set_json_parser_resource(&handler, handler_resource);

tio_handler_set_cb_sock_connect_http(&handler, http_cb_connect, http_ssl_ctx);
tio_handler_set_cb_sock_send_http(&handler, http_cb_send, http_ssl_ctx);
tio_handler_set_cb_sock_recv_http(&handler, http_cb_recv, http_ssl_ctx);
tio_handler_set_cb_sock_close_http(&handler, http_cb_close, http_ssl_ctx);

tio_handler_set_cb_sock_connect_mqtt(&handler, mqtt_cb_connect, mqtt_ssl_ctx);
tio_handler_set_cb_sock_send_mqtt(&handler, mqtt_cb_send, mqtt_ssl_ctx);
tio_handler_set_cb_sock_recv_mqtt(&handler, mqtt_cb_recv, mqtt_ssl_ctx);
tio_handler_set_cb_sock_close_mqtt(&handler, mqtt_cb_close, mqtt_ssl_ctx);

tio_handler_set_cb_task_create(&handler, task_create_cb_impl, NULL);

tio_handler_set_cb_delay_ms(&handler, delay_ms_cb_impl, NULL);
  • tio_handler_t

    A tio_handler_t instance that the SDK uses for receiving commands.

    In the subsequent function, we will pass the pointer to the initialized tio_handler_t instance. We need to preserve the initialized tio_handler_t instance while the program on the thing is running.

  • tio_handler_set_app

    Specify the AppID you've got on the developer portal in the KII_APP_ID. See Create an Application for the details.

    In the KII_APP_HOST, set the server location you've selected on the developer portal. Either specify the following URL directly, or specify the corresponding value ("JP","US","CN3", "SG", or "EU").

    • United States: https://api.kii.com/
    • Japan: https://api-jp.kii.com/
    • China: https://api-cn3.kii.com/
    • Singapore: https://api-sg.kii.com/
    • Europe: https://api-eu.kii.com/
  • tio_handler_set_http_buff

    Specify the buffer for HTTP communication during command reception.

  • tio_handler_set_mqtt_buff

    Specify the buffer for MQTT communication during command reception.

  • tio_handler_set_keep_alive_interval

    Specify the Keep-Alive interval of the mqtt communication during command reception in seconds.

  • tio_handler_set_json_parser_resource

    Specify the resource for processing JSON during command reception. The resource is set to a fixed length in the sample code. You can also allocate the resource dynamically. See tio_handler_set_cb_json_parser_resource method for more information.

  • tio_handler_set_cb_sock_*

    Specify callback functions for a socket communication during command reception. You can pass any user data in each callback. You can get the user data in the callback functions. There are two callback function sets for socket communications, one set for HTTP and another set for MQTT. If you are going to use both communications, set both callback function sets.

    Implement the callback function for tio_handler_set_cb_sock_recv_mqtt in blocking mode. If you implement the function in non-blocking mode, your code might fail to receive some commands.

  • tio_handler_set_cb_task_create

    Specify the callback function for task handling.

  • tio_handler_set_cb_delay_ms

    Specify the callback function for sleep operation.

The initialization is done once you are done with these settings. You might need, however, to make more configurations in some target environments. In this case, make the necessary configurations for your environments by using setter/getter methods defined in tio.h.