OpenHarmony-wifi开发学习路径:https://laval.csdn.net/69159bca0e4c466a32e78763.html

EnableHotpot的主要流程:


所在文件:foundation\communication\wifi\wifi\frameworks\js\napi\src\wifi_napi_hotspot.cpp
NO_SANITIZE("cfi") napi_value EnableHotspot(napi_env env, napi_callback_info info)
{
    ErrCode ret = wifiHotspotPtr->EnableHotspot();
    WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_AP_CORE);
}

所在文件:foundation\communication\wifi\wifi\frameworks\native\src\wifi_hotspot_impl.cpp
ErrCode WifiHotspotImpl::EnableHotspot(const ServiceType type)
{
    return client_->EnableHotspot(type);
}

所在文件:foundation\communication\wifi\wifi\frameworks\native\src\wifi_hotspot_proxy.cpp
ErrCode WifiHotspotProxy::EnableHotspot(const ServiceType type)
{
    MessageOption option;
    MessageParcel data;
    MessageParcel reply;
    data.WriteInterfaceToken(GetDescriptor());
    data.WriteInt32(0);
    data.WriteInt32(static_cast<int>(type));
    int error = Remote()->SendRequest(static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_ENABLE_WIFI_AP), data,
        reply, option);

    return ErrCode(reply.ReadInt32());
}

所在文件:foundation\communication\wifi\wifi\services\wifi_standard\wifi_framework\wifi_manage\wifi_ap_sa\wifi_hotspot_stub.cpp
    handleFuncMap[static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_ENABLE_WIFI_AP)] = [this](uint32_t code,
        MessageParcel &data, MessageParcel &reply,
        MessageOption &option) { OnEnableWifiAp(code, data, reply, option); };
        
    
int WifiHotspotStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
    if (data.ReadInterfaceToken() != GetDescriptor()) {
        WIFI_LOGE("Hotspot stub token verification error: %{public}d", code);
        return WIFI_OPT_FAILED;
    }

    int exception = data.ReadInt32();

    HandleFuncMap::iterator iter = handleFuncMap.find(code);
    (iter->second)(code, data, reply, option);
    return 0;
}
    
void WifiHotspotStub::OnEnableWifiAp(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
    int32_t serviceType = data.ReadInt32();
    ErrCode ret = EnableHotspot(ServiceType(serviceType));
    reply.WriteInt32(0);
    reply.WriteInt32(ret);
    return;
}

所在文件:foundation\communication\wifi\wifi\services\wifi_standard\wifi_framework\wifi_manage\wifi_ap_sa\wifi_hotspot_service_impl.cpp
ErrCode WifiHotspotServiceImpl::EnableHotspot(const ServiceType type)
{
    ErrCode errCode = CheckCanEnableHotspot(type);
    if (errCode != WIFI_OPT_SUCCESS) {
        return errCode;
    }
    WifiManager::GetInstance().StopGetCacResultAndLocalCac(CAC_STOP_BY_AP_REQUEST);
    return  WifiManager::GetInstance().GetWifiTogglerManager()->SoftapToggled(1, m_id);
}

所在文件:foundation\communication\wifi\wifi\services\wifi_standard\wifi_framework\wifi_manage\wifi_sub_manage\wifi_toggler_manager.cpp
ErrCode WifiTogglerManager::SoftapToggled(int isOpen, int id)
{
    pWifiControllerMachine->SendMessage(CMD_SOFTAP_TOGGLED, isOpen, id);
    return WIFI_OPT_SUCCESS;
}


