Beispiel #1
0
static enum cmd_status cmd_mqtt_subscribe_exec(char *cmd)
{
	int32_t cnt;
	uint32_t i;
	char *tmp;
	uint32_t qos;
	int rc;

	/* get param */
	cnt = cmd_sscanf(cmd, "qos=%u",
			 &qos);

	if ((tmp = cmd_strrchr(cmd, '\"')) == NULL)
		return CMD_STATUS_INVALID_ARG;
	*tmp = '\0';
	if ((tmp = cmd_strchr(cmd, '\"')) == NULL)
		return CMD_STATUS_INVALID_ARG;
	tmp++;


	/* check param */
	if (cnt != 1) {
		CMD_ERR("invalid param number %d\n", cnt);
		return CMD_STATUS_INVALID_ARG;
	}

	if (qos > 2) {
		CMD_ERR("invalid qos %d\n", qos);
		return CMD_STATUS_INVALID_ARG;
	}

	i = 0;
	while (sub_topic[i] != NULL) {
		i++;
		if (i >= MAX_SUB_TOPICS) {
			CMD_WRN("Subscribe topics is max\n");
			return CMD_STATUS_FAIL;
		}
	}

	sub_topic[i] = cmd_malloc(cmd_strlen(tmp) + 1);
	cmd_memcpy(sub_topic[i], tmp, cmd_strlen(tmp) + 1);

	if (OS_MutexLock(&lock, 60000) == OS_E_TIMEOUT)
		return CMD_STATUS_FAIL;

	if ((rc = MQTTSubscribe(&client, sub_topic[i], qos, mqtt_msg_cb)) != 0) {
		CMD_ERR("Return code from MQTT subscribe is %d\n", rc);
		return CMD_STATUS_FAIL;
	}

	OS_MutexUnlock(&lock);

	return CMD_STATUS_OK;
}
Beispiel #2
0
static enum cmd_status cmd_mqtt_connect_exec(char *cmd)
{
	int32_t cnt;
	char addr_str[15];
	uint32_t port;
	int32_t rc;
	uint32_t i;

	/* get param */
	cnt = cmd_sscanf(cmd, "server=%15s port=%u",
			 addr_str, &port);

	/* check param */
	if (cnt != 2) {
		CMD_ERR("invalid param number %d\n", cnt);
		return CMD_STATUS_INVALID_ARG;
	}

	i = 10;
	char* address = addr_str;
	while ((rc = ConnectNetwork(&network, address, port)) != 0)
	{
		CMD_WRN("Return code from network connect is %d\n", rc);
		cmd_msleep(2000);
		if (!(i--))
			return CMD_STATUS_FAIL;
	}

	if ((rc = MQTTConnect(&client, &connectData)) != 0) {
		CMD_ERR("Return code from MQTT connect is %d\n", rc);
		return CMD_STATUS_FAIL;
	}
	else
		CMD_DBG("MQTT Connected\n");



	if (OS_ThreadCreate(&mqtt_bg_thread,
		                "",
		                cmd_mqtt_bg,
		                NULL,
		                OS_PRIORITY_NORMAL,
		                CMD_MQTT_BG_THREAD_STACK_SIZE) != OS_OK) {
		CMD_ERR("create mqtt background failed\n");
		return CMD_STATUS_FAIL;
	}


	return CMD_STATUS_OK;
}
Beispiel #3
0
int
cli_resource_print_attribute(const char *rsc, const char *attr, pe_working_set_t * data_set)
{
    int rc = -ENXIO;
    node_t *current = NULL;
    GHashTable *params = NULL;
    resource_t *the_rsc = find_rsc_or_clone(rsc, data_set);
    const char *value = NULL;

    if (the_rsc == NULL) {
        return -ENXIO;
    }

    if (g_list_length(the_rsc->running_on) == 1) {
        current = the_rsc->running_on->data;

    } else if (g_list_length(the_rsc->running_on) > 1) {
        CMD_ERR("%s is active on more than one node,"
                " returning the default value for %s", the_rsc->id, crm_str(value));
    }

    params = g_hash_table_new_full(crm_str_hash, g_str_equal,
                                   g_hash_destroy_str, g_hash_destroy_str);

    if (safe_str_eq(attr_set_type, XML_TAG_ATTR_SETS)) {
        get_rsc_attributes(params, the_rsc, current, data_set);

    } else if (safe_str_eq(attr_set_type, XML_TAG_META_SETS)) {
        /* No need to redirect to the parent */ 
        get_meta_attributes(params, the_rsc, current, data_set);

    } else {
        unpack_instance_attributes(data_set->input, the_rsc->xml, XML_TAG_UTILIZATION, NULL,
                                   params, NULL, FALSE, data_set->now);
    }

    crm_debug("Looking up %s in %s", attr, the_rsc->id);
    value = g_hash_table_lookup(params, attr);
    if (value != NULL) {
        fprintf(stdout, "%s\n", value);
        rc = 0;

    } else {
        CMD_ERR("Attribute '%s' not found for '%s'", attr, the_rsc->id);
    }

    g_hash_table_destroy(params);
    return rc;
}
Beispiel #4
0
static int 
default_action_third(char *cmd)
{
	int ret;

	ret = convert_command_tbl(cmd);
	switch(ret)
	{
		case CMD_LS:
			ls_type = 1;
			return IS_LS;
			break;
		case CMD_FIND:
			find_type = FIND_DEFAULT; //find . -name argv[2];
			return IS_FIND;
			break;
		case CMD_GREP:
			return IS_GREP;
			break;
		default:
			CMD_ERR(cmd,"%s","This command may not support by ncurses");
	}

	return -1;
}
Beispiel #5
0
static enum cmd_status cmd_mqtt_disconnect_exec(char *cmd)
{
	int32_t cnt;
	uint32_t tcponly;
	int rc;
	uint32_t i;

	/* get param */
	cnt = cmd_sscanf(cmd, "tcponly=%u",
			 &tcponly);

	/* check param */
	if (cnt != 1) {
		CMD_ERR("invalid param number %d\n", cnt);
		return CMD_STATUS_INVALID_ARG;
	}

	if (tcponly > 1) {
		CMD_ERR("invalid tcponly %d\n", tcponly);
		return CMD_STATUS_INVALID_ARG;
	}


	OS_ThreadDelete(&mqtt_bg_thread);

	i = 0;
	while (i < MAX_SUB_TOPICS) {
		if (sub_topic[i] != NULL) {
			cmd_free(sub_topic[i]);
			sub_topic[i] = NULL;
		}
		i++;
	}

	if (tcponly == 0) {
		if ((rc = MQTTDisconnect(&client)) != 0){
			CMD_ERR("Return code from MQTT disconnect is %d\n", rc);
			network.disconnect(&network);
			return CMD_STATUS_FAIL;
		}
	} else {
		network.disconnect(&network);
	}

	return CMD_STATUS_OK;
}
Beispiel #6
0
int sntp_start()
{
        if (OS_ThreadIsValid(&g_sntp_thread)) {
                CMD_ERR("sntp task is running\n");
                return -1;
        }

        if (OS_ThreadCreate(&g_sntp_thread,
                                "",
                                sntp_run,
                                NULL,
                                OS_THREAD_PRIO_APP,
                                SNTP_THREAD_STACK_SIZE) != OS_OK) {
                CMD_ERR("sntp task create failed\n");
                return -1;
        }

        return 0;
}
int
cli_resource_print_attribute(resource_t *rsc, const char *attr, pe_working_set_t * data_set)
{
    int rc = -ENXIO;
    unsigned int count = 0;
    GHashTable *params = NULL;
    const char *value = NULL;
    node_t *current = pe__find_active_on(rsc, &count, NULL);

    if (count > 1) {
        CMD_ERR("%s is active on more than one node,"
                " returning the default value for %s", rsc->id, crm_str(attr));
        current = NULL;
    }

    params = crm_str_table_new();

    if (safe_str_eq(attr_set_type, XML_TAG_ATTR_SETS)) {
        get_rsc_attributes(params, rsc, current, data_set);

    } else if (safe_str_eq(attr_set_type, XML_TAG_META_SETS)) {
        /* No need to redirect to the parent */
        get_meta_attributes(params, rsc, current, data_set);

    } else {
        unpack_instance_attributes(data_set->input, rsc->xml,
                                   XML_TAG_UTILIZATION, NULL,
                                   params, NULL, FALSE, data_set->now);
    }

    crm_debug("Looking up %s in %s", attr, rsc->id);
    value = g_hash_table_lookup(params, attr);
    if (value != NULL) {
        fprintf(stdout, "%s\n", value);
        rc = 0;

    } else {
        CMD_ERR("Attribute '%s' not found for '%s'", attr, rsc->id);
    }

    g_hash_table_destroy(params);
    return rc;
}
Beispiel #8
0
static int cmd_wpas_parse_int(const char *value, int min, int max, int *dst)
{
	int val;
	char *end;

	val = cmd_strtol(value, &end, 0);
	if (*end) {
		CMD_ERR("Invalid number '%s'", value);
		return -1;
	}

	if (val < min || val > max) {
		CMD_ERR("out of range value %d (%s), range is [%d, %d]\n",
			 val, value, min, max);
		return -1;
	}

	*dst = val;
	return 0;
}
static char *
parse_cli_lifetime(const char *input)
{
    char *later_s = NULL;
    crm_time_t *now = NULL;
    crm_time_t *later = NULL;
    crm_time_t *duration = NULL;

    if (input == NULL) {
        return NULL;
    }

    duration = crm_time_parse_duration(move_lifetime);
    if (duration == NULL) {
        CMD_ERR("Invalid duration specified: %s", move_lifetime);
        CMD_ERR("Please refer to"
                " http://en.wikipedia.org/wiki/ISO_8601#Durations"
                " for examples of valid durations");
        return NULL;
    }

    now = crm_time_new(NULL);
    later = crm_time_add(now, duration);
    crm_time_log(LOG_INFO, "now     ", now,
                 crm_time_log_date | crm_time_log_timeofday | crm_time_log_with_timezone);
    crm_time_log(LOG_INFO, "later   ", later,
                 crm_time_log_date | crm_time_log_timeofday | crm_time_log_with_timezone);
    crm_time_log(LOG_INFO, "duration", duration, crm_time_log_date | crm_time_log_timeofday);
    later_s = crm_time_as_string(later, crm_time_log_date | crm_time_log_timeofday | crm_time_log_with_timezone);
    printf("Migration will take effect until: %s\n", later_s);

    crm_time_free(duration);
    crm_time_free(later);
    crm_time_free(now);
    return later_s;
}
Beispiel #10
0
static enum cmd_status cmd_mqtt_unsubscribe_exec(char *cmd)
{
	uint32_t i;
	char *tmp;
	int rc;

