Skip to content

Commit

Permalink
feat(Simcom-A7670C):
Browse files Browse the repository at this point in the history
Add platone to Simcom-A7670C #1377
BoATE-872
  • Loading branch information
zt222 committed Feb 21, 2023
1 parent b229bf3 commit 1530abb
Show file tree
Hide file tree
Showing 7 changed files with 741 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
******************************************************************************
* @file sc_application.c
* @author SIMCom OpenSDK Team
* @brief Source code for SIMCom OpenSDK application, void userSpace_Main(void * arg) is the app entry for OpenSDK application,customer should start application from this call.
******************************************************************************
* @attention
*
* Copyright (c) 2022 SIMCom Wireless.
* All rights reserved.
*
******************************************************************************
*/

/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include "sc_ApiMap.h"
#include "simcom_debug.h"
#include "simcom_os.h"
#include "simcom_uart.h"

extern void sAPP_SimcomUIDemo(void);
extern void sAPP_HelloWorldDemo(void);
extern void sAPP_UrcTask(void);
extern void sAPP_UartTask(void);
extern void sAPP_UsbVcomTask(void);
extern void sAPP_PlatoneDemo(void);

/**
* @brief OpenSDK app entry.
* @param Pointer arg
* @note This is the app entry,like main(),all functions must start from here!!!!!!
* @retval void
*/
void userSpace_Main(void *arg)
{
/* simcom api init. Do not modify! */
ApiMapInit(arg);
// sAPI_enableDUMP();
/* UI demo task for customer with CLI method for all demo running,
customer need to define SIMCOM_UI_DEMO_TO_USB_AT_PORT or
SIMCOM_UI_DEMO_TO_UART1_PORT to select hardware interface.
*/
sleep(5);//Wait for USB initialization to complete and print catstudio log.

// sAPP_SimcomUIDemo();

sAPP_UartTask();
sAPP_UrcTask();
sAPP_UsbVcomTask();
// sAPP_HelloWorldDemo();
sAPP_PlatoneDemo();
printf("user app is running...");

}
#define _appRegTable_attr_ __attribute__((unused, section(".userSpaceRegTable")))

#define appMainStackSize (1024*10)
userSpaceEntry_t userSpaceEntry _appRegTable_attr_ = {NULL, NULL, appMainStackSize, 30, "userSpaceMain", userSpace_Main, NULL };
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef __MBTK_BOAT_PLATONE_DEMO_H__
#define __MBTK_BOAT_PLATONE_DEMO_H__

#include "boattypes.h"

BOAT_RESULT PlatoneDemo(void);

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/******************************************************************************
This file is generated from contract ABI. DO NOT modify it by hand.
******************************************************************************/


// Generated C function interface from smart contract ABI

#include "boatiotsdk.h"

BCHAR * my_contract_cpp_abi_setName(BoatPlatoneTx *tx_ptr, BCHAR* msg);
BCHAR * my_contract_cpp_abi_getName(BoatPlatoneTx *tx_ptr);
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
/* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
* All rights reserved.
*
* This software is supplied "AS IS" without any warranties.
* RDA assumes no responsibility or liability for the use of the software,
* conveys no license or title under any patent, copyright, or mask work
* right to the product. RDA reserves the right to make changes in the
* software without notification. RDA also make no representation or
* warranty that such application will be suitable for the specified use
* without further testing or modification.
*/

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "api_platone.h"
#include "boatiotsdk.h"
#include "my_contract.cpp.abi.h"
#include "simcom_os.h"
#include "simcom_ntp_client.h"
#include "simcom_debug.h"

sTaskRef PlatoneProcesser;
static UINT8 PlatoneProcesserStack[30 * 1024];
sMsgQRef ntpUIResp_msgq;

const char *native_demoKey = "0xfcf6d76706e66250dbacc9827bc427321edb9542d58a74a67624b253960465ca";

/**
* test node url
*/
const BCHAR *demoUrl = "http://116.236.47.90:7545";

/**
* transfer recipient address
*/
const BCHAR *demoRecipientAddress = "0xaac9fb1d70ee0d4b5a857a28b9c3b16114518e45";

BoatPlatoneWallet *g_platone_wallet_ptr;
BUINT8 keypairIndex = 0;
BUINT8 networkIndex = 0;

