1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 
5 #ifndef __IOTX_COAP_INTERNAL__
6 #define __IOTX_COAP_INTERNAL__
7 
8 #include <stdio.h>
9 #include <string.h>
10 #include "linkkit/infra/infra_string.h"
11 #include "linkkit/infra/infra_compat.h"
12 #include "linkkit/infra/infra_types.h"
13 #include "linkkit/infra/infra_defs.h"
14 #include "linkkit/infra/infra_log.h"
15 #include "linkkit/infra/infra_json_parser.h"
16 #include "linkkit/infra/infra_cjson.h"
17 #include "linkkit/infra/infra_list.h"
18 #include "linkkit/infra/infra_md5.h"
19 #include "linkkit/infra/infra_sha256.h"
20 #include "linkkit/infra/infra_report.h"
21 #include "iotx_coap_config.h"
22 #include "linkkit/wrappers/wrappers.h"
23 #ifdef __cplusplus
24 extern "C" {
25 #endif /* __cplusplus */
26 
27 /*CoAP Content Type*/
28 #define COAP_CT_TEXT_PLAIN           0  /* text/plain (UTF-8) */
29 #define COAP_CT_APP_LINK_FORMAT      40 /* application/link-format */
30 #define COAP_CT_APP_XML              41 /* application/xml */
31 #define COAP_CT_APP_OCTET_STREAM     42 /* application/octet-stream */
32 #define COAP_CT_APP_RDF_XML          43 /* application/rdf+xml */
33 #define COAP_CT_APP_EXI              47 /* application/exi  */
34 #define COAP_CT_APP_JSON             50 /* application/json  */
35 #define COAP_CT_APP_CBOR             60 /* application/cbor  */
36 
37 /*CoAP option types. */
38 #define COAP_OPTION_IF_MATCH         1 /* C, opaque,  0-8 B, (none) */
39 #define COAP_OPTION_URI_HOST         3 /* C, String,  1-255 B, destination address */
40 #define COAP_OPTION_ETAG             4 /* E, opaque,  1-8 B, (none) */
41 #define COAP_OPTION_IF_NONE_MATCH    5 /* empty,      0 B, (none) */
42 #define COAP_OPTION_OBSERVE          6 /* E, empty/uint, 0 B/0-3 B, (none)*/
43 #define COAP_OPTION_URI_PORT         7 /* C, uint,    0-2 B, destination port */
44 #define COAP_OPTION_LOCATION_PATH    8 /* E, String,  0-255 B, - */
45 #define COAP_OPTION_URI_PATH         11 /* C, String,  0-255 B, (none) */
46 #define COAP_OPTION_CONTENT_FORMAT   12 /* E, uint,      0-2 B, (none) */
47 #define COAP_OPTION_MAXAGE           14 /* E, uint,    0--4 B, 60 Seconds */
48 #define COAP_OPTION_URI_QUERY        15 /* C, String,  1-255 B, (none) */
49 #define COAP_OPTION_ACCEPT           17 /* C, uint,    0-2 B, (none) */
50 #define COAP_OPTION_LOCATION_QUERY   20 /* E, String,      0-255 B, (none) */
51 #define COAP_OPTION_BLOCK2           23 /* C, uint,    0--3 B, (none) */
52 #define COAP_OPTION_BLOCK1           27 /* C, uint,    0--3 B, (none) */
53 #define COAP_OPTION_PROXY_URI        35 /* C, String,  1-1024 B, (none) */
54 #define COAP_OPTION_PROXY_SCHEME     39 /* C, String,  1-255 B, (none) */
55 #define COAP_OPTION_SIZE1            60 /* E, uint,    0-4 B, (none) */
56 #define COAP_OPTION_AUTH_TOKEN       61 /* C, String,  1-255B, (none)*/
57 
58 #define COAP_PERM_NONE               0x0000
59 #define COAP_PERM_GET                0x0001
60 #define COAP_PERM_POST               0x0002
61 #define COAP_PERM_PUT                0x0004
62 #define COAP_PERM_DELETE             0x0008
63 #define COAP_PERM_OBSERVE            0x0100
64 
65 /*CoAP Message types*/
66 #define COAP_MESSAGE_TYPE_CON        0
67 #define COAP_MESSAGE_TYPE_NON        1
68 #define COAP_MESSAGE_TYPE_ACK        2
69 #define COAP_MESSAGE_TYPE_RST        3
70 
71 /* CoAP module error code base */
72 #define COAP_ERROR_BASE              (1 << 8)
73 #define COAP_ERROR_DTLS_BASE         (1 << 16)
74 
75 /* CoAP base error code */
76 #define COAP_SUCCESS                 (0)                   /* Successful */
77 #define COAP_ERROR_INVALID_PARAM     (COAP_ERROR_BASE | 1) /* Invalid Parameter */
78 #define COAP_ERROR_NULL              (COAP_ERROR_BASE | 2) /* Null Pointer */
79 #define COAP_ERROR_MALLOC            (COAP_ERROR_BASE | 3)
80 #define COAP_ERROR_INVALID_LENGTH    (COAP_ERROR_BASE | 4) /* Invalid Length */
81 #define COAP_ERROR_DATA_SIZE         (COAP_ERROR_BASE | 5) /* Data size exceeds limit \
82                                                             */
83 #define COAP_ERROR_INVALID_URI       (COAP_ERROR_BASE | 6)
84 #define COAP_ERROR_NOT_FOUND         (COAP_ERROR_BASE | 7)
85 #define COAP_ERROR_NET_INIT_FAILED   (COAP_ERROR_BASE | 8)
86 #define COAP_ERROR_INTERNAL          (COAP_ERROR_BASE | 9) /* Internal Error */
87 #define COAP_ERROR_WRITE_FAILED      (COAP_ERROR_BASE | 10)
88 #define COAP_ERROR_READ_FAILED       (COAP_ERROR_BASE | 11)
89 #define COAP_ERROR_ENCRYPT_FAILED    (COAP_ERROR_BASE | 12)
90 #define COAP_ERROR_UNSUPPORTED       (COAP_ERROR_BASE | 13)
91 #define COAP_ERROR_OBJ_ALREADY_EXIST (COAP_ERROR_BASE | 14)
92 
93 #define COAP_MSG_CODE_DEF(N)         (((N) / 100 << 5) | (N) % 100)
94 
95 /*CoAP Message codes*/
96 typedef enum {
97     /* CoAP Empty Message */
98     COAP_MSG_CODE_EMPTY_MESSAGE =
99         COAP_MSG_CODE_DEF(0), /* Mapping to CoAP code 0.00 */
100 
101     /* CoAP Method Codes */
102     COAP_MSG_CODE_GET = COAP_MSG_CODE_DEF(1),    /* CoAP Get method */
103     COAP_MSG_CODE_POST = COAP_MSG_CODE_DEF(2),   /* CoAP Post method */
104     COAP_MSG_CODE_PUT = COAP_MSG_CODE_DEF(3),    /* CoAP Put method */
105     COAP_MSG_CODE_DELETE = COAP_MSG_CODE_DEF(4), /* CoAP Delete method */
106 
107     /* CoAP Success Response Codes */
108     COAP_MSG_CODE_201_CREATED = COAP_MSG_CODE_DEF(
109         201), /* Mapping to CoAP code 2.01, Hex:0x41, Created */
110     COAP_MSG_CODE_202_DELETED = COAP_MSG_CODE_DEF(
111         202), /* Mapping to CoAP code 2.02, Hex:0x42, Deleted*/
112     COAP_MSG_CODE_203_VALID =
113         COAP_MSG_CODE_DEF(203), /* Mapping to CoAP code 2.03, Hex:0x43, Valid*/
114     COAP_MSG_CODE_204_CHANGED = COAP_MSG_CODE_DEF(
115         204), /* Mapping to CoAP code 2.04, Hex:0x44, Changed*/
116     COAP_MSG_CODE_205_CONTENT = COAP_MSG_CODE_DEF(
117         205), /* Mapping to CoAP code 2.05, Hex:0x45, Content*/
118     COAP_MSG_CODE_231_CONTINUE = COAP_MSG_CODE_DEF(
119         231), /* Mapping to CoAP code 2.31, Hex:0x5F, Continue*/
120 
121     /* CoAP Client Error Response Codes */
122     COAP_MSG_CODE_400_BAD_REQUEST = COAP_MSG_CODE_DEF(
123         400), /* Mapping to CoAP code 4.00, Hex:0x80, Bad Request */
124     COAP_MSG_CODE_401_UNAUTHORIZED = COAP_MSG_CODE_DEF(
125         401), /* Mapping to CoAP code 4.01, Hex:0x81, Unauthorized */
126     COAP_MSG_CODE_402_BAD_OPTION = COAP_MSG_CODE_DEF(
127         402), /* Mapping to CoAP code 4.02, Hex:0x82, Bad Option */
128     COAP_MSG_CODE_403_FORBIDDEN = COAP_MSG_CODE_DEF(
129         403), /* Mapping to CoAP code 4.03, Hex:0x83, Forbidden */
130     COAP_MSG_CODE_404_NOT_FOUND = COAP_MSG_CODE_DEF(
131         404), /* Mapping to CoAP code 4.04, Hex:0x84, Not Found */
132     COAP_MSG_CODE_405_METHOD_NOT_ALLOWED = COAP_MSG_CODE_DEF(
133         405), /* Mapping to CoAP code 4.05, Hex:0x85, Method Not Allowed */
134     COAP_MSG_CODE_406_NOT_ACCEPTABLE = COAP_MSG_CODE_DEF(
135         406), /* Mapping to CoAP code 4.06, Hex:0x86, Not Acceptable */
136     COAP_MSG_CODE_408_REQUEST_ENTITY_INCOMPLETE =
137         COAP_MSG_CODE_DEF(408), /* Mapping to CoAP code 4.08, Hex:0x88, Request
138                                    Entity Incomplete */
139     COAP_MSG_CODE_412_PRECONDITION_FAILED = COAP_MSG_CODE_DEF(
140         412), /* Mapping to CoAP code 4.12, Hex:0x8C, Precondition Failed */
141     COAP_MSG_CODE_413_REQUEST_ENTITY_TOO_LARGE =
142         COAP_MSG_CODE_DEF(413), /* Mapping to CoAP code 4.13, Hex:0x8D, Request
143                                    Entity Too Large */
144     COAP_MSG_CODE_415_UNSUPPORTED_CONTENT_FORMAT =
145         COAP_MSG_CODE_DEF(415), /* Mapping to CoAP code 4.15, Hex:0x8F,
146                                    Unsupported Content-Format */
147 
148     /* CoAP Server Error Response Codes */
149     COAP_MSG_CODE_500_INTERNAL_SERVER_ERROR = COAP_MSG_CODE_DEF(
150         500), /* Mapping to CoAP code 5.00, Hex:0xA0, Internal Server Error */
151     COAP_MSG_CODE_501_NOT_IMPLEMENTED = COAP_MSG_CODE_DEF(
152         501), /* Mapping to CoAP code 5.01, Hex:0xA1, Not Implemented */
153     COAP_MSG_CODE_502_BAD_GATEWAY = COAP_MSG_CODE_DEF(
154         502), /* Mapping to CoAP code 5.02, Hex:0xA2, Bad Gateway */
155     COAP_MSG_CODE_503_SERVICE_UNAVAILABLE = COAP_MSG_CODE_DEF(
156         503), /* Mapping to CoAP code 5.03, Hex:0xA3, Service Unavailable */
157     COAP_MSG_CODE_504_GATEWAY_TIMEOUT = COAP_MSG_CODE_DEF(
158         504), /* Mapping to CoAP code 5.04, Hex:0xA4, Gateway Timeout */
159     COAP_MSG_CODE_505_PROXYING_NOT_SUPPORTED = COAP_MSG_CODE_DEF(
160         505) /* Mapping to CoAP code 5.05, Hex:0xA5, Proxying Not Supported */
161 
162 } CoAPMessageCode;
163 
164 typedef enum {
165     COAP_REQUEST_SUCCESS = 0,
166     COAP_RECV_RESP_TIMEOUT,
167     COAP_RECV_RESP_SUC,
168 } CoAPReqResult;
169 
170 typedef struct {
171     int len;
172     unsigned char *data;
173 } CoAPLenString;
174 
175 typedef struct {
176     unsigned char version:2;
177     unsigned char type:2;
178     unsigned char tokenlen:4;
179     unsigned char code;
180     unsigned short msgid;
181 } CoAPMsgHeader;
182 
183 typedef struct {
184     unsigned short num;
185     unsigned short len;
186     unsigned char *val;
187 } CoAPMsgOption;
188 
189 typedef void CoAPContext;
190 typedef struct CoAPMessage CoAPMessage;
191 
192 typedef void (*CoAPSendMsgHandler)(CoAPContext *context, CoAPReqResult result,
193                                    void *userdata, NetworkAddr *remote,
194                                    CoAPMessage *message);
195 
196 typedef void (*CoAPEventNotifier)(unsigned int event, NetworkAddr *remote,
197                                   void *message);
198 
199 typedef void (*CoAPRecvMsgHandler)(CoAPContext *context, const char *paths,
200                                    NetworkAddr *remote, CoAPMessage *message);
201 
202 typedef int (*CoAPDataEncrypt)(CoAPContext *context, const char *paths,
203                                NetworkAddr *addr, CoAPMessage *message,
204                                CoAPLenString *src, CoAPLenString *dest);
205 typedef void (*CoAPRespMsgHandler)(void *data, void *message);
206 
207 struct CoAPMessage {
208     CoAPMsgHeader header;
209     unsigned char token[COAP_MSG_MAX_TOKEN_LEN];
210     CoAPMsgOption options[COAP_MSG_MAX_OPTION_NUM];
211     unsigned char optcount;
212     unsigned char optdelta;
213     unsigned short payloadlen;
214     unsigned char *payload;
215     CoAPSendMsgHandler handler;
216     CoAPRespMsgHandler resp;
217     void *user;
218     int keep;
219 };
220 
221 /* CoAP message options APIs*/
222 extern int CoAPStrOption_add(CoAPMessage *message, unsigned short optnum,
223                              unsigned char *data, unsigned short datalen);
224 
225 extern int CoAPStrOption_get(CoAPMessage *message, unsigned short optnum,
226                              unsigned char *data, unsigned short *datalen);
227 
228 extern int CoAPUintOption_add(CoAPMessage *message, unsigned short optnum,
229                               unsigned int data);
230 
231 extern int CoAPUintOption_get(CoAPMessage *message, unsigned short optnum,
232                               unsigned int *data);
233 
234 extern int CoAPOption_present(CoAPMessage *message, unsigned short option);
235 
236 extern int CoAPMessageId_set(CoAPMessage *message, unsigned short msgid);
237 
238 extern int CoAPMessageType_set(CoAPMessage *message, unsigned char type);
239 
240 extern int CoAPMessageCode_set(CoAPMessage *message, CoAPMessageCode code);
241 
242 extern int CoAPMessageCode_get(CoAPMessage *message, CoAPMessageCode *code);
243 
244 extern int CoAPMessageToken_set(CoAPMessage *message, unsigned char *token,
245                                 unsigned char tokenlen);
246 
247 extern int CoAPMessageUserData_set(CoAPMessage *message, void *userdata);
248 
249 extern int CoAPMessageKeep_Set(CoAPMessage *message, int keep);
250 
251 extern int CoAPMessagePayload_set(CoAPMessage *message, unsigned char *payload,
252                                   unsigned short payloadlen);
253 
254 extern int CoAPMessageHandler_set(CoAPMessage *message,
255                                   CoAPSendMsgHandler handler);
256 
257 extern int CoAPMessage_init(CoAPMessage *message);
258 
259 extern int CoAPMessage_destory(CoAPMessage *message);
260 
261 #ifdef __cplusplus
262 }
263 #endif /* __cplusplus */
264 
265 #endif
266