恒玄BES2600W SoC 的欧智通的单板基于Openharmony v3.0的底座,其测试的样例代码是基于恒玄公司开发的轻量带屏显示产品样例代码,主要包括图形、软总线等特性产品的开发。

基础介绍:

​ L0的系统基于Liteos-m的m内核,网络使用lwip,其运行的系统为单进程,添加的应用方式采用APP_FEATURE_INIT宏将应用添加进系统中运行。

1 分布式软总线

1.1 应用入口main.c

// main.c
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "cmsis_os2.h"
#include "ohos_init.h"

#include "wifi/connectwifi.h"
#include "bussiness/JoinLnnTransmit.h"

#define TEST_PKG_NAME "DSoftBus_APP"
#define MY_SESSION_NAME "mySessionName"
#define PEER_SESSION_NAME "peerSessionName"
extern int g_wifiState;
extern int g_jonLNNState;
extern int g_readySession;

void DSoftBus(void)
{
    printf("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n");

    IotWifiConnect();

    while(g_wifiState != 0)
    {
        printf("----------wifi Is not connected-------------\n");
        osDelay(100);
    }

    InitSoftBusThread();

    while (GetServerIsInit() == false)
    {
        printf("----------Server Is not Init-------------\n");
        sleep(1);
    }

    printf("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n");

    DsoftJonnLNN(TEST_PKG_NAME);

    while(g_jonLNNState != 0)
    {
        printf("----------Joni LNN Is not Succ-------------\n");
        DsoftJonnLNN(TEST_PKG_NAME);
        sleep(1);
    }

    printf("CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n");

    if(ReadySession(TEST_PKG_NAME, MY_SESSION_NAME, PEER_SESSION_NAME) != 0)
    {
        return;
    }

    while(g_readySession != 0)
    {
        printf("----------Session Is not Ready-------------\n");
        sleep(1);
    }

    const char *data = "Bestech testdata YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";
    uint32_t len = strlen(data);

    DsoftSendBytes(data, len);

    DsoftSendMessage(data, len);

    DsoftCloseSession();
}

APP_FEATURE_INIT(DSoftBus);

1.2 wifi连接

通过连接固定的AP名amt-hdc-2022,密码固化为hmg20220905

// connectwifi.c
#include <stdlib.h>
#include "connectwifi.h"
#include "cmsis_os2.h"
#include "ohos_init.h"
#include "lwip/netif.h"
#include "lwip/netifapi.h"
#include "wifi_device.h"
#include "wifi_device_config.h"
#include "wifi_error_code.h"

#define SELECT_WIFI_SSID "amt-hdc-2022"
#define SELECT_WIFI_PASSWORD  "hmg20220905"

int g_wifiState = -1;
#define SCAN_OPTION 1
#define SCAN_SUCCESS_FLAGS 1U
osEventFlagsId_t eventId;
WifiEvent g_wifiEventHandler = {0};

static void OnWifiScanStateChangedHandler(int state, int size)
{
    (void)state;
    if (state > 0) {
#if SCAN_OPTION
        osEventFlagsSet(eventId, SCAN_SUCCESS_FLAGS);
#endif
        printf("wifi scan result %d\r\n", size);
    } else {
        printf("wifi scan failed\r\n");
    }
}

static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info)
{
    (void)info;
    if (state > 0) {
        printf("callback function for wifi connect\r\n");
    } else {
        printf("connect error,please check password\r\n");
    }
}

static void OnHotspotStaJoinHandler(StationInfo *info)
{
    (void)info;
    printf("STA join AP\n");
}

static void OnHotspotStaLeaveHandler(StationInfo *info)
{
    (void)info;
    printf("HotspotStaLeave:info is null.\n");
}

static void OnHotspotStateChangedHandler(int state)
{
    printf("HotspotStateChanged:state is %d.\n", state);
}

