Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MQTTPacket
main.cpp@0:92f8cff1cf35, 2016-11-25 (annotated)
- Committer:
- jhamel
- Date:
- Fri Nov 25 16:12:09 2016 +0100
- Revision:
- 0:92f8cff1cf35
- Child:
- 3:a348ae67024e
Initial release
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jhamel | 0:92f8cff1cf35 | 1 | /* |
| jhamel | 0:92f8cff1cf35 | 2 | * Copyright (C) 2016 Orange |
| jhamel | 0:92f8cff1cf35 | 3 | * |
| jhamel | 0:92f8cff1cf35 | 4 | * This software is distributed under the terms and conditions of the 'BSD-3-Clause' |
| jhamel | 0:92f8cff1cf35 | 5 | * license which can be found in the file 'LICENSE.txt' in this package distribution |
| jhamel | 0:92f8cff1cf35 | 6 | * or at 'https://opensource.org/licenses/BSD-3-Clause'. |
| jhamel | 0:92f8cff1cf35 | 7 | */ |
| jhamel | 0:92f8cff1cf35 | 8 | |
| jhamel | 0:92f8cff1cf35 | 9 | |
| jhamel | 0:92f8cff1cf35 | 10 | /** |
| jhamel | 0:92f8cff1cf35 | 11 | * @file main.cpp |
| jhamel | 0:92f8cff1cf35 | 12 | * @brief A simple user application using all available LiveObjects iotsotbox-mqtt features |
| jhamel | 0:92f8cff1cf35 | 13 | */ |
| jhamel | 0:92f8cff1cf35 | 14 | |
| jhamel | 0:92f8cff1cf35 | 15 | #include "liveobjects_sample.h" |
| jhamel | 0:92f8cff1cf35 | 16 | |
| jhamel | 0:92f8cff1cf35 | 17 | #include <stdint.h> |
| jhamel | 0:92f8cff1cf35 | 18 | #include <string.h> |
| jhamel | 0:92f8cff1cf35 | 19 | |
| jhamel | 0:92f8cff1cf35 | 20 | #include "mbed-trace/mbed_trace.h" |
| jhamel | 0:92f8cff1cf35 | 21 | |
| jhamel | 0:92f8cff1cf35 | 22 | #include "mbed.h" |
| jhamel | 0:92f8cff1cf35 | 23 | #include "rtos.h" |
| jhamel | 0:92f8cff1cf35 | 24 | |
| jhamel | 0:92f8cff1cf35 | 25 | static const char* appv_version = "MBED SAMPLE V02.01"; |
| jhamel | 0:92f8cff1cf35 | 26 | |
| jhamel | 0:92f8cff1cf35 | 27 | #if 0 |
| jhamel | 0:92f8cff1cf35 | 28 | #define DBG_DFT_MAIN_LOG_LEVEL 3 |
| jhamel | 0:92f8cff1cf35 | 29 | #define DBG_DFT_LOMC_LOG_LEVEL 1 |
| jhamel | 0:92f8cff1cf35 | 30 | #define DBG_DFT_MBED_LOG_LEVEL TRACE_ACTIVE_LEVEL_ALL |
| jhamel | 0:92f8cff1cf35 | 31 | #else |
| jhamel | 0:92f8cff1cf35 | 32 | #define DBG_DFT_MAIN_LOG_LEVEL 0 |
| jhamel | 0:92f8cff1cf35 | 33 | #define DBG_DFT_LOMC_LOG_LEVEL 0 |
| jhamel | 0:92f8cff1cf35 | 34 | #define DBG_DFT_MBED_LOG_LEVEL TRACE_ACTIVE_LEVEL_INFO |
| jhamel | 0:92f8cff1cf35 | 35 | #endif |
| jhamel | 0:92f8cff1cf35 | 36 | |
| jhamel | 0:92f8cff1cf35 | 37 | // Two application threads: |
| jhamel | 0:92f8cff1cf35 | 38 | |
| jhamel | 0:92f8cff1cf35 | 39 | /// A very simple application thread |
| jhamel | 0:92f8cff1cf35 | 40 | Thread appli_thread; |
| jhamel | 0:92f8cff1cf35 | 41 | |
| jhamel | 0:92f8cff1cf35 | 42 | /// Thread to manage the input from console |
| jhamel | 0:92f8cff1cf35 | 43 | Thread console_thread; |
| jhamel | 0:92f8cff1cf35 | 44 | |
| jhamel | 0:92f8cff1cf35 | 45 | #if MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET |
| jhamel | 0:92f8cff1cf35 | 46 | #include "EthernetInterface.h" |
| jhamel | 0:92f8cff1cf35 | 47 | EthernetInterface eth; |
| jhamel | 0:92f8cff1cf35 | 48 | #endif |
| jhamel | 0:92f8cff1cf35 | 49 | |
| jhamel | 0:92f8cff1cf35 | 50 | NetworkInterface* appv_network_interface = NULL; |
| jhamel | 0:92f8cff1cf35 | 51 | const char* appv_network_id = NULL; |
| jhamel | 0:92f8cff1cf35 | 52 | |
| jhamel | 0:92f8cff1cf35 | 53 | Serial output(USBTX, USBRX); |
| jhamel | 0:92f8cff1cf35 | 54 | osThreadId appv_thread_id; |
| jhamel | 0:92f8cff1cf35 | 55 | uint8_t appv_log_level = DBG_DFT_MAIN_LOG_LEVEL; |
| jhamel | 0:92f8cff1cf35 | 56 | |
| jhamel | 0:92f8cff1cf35 | 57 | // ========================================================== |
| jhamel | 0:92f8cff1cf35 | 58 | // Green LED blinking according to the LiveObjects Connection state. |
| jhamel | 0:92f8cff1cf35 | 59 | |
| jhamel | 0:92f8cff1cf35 | 60 | /// Application state according to the connectivity. |
| jhamel | 0:92f8cff1cf35 | 61 | enum app_state_enum { |
| jhamel | 0:92f8cff1cf35 | 62 | APP_STATE_UNKNOWN = 0, ///< Unknown state |
| jhamel | 0:92f8cff1cf35 | 63 | APP_STATE_INIT, ///< Initilalization |
| jhamel | 0:92f8cff1cf35 | 64 | APP_STATE_NETWORK_READY, ///< Ethernet Network is ready. Device has IP address. |
| jhamel | 0:92f8cff1cf35 | 65 | APP_STATE_CONNECTING, ///< Connecting to the LiveObjects platform |
| jhamel | 0:92f8cff1cf35 | 66 | APP_STATE_CONNECTED, ///< Connected to the LiveObjects platform |
| jhamel | 0:92f8cff1cf35 | 67 | APP_STATE_DOWN ///< the LiveObjects thread is down (or stopped) |
| jhamel | 0:92f8cff1cf35 | 68 | } appv_state = APP_STATE_INIT; |
| jhamel | 0:92f8cff1cf35 | 69 | |
| jhamel | 0:92f8cff1cf35 | 70 | |
| jhamel | 0:92f8cff1cf35 | 71 | // ---------------------------------------------------------- |
| jhamel | 0:92f8cff1cf35 | 72 | // Status indication |
| jhamel | 0:92f8cff1cf35 | 73 | |
| jhamel | 0:92f8cff1cf35 | 74 | /// Green Status LED |
| jhamel | 0:92f8cff1cf35 | 75 | DigitalOut app_led_status(LED2, 0); |
| jhamel | 0:92f8cff1cf35 | 76 | |
| jhamel | 0:92f8cff1cf35 | 77 | /// Ticker to blink the Status LED |
| jhamel | 0:92f8cff1cf35 | 78 | Ticker app_led_ticker; |
| jhamel | 0:92f8cff1cf35 | 79 | |
| jhamel | 0:92f8cff1cf35 | 80 | /** |
| jhamel | 0:92f8cff1cf35 | 81 | * Called periodically by the Status LED ticker. |
| jhamel | 0:92f8cff1cf35 | 82 | */ |
| jhamel | 0:92f8cff1cf35 | 83 | void app_led_blinky(void) |
| jhamel | 0:92f8cff1cf35 | 84 | { |
| jhamel | 0:92f8cff1cf35 | 85 | static uint32_t appv_led_cnt = 0; |
| jhamel | 0:92f8cff1cf35 | 86 | static int32_t appv_led_state = -1; |
| jhamel | 0:92f8cff1cf35 | 87 | |
| jhamel | 0:92f8cff1cf35 | 88 | if (appv_state == APP_STATE_CONNECTED) { |
| jhamel | 0:92f8cff1cf35 | 89 | if (appv_led_state != 4) { |
| jhamel | 0:92f8cff1cf35 | 90 | appv_led_state = 4; |
| jhamel | 0:92f8cff1cf35 | 91 | appv_led_cnt = 0; |
| jhamel | 0:92f8cff1cf35 | 92 | app_led_status = 1; |
| jhamel | 0:92f8cff1cf35 | 93 | } |
| jhamel | 0:92f8cff1cf35 | 94 | else { |
| jhamel | 0:92f8cff1cf35 | 95 | if ((++appv_led_cnt%6) == 0) app_led_status = !app_led_status; |
| jhamel | 0:92f8cff1cf35 | 96 | } |
| jhamel | 0:92f8cff1cf35 | 97 | } |
| jhamel | 0:92f8cff1cf35 | 98 | #if 1 |
| jhamel | 0:92f8cff1cf35 | 99 | else if (appv_state == APP_STATE_CONNECTING) { |
| jhamel | 0:92f8cff1cf35 | 100 | if (appv_led_state != 3) { |
| jhamel | 0:92f8cff1cf35 | 101 | appv_led_state = 3; |
| jhamel | 0:92f8cff1cf35 | 102 | appv_led_cnt = 0; |
| jhamel | 0:92f8cff1cf35 | 103 | app_led_status = 0; |
| jhamel | 0:92f8cff1cf35 | 104 | } |
| jhamel | 0:92f8cff1cf35 | 105 | app_led_status = !app_led_status; |
| jhamel | 0:92f8cff1cf35 | 106 | } |
| jhamel | 0:92f8cff1cf35 | 107 | else if (appv_state == APP_STATE_NETWORK_READY) { |
| jhamel | 0:92f8cff1cf35 | 108 | if (appv_led_state != 1) { |
| jhamel | 0:92f8cff1cf35 | 109 | appv_led_state = 1; |
| jhamel | 0:92f8cff1cf35 | 110 | appv_led_cnt = 0; |
| jhamel | 0:92f8cff1cf35 | 111 | app_led_status = 0; |
| jhamel | 0:92f8cff1cf35 | 112 | } |
| jhamel | 0:92f8cff1cf35 | 113 | if ((++appv_led_cnt%4) == 0) app_led_status = !app_led_status; |
| jhamel | 0:92f8cff1cf35 | 114 | } |
| jhamel | 0:92f8cff1cf35 | 115 | else { |
| jhamel | 0:92f8cff1cf35 | 116 | if (appv_led_state != 2) { |
| jhamel | 0:92f8cff1cf35 | 117 | appv_led_state = 2; |
| jhamel | 0:92f8cff1cf35 | 118 | appv_led_cnt = 0; |
| jhamel | 0:92f8cff1cf35 | 119 | app_led_status = 0; |
| jhamel | 0:92f8cff1cf35 | 120 | } |
| jhamel | 0:92f8cff1cf35 | 121 | if ((++appv_led_cnt%2) == 0) app_led_status = !app_led_status; |
| jhamel | 0:92f8cff1cf35 | 122 | } |
| jhamel | 0:92f8cff1cf35 | 123 | #endif |
| jhamel | 0:92f8cff1cf35 | 124 | } |
| jhamel | 0:92f8cff1cf35 | 125 | |
| jhamel | 0:92f8cff1cf35 | 126 | |
| jhamel | 0:92f8cff1cf35 | 127 | // ========================================================== |
| jhamel | 0:92f8cff1cf35 | 128 | // mbed_trace |
| jhamel | 0:92f8cff1cf35 | 129 | // |
| jhamel | 0:92f8cff1cf35 | 130 | Mutex trace_mutex; |
| jhamel | 0:92f8cff1cf35 | 131 | extern "C" void trace_mutex_wait(void) { |
| jhamel | 0:92f8cff1cf35 | 132 | trace_mutex.lock(); |
| jhamel | 0:92f8cff1cf35 | 133 | } |
| jhamel | 0:92f8cff1cf35 | 134 | |
| jhamel | 0:92f8cff1cf35 | 135 | extern "C" void trace_mutex_release(void) { |
| jhamel | 0:92f8cff1cf35 | 136 | trace_mutex.unlock(); |
| jhamel | 0:92f8cff1cf35 | 137 | } |
| jhamel | 0:92f8cff1cf35 | 138 | |
| jhamel | 0:92f8cff1cf35 | 139 | // debug printf function |
| jhamel | 0:92f8cff1cf35 | 140 | extern "C" unsigned int rt_time_get (void); |
| jhamel | 0:92f8cff1cf35 | 141 | |
| jhamel | 0:92f8cff1cf35 | 142 | extern "C" void trace_printer(const char* str) { |
| jhamel | 0:92f8cff1cf35 | 143 | unsigned int clk = rt_time_get(); |
| jhamel | 0:92f8cff1cf35 | 144 | printf("%8u %s\r\n", clk, str); |
| jhamel | 0:92f8cff1cf35 | 145 | } |
| jhamel | 0:92f8cff1cf35 | 146 | |
| jhamel | 0:92f8cff1cf35 | 147 | extern "C" char* trace_prefix(size_t sz) { |
| jhamel | 0:92f8cff1cf35 | 148 | return (char*)" ** "; |
| jhamel | 0:92f8cff1cf35 | 149 | } |
| jhamel | 0:92f8cff1cf35 | 150 | |
| jhamel | 0:92f8cff1cf35 | 151 | // ---------------------------------------------------------- |
| jhamel | 0:92f8cff1cf35 | 152 | // |
| jhamel | 0:92f8cff1cf35 | 153 | static void app_trace_setup(void) |
| jhamel | 0:92f8cff1cf35 | 154 | { |
| jhamel | 0:92f8cff1cf35 | 155 | mbed_trace_init(); |
| jhamel | 0:92f8cff1cf35 | 156 | |
| jhamel | 0:92f8cff1cf35 | 157 | mbed_trace_print_function_set(trace_printer); |
| jhamel | 0:92f8cff1cf35 | 158 | mbed_trace_prefix_function_set(trace_prefix); |
| jhamel | 0:92f8cff1cf35 | 159 | |
| jhamel | 0:92f8cff1cf35 | 160 | mbed_trace_mutex_wait_function_set(trace_mutex_wait); |
| jhamel | 0:92f8cff1cf35 | 161 | mbed_trace_mutex_release_function_set(trace_mutex_release); |
| jhamel | 0:92f8cff1cf35 | 162 | |
| jhamel | 0:92f8cff1cf35 | 163 | uint8_t trace_msk = mbed_trace_config_get(); |
| jhamel | 0:92f8cff1cf35 | 164 | |
| jhamel | 0:92f8cff1cf35 | 165 | output.printf("trace_msk = x%X\r\n", trace_msk); |
| jhamel | 0:92f8cff1cf35 | 166 | |
| jhamel | 0:92f8cff1cf35 | 167 | // TRACE_ACTIVE_LEVEL_INFO , TRACE_ACTIVE_LEVEL_ALL |
| jhamel | 0:92f8cff1cf35 | 168 | // TRACE_MODE_COLOR or TRACE_MODE_PLAIN |
| jhamel | 0:92f8cff1cf35 | 169 | // TRACE_CARRIAGE_RETURN |
| jhamel | 0:92f8cff1cf35 | 170 | mbed_trace_config_set(DBG_DFT_MBED_LOG_LEVEL|TRACE_MODE_COLOR); |
| jhamel | 0:92f8cff1cf35 | 171 | } |
| jhamel | 0:92f8cff1cf35 | 172 | |
| jhamel | 0:92f8cff1cf35 | 173 | |
| jhamel | 0:92f8cff1cf35 | 174 | // ========================================================== |
| jhamel | 0:92f8cff1cf35 | 175 | // Network Initialization |
| jhamel | 0:92f8cff1cf35 | 176 | |
| jhamel | 0:92f8cff1cf35 | 177 | #if MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET |
| jhamel | 0:92f8cff1cf35 | 178 | // ---------------------------------------------------------- |
| jhamel | 0:92f8cff1cf35 | 179 | /// Network Initialization |
| jhamel | 0:92f8cff1cf35 | 180 | static int app_net_init(void) |
| jhamel | 0:92f8cff1cf35 | 181 | { |
| jhamel | 0:92f8cff1cf35 | 182 | int rc; |
| jhamel | 0:92f8cff1cf35 | 183 | output.printf("Using Ethernet - Get Dynamic IP Address ...\r\n"); |
| jhamel | 0:92f8cff1cf35 | 184 | |
| jhamel | 0:92f8cff1cf35 | 185 | appv_network_interface = NULL; |
| jhamel | 0:92f8cff1cf35 | 186 | rc = eth.connect(); |
| jhamel | 0:92f8cff1cf35 | 187 | if (rc != 0) { |
| jhamel | 0:92f8cff1cf35 | 188 | output.printf("\n\rConnection to Network Failed, rc=%d\r\n", rc); |
| jhamel | 0:92f8cff1cf35 | 189 | return rc; |
| jhamel | 0:92f8cff1cf35 | 190 | } |
| jhamel | 0:92f8cff1cf35 | 191 | const char *ip_addr = eth.get_ip_address(); |
| jhamel | 0:92f8cff1cf35 | 192 | if (ip_addr) { |
| jhamel | 0:92f8cff1cf35 | 193 | output.printf("Dynamic IP address %s\r\n", ip_addr); |
| jhamel | 0:92f8cff1cf35 | 194 | } else { |
| jhamel | 0:92f8cff1cf35 | 195 | output.printf("No IP address\r\n"); |
| jhamel | 0:92f8cff1cf35 | 196 | return -1; |
| jhamel | 0:92f8cff1cf35 | 197 | } |
| jhamel | 0:92f8cff1cf35 | 198 | |
| jhamel | 0:92f8cff1cf35 | 199 | appv_network_id = eth.get_mac_address(); |
| jhamel | 0:92f8cff1cf35 | 200 | if (appv_network_id) { |
| jhamel | 0:92f8cff1cf35 | 201 | output.printf("MAC address %s\r\n", appv_network_id); |
| jhamel | 0:92f8cff1cf35 | 202 | } |
| jhamel | 0:92f8cff1cf35 | 203 | else { |
| jhamel | 0:92f8cff1cf35 | 204 | output.printf("ERROR: No MAC address !!!!!!\r\n"); |
| jhamel | 0:92f8cff1cf35 | 205 | } |
| jhamel | 0:92f8cff1cf35 | 206 | |
| jhamel | 0:92f8cff1cf35 | 207 | appv_network_interface = ð |
| jhamel | 0:92f8cff1cf35 | 208 | |
| jhamel | 0:92f8cff1cf35 | 209 | return 0; |
| jhamel | 0:92f8cff1cf35 | 210 | } |
| jhamel | 0:92f8cff1cf35 | 211 | #endif |
| jhamel | 0:92f8cff1cf35 | 212 | |
| jhamel | 0:92f8cff1cf35 | 213 | |
| jhamel | 0:92f8cff1cf35 | 214 | // ========================================================== |
| jhamel | 0:92f8cff1cf35 | 215 | // |
| jhamel | 0:92f8cff1cf35 | 216 | // Live Objects IoT Client object (using iotsoftbox-mqtt library) |
| jhamel | 0:92f8cff1cf35 | 217 | // |
| jhamel | 0:92f8cff1cf35 | 218 | // - status information at connection |
| jhamel | 0:92f8cff1cf35 | 219 | // - collected data to send |
| jhamel | 0:92f8cff1cf35 | 220 | // - supported configuration parameters |
| jhamel | 0:92f8cff1cf35 | 221 | // - supported commands |
| jhamel | 0:92f8cff1cf35 | 222 | // - resources declaration (firmware, text file, etc.) |
| jhamel | 0:92f8cff1cf35 | 223 | |
| jhamel | 0:92f8cff1cf35 | 224 | |
| jhamel | 0:92f8cff1cf35 | 225 | |
| jhamel | 0:92f8cff1cf35 | 226 | // ---------------------------------------------------------- |
| jhamel | 0:92f8cff1cf35 | 227 | // STATUS data |
| jhamel | 0:92f8cff1cf35 | 228 | // |
| jhamel | 0:92f8cff1cf35 | 229 | |
| jhamel | 0:92f8cff1cf35 | 230 | int32_t appv_status_counter = 0; |
| jhamel | 0:92f8cff1cf35 | 231 | char appv_status_message[150] = "READY"; |
| jhamel | 0:92f8cff1cf35 | 232 | |
| jhamel | 0:92f8cff1cf35 | 233 | /// Set of status |
| jhamel | 0:92f8cff1cf35 | 234 | LiveObjectsD_Data_t appv_set_status[] = { |
| jhamel | 0:92f8cff1cf35 | 235 | { LOD_TYPE_STRING_C, "sample_version" , (void*)appv_version }, |
| jhamel | 0:92f8cff1cf35 | 236 | { LOD_TYPE_INT32, "sample_counter" , &appv_status_counter}, |
| jhamel | 0:92f8cff1cf35 | 237 | { LOD_TYPE_STRING_C, "sample_message" , appv_status_message} |
| jhamel | 0:92f8cff1cf35 | 238 | }; |
| jhamel | 0:92f8cff1cf35 | 239 | #define SET_STATUS_NB (sizeof(appv_set_status) / sizeof(LiveObjectsD_Data_t)) |
| jhamel | 0:92f8cff1cf35 | 240 | |
| jhamel | 0:92f8cff1cf35 | 241 | int appv_hdl_status = -1; |
| jhamel | 0:92f8cff1cf35 | 242 | |
| jhamel | 0:92f8cff1cf35 | 243 | |
| jhamel | 0:92f8cff1cf35 | 244 | // ---------------------------------------------------------- |
| jhamel | 0:92f8cff1cf35 | 245 | // 'COLLECTED DATA' |
| jhamel | 0:92f8cff1cf35 | 246 | // |
| jhamel | 0:92f8cff1cf35 | 247 | #define STREAM_PREFIX 0 |
| jhamel | 0:92f8cff1cf35 | 248 | |
| jhamel | 0:92f8cff1cf35 | 249 | |
| jhamel | 0:92f8cff1cf35 | 250 | uint8_t appv_measures_enabled = 1; |
| jhamel | 0:92f8cff1cf35 | 251 | |
| jhamel | 0:92f8cff1cf35 | 252 | int32_t appv_measures_temp_grad = -1; |
| jhamel | 0:92f8cff1cf35 | 253 | float appv_measures_volt_grad = -0.2; |
| jhamel | 0:92f8cff1cf35 | 254 | |
| jhamel | 0:92f8cff1cf35 | 255 | // contains a counter incremented after each data sent |
| jhamel | 0:92f8cff1cf35 | 256 | uint32_t appv_measures_counter = 0; |
| jhamel | 0:92f8cff1cf35 | 257 | |
| jhamel | 0:92f8cff1cf35 | 258 | // contains the temperature level |
| jhamel | 0:92f8cff1cf35 | 259 | int32_t appv_measures_temp = 20; |
| jhamel | 0:92f8cff1cf35 | 260 | |
| jhamel | 0:92f8cff1cf35 | 261 | // contains the battery level |
| jhamel | 0:92f8cff1cf35 | 262 | float appv_measures_volt = 5.0; |
| jhamel | 0:92f8cff1cf35 | 263 | |
| jhamel | 0:92f8cff1cf35 | 264 | /// Set of Collected data (published on a data stream) |
| jhamel | 0:92f8cff1cf35 | 265 | LiveObjectsD_Data_t appv_set_measures[] = { |
| jhamel | 0:92f8cff1cf35 | 266 | { LOD_TYPE_UINT32, "counter" , &appv_measures_counter}, |
| jhamel | 0:92f8cff1cf35 | 267 | { LOD_TYPE_INT32, "temperature" , &appv_measures_temp}, |
| jhamel | 0:92f8cff1cf35 | 268 | { LOD_TYPE_FLOAT, "battery_level" , &appv_measures_volt } |
| jhamel | 0:92f8cff1cf35 | 269 | }; |
| jhamel | 0:92f8cff1cf35 | 270 | #define SET_MEASURES_NB (sizeof(appv_set_measures) / sizeof(LiveObjectsD_Data_t)) |
| jhamel | 0:92f8cff1cf35 | 271 | |
| jhamel | 0:92f8cff1cf35 | 272 | int appv_hdl_data = -1; |
| jhamel | 0:92f8cff1cf35 | 273 | |
| jhamel | 0:92f8cff1cf35 | 274 | |
| jhamel | 0:92f8cff1cf35 | 275 | // ---------------------------------------------------------- |
| jhamel | 0:92f8cff1cf35 | 276 | // CONFIGURATION data |
| jhamel | 0:92f8cff1cf35 | 277 | // |
| jhamel | 0:92f8cff1cf35 | 278 | volatile uint32_t appv_cfg_timeout = 10; |
| jhamel | 0:92f8cff1cf35 | 279 | |
| jhamel | 0:92f8cff1cf35 | 280 | // a structure containg various kind of parameters (char[], int and float) |
| jhamel | 0:92f8cff1cf35 | 281 | struct conf_s { |
| jhamel | 0:92f8cff1cf35 | 282 | char name[20]; |
| jhamel | 0:92f8cff1cf35 | 283 | int32_t threshold; |
| jhamel | 0:92f8cff1cf35 | 284 | float gain; |
| jhamel | 0:92f8cff1cf35 | 285 | } appv_conf = { |
| jhamel | 0:92f8cff1cf35 | 286 | "TICTAC", |
| jhamel | 0:92f8cff1cf35 | 287 | -3, |
| jhamel | 0:92f8cff1cf35 | 288 | 1.05 |
| jhamel | 0:92f8cff1cf35 | 289 | }; |
| jhamel | 0:92f8cff1cf35 | 290 | |
| jhamel | 0:92f8cff1cf35 | 291 | // definition of identifer for each kind of parameters |
| jhamel | 0:92f8cff1cf35 | 292 | #define PARM_IDX_NAME 1 |
| jhamel | 0:92f8cff1cf35 | 293 | #define PARM_IDX_TIMEOUT 2 |
| jhamel | 0:92f8cff1cf35 | 294 | #define PARM_IDX_THRESHOLD 3 |
| jhamel | 0:92f8cff1cf35 | 295 | #define PARM_IDX_GAIN 4 |
| jhamel | 0:92f8cff1cf35 | 296 | |
| jhamel | 0:92f8cff1cf35 | 297 | /// Set of configuration parameters |
| jhamel | 0:92f8cff1cf35 | 298 | LiveObjectsD_Param_t appv_set_param[] = { |
| jhamel | 0:92f8cff1cf35 | 299 | { PARM_IDX_NAME, { LOD_TYPE_STRING_C, "name" , appv_conf.name } }, |
| jhamel | 0:92f8cff1cf35 | 300 | { PARM_IDX_TIMEOUT, { LOD_TYPE_UINT32, "timeout" , (void*)&appv_cfg_timeout } }, |
| jhamel | 0:92f8cff1cf35 | 301 | { PARM_IDX_THRESHOLD, { LOD_TYPE_INT32, "threshold" , &appv_conf.threshold } }, |
| jhamel | 0:92f8cff1cf35 | 302 | { PARM_IDX_GAIN, { LOD_TYPE_FLOAT, "gain" , &appv_conf.gain } } |
| jhamel | 0:92f8cff1cf35 | 303 | }; |
| jhamel | 0:92f8cff1cf35 | 304 | #define SET_PARAM_NB (sizeof(appv_set_param) / sizeof(LiveObjectsD_Param_t)) |
| jhamel | 0:92f8cff1cf35 | 305 | |
| jhamel | 0:92f8cff1cf35 | 306 | |
| jhamel | 0:92f8cff1cf35 | 307 | // ---------------------------------------------------------- |
| jhamel | 0:92f8cff1cf35 | 308 | // COMMANDS |
| jhamel | 0:92f8cff1cf35 | 309 | // Digital output to change the status of the RED LED |
| jhamel | 0:92f8cff1cf35 | 310 | DigitalOut app_led_user(LED1, 0); |
| jhamel | 0:92f8cff1cf35 | 311 | |
| jhamel | 0:92f8cff1cf35 | 312 | /// counter used to postpone the LED command response |
| jhamel | 0:92f8cff1cf35 | 313 | static int cmd_cnt = 0; |
| jhamel | 0:92f8cff1cf35 | 314 | |
| jhamel | 0:92f8cff1cf35 | 315 | #define CMD_IDX_RESET 1 |
| jhamel | 0:92f8cff1cf35 | 316 | #define CMD_IDX_LED 2 |
| jhamel | 0:92f8cff1cf35 | 317 | |
| jhamel | 0:92f8cff1cf35 | 318 | /// set of commands |
| jhamel | 0:92f8cff1cf35 | 319 | LiveObjectsD_Command_t appv_set_commands[] = { |
| jhamel | 0:92f8cff1cf35 | 320 | { CMD_IDX_RESET, "RESET" , 0}, |
| jhamel | 0:92f8cff1cf35 | 321 | { CMD_IDX_LED, "LED" , 0} |
| jhamel | 0:92f8cff1cf35 | 322 | }; |
| jhamel | 0:92f8cff1cf35 | 323 | #define SET_COMMANDS_NB (sizeof(appv_set_commands) / sizeof(LiveObjectsD_Command_t)) |
| jhamel | 0:92f8cff1cf35 | 324 | |
| jhamel | 0:92f8cff1cf35 | 325 | |
| jhamel | 0:92f8cff1cf35 | 326 | |
| jhamel | 0:92f8cff1cf35 | 327 | // ---------------------------------------------------------- |
| jhamel | 0:92f8cff1cf35 | 328 | // RESOURCE data |
| jhamel | 0:92f8cff1cf35 | 329 | // |
| jhamel | 0:92f8cff1cf35 | 330 | char appv_rsc_image[5*1024] = ""; |
| jhamel | 0:92f8cff1cf35 | 331 | |
| jhamel | 0:92f8cff1cf35 | 332 | |
| jhamel | 0:92f8cff1cf35 | 333 | char appv_rv_message[10] = "01.00"; |
| jhamel | 0:92f8cff1cf35 | 334 | char appv_rv_image[10] = "01.00"; |
| jhamel | 0:92f8cff1cf35 | 335 | |
| jhamel | 0:92f8cff1cf35 | 336 | #define RSC_IDX_MESSAGE 1 |
| jhamel | 0:92f8cff1cf35 | 337 | #define RSC_IDX_IMAGE 2 |
| jhamel | 0:92f8cff1cf35 | 338 | |
| jhamel | 0:92f8cff1cf35 | 339 | /// Set of resources |
| jhamel | 0:92f8cff1cf35 | 340 | LiveObjectsD_Resource_t appv_set_resources[] = { |
| jhamel | 0:92f8cff1cf35 | 341 | { RSC_IDX_MESSAGE, "message", appv_rv_message, sizeof(appv_rv_message)-1 }, // resource used to update appv_status_message |
| jhamel | 0:92f8cff1cf35 | 342 | { RSC_IDX_IMAGE, "image", appv_rv_image, sizeof(appv_rv_image)-1 } |
| jhamel | 0:92f8cff1cf35 | 343 | }; |
| jhamel | 0:92f8cff1cf35 | 344 | #define SET_RESOURCES_NB (sizeof(appv_set_resources) / sizeof(LiveObjectsD_Resource_t)) |
| jhamel | 0:92f8cff1cf35 | 345 | |
| jhamel | 0:92f8cff1cf35 | 346 | // variables used to process the current resource transfer |
| jhamel | 0:92f8cff1cf35 | 347 | uint32_t appv_rsc_size = 0; |
| jhamel | 0:92f8cff1cf35 | 348 | uint32_t appv_rsc_offset = 0; |
| jhamel | 0:92f8cff1cf35 | 349 | |
| jhamel | 0:92f8cff1cf35 | 350 | |
| jhamel | 0:92f8cff1cf35 | 351 | |
| jhamel | 0:92f8cff1cf35 | 352 | |
| jhamel | 0:92f8cff1cf35 | 353 | |
| jhamel | 0:92f8cff1cf35 | 354 | // ========================================================== |
| jhamel | 0:92f8cff1cf35 | 355 | // IotSoftbox-mqtt callback functions |
| jhamel | 0:92f8cff1cf35 | 356 | |
| jhamel | 0:92f8cff1cf35 | 357 | // ---------------------------------------------------------- |
| jhamel | 0:92f8cff1cf35 | 358 | // CONFIGURATION PARAMETERS Callback function |
| jhamel | 0:92f8cff1cf35 | 359 | // Check value in range, and copy string parameters |
| jhamel | 0:92f8cff1cf35 | 360 | |
| jhamel | 0:92f8cff1cf35 | 361 | |
| jhamel | 0:92f8cff1cf35 | 362 | /// Called (by the LiveObjects thread) to update configuration parameters |
| jhamel | 0:92f8cff1cf35 | 363 | extern "C" int main_cb_param_udp(const LiveObjectsD_Param_t* param_ptr, const void* value, int len) |
| jhamel | 0:92f8cff1cf35 | 364 | { |
| jhamel | 0:92f8cff1cf35 | 365 | if (param_ptr == NULL) { |
| jhamel | 0:92f8cff1cf35 | 366 | output.printf("UPDATE ERROR - invalid parameter x%p\r\n",param_ptr); |
| jhamel | 0:92f8cff1cf35 | 367 | return -1; |
| jhamel | 0:92f8cff1cf35 | 368 | } |
| jhamel | 0:92f8cff1cf35 | 369 | output.printf("UPDATE user_ref=%d %s ....\r\n", param_ptr->parm_uref, param_ptr->parm_data.data_name); |
| jhamel | 0:92f8cff1cf35 | 370 | switch(param_ptr->parm_uref) { |
| jhamel | 0:92f8cff1cf35 | 371 | case PARM_IDX_NAME: |
| jhamel | 0:92f8cff1cf35 | 372 | { |
| jhamel | 0:92f8cff1cf35 | 373 | output.printf("update name = %.*s\r\n", len , (const char*) value); |
| jhamel | 0:92f8cff1cf35 | 374 | if ((len > 0) && (len < (sizeof(appv_conf.name)-1))) { |
| jhamel | 0:92f8cff1cf35 | 375 | // Only c-string parameter must be updated by the user application (to check the string length) |
| jhamel | 0:92f8cff1cf35 | 376 | strncpy(appv_conf.name, (const char*) value, len); |
| jhamel | 0:92f8cff1cf35 | 377 | appv_conf.name[len] = 0; |
| jhamel | 0:92f8cff1cf35 | 378 | return 0; // OK. |
| jhamel | 0:92f8cff1cf35 | 379 | } |
| jhamel | 0:92f8cff1cf35 | 380 | } |
| jhamel | 0:92f8cff1cf35 | 381 | break; |
| jhamel | 0:92f8cff1cf35 | 382 | |
| jhamel | 0:92f8cff1cf35 | 383 | case PARM_IDX_TIMEOUT: |
| jhamel | 0:92f8cff1cf35 | 384 | { |
| jhamel | 0:92f8cff1cf35 | 385 | uint32_t timeout = *((const uint32_t*)value); |
| jhamel | 0:92f8cff1cf35 | 386 | output.printf("update timeout = %"PRIu32"\r\n", timeout); |
| jhamel | 0:92f8cff1cf35 | 387 | if ((timeout > 0) && (timeout <= 120) && (timeout != appv_cfg_timeout)) { |
| jhamel | 0:92f8cff1cf35 | 388 | return 0; // primitive parameter is updated by library |
| jhamel | 0:92f8cff1cf35 | 389 | } |
| jhamel | 0:92f8cff1cf35 | 390 | } |
| jhamel | 0:92f8cff1cf35 | 391 | break; |
| jhamel | 0:92f8cff1cf35 | 392 | case PARM_IDX_THRESHOLD: |
| jhamel | 0:92f8cff1cf35 | 393 | { |
| jhamel | 0:92f8cff1cf35 | 394 | int32_t threshold = *((const int32_t*)value); |
| jhamel | 0:92f8cff1cf35 | 395 | output.printf("update threshold = %"PRIi32"\r\n", threshold); |
| jhamel | 0:92f8cff1cf35 | 396 | if ((threshold >= -10) && (threshold <= 10) && (threshold != appv_conf.threshold)) { |
| jhamel | 0:92f8cff1cf35 | 397 | return 0; // primitive parameter is updated by library |
| jhamel | 0:92f8cff1cf35 | 398 | } |
| jhamel | 0:92f8cff1cf35 | 399 | } |
| jhamel | 0:92f8cff1cf35 | 400 | break; |
| jhamel | 0:92f8cff1cf35 | 401 | case PARM_IDX_GAIN: |
| jhamel | 0:92f8cff1cf35 | 402 | { |
| jhamel | 0:92f8cff1cf35 | 403 | float gain = *((const float*)value); |
| jhamel | 0:92f8cff1cf35 | 404 | output.printf("update gain = %f\r\n", gain); |
| jhamel | 0:92f8cff1cf35 | 405 | if ((gain > 0.0) && (gain < 10.0) && (gain != appv_conf.gain)) { |
| jhamel | 0:92f8cff1cf35 | 406 | return 0; // primitive parameter is updated by library |
| jhamel | 0:92f8cff1cf35 | 407 | } |
| jhamel | 0:92f8cff1cf35 | 408 | } |
| jhamel | 0:92f8cff1cf35 | 409 | break; |
| jhamel | 0:92f8cff1cf35 | 410 | } |
| jhamel | 0:92f8cff1cf35 | 411 | output.printf("ERROR to update param[%d] %s !!!\r\n", param_ptr->parm_uref, param_ptr->parm_data.data_name); |
| jhamel | 0:92f8cff1cf35 | 412 | return -1; |
| jhamel | 0:92f8cff1cf35 | 413 | } |
| jhamel | 0:92f8cff1cf35 | 414 | |
| jhamel | 0:92f8cff1cf35 | 415 | // ---------------------------------------------------------- |
| jhamel | 0:92f8cff1cf35 | 416 | // RESOURCE Callback Functions |
| jhamel | 0:92f8cff1cf35 | 417 | |
| jhamel | 0:92f8cff1cf35 | 418 | /** |
| jhamel | 0:92f8cff1cf35 | 419 | * Called (by the LiveObjects thread) to notify either, |
| jhamel | 0:92f8cff1cf35 | 420 | * - state = 0 : the begin of resource request |
| jhamel | 0:92f8cff1cf35 | 421 | * - state = 1 : the end without error |
| jhamel | 0:92f8cff1cf35 | 422 | * - state != 1 : the end with an error |
| jhamel | 0:92f8cff1cf35 | 423 | */ |
| jhamel | 0:92f8cff1cf35 | 424 | extern "C" LiveObjectsD_ResourceRespCode_t main_cb_rsc_ntfy ( |
| jhamel | 0:92f8cff1cf35 | 425 | uint8_t state, const LiveObjectsD_Resource_t* rsc_ptr, |
| jhamel | 0:92f8cff1cf35 | 426 | const char* version_old, const char* version_new, uint32_t size) |
| jhamel | 0:92f8cff1cf35 | 427 | { |
| jhamel | 0:92f8cff1cf35 | 428 | LiveObjectsD_ResourceRespCode_t ret = RSC_RSP_OK; // OK to update the resource |
| jhamel | 0:92f8cff1cf35 | 429 | |
| jhamel | 0:92f8cff1cf35 | 430 | output.printf("*** rsc_ntfy: ...\r\n"); |
| jhamel | 0:92f8cff1cf35 | 431 | |
| jhamel | 0:92f8cff1cf35 | 432 | if ((rsc_ptr) && (rsc_ptr->rsc_uref > 0) && (rsc_ptr->rsc_uref <= SET_RESOURCES_NB)) { |
| jhamel | 0:92f8cff1cf35 | 433 | output.printf("*** user ref = %d\r\n", rsc_ptr->rsc_uref); |
| jhamel | 0:92f8cff1cf35 | 434 | output.printf("*** name = %s\r\n", rsc_ptr->rsc_name); |
| jhamel | 0:92f8cff1cf35 | 435 | output.printf("*** version_old = %s\r\n", version_old); |
| jhamel | 0:92f8cff1cf35 | 436 | output.printf("*** version_new = %s\r\n", version_new); |
| jhamel | 0:92f8cff1cf35 | 437 | output.printf("*** size = %u\r\n", size); |
| jhamel | 0:92f8cff1cf35 | 438 | if (state) { |
| jhamel | 0:92f8cff1cf35 | 439 | if (state == 1) { // Completed without error |
| jhamel | 0:92f8cff1cf35 | 440 | output.printf("*** state = COMPLETED without error\r\n"); |
| jhamel | 0:92f8cff1cf35 | 441 | // Update version |
| jhamel | 0:92f8cff1cf35 | 442 | output.printf(" ===> UPDATE - version %s to %s\r\n", rsc_ptr->rsc_version_ptr, version_new); |
| jhamel | 0:92f8cff1cf35 | 443 | strncpy((char*)rsc_ptr->rsc_version_ptr, version_new, rsc_ptr->rsc_version_sz); |
| jhamel | 0:92f8cff1cf35 | 444 | |
| jhamel | 0:92f8cff1cf35 | 445 | if (rsc_ptr->rsc_uref == RSC_IDX_IMAGE) { |
| jhamel | 0:92f8cff1cf35 | 446 | trace_mutex_wait(); |
| jhamel | 0:92f8cff1cf35 | 447 | output.printf("\r\n\r\n"); |
| jhamel | 0:92f8cff1cf35 | 448 | output.printf(appv_rsc_image); |
| jhamel | 0:92f8cff1cf35 | 449 | output.printf("\r\n\r\n"); |
| jhamel | 0:92f8cff1cf35 | 450 | trace_mutex_release(); |
| jhamel | 0:92f8cff1cf35 | 451 | } |
| jhamel | 0:92f8cff1cf35 | 452 | } |
| jhamel | 0:92f8cff1cf35 | 453 | else { |
| jhamel | 0:92f8cff1cf35 | 454 | output.printf("*** state = COMPLETED with error !!!!\r\n"); |
| jhamel | 0:92f8cff1cf35 | 455 | // Roll back ? |
| jhamel | 0:92f8cff1cf35 | 456 | } |
| jhamel | 0:92f8cff1cf35 | 457 | appv_rsc_offset = 0; |
| jhamel | 0:92f8cff1cf35 | 458 | appv_rsc_size = 0; |
| jhamel | 0:92f8cff1cf35 | 459 | |
| jhamel | 0:92f8cff1cf35 | 460 | // Push Status (message has been updated or not) |
| jhamel | 0:92f8cff1cf35 | 461 | LiveObjectsClient_PushStatus( appv_hdl_status ); |
| jhamel | 0:92f8cff1cf35 | 462 | } |
| jhamel | 0:92f8cff1cf35 | 463 | else { |
| jhamel | 0:92f8cff1cf35 | 464 | appv_rsc_offset = 0; |
| jhamel | 0:92f8cff1cf35 | 465 | ret = RSC_RSP_ERR_NOT_AUTHORIZED; |
| jhamel | 0:92f8cff1cf35 | 466 | switch (rsc_ptr->rsc_uref ) { |
| jhamel | 0:92f8cff1cf35 | 467 | case RSC_IDX_MESSAGE: |
| jhamel | 0:92f8cff1cf35 | 468 | if (size < (sizeof(appv_status_message)-1)) { |
| jhamel | 0:92f8cff1cf35 | 469 | ret = RSC_RSP_OK; |
| jhamel | 0:92f8cff1cf35 | 470 | } |
| jhamel | 0:92f8cff1cf35 | 471 | break; |
| jhamel | 0:92f8cff1cf35 | 472 | case RSC_IDX_IMAGE: |
| jhamel | 0:92f8cff1cf35 | 473 | if (size < (sizeof(appv_rsc_image)-1)) { |
| jhamel | 0:92f8cff1cf35 | 474 | ret = RSC_RSP_OK; |
| jhamel | 0:92f8cff1cf35 | 475 | } |
| jhamel | 0:92f8cff1cf35 | 476 | break; |
| jhamel | 0:92f8cff1cf35 | 477 | } |
| jhamel | 0:92f8cff1cf35 | 478 | if (ret == RSC_RSP_OK) { |
| jhamel | 0:92f8cff1cf35 | 479 | appv_rsc_size = size; |
| jhamel | 0:92f8cff1cf35 | 480 | output.printf("*** state = START - ACCEPTED\r\n");; |
| jhamel | 0:92f8cff1cf35 | 481 | } |
| jhamel | 0:92f8cff1cf35 | 482 | else { |
| jhamel | 0:92f8cff1cf35 | 483 | appv_rsc_size = 0; |
| jhamel | 0:92f8cff1cf35 | 484 | output.printf("*** state = START - REFUSED\r\n"); |
| jhamel | 0:92f8cff1cf35 | 485 | } |
| jhamel | 0:92f8cff1cf35 | 486 | } |
| jhamel | 0:92f8cff1cf35 | 487 | } |
| jhamel | 0:92f8cff1cf35 | 488 | else { |
| jhamel | 0:92f8cff1cf35 | 489 | output.printf("*** UNKNOWN USER REF (x%p %d) in state=%d\r\n", rsc_ptr, rsc_ptr->rsc_uref, state); |
| jhamel | 0:92f8cff1cf35 | 490 | ret = RSC_RSP_ERR_INVALID_RESOURCE; |
| jhamel | 0:92f8cff1cf35 | 491 | } |
| jhamel | 0:92f8cff1cf35 | 492 | return ret; |
| jhamel | 0:92f8cff1cf35 | 493 | } |
| jhamel | 0:92f8cff1cf35 | 494 | |
| jhamel | 0:92f8cff1cf35 | 495 | /** |
| jhamel | 0:92f8cff1cf35 | 496 | * Called (by the LiveObjects thread) to request the user |
| jhamel | 0:92f8cff1cf35 | 497 | * to read data from current resource transfer. |
| jhamel | 0:92f8cff1cf35 | 498 | */ |
| jhamel | 0:92f8cff1cf35 | 499 | extern "C" int main_cb_rsc_data (const LiveObjectsD_Resource_t* rsc_ptr, uint32_t offset) |
| jhamel | 0:92f8cff1cf35 | 500 | { |
| jhamel | 0:92f8cff1cf35 | 501 | int ret; |
| jhamel | 0:92f8cff1cf35 | 502 | |
| jhamel | 0:92f8cff1cf35 | 503 | if (appv_log_level > 1) output.printf("*** rsc_data: rsc[%d]='%s' offset=%u - data ready ...\r\n", rsc_ptr->rsc_uref, rsc_ptr->rsc_name, offset); |
| jhamel | 0:92f8cff1cf35 | 504 | |
| jhamel | 0:92f8cff1cf35 | 505 | if (rsc_ptr->rsc_uref == RSC_IDX_MESSAGE) { |
| jhamel | 0:92f8cff1cf35 | 506 | char buf[40]; |
| jhamel | 0:92f8cff1cf35 | 507 | if (offset > (sizeof(appv_status_message)-1)) { |
| jhamel | 0:92f8cff1cf35 | 508 | output.printf("*** rsc_data: rsc[%d]='%s' offset=%u > %d - OUT OF ARRAY\r\n", |
| jhamel | 0:92f8cff1cf35 | 509 | rsc_ptr->rsc_uref, rsc_ptr->rsc_name, offset, sizeof(appv_status_message)-1); |
| jhamel | 0:92f8cff1cf35 | 510 | return -1; |
| jhamel | 0:92f8cff1cf35 | 511 | } |
| jhamel | 0:92f8cff1cf35 | 512 | ret = LiveObjectsClient_RscGetChunck(rsc_ptr, buf, sizeof(buf)-1); |
| jhamel | 0:92f8cff1cf35 | 513 | if (ret > 0) { |
| jhamel | 0:92f8cff1cf35 | 514 | if ((offset+ret) > (sizeof(appv_status_message)-1)) { |
| jhamel | 0:92f8cff1cf35 | 515 | output.printf("*** rsc_data: rsc[%d]='%s' offset=%u - read=%d => %d > %d - OUT OF ARRAY\r\n", |
| jhamel | 0:92f8cff1cf35 | 516 | rsc_ptr->rsc_uref, rsc_ptr->rsc_name, offset, ret, offset + ret, sizeof(appv_status_message)-1); |
| jhamel | 0:92f8cff1cf35 | 517 | return -1; |
| jhamel | 0:92f8cff1cf35 | 518 | } |
| jhamel | 0:92f8cff1cf35 | 519 | appv_rsc_offset += ret; |
| jhamel | 0:92f8cff1cf35 | 520 | memcpy(&appv_status_message[offset], buf, ret); |
| jhamel | 0:92f8cff1cf35 | 521 | appv_status_message[offset+ret] = 0; |
| jhamel | 0:92f8cff1cf35 | 522 | output.printf("*** rsc_data: rsc[%d]='%s' offset=%u - read=%d/%d '%s'\r\n", |
| jhamel | 0:92f8cff1cf35 | 523 | rsc_ptr->rsc_uref, rsc_ptr->rsc_name, offset, ret, sizeof(buf)-1, appv_status_message); |
| jhamel | 0:92f8cff1cf35 | 524 | } |
| jhamel | 0:92f8cff1cf35 | 525 | } |
| jhamel | 0:92f8cff1cf35 | 526 | else if (rsc_ptr->rsc_uref == RSC_IDX_IMAGE) { |
| jhamel | 0:92f8cff1cf35 | 527 | if (offset > (sizeof(appv_rsc_image)-1)) { |
| jhamel | 0:92f8cff1cf35 | 528 | output.printf("*** rsc_data: rsc[%d]='%s' offset=%u > %d - OUT OF ARRAY\r\n", |
| jhamel | 0:92f8cff1cf35 | 529 | rsc_ptr->rsc_uref, rsc_ptr->rsc_name, offset, sizeof(appv_rsc_image)-1); |
| jhamel | 0:92f8cff1cf35 | 530 | return -1; |
| jhamel | 0:92f8cff1cf35 | 531 | } |
| jhamel | 0:92f8cff1cf35 | 532 | int data_len = sizeof(appv_rsc_image) - offset - 1; |
| jhamel | 0:92f8cff1cf35 | 533 | ret = LiveObjectsClient_RscGetChunck(rsc_ptr, &appv_rsc_image[offset], data_len); |
| jhamel | 0:92f8cff1cf35 | 534 | if (ret > 0) { |
| jhamel | 0:92f8cff1cf35 | 535 | if ((offset+ret) > (sizeof(appv_rsc_image)-1)) { |
| jhamel | 0:92f8cff1cf35 | 536 | output.printf("*** rsc_data: rsc[%d]='%s' offset=%u - read=%d => %d > %d - OUT OF ARRAY\r\n", |
| jhamel | 0:92f8cff1cf35 | 537 | rsc_ptr->rsc_uref, rsc_ptr->rsc_name, offset, ret, offset + ret, sizeof(appv_rsc_image)-1); |
| jhamel | 0:92f8cff1cf35 | 538 | return -1; |
| jhamel | 0:92f8cff1cf35 | 539 | } |
| jhamel | 0:92f8cff1cf35 | 540 | appv_rsc_offset += ret; |
| jhamel | 0:92f8cff1cf35 | 541 | if (appv_log_level > 0) |
| jhamel | 0:92f8cff1cf35 | 542 | output.printf("*** rsc_data: rsc[%d]='%s' offset=%u - read=%d/%d - %u/%u\r\n", |
| jhamel | 0:92f8cff1cf35 | 543 | rsc_ptr->rsc_uref, rsc_ptr->rsc_name, offset, ret, data_len, appv_rsc_offset, appv_rsc_size); |
| jhamel | 0:92f8cff1cf35 | 544 | } |
| jhamel | 0:92f8cff1cf35 | 545 | else { |
| jhamel | 0:92f8cff1cf35 | 546 | output.printf("*** rsc_data: rsc[%d]='%s' offset=%u - read error (%d) - %u/%u\r\n", |
| jhamel | 0:92f8cff1cf35 | 547 | rsc_ptr->rsc_uref, rsc_ptr->rsc_name, offset, ret, appv_rsc_offset, appv_rsc_size); |
| jhamel | 0:92f8cff1cf35 | 548 | } |
| jhamel | 0:92f8cff1cf35 | 549 | } |
| jhamel | 0:92f8cff1cf35 | 550 | else { |
| jhamel | 0:92f8cff1cf35 | 551 | ret = -1; |
| jhamel | 0:92f8cff1cf35 | 552 | } |
| jhamel | 0:92f8cff1cf35 | 553 | |
| jhamel | 0:92f8cff1cf35 | 554 | return ret; |
| jhamel | 0:92f8cff1cf35 | 555 | } |
| jhamel | 0:92f8cff1cf35 | 556 | |
| jhamel | 0:92f8cff1cf35 | 557 | // ---------------------------------------------------------- |
| jhamel | 0:92f8cff1cf35 | 558 | // COMMAND Callback Functions |
| jhamel | 0:92f8cff1cf35 | 559 | |
| jhamel | 0:92f8cff1cf35 | 560 | static int main_cmd_doSystemReset(LiveObjectsD_CommandRequestBlock_t* pCmdReqBlk); |
| jhamel | 0:92f8cff1cf35 | 561 | static int main_cmd_doLED(LiveObjectsD_CommandRequestBlock_t* pCmdReqBlk); |
| jhamel | 0:92f8cff1cf35 | 562 | |
| jhamel | 0:92f8cff1cf35 | 563 | /// Called (by the LiveObjects thread) to perform an 'attached/registered' command |
| jhamel | 0:92f8cff1cf35 | 564 | extern "C" int main_cb_command(LiveObjectsD_CommandRequestBlock_t* pCmdReqBlk) |
| jhamel | 0:92f8cff1cf35 | 565 | { |
| jhamel | 0:92f8cff1cf35 | 566 | int ret; |
| jhamel | 0:92f8cff1cf35 | 567 | const LiveObjectsD_Command_t* cmd_ptr; |
| jhamel | 0:92f8cff1cf35 | 568 | |
| jhamel | 0:92f8cff1cf35 | 569 | if ((pCmdReqBlk == NULL) || (pCmdReqBlk->hd.cmd_ptr == NULL) || (pCmdReqBlk->hd.cmd_cid == 0) ) { |
| jhamel | 0:92f8cff1cf35 | 570 | output.printf("**** COMMAND : ERROR, Invalid parameter\r\n"); |
| jhamel | 0:92f8cff1cf35 | 571 | return -1; |
| jhamel | 0:92f8cff1cf35 | 572 | } |
| jhamel | 0:92f8cff1cf35 | 573 | |
| jhamel | 0:92f8cff1cf35 | 574 | cmd_ptr = pCmdReqBlk->hd.cmd_ptr; |
| jhamel | 0:92f8cff1cf35 | 575 | output.printf("**** COMMAND %d %s - cid=%d\r\n", cmd_ptr->cmd_uref, cmd_ptr->cmd_name, pCmdReqBlk->hd.cmd_cid); |
| jhamel | 0:92f8cff1cf35 | 576 | { |
| jhamel | 0:92f8cff1cf35 | 577 | int i; |
| jhamel | 0:92f8cff1cf35 | 578 | output.printf("**** ARGS %d : \r\n", pCmdReqBlk->hd.cmd_args_nb); |
| jhamel | 0:92f8cff1cf35 | 579 | for (i=0; i < pCmdReqBlk->hd.cmd_args_nb; i++) { |
| jhamel | 0:92f8cff1cf35 | 580 | output.printf("**** ARG [%d] (%d) : %s %s\r\n", i, pCmdReqBlk->args_array[i].arg_type, |
| jhamel | 0:92f8cff1cf35 | 581 | pCmdReqBlk->args_array[i].arg_name, pCmdReqBlk->args_array[i].arg_value); |
| jhamel | 0:92f8cff1cf35 | 582 | } |
| jhamel | 0:92f8cff1cf35 | 583 | } |
| jhamel | 0:92f8cff1cf35 | 584 | |
| jhamel | 0:92f8cff1cf35 | 585 | switch(cmd_ptr->cmd_uref) { |
| jhamel | 0:92f8cff1cf35 | 586 | case CMD_IDX_RESET: // RESET |
| jhamel | 0:92f8cff1cf35 | 587 | output.printf("main_callbackCommand: command[%d] %s\r\n", cmd_ptr->cmd_uref, cmd_ptr->cmd_name); |
| jhamel | 0:92f8cff1cf35 | 588 | ret = main_cmd_doSystemReset(pCmdReqBlk); |
| jhamel | 0:92f8cff1cf35 | 589 | break; |
| jhamel | 0:92f8cff1cf35 | 590 | |
| jhamel | 0:92f8cff1cf35 | 591 | case CMD_IDX_LED: // LED |
| jhamel | 0:92f8cff1cf35 | 592 | output.printf("main_callbackCommand: command[%d] %s\r\n", cmd_ptr->cmd_uref, cmd_ptr->cmd_name); |
| jhamel | 0:92f8cff1cf35 | 593 | ret = main_cmd_doLED(pCmdReqBlk); |
| jhamel | 0:92f8cff1cf35 | 594 | break; |
| jhamel | 0:92f8cff1cf35 | 595 | default : |
| jhamel | 0:92f8cff1cf35 | 596 | output.printf("main_callbackCommand: ERROR, unknown command %d\r\n", cmd_ptr->cmd_uref); |
| jhamel | 0:92f8cff1cf35 | 597 | ret = -4; |
| jhamel | 0:92f8cff1cf35 | 598 | } |
| jhamel | 0:92f8cff1cf35 | 599 | return ret; |
| jhamel | 0:92f8cff1cf35 | 600 | } |
| jhamel | 0:92f8cff1cf35 | 601 | |
| jhamel | 0:92f8cff1cf35 | 602 | // ---------------------------------------------------------- |
| jhamel | 0:92f8cff1cf35 | 603 | /// Board reset |
| jhamel | 0:92f8cff1cf35 | 604 | static void main_SystemReset(void) |
| jhamel | 0:92f8cff1cf35 | 605 | { |
| jhamel | 0:92f8cff1cf35 | 606 | NVIC_SystemReset(); |
| jhamel | 0:92f8cff1cf35 | 607 | } |
| jhamel | 0:92f8cff1cf35 | 608 | |
| jhamel | 0:92f8cff1cf35 | 609 | // ---------------------------------------------------------- |
| jhamel | 0:92f8cff1cf35 | 610 | /// do a RESET command |
| jhamel | 0:92f8cff1cf35 | 611 | static int main_cmd_doSystemReset(LiveObjectsD_CommandRequestBlock_t* pCmdReqBlk) |
| jhamel | 0:92f8cff1cf35 | 612 | { |
| jhamel | 0:92f8cff1cf35 | 613 | if (LiveObjectsClient_Stop()) { |
| jhamel | 0:92f8cff1cf35 | 614 | output.printf("doSystemReset: not running => wait 500 ms and reset ...\r\n"); |
| jhamel | 0:92f8cff1cf35 | 615 | wait_ms(200); |
| jhamel | 0:92f8cff1cf35 | 616 | main_SystemReset(); |
| jhamel | 0:92f8cff1cf35 | 617 | } |
| jhamel | 0:92f8cff1cf35 | 618 | return 1; // response = OK |
| jhamel | 0:92f8cff1cf35 | 619 | } |
| jhamel | 0:92f8cff1cf35 | 620 | |
| jhamel | 0:92f8cff1cf35 | 621 | |
| jhamel | 0:92f8cff1cf35 | 622 | // ---------------------------------------------------------- |
| jhamel | 0:92f8cff1cf35 | 623 | /// Delayed Response to the LED command |
| jhamel | 0:92f8cff1cf35 | 624 | static void main_cmd_delayed_resp_LED () |
| jhamel | 0:92f8cff1cf35 | 625 | { |
| jhamel | 0:92f8cff1cf35 | 626 | if (cmd_cnt > 0) { |
| jhamel | 0:92f8cff1cf35 | 627 | LiveObjectsD_Command_t* cmd_ptr = &appv_set_commands[CMD_IDX_LED-1]; |
| jhamel | 0:92f8cff1cf35 | 628 | if (appv_log_level > 1) output.printf("main_cmd_delayed_resp_LED: cnt=%d cid=%d\r\n", cmd_cnt, cmd_ptr->cmd_cid); |
| jhamel | 0:92f8cff1cf35 | 629 | if ((--cmd_cnt <= 0) && (cmd_ptr->cmd_cid)) { |
| jhamel | 0:92f8cff1cf35 | 630 | uint32_t code = 200; |
| jhamel | 0:92f8cff1cf35 | 631 | char msg [] = "USER LED TEST = OK"; |
| jhamel | 0:92f8cff1cf35 | 632 | LiveObjectsD_Data_t cmd_resp[] = { |
| jhamel | 0:92f8cff1cf35 | 633 | { LOD_TYPE_UINT32, "code" , &code}, |
| jhamel | 0:92f8cff1cf35 | 634 | { LOD_TYPE_STRING_C, "msg" , msg} |
| jhamel | 0:92f8cff1cf35 | 635 | }; |
| jhamel | 0:92f8cff1cf35 | 636 | // switch off the Red LED |
| jhamel | 0:92f8cff1cf35 | 637 | app_led_user = 1; |
| jhamel | 0:92f8cff1cf35 | 638 | |
| jhamel | 0:92f8cff1cf35 | 639 | output.printf("\r\n*** main_cmd_resp_LED: RESPONSE ...\r\n"); |
| jhamel | 0:92f8cff1cf35 | 640 | LiveObjectsClient_CommandResponse(cmd_ptr->cmd_cid, cmd_resp, 2); |
| jhamel | 0:92f8cff1cf35 | 641 | |
| jhamel | 0:92f8cff1cf35 | 642 | cmd_ptr->cmd_cid = 0; |
| jhamel | 0:92f8cff1cf35 | 643 | cmd_cnt = 0; |
| jhamel | 0:92f8cff1cf35 | 644 | } |
| jhamel | 0:92f8cff1cf35 | 645 | } |
| jhamel | 0:92f8cff1cf35 | 646 | } |
| jhamel | 0:92f8cff1cf35 | 647 | |
| jhamel | 0:92f8cff1cf35 | 648 | // ---------------------------------------------------------- |
| jhamel | 0:92f8cff1cf35 | 649 | /// do a LED command |
| jhamel | 0:92f8cff1cf35 | 650 | static int main_cmd_doLED(LiveObjectsD_CommandRequestBlock_t* pCmdReqBlk) |
| jhamel | 0:92f8cff1cf35 | 651 | { |
| jhamel | 0:92f8cff1cf35 | 652 | int ret; |
| jhamel | 0:92f8cff1cf35 | 653 | // switch on the Red LED |
| jhamel | 0:92f8cff1cf35 | 654 | app_led_user = 0; |
| jhamel | 0:92f8cff1cf35 | 655 | |
| jhamel | 0:92f8cff1cf35 | 656 | if (pCmdReqBlk->hd.cmd_args_nb == 0) { |
| jhamel | 0:92f8cff1cf35 | 657 | output.printf("main_cmd_doLED: No ARG\r\n"); |
| jhamel | 0:92f8cff1cf35 | 658 | app_led_user = ! app_led_user; |
| jhamel | 0:92f8cff1cf35 | 659 | ret = 1; // Response OK |
| jhamel | 0:92f8cff1cf35 | 660 | cmd_cnt = 0; |
| jhamel | 0:92f8cff1cf35 | 661 | } |
| jhamel | 0:92f8cff1cf35 | 662 | else { |
| jhamel | 0:92f8cff1cf35 | 663 | int i; |
| jhamel | 0:92f8cff1cf35 | 664 | int cnt; |
| jhamel | 0:92f8cff1cf35 | 665 | for (i=0; i<pCmdReqBlk->hd.cmd_args_nb;i++ ) { |
| jhamel | 0:92f8cff1cf35 | 666 | if ( strncasecmp("ticks", pCmdReqBlk->args_array[i].arg_name, 5) |
| jhamel | 0:92f8cff1cf35 | 667 | && pCmdReqBlk->args_array[i].arg_type == 0 ) { |
| jhamel | 0:92f8cff1cf35 | 668 | cnt = atoi(pCmdReqBlk->args_array[i].arg_value); |
| jhamel | 0:92f8cff1cf35 | 669 | if ((cnt >= 0) || (cnt <= 3)) { |
| jhamel | 0:92f8cff1cf35 | 670 | cmd_cnt = cnt; |
| jhamel | 0:92f8cff1cf35 | 671 | } |
| jhamel | 0:92f8cff1cf35 | 672 | else cmd_cnt = 0; |
| jhamel | 0:92f8cff1cf35 | 673 | output.printf("main_cmd_doLED: cmd_cnt = %di (%d)\r\n", cmd_cnt, cnt); |
| jhamel | 0:92f8cff1cf35 | 674 | } |
| jhamel | 0:92f8cff1cf35 | 675 | } |
| jhamel | 0:92f8cff1cf35 | 676 | } |
| jhamel | 0:92f8cff1cf35 | 677 | |
| jhamel | 0:92f8cff1cf35 | 678 | if (cmd_cnt == 0) { |
| jhamel | 0:92f8cff1cf35 | 679 | app_led_user = ! app_led_user; |
| jhamel | 0:92f8cff1cf35 | 680 | ret = 1; // Response OK |
| jhamel | 0:92f8cff1cf35 | 681 | } |
| jhamel | 0:92f8cff1cf35 | 682 | else { |
| jhamel | 0:92f8cff1cf35 | 683 | LiveObjectsD_Command_t* cmd_ptr = (LiveObjectsD_Command_t*)(pCmdReqBlk->hd.cmd_ptr); |
| jhamel | 0:92f8cff1cf35 | 684 | app_led_user = 0; |
| jhamel | 0:92f8cff1cf35 | 685 | output.printf("main_cmd_doLED: ccid=%d (%d)\r\n", pCmdReqBlk->hd.cmd_cid, cmd_ptr->cmd_cid); |
| jhamel | 0:92f8cff1cf35 | 686 | cmd_ptr->cmd_cid = pCmdReqBlk->hd.cmd_cid; |
| jhamel | 0:92f8cff1cf35 | 687 | ret = 0; // pending |
| jhamel | 0:92f8cff1cf35 | 688 | } |
| jhamel | 0:92f8cff1cf35 | 689 | return ret; // response = OK |
| jhamel | 0:92f8cff1cf35 | 690 | } |
| jhamel | 0:92f8cff1cf35 | 691 | |
| jhamel | 0:92f8cff1cf35 | 692 | // ========================================================== |
| jhamel | 0:92f8cff1cf35 | 693 | |
| jhamel | 0:92f8cff1cf35 | 694 | |
| jhamel | 0:92f8cff1cf35 | 695 | /// Counter ticker to increment periodically a application counter |
| jhamel | 0:92f8cff1cf35 | 696 | Ticker counter_ticker; |
| jhamel | 0:92f8cff1cf35 | 697 | |
| jhamel | 0:92f8cff1cf35 | 698 | /// Called periodically by the ticker |
| jhamel | 0:92f8cff1cf35 | 699 | void counter_inc(void) { |
| jhamel | 0:92f8cff1cf35 | 700 | appv_measures_counter ++; |
| jhamel | 0:92f8cff1cf35 | 701 | } |
| jhamel | 0:92f8cff1cf35 | 702 | |
| jhamel | 0:92f8cff1cf35 | 703 | |
| jhamel | 0:92f8cff1cf35 | 704 | /** |
| jhamel | 0:92f8cff1cf35 | 705 | * Application thread: |
| jhamel | 0:92f8cff1cf35 | 706 | * - Send the delayed LED command response (if pending) |
| jhamel | 0:92f8cff1cf35 | 707 | */ |
| jhamel | 0:92f8cff1cf35 | 708 | void thread_appli(void) { |
| jhamel | 0:92f8cff1cf35 | 709 | uint32_t loop_cnt = 0; |
| jhamel | 0:92f8cff1cf35 | 710 | osThreadId id = osThreadGetId(); |
| jhamel | 0:92f8cff1cf35 | 711 | output.printf("thread_appli: running %x\r\n", id); |
| jhamel | 0:92f8cff1cf35 | 712 | |
| jhamel | 0:92f8cff1cf35 | 713 | while(1) { |
| jhamel | 0:92f8cff1cf35 | 714 | uint32_t delay = appv_cfg_timeout*1000; // set to milliseconds |
| jhamel | 0:92f8cff1cf35 | 715 | if (delay == 0) delay = 1000; // min. 1 seconds |
| jhamel | 0:92f8cff1cf35 | 716 | else if (delay > 120000) delay = 120000; // max. 2 minutes |
| jhamel | 0:92f8cff1cf35 | 717 | |
| jhamel | 0:92f8cff1cf35 | 718 | wait_ms(delay); |
| jhamel | 0:92f8cff1cf35 | 719 | |
| jhamel | 0:92f8cff1cf35 | 720 | ++loop_cnt; |
| jhamel | 0:92f8cff1cf35 | 721 | if (appv_log_level > 1) output.printf("thread_appli: %u - period= %u ms\r\n", loop_cnt, delay); |
| jhamel | 0:92f8cff1cf35 | 722 | |
| jhamel | 0:92f8cff1cf35 | 723 | // Process the LED command response if pending. |
| jhamel | 0:92f8cff1cf35 | 724 | main_cmd_delayed_resp_LED(); |
| jhamel | 0:92f8cff1cf35 | 725 | |
| jhamel | 0:92f8cff1cf35 | 726 | // Simulate measures : Voltage and Temperature ... |
| jhamel | 0:92f8cff1cf35 | 727 | |
| jhamel | 0:92f8cff1cf35 | 728 | if (appv_measures_volt <= 0.0) appv_measures_volt_grad = 0.2; |
| jhamel | 0:92f8cff1cf35 | 729 | else if (appv_measures_volt >= 5.0) appv_measures_volt_grad = -0.3; |
| jhamel | 0:92f8cff1cf35 | 730 | |
| jhamel | 0:92f8cff1cf35 | 731 | if (appv_measures_temp <= -3) appv_measures_temp_grad = 1; |
| jhamel | 0:92f8cff1cf35 | 732 | else if (appv_measures_temp >= 20) appv_measures_temp_grad = -1; |
| jhamel | 0:92f8cff1cf35 | 733 | |
| jhamel | 0:92f8cff1cf35 | 734 | appv_measures_volt += appv_measures_volt_grad; |
| jhamel | 0:92f8cff1cf35 | 735 | appv_measures_temp += appv_measures_temp_grad; |
| jhamel | 0:92f8cff1cf35 | 736 | |
| jhamel | 0:92f8cff1cf35 | 737 | if (appv_log_level > 2) output.printf("thread_appli: %u - %s PUBLISH - volt=%2.2f temp=%d\r\n", loop_cnt, |
| jhamel | 0:92f8cff1cf35 | 738 | appv_measures_enabled ? "DATA" : "NO", |
| jhamel | 0:92f8cff1cf35 | 739 | appv_measures_volt, appv_measures_temp); |
| jhamel | 0:92f8cff1cf35 | 740 | |
| jhamel | 0:92f8cff1cf35 | 741 | if (appv_measures_enabled) { |
| jhamel | 0:92f8cff1cf35 | 742 | LiveObjectsClient_PushData( appv_hdl_data ); |
| jhamel | 0:92f8cff1cf35 | 743 | } |
| jhamel | 0:92f8cff1cf35 | 744 | |
| jhamel | 0:92f8cff1cf35 | 745 | } |
| jhamel | 0:92f8cff1cf35 | 746 | } |
| jhamel | 0:92f8cff1cf35 | 747 | |
| jhamel | 0:92f8cff1cf35 | 748 | |
| jhamel | 0:92f8cff1cf35 | 749 | // ========================================================== |
| jhamel | 0:92f8cff1cf35 | 750 | // |
| jhamel | 0:92f8cff1cf35 | 751 | const char* appv_help = |
| jhamel | 0:92f8cff1cf35 | 752 | " R : system reset\r\n" |
| jhamel | 0:92f8cff1cf35 | 753 | " d : push 'collected data'\r\n" |
| jhamel | 0:92f8cff1cf35 | 754 | " s : push 'status'\r\n" |
| jhamel | 0:92f8cff1cf35 | 755 | " c : push 'config. parameters'\r\n" |
| jhamel | 0:92f8cff1cf35 | 756 | " r : push 'resources'\r\n" |
| jhamel | 0:92f8cff1cf35 | 757 | " p : publish STATUS message\r\n" |
| jhamel | 0:92f8cff1cf35 | 758 | " e : Enable/Disable the data publishing\r\n" |
| jhamel | 0:92f8cff1cf35 | 759 | " X|M|n : Enable/Disable the dump of published message (X to enable also hexa dump)\r\n" |
| jhamel | 0:92f8cff1cf35 | 760 | " D|I|W : set debug log level (mbed_trace)\r\n" |
| jhamel | 0:92f8cff1cf35 | 761 | " 0-9 : set appli log level \r\n" |
| jhamel | 0:92f8cff1cf35 | 762 | ; |
| jhamel | 0:92f8cff1cf35 | 763 | /** |
| jhamel | 0:92f8cff1cf35 | 764 | * Console thread |
| jhamel | 0:92f8cff1cf35 | 765 | * - wait for an input character from terminal |
| jhamel | 0:92f8cff1cf35 | 766 | * - perform the requested operation |
| jhamel | 0:92f8cff1cf35 | 767 | */ |
| jhamel | 0:92f8cff1cf35 | 768 | void thread_input_cons(void) { |
| jhamel | 0:92f8cff1cf35 | 769 | int c; |
| jhamel | 0:92f8cff1cf35 | 770 | osThreadId id = osThreadGetId(); |
| jhamel | 0:92f8cff1cf35 | 771 | output.printf("thread_input_cons: running %x\r\n", id); |
| jhamel | 0:92f8cff1cf35 | 772 | while(1) { |
| jhamel | 0:92f8cff1cf35 | 773 | osDelay(500); |
| jhamel | 0:92f8cff1cf35 | 774 | #if 1 |
| jhamel | 0:92f8cff1cf35 | 775 | if (output.readable()) { |
| jhamel | 0:92f8cff1cf35 | 776 | // WARNING: getc is a blocking call, and can also block the printf !!! |
| jhamel | 0:92f8cff1cf35 | 777 | c = output.getc(); |
| jhamel | 0:92f8cff1cf35 | 778 | if (c) { |
| jhamel | 0:92f8cff1cf35 | 779 | if (appv_log_level > 1) output.printf("thread_input_cons: input x%x\r\n", c); |
| jhamel | 0:92f8cff1cf35 | 780 | if (c == 'h') { |
| jhamel | 0:92f8cff1cf35 | 781 | output.printf(appv_help); |
| jhamel | 0:92f8cff1cf35 | 782 | } |
| jhamel | 0:92f8cff1cf35 | 783 | else if ( c == 'e' ) { |
| jhamel | 0:92f8cff1cf35 | 784 | appv_measures_enabled = (appv_measures_enabled) ? 0 : 1; |
| jhamel | 0:92f8cff1cf35 | 785 | output.printf(">>> %s data publish\r\n", appv_measures_enabled ? "Enable" : "Disable"); |
| jhamel | 0:92f8cff1cf35 | 786 | } |
| jhamel | 0:92f8cff1cf35 | 787 | else if ( c == 'p' ) { |
| jhamel | 0:92f8cff1cf35 | 788 | char msg[40]; |
| jhamel | 0:92f8cff1cf35 | 789 | appv_status_counter++; |
| jhamel | 0:92f8cff1cf35 | 790 | snprintf(msg,sizeof(msg)-1,"{\"info\":{\"counter\": %u}}", appv_status_counter); |
| jhamel | 0:92f8cff1cf35 | 791 | output.printf(">>> Publish t=/dev/info msg=%s ...\r\n", msg); |
| jhamel | 0:92f8cff1cf35 | 792 | LiveObjectsClient_Publish("dev/info", msg); // Publish JSON message built by user |
| jhamel | 0:92f8cff1cf35 | 793 | } |
| jhamel | 0:92f8cff1cf35 | 794 | else if ( c == 'd' ) { |
| jhamel | 0:92f8cff1cf35 | 795 | appv_measures_counter++; |
| jhamel | 0:92f8cff1cf35 | 796 | output.printf(">>> Push DATA - cnt %u\r\n", appv_measures_counter); |
| jhamel | 0:92f8cff1cf35 | 797 | LiveObjectsClient_PushData( appv_hdl_data ); |
| jhamel | 0:92f8cff1cf35 | 798 | } |
| jhamel | 0:92f8cff1cf35 | 799 | else if (c == 's' ) { |
| jhamel | 0:92f8cff1cf35 | 800 | appv_status_counter ++; |
| jhamel | 0:92f8cff1cf35 | 801 | output.printf(">>> Push STATUS - counter %u\r\n", appv_status_counter); |
| jhamel | 0:92f8cff1cf35 | 802 | LiveObjectsClient_PushStatus( appv_hdl_status ); |
| jhamel | 0:92f8cff1cf35 | 803 | } |
| jhamel | 0:92f8cff1cf35 | 804 | else if (c == 'c' ) { |
| jhamel | 0:92f8cff1cf35 | 805 | output.printf(">>> Push CONFIG\r\n"); |
| jhamel | 0:92f8cff1cf35 | 806 | LiveObjectsClient_PushCfgParams(); |
| jhamel | 0:92f8cff1cf35 | 807 | } |
| jhamel | 0:92f8cff1cf35 | 808 | else if (c == 'r' ) { |
| jhamel | 0:92f8cff1cf35 | 809 | output.printf(">>> Push RESOURCES\r\n"); |
| jhamel | 0:92f8cff1cf35 | 810 | LiveObjectsClient_PushResources(); |
| jhamel | 0:92f8cff1cf35 | 811 | } |
| jhamel | 0:92f8cff1cf35 | 812 | else if (c == 'X') { |
| jhamel | 0:92f8cff1cf35 | 813 | output.printf(">>> Enable Message Dump (+ hexa dump)\r\n"); |
| jhamel | 0:92f8cff1cf35 | 814 | LiveObjectsClient_SetDbgLevel(3); |
| jhamel | 0:92f8cff1cf35 | 815 | } |
| jhamel | 0:92f8cff1cf35 | 816 | else if (c == 'M') { |
| jhamel | 0:92f8cff1cf35 | 817 | output.printf(">>> Enable Message Dump\r\n"); |
| jhamel | 0:92f8cff1cf35 | 818 | LiveObjectsClient_SetDbgLevel(1); |
| jhamel | 0:92f8cff1cf35 | 819 | } |
| jhamel | 0:92f8cff1cf35 | 820 | else if (c == 'm') { |
| jhamel | 0:92f8cff1cf35 | 821 | output.printf(">>> Disable Message Dump\r\n"); |
| jhamel | 0:92f8cff1cf35 | 822 | LiveObjectsClient_SetDbgLevel(0); |
| jhamel | 0:92f8cff1cf35 | 823 | } |
| jhamel | 0:92f8cff1cf35 | 824 | else if (c == 'D') { |
| jhamel | 0:92f8cff1cf35 | 825 | output.printf(">>> Set trace level : DEBUG\r\n"); |
| jhamel | 0:92f8cff1cf35 | 826 | mbed_trace_config_set(TRACE_ACTIVE_LEVEL_ALL|TRACE_MODE_COLOR); |
| jhamel | 0:92f8cff1cf35 | 827 | } |
| jhamel | 0:92f8cff1cf35 | 828 | else if (c == 'I') { |
| jhamel | 0:92f8cff1cf35 | 829 | output.printf(">>> Set trace level : INFO\r\n"); |
| jhamel | 0:92f8cff1cf35 | 830 | mbed_trace_config_set(TRACE_ACTIVE_LEVEL_INFO|TRACE_MODE_COLOR); |
| jhamel | 0:92f8cff1cf35 | 831 | } |
| jhamel | 0:92f8cff1cf35 | 832 | else if (c == 'W') { |
| jhamel | 0:92f8cff1cf35 | 833 | output.printf(">>> Set trace level : WARNING\r\n"); |
| jhamel | 0:92f8cff1cf35 | 834 | mbed_trace_config_set(TRACE_ACTIVE_LEVEL_WARN|TRACE_MODE_COLOR); |
| jhamel | 0:92f8cff1cf35 | 835 | } |
| jhamel | 0:92f8cff1cf35 | 836 | else if ((c >= '0') && (c <= '9')) { |
| jhamel | 0:92f8cff1cf35 | 837 | output.printf(">>> Set appli dbg level : %d -> %c\r\n", appv_log_level, c); |
| jhamel | 0:92f8cff1cf35 | 838 | appv_log_level = c - '0'; |
| jhamel | 0:92f8cff1cf35 | 839 | } |
| jhamel | 0:92f8cff1cf35 | 840 | else if (c == 'R') { |
| jhamel | 0:92f8cff1cf35 | 841 | main_SystemReset(); |
| jhamel | 0:92f8cff1cf35 | 842 | } |
| jhamel | 0:92f8cff1cf35 | 843 | } |
| jhamel | 0:92f8cff1cf35 | 844 | } |
| jhamel | 0:92f8cff1cf35 | 845 | #endif |
| jhamel | 0:92f8cff1cf35 | 846 | } |
| jhamel | 0:92f8cff1cf35 | 847 | } |
| jhamel | 0:92f8cff1cf35 | 848 | |
| jhamel | 0:92f8cff1cf35 | 849 | |
| jhamel | 0:92f8cff1cf35 | 850 | // ========================================================== |
| jhamel | 0:92f8cff1cf35 | 851 | // |
| jhamel | 0:92f8cff1cf35 | 852 | |
| jhamel | 0:92f8cff1cf35 | 853 | // ---------------------------------------------------------- |
| jhamel | 0:92f8cff1cf35 | 854 | /// Called (by LiveOjbects thread) to notify a connectivity state change |
| jhamel | 0:92f8cff1cf35 | 855 | static void appli_client_state_cb(LiveObjectsD_State_t state) |
| jhamel | 0:92f8cff1cf35 | 856 | { |
| jhamel | 0:92f8cff1cf35 | 857 | enum app_state_enum new_state = APP_STATE_UNKNOWN; |
| jhamel | 0:92f8cff1cf35 | 858 | switch (state) { |
| jhamel | 0:92f8cff1cf35 | 859 | case CSTATE_CONNECTING: new_state = APP_STATE_CONNECTING; break; |
| jhamel | 0:92f8cff1cf35 | 860 | case CSTATE_CONNECTED: new_state = APP_STATE_CONNECTED; break; |
| jhamel | 0:92f8cff1cf35 | 861 | case CSTATE_DISCONNECTED: new_state = APP_STATE_NETWORK_READY; break; |
| jhamel | 0:92f8cff1cf35 | 862 | case CSTATE_DOWN: new_state = APP_STATE_DOWN; break; |
| jhamel | 0:92f8cff1cf35 | 863 | } |
| jhamel | 0:92f8cff1cf35 | 864 | output.printf("\n\rLIVEOBJECTS CLIENT STATE CHANGE (%d) : %d -> %d \r\n", state, appv_state, new_state); |
| jhamel | 0:92f8cff1cf35 | 865 | appv_state = new_state; |
| jhamel | 0:92f8cff1cf35 | 866 | |
| jhamel | 0:92f8cff1cf35 | 867 | } |
| jhamel | 0:92f8cff1cf35 | 868 | |
| jhamel | 0:92f8cff1cf35 | 869 | // ---------------------------------------------------------- |
| jhamel | 0:92f8cff1cf35 | 870 | /// Entry point to the program |
| jhamel | 0:92f8cff1cf35 | 871 | int main() { |
| jhamel | 0:92f8cff1cf35 | 872 | int ret; |
| jhamel | 0:92f8cff1cf35 | 873 | |
| jhamel | 0:92f8cff1cf35 | 874 | app_led_status = 1; |
| jhamel | 0:92f8cff1cf35 | 875 | app_led_user = 1; |
| jhamel | 0:92f8cff1cf35 | 876 | |
| jhamel | 0:92f8cff1cf35 | 877 | appv_state = APP_STATE_INIT; |
| jhamel | 0:92f8cff1cf35 | 878 | |
| jhamel | 0:92f8cff1cf35 | 879 | // Led blink at 250 ms |
| jhamel | 0:92f8cff1cf35 | 880 | app_led_ticker.attach_us(app_led_blinky, 250000); |
| jhamel | 0:92f8cff1cf35 | 881 | |
| jhamel | 0:92f8cff1cf35 | 882 | // Keep track of the main thread |
| jhamel | 0:92f8cff1cf35 | 883 | appv_thread_id = osThreadGetId(); |
| jhamel | 0:92f8cff1cf35 | 884 | |
| jhamel | 0:92f8cff1cf35 | 885 | // Sets the console baud-rate |
| jhamel | 0:92f8cff1cf35 | 886 | output.baud(9600); |
| jhamel | 0:92f8cff1cf35 | 887 | |
| jhamel | 0:92f8cff1cf35 | 888 | output.printf("\r\n\r\n"); |
| jhamel | 0:92f8cff1cf35 | 889 | output.printf("Starting LiveObject Client Example %s (tid=x%p) ...\r\n", appv_version, appv_thread_id); |
| jhamel | 0:92f8cff1cf35 | 890 | |
| jhamel | 0:92f8cff1cf35 | 891 | app_trace_setup(); |
| jhamel | 0:92f8cff1cf35 | 892 | |
| jhamel | 0:92f8cff1cf35 | 893 | // Start program only if LiveObjet Apikey parameter is well defined |
| jhamel | 0:92f8cff1cf35 | 894 | if (LiveObjectsClient_CheckApiKey(LOC_CLIENT_DEV_API_KEY)) { |
| jhamel | 0:92f8cff1cf35 | 895 | output.printf("apikey not set, '%s'\r\n", LOC_CLIENT_DEV_API_KEY); |
| jhamel | 0:92f8cff1cf35 | 896 | output.printf("\n\rExiting application ....\r\n"); |
| jhamel | 0:92f8cff1cf35 | 897 | return -1; |
| jhamel | 0:92f8cff1cf35 | 898 | } |
| jhamel | 0:92f8cff1cf35 | 899 | |
| jhamel | 0:92f8cff1cf35 | 900 | // Setup the Ethernet Interface |
| jhamel | 0:92f8cff1cf35 | 901 | if (0 == app_net_init()) { |
| jhamel | 0:92f8cff1cf35 | 902 | output.printf("\n\rConnected to Network successfully\r\n"); |
| jhamel | 0:92f8cff1cf35 | 903 | |
| jhamel | 0:92f8cff1cf35 | 904 | LiveObjectsClient_SetDbgLevel(DBG_DFT_LOMC_LOG_LEVEL); |
| jhamel | 0:92f8cff1cf35 | 905 | |
| jhamel | 0:92f8cff1cf35 | 906 | // Initialize the LiveObjects Client Context |
| jhamel | 0:92f8cff1cf35 | 907 | // ----------------------------------------- |
| jhamel | 0:92f8cff1cf35 | 908 | output.printf("\n\rLiveObjectsClient_Init ...\r\n"); |
| jhamel | 0:92f8cff1cf35 | 909 | ret = LiveObjectsClient_Init(appv_network_interface); |
| jhamel | 0:92f8cff1cf35 | 910 | if (ret) { |
| jhamel | 0:92f8cff1cf35 | 911 | output.printf("\n\rLiveObjectsClient_Init Failed !\r\n"); |
| jhamel | 0:92f8cff1cf35 | 912 | } |
| jhamel | 0:92f8cff1cf35 | 913 | else { |
| jhamel | 0:92f8cff1cf35 | 914 | |
| jhamel | 0:92f8cff1cf35 | 915 | #if LOC_CLIENT_USE_MAC_ADDR |
| jhamel | 0:92f8cff1cf35 | 916 | if (appv_network_id) { |
| jhamel | 0:92f8cff1cf35 | 917 | // Use the MAC address to set the Device Identifier |
| jhamel | 0:92f8cff1cf35 | 918 | // -------------------------------------------- |
| jhamel | 0:92f8cff1cf35 | 919 | output.printf("\n\rLiveObjectsClient_SetDevId '%s' ...\r\n", appv_network_id); |
| jhamel | 0:92f8cff1cf35 | 920 | ret = LiveObjectsClient_SetDevId(appv_network_id); |
| jhamel | 0:92f8cff1cf35 | 921 | if (ret) { |
| jhamel | 0:92f8cff1cf35 | 922 | output.printf("\n\rLiveObjectsClient_SetDevId Failed !\r\n"); |
| jhamel | 0:92f8cff1cf35 | 923 | } |
| jhamel | 0:92f8cff1cf35 | 924 | } |
| jhamel | 0:92f8cff1cf35 | 925 | #endif |
| jhamel | 0:92f8cff1cf35 | 926 | // Attach my local RESOURCES to the LiveObjects Client instance |
| jhamel | 0:92f8cff1cf35 | 927 | // ------------------------------------------------------------ |
| jhamel | 0:92f8cff1cf35 | 928 | ret = LiveObjectsClient_AttachResources(appv_set_resources, SET_RESOURCES_NB, main_cb_rsc_ntfy, main_cb_rsc_data); |
| jhamel | 0:92f8cff1cf35 | 929 | if (ret) output.printf(" !!! ERROR (%d) to attach RESOURCES !\r\n", ret); |
| jhamel | 0:92f8cff1cf35 | 930 | |
| jhamel | 0:92f8cff1cf35 | 931 | // Attach my local Configuration Parameters to the LiveObjects Client instance |
| jhamel | 0:92f8cff1cf35 | 932 | // ---------------------------------------------------------------------------- |
| jhamel | 0:92f8cff1cf35 | 933 | ret = LiveObjectsClient_AttachCfgParams(appv_set_param, SET_PARAM_NB, main_cb_param_udp); |
| jhamel | 0:92f8cff1cf35 | 934 | if (ret) output.printf(" !!! ERROR (%d) to attach Config Parameters !\r\n", ret); |
| jhamel | 0:92f8cff1cf35 | 935 | |
| jhamel | 0:92f8cff1cf35 | 936 | // Attach a set of commands to the LiveObjects Client instance |
| jhamel | 0:92f8cff1cf35 | 937 | // ----------------------------------------------------------- |
| jhamel | 0:92f8cff1cf35 | 938 | ret = LiveObjectsClient_AttachCommands(appv_set_commands, SET_COMMANDS_NB, main_cb_command); |
| jhamel | 0:92f8cff1cf35 | 939 | if (ret < 0) output.printf(" !!! ERROR (%d) to attach a set of commands !\r\n", ret); |
| jhamel | 0:92f8cff1cf35 | 940 | |
| jhamel | 0:92f8cff1cf35 | 941 | |
| jhamel | 0:92f8cff1cf35 | 942 | // Attach my local STATUS data to the LiveObjects Client instance |
| jhamel | 0:92f8cff1cf35 | 943 | // -------------------------------------------------------------- |
| jhamel | 0:92f8cff1cf35 | 944 | appv_hdl_status = LiveObjectsClient_AttachStatus(appv_set_status, SET_STATUS_NB); |
| jhamel | 0:92f8cff1cf35 | 945 | if (appv_hdl_status) output.printf(" !!! ERROR (%d) to attach status !\r\n", appv_hdl_status); |
| jhamel | 0:92f8cff1cf35 | 946 | |
| jhamel | 0:92f8cff1cf35 | 947 | // Attach one set of collected data to the LiveObjects Client instance |
| jhamel | 0:92f8cff1cf35 | 948 | // -------------------------------------------------------------------- |
| jhamel | 0:92f8cff1cf35 | 949 | appv_hdl_data = LiveObjectsClient_AttachData(STREAM_PREFIX, "LO_sample_measures", "mV1","\"Test\"", NULL, appv_set_measures, SET_MEASURES_NB); |
| jhamel | 0:92f8cff1cf35 | 950 | if (appv_hdl_data < 0) output.printf(" !!! ERROR (%d) to attach a collected data stream !\r\n", appv_hdl_data); |
| jhamel | 0:92f8cff1cf35 | 951 | |
| jhamel | 0:92f8cff1cf35 | 952 | // ================================== |
| jhamel | 0:92f8cff1cf35 | 953 | // User Application part. |
| jhamel | 0:92f8cff1cf35 | 954 | #if 1 |
| jhamel | 0:92f8cff1cf35 | 955 | wait_ms(1000); |
| jhamel | 0:92f8cff1cf35 | 956 | |
| jhamel | 0:92f8cff1cf35 | 957 | // Start the counter ticker at 500 ms |
| jhamel | 0:92f8cff1cf35 | 958 | counter_ticker.attach_us(counter_inc, 500000); // appv_measures_counter |
| jhamel | 0:92f8cff1cf35 | 959 | |
| jhamel | 0:92f8cff1cf35 | 960 | output.printf(" ---- Start thread : thread_input_cons ....\r\n"); |
| jhamel | 0:92f8cff1cf35 | 961 | ret = console_thread.start(thread_input_cons); |
| jhamel | 0:92f8cff1cf35 | 962 | if (ret) { |
| jhamel | 0:92f8cff1cf35 | 963 | output.printf("\n\r !!!! ERROR(%d) to start thread : thread_input_cons\r\n", ret); |
| jhamel | 0:92f8cff1cf35 | 964 | } |
| jhamel | 0:92f8cff1cf35 | 965 | |
| jhamel | 0:92f8cff1cf35 | 966 | output.printf(" ---- Start thread : thread_appli ....\r\n"); |
| jhamel | 0:92f8cff1cf35 | 967 | ret = appli_thread.start(thread_appli); |
| jhamel | 0:92f8cff1cf35 | 968 | if (ret) { |
| jhamel | 0:92f8cff1cf35 | 969 | output.printf("\n\r !!!! ERROR(%d) to start thread : thread_appli\r\n", ret); |
| jhamel | 0:92f8cff1cf35 | 970 | } |
| jhamel | 0:92f8cff1cf35 | 971 | |
| jhamel | 0:92f8cff1cf35 | 972 | wait_ms(1000); |
| jhamel | 0:92f8cff1cf35 | 973 | #endif |
| jhamel | 0:92f8cff1cf35 | 974 | |
| jhamel | 0:92f8cff1cf35 | 975 | // Enable the receipt of commands |
| jhamel | 0:92f8cff1cf35 | 976 | ret = LiveObjectsClient_ControlCommands(true); |
| jhamel | 0:92f8cff1cf35 | 977 | if (ret < 0) output.printf(" !!! ERROR (%d) to enable the receipt of commands !\r\n", ret); |
| jhamel | 0:92f8cff1cf35 | 978 | |
| jhamel | 0:92f8cff1cf35 | 979 | // Enable the receipt of resource update requests |
| jhamel | 0:92f8cff1cf35 | 980 | ret = LiveObjectsClient_ControlResources(true); |
| jhamel | 0:92f8cff1cf35 | 981 | if (ret < 0) output.printf(" !!! ERROR (%d) to enable the receipt of resource update request !\r\n", ret); |
| jhamel | 0:92f8cff1cf35 | 982 | |
| jhamel | 0:92f8cff1cf35 | 983 | #if 0 |
| jhamel | 0:92f8cff1cf35 | 984 | // ================================== |
| jhamel | 0:92f8cff1cf35 | 985 | // Run LiveObjects Client in main thread (forever ...) |
| jhamel | 0:92f8cff1cf35 | 986 | output.printf("\n\rLiveObjectsClient_Run ...\r\n"); |
| jhamel | 0:92f8cff1cf35 | 987 | |
| jhamel | 0:92f8cff1cf35 | 988 | LiveObjectsClient_Run(appli_client_state_cb); |
| jhamel | 0:92f8cff1cf35 | 989 | |
| jhamel | 0:92f8cff1cf35 | 990 | output.printf("\n\rLiveObjectsClient_Run Failed !!!!\r\n"); |
| jhamel | 0:92f8cff1cf35 | 991 | #else |
| jhamel | 0:92f8cff1cf35 | 992 | // ================================== |
| jhamel | 0:92f8cff1cf35 | 993 | // Start a new sthread to run LiveObjects Client (forever ...) |
| jhamel | 0:92f8cff1cf35 | 994 | output.printf("\n\rLiveObjectsClient_ThreadStart ...\r\n"); |
| jhamel | 0:92f8cff1cf35 | 995 | |
| jhamel | 0:92f8cff1cf35 | 996 | ret = LiveObjectsClient_ThreadStart(appli_client_state_cb); |
| jhamel | 0:92f8cff1cf35 | 997 | |
| jhamel | 0:92f8cff1cf35 | 998 | if (ret == 0) { |
| jhamel | 0:92f8cff1cf35 | 999 | while(LiveObjectsClient_ThreadState() >= 0) { |
| jhamel | 0:92f8cff1cf35 | 1000 | wait_ms(1000); |
| jhamel | 0:92f8cff1cf35 | 1001 | } |
| jhamel | 0:92f8cff1cf35 | 1002 | } |
| jhamel | 0:92f8cff1cf35 | 1003 | output.printf("\n\rLiveObjectsClient_ThreadStart ERROR %d\r\n", ret); |
| jhamel | 0:92f8cff1cf35 | 1004 | #endif |
| jhamel | 0:92f8cff1cf35 | 1005 | |
| jhamel | 0:92f8cff1cf35 | 1006 | } |
| jhamel | 0:92f8cff1cf35 | 1007 | } else { |
| jhamel | 0:92f8cff1cf35 | 1008 | output.printf("\n\rConnection to Network Failed ! \r\n"); |
| jhamel | 0:92f8cff1cf35 | 1009 | } |
| jhamel | 0:92f8cff1cf35 | 1010 | |
| jhamel | 0:92f8cff1cf35 | 1011 | output.printf("\n\rExiting application ....\r\n"); |
| jhamel | 0:92f8cff1cf35 | 1012 | |
| jhamel | 0:92f8cff1cf35 | 1013 | app_led_ticker.detach(); |
| jhamel | 0:92f8cff1cf35 | 1014 | |
| jhamel | 0:92f8cff1cf35 | 1015 | app_led_status = 0; |
| jhamel | 0:92f8cff1cf35 | 1016 | |
| jhamel | 0:92f8cff1cf35 | 1017 | app_led_user = 0; |
| jhamel | 0:92f8cff1cf35 | 1018 | |
| jhamel | 0:92f8cff1cf35 | 1019 | main_SystemReset(); |
| jhamel | 0:92f8cff1cf35 | 1020 | } |