Datavenue Live Objects basic sample using liveobjects-iotsoftbox-mqtt library.

Dependencies:   MQTTPacket

Committer:
jhamel
Date:
Tue Apr 04 16:17:23 2017 +0200
Revision:
8:82317399e4ce
Parent:
4:919c25da5ec8
Use CA from Symantec/VeriSign, and fix TLS issue

Who changed what in which revision?

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