/* Function called for 'local' interactive session and for 'remote' interactive session */
static int
do_interactive(struct ifreq *ifr)
{
    int err = 0;

#ifdef RWL_WIFI
    int retry;
#endif

    while (1) {
        char *fgsret;
        char line[INTERACTIVE_MAX_INPUT_LENGTH];
        fprintf(stdout, "> ");
        fgsret = fgets(line, sizeof(line), stdin);

        /* end of file */
        if (fgsret == NULL)
            break;
        if (line[0] == '\n')
            continue;

        if (strlen (line) > 0) {
            /* skip past first arg if it's "wl" and parse up arguments */
            char *argv[INTERACTIVE_NUM_ARGS];
            int argc;
            char *token;
            argc = 0;

            while (argc < (INTERACTIVE_NUM_ARGS - 1) &&
                    (token = strtok(argc ? NULL : line, " \t\n")) != NULL) {

                /* Specifically make sure empty arguments (like SSID) are empty */
                if (token[0] == '"' && token[1] == '"') {
                    token[0] = '\0';
                }

                argv[argc++] = token;
            }
            argv[argc] = NULL;
            if (argc == (INTERACTIVE_NUM_ARGS - 1) &&
                    (token = strtok(NULL, " \t")) != NULL) {
                printf("wl:error: too many args; argc must be < %d\n",
                       (INTERACTIVE_NUM_ARGS - 1));
                continue;
            }
#ifdef RWL_WIFI
            if (!strcmp(argv[0], "findserver")) {
                remote_wifi_ser_init_cmds((void *) ifr);
            }
#endif /* RWL_WIFI */

            if (strcmp(argv[0], "q") == 0 || strcmp(argv[0], "exit") == 0) {
                break;
            }

            if ((strcmp(argv[0], "sh") == 0) && (remote_type != NO_REMOTE))  {
                if (argv[1]) {
                    process_args(ifr, argv);
                } else {
                    DPRINT_ERR(ERR, "Give shell command");
                    continue;
                }
            } else { /* end shell */
                /* check for lchannel support,applicable only for wifi transport.
                * when lchannel is called remote type is swapped by calling swap_
                * remote_type.This is done to change, the remote type to local,
                * so that local machine's channel can be executed and
                * returned to the user.
                * To get back the original remote type, swap is recalled.
                */
                if (remote_type == REMOTE_WIFI) {
#ifdef RWL_WIFI
                    if (!strcmp(argv[0], "lchannel")) {
                        strcpy(argv[0], "channel");
                        rwl_wifi_swap_remote_type(remote_type);
                        err = wl_do_cmd(ifr, argv);
                        rwl_wifi_swap_remote_type(remote_type);
                    } else {
                        err = wl_do_cmd(ifr, argv);
                    }
                    /* After join cmd's gets exeuted on the server side, client
                     * needs to know the channel on which the server is associated
                     * with AP , after delay of few seconds client will intiate the
                     * scan on diffrent channels by calling
                     * rwl_find_remote_wifi_server function
                     */
                    if ((!strcmp(argv[0], "join")) ||
                            (!strcmp(argv[0], "ssid"))) {
                        DPRINT_INFO(OUTPUT, "\n Findserver is called"
                                    "after the join issued to remote \n \n");
                        sleep(RWL_WIFI_JOIN_DELAY);
                        for (retry = 0; retry < RWL_WIFI_RETRY; retry++) {
                            if ((rwl_find_remote_wifi_server(ifr,
                                                             &g_rwl_buf_mac[0]) == 0)) {
                                break;
                            }
                        }
                    }
#endif /* RWL_WIFI */
                } else {
                    err = wl_do_cmd(ifr, argv);
                }
            } /* end of wl */
        } /* end of strlen (line) > 0 */
    } /* while (1) */

    return err;
}
Esempio n. 2
0
/*
 * Send the valid action frame (CDC+DATA) through the REF driver interface.
 * if the CMD is "findserver" then "findmypeer" frames are sent on the diffrent
 * channels to reconnect
 * to with server. Other wl cmd takes the normal path.
 * parameter 3 , i.e. buf contains the cmd line arguments  and buf_len is the actual
 * length of the buf. data_len is the length of the actual data to be sent to remote server.
 */
