• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <dlfcn.h>
17 #include <pthread.h>
18 #include <stdio.h>
19 #include <string.h>
20 
21 pthread_t g_wpaThread;
22 
23 char* g_wpaArg[20] = {0};
24 int g_wpaArgc = 0;
25 
ThreadMain()26 static void* ThreadMain()
27 {
28     printf("[WpaSample]init wpa_supplicant.\n");
29 
30     void *handleLibWpa = dlopen("/usr/lib/libwpa.so", RTLD_NOW | RTLD_LOCAL);
31     if (handleLibWpa == NULL) {
32         printf("[WpaSample]dlopen libwpa failed.\n");
33         return NULL;
34     }
35     int (*func)(int, char **) = NULL;
36     func =  dlsym(handleLibWpa, "wpa_main");
37     if (func == NULL) {
38         dlclose(handleLibWpa);
39         printf("[WpaSample]dlsym wpa_main failed.\n");
40         return NULL;
41     }
42     int ret = func(g_wpaArgc, g_wpaArg);
43 
44     printf("[WpaSample]run wpa_main failed, ret:%d.\n", ret);
45     for (int i = 0; i < g_wpaArgc; i++) {
46         printf("[WpaSample]arg %d:%s.\n", i, g_wpaArg[i]);
47     }
48 
49     if (dlclose(handleLibWpa) != 0) {
50         printf("[WpaSample]dlclose libwpa failed.\n");
51         return NULL;
52     }
53     return NULL;
54 }
55 
main(int argc,char * argv[])56 int main(int argc, char *argv[])
57 {
58     g_wpaArgc = argc;
59     for (int i = 0; i < g_wpaArgc; i++) {
60         g_wpaArg[i] = argv[i];
61     }
62 
63     int ret = pthread_create(&g_wpaThread, NULL, ThreadMain, NULL);
64     if (ret != 0) {
65         printf("[WpaSample]create thread failed error:%s.\n", strerror(ret));
66         return 1;
67     }
68     pthread_join(g_wpaThread, NULL);
69     return 0;
70 }
71