	/* get param */
	if ((tmp = cmd_strrchr(cmd, '\"')) == NULL)
		return CMD_STATUS_INVALID_ARG;
	*tmp = '\0';
	if ((tmp = cmd_strchr(cmd, '\"')) == NULL)
		return CMD_STATUS_INVALID_ARG;
	tmp++;

	i = 0;
	while (i < MAX_SUB_TOPICS) {
		if ((sub_topic[i] != NULL) && (!cmd_memcmp(sub_topic[i], tmp, cmd_strlen(tmp) + 1)))
			break;
		i++;
	}

	if (OS_MutexLock(&lock, 60000) == OS_E_TIMEOUT)
		return CMD_STATUS_FAIL;

	if ((rc = MQTTUnsubscribe(&client, tmp/*sub_topic[i]*/)) != 0) {
		CMD_ERR("Return code from MQTT unsubscribe is %d\n", rc);
		return CMD_STATUS_FAIL;
	}

	if (i == MAX_SUB_TOPICS)
		CMD_WRN("Unsubscribe topics is inexist\n");
	else {
		cmd_free(sub_topic[i]);
		sub_topic[i] = NULL;
	}

	OS_MutexUnlock(&lock);

	return CMD_STATUS_OK;
}
Beispiel #11
0
/**
 *This function is involed when the argc == 2,
 *We just action the basement of the command
 *may some commands need argument,so here will
 *make a dicision
 */
static int
default_action(char *cmd)
{
	int ret;

	ret = convert_command_tbl(cmd);	
	switch(ret)
	{
			case CMD_LS:
				ls_type = 0;
				return IS_LS;
				break;
			case CMD_FIND:
				show_find_usage();
				break;
			case CMD_GREP:
				CMD_LOG("grep","%s\n","argument is needed");
				break;
			default:
				CMD_ERR(cmd,"%s","This command may not show output by ncurses");
	}
	
	return -1;
}
int
cli_resource_ban(const char *rsc_id, const char *host, GListPtr allnodes, cib_t * cib_conn)
{
    char *later_s = NULL;
    int rc = pcmk_ok;
    xmlNode *fragment = NULL;
    xmlNode *location = NULL;

    if(host == NULL) {
        GListPtr n = allnodes;
        for(; n && rc == pcmk_ok; n = n->next) {
            node_t *target = n->data;

            rc = cli_resource_ban(rsc_id, target->details->uname, NULL, cib_conn);
        }
        return rc;
    }

    later_s = parse_cli_lifetime(move_lifetime);
    if(move_lifetime && later_s == NULL) {
        return -EINVAL;
    }

    fragment = create_xml_node(NULL, XML_CIB_TAG_CONSTRAINTS);

    location = create_xml_node(fragment, XML_CONS_TAG_RSC_LOCATION);
    crm_xml_set_id(location, "cli-ban-%s-on-%s", rsc_id, host);

    if (BE_QUIET == FALSE) {
        CMD_ERR("WARNING: Creating rsc_location constraint '%s'"
                " with a score of -INFINITY for resource %s"
                " on %s.", ID(location), rsc_id, host);
        CMD_ERR("\tThis will prevent %s from %s on %s until the constraint "
                "is removed using the clear option or by editing the CIB "
                "with an appropriate tool",
                rsc_id, (scope_master? "being promoted" : "running"), host);
        CMD_ERR("\tThis will be the case even if %s is"
                " the last node in the cluster", host);
    }

    crm_xml_add(location, XML_LOC_ATTR_SOURCE, rsc_id);
    if(scope_master) {
        crm_xml_add(location, XML_RULE_ATTR_ROLE, RSC_ROLE_MASTER_S);
    } else {
        crm_xml_add(location, XML_RULE_ATTR_ROLE, RSC_ROLE_STARTED_S);
    }

    if (later_s == NULL) {
        /* Short form */
        crm_xml_add(location, XML_CIB_TAG_NODE, host);
        crm_xml_add(location, XML_RULE_ATTR_SCORE, CRM_MINUS_INFINITY_S);

    } else {
        xmlNode *rule = create_xml_node(location, XML_TAG_RULE);
        xmlNode *expr = create_xml_node(rule, XML_TAG_EXPRESSION);

        crm_xml_set_id(rule, "cli-ban-%s-on-%s-rule", rsc_id, host);
        crm_xml_add(rule, XML_RULE_ATTR_SCORE, CRM_MINUS_INFINITY_S);
        crm_xml_add(rule, XML_RULE_ATTR_BOOLEAN_OP, "and");

        crm_xml_set_id(expr, "cli-ban-%s-on-%s-expr", rsc_id, host);
        crm_xml_add(expr, XML_EXPR_ATTR_ATTRIBUTE, CRM_ATTR_UNAME);
        crm_xml_add(expr, XML_EXPR_ATTR_OPERATION, "eq");
        crm_xml_add(expr, XML_EXPR_ATTR_VALUE, host);
        crm_xml_add(expr, XML_EXPR_ATTR_TYPE, "string");

        expr = create_xml_node(rule, "date_expression");
        crm_xml_set_id(expr, "cli-ban-%s-on-%s-lifetime", rsc_id, host);
        crm_xml_add(expr, "operation", "lt");
        crm_xml_add(expr, "end", later_s);
    }

    crm_log_xml_notice(fragment, "Modify");
    rc = cib_conn->cmds->update(cib_conn, XML_CIB_TAG_CONSTRAINTS, fragment, cib_options);

    free_xml(fragment);
    free(later_s);
    return rc;
}
Beispiel #13
0
static enum cmd_status cmd_mqtt_init_exec(char *cmd)
{
	int32_t cnt;
	uint32_t buf_size;
	uint32_t alive_interval;
	uint32_t clean;
	char *tmp;

	/* get param */
	cnt = cmd_sscanf(cmd, "bufsize=%u alive=%u clean=%u",
			 &buf_size, &alive_interval, &clean);

	/* check param */
	if (cnt != 3) {
		CMD_ERR("invalid param number %d\n", cnt);
		return CMD_STATUS_INVALID_ARG;
	}

	if (clean > 1) {
		CMD_ERR("invalid clean %d\n", clean);
		return CMD_STATUS_INVALID_ARG;
	}

	if (OS_MutexCreate(&lock) != OS_OK)
		return CMD_STATUS_FAIL;;

	if ((tmp = cmd_strrchr(cmd, '\"')) == NULL)
		return CMD_STATUS_INVALID_ARG;
	*tmp = '\0';
	if ((tmp = cmd_strchr(cmd, '\"')) == NULL)
		return CMD_STATUS_INVALID_ARG;
	tmp++;

	client_name = cmd_malloc(cmd_strlen(tmp) + 1);

	cmd_memcpy(client_name, tmp, cmd_strlen(tmp) + 1);
	CMD_DBG("client name = %s\n", client_name);

	connectData.clientID.cstring = client_name;
	connectData.keepAliveInterval = alive_interval;
	connectData.cleansession = clean;

	send_buf = cmd_malloc(buf_size);
	if (send_buf == NULL) {
		CMD_ERR("no memory\n");
		OS_MutexDelete(&lock);
		return CMD_STATUS_FAIL;
	}
	recv_buf = cmd_malloc(buf_size);
	if (recv_buf == NULL) {
		cmd_free(send_buf);
		CMD_ERR("no memory\n");
		OS_MutexDelete(&lock);
		return CMD_STATUS_FAIL;
	}

	NewNetwork(&network);
	MQTTClient(&client, &network, 6000, send_buf, buf_size, recv_buf, buf_size);



