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 <span class="notes">**Note:** To port mbed Client, you need to [install yotta and its dependencies](https://github.com/ARMmbed/yotta/blob/master/docs/index.md).</span>
edamame22 0:29983394c6b6 2
edamame22 0:29983394c6b6 3 # mbed Client structure and build process
edamame22 0:29983394c6b6 4
edamame22 0:29983394c6b6 5 <span class="tips">**Tip:** Before embarking on your own port, you should build the core mbed Client for an existing compilation target to get an understanding of how the mbed Client builds.</span>
edamame22 0:29983394c6b6 6
edamame22 0:29983394c6b6 7 mbed Client is structured as a set of modules. Each module declares which other modules it depends on. When you build a module, our build system `yotta` looks at these dependencies and installs the necessary modules before completing the build.
edamame22 0:29983394c6b6 8
edamame22 0:29983394c6b6 9 This is also the process to build applications for the mbed Client (including the example application in the release). The application declares dependencies on the mbed Client and when it is built, `yotta` ensures that the modules (and anything that they depend on, recursively) are present before building.
edamame22 0:29983394c6b6 10
edamame22 0:29983394c6b6 11 In general, `yotta` downloads and installs the necessary modules over the internet from the public yotta Registry (which saves published versions of modules) or from a specified source control URL.
edamame22 0:29983394c6b6 12
edamame22 0:29983394c6b6 13 # Components of mbed Client
edamame22 0:29983394c6b6 14
edamame22 0:29983394c6b6 15 ## Component software modules
edamame22 0:29983394c6b6 16
edamame22 0:29983394c6b6 17 mbed Client consists of one main component. More major components will be added in future development phases:
edamame22 0:29983394c6b6 18
edamame22 0:29983394c6b6 19 * `mbed-client` is the core mbed Client, providing C++ APIs for the mbed Client.
edamame22 0:29983394c6b6 20
edamame22 0:29983394c6b6 21 This module depends on further internal modules:
edamame22 0:29983394c6b6 22
edamame22 0:29983394c6b6 23 ```
edamame22 0:29983394c6b6 24 mbed-client x.x.x
edamame22 0:29983394c6b6 25 |
edamame22 0:29983394c6b6 26 |_mbed-client-c x.x.x
edamame22 0:29983394c6b6 27 | |_mbed-client-libservice x.x.x
edamame22 0:29983394c6b6 28 |
edamame22 0:29983394c6b6 29 |_mbed-client-linux x.x.x
edamame22 0:29983394c6b6 30 ```
edamame22 0:29983394c6b6 31
edamame22 0:29983394c6b6 32 To list the dependency trees, use the [`yotta list --all` command](http://docs.yottabuild.org/reference/commands.html).
edamame22 0:29983394c6b6 33
edamame22 0:29983394c6b6 34 <span class="notes">**Note**: In this case, we have listed the dependencies for the `x86-linux-native` compilation target. Different modules are needed for different compilation targets.</span>
edamame22 0:29983394c6b6 35
edamame22 0:29983394c6b6 36 We are using the [mbed Client Linux example](https://github.com/ARMmbed/mbed-client-linux-example) in this document. You can see that it depends directly only on the `mbed-client` and `mbed-client-linux` modules. These modules depend internally on various other modules.
edamame22 0:29983394c6b6 37
edamame22 0:29983394c6b6 38 ```
edamame22 0:29983394c6b6 39 mbed-client-linux-example x.x.x
edamame22 0:29983394c6b6 40 |
edamame22 0:29983394c6b6 41 |_mbed-client x.x.x
edamame22 0:29983394c6b6 42 |
edamame22 0:29983394c6b6 43 |_mbed-client-c x.x.x
edamame22 0:29983394c6b6 44 | |_mbed-client-libservice x.x.x
edamame22 0:29983394c6b6 45 |
edamame22 0:29983394c6b6 46 |_mbed-client-linux x.x.x
edamame22 0:29983394c6b6 47 ```
edamame22 0:29983394c6b6 48
edamame22 0:29983394c6b6 49 ## Compilation targets
edamame22 0:29983394c6b6 50
edamame22 0:29983394c6b6 51 To compile for a target board, you need a [target description](http://docs.yottabuild.org/tutorial/targets.html) that describes how to compile for the target.
edamame22 0:29983394c6b6 52
edamame22 0:29983394c6b6 53 The `mbed-client` module uses the platform name that each target defines to choose which `mbed-client-<platform-name>` module to depend on to provide the platform-specific implementation.
edamame22 0:29983394c6b6 54
edamame22 0:29983394c6b6 55 # Porting mbed Client to a different platform
edamame22 0:29983394c6b6 56
edamame22 0:29983394c6b6 57 To port mbed Client to a new platform:
edamame22 0:29983394c6b6 58
edamame22 0:29983394c6b6 59 1. [Request for a development repository](#requesting-for-a-development-repository).
edamame22 0:29983394c6b6 60 2. [Create a yotta compilation target for your board](#creating-a-yotta-compilation-target).
edamame22 0:29983394c6b6 61 3. [Implement the `mbed-client-xxx` module for your target platform](#implementing-mbed-client-xxx).
edamame22 0:29983394c6b6 62 4. [Modify the `module.json` of the `mbed-client` module](#modifying-the-json-file-in-the-mbed-client-module).
edamame22 0:29983394c6b6 63 5. [Verify that your implementation is correct](#testing-and-verification).
edamame22 0:29983394c6b6 64
edamame22 0:29983394c6b6 65 The `yotta` build system is designed for easy reuse of generic modules. If you intend to support multiple platforms that share common features, we recommend moving the common functionality into a separate module and use it for each platform.
edamame22 0:29983394c6b6 66
edamame22 0:29983394c6b6 67 ## Requesting for a development repository
edamame22 0:29983394c6b6 68
edamame22 0:29983394c6b6 69 We provide private git repositories to our partners porting mbed Client. Only the members of the mbed Client team and relevant partner contacts and engineers have access to these repositories.
edamame22 0:29983394c6b6 70
edamame22 0:29983394c6b6 71 When you contact `support@mbed.org`, a repository will be created for your module. You also need to provide the target description of your board as follows:
edamame22 0:29983394c6b6 72
edamame22 0:29983394c6b6 73 - **`mbed-client-<platform-name>`** is the module that provides the `mbed-client-xxx` implementation for your platform. You may choose to split it into further modules in the future, to enable sharing of code, but we recommend that you implement the port for your first board in this module itself.
edamame22 0:29983394c6b6 74
edamame22 0:29983394c6b6 75 - **`target-<targetname>`** contains the yotta target description of the target you are porting to. This is usually your platform name.
edamame22 0:29983394c6b6 76
edamame22 0:29983394c6b6 77 ## Creating a yotta compilation target
edamame22 0:29983394c6b6 78
edamame22 0:29983394c6b6 79 An example on compiling for linux target can be found in the `yotta_targets` directory of [the example application] (https://github.com/ARMmbed/mbed-client-linux-example).
edamame22 0:29983394c6b6 80
edamame22 0:29983394c6b6 81 Please, refer to the [yotta documentation](http://yottadocs.mbed.com/tutorial/targets.html) for setting up your compilation target.
edamame22 0:29983394c6b6 82
edamame22 0:29983394c6b6 83 1.To make your target available locally (without publishing it), you use the `yotta link-target` command to link it into the global install targets directory:
edamame22 0:29983394c6b6 84
edamame22 0:29983394c6b6 85 ```
edamame22 0:29983394c6b6 86 # in the directory of your target:
edamame22 0:29983394c6b6 87 yotta link-target
edamame22 0:29983394c6b6 88 ```
edamame22 0:29983394c6b6 89
edamame22 0:29983394c6b6 90 2.Use `yotta link-target <targetname>` command to make the globally linked target available when compiling another module.
edamame22 0:29983394c6b6 91
edamame22 0:29983394c6b6 92 3.Use the `yotta target <targetname>` command to select your target for the compilation.
edamame22 0:29983394c6b6 93
edamame22 0:29983394c6b6 94 ## Implementing mbed-client-xxx
edamame22 0:29983394c6b6 95
edamame22 0:29983394c6b6 96 Clone your `mbed-client-<your-platform-name>` module and `mbed-client` modules from GitHub.
edamame22 0:29983394c6b6 97
edamame22 0:29983394c6b6 98 The `mbed-client-<your-platform-name>` module needs to provide a socket and timer implementation for your target platform. The `mbed-client-xxx` module should include files `m2mconnectionhandler.h`and `m2mtimer.h` from `mbed-client` and implement a corresponding `.cpp` file that points to the platform-specific private implementations of the timer and the socket.
edamame22 0:29983394c6b6 99
edamame22 0:29983394c6b6 100 <span class="notes">**Note**: Private implementation classes **must** be named as `M2MConnectionHandlerPimpl` and `M2MTimerPimpl`, because of forward declarations.</span>
edamame22 0:29983394c6b6 101
edamame22 0:29983394c6b6 102 An example of mbed-client-platform:
edamame22 0:29983394c6b6 103
edamame22 0:29983394c6b6 104 ```
edamame22 0:29983394c6b6 105 |_module.json
edamame22 0:29983394c6b6 106 |
edamame22 0:29983394c6b6 107 |_mbed-client-platform
edamame22 0:29983394c6b6 108 | |_m2mconnectionhandlerpimpl.h
edamame22 0:29983394c6b6 109 | |_m2mtimerpimpl.h
edamame22 0:29983394c6b6 110 |
edamame22 0:29983394c6b6 111 |_source
edamame22 0:29983394c6b6 112 |_m2mconnectionhandler.cpp
edamame22 0:29983394c6b6 113 |_m2mconnectionhandlerpimpl.cpp
edamame22 0:29983394c6b6 114 |_m2mtimer.cpp
edamame22 0:29983394c6b6 115 |_m2mtimerpimpl.cpp
edamame22 0:29983394c6b6 116 ```
edamame22 0:29983394c6b6 117
edamame22 0:29983394c6b6 118 To make your module available to other modules that you want to build, you need to use the [`yotta link`](http://docs.yottabuild.org/reference/commands.html#yotta-link) command to link it to the module where you want to test it out.
edamame22 0:29983394c6b6 119
edamame22 0:29983394c6b6 120 For example, to use your local your in-development mbed-client implementation, use the command `yotta link mbed-client-xxx` in the main `mbed-client` module.
edamame22 0:29983394c6b6 121
edamame22 0:29983394c6b6 122 ```
edamame22 0:29983394c6b6 123 # in mbed-client, link your module:
edamame22 0:29983394c6b6 124 yotta link mbed-client-xxx
edamame22 0:29983394c6b6 125 ```
edamame22 0:29983394c6b6 126
edamame22 0:29983394c6b6 127 You can also just commit and push your untested code to GitHub, but it is always a good idea to test before committing.
edamame22 0:29983394c6b6 128
edamame22 0:29983394c6b6 129 Your `mbed-client-xxx` module must provide a platform-specific implementation for the mbed-client. The APIs that need porting are defined in the `mbed-client-linux` module. The header files contain documentation alongside the declaration of each function, where the function is described along with its parameters and return value.
edamame22 0:29983394c6b6 130
edamame22 0:29983394c6b6 131 There are two header files that require porting for your platform:
edamame22 0:29983394c6b6 132
edamame22 0:29983394c6b6 133 - `m2mconnectionhandler.h`
edamame22 0:29983394c6b6 134 - `m2mtimer.h`
edamame22 0:29983394c6b6 135
edamame22 0:29983394c6b6 136 To see how this is done in Linux, check the `mbed-client-linux` module from the mbed [Client Linux Example](https://github.com/ARMmbed/mbed-client-linux-example).
edamame22 0:29983394c6b6 137
edamame22 0:29983394c6b6 138 ### Implementing the M2MConnectionHandler class for your platform
edamame22 0:29983394c6b6 139
edamame22 0:29983394c6b6 140 ```
edamame22 0:29983394c6b6 141 /*
edamame22 0:29983394c6b6 142 * Copyright (c) 2015 ARM. All rights reserved.
edamame22 0:29983394c6b6 143 */
edamame22 0:29983394c6b6 144 #ifndef M2M_CONNECTION_HANDLER_H__
edamame22 0:29983394c6b6 145 #define M2M_CONNECTION_HANDLER_H__
edamame22 0:29983394c6b6 146
edamame22 0:29983394c6b6 147 #include "mbed-client/m2mconfig.h"
edamame22 0:29983394c6b6 148 #include "mbed-client/m2minterface.h"
edamame22 0:29983394c6b6 149 #include "mbed-client/m2mconnectionobserver.h"
edamame22 0:29983394c6b6 150 #include "nsdl-c/sn_nsdl.h"
edamame22 0:29983394c6b6 151
edamame22 0:29983394c6b6 152 /**
edamame22 0:29983394c6b6 153 * \brief M2MConnectionHandler.
edamame22 0:29983394c6b6 154 * This class handles the socket connection for the LWM2M Client.
edamame22 0:29983394c6b6 155 */
edamame22 0:29983394c6b6 156
edamame22 0:29983394c6b6 157 class M2MConnectionHandler {
edamame22 0:29983394c6b6 158 public:
edamame22 0:29983394c6b6 159
edamame22 0:29983394c6b6 160 /**
edamame22 0:29983394c6b6 161 * @enum ConnectionError
edamame22 0:29983394c6b6 162 * This enum defines an error that can come from
edamame22 0:29983394c6b6 163 * socket read and write operation.
edamame22 0:29983394c6b6 164 */
edamame22 0:29983394c6b6 165 typedef enum {
edamame22 0:29983394c6b6 166 CONNECTION_ERROR_WANTS_READ = -1000,
edamame22 0:29983394c6b6 167 CONNECTION_ERROR_WANTS_WRITE = -1001,
edamame22 0:29983394c6b6 168 ERROR_NONE = 0,
edamame22 0:29983394c6b6 169 SSL_CONNECTION_ERROR,
edamame22 0:29983394c6b6 170 SOCKET_READ_ERROR,
edamame22 0:29983394c6b6 171 SOCKET_SEND_ERROR,
edamame22 0:29983394c6b6 172 SOCKET_ABORT,
edamame22 0:29983394c6b6 173 DNS_RESOLVING_ERROR,
edamame22 0:29983394c6b6 174 SSL_HANDSHAKE_ERROR
edamame22 0:29983394c6b6 175 }ConnectionError;
edamame22 0:29983394c6b6 176
edamame22 0:29983394c6b6 177
edamame22 0:29983394c6b6 178 public:
edamame22 0:29983394c6b6 179
edamame22 0:29983394c6b6 180 /**
edamame22 0:29983394c6b6 181 * \brief Constructor
edamame22 0:29983394c6b6 182 */
edamame22 0:29983394c6b6 183 M2MConnectionHandler(M2MConnectionObserver &observer,
edamame22 0:29983394c6b6 184 M2MConnectionSecurity* sec,
edamame22 0:29983394c6b6 185 M2MInterface::BindingMode mode,
edamame22 0:29983394c6b6 186 M2MInterface::NetworkStack stack);
edamame22 0:29983394c6b6 187
edamame22 0:29983394c6b6 188 /**
edamame22 0:29983394c6b6 189 * \brief Destructor
edamame22 0:29983394c6b6 190 */
edamame22 0:29983394c6b6 191 ~M2MConnectionHandler();
edamame22 0:29983394c6b6 192
edamame22 0:29983394c6b6 193 /**
edamame22 0:29983394c6b6 194 * \brief This binds the socket connection.
edamame22 0:29983394c6b6 195 * \param listen_port Port to be listened to for an incoming connection.
edamame22 0:29983394c6b6 196 * \return True if successful, else false.
edamame22 0:29983394c6b6 197 */
edamame22 0:29983394c6b6 198 bool bind_connection(const uint16_t listen_port);
edamame22 0:29983394c6b6 199
edamame22 0:29983394c6b6 200 /**
edamame22 0:29983394c6b6 201 * \brief This resolves the server address. Output is
edamame22 0:29983394c6b6 202 * returned through a callback.
edamame22 0:29983394c6b6 203 * \param String The server address.
edamame22 0:29983394c6b6 204 * \param uint16_t The server port.
edamame22 0:29983394c6b6 205 * \param ServerType The server type to be resolved.
edamame22 0:29983394c6b6 206 * \param security The M2MSecurity object that determines which
edamame22 0:29983394c6b6 207 * type of secure connection will be used by the socket.
edamame22 0:29983394c6b6 208 * \return True if address is valid, else false.
edamame22 0:29983394c6b6 209 */
edamame22 0:29983394c6b6 210 bool resolve_server_address(const String& server_address,
edamame22 0:29983394c6b6 211 const uint16_t server_port,
edamame22 0:29983394c6b6 212 M2MConnectionObserver::ServerType server_type,
edamame22 0:29983394c6b6 213 const M2MSecurity* security);
edamame22 0:29983394c6b6 214
edamame22 0:29983394c6b6 215 /**
edamame22 0:29983394c6b6 216 * \brief Sends data to the connected server.
edamame22 0:29983394c6b6 217 * \param data_ptr The data to be sent.
edamame22 0:29983394c6b6 218 * \param data_len The length of data to be sent.
edamame22 0:29983394c6b6 219 * \param address_ptr The address structure to which the data needs to be sent.
edamame22 0:29983394c6b6 220 * \return True if data is sent successfully, else false.
edamame22 0:29983394c6b6 221 */
edamame22 0:29983394c6b6 222 bool send_data(uint8_t *data_ptr,
edamame22 0:29983394c6b6 223 uint16_t data_len,
edamame22 0:29983394c6b6 224 sn_nsdl_addr_s *address_ptr);
edamame22 0:29983394c6b6 225
edamame22 0:29983394c6b6 226 /**
edamame22 0:29983394c6b6 227 * \brief Listens to the incoming data from a remote server.
edamame22 0:29983394c6b6 228 * \return True if successful, else false.
edamame22 0:29983394c6b6 229 */
edamame22 0:29983394c6b6 230 bool start_listening_for_data();
edamame22 0:29983394c6b6 231
edamame22 0:29983394c6b6 232 /**
edamame22 0:29983394c6b6 233 * \brief Stops listening to the incoming data.
edamame22 0:29983394c6b6 234 */
edamame22 0:29983394c6b6 235 void stop_listening();
edamame22 0:29983394c6b6 236
edamame22 0:29983394c6b6 237 /**
edamame22 0:29983394c6b6 238 * \brief Sends directly to the socket. This is used by
edamame22 0:29983394c6b6 239 * security classes to send the data after it has been encrypted.
edamame22 0:29983394c6b6 240 * \param buf Buffer to send.
edamame22 0:29983394c6b6 241 * \param len The length of the buffer.
edamame22 0:29983394c6b6 242 * \return Number of bytes sent or -1 if failed.
edamame22 0:29983394c6b6 243 */
edamame22 0:29983394c6b6 244 int send_to_socket(const unsigned char *buf, size_t len);
edamame22 0:29983394c6b6 245
edamame22 0:29983394c6b6 246 /**
edamame22 0:29983394c6b6 247 * \brief Receives directly from the socket. This
edamame22 0:29983394c6b6 248 * is used by the security classes to receive raw data to be decrypted.
edamame22 0:29983394c6b6 249 * \param buf Buffer to send.
edamame22 0:29983394c6b6 250 * \param len The length of the buffer.
edamame22 0:29983394c6b6 251 * \return Number of bytes read or -1 if failed.
edamame22 0:29983394c6b6 252 */
edamame22 0:29983394c6b6 253 int receive_from_socket(unsigned char *buf, size_t len);
edamame22 0:29983394c6b6 254
edamame22 0:29983394c6b6 255 /**
edamame22 0:29983394c6b6 256 * \brief Closes the open connection.
edamame22 0:29983394c6b6 257 */
edamame22 0:29983394c6b6 258 void close_connection();
edamame22 0:29983394c6b6 259
edamame22 0:29983394c6b6 260 /**
edamame22 0:29983394c6b6 261 * \brief Error handling for DTLS connectivity.
edamame22 0:29983394c6b6 262 * \param error Error code from the TLS library.
edamame22 0:29983394c6b6 263 */
edamame22 0:29983394c6b6 264 void handle_connection_error(int error);
edamame22 0:29983394c6b6 265
edamame22 0:29983394c6b6 266 /**
edamame22 0:29983394c6b6 267 * \brief Sets the network interface handler that is used by client to connect
edamame22 0:29983394c6b6 268 * to a network over IP..
edamame22 0:29983394c6b6 269 * \param handler A network interface handler that is used by client to connect.
edamame22 0:29983394c6b6 270 * This API is optional but provides a mechanism for different platforms to
edamame22 0:29983394c6b6 271 * manage usage of underlying network interface by client.
edamame22 0:29983394c6b6 272 */
edamame22 0:29983394c6b6 273 void set_platform_network_handler(void *handler = NULL);
edamame22 0:29983394c6b6 274
edamame22 0:29983394c6b6 275 /**
edamame22 0:29983394c6b6 276 * \brief Claims mutex to prevent thread clashes
edamame22 0:29983394c6b6 277 * in multithreaded environment.
edamame22 0:29983394c6b6 278 */
edamame22 0:29983394c6b6 279 void claim_mutex();
edamame22 0:29983394c6b6 280
edamame22 0:29983394c6b6 281 /**
edamame22 0:29983394c6b6 282 * \brief Releases mutex to prevent thread clashes
edamame22 0:29983394c6b6 283 * in multithreaded environment.
edamame22 0:29983394c6b6 284 */
edamame22 0:29983394c6b6 285 void release_mutex();
edamame22 0:29983394c6b6 286
edamame22 0:29983394c6b6 287 private:
edamame22 0:29983394c6b6 288
edamame22 0:29983394c6b6 289 M2MConnectionObserver &_observer;
edamame22 0:29983394c6b6 290 M2MConnectionHandlerPimpl *_private_impl;
edamame22 0:29983394c6b6 291
edamame22 0:29983394c6b6 292 friend class Test_M2MConnectionHandler;
edamame22 0:29983394c6b6 293 friend class Test_M2MConnectionHandler_mbed;
edamame22 0:29983394c6b6 294 friend class Test_M2MConnectionHandler_linux;
edamame22 0:29983394c6b6 295 friend class M2MConnection_TestObserver;
edamame22 0:29983394c6b6 296 };
edamame22 0:29983394c6b6 297
edamame22 0:29983394c6b6 298 #endif //M2M_CONNECTION_HANDLER_H__
edamame22 0:29983394c6b6 299
edamame22 0:29983394c6b6 300 ```
edamame22 0:29983394c6b6 301
edamame22 0:29983394c6b6 302 Please note that some of these functions are asynchronous in nature and some are expecting a callback from the network. For example, receiving data from a socket needs to be communicated back to `mbed-client` so that the library can act on the data received. The callback comes through the Observer class defined in `M2MConnectionObserver`.
edamame22 0:29983394c6b6 303
edamame22 0:29983394c6b6 304 The file `m2mconnectionobserver.h` is present in `mbed-client`. To see how the callback needs to be called, check the implementation in `m2mconnectionhandlerpimpl.cpp` in `mbed-client-linux`.
edamame22 0:29983394c6b6 305
edamame22 0:29983394c6b6 306 ```
edamame22 0:29983394c6b6 307 /*
edamame22 0:29983394c6b6 308 * Copyright (c) 2015 ARM. All rights reserved.
edamame22 0:29983394c6b6 309 */
edamame22 0:29983394c6b6 310 #ifndef M2M_CONNECTION_OBSERVER_H__
edamame22 0:29983394c6b6 311 #define M2M_CONNECTION_OBSERVER_H__
edamame22 0:29983394c6b6 312
edamame22 0:29983394c6b6 313 #include "mbed-client/m2minterface.h"
edamame22 0:29983394c6b6 314
edamame22 0:29983394c6b6 315 /**
edamame22 0:29983394c6b6 316 * @brief Observer class for informing socket activity to the state machine
edamame22 0:29983394c6b6 317 */
edamame22 0:29983394c6b6 318
edamame22 0:29983394c6b6 319 class M2MConnectionObserver
edamame22 0:29983394c6b6 320 {
edamame22 0:29983394c6b6 321
edamame22 0:29983394c6b6 322 public :
edamame22 0:29983394c6b6 323
edamame22 0:29983394c6b6 324 /**
edamame22 0:29983394c6b6 325 * \enum ServerType, Defines the type of the
edamame22 0:29983394c6b6 326 * server that the client wants to use.
edamame22 0:29983394c6b6 327 */
edamame22 0:29983394c6b6 328 typedef enum {
edamame22 0:29983394c6b6 329 Bootstrap,
edamame22 0:29983394c6b6 330 LWM2MServer
edamame22 0:29983394c6b6 331 }ServerType;
edamame22 0:29983394c6b6 332
edamame22 0:29983394c6b6 333 /**
edamame22 0:29983394c6b6 334 * \brief The M2MSocketAddress struct.
edamame22 0:29983394c6b6 335 * Unified container for holding socket address data
edamame22 0:29983394c6b6 336 * across different platforms.
edamame22 0:29983394c6b6 337 */
edamame22 0:29983394c6b6 338 struct SocketAddress{
edamame22 0:29983394c6b6 339 M2MInterface::NetworkStack _stack;
edamame22 0:29983394c6b6 340 void *_address;
edamame22 0:29983394c6b6 341 uint8_t _length;
edamame22 0:29983394c6b6 342 uint16_t _port;
edamame22 0:29983394c6b6 343 };
edamame22 0:29983394c6b6 344
edamame22 0:29983394c6b6 345 /**
edamame22 0:29983394c6b6 346 * \brief Indicates that data is available from socket.
edamame22 0:29983394c6b6 347 * \param data The data read from the socket.
edamame22 0:29983394c6b6 348 * \param data_size The length of the data read from the socket.
edamame22 0:29983394c6b6 349 * \param address The address of the server where the data is coming from.
edamame22 0:29983394c6b6 350 */
edamame22 0:29983394c6b6 351 virtual void data_available(uint8_t* data,
edamame22 0:29983394c6b6 352 uint16_t data_size,
edamame22 0:29983394c6b6 353 const M2MConnectionObserver::SocketAddress &address) = 0;
edamame22 0:29983394c6b6 354
edamame22 0:29983394c6b6 355 /**
edamame22 0:29983394c6b6 356 * \brief Indicates an error occured in socket.
edamame22 0:29983394c6b6 357 * \param error_code The error code from socket, it cannot be used any further.
edamame22 0:29983394c6b6 358 * \param retry Indicates whether to re-establish connection.
edamame22 0:29983394c6b6 359 */
edamame22 0:29983394c6b6 360 virtual void socket_error(uint8_t error_code, bool retry = true) = 0;
edamame22 0:29983394c6b6 361
edamame22 0:29983394c6b6 362 /**
edamame22 0:29983394c6b6 363 * \brief Indicates that the server address resolving is ready.
edamame22 0:29983394c6b6 364 * \param address The resolved socket address.
edamame22 0:29983394c6b6 365 * \param server_type The type of the server.
edamame22 0:29983394c6b6 366 * \param server_port The port of the resolved server address.
edamame22 0:29983394c6b6 367 */
edamame22 0:29983394c6b6 368 virtual void address_ready(const M2MConnectionObserver::SocketAddress &address,
edamame22 0:29983394c6b6 369 M2MConnectionObserver::ServerType server_type,
edamame22 0:29983394c6b6 370 const uint16_t server_port) = 0;
edamame22 0:29983394c6b6 371
edamame22 0:29983394c6b6 372 /**
edamame22 0:29983394c6b6 373 * \brief Indicates that data has been sent successfully.
edamame22 0:29983394c6b6 374 */
edamame22 0:29983394c6b6 375 virtual void data_sent() = 0;
edamame22 0:29983394c6b6 376 };
edamame22 0:29983394c6b6 377
edamame22 0:29983394c6b6 378 #endif // M2M_CONNECTION_OBSERVER_H__
edamame22 0:29983394c6b6 379
edamame22 0:29983394c6b6 380 ```
edamame22 0:29983394c6b6 381
edamame22 0:29983394c6b6 382 ### Implementing M2MTimer class for your platform
edamame22 0:29983394c6b6 383
edamame22 0:29983394c6b6 384 This class provides the periodic timer functionality for your platform.
edamame22 0:29983394c6b6 385
edamame22 0:29983394c6b6 386 ```
edamame22 0:29983394c6b6 387 /*
edamame22 0:29983394c6b6 388 * Copyright (c) 2015 ARM. All rights reserved.
edamame22 0:29983394c6b6 389 */
edamame22 0:29983394c6b6 390 #ifndef M2M_TIMER_H
edamame22 0:29983394c6b6 391 #define M2M_TIMER_H
edamame22 0:29983394c6b6 392
edamame22 0:29983394c6b6 393 #include <stdint.h>
edamame22 0:29983394c6b6 394
edamame22 0:29983394c6b6 395 class M2MTimerObserver;
edamame22 0:29983394c6b6 396 /**
edamame22 0:29983394c6b6 397 * @brief M2MTimerImpl
edamame22 0:29983394c6b6 398 * Private implementation class for timer, this can be
edamame22 0:29983394c6b6 399 * modified based on the board on which mbed Client needs
edamame22 0:29983394c6b6 400 * to be used.
edamame22 0:29983394c6b6 401 */
edamame22 0:29983394c6b6 402 class M2MTimerImpl
edamame22 0:29983394c6b6 403 {
edamame22 0:29983394c6b6 404 private:
edamame22 0:29983394c6b6 405
edamame22 0:29983394c6b6 406 // Prevents the use of assignment operator
edamame22 0:29983394c6b6 407 M2MTimer& operator=(const M2MTimer& other);
edamame22 0:29983394c6b6 408
edamame22 0:29983394c6b6 409 // Prevents the use of copy constructor
edamame22 0:29983394c6b6 410 M2MTimer(const M2MTimer& other);
edamame22 0:29983394c6b6 411
edamame22 0:29983394c6b6 412 public:
edamame22 0:29983394c6b6 413
edamame22 0:29983394c6b6 414 /**
edamame22 0:29983394c6b6 415 * Constructor.
edamame22 0:29983394c6b6 416 */
edamame22 0:29983394c6b6 417 M2MTimer(M2MTimerObserver& _observer);
edamame22 0:29983394c6b6 418
edamame22 0:29983394c6b6 419 /**
edamame22 0:29983394c6b6 420 * Destructor.
edamame22 0:29983394c6b6 421 */
edamame22 0:29983394c6b6 422 virtual ~M2MTimer();
edamame22 0:29983394c6b6 423
edamame22 0:29983394c6b6 424 /**
edamame22 0:29983394c6b6 425 * Starts timer
edamame22 0:29983394c6b6 426 * @param interval Timer's interval in milliseconds
edamame22 0:29983394c6b6 427 * @param single_shot defines if timer is ticked
edamame22 0:29983394c6b6 428 * once or is it restarted everytime timer expires.
edamame22 0:29983394c6b6 429 */
edamame22 0:29983394c6b6 430 void start_timer(uint64_t interval, bool single_shot = true);
edamame22 0:29983394c6b6 431
edamame22 0:29983394c6b6 432 /**
edamame22 0:29983394c6b6 433 * @brief Starts timer in DTLS manner.
edamame22 0:29983394c6b6 434 * @param intermediate_interval Intermediate interval to use, must be smaller than tiotal (usually 1/4 of total).
edamame22 0:29983394c6b6 435 * @param total_interval Total interval to use, this is the timeout value of a DTLS packet.
edamame22 0:29983394c6b6 436 * @param type Type of the timer
edamame22 0:29983394c6b6 437 */
edamame22 0:29983394c6b6 438 void start_dtls_timer(uint64_t intermediate_interval, uint64_t total_interval,
edamame22 0:29983394c6b6 439 M2MTimerObserver::Type type = M2MTimerObserver::Dtls);
edamame22 0:29983394c6b6 440
edamame22 0:29983394c6b6 441 /**
edamame22 0:29983394c6b6 442 * Stops timer.
edamame22 0:29983394c6b6 443 * This cancels the ongoing timer.
edamame22 0:29983394c6b6 444 */
edamame22 0:29983394c6b6 445 void stop_timer();
edamame22 0:29983394c6b6 446
edamame22 0:29983394c6b6 447 /**
edamame22 0:29983394c6b6 448 * @brief Checks if the intermediate interval has passed.
edamame22 0:29983394c6b6 449 * @return true if interval has passed, false otherwise.
edamame22 0:29983394c6b6 450 */
edamame22 0:29983394c6b6 451 bool is_intermediate_interval_passed();
edamame22 0:29983394c6b6 452
edamame22 0:29983394c6b6 453 /**
edamame22 0:29983394c6b6 454 * @brief Checks if the total interval has passed.
edamame22 0:29983394c6b6 455 * @return true if interval has passed, false otherwise.
edamame22 0:29983394c6b6 456 */
edamame22 0:29983394c6b6 457 bool is_total_interval_passed();
edamame22 0:29983394c6b6 458
edamame22 0:29983394c6b6 459 };
edamame22 0:29983394c6b6 460
edamame22 0:29983394c6b6 461 #endif // M2M_TIMER_H
edamame22 0:29983394c6b6 462 ```
edamame22 0:29983394c6b6 463
edamame22 0:29983394c6b6 464 The timer API functions are asynchronous in nature and whenever a timer event is available, `mbed-client` is notified, so that the library can act on the _timer expired_ signal. The callback is received through an Observer class defined in `M2MTimerObserver` .
edamame22 0:29983394c6b6 465
edamame22 0:29983394c6b6 466 The file `m2mtimerobserver.h` is present in `mbed-client`. To see how the callback needs to be called, check the implementation in `m2mtimerimpl.cpp` in `mbed-client-linux`.
edamame22 0:29983394c6b6 467
edamame22 0:29983394c6b6 468 ```
edamame22 0:29983394c6b6 469 /*
edamame22 0:29983394c6b6 470 * Copyright (c) 2015 ARM. All rights reserved.
edamame22 0:29983394c6b6 471 */
edamame22 0:29983394c6b6 472 #ifndef M2M_TIMER_OBSERVER_H
edamame22 0:29983394c6b6 473 #define M2M_TIMER_OBSERVER_H
edamame22 0:29983394c6b6 474
edamame22 0:29983394c6b6 475 /**
edamame22 0:29983394c6b6 476 * Observer class for informing the parent class of the timer expiry.
edamame22 0:29983394c6b6 477 */
edamame22 0:29983394c6b6 478 class M2MTimerObserver
edamame22 0:29983394c6b6 479 {
edamame22 0:29983394c6b6 480 public:
edamame22 0:29983394c6b6 481 /**
edamame22 0:29983394c6b6 482 * \enum Defines the types of timer
edamame22 0:29983394c6b6 483 * that can be created for mbed Client.
edamame22 0:29983394c6b6 484 */
edamame22 0:29983394c6b6 485 typedef enum {
edamame22 0:29983394c6b6 486 Notdefined,
edamame22 0:29983394c6b6 487 Registration,
edamame22 0:29983394c6b6 488 NsdlExecution,
edamame22 0:29983394c6b6 489 PMinTimer,
edamame22 0:29983394c6b6 490 PMaxTimer,
edamame22 0:29983394c6b6 491 Dtls,
edamame22 0:29983394c6b6 492 QueueSleep,
edamame22 0:29983394c6b6 493 RetryTimer
edamame22 0:29983394c6b6 494 }Type;
edamame22 0:29983394c6b6 495
edamame22 0:29983394c6b6 496 /**
edamame22 0:29983394c6b6 497 * \brief Indicates that the timer has expired.
edamame22 0:29983394c6b6 498 * \param type The type of the timer that has expired.
edamame22 0:29983394c6b6 499 */
edamame22 0:29983394c6b6 500 virtual void timer_expired(M2MTimerObserver::Type type =
edamame22 0:29983394c6b6 501 M2MTimerObserver::Notdefined) = 0;
edamame22 0:29983394c6b6 502 };
edamame22 0:29983394c6b6 503
edamame22 0:29983394c6b6 504 #endif // M2M_TIMER_OBSERVER_H
edamame22 0:29983394c6b6 505 ```
edamame22 0:29983394c6b6 506
edamame22 0:29983394c6b6 507 ## Modifying the `json` file in the `mbed-client` module
edamame22 0:29983394c6b6 508
edamame22 0:29983394c6b6 509 You need to add your target name to `module.json` so that when you set `yt target <platform>`, yotta can resolve the dependency correctly and link the main library with your module.
edamame22 0:29983394c6b6 510
edamame22 0:29983394c6b6 511 Two platforms, mbed OS and Linux, are already supported. You just need to add your module support after that.
edamame22 0:29983394c6b6 512
edamame22 0:29983394c6b6 513 ```
edamame22 0:29983394c6b6 514 {
edamame22 0:29983394c6b6 515 "name": "mbed-client",
edamame22 0:29983394c6b6 516 "version": "1.12.0",
edamame22 0:29983394c6b6 517 "description": "Mbed Client API",
edamame22 0:29983394c6b6 518 "private": true,
edamame22 0:29983394c6b6 519 "keywords": [],
edamame22 0:29983394c6b6 520 "author": "XXX XXX <xxx.xxx@xxx.com>",
edamame22 0:29983394c6b6 521 "homepage": "https://github.com/ARMmbed/mbed-client",
edamame22 0:29983394c6b6 522 "licenses": [
edamame22 0:29983394c6b6 523 {
edamame22 0:29983394c6b6 524 "url": "https://spdx.org/licenses/Apache-2.0",
edamame22 0:29983394c6b6 525 "type": "Apache-2.0"
edamame22 0:29983394c6b6 526 }
edamame22 0:29983394c6b6 527 ],
edamame22 0:29983394c6b6 528 "dependencies": {
edamame22 0:29983394c6b6 529 "mbed-client-c": "^2.0.0"
edamame22 0:29983394c6b6 530 },
edamame22 0:29983394c6b6 531 "targetDependencies": {
edamame22 0:29983394c6b6 532 "arm": {
edamame22 0:29983394c6b6 533 "mbed-client-mbed": "^3.0.0"
edamame22 0:29983394c6b6 534 },
edamame22 0:29983394c6b6 535 "linux": {
edamame22 0:29983394c6b6 536 "mbed-client-linux": "^3.0.0"
edamame22 0:29983394c6b6 537 },
edamame22 0:29983394c6b6 538 "<your platform as defined in target.json>" : {
edamame22 0:29983394c6b6 539 "mbed-client-platform": "<published version , can be done later, first link locally as explained in the steps above>"
edamame22 0:29983394c6b6 540 },
edamame22 0:29983394c6b6 541 }
edamame22 0:29983394c6b6 542 }
edamame22 0:29983394c6b6 543 ```
edamame22 0:29983394c6b6 544
edamame22 0:29983394c6b6 545 ## Testing and verification
edamame22 0:29983394c6b6 546
edamame22 0:29983394c6b6 547 You can build your mbed-client port immediately:
edamame22 0:29983394c6b6 548
edamame22 0:29983394c6b6 549 ```
edamame22 0:29983394c6b6 550 # use the target we previously made locally available (not necessary if your target has been published):
edamame22 0:29983394c6b6 551 yotta link-target <yourtargetname>
edamame22 0:29983394c6b6 552 # build!
edamame22 0:29983394c6b6 553 yotta build
edamame22 0:29983394c6b6 554 ```
edamame22 0:29983394c6b6 555
edamame22 0:29983394c6b6 556 A `helloworld-mbedclient` program will be produced inside the `build/<yourtargetname>/test/` directory. This test application may require some changes to compile and run for your platform. Check for compilation errors. If you find any, fix the test application for your testing.
edamame22 0:29983394c6b6 557
edamame22 0:29983394c6b6 558 Follow the `readme` [instructions](https://github.com/ARMmbed/mbed-client-linux-example) of the `mbed-client-linux` example to see what the test application can do.