__BOATSTATIC BOAT_RESULT platone_createKeypair(BCHAR *keypairName)
{
BOAT_RESULT result = BOAT_SUCCESS;
BoatKeypairPriKeyCtx_config keypair_config = {0};
BUINT8 binFormatKey[32] = {0};

(void)binFormatKey; //avoid warning

//sAPI_Debug(">>>>>>>>>> wallet format: external injection[native].");
keypair_config.prikey_genMode = BOAT_KEYPAIR_PRIKEY_GENMODE_EXTERNAL_INJECTION;
keypair_config.prikey_format = BOAT_KEYPAIR_PRIKEY_FORMAT_NATIVE;
keypair_config.prikey_type = BOAT_KEYPAIR_PRIKEY_TYPE_SECP256K1;
UtilityHexToBin(binFormatKey, 32, native_demoKey, TRIMBIN_TRIM_NO, BOAT_FALSE);
keypair_config.prikey_content.field_ptr = binFormatKey;
keypair_config.prikey_content.field_len = 32;

/* create platone wallet */
result = BoatKeypairCreate( &keypair_config, keypairName,BOAT_STORE_TYPE_RAM);

if (result < 0)
{
//sAPI_Debug("create one-time keypair failed.");
return BOAT_ERROR_WALLET_CREATE_FAIL;
}
keypairIndex = result;

return BOAT_SUCCESS;
}

__BOATSTATIC BOAT_RESULT createOnetimeNetwork(void)
{
BOAT_RESULT result = BOAT_SUCCESS;
BoatPlatoneNetworkConfig network_config = {0};

network_config.chain_id = 1;
network_config.eip155_compatibility = BOAT_FALSE;
strncpy(network_config.node_url_str, demoUrl, BOAT_PLATONE_NODE_URL_MAX_LEN - 1);

/* create platone network */
result = BoatPlatoneNetworkCreate( &network_config, BOAT_STORE_TYPE_RAM);

if (result < 0)
{
//sAPI_Debug("create one-time wallet failed.");
return BOAT_ERROR_WALLET_CREATE_FAIL;
}
networkIndex = result;

return BOAT_SUCCESS;
}

BOAT_RESULT platone_call_mycontract(BoatPlatoneWallet *wallet_ptr)
{
BCHAR *result_str;
BoatPlatoneTx tx_ctx;
BOAT_RESULT result;

/* Set Contract Address */
result = BoatPlatoneTxInit(wallet_ptr, &tx_ctx, BOAT_TRUE, NULL,
"0x333333",
(BCHAR *)demoRecipientAddress,
BOAT_PLATONE_TX_TYPE_CONTRACT_NULL_TERMED_STR);

if (result != BOAT_SUCCESS)
{
//sAPI_Debug("BoatPlatoneTxInit fails.");
return BOAT_ERROR_WALLET_INIT_FAIL;
}

result_str = my_contract_cpp_abi_setName(&tx_ctx, "HelloWorld");
if (result_str == NULL)
{
//sAPI_Debug("my_contract_cpp_abi_setName failed: %s.", result_str);
return BOAT_ERROR;
}
//sAPI_Debug("setName returns: %s", result_str);

result_str = my_contract_cpp_abi_getName(&tx_ctx);
if (result_str == NULL)
{
//sAPI_Debug("my_contract_cpp_abi_getName failed: %s.", result_str);
return BOAT_ERROR;
}
//sAPI_Debug("getName returns: %s", result_str);

return BOAT_SUCCESS;
}