	return CMD_STATUS_OK;
}
Beispiel #14
0
int
main(int argc, char **argv)
{
    char rsc_cmd = 'L';

    const char *v_class = NULL;
    const char *v_agent = NULL;
    const char *v_provider = NULL;
    char *name = NULL;
    char *value = NULL;
    GHashTable *validate_options = NULL;

    const char *rsc_id = NULL;
    const char *host_uname = NULL;
    const char *prop_name = NULL;
    const char *prop_value = NULL;
    const char *rsc_type = NULL;
    const char *prop_id = NULL;
    const char *prop_set = NULL;
    const char *rsc_long_cmd = NULL;
    const char *longname = NULL;
    const char *operation = NULL;
    const char *interval_spec = NULL;
    const char *cib_file = getenv("CIB_file");
    GHashTable *override_params = NULL;

    char *xml_file = NULL;
    crm_ipc_t *crmd_channel = NULL;
    pe_working_set_t *data_set = NULL;
    xmlNode *cib_xml_copy = NULL;
    cib_t *cib_conn = NULL;
    resource_t *rsc = NULL;
    bool recursive = FALSE;
    char *our_pid = NULL;

    bool validate_cmdline = FALSE; /* whether we are just validating based on command line options */
    bool require_resource = TRUE; /* whether command requires that resource be specified */
    bool require_dataset = TRUE;  /* whether command requires populated dataset instance */
    bool require_crmd = FALSE;    // whether command requires controller connection
    bool clear_expired = FALSE;

    int rc = pcmk_ok;
    int is_ocf_rc = 0;
    int option_index = 0;
    int timeout_ms = 0;
    int argerr = 0;
    int flag;
    int find_flags = 0;           // Flags to use when searching for resource
    crm_exit_t exit_code = CRM_EX_OK;

    crm_log_cli_init("crm_resource");
    crm_set_options(NULL, "(query|command) [options]", long_options,
                    "Perform tasks related to cluster resources.\nAllows resources to be queried (definition and location), modified, and moved around the cluster.\n");

    validate_options = crm_str_table_new();

    while (1) {
        flag = crm_get_option_long(argc, argv, &option_index, &longname);
        if (flag == -1)
            break;

        switch (flag) {
            case 0: /* long options with no short equivalent */
                if (safe_str_eq("master", longname)) {
                    scope_master = TRUE;

                } else if(safe_str_eq(longname, "recursive")) {
                    recursive = TRUE;

                } else if (safe_str_eq("wait", longname)) {
                    rsc_cmd = flag;
                    rsc_long_cmd = longname;
                    require_resource = FALSE;
                    require_dataset = FALSE;

                } else if (
                    safe_str_eq("validate", longname)
                    || safe_str_eq("restart", longname)
                    || safe_str_eq("force-demote",  longname)
                    || safe_str_eq("force-stop",    longname)
                    || safe_str_eq("force-start",   longname)
                    || safe_str_eq("force-promote", longname)
                    || safe_str_eq("force-check",   longname)) {
                    rsc_cmd = flag;
                    rsc_long_cmd = longname;
                    find_flags = pe_find_renamed|pe_find_anon;
                    crm_log_args(argc, argv);

                } else if (safe_str_eq("list-ocf-providers", longname)
                           || safe_str_eq("list-ocf-alternatives", longname)
                           || safe_str_eq("list-standards", longname)) {
                    const char *text = NULL;
                    lrmd_list_t *list = NULL;
                    lrmd_list_t *iter = NULL;
                    lrmd_t *lrmd_conn = lrmd_api_new();

                    if (safe_str_eq("list-ocf-providers", longname)
                        || safe_str_eq("list-ocf-alternatives", longname)) {
                        rc = lrmd_conn->cmds->list_ocf_providers(lrmd_conn, optarg, &list);
                        text = "OCF providers";

                    } else if (safe_str_eq("list-standards", longname)) {
                        rc = lrmd_conn->cmds->list_standards(lrmd_conn, &list);
                        text = "standards";
                    }

                    if (rc > 0) {
                        for (iter = list; iter != NULL; iter = iter->next) {
                            printf("%s\n", iter->val);
                        }
                        lrmd_list_freeall(list);

                    } else if (optarg) {
                        fprintf(stderr, "No %s found for %s\n", text, optarg);
                        exit_code = CRM_EX_NOSUCH;

                    } else {
                        fprintf(stderr, "No %s found\n", text);
                        exit_code = CRM_EX_NOSUCH;
                    }

                    lrmd_api_delete(lrmd_conn);
                    crm_exit(exit_code);

                } else if (safe_str_eq("show-metadata", longname)) {
                    char *standard = NULL;
                    char *provider = NULL;
                    char *type = NULL;
                    char *metadata = NULL;
                    lrmd_t *lrmd_conn = lrmd_api_new();

                    rc = crm_parse_agent_spec(optarg, &standard, &provider, &type);
                    if (rc == pcmk_ok) {
                        rc = lrmd_conn->cmds->get_metadata(lrmd_conn, standard,
                                                           provider, type,
                                                           &metadata, 0);
                    } else {
                        fprintf(stderr,
                                "'%s' is not a valid agent specification\n",
                                optarg);
                        rc = -ENXIO;
                    }

                    if (metadata) {
                        printf("%s\n", metadata);
                    } else {
                        fprintf(stderr, "Metadata query for %s failed: %s\n",
                                optarg, pcmk_strerror(rc));
                        exit_code = crm_errno2exit(rc);
                    }
                    lrmd_api_delete(lrmd_conn);
                    crm_exit(exit_code);

                } else if (safe_str_eq("list-agents", longname)) {
                    lrmd_list_t *list = NULL;
                    lrmd_list_t *iter = NULL;
                    char *provider = strchr (optarg, ':');
                    lrmd_t *lrmd_conn = lrmd_api_new();

                    if (provider) {
                        *provider++ = 0;
                    }
                    rc = lrmd_conn->cmds->list_agents(lrmd_conn, &list, optarg, provider);

                    if (rc > 0) {
                        for (iter = list; iter != NULL; iter = iter->next) {
                            printf("%s\n", iter->val);
                        }
                        lrmd_list_freeall(list);
                    } else {
                        fprintf(stderr, "No agents found for standard=%s, provider=%s\n",
                                optarg, (provider? provider : "*"));
                        exit_code = CRM_EX_NOSUCH;
                    }
                    lrmd_api_delete(lrmd_conn);
                    crm_exit(exit_code);

                } else if (safe_str_eq("class", longname)) {
                    if (!(pcmk_get_ra_caps(optarg) & pcmk_ra_cap_params)) {
                        if (BE_QUIET == FALSE) {
                            fprintf(stdout, "Standard %s does not support parameters\n",
                                    optarg);
                        }

                        crm_exit(exit_code);
                    } else {
                        v_class = optarg;
                    }

                    validate_cmdline = TRUE;
                    require_resource = FALSE;

                } else if (safe_str_eq("agent", longname)) {
                    validate_cmdline = TRUE;
                    require_resource = FALSE;
                    v_agent = optarg;

                } else if (safe_str_eq("provider", longname)) {
                    validate_cmdline = TRUE;
                    require_resource = FALSE;
                    v_provider = optarg;

                } else if (safe_str_eq("option", longname)) {
                    crm_info("Scanning: --option %s", optarg);
                    rc = pcmk_scan_nvpair(optarg, &name, &value);
                    if (rc != 2) {
                        fprintf(stderr, "Invalid option: --option %s: %s", optarg, pcmk_strerror(rc));
                        argerr++;
                    } else {
                        crm_info("Got: '%s'='%s'", name, value);
                    }

                    g_hash_table_replace(validate_options, name, value);

                } else {
                    crm_err("Unhandled long option: %s", longname);
                }
                break;
            case 'V':
                resource_verbose++;
                crm_bump_log_level(argc, argv);
                break;
            case '$':
            case '?':
                crm_help(flag, CRM_EX_OK);
                break;
            case 'x':
                xml_file = strdup(optarg);
                break;
            case 'Q':
                BE_QUIET = TRUE;
                break;
            case 'm':
                attr_set_type = XML_TAG_META_SETS;
                break;
            case 'z':
                attr_set_type = XML_TAG_UTILIZATION;
                break;
            case 'u':
                move_lifetime = strdup(optarg);
                break;
            case 'f':
                do_force = TRUE;
                crm_log_args(argc, argv);
                break;
            case 'i':
                prop_id = optarg;
                break;
            case 's':
                prop_set = optarg;
                break;
            case 'r':
                rsc_id = optarg;
                break;
            case 'v':
                prop_value = optarg;
                break;
            case 't':
                rsc_type = optarg;
                break;
            case 'T':
                timeout_ms = crm_get_msec(optarg);
                break;
            case 'e':
                clear_expired = TRUE;
                require_resource = FALSE;
                break;

            case 'C':
            case 'R':
                crm_log_args(argc, argv);
                require_resource = FALSE;
                if (cib_file == NULL) {
                    require_crmd = TRUE;
                }
                rsc_cmd = flag;
                find_flags = pe_find_renamed|pe_find_anon;
                break;

            case 'n':
                operation = optarg;
                break;

            case 'I':
                interval_spec = optarg;
                break;

            case 'D':
                require_dataset = FALSE;
                crm_log_args(argc, argv);
                rsc_cmd = flag;
                find_flags = pe_find_renamed|pe_find_any;
                break;

            case 'F':
                require_crmd = TRUE;
                crm_log_args(argc, argv);
                rsc_cmd = flag;
                break;

            case 'U':
            case 'B':
            case 'M':
                crm_log_args(argc, argv);
                rsc_cmd = flag;
                find_flags = pe_find_renamed|pe_find_anon;
                break;

            case 'c':
            case 'L':
            case 'l':
            case 'O':
            case 'o':
                require_resource = FALSE;
                rsc_cmd = flag;
                break;

            case 'Y':
                require_resource = FALSE;
                rsc_cmd = flag;
                find_flags = pe_find_renamed|pe_find_anon;
                break;

            case 'q':
            case 'w':
                rsc_cmd = flag;
                find_flags = pe_find_renamed|pe_find_any;
                break;

            case 'W':
            case 'A':
            case 'a':
                rsc_cmd = flag;
                find_flags = pe_find_renamed|pe_find_anon;
                break;

            case 'S':
                require_dataset = FALSE;
                crm_log_args(argc, argv);
                prop_name = optarg;
                rsc_cmd = flag;
                find_flags = pe_find_renamed|pe_find_any;
                break;

            case 'p':
            case 'd':
                crm_log_args(argc, argv);
                prop_name = optarg;
                rsc_cmd = flag;
                find_flags = pe_find_renamed|pe_find_any;
                break;

            case 'G':
            case 'g':
                prop_name = optarg;
                rsc_cmd = flag;
                find_flags = pe_find_renamed|pe_find_any;
                break;

            case 'H':
            case 'N':
                crm_trace("Option %c => %s", flag, optarg);
                host_uname = optarg;
                break;

            default:
                CMD_ERR("Argument code 0%o (%c) is not (?yet?) supported", flag, flag);
                ++argerr;
                break;
        }
    }

    // Catch the case where the user didn't specify a command
    if (rsc_cmd == 'L') {
        require_resource = FALSE;
    }

    // --expired without --clear/-U doesn't make sense
    if (clear_expired == TRUE && rsc_cmd != 'U') {
        CMD_ERR("--expired requires --clear or -U");
        argerr++;
    }

    if (optind < argc
        && argv[optind] != NULL
        && rsc_cmd == 0
        && rsc_long_cmd) {

        override_params = crm_str_table_new();
        while (optind < argc && argv[optind] != NULL) {
            char *name = calloc(1, strlen(argv[optind]));
            char *value = calloc(1, strlen(argv[optind]));
            int rc = sscanf(argv[optind], "%[^=]=%s", name, value);

            if(rc == 2) {
                g_hash_table_replace(override_params, name, value);

            } else {
                CMD_ERR("Error parsing '%s' as a name=value pair for --%s", argv[optind], rsc_long_cmd);
                free(value);
                free(name);
                argerr++;
            }
            optind++;
        }

    } else if (optind < argc && argv[optind] != NULL && rsc_cmd == 0) {
        CMD_ERR("non-option ARGV-elements: ");
        while (optind < argc && argv[optind] != NULL) {
            CMD_ERR("[%d of %d] %s ", optind, argc, argv[optind]);
            optind++;
            argerr++;
        }
    }

    if (optind > argc) {
        ++argerr;
    }

    // Sanity check validating from command line parameters.  If everything checks out,
    // go ahead and run the validation.  This way we don't need a CIB connection.
    if (validate_cmdline == TRUE) {
        // -r cannot be used with any of --class, --agent, or --provider
        if (rsc_id != NULL) {
            CMD_ERR("--resource cannot be used with --class, --agent, and --provider");
            argerr++;

        // If --class, --agent, or --provider are given, --validate must also be given.
        } else if (!safe_str_eq(rsc_long_cmd, "validate")) {
            CMD_ERR("--class, --agent, and --provider require --validate");
            argerr++;

        // Not all of --class, --agent, and --provider need to be given.  Not all
        // classes support the concept of a provider.  Check that what we were given
        // is valid.
        } else if (crm_str_eq(v_class, "stonith", TRUE)) {
            if (v_provider != NULL) {
                CMD_ERR("stonith does not support providers");
                argerr++;

            } else if (stonith_agent_exists(v_agent, 0) == FALSE) {
                CMD_ERR("%s is not a known stonith agent", v_agent ? v_agent : "");
                argerr++;
            }

        } else if (resources_agent_exists(v_class, v_provider, v_agent) == FALSE) {
            CMD_ERR("%s:%s:%s is not a known resource",
                    v_class ? v_class : "",
                    v_provider ? v_provider : "",
                    v_agent ? v_agent : "");
            argerr++;
        }

        if (argerr == 0) {
            rc = cli_resource_execute_from_params("test", v_class, v_provider, v_agent,
                                                  "validate-all", validate_options,
                                                  override_params, timeout_ms);
            exit_code = crm_errno2exit(rc);
            return crm_exit(exit_code);
        }
    }

    if (argerr) {
        CMD_ERR("Invalid option(s) supplied, use --help for valid usage");
        crm_exit(CRM_EX_USAGE);
    }

    our_pid = crm_getpid_s();

    if (do_force) {
        crm_debug("Forcing...");
        cib_options |= cib_quorum_override;
    }

    if (require_resource && !rsc_id) {
        CMD_ERR("Must supply a resource id with -r");
        rc = -ENXIO;
        goto bail;
    }

    if (find_flags && rsc_id) {
        require_dataset = TRUE;
    }

    /* Establish a connection to the CIB manager */
    cib_conn = cib_new();
    rc = cib_conn->cmds->signon(cib_conn, crm_system_name, cib_command);
    if (rc != pcmk_ok) {
        CMD_ERR("Error connecting to the CIB manager: %s", pcmk_strerror(rc));
        goto bail;
    }

    /* Populate working set from XML file if specified or CIB query otherwise */
    if (require_dataset) {
        if (xml_file != NULL) {
            cib_xml_copy = filename2xml(xml_file);

        } else {
            rc = cib_conn->cmds->query(cib_conn, NULL, &cib_xml_copy, cib_scope_local | cib_sync_call);
        }

        if(rc != pcmk_ok) {
            goto bail;
        }

        /* Populate the working set instance */
        data_set = pe_new_working_set();
        if (data_set == NULL) {
            rc = -ENOMEM;
            goto bail;
        }
        rc = update_working_set_xml(data_set, &cib_xml_copy);
        if (rc != pcmk_ok) {
            goto bail;
        }
        cluster_status(data_set);
    }

    // If command requires that resource exist if specified, find it
    if (find_flags && rsc_id) {
        rsc = pe_find_resource_with_flags(data_set->resources, rsc_id,
                                          find_flags);
        if (rsc == NULL) {
            CMD_ERR("Resource '%s' not found", rsc_id);
            rc = -ENXIO;
            goto bail;
        }
    }

    // Establish a connection to the controller if needed
    if (require_crmd) {
        xmlNode *xml = NULL;
        mainloop_io_t *source =
            mainloop_add_ipc_client(CRM_SYSTEM_CRMD, G_PRIORITY_DEFAULT, 0, NULL, &crm_callbacks);
        crmd_channel = mainloop_get_ipc_client(source);

        if (crmd_channel == NULL) {
            CMD_ERR("Error connecting to the controller");
            rc = -ENOTCONN;
            goto bail;
        }

        xml = create_hello_message(our_pid, crm_system_name, "0", "1");
        crm_ipc_send(crmd_channel, xml, 0, 0, NULL);
        free_xml(xml);
    }

    /* Handle rsc_cmd appropriately */
    if (rsc_cmd == 'L') {
        rc = pcmk_ok;
        cli_resource_print_list(data_set, FALSE);

    } else if (rsc_cmd == 'l') {
        int found = 0;
        GListPtr lpc = NULL;

        rc = pcmk_ok;
        for (lpc = data_set->resources; lpc != NULL; lpc = lpc->next) {
            rsc = (resource_t *) lpc->data;

            found++;
            cli_resource_print_raw(rsc);
        }

        if (found == 0) {
            printf("NO resources configured\n");
            rc = -ENXIO;
        }

    } else if (rsc_cmd == 0 && rsc_long_cmd && safe_str_eq(rsc_long_cmd, "restart")) {
        /* We don't pass data_set because rsc needs to stay valid for the entire
         * lifetime of cli_resource_restart(), but it will reset and update the
         * working set multiple times, so it needs to use its own copy.
         */
        rc = cli_resource_restart(rsc, host_uname, timeout_ms, cib_conn);

    } else if (rsc_cmd == 0 && rsc_long_cmd && safe_str_eq(rsc_long_cmd, "wait")) {
        rc = wait_till_stable(timeout_ms, cib_conn);

    } else if (rsc_cmd == 0 && rsc_long_cmd) {
        // validate, force-(stop|start|demote|promote|check)
        rc = cli_resource_execute(rsc, rsc_id, rsc_long_cmd, override_params,
                                  timeout_ms, cib_conn, data_set);
        if (rc >= 0) {
            is_ocf_rc = 1;
        }

    } else if (rsc_cmd == 'A' || rsc_cmd == 'a') {
        GListPtr lpc = NULL;
        xmlNode *cib_constraints = get_object_root(XML_CIB_TAG_CONSTRAINTS,
                                                   data_set->input);

        unpack_constraints(cib_constraints, data_set);

        // Constraints apply to group/clone, not member/instance
        rsc = uber_parent(rsc);

        for (lpc = data_set->resources; lpc != NULL; lpc = lpc->next) {
            resource_t *r = (resource_t *) lpc->data;

            clear_bit(r->flags, pe_rsc_allocating);
        }

        cli_resource_print_colocation(rsc, TRUE, rsc_cmd == 'A', 1);

        fprintf(stdout, "* %s\n", rsc->id);
        cli_resource_print_location(rsc, NULL);

        for (lpc = data_set->resources; lpc != NULL; lpc = lpc->next) {
            resource_t *r = (resource_t *) lpc->data;

            clear_bit(r->flags, pe_rsc_allocating);
        }

        cli_resource_print_colocation(rsc, FALSE, rsc_cmd == 'A', 1);

    } else if (rsc_cmd == 'c') {
        GListPtr lpc = NULL;

        rc = pcmk_ok;
        for (lpc = data_set->resources; lpc != NULL; lpc = lpc->next) {
            rsc = (resource_t *) lpc->data;
            cli_resource_print_cts(rsc);
        }
        cli_resource_print_cts_constraints(data_set);

    } else if (rsc_cmd == 'F') {
        rc = cli_resource_fail(crmd_channel, host_uname, rsc_id, data_set);
        if (rc == pcmk_ok) {
            start_mainloop();
        }

    } else if (rsc_cmd == 'O') {
        rc = cli_resource_print_operations(rsc_id, host_uname, TRUE, data_set);

    } else if (rsc_cmd == 'o') {
        rc = cli_resource_print_operations(rsc_id, host_uname, FALSE, data_set);

    } else if (rsc_cmd == 'W') {
        rc = cli_resource_search(rsc, rsc_id, data_set);
        if (rc >= 0) {
            rc = pcmk_ok;
        }

    } else if (rsc_cmd == 'q') {
        rc = cli_resource_print(rsc, data_set, TRUE);

    } else if (rsc_cmd == 'w') {
        rc = cli_resource_print(rsc, data_set, FALSE);

    } else if (rsc_cmd == 'Y') {
        node_t *dest = NULL;

        if (host_uname) {
            dest = pe_find_node(data_set->nodes, host_uname);
            if (dest == NULL) {
                rc = -pcmk_err_node_unknown;
                goto bail;
            }
        }
        cli_resource_why(cib_conn, data_set->resources, rsc, dest);
        rc = pcmk_ok;

    } else if (rsc_cmd == 'U') {
        GListPtr before = NULL;
        GListPtr after = NULL;
        GListPtr remaining = NULL;
        GListPtr ele = NULL;
        node_t *dest = NULL;

        if (BE_QUIET == FALSE) {
            before = build_constraint_list(data_set->input);
        }

        if (clear_expired == TRUE) {
            rc = cli_resource_clear_all_expired(data_set->input, cib_conn, rsc_id, host_uname, scope_master);

        } else if (host_uname) {
            dest = pe_find_node(data_set->nodes, host_uname);
            if (dest == NULL) {
                rc = -pcmk_err_node_unknown;
                if (BE_QUIET == FALSE) {
                    g_list_free(before);
                }
                goto bail;
            }
            rc = cli_resource_clear(rsc_id, dest->details->uname, NULL, cib_conn, TRUE);

        } else {
            rc = cli_resource_clear(rsc_id, NULL, data_set->nodes, cib_conn, TRUE);
        }

        if (BE_QUIET == FALSE) {
            rc = cib_conn->cmds->query(cib_conn, NULL, &cib_xml_copy, cib_scope_local | cib_sync_call);
            if (rc != pcmk_ok) {
                CMD_ERR("Could not get modified CIB: %s\n", pcmk_strerror(rc));
                g_list_free(before);
                goto bail;
            }

            data_set->input = cib_xml_copy;
            cluster_status(data_set);

            after = build_constraint_list(data_set->input);
            remaining = subtract_lists(before, after, (GCompareFunc) strcmp);

            for (ele = remaining; ele != NULL; ele = ele->next) {
                printf("Removing constraint: %s\n", (char *) ele->data);
            }

            g_list_free(before);
            g_list_free(after);
            g_list_free(remaining);
        }

    } else if (rsc_cmd == 'M' && host_uname) {
        rc = cli_resource_move(rsc, rsc_id, host_uname, cib_conn, data_set);

    } else if (rsc_cmd == 'B' && host_uname) {
        node_t *dest = pe_find_node(data_set->nodes, host_uname);

        if (dest == NULL) {
            rc = -pcmk_err_node_unknown;
            goto bail;
        }
        rc = cli_resource_ban(rsc_id, dest->details->uname, NULL, cib_conn);

    } else if (rsc_cmd == 'B' || rsc_cmd == 'M') {
        pe_node_t *current = NULL;
        unsigned int nactive = 0;

        current = pe__find_active_requires(rsc, &nactive);

        if (nactive == 1) {
            rc = cli_resource_ban(rsc_id, current->details->uname, NULL, cib_conn);

        } else if (is_set(rsc->flags, pe_rsc_promotable)) {
            int count = 0;
            GListPtr iter = NULL;

            current = NULL;
            for(iter = rsc->children; iter; iter = iter->next) {
                resource_t *child = (resource_t *)iter->data;
                enum rsc_role_e child_role = child->fns->state(child, TRUE);

                if(child_role == RSC_ROLE_MASTER) {
                    count++;
                    current = pe__current_node(child);
                }
            }

            if(count == 1 && current) {
                rc = cli_resource_ban(rsc_id, current->details->uname, NULL, cib_conn);

            } else {
                rc = -EINVAL;
                exit_code = CRM_EX_USAGE;
                CMD_ERR("Resource '%s' not moved: active in %d locations (promoted in %d).",
                        rsc_id, nactive, count);
                CMD_ERR("To prevent '%s' from running on a specific location, "
                        "specify a node.", rsc_id);
                CMD_ERR("To prevent '%s' from being promoted at a specific "
                        "location, specify a node and the master option.",
                        rsc_id);
            }

        } else {
            rc = -EINVAL;
            exit_code = CRM_EX_USAGE;
            CMD_ERR("Resource '%s' not moved: active in %d locations.", rsc_id, nactive);
            CMD_ERR("To prevent '%s' from running on a specific location, "
                    "specify a node.", rsc_id);
        }

    } else if (rsc_cmd == 'G') {
        rc = cli_resource_print_property(rsc, prop_name, data_set);

    } else if (rsc_cmd == 'S') {
        xmlNode *msg_data = NULL;

        if ((rsc_type == NULL) || !strlen(rsc_type)) {
            CMD_ERR("Must specify -t with resource type");
            rc = -ENXIO;
            goto bail;

        } else if ((prop_value == NULL) || !strlen(prop_value)) {
            CMD_ERR("Must supply -v with new value");
            rc = -EINVAL;
            goto bail;
        }

        CRM_LOG_ASSERT(prop_name != NULL);

        msg_data = create_xml_node(NULL, rsc_type);
        crm_xml_add(msg_data, XML_ATTR_ID, rsc_id);
        crm_xml_add(msg_data, prop_name, prop_value);

        rc = cib_conn->cmds->modify(cib_conn, XML_CIB_TAG_RESOURCES, msg_data, cib_options);
        free_xml(msg_data);

    } else if (rsc_cmd == 'g') {
        rc = cli_resource_print_attribute(rsc, prop_name, data_set);

    } else if (rsc_cmd == 'p') {
        if (prop_value == NULL || strlen(prop_value) == 0) {
            CMD_ERR("You need to supply a value with the -v option");
            rc = -EINVAL;
            goto bail;
        }

        /* coverity[var_deref_model] False positive */
        rc = cli_resource_update_attribute(rsc, rsc_id, prop_set, prop_id,
                                           prop_name, prop_value, recursive,
                                           cib_conn, data_set);

    } else if (rsc_cmd == 'd') {
        /* coverity[var_deref_model] False positive */
        rc = cli_resource_delete_attribute(rsc, rsc_id, prop_set, prop_id,
                                           prop_name, cib_conn, data_set);

    } else if ((rsc_cmd == 'C') && rsc) {
        if (do_force == FALSE) {
            rsc = uber_parent(rsc);
        }
        crmd_replies_needed = 0;

        crm_debug("Erasing failures of %s (%s requested) on %s",
                  rsc->id, rsc_id, (host_uname? host_uname: "all nodes"));
        rc = cli_resource_delete(crmd_channel, host_uname, rsc,
                                 operation, interval_spec, TRUE, data_set);

        if ((rc == pcmk_ok) && !BE_QUIET) {
            // Show any reasons why resource might stay stopped
            cli_resource_check(cib_conn, rsc);
        }

        if (rc == pcmk_ok) {
            start_mainloop();
        }

    } else if (rsc_cmd == 'C') {
        rc = cli_cleanup_all(crmd_channel, host_uname, operation, interval_spec,
                             data_set);
        if (rc == pcmk_ok) {
            start_mainloop();
        }

    } else if ((rsc_cmd == 'R') && rsc) {
        if (do_force == FALSE) {
            rsc = uber_parent(rsc);
        }
        crmd_replies_needed = 0;

        crm_debug("Re-checking the state of %s (%s requested) on %s",
                  rsc->id, rsc_id, (host_uname? host_uname: "all nodes"));
        rc = cli_resource_delete(crmd_channel, host_uname, rsc,
                                 NULL, 0, FALSE, data_set);

        if ((rc == pcmk_ok) && !BE_QUIET) {
            // Show any reasons why resource might stay stopped
            cli_resource_check(cib_conn, rsc);
        }

        if (rc == pcmk_ok) {
            start_mainloop();
        }

    } else if (rsc_cmd == 'R') {
        const char *router_node = host_uname;
        xmlNode *msg_data = NULL;
        xmlNode *cmd = NULL;
        int attr_options = attrd_opt_none;

        if (host_uname) {
            node_t *node = pe_find_node(data_set->nodes, host_uname);

            if (node && is_remote_node(node)) {
                node = pe__current_node(node->details->remote_rsc);
                if (node == NULL) {
                    CMD_ERR("No cluster connection to Pacemaker Remote node %s detected",
                            host_uname);
                    rc = -ENXIO;
                    goto bail;
                }
                router_node = node->details->uname;
                attr_options |= attrd_opt_remote;
            }
        }

        if (crmd_channel == NULL) {
            printf("Dry run: skipping clean-up of %s due to CIB_file\n",
                   host_uname? host_uname : "all nodes");
            rc = pcmk_ok;
            goto bail;
        }

        msg_data = create_xml_node(NULL, "crm-resource-reprobe-op");
        crm_xml_add(msg_data, XML_LRM_ATTR_TARGET, host_uname);
        if (safe_str_neq(router_node, host_uname)) {
            crm_xml_add(msg_data, XML_LRM_ATTR_ROUTER_NODE, router_node);
        }

        cmd = create_request(CRM_OP_REPROBE, msg_data, router_node,
                             CRM_SYSTEM_CRMD, crm_system_name, our_pid);
        free_xml(msg_data);

        crm_debug("Re-checking the state of all resources on %s", host_uname?host_uname:"all nodes");

        rc = attrd_clear_delegate(NULL, host_uname, NULL, NULL, NULL, NULL,
                                  attr_options);

        if (crm_ipc_send(crmd_channel, cmd, 0, 0, NULL) > 0) {
            start_mainloop();
        }

        free_xml(cmd);

    } else if (rsc_cmd == 'D') {
        xmlNode *msg_data = NULL;

        if (rsc_type == NULL) {
            CMD_ERR("You need to specify a resource type with -t");
            rc = -ENXIO;
            goto bail;
        }

        msg_data = create_xml_node(NULL, rsc_type);
        crm_xml_add(msg_data, XML_ATTR_ID, rsc_id);

        rc = cib_conn->cmds->remove(cib_conn, XML_CIB_TAG_RESOURCES, msg_data, cib_options);
        free_xml(msg_data);

    } else {
        CMD_ERR("Unknown command: %c", rsc_cmd);
    }

  bail:

    free(our_pid);
    pe_free_working_set(data_set);
    if (cib_conn != NULL) {
        cib_conn->cmds->signoff(cib_conn);
        cib_delete(cib_conn);
    }

    if (is_ocf_rc) {
        exit_code = rc;

    } else if (rc != pcmk_ok) {
        CMD_ERR("Error performing operation: %s", pcmk_strerror(rc));
        if (rc == -pcmk_err_no_quorum) {
            CMD_ERR("To ignore quorum, use the force option");
        }
        if (exit_code == CRM_EX_OK) {
            exit_code = crm_errno2exit(rc);
        }
    }

    return crm_exit(exit_code);
}
Beispiel #15
0
static enum cmd_status cmd_mqtt_publish_exec(char *cmd)
{
	int32_t cnt;
	MQTTMessage message;
	char *topic;
	uint8_t *buf;
	char *tmp;
	uint32_t qos;
	uint32_t retain;
	uint32_t size;
	int rc;

