|
| 1 | +/* Copyright 2017 Wirepas Ltd. All Rights Reserved. |
| 2 | + * |
| 3 | + * See file LICENSE.txt for full license details. |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * \file wms_app.h |
| 9 | + * |
| 10 | + * The global macros, types and functions available to applications can be found |
| 11 | + * in the wms_app.h header. |
| 12 | + */ |
| 13 | +#ifndef APP_H_ |
| 14 | +#define APP_H_ |
| 15 | + |
| 16 | +#include <stdlib.h> |
| 17 | +#include <stdint.h> |
| 18 | + |
| 19 | +/* TODO: Move to a separate header */ |
| 20 | +#define __STATIC_INLINE static inline |
| 21 | + |
| 22 | +/** |
| 23 | + * Global application API version. This is incremented if the global function |
| 24 | + * table format changes in an incompatible way. The value of this macro is |
| 25 | + * placed in the application header, so that the stack can detect and handle the |
| 26 | + * situation. |
| 27 | + */ |
| 28 | +#define APP_API_VERSION 0x200 |
| 29 | + |
| 30 | +/** Magic 16-byte string for locating a v2 application in Flash */ |
| 31 | +#define APP_V2_TAG ("APP2\171\306\073\165" \ |
| 32 | + "\263\303\334\322\035\266\006\115") |
| 33 | + |
| 34 | +/** Length of \ref APP_V2_TAG, in bytes */ |
| 35 | +#define APP_V2_TAG_LENGTH 16 |
| 36 | + |
| 37 | +/** Byte offset of \ref APP_V2_TAG from the start of application memory area */ |
| 38 | +#define APP_V2_TAG_OFFSET 48 |
| 39 | + |
| 40 | + |
| 41 | +/** Minimum supported application API version, when a tag is present: v2 */ |
| 42 | +#define APP_V2_TAG_MIN_API_VERSION 0x200 |
| 43 | + |
| 44 | +/** Application API version when no tag present: v1 */ |
| 45 | +#define APP_API_V1_VERSION 0x100 |
| 46 | + |
| 47 | +/** Container for \ref APP_V2_TAG, for pointer arithmetic */ |
| 48 | +typedef union |
| 49 | +{ |
| 50 | + uint8_t bytes[APP_V2_TAG_LENGTH]; |
| 51 | + uint32_t array[APP_V2_TAG_LENGTH / 4]; |
| 52 | +} app_v2_tag_t; |
| 53 | + |
| 54 | +/** |
| 55 | + * \brief Application information header |
| 56 | + * |
| 57 | + * If an application is compiled to support application API v2, this header |
| 58 | + * can found in the beginning of the application memory area. It is placed |
| 59 | + * right after the \ref APP_V2_TAG, which is at \ref APP_V2_TAG_OFFSET. |
| 60 | + */ |
| 61 | +typedef struct |
| 62 | +{ |
| 63 | + /** Expected API version of application */ |
| 64 | + uint32_t api_version; |
| 65 | + /** Expected start address of application memory area, for sanity checks */ |
| 66 | + uint32_t start_address; |
| 67 | + /** Total number of bytes used in the application memory area */ |
| 68 | + uint32_t length; |
| 69 | + /** First address used in RAM area (was forced to be 0 before) */ |
| 70 | + uint32_t start_ram_address; |
| 71 | +} app_information_header_t; |
| 72 | + |
| 73 | +/** |
| 74 | + * Firmware version type, returned from \ref |
| 75 | + * app_global_functions_t.getStackFirmwareVersion(). The individual sub-fields |
| 76 | + * can be accessed, or the whole 32-bit value can be read all at once, using |
| 77 | + * the version field. |
| 78 | + */ |
| 79 | +typedef union |
| 80 | +{ |
| 81 | + struct |
| 82 | + { |
| 83 | + /** Firmware development version number component */ |
| 84 | + uint8_t devel; |
| 85 | + /** Firmware maintenance version number component */ |
| 86 | + uint8_t maint; |
| 87 | + /** Firmware minor version number component */ |
| 88 | + uint8_t minor; |
| 89 | + /** Firmware major version number component */ |
| 90 | + uint8_t major; |
| 91 | + }; |
| 92 | + uint32_t version; |
| 93 | +} app_firmware_version_t; |
| 94 | + |
| 95 | +/** |
| 96 | + * Get the global API version, which may be greater than the \ref |
| 97 | + * APP_API_VERSION macro if the stack firmware is newer than the SDK used to |
| 98 | + * compile the application. It is up to the stack firmware to be backward |
| 99 | + * compatible with any global API changes, or not run the application at all. |
| 100 | + * \return Supported application API version, \ref APP_API_VERSION |
| 101 | + */ |
| 102 | +typedef uint32_t (*app_get_api_version_f)(void); |
| 103 | + |
| 104 | +/** |
| 105 | + * \brief Get stack firmware version |
| 106 | + * \return Stack firmware version |
| 107 | + */ |
| 108 | +typedef app_firmware_version_t (*app_get_stack_firmware_version_f)(void); |
| 109 | + |
| 110 | +/** |
| 111 | + * \brief Open a library |
| 112 | + * |
| 113 | + * This is the most important function of the Single-MCU API. All other |
| 114 | + * functions are in libraries, opened using the this function. Parameters are |
| 115 | + * the name and version of the library. The name is actually a 32-bit value, |
| 116 | + * which is defined in each library header as a macro: |
| 117 | + * \c APP_LIB_LIBRARY_NAME. The library version is also a macro defined in the same |
| 118 | + * library header: \c APP_LIB_LIBRARY_VERSION. |
| 119 | + * |
| 120 | + * Example of opening a library: |
| 121 | + * |
| 122 | + * @code |
| 123 | + * |
| 124 | + * // The System library |
| 125 | + * static const app_lib_system_t * lib_system = NULL; |
| 126 | + * |
| 127 | + * ... |
| 128 | + * |
| 129 | + * void App_init(const app_global_functions_t * functions) |
| 130 | + * { |
| 131 | + * lib_system = functions->openLibrary(APP_LIB_SYSTEM_NAME, |
| 132 | + * APP_LIB_SYSTEM_VERSION); |
| 133 | + * if (lib_system == NULL) |
| 134 | + * { |
| 135 | + * // Could not open the System library |
| 136 | + * return; |
| 137 | + * } |
| 138 | + * ... |
| 139 | + * } |
| 140 | + * @endcode |
| 141 | + * |
| 142 | + * \param name |
| 143 | + * Symbolic name of library, a macro in library header file |
| 144 | + * \param version |
| 145 | + * Requested library version, a macro in library header file |
| 146 | + * \return Pointer to the function table of the library. Each library has its |
| 147 | + * own function table. If a NULL is returned, the library could not be |
| 148 | + * opened. Either it didn't exist, or the version is too old or new. |
| 149 | + * \note There is no corresponding closeLibrary() call. Applications never |
| 150 | + * close libraries they open. |
| 151 | + */ |
| 152 | +typedef const void * (*app_open_library_f)(uint32_t name, uint32_t version); |
| 153 | + |
| 154 | +/** |
| 155 | + * \brief List of global functions, passed to \ref App_entrypoint() |
| 156 | + */ |
| 157 | +typedef struct |
| 158 | +{ |
| 159 | + app_get_api_version_f getApiVersion; |
| 160 | + app_get_stack_firmware_version_f getStackFirmwareVersion; |
| 161 | + app_open_library_f openLibrary; |
| 162 | +} app_global_functions_t; |
| 163 | + |
| 164 | +/** |
| 165 | + * \brief Application initial entrypoint |
| 166 | + * \param functions |
| 167 | + * Pointer to a global function table, \ref app_global_functions_t |
| 168 | + * \param reserved1 |
| 169 | + * Reserved for future use |
| 170 | + * \param reserved2 |
| 171 | + * Reserved for future use |
| 172 | + * \param ram_top |
| 173 | + * Pointer to a pointer of first free RAM address. The application may |
| 174 | + * reduce this value, if it does not need all the RAM provided to it |
| 175 | + */ |
| 176 | +intptr_t App_entrypoint(const void * functions, |
| 177 | + size_t reserved1, |
| 178 | + const void ** reserved2, |
| 179 | + void ** ram_top); |
| 180 | + |
| 181 | +/** A function to safely call either getApiVersion() or getCurrentRole() */ |
| 182 | +typedef uint32_t (*get_api_version_compatible_f)(void); |
| 183 | + |
| 184 | +__STATIC_INLINE uint32_t App_getApiVersion(const void * const global_cb) |
| 185 | +{ |
| 186 | + uint32_t version = |
| 187 | + ((const get_api_version_compatible_f)*(const void **)global_cb)(); |
| 188 | + if (version < APP_API_V1_VERSION) |
| 189 | + { |
| 190 | + // Called function was actually the getCurrentRole() function, |
| 191 | + // so the firmware only supports the v1 application API |
| 192 | + return APP_API_V1_VERSION; |
| 193 | + } |
| 194 | + return version; |
| 195 | +} |
| 196 | + |
| 197 | +/** |
| 198 | + * This is the most common return type from library functions. Functions use |
| 199 | + * these return values, unless more specific return values are required. |
| 200 | + */ |
| 201 | +typedef enum |
| 202 | +{ |
| 203 | + /** Everything is OK */ |
| 204 | + APP_RES_OK = 0, |
| 205 | + /** Error: Other or internal error */ |
| 206 | + APP_RES_UNSPECIFIED_ERROR = 1, |
| 207 | + /** Error: Feature is not implemented */ |
| 208 | + APP_RES_NOT_IMPLEMENTED = 2, |
| 209 | + /** Error: One or more parameter value is invalid */ |
| 210 | + APP_RES_INVALID_VALUE = 3, |
| 211 | + /** Error: One or more required pointer parameter is NULL */ |
| 212 | + APP_RES_INVALID_NULL_POINTER = 4, |
| 213 | + /** Error: Current configuration does not support the requested operation */ |
| 214 | + APP_RES_INVALID_CONFIGURATION = 5, |
| 215 | + /** Error: Requested resource is not available */ |
| 216 | + APP_RES_RESOURCE_UNAVAILABLE = 6, |
| 217 | + /** Error: Stack is in invalid state for the requested operation */ |
| 218 | + APP_RES_INVALID_STACK_STATE = 7, |
| 219 | + /** Error: Feature lock bits forbid the requested operation */ |
| 220 | + APP_RES_ACCESS_DENIED = 8, |
| 221 | +} app_res_e; |
| 222 | + |
| 223 | +/** |
| 224 | + * Node address. Each node must have a unique address in the network. There are |
| 225 | + * special addresses that can be used as destination addresses, see \ref |
| 226 | + * app_special_addr_e. For various addressing modes, see @c addressing. |
| 227 | + */ |
| 228 | +typedef uint32_t app_addr_t; |
| 229 | + |
| 230 | +/** |
| 231 | + * \brief Special destination addresses for sending packets |
| 232 | + */ |
| 233 | +typedef enum |
| 234 | +{ |
| 235 | + /** Send packet to the best available sink */ |
| 236 | + APP_ADDR_ANYSINK = 0xfffffffeu, |
| 237 | + /** |
| 238 | + * @brief Send packet as broadcast to all nodes |
| 239 | + * |
| 240 | + * @note When transmitting from a sink, note that a broadcast will only be |
| 241 | + * transmitted to the nodes directly under the sink's routing tree. |
| 242 | + * To reach all nodes on the network, it is necessary to send the |
| 243 | + * broadcast from all sinks. |
| 244 | + */ |
| 245 | + APP_ADDR_BROADCAST = 0xffffffffu, |
| 246 | + /** |
| 247 | + * This is a bitmask that should be ORed with group address to send data |
| 248 | + * packet to the multicast group. For example: 0x80000002 address sends |
| 249 | + * packet to the multicast group #2. |
| 250 | + */ |
| 251 | + APP_ADDR_MULTICAST = 0x80000000u, |
| 252 | + /** This is last valid multicast address. Addresses larger than this (until |
| 253 | + * @ref APP_ADDR_ANYSINK) are unicast addresses. */ |
| 254 | + APP_ADDR_MULTICAST_LAST = 0x80ffffffu, |
| 255 | +} app_special_addr_e; |
| 256 | + |
| 257 | +#endif /* APP_H_ */ |
0 commit comments