Skip to content

SDK access of LWS Linux system

BigBang-Core edited this page Dec 29, 2020 · 1 revision

lws-iot-sdk-c

Installation

Ubuntu Installation

  1. Install libssl sudo apt install libssl-dev

  2. Install libmbedtls sudo apt install libmbedtls-dev

  3. Install libsodium sudo apt install libsodium-dev

  4. Download SDK in C Programming Language

Functional Interface

First quote lwsiot.h in your project and add -llwsiot in the compile option. SDK consists of 11 functional interfaces to maneuver transactions in the BigBang blockchain system.

1. lwsiot_client_new

To create an LWS client instance, you need to specify the client ID and client type. At present, client type only supports AWS IoT core.

LwsClient *lwsiot_client_new(char *id, ClientType type);

2. lwsiot_secret_set

To set the private key, you need to specify the callback function secret_ set_ cb, if set to NULL, the context parameter will be set to the private key by default.

LwsIoTError lwsiot_secret_set(LwsClient *lws_client, SecretSetCallback secret_set_cb, void *context);

3.lwsiot_certs_set

Configure certificate directory. The files need to be named as AmazonRootCA1.pem, certificate.pem.crt, private.pem.key, and public.pem.key respectively. There will be following improvement to make this part configurable.

LwsIoTError lwsiot_certs_set(LwsClient *lws_client, char *path);

4.lwsiot_fork_set

Configure the BigBang blockchain branch where the transaction happens.

LwsIoTError lwsiot_fork_set(LwsClient *lws_client, char *fork, LwsForkSet set);

5.lwsiot_connect

Assign the name of node connected, server address, port number and connect to the server.

LwsIoTError lwsiot_connect(LwsClient *lws_client, char *lws, char *host, unsigned int port);

6.lwsiot_disconnect

Actively disconnect the client.

LwsIoTError lwsiot_disconnect(LwsClient *lws_client);

7. lwsiot_sync

LwsIoTError lwsiot_sync(LwsClient *lws_client);

Synchronize UTXO from LWS(light wallet service) to locality

8. lwsiot_abort

LwsIoTError lwsiot_abort(LwsClient *lws_client);

Stop synchronization of UTXO list.

9. lwsiot_uuid

LwsIoTError lwsiot_uuid(LwsClient *lws_client, unsigned char *uuid);

Produce UUID v4

10. lwsiot_send_tx

LwsIoTError lwsiot_send_tx(LwsClient *lws_client, char *address_hex, TxVchData *vch_data, int *out_nonce);

Create and send transaction to LWS.

11. lwsiot_destroy

void lwsiot_destroy(LwsClient *lws_client);

Destroy lwc instance.

** Sample codes**

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <time.h>
#include "lwsiot.h"

static float get_temperature() { return 25.0; }

LwsIoTError lwsiot_sys_init(LwsClient *lws_client, uint8_t *deviceID, uint8_t *guardID, uint8_t *smecID);
LwsIoTError lwsiot_device_info(LwsClient *lws_client, uint8_t *deviceID);

//Device deployment information
LwsIoTError lwsiot_deploy(LwsClient *lws_client, uint8_t deviceLen, uint8_t *deviceID, char *deploy_ini);

//Program download
LwsIoTError lwsiot_download(LwsClient *lws_client, uint8_t *deviceID);

int main(int argc, char **argv)
{
    // Configure certificate directory
    char current_wd[PATH_MAX + 1] = {'\0'};
    getcwd(current_wd, sizeof(current_wd));
    char certs_path[PATH_MAX] = {'\0'};
    sprintf(certs_path, "%s/%s", current_wd, "certs");

    // Address of target wallet
    char *address_hex = "da915f7d9e1b1f6ed99fd816ff977a7d1f17cc95ba0209eef770fb9d00638b49";
	// Device ID
    char *client_id = "lwc20200926";

    // Configure skd log file
    FILE *log_fp = fopen("./lwc.log", "a");
    // Create client
    LwsClient *lws_client = lwsiot_client_new(client_id, AwsClient, log_fp);
    if (lws_client) {
        // Configure blockchain business branch
        char *fork = "0000000006854ebdc236f48dbbe5c87312ea0abd7398888374b5ee9a5eb1d291";
        // Configure transaction private key
        char *key = "9df809804369829983150491d1086b99f6493356f91ccc080e661a76a976a4ee";
        // Configure LWS connected
        char *lws = "LWS20200926";
		// Configure Broker address and port
        char *host = "a2ckh7mlpc878s-ats.iot.us-west-2.amazonaws.com";
        int port = 443;
		
        // Bind certificate
        lwsiot_certs_set(lws_client, certs_path);
        // Bind private key
        lwsiot_secret_set(lws_client, NULL, key);
        // Bind branch
        lwsiot_fork_set(lws_client, fork, LwsForkAdd);
        // Connect
        LwsIoTError rc = lwsiot_connect(lws_client, lws, host, port);
        if (LwsSuccess != rc) {
            printf("mqtt cant open: %s:%d\n", lws, rc);
            return EXIT_FAILURE;
        } else {
            printf("mqtt open successful\n");
        }
		
        // Establish protocol tunnel
        rc = lwsiot_service_req(lws_client);
        if (LwsSuccess != rc) {
            printf("send ServiceReq failed. rc:%d\n", rc);
            return EXIT_FAILURE;
        }
		
        // First synchronization of UTXO
        rc = lwsiot_sync(lws_client);
        if (LwsSuccess != rc) {
            printf("sync error: %s:%d\n", lws, rc);
            return EXIT_FAILURE;
        }

        //"josn" base64 describe
        char *b64_json = "anNvbg==";

        int i = 0;
        while (1) {
            // Create TxVchData
            TxVchData vch_data;
            unsigned char uuid[16] = {'\0'};
            lwsiot_uuid(lws_client, uuid);
            memcpy(vch_data.uuid, uuid, 16);

            time_t now_time;
            time(&now_time);
            memcpy(vch_data.timestamp, &now_time, sizeof(now_time));

            vch_data.desc = b64_json;
            vch_data.desc_size = strlen(b64_json);
            
            // Simulate sensor data
            char json[100] = {'\0'};
            sprintf(json, "{\"temperature\": %f}", get_temperature());

            vch_data.len = strlen(json);
            vch_data.data = json;

            int nonce = 0;
            // Send transaction
            rc = lwsiot_send_tx(lws_client, address_hex, &vch_data, &nonce);
            if (LwsSuccess != rc) {
                printf("##main# send tx error:%d\n", rc);

                if (LwsCreateTxNoAvailableUTXO == rc) {
                    lwsiot_sync(lws_client);
                }

                sleep(5);
                continue;
            }

            sleep(5);
        };
		
        lwsiot_destroy(lws_client);
        lws_client = NULL;
    }

    return EXIT_SUCCESS;
}

Clone this wiki locally