Ram Gandikota
/
IOTMetronome
FRDM K64F Metronome
mbed-client/source/m2mblockmessage.cpp@0:dbad57390bd1, 2017-05-14 (annotated)
- Committer:
- ram54288
- Date:
- Sun May 14 18:37:05 2017 +0000
- Revision:
- 0:dbad57390bd1
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ram54288 | 0:dbad57390bd1 | 1 | /* |
ram54288 | 0:dbad57390bd1 | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
ram54288 | 0:dbad57390bd1 | 3 | * SPDX-License-Identifier: Apache-2.0 |
ram54288 | 0:dbad57390bd1 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
ram54288 | 0:dbad57390bd1 | 5 | * not use this file except in compliance with the License. |
ram54288 | 0:dbad57390bd1 | 6 | * You may obtain a copy of the License at |
ram54288 | 0:dbad57390bd1 | 7 | * |
ram54288 | 0:dbad57390bd1 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
ram54288 | 0:dbad57390bd1 | 9 | * |
ram54288 | 0:dbad57390bd1 | 10 | * Unless required by applicable law or agreed to in writing, software |
ram54288 | 0:dbad57390bd1 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
ram54288 | 0:dbad57390bd1 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ram54288 | 0:dbad57390bd1 | 13 | * See the License for the specific language governing permissions and |
ram54288 | 0:dbad57390bd1 | 14 | * limitations under the License. |
ram54288 | 0:dbad57390bd1 | 15 | */ |
ram54288 | 0:dbad57390bd1 | 16 | #include "mbed-client/m2mblockmessage.h" |
ram54288 | 0:dbad57390bd1 | 17 | #include "mbed-client/m2mconfig.h" |
ram54288 | 0:dbad57390bd1 | 18 | #include <stdlib.h> |
ram54288 | 0:dbad57390bd1 | 19 | #include <string.h> |
ram54288 | 0:dbad57390bd1 | 20 | |
ram54288 | 0:dbad57390bd1 | 21 | M2MBlockMessage::M2MBlockMessage() : |
ram54288 | 0:dbad57390bd1 | 22 | _block_data_ptr(NULL), |
ram54288 | 0:dbad57390bd1 | 23 | _total_message_size(0), |
ram54288 | 0:dbad57390bd1 | 24 | _block_data_len(0), |
ram54288 | 0:dbad57390bd1 | 25 | _block_number(0), |
ram54288 | 0:dbad57390bd1 | 26 | _error_code(M2MBlockMessage::ErrorNone), |
ram54288 | 0:dbad57390bd1 | 27 | _is_last_block(false), |
ram54288 | 0:dbad57390bd1 | 28 | _is_block_message(false) |
ram54288 | 0:dbad57390bd1 | 29 | { |
ram54288 | 0:dbad57390bd1 | 30 | } |
ram54288 | 0:dbad57390bd1 | 31 | |
ram54288 | 0:dbad57390bd1 | 32 | M2MBlockMessage::~M2MBlockMessage() |
ram54288 | 0:dbad57390bd1 | 33 | { |
ram54288 | 0:dbad57390bd1 | 34 | free(_block_data_ptr); |
ram54288 | 0:dbad57390bd1 | 35 | _block_data_ptr = NULL; |
ram54288 | 0:dbad57390bd1 | 36 | } |
ram54288 | 0:dbad57390bd1 | 37 | |
ram54288 | 0:dbad57390bd1 | 38 | void M2MBlockMessage::set_message_info(sn_coap_hdr_s *coap_header) |
ram54288 | 0:dbad57390bd1 | 39 | { |
ram54288 | 0:dbad57390bd1 | 40 | _is_block_message = (coap_header && |
ram54288 | 0:dbad57390bd1 | 41 | coap_header->options_list_ptr && |
ram54288 | 0:dbad57390bd1 | 42 | coap_header->options_list_ptr->block1 != -1) ? true : false; |
ram54288 | 0:dbad57390bd1 | 43 | |
ram54288 | 0:dbad57390bd1 | 44 | if (coap_header && coap_header->options_list_ptr) { |
ram54288 | 0:dbad57390bd1 | 45 | // Check total size |
ram54288 | 0:dbad57390bd1 | 46 | if (coap_header->options_list_ptr->use_size1) { |
ram54288 | 0:dbad57390bd1 | 47 | _total_message_size = coap_header->options_list_ptr->size1; |
ram54288 | 0:dbad57390bd1 | 48 | } |
ram54288 | 0:dbad57390bd1 | 49 | |
ram54288 | 0:dbad57390bd1 | 50 | // Default value in coap library is 65kb |
ram54288 | 0:dbad57390bd1 | 51 | uint32_t max_size = SN_COAP_MAX_INCOMING_MESSAGE_SIZE; |
ram54288 | 0:dbad57390bd1 | 52 | if (_total_message_size > max_size) { |
ram54288 | 0:dbad57390bd1 | 53 | _error_code = M2MBlockMessage::EntityTooLarge; |
ram54288 | 0:dbad57390bd1 | 54 | } else { |
ram54288 | 0:dbad57390bd1 | 55 | _error_code = M2MBlockMessage::ErrorNone; |
ram54288 | 0:dbad57390bd1 | 56 | } |
ram54288 | 0:dbad57390bd1 | 57 | if (M2MBlockMessage::ErrorNone == _error_code) { |
ram54288 | 0:dbad57390bd1 | 58 | // Is last block |
ram54288 | 0:dbad57390bd1 | 59 | if (coap_header->options_list_ptr->block1 != -1) { |
ram54288 | 0:dbad57390bd1 | 60 | // if (!(*(coap_header->options_list_ptr->block1_ptr + (coap_header->options_list_ptr->block1_len - 1)) & 0x08)) { |
ram54288 | 0:dbad57390bd1 | 61 | if (!((coap_header->options_list_ptr->block1) & 0x08)) { |
ram54288 | 0:dbad57390bd1 | 62 | _is_last_block = true; |
ram54288 | 0:dbad57390bd1 | 63 | } else { |
ram54288 | 0:dbad57390bd1 | 64 | _is_last_block = false; |
ram54288 | 0:dbad57390bd1 | 65 | } |
ram54288 | 0:dbad57390bd1 | 66 | } |
ram54288 | 0:dbad57390bd1 | 67 | |
ram54288 | 0:dbad57390bd1 | 68 | _block_number = coap_header->options_list_ptr->block1 >> 4; |
ram54288 | 0:dbad57390bd1 | 69 | // Block number |
ram54288 | 0:dbad57390bd1 | 70 | // if (coap_header->options_list_ptr->block1_len == 3) { |
ram54288 | 0:dbad57390bd1 | 71 | // _block_number = *(coap_header->options_list_ptr->block1_ptr) << 12; |
ram54288 | 0:dbad57390bd1 | 72 | // _block_number |= *(coap_header->options_list_ptr->block1_ptr + 1) << 4; |
ram54288 | 0:dbad57390bd1 | 73 | // _block_number |= (*(coap_header->options_list_ptr->block1_ptr + 2)) >> 4; |
ram54288 | 0:dbad57390bd1 | 74 | // } |
ram54288 | 0:dbad57390bd1 | 75 | |
ram54288 | 0:dbad57390bd1 | 76 | // else if (coap_header->options_list_ptr->block1_len == 2) { |
ram54288 | 0:dbad57390bd1 | 77 | // _block_number = *(coap_header->options_list_ptr->block1_ptr) << 4; |
ram54288 | 0:dbad57390bd1 | 78 | // _block_number |= (*(coap_header->options_list_ptr->block1_ptr + 1)) >> 4; |
ram54288 | 0:dbad57390bd1 | 79 | // } |
ram54288 | 0:dbad57390bd1 | 80 | // else if (coap_header->options_list_ptr->block1_len == 1) { |
ram54288 | 0:dbad57390bd1 | 81 | // _block_number = (*coap_header->options_list_ptr->block1_ptr) >> 4; |
ram54288 | 0:dbad57390bd1 | 82 | // } |
ram54288 | 0:dbad57390bd1 | 83 | // else { |
ram54288 | 0:dbad57390bd1 | 84 | // _block_number = 0; |
ram54288 | 0:dbad57390bd1 | 85 | // } |
ram54288 | 0:dbad57390bd1 | 86 | |
ram54288 | 0:dbad57390bd1 | 87 | // Payload |
ram54288 | 0:dbad57390bd1 | 88 | free(_block_data_ptr); |
ram54288 | 0:dbad57390bd1 | 89 | _block_data_ptr = NULL; |
ram54288 | 0:dbad57390bd1 | 90 | _block_data_len = coap_header->payload_len; |
ram54288 | 0:dbad57390bd1 | 91 | if(_block_data_len > 0) { |
ram54288 | 0:dbad57390bd1 | 92 | _block_data_ptr = (uint8_t *)malloc(_block_data_len); |
ram54288 | 0:dbad57390bd1 | 93 | if (_block_data_ptr) { |
ram54288 | 0:dbad57390bd1 | 94 | memcpy(_block_data_ptr, coap_header->payload_ptr, _block_data_len); |
ram54288 | 0:dbad57390bd1 | 95 | } |
ram54288 | 0:dbad57390bd1 | 96 | } |
ram54288 | 0:dbad57390bd1 | 97 | } |
ram54288 | 0:dbad57390bd1 | 98 | } |
ram54288 | 0:dbad57390bd1 | 99 | } |
ram54288 | 0:dbad57390bd1 | 100 | |
ram54288 | 0:dbad57390bd1 | 101 | void M2MBlockMessage::clear_values() |
ram54288 | 0:dbad57390bd1 | 102 | { |
ram54288 | 0:dbad57390bd1 | 103 | free(_block_data_ptr); |
ram54288 | 0:dbad57390bd1 | 104 | _block_data_ptr = NULL; |
ram54288 | 0:dbad57390bd1 | 105 | _block_data_len = 0; |
ram54288 | 0:dbad57390bd1 | 106 | _block_number = 0; |
ram54288 | 0:dbad57390bd1 | 107 | _total_message_size = 0; |
ram54288 | 0:dbad57390bd1 | 108 | _is_last_block = false; |
ram54288 | 0:dbad57390bd1 | 109 | _error_code = M2MBlockMessage::ErrorNone; |
ram54288 | 0:dbad57390bd1 | 110 | } |
ram54288 | 0:dbad57390bd1 | 111 | |
ram54288 | 0:dbad57390bd1 | 112 | bool M2MBlockMessage::is_block_message() const |
ram54288 | 0:dbad57390bd1 | 113 | { |
ram54288 | 0:dbad57390bd1 | 114 | return _is_block_message; |
ram54288 | 0:dbad57390bd1 | 115 | } |
ram54288 | 0:dbad57390bd1 | 116 | |
ram54288 | 0:dbad57390bd1 | 117 | uint16_t M2MBlockMessage::block_number() const |
ram54288 | 0:dbad57390bd1 | 118 | { |
ram54288 | 0:dbad57390bd1 | 119 | return _block_number; |
ram54288 | 0:dbad57390bd1 | 120 | } |
ram54288 | 0:dbad57390bd1 | 121 | |
ram54288 | 0:dbad57390bd1 | 122 | uint32_t M2MBlockMessage::total_message_size() const |
ram54288 | 0:dbad57390bd1 | 123 | { |
ram54288 | 0:dbad57390bd1 | 124 | return _total_message_size; |
ram54288 | 0:dbad57390bd1 | 125 | } |
ram54288 | 0:dbad57390bd1 | 126 | |
ram54288 | 0:dbad57390bd1 | 127 | bool M2MBlockMessage::is_last_block() const |
ram54288 | 0:dbad57390bd1 | 128 | { |
ram54288 | 0:dbad57390bd1 | 129 | return _is_last_block; |
ram54288 | 0:dbad57390bd1 | 130 | } |
ram54288 | 0:dbad57390bd1 | 131 | |
ram54288 | 0:dbad57390bd1 | 132 | uint8_t* M2MBlockMessage::block_data() const |
ram54288 | 0:dbad57390bd1 | 133 | { |
ram54288 | 0:dbad57390bd1 | 134 | return _block_data_ptr; |
ram54288 | 0:dbad57390bd1 | 135 | } |
ram54288 | 0:dbad57390bd1 | 136 | |
ram54288 | 0:dbad57390bd1 | 137 | uint32_t M2MBlockMessage::block_data_len() const |
ram54288 | 0:dbad57390bd1 | 138 | { |
ram54288 | 0:dbad57390bd1 | 139 | return _block_data_len; |
ram54288 | 0:dbad57390bd1 | 140 | } |
ram54288 | 0:dbad57390bd1 | 141 | |
ram54288 | 0:dbad57390bd1 | 142 | M2MBlockMessage::Error M2MBlockMessage::error_code() const |
ram54288 | 0:dbad57390bd1 | 143 | { |
ram54288 | 0:dbad57390bd1 | 144 | return _error_code; |
ram54288 | 0:dbad57390bd1 | 145 | } |