	/* get param */
	cnt = cmd_sscanf(cmd, "qos=%u retain=%u",
			 &qos, &retain);

	if ((tmp = cmd_strrchr(cmd, '\"')) == NULL)
		return CMD_STATUS_INVALID_ARG;
	*tmp = '\0';
  	cnt += cmd_sscanf(tmp + 2, "size=%u", &size);
	if ((tmp = cmd_strchr(cmd, '\"')) == NULL)
		return CMD_STATUS_INVALID_ARG;
	tmp++;


	/* check param */
	if (cnt != 3) {
		CMD_ERR("invalid param number %d\n", cnt);
		return CMD_STATUS_INVALID_ARG;
	}

	if (qos > 2) {
		CMD_ERR("invalid qos %d\n", qos);
		return CMD_STATUS_INVALID_ARG;
	}

	if (retain > 1) {
		CMD_ERR("invalid retain %d\n", retain);
		return CMD_STATUS_INVALID_ARG;
	}

	topic = tmp;
	CMD_DBG("topic name = %s\n", topic);

	buf = cmd_malloc(size);
	if (buf == NULL) {
		CMD_ERR("no memory\n");
		return CMD_STATUS_FAIL;
	}


	cmd_write_respond(CMD_STATUS_OK, "OK");
	cmd_raw_mode_enable();
	cmd_raw_mode_read(buf, size, 30000);

