Pfp Cybersecurity (Aka Power Fingerprinting, Inc.) / Mbed OS pfp-emon-nxp

Dependencies:   FXAS21002 FXOS8700Q

Committer:
vithyat
Date:
Wed Aug 28 19:24:56 2019 +0000
Revision:
0:977e87915078
init

Who changed what in which revision?

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