BOAT_RESULT PlatoneDemo(void)
{
BOAT_RESULT result = BOAT_SUCCESS;
boat_try_declare;
/* step-1: Boat SDK initialization */
BoatIotSdkInit();

/* step-2: create platone wallet */
//sAPI_Debug(">>>>>>>>>> wallet type: create one-time wallet.");
result = platone_createKeypair("keypair00");
if (result != BOAT_SUCCESS)
{
//sAPI_Debug("platoneWalletPrepare_create failed : %d.", result);
//return -1;
boat_throw(result, platone_demo_catch);
}
result = createOnetimeNetwork();
if (result != BOAT_SUCCESS)
{
//sAPI_Debug("platoneWalletPrepare_create failed : %d.", result);
//return -1;
boat_throw(result, platone_demo_catch);
}
if (result != BOAT_SUCCESS)
{
//sAPI_Debug("platoneWalletPrepare_create failed : %d.", result);
//return -1;
boat_throw(result, platone_demo_catch);
}
g_platone_wallet_ptr = BoatPlatoneWalletInit(keypairIndex,networkIndex);
if(g_platone_wallet_ptr == NULL){
// sAPI_Debug("BoatPlatoneWalletInit fail");
boat_throw(BOAT_ERROR, platone_demo_catch);
}
/* step-3: execute 'platone_call_mycontract' */
result = platone_call_mycontract(g_platone_wallet_ptr);
if (result != BOAT_SUCCESS)
{
//sAPI_Debug("platone mycontract access Failed: %d.", result);
}
else
{
//sAPI_Debug("platone mycontract access Passed.");
}
boat_catch(platone_demo_catch)
{
}
/* step-4: Boat SDK Deinitialization */
BoatIotSdkDeInit();

return result;
}

BOAT_RESULT NtpService(void)
{
SCsysTime_t currUtcTime;
char buff[220]={0};
BOAT_RESULT result = BOAT_SUCCESS;

// SIM_MSG_T ntp_result = {SC_SRV_NONE, -1, 0, NULL};
if(NULL == ntpUIResp_msgq)
{
SC_STATUS status;
status = sAPI_MsgQCreate(&ntpUIResp_msgq, "ntpUIResp_msgq", sizeof(SIM_MSG_T), 4, SC_FIFO);
if(SC_SUCCESS != status)
{
sAPI_Debug("[CNTP]msgQ create fail\n");
sAPI_Debug("\r\nNTP Update Fail!\r\n");
}
}

memset(&currUtcTime,0,sizeof(currUtcTime));

sAPI_GetSysLocalTime(&currUtcTime);
sAPI_Debug("[CNTP] sAPI_GetSysLocalTime %d - %d - %d - %d : %d : %d %d\n",currUtcTime.tm_year,currUtcTime.tm_mon,currUtcTime.tm_mday,
currUtcTime.tm_hour,currUtcTime.tm_min,currUtcTime.tm_sec,currUtcTime.tm_wday);
result = sAPI_NtpUpdate(SC_NTP_OP_SET, "ntp3.aliyun.com", 32, NULL); //Unavailable addr may cause long time suspend
sAPI_Debug("SC_NTP_OP_SET result[%d]\n",result);

result = sAPI_NtpUpdate(SC_NTP_OP_GET, buff, 0, NULL);
sAPI_Debug("SC_NTP_OP_GET result[%d] buff[%s]\n", result, buff);

result = sAPI_NtpUpdate(SC_NTP_OP_EXC, NULL, 0, ntpUIResp_msgq);
sAPI_Debug("SC_NTP_OP_EXC result[%d]\n",result );

memset(&currUtcTime,0,sizeof(currUtcTime));
sAPI_GetSysLocalTime(&currUtcTime);
sAPI_Debug("[CNTP] sAPI_GetSysLocalTime %d - %d - %d - %d : %d : %d %d\n",currUtcTime.tm_year,currUtcTime.tm_mon,currUtcTime.tm_mday,
currUtcTime.tm_hour,currUtcTime.tm_min,currUtcTime.tm_sec,currUtcTime.tm_wday);

return result;
}

void sTask_PlatoneProcesser(void* argv)
{
BOAT_RESULT result = BOAT_SUCCESS;
sAPI_DelayUs(3000000);
result = NtpService();
if(result != BOAT_SUCCESS)
{
sAPI_Debug("NTP get time failed : %d.", result);
}
result = PlatoneDemo();
if(result != BOAT_SUCCESS)
{
sAPI_Debug("Platone Demo failed : %d.", result);
}

}

void sAPP_PlatoneDemo(void)
{
SC_STATUS status = SC_SUCCESS;

status = sAPI_TaskCreate(&PlatoneProcesser, PlatoneProcesserStack, 30 * 1024, 150, "PlatoneProcesser",sTask_PlatoneProcesser,(void *)0);
if(SC_SUCCESS != status)
{
sAPI_Debug("Task create fail,status = [%d]",status);
}
}
Loading

0 comments on commit 1530abb

Please sign in to comment.