	message.qos = qos;
	message.retained = retain;
	message.payload = buf;
	message.payloadlen = size;

	if (OS_MutexLock(&lock, 60000) == OS_E_TIMEOUT)
		return CMD_STATUS_FAIL;

	if ((rc = MQTTPublish(&client, topic, &message)) != 0)
		CMD_ERR("Return code from MQTT publish is %d\n", rc);
	else
		CMD_DBG("MQTT publish is success\n");

	OS_MutexUnlock(&lock);

	cmd_raw_mode_write((uint8_t *)&rc, sizeof(rc));
	cmd_raw_mode_disable();

	cmd_free(buf);

	return CMD_STATUS_ACKED;

}
Beispiel #16
0
/* @return
 *   -2: CMD_STATUS_INVALID_ARG
 *   -1: CMD_STATUS_FAIL
 *    0: CMD_STATUS_OK
 */
static int cmd_wlan_sta_set(char *cmd)
{
	char *value;
	wlan_sta_config_t config;

	value = cmd_strchr(cmd, ' ');
	if (value == NULL)
		return -2;
	*value++ = '\0';

	config.field = WLAN_STA_FIELD_NUM;

	if (cmd_strcmp(cmd, "ssid") == 0) {
		uint8_t ssid_len = cmd_strlen(value);
		if ((ssid_len >= 1) && (ssid_len <= 32)) {
			config.field = WLAN_STA_FIELD_SSID;
			cmd_memcpy(config.u.ssid.ssid, value, ssid_len);
			config.u.ssid.ssid_len = ssid_len;
		}
	} else if (cmd_strcmp(cmd, "psk") == 0) {
		config.field = WLAN_STA_FIELD_PSK;
		cmd_strlcpy((char *)config.u.psk, value, sizeof(config.u.psk));
	} else if (cmd_strcmp(cmd, "wep_key0") == 0) {
		config.field = WLAN_STA_FIELD_WEP_KEY0;
		cmd_strlcpy((char *)config.u.wep_key, value, sizeof(config.u.wep_key));
	} else if (cmd_strcmp(cmd, "wep_key1") == 0) {
		config.field = WLAN_STA_FIELD_WEP_KEY1;
		cmd_strlcpy((char *)config.u.wep_key, value, sizeof(config.u.wep_key));
	} else if (cmd_strcmp(cmd, "wep_key2") == 0) {
		config.field = WLAN_STA_FIELD_WEP_KEY2;
		cmd_strlcpy((char *)config.u.wep_key, value, sizeof(config.u.wep_key));
	} else if (cmd_strcmp(cmd, "wep_key3") == 0) {
		config.field = WLAN_STA_FIELD_WEP_KEY3;
		cmd_strlcpy((char *)config.u.wep_key, value, sizeof(config.u.wep_key));
	} else if (cmd_strcmp(cmd, "wep_key_index") == 0) {
		int index;
		if (cmd_wpas_parse_int(value, 0, 3, &index) == 0) {
			config.field = WLAN_STA_FIELD_WEP_KEY_INDEX;
			config.u.wep_tx_keyidx = index;
		}
	} else if (cmd_strcmp(cmd, "key_mgmt") == 0) {
		int key_mgmt = cmd_wpas_parse_key_mgmt(value);
		if (key_mgmt > 0) {
			config.field = WLAN_STA_FIELD_KEY_MGMT;
			config.u.key_mgmt = key_mgmt;
		}
	} else if (cmd_strcmp(cmd, "pairwise") == 0) {
		int pairwise_cipher = cmd_wpas_parse_cipher(value);
		if (pairwise_cipher > 0) {
			config.field = WLAN_STA_FIELD_PAIRWISE_CIPHER;
			config.u.pairwise_cipher = pairwise_cipher;
		}
	} else if (cmd_strcmp(cmd, "group") == 0) {
		int group_cipher = cmd_wpas_parse_cipher(value);
		if (group_cipher > 0) {
			config.field = WLAN_STA_FIELD_GROUP_CIPHER;
			config.u.group_cipher = group_cipher;
		}
	} else if (cmd_strcmp(cmd, "proto") == 0) {
		int proto = cmd_wpas_parse_proto(value);
		if (proto >= 0) {
			config.field = WLAN_STA_FIELD_PROTO;
			config.u.proto = proto;
		}
	} else if (cmd_strcmp(cmd, "auth_alg") == 0) {
		int auth_alg = cmd_wpas_parse_auth_alg(value);
		if (auth_alg > 0) {
			config.field = WLAN_STA_FIELD_AUTH_ALG;
			config.u.auth_alg = auth_alg;
		}
	} else if (cmd_strcmp(cmd, "ptk_rekey") == 0) {
		int sec;
		if (cmd_wpas_parse_int(value, 0, INT32_MAX, &sec) == 0) {
			config.field = WLAN_STA_FIELD_WPA_PTK_REKEY;
			config.u.wpa_ptk_rekey = sec;
		}
	} else if (cmd_strcmp(cmd, "scan_ssid") == 0) {
		int enable;
		if (cmd_wpas_parse_int(value, 0, 1, &enable) == 0) {
			config.field = WLAN_STA_FIELD_SCAN_SSID;
			config.u.scan_ssid = enable;
		}
	}

	if (config.field < WLAN_STA_FIELD_NUM)
		return wlan_sta_set_config(&config);

	CMD_ERR("%s: invalid arg '%s %s'\n", __func__, cmd, value);
	return -2;
}
Beispiel #17
0
static enum cmd_status cmd_mqtt_will_exec(char *cmd)
{
	int32_t cnt;
	char *topic;
	uint8_t *buf;
	char *tmp;
	uint32_t qos;
	uint32_t retain;
	uint32_t size;

