Generic Pelion Device Management example for various Advantech modules.

This example is known to work great on the following platforms:

Example Functionality

This example showcases the following device functionality:

  • On timer button increment, simulate Pelion LWM2M button resource change

Use this example with Mbed CLI

1. Import the application into your desktop:

mbed import https://os.mbed.com/teams/Advantech/code/pelion-example-common
cd pelion-example-common

2. Download your developer certificate from pelion portal

3. Compile the program

mbed compile -t <toolchain> -m <TARGET_BOARD>

(supported toolchains : GCC_ARM / ARM / IAR)

4. Copy the binary file pelion-example-common.bin to your mbed device.

Committer:
chuanga
Date:
Tue Mar 12 13:48:39 2019 +0800
Revision:
0:43ff9e3bc244
copying sources from github repository

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chuanga 0:43ff9e3bc244 1 /* ESP32 implementation of NetworkInterfaceAPI
chuanga 0:43ff9e3bc244 2 * Copyright (c) 2015 ARM Limited
chuanga 0:43ff9e3bc244 3 * Copyright (c) 2017 Renesas Electronics Corporation
chuanga 0:43ff9e3bc244 4 *
chuanga 0:43ff9e3bc244 5 * Licensed under the Apache License, Version 2.0 (the "License");
chuanga 0:43ff9e3bc244 6 * you may not use this file except in compliance with the License.
chuanga 0:43ff9e3bc244 7 * You may obtain a copy of the License at
chuanga 0:43ff9e3bc244 8 *
chuanga 0:43ff9e3bc244 9 * http://www.apache.org/licenses/LICENSE-2.0
chuanga 0:43ff9e3bc244 10 *
chuanga 0:43ff9e3bc244 11 * Unless required by applicable law or agreed to in writing, software
chuanga 0:43ff9e3bc244 12 * distributed under the License is distributed on an "AS IS" BASIS,
chuanga 0:43ff9e3bc244 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
chuanga 0:43ff9e3bc244 14 * See the License for the specific language governing permissions and
chuanga 0:43ff9e3bc244 15 * limitations under the License.
chuanga 0:43ff9e3bc244 16 */
chuanga 0:43ff9e3bc244 17
chuanga 0:43ff9e3bc244 18 #ifndef ESP32_STACK_H
chuanga 0:43ff9e3bc244 19 #define ESP32_STACK_H
chuanga 0:43ff9e3bc244 20
chuanga 0:43ff9e3bc244 21 #include "mbed.h"
chuanga 0:43ff9e3bc244 22 #include "ESP32.h"
chuanga 0:43ff9e3bc244 23
chuanga 0:43ff9e3bc244 24 /** ESP32Stack class
chuanga 0:43ff9e3bc244 25 * Implementation of the NetworkStack for the ESP32
chuanga 0:43ff9e3bc244 26 */
chuanga 0:43ff9e3bc244 27 class ESP32Stack : public NetworkStack
chuanga 0:43ff9e3bc244 28 {
chuanga 0:43ff9e3bc244 29 protected:
chuanga 0:43ff9e3bc244 30 /** ESP32Stack lifetime
chuanga 0:43ff9e3bc244 31 * @param en EN pin
chuanga 0:43ff9e3bc244 32 * @param io0 IO0 pin
chuanga 0:43ff9e3bc244 33 * @param tx TX pin
chuanga 0:43ff9e3bc244 34 * @param rx RX pin
chuanga 0:43ff9e3bc244 35 * @param debug Enable debugging
chuanga 0:43ff9e3bc244 36 * @param rts RTS pin
chuanga 0:43ff9e3bc244 37 * @param cts CTS pin
chuanga 0:43ff9e3bc244 38 * @param baudrate The baudrate of the serial port.
chuanga 0:43ff9e3bc244 39 */
chuanga 0:43ff9e3bc244 40 ESP32Stack(PinName en, PinName io0, PinName tx, PinName rx, bool debug,
chuanga 0:43ff9e3bc244 41 PinName rts, PinName cts, int baudrate);
chuanga 0:43ff9e3bc244 42
chuanga 0:43ff9e3bc244 43 protected:
chuanga 0:43ff9e3bc244 44 /** Open a socket
chuanga 0:43ff9e3bc244 45 * @param handle Handle in which to store new socket
chuanga 0:43ff9e3bc244 46 * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP
chuanga 0:43ff9e3bc244 47 * @return 0 on success, negative on failure
chuanga 0:43ff9e3bc244 48 */
chuanga 0:43ff9e3bc244 49 virtual int socket_open(void **handle, nsapi_protocol_t proto);
chuanga 0:43ff9e3bc244 50
chuanga 0:43ff9e3bc244 51 /** Close the socket
chuanga 0:43ff9e3bc244 52 * @param handle Socket handle
chuanga 0:43ff9e3bc244 53 * @return 0 on success, negative on failure
chuanga 0:43ff9e3bc244 54 * @note On failure, any memory associated with the socket must still
chuanga 0:43ff9e3bc244 55 * be cleaned up
chuanga 0:43ff9e3bc244 56 */
chuanga 0:43ff9e3bc244 57 virtual int socket_close(void *handle);
chuanga 0:43ff9e3bc244 58
chuanga 0:43ff9e3bc244 59 /** Bind a server socket to a specific port
chuanga 0:43ff9e3bc244 60 * @param handle Socket handle
chuanga 0:43ff9e3bc244 61 * @param address Local address to listen for incoming connections on
chuanga 0:43ff9e3bc244 62 * @return 0 on success, negative on failure.
chuanga 0:43ff9e3bc244 63 */
chuanga 0:43ff9e3bc244 64 virtual int socket_bind(void *handle, const SocketAddress &address);
chuanga 0:43ff9e3bc244 65
chuanga 0:43ff9e3bc244 66 /** Start listening for incoming connections
chuanga 0:43ff9e3bc244 67 * @param handle Socket handle
chuanga 0:43ff9e3bc244 68 * @param backlog Number of pending connections that can be queued up at any
chuanga 0:43ff9e3bc244 69 * one time [Default: 1]
chuanga 0:43ff9e3bc244 70 * @return 0 on success, negative on failure
chuanga 0:43ff9e3bc244 71 */
chuanga 0:43ff9e3bc244 72 virtual int socket_listen(void *handle, int backlog);
chuanga 0:43ff9e3bc244 73
chuanga 0:43ff9e3bc244 74 /** Connects this TCP socket to the server
chuanga 0:43ff9e3bc244 75 * @param handle Socket handle
chuanga 0:43ff9e3bc244 76 * @param address SocketAddress to connect to
chuanga 0:43ff9e3bc244 77 * @return 0 on success, negative on failure
chuanga 0:43ff9e3bc244 78 */
chuanga 0:43ff9e3bc244 79 virtual int socket_connect(void *handle, const SocketAddress &address);
chuanga 0:43ff9e3bc244 80
chuanga 0:43ff9e3bc244 81 /** Accept a new connection.
chuanga 0:43ff9e3bc244 82 * @param handle Handle in which to store new socket
chuanga 0:43ff9e3bc244 83 * @param server Socket handle to server to accept from
chuanga 0:43ff9e3bc244 84 * @return 0 on success, negative on failure
chuanga 0:43ff9e3bc244 85 * @note This call is not-blocking, if this call would block, must
chuanga 0:43ff9e3bc244 86 * immediately return NSAPI_ERROR_WOULD_WAIT
chuanga 0:43ff9e3bc244 87 */
chuanga 0:43ff9e3bc244 88 virtual int socket_accept(void *handle, void **socket, SocketAddress *address);
chuanga 0:43ff9e3bc244 89
chuanga 0:43ff9e3bc244 90 /** Send data to the remote host
chuanga 0:43ff9e3bc244 91 * @param handle Socket handle
chuanga 0:43ff9e3bc244 92 * @param data The buffer to send to the host
chuanga 0:43ff9e3bc244 93 * @param size The length of the buffer to send
chuanga 0:43ff9e3bc244 94 * @return Number of written bytes on success, negative on failure
chuanga 0:43ff9e3bc244 95 * @note This call is not-blocking, if this call would block, must
chuanga 0:43ff9e3bc244 96 * immediately return NSAPI_ERROR_WOULD_WAIT
chuanga 0:43ff9e3bc244 97 */
chuanga 0:43ff9e3bc244 98 virtual int socket_send(void *handle, const void *data, unsigned size);
chuanga 0:43ff9e3bc244 99
chuanga 0:43ff9e3bc244 100 /** Receive data from the remote host
chuanga 0:43ff9e3bc244 101 * @param handle Socket handle
chuanga 0:43ff9e3bc244 102 * @param data The buffer in which to store the data received from the host
chuanga 0:43ff9e3bc244 103 * @param size The maximum length of the buffer
chuanga 0:43ff9e3bc244 104 * @return Number of received bytes on success, negative on failure
chuanga 0:43ff9e3bc244 105 * @note This call is not-blocking, if this call would block, must
chuanga 0:43ff9e3bc244 106 * immediately return NSAPI_ERROR_WOULD_WAIT
chuanga 0:43ff9e3bc244 107 */
chuanga 0:43ff9e3bc244 108 virtual int socket_recv(void *handle, void *data, unsigned size);
chuanga 0:43ff9e3bc244 109
chuanga 0:43ff9e3bc244 110 /** Send a packet to a remote endpoint
chuanga 0:43ff9e3bc244 111 * @param handle Socket handle
chuanga 0:43ff9e3bc244 112 * @param address The remote SocketAddress
chuanga 0:43ff9e3bc244 113 * @param data The packet to be sent
chuanga 0:43ff9e3bc244 114 * @param size The length of the packet to be sent
chuanga 0:43ff9e3bc244 115 * @return The number of written bytes on success, negative on failure
chuanga 0:43ff9e3bc244 116 * @note This call is not-blocking, if this call would block, must
chuanga 0:43ff9e3bc244 117 * immediately return NSAPI_ERROR_WOULD_WAIT
chuanga 0:43ff9e3bc244 118 */
chuanga 0:43ff9e3bc244 119 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
chuanga 0:43ff9e3bc244 120
chuanga 0:43ff9e3bc244 121 /** Receive a packet from a remote endpoint
chuanga 0:43ff9e3bc244 122 * @param handle Socket handle
chuanga 0:43ff9e3bc244 123 * @param address Destination for the remote SocketAddress or null
chuanga 0:43ff9e3bc244 124 * @param buffer The buffer for storing the incoming packet data
chuanga 0:43ff9e3bc244 125 * If a packet is too long to fit in the supplied buffer,
chuanga 0:43ff9e3bc244 126 * excess bytes are discarded
chuanga 0:43ff9e3bc244 127 * @param size The length of the buffer
chuanga 0:43ff9e3bc244 128 * @return The number of received bytes on success, negative on failure
chuanga 0:43ff9e3bc244 129 * @note This call is not-blocking, if this call would block, must
chuanga 0:43ff9e3bc244 130 * immediately return NSAPI_ERROR_WOULD_WAIT
chuanga 0:43ff9e3bc244 131 */
chuanga 0:43ff9e3bc244 132 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
chuanga 0:43ff9e3bc244 133
chuanga 0:43ff9e3bc244 134 /** Register a callback on state change of the socket
chuanga 0:43ff9e3bc244 135 * @param handle Socket handle
chuanga 0:43ff9e3bc244 136 * @param callback Function to call on state change
chuanga 0:43ff9e3bc244 137 * @param data Argument to pass to callback
chuanga 0:43ff9e3bc244 138 * @note Callback may be called in an interrupt context.
chuanga 0:43ff9e3bc244 139 */
chuanga 0:43ff9e3bc244 140 virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
chuanga 0:43ff9e3bc244 141
chuanga 0:43ff9e3bc244 142 /** Set stack-specific socket options
chuanga 0:43ff9e3bc244 143 * The setsockopt allow an application to pass stack-specific hints
chuanga 0:43ff9e3bc244 144 * to the underlying stack. For unsupported options,
chuanga 0:43ff9e3bc244 145 * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified.
chuanga 0:43ff9e3bc244 146 *
chuanga 0:43ff9e3bc244 147 * @param handle Socket handle
chuanga 0:43ff9e3bc244 148 * @param level Stack-specific protocol level
chuanga 0:43ff9e3bc244 149 * @param optname Stack-specific option identifier
chuanga 0:43ff9e3bc244 150 * @param optval Option value
chuanga 0:43ff9e3bc244 151 * @param optlen Length of the option value
chuanga 0:43ff9e3bc244 152 * @return 0 on success, negative error code on failure
chuanga 0:43ff9e3bc244 153 */
chuanga 0:43ff9e3bc244 154 virtual nsapi_error_t setsockopt(nsapi_socket_t handle, int level,
chuanga 0:43ff9e3bc244 155 int optname, const void *optval, unsigned optlen);
chuanga 0:43ff9e3bc244 156
chuanga 0:43ff9e3bc244 157 /** Get stack-specific socket options
chuanga 0:43ff9e3bc244 158 * The getstackopt allow an application to retrieve stack-specific hints
chuanga 0:43ff9e3bc244 159 * from the underlying stack. For unsupported options,
chuanga 0:43ff9e3bc244 160 * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified.
chuanga 0:43ff9e3bc244 161 *
chuanga 0:43ff9e3bc244 162 * @param handle Socket handle
chuanga 0:43ff9e3bc244 163 * @param level Stack-specific protocol level
chuanga 0:43ff9e3bc244 164 * @param optname Stack-specific option identifier
chuanga 0:43ff9e3bc244 165 * @param optval Destination for option value
chuanga 0:43ff9e3bc244 166 * @param optlen Length of the option value
chuanga 0:43ff9e3bc244 167 * @return 0 on success, negative error code on failure
chuanga 0:43ff9e3bc244 168 */
chuanga 0:43ff9e3bc244 169 virtual nsapi_error_t getsockopt(nsapi_socket_t handle, int level,
chuanga 0:43ff9e3bc244 170 int optname, void *optval, unsigned *optlen);
chuanga 0:43ff9e3bc244 171
chuanga 0:43ff9e3bc244 172 protected:
chuanga 0:43ff9e3bc244 173 ESP32 *_esp;
chuanga 0:43ff9e3bc244 174 uint16_t _local_ports[ESP32::SOCKET_COUNT];
chuanga 0:43ff9e3bc244 175 };
chuanga 0:43ff9e3bc244 176
chuanga 0:43ff9e3bc244 177 #endif