void net_http_post(const char* url, const char* data, http_callback http_cb)
{
	// FIXME: handle HTTP auth with http://user:pass@host/
		// FIXME: make https work.
		// FIXME: get rid of the #anchor part if present.

		char hostname[128] = "";
		int port = 443;
		PRINTF("URL is %s\n", url);
		if (pd_strncmp(url, "https://", pd_strlen("https://")) != 0) {
			PRINTF("URL is not HTTPS %s\n", url);
			return;
		}
		url += pd_strlen("https://"); // Get rid of the protocol.

		char * path = pd_strchr(url, '/');
		if (path == NULL) {
			path = pd_strchr(url, '\0'); // Pointer to end of string.
		}

		char * colon = pd_strchr(url, ':');
		if (colon > path) {
			colon = NULL; // Limit the search to characters before the path.
		}

		if (colon == NULL) { // The port is not present.
			pd_memcpy(hostname, url, path - url);
			hostname[path - url] = '\0';
		}
		else {
			port = atoi(colon + 1);
			if (port == 0) {
				PRINTF("Port error %s\n", url);
				return;
			}

			pd_memcpy(hostname, url, colon - url);
			hostname[colon - url] = '\0';
		}


		if (path[0] == '\0') { // Empty path is not allowed.
			path = "/";
		}

		PRINTF("hostname=%s\n", hostname);
		PRINTF("port=%d\n", port);
		PRINTF("path=%s\n", path);
		http_raw_request(hostname, port, path, data, http_cb);
}
Ejemplo n.º 2
0
/**
  * @brief  MQTT initialization mqtt client function
  * @param  client: 	MQTT_Client reference
  * @param  clientid: 	MQTT client id
  * @param  client_user:MQTT client user
  * @param  client_pass:MQTT client password
  * @param  client_pass:MQTT keep alive timer, in second
  * @retval None
  */
void FUNCTION_ATTRIBUTE
MQTT_InitClient(MQTT_Client *mqttClient, uint8_t* client_id, uint8_t* client_user, uint8_t* client_pass, uint32_t keepAliveTime, uint8_t cleanSession)
{
	uint32_t temp;
	INFO("MQTT_InitClient\r\n");
	pd_memset(&mqttClient->connect_info, 0, sizeof(mqtt_connect_info_t));

	temp = pd_strlen(client_id);
	mqttClient->connect_info.client_id = (uint8_t*)pd_malloc(temp + 1);
    pd_memset(mqttClient->connect_info.client_id, 0, temp + 1);
	pd_strcpy(mqttClient->connect_info.client_id, client_id);
	mqttClient->connect_info.client_id[temp] = 0;

	temp = pd_strlen(client_user);
	mqttClient->connect_info.username = (uint8_t*)pd_malloc(temp + 1);
	pd_memset(mqttClient->connect_info.username, 0, temp + 1);
	pd_strcpy(mqttClient->connect_info.username, client_user);
	mqttClient->connect_info.username[temp] = 0;

	temp = pd_strlen(client_pass);
	mqttClient->connect_info.password = (uint8_t*)pd_malloc(temp + 1);
	pd_memset(mqttClient->connect_info.password, 0, temp + 1);
	pd_strcpy(mqttClient->connect_info.password, client_pass);
	mqttClient->connect_info.password[temp] = 0;

	mqttClient->connect_info.keepalive = keepAliveTime;
	mqttClient->connect_info.clean_session = cleanSession;

	mqttClient->mqtt_state.in_buffer = (uint8_t *)pd_malloc(MQTT_BUF_SIZE);
    pd_memset(mqttClient->mqtt_state.in_buffer, 0, MQTT_BUF_SIZE);
	mqttClient->mqtt_state.in_buffer_length = MQTT_BUF_SIZE;
	mqttClient->mqtt_state.out_buffer =  (uint8_t *)pd_malloc(MQTT_BUF_SIZE);
    pd_memset(mqttClient->mqtt_state.out_buffer, 0, MQTT_BUF_SIZE);
    mqttClient->mqtt_state.out_buffer_length = MQTT_BUF_SIZE;
	mqttClient->mqtt_state.connect_info = &mqttClient->connect_info;
	mqttClient->mqtt_state.message_length_read = 0;

	mqtt_msg_init(&mqttClient->mqtt_state.mqtt_connection, mqttClient->mqtt_state.out_buffer, mqttClient->mqtt_state.out_buffer_length);
	QUEUE_Init(&mqttClient->msgQueue, QUEUE_BUFFER_SIZE);
    //MQTT_Task(mqttClient);
}
Ejemplo n.º 3
0
void FUNCTION_ATTRIBUTE
MQTT_InitLWT(MQTT_Client *mqttClient, uint8_t* will_topic, uint8_t* will_msg, uint8_t will_qos, uint8_t will_retain)
{
	uint32_t temp;
	temp = pd_strlen(will_topic);
	mqttClient->connect_info.will_topic = (uint8_t*)pd_malloc(temp + 1);
    pd_memset(mqttClient->connect_info.will_topic, 0, temp + 1);
	pd_strcpy(mqttClient->connect_info.will_topic, will_topic);
	mqttClient->connect_info.will_topic[temp] = 0;

	temp = pd_strlen(will_msg);
	mqttClient->connect_info.will_message = (uint8_t*)pd_malloc(temp + 1);
    pd_memset(mqttClient->connect_info.will_message, 0, temp + 1);
    
	pd_strcpy(mqttClient->connect_info.will_message, will_msg);
	mqttClient->connect_info.will_message[temp] = 0;


	mqttClient->connect_info.will_qos = will_qos;
	mqttClient->connect_info.will_retain = will_retain;
}
Ejemplo n.º 4
0
/**
  * @brief  MQTT initialization connection function
  * @param  client: 	MQTT_Client reference
  * @param  host: 	Domain or IP string
  * @param  port: 	Port to connect
  * @param  security:		1 for ssl, 0 for none
  * @retval None
  */
