196a7f077Sopenharmony_ci/*
296a7f077Sopenharmony_ci * Copyright (c) 2020 Huawei Device Co., Ltd.
396a7f077Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
496a7f077Sopenharmony_ci * you may not use this file except in compliance with the License.
596a7f077Sopenharmony_ci * You may obtain a copy of the License at
696a7f077Sopenharmony_ci *
796a7f077Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
896a7f077Sopenharmony_ci *
996a7f077Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1096a7f077Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1196a7f077Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1296a7f077Sopenharmony_ci * See the License for the specific language governing permissions and
1396a7f077Sopenharmony_ci * limitations under the License.
1496a7f077Sopenharmony_ci */
1596a7f077Sopenharmony_ci
1696a7f077Sopenharmony_ci#include <dlfcn.h>
1796a7f077Sopenharmony_ci#include <pthread.h>
1896a7f077Sopenharmony_ci#include <stdio.h>
1996a7f077Sopenharmony_ci#include <string.h>
2096a7f077Sopenharmony_ci
2196a7f077Sopenharmony_cipthread_t g_apThread;
2296a7f077Sopenharmony_ci
2396a7f077Sopenharmony_cichar* g_apArg[20] = {0};
2496a7f077Sopenharmony_ciint g_apArgc = 0;
2596a7f077Sopenharmony_ci
2696a7f077Sopenharmony_cistatic void* ThreadMain()
2796a7f077Sopenharmony_ci{
2896a7f077Sopenharmony_ci    printf("[HostapdSample]init hostapd.\n");
2996a7f077Sopenharmony_ci
3096a7f077Sopenharmony_ci    void *handleLibWpa = dlopen("/usr/lib/libwpa.so", RTLD_NOW | RTLD_LOCAL);
3196a7f077Sopenharmony_ci    if (handleLibWpa == NULL) {
3296a7f077Sopenharmony_ci        printf("[HostapdSample]dlopen libwpa failed.\n");
3396a7f077Sopenharmony_ci        return NULL;
3496a7f077Sopenharmony_ci    }
3596a7f077Sopenharmony_ci    int (*func)(int, char **) = NULL;
3696a7f077Sopenharmony_ci    func =  dlsym(handleLibWpa, "ap_main");
3796a7f077Sopenharmony_ci    if (func == NULL) {
3896a7f077Sopenharmony_ci        dlclose(handleLibWpa);
3996a7f077Sopenharmony_ci        printf("[HostapdSample]dlsym ap_main failed.\n");
4096a7f077Sopenharmony_ci        return NULL;
4196a7f077Sopenharmony_ci    }
4296a7f077Sopenharmony_ci    int ret = func(g_apArgc, g_apArg);
4396a7f077Sopenharmony_ci
4496a7f077Sopenharmony_ci    printf("[HostapdSample]run ap_main failed, ret:%d.\n", ret);
4596a7f077Sopenharmony_ci    for (int i = 0; i < g_apArgc; i++) {
4696a7f077Sopenharmony_ci        printf("[HostapdSample]arg %d:%s.\n", i, g_apArg[i]);
4796a7f077Sopenharmony_ci    }
4896a7f077Sopenharmony_ci
4996a7f077Sopenharmony_ci    if (dlclose(handleLibWpa) != 0) {
5096a7f077Sopenharmony_ci        printf("[HostapdSample]dlclose libwpa failed.\n");
5196a7f077Sopenharmony_ci        return NULL;
5296a7f077Sopenharmony_ci    }
5396a7f077Sopenharmony_ci    return NULL;
5496a7f077Sopenharmony_ci}
5596a7f077Sopenharmony_ci
5696a7f077Sopenharmony_ciint main(int argc, char *argv[])
5796a7f077Sopenharmony_ci{
5896a7f077Sopenharmony_ci    g_apArgc = argc;
5996a7f077Sopenharmony_ci    for (int i = 0; i < g_apArgc; i++) {
6096a7f077Sopenharmony_ci        g_apArg[i] = argv[i];
6196a7f077Sopenharmony_ci    }
6296a7f077Sopenharmony_ci
6396a7f077Sopenharmony_ci    int ret = pthread_create(&g_apThread, NULL, ThreadMain, NULL);
6496a7f077Sopenharmony_ci    if (ret != 0) {
6596a7f077Sopenharmony_ci        printf("[HostapdSample]create thread failed error:%s.\n", strerror(ret));
6696a7f077Sopenharmony_ci        return 1;
6796a7f077Sopenharmony_ci    }
6896a7f077Sopenharmony_ci    pthread_join(g_apThread, NULL);
6996a7f077Sopenharmony_ci    return 0;
7096a7f077Sopenharmony_ci}
71