1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 
5 #ifndef _IOT_EXPORT_HTTP_H_
6 #define _IOT_EXPORT_HTTP_H_
7 
8 #include "linkkit/infra/infra_types.h"
9 #include "linkkit/infra/infra_defs.h"
10 
11 #if defined(__cplusplus)
12 extern "C" {
13 #endif
14 
15 typedef struct {
16     char product_key[IOTX_PRODUCT_KEY_LEN + 1];
17     char device_name[IOTX_DEVICE_NAME_LEN + 1];
18     char device_id[IOTX_DEVICE_ID_LEN + 1];
19     char device_secret[IOTX_DEVICE_SECRET_LEN + 1];
20     char module_vendor_id[IOTX_MODULE_ID_LEN + 1];
21 } iotx_device_info_t;
22 
23 /* IoTx http initial param */
24 typedef struct {
25     iotx_device_info_t *device_info;
26     int keep_alive;
27     int timeout_ms;
28 } iotx_http_param_t;
29 
30 /* IoTx http context */
31 typedef struct {
32     char *p_auth_token;
33     uint32_t auth_token_len;
34     char is_authed;
35     const char *version;
36     const char *signmethod;
37     const char *sign;
38     iotx_device_info_t *p_devinfo;
39     const char *timestamp;
40     void *httpc;
41     int keep_alive;
42     int timeout_ms;
43 } iotx_http_t, *iotx_http_pt;
44 
45 /* IoTx http message definition
46  * request_payload and response_payload need to be allocate in order to save
47  * memory. topic_path specify the topic url you want to publish message.
48  */
49 typedef struct {
50     char *topic_path;
51     uint32_t request_payload_len;
52     char *request_payload;
53     uint32_t response_payload_len;
54     char *response_payload;
55     uint32_t timeout_ms;
56 } iotx_http_message_param_t;
57 
58 /* The response code from sever */
59 typedef enum {
60     IOTX_HTTP_SUCCESS = 0,
61     IOTX_HTTP_COMMON_ERROR = 10000,
62     IOTX_HTTP_PARAM_ERROR = 10001,
63     IOTX_HTTP_AUTH_CHECK_ERROR = 20000,
64     IOTX_HTTP_TOKEN_EXPIRED_ERROR = 20001,
65     IOTX_HTTP_TOKEN_NULL_ERROR = 20002,
66     IOTX_HTTP_TOKEN_CHECK_ERROR = 20003,
67     IOTX_HTTP_UPDATE_SESSION_ERROR = 20004,
68     IOTX_HTTP_PUBLISH_MESSAGE_ERROR = 30001,
69     IOTX_HTTP_REQUEST_TOO_MANY_ERROR = 40000,
70 } iotx_http_upstream_response_t;
71 
72 /** @defgroup group_api api
73  *  @{
74  */
75 
76 /** @defgroup group_api_http http
77  *  @{
78  */
79 
80 /**
81  * @brief   Initialize the HTTP client
82  *        This function initialize the data.
83  *
84  * @param [in] pInitParams: Specify the init param infomation.
85  *
86  * @retval NULL     : Initialize failed.
87  * @retval NOT_NULL : The contex of HTTP client.
88  * @see None.
89  */
90 void *IOT_HTTP_Init(iotx_http_param_t *pInitParams);
91 
92 /**
93  * @brief   De-initialize the HTTP client
94  *        This function release the related resource.
95  *
96  * @param [in] handle: pointer to http context pointer.
97  * @return None.
98  * @see None.
99  */
100 void IOT_HTTP_DeInit(void **handle);
101 
102 /**
103  * @brief   Handle device name authentication with remote server.
104  *
105  * @param [in] handle: Pointer of context, specify the HTTP client.
106  *
107  * @retval  0 : Authenticate success.
108  * @retval -1 : Authenticate failed.
109  * @see iotx_err_t.
110  */
111 int IOT_HTTP_DeviceNameAuth(void *handle);
112 
113 /**
114  * @brief   Send a message with specific path to server.
115  *        Client must authentication with server before send message.
116  *
117  * @param [in] handle: Pointer of contex, specify the HTTP client.
118  * @param [in] msg_param: Specify the topic path and http payload configuration.
119  *
120  * @retval  0 : Success.
121  * @retval -1 : Failed.
122  * @see iotx_err_t.
123  */
124 int IOT_HTTP_SendMessage(void *handle, iotx_http_message_param_t *msg_param);
125 
126 /**
127  * @brief   close tcp connection from client to server.
128  *
129  * @param [in] handle: Pointer of contex, specify the HTTP client.
130  * @return None.
131  * @see None.
132  */
133 void IOT_HTTP_Disconnect(void *handle);
134 
135 /** @} */ /* end of api_http */
136 /** @} */ /* end of api */
137 
138 #if defined(__cplusplus)
139 }
140 #endif
141 #endif
142