Nespresso coffee demo working on the Arch Pro

Dependencies:   EthernetInterface mbed-rtos mbed nsdl rgb_sensor_buffer

Fork of mbed_nsdl by Nespresso RGB Sensor

Committer:
bjblazkowicz
Date:
Mon Jul 14 10:45:33 2014 +0000
Revision:
4:ab3c8d25260e
Parent:
3:8e1117ec91ba
Child:
5:cb0df4fbe7a1
First successful end-to-end integration.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GeofferyOmlette 0:345864e9ee85 1 // NSDL library support functions
GeofferyOmlette 0:345864e9ee85 2
GeofferyOmlette 0:345864e9ee85 3 #include "mbed.h"
GeofferyOmlette 0:345864e9ee85 4 #include "nsdl_support.h"
GeofferyOmlette 0:345864e9ee85 5 #include "mbed.h"
GeofferyOmlette 0:345864e9ee85 6 #include "rtos.h"
GeofferyOmlette 0:345864e9ee85 7 #include "EthernetInterface.h"
GeofferyOmlette 0:345864e9ee85 8
GeofferyOmlette 0:345864e9ee85 9 extern Serial pc;
GeofferyOmlette 0:345864e9ee85 10 extern EthernetInterface eth;
GeofferyOmlette 0:345864e9ee85 11 extern Endpoint nsp;
GeofferyOmlette 0:345864e9ee85 12 extern UDPSocket server;
GeofferyOmlette 0:345864e9ee85 13 extern char endpoint_name[16];
GeofferyOmlette 0:345864e9ee85 14 extern uint8_t ep_type[];
GeofferyOmlette 0:345864e9ee85 15 extern uint8_t lifetime_ptr[];
GeofferyOmlette 0:345864e9ee85 16
GeofferyOmlette 0:345864e9ee85 17 /* The number of seconds between NSP registration messages */
GeofferyOmlette 0:345864e9ee85 18 #define RD_UPDATE_PERIOD 60
GeofferyOmlette 0:345864e9ee85 19
GeofferyOmlette 0:345864e9ee85 20 void *nsdl_alloc(uint16_t size)
GeofferyOmlette 0:345864e9ee85 21 {
GeofferyOmlette 0:345864e9ee85 22 return malloc(size);
GeofferyOmlette 0:345864e9ee85 23 }
GeofferyOmlette 0:345864e9ee85 24
GeofferyOmlette 0:345864e9ee85 25 void nsdl_free(void* ptr_to_free)
GeofferyOmlette 0:345864e9ee85 26 {
GeofferyOmlette 0:345864e9ee85 27 free(ptr_to_free);
GeofferyOmlette 0:345864e9ee85 28 }
GeofferyOmlette 0:345864e9ee85 29
GeofferyOmlette 0:345864e9ee85 30 /*
GeofferyOmlette 0:345864e9ee85 31 * Create a static resoure
GeofferyOmlette 0:345864e9ee85 32 * Only get is allowed
GeofferyOmlette 0:345864e9ee85 33 */
GeofferyOmlette 0:345864e9ee85 34 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)
GeofferyOmlette 0:345864e9ee85 35 {
GeofferyOmlette 0:345864e9ee85 36 resource_structure->access = SN_GRS_GET_ALLOWED;
GeofferyOmlette 0:345864e9ee85 37 resource_structure->mode = SN_GRS_STATIC;
GeofferyOmlette 0:345864e9ee85 38 resource_structure->pathlen = pt_len;
GeofferyOmlette 0:345864e9ee85 39 resource_structure->path = pt;
GeofferyOmlette 0:345864e9ee85 40 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
GeofferyOmlette 0:345864e9ee85 41 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
GeofferyOmlette 0:345864e9ee85 42 resource_structure->resource = rsc;
GeofferyOmlette 0:345864e9ee85 43 resource_structure->resourcelen = rsc_len;
GeofferyOmlette 0:345864e9ee85 44 sn_nsdl_create_resource(resource_structure);
GeofferyOmlette 0:345864e9ee85 45 }
GeofferyOmlette 0:345864e9ee85 46
GeofferyOmlette 0:345864e9ee85 47 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)
GeofferyOmlette 0:345864e9ee85 48 {
GeofferyOmlette 0:345864e9ee85 49 resource_structure->access = (sn_grs_resource_acl_e)access_right;
GeofferyOmlette 0:345864e9ee85 50 resource_structure->resource = 0;
GeofferyOmlette 0:345864e9ee85 51 resource_structure->resourcelen = 0;
GeofferyOmlette 0:345864e9ee85 52 resource_structure->sn_grs_dyn_res_callback = callback_ptr;
GeofferyOmlette 0:345864e9ee85 53 resource_structure->mode = SN_GRS_DYNAMIC;
GeofferyOmlette 0:345864e9ee85 54 resource_structure->pathlen = pt_len;
GeofferyOmlette 0:345864e9ee85 55 resource_structure->path = pt;
GeofferyOmlette 0:345864e9ee85 56 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
GeofferyOmlette 0:345864e9ee85 57 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
GeofferyOmlette 0:345864e9ee85 58 resource_structure->resource_parameters_ptr->observable = is_observable;
GeofferyOmlette 0:345864e9ee85 59 sn_nsdl_create_resource(resource_structure);
GeofferyOmlette 0:345864e9ee85 60 }
GeofferyOmlette 0:345864e9ee85 61
GeofferyOmlette 0:345864e9ee85 62 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)
GeofferyOmlette 0:345864e9ee85 63 {
GeofferyOmlette 0:345864e9ee85 64 if (NULL == endpoint_structure)
GeofferyOmlette 0:345864e9ee85 65 {
GeofferyOmlette 0:345864e9ee85 66 endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s));
GeofferyOmlette 0:345864e9ee85 67 }
GeofferyOmlette 0:345864e9ee85 68 if (endpoint_structure)
GeofferyOmlette 0:345864e9ee85 69 {
GeofferyOmlette 0:345864e9ee85 70 memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s));
GeofferyOmlette 0:345864e9ee85 71 endpoint_structure->endpoint_name_ptr = name;
GeofferyOmlette 0:345864e9ee85 72 endpoint_structure->endpoint_name_len = strlen((char*)name);
bjblazkowicz 3:8e1117ec91ba 73 // endpoint_structure->domain_name_ptr = (uint8_t*) "Robert\0";
bjblazkowicz 3:8e1117ec91ba 74 // endpoint_structure->domain_name_len = strlen((char*) "Robert\0");
GeofferyOmlette 0:345864e9ee85 75 endpoint_structure->type_ptr = typename_ptr;
GeofferyOmlette 0:345864e9ee85 76 endpoint_structure->type_len = strlen((char*)typename_ptr);
GeofferyOmlette 0:345864e9ee85 77 endpoint_structure->lifetime_ptr = lifetime_ptr;
GeofferyOmlette 0:345864e9ee85 78 endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr);
GeofferyOmlette 0:345864e9ee85 79 }
GeofferyOmlette 0:345864e9ee85 80 return endpoint_structure;
GeofferyOmlette 0:345864e9ee85 81 }
GeofferyOmlette 0:345864e9ee85 82
GeofferyOmlette 0:345864e9ee85 83 void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure)
GeofferyOmlette 0:345864e9ee85 84 {
GeofferyOmlette 0:345864e9ee85 85 if (*endpoint_structure)
GeofferyOmlette 0:345864e9ee85 86 {
GeofferyOmlette 0:345864e9ee85 87 nsdl_free(*endpoint_structure);
GeofferyOmlette 0:345864e9ee85 88 *endpoint_structure = NULL;
GeofferyOmlette 0:345864e9ee85 89 }
GeofferyOmlette 0:345864e9ee85 90 }
GeofferyOmlette 0:345864e9ee85 91
GeofferyOmlette 0:345864e9ee85 92 static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr)
GeofferyOmlette 0:345864e9ee85 93 {
GeofferyOmlette 0:345864e9ee85 94 pc.printf("TX callback!\n\rSending %d bytes\r\n", data_len);
GeofferyOmlette 0:345864e9ee85 95
GeofferyOmlette 0:345864e9ee85 96 if(server.sendTo(nsp, (char*)data_ptr, data_len) != data_len)
GeofferyOmlette 0:345864e9ee85 97 pc.printf("sending failed\n\r");
GeofferyOmlette 0:345864e9ee85 98
GeofferyOmlette 0:345864e9ee85 99 return 1;
GeofferyOmlette 0:345864e9ee85 100 }
GeofferyOmlette 0:345864e9ee85 101
GeofferyOmlette 0:345864e9ee85 102 static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr)
GeofferyOmlette 0:345864e9ee85 103 {
GeofferyOmlette 0:345864e9ee85 104 pc.printf("RX callback!\r\n");
GeofferyOmlette 0:345864e9ee85 105 return 0;
GeofferyOmlette 0:345864e9ee85 106 }
GeofferyOmlette 0:345864e9ee85 107
GeofferyOmlette 0:345864e9ee85 108 static void registration_update_thread(void const *args)
GeofferyOmlette 0:345864e9ee85 109 {
GeofferyOmlette 0:345864e9ee85 110 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
GeofferyOmlette 0:345864e9ee85 111
GeofferyOmlette 0:345864e9ee85 112 while(true)
GeofferyOmlette 0:345864e9ee85 113 {
GeofferyOmlette 0:345864e9ee85 114 wait(RD_UPDATE_PERIOD);
GeofferyOmlette 0:345864e9ee85 115 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
bjblazkowicz 4:ab3c8d25260e 116 if(sn_nsdl_update_registration(endpoint_ptr) != 0)
GeofferyOmlette 0:345864e9ee85 117 pc.printf("NSP re-registering failed\r\n");
GeofferyOmlette 0:345864e9ee85 118 else
GeofferyOmlette 0:345864e9ee85 119 pc.printf("NSP re-registering OK\r\n");
GeofferyOmlette 0:345864e9ee85 120 nsdl_clean_register_endpoint(&endpoint_ptr);
GeofferyOmlette 0:345864e9ee85 121 }
GeofferyOmlette 0:345864e9ee85 122 }
GeofferyOmlette 0:345864e9ee85 123
GeofferyOmlette 0:345864e9ee85 124 void nsdl_init()
GeofferyOmlette 0:345864e9ee85 125 {
GeofferyOmlette 0:345864e9ee85 126 uint8_t nsp_addr[4];
GeofferyOmlette 0:345864e9ee85 127 sn_nsdl_mem_s memory_cbs;
GeofferyOmlette 0:345864e9ee85 128
GeofferyOmlette 0:345864e9ee85 129 /* Initialize libNsdl */
GeofferyOmlette 0:345864e9ee85 130 memory_cbs.sn_nsdl_alloc = &nsdl_alloc;
GeofferyOmlette 0:345864e9ee85 131 memory_cbs.sn_nsdl_free = &nsdl_free;
GeofferyOmlette 0:345864e9ee85 132 if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1)
GeofferyOmlette 0:345864e9ee85 133 pc.printf("libNsdl init failed\r\n");
GeofferyOmlette 0:345864e9ee85 134 else
GeofferyOmlette 0:345864e9ee85 135 pc.printf("libNsdl init done\r\n");
GeofferyOmlette 0:345864e9ee85 136
GeofferyOmlette 0:345864e9ee85 137 /* Set nsp address for library */
GeofferyOmlette 0:345864e9ee85 138 set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4);
GeofferyOmlette 0:345864e9ee85 139 }
GeofferyOmlette 0:345864e9ee85 140
GeofferyOmlette 0:345864e9ee85 141 void nsdl_event_loop()
GeofferyOmlette 0:345864e9ee85 142 {
GeofferyOmlette 0:345864e9ee85 143 Thread registration_thread(registration_update_thread);
GeofferyOmlette 0:345864e9ee85 144
GeofferyOmlette 0:345864e9ee85 145 sn_nsdl_addr_s received_packet_address;
GeofferyOmlette 0:345864e9ee85 146 uint8_t received_address[4];
GeofferyOmlette 0:345864e9ee85 147 char buffer[1024];
GeofferyOmlette 0:345864e9ee85 148 Endpoint from;
GeofferyOmlette 0:345864e9ee85 149
GeofferyOmlette 0:345864e9ee85 150 memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s));
GeofferyOmlette 0:345864e9ee85 151 received_packet_address.addr_ptr = received_address;
GeofferyOmlette 0:345864e9ee85 152
GeofferyOmlette 0:345864e9ee85 153 while(1)
GeofferyOmlette 0:345864e9ee85 154 {
GeofferyOmlette 0:345864e9ee85 155 int n = server.receiveFrom(from, buffer, sizeof(buffer));
GeofferyOmlette 0:345864e9ee85 156 if (n < 0)
GeofferyOmlette 0:345864e9ee85 157 {
GeofferyOmlette 0:345864e9ee85 158 pc.printf("Socket error\n\r");
GeofferyOmlette 0:345864e9ee85 159 }
GeofferyOmlette 0:345864e9ee85 160 else
GeofferyOmlette 0:345864e9ee85 161 {
GeofferyOmlette 0:345864e9ee85 162 pc.printf("Received %d bytes\r\n", n);
GeofferyOmlette 0:345864e9ee85 163 sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address);
GeofferyOmlette 0:345864e9ee85 164 }
GeofferyOmlette 0:345864e9ee85 165 }
GeofferyOmlette 0:345864e9ee85 166 }
GeofferyOmlette 0:345864e9ee85 167
GeofferyOmlette 0:345864e9ee85 168