This is a full demo that connects to mbed Device Server. It is used with a I2C Grove Color sensor, so it should be compatible on all platforms that support Ethernet.

Dependencies:   ColorDetectorV2 EthernetInterface mbed-rtos mbed nsdl

Fork of nespresso_endpoint by Brian Daniels

mbed Software Configuration

The default mbed Device Server (NSP or mDS) configuration is used by an instance of the web app (Barista) here: http://barista-test.cloudapp.net:4005.

In main.cpp, find the following lines:

// ****************************************************************************
// Configuration section
 
// I2C Settings
#define COLOR_SENSOR_SCL I2C_SCL
#define COLOR_SENSOR_SDA I2C_SDA
 
// Ethernet configuration
/* Define this to enable DHCP, otherwise manual address configuration is used */
#define DHCP
 
/* Manual IP configurations, if DHCP not defined */
#define IP      "0.0.0.0"
#define MASK    "255.255.255.0"
#define GW      "0.0.0.0"
 
// NSP configuration
/* Change this IP address to that of your mbed Device Server installation if you're not using mbed Connector */
static const char* NSP_ADDRESS = "api.connector.mbed.org";
static const int NSP_PORT = 5683;
char endpoint_name[] = "nespresso-machine-ethernet";
uint8_t ep_type[] = {"nespresso-endpoint"};
char nsp_domain[] = "56645321f5e24c49908e42f4d71b9ccb";
uint8_t lifetime_ptr[] = {"1200"};
 
// ****************************************************************************

Edit COLOR_SENSOR_SCL and COLOR_SENSOR_SDA to the appropriate I2C pins on your mbed platform.

You should not edit the DHCP/static IP settings if you are not using a static IP address. If you are using a static IP address, provide the appropriate information and remove the following line:

#define DHCP

Change endpoint_name to uniquely identify your Nespresso endpoint.

If you are using mbed Connector, change the nsp_domain string to your domain.

If you are using your own installation of mDS, you should change NSP_ADDRESS to the url of your mDS installation, NSP_PORT if you're using a non default port on mDS for CoAP, and nsp_domain to a valid domain on your mDS instance ("domain" is the default mDS domain).

nsdl_support.cpp

Committer:
bridadan
Date:
2015-07-16
Revision:
15:73e84ac7fb2d
Parent:
13:867a5ab8d474

File content as of revision 15:73e84ac7fb2d:

// NSDL library support functions

#include "mbed.h"
#include "nsdl_support.h"
#include "mbed.h"
#include "rtos.h"
#include "EthernetInterface.h"

extern Serial pc;
extern EthernetInterface eth;
extern Endpoint nsp;
extern UDPSocket server;
extern char endpoint_name[];
extern char nsp_domain[];
extern uint8_t ep_type[];
extern uint8_t lifetime_ptr[];

// Registration update variables
sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
Ticker registration_updater;
bool update_registration = false;

// NSDL loop variables
sn_nsdl_addr_s received_packet_address;
uint8_t received_address[4];
char buffer[1024];
Endpoint from;

/* The number of seconds between NSP registration messages */
#define RD_UPDATE_PERIOD  60

void *nsdl_alloc(uint16_t size)
{
    return malloc(size);
}

void nsdl_free(void* ptr_to_free)
{
    free(ptr_to_free);
}

/*
 * Create a static resoure
 * Only get is allowed
 */
void nsdl_create_static_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t *rsc, uint16_t rsc_len)
{
    resource_structure->access = SN_GRS_GET_ALLOWED;
    resource_structure->mode = SN_GRS_STATIC;
    resource_structure->pathlen = pt_len;
    resource_structure->path = pt;
    resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
    resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
    resource_structure->resource = rsc;
    resource_structure->resourcelen = rsc_len;
    sn_nsdl_create_resource(resource_structure);
}

void nsdl_create_dynamic_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t is_observable, sn_grs_dyn_res_callback_t callback_ptr, int access_right)
{
    resource_structure->access = (sn_grs_resource_acl_e)access_right;
    resource_structure->resource = 0;
    resource_structure->resourcelen = 0;
    resource_structure->sn_grs_dyn_res_callback = callback_ptr;
    resource_structure->mode = SN_GRS_DYNAMIC;
    resource_structure->pathlen = pt_len;
    resource_structure->path = pt;
    resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
    resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
    resource_structure->resource_parameters_ptr->observable = is_observable;
    sn_nsdl_create_resource(resource_structure);
}

sn_nsdl_ep_parameters_s* nsdl_init_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_structure, uint8_t* name, uint8_t* typename_ptr, uint8_t *lifetime_ptr)
{
    printf("NSP_DOMAIN: %s\r\n", nsp_domain);
    if (NULL == endpoint_structure)
    {   
        endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s));
    }
    if (endpoint_structure)
    {        
        memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s));
        endpoint_structure->endpoint_name_ptr = name;
        endpoint_structure->endpoint_name_len = strlen((char*)name);
        endpoint_structure->domain_name_ptr = (uint8_t*)nsp_domain;
        endpoint_structure->domain_name_len = strlen((char*)nsp_domain);
        endpoint_structure->type_ptr = typename_ptr;
        endpoint_structure->type_len =  strlen((char*)typename_ptr);
        endpoint_structure->lifetime_ptr = lifetime_ptr;
        endpoint_structure->lifetime_len =  strlen((char*)lifetime_ptr);
    }
    return endpoint_structure;
}

void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure)
{
    if (*endpoint_structure)
    {
        nsdl_free(*endpoint_structure);
        *endpoint_structure = NULL;
    }
}

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);

    if(server.sendTo(nsp, (char*)data_ptr, data_len) != data_len)
        pc.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");
    return 0;
}

void registration_ticker_func() {
    update_registration = true;
}

bool register_endpoint(bool init) {
    bool endpoint_registered;
    sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
    if (init) {
        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 initial registration failed\r\n");
            endpoint_registered = false;
        }
        else {
            pc.printf("NSP initial registration OK\r\n");
            endpoint_registered = true;
        }
    }
    else {
        endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, NULL, NULL, NULL);
        if(sn_nsdl_update_registration(endpoint_ptr) != 0) {
            pc.printf("NSP re-registration failed\r\n");
            endpoint_registered = false;
        }
        else {
            pc.printf("NSP re-registration OK\r\n");
            endpoint_registered = true;
        }
    }
    nsdl_clean_register_endpoint(&endpoint_ptr);
    
    return endpoint_registered;
}
void nsdl_init()
{
    uint8_t nsp_addr[4];
    sn_nsdl_mem_s memory_cbs;

    /* Initialize libNsdl */
    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");
    else
        pc.printf("libNsdl init done\r\n");

    /* Set nsp address for library */
    set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4);    
}

void nsdl_event_loop_init() {
    server.set_blocking(false, 0);
    memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s));
    received_packet_address.addr_ptr = received_address;
    
    registration_updater.attach(&registration_ticker_func, RD_UPDATE_PERIOD);
}

void nsdl_event_loop_run_once()
{
    if (update_registration) {
        update_registration = false;
        if (!register_endpoint(false)) {
            register_endpoint(true);
        }
    }
    
    int n = server.receiveFrom(from, buffer, sizeof(buffer));
    
    if (n < 0)
    {
        pc.printf("Socket error\n\r");
    }
    else
    {   
        sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address);
    }
}