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 /* ISM43362Interface Example
chuanga 0:43ff9e3bc244 2 * Copyright (c) STMicroelectronics 2017
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 ISM43362_H
chuanga 0:43ff9e3bc244 18 #define ISM43362_H
chuanga 0:43ff9e3bc244 19 #include "ATParser.h"
chuanga 0:43ff9e3bc244 20
chuanga 0:43ff9e3bc244 21 #define ES_WIFI_MAX_SSID_NAME_SIZE 32
chuanga 0:43ff9e3bc244 22 #define ES_WIFI_MAX_PSWD_NAME_SIZE 32
chuanga 0:43ff9e3bc244 23 #define ES_WIFI_PRODUCT_ID_SIZE 32
chuanga 0:43ff9e3bc244 24 #define ES_WIFI_PRODUCT_NAME_SIZE 32
chuanga 0:43ff9e3bc244 25 #define ES_WIFI_FW_REV_SIZE 16
chuanga 0:43ff9e3bc244 26 #define ES_WIFI_API_REV_SIZE 16
chuanga 0:43ff9e3bc244 27 #define ES_WIFI_STACK_REV_SIZE 16
chuanga 0:43ff9e3bc244 28 #define ES_WIFI_RTOS_REV_SIZE 16
chuanga 0:43ff9e3bc244 29
chuanga 0:43ff9e3bc244 30 // The input range for AT Command 'R1' is 0 to 1200 bytes
chuanga 0:43ff9e3bc244 31 // 'R1' Set Read Transport Packet Size (bytes)
chuanga 0:43ff9e3bc244 32 #define ES_WIFI_MAX_RX_PACKET_SIZE 1200
chuanga 0:43ff9e3bc244 33 // Module maxume DATA payload for Tx packet is 1460
chuanga 0:43ff9e3bc244 34 #define ES_WIFI_MAX_TX_PACKET_SIZE 1460
chuanga 0:43ff9e3bc244 35 typedef enum ism_security {
chuanga 0:43ff9e3bc244 36 ISM_SECURITY_NONE = 0x0, /*!< open access point */
chuanga 0:43ff9e3bc244 37 ISM_SECURITY_WEP = 0x1, /*!< phrase conforms to WEP */
chuanga 0:43ff9e3bc244 38 ISM_SECURITY_WPA = 0x2, /*!< phrase conforms to WPA */
chuanga 0:43ff9e3bc244 39 ISM_SECURITY_WPA2 = 0x3, /*!< phrase conforms to WPA2 */
chuanga 0:43ff9e3bc244 40 ISM_SECURITY_WPA_WPA2 = 0x4, /*!< phrase conforms to WPA/WPA2 */
chuanga 0:43ff9e3bc244 41 ISM_SECURITY_UNKNOWN = 0xFF, /*!< unknown/unsupported security in scan results */
chuanga 0:43ff9e3bc244 42 } ism_security_t;
chuanga 0:43ff9e3bc244 43
chuanga 0:43ff9e3bc244 44 extern "C" int32_t ParseNumber(char *ptr, uint8_t *cnt);
chuanga 0:43ff9e3bc244 45
chuanga 0:43ff9e3bc244 46 /** ISM43362Interface class.
chuanga 0:43ff9e3bc244 47 This is an interface to a ISM43362 radio.
chuanga 0:43ff9e3bc244 48 */
chuanga 0:43ff9e3bc244 49 class ISM43362 {
chuanga 0:43ff9e3bc244 50 public:
chuanga 0:43ff9e3bc244 51 ISM43362(PinName mosi, PinName miso, PinName clk, PinName nss, PinName resetpin, PinName datareadypin, PinName wakeup, bool debug = false);
chuanga 0:43ff9e3bc244 52
chuanga 0:43ff9e3bc244 53 /**
chuanga 0:43ff9e3bc244 54 * Check firmware version of ISM43362
chuanga 0:43ff9e3bc244 55 *
chuanga 0:43ff9e3bc244 56 * @return fw version or null if no version is read
chuanga 0:43ff9e3bc244 57 */
chuanga 0:43ff9e3bc244 58 uint32_t get_firmware_version(void);
chuanga 0:43ff9e3bc244 59
chuanga 0:43ff9e3bc244 60 /**
chuanga 0:43ff9e3bc244 61 * Reset ISM43362
chuanga 0:43ff9e3bc244 62 *
chuanga 0:43ff9e3bc244 63 * @return true only if ISM43362 resets successfully
chuanga 0:43ff9e3bc244 64 */
chuanga 0:43ff9e3bc244 65 bool reset(void);
chuanga 0:43ff9e3bc244 66
chuanga 0:43ff9e3bc244 67 /**
chuanga 0:43ff9e3bc244 68 * Enable/Disable DHCP
chuanga 0:43ff9e3bc244 69 *
chuanga 0:43ff9e3bc244 70 * @param enabled DHCP enabled when true
chuanga 0:43ff9e3bc244 71 * @return true only if ISM43362 enables/disables DHCP successfully
chuanga 0:43ff9e3bc244 72 */
chuanga 0:43ff9e3bc244 73 bool dhcp(bool enabled);
chuanga 0:43ff9e3bc244 74
chuanga 0:43ff9e3bc244 75 /**
chuanga 0:43ff9e3bc244 76 * Connect ISM43362 to AP
chuanga 0:43ff9e3bc244 77 *
chuanga 0:43ff9e3bc244 78 * @param ap the name of the AP
chuanga 0:43ff9e3bc244 79 * @param passPhrase the password of AP
chuanga 0:43ff9e3bc244 80 * @param ap_sec the security level of network AP
chuanga 0:43ff9e3bc244 81 * @return nsapi_error enum
chuanga 0:43ff9e3bc244 82 */
chuanga 0:43ff9e3bc244 83 int connect(const char *ap, const char *passPhrase, ism_security_t ap_sec);
chuanga 0:43ff9e3bc244 84
chuanga 0:43ff9e3bc244 85 /**
chuanga 0:43ff9e3bc244 86 * Disconnect ISM43362 from AP
chuanga 0:43ff9e3bc244 87 *
chuanga 0:43ff9e3bc244 88 * @return true only if ISM43362 is disconnected successfully
chuanga 0:43ff9e3bc244 89 */
chuanga 0:43ff9e3bc244 90 bool disconnect(void);
chuanga 0:43ff9e3bc244 91
chuanga 0:43ff9e3bc244 92 /**
chuanga 0:43ff9e3bc244 93 * Get the IP address of ISM43362
chuanga 0:43ff9e3bc244 94 *
chuanga 0:43ff9e3bc244 95 * @return null-teriminated IP address or null if no IP address is assigned
chuanga 0:43ff9e3bc244 96 */
chuanga 0:43ff9e3bc244 97 const char *getIPAddress(void);
chuanga 0:43ff9e3bc244 98
chuanga 0:43ff9e3bc244 99 /**
chuanga 0:43ff9e3bc244 100 * Get the MAC address of ISM43362
chuanga 0:43ff9e3bc244 101 *
chuanga 0:43ff9e3bc244 102 * @return null-terminated MAC address or null if no MAC address is assigned
chuanga 0:43ff9e3bc244 103 */
chuanga 0:43ff9e3bc244 104 const char *getMACAddress(void);
chuanga 0:43ff9e3bc244 105
chuanga 0:43ff9e3bc244 106 /** Get the local gateway
chuanga 0:43ff9e3bc244 107 *
chuanga 0:43ff9e3bc244 108 * @return Null-terminated representation of the local gateway
chuanga 0:43ff9e3bc244 109 * or null if no network mask has been recieved
chuanga 0:43ff9e3bc244 110 */
chuanga 0:43ff9e3bc244 111 const char *getGateway();
chuanga 0:43ff9e3bc244 112
chuanga 0:43ff9e3bc244 113 /** Get the local network mask
chuanga 0:43ff9e3bc244 114 *
chuanga 0:43ff9e3bc244 115 * @return Null-terminated representation of the local network mask
chuanga 0:43ff9e3bc244 116 * or null if no network mask has been recieved
chuanga 0:43ff9e3bc244 117 */
chuanga 0:43ff9e3bc244 118 const char *getNetmask();
chuanga 0:43ff9e3bc244 119
chuanga 0:43ff9e3bc244 120 /* Return RSSI for active connection
chuanga 0:43ff9e3bc244 121 *
chuanga 0:43ff9e3bc244 122 * @return Measured RSSI
chuanga 0:43ff9e3bc244 123 */
chuanga 0:43ff9e3bc244 124 int8_t getRSSI();
chuanga 0:43ff9e3bc244 125
chuanga 0:43ff9e3bc244 126 /**
chuanga 0:43ff9e3bc244 127 * Check if ISM43362 is conenected
chuanga 0:43ff9e3bc244 128 *
chuanga 0:43ff9e3bc244 129 * @return true only if the chip has an IP address
chuanga 0:43ff9e3bc244 130 */
chuanga 0:43ff9e3bc244 131 bool isConnected(void);
chuanga 0:43ff9e3bc244 132
chuanga 0:43ff9e3bc244 133 /** Scan for available networks
chuanga 0:43ff9e3bc244 134 *
chuanga 0:43ff9e3bc244 135 * @param ap Pointer to allocated array to store discovered AP
chuanga 0:43ff9e3bc244 136 * @param limit Size of allocated @a res array, or 0 to only count available AP
chuanga 0:43ff9e3bc244 137 * @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error
chuanga 0:43ff9e3bc244 138 * see @a nsapi_error
chuanga 0:43ff9e3bc244 139 */
chuanga 0:43ff9e3bc244 140 int scan(WiFiAccessPoint *res, unsigned limit);
chuanga 0:43ff9e3bc244 141
chuanga 0:43ff9e3bc244 142 /**Perform a dns query
chuanga 0:43ff9e3bc244 143 *
chuanga 0:43ff9e3bc244 144 * @param name Hostname to resolve
chuanga 0:43ff9e3bc244 145 * @param ip Buffer to store IP address
chuanga 0:43ff9e3bc244 146 * @return 0 true on success, false on failure
chuanga 0:43ff9e3bc244 147 */
chuanga 0:43ff9e3bc244 148 bool dns_lookup(const char *name, char *ip);
chuanga 0:43ff9e3bc244 149
chuanga 0:43ff9e3bc244 150 /**
chuanga 0:43ff9e3bc244 151 * Open a socketed connection
chuanga 0:43ff9e3bc244 152 *
chuanga 0:43ff9e3bc244 153 * @param type the type of socket to open "UDP" or "TCP"
chuanga 0:43ff9e3bc244 154 * @param id id to give the new socket, valid 0-4
chuanga 0:43ff9e3bc244 155 * @param port port to open connection with
chuanga 0:43ff9e3bc244 156 * @param addr the IP address of the destination
chuanga 0:43ff9e3bc244 157 * @return true only if socket opened successfully
chuanga 0:43ff9e3bc244 158 */
chuanga 0:43ff9e3bc244 159 bool open(const char *type, int id, const char *addr, int port);
chuanga 0:43ff9e3bc244 160
chuanga 0:43ff9e3bc244 161 /**
chuanga 0:43ff9e3bc244 162 * Sends data to an open socket
chuanga 0:43ff9e3bc244 163 *
chuanga 0:43ff9e3bc244 164 * @param id id of socket to send to
chuanga 0:43ff9e3bc244 165 * @param data data to be sent
chuanga 0:43ff9e3bc244 166 * @param amount amount of data to be sent - max 1024
chuanga 0:43ff9e3bc244 167 * @return true only if data sent successfully
chuanga 0:43ff9e3bc244 168 */
chuanga 0:43ff9e3bc244 169 bool send(int id, const void *data, uint32_t amount);
chuanga 0:43ff9e3bc244 170
chuanga 0:43ff9e3bc244 171 /**
chuanga 0:43ff9e3bc244 172 * Receives data from an open socket
chuanga 0:43ff9e3bc244 173 *
chuanga 0:43ff9e3bc244 174 * @param id id to receive from
chuanga 0:43ff9e3bc244 175 * @param data placeholder for returned information
chuanga 0:43ff9e3bc244 176 * @param amount number of bytes to be received
chuanga 0:43ff9e3bc244 177 * @return the number of bytes received
chuanga 0:43ff9e3bc244 178 */
chuanga 0:43ff9e3bc244 179 int32_t recv(int id, void *data, uint32_t amount);
chuanga 0:43ff9e3bc244 180
chuanga 0:43ff9e3bc244 181 /**
chuanga 0:43ff9e3bc244 182 * Closes a socket
chuanga 0:43ff9e3bc244 183 *
chuanga 0:43ff9e3bc244 184 * @param id id of socket to close, valid only 0-4
chuanga 0:43ff9e3bc244 185 * @return true only if socket is closed successfully
chuanga 0:43ff9e3bc244 186 */
chuanga 0:43ff9e3bc244 187 bool close(int id);
chuanga 0:43ff9e3bc244 188
chuanga 0:43ff9e3bc244 189 /**
chuanga 0:43ff9e3bc244 190 * Checks if data is available
chuanga 0:43ff9e3bc244 191 */
chuanga 0:43ff9e3bc244 192 bool readable();
chuanga 0:43ff9e3bc244 193
chuanga 0:43ff9e3bc244 194 /**
chuanga 0:43ff9e3bc244 195 * Checks if data can be written
chuanga 0:43ff9e3bc244 196 */
chuanga 0:43ff9e3bc244 197 bool writeable();
chuanga 0:43ff9e3bc244 198
chuanga 0:43ff9e3bc244 199 /**
chuanga 0:43ff9e3bc244 200 * Attach a function to call whenever network state has changed
chuanga 0:43ff9e3bc244 201 *
chuanga 0:43ff9e3bc244 202 * @param func A pointer to a void function, or 0 to set as none
chuanga 0:43ff9e3bc244 203 */
chuanga 0:43ff9e3bc244 204 void attach(Callback<void()> func);
chuanga 0:43ff9e3bc244 205
chuanga 0:43ff9e3bc244 206 /**
chuanga 0:43ff9e3bc244 207 * Check is datas are available to read for a socket
chuanga 0:43ff9e3bc244 208 * @param id socket id
chuanga 0:43ff9e3bc244 209 * @param data placeholder for returned information
chuanga 0:43ff9e3bc244 210 * @param amount size to read for the check
chuanga 0:43ff9e3bc244 211 * @return amount of read value, or -1 for errors
chuanga 0:43ff9e3bc244 212 */
chuanga 0:43ff9e3bc244 213 int check_recv_status(int id, void *data);
chuanga 0:43ff9e3bc244 214
chuanga 0:43ff9e3bc244 215 /**
chuanga 0:43ff9e3bc244 216 * Attach a function to call whenever network state has changed
chuanga 0:43ff9e3bc244 217 *
chuanga 0:43ff9e3bc244 218 * @param obj pointer to the object to call the member function on
chuanga 0:43ff9e3bc244 219 * @param method pointer to the member function to call
chuanga 0:43ff9e3bc244 220 */
chuanga 0:43ff9e3bc244 221 template <typename T, typename M>
chuanga 0:43ff9e3bc244 222 void attach(T *obj, M method)
chuanga 0:43ff9e3bc244 223 {
chuanga 0:43ff9e3bc244 224 attach(Callback<void()>(obj, method));
chuanga 0:43ff9e3bc244 225 }
chuanga 0:43ff9e3bc244 226
chuanga 0:43ff9e3bc244 227 /** Get the connection status
chuanga 0:43ff9e3bc244 228 *
chuanga 0:43ff9e3bc244 229 * @return The connection status according to ConnectionStatusType
chuanga 0:43ff9e3bc244 230 */
chuanga 0:43ff9e3bc244 231 nsapi_connection_status_t connection_status() const;
chuanga 0:43ff9e3bc244 232
chuanga 0:43ff9e3bc244 233
chuanga 0:43ff9e3bc244 234 private:
chuanga 0:43ff9e3bc244 235 BufferedSpi _bufferspi;
chuanga 0:43ff9e3bc244 236 ATParser _parser;
chuanga 0:43ff9e3bc244 237 DigitalOut _resetpin;
chuanga 0:43ff9e3bc244 238 volatile int _active_id;
chuanga 0:43ff9e3bc244 239 void print_rx_buff(void);
chuanga 0:43ff9e3bc244 240 bool check_response(void);
chuanga 0:43ff9e3bc244 241 struct packet {
chuanga 0:43ff9e3bc244 242 struct packet *next;
chuanga 0:43ff9e3bc244 243 int id;
chuanga 0:43ff9e3bc244 244 uint32_t len;
chuanga 0:43ff9e3bc244 245 // data follows
chuanga 0:43ff9e3bc244 246 } *_packets, * *_packets_end;
chuanga 0:43ff9e3bc244 247 void _packet_handler();
chuanga 0:43ff9e3bc244 248 bool _ism_debug;
chuanga 0:43ff9e3bc244 249
chuanga 0:43ff9e3bc244 250 char _ip_buffer[16];
chuanga 0:43ff9e3bc244 251 char _gateway_buffer[16];
chuanga 0:43ff9e3bc244 252 char _netmask_buffer[16];
chuanga 0:43ff9e3bc244 253 char _mac_buffer[18];
chuanga 0:43ff9e3bc244 254 uint32_t _FwVersionId;
chuanga 0:43ff9e3bc244 255
chuanga 0:43ff9e3bc244 256 // Connection state reporting
chuanga 0:43ff9e3bc244 257 nsapi_connection_status_t _conn_status;
chuanga 0:43ff9e3bc244 258 mbed::Callback<void()> _conn_stat_cb;
chuanga 0:43ff9e3bc244 259 };
chuanga 0:43ff9e3bc244 260
chuanga 0:43ff9e3bc244 261 #endif