高通WIFI GPS 测试demo

来源:互联网 发布:zero shot tensorflow 编辑:程序博客网 时间:2024/05/21 10:40

Android.mk

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_SRC_FILES := \  wifi.cLOCAL_CFLAGS += -DCONFIG_LIBNL20# Silence some warnings for now. Needs to be fixed upstream. b/26105799LOCAL_CFLAGS += -Wno-unused-parameter \                -Wno-sign-compare \                -Wno-formatLOCAL_CLANG_CFLAGS += -Wno-enum-conversionLOCAL_LDFLAGS := -Wl,--no-gc-sectionsLOCAL_MODULE_TAGS := debugLOCAL_MODULE := wifiLOCAL_MODULE_CLASS := EXECUTABLESinclude $(BUILD_EXECUTABLE)LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_SRC_FILES := \  gps.cLOCAL_CFLAGS += -DCONFIG_LIBNL20# Silence some warnings for now. Needs to be fixed upstream. b/26105799LOCAL_CFLAGS += -Wno-unused-parameter \                -Wno-sign-compare \                -Wno-formatLOCAL_CLANG_CFLAGS += -Wno-enum-conversionLOCAL_LDFLAGS := -Wl,--no-gc-sectionsLOCAL_MODULE_TAGS := debugLOCAL_MODULE := gpsLOCAL_MODULE_CLASS := EXECUTABLESinclude $(BUILD_EXECUTABLE)

wifi.c

#include <cutils/log.h>#include <cutils/properties.h>#include <ctype.h>#include <dlfcn.h>#include <dirent.h>#include <errno.h>#include <fcntl.h>#include <limits.h>#include <inttypes.h>#include <pthread.h>#include <semaphore.h>#include <stdio.h>#include <stdint.h>#include <sys/prctl.h>#include <sys/epoll.h>#include <sys/socket.h>#include <sys/select.h>#include <sys/types.h>#include <sys/un.h>#include <sys/wait.h>#include <sys/mman.h>#include <sys/stat.h>#include <linux/input.h>#include <unistd.h>#include <stdbool.h>#include <stdlib.h>#include <string.h>#define READ_BUF_SIZE 1024#define SIZE_1K 1024static int finish_wifi_test = 0;static int wifi_test_result = 0;static int wifi_test_error = 0;static pthread_t processThreadPid;#define   FAILED  -1#define   SUCCESS  0#define KEY_WORD_CHANNEL "Channel:"#define KEY_WORD_ESSID "SSID:"#define KEY_WORD_SIGNAL_LEVEL "Signal level"static const char* tag = LOG_TAG;static pthread_mutex_t test_mutex;     /* Only one request at a time */static pthread_cond_t test_cond;       /* Waiting for RESP message */bool check_file_exist(const char *path) {    struct stat buffer;    int err = stat(path, &buffer);    if(err == 0)        return true;    if(errno == ENOENT) {        printf("file(%s) do not exist\n", path);        return false;    }    return false;}void local_exe_cmd(const char *cmd, char *result, int size){    char line[1024];    char ps[1024] = {0};    FILE *pp;    printf("local_exe_cmd cmd = %s\n",cmd);    strlcpy(ps, cmd, sizeof(ps));    if((pp = popen(ps, "r")) != NULL)    {        printf("begain gets");        while(fgets(line, sizeof(line), pp) != NULL)        {            if(strlen(line) > 1)                strlcat(result, line, size);            printf("%s",line);        }        printf("close the pipe \n");        pclose(pp);        pp = NULL;    } else    {            printf( "popen %s error\n", ps);    }}void signalHandler(int signal){    printf("pthread_exit");    pthread_exit(NULL);}void kill_pid_by_name( const char* pidName){    DIR *dir;    struct dirent *next;    long pidList=0;    char cmd[20];    dir = opendir("/proc");    if (!dir)    {        //    perror_msg_and_die("Cannot open /proc");        printf("cannot open /proc");    }    while ((next = readdir(dir)) != NULL)    {        FILE *status;        char filename[READ_BUF_SIZE];        char buffer[READ_BUF_SIZE];        char name[READ_BUF_SIZE];        memset(buffer, 0, sizeof(buffer));        /* Must skip ".." since that is outside /proc */        if (strcmp(next->d_name, "..") == 0)            continue;        /* If it isn't a number, we don't want it */        if (!isdigit(*next->d_name))            continue;        sprintf(filename, "/proc/%s/status", next->d_name);        if (! (status = fopen(filename, "r")) )        {            continue;        }        if (fgets(buffer, READ_BUF_SIZE-1, status) == NULL)        {            fclose(status);            continue;        }        fclose(status);        /* Buffer should contain a string like "Name:   binary_name" */        if (buffer[0] != '\0')            sscanf(buffer, "%*s %s", name);        if (strcmp(name, pidName) == 0)        {            pidList = strtol(next->d_name, NULL, 0);            sprintf(cmd, "kill %ld", pidList);            system(cmd);        }    }}void stop_wifi(){    kill_pid_by_name("iw");}static void load_driver() {    char result[1024 * 10] = {0};    local_exe_cmd("insmod /system/lib/modules/wlan.ko", result, sizeof(result));}static void unload_driver() {    char result[1024 * 10] = {0};    local_exe_cmd("rmmod wlan", result, sizeof(result));}static void config_wlan() {    char result[1024 * 10] = {0};    local_exe_cmd("ifconfig wlan0 up", result, sizeof(result));}void test_wifi_func(){    struct timeval before;    struct timeval after;    char result[1024 * 10] = {0};    char cmdstr[256] = {0};    int i = 0;    while (i < 2)    {        printf("begin %d times searching...\n", i+1);        local_exe_cmd("iw dev wlan0 scan", result, sizeof(result));        char* p = result;        if (*p != '\0')        {                if(strstr(p,KEY_WORD_ESSID) != NULL)                {                printf("find wifi ssid\n");                wifi_test_result = 1;                pthread_cond_signal( &test_cond );                break;                }                else                {                        printf("%s",p);                }        }        i++;    }    if (wifi_test_result == 0)    {        printf("can't find wifi \n");    }}static int start_test() {    if(!check_file_exist("/system/lib/modules/wlan.ko") || !check_file_exist("/system/bin/iw"))        return FAILED;    load_driver();    config_wlan();    test_wifi_func();    unload_driver();    if(wifi_test_result) {        printf("wifi test pass\n");    } else {        printf("wifi test fail\n");    }    return wifi_test_result ;}void *processThread_bt(void* parm){    signal(SIGUSR1, signalHandler);    start_test();    return NULL;}int get_wifi_result(){    printf("get wifi test result = %d\n", wifi_test_result);    return wifi_test_result;}int get_wifi_status(){    printf("get wifi test status = %d\n", finish_wifi_test);    return finish_wifi_test;}void end_test_wifi(){    printf("end test wifi\n");    stop_wifi();    pthread_kill(processThreadPid, 0);    wifi_test_result = 0;    finish_wifi_test = 0;}int main(int argc, char **argv){        start_test();}

