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 /* SPWFSAxx Devices
chuanga 0:43ff9e3bc244 2 * Copyright (c) 2015 ARM Limited
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 SPWFSAXX_H
chuanga 0:43ff9e3bc244 18 #define SPWFSAXX_H
chuanga 0:43ff9e3bc244 19
chuanga 0:43ff9e3bc244 20 #include "mbed.h"
chuanga 0:43ff9e3bc244 21 #include "ATCmdParser.h"
chuanga 0:43ff9e3bc244 22 #include "BlockExecuter.h"
chuanga 0:43ff9e3bc244 23
chuanga 0:43ff9e3bc244 24 /* Common SPWFSAxx macros */
chuanga 0:43ff9e3bc244 25 #define SPWFXX_WINDS_LOW_ON "0x00000000"
chuanga 0:43ff9e3bc244 26 #define SPWFXX_DEFAULT_BAUD_RATE 115200
chuanga 0:43ff9e3bc244 27 #define SPWFXX_MAX_TRIALS 3
chuanga 0:43ff9e3bc244 28
chuanga 0:43ff9e3bc244 29 #if !defined(SPWFSAXX_RTS_PIN)
chuanga 0:43ff9e3bc244 30 #define SPWFSAXX_RTS_PIN NC
chuanga 0:43ff9e3bc244 31 #endif // !defined(SPWFSAXX_RTS_PIN)
chuanga 0:43ff9e3bc244 32 #if !defined(SPWFSAXX_CTS_PIN)
chuanga 0:43ff9e3bc244 33 #define SPWFSAXX_CTS_PIN NC
chuanga 0:43ff9e3bc244 34 #endif // !defined(SPWFSAXX_CTS_PIN)
chuanga 0:43ff9e3bc244 35
chuanga 0:43ff9e3bc244 36 #define SPWFXX_ERR_OK (+1)
chuanga 0:43ff9e3bc244 37 #define SPWFXX_ERR_OOM (-1)
chuanga 0:43ff9e3bc244 38 #define SPWFXX_ERR_READ (-2)
chuanga 0:43ff9e3bc244 39 #define SPWFXX_ERR_LEN (-3)
chuanga 0:43ff9e3bc244 40
chuanga 0:43ff9e3bc244 41
chuanga 0:43ff9e3bc244 42 /* Max number of sockets & packets */
chuanga 0:43ff9e3bc244 43 #define SPWFSA_SOCKET_COUNT (8)
chuanga 0:43ff9e3bc244 44 #define SPWFSA_MAX_PACKETS (4)
chuanga 0:43ff9e3bc244 45
chuanga 0:43ff9e3bc244 46 #define PENDING_DATA_SLOTS (13)
chuanga 0:43ff9e3bc244 47
chuanga 0:43ff9e3bc244 48 /* Pending data packets size buffer */
chuanga 0:43ff9e3bc244 49 class SpwfRealPendingPackets {
chuanga 0:43ff9e3bc244 50 public:
chuanga 0:43ff9e3bc244 51 SpwfRealPendingPackets() {
chuanga 0:43ff9e3bc244 52 reset();
chuanga 0:43ff9e3bc244 53 }
chuanga 0:43ff9e3bc244 54
chuanga 0:43ff9e3bc244 55 void add(uint32_t new_cum_size) {
chuanga 0:43ff9e3bc244 56 MBED_ASSERT(new_cum_size >= cumulative_size);
chuanga 0:43ff9e3bc244 57
chuanga 0:43ff9e3bc244 58 if(new_cum_size == cumulative_size) {
chuanga 0:43ff9e3bc244 59 /* nothing to do */
chuanga 0:43ff9e3bc244 60 return;
chuanga 0:43ff9e3bc244 61 }
chuanga 0:43ff9e3bc244 62
chuanga 0:43ff9e3bc244 63 /* => `new_cum_size > cumulative_size` */
chuanga 0:43ff9e3bc244 64 real_pkt_sizes[last_pkt_ptr] = (uint16_t)(new_cum_size - cumulative_size);
chuanga 0:43ff9e3bc244 65 cumulative_size = new_cum_size;
chuanga 0:43ff9e3bc244 66
chuanga 0:43ff9e3bc244 67 last_pkt_ptr = (last_pkt_ptr + 1) % PENDING_DATA_SLOTS;
chuanga 0:43ff9e3bc244 68
chuanga 0:43ff9e3bc244 69 MBED_ASSERT(first_pkt_ptr != last_pkt_ptr);
chuanga 0:43ff9e3bc244 70 }
chuanga 0:43ff9e3bc244 71
chuanga 0:43ff9e3bc244 72 uint32_t get(void) {
chuanga 0:43ff9e3bc244 73 if(empty()) return 0;
chuanga 0:43ff9e3bc244 74
chuanga 0:43ff9e3bc244 75 return real_pkt_sizes[first_pkt_ptr];
chuanga 0:43ff9e3bc244 76 }
chuanga 0:43ff9e3bc244 77
chuanga 0:43ff9e3bc244 78 uint32_t cumulative(void) {
chuanga 0:43ff9e3bc244 79 return cumulative_size;
chuanga 0:43ff9e3bc244 80 }
chuanga 0:43ff9e3bc244 81
chuanga 0:43ff9e3bc244 82 uint32_t remove(uint32_t size) {
chuanga 0:43ff9e3bc244 83 MBED_ASSERT(!empty());
chuanga 0:43ff9e3bc244 84
chuanga 0:43ff9e3bc244 85 uint32_t ret = real_pkt_sizes[first_pkt_ptr];
chuanga 0:43ff9e3bc244 86 first_pkt_ptr = (first_pkt_ptr + 1) % PENDING_DATA_SLOTS;
chuanga 0:43ff9e3bc244 87
chuanga 0:43ff9e3bc244 88 MBED_ASSERT(ret == size);
chuanga 0:43ff9e3bc244 89 MBED_ASSERT(ret <= cumulative_size);
chuanga 0:43ff9e3bc244 90 cumulative_size -= ret;
chuanga 0:43ff9e3bc244 91
chuanga 0:43ff9e3bc244 92 return ret;
chuanga 0:43ff9e3bc244 93 }
chuanga 0:43ff9e3bc244 94
chuanga 0:43ff9e3bc244 95 void reset(void) {
chuanga 0:43ff9e3bc244 96 memset(this, 0, sizeof(*this));
chuanga 0:43ff9e3bc244 97 }
chuanga 0:43ff9e3bc244 98
chuanga 0:43ff9e3bc244 99 private:
chuanga 0:43ff9e3bc244 100 bool empty(void) {
chuanga 0:43ff9e3bc244 101 if(first_pkt_ptr == last_pkt_ptr) {
chuanga 0:43ff9e3bc244 102 MBED_ASSERT(cumulative_size == 0);
chuanga 0:43ff9e3bc244 103 return true;
chuanga 0:43ff9e3bc244 104 }
chuanga 0:43ff9e3bc244 105 return false;
chuanga 0:43ff9e3bc244 106 }
chuanga 0:43ff9e3bc244 107
chuanga 0:43ff9e3bc244 108 uint16_t real_pkt_sizes[PENDING_DATA_SLOTS];
chuanga 0:43ff9e3bc244 109 uint8_t first_pkt_ptr;
chuanga 0:43ff9e3bc244 110 uint8_t last_pkt_ptr;
chuanga 0:43ff9e3bc244 111 uint32_t cumulative_size;
chuanga 0:43ff9e3bc244 112 };
chuanga 0:43ff9e3bc244 113
chuanga 0:43ff9e3bc244 114 class SpwfSAInterface;
chuanga 0:43ff9e3bc244 115
chuanga 0:43ff9e3bc244 116 /** SPWFSAxx Interface class.
chuanga 0:43ff9e3bc244 117 This is an interface to a SPWFSAxx module.
chuanga 0:43ff9e3bc244 118 */
chuanga 0:43ff9e3bc244 119 class SPWFSAxx
chuanga 0:43ff9e3bc244 120 {
chuanga 0:43ff9e3bc244 121 private:
chuanga 0:43ff9e3bc244 122 /* abstract class*/
chuanga 0:43ff9e3bc244 123 SPWFSAxx(PinName tx, PinName rx, PinName rts, PinName cts,
chuanga 0:43ff9e3bc244 124 SpwfSAInterface &ifce, bool debug,
chuanga 0:43ff9e3bc244 125 PinName wakeup, PinName reset);
chuanga 0:43ff9e3bc244 126
chuanga 0:43ff9e3bc244 127 public:
chuanga 0:43ff9e3bc244 128 /**
chuanga 0:43ff9e3bc244 129 * Init the SPWFSAxx
chuanga 0:43ff9e3bc244 130 *
chuanga 0:43ff9e3bc244 131 * @param mode mode in which to startup
chuanga 0:43ff9e3bc244 132 * @return true only if SPWFSAxx has started up correctly
chuanga 0:43ff9e3bc244 133 */
chuanga 0:43ff9e3bc244 134 bool startup(int mode);
chuanga 0:43ff9e3bc244 135
chuanga 0:43ff9e3bc244 136 /**
chuanga 0:43ff9e3bc244 137 * Connect SPWFSAxx to AP
chuanga 0:43ff9e3bc244 138 *
chuanga 0:43ff9e3bc244 139 * @param ap the name of the AP
chuanga 0:43ff9e3bc244 140 * @param passPhrase the password of AP
chuanga 0:43ff9e3bc244 141 * @param securityMode the security mode of AP (WPA/WPA2, WEP, Open)
chuanga 0:43ff9e3bc244 142 * @return true only if SPWFSAxx is connected successfully
chuanga 0:43ff9e3bc244 143 */
chuanga 0:43ff9e3bc244 144 bool connect(const char *ap, const char *passPhrase, int securityMode);
chuanga 0:43ff9e3bc244 145
chuanga 0:43ff9e3bc244 146 /**
chuanga 0:43ff9e3bc244 147 * Disconnect SPWFSAxx from AP
chuanga 0:43ff9e3bc244 148 *
chuanga 0:43ff9e3bc244 149 * @return true only if SPWFSAxx is disconnected successfully
chuanga 0:43ff9e3bc244 150 */
chuanga 0:43ff9e3bc244 151 bool disconnect(void);
chuanga 0:43ff9e3bc244 152
chuanga 0:43ff9e3bc244 153 /**
chuanga 0:43ff9e3bc244 154 * Get the IP address of SPWFSAxx
chuanga 0:43ff9e3bc244 155 *
chuanga 0:43ff9e3bc244 156 * @return null-terminated IP address or null if no IP address is assigned
chuanga 0:43ff9e3bc244 157 */
chuanga 0:43ff9e3bc244 158 const char *getIPAddress(void);
chuanga 0:43ff9e3bc244 159
chuanga 0:43ff9e3bc244 160 /**
chuanga 0:43ff9e3bc244 161 * Get the MAC address of SPWFSAxx
chuanga 0:43ff9e3bc244 162 *
chuanga 0:43ff9e3bc244 163 * @return null-terminated MAC address or null if no MAC address is assigned
chuanga 0:43ff9e3bc244 164 */
chuanga 0:43ff9e3bc244 165 const char *getMACAddress(void);
chuanga 0:43ff9e3bc244 166
chuanga 0:43ff9e3bc244 167 /** Get the local gateway
chuanga 0:43ff9e3bc244 168 *
chuanga 0:43ff9e3bc244 169 * @return Null-terminated representation of the local gateway
chuanga 0:43ff9e3bc244 170 * or null if no network mask has been received
chuanga 0:43ff9e3bc244 171 */
chuanga 0:43ff9e3bc244 172 const char *getGateway(void);
chuanga 0:43ff9e3bc244 173
chuanga 0:43ff9e3bc244 174 /** Get the local network mask
chuanga 0:43ff9e3bc244 175 *
chuanga 0:43ff9e3bc244 176 * @return Null-terminated representation of the local network mask
chuanga 0:43ff9e3bc244 177 * or null if no network mask has been received
chuanga 0:43ff9e3bc244 178 */
chuanga 0:43ff9e3bc244 179 const char *getNetmask(void);
chuanga 0:43ff9e3bc244 180
chuanga 0:43ff9e3bc244 181 /** Gets the current radio signal strength for active connection
chuanga 0:43ff9e3bc244 182 *
chuanga 0:43ff9e3bc244 183 * @return Connection strength in dBm (negative value)
chuanga 0:43ff9e3bc244 184 */
chuanga 0:43ff9e3bc244 185 int8_t getRssi();
chuanga 0:43ff9e3bc244 186
chuanga 0:43ff9e3bc244 187 /**
chuanga 0:43ff9e3bc244 188 * Sends data to an open socket
chuanga 0:43ff9e3bc244 189 *
chuanga 0:43ff9e3bc244 190 * @param spwf_id module id of socket to send to
chuanga 0:43ff9e3bc244 191 * @param data data to be sent
chuanga 0:43ff9e3bc244 192 * @param amount amount of data to be sent - max 1024
chuanga 0:43ff9e3bc244 193 * @param internal_id driver id of socket to send to
chuanga 0:43ff9e3bc244 194 * @return number of written bytes on success, negative on failure
chuanga 0:43ff9e3bc244 195 */
chuanga 0:43ff9e3bc244 196 nsapi_size_or_error_t send(int spwf_id, const void *data, uint32_t amount, int internal_id);
chuanga 0:43ff9e3bc244 197
chuanga 0:43ff9e3bc244 198 /**
chuanga 0:43ff9e3bc244 199 * Receives data from an open socket
chuanga 0:43ff9e3bc244 200 *
chuanga 0:43ff9e3bc244 201 * @param id id to receive from
chuanga 0:43ff9e3bc244 202 * @param data placeholder for returned information
chuanga 0:43ff9e3bc244 203 * @param amount number of bytes to be received
chuanga 0:43ff9e3bc244 204 * @param datagram receive a datagram packet
chuanga 0:43ff9e3bc244 205 * @return the number of bytes received
chuanga 0:43ff9e3bc244 206 */
chuanga 0:43ff9e3bc244 207 int32_t recv(int id, void *data, uint32_t amount, bool datagram);
chuanga 0:43ff9e3bc244 208
chuanga 0:43ff9e3bc244 209 /**
chuanga 0:43ff9e3bc244 210 * Closes a socket
chuanga 0:43ff9e3bc244 211 *
chuanga 0:43ff9e3bc244 212 * @param id id of socket to close, valid only 0-4
chuanga 0:43ff9e3bc244 213 * @return true only if socket is closed successfully
chuanga 0:43ff9e3bc244 214 */
chuanga 0:43ff9e3bc244 215 bool close(int id);
chuanga 0:43ff9e3bc244 216
chuanga 0:43ff9e3bc244 217 /**
chuanga 0:43ff9e3bc244 218 * Allows timeout to be changed between commands
chuanga 0:43ff9e3bc244 219 *
chuanga 0:43ff9e3bc244 220 * @param timeout_ms timeout of the connection
chuanga 0:43ff9e3bc244 221 */
chuanga 0:43ff9e3bc244 222 void setTimeout(uint32_t timeout_ms);
chuanga 0:43ff9e3bc244 223
chuanga 0:43ff9e3bc244 224 /**
chuanga 0:43ff9e3bc244 225 * Attach a function to call whenever network state has changed
chuanga 0:43ff9e3bc244 226 *
chuanga 0:43ff9e3bc244 227 * @param func A pointer to a void function, or 0 to set as none
chuanga 0:43ff9e3bc244 228 */
chuanga 0:43ff9e3bc244 229 void attach(Callback<void()> func);
chuanga 0:43ff9e3bc244 230
chuanga 0:43ff9e3bc244 231 /**
chuanga 0:43ff9e3bc244 232 * Attach a function to call whenever network state has changed
chuanga 0:43ff9e3bc244 233 *
chuanga 0:43ff9e3bc244 234 * @param obj pointer to the object to call the member function on
chuanga 0:43ff9e3bc244 235 * @param method pointer to the member function to call
chuanga 0:43ff9e3bc244 236 */
chuanga 0:43ff9e3bc244 237 template <typename T, typename M>
chuanga 0:43ff9e3bc244 238 void attach(T *obj, M method) {
chuanga 0:43ff9e3bc244 239 attach(Callback<void()>(obj, method));
chuanga 0:43ff9e3bc244 240 }
chuanga 0:43ff9e3bc244 241
chuanga 0:43ff9e3bc244 242 static const char _cr_ = '\x0d'; // '\r' carriage return
chuanga 0:43ff9e3bc244 243 static const char _lf_ = '\x0a'; // '\n' line feed
chuanga 0:43ff9e3bc244 244
chuanga 0:43ff9e3bc244 245 private:
chuanga 0:43ff9e3bc244 246 UARTSerial _serial;
chuanga 0:43ff9e3bc244 247 ATCmdParser _parser;
chuanga 0:43ff9e3bc244 248
chuanga 0:43ff9e3bc244 249 DigitalOut _wakeup;
chuanga 0:43ff9e3bc244 250 DigitalOut _reset;
chuanga 0:43ff9e3bc244 251 PinName _rts;
chuanga 0:43ff9e3bc244 252 PinName _cts;
chuanga 0:43ff9e3bc244 253
chuanga 0:43ff9e3bc244 254 int _timeout;
chuanga 0:43ff9e3bc244 255 bool _dbg_on;
chuanga 0:43ff9e3bc244 256
chuanga 0:43ff9e3bc244 257 int _pending_sockets_bitmap;
chuanga 0:43ff9e3bc244 258 SpwfRealPendingPackets _pending_pkt_sizes[SPWFSA_SOCKET_COUNT];
chuanga 0:43ff9e3bc244 259
chuanga 0:43ff9e3bc244 260 bool _network_lost_flag;
chuanga 0:43ff9e3bc244 261 SpwfSAInterface &_associated_interface;
chuanga 0:43ff9e3bc244 262
chuanga 0:43ff9e3bc244 263 /**
chuanga 0:43ff9e3bc244 264 * Reset SPWFSAxx
chuanga 0:43ff9e3bc244 265 *
chuanga 0:43ff9e3bc244 266 * @return true only if SPWFSAxx resets successfully
chuanga 0:43ff9e3bc244 267 */
chuanga 0:43ff9e3bc244 268 bool hw_reset(void);
chuanga 0:43ff9e3bc244 269 bool reset(void);
chuanga 0:43ff9e3bc244 270
chuanga 0:43ff9e3bc244 271 /**
chuanga 0:43ff9e3bc244 272 * Check if SPWFSAxx is connected
chuanga 0:43ff9e3bc244 273 *
chuanga 0:43ff9e3bc244 274 * @return true only if the chip has an IP address
chuanga 0:43ff9e3bc244 275 */
chuanga 0:43ff9e3bc244 276 bool isConnected(void);
chuanga 0:43ff9e3bc244 277
chuanga 0:43ff9e3bc244 278 /**
chuanga 0:43ff9e3bc244 279 * Checks if data is available
chuanga 0:43ff9e3bc244 280 */
chuanga 0:43ff9e3bc244 281 bool readable(void) {
chuanga 0:43ff9e3bc244 282 return _serial.FileHandle::readable();
chuanga 0:43ff9e3bc244 283 }
chuanga 0:43ff9e3bc244 284
chuanga 0:43ff9e3bc244 285 /**
chuanga 0:43ff9e3bc244 286 * Checks if data can be written
chuanga 0:43ff9e3bc244 287 */
chuanga 0:43ff9e3bc244 288 bool writeable(void) {
chuanga 0:43ff9e3bc244 289 return _serial.FileHandle::writable();
chuanga 0:43ff9e3bc244 290 }
chuanga 0:43ff9e3bc244 291
chuanga 0:43ff9e3bc244 292 /**
chuanga 0:43ff9e3bc244 293 * Try to empty RX buffer
chuanga 0:43ff9e3bc244 294 * Can be used when commands fail receiving expected response to try to recover situation
chuanga 0:43ff9e3bc244 295 * @note Gives no guarantee that situation improves
chuanga 0:43ff9e3bc244 296 */
chuanga 0:43ff9e3bc244 297 void empty_rx_buffer(void) {
chuanga 0:43ff9e3bc244 298 while(readable()) _parser.getc();
chuanga 0:43ff9e3bc244 299 }
chuanga 0:43ff9e3bc244 300
chuanga 0:43ff9e3bc244 301 /* block calling (external) callback */
chuanga 0:43ff9e3bc244 302 volatile unsigned int _call_event_callback_blocked;
chuanga 0:43ff9e3bc244 303 Callback<void()> _callback_func;
chuanga 0:43ff9e3bc244 304
chuanga 0:43ff9e3bc244 305 struct packet {
chuanga 0:43ff9e3bc244 306 struct packet *next;
chuanga 0:43ff9e3bc244 307 int id;
chuanga 0:43ff9e3bc244 308 uint32_t len;
chuanga 0:43ff9e3bc244 309 // data follows
chuanga 0:43ff9e3bc244 310 } *_packets, **_packets_end;
chuanga 0:43ff9e3bc244 311
chuanga 0:43ff9e3bc244 312 void _packet_handler_th(void);
chuanga 0:43ff9e3bc244 313 void _execute_bottom_halves(void);
chuanga 0:43ff9e3bc244 314 void _network_lost_handler_th(void);
chuanga 0:43ff9e3bc244 315 void _network_lost_handler_bh(void);
chuanga 0:43ff9e3bc244 316 void _hard_fault_handler(void);
chuanga 0:43ff9e3bc244 317 void _wifi_hwfault_handler(void);
chuanga 0:43ff9e3bc244 318 void _server_gone_handler(void);
chuanga 0:43ff9e3bc244 319 #if MBED_CONF_IDW0XX1_EXPANSION_BOARD == IDW04A1
chuanga 0:43ff9e3bc244 320 void _skip_oob(void);
chuanga 0:43ff9e3bc244 321 #endif
chuanga 0:43ff9e3bc244 322 bool _wait_wifi_hw_started(void);
chuanga 0:43ff9e3bc244 323 bool _wait_console_active(void);
chuanga 0:43ff9e3bc244 324 int _read_len(int);
chuanga 0:43ff9e3bc244 325 int _flush_in(char*, int);
chuanga 0:43ff9e3bc244 326 bool _winds_off(void);
chuanga 0:43ff9e3bc244 327 void _winds_on(void);
chuanga 0:43ff9e3bc244 328 void _read_in_pending(void);
chuanga 0:43ff9e3bc244 329 int _read_in_pkt(int spwf_id, bool close);
chuanga 0:43ff9e3bc244 330 int _read_in_packet(int spwf_id, uint32_t amount);
chuanga 0:43ff9e3bc244 331 void _recover_from_hard_faults(void);
chuanga 0:43ff9e3bc244 332 void _free_packets(int spwf_id);
chuanga 0:43ff9e3bc244 333 void _free_all_packets(void);
chuanga 0:43ff9e3bc244 334 void _process_winds();
chuanga 0:43ff9e3bc244 335
chuanga 0:43ff9e3bc244 336 virtual int _read_in(char*, int, uint32_t) = 0;
chuanga 0:43ff9e3bc244 337
chuanga 0:43ff9e3bc244 338 bool _recv_delim_lf(void) {
chuanga 0:43ff9e3bc244 339 return (_parser.getc() == _lf_);
chuanga 0:43ff9e3bc244 340 }
chuanga 0:43ff9e3bc244 341
chuanga 0:43ff9e3bc244 342 bool _recv_delim_cr(void) {
chuanga 0:43ff9e3bc244 343 return (_parser.getc() == _cr_);
chuanga 0:43ff9e3bc244 344 }
chuanga 0:43ff9e3bc244 345
chuanga 0:43ff9e3bc244 346 bool _recv_delim_cr_lf(void) {
chuanga 0:43ff9e3bc244 347 return _recv_delim_cr() && _recv_delim_lf();
chuanga 0:43ff9e3bc244 348 }
chuanga 0:43ff9e3bc244 349
chuanga 0:43ff9e3bc244 350 bool _recv_ok(void) {
chuanga 0:43ff9e3bc244 351 return _parser.recv(SPWFXX_RECV_OK) && _recv_delim_lf();
chuanga 0:43ff9e3bc244 352 }
chuanga 0:43ff9e3bc244 353
chuanga 0:43ff9e3bc244 354 void _add_pending_packet_sz(int spwf_id, uint32_t size);
chuanga 0:43ff9e3bc244 355 void _add_pending_pkt_size(int spwf_id, uint32_t size) {
chuanga 0:43ff9e3bc244 356 _pending_pkt_sizes[spwf_id].add(size);
chuanga 0:43ff9e3bc244 357 }
chuanga 0:43ff9e3bc244 358
chuanga 0:43ff9e3bc244 359 uint32_t _get_cumulative_size(int spwf_id) {
chuanga 0:43ff9e3bc244 360 return _pending_pkt_sizes[spwf_id].cumulative();
chuanga 0:43ff9e3bc244 361 }
chuanga 0:43ff9e3bc244 362
chuanga 0:43ff9e3bc244 363 uint32_t _remove_pending_pkt_size(int spwf_id, uint32_t size) {
chuanga 0:43ff9e3bc244 364 return _pending_pkt_sizes[spwf_id].remove(size);
chuanga 0:43ff9e3bc244 365 }
chuanga 0:43ff9e3bc244 366
chuanga 0:43ff9e3bc244 367 uint32_t _get_pending_pkt_size(int spwf_id) {
chuanga 0:43ff9e3bc244 368 return _pending_pkt_sizes[spwf_id].get();
chuanga 0:43ff9e3bc244 369 }
chuanga 0:43ff9e3bc244 370
chuanga 0:43ff9e3bc244 371 void _reset_pending_pkt_sizes(int spwf_id) {
chuanga 0:43ff9e3bc244 372 _pending_pkt_sizes[spwf_id].reset();
chuanga 0:43ff9e3bc244 373 }
chuanga 0:43ff9e3bc244 374
chuanga 0:43ff9e3bc244 375 void _set_pending_data(int spwf_id) {
chuanga 0:43ff9e3bc244 376 _pending_sockets_bitmap |= (1 << spwf_id);
chuanga 0:43ff9e3bc244 377 }
chuanga 0:43ff9e3bc244 378
chuanga 0:43ff9e3bc244 379 void _clear_pending_data(int spwf_id) {
chuanga 0:43ff9e3bc244 380 _pending_sockets_bitmap &= ~(1 << spwf_id);
chuanga 0:43ff9e3bc244 381 }
chuanga 0:43ff9e3bc244 382
chuanga 0:43ff9e3bc244 383 bool _is_data_pending(int spwf_id) {
chuanga 0:43ff9e3bc244 384 return (_pending_sockets_bitmap & (1 << spwf_id)) ? true : false;
chuanga 0:43ff9e3bc244 385 }
chuanga 0:43ff9e3bc244 386
chuanga 0:43ff9e3bc244 387 bool _is_data_pending(void) {
chuanga 0:43ff9e3bc244 388 if(_pending_sockets_bitmap != 0) return true;
chuanga 0:43ff9e3bc244 389 else return false;
chuanga 0:43ff9e3bc244 390 }
chuanga 0:43ff9e3bc244 391
chuanga 0:43ff9e3bc244 392 void _packet_handler_bh(void) {
chuanga 0:43ff9e3bc244 393 /* read in other eventually pending packages */
chuanga 0:43ff9e3bc244 394 _read_in_pending();
chuanga 0:43ff9e3bc244 395 }
chuanga 0:43ff9e3bc244 396
chuanga 0:43ff9e3bc244 397 /* Do not call the (external) callback in IRQ context while performing critical module operations */
chuanga 0:43ff9e3bc244 398 void _event_handler(void);
chuanga 0:43ff9e3bc244 399
chuanga 0:43ff9e3bc244 400 void _error_handler(void);
chuanga 0:43ff9e3bc244 401
chuanga 0:43ff9e3bc244 402 void _call_callback(void) {
chuanga 0:43ff9e3bc244 403 if((bool)_callback_func) {
chuanga 0:43ff9e3bc244 404 _callback_func();
chuanga 0:43ff9e3bc244 405 }
chuanga 0:43ff9e3bc244 406 }
chuanga 0:43ff9e3bc244 407
chuanga 0:43ff9e3bc244 408 bool _is_event_callback_blocked(void) {
chuanga 0:43ff9e3bc244 409 return (_call_event_callback_blocked != 0);
chuanga 0:43ff9e3bc244 410 }
chuanga 0:43ff9e3bc244 411
chuanga 0:43ff9e3bc244 412 void _block_event_callback(void) {
chuanga 0:43ff9e3bc244 413 _call_event_callback_blocked++;
chuanga 0:43ff9e3bc244 414 }
chuanga 0:43ff9e3bc244 415
chuanga 0:43ff9e3bc244 416 void _unblock_event_callback(void) {
chuanga 0:43ff9e3bc244 417 MBED_ASSERT(_call_event_callback_blocked > 0);
chuanga 0:43ff9e3bc244 418 _call_event_callback_blocked--;
chuanga 0:43ff9e3bc244 419 if(_call_event_callback_blocked == 0) {
chuanga 0:43ff9e3bc244 420 _trigger_event_callback();
chuanga 0:43ff9e3bc244 421 }
chuanga 0:43ff9e3bc244 422 }
chuanga 0:43ff9e3bc244 423
chuanga 0:43ff9e3bc244 424 /* trigger call of (external) callback in case there is still data */
chuanga 0:43ff9e3bc244 425 void _trigger_event_callback(void) {
chuanga 0:43ff9e3bc244 426 MBED_ASSERT(_call_event_callback_blocked == 0);
chuanga 0:43ff9e3bc244 427 /* if still data available */
chuanga 0:43ff9e3bc244 428 if(readable()) {
chuanga 0:43ff9e3bc244 429 _call_callback();
chuanga 0:43ff9e3bc244 430 }
chuanga 0:43ff9e3bc244 431 }
chuanga 0:43ff9e3bc244 432
chuanga 0:43ff9e3bc244 433 char _ip_buffer[16];
chuanga 0:43ff9e3bc244 434 char _gateway_buffer[16];
chuanga 0:43ff9e3bc244 435 char _netmask_buffer[16];
chuanga 0:43ff9e3bc244 436 char _mac_buffer[18];
chuanga 0:43ff9e3bc244 437
chuanga 0:43ff9e3bc244 438 char _msg_buffer[256];
chuanga 0:43ff9e3bc244 439
chuanga 0:43ff9e3bc244 440 private:
chuanga 0:43ff9e3bc244 441 friend class SPWFSA01;
chuanga 0:43ff9e3bc244 442 friend class SPWFSA04;
chuanga 0:43ff9e3bc244 443 friend class SpwfSAInterface;
chuanga 0:43ff9e3bc244 444 };
chuanga 0:43ff9e3bc244 445
chuanga 0:43ff9e3bc244 446 #endif // SPWFSAXX_H