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.
m2mblockmessage.cpp
00001 /* 00002 * Copyright (c) 2015 ARM Limited. All rights reserved. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * Licensed under the Apache License, Version 2.0 (the License); you may 00005 * not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an AS IS BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #include "mbed-client/m2mblockmessage.h" 00017 #include "mbed-client/m2mconfig.h" 00018 #include <stdlib.h> 00019 #include <string.h> 00020 00021 M2MBlockMessage::M2MBlockMessage() : 00022 _block_data_ptr(NULL), 00023 _total_message_size(0), 00024 _block_data_len(0), 00025 _block_number(0), 00026 _error_code(M2MBlockMessage::ErrorNone), 00027 _is_last_block(false), 00028 _is_block_message(false) 00029 { 00030 } 00031 00032 M2MBlockMessage::~M2MBlockMessage() 00033 { 00034 free(_block_data_ptr); 00035 _block_data_ptr = NULL; 00036 } 00037 00038 void M2MBlockMessage::set_message_info(sn_coap_hdr_s *coap_header) 00039 { 00040 _is_block_message = (coap_header && 00041 coap_header->options_list_ptr && 00042 coap_header->options_list_ptr->block1 != -1) ? true : false; 00043 00044 if (coap_header && coap_header->options_list_ptr) { 00045 // Check total size 00046 if (coap_header->options_list_ptr->use_size1) { 00047 _total_message_size = coap_header->options_list_ptr->size1; 00048 } 00049 00050 // Default value in coap library is 65kb 00051 uint32_t max_size = SN_COAP_MAX_INCOMING_MESSAGE_SIZE; 00052 if (_total_message_size > max_size) { 00053 _error_code = M2MBlockMessage::EntityTooLarge; 00054 } else { 00055 _error_code = M2MBlockMessage::ErrorNone; 00056 } 00057 if (M2MBlockMessage::ErrorNone == _error_code) { 00058 // Is last block 00059 if (coap_header->options_list_ptr->block1 != -1) { 00060 // if (!(*(coap_header->options_list_ptr->block1_ptr + (coap_header->options_list_ptr->block1_len - 1)) & 0x08)) { 00061 if (!((coap_header->options_list_ptr->block1) & 0x08)) { 00062 _is_last_block = true; 00063 } else { 00064 _is_last_block = false; 00065 } 00066 } 00067 00068 _block_number = coap_header->options_list_ptr->block1 >> 4; 00069 // Block number 00070 // if (coap_header->options_list_ptr->block1_len == 3) { 00071 // _block_number = *(coap_header->options_list_ptr->block1_ptr) << 12; 00072 // _block_number |= *(coap_header->options_list_ptr->block1_ptr + 1) << 4; 00073 // _block_number |= (*(coap_header->options_list_ptr->block1_ptr + 2)) >> 4; 00074 // } 00075 00076 // else if (coap_header->options_list_ptr->block1_len == 2) { 00077 // _block_number = *(coap_header->options_list_ptr->block1_ptr) << 4; 00078 // _block_number |= (*(coap_header->options_list_ptr->block1_ptr + 1)) >> 4; 00079 // } 00080 // else if (coap_header->options_list_ptr->block1_len == 1) { 00081 // _block_number = (*coap_header->options_list_ptr->block1_ptr) >> 4; 00082 // } 00083 // else { 00084 // _block_number = 0; 00085 // } 00086 00087 // Payload 00088 free(_block_data_ptr); 00089 _block_data_ptr = NULL; 00090 _block_data_len = coap_header->payload_len; 00091 if(_block_data_len > 0) { 00092 _block_data_ptr = (uint8_t *)malloc(_block_data_len); 00093 if (_block_data_ptr) { 00094 memcpy(_block_data_ptr, coap_header->payload_ptr, _block_data_len); 00095 } 00096 } 00097 } 00098 } 00099 } 00100 00101 void M2MBlockMessage::clear_values() 00102 { 00103 free(_block_data_ptr); 00104 _block_data_ptr = NULL; 00105 _block_data_len = 0; 00106 _block_number = 0; 00107 _total_message_size = 0; 00108 _is_last_block = false; 00109 _error_code = M2MBlockMessage::ErrorNone; 00110 } 00111 00112 bool M2MBlockMessage::is_block_message() const 00113 { 00114 return _is_block_message; 00115 } 00116 00117 uint16_t M2MBlockMessage::block_number() const 00118 { 00119 return _block_number; 00120 } 00121 00122 uint32_t M2MBlockMessage::total_message_size() const 00123 { 00124 return _total_message_size; 00125 } 00126 00127 bool M2MBlockMessage::is_last_block() const 00128 { 00129 return _is_last_block; 00130 } 00131 00132 uint8_t* M2MBlockMessage::block_data() const 00133 { 00134 return _block_data_ptr; 00135 } 00136 00137 uint32_t M2MBlockMessage::block_data_len() const 00138 { 00139 return _block_data_len; 00140 } 00141 00142 M2MBlockMessage::Error M2MBlockMessage::error_code() const 00143 { 00144 return _error_code; 00145 }
Generated on Mon Aug 29 2022 19:53:39 by
