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) 2017 Renesas Electronics Corporation
chuanga 0:43ff9e3bc244 3 *
chuanga 0:43ff9e3bc244 4 * Licensed under the Apache License, Version 2.0 (the "License");
chuanga 0:43ff9e3bc244 5 * you may not use this file except in compliance with the License.
chuanga 0:43ff9e3bc244 6 * You may obtain a copy of the License at
chuanga 0:43ff9e3bc244 7 *
chuanga 0:43ff9e3bc244 8 * http://www.apache.org/licenses/LICENSE-2.0
chuanga 0:43ff9e3bc244 9 *
chuanga 0:43ff9e3bc244 10 * Unless required by applicable law or agreed to in writing, software
chuanga 0:43ff9e3bc244 11 * distributed under the License is distributed on an "AS IS" BASIS,
chuanga 0:43ff9e3bc244 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
chuanga 0:43ff9e3bc244 13 * See the License for the specific language governing permissions and
chuanga 0:43ff9e3bc244 14 * limitations under the License.
chuanga 0:43ff9e3bc244 15 */
chuanga 0:43ff9e3bc244 16
chuanga 0:43ff9e3bc244 17 #ifndef ESP32_INTERFACE_AP_H
chuanga 0:43ff9e3bc244 18 #define ESP32_INTERFACE_AP_H
chuanga 0:43ff9e3bc244 19
chuanga 0:43ff9e3bc244 20 #include "mbed.h"
chuanga 0:43ff9e3bc244 21 #include "ESP32Stack.h"
chuanga 0:43ff9e3bc244 22
chuanga 0:43ff9e3bc244 23
chuanga 0:43ff9e3bc244 24 /** ESP32Interface class
chuanga 0:43ff9e3bc244 25 * Implementation of the NetworkStack for the ESP32
chuanga 0:43ff9e3bc244 26 */
chuanga 0:43ff9e3bc244 27 class ESP32InterfaceAP : public ESP32Stack, public WiFiInterface
chuanga 0:43ff9e3bc244 28 {
chuanga 0:43ff9e3bc244 29 public:
chuanga 0:43ff9e3bc244 30 /** ESP32InterfaceAP lifetime
chuanga 0:43ff9e3bc244 31 * @param en EN pin (If not used this pin, please set "NC")
chuanga 0:43ff9e3bc244 32 * @param io0 IO0 pin (If not used this pin, please set "NC")
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 (default = 230400).
chuanga 0:43ff9e3bc244 39 */
chuanga 0:43ff9e3bc244 40 ESP32InterfaceAP(PinName en, PinName io0, PinName tx, PinName rx, bool debug = false,
chuanga 0:43ff9e3bc244 41 PinName rts = NC, PinName cts = NC, int baudrate = 230400);
chuanga 0:43ff9e3bc244 42
chuanga 0:43ff9e3bc244 43 /** ESP32InterfaceAP lifetime
chuanga 0:43ff9e3bc244 44 * @param tx TX pin
chuanga 0:43ff9e3bc244 45 * @param rx RX pin
chuanga 0:43ff9e3bc244 46 * @param debug Enable debugging
chuanga 0:43ff9e3bc244 47 */
chuanga 0:43ff9e3bc244 48 ESP32InterfaceAP(PinName tx, PinName rx, bool debug = false);
chuanga 0:43ff9e3bc244 49
chuanga 0:43ff9e3bc244 50 /** Set a static IP address
chuanga 0:43ff9e3bc244 51 *
chuanga 0:43ff9e3bc244 52 * Configures this network interface to use a static IP address.
chuanga 0:43ff9e3bc244 53 * Implicitly disables DHCP, which can be enabled in set_dhcp.
chuanga 0:43ff9e3bc244 54 * Requires that the network is disconnected.
chuanga 0:43ff9e3bc244 55 *
chuanga 0:43ff9e3bc244 56 * @param ip_address Null-terminated representation of the local IP address
chuanga 0:43ff9e3bc244 57 * @param netmask Null-terminated representation of the local network mask
chuanga 0:43ff9e3bc244 58 * @param gateway Null-terminated representation of the local gateway
chuanga 0:43ff9e3bc244 59 * @return 0 on success, negative error code on failure
chuanga 0:43ff9e3bc244 60 */
chuanga 0:43ff9e3bc244 61 virtual nsapi_error_t set_network(
chuanga 0:43ff9e3bc244 62 const char *ip_address, const char *netmask, const char *gateway);
chuanga 0:43ff9e3bc244 63
chuanga 0:43ff9e3bc244 64 /** Enable or disable DHCP on the network
chuanga 0:43ff9e3bc244 65 *
chuanga 0:43ff9e3bc244 66 * Enables DHCP on connecting the network. Defaults to enabled unless
chuanga 0:43ff9e3bc244 67 * a static IP address has been assigned. Requires that the network is
chuanga 0:43ff9e3bc244 68 * disconnected.
chuanga 0:43ff9e3bc244 69 *
chuanga 0:43ff9e3bc244 70 * @param dhcp True to enable DHCP
chuanga 0:43ff9e3bc244 71 * @return 0 on success, negative error code on failure
chuanga 0:43ff9e3bc244 72 */
chuanga 0:43ff9e3bc244 73 virtual nsapi_error_t set_dhcp(bool dhcp);
chuanga 0:43ff9e3bc244 74
chuanga 0:43ff9e3bc244 75 /** Start the interface
chuanga 0:43ff9e3bc244 76 *
chuanga 0:43ff9e3bc244 77 * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set.
chuanga 0:43ff9e3bc244 78 * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned.
chuanga 0:43ff9e3bc244 79 *
chuanga 0:43ff9e3bc244 80 * @return 0 on success, negative error code on failure
chuanga 0:43ff9e3bc244 81 */
chuanga 0:43ff9e3bc244 82 virtual int connect();
chuanga 0:43ff9e3bc244 83
chuanga 0:43ff9e3bc244 84 /** Start the interface
chuanga 0:43ff9e3bc244 85 *
chuanga 0:43ff9e3bc244 86 * Attempts to connect to a WiFi network.
chuanga 0:43ff9e3bc244 87 *
chuanga 0:43ff9e3bc244 88 * @param ssid Name of the network to connect to
chuanga 0:43ff9e3bc244 89 * @param pass Security passphrase to connect to the network
chuanga 0:43ff9e3bc244 90 * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE)
chuanga 0:43ff9e3bc244 91 * @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED
chuanga 0:43ff9e3bc244 92 * @return 0 on success, or error code on failure
chuanga 0:43ff9e3bc244 93 */
chuanga 0:43ff9e3bc244 94 virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE,
chuanga 0:43ff9e3bc244 95 uint8_t channel = 0);
chuanga 0:43ff9e3bc244 96
chuanga 0:43ff9e3bc244 97 /** Set the WiFi network credentials
chuanga 0:43ff9e3bc244 98 *
chuanga 0:43ff9e3bc244 99 * @param ssid Name of the network to connect to
chuanga 0:43ff9e3bc244 100 * @param pass Security passphrase to connect to the network
chuanga 0:43ff9e3bc244 101 * @param security Type of encryption for connection
chuanga 0:43ff9e3bc244 102 * (defaults to NSAPI_SECURITY_NONE)
chuanga 0:43ff9e3bc244 103 * @return 0 on success, or error code on failure
chuanga 0:43ff9e3bc244 104 */
chuanga 0:43ff9e3bc244 105 virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE);
chuanga 0:43ff9e3bc244 106
chuanga 0:43ff9e3bc244 107 /** Set the WiFi network channel - NOT SUPPORTED
chuanga 0:43ff9e3bc244 108 *
chuanga 0:43ff9e3bc244 109 * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED
chuanga 0:43ff9e3bc244 110 *
chuanga 0:43ff9e3bc244 111 * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0)
chuanga 0:43ff9e3bc244 112 * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED
chuanga 0:43ff9e3bc244 113 */
chuanga 0:43ff9e3bc244 114 virtual int set_channel(uint8_t channel);
chuanga 0:43ff9e3bc244 115
chuanga 0:43ff9e3bc244 116 /** Stop the interface
chuanga 0:43ff9e3bc244 117 * @return 0 on success, negative on failure
chuanga 0:43ff9e3bc244 118 */
chuanga 0:43ff9e3bc244 119 virtual int disconnect();
chuanga 0:43ff9e3bc244 120
chuanga 0:43ff9e3bc244 121 /** Get the internally stored IP address
chuanga 0:43ff9e3bc244 122 * @return IP address of the interface or null if not yet connected
chuanga 0:43ff9e3bc244 123 */
chuanga 0:43ff9e3bc244 124 virtual const char *get_ip_address();
chuanga 0:43ff9e3bc244 125
chuanga 0:43ff9e3bc244 126 /** Get the internally stored MAC address
chuanga 0:43ff9e3bc244 127 * @return MAC address of the interface
chuanga 0:43ff9e3bc244 128 */
chuanga 0:43ff9e3bc244 129 virtual const char *get_mac_address();
chuanga 0:43ff9e3bc244 130
chuanga 0:43ff9e3bc244 131 /** Get the local gateway
chuanga 0:43ff9e3bc244 132 *
chuanga 0:43ff9e3bc244 133 * @return Null-terminated representation of the local gateway
chuanga 0:43ff9e3bc244 134 * or null if no network mask has been recieved
chuanga 0:43ff9e3bc244 135 */
chuanga 0:43ff9e3bc244 136 virtual const char *get_gateway();
chuanga 0:43ff9e3bc244 137
chuanga 0:43ff9e3bc244 138 /** Get the local network mask
chuanga 0:43ff9e3bc244 139 *
chuanga 0:43ff9e3bc244 140 * @return Null-terminated representation of the local network mask
chuanga 0:43ff9e3bc244 141 * or null if no network mask has been recieved
chuanga 0:43ff9e3bc244 142 */
chuanga 0:43ff9e3bc244 143 virtual const char *get_netmask();
chuanga 0:43ff9e3bc244 144
chuanga 0:43ff9e3bc244 145 /** Gets the current radio signal strength for active connection
chuanga 0:43ff9e3bc244 146 *
chuanga 0:43ff9e3bc244 147 * @return Connection strength in dBm (negative value)
chuanga 0:43ff9e3bc244 148 */
chuanga 0:43ff9e3bc244 149 virtual int8_t get_rssi();
chuanga 0:43ff9e3bc244 150
chuanga 0:43ff9e3bc244 151 /** Scan for available networks
chuanga 0:43ff9e3bc244 152 *
chuanga 0:43ff9e3bc244 153 * This function will block.
chuanga 0:43ff9e3bc244 154 *
chuanga 0:43ff9e3bc244 155 * @param ap Pointer to allocated array to store discovered AP
chuanga 0:43ff9e3bc244 156 * @param count Size of allocated @a res array, or 0 to only count available AP
chuanga 0:43ff9e3bc244 157 * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0)
chuanga 0:43ff9e3bc244 158 * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
chuanga 0:43ff9e3bc244 159 * see @a nsapi_error
chuanga 0:43ff9e3bc244 160 */
chuanga 0:43ff9e3bc244 161 virtual int scan(WiFiAccessPoint *res, unsigned count);
chuanga 0:43ff9e3bc244 162
chuanga 0:43ff9e3bc244 163 /** Translates a hostname to an IP address with specific version
chuanga 0:43ff9e3bc244 164 *
chuanga 0:43ff9e3bc244 165 * The hostname may be either a domain name or an IP address. If the
chuanga 0:43ff9e3bc244 166 * hostname is an IP address, no network transactions will be performed.
chuanga 0:43ff9e3bc244 167 *
chuanga 0:43ff9e3bc244 168 * If no stack-specific DNS resolution is provided, the hostname
chuanga 0:43ff9e3bc244 169 * will be resolve using a UDP socket on the stack.
chuanga 0:43ff9e3bc244 170 *
chuanga 0:43ff9e3bc244 171 * @param address Destination for the host SocketAddress
chuanga 0:43ff9e3bc244 172 * @param host Hostname to resolve
chuanga 0:43ff9e3bc244 173 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates
chuanga 0:43ff9e3bc244 174 * version is chosen by the stack (defaults to NSAPI_UNSPEC)
chuanga 0:43ff9e3bc244 175 * @return 0 on success, negative error code on failure
chuanga 0:43ff9e3bc244 176 */
chuanga 0:43ff9e3bc244 177 using NetworkInterface::gethostbyname;
chuanga 0:43ff9e3bc244 178
chuanga 0:43ff9e3bc244 179 /** Add a domain name server to list of servers to query
chuanga 0:43ff9e3bc244 180 *
chuanga 0:43ff9e3bc244 181 * @param addr Destination for the host address
chuanga 0:43ff9e3bc244 182 * @return 0 on success, negative error code on failure
chuanga 0:43ff9e3bc244 183 */
chuanga 0:43ff9e3bc244 184 using NetworkInterface::add_dns_server;
chuanga 0:43ff9e3bc244 185
chuanga 0:43ff9e3bc244 186 /** Register callback for status reporting
chuanga 0:43ff9e3bc244 187 *
chuanga 0:43ff9e3bc244 188 * The specified status callback function will be called on status changes
chuanga 0:43ff9e3bc244 189 * on the network. The parameters on the callback are the event type and
chuanga 0:43ff9e3bc244 190 * event-type dependent reason parameter.
chuanga 0:43ff9e3bc244 191 *
chuanga 0:43ff9e3bc244 192 * In ESP32 the callback will be called when processing OOB-messages via
chuanga 0:43ff9e3bc244 193 * AT-parser. Do NOT call any ESP8266Interface -functions or do extensive
chuanga 0:43ff9e3bc244 194 * processing in the callback.
chuanga 0:43ff9e3bc244 195 *
chuanga 0:43ff9e3bc244 196 * @param status_cb The callback for status changes
chuanga 0:43ff9e3bc244 197 */
chuanga 0:43ff9e3bc244 198 virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
chuanga 0:43ff9e3bc244 199
chuanga 0:43ff9e3bc244 200 /** Get the connection status
chuanga 0:43ff9e3bc244 201 *
chuanga 0:43ff9e3bc244 202 * @return The connection status according to ConnectionStatusType
chuanga 0:43ff9e3bc244 203 */
chuanga 0:43ff9e3bc244 204 virtual nsapi_connection_status_t get_connection_status() const;
chuanga 0:43ff9e3bc244 205
chuanga 0:43ff9e3bc244 206
chuanga 0:43ff9e3bc244 207 /** Provide access to the NetworkStack object
chuanga 0:43ff9e3bc244 208 *
chuanga 0:43ff9e3bc244 209 * @return The underlying NetworkStack object
chuanga 0:43ff9e3bc244 210 */
chuanga 0:43ff9e3bc244 211 virtual NetworkStack *get_stack()
chuanga 0:43ff9e3bc244 212 {
chuanga 0:43ff9e3bc244 213 return this;
chuanga 0:43ff9e3bc244 214 }
chuanga 0:43ff9e3bc244 215
chuanga 0:43ff9e3bc244 216 private:
chuanga 0:43ff9e3bc244 217 bool _dhcp;
chuanga 0:43ff9e3bc244 218 uint8_t _own_ch;
chuanga 0:43ff9e3bc244 219 char _own_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
chuanga 0:43ff9e3bc244 220 char _own_pass[64]; /* The longest allowed passphrase */
chuanga 0:43ff9e3bc244 221 nsapi_security_t _own_sec;
chuanga 0:43ff9e3bc244 222 char _ip_address[NSAPI_IPv6_SIZE];
chuanga 0:43ff9e3bc244 223 char _netmask[NSAPI_IPv4_SIZE];
chuanga 0:43ff9e3bc244 224 char _gateway[NSAPI_IPv4_SIZE];
chuanga 0:43ff9e3bc244 225 nsapi_connection_status_t _connection_status;
chuanga 0:43ff9e3bc244 226 Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
chuanga 0:43ff9e3bc244 227 };
chuanga 0:43ff9e3bc244 228
chuanga 0:43ff9e3bc244 229 #endif