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:
Fri Dec 05 17:23:31 2014 +0000
Revision:
11:2a853cd96bf7
Parent:
9:9d5b0c43579b
Small refactoring - domains and mDS IP address.

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