1 /* 2 * Copyright (C) 2022 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 __ATTEST_COAP_H__ 17 #define __ATTEST_COAP_H__ 18 19 #include <stdint.h> 20 #include <stdlib.h> 21 #include <attest_coap_def.h> 22 23 #ifdef __cplusplus 24 #if __cplusplus 25 extern "C" { 26 #endif 27 #endif 28 29 #define OVER_TCP 30 #ifdef OVER_TCP 31 #define COAP_TCP_SHIM_HEADER_LEN (2U) 32 33 #ifdef COAP_CONF_MAX_TCP_PDU_SIZE 34 #define COAP_MAX_TCP_PDU_SIZE (COAP_CONF_MAX_TCP_PDU_SIZE) 35 #else 36 #define COAP_MAX_TCP_PDU_SIZE (512) 37 #endif 38 #endif // OVER_TCP 39 40 #define COAP_MAX_SEGMENTS 4 41 #define HEADER_LEN 2 42 #define CODE_LEN 1 43 #define LENTKL_LEN 1 44 #define MARKER_LEN 1 45 #define PKT_TOKEN_LEN 2 46 #define DEFAULT_TOK_LEN 0 47 #define MAX_TOK_LEN 8 48 #define COAP_OPT_QUERY_LEN 64 49 50 #define BITS_PER_BYTE 8 51 52 /* used for generation of custom response codes */ 53 #define COAP_RESPONSE_CODE(N) ((N) / 100 << 5 | (N) % 100) 54 #define COAP_VERSION (0x01) 55 56 #define MAX_MESSAGE_LEN 1024 // kitFWK 2048 57 58 typedef struct { 59 size_t buffer; 60 size_t len; 61 } CoapExtendedLength; 62 63 typedef struct { 64 uint8_t len : 4; 65 uint8_t tkl : 4; 66 CoapExtendedLength extendedLength; 67 uint8_t code; 68 } CoapHead; 69 70 typedef struct { 71 const uint8_t* buffer; 72 size_t len; 73 } CoapBuffer; 74 75 typedef struct { 76 uint16_t num; 77 const uint8_t* optionBuffer; 78 size_t len; 79 } CoapOption; 80 81 typedef struct { 82 uint32_t transType; 83 size_t len; /* needed buffer length if serializing to a buffer */ 84 CoapHead hdr; 85 CoapBuffer tok; 86 uint8_t optsCnt; 87 CoapOption opts[COAP_MAX_OPTION]; 88 CoapBuffer payload; 89 } CoapPacket; 90 91 typedef struct { 92 char* rwBuffer; /* raw buffer for sending out the packet in serialized wag */ 93 size_t len; /* len indicates the size of filled data */ 94 size_t size; /* size indicates the Max PDU length */ 95 } CoapRWBuffer; 96 97 typedef struct { 98 uint32_t transType; 99 uint8_t code; 100 CoapOption* opts; 101 uint8_t optsCnt; 102 } CoapPacketParam; 103 104 int32_t CoapBuildMessage(CoapPacket* coapPacket, CoapPacketParam* coapPacketParam, CoapBuffer* payload, 105 char* buff, uint32_t* len); 106 107 int32_t CoapDecode(CoapPacket* pkt, const uint8_t* buf, size_t bufLen); 108 109 int32_t CoapGetExtensionLen(uint8_t param, size_t *length); 110 111 #ifdef __cplusplus 112 #if __cplusplus 113 } 114 #endif 115 #endif 116 117 #endif