	/* get param */
	cnt = cmd_sscanf(cmd, "qos=%u retain=%u",
			 &qos, &retain);

	if ((tmp = cmd_strrchr(cmd, '\"')) == NULL)
		return CMD_STATUS_INVALID_ARG;
	*tmp = '\0';
	cnt += cmd_sscanf(tmp + 2, "size=%u", &size);
	if ((tmp = cmd_strchr(cmd, '\"')) == NULL)
		return CMD_STATUS_INVALID_ARG;
	tmp++;

	/* check param */
	if (cnt != 3) {
		CMD_ERR("invalid param number %d\n", cnt);
		return CMD_STATUS_INVALID_ARG;
	}

	if (qos > 2) {
		CMD_ERR("invalid qos %d\n", qos);
		return CMD_STATUS_INVALID_ARG;
	}

	if (retain > 1) {
		CMD_ERR("invalid retain %d\n", retain);
		return CMD_STATUS_INVALID_ARG;
	}

	topic = tmp;
	CMD_DBG("topic name = %s\n", topic);

	buf = cmd_malloc(size);
	if (buf == NULL) {
		CMD_ERR("no memory\n");
		return CMD_STATUS_FAIL;
	}


	cmd_write_respond(CMD_STATUS_OK, "OK");
	cmd_raw_mode_enable();
	cmd_raw_mode_read(buf, size, 30000);
	cmd_raw_mode_disable();

	//will function
	connectData.willFlag = 1;
	connectData.will.message.lenstring.data = (char *)buf;
	connectData.will.message.lenstring.len = size;
	connectData.will.qos = qos;
	connectData.will.retained = retain;
	connectData.will.topicName.cstring = topic;

	cmd_free(buf);

	return CMD_STATUS_ACKED;
}
Beispiel #18
0
/* @return
 *   -2: CMD_STATUS_INVALID_ARG
 *   -1: CMD_STATUS_FAIL
 *    0: CMD_STATUS_OK
 */
static int cmd_wlan_sta_get(char *cmd)
{
	wlan_sta_config_t config;
	cmd_memset(&config, 0, sizeof(config));

	if (cmd_strcmp(cmd, "ssid") == 0) {
		config.field = WLAN_STA_FIELD_SSID;
	} else if (cmd_strcmp(cmd, "psk") == 0) {
		config.field = WLAN_STA_FIELD_PSK;
	} else if (cmd_strcmp(cmd, "wep_key0") == 0) {
		config.field = WLAN_STA_FIELD_WEP_KEY0;
	} else if (cmd_strcmp(cmd, "wep_key1") == 0) {
		config.field = WLAN_STA_FIELD_WEP_KEY1;
	} else if (cmd_strcmp(cmd, "wep_key2") == 0) {
		config.field = WLAN_STA_FIELD_WEP_KEY2;
	} else if (cmd_strcmp(cmd, "wep_key3") == 0) {
		config.field = WLAN_STA_FIELD_WEP_KEY3;
	} else if (cmd_strcmp(cmd, "wep_key_index") == 0) {
		config.field = WLAN_STA_FIELD_WEP_KEY_INDEX;
	} else if (cmd_strcmp(cmd, "key_mgmt") == 0) {
		config.field = WLAN_STA_FIELD_KEY_MGMT;
	} else if (cmd_strcmp(cmd, "pairwise") == 0) {
		config.field = WLAN_STA_FIELD_PAIRWISE_CIPHER;
	} else if (cmd_strcmp(cmd, "group") == 0) {
		config.field = WLAN_STA_FIELD_GROUP_CIPHER;
	} else if (cmd_strcmp(cmd, "proto") == 0) {
		config.field = WLAN_STA_FIELD_PROTO;
	} else if (cmd_strcmp(cmd, "auth_alg") == 0) {
		config.field = WLAN_STA_FIELD_AUTH_ALG;
	} else if (cmd_strcmp(cmd, "ptk_rekey") == 0) {
		config.field = WLAN_STA_FIELD_WPA_PTK_REKEY;
	} else if (cmd_strcmp(cmd, "scan_ssid") == 0) {
		config.field = WLAN_STA_FIELD_SCAN_SSID;
	} else {
		CMD_ERR("%s: invalid arg '%s'\n", __func__, cmd);
		return -2;
	}

	if (wlan_sta_get_config(&config) != 0) {
		CMD_ERR("%s: get config failed\n", __func__);
		return -1;
	}

	if (config.field == WLAN_STA_FIELD_SSID) {
		CMD_LOG(1, "ssid: %.32s\n", config.u.ssid.ssid);
	} else if (config.field == WLAN_STA_FIELD_PSK) {
		CMD_LOG(1, "psk: %s\n", config.u.psk);
	} else if (config.field == WLAN_STA_FIELD_WEP_KEY0) {
		CMD_LOG(1, "wep_key0: %s\n", config.u.wep_key);
	} else if (config.field == WLAN_STA_FIELD_WEP_KEY1) {
		CMD_LOG(1, "wep_key1: %s\n", config.u.wep_key);
	} else if (config.field == WLAN_STA_FIELD_WEP_KEY2) {
		CMD_LOG(1, "wep_key2: %s\n", config.u.wep_key);
	} else if (config.field == WLAN_STA_FIELD_WEP_KEY3) {
		CMD_LOG(1, "wep_key3: %s\n", config.u.wep_key);
	} else if (config.field == WLAN_STA_FIELD_WEP_KEY_INDEX) {
		CMD_LOG(1, "wep_key_index: %d\n", config.u.wep_tx_keyidx);
	} else if (config.field == WLAN_STA_FIELD_KEY_MGMT) {
		CMD_LOG(1, "key_mgmt: %#06x\n", config.u.key_mgmt);
	} else if (config.field == WLAN_STA_FIELD_PAIRWISE_CIPHER) {
		CMD_LOG(1, "pairwise_cipher: %#06x\n", config.u.pairwise_cipher);
	} else if (config.field == WLAN_STA_FIELD_GROUP_CIPHER) {
		CMD_LOG(1, "group_cipher: %#06x\n", config.u.group_cipher);
	} else if (config.field == WLAN_STA_FIELD_PROTO) {
		CMD_LOG(1, "proto: %#06x\n", config.u.proto);
	} else if (config.field == WLAN_STA_FIELD_AUTH_ALG) {
		CMD_LOG(1, "auth_alg: %#06x\n", config.u.auth_alg);
	} else if (config.field == WLAN_STA_FIELD_WPA_PTK_REKEY) {
		CMD_LOG(1, "ptk_rekey: %d\n", config.u.wpa_ptk_rekey);
	} else if (config.field == WLAN_STA_FIELD_SCAN_SSID) {
		CMD_LOG(1, "scan_ssid: %d\n", config.u.scan_ssid);
	}

	return 0;
}
Beispiel #19
0
enum cmd_status cmd_wlan_sta_exec(char *cmd)
{
	int ret;

