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