blob: 20c9633be2334424269023f8f044b8e37df025e3 [file] [log] [blame]
Bing Jianga88a0722022-02-10 14:50:54 +08001#include <dirent.h>
Jun Zhanga3357c32022-04-24 11:48:52 +08002#include <fcntl.h>
3#include <stdbool.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <sys/ioctl.h>
7#include <unistd.h>
Matthew.Shyu2ab6c1d2014-01-16 18:22:27 +08008
Jun Zhanga3357c32022-04-24 11:48:52 +08009#define USB_POWER_UP _IO('m', 1)
10#define USB_POWER_DOWN _IO('m', 2)
11#define SDIO_POWER_UP _IO('m', 3)
12#define SDIO_POWER_DOWN _IO('m', 4)
13#define SDIO_POWER_DOWN _IO('m', 4)
14#define SDIO_GET_DEV_TYPE _IO('m', 5)
Weiguang Ruan41123722019-06-27 21:10:58 +080015
16char dev_type[128] = {0};
Matthew.Shyu2ab6c1d2014-01-16 18:22:27 +080017
Jun Zhanga3357c32022-04-24 11:48:52 +080018bool set_wifi_power(int on) {
19 int fd;
20 bool ret = true;
Matthew.Shyu2ab6c1d2014-01-16 18:22:27 +080021
Jun Zhanga3357c32022-04-24 11:48:52 +080022 fd = open("/dev/wifi_power", O_RDWR);
23 if (fd < 0) {
24 fprintf(stderr, "Device open failed !!!\n");
25 return false;
26 }
27
28 switch (on) {
29 case 0: {
30 if (ioctl(fd, SDIO_POWER_DOWN) < 0) {
31 if (ioctl(fd, USB_POWER_DOWN) < 0) {
32 fprintf(stderr, "Set Wi-Fi power down error!!!\n");
Jun Zhanga3357c32022-04-24 11:48:52 +080033 ret = false;
34 }
Matthew.Shyu2ab6c1d2014-01-16 18:22:27 +080035 }
Jun Zhanga3357c32022-04-24 11:48:52 +080036 } break;
37 case 1: {
38 if (ioctl(fd, SDIO_POWER_UP) < 0) {
39 if (ioctl(fd, USB_POWER_UP) < 0) {
40 fprintf(stderr, "Set Wi-Fi power up error!!!\n");
Jun Zhanga3357c32022-04-24 11:48:52 +080041 ret = false;
42 }
43 }
44 } break;
45 case 2: {
46 if (ioctl(fd, SDIO_GET_DEV_TYPE, dev_type) < 0) {
47 fprintf(stderr, "Failed to get dev type!!!\n");
Jun Zhanga3357c32022-04-24 11:48:52 +080048 ret = false;
49 } else
50 fprintf(stdout, "inf=%s\n", dev_type);
51 } break;
52 default:
53 fprintf(stderr, "Invalid command %d\n", on);
54 ret = false;
55 }
56 close(fd);
Matthew.Shyu2ab6c1d2014-01-16 18:22:27 +080057
Jun Zhanga3357c32022-04-24 11:48:52 +080058 return ret;
Matthew.Shyu2ab6c1d2014-01-16 18:22:27 +080059}
60
Jun Zhanga3357c32022-04-24 11:48:52 +080061int main(int argc, char *argv[]) {
62 long value = 0;
63 if (argc < 2) {
64 fprintf(stderr, "wrong number of arguments\n");
65 return -1;
66 }
67 value = strtol(argv[1], NULL, 10);
68 if (!set_wifi_power(value)) {
69 return -1;
70 }
Bing Jianga88a0722022-02-10 14:50:54 +080071
Jun Zhanga3357c32022-04-24 11:48:52 +080072 if (value == 1 && argc > 2) {
73 const char *interface_name = argv[2];
Bing Jianga88a0722022-02-10 14:50:54 +080074
Jun Zhanga3357c32022-04-24 11:48:52 +080075 int wait_time = 100;
76 char fpath[64];
77 snprintf(fpath, sizeof(fpath), "/sys/class/net/%s", interface_name);
78 do {
79 if (access(fpath, 0) == 0)
80 break;
81 usleep(50000);
82 } while (wait_time--);
83 if (wait_time < 0) {
84 fprintf(stderr, "wait for interface [%s] timeout\n", interface_name);
85 return -1;
Matthew.Shyu2ab6c1d2014-01-16 18:22:27 +080086 }
Jun Zhanga3357c32022-04-24 11:48:52 +080087 }
Bing Jianga88a0722022-02-10 14:50:54 +080088
Jun Zhanga3357c32022-04-24 11:48:52 +080089 return 0;
Matthew.Shyu2ab6c1d2014-01-16 18:22:27 +080090}
91