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/

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?

UserRevisionLine numberNew 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
edamame22 0:29983394c6b6 17 #ifndef __M2M_CONNECTION_SECURITY_PIMPL_H__
edamame22 0:29983394c6b6 18 #define __M2M_CONNECTION_SECURITY_PIMPL_H__
edamame22 0:29983394c6b6 19
edamame22 0:29983394c6b6 20 #include "mbed-client/m2mconnectionsecurity.h"
edamame22 0:29983394c6b6 21 #include "mbed-client/m2mtimerobserver.h"
edamame22 0:29983394c6b6 22 #include "mbed-client/m2mconstants.h"
edamame22 0:29983394c6b6 23 #include "mbed-client/m2msecurity.h"
edamame22 0:29983394c6b6 24
edamame22 0:29983394c6b6 25 #include "mbedtls/config.h"
edamame22 0:29983394c6b6 26 #include "mbedtls/platform.h"
edamame22 0:29983394c6b6 27 #include "mbedtls/debug.h"
edamame22 0:29983394c6b6 28 #include "mbedtls/ssl.h"
edamame22 0:29983394c6b6 29 #include "mbedtls/entropy.h"
edamame22 0:29983394c6b6 30 #include "mbedtls/ctr_drbg.h"
edamame22 0:29983394c6b6 31 #include "mbedtls/error.h"
edamame22 0:29983394c6b6 32 #include "mbedtls/certs.h"
edamame22 0:29983394c6b6 33 #include "mbedtls/entropy_poll.h"
edamame22 0:29983394c6b6 34
edamame22 0:29983394c6b6 35 class M2MTimer;
edamame22 0:29983394c6b6 36
edamame22 0:29983394c6b6 37 //TODO: Should we let application to select these or not??
edamame22 0:29983394c6b6 38 const static int PSK_SUITES[] = {
edamame22 0:29983394c6b6 39 MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA256,
edamame22 0:29983394c6b6 40 MBEDTLS_TLS_PSK_WITH_AES_256_CCM_8,
edamame22 0:29983394c6b6 41 MBEDTLS_TLS_PSK_WITH_AES_128_CCM_8,
edamame22 0:29983394c6b6 42 0
edamame22 0:29983394c6b6 43 };
edamame22 0:29983394c6b6 44
edamame22 0:29983394c6b6 45
edamame22 0:29983394c6b6 46 /**
edamame22 0:29983394c6b6 47 * @brief The M2MConnectionSecurityPimpl class
edamame22 0:29983394c6b6 48 */
edamame22 0:29983394c6b6 49 class M2MConnectionSecurityPimpl : public M2MTimerObserver {
edamame22 0:29983394c6b6 50
edamame22 0:29983394c6b6 51 private:
edamame22 0:29983394c6b6 52
edamame22 0:29983394c6b6 53 // Prevents the use of assignment operator by accident.
edamame22 0:29983394c6b6 54 M2MConnectionSecurityPimpl& operator=( const M2MConnectionSecurityPimpl& /*other*/ );
edamame22 0:29983394c6b6 55 // Prevents the use of copy constructor by accident
edamame22 0:29983394c6b6 56 M2MConnectionSecurityPimpl( const M2MConnectionSecurityPimpl& /*other*/ );
edamame22 0:29983394c6b6 57
edamame22 0:29983394c6b6 58 public:
edamame22 0:29983394c6b6 59
edamame22 0:29983394c6b6 60 /**
edamame22 0:29983394c6b6 61 * @brief Constructor
edamame22 0:29983394c6b6 62 */
edamame22 0:29983394c6b6 63 M2MConnectionSecurityPimpl(M2MConnectionSecurity::SecurityMode mode);
edamame22 0:29983394c6b6 64
edamame22 0:29983394c6b6 65 /**
edamame22 0:29983394c6b6 66 * @brief Destructor
edamame22 0:29983394c6b6 67 */
edamame22 0:29983394c6b6 68 virtual ~M2MConnectionSecurityPimpl();
edamame22 0:29983394c6b6 69
edamame22 0:29983394c6b6 70 /**
edamame22 0:29983394c6b6 71 * \brief Resets the socket connection states.
edamame22 0:29983394c6b6 72 */
edamame22 0:29983394c6b6 73 void reset();
edamame22 0:29983394c6b6 74
edamame22 0:29983394c6b6 75 /**
edamame22 0:29983394c6b6 76 * \brief Initiatlizes the socket connection states.
edamame22 0:29983394c6b6 77 */
edamame22 0:29983394c6b6 78 int init(const M2MSecurity *security);
edamame22 0:29983394c6b6 79
edamame22 0:29983394c6b6 80 /**
edamame22 0:29983394c6b6 81 * \brief Starts the connection in non-blocking mode.
edamame22 0:29983394c6b6 82 * \param connHandler The ConnectionHandler object that maintains the socket.
edamame22 0:29983394c6b6 83 * \return Returns the state of the connection. Successful or not.
edamame22 0:29983394c6b6 84 */
edamame22 0:29983394c6b6 85 int start_connecting_non_blocking(M2MConnectionHandler* connHandler);
edamame22 0:29983394c6b6 86
edamame22 0:29983394c6b6 87 /**
edamame22 0:29983394c6b6 88 * \brief Continues connectivity logic for secure connection.
edamame22 0:29983394c6b6 89 * \return Returns an error code if any while continuing the connection sequence.
edamame22 0:29983394c6b6 90 */
edamame22 0:29983394c6b6 91 int continue_connecting();
edamame22 0:29983394c6b6 92
edamame22 0:29983394c6b6 93 /**
edamame22 0:29983394c6b6 94 * \brief Connects the client to the server.
edamame22 0:29983394c6b6 95 * \param connHandler The ConnectionHandler object that maintains the socket.
edamame22 0:29983394c6b6 96 * \return Returns the state of the connection. Successful or not.
edamame22 0:29983394c6b6 97 */
edamame22 0:29983394c6b6 98 int connect(M2MConnectionHandler* connHandler);
edamame22 0:29983394c6b6 99
edamame22 0:29983394c6b6 100 /**
edamame22 0:29983394c6b6 101 * \brief Sends data to the server.
edamame22 0:29983394c6b6 102 * \param message The data to be sent.
edamame22 0:29983394c6b6 103 * \param len The length of the data.
edamame22 0:29983394c6b6 104 * @return Indicates whether the data is sent successfully or not.
edamame22 0:29983394c6b6 105 */
edamame22 0:29983394c6b6 106 int send_message(unsigned char *message, int len);
edamame22 0:29983394c6b6 107
edamame22 0:29983394c6b6 108 /**
edamame22 0:29983394c6b6 109 * \brief Reads the data received from the server.
edamame22 0:29983394c6b6 110 * \param message The data to be read.
edamame22 0:29983394c6b6 111 * \param len The length of the data.
edamame22 0:29983394c6b6 112 * \return Indicates whether the data is read successfully or not.
edamame22 0:29983394c6b6 113 */
edamame22 0:29983394c6b6 114 int read(unsigned char* buffer, uint16_t len);
edamame22 0:29983394c6b6 115
edamame22 0:29983394c6b6 116 /**
edamame22 0:29983394c6b6 117 * \brief Sets the function callback that will be called by mbed-client for
edamame22 0:29983394c6b6 118 * fetching random number from application for ensuring strong entropy.
edamame22 0:29983394c6b6 119 * \param random_callback A function pointer that will be called by mbed-client
edamame22 0:29983394c6b6 120 * while performing secure handshake.
edamame22 0:29983394c6b6 121 * Function signature should be uint32_t (*random_number_callback)(void);
edamame22 0:29983394c6b6 122 */
edamame22 0:29983394c6b6 123 void set_random_number_callback(random_number_cb callback);
edamame22 0:29983394c6b6 124
edamame22 0:29983394c6b6 125 /**
edamame22 0:29983394c6b6 126 * \brief Sets the function callback that will be called by mbed-client for
edamame22 0:29983394c6b6 127 * providing entropy source from application for ensuring strong entropy.
edamame22 0:29983394c6b6 128 * \param entropy_callback A function pointer that will be called by mbed-client
edamame22 0:29983394c6b6 129 * while performing secure handshake.
edamame22 0:29983394c6b6 130 * Function signature , if using mbed-client-mbedtls should be
edamame22 0:29983394c6b6 131 * int (*mbedtls_entropy_f_source_ptr)(void *data, unsigned char *output,
edamame22 0:29983394c6b6 132 * size_t len, size_t *olen);
edamame22 0:29983394c6b6 133 */
edamame22 0:29983394c6b6 134 void set_entropy_callback(entropy_cb callback);
edamame22 0:29983394c6b6 135
edamame22 0:29983394c6b6 136 protected: //From M2MTimerObserver
edamame22 0:29983394c6b6 137
edamame22 0:29983394c6b6 138 virtual void timer_expired(M2MTimerObserver::Type type);
edamame22 0:29983394c6b6 139
edamame22 0:29983394c6b6 140 private:
edamame22 0:29983394c6b6 141
edamame22 0:29983394c6b6 142 int start_handshake();
edamame22 0:29983394c6b6 143
edamame22 0:29983394c6b6 144 private:
edamame22 0:29983394c6b6 145
edamame22 0:29983394c6b6 146 bool _init_done;
edamame22 0:29983394c6b6 147 mbedtls_ssl_config _conf;
edamame22 0:29983394c6b6 148 mbedtls_ssl_context _ssl;
edamame22 0:29983394c6b6 149 mbedtls_x509_crt _cacert;
edamame22 0:29983394c6b6 150 mbedtls_x509_crt _owncert;
edamame22 0:29983394c6b6 151 mbedtls_pk_context _pkey;
edamame22 0:29983394c6b6 152 mbedtls_ctr_drbg_context _ctr_drbg;
edamame22 0:29983394c6b6 153 mbedtls_entropy_context _entropy;
edamame22 0:29983394c6b6 154 uint32_t _flags;
edamame22 0:29983394c6b6 155 M2MTimer *_timer;
edamame22 0:29983394c6b6 156 M2MConnectionSecurity::SecurityMode _sec_mode;
edamame22 0:29983394c6b6 157
edamame22 0:29983394c6b6 158 friend class Test_M2MConnectionSecurityPimpl;
edamame22 0:29983394c6b6 159 };
edamame22 0:29983394c6b6 160
edamame22 0:29983394c6b6 161 #endif //__M2M_CONNECTION_SECURITY_PIMPL_H__