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