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