	cmd_write_respond(CMD_STATUS_OK, "OK");

	if (cmd_strncmp(cmd, "config ", 7) == 0) {
		char *argv[2];
		if (cmd_parse_argv(cmd + 7, argv, cmd_nitems(argv)) == 0) {
			ret = -2;
			goto out;
		}
		ret = wlan_sta_set((uint8_t *)argv[0], cmd_strlen(argv[0]), (uint8_t *)argv[1]);
	} else if (cmd_strncmp(cmd, "set ", 4) == 0) {
		ret = cmd_wlan_sta_set(cmd + 4);
	} else if (cmd_strncmp(cmd, "get ", 4) == 0) {
		ret = cmd_wlan_sta_get(cmd + 4);
	} else if (cmd_strcmp(cmd, "enable") == 0) {
		ret = wlan_sta_enable();
	} else if (cmd_strcmp(cmd, "disable") == 0) {
		ret = wlan_sta_disable();
	} else if (cmd_strcmp(cmd, "scan once") == 0) {
		ret = wlan_sta_scan_once();
	} else if (cmd_strncmp(cmd, "scan result ", 12) == 0) {
		int size;
		if (cmd_wpas_parse_int(cmd + 12, 1, 30, &size) != 0) {
			ret = -2;
			goto out;
		}
		wlan_sta_scan_results_t results;
		results.ap = cmd_malloc(size * sizeof(wlan_sta_ap_t));
		if (results.ap == NULL) {
			CMD_ERR("%s: malloc failed\n", __func__);
			ret = -1;
			goto out;
		}
		results.size = size;
		ret = wlan_sta_scan_result(&results);
		if (ret == 0)
			cmd_wlan_sta_print_scan_results(&results);
		cmd_free(results.ap);
	} else if (cmd_strncmp(cmd, "scan interval ", 14) == 0) {
		int sec;
		if (cmd_wpas_parse_int(cmd + 14, 0, INT32_MAX, &sec) != 0) {
			ret = -2;
			goto out;
		}
		ret = wlan_sta_scan_interval(sec);
	} else if (cmd_strncmp(cmd, "bss max count ", 14) == 0) {
		int count;
		if (cmd_wpas_parse_int(cmd + 14, 0, UINT8_MAX, &count) != 0) {
			ret = -2;
			goto out;
		}
		ret = wlan_sta_bss_max_count((uint8_t)count);
	} else if (cmd_strncmp(cmd, "bss flush ", 10) == 0) {
		int age;
		if (cmd_wpas_parse_int(cmd + 10, 0, INT32_MAX, &age) != 0) {
			ret = -2;
			goto out;
		}
		ret = wlan_sta_bss_flush(age);
	} else if (cmd_strcmp(cmd, "connect") == 0) {
		ret = wlan_sta_connect();
	} else if (cmd_strcmp(cmd, "disconnect") == 0) {
		ret = wlan_sta_disconnect();
	} else if (cmd_strcmp(cmd, "state") == 0) {
		wlan_sta_states_t state;
		ret = wlan_sta_state(&state);
		if (ret == 0)
			CMD_LOG(1, "sta state: %d\n", state);
	} else if (cmd_strcmp(cmd, "ap") == 0) {
		wlan_sta_ap_t *ap = cmd_malloc(sizeof(wlan_sta_ap_t));
		if (ap == NULL) {
			CMD_ERR("%s: malloc failed\n", __func__);
			ret = -1;
			goto out;
		}
		ret = wlan_sta_ap_info(ap);
		if (ret == 0)
			cmd_wlan_sta_print_ap(ap);
		cmd_free(ap);
	} else if (cmd_strcmp(cmd, "wps pbc") == 0) {
		ret = wlan_sta_wps_pbc();
	} else if ((cmd_strlen(cmd) == 7) || (cmd_strcmp(cmd, "wps pin") == 0)) {
		wlan_sta_wps_pin_t wps;
		ret = wlan_sta_wps_pin_get(&wps);
		if (ret == 0)
			CMD_LOG(1, "WPS pin: %s\n", wps.pin);
	} else if (cmd_strncmp(cmd, "wps pin ", 8) == 0) {
		if (cmd_strlen(cmd + 8) != 8) {
			ret = -2;
			goto out;
		}
		wlan_sta_wps_pin_t wps;
		cmd_memcpy(wps.pin, cmd + 8, 8);
		wps.pin[8] = '\0';
		ret = wlan_sta_wps_pin_set(&wps);
	} else {
		CMD_ERR("%s: unknown command '%s'\n", __func__, cmd);
		return CMD_STATUS_ACKED;
	}

out:
	if (ret == -2) {
		CMD_ERR("%s: command '%s' invalid arg\n", __func__, cmd);
		return CMD_STATUS_ACKED;
	} else if (ret == -1) {
		CMD_ERR("%s: command '%s' exec failed\n", __func__, cmd);
		return CMD_STATUS_ACKED;
	}

	return CMD_STATUS_ACKED;
}
Beispiel #20
0
enum cmd_status cmd_wlan_ap_exec(char *cmd)
{
	int ret;

	cmd_write_respond(CMD_STATUS_OK, "OK");

	if (cmd_strncmp(cmd, "config ", 7) == 0) {
		char *argv[2];
		if (cmd_parse_argv(cmd + 7, argv, cmd_nitems(argv)) == 0) {
			ret = -2;
			goto out;
		}
		ret = wlan_ap_set((uint8_t *)argv[0], cmd_strlen(argv[0]), (uint8_t *)argv[1]);
	} else if (cmd_strncmp(cmd, "set ", 4) == 0) {
		ret = cmd_wlan_ap_set(cmd + 4);
	} else if (cmd_strncmp(cmd, "get ", 4) == 0) {
		ret = cmd_wlan_ap_get(cmd + 4);
	} else if (cmd_strcmp(cmd, "enable") == 0) {
		ret = wlan_ap_enable();
	} else if (cmd_strcmp(cmd, "reload") == 0) {
		ret = wlan_ap_reload();
	} else if (cmd_strcmp(cmd, "disable") == 0) {
		ret = wlan_ap_disable();
	} else if (cmd_strcmp(cmd, "sta num") == 0) {
		int num;
		ret = wlan_ap_sta_num(&num);
		if (ret == 0)
			CMD_LOG(1, "sta num: %d\n", num);
	} else if (cmd_strncmp(cmd, "sta info ", 9) == 0) {
		int size;
		if (cmd_wpas_parse_int(cmd + 9, 1, 30, &size) != 0) {
			ret = -2;
			goto out;
		}
		wlan_ap_stas_t stas;
		stas.sta = (wlan_ap_sta_t *)cmd_malloc(size * sizeof(wlan_ap_sta_t));
		if (stas.sta == NULL) {
			CMD_ERR("%s: malloc failed\n", __func__);
			ret = -1;
			goto out;
		}
		stas.size = size;
		ret = wlan_ap_sta_info(&stas);
		if (ret == 0)
			cmd_wlan_ap_print_sta_info(&stas);
		cmd_free(stas.sta);
	} else {
		CMD_ERR("%s: unknown command '%s'\n", __func__, cmd);
		return CMD_STATUS_ACKED;
	}

out:
	if (ret == -2) {
		CMD_ERR("%s: command '%s' invalid arg\n", __func__, cmd);
		return CMD_STATUS_ACKED;
	} else if (ret == -1) {
		CMD_ERR("%s: command '%s' exec failed\n", __func__, cmd);
		return CMD_STATUS_ACKED;
	}

	return CMD_STATUS_ACKED;
}
Beispiel #21
0
/* @return
 *   -2: CMD_STATUS_INVALID_ARG
 *   -1: CMD_STATUS_FAIL
 *    0: CMD_STATUS_OK
 */
