Example project to publish messages to a MQTT-SN broker using the u-blox SARA-N200 NB-IoT modem

Dependencies:   MQTTSNPacket X-NUCLEO-SARA-N200

Committer:
KeystoneElectronic
Date:
Thu Aug 23 11:32:34 2018 +0200
Revision:
12:9a2dab9b927d
Parent:
1:70b751b7a189
Update readme

Who changed what in which revision?

UserRevisionLine numberNew contents of line
keystone.electronic.solutions@gmail.com 1:70b751b7a189 1 /**
keystone.electronic.solutions@gmail.com 1:70b751b7a189 2 * @file FP.h
keystone.electronic.solutions@gmail.com 1:70b751b7a189 3 * @brief Core Utility - Templated Function Pointer Class
keystone.electronic.solutions@gmail.com 1:70b751b7a189 4 * @author sam grove
keystone.electronic.solutions@gmail.com 1:70b751b7a189 5 * @version 1.1
keystone.electronic.solutions@gmail.com 1:70b751b7a189 6 * @see http://mbed.org/users/sam_grove/code/FP/
keystone.electronic.solutions@gmail.com 1:70b751b7a189 7 *
keystone.electronic.solutions@gmail.com 1:70b751b7a189 8 * Copyright (c) 2013
keystone.electronic.solutions@gmail.com 1:70b751b7a189 9 *
keystone.electronic.solutions@gmail.com 1:70b751b7a189 10 * Licensed under the Apache License, Version 2.0 (the "License");
keystone.electronic.solutions@gmail.com 1:70b751b7a189 11 * you may not use this file except in compliance with the License.
keystone.electronic.solutions@gmail.com 1:70b751b7a189 12 * You may obtain a copy of the License at
keystone.electronic.solutions@gmail.com 1:70b751b7a189 13 *
keystone.electronic.solutions@gmail.com 1:70b751b7a189 14 * http://www.apache.org/licenses/LICENSE-2.0
keystone.electronic.solutions@gmail.com 1:70b751b7a189 15 *
keystone.electronic.solutions@gmail.com 1:70b751b7a189 16 * Unless required by applicable law or agreed to in writing, software
keystone.electronic.solutions@gmail.com 1:70b751b7a189 17 * distributed under the License is distributed on an "AS IS" BASIS,
keystone.electronic.solutions@gmail.com 1:70b751b7a189 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
keystone.electronic.solutions@gmail.com 1:70b751b7a189 19 * See the License for the specific language governing permissions and
keystone.electronic.solutions@gmail.com 1:70b751b7a189 20 * limitations under the License.
keystone.electronic.solutions@gmail.com 1:70b751b7a189 21 */
keystone.electronic.solutions@gmail.com 1:70b751b7a189 22
keystone.electronic.solutions@gmail.com 1:70b751b7a189 23 #ifndef FP_H
keystone.electronic.solutions@gmail.com 1:70b751b7a189 24 #define FP_H
keystone.electronic.solutions@gmail.com 1:70b751b7a189 25
keystone.electronic.solutions@gmail.com 1:70b751b7a189 26 /** Example using the FP Class with global functions
keystone.electronic.solutions@gmail.com 1:70b751b7a189 27 * @code
keystone.electronic.solutions@gmail.com 1:70b751b7a189 28 * #include "mbed.h"
keystone.electronic.solutions@gmail.com 1:70b751b7a189 29 * #include "FP.h"
keystone.electronic.solutions@gmail.com 1:70b751b7a189 30 *
keystone.electronic.solutions@gmail.com 1:70b751b7a189 31 * FP<void,bool>fp;
keystone.electronic.solutions@gmail.com 1:70b751b7a189 32 * DigitalOut myled(LED1);
keystone.electronic.solutions@gmail.com 1:70b751b7a189 33 *
keystone.electronic.solutions@gmail.com 1:70b751b7a189 34 * void handler(bool value)
keystone.electronic.solutions@gmail.com 1:70b751b7a189 35 * {
keystone.electronic.solutions@gmail.com 1:70b751b7a189 36 * myled = value;
keystone.electronic.solutions@gmail.com 1:70b751b7a189 37 * return;
keystone.electronic.solutions@gmail.com 1:70b751b7a189 38 * }
keystone.electronic.solutions@gmail.com 1:70b751b7a189 39 *
keystone.electronic.solutions@gmail.com 1:70b751b7a189 40 * int main()
keystone.electronic.solutions@gmail.com 1:70b751b7a189 41 * {
keystone.electronic.solutions@gmail.com 1:70b751b7a189 42 * fp.attach(&handler);
keystone.electronic.solutions@gmail.com 1:70b751b7a189 43 *
keystone.electronic.solutions@gmail.com 1:70b751b7a189 44 * while(1)
keystone.electronic.solutions@gmail.com 1:70b751b7a189 45 * {
keystone.electronic.solutions@gmail.com 1:70b751b7a189 46 * fp(1);
keystone.electronic.solutions@gmail.com 1:70b751b7a189 47 * wait(0.2);
keystone.electronic.solutions@gmail.com 1:70b751b7a189 48 * fp(0);
keystone.electronic.solutions@gmail.com 1:70b751b7a189 49 * wait(0.2);
keystone.electronic.solutions@gmail.com 1:70b751b7a189 50 * }
keystone.electronic.solutions@gmail.com 1:70b751b7a189 51 * }
keystone.electronic.solutions@gmail.com 1:70b751b7a189 52 * @endcode
keystone.electronic.solutions@gmail.com 1:70b751b7a189 53 */
keystone.electronic.solutions@gmail.com 1:70b751b7a189 54
keystone.electronic.solutions@gmail.com 1:70b751b7a189 55 /** Example using the FP Class with different class member functions
keystone.electronic.solutions@gmail.com 1:70b751b7a189 56 * @code
keystone.electronic.solutions@gmail.com 1:70b751b7a189 57 * #include "mbed.h"
keystone.electronic.solutions@gmail.com 1:70b751b7a189 58 * #include "FP.h"
keystone.electronic.solutions@gmail.com 1:70b751b7a189 59 *
keystone.electronic.solutions@gmail.com 1:70b751b7a189 60 * FP<void,bool>fp;
keystone.electronic.solutions@gmail.com 1:70b751b7a189 61 * DigitalOut myled(LED4);
keystone.electronic.solutions@gmail.com 1:70b751b7a189 62 *
keystone.electronic.solutions@gmail.com 1:70b751b7a189 63 * class Wrapper
keystone.electronic.solutions@gmail.com 1:70b751b7a189 64 * {
keystone.electronic.solutions@gmail.com 1:70b751b7a189 65 * public:
keystone.electronic.solutions@gmail.com 1:70b751b7a189 66 * Wrapper(){}
keystone.electronic.solutions@gmail.com 1:70b751b7a189 67 *
keystone.electronic.solutions@gmail.com 1:70b751b7a189 68 * void handler(bool value)
keystone.electronic.solutions@gmail.com 1:70b751b7a189 69 * {
keystone.electronic.solutions@gmail.com 1:70b751b7a189 70 * myled = value;
keystone.electronic.solutions@gmail.com 1:70b751b7a189 71 * return;
keystone.electronic.solutions@gmail.com 1:70b751b7a189 72 * }
keystone.electronic.solutions@gmail.com 1:70b751b7a189 73 * };
keystone.electronic.solutions@gmail.com 1:70b751b7a189 74 *
keystone.electronic.solutions@gmail.com 1:70b751b7a189 75 * int main()
keystone.electronic.solutions@gmail.com 1:70b751b7a189 76 * {
keystone.electronic.solutions@gmail.com 1:70b751b7a189 77 * Wrapper wrapped;
keystone.electronic.solutions@gmail.com 1:70b751b7a189 78 * fp.attach(&wrapped, &Wrapper::handler);
keystone.electronic.solutions@gmail.com 1:70b751b7a189 79 *
keystone.electronic.solutions@gmail.com 1:70b751b7a189 80 * while(1)
keystone.electronic.solutions@gmail.com 1:70b751b7a189 81 * {
keystone.electronic.solutions@gmail.com 1:70b751b7a189 82 * fp(1);
keystone.electronic.solutions@gmail.com 1:70b751b7a189 83 * wait(0.2);
keystone.electronic.solutions@gmail.com 1:70b751b7a189 84 * fp(0);
keystone.electronic.solutions@gmail.com 1:70b751b7a189 85 * wait(0.2);
keystone.electronic.solutions@gmail.com 1:70b751b7a189 86 * }
keystone.electronic.solutions@gmail.com 1:70b751b7a189 87 * }
keystone.electronic.solutions@gmail.com 1:70b751b7a189 88 * @endcode
keystone.electronic.solutions@gmail.com 1:70b751b7a189 89 */
keystone.electronic.solutions@gmail.com 1:70b751b7a189 90
keystone.electronic.solutions@gmail.com 1:70b751b7a189 91 /** Example using the FP Class with member FP and member function
keystone.electronic.solutions@gmail.com 1:70b751b7a189 92 * @code
keystone.electronic.solutions@gmail.com 1:70b751b7a189 93 * #include "mbed.h"
keystone.electronic.solutions@gmail.com 1:70b751b7a189 94 * #include "FP.h"
keystone.electronic.solutions@gmail.com 1:70b751b7a189 95 *
keystone.electronic.solutions@gmail.com 1:70b751b7a189 96 * DigitalOut myled(LED2);
keystone.electronic.solutions@gmail.com 1:70b751b7a189 97 *
keystone.electronic.solutions@gmail.com 1:70b751b7a189 98 * class Wrapper
keystone.electronic.solutions@gmail.com 1:70b751b7a189 99 * {
keystone.electronic.solutions@gmail.com 1:70b751b7a189 100 * public:
keystone.electronic.solutions@gmail.com 1:70b751b7a189 101 * Wrapper()
keystone.electronic.solutions@gmail.com 1:70b751b7a189 102 * {
keystone.electronic.solutions@gmail.com 1:70b751b7a189 103 * fp.attach(this, &Wrapper::handler);
keystone.electronic.solutions@gmail.com 1:70b751b7a189 104 * }
keystone.electronic.solutions@gmail.com 1:70b751b7a189 105 *
keystone.electronic.solutions@gmail.com 1:70b751b7a189 106 * void handler(bool value)
keystone.electronic.solutions@gmail.com 1:70b751b7a189 107 * {
keystone.electronic.solutions@gmail.com 1:70b751b7a189 108 * myled = value;
keystone.electronic.solutions@gmail.com 1:70b751b7a189 109 * return;
keystone.electronic.solutions@gmail.com 1:70b751b7a189 110 * }
keystone.electronic.solutions@gmail.com 1:70b751b7a189 111 *
keystone.electronic.solutions@gmail.com 1:70b751b7a189 112 * FP<void,bool>fp;
keystone.electronic.solutions@gmail.com 1:70b751b7a189 113 * };
keystone.electronic.solutions@gmail.com 1:70b751b7a189 114 *
keystone.electronic.solutions@gmail.com 1:70b751b7a189 115 * int main()
keystone.electronic.solutions@gmail.com 1:70b751b7a189 116 * {
keystone.electronic.solutions@gmail.com 1:70b751b7a189 117 * Wrapper wrapped;
keystone.electronic.solutions@gmail.com 1:70b751b7a189 118 *
keystone.electronic.solutions@gmail.com 1:70b751b7a189 119 * while(1)
keystone.electronic.solutions@gmail.com 1:70b751b7a189 120 * {
keystone.electronic.solutions@gmail.com 1:70b751b7a189 121 * wrapped.fp(1);
keystone.electronic.solutions@gmail.com 1:70b751b7a189 122 * wait(0.2);
keystone.electronic.solutions@gmail.com 1:70b751b7a189 123 * wrapped.fp(0);
keystone.electronic.solutions@gmail.com 1:70b751b7a189 124 * wait(0.2);
keystone.electronic.solutions@gmail.com 1:70b751b7a189 125 * }
keystone.electronic.solutions@gmail.com 1:70b751b7a189 126 * }
keystone.electronic.solutions@gmail.com 1:70b751b7a189 127 * @endcode
keystone.electronic.solutions@gmail.com 1:70b751b7a189 128 */
keystone.electronic.solutions@gmail.com 1:70b751b7a189 129
keystone.electronic.solutions@gmail.com 1:70b751b7a189 130 /**
keystone.electronic.solutions@gmail.com 1:70b751b7a189 131 * @class FP
keystone.electronic.solutions@gmail.com 1:70b751b7a189 132 * @brief API for managing Function Pointers
keystone.electronic.solutions@gmail.com 1:70b751b7a189 133 */
keystone.electronic.solutions@gmail.com 1:70b751b7a189 134 template<class retT, class argT>
keystone.electronic.solutions@gmail.com 1:70b751b7a189 135 class FP
keystone.electronic.solutions@gmail.com 1:70b751b7a189 136 {
keystone.electronic.solutions@gmail.com 1:70b751b7a189 137 public:
keystone.electronic.solutions@gmail.com 1:70b751b7a189 138 /** Create the FP object - only one callback can be attached to the object, that is
keystone.electronic.solutions@gmail.com 1:70b751b7a189 139 * a member function or a global function, not both at the same time
keystone.electronic.solutions@gmail.com 1:70b751b7a189 140 */
keystone.electronic.solutions@gmail.com 1:70b751b7a189 141 FP()
keystone.electronic.solutions@gmail.com 1:70b751b7a189 142 {
keystone.electronic.solutions@gmail.com 1:70b751b7a189 143 obj_callback = 0;
keystone.electronic.solutions@gmail.com 1:70b751b7a189 144 c_callback = 0;
keystone.electronic.solutions@gmail.com 1:70b751b7a189 145 }
keystone.electronic.solutions@gmail.com 1:70b751b7a189 146
keystone.electronic.solutions@gmail.com 1:70b751b7a189 147 /** Add a callback function to the object
keystone.electronic.solutions@gmail.com 1:70b751b7a189 148 * @param item - Address of the initialized object
keystone.electronic.solutions@gmail.com 1:70b751b7a189 149 * @param member - Address of the member function (dont forget the scope that the function is defined in)
keystone.electronic.solutions@gmail.com 1:70b751b7a189 150 */
keystone.electronic.solutions@gmail.com 1:70b751b7a189 151 template<class T>
keystone.electronic.solutions@gmail.com 1:70b751b7a189 152 void attach(T *item, retT (T::*method)(argT))
keystone.electronic.solutions@gmail.com 1:70b751b7a189 153 {
keystone.electronic.solutions@gmail.com 1:70b751b7a189 154 obj_callback = (FPtrDummy *)(item);
keystone.electronic.solutions@gmail.com 1:70b751b7a189 155 method_callback = (retT (FPtrDummy::*)(argT))(method);
keystone.electronic.solutions@gmail.com 1:70b751b7a189 156 return;
keystone.electronic.solutions@gmail.com 1:70b751b7a189 157 }
keystone.electronic.solutions@gmail.com 1:70b751b7a189 158
keystone.electronic.solutions@gmail.com 1:70b751b7a189 159 /** Add a callback function to the object
keystone.electronic.solutions@gmail.com 1:70b751b7a189 160 * @param function - The address of a globally defined function
keystone.electronic.solutions@gmail.com 1:70b751b7a189 161 */
keystone.electronic.solutions@gmail.com 1:70b751b7a189 162 void attach(retT (*function)(argT))
keystone.electronic.solutions@gmail.com 1:70b751b7a189 163 {
keystone.electronic.solutions@gmail.com 1:70b751b7a189 164 c_callback = function;
keystone.electronic.solutions@gmail.com 1:70b751b7a189 165 }
keystone.electronic.solutions@gmail.com 1:70b751b7a189 166
keystone.electronic.solutions@gmail.com 1:70b751b7a189 167 /** Invoke the function attached to the class
keystone.electronic.solutions@gmail.com 1:70b751b7a189 168 * @param arg - An argument that is passed into the function handler that is called
keystone.electronic.solutions@gmail.com 1:70b751b7a189 169 * @return The return from the function hanlder called by this class
keystone.electronic.solutions@gmail.com 1:70b751b7a189 170 */
keystone.electronic.solutions@gmail.com 1:70b751b7a189 171 retT operator()(argT arg) const
keystone.electronic.solutions@gmail.com 1:70b751b7a189 172 {
keystone.electronic.solutions@gmail.com 1:70b751b7a189 173 if( 0 != c_callback ) {
keystone.electronic.solutions@gmail.com 1:70b751b7a189 174 return obj_callback ? (obj_callback->*method_callback)(arg) : (*c_callback)(arg);
keystone.electronic.solutions@gmail.com 1:70b751b7a189 175 }
keystone.electronic.solutions@gmail.com 1:70b751b7a189 176 return (retT)0;
keystone.electronic.solutions@gmail.com 1:70b751b7a189 177 }
keystone.electronic.solutions@gmail.com 1:70b751b7a189 178
keystone.electronic.solutions@gmail.com 1:70b751b7a189 179 /** Determine if an callback is currently hooked
keystone.electronic.solutions@gmail.com 1:70b751b7a189 180 * @return 1 if a method is hooked, 0 otherwise
keystone.electronic.solutions@gmail.com 1:70b751b7a189 181 */
keystone.electronic.solutions@gmail.com 1:70b751b7a189 182 bool attached()
keystone.electronic.solutions@gmail.com 1:70b751b7a189 183 {
keystone.electronic.solutions@gmail.com 1:70b751b7a189 184 return obj_callback || c_callback;
keystone.electronic.solutions@gmail.com 1:70b751b7a189 185 }
keystone.electronic.solutions@gmail.com 1:70b751b7a189 186
keystone.electronic.solutions@gmail.com 1:70b751b7a189 187 /** Release a function from the callback hook
keystone.electronic.solutions@gmail.com 1:70b751b7a189 188 */
keystone.electronic.solutions@gmail.com 1:70b751b7a189 189 void detach()
keystone.electronic.solutions@gmail.com 1:70b751b7a189 190 {
keystone.electronic.solutions@gmail.com 1:70b751b7a189 191 obj_callback = 0;
keystone.electronic.solutions@gmail.com 1:70b751b7a189 192 c_callback = 0;
keystone.electronic.solutions@gmail.com 1:70b751b7a189 193 }
keystone.electronic.solutions@gmail.com 1:70b751b7a189 194
keystone.electronic.solutions@gmail.com 1:70b751b7a189 195 private:
keystone.electronic.solutions@gmail.com 1:70b751b7a189 196
keystone.electronic.solutions@gmail.com 1:70b751b7a189 197 // empty type used for casting
keystone.electronic.solutions@gmail.com 1:70b751b7a189 198 class FPtrDummy;
keystone.electronic.solutions@gmail.com 1:70b751b7a189 199
keystone.electronic.solutions@gmail.com 1:70b751b7a189 200 FPtrDummy *obj_callback;
keystone.electronic.solutions@gmail.com 1:70b751b7a189 201
keystone.electronic.solutions@gmail.com 1:70b751b7a189 202 /**
keystone.electronic.solutions@gmail.com 1:70b751b7a189 203 * @union Funciton
keystone.electronic.solutions@gmail.com 1:70b751b7a189 204 * @brief Member or global callback function
keystone.electronic.solutions@gmail.com 1:70b751b7a189 205 */
keystone.electronic.solutions@gmail.com 1:70b751b7a189 206 union {
keystone.electronic.solutions@gmail.com 1:70b751b7a189 207 retT (*c_callback)(argT); /*!< Footprint for a global function */
keystone.electronic.solutions@gmail.com 1:70b751b7a189 208 retT (FPtrDummy::*method_callback)(argT); /*!< Footprint for a member function */
keystone.electronic.solutions@gmail.com 1:70b751b7a189 209 };
keystone.electronic.solutions@gmail.com 1:70b751b7a189 210 };
keystone.electronic.solutions@gmail.com 1:70b751b7a189 211
keystone.electronic.solutions@gmail.com 1:70b751b7a189 212 #endif