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