static int cmd_wlan_ap_get(char *cmd)
{
	wlan_ap_config_t config;
	cmd_memset(&config, 0, sizeof(config));

	if (cmd_strcmp(cmd, "ssid") == 0) {
		config.field = WLAN_AP_FIELD_SSID;
	} else if (cmd_strcmp(cmd, "psk") == 0) {
		config.field = WLAN_AP_FIELD_PSK;
	} else if (cmd_strcmp(cmd, "key_mgmt") == 0) {
		config.field = WLAN_AP_FIELD_KEY_MGMT;
	} else if (cmd_strcmp(cmd, "wpa") == 0) {
		config.field = WLAN_AP_FIELD_WPA_CIPHER;
	} else if (cmd_strcmp(cmd, "rsn") == 0) {
		config.field = WLAN_AP_FIELD_RSN_CIPHER;
	} else if (cmd_strcmp(cmd, "proto") == 0) {
		config.field = WLAN_AP_FIELD_PROTO;
	} else if (cmd_strcmp(cmd, "auth_alg") == 0) {
		config.field = WLAN_AP_FIELD_AUTH_ALG;
	} else if (cmd_strcmp(cmd, "group_rekey") == 0) {
		config.field = WLAN_AP_FIELD_GROUP_REKEY;
	} else if (cmd_strcmp(cmd, "strict_rekey") == 0) {
		config.field = WLAN_AP_FIELD_STRICT_REKEY;
	} else if (cmd_strcmp(cmd, "gmk_rekey") == 0) {
		config.field = WLAN_AP_FIELD_GMK_REKEY;
	} else if (cmd_strcmp(cmd, "ptk_rekey") == 0) {
		config.field = WLAN_AP_FIELD_PTK_REKEY;
	} else if (cmd_strcmp(cmd, "hw_mode") == 0) {
		config.field = WLAN_AP_FIELD_HW_MODE;
	} else if (cmd_strcmp(cmd, "80211n") == 0) {
		config.field = WLAN_AP_FIELD_IEEE80211N;
	} else if (cmd_strcmp(cmd, "channel") == 0) {
		config.field = WLAN_AP_FIELD_CHANNEL;
	} else if (cmd_strcmp(cmd, "beacon_int") == 0) {
		config.field = WLAN_AP_FIELD_BEACON_INT;
	} else if (cmd_strcmp(cmd, "dtim") == 0) {
		config.field = WLAN_AP_FIELD_DTIM;
	} else if (cmd_strcmp(cmd, "max_num_sta") == 0) {
		config.field = WLAN_AP_FIELD_MAX_NUM_STA;
	} else {
		CMD_ERR("%s: invalid arg '%s'\n", __func__, cmd);
		return -2;
	}

	if (wlan_ap_get_config(&config) != 0) {
		CMD_ERR("%s: get config failed\n", __func__);
		return -1;
	}

	if (config.field == WLAN_AP_FIELD_SSID) {
		CMD_LOG(1, "ssid: %.32s\n", config.u.ssid.ssid);
	} else if (config.field == WLAN_AP_FIELD_PSK) {
		CMD_LOG(1, "psk: %s\n", config.u.psk);
	} else if (config.field == WLAN_AP_FIELD_KEY_MGMT) {
		CMD_LOG(1, "key_mgmt: %#06x\n", config.u.key_mgmt);
	} else if (config.field == WLAN_AP_FIELD_WPA_CIPHER) {
		CMD_LOG(1, "wpa_cipher: %#06x\n", config.u.wpa_cipher);
	} else if (config.field == WLAN_AP_FIELD_RSN_CIPHER) {
		CMD_LOG(1, "rsn_cipher: %#06x\n", config.u.rsn_cipher);
	} else if (config.field == WLAN_AP_FIELD_PROTO) {
		CMD_LOG(1, "proto: %#06x\n", config.u.proto);
	} else if (config.field == WLAN_AP_FIELD_AUTH_ALG) {
		CMD_LOG(1, "auth_alg: %#06x\n", config.u.auth_alg);
	} else if (config.field == WLAN_AP_FIELD_GROUP_REKEY) {
		CMD_LOG(1, "group_rekey: %d\n", config.u.group_rekey);
	} else if (config.field == WLAN_AP_FIELD_STRICT_REKEY) {
		CMD_LOG(1, "strict_rekey: %d\n", config.u.strict_rekey);
	} else if (config.field == WLAN_AP_FIELD_GMK_REKEY) {
		CMD_LOG(1, "gmk_rekey: %d\n", config.u.gmk_rekey);
	} else if (config.field == WLAN_AP_FIELD_PTK_REKEY) {
		CMD_LOG(1, "ptk_rekey: %d\n", config.u.ptk_rekey);
	} else if (config.field == WLAN_AP_FIELD_HW_MODE) {
		if (config.u.hw_mode == WLAN_AP_HW_MODE_IEEE80211B) {
			CMD_LOG(1, "hw_mode: b\n");
		} else if (config.u.hw_mode == WLAN_AP_HW_MODE_IEEE80211G) {
			CMD_LOG(1, "hw_mode: g\n");
		} else if (config.u.hw_mode == WLAN_AP_HW_MODE_IEEE80211A) {
			CMD_LOG(1, "hw_mode: a\n");
		} else if (config.u.hw_mode == WLAN_AP_HW_MODE_IEEE80211AD) {
			CMD_LOG(1, "hw_mode: ad\n");
		} else {
			CMD_ERR("%s: invalid hw_mode\n", __func__);
		}
	} else if (config.field == WLAN_AP_FIELD_IEEE80211N) {
		CMD_LOG(1, "ieee80211n: %d\n", config.u.ieee80211n);
	} else if (config.field == WLAN_AP_FIELD_CHANNEL) {
		CMD_LOG(1, "channel: %d\n", config.u.channel);
	} else if (config.field == WLAN_AP_FIELD_BEACON_INT) {
		CMD_LOG(1, "beacon_int: %d\n", config.u.beacon_int);
	} else if (config.field == WLAN_AP_FIELD_DTIM) {
		CMD_LOG(1, "dtim: %d\n", config.u.dtim);
	} else if (config.field == WLAN_AP_FIELD_MAX_NUM_STA) {
		CMD_LOG(1, "max_num_sta: %d\n", config.u.max_num_sta);
	}

	return 0;
}
Beispiel #22
0
/* @return
 *   -2: CMD_STATUS_INVALID_ARG
 *   -1: CMD_STATUS_FAIL
 *    0: CMD_STATUS_OK
 */
static int cmd_wlan_ap_set(char *cmd)
{
	char *value;
	wlan_ap_config_t config;

	value = cmd_strchr(cmd, ' ');
	if (value == NULL)
		return -2;
	*value++ = '\0';

	config.field = WLAN_AP_FIELD_NUM;

	if (cmd_strcmp(cmd, "ssid") == 0) {
		uint8_t ssid_len = cmd_strlen(value);
		if ((ssid_len >= 1) && (ssid_len <= 32)) {
			config.field = WLAN_STA_FIELD_SSID;
			cmd_memcpy(config.u.ssid.ssid, value, ssid_len);
			config.u.ssid.ssid_len = ssid_len;
		}
	} else if (cmd_strcmp(cmd, "psk") == 0) {
		config.field = WLAN_AP_FIELD_PSK;
		cmd_strlcpy((char *)config.u.psk, value, sizeof(config.u.psk));
	} else if (cmd_strcmp(cmd, "key_mgmt") == 0) {
		int key_mgmt = cmd_wpas_parse_key_mgmt(value);
		if (key_mgmt > 0) {
			config.field = WLAN_AP_FIELD_KEY_MGMT;
			config.u.key_mgmt = key_mgmt;
		}
	} else if (cmd_strcmp(cmd, "wpa") == 0) {
		int wpa_cipher = cmd_wpas_parse_cipher(value);
		if (wpa_cipher > 0) {
			config.field = WLAN_AP_FIELD_WPA_CIPHER;
			config.u.wpa_cipher = wpa_cipher;
		}
	} else if (cmd_strcmp(cmd, "rsn") == 0) {
		int rsn_cipher = cmd_wpas_parse_cipher(value);
		if (rsn_cipher > 0) {
			config.field = WLAN_AP_FIELD_RSN_CIPHER;
			config.u.rsn_cipher = rsn_cipher;
		}
	} else if (cmd_strcmp(cmd, "proto") == 0) {
		int proto = cmd_wpas_parse_proto(value);
		if (proto >= 0) {
			config.field = WLAN_AP_FIELD_PROTO;
			config.u.proto = proto;
		}
	} else if (cmd_strcmp(cmd, "auth_alg") == 0) {
		int auth_alg = cmd_wpas_parse_auth_alg(value);
		if (auth_alg > 0) {
			config.field = WLAN_AP_FIELD_AUTH_ALG;
			config.u.auth_alg = auth_alg;
		}
	} else if (cmd_strcmp(cmd, "group_rekey") == 0) {
		int group_rekey;
		if (cmd_wpas_parse_int(value, 0, INT32_MAX, &group_rekey) == 0) {
			config.field = WLAN_AP_FIELD_GROUP_REKEY;
			config.u.group_rekey = group_rekey;
		}
	} else if (cmd_strcmp(cmd, "strict_rekey") == 0) {
		int strict_rekey;
		if (cmd_wpas_parse_int(value, 0, 1, &strict_rekey) == 0) {
			config.field = WLAN_AP_FIELD_STRICT_REKEY;
			config.u.strict_rekey = strict_rekey;
		}
	} else if (cmd_strcmp(cmd, "gmk_rekey") == 0) {
		int gmk_rekey;
		if (cmd_wpas_parse_int(value, 0, INT32_MAX, &gmk_rekey) == 0) {
			config.field = WLAN_AP_FIELD_GMK_REKEY;
			config.u.gmk_rekey = gmk_rekey;
		}
	} else if (cmd_strcmp(cmd, "ptk_rekey") == 0) {
		int ptk_rekey;
		if (cmd_wpas_parse_int(value, 0, INT32_MAX, &ptk_rekey) == 0) {
			config.field = WLAN_AP_FIELD_PTK_REKEY;
			config.u.ptk_rekey = ptk_rekey;
		}
	} else if (cmd_strcmp(cmd, "hw_mode") == 0) {
		if ((value[0] == 'b') && (value[1] == '\0')) {
			config.field = WLAN_AP_FIELD_HW_MODE;
			config.u.hw_mode = WLAN_AP_HW_MODE_IEEE80211B;
		} else if ((value[0] == 'g') && (value[1] == '\0')) {
			config.field = WLAN_AP_FIELD_HW_MODE;
			config.u.hw_mode = WLAN_AP_HW_MODE_IEEE80211G;
		}
	} else if (cmd_strcmp(cmd, "80211n") == 0) {
		int ieee80211n;
		if (cmd_wpas_parse_int(value, 0, 1, &ieee80211n) == 0) {
			config.field = WLAN_AP_FIELD_IEEE80211N;
			config.u.ieee80211n = ieee80211n;
		}
	} else if (cmd_strcmp(cmd, "channel") == 0) {
		int channel;
		if (cmd_wpas_parse_int(value, 1, 14, &channel) == 0) {
			config.field = WLAN_AP_FIELD_CHANNEL;
			config.u.channel = channel;
		}
	} else if (cmd_strcmp(cmd, "beacon_int") == 0) {
		int beacon_int;
		if (cmd_wpas_parse_int(value, 15, 65535, &beacon_int) == 0) {
			config.field = WLAN_AP_FIELD_BEACON_INT;
			config.u.beacon_int = beacon_int;
		}
	} else if (cmd_strcmp(cmd, "dtim") == 0) {
		int dtim;
		if (cmd_wpas_parse_int(value, 1, 255, &dtim) == 0) {
			config.field = WLAN_AP_FIELD_DTIM;
			config.u.dtim = dtim;
		}
	} else if (cmd_strcmp(cmd, "max_num_sta") == 0) {
		int max_num_sta;
		if (cmd_wpas_parse_int(value, 0, INT32_MAX, &max_num_sta) == 0) {
			config.field = WLAN_AP_FIELD_MAX_NUM_STA;
			config.u.max_num_sta = max_num_sta;
		}
	}

	if (config.field < WLAN_AP_FIELD_NUM)
		return wlan_ap_set_config(&config);

	CMD_ERR("%s: invalid arg '%s %s'\n", __func__, cmd, value);
	return -2;
}