1717#include < string>
1818#include < sstream>
1919#include < vector>
20+ #include " mbed-trace/mbed_trace.h"
2021
2122#include " security.h"
2223
@@ -48,6 +49,22 @@ LoWPANNDInterface mesh;
4849ThreadInterface mesh;
4950#endif
5051
52+ #ifndef MESH
53+ #include " eventOS_scheduler.h"
54+ #include " eventOS_event.h"
55+ #include " nsdynmemLIB.h"
56+ #include " platform/arm_hal_timer.h"
57+ #include " ns_event_loop.h"
58+
59+ #define HEAP_SIZE 1023
60+ static uint8_t app_stack_heap[HEAP_SIZE + 1 ];
61+ // This is address to mbed Device Connector
62+ #define MBED_SERVER_ADDRESS " coap://api.connector.mbed.com:5684"
63+ #else
64+ // This is address to mbed Device Connector
65+ #define MBED_SERVER_ADDRESS YOTTA_CFG_DEVICE_CONNECTOR_URI
66+ #endif
67+
5168Serial output (USBTX, USBRX);
5269
5370// These are example resource values for the Device Object
@@ -157,7 +174,7 @@ class LedResource {
157174 */
158175class ButtonResource {
159176public:
160- ButtonResource (){
177+ ButtonResource (): counter( 0 ) {
161178 // create ObjectID with metadata tag of '3200', which is 'digital input'
162179 btn_object = M2MInterfaceFactory::create_object (" 3200" );
163180 M2MObjectInstance* btn_inst = btn_object->create_object_instance ();
@@ -178,10 +195,6 @@ class ButtonResource {
178195 return btn_object;
179196 }
180197
181- void button_clicked () {
182- output.printf (" \n\r Button clicked\r\n " );
183- }
184-
185198 /*
186199 * When you press the button, we read the current value of the click counter
187200 * from mbed Device Connector, then up the value with one.
@@ -204,19 +217,30 @@ class ButtonResource {
204217
205218private:
206219 M2MObject* btn_object;
207- uint16_t counter = 0 ;
220+ uint16_t counter;
208221};
209222
210223// Network interaction must be performed outside of interrupt context
211224Semaphore updates (0 );
212225volatile bool registered = false ;
226+ volatile bool clicked = false ;
213227osThreadId mainThread;
214228
215229void unregister () {
216230 registered = false ;
217231 updates.release ();
218232}
219233
234+ void button_clicked () {
235+ clicked = true ;
236+ updates.release ();
237+ }
238+
239+ // debug printf function
240+ void trace_printer (const char * str) {
241+ printf (" %s\r\n " , str);
242+ }
243+
220244// Status indication
221245Ticker status_ticker;
222246DigitalOut status_led (LED1);
@@ -235,6 +259,18 @@ int main() {
235259
236260 output.printf (" Starting mbed Client example...\r\n " );
237261
262+ mbed_trace_init ();
263+ mbed_trace_print_function_set (trace_printer);
264+
265+ #ifndef MESH
266+ ns_dyn_mem_init (app_stack_heap, HEAP_SIZE,
267+ NULL , 0 );
268+ platform_timer_enable ();
269+ eventOS_scheduler_init ();
270+ ns_event_loop_thread_create ();
271+ ns_event_loop_thread_start ();
272+ #endif
273+
238274 NetworkStack *network_stack = 0 ;
239275#if defined WIFI
240276 output.printf (" \n\r Using WiFi \r\n " );
@@ -263,19 +299,19 @@ int main() {
263299 }
264300
265301 // we create our button and LED resources
266- ButtonResource * button_resource = new ButtonResource () ;
267- LedResource * led_resource = new LedResource () ;
302+ ButtonResource button_resource;
303+ LedResource led_resource;
268304
269305 // On press of SW3 button on K64F board, example application
270306 // will call unregister API towards mbed Device Connector
271307 // unreg_button.fall(&mbed_client,&MbedClient::test_unregister);
272308 unreg_button.fall (&unregister);
273309
274310 // Observation Button (SW2) press will send update of endpoint resource values to connector
275- obs_button.fall (button_resource, &ButtonResource::handle_button_click );
311+ obs_button.fall (&button_clicked );
276312
277313 // Create endpoint interface to manage register and unregister
278- mbed_client.create_interface (network_stack);
314+ mbed_client.create_interface (MBED_SERVER_ADDRESS, network_stack);
279315
280316 // Create Objects of varying types, see simpleclient.h for more details on implementation.
281317 M2MSecurity* register_object = mbed_client.create_register_object (); // server object specifying connector info
@@ -286,8 +322,8 @@ int main() {
286322
287323 // Add objects to list
288324 object_list.push_back (device_object);
289- object_list.push_back (button_resource-> get_object ());
290- object_list.push_back (led_resource-> get_object ());
325+ object_list.push_back (button_resource. get_object ());
326+ object_list.push_back (led_resource. get_object ());
291327
292328 // Set endpoint registration object
293329 mbed_client.set_register_object (register_object);
@@ -298,14 +334,19 @@ int main() {
298334
299335 while (true ) {
300336 updates.wait (25000 );
301-
302- if (!registered) {
337+ if (registered) {
338+ if (!clicked) {
339+ mbed_client.test_update_register ();
340+ }
341+ }else {
303342 break ;
304343 }
344+ if (clicked) {
345+ clicked = false ;
346+ button_resource.handle_button_click ();
347+ }
305348 }
306349
307350 mbed_client.test_unregister ();
308351 status_ticker.detach ();
309352}
310-
311-
0 commit comments