Brian Daniels / Mbed 2 deprecated nespresso_demo

Dependencies:   EthernetInterface mbed-rtos mbed nsdl rgb_sensor_buffer

Fork of mbed_nsdl by Nespresso RGB Sensor

Files at this revision

API Documentation at this revision

Comitter:
bjblazkowicz
Date:
Wed Oct 29 18:28:59 2014 +0000
Parent:
8:27e94f52810b
Child:
10:9c11a8889d62
Commit message:
Switched to using MODSERIAL.; Added BLE beacon resource.

Changed in this revision

MODSERIAL.lib Show annotated file Show diff for this revision Revisions of this file
dbg.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
nsdl_support.cpp Show annotated file Show diff for this revision Revisions of this file
resources/beacon_resource.cpp Show annotated file Show diff for this revision Revisions of this file
resources/beacon_resource.h Show annotated file Show diff for this revision Revisions of this file
resources/buffered_rgb_resource.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MODSERIAL.lib	Wed Oct 29 18:28:59 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/AjK/code/MODSERIAL/#ae0408ebdd68
--- a/dbg.h	Mon Sep 15 14:14:44 2014 +0000
+++ b/dbg.h	Wed Oct 29 18:28:59 2014 +0000
@@ -8,8 +8,8 @@
 #define DEBUG 1
 
 #if (DEBUG)
-extern Serial pc;
-#define NSDL_DEBUG(x, ...) pc.printf("[NSDL_DEBUG: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
+extern MODSERIAL console;
+#define NSDL_DEBUG(x, ...) console.printf("[NSDL_DEBUG: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
 #else
 #define NSDL_DEBUG(x, ...)
 #endif
--- a/main.cpp	Mon Sep 15 14:14:44 2014 +0000
+++ b/main.cpp	Wed Oct 29 18:28:59 2014 +0000
@@ -1,10 +1,12 @@
 #include "mbed.h"
+#include "MODSERIAL.h"
 #include "EthernetInterface.h"
 #include "nsdl_support.h"
 #include "dbg.h"
 #include "buffered_rgb_resource.h"
+#include "beacon_resource.h"
 
-Serial pc(USBTX, USBRX); // tx, rx
+MODSERIAL console(USBTX, USBRX); // tx, rx
 
 // ****************************************************************************
 // Configuration section
@@ -19,8 +21,8 @@
 #define GW      "10.45.0.1"
 
 // NSP configuration
-/* Change this IP address to that of your NanoService Platform installation */
-static const char* NSP_ADDRESS = "23.99.96.113";
+/* Change this create_beacon_resource IP address to that of your NanoService Platform installation */
+static const char* NSP_ADDRESS = "191.239.5.150";
 static const int NSP_PORT = 5683;
 char endpoint_name[] = "nespresso-client";
 uint8_t ep_type[] = {"mbed_device"};
@@ -40,10 +42,9 @@
 #else
     eth.init(IP, MASK, GW);
 #endif
-    if(eth.connect(30000) == 0) 
-    {
-        pc.printf("Connect OK\n\r");
-    } 
+    if(eth.connect(30000) == 0) {
+        console.printf("Connect OK\n\r");
+    }
 
     NSDL_DEBUG("IP Address:%s ", eth.getIPAddress());
 }
@@ -60,7 +61,7 @@
     server.bind(NSP_PORT);
 
     nsp.set_address(NSP_ADDRESS, NSP_PORT);
-    
+
     NSDL_DEBUG("name: %s", endpoint_name);
     NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT);
 }
