1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 
5 #ifndef _IOT_EXPORT_ALCS_BACKUP_H_
6 #define _IOT_EXPORT_ALCS_BACKUP_H_
7 
8 #define IOTX_ALCS_ROLE_CLIENT  (0x01)
9 #define IOTX_ALCS_ROLE_SERVER  (0x02)
10 
11 #define ALCS_MSG_MAX_TOKEN_LEN (8)
12 
13 typedef enum {
14     /*iotx_alcs_send Message Result And Receive Message*/
15     IOTX_ALCS_EVENT_MSG_SEND_MESSAGE_SUCCESS,
16     IOTX_ALCS_EVENT_MSG_SEND_MESSAGE_RESP_TIMEOUT,
17     IOTX_ALCS_EVENT_MSG_RECV_MESSAGE,
18     /*Its data type is @iotx_alcs_transfer_msg_t and see detail at the declare
19      * of this type.*/
20 
21 } iotx_alcs_event_type_t;
22 
23 typedef struct iotx_alcs_event_msg_st {
24     /* Specify the event type */
25     iotx_alcs_event_type_t event_type;
26 
27     void *msg;
28 } iotx_alcs_event_msg_t, *iotx_alcs_event_msg_pt;
29 
30 typedef struct iotx_alcs_transfer_msg_st {
31     char *ip;
32     uint16_t port;
33     char *uri;
34     uint8_t token_len;
35     uint8_t *token;
36     uint16_t payload_len;
37     uint8_t *payload;
38 } iotx_alcs_transfer_msg_t, *iotx_alcs_transfer_msg_pt;
39 
40 typedef void (*iotx_alcs_event_handle_func_fpt)(void *pcontext, void *phandle,
41                                                 iotx_alcs_event_msg_t *msg);
42 
43 typedef struct {
44     iotx_alcs_event_handle_func_fpt h_fp;
45     void *pcontext;
46 } iotx_alcs_event_handle_t;
47 
48 typedef struct iotx_alcs_param_st {
49     uint8_t role; /*can be client, server or both*/
50 
51     uint8_t send_maxcount; /*list maximal count*/
52     uint8_t obs_maxcount;  /*observe maximal count*/
53     uint16_t port;         /* Local port */
54     char *group;           /* Multicast address */
55     uint32_t waittime;
56     uint8_t res_maxcount;
57 
58     iotx_alcs_event_handle_t *handle_event;
59 } iotx_alcs_param_t, *iotx_alcs_param_pt;
60 
61 #define ITOX_ALCS_COAP_MSG_CODE_DEF(N) (((N) / 100 << 5) | (N) % 100)
62 
63 /*CoAP Message codes*/
64 typedef enum {
65     /* CoAP Empty Message */
66     ITOX_ALCS_COAP_MSG_CODE_EMPTY_MESSAGE =
67         ITOX_ALCS_COAP_MSG_CODE_DEF(0), /* Mapping to CoAP code 0.00 */
68 
69     /* CoAP Method Codes */
70     ITOX_ALCS_COAP_MSG_CODE_GET =
71         ITOX_ALCS_COAP_MSG_CODE_DEF(1), /* CoAP Get method */
72     ITOX_ALCS_COAP_MSG_CODE_POST =
73         ITOX_ALCS_COAP_MSG_CODE_DEF(2), /* CoAP Post method */
74     ITOX_ALCS_COAP_MSG_CODE_PUT =
75         ITOX_ALCS_COAP_MSG_CODE_DEF(3), /* CoAP Put method */
76     ITOX_ALCS_COAP_MSG_CODE_DELETE =
77         ITOX_ALCS_COAP_MSG_CODE_DEF(4), /* CoAP Delete method */
78 
79     /* CoAP Success Response Codes */
80     ITOX_ALCS_COAP_MSG_CODE_201_CREATED = ITOX_ALCS_COAP_MSG_CODE_DEF(
81         201), /* Mapping to CoAP code 2.01, Hex:0x41, Created */
82     ITOX_ALCS_COAP_MSG_CODE_202_DELETED = ITOX_ALCS_COAP_MSG_CODE_DEF(
83         202), /* Mapping to CoAP code 2.02, Hex:0x42, Deleted*/
84     ITOX_ALCS_COAP_MSG_CODE_203_VALID = ITOX_ALCS_COAP_MSG_CODE_DEF(
85         203), /* Mapping to CoAP code 2.03, Hex:0x43, Valid*/
86     ITOX_ALCS_COAP_MSG_CODE_204_CHANGED = ITOX_ALCS_COAP_MSG_CODE_DEF(
87         204), /* Mapping to CoAP code 2.04, Hex:0x44, Changed*/
88     ITOX_ALCS_COAP_MSG_CODE_205_CONTENT = ITOX_ALCS_COAP_MSG_CODE_DEF(
89         205), /* Mapping to CoAP code 2.05, Hex:0x45, Content*/
90     ITOX_ALCS_COAP_MSG_CODE_231_CONTINUE = ITOX_ALCS_COAP_MSG_CODE_DEF(
91         231), /* Mapping to CoAP code 2.31, Hex:0x5F, Continue*/
92 
93     /* CoAP Client Error Response Codes */
94     ITOX_ALCS_COAP_MSG_CODE_400_BAD_REQUEST = ITOX_ALCS_COAP_MSG_CODE_DEF(
95         400), /* Mapping to CoAP code 4.00, Hex:0x80, Bad Request */
96     ITOX_ALCS_COAP_MSG_CODE_401_UNAUTHORIZED = ITOX_ALCS_COAP_MSG_CODE_DEF(
97         401), /* Mapping to CoAP code 4.01, Hex:0x81, Unauthorized */
98     ITOX_ALCS_COAP_MSG_CODE_402_BAD_OPTION = ITOX_ALCS_COAP_MSG_CODE_DEF(
99         402), /* Mapping to CoAP code 4.02, Hex:0x82, Bad Option */
100     ITOX_ALCS_COAP_MSG_CODE_403_FORBIDDEN = ITOX_ALCS_COAP_MSG_CODE_DEF(
101         403), /* Mapping to CoAP code 4.03, Hex:0x83, Forbidden */
102     ITOX_ALCS_COAP_MSG_CODE_404_NOT_FOUND = ITOX_ALCS_COAP_MSG_CODE_DEF(
103         404), /* Mapping to CoAP code 4.04, Hex:0x84, Not Found */
104     ITOX_ALCS_COAP_MSG_CODE_405_METHOD_NOT_ALLOWED =
105         ITOX_ALCS_COAP_MSG_CODE_DEF(
106             405), /* Mapping to CoAP code 4.05, Hex:0x85, Method Not Allowed */
107     ITOX_ALCS_COAP_MSG_CODE_406_NOT_ACCEPTABLE = ITOX_ALCS_COAP_MSG_CODE_DEF(
108         406), /* Mapping to CoAP code 4.06, Hex:0x86, Not Acceptable */
109     ITOX_ALCS_COAP_MSG_CODE_408_REQUEST_ENTITY_INCOMPLETE =
110         ITOX_ALCS_COAP_MSG_CODE_DEF(
111             408), /* Mapping to CoAP code 4.08, Hex:0x88, Request Entity
112                      Incomplete */
113     ITOX_ALCS_COAP_MSG_CODE_412_PRECONDITION_FAILED =
114         ITOX_ALCS_COAP_MSG_CODE_DEF(
115             412), /* Mapping to CoAP code 4.12, Hex:0x8C, Precondition Failed */
116     ITOX_ALCS_COAP_MSG_CODE_413_REQUEST_ENTITY_TOO_LARGE =
117         ITOX_ALCS_COAP_MSG_CODE_DEF(
118             413), /* Mapping to CoAP code 4.13, Hex:0x8D, Request Entity Too
119                      Large */
120     ITOX_ALCS_COAP_MSG_CODE_415_UNSUPPORTED_CONTENT_FORMAT =
121         ITOX_ALCS_COAP_MSG_CODE_DEF(
122             415), /* Mapping to CoAP code 4.15, Hex:0x8F, Unsupported
123                      Content-Format */
124 
125     /* CoAP Server Error Response Codes */
126     ITOX_ALCS_COAP_MSG_CODE_500_INTERNAL_SERVER_ERROR =
127         ITOX_ALCS_COAP_MSG_CODE_DEF(500), /* Mapping to CoAP code 5.00,
128                                              Hex:0xA0, Internal Server Error */
129     ITOX_ALCS_COAP_MSG_CODE_501_NOT_IMPLEMENTED = ITOX_ALCS_COAP_MSG_CODE_DEF(
130         501), /* Mapping to CoAP code 5.01, Hex:0xA1, Not Implemented */
131     ITOX_ALCS_COAP_MSG_CODE_502_BAD_GATEWAY = ITOX_ALCS_COAP_MSG_CODE_DEF(
132         502), /* Mapping to CoAP code 5.02, Hex:0xA2, Bad Gateway */
133     ITOX_ALCS_COAP_MSG_CODE_503_SERVICE_UNAVAILABLE =
134         ITOX_ALCS_COAP_MSG_CODE_DEF(
135             503), /* Mapping to CoAP code 5.03, Hex:0xA3, Service Unavailable */
136     ITOX_ALCS_COAP_MSG_CODE_504_GATEWAY_TIMEOUT = ITOX_ALCS_COAP_MSG_CODE_DEF(
137         504), /* Mapping to CoAP code 5.04, Hex:0xA4, Gateway Timeout */
138     ITOX_ALCS_COAP_MSG_CODE_505_PROXYING_NOT_SUPPORTED =
139         ITOX_ALCS_COAP_MSG_CODE_DEF(505) /* Mapping to CoAP code 5.05, Hex:0xA5,
140                                             Proxying Not Supported */
141 
142 } iotx_alcs_message_code_t;
143 
144 typedef enum {
145     IOTX_ALCS_MESSAGE_TYPE_CON = 0,
146     IOTX_ALCS_MESSAGE_TYPE_NON = 1,
147     IOTX_ALCS_MESSAGE_TYPE_ACK = 2,
148     IOTX_ALCS_MESSAGE_TYPE_RST = 3
149 } iotx_alcs_message_type_t;
150 
151 typedef enum {
152     IOTX_ALCS_MESSAGE_PERM_NONE = 0x0000,
153     IOTX_ALCS_MESSAGE_PERM_GET = 0x0001,
154     IOTX_ALCS_MESSAGE_PERM_POST = 0x0002,
155     IOTX_ALCS_MESSAGE_PERM_PUT = 0x0004,
156     IOTX_ALCS_MESSAGE_PERM_DEL = 0x0008,
157     IOTX_ALCS_MESSAGE_PERM_OBSERVE = 0x0100
158 } iotx_alcs_message_perm_t;
159 
160 typedef enum {
161     IOTX_ALCS_MESSAGE_CT_TEXT_PLAIN = 0,        /* text/plain (UTF-8) */
162     IOTX_ALCS_MESSAGE_CT_APP_LINK_FORMAT = 40,  /* application/link-format */
163     IOTX_ALCS_MESSAGE_CT_APP_XML = 41,          /* application/xml */
164     IOTX_ALCS_MESSAGE_CT_APP_OCTET_STREAM = 42, /* application/octet-stream */
165     IOTX_ALCS_MESSAGE_CT_APP_RDF_XML = 43,      /* application/rdf+xml */
166     IOTX_ALCS_MESSAGE_CT_APP_EXI = 47,          /* application/exi  */
167     IOTX_ALCS_MESSAGE_CT_APP_JSON = 50,         /* application/json  */
168     IOTX_ALCS_MESSAGE_CT_APP_CBOR = 60          /* application/cbor  */
169 } iotx_alcs_message_content_type_t;
170 
171 typedef struct iotx_alcs_msg_st {
172     uint16_t group_id; /*multicast group id, used as unicast when 0*/
173     char *ip;          /*dotted decimal notation, max len 16*/
174     uint16_t port;
175     iotx_alcs_message_code_t msg_code;
176     iotx_alcs_message_type_t msg_type;
177     char *uri;
178     uint32_t payload_len;
179     uint8_t *payload;
180 } iotx_alcs_msg_t, *iotx_alcs_msg_pt;
181 
182 typedef struct iotx_alcs_res_st {
183     char *uri;
184     int32_t need_auth;
185     iotx_alcs_message_perm_t msg_perm;
186     iotx_alcs_message_content_type_t msg_ct;
187     uint32_t maxage; /*0~60*/
188     CoAPRecvMsgHandler callback;
189 } iotx_alcs_res_t, *iotx_alcs_res_pt;
190 
191 /**
192  * @brief Construct the ALCS handle
193  *        This function initialize the data structures, initialize ALCS
194  * information.
195  *
196  * @param [in] params: specify the ALCS initialize parameter.
197  *
198  * @retval     NULL : Construct failed.
199  * @retval NOT_NULL : The handle of ALCS.
200  * @see None.
201  */
202 void *iotx_alcs_construct(iotx_alcs_param_t *params);
203 
204 /**
205  * @brief Init Cloud Part
206  *        This function initialize the cloud part.
207  *
208  * @param [in] params: specify the ALCS initialize parameter.
209  *
210  * @retval     NULL : Construct failed.
211  * @retval NOT_NULL : The handle of ALCS.
212  * @see None.
213  */
214 int iotx_alcs_cloud_init(void *handle);
215 
216 /**
217  * @brief Deconstruct the ALCS handle
218  *        This function distroy ALCS handle and release the related resource.
219  *
220  * @param [in] phandle: pointer of handle, specify the MQTT client.
221  *
222  * @retval  0 : Deconstruct success.
223  * @retval -1 : Deconstruct failed.
224  * @see None.
225  */
226 int iotx_alcs_destroy(void **phandle);
227 
228 /**
229  * @brief Handle ALCS message from specific udp port
230  *
231  * @param [in] handle: specify the ALCS handle.
232  *
233  * @return status.
234  * @see None.
235  */
236 int iotx_alcs_yield(void *handle);
237 
238 /**
239  * @brief Send Message To Secific Deivce
240  *
241  * @param [in] handle: specify the ALCS handle.
242  *
243  * @return status.
244  * @see None.
245  */
246 int iotx_alcs_send(void *handle, iotx_alcs_msg_t *msg);
247 
248 /**
249  * @brief Send Response Message To Secific Deivce
250  *
251  * @param [in] handle: specify the ALCS handle.
252  *
253  * @return status.
254  * @see None.
255  */
256 int iotx_alcs_send_Response(void *handle, iotx_alcs_msg_t *msg,
257                             uint8_t token_len, uint8_t *token);
258 
259 /**
260  * @brief Register Resource
261  *
262  * @param [in] handle: specify the ALCS handle.
263  * @param [in] handle: the resource need to be registered.
264  *
265  * @return status.
266  * @see None.
267  */
268 int iotx_alcs_register_resource(void *handle, iotx_alcs_res_t *resource);
269 
270 /**
271  * @brief ALCS Observe Notify
272  *
273  * @param [in] handle: specify the ALCS handle.
274  * @param [in] uri: the resource need to notify.
275  *
276  * @return status.
277  * @see None.
278  */
279 int iotx_alcs_observe_notify(void *handle, const char *uri,
280                              uint32_t payload_len, uint8_t *payload);
281 
282 /**
283  * @brief Unregister Resource
284  *
285  * @param [in] handle: specify the ALCS handle.
286  * @param [in] handle: the resource need to be registered.
287  *
288  * @return status.
289  * @see None.
290  */
291 int iotx_alcs_unregister_resource(void *handle, char *uri);
292 
293 /**
294  * @brief Add sub device
295  *
296  * @param [in] handle: specify the ALCS handle.
297  * @param [in] pk: the productkey of device.
298  * @param [in] dn: the deviceName of device.
299  *
300  * @return status.
301  * @see None.
302  */
303 int iotx_alcs_add_sub_device(void *handle, const char *pk, const char *dn);
304 
305 /**
306  * @brief remove sub device
307  *
308  * @param [in] handle: specify the ALCS handle.
309  * @param [in] pk: the productkey of device.
310  * @param [in] dn: the deviceName of device.
311  *
312  * @return status.
313  * @see None.
314  */
315 int iotx_alcs_remove_sub_device(void *handle, const char *pk, const char *dn);
316 
317 #endif
318