-
Notifications
You must be signed in to change notification settings - Fork 0
LWS Linux系统SDK接入
BigBang-Core edited this page Dec 4, 2020
·
1 revision
-
安装libssl
sudo apt install libssl-dev
-
安装libmbedtls
sudo apt install libmbedtls-dev
-
安装libsodium
sudo apt install libsodium-dev
-
下载C语言版本SDK
首先在你的项目中引用lwsiot.h
,并在编译选项中加入-llwsiot
,SDK中包含了11个函数接口用于操作BigBang区块链系统中的交易操作。
LwsClient *lwsiot_client_new(char *id, ClientType type);
创建lws client实例,需要指定client id和client type。client type目前只支持AWS IoT core。
LwsIoTError lwsiot_secret_set(LwsClient *lws_client, SecretSetCallback secret_set_cb, void *context);
设置私钥,需要指定回调函数secret_set_cb
,如果设置为NULL则context
参数将被默认设置为私钥。
LwsIoTError lwsiot_certs_set(LwsClient *lws_client, char *path);
设置证书目录,文件名需要分别被设置为AmazonRootCA1.pem,certificate.pem.crt, private.pem.key,public.pem.key,这部分后续会有提升使其可配置。
LwsIoTError lwsiot_fork_set(LwsClient *lws_client, char *fork, LwsForkSet set);
设置交易发生的BigBang区块链分支。
LwsIoTError lwsiot_connect(LwsClient *lws_client, char *lws, char *host, unsigned int port);
指定连接的节点名称、服务端地址和端口号并连接服务器。
LwsIoTError lwsiot_disconnect(LwsClient *lws_client);
主动断开客户端连接。
LwsIoTError lwsiot_sync(LwsClient *lws_client);
从LWS(light wallet service)同步UTXO到本地。
LwsIoTError lwsiot_abort(LwsClient *lws_client);
停止同步UTXO列表。
LwsIoTError lwsiot_uuid(LwsClient *lws_client, unsigned char *uuid);
生成UUID v4
LwsIoTError lwsiot_send_tx(LwsClient *lws_client, char *address_hex, TxVchData *vch_data, int *out_nonce);
创建交易并发送到LWS。
void lwsiot_destroy(LwsClient *lws_client);
Destroy lwc instance.
销毁LWC实例。
#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);
// 设备部署信息
LwsIoTError lwsiot_deploy(LwsClient *lws_client, uint8_t deviceLen, uint8_t *deviceID, char *deploy_ini);
// 程序下载
LwsIoTError lwsiot_download(LwsClient *lws_client, uint8_t *deviceID);
int main(int argc, char **argv)
{
// 设置证书目录
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");
// 目标钱包地址
char *address_hex = "da915f7d9e1b1f6ed99fd816ff977a7d1f17cc95ba0209eef770fb9d00638b49";
// 设备ID
char *client_id = "lwc20200926";
// 设置skd日志文件
FILE *log_fp = fopen("./lwc.log", "a");
// 创建client
LwsClient *lws_client = lwsiot_client_new(client_id, AwsClient, log_fp);
if (lws_client) {
// 设置区块链业务分支
char *fork = "0000000006854ebdc236f48dbbe5c87312ea0abd7398888374b5ee9a5eb1d291";
// 设置交易私钥
char *key = "9df809804369829983150491d1086b99f6493356f91ccc080e661a76a976a4ee";
// 设置连接的LWS
char *lws = "LWS20200926";
// 设置Broker地址和端口
char *host = "a2ckh7mlpc878s-ats.iot.us-west-2.amazonaws.com";
int port = 443;
// 绑定证书
lwsiot_certs_set(lws_client, certs_path);
// 绑定私钥
lwsiot_secret_set(lws_client, NULL, key);
// 绑定分支
lwsiot_fork_set(lws_client, fork, LwsForkAdd);
// 连接
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");
}
// 建立协议通道
rc = lwsiot_service_req(lws_client);
if (LwsSuccess != rc) {
printf("send ServiceReq failed. rc:%d\n", rc);
return EXIT_FAILURE;
}
// 首次同步未花费交易
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) {
// 创建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);
// 模拟传感器数据
char json[100] = {'\0'};
sprintf(json, "{\"temperature\": %f}", get_temperature());
vch_data.len = strlen(json);
vch_data.data = json;
int nonce = 0;
// 发送交易
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;
}