Example #1
0
int main(int argc, char **argv)
{
	int ret = EXIT_SUCCESS;
	int c;
	char *_in = NULL;
	char *_key = NULL;
	char *_out = NULL;
	FILE *in = NULL;
	FILE *out = NULL;
	char *key = NULL;
	long key_length;
 
	while((c = getopt(argc, argv, "i:k:o:")) != -1) {
		switch(c) {
		case 'i':
			_in = optarg;
			break;
		case 'k':
			_key = optarg;
			break;
		case 'o':
			_out = optarg;
			break;
		default:
			printf("Usage: xor -i in -k key -o out\n");
			return EXIT_FAILURE;
		}
	}
	if(_in == NULL || _key == NULL || _out == NULL) {
		printf("Usage: xor -i in -k key -o out\n");
		return EXIT_FAILURE;
	}
 
	if(read_key(_key, &key, &key_length) != 0) {
		printf("error reading %s\n", _key);
		return EXIT_FAILURE;
	}
 
	in = fopen(_in, "rb");
	if(in == NULL) {
		printf("error opening %s\n", _in);
		RET_GOTO(EXIT_FAILURE, free_key);
	}
 
	out = fopen(_out, "wb");
	if(out == NULL) {
		printf("error opening %s\n", _out);
		RET_GOTO(EXIT_FAILURE, close_in);
	}
	xor(in, out, key, key_length);
 
	fclose(out);
close_in:
	fclose(in);
free_key:
	free(key);
 
	return ret;
}
Example #2
0
int main(int argc, char **argv)
{
	int ret = EXIT_SUCCESS;
	FILE *in = NULL, *out = NULL;

	if(argc != 4) {
		printf("Usage: hc in out d|e");
		return EXIT_FAILURE;
	}
		
	in = fopen(argv[1], "rb");
	if(in == NULL) {
		printf("could not open file\n");
		return EXIT_FAILURE;
	}
	
	out = fopen(argv[2], "wb");
	if(out == NULL) {
		printf("could not open file\n");
		RET_GOTO(EXIT_FAILURE, close_in);
	}
	hitron_crypt(out, in, argv[3][0] == 'e');
	
	fclose(out);
close_in:
	fclose(in);
end:
	return ret;
}
Example #3
0
/*
--> {"uuid":"42E6E69DAEF6483FBBA412A28AF7CD76","attrSet":["wlanSwitchState"],"wlanSwitchState":{"value":"1","when":"1404443369"}}
*/
static int msdp_set_status(char *params)
{
    int ret = SERVICE_RESULT_OK;
    char *json_out = NULL;
    //log_trace("json_in = %s\n", json_in);

    //遍历attrset,设置属性值
    ret = msdp_set_device_status_handler(params, msdp_set_attr_each_cb,
                                         __set_attribute);
    RET_RETURN(ret, CALL_FUCTION_FAILED, "msdp_set_attr_each_cb");

    //遍历attrset,获取属性值
    ret = msdp_get_device_status_handler(params, msdp_get_attr_each_cb,
                                         __get_attribute, &json_out);
    RET_RETURN(ret, CALL_FUCTION_FAILED, "msdp_get_attr_each_cb");

    log_trace("json_out = %s", json_out ? (json_out) : "NULL");
    //todo: postDeviceData/deviceDataChange
    if (json_out) {
        int str_len, post_ret = SERVICE_RESULT_ERR;
        char *dev_id = json_get_value_by_name(params, strlen(params), JSON_KEY_DEVID,
                                              &str_len, NULL);
        if (dev_id != NULL) {
            char devid_str[18] = {0};
            strncpy(devid_str, dev_id, str_len);

            post_ret = lmns_post_device_data(devid_str, json_out);
            RET_GOTO(post_ret, end, CALL_FUCTION_FAILED, "lmns_post_device_data");
        } else {
            int post_ret = msdp_add_asyncpost_task(json_out);
            RET_GOTO(post_ret, end, "add async post task fail, params:%s", json_out);
            json_out = NULL;
        }
    }
end:
    if (json_out) {
        msdp_free_buff(json_out);
    }

    return ret;
}
Example #4
0
/*
--> {"uuid":"42E6E69DAEF6483FBBA412A28AF7CD76","service\":"startBwCheck","args":{}}
<-- {"uuid": "42E6E69DAEF6483FBBA412A28AF7CD76", "service": "stopBwCheck", "code": "1", "result": [ ] }
*/
static int msdp_rpc(char *params)
{
    int ret = SERVICE_RESULT_ERR;
    char *json_ptr, *rpc_name, *rpc_args;
    json_ptr = rpc_name = rpc_args = NULL;
    int len = 0;

    log_trace("params = %s", params);

    json_ptr = json_get_value_by_name(params, strlen(params), JSON_KEY_SERVICE,
                                      &len, NULL);
    PTR_GOTO(json_ptr, end, CALL_FUCTION_FAILED, "json_get_value_by_name");
    rpc_name = msdp_dup_buff(json_ptr, len + 1);
    PTR_GOTO(rpc_name, end, "pmalloc fail");
    rpc_name[len] = '\0';

    json_ptr = json_get_value_by_name(params, strlen(params), JSON_KEY_ARGS, &len,
                                      NULL);
    PTR_GOTO(json_ptr, end, CALL_FUCTION_FAILED, "json_get_value_by_name");
    rpc_args = msdp_dup_buff(json_ptr, len + 1);
    PTR_GOTO(rpc_args, end, "pmalloc fail");
    rpc_args[len] = '\0';

    char uuid[MAX_UUID_LEN] = {0};
    json_ptr = json_get_value_by_name(params, strlen(params), JSON_KEY_UUID, &len,
                                      NULL);
    PTR_GOTO(json_ptr, end, CALL_FUCTION_FAILED, "json_get_value_by_name");
    strncpy(uuid, json_ptr, len);

    device_helper_ptr helper = msdp_get_helper(uuid);
    PTR_GOTO(helper, end, "no exist model helper, uuid = %s", uuid);

    ret = msdp_rpc_handler(helper, rpc_name, rpc_args);
    RET_GOTO(ret, end, CALL_FUCTION_FAILED, "msdp_service_handler");

    ret = SERVICE_RESULT_OK;
end:
    if (rpc_name) {
        msdp_free_buff(rpc_name);
    }
    if (rpc_args) {
        msdp_free_buff(rpc_args);
    }

    return ret;
}
Example #5
0
/*
--> {"uuid":"42E6E69DAEF6483FBBA412A28AF7CD76","attrSet":["wlanSwitchState", "lightSwitchState"]}
<-- {"uuid":"42E6E69DAEF6483FBBA412A28AF7CD76","attrSet":["wlanSwitchState"],"wlanSwitchState":{"value":"1","when":"1404443369"}}
*/
static int msdp_get_status(char *params)
{
    int ret = SERVICE_RESULT_ERR;
    char *attrset;
    char *json_out = NULL;
    char params_in[512] = {0};
    int attrset_size, attrset_len = 0;

    //char szPostString[1024] = "{\"uuid\":\"2427287C75CD579897D3DB5854E46A2F\",\"wlanSwitchState\":{\"value\":\"1\",\"when\":\"1404443369\"}}";
    attrset = json_get_value_by_name(params, strlen(params), JSON_KEY_ATTRSET,
                                     &attrset_len, NULL);
    if (attrset) {
        attrset_size = json_get_array_size(attrset, attrset_len);
    }

    if (NULL == attrset || attrset_size == 0) {
        char uuid[MAX_UUID_LEN] = {0};
        int uuid_len = 0;
        char *uuid_ptr = json_get_value_by_name(params, strlen(params), JSON_KEY_UUID,
                                                &uuid_len, NULL);
        PTR_RETURN(uuid_ptr, SERVICE_RESULT_ERR, CALL_FUCTION_FAILED,
                   "json_get_value_by_name(uuid)");
        strncpy(uuid, uuid_ptr, sizeof(uuid) > uuid_len ? uuid_len : sizeof(uuid) - 1);

        char attrset[512] = {0};
        ret = msdp_get_all_attrname(uuid, attrset, sizeof(attrset));
        RET_RETURN(ret, "get all attribute name fail, uuid:%s", uuid);
        log_trace("attrset = %s\n", attrset);

        snprintf(params_in, sizeof(params_in) - 1, GET_DEVICE_ATTR_STRING_FMT, uuid,
                 attrset);
        params = params_in;
    }

    //遍历attrset,并调用厂商注册的接口,返回属性json串
    ret = msdp_get_device_status_handler(params, msdp_get_attr_each_cb,
                                         __get_attribute, &json_out);
    RET_RETURN(ret, CALL_FUCTION_FAILED, "msdp_status_handler");

    log_trace("json_out = %s", json_out ? (json_out) : "NULL");
    //todo: postDeviceData/deviceDataChange
    if (json_out) {
        int str_len, post_ret = SERVICE_RESULT_ERR;
        char devid_str[18] = {0};
        char *dev_id = json_get_value_by_name(params, strlen(params), JSON_KEY_DEVID,
                                              &str_len, NULL);
        if (dev_id != NULL) {
            post_ret = lmns_post_device_data(devid_str, json_out);
            RET_GOTO(post_ret, end, CALL_FUCTION_FAILED, "lmns_post_device_data");
        } else {
            int post_ret = msdp_add_asyncpost_task(json_out);
            RET_GOTO(post_ret, end, "add async post task fail, params:%s", json_out);
            json_out = NULL;
        }
    }

end:
    if (json_out) {
        msdp_free_buff(json_out);
    }

    return ret;
}
Example #6
0
static int __get_attribute(const char *uuid, const char *attr_name,
                           char **ppbuf)
{
    int ret = SERVICE_RESULT_ERR;
    attr_hanler_ptr attr_handler = NULL;
    int buff_size, count = 0;

    log_trace("uuid = %s, attr_name = %s", uuid, attr_name);

    *ppbuf = NULL;
    buff_size = MAX_PARAMS_LEN / (1 << MAX_VENDOR_CB_REMALLOC_COUNT);

    device_helper_ptr helper = NULL;
    helper = msdp_get_helper(uuid);
    PTR_RETURN(helper, ret, "no exist model helper, uuid = %s", uuid)

    os_mutex_lock((helper->mutex_lock));
    attr_handler = msdp_get_attr_handler(helper, attr_name);
    if (attr_handler) {
        log_debug("exist attribute(%s) handler", attr_name);

        void *get_cb = NULL;
        get_cb = attr_handler->get_cb;

        PTR_GOTO(get_cb, unlock_out, "attribute %s's get_cb is NULL", attr_name);

        while (count < MAX_VENDOR_CB_REMALLOC_COUNT) {
            buff_size = MAX_PARAMS_LEN / (1 << (MAX_VENDOR_CB_REMALLOC_COUNT - count));
            *ppbuf = msdp_new_buff( buff_size);
            PTR_GOTO(*ppbuf, unlock_out, "malloc failed");

            memset(*ppbuf, 0, buff_size);
            if (helper->dev_type == DEV_TYPE_GATEWAY) {
                ret = ((get_attr_cb)attr_handler->get_cb)(*ppbuf, buff_size);
            } else {
                // modify by wukong 2017-4-17
                char mac[ETHER_ADDR_BYTES] = {0};
                get_mac_by_uuid(uuid, mac);
                //modify by wukong end 2017-4-17

                ret = ((get_subdev_attr_cb)attr_handler->get_cb)(mac, *ppbuf, buff_size);
            }
            if (ret != SERVICE_BUFFER_INSUFFICENT) {
                break;
            }

            log_error("buff too short,buff_size = %d, remalloc", buff_size);
            msdp_free_buff(*ppbuf);
            *ppbuf = NULL;
            count++;
        }

        //检查attribute调用返回值
        RET_GOTO(ret, unlock_out, "get attibute \"%s\" handler faild", attr_name);
        if (NULL == *ppbuf) { // || strlen(*ppbuf) == 0)
            log_error("get attibute \"%s\" handler error, output_len == 0", attr_name);
            ret = SERVICE_RESULT_ERR;
            goto unlock_out;
        }

        log_debug("get attibute \"%s\" handler output = %s", attr_name, *ppbuf);
    } else {
        log_error("unsupport attribute \"%s\"", attr_name);
    }

    os_mutex_unlock((helper->mutex_lock));
    return ret;

unlock_out:
    os_mutex_unlock((helper->mutex_lock));

    if (*ppbuf) {
        msdp_free_buff(*ppbuf);
        *ppbuf = NULL;
    }

    return ret;
}