@@ -72,7 +73,7 @@
 {
     sn_nsdl_resource_info_s *resource_ptr = NULL;
     sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
-    
+
     NSDL_DEBUG("Creating resources");
 
     /* Create resources */
@@ -82,22 +83,21 @@
     memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
 
     resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
-    if(!resource_ptr->resource_parameters_ptr)
-    {
+    if(!resource_ptr->resource_parameters_ptr) {
         nsdl_free(resource_ptr);
         return 0;
     }
     memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
 
-    //create_nespresso_resource(resource_ptr);
+    create_beacon_resource(resource_ptr);
     create_buffered_rgb_resource(resource_ptr);
 
     /* Register with NSP */
     endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
     if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
-        pc.printf("NSP registering failed\r\n");
+        console.printf("NSP registering failed\r\n");
     else
-        pc.printf("NSP registering OK\r\n");
+        console.printf("NSP registering OK\r\n");
     nsdl_clean_register_endpoint(&endpoint_ptr);
 
     nsdl_free(resource_ptr->resource_parameters_ptr);
@@ -110,28 +110,28 @@
 
 int main()
 {
-    pc.baud(115200);
+    console.baud(115200);
 
     // Initialize Ethernet interface first
-    pc.printf("Initializing ethernet... ");
+    console.printf("Initializing ethernet... ");
     ethernet_init();
-    pc.printf("done.\r\n");
-        
+    console.printf("done.\r\n");
+
     // Initialize NSP node
-    pc.printf("Initializing nsp node... ");
+    console.printf("Initializing nsp node... ");
     nsp_init();
-    pc.printf("done.\r\n");
-    
+    console.printf("done.\r\n");
+
     // Initialize NSDL stack
-    pc.printf("Initializing NSDL stack... ");    
+    console.printf("Initializing NSDL stack... ");
     nsdl_init();
-    pc.printf("done.\r\n");
-    
+    console.printf("done.\r\n");
+
     // Create NSDL resources
-    pc.printf("Initializing resources... ");    
+    console.printf("Initializing resources... ");
     create_resources();
-    pc.printf("done.\r\n");
-    
+    console.printf("done.\r\n");
+
     // Run the NSDL event loop (never returns)
     nsdl_event_loop();
 }
--- a/nsdl_support.cpp	Mon Sep 15 14:14:44 2014 +0000
+++ b/nsdl_support.cpp	Wed Oct 29 18:28:59 2014 +0000
@@ -1,12 +1,13 @@
 // NSDL library support functions
 
 #include "mbed.h"
+#include "MODSERIAL.h"
 #include "nsdl_support.h"
 #include "mbed.h"
 #include "rtos.h"
 #include "EthernetInterface.h"
 
-extern Serial pc;
+extern MODSERIAL console;
 extern EthernetInterface eth;
 extern Endpoint nsp;
 extern UDPSocket server;
@@ -93,17 +94,17 @@
 
 static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr)
 {
-    pc.printf("TX callback!\n\rSending %d bytes\r\n", data_len);
+    console.printf("TX callback!\n\rSending %d bytes\r\n", data_len);
 
     if(server.sendTo(nsp, (char*)data_ptr, data_len) != data_len)
-        pc.printf("sending failed\n\r");
+        console.printf("sending failed\n\r");
 
     return 1;
 }
 
 static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr)
 {
-    pc.printf("RX callback!\r\n");
+    console.printf("RX callback!\r\n");
     return 0;
 }
 
@@ -116,9 +117,9 @@
         wait(RD_UPDATE_PERIOD);
         endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
         if(sn_nsdl_update_registration(endpoint_ptr) != 0)
-            pc.printf("NSP re-registering failed\r\n");
+            console.printf("NSP re-registering failed\r\n");
         else
-            pc.printf("NSP re-registering OK\r\n");
+            console.printf("NSP re-registering OK\r\n");
         nsdl_clean_register_endpoint(&endpoint_ptr);
     }
 }
@@ -132,9 +133,9 @@
     memory_cbs.sn_nsdl_alloc = &nsdl_alloc;
     memory_cbs.sn_nsdl_free = &nsdl_free;
     if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1)
-        pc.printf("libNsdl init failed\r\n");
+        console.printf("libNsdl init failed\r\n");
     else
