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
chuanga 0:43ff9e3bc244 2 /**
chuanga 0:43ff9e3bc244 3 * @file Buffer.h
chuanga 0:43ff9e3bc244 4 * @brief Software Buffer - Templated Ring Buffer for most data types
chuanga 0:43ff9e3bc244 5 * @author sam grove
chuanga 0:43ff9e3bc244 6 * @version 1.0
chuanga 0:43ff9e3bc244 7 * @see
chuanga 0:43ff9e3bc244 8 *
chuanga 0:43ff9e3bc244 9 * Copyright (c) 2013
chuanga 0:43ff9e3bc244 10 *
chuanga 0:43ff9e3bc244 11 * Licensed under the Apache License, Version 2.0 (the "License");
chuanga 0:43ff9e3bc244 12 * you may not use this file except in compliance with the License.
chuanga 0:43ff9e3bc244 13 * You may obtain a copy of the License at
chuanga 0:43ff9e3bc244 14 *
chuanga 0:43ff9e3bc244 15 * http://www.apache.org/licenses/LICENSE-2.0
chuanga 0:43ff9e3bc244 16 *
chuanga 0:43ff9e3bc244 17 * Unless required by applicable law or agreed to in writing, software
chuanga 0:43ff9e3bc244 18 * distributed under the License is distributed on an "AS IS" BASIS,
chuanga 0:43ff9e3bc244 19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
chuanga 0:43ff9e3bc244 20 * See the License for the specific language governing permissions and
chuanga 0:43ff9e3bc244 21 * limitations under the License.
chuanga 0:43ff9e3bc244 22 */
chuanga 0:43ff9e3bc244 23
chuanga 0:43ff9e3bc244 24 #ifndef MYBUFFER_H
chuanga 0:43ff9e3bc244 25 #define MYBUFFER_H
chuanga 0:43ff9e3bc244 26
chuanga 0:43ff9e3bc244 27 #include <stdint.h>
chuanga 0:43ff9e3bc244 28 #include <string.h>
chuanga 0:43ff9e3bc244 29
chuanga 0:43ff9e3bc244 30 /** A templated software ring buffer
chuanga 0:43ff9e3bc244 31 *
chuanga 0:43ff9e3bc244 32 * Example:
chuanga 0:43ff9e3bc244 33 * @code
chuanga 0:43ff9e3bc244 34 * #include "mbed.h"
chuanga 0:43ff9e3bc244 35 * #include "MyBuffer.h"
chuanga 0:43ff9e3bc244 36 *
chuanga 0:43ff9e3bc244 37 * MyBuffer <char> buf;
chuanga 0:43ff9e3bc244 38 *
chuanga 0:43ff9e3bc244 39 * int main()
chuanga 0:43ff9e3bc244 40 * {
chuanga 0:43ff9e3bc244 41 * buf = 'a';
chuanga 0:43ff9e3bc244 42 * buf.put('b');
chuanga 0:43ff9e3bc244 43 * char *head = buf.head();
chuanga 0:43ff9e3bc244 44 * puts(head);
chuanga 0:43ff9e3bc244 45 *
chuanga 0:43ff9e3bc244 46 * char whats_in_there[2] = {0};
chuanga 0:43ff9e3bc244 47 * int pos = 0;
chuanga 0:43ff9e3bc244 48 *
chuanga 0:43ff9e3bc244 49 * while(buf.available())
chuanga 0:43ff9e3bc244 50 * {
chuanga 0:43ff9e3bc244 51 * whats_in_there[pos++] = buf;
chuanga 0:43ff9e3bc244 52 * }
chuanga 0:43ff9e3bc244 53 * printf("%c %c\n", whats_in_there[0], whats_in_there[1]);
chuanga 0:43ff9e3bc244 54 * buf.clear();
chuanga 0:43ff9e3bc244 55 * error("done\n\n\n");
chuanga 0:43ff9e3bc244 56 * }
chuanga 0:43ff9e3bc244 57 * @endcode
chuanga 0:43ff9e3bc244 58 */
chuanga 0:43ff9e3bc244 59
chuanga 0:43ff9e3bc244 60 template <typename T>
chuanga 0:43ff9e3bc244 61 class MyBuffer {
chuanga 0:43ff9e3bc244 62 private:
chuanga 0:43ff9e3bc244 63 T *_buf;
chuanga 0:43ff9e3bc244 64 volatile uint32_t _wloc;
chuanga 0:43ff9e3bc244 65 volatile uint32_t _rloc;
chuanga 0:43ff9e3bc244 66 uint32_t _size;
chuanga 0:43ff9e3bc244 67
chuanga 0:43ff9e3bc244 68 public:
chuanga 0:43ff9e3bc244 69 /** Create a Buffer and allocate memory for it
chuanga 0:43ff9e3bc244 70 * @param size The size of the buffer
chuanga 0:43ff9e3bc244 71 */
chuanga 0:43ff9e3bc244 72 MyBuffer(uint32_t size = 0x100);
chuanga 0:43ff9e3bc244 73
chuanga 0:43ff9e3bc244 74 /** Get the size of the ring buffer
chuanga 0:43ff9e3bc244 75 * @return the size of the ring buffer
chuanga 0:43ff9e3bc244 76 */
chuanga 0:43ff9e3bc244 77 uint32_t getSize();
chuanga 0:43ff9e3bc244 78 uint32_t getNbAvailable();
chuanga 0:43ff9e3bc244 79
chuanga 0:43ff9e3bc244 80 /** Destry a Buffer and release it's allocated memory
chuanga 0:43ff9e3bc244 81 */
chuanga 0:43ff9e3bc244 82 ~MyBuffer();
chuanga 0:43ff9e3bc244 83
chuanga 0:43ff9e3bc244 84 /** Add a data element into the buffer
chuanga 0:43ff9e3bc244 85 * @param data Something to add to the buffer
chuanga 0:43ff9e3bc244 86 */
chuanga 0:43ff9e3bc244 87 void put(T data);
chuanga 0:43ff9e3bc244 88
chuanga 0:43ff9e3bc244 89 /** Remove a data element from the buffer
chuanga 0:43ff9e3bc244 90 * @return Pull the oldest element from the buffer
chuanga 0:43ff9e3bc244 91 */
chuanga 0:43ff9e3bc244 92 T get(void);
chuanga 0:43ff9e3bc244 93
chuanga 0:43ff9e3bc244 94 /** Get the address to the head of the buffer
chuanga 0:43ff9e3bc244 95 * @return The address of element 0 in the buffer
chuanga 0:43ff9e3bc244 96 */
chuanga 0:43ff9e3bc244 97 T *head(void);
chuanga 0:43ff9e3bc244 98
chuanga 0:43ff9e3bc244 99 /** Reset the buffer to 0. Useful if using head() to parse packeted data
chuanga 0:43ff9e3bc244 100 */
chuanga 0:43ff9e3bc244 101 void clear(void);
chuanga 0:43ff9e3bc244 102
chuanga 0:43ff9e3bc244 103 /** Determine if anything is readable in the buffer
chuanga 0:43ff9e3bc244 104 * @return 1 if something can be read, 0 otherwise
chuanga 0:43ff9e3bc244 105 */
chuanga 0:43ff9e3bc244 106 uint32_t available(void);
chuanga 0:43ff9e3bc244 107
chuanga 0:43ff9e3bc244 108 /** Overloaded operator for writing to the buffer
chuanga 0:43ff9e3bc244 109 * @param data Something to put in the buffer
chuanga 0:43ff9e3bc244 110 * @return
chuanga 0:43ff9e3bc244 111 */
chuanga 0:43ff9e3bc244 112 MyBuffer &operator= (T data)
chuanga 0:43ff9e3bc244 113 {
chuanga 0:43ff9e3bc244 114 put(data);
chuanga 0:43ff9e3bc244 115 return *this;
chuanga 0:43ff9e3bc244 116 }
chuanga 0:43ff9e3bc244 117
chuanga 0:43ff9e3bc244 118 /** Overloaded operator for reading from the buffer
chuanga 0:43ff9e3bc244 119 * @return Pull the oldest element from the buffer
chuanga 0:43ff9e3bc244 120 */
chuanga 0:43ff9e3bc244 121 operator int(void)
chuanga 0:43ff9e3bc244 122 {
chuanga 0:43ff9e3bc244 123 return get();
chuanga 0:43ff9e3bc244 124 }
chuanga 0:43ff9e3bc244 125
chuanga 0:43ff9e3bc244 126 uint32_t peek(char c);
chuanga 0:43ff9e3bc244 127
chuanga 0:43ff9e3bc244 128 };
chuanga 0:43ff9e3bc244 129
chuanga 0:43ff9e3bc244 130 template <class T>
chuanga 0:43ff9e3bc244 131 inline void MyBuffer<T>::put(T data)
chuanga 0:43ff9e3bc244 132 {
chuanga 0:43ff9e3bc244 133 _buf[_wloc++] = data;
chuanga 0:43ff9e3bc244 134 _wloc %= (_size - 1);
chuanga 0:43ff9e3bc244 135
chuanga 0:43ff9e3bc244 136 return;
chuanga 0:43ff9e3bc244 137 }
chuanga 0:43ff9e3bc244 138
chuanga 0:43ff9e3bc244 139 template <class T>
chuanga 0:43ff9e3bc244 140 inline T MyBuffer<T>::get(void)
chuanga 0:43ff9e3bc244 141 {
chuanga 0:43ff9e3bc244 142 T data_pos = _buf[_rloc++];
chuanga 0:43ff9e3bc244 143 _rloc %= (_size - 1);
chuanga 0:43ff9e3bc244 144
chuanga 0:43ff9e3bc244 145 return data_pos;
chuanga 0:43ff9e3bc244 146 }
chuanga 0:43ff9e3bc244 147
chuanga 0:43ff9e3bc244 148 template <class T>
chuanga 0:43ff9e3bc244 149 inline T *MyBuffer<T>::head(void)
chuanga 0:43ff9e3bc244 150 {
chuanga 0:43ff9e3bc244 151 T *data_pos = &_buf[0];
chuanga 0:43ff9e3bc244 152
chuanga 0:43ff9e3bc244 153 return data_pos;
chuanga 0:43ff9e3bc244 154 }
chuanga 0:43ff9e3bc244 155
chuanga 0:43ff9e3bc244 156 template <class T>
chuanga 0:43ff9e3bc244 157 inline uint32_t MyBuffer<T>::available(void)
chuanga 0:43ff9e3bc244 158 {
chuanga 0:43ff9e3bc244 159 return (_wloc == _rloc) ? 0 : 1;
chuanga 0:43ff9e3bc244 160 //return 1;
chuanga 0:43ff9e3bc244 161 }
chuanga 0:43ff9e3bc244 162
chuanga 0:43ff9e3bc244 163 #endif
chuanga 0:43ff9e3bc244 164