coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data) { OIC_LOG(DEBUG, TAG, "IN"); if (!data) { OIC_LOG(ERROR, TAG, "invalid pointer parameter"); return NULL; } coap_option *option = coap_malloc(sizeof(coap_option) + length + 1); if (!option) { OIC_LOG(ERROR, TAG, "Out of memory"); return NULL; } memset(option, 0, sizeof(coap_option) + length + 1); COAP_OPTION_KEY(*option) = key; coap_option_def_t* def = coap_opt_def(key); if (NULL != def && coap_is_var_bytes(def)) { if (length > def->max) { // make sure we shrink the value so it fits the coap option definition // by truncating the value, disregard the leading bytes. OIC_LOG_V(DEBUG, TAG, "Option [%d] data size [%d] shrunk to [%d]", def->key, length, def->max); data = &(data[length-def->max]); length = def->max; } // Shrink the encoding length to a minimum size for coap // options that support variable length encoding. COAP_OPTION_LENGTH(*option) = coap_encode_var_bytes( COAP_OPTION_DATA(*option), coap_decode_var_bytes((unsigned char *)data, length)); } else { COAP_OPTION_LENGTH(*option) = length; memcpy(COAP_OPTION_DATA(*option), data, length); } /* we can pass NULL here as delete function since option is released automatically */ coap_list_t *node = coap_new_listnode(option, NULL); if (!node) { OIC_LOG(ERROR, TAG, "node is NULL"); coap_free(option); return NULL; } OIC_LOG(DEBUG, TAG, "OUT"); return node; }
// Put ordered list into pdu void coap_add_options(coap_pdu_t *pdu) { coap_list_t *option; for (option = optlist; option; option = option->next ) { coap_add_option ( pdu, COAP_OPTION_KEY(*(coap_option *)option->data), COAP_OPTION_LENGTH(*(coap_option *)option->data), COAP_OPTION_DATA(*(coap_option *)option->data) ); } }
coap_pdu_t* CACreatePDUforRequestWithPayload(const code_t code, coap_list_t *options, const char* payload) { OIC_LOG(DEBUG, TAG, "CACreatePDUforRequestWithPayload"); coap_pdu_t *pdu; coap_list_t *opt; unsigned char _token_data[8]; str the_token = { 0, _token_data }; if (!(pdu = coap_new_pdu())) return NULL; /* initialize message id */ unsigned short message_id; prng((unsigned char *)&message_id, sizeof(unsigned short)); pdu->hdr->type = msgtype; pdu->hdr->id = htons(++message_id); pdu->hdr->code = code; pdu->hdr->token_length = the_token.length; if (!coap_add_token(pdu, the_token.length, the_token.s)) { OIC_LOG(DEBUG, TAG,"cannot add token to request"); } for (opt = options; opt; opt = opt->next) { coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *)opt->data), COAP_OPTION_LENGTH(*(coap_option *)opt->data), COAP_OPTION_DATA(*(coap_option *)opt->data)); } if (NULL != payload) { uint32_t len = strlen(payload); if ((flags & CA_FLAGS_BLOCK) == 0) { OIC_LOG_V(DEBUG, TAG, "coap_add_data, payload: %s", payload); coap_add_data(pdu, len, payload); } else { OIC_LOG_V(DEBUG, TAG, "coap_add_block, payload: %s", payload); coap_add_block(pdu, len, payload, block.num, block.szx); } } return pdu; }
static coap_list_t * new_option_node(unsigned short key, unsigned char *data, unsigned int length) { coap_option *option; coap_list_t *node; option = coap_malloc(sizeof(coap_option) + length); if ( !option ) return NULL; COAP_OPTION_KEY(*option) = key; COAP_OPTION_LENGTH(*option) = length; memcpy(COAP_OPTION_DATA(*option), data, length); /* we can pass NULL here as delete function since option is released automatically */ node = coap_new_listnode(option, free); if (!node) coap_free(option); return node; }
coap_list_t* CACreateNewOptionNode(const uint16_t key, const uint32_t length, const uint8_t *data) { coap_option *option; coap_list_t *node; option = coap_malloc(sizeof(coap_option) + length); if (!option) goto error; COAP_OPTION_KEY(*option) = key; COAP_OPTION_LENGTH(*option) = length; memcpy(COAP_OPTION_DATA(*option), data, length); /* we can pass NULL here as delete function since option is released automatically */ node = coap_new_listnode(option, NULL); if (node) return node; error: perror("new_option_node: malloc"); coap_free( option); return NULL; }
coap_pdu_t* CACreatePDUforRequest(const code_t code, coap_list_t *options) { OIC_LOG(DEBUG, TAG, "CACreatePDUforRequest"); coap_pdu_t *pdu; coap_list_t *opt; unsigned char _token_data[8]; str the_token = { 0, _token_data }; if (!(pdu = coap_new_pdu())) return NULL; /* initialize message id */ unsigned short message_id; prng((unsigned char *)&message_id, sizeof(unsigned short)); pdu->hdr->type = msgtype; pdu->hdr->id = htons(++message_id); pdu->hdr->code = code; pdu->hdr->token_length = the_token.length; if (!coap_add_token(pdu, the_token.length, the_token.s)) { OIC_LOG(DEBUG, TAG, "cannot add token to request"); } for (opt = options; opt; opt = opt->next) { coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *)opt->data), COAP_OPTION_LENGTH(*(coap_option *)opt->data), COAP_OPTION_DATA(*(coap_option *)opt->data)); } return pdu; }
coap_pdu_t *CAGeneratePDUImpl(code_t code, coap_list_t *options, const CAInfo_t *info, const CAEndpoint_t *endpoint) { OIC_LOG(DEBUG, TAG, "IN"); VERIFY_NON_NULL_RET(info, TAG, "info", NULL); VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL); coap_pdu_t *pdu = coap_new_pdu(); if (NULL == pdu) { OIC_LOG(ERROR, TAG, "malloc failed"); return NULL; } OIC_LOG_V(DEBUG, TAG, "msgID is %d", info->messageId); uint16_t message_id; if (0 == info->messageId) { /* initialize message id */ prng((uint8_t * ) &message_id, sizeof(message_id)); OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id); } else { /* use saved message id */ message_id = info->messageId; } pdu->hdr->id = message_id; OIC_LOG_V(DEBUG, TAG, "messageId in pdu is %d, %d", message_id, pdu->hdr->id); pdu->hdr->type = info->type; pdu->hdr->code = COAP_RESPONSE_CODE(code); if (info->token && CA_EMPTY != code) { uint32_t tokenLength = info->tokenLength; OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength); OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)info->token, tokenLength); int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *)info->token); if (0 == ret) { OIC_LOG(ERROR, TAG, "can't add token"); } } if (options) { for (coap_list_t *opt = options; opt; opt = opt->next) { OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.", COAP_OPTION_DATA(*(coap_option *) opt->data)); coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data), COAP_OPTION_LENGTH(*(coap_option *) opt->data), COAP_OPTION_DATA(*(coap_option *) opt->data)); } } bool enabledPayload = false; #ifndef WITH_BWT enabledPayload = true; #endif if (enabledPayload || CA_ADAPTER_GATT_BTLE == endpoint->adapter) { if (NULL != info->payload && 0 < info->payloadSize) { OIC_LOG(DEBUG, TAG, "payload is added"); coap_add_data(pdu, info->payloadSize, (const unsigned char *) info->payload); } } OIC_LOG(DEBUG, TAG, "OUT"); return pdu; }