所在文件:foundation\communication\wifi\wifi\services\wifi_standard\wifi_framework\wifi_manage\wifi_controller\wifi_controller_state_machine.cpp
bool WifiControllerMachine::DisableState::ExecuteStateMsg(InternalMessagePtr msg)
{
    switch (msg->GetMessageName()) {
#ifdef FEATURE_AP_SUPPORT
        case CMD_SOFTAP_TOGGLED:
            if (msg->GetParam1()) {
                int id = msg->GetParam2();
                pWifiControllerMachine->MakeHotspotManager(id, true);
                pWifiControllerMachine->SwitchState(pWifiControllerMachine->pEnableState);
            }
            break;
#endif
    return true;
}


void WifiControllerMachine::MakeHotspotManager(int id, bool startTimer)
{
    hotspotMode = CalculateHotspotMode(id);
    if (hotspotMode == HotspotMode::SOFTAP && !softApManagers.IdExist(id)) {
        MakeSoftapManager(SoftApManager::Role::ROLE_SOFTAP, id);
        if (startTimer) {
            StartTimer(CMD_AP_START_TIME, SOFT_AP_TIME_OUT);
        }
        return;
    }
}

void WifiControllerMachine::MakeSoftapManager(SoftApManager::Role role, int id)
{
    auto softapmode = std::make_shared<SoftApManager>(role, id);
    softapmode->RegisterCallback(WifiManager::GetInstance().GetWifiTogglerManager()->GetSoftApCallback());
    softapmode->InitSoftapManager();
    softApManagers.AddManager(softapmode);
}

所在文件:foundation\communication\wifi\wifi\services\wifi_standard\wifi_framework\wifi_manage\wifi_controller\softap_manager.cpp
ErrCode SoftApManager::InitSoftapManager()
{
    pSoftapManagerMachine = new (std::nothrow) SoftapManagerMachine();
    pSoftapManagerMachine->InitSoftapManagerMachine()
    pSoftapManagerMachine->RegisterCallback(mcb);
    pSoftapManagerMachine->SendMessage(SOFTAP_CMD_START, static_cast<int>(curRole), mid);
    return WIFI_OPT_SUCCESS;
}

所在文件:foundation\communication\wifi\wifi\services\wifi_standard\wifi_framework\wifi_manage\wifi_controller\softap_manager_state_machine.cpp
bool SoftapManagerMachine::IdleState::ExecuteStateMsg(InternalMessagePtr msg)
{
    switch (msg->GetMessageName()) {
        case SOFTAP_CMD_START:
            HandleStartInIdleState(msg);
            break;
        case SOFTAP_CMD_STOP:
            pSoftapManagerMachine->StopSoftap();
            break;
        default:
            break;
    }
    return true;
}

void SoftapManagerMachine::IdleState::HandleStartInIdleState(InternalMessagePtr msg)
{
    mid = msg->GetParam2();
    WifiServiceScheduler::GetInstance().AutoStartApService(mid, pSoftapManagerMachine->ifaceName);
    pSoftapManagerMachine->SwitchState(pSoftapManagerMachine->pStartedState);
}

所在文件:foundation\communication\wifi\wifi\services\wifi_standard\wifi_framework\wifi_manage\wifi_controller\wifi_service_scheduler.cpp
ErrCode WifiServiceScheduler::AutoStartApService(int instId, std::string &softApIfName)
{
    WifiOprMidState apState = WifiConfigCenter::GetInstance().GetApMidState(instId);
    if (apState != WifiOprMidState::CLOSED) {
        if (apState == WifiOprMidState::CLOSING) {
            return WIFI_OPT_FAILED;
        } else {
            return WIFI_OPT_SUCCESS;
        }
    }
#ifdef HDI_CHIP_INTERFACE_SUPPORT
    std::string ifaceName = "";
    if (softApIfaceNameMap.count(instId) > 0) {
        ifaceName = softApIfaceNameMap[instId];
    }
    if (ifaceName.empty() && !HalDeviceManager::GetInstance().CreateApIface(
        [this](std::string &destoryIfaceName, int createIfaceType) {
            this->SoftApIfaceDestoryCallback(destoryIfaceName, createIfaceType);
        },
        ifaceName)) {
        WIFI_LOGE("AutoStartApService, create iface failed!");
        return WIFI_OPT_FAILED;
    }
    WifiConfigCenter::GetInstance().SetApIfaceName(ifaceName);
    softApIfName = ifaceName;
    softApIfaceNameMap.insert(std::make_pair(instId, ifaceName));
#endif
    WifiConfigCenter::GetInstance().SetApMidState(apState, WifiOprMidState::OPENING, 0);
    ErrCode errCode = TryToStartApService(instId);
    WifiManager::GetInstance().GetWifiHotspotManager()->StopUnloadApSaTimer();
    return WIFI_OPT_SUCCESS;
}

ErrCode WifiServiceScheduler::TryToStartApService(int instId)
{
    ErrCode errCode = WIFI_OPT_FAILED;
    do {
        // 记载WIFI_SERVICE_P2P
        WifiServiceManager::GetInstance().CheckAndEnforceService(WIFI_SERVICE_AP);
        IApService *pService = WifiServiceManager::GetInstance().GetApServiceInst(instId);
        errCode = pService->RegisterApServiceCallbacks(
            WifiManager::GetInstance().GetWifiHotspotManager()->GetApCallback());
        errCode = pService->RegisterApServiceCallbacks(WifiCountryCodeManager::GetInstance().GetApCallback());
        errCode = pService->EnableHotspot();
    } while (false);
    return errCode;
}

所在文件:foundation\communication\wifi\wifi\services\wifi_standard\wifi_framework\wifi_manage\wifi_ap\ap_interface.cpp
ErrCode ApInterface::EnableHotspot()
{
    return m_ApService.EnableHotspot();
}

所在文件:foundation\communication\wifi\wifi\services\wifi_standard\wifi_framework\wifi_manage\wifi_ap\ap_service.cpp
ErrCode ApService::EnableHotspot()
{
    std::string moduleName = "ApService_" + std::to_string(m_id);
    m_apObserver = std::make_shared<WifiCountryCodeChangeObserver>(moduleName, m_ApStateMachine);
    WifiCountryCodeManager::GetInstance().RegisterWifiCountryCodeChangeListener(m_apObserver);
    m_ApStateMachine.OnApStateChange(ApState::AP_STATE_STARTING);
    m_ApStateMachine.RegisterEventHandler();
    apStartedState_.StartMonitor();
#ifdef SUPPORT_LOCAL_RANDOM_MAC
    apStartedState_.SetRandomMac();
#endif
    do {
        apStartedState_.SetCountry();
        apStartedState_.StartAp();
        WIFI_LOGI("StartAP is ok.");
        apStartedState_.SetConfig();
        m_ApStateMachine.SendMessage(static_cast<int>(ApStatemachineEvent::CMD_START_HOTSPOT));
        return ErrCode::WIFI_OPT_SUCCESS;
    } while (0);

    return WIFI_OPT_FAILED;
}

所在文件:foundation\communication\wifi\wifi\services\wifi_standard\wifi_framework\wifi_manage\wifi_ap\ap_started_state.cpp
bool ApStartedState::StartAp() const
{
    std::string ifaceName = WifiConfigCenter::GetInstance().GetApIfaceName();
    WifiApHalInterface::GetInstance().StartAp(m_id, ifaceName);
    return true;
}
所在文件:foundation\communication\wifi\wifi\services\wifi_standard\wifi_framework\wifi_manage\wifi_native\wifi_hal_interface\wifi_ap_hal_interface.cpp
WifiErrorNo WifiApHalInterface::StartAp(int id, const std::string &ifaceName)
{
    CHECK_NULL_AND_RETURN(mHdiWpaClient, WIFI_HAL_OPT_FAILED);
    WifiErrorNo ret = mHdiWpaClient->StartAp(id, ifaceName);
    return ret;
}

所在文件:foundation\communication\wifi\wifi\services\wifi_standard\wifi_framework\wifi_manage\wifi_native\client\hdi_client\wifi_hdi_wpa_client.cpp
WifiErrorNo WifiHdiWpaClient::StartAp(int id, const std::string &ifaceName)
{
    return HdiStartAp(ifaceName.c_str(), id);
}
所在文件:foundation\communication\wifi\wifi\services\wifi_standard\wifi_framework\wifi_manage\wifi_native\client\hdi_client\hdi_interface\wifi_hdi_wpa_ap_impl.c
WifiErrorNo HdiStartAp(const char *ifaceName, int id)
{
    LOGI("Ready to start hostpad: %{public}d, %{public}s", id, ifaceName);
    SetHdiApIfaceName(ifaceName);
    HdiApStart(id, ifaceName);
    RegisterApEventCallback();
    LOGI("HdiStartAp: success.");
    return WIFI_HAL_OPT_OK;
}

所在文件:foundation\communication\wifi\wifi\services\wifi_standard\wifi_framework\wifi_manage\wifi_native\client\hdi_client\hdi_interface\wifi_hdi_wpa_proxy.c
WifiErrorNo HdiApStart(int id, const char *ifaceName)
{
    LOGI("HdiApStart start...");
    g_id = id;
    WifiErrorNo result = WIFI_HAL_OPT_FAILED;
    do {
        result = CopyConfigFile(WIFI_DEFAULT_CFG);
        result = GetApInstance();
        result = StartApHdi(id, ifaceName);
        result = RegistHdfApDeathCallBack();
        g_apIsRunning = true;
        LOGI("HdiApStart start success");
    } while (0);

    return result;
}

所在文件:foundation\communication\wifi\wifi\services\wifi_standard\wifi_framework\wifi_manage\wifi_native\client\hdi_client\hdi_interface\wifi_hdi_wpa_proxy.c
static WifiErrorNo StartApHdi(int id, const char *ifaceName)
{
    int32_t ret = g_apObj->StartApWithCmd(g_apObj, ifaceName, id);
    return WIFI_HAL_OPT_OK;
}

所在文件:

foundation\communication\wifi\wifi\services\wifi_standard\wifi_framework\wifi_manage\wifi_native\client\hdi_client\hdi_interface\wifi_hdi_wpa_proxy.c
int32_t HostapdInterfaceStartApWithCmd(struct IHostapdInterface *self, const char *ifName, int id)
{
    HDF_LOGI("Enter hdi %{public}s", __func__);
    InitCfg(ifName);
    StartHostapd();
    StartHostapdHal(id);
    HDF_LOGI("%{public}s: hostapd start successfully", __func__);
    return HDF_SUCCESS;
}
所在文件:drivers\peripheral\wlan\hostapd\interfaces\hdi_service\service_common\hostapd_common_cmd.c
static int32_t StartHostapd(void)
{
    // #define WIFI_MULTI_CMD_MAX_LEN 1024
    // #define HOSTAPD_START_CMD "hostapd -g /data/service/el1/public/wifi/sockets/wpa/hostapd"
    char startCmd[WIFI_MULTI_CMD_MAX_LEN] = {0};
    memcpy_s(startCmd, WIFI_MULTI_CMD_MAX_LEN, HOSTAPD_START_CMD,
        strlen(HOSTAPD_START_CMD);
    HDF_LOGI("Cmd is %{public}s", startCmd);
    StartApMain(WPA_HOSTAPD_NAME, startCmd);
    return HDF_SUCCESS;
}

static int32_t StartApMain(const char *moduleName, const char *startCmd)
{
    int32_t ret;

    ret = pthread_create(&g_tid, NULL, ApThreadMain, (void *)startCmd);
    pthread_setname_np(g_tid, "ApMainThread");

    usleep(WPA_SLEEP_TIME);
    return HDF_SUCCESS;
}

static void *ApThreadMain(void *p)
{
    const char *startCmd;
    struct StApMainParam param = {0};
    char *tmpArgv[MAX_WPA_MAIN_ARGC_NUM] = {0};

    startCmd = (const char *)p;
    HDF_LOGI("%{public}s: startCmd: %{public}s", __func__, startCmd);
    SplitCmdString(startCmd, &param);
    for (int i = 0; i < param.argc; i++) {
        tmpArgv[i] = param.argv[i];
        HDF_LOGE("%{public}s: tmpArgv[%{public}d]: %{public}s", __func__, i, tmpArgv[i]);
    }
    int ret = ap_main(param.argc, tmpArgv);
    HDF_LOGI("%{public}s: run ap_main ret:%{public}d", __func__, ret);
    return NULL;
}

Logo

社区规范:仅讨论OpenHarmony相关问题。

更多推荐