return length;
}
+size_t coap_get_opt_header_length(unsigned short key, size_t length)
+{
+ size_t headerLength = 0;
+
+ unsigned short optDeltaLength = 0;
+ if (COAP_OPTION_FIELD_8_BIT >= key)
+ {
+ optDeltaLength = 0;
+ }
+ else if (COAP_OPTION_FIELD_8_BIT < key && COAP_OPTION_FIELD_16_BIT >= key)
+ {
+ optDeltaLength = 1;
+ }
+ else if (COAP_OPTION_FIELD_16_BIT < key && COAP_OPTION_FIELD_32_BIT >= key)
+ {
+ optDeltaLength = 2;
+ }
+ else
+ {
+ printf("Error : Reserved for the Payload marker for Delta");
+ return 0;
+ }
+
+ size_t optLength = 0;
+ if (COAP_OPTION_FIELD_8_BIT >= length)
+ {
+ optLength = 0;
+ }
+ else if (COAP_OPTION_FIELD_8_BIT < length && COAP_OPTION_FIELD_16_BIT >= length)
+ {
+ optLength = 1;
+ }
+ else if (COAP_OPTION_FIELD_16_BIT < length && COAP_OPTION_FIELD_32_BIT >= length)
+ {
+ optLength = 2;
+ }
+ else
+ {
+ printf("Error : Reserved for the Payload marker for length");
+ return 0;
+ }
+
+ headerLength = length + optDeltaLength + optLength + 1;
+
+ return headerLength;
+}
+
#endif
void coap_add_code(const coap_pdu_t *pdu, coap_transport_type transport, unsigned int code)
#define COAP_TCP_LENGTH_FIELD_NUM_16_BIT 14
#define COAP_TCP_LENGTH_FIELD_NUM_32_BIT 15
+#define COAP_OPTION_FIELD_8_BIT 12
+#define COAP_OPTION_FIELD_16_BIT 256
+#define COAP_OPTION_FIELD_32_BIT 65536
+
typedef enum
{
coap_udp = 0,
* @return header length.
*/
unsigned int coap_get_tcp_header_length_for_transport(coap_transport_type transport);
+
+/**
+ * Get option length.
+ *
+ * @param key delta of option
+ * @param length length of option
+ * @return total option length
+ */
+size_t coap_get_opt_header_length(unsigned short key, size_t length);
#endif
/**
coap_transport_type transport;
unsigned int length = COAP_MAX_PDU_SIZE;
#ifdef TCP_ADAPTER
- unsigned int headerLength = 0;
+ unsigned int msgLength = 0;
if (CA_ADAPTER_TCP == endpoint->adapter)
{
if (options)
{
+ unsigned short prevOptNumber = 0;
for (coap_list_t *opt = options; opt; opt = opt->next)
{
- headerLength += COAP_OPTION_LENGTH(*(coap_option *) opt->data) + 1;
+ unsigned short curOptNumber = COAP_OPTION_KEY(*(coap_option *) opt->data);
+ if (prevOptNumber > curOptNumber)
+ {
+ OIC_LOG(ERROR, TAG, "option list is wrong");
+ return NULL;
+ }
+
+ size_t optValueLen = COAP_OPTION_LENGTH(*(coap_option *) opt->data);
+ size_t optLength = coap_get_opt_header_length(curOptNumber - prevOptNumber, optValueLen);
+ if (0 == optLength)
+ {
+ OIC_LOG(ERROR, TAG, "Reserved for the Payload marker for the option");
+ return NULL;
+ }
+ msgLength += optLength;
+ prevOptNumber = curOptNumber;
+ OIC_LOG_V(DEBUG, TAG, "curOptNumber[%d], prevOptNumber[%d], optValueLen[%d], "
+ "optLength[%d], msgLength[%d]",
+ curOptNumber, prevOptNumber, optValueLen, optLength, msgLength);
}
}
if (info->payloadSize > 0)
{
- headerLength = headerLength + info->payloadSize + 1;
+ msgLength = msgLength + info->payloadSize + PAYLOAD_MARKER;
}
- transport = coap_get_tcp_header_type_from_size(headerLength);
- length = headerLength + coap_get_tcp_header_length_for_transport(transport)
+ transport = coap_get_tcp_header_type_from_size(msgLength);
+ length = msgLength + coap_get_tcp_header_length_for_transport(transport)
+ info->tokenLength;
}
else
#ifdef TCP_ADAPTER
if (CA_ADAPTER_TCP == endpoint->adapter)
{
- coap_add_length(pdu, transport, headerLength);
+ coap_add_length(pdu, transport, msgLength);
}
else
#endif