1 /*
2  * Copyright (C) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef COAP_ADAPTER_H
17 #define COAP_ADAPTER_H
18 
19 #include <stdbool.h>
20 
21 #include "coap_def.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #define COAP_VERSION 1
28 #define DEFAULT_TOK_LEN 0
29 #define MAX_TOK_LEN 8
30 #define HEADER_LEN 4
31 #define COAP_MAX_PDU_SIZE 1024
32 
33 typedef struct {
34     enum CoapProtocolTypeEnum protocol;
35     enum CoapMsgTypeEnum type;
36     uint8_t code;
37     uint8_t optionsNum;
38     uint16_t msgId;
39     CoapOption *options;
40 } CoapPacketParam;
41 
42 typedef struct {
43     char *readWriteBuf;
44     uint32_t len;
45     uint32_t size;
46 } CoapReadWriteBuffer;
47 
48 typedef struct {
49     char *remoteIp;
50     char *uriPath;
51     CoapMsgTypeEnum msgType;
52     CoapMethodTypeEnum methodType;
53     uint16_t msgId;
54 } CoapBuildParam;
55 
56 enum ErrorTypeEnum {
57     DISCOVERY_ERR_SUCCESS                      = 0,
58     DISCOVERY_ERR_HEADER_INVALID_SHORT         = 1,
59     DISCOVERY_ERR_VER_INVALID                  = 2,
60     DISCOVERY_ERR_TOKEN_INVALID_SHORT          = 3,
61     DISCOVERY_ERR_OPT_INVALID_SHORT_FOR_HEADER = 4,
62     DISCOVERY_ERR_OPT_INVALID_SHORT            = 5,
63     DISCOVERY_ERR_OPT_OVERRUNS_PKT             = 6,
64     DISCOVERY_ERR_OPT_INVALID_BIG              = 7,
65     DISCOVERY_ERR_OPT_INVALID_LEN              = 8,
66     DISCOVERY_ERR_BUF_INVALID_SMALL            = 9,
67     DISCOVERY_ERR_NOT_SUPPORTED                = 10,
68     DISCOVERY_ERR_OPT_INVALID_DELTA            = 11,
69     DISCOVERY_ERR_PKT_EXCEED_MAX_PDU           = 12,
70     DISCOVERY_ERR_TCP_TYPE_INVALID             = 13,
71     DISCOVERY_ERR_UNKNOWN_MSG_TYPE             = 14,
72     DISCOVERY_ERR_INVALID_PKT                  = 15,
73     DISCOVERY_ERR_INVALID_TOKEN_LEN            = 16,
74     DISCOVERY_ERR_INVALID_ARGUMENT             = 17,
75     DISCOVERY_ERR_TRANSPORT_NOT_UDP_OR_TCP     = 18,
76     DISCOVERY_ERR_INVALID_EMPTY_MSG            = 19,
77     DISCOVERY_ERR_SERVER_ERR                   = 20,
78     DISCOVERY_ERR_BAD_REQ                      = 21,
79     DISCOVERY_ERR_UNKNOWN_METHOD               = 22,
80     DISCOVERY_ERR_BLOCK_NO_PAYLOAD             = 23
81 };
82 
83 int32_t CoapSoftBusDecode(CoapPacket *pkt, const uint8_t *buf, uint32_t bufLen);
84 int32_t BuildCoapPkt(const CoapBuildParam *param, const char *pktPayload, CoapReadWriteBuffer *sndPktBuff, bool isAck);
85 void CoapSoftBusInitMsgId(void);
86 uint16_t CoapSoftBusMsgId(void);
87 
88 #ifdef __cplusplus
89 }
90 #endif
91 
92 #endif /* COAP_ADAPTER_H */
93