LPC1768 hell world with LWM2M
Dependencies: Beep C12832_lcd EthernetInterface LM75B MMA7660 mbed-rtos mbed nsdl_lib
resources/temperature.cpp@0:9101343a70cd, 2014-07-22 (annotated)
- Committer:
- michaeljkoster
- Date:
- Tue Jul 22 23:58:23 2014 +0000
- Revision:
- 0:9101343a70cd
Demo with lwm2m
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
michaeljkoster | 0:9101343a70cd | 1 | // Temperature resource implementation |
michaeljkoster | 0:9101343a70cd | 2 | |
michaeljkoster | 0:9101343a70cd | 3 | #include "mbed.h" |
michaeljkoster | 0:9101343a70cd | 4 | #include "rtos.h" |
michaeljkoster | 0:9101343a70cd | 5 | #include "LM75B.h" |
michaeljkoster | 0:9101343a70cd | 6 | #include "nsdl_support.h" |
michaeljkoster | 0:9101343a70cd | 7 | #include "temperature.h" |
michaeljkoster | 0:9101343a70cd | 8 | |
michaeljkoster | 0:9101343a70cd | 9 | #define TEMP_RES_ID "sen/temp" |
michaeljkoster | 0:9101343a70cd | 10 | |
michaeljkoster | 0:9101343a70cd | 11 | static LM75B tmp(p28,p27); |
michaeljkoster | 0:9101343a70cd | 12 | /* stored data for observable resource */ |
michaeljkoster | 0:9101343a70cd | 13 | static uint8_t obs_number = 0; |
michaeljkoster | 0:9101343a70cd | 14 | static uint8_t *obs_token_ptr = NULL; |
michaeljkoster | 0:9101343a70cd | 15 | static uint8_t obs_token_len = 0; |
michaeljkoster | 0:9101343a70cd | 16 | static char temp_val[5]; |
michaeljkoster | 0:9101343a70cd | 17 | extern Serial pc; |
michaeljkoster | 0:9101343a70cd | 18 | |
michaeljkoster | 0:9101343a70cd | 19 | /* Thread for calling libNsdl exec function (cleanup, resendings etc..) */ |
michaeljkoster | 0:9101343a70cd | 20 | /* Node updates temperature every 10 seconds. Notification sending is done here. */ |
michaeljkoster | 0:9101343a70cd | 21 | static void exec_call_thread(void const *args) |
michaeljkoster | 0:9101343a70cd | 22 | { |
michaeljkoster | 0:9101343a70cd | 23 | int32_t time = 0; |
michaeljkoster | 0:9101343a70cd | 24 | while (true) |
michaeljkoster | 0:9101343a70cd | 25 | { |
michaeljkoster | 0:9101343a70cd | 26 | wait(1); |
michaeljkoster | 0:9101343a70cd | 27 | time++; |
michaeljkoster | 0:9101343a70cd | 28 | sn_nsdl_exec(time); |
michaeljkoster | 0:9101343a70cd | 29 | if((!(time % 10)) && obs_number != 0 && obs_token_ptr != NULL) |
michaeljkoster | 0:9101343a70cd | 30 | { |
michaeljkoster | 0:9101343a70cd | 31 | obs_number++; |
michaeljkoster | 0:9101343a70cd | 32 | sprintf(temp_val,"%2.2f" ,tmp.read()); |
michaeljkoster | 0:9101343a70cd | 33 | if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)temp_val, 5, &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) |
michaeljkoster | 0:9101343a70cd | 34 | pc.printf("Observation sending failed\r\n"); |
michaeljkoster | 0:9101343a70cd | 35 | else |
michaeljkoster | 0:9101343a70cd | 36 | pc.printf("Observation\r\n"); |
michaeljkoster | 0:9101343a70cd | 37 | } |
michaeljkoster | 0:9101343a70cd | 38 | } |
michaeljkoster | 0:9101343a70cd | 39 | } |
michaeljkoster | 0:9101343a70cd | 40 | |
michaeljkoster | 0:9101343a70cd | 41 | /* Only GET method allowed */ |
michaeljkoster | 0:9101343a70cd | 42 | /* Observable resource */ |
michaeljkoster | 0:9101343a70cd | 43 | static uint8_t temp_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) |
michaeljkoster | 0:9101343a70cd | 44 | { |
michaeljkoster | 0:9101343a70cd | 45 | sprintf(temp_val,"%2.2f" ,tmp.read()); |
michaeljkoster | 0:9101343a70cd | 46 | sn_coap_hdr_s *coap_res_ptr = 0; |
michaeljkoster | 0:9101343a70cd | 47 | |
michaeljkoster | 0:9101343a70cd | 48 | pc.printf("temp callback\r\n"); |
michaeljkoster | 0:9101343a70cd | 49 | coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); |
michaeljkoster | 0:9101343a70cd | 50 | |
michaeljkoster | 0:9101343a70cd | 51 | coap_res_ptr->payload_len = 5; |
michaeljkoster | 0:9101343a70cd | 52 | coap_res_ptr->payload_ptr = (uint8_t*)temp_val; |
michaeljkoster | 0:9101343a70cd | 53 | |
michaeljkoster | 0:9101343a70cd | 54 | if(received_coap_ptr->token_ptr) |
michaeljkoster | 0:9101343a70cd | 55 | { |
michaeljkoster | 0:9101343a70cd | 56 | pc.printf("Token included\r\n"); |
michaeljkoster | 0:9101343a70cd | 57 | if(obs_token_ptr) |
michaeljkoster | 0:9101343a70cd | 58 | { |
michaeljkoster | 0:9101343a70cd | 59 | free(obs_token_ptr); |
michaeljkoster | 0:9101343a70cd | 60 | obs_token_ptr = 0; |
michaeljkoster | 0:9101343a70cd | 61 | } |
michaeljkoster | 0:9101343a70cd | 62 | obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len); |
michaeljkoster | 0:9101343a70cd | 63 | if(obs_token_ptr) |
michaeljkoster | 0:9101343a70cd | 64 | { |
michaeljkoster | 0:9101343a70cd | 65 | memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len); |
michaeljkoster | 0:9101343a70cd | 66 | obs_token_len = received_coap_ptr->token_len; |
michaeljkoster | 0:9101343a70cd | 67 | } |
michaeljkoster | 0:9101343a70cd | 68 | } |
michaeljkoster | 0:9101343a70cd | 69 | |
michaeljkoster | 0:9101343a70cd | 70 | if(received_coap_ptr->options_list_ptr->observe) |
michaeljkoster | 0:9101343a70cd | 71 | { |
michaeljkoster | 0:9101343a70cd | 72 | coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); |
michaeljkoster | 0:9101343a70cd | 73 | memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); |
michaeljkoster | 0:9101343a70cd | 74 | coap_res_ptr->options_list_ptr->observe_ptr = &obs_number; |
michaeljkoster | 0:9101343a70cd | 75 | coap_res_ptr->options_list_ptr->observe_len = 1; |
michaeljkoster | 0:9101343a70cd | 76 | obs_number++; |
michaeljkoster | 0:9101343a70cd | 77 | } |
michaeljkoster | 0:9101343a70cd | 78 | |
michaeljkoster | 0:9101343a70cd | 79 | sn_nsdl_send_coap_message(address, coap_res_ptr); |
michaeljkoster | 0:9101343a70cd | 80 | |
michaeljkoster | 0:9101343a70cd | 81 | coap_res_ptr->options_list_ptr->observe_ptr = 0; |
michaeljkoster | 0:9101343a70cd | 82 | sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); |
michaeljkoster | 0:9101343a70cd | 83 | return 0; |
michaeljkoster | 0:9101343a70cd | 84 | } |
michaeljkoster | 0:9101343a70cd | 85 | |
michaeljkoster | 0:9101343a70cd | 86 | int create_temperature_resource(sn_nsdl_resource_info_s *resource_ptr) |
michaeljkoster | 0:9101343a70cd | 87 | { |
michaeljkoster | 0:9101343a70cd | 88 | static Thread exec_thread(exec_call_thread); |
michaeljkoster | 0:9101343a70cd | 89 | |
michaeljkoster | 0:9101343a70cd | 90 | nsdl_create_dynamic_resource(resource_ptr, sizeof(TEMP_RES_ID)-1, (uint8_t*)TEMP_RES_ID, 0, 0, 1, &temp_resource_cb, SN_GRS_GET_ALLOWED); |
michaeljkoster | 0:9101343a70cd | 91 | return 0; |
michaeljkoster | 0:9101343a70cd | 92 | } |