gps.c

#include <cutils/log.h>#include <cutils/properties.h>#include <ctype.h>#include <dlfcn.h>#include <dirent.h>#include <errno.h>#include <fcntl.h>#include <limits.h>#include <inttypes.h>#include <pthread.h>#include <semaphore.h>#include <stdio.h>#include <stdint.h>#include <sys/prctl.h>#include <sys/epoll.h>#include <sys/socket.h>#include <sys/select.h>#include <sys/types.h>#include <sys/un.h>#include <sys/wait.h>#include <sys/mman.h>#include <sys/stat.h>#include <linux/input.h>#include <unistd.h>#include <stdbool.h>#include <stdlib.h>#include <string.h>#define READ_BUF_SIZE 1024#define SIZE_1K 1024#define stderr_file    "/data/stderr_file"static int finish_gps_test = 0;static int gps_test_result = 0;static int gps_test_error = 0;static pthread_t processThreadPid;#define   FAILED  -1#define   SUCCESS  0#define KEY_WORD_CHANNEL "Channel:"#define KEY_WORD_ESSID "SSID:"#define KEY_WORD_SIGNAL_LEVEL "Signal level"#define JUSTIFY_KEY "Stop test"static const char* tag = LOG_TAG;static pthread_mutex_t test_mutex;     /* Only one request at a time */static pthread_cond_t test_cond;       /* Waiting for RESP message */bool check_file_exist(const char *path) {    struct stat buffer;    int err = stat(path, &buffer);        if(err == 0)        return true;    if(errno == ENOENT) {        printf("file(%s) do not exist\n", path);        return false;    }    return false;}void local_exe_cmd(const char *cmd, char *result, int size){    char line[1024];    char ps[1024] = {0};    int save_fd ;    int fd;    FILE *pp;    //save_fd = dup(STDOUT_FILENO);    save_fd = dup(STDERR_FILENO);    fd = open(stderr_file,(O_RDWR | O_CREAT), 0644);    //fflush(stdout);    fflush(stderr);    //setvbuf(stdout,NULL,_IONBF,0);      setvbuf(stderr,NULL,_IONBF,0);    printf("test stdout\n");   // dup2(fd,STDOUT_FILENO); // 用我们新打开的文件描述符替换掉 标准输出     dup2(fd,STDERR_FILENO); // 用我们新打开的文件描述符替换掉 标准输出     printf("test file\n");    printf("local_exe_cmd cmd = %s\n",cmd);    strlcpy(ps, cmd, sizeof(ps));    if((pp = popen(ps, "r")) != NULL)    {        printf("begain gets\n");        while(fgets(line, sizeof(line), pp) != NULL)        {            if(strlen(line) > 1)                strlcat(result, line, size);            printf("l:%s",line);        }        printf("close the pipe \n");        pclose(pp);        close(fd);        pp = NULL;    } else    {        printf( "popen %s error\n", ps);    }}void signalHandler(int signal){    printf("pthread_exit");    pthread_exit(NULL);}void kill_pid_by_name( const char* pidName){    DIR *dir;    struct dirent *next;    long pidList=0;    char cmd[20];    dir = opendir("/proc");    if (!dir)    {        //    perror_msg_and_die("Cannot open /proc");        printf("cannot open /proc");    }    while ((next = readdir(dir)) != NULL)    {        FILE *status;        char filename[READ_BUF_SIZE];        char buffer[READ_BUF_SIZE];        char name[READ_BUF_SIZE];        memset(buffer, 0, sizeof(buffer));        /* Must skip ".." since that is outside /proc */        if (strcmp(next->d_name, "..") == 0)            continue;        /* If it isn't a number, we don't want it */        if (!isdigit(*next->d_name))            continue;        sprintf(filename, "/proc/%s/status", next->d_name);        if (! (status = fopen(filename, "r")) )        {            continue;        }        if (fgets(buffer, READ_BUF_SIZE-1, status) == NULL)        {            fclose(status);            continue;        }        fclose(status);        /* Buffer should contain a string like "Name:   binary_name" */        if (buffer[0] != '\0')            sscanf(buffer, "%*s %s", name);        if (strcmp(name, pidName) == 0)        {            pidList = strtol(next->d_name, NULL, 0);            sprintf(cmd, "kill %ld", pidList);            system(cmd);        }    }}void stop_gps(){    kill_pid_by_name("garden_app");}void test_gps_func(){    struct timeval before;    struct timeval after;    char result[1024 * 10] = {0};    char cmdstr[256] = {0};    int i = 0;    char buf[1024] = {0};    int fd;    FILE *f1;    while (i < 1)    {        printf("begin %d times searching...\n", i+1);        local_exe_cmd("garden_app -z 0 -u 0 -q 0 -A 1 -B 28", result, sizeof(result));        f1 = fopen(stderr_file, "r");        if (f1 == NULL)        {                break;        }        while (!feof(f1))        {                memset(buf, 0, sizeof(buf));                fgets(buf, sizeof(buf), f1);                printf("LSN :%s",buf);                if(strstr(buf,JUSTIFY_KEY) != NULL)                {                        printf("find gps \n");                        gps_test_result = 1;                        pthread_cond_signal( &test_cond );                        break;                }        }        i++;    }    if (gps_test_result == 0)    {        printf("can't find gps \n");    }}static int start_test() {    if(!check_file_exist("/system/bin/garden_app"))        return FAILED;    test_gps_func();    if(gps_test_result) {        printf("gps test pass\n");    } else {        printf("gps test fail\n");    }    return gps_test_result ;}void *processThread_bt(void* parm){    signal(SIGUSR1, signalHandler);    start_test();    return NULL;}int get_gps_result(){    printf("get gps test result = %d\n", gps_test_result);    return gps_test_result;}int get_gps_status(){    printf("get gps test status = %d\n", finish_gps_test);    return finish_gps_test;}void end_test_gps(){    printf("end test gps\n");    stop_gps();    pthread_kill(processThreadPid, 0);    gps_test_result = 0;    finish_gps_test = 0;}int main(int argc, char **argv){        start_test();}
原创粉丝点击