int
remote_CDC_wifi_tx(void *wl, uint cmd, uchar *buf, uint buf_len, uint data_len, uint flags)
{
	rem_ioctl_t *rem_ptr = &rem_cdc;
	int error, read_try;
	dot11_action_wifi_vendor_specific_t *rem_wifi_send;

	/* prepare CDC header */
	rem_ptr->msg.cmd = cmd;
	rem_ptr->msg.len = buf_len;
	rem_ptr->msg.flags = flags;
	rem_ptr->data_len = data_len;

	if ((data_len > buf_len)) {
		DPRINT_ERR(ERR, "remote_CDC_wifi_tx: data_len (%d) > buf_len (%d)\n",
		data_len, buf_len);
		return (FAIL);
	}
	/* client will not send data greater than RWL_WIFI_FRAG_DATA_SIZE to server,
	 * this condition should not be hit on client side, when sending the cmd
	 * to remote server
	 */
	if (rem_ptr->data_len > RWL_WIFI_FRAG_DATA_SIZE)
		DPRINT_DBG(OUTPUT, "data size exceeds data_len %d\n", rem_ptr->msg.len);

	if ((buf != NULL) && (strlen((char*)buf) >= (sizeof(RWL_WIFI_FIND_SER_CMD)-1)) &&
		(!strcmp((char*)buf, RWL_WIFI_FIND_SER_CMD))) {
		/* This is special case for wifi, when user wants to findserver,
		* client has to execute it locally.Find the channel on the on
		* which DUT is operating and sync up with specified MAC address,
		* retry if fails to find the server
		*/
		for (read_try = 0; read_try < RWL_WIFI_RETRY; read_try++) {
			if (((error = rwl_find_remote_wifi_server(wl,
			&g_rwl_buf_mac[0])) == 0)) {
				break;
			}
		}
		return error;
	}

	if ((rem_wifi_send = rwl_wifi_allocate_actionframe()) == NULL) {
		DPRINT_ERR(ERR, "remote_CDC_wifi_tx: Failed to allocate memory\n");
		return (FAIL);
	}
	/* only data length needs to be sent to remote server using this function 
	* This function is only meant for client to send data to server
	* Copy the CDC header and data to action frame data feild
	* Now we have encapsulated the CDC header and data completely to in the
	* action frame.
	*/
	memcpy(&rem_wifi_send->data[RWL_WIFI_CDC_HEADER_OFFSET], (char*)rem_ptr, REMOTE_SIZE);
	if (buf != NULL) {
		memcpy(&rem_wifi_send->data[REMOTE_SIZE], buf, data_len);
	}

	/* Send the action frame to remote server using the  rwl_var_setbuf fucntion,
	 * which will use the local driver interface to send this frame on the air
	 */
	if ((error = rwl_var_setbuf(wl, RWL_WIFI_ACTION_CMD, rem_wifi_send,
	RWL_WIFI_ACTION_FRAME_SIZE)) < 0) {
		DPRINT_ERR(ERR, "Unable to read the action frame %d error\n", error);
	}
	free(rem_wifi_send);
	return error;
}
int
wl_lib(char *input_str)
{
    struct ifreq ifr;
    char *ifname = NULL;
    int err = 0;
    int help = 0;
    int status = CMD_WL;
    void* serialHandle = NULL;
    char *tmp_argv[LINUX_NUM_ARGS];
    char **argv = tmp_argv;
    int argc;

    /* buf_to_args return 0 if no args or string too long
     * or return NDIS_NUM_ARGS if too many args
     */
    if (((argc = buf_to_args(input_str, argv)) == 0) || (argc == LINUX_NUM_ARGS)) {
        printf("wl:error: can't convert input string\n");
        return (-1);
    }
#else
/* Main client function */
int
main(int argc, char **argv)
{
    struct ifreq ifr;
    char *ifname = NULL;
    int err = 0;
    int help = 0;
    int status = CMD_WL;
#if defined(RWL_DONGLE) || RWL_SERIAL
    void* serialHandle = NULL;
#endif

#endif /* WLMSO */
    wlu_av0 = argv[0];

    wlu_init();
    memset(&ifr, 0, sizeof(ifr));
    (void)*argv++;

    if ((status = wl_option(&argv, &ifname, &help)) == CMD_OPT) {
        if (ifname)
        {
            if (strcmp(ifname, "eth1") == 0)
            {
                strncpy(ifr.ifr_name, "wl0", IFNAMSIZ);
            }
            else if (strcmp(ifname, "eth2") == 0)
            {
                strncpy(ifr.ifr_name, "wl1", IFNAMSIZ);
            }
            else
            {
                strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
            }
        }
        /* Bug fix: If -h is used as an option, the above function call
         * will notice it and raise the flag but it won't be processed
         * in this function so we undo the argv increment so that the -h
         * can be spotted by the next call of wl_option. This will ensure
         * that wl -h [cmd] will function as desired.
         */
        else if (help)
            (void)*argv--;
    }

    /* Linux client looking for a indongle reflector */
    if (*argv && strncmp (*argv, "--indongle", strlen(*argv)) == 0) {
        rwl_dut_autodetect = FALSE;
        (void)*argv++;
    }
    /* Linux client looking for a WinVista server */
    if (*argv && strncmp (*argv, "--vista", strlen(*argv)) == 0) {
        rwl_os_type = WINVISTA_OS;
        rwl_dut_autodetect = FALSE;
        (void)*argv++;
    }

    /* Provide option for disabling remote DUT autodetect */
    if (*argv && strncmp(*argv, "--nodetect", strlen(*argv)) == 0) {
        rwl_dut_autodetect = FALSE;
        argv++;
    }

    if (*argv && strncmp (*argv, "--debug", strlen(*argv)) == 0) {
        debug = TRUE;
        argv++;
    }

    /* RWL socket transport Usage: --socket ipaddr/hostname [port num] */
    if (*argv && strncmp (*argv, "--socket", strlen(*argv)) == 0) {
        (void)*argv++;

        remote_type = REMOTE_SOCKET;

        if (!(*argv)) {
            rwl_usage(remote_type);
            return err;
        }
        /* IP address validation is done in client_shared file */
        g_rwl_servIP = *argv;
        (void)*argv++;

        g_rwl_servport = DEFAULT_SERVER_PORT;
        if ((*argv) && isdigit(**argv)) {
            g_rwl_servport = atoi(*argv);
            (void)*argv++;
        }
    }

    /* RWL from system serial port on client to uart serial port on server */
    /* Usage: --serial /dev/ttyS0 */
    if (*argv && strncmp (*argv, "--serial", strlen(*argv)) == 0) {
        (void)*argv++;
        remote_type = REMOTE_SERIAL;
    }

    /* RWL from system serial port on client to uart dongle port on server */
    /* Usage: --dongle /dev/ttyS0 */
    if (*argv && strncmp (*argv, "--dongle", strlen(*argv)) == 0) {
        (void)*argv++;
        remote_type = REMOTE_DONGLE;
    }

#if defined(RWL_SERIAL) || defined(RWL_DONGLE)
    if (remote_type == REMOTE_SERIAL || remote_type == REMOTE_DONGLE) {
        if (!(*argv)) {
            rwl_usage(remote_type);
            return err;
        }
        g_rwl_device_name_serial = *argv;
        (void)*argv++;
        if ((serialHandle = rwl_open_pipe(remote_type, g_rwl_device_name_serial, 0, 0))
                == NULL) {
            DPRINT_ERR(ERR, "serial device open error\r\n");
            return -1;
        }
        ifr = (*(struct ifreq *)serialHandle);
    }
#endif /*  RWL_SERIAL */

    /* RWL over wifi.  Usage: --wifi mac_address */
    if (*argv && strncmp (*argv, "--wifi", strlen(*argv)) == 0) {
        (void)*argv++;
        /* use default interface */
        if (!*ifr.ifr_name)
            wl_find(&ifr);
        /* validate the interface */
        if (!*ifr.ifr_name) {
            errno = ENXIO;
            syserr("interface");
        }
        if ((err = wl_check((void *)&ifr)) < 0) {
            fprintf(stderr, "%s: wl driver adapter not found\n", wlu_av0);
            exit(1);
        }

        remote_type = REMOTE_WIFI;

        if (argc < 4) {
            rwl_usage(remote_type);
            return err;
        }
        /* copy server mac address to local buffer for later use by findserver cmd */
        if (!wl_ether_atoe(*argv, (struct ether_addr *)g_rwl_buf_mac)) {
            fprintf(stderr,
                    "could not parse as an ethternet MAC address\n");
            return FAIL;
        }
        (void)*argv++;
    }

    if ((*argv) && (strlen(*argv) > 2) &&
            (strncmp(*argv, "--interactive", strlen(*argv)) == 0)) {
        interactive_flag = 1;
    }

    /* Process for local wl */
    if (remote_type == NO_REMOTE) {
        if (interactive_flag == 1)
            (void)*argv--;
        err = process_args(&ifr, argv);
        return err;
    } else {
#ifndef OLYMPIC_RWL
        /* Autodetect remote DUT */
        if (rwl_dut_autodetect == TRUE)
            rwl_detect((void*)&ifr, debug, &rwl_os_type);
#endif /* OLYMPIC_RWL */
    }

    /* RWL client needs to initialize ioctl_version */
    if (wl_check((void *)&ifr) != 0) {
        fprintf(stderr, "%s: wl driver adapter not found\n", wlu_av0);
        exit(1);
    }

    if (interactive_flag == 1) {
        err = do_interactive(&ifr);
        return err;
    }

    if ((*argv) && (interactive_flag == 0)) {
        err = process_args(&ifr, argv);
        if ((err == SERIAL_PORT_ERR) && (remote_type == REMOTE_DONGLE)) {
            DPRINT_ERR(ERR, "\n Retry again\n");
            err = process_args((struct ifreq*)&ifr, argv);
        }
        return err;
    }
    rwl_usage(remote_type);
#if defined(RWL_DONGLE) || RWL_SERIAL
    if (remote_type == REMOTE_DONGLE || remote_type == REMOTE_SERIAL)
        rwl_close_pipe(remote_type, (void*)&ifr);
#endif /* RWL_DONGLE || RWL_SERIAL */
    return err;
}

/*
 * Function called for  'local' execution and for 'remote' non-interactive session
 * (shell cmd, wl cmd)
 */
int
process_args(struct ifreq* ifr, char **argv)
{
    char *ifname = NULL;
    int help = 0;
    int status = 0;
    int vista_cmd_index;
    int err = 0;
    cmd_t *cmd = NULL;
#ifdef RWL_WIFI
    int retry;
#endif

    while (*argv) {
        if ((strcmp (*argv, "sh") == 0) && (remote_type != NO_REMOTE)) {
            (void)*argv++; /* Get the shell command */
            if (*argv) {
                /* Register handler in case of shell command only */
                err = rwl_shell_cmd_proc((void*)ifr, argv, SHELL_CMD);
            } else {
                DPRINT_ERR(ERR, "Enter the shell "
                           "command, e.g. ls(Linux) or dir(Win CE)\n");
                err = -1;
            }
            return err;
        }

#ifdef RWLASD
        if ((strcmp (*argv, "asd") == 0) && (remote_type != NO_REMOTE)) {
            (void)*argv++; /* Get the asd command */
            if (*argv) {
                err = rwl_shell_cmd_proc((void*)ifr, argv, ASD_CMD);
            } else {
                DPRINT_ERR(ERR, "Enter the ASD command, e.g. ca_get_version\n");
                err = -1;
            }
            return err;
        }
#endif
        if (rwl_os_type == WINVISTA_OS) {
            for (vista_cmd_index = 0; remote_vista_cmds[vista_cmd_index] &&
                    strcmp(remote_vista_cmds[vista_cmd_index], *argv);
                    vista_cmd_index++);
            if (remote_vista_cmds[vista_cmd_index] != NULL) {
                err = rwl_shell_cmd_proc((void *)ifr, argv, VISTA_CMD);
                if ((remote_type == REMOTE_WIFI) && ((!strcmp(*argv, "join")))) {
#ifdef RWL_WIFI
                    DPRINT_INFO(OUTPUT,
                                "\nChannel will be synchronized by Findserver\n\n");
                    sleep(RWL_WIFI_JOIN_DELAY);
                    for (retry = 0; retry < RWL_WIFI_RETRY; retry++) {
                        if ((rwl_find_remote_wifi_server(ifr,
                                                         &g_rwl_buf_mac[0]) == 0)) {
                            break;
                        }
                    }
#endif /* RWL_WIFI */
                }
                return err;
            }
        }

        if ((status = wl_option(&argv, &ifname, &help)) == CMD_OPT) {
            if (help)
                break;
            if (ifname) {
                if (remote_type == NO_REMOTE) {
                    strncpy((*ifr).ifr_name, ifname, IFNAMSIZ);
                }
                else {
                    strncpy(g_rem_ifname, ifname, IFNAMSIZ);
                }
            }
            continue;
        }
        /* parse error */
        else if (status == CMD_ERR)
            break;

        if (remote_type == NO_REMOTE) {
            /* use default interface */
            if (!*(*ifr).ifr_name)
                wl_find(ifr);
            /* validate the interface */
            if (!*(*ifr).ifr_name) {
                errno = ENXIO;
                syserr("interface");
            }
            if ((err = wl_check((void *)ifr)) < 0) {
                fprintf(stderr, "%s: wl driver adapter not found\n", wlu_av0);
                exit(1);
            }

            if ((strcmp (*argv, "--interactive") == 0) || (interactive_flag == 1)) {
                err = do_interactive(ifr);
                return err;
            }
        }
        /* search for command */
        cmd = wl_find_cmd(*argv);
        /* if not found, use default set_var and get_var commands */
        if (!cmd) {
            cmd = &wl_varcmd;
        }
#ifdef RWL_WIFI
        if (!strcmp(cmd->name, "findserver")) {
            remote_wifi_ser_init_cmds((void *) ifr);
        }
#endif /* RWL_WIFI */

        /* RWL over Wifi supports 'lchannel' command which lets client
         * (ie *this* machine) change channels since normal 'channel' command
         * applies to the server (ie target machine)
         */
        if (remote_type == REMOTE_WIFI)	{
#ifdef RWL_WIFI
            if (!strcmp(argv[0], "lchannel")) {
                strcpy(argv[0], "channel");
                rwl_wifi_swap_remote_type(remote_type);
                err = (*cmd->func)((void *) ifr, cmd, argv);
                rwl_wifi_swap_remote_type(remote_type);
            } else {
                err = (*cmd->func)((void *) ifr, cmd, argv);
            }
            /* After join cmd's gets exeuted on the server side , client needs to know
            * the channel on which the server is associated with AP , after delay of
            * few seconds client will intiate the scan on diffrent channels by calling
            * rwl_find_remote_wifi_server fucntion
            */
            if ((!strcmp(cmd->name, "join") || ((!strcmp(cmd->name, "ssid") &&
                                                 (*(++argv) != NULL))))) {
                DPRINT_INFO(OUTPUT, "\n Findserver is called to synchronize the"
                            "channel\n\n");
                sleep(RWL_WIFI_JOIN_DELAY);
                for (retry = 0; retry < RWL_WIFI_RETRY; retry++) {
                    if ((rwl_find_remote_wifi_server(ifr,
                                                     &g_rwl_buf_mac[0]) == 0)) {
                        break;
                    }
                }
            }
#endif /* RWL_WIFI */
        } else {
            /* do command */
            err = (*cmd->func)((void *) ifr, cmd, argv);
        }
        break;
    } /* while loop end */

    /* provide for help on a particular command */
    if (help && *argv) {
        cmd = wl_find_cmd(*argv);
        if (cmd) {
            wl_cmd_usage(stdout, cmd);
        } else {
            DPRINT_ERR(ERR, "%s: Unrecognized command \"%s\", type -h for help\n",
                       wlu_av0, *argv);
        }
    } else if (!cmd)
        wl_usage(stdout, NULL);
    else if (err == USAGE_ERROR)
        wl_cmd_usage(stderr, cmd);
    else if (err == IOCTL_ERROR)
        wl_printlasterror((void *) ifr);
    else if (err == BCME_NODEVICE)
        DPRINT_ERR(ERR, "%s : wl driver adapter not found\n", g_rem_ifname);

    return err;
}