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:
bjblazkowicz
Date:
Mon Jun 30 09:44:00 2014 +0000
Revision:
3:8e1117ec91ba
Parent:
2:88a30cc88a86
Child:
4:ab3c8d25260e
Added dummy do_detection function.

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
bjblazkowicz 3:8e1117ec91ba 18 /*
bjblazkowicz 3:8e1117ec91ba 19 *
bjblazkowicz 3:8e1117ec91ba 20 *
bjblazkowicz 3:8e1117ec91ba 21 */
bjblazkowicz 3:8e1117ec91ba 22 const char* do_detection()
bjblazkowicz 3:8e1117ec91ba 23 {
bjblazkowicz 3:8e1117ec91ba 24 static int counter = 0;
bjblazkowicz 3:8e1117ec91ba 25
bjblazkowicz 3:8e1117ec91ba 26 const char *names[] = {
bjblazkowicz 3:8e1117ec91ba 27 {"VOLLUTO"},
bjblazkowicz 3:8e1117ec91ba 28 {"INDRIYA"},
bjblazkowicz 3:8e1117ec91ba 29 };
bjblazkowicz 3:8e1117ec91ba 30 const int num_names = sizeof(names) / sizeof(names[0]);
bjblazkowicz 3:8e1117ec91ba 31
bjblazkowicz 3:8e1117ec91ba 32 wait_ms(1500);
bjblazkowicz 3:8e1117ec91ba 33
bjblazkowicz 3:8e1117ec91ba 34 counter++;
bjblazkowicz 3:8e1117ec91ba 35 return names[counter % num_names];
bjblazkowicz 3:8e1117ec91ba 36 }
bjblazkowicz 3:8e1117ec91ba 37
GeofferyOmlette 2:88a30cc88a86 38 /* Thread for calling libNsdl exec function (cleanup, resendings etc..) */
GeofferyOmlette 2:88a30cc88a86 39 /* Node updates nespresso value every 10 seconds. Notification sending is done here. */
GeofferyOmlette 2:88a30cc88a86 40 static void exec_call_thread(void const *args)
GeofferyOmlette 2:88a30cc88a86 41 {
GeofferyOmlette 2:88a30cc88a86 42 int32_t time = 0;
GeofferyOmlette 2:88a30cc88a86 43 while (true)
GeofferyOmlette 2:88a30cc88a86 44 {
GeofferyOmlette 2:88a30cc88a86 45 time++;
GeofferyOmlette 2:88a30cc88a86 46 sn_nsdl_exec(time);
GeofferyOmlette 2:88a30cc88a86 47 if((!(time % 10)) && obs_number != 0 && obs_token_ptr != NULL)
GeofferyOmlette 2:88a30cc88a86 48 {
GeofferyOmlette 2:88a30cc88a86 49 obs_number++;
bjblazkowicz 3:8e1117ec91ba 50 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)
bjblazkowicz 3:8e1117ec91ba 51 {
GeofferyOmlette 2:88a30cc88a86 52 pc.printf("Observation sending failed\r\n");
bjblazkowicz 3:8e1117ec91ba 53 }
bjblazkowicz 3:8e1117ec91ba 54 else
bjblazkowicz 3:8e1117ec91ba 55 {
GeofferyOmlette 2:88a30cc88a86 56 pc.printf("Observation\r\n");
bjblazkowicz 3:8e1117ec91ba 57 }
GeofferyOmlette 2:88a30cc88a86 58 }
GeofferyOmlette 2:88a30cc88a86 59 }
GeofferyOmlette 2:88a30cc88a86 60 }
GeofferyOmlette 2:88a30cc88a86 61
GeofferyOmlette 2:88a30cc88a86 62 /* Only GET method allowed */
GeofferyOmlette 2:88a30cc88a86 63 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 64 {
GeofferyOmlette 2:88a30cc88a86 65 sn_coap_hdr_s *coap_res_ptr = 0;
GeofferyOmlette 2:88a30cc88a86 66
GeofferyOmlette 2:88a30cc88a86 67 pc.printf("nespresso callback\r\n");
GeofferyOmlette 2:88a30cc88a86 68
GeofferyOmlette 2:88a30cc88a86 69 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
GeofferyOmlette 2:88a30cc88a86 70
GeofferyOmlette 2:88a30cc88a86 71 coap_res_ptr->payload_len = sizeof(res_nespresso_val)-1;
GeofferyOmlette 2:88a30cc88a86 72 coap_res_ptr->payload_ptr = (uint8_t*)res_nespresso_val;
GeofferyOmlette 2:88a30cc88a86 73
GeofferyOmlette 2:88a30cc88a86 74 if(received_coap_ptr->token_ptr)
GeofferyOmlette 2:88a30cc88a86 75 {
GeofferyOmlette 2:88a30cc88a86 76 pc.printf("Token included\r\n");
GeofferyOmlette 2:88a30cc88a86 77 if(obs_token_ptr)
GeofferyOmlette 2:88a30cc88a86 78 {
GeofferyOmlette 2:88a30cc88a86 79 free(obs_token_ptr);
GeofferyOmlette 2:88a30cc88a86 80 obs_token_ptr = 0;
GeofferyOmlette 2:88a30cc88a86 81 }
GeofferyOmlette 2:88a30cc88a86 82 obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len);
GeofferyOmlette 2:88a30cc88a86 83 if(obs_token_ptr)
GeofferyOmlette 2:88a30cc88a86 84 {
GeofferyOmlette 2:88a30cc88a86 85 memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len);
GeofferyOmlette 2:88a30cc88a86 86 obs_token_len = received_coap_ptr->token_len;
GeofferyOmlette 2:88a30cc88a86 87 }
GeofferyOmlette 2:88a30cc88a86 88 }
GeofferyOmlette 2:88a30cc88a86 89
GeofferyOmlette 2:88a30cc88a86 90 if(received_coap_ptr->options_list_ptr->observe)
GeofferyOmlette 2:88a30cc88a86 91 {
GeofferyOmlette 2:88a30cc88a86 92 coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s));
GeofferyOmlette 2:88a30cc88a86 93 memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
GeofferyOmlette 2:88a30cc88a86 94 coap_res_ptr->options_list_ptr->observe_ptr = &obs_number;
GeofferyOmlette 2:88a30cc88a86 95 coap_res_ptr->options_list_ptr->observe_len = 1;
GeofferyOmlette 2:88a30cc88a86 96 obs_number++;
GeofferyOmlette 2:88a30cc88a86 97 }
GeofferyOmlette 2:88a30cc88a86 98
GeofferyOmlette 2:88a30cc88a86 99 sn_nsdl_send_coap_message(address, coap_res_ptr);
GeofferyOmlette 2:88a30cc88a86 100
GeofferyOmlette 2:88a30cc88a86 101 coap_res_ptr->options_list_ptr->observe_ptr = 0;
GeofferyOmlette 2:88a30cc88a86 102 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
GeofferyOmlette 2:88a30cc88a86 103
GeofferyOmlette 2:88a30cc88a86 104 return 0;
GeofferyOmlette 2:88a30cc88a86 105 }
GeofferyOmlette 2:88a30cc88a86 106
GeofferyOmlette 2:88a30cc88a86 107 int create_nespresso_resource(sn_nsdl_resource_info_s *resource_ptr)
GeofferyOmlette 2:88a30cc88a86 108 {
GeofferyOmlette 2:88a30cc88a86 109 static Thread exec_thread(exec_call_thread);
GeofferyOmlette 2:88a30cc88a86 110
GeofferyOmlette 2:88a30cc88a86 111 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 112 return 0;
GeofferyOmlette 2:88a30cc88a86 113 }