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_DEF_H 17 #define COAP_DEF_H 18 19 #include <stdint.h> 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 25 #define COAP_MAX_OPTION 16 26 #define DISCOVERY_MSG_URI_HOST 3 27 #define DISCOVERY_MSG_URI_PATH 11 28 29 #define COAP_RESPONSE_CODE(N) ((((N) / 100) << 5) | ((N) % 100)) 30 31 enum CoapProtocolTypeEnum { 32 COAP_UDP = 0, 33 COAP_TCP 34 }; 35 36 typedef enum CoapMethodTypeEnum { 37 COAP_METHOD_GET = 1, 38 COAP_METHOD_POST = 2, 39 COAP_METHOD_PUT = 3, 40 COAP_METHOD_DELETE = 4, 41 COAP_RESPONSE_201 = 201 42 } CoapMethodTypeEnum; 43 44 typedef enum CoapMsgTypeEnum { 45 COAP_TYPE_CON = 0, 46 COAP_TYPE_NONCON = 1, 47 COAP_TYPE_ACK = 2, 48 COAP_TYPE_RESET = 3 49 } CoapMsgTypeEnum; 50 51 typedef struct { 52 uint32_t ver : 2; 53 uint32_t type : 2; 54 uint32_t tokenLen : 4; 55 uint32_t code : 8; 56 union { 57 uint16_t msgLen; 58 uint16_t msgId; 59 } varSection; 60 } CoapHeader; 61 62 typedef struct { 63 const uint8_t *buffer; 64 uint32_t len; 65 } CoapBuffer; 66 67 typedef struct { 68 uint16_t num; 69 const uint8_t *optionBuf; 70 uint32_t len; 71 } CoapOption; 72 73 typedef struct { 74 enum CoapProtocolTypeEnum protocol; 75 uint32_t len; 76 CoapHeader header; 77 CoapBuffer token; 78 uint8_t optionsNum; 79 CoapOption options[COAP_MAX_OPTION]; 80 CoapBuffer payload; 81 } CoapPacket; 82 83 #ifdef __cplusplus 84 } 85 #endif 86 87 #endif /* COAP_DEF_H */ 88