static WifiErrorCode WiFiInit(void)
{
    g_wifiEventHandler.OnWifiScanStateChanged = OnWifiScanStateChangedHandler;
    g_wifiEventHandler.OnWifiConnectionChanged = OnWifiConnectionChangedHandler;
    g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler;
    g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler;
    g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler;
    return RegisterWifiEvent(&g_wifiEventHandler);
}

int hi_wifi_start_sta(void)
{
    WifiDeviceConfig select_ap_config = {0};
    strcpy(select_ap_config.ssid, SELECT_WIFI_SSID);
    strcpy(select_ap_config.preSharedKey, SELECT_WIFI_PASSWORD);
    select_ap_config.securityType = WIFI_SEC_TYPE_PSK;

    printf("<--WifiSTATask Init-->\r\n");

    if (WiFiInit() != WIFI_SUCCESS) {
        printf("WiFiInit failed!\r\n");
        return - 1;
    }

    if (EnableWifi() != WIFI_SUCCESS) {
        printf("EnableWifi failed\r\n");
        return -1;
    }

    if (IsWifiActive() == WIFI_STA_NOT_ACTIVE) {
        printf("Wifi station is not activated.\n");
        return -1;
    }

    int networkId = -1;
    if (AddDeviceConfig(&select_ap_config, &networkId) != WIFI_SUCCESS) {
        printf("AddDeviceConfig failed!\r\n");
        return -1;
    }
    printf("Connecting to %s...\r\n", SELECT_WIFI_SSID);
    WifiErrorCode error = ConnectTo(networkId); ///< block and retry
    printf("WiFi connect %s!\r\n", (error == WIFI_SUCCESS) ? "succeed" : "failed");

    g_wifiState = 0;
    for (;;) {
        osDelay(100);
    }
    printf("g_wifiState: %d\r\n", g_wifiState);
    sleep(3000);

    return 0;
}

void IotWifiConnect(void)
{
    printf("\n\n entry wifi connect.\r\n");
    osThreadAttr_t attr;
    attr.name = "IotWifiConnect task";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 10240;
    attr.priority = 24;

    if (osThreadNew((osThreadFunc_t)hi_wifi_start_sta, NULL, &attr) == NULL) {
        printf("Failed to create WifiSTATask!\n");
    }
}

1.3 软总线接口调用

// JoinLnnTransmit.h
#ifndef JONILNN_TRANSMIT_H
#define JONILNN_TRANSMIT_H

void InitSoftBusThread(void);

void DsoftJonnLNN(const char* pkgName);

int ReadySession(const char* pkgName, const char * mySessionName, const char *peerSessionName);

int DsoftSendBytes(const void *data, unsigned int len);

int DsoftSendMessage(const void *data, unsigned int len);

void DsoftCloseSession(void);
#endif
// JoinLnnTransmit.c
#include "JoinLnnTransmit.h"
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "cmsis_os2.h"
#include "ohos_init.h"
#include "softbus_server_frame.h"
#include "softbus_common.h"
#include "session.h"

int g_jonLNNState = -1;
const char* g_networkid;
int g_sessionId = 0;
int g_readySession = -1;

void InitSoftBusThread(void)
{
    osThreadAttr_t attr;
    attr.name = "dsoftbus task";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 65536;
    attr.priority = 24;

    if (osThreadNew((osThreadFunc_t)InitSoftBusServer, NULL, &attr) == NULL) {
        printf("Failed to create WifiSTATask!\n");
    }
}

void OnJoinLNNDone(ConnectionAddr *addr, const char *networkId, int32_t retCode)
{
    (void)addr;
    (void)networkId;
    printf("ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ networkId is %s\n", networkId);
    (void)retCode;
    printf("ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ retCode is %d\n", retCode);

    if(retCode == 0) {
        g_jonLNNState = 0;
        g_networkid = networkId;
    }
}

void DsoftJonnLNN(const char* pkgName)
{
    int32_t ret = JoinLNN(pkgName, NULL, OnJoinLNNDone);
}

