Skip to content

SDK access of bare LWC system(MCU)

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

Overview

When the blockchain module is connected to the MCU console as an extension, the module needs MCU’s communication capacity to communicate with LWS. In compiling, the firmware of MCU console needs to introduce two source files to support the generation and parsing of LWS protocol message. They are LWS_ Protocol. C and LWS_ Protocol. H, project address: lws-protocol-sdk-c.

The main control unit of MCU synchronizes the transaction status with LWS upward, and generates transaction data through the combination of Hardware Interface and Blockchain Module. For the method of interacting with hardware, please refer to safe hardware operation interface. This paper will focus on the process and method of communication between MCU master and LWS.

Interface Specification

Callback interface and corresponding registration function

1.NonceGet

When SDK is being initiated, a function that produces uint32_t-type random numbers needs to be registered. SDK will call this function when running to acquire random numbers.

typedef unsigned int (*NonceGet)(const void *ctx);
int hook_nonce_get(const NonceGet callback, void *ctx);

Implementation example:

// Implementation
unsigned int lwsiot_nonce_get(const void *ctx) { return lwsiot_rand(); }

// Register
hook_nonce_get(lwsiot_nonce_get, lws_client);

2. DatetimeGet

When SDK is being initiated, register a function that produces uint32_t-type timestamp. SDK will call this function when running to acquire timestamp.

typedef unsigned int (*DatetimeGet)(const void *ctx);
int hook_datetime_get(const DatetimeGet callback, void *ctx);

Implementation example:

// Implementation
unsigned int lwsiot_datetime_get(const void *ctx)
{
    time_t now_time;
    time(&now_time);
    return now_time;
}

// Register
hook_datetime_get(lwsiot_datetime_get, lws_client);

3.DeviceIDGet

Register a function to acquire device id, acquire id by calling this function when SDK is running.

typedef int (*DeviceIDGet)(const void *ctx, char *id);
int hook_device_id_get(const DeviceIDGet callback, void *ctx);

Implementation example:

// Implementation
static int lwsiot_device_id_get(const void *ctx, char *id)
{
    if (NULL != id) {
        strncpy(id, "12345", 5);
        return 0;
    }
    return -1;
}

// Register
hook_device_id_get(lwsiot_device_id_get, lws_client);
typedef int (*ForkGet)(const void *ctx, big_num fork_out);
int hook_fork_get(const ForkGet callback, void *ctx);
typedef int (*PublicKeyGet)(const void *ctx, ed25519_public_key pk_out);
int hook_public_key_get(const PublicKeyGet callback, void *ctx);
typedef int (*SharedKeyGet)(const void *ctx, const key_seed seed, const unsigned char *data, const size_t size, shared_key key);
int hook_shared_key_get(const SharedKeyGet callback, void *ctx);
typedef int (*Blake2bGet)(const void *ctx, const unsigned char *data, const size_t len, blake2b_hash hash);
int hook_blake2b_get(const Blake2bGet callback, void *ctx);
typedef int (*SignEd25519)(const void *ctx, const unsigned char *data, const size_t len, ed25519_signature signature);
int hook_public_sign_ed25519(const SignEd25519 callback, void *ctx);