ucloud-iot-device-sdk-c/samples/http/http_client_sample.c

67 lines
1.9 KiB
C
Raw Normal View History

2019-11-22 15:57:51 +08:00
/*
* Copyright (C) 2012-2019 UCloud. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <stdbool.h>
#include <string.h>
#include <signal.h>
#include "uiot_import.h"
#include "ca.h"
#include "utils_httpc.h"
2020-02-21 17:33:21 +08:00
#include "uiot_export_http.h"
2019-11-22 15:57:51 +08:00
2019-12-03 18:10:23 +08:00
#define UIOT_MY_PRODUCT_SN "PRODUCT_SN"
2019-11-22 15:57:51 +08:00
2019-12-03 18:10:23 +08:00
#define UIOT_MY_DEVICE_SN "DEVICE_SN"
2019-11-22 15:57:51 +08:00
2019-12-03 18:10:23 +08:00
#define UIOT_MY_DEVICE_SECRET "DEVICE_SECRET"
2019-11-22 15:57:51 +08:00
2020-02-25 14:52:40 +08:00
#define UIOT_PUBLISH_TOPIC "%s/%s/upload/event"
2019-11-22 15:57:51 +08:00
int main(int argc, char **argv) {
2020-02-25 14:52:40 +08:00
char *token = (char *)HAL_Malloc(1024);
2020-02-21 17:33:21 +08:00
memset(token, 0, 1024);
2019-11-22 15:57:51 +08:00
int ret = SUCCESS_RET;
2020-02-25 14:52:40 +08:00
char *topic = (char *)HAL_Malloc(256);
2020-02-21 17:33:21 +08:00
memset(topic, 0, 256);
HAL_Snprintf((char *)topic, 256, UIOT_PUBLISH_TOPIC,UIOT_MY_PRODUCT_SN, UIOT_MY_DEVICE_SN);
char *data = "{\"test\": \"18\"}";
2019-11-22 15:57:51 +08:00
2020-02-21 17:33:21 +08:00
ret = IOT_HTTP_Get_Token(UIOT_MY_PRODUCT_SN, UIOT_MY_DEVICE_SN, UIOT_MY_DEVICE_SECRET, token);
2019-11-22 15:57:51 +08:00
if(SUCCESS_RET != ret)
{
2020-02-21 17:33:21 +08:00
HAL_Printf("get Token fail,ret:%d\r\n", ret);
2019-11-22 15:57:51 +08:00
return FAILURE_RET;
}
2020-02-21 17:33:21 +08:00
HAL_Printf("get token:%s\n", token);
HAL_Printf("topic:%s\n", topic);
ret = IOT_HTTP_Publish(token, topic, data, 5000);
2019-11-22 15:57:51 +08:00
if(SUCCESS_RET != ret)
{
2020-02-21 17:33:21 +08:00
HAL_Printf("Publish fail,ret:%d\r\n", ret);
2019-11-22 15:57:51 +08:00
return FAILURE_RET;
}
2020-02-21 17:33:21 +08:00
HAL_Printf("Publish success\n");
HAL_Free(token);
HAL_Free(topic);
return ret;
2019-11-22 15:57:51 +08:00
}