int SessionOpened(int sessionId, int result)
{
    printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX result is %d,sessionId is %d\n", result, sessionId);
    if(result == 0)
    {
        g_readySession = 0;
        g_sessionId = sessionId;
    }
    return 0;
}

void SessionClosed(int sessionId)
{
    printf("MMMMMMMMMMMMMMMMMMMMMMMMMMM sessionId is %d\n", sessionId);
}

void BytesReceived(int sessionId, const void *data, unsigned int dataLen)
{
    printf("MMMMMMMMMMMMMMMMMMMMMMMMMMM data is %s,sessionId is %d\n", data, sessionId);
}

void MessageReceived(int sessionId, const void *data, unsigned int dataLen)
{
    printf("MMMMMMMMMMMMMMMMMMMMMMMMMMM data is %s,sessionId is %d\n", data, sessionId);
}


void OnStreamReceived(int sessionId, const StreamData *data, const StreamData *ext,
        const StreamFrameInfo *param)
{

}

int ReadySession(const char* pkgName, const char * mySessionName, const char *peerSessionName)
{
    ISessionListener g_sessionlistener = {
        .OnSessionOpened = SessionOpened,
        .OnSessionClosed = SessionClosed,
        .OnBytesReceived = BytesReceived,
        .OnMessageReceived = MessageReceived,
        .OnStreamReceived = OnStreamReceived,
    };

    int32_t ret = CreateSessionServer(pkgName, mySessionName, &g_sessionlistener);
    printf("YYYYYYYYYYYYYYYYYYYYYYYYYYYYY ret is %d\n", ret);
    if(ret == 0)
    {
        SessionAttribute g_sessionAttr = {
            .dataType = TYPE_BYTES,
        };
        ret = OpenSession(mySessionName, peerSessionName, g_networkid, "g_groupid", &g_sessionAttr);

        printf("YYYYYYYYYYYYYYYYYYYYYYYYYYYYY ret is %d\n", ret);
    }

    return ret;
}

int DsoftSendBytes(const void *data, unsigned int len)
{
    int ret = SendBytes(g_sessionId, data, len);
    printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ret is %d\n", ret);
    return ret;
}

int DsoftSendMessage(const void *data, unsigned int len)
{
    int ret = SendMessage(g_sessionId, data, len);
    printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ret is %d\n", ret);
    return ret;
}

void DsoftCloseSession(void)
{
    CloseSession(g_sessionId);
}

通过以上示例可以将两台L0的恒玄设备通过分布式软总线进行发布、发现、并组网再发送或接收数据,而且如果将L2设备中软总线关于hichain认证部份进行剥离也可以说不进行认证的情况下,亦可以将一台L0设备与L2设备之间进行发布、发现、组网、数据发送、数据接收。

关于跳过hichain部份在Openharmony的不同版本中处理不同,关于此节会专门做一章节进行描述。

1.4 其它配置

// vendor/bestechnic/display_demo/config.json添加dsoftbus_test
"force_link_libs":[
  ...
  "dsoftbus_test"
]
// vendor\bestechnic\display_demo\tests\BUILD.gn注释bes_wifi_test
#   if (bes_wifi_test) {
#     deps += [ "wifi:wifi_test" ]
#     defines += [ "WIFI_TEST" ]
#   }
// vendor\bestechnic\display_demo\tests\dsoftbus\BUILD.gn修改后如下:
static_library("dsoftbus_test") {
  sources = [ 
    "main.c",
    "./wifi/connectwifi.c",
    "./bussiness/JoinLnnTransmit.c"
  ]

  include_dirs = [
    "./bussiness",
    "./wifi",
    "//commonlibrary/utils_lite/include",
    "//foundation/communication/wifi_lite/interfaces/wifiservice",
    "//foundation/communication/dsoftbus/interfaces/kits/common",
    "//foundation/communication/dsoftbus/interfaces/kits/transport",
    "//foundation/communication/dsoftbus/core/frame/common/include"
  ]
}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部