-        pc.printf("libNsdl init done\r\n");
+        console.printf("libNsdl init done\r\n");
 
     /* Set nsp address for library */
     set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4);
@@ -157,11 +158,11 @@
         int n = server.receiveFrom(from, buffer, sizeof(buffer));
         if (n < 0)
         {
-            pc.printf("Socket error\n\r");
+            console.printf("Socket error\n\r");
         }
         else
         {   
-            pc.printf("Received %d bytes\r\n", n);
+            console.printf("Received %d bytes\r\n", n);
             sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address);
         }
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/resources/beacon_resource.cpp	Wed Oct 29 18:28:59 2014 +0000
@@ -0,0 +1,129 @@
+#include <ctype.h>
+#include "mbed.h"
+#include "rtos.h"
+#include "MODSERIAL.h"
+#include "nsdl_support.h"
+#include "beacon_resource.h"
+#include "payload.h"
+
+#define RESOURCE_ID    "sensor/beacon"
+
+extern MODSERIAL console;
+
+/* stored data for observable resource */
+static uint8_t obs_number = 0;
+static uint8_t *obs_token_ptr = NULL;
+static uint8_t obs_token_len = 0;
+
+static MODSERIAL beacon(p9, p10);
+
+#define UUID_LEN    (36)
+
+static const char *UUID = "3AD81C9B-8B1A-494D-B0C3-C1B6DC87A2A8";
+
+//
+// A BLE beacon advertisement record
+typedef struct { 
+    char uuid[UUID_LEN + 1];
+    int rssi;
+    int calibration;
+    int major;
+    int minor;
+    int channel;
+} beacon_record_t;
+
+static beacon_record_t record = {0};
+
+// Encode the beacon record as JSON.
+void encode_beacon_record(const beacon_record_t *record, char *json, int *json_length) {
+    static const char *format = "{'r':%d, 'c':%d,'m':%d,'n':%d,'ch':%d}";
+    *json_length = sprintf(json, format, record->rssi, record->calibration, record->major, record->minor, record->channel);
+}
+
+
+// read next character from UART
+static inline int next_char() {
+    while (!beacon.readable()) {
+        __WFE();
+    }
+    return beacon.getc();
+}
+
+// Given that we have consumed the begin record character ('>'), parse 
+// characters until a comma is met, and confirm this is the UUID we are 
+// looking for.
+//
+// Returns true if the read UUID matches, or false otherwise.
+//
+static bool parse_uuid(char *uuid) {
+    int chars_read = 0;
+    
+    int ch = next_char();
+    while (isspace(ch)) {
+        ch = next_char();
+    }
+    
+    do {
+        uuid[chars_read++] = ch;
+    } while ((ch = next_char()) != ',' && chars_read <= UUID_LEN);
+    
+    uuid[chars_read] = '\0';
+    
+    return chars_read == UUID_LEN && memcmp(uuid, UUID, UUID_LEN) == 0;
+}
+
+// Parse an integer using the given radix.
+static void parse_int(int *value, int radix) {
+    static const int BUF_SIZE = 16;
+    char buf[BUF_SIZE] = {0};
+    int chars_read = 0;
+    int ch = next_char();
+    
+    do {
+        buf[chars_read++] = ch;
+    } while ((ch = next_char()) != ',' && chars_read <= BUF_SIZE);
+    
+    *value = strtol(buf, NULL, radix);
+}
+
+static void exec_call_thread(void const *args)
+{
+    while(1) {
+        int ch = next_char();
+        if (ch == '>') {
+            if (!parse_uuid(record.uuid)) {
+                continue;
+            }
+            
+            parse_int(&record.rssi, 10);
+            parse_int(&record.calibration, 10);
+            parse_int(&record.major, 16);
+            parse_int(&record.minor, 16);
+            parse_int(&record.channel, 10);
+            
+            console.printf("Parsed record: [uuid:%s, rssi:%d, calibration:%d, major:%d, minor:%d, channel: %d]\r\n", record.uuid, record.rssi, record.calibration, record.major, record.minor, record.channel);
+            
+            if (obs_number == 0 && obs_token_ptr == NULL)
+                console.printf("ERROR: obs_number: %d, obs_token_ptr: %0x%08x\r\n", obs_number, obs_token_ptr);
+    
+            obs_number++;
+            
+            static const int JSON_BUFFER_SIZE = 1024;
+            char json[JSON_BUFFER_SIZE] = {0};
+            int json_length = 0;
+            encode_beacon_record(&record, json, &json_length);
+            
+            int result = sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t *)json, json_length, &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0);
+            console.printf("Sent to mDS: '%s', result: %d\r\n", json, result);
+        }
+    }
+}
+
+int create_beacon_resource(sn_nsdl_resource_info_s *resource_ptr)
+{
+    beacon.baud(115200);
+    static Thread exec_thread(exec_call_thread);   
+    //nsdl_create_dynamic_resource(resource_ptr, sizeof(RESOURCE_ID) - 1, (uint8_t*)RESOURCE_ID, 0, 0, 1, &resource_cb, SN_GRS_GET_ALLOWED);    
+    console.printf("Created beacon resource\r\n");
+    return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/resources/beacon_resource.h	Wed Oct 29 18:28:59 2014 +0000
@@ -0,0 +1,10 @@
+// BLE Beacon resource
+
+#ifndef BEACON_RESOURCE_H
+#define BEACON_RESOURCE_H
+
+#include "nsdl_support.h"
+
+int create_beacon_resource(sn_nsdl_resource_info_s *resource_ptr);
+
+#endif
--- a/resources/buffered_rgb_resource.cpp	Mon Sep 15 14:14:44 2014 +0000
+++ b/resources/buffered_rgb_resource.cpp	Wed Oct 29 18:28:59 2014 +0000
@@ -1,5 +1,6 @@
 #include "mbed.h"
 #include "rtos.h"
+#include "MODSERIAL.h"
 #include "nsdl_support.h"
 #include "buffered_rgb_resource.h"
 #include "rgb_sensor_buffer.h"
@@ -9,7 +10,7 @@
 #define RGB_VALUES 512
 #define RGB_THRESHOLD 115
 
-extern Serial pc;
+extern MODSERIAL console;
 /* stored data for observable resource */
 static uint8_t obs_number = 0;
 static uint8_t *obs_token_ptr = NULL;
@@ -24,17 +25,17 @@
     while (true)
     {
         if (obs_number == 0 && obs_token_ptr == NULL)
-            pc.printf("ERROR: obs_number: %d, obs_token_ptr: %0x%08x\r\n", obs_number, obs_token_ptr);
+            console.printf("ERROR: obs_number: %d, obs_token_ptr: %0x%08x\r\n", obs_number, obs_token_ptr);
 
         obs_number++;        
-        pc.printf("Waiting for detector...\r\n");        
-        int samples_captured = g_rgb.trigger(g_buffer, RGB_VALUES, RGB_THRESHOLD); 
-        
+        console.printf("Waiting for detector...\r\n");        
+        int samples_captured = g_rgb.trigger(g_buffer, RGB_VALUES, RGB_THRESHOLD);
+
         g_payload.build(g_buffer, samples_captured);
         
         int result = sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, g_payload.raw_bytes(), g_payload.raw_bytes_size(), &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0);
         
-        pc.printf("Observation send %s. [samples: %d, bytes: %d]\r\n", result == 0 ? "failed" : "succeeded", samples_captured, g_payload.raw_bytes_size());
+        console.printf("Observation send %s. [samples: %d, bytes: %d]\r\n", result == 0 ? "failed" : "succeeded", samples_captured, g_payload.raw_bytes_size());
     }
 }