void FUNCTION_ATTRIBUTE
MQTT_InitConnection(MQTT_Client *mqttClient, uint8_t* host, uint32_t port, uint8_t security)
{
	uint32_t temp;
	INFO("MQTT_InitConnection\r\n");
	pd_memset(mqttClient, 0, sizeof(MQTT_Client));
	temp = pd_strlen(host);
	mqttClient->host = (uint8_t*)pd_malloc(temp + 1);
    pd_memset(mqttClient->host, 0, temp + 1);
	pd_strcpy(mqttClient->host, host);
	mqttClient->host[temp] = 0;
	mqttClient->port = port;
	mqttClient->security = security;

}
static void FUNCTION_ATTRIBUTE
http_callback_login(char * response)
{
    if(request != NULL)
    {
        pd_free(request);
        request = NULL;
    }
    
    if(response == NULL)
    {
        device_login_callback(PANDO_LOGIN_FAIL);
        return;
    }
    
    pd_printf("response=%s\n(end)\n", response);
    
    struct jsonparse_state json_state;
    jsonparse_setup(&json_state, response, pd_strlen(response));
    int code;
    char message[MSG_BUF_LEN];
    char access_token[ACCESS_TOKEN_LEN*2 + 16];
    char access_addr[KEY_BUF_LEN];

    access_token[ACCESS_TOKEN_LEN*2] = '\0';
    int type;
    while ((type = jsonparse_next(&json_state)) != 0)
    {
        if (type == JSON_TYPE_PAIR_NAME) 
        {
            if(jsonparse_strcmp_value(&json_state, "code") == 0) 
            {
                jsonparse_next(&json_state);
                jsonparse_next(&json_state);
                code = jsonparse_get_value_as_int(&json_state);
            }
            else if(jsonparse_strcmp_value(&json_state, "message") == 0)
            {
                jsonparse_next(&json_state);
                jsonparse_next(&json_state);
                jsonparse_copy_value(&json_state, message, MSG_BUF_LEN);
            }
            else if(jsonparse_strcmp_value(&json_state, "data") == 0)
            {
                while((type = jsonparse_next(&json_state)) != 0 && json_state.depth > 1)
                {
                    if(type == JSON_TYPE_PAIR_NAME)
                    {
                        if(jsonparse_strcmp_value(&json_state, "access_token") == 0) 
                        {
                            jsonparse_next(&json_state);
                            jsonparse_next(&json_state);
                            jsonparse_copy_value(&json_state, access_token, ACCESS_TOKEN_LEN*2 + 16);
                        }
                        else if(jsonparse_strcmp_value(&json_state, "access_addr") == 0)
                        {
                            jsonparse_next(&json_state);
                            jsonparse_next(&json_state);
                            jsonparse_copy_value(&json_state, access_addr, KEY_BUF_LEN);
                        }
                    }
                }
            }
        }
    }

    if(code != 0)
    {
        pd_printf("device login failed: %s\n", message);
        if(device_login_callback != NULL) 
        {
            device_login_callback(PANDO_LOGIN_FAIL);
        }
        return;
    }

    hex2bin(pando_device_token, access_token);


    pd_printf("device login success, access_addr : %s\n", access_addr);

    pando_data_set(DATANAME_ACCESS_ADDR, access_addr);
    pando_data_set(DATANAME_ACCESS_TOKEN, access_token);
    if(device_login_callback != NULL) 
    {
        device_login_callback(PANDO_LOGIN_OK);
    }
}