developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 1 | /* Copyright (C) 2021-2022 Mediatek Inc. */ |
| 2 | |
| 3 | #include <signal.h> |
| 4 | #include <sys/select.h> |
| 5 | #include <sys/wait.h> |
| 6 | #include "atenl.h" |
| 7 | |
| 8 | static const char *progname; |
| 9 | bool atenl_enable; |
| 10 | |
| 11 | void sig_handler(int signum) |
| 12 | { |
| 13 | atenl_enable = false; |
| 14 | } |
| 15 | |
| 16 | void atenl_init_signals() |
| 17 | { |
| 18 | if (signal(SIGINT, sig_handler) == SIG_ERR) |
| 19 | goto out; |
| 20 | if (signal(SIGTERM, sig_handler) == SIG_ERR) |
| 21 | goto out; |
| 22 | if (signal(SIGABRT, sig_handler) == SIG_ERR) |
| 23 | goto out; |
| 24 | if (signal(SIGUSR1, sig_handler) == SIG_ERR) |
| 25 | goto out; |
| 26 | if (signal(SIGUSR2, sig_handler) == SIG_ERR) |
| 27 | goto out; |
| 28 | |
| 29 | return; |
| 30 | out: |
| 31 | perror("signal"); |
| 32 | } |
| 33 | |
| 34 | static int phy_lookup_idx(const char *name) |
| 35 | { |
| 36 | char buf[128]; |
| 37 | FILE *f; |
| 38 | size_t len; |
| 39 | int ret; |
| 40 | |
| 41 | ret = snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name); |
| 42 | if (snprintf_error(sizeof(buf), ret)) |
| 43 | return -1; |
| 44 | |
| 45 | f = fopen(buf, "r"); |
| 46 | if (!f) |
| 47 | return -1; |
| 48 | |
| 49 | len = fread(buf, 1, sizeof(buf) - 1, f); |
| 50 | fclose(f); |
| 51 | |
| 52 | if (!len) |
| 53 | return -1; |
| 54 | |
| 55 | buf[len] = 0; |
| 56 | return atoi(buf); |
| 57 | } |
| 58 | |
developer | 22227f4 | 2022-10-07 15:54:07 +0800 | [diff] [blame] | 59 | static int get_default_bridge_name(struct atenl *an) |
| 60 | { |
| 61 | char buf[128]; |
| 62 | FILE *f; |
| 63 | size_t len; |
| 64 | int ret; |
| 65 | |
| 66 | ret = snprintf(buf, sizeof(buf), "/sbin/procd"); |
| 67 | if (snprintf_error(sizeof(buf), ret)) |
| 68 | return -1; |
| 69 | |
| 70 | f = fopen(buf, "r"); |
| 71 | |
| 72 | /* This procd is openwrt only */ |
| 73 | if (f) { |
| 74 | an->bridge_name = BRIDGE_NAME_OPENWRT; |
| 75 | fclose(f); |
| 76 | } else { |
| 77 | an->bridge_name = BRIDGE_NAME_RDKB; |
| 78 | } |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 83 | static void usage(void) |
| 84 | { |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 85 | printf("Usage:\n"); |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 86 | printf(" %s [-u] [-i phyX]\n", progname); |
| 87 | printf("options:\n" |
| 88 | " -h = show help text\n" |
| 89 | " -i = phy name of driver interface, please use first phy for dbdc\n" |
developer | 22227f4 | 2022-10-07 15:54:07 +0800 | [diff] [blame] | 90 | " -u = use unicast to respond to HQADLL\n" |
| 91 | " -b = specify your bridge name\n"); |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 92 | printf("examples:\n" |
developer | 22227f4 | 2022-10-07 15:54:07 +0800 | [diff] [blame] | 93 | " %s -u -i phy0 -b br-lan\n", progname); |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 94 | |
| 95 | exit(EXIT_FAILURE); |
| 96 | } |
| 97 | |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 98 | static void atenl_handler_run(struct atenl *an) |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 99 | { |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 100 | int count, sock_eth = an->sock_eth; |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 101 | fd_set readfds; |
| 102 | |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 103 | atenl_info("Start atenl HQA command handler\n"); |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 104 | |
| 105 | while (atenl_enable) { |
| 106 | FD_ZERO(&readfds); |
| 107 | FD_SET(sock_eth, &readfds); |
| 108 | count = select(sock_eth + 1, &readfds, NULL, NULL, NULL); |
| 109 | |
| 110 | if (count < 0) { |
| 111 | atenl_err("%s: select failed, %s\n", __func__, strerror(errno)); |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 112 | } else if (count == 0) { |
| 113 | usleep(1000); |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 114 | } else { |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 115 | if (!FD_ISSET(sock_eth, &readfds)) |
| 116 | continue; |
| 117 | atenl_hqa_proc_cmd(an); |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 121 | atenl_dbg("HQA command handler end\n"); |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 122 | } |
| 123 | |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 124 | int main(int argc, char **argv) |
| 125 | { |
| 126 | int opt, phy_idx, ret = 0; |
| 127 | char *phy = "phy0", *cmd = NULL; |
| 128 | struct atenl *an; |
| 129 | |
| 130 | progname = argv[0]; |
| 131 | |
| 132 | an = calloc(1, sizeof(struct atenl)); |
| 133 | if (!an) |
| 134 | return -ENOMEM; |
| 135 | |
| 136 | while(1) { |
developer | 22227f4 | 2022-10-07 15:54:07 +0800 | [diff] [blame] | 137 | opt = getopt(argc, argv, "hi:uc:b:"); |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 138 | if (opt == -1) |
| 139 | break; |
| 140 | |
| 141 | switch (opt) { |
| 142 | case 'h': |
| 143 | usage(); |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 144 | goto out; |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 145 | case 'i': |
| 146 | phy = optarg; |
| 147 | break; |
developer | 22227f4 | 2022-10-07 15:54:07 +0800 | [diff] [blame] | 148 | case 'b': |
| 149 | an->bridge_name = optarg; |
| 150 | break; |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 151 | case 'u': |
| 152 | an->unicast = true; |
| 153 | printf("Opt: use unicast to send response\n"); |
| 154 | break; |
| 155 | case 'c': |
| 156 | cmd = optarg; |
| 157 | break; |
| 158 | default: |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 159 | atenl_err("Not supported option: %c\n", opt); |
| 160 | goto out; |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | phy_idx = phy_lookup_idx(phy); |
| 165 | if (phy_idx < 0 || phy_idx > UCHAR_MAX) { |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 166 | atenl_err("Could not find phy '%s'\n", phy); |
| 167 | goto out; |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | if (cmd) { |
| 171 | atenl_eeprom_cmd_handler(an, phy_idx, cmd); |
| 172 | goto out; |
| 173 | } |
| 174 | |
| 175 | atenl_enable = true; |
| 176 | atenl_init_signals(); |
| 177 | |
developer | 22227f4 | 2022-10-07 15:54:07 +0800 | [diff] [blame] | 178 | if (!an->bridge_name) { |
| 179 | ret = get_default_bridge_name(an); |
| 180 | if (ret) { |
| 181 | atenl_err("Get default bridge name failed\n"); |
| 182 | goto out; |
| 183 | } |
| 184 | |
| 185 | atenl_info("Bridge name is not specified, use default bridge name: %s\n", an->bridge_name); |
| 186 | } else { |
| 187 | atenl_info("Currently using bridge name: %s\n", an->bridge_name); |
| 188 | } |
| 189 | |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 190 | /* background ourself */ |
| 191 | if (!fork()) { |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 192 | ret = atenl_eeprom_init(an, phy_idx); |
| 193 | if (ret) |
| 194 | goto out; |
| 195 | |
| 196 | ret = atenl_eth_init(an); |
| 197 | if (ret) |
| 198 | goto out; |
| 199 | |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 200 | atenl_handler_run(an); |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 201 | } else { |
| 202 | usleep(800000); |
| 203 | } |
| 204 | |
| 205 | ret = 0; |
| 206 | out: |
| 207 | if (an->sock_eth) |
| 208 | close(an->sock_eth); |
| 209 | if (an->eeprom_fd || an->eeprom_data) |
| 210 | atenl_eeprom_close(an); |
| 211 | free(an); |
| 212 | |
| 213 | return ret; |
| 214 | } |