Brian Daniels / Mbed 2 deprecated nespresso_demo

Dependencies:   EthernetInterface mbed-rtos mbed nsdl rgb_sensor_buffer

Fork of mbed_nsdl by Nespresso RGB Sensor

Committer:
GeofferyOmlette
Date:
Wed Jun 18 16:25:55 2014 +0000
Revision:
2:88a30cc88a86
Child:
3:8e1117ec91ba
hello

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GeofferyOmlette 2:88a30cc88a86 1 // GPS resource implementation
GeofferyOmlette 2:88a30cc88a86 2
GeofferyOmlette 2:88a30cc88a86 3 #include "mbed.h"
GeofferyOmlette 2:88a30cc88a86 4 #include "rtos.h"
GeofferyOmlette 2:88a30cc88a86 5 #include "nsdl_support.h"
GeofferyOmlette 2:88a30cc88a86 6 #include "nespresso.h"
GeofferyOmlette 2:88a30cc88a86 7
GeofferyOmlette 2:88a30cc88a86 8 #define NESPRESSO_RES_ID "sen/0"
GeofferyOmlette 2:88a30cc88a86 9
GeofferyOmlette 2:88a30cc88a86 10 extern Serial pc;
GeofferyOmlette 2:88a30cc88a86 11 /* stored data for observable resource */
GeofferyOmlette 2:88a30cc88a86 12 static uint8_t obs_number = 0;
GeofferyOmlette 2:88a30cc88a86 13 static uint8_t *obs_token_ptr = NULL;
GeofferyOmlette 2:88a30cc88a86 14 static uint8_t obs_token_len = 0;
GeofferyOmlette 2:88a30cc88a86 15
GeofferyOmlette 2:88a30cc88a86 16 static uint8_t res_nespresso_val[] = {"Decaffeinto Lungo (Orange)"};
GeofferyOmlette 2:88a30cc88a86 17
GeofferyOmlette 2:88a30cc88a86 18 /* Thread for calling libNsdl exec function (cleanup, resendings etc..) */
GeofferyOmlette 2:88a30cc88a86 19 /* Node updates nespresso value every 10 seconds. Notification sending is done here. */
GeofferyOmlette 2:88a30cc88a86 20 static void exec_call_thread(void const *args)
GeofferyOmlette 2:88a30cc88a86 21 {
GeofferyOmlette 2:88a30cc88a86 22 int32_t time = 0;
GeofferyOmlette 2:88a30cc88a86 23 while (true)
GeofferyOmlette 2:88a30cc88a86 24 {
GeofferyOmlette 2:88a30cc88a86 25 wait(1);
GeofferyOmlette 2:88a30cc88a86 26 time++;
GeofferyOmlette 2:88a30cc88a86 27 sn_nsdl_exec(time);
GeofferyOmlette 2:88a30cc88a86 28 if((!(time % 10)) && obs_number != 0 && obs_token_ptr != NULL)
GeofferyOmlette 2:88a30cc88a86 29 {
GeofferyOmlette 2:88a30cc88a86 30 obs_number++;
GeofferyOmlette 2:88a30cc88a86 31 if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)res_nespresso_val, (sizeof(res_nespresso_val)-1), &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0)
GeofferyOmlette 2:88a30cc88a86 32 pc.printf("Observation sending failed\r\n");
GeofferyOmlette 2:88a30cc88a86 33 else
GeofferyOmlette 2:88a30cc88a86 34 pc.printf("Observation\r\n");
GeofferyOmlette 2:88a30cc88a86 35 }
GeofferyOmlette 2:88a30cc88a86 36 }
GeofferyOmlette 2:88a30cc88a86 37 }
GeofferyOmlette 2:88a30cc88a86 38
GeofferyOmlette 2:88a30cc88a86 39 /* Only GET method allowed */
GeofferyOmlette 2:88a30cc88a86 40 static uint8_t nespresso_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
GeofferyOmlette 2:88a30cc88a86 41 {
GeofferyOmlette 2:88a30cc88a86 42 sn_coap_hdr_s *coap_res_ptr = 0;
GeofferyOmlette 2:88a30cc88a86 43
GeofferyOmlette 2:88a30cc88a86 44 pc.printf("nespresso callback\r\n");
GeofferyOmlette 2:88a30cc88a86 45
GeofferyOmlette 2:88a30cc88a86 46 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
GeofferyOmlette 2:88a30cc88a86 47
GeofferyOmlette 2:88a30cc88a86 48 coap_res_ptr->payload_len = sizeof(res_nespresso_val)-1;
GeofferyOmlette 2:88a30cc88a86 49 coap_res_ptr->payload_ptr = (uint8_t*)res_nespresso_val;
GeofferyOmlette 2:88a30cc88a86 50
GeofferyOmlette 2:88a30cc88a86 51 if(received_coap_ptr->token_ptr)
GeofferyOmlette 2:88a30cc88a86 52 {
GeofferyOmlette 2:88a30cc88a86 53 pc.printf("Token included\r\n");
GeofferyOmlette 2:88a30cc88a86 54 if(obs_token_ptr)
GeofferyOmlette 2:88a30cc88a86 55 {
GeofferyOmlette 2:88a30cc88a86 56 free(obs_token_ptr);
GeofferyOmlette 2:88a30cc88a86 57 obs_token_ptr = 0;
GeofferyOmlette 2:88a30cc88a86 58 }
GeofferyOmlette 2:88a30cc88a86 59 obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len);
GeofferyOmlette 2:88a30cc88a86 60 if(obs_token_ptr)
GeofferyOmlette 2:88a30cc88a86 61 {
GeofferyOmlette 2:88a30cc88a86 62 memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len);
GeofferyOmlette 2:88a30cc88a86 63 obs_token_len = received_coap_ptr->token_len;
GeofferyOmlette 2:88a30cc88a86 64 }
GeofferyOmlette 2:88a30cc88a86 65 }
GeofferyOmlette 2:88a30cc88a86 66
GeofferyOmlette 2:88a30cc88a86 67 if(received_coap_ptr->options_list_ptr->observe)
GeofferyOmlette 2:88a30cc88a86 68 {
GeofferyOmlette 2:88a30cc88a86 69 coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s));
GeofferyOmlette 2:88a30cc88a86 70 memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
GeofferyOmlette 2:88a30cc88a86 71 coap_res_ptr->options_list_ptr->observe_ptr = &obs_number;
GeofferyOmlette 2:88a30cc88a86 72 coap_res_ptr->options_list_ptr->observe_len = 1;
GeofferyOmlette 2:88a30cc88a86 73 obs_number++;
GeofferyOmlette 2:88a30cc88a86 74 }
GeofferyOmlette 2:88a30cc88a86 75
GeofferyOmlette 2:88a30cc88a86 76 sn_nsdl_send_coap_message(address, coap_res_ptr);
GeofferyOmlette 2:88a30cc88a86 77
GeofferyOmlette 2:88a30cc88a86 78 coap_res_ptr->options_list_ptr->observe_ptr = 0;
GeofferyOmlette 2:88a30cc88a86 79 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
GeofferyOmlette 2:88a30cc88a86 80
GeofferyOmlette 2:88a30cc88a86 81 return 0;
GeofferyOmlette 2:88a30cc88a86 82 }
GeofferyOmlette 2:88a30cc88a86 83
GeofferyOmlette 2:88a30cc88a86 84 int create_nespresso_resource(sn_nsdl_resource_info_s *resource_ptr)
GeofferyOmlette 2:88a30cc88a86 85 {
GeofferyOmlette 2:88a30cc88a86 86 static Thread exec_thread(exec_call_thread);
GeofferyOmlette 2:88a30cc88a86 87
GeofferyOmlette 2:88a30cc88a86 88 nsdl_create_dynamic_resource(resource_ptr, sizeof(NESPRESSO_RES_ID)-1, (uint8_t*)NESPRESSO_RES_ID, 0, 0, 1, &nespresso_resource_cb, SN_GRS_GET_ALLOWED);
GeofferyOmlette 2:88a30cc88a86 89 return 0;
GeofferyOmlette 2:88a30cc88a86 90 }