示例#1
0
static void set_device_time()
{
    http_session_t handle;
    http_resp_t *resp = NULL;
    char buf[MAX_DOWNLOAD_DATA];
    int64_t timestamp, offset;
    int size = 0;
    int status = 0;
    char url_name[MAX_URL_LEN];

    memset(url_name, 0, sizeof(url_name));
    strncpy(url_name, EVRYTHNG_GET_TIME_URL, strlen(EVRYTHNG_GET_TIME_URL));

    wmprintf("Get time from : %s\r\n", url_name);
    status = httpc_get(url_name, &handle, &resp, NULL);
    if (status != WM_SUCCESS) {
        wmprintf("Getting URL failed");
        return;
    }
    size = http_read_content(handle, buf, MAX_DOWNLOAD_DATA);
    if (size <= 0) {
        wmprintf("Reading time failed\r\n");
        goto out_time;
    }
    /*
      If timezone is present in PSM
      Get on http://api.evrythng.com/time?tz=<timezone>
      The data will look like this
      {
      "timestamp":1429514751927,
      "offset":-18000000,
      "localTime":"2015-04-20T02:25:51.927-05:00",
      "nextChange":1446361200000
      }
      If timezone is not presentin PSM
      Get on http://api.evrythng.com/time
      The data will look like this
      {
      "timestamp":1429514751927
      }
    */
    jsontok_t json_tokens[9];
    jobj_t json_obj;
    if (json_init(&json_obj, json_tokens, 10, buf, size) != WM_SUCCESS) {
        wmprintf("Wrong json string\r\n");
        goto out_time;
    }

    if (json_get_val_int64(&json_obj, "timestamp", &timestamp) == WM_SUCCESS) {
        if (json_get_val_int64(&json_obj, "offset", &offset)
                != WM_SUCCESS) {
            offset = 0;
        }
        timestamp = timestamp + offset;
    }
    wmtime_time_set_posix(timestamp/1000);
out_time:
    http_close_session(&handle);
    return;
}
示例#2
0
static void cmd_httpc_post(int argc, char **argv)
{
	if (argc < 3) {
		dbg("\nUsage: %s\n", httpc_test_cmds[0].help);
		return;
	}

	const char *url = argv[1];
	const char *data = argv[2];
	int len = strlen(data);
	int count = 1;		/* Default value */

	/* Check if user has given count */
	if (argc > 3) {
		count = strtol(argv[3], NULL, 0);
	}

	http_session_t hnd;
	int rv = http_open_session(&hnd, url, 0, NULL, 0);
	if (rv != 0) {
		dbg("Open session failed: %s (%d)", url, rv);
		return;
	}

	while (count--) {
		http_req_t req = {
			.type = HTTP_POST,
			.resource = url,
			.version = HTTP_VER_1_1,
			.content = data,
			.content_len = len,
		};

		rv = http_prepare_req(hnd, &req,
				      STANDARD_HDR_FLAGS |
				      HDR_ADD_CONN_KEEP_ALIVE);
		if (rv != 0) {
			dbg("Prepare request failed: %d", rv);
			break;
		}

		rv = http_send_request(hnd, &req);
		if (rv != 0) {
			dbg("Send request failed: %d", rv);
			break;
		}

		http_resp_t *resp;
		rv = http_get_response_hdr(hnd, &resp);
		if (rv != 0) {
			dbg("Get resp header failed: %d", rv);
			break;
		}

		dbg("Content length: %d", resp->content_length);
		if (resp->content_length == 0) {
			continue;
		}

		dbg("------------Content------------");
		while (1) {
			char buf[32];
			rv = http_read_content(hnd, buf, sizeof(buf));
			if (rv == 0 || rv < 0) {
				break;
			}
			wmprintf("%s", buf);
		}

		if (rv < 0) {
			/* Error condition */
			break;
		}
	}
}