Red Hat Summit NanoService Demo for LPC1768 App Board using OMA Lightweight Objects

Dependencies:   Beep C12832_lcd EthernetInterface LM75B MMA7660 mbed-rtos mbed nsdl_lib

Fork of LWM2M_NanoService_Ethernet by MBED_DEMOS

Committer:
sam_grove
Date:
Mon Apr 28 22:49:53 2014 +0000
Revision:
29:7512729587cf
Parent:
14:5acd59fec679
Child:
31:7ce5bef2d369
firehouse starting point;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 2:7e489126fe7a 1 // Light resource implementation
bogdanm 2:7e489126fe7a 2
bogdanm 2:7e489126fe7a 3 #include "mbed.h"
bogdanm 2:7e489126fe7a 4 #include "nsdl_support.h"
bogdanm 2:7e489126fe7a 5 #include "light.h"
bogdanm 2:7e489126fe7a 6
zdshelby 8:f0ecb62bda47 7 #define LIGHT_RES_ID "311/0/5851"
sam_grove 29:7512729587cf 8 #define LIGHT_FLASH_ID "311/0/5850"
bogdanm 2:7e489126fe7a 9
bogdanm 2:7e489126fe7a 10 extern Serial pc;
bogdanm 2:7e489126fe7a 11 static PwmOut led2(LED2);
bogdanm 2:7e489126fe7a 12 static PwmOut led3(LED3);
zdshelby 14:5acd59fec679 13
sam_grove 29:7512729587cf 14 static DigitalOut out1(LED1);
sam_grove 29:7512729587cf 15 static Ticker flash;
sam_grove 29:7512729587cf 16
sam_grove 29:7512729587cf 17 static void flashHandler(void)
sam_grove 29:7512729587cf 18 {
sam_grove 29:7512729587cf 19 out1 = !out1;
sam_grove 29:7512729587cf 20 }
bogdanm 2:7e489126fe7a 21
bogdanm 2:7e489126fe7a 22 /* Only GET and PUT method allowed */
bogdanm 2:7e489126fe7a 23 static uint8_t light_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
bogdanm 2:7e489126fe7a 24 {
bogdanm 2:7e489126fe7a 25 sn_coap_hdr_s *coap_res_ptr = 0;
bogdanm 2:7e489126fe7a 26 static float led_dimm = 0;
bogdanm 2:7e489126fe7a 27 int led_state = 0;
bogdanm 2:7e489126fe7a 28 char led_dimm_temp[4];
bogdanm 2:7e489126fe7a 29
sam_grove 29:7512729587cf 30 pc.printf("light dimmer callback\r\n");
bogdanm 2:7e489126fe7a 31
sam_grove 29:7512729587cf 32 if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) {
bogdanm 2:7e489126fe7a 33 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
bogdanm 2:7e489126fe7a 34
bogdanm 2:7e489126fe7a 35 led_state = led_dimm * 100;
bogdanm 2:7e489126fe7a 36 sprintf(led_dimm_temp, "%d", led_state);
bogdanm 2:7e489126fe7a 37
bogdanm 2:7e489126fe7a 38 coap_res_ptr->payload_len = strlen(led_dimm_temp);
bogdanm 2:7e489126fe7a 39 coap_res_ptr->payload_ptr = (uint8_t*)led_dimm_temp;
bogdanm 2:7e489126fe7a 40 sn_nsdl_send_coap_message(address, coap_res_ptr);
sam_grove 29:7512729587cf 41 } else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) {
bogdanm 2:7e489126fe7a 42 memcpy(led_dimm_temp, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len);
bogdanm 2:7e489126fe7a 43
bogdanm 2:7e489126fe7a 44 led_dimm_temp[received_coap_ptr->payload_len] = '\0';
bogdanm 2:7e489126fe7a 45
bogdanm 2:7e489126fe7a 46 led_dimm = atof(led_dimm_temp);
bogdanm 2:7e489126fe7a 47 led_dimm = led_dimm/100;
bogdanm 2:7e489126fe7a 48
bogdanm 2:7e489126fe7a 49 led2.write(led_dimm);
bogdanm 2:7e489126fe7a 50 led3.write(led_dimm);
bogdanm 2:7e489126fe7a 51
bogdanm 2:7e489126fe7a 52 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED);
bogdanm 2:7e489126fe7a 53 sn_nsdl_send_coap_message(address, coap_res_ptr);
bogdanm 2:7e489126fe7a 54 }
bogdanm 2:7e489126fe7a 55
bogdanm 2:7e489126fe7a 56 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
bogdanm 2:7e489126fe7a 57 return 0;
bogdanm 2:7e489126fe7a 58 }
bogdanm 2:7e489126fe7a 59
sam_grove 29:7512729587cf 60
sam_grove 29:7512729587cf 61 /* Only GET and PUT method allowed */
sam_grove 29:7512729587cf 62 static uint8_t light_flash_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
sam_grove 29:7512729587cf 63 {
sam_grove 29:7512729587cf 64 sn_coap_hdr_s *coap_res_ptr = 0;
sam_grove 29:7512729587cf 65 static uint8_t flash_state = '0';
sam_grove 29:7512729587cf 66
sam_grove 29:7512729587cf 67 pc.printf("flash callback\r\n");
sam_grove 29:7512729587cf 68
sam_grove 29:7512729587cf 69 if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
sam_grove 29:7512729587cf 70 {
sam_grove 29:7512729587cf 71 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
sam_grove 29:7512729587cf 72
sam_grove 29:7512729587cf 73 coap_res_ptr->payload_len = 1;
sam_grove 29:7512729587cf 74 coap_res_ptr->payload_ptr = &flash_state;
sam_grove 29:7512729587cf 75 sn_nsdl_send_coap_message(address, coap_res_ptr);
sam_grove 29:7512729587cf 76 }
sam_grove 29:7512729587cf 77 else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
sam_grove 29:7512729587cf 78 {
sam_grove 29:7512729587cf 79 if(received_coap_ptr->payload_len)
sam_grove 29:7512729587cf 80 {
sam_grove 29:7512729587cf 81 if(*(received_coap_ptr->payload_ptr) == '1')
sam_grove 29:7512729587cf 82 {
sam_grove 29:7512729587cf 83 flash.attach(&flashHandler, 1.0f);
sam_grove 29:7512729587cf 84 flash_state = '1';
sam_grove 29:7512729587cf 85
sam_grove 29:7512729587cf 86 }
sam_grove 29:7512729587cf 87 else if(*(received_coap_ptr->payload_ptr) == '0')
sam_grove 29:7512729587cf 88 {
sam_grove 29:7512729587cf 89 out1 = 0;
sam_grove 29:7512729587cf 90 flash.detach();
sam_grove 29:7512729587cf 91 flash_state = '0';
sam_grove 29:7512729587cf 92 }
sam_grove 29:7512729587cf 93 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED);
sam_grove 29:7512729587cf 94 sn_nsdl_send_coap_message(address, coap_res_ptr);
sam_grove 29:7512729587cf 95 }
sam_grove 29:7512729587cf 96 }
sam_grove 29:7512729587cf 97
sam_grove 29:7512729587cf 98 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
sam_grove 29:7512729587cf 99
sam_grove 29:7512729587cf 100 return 0;
sam_grove 29:7512729587cf 101 }
sam_grove 29:7512729587cf 102
bogdanm 2:7e489126fe7a 103 int create_light_resource(sn_nsdl_resource_info_s *resource_ptr)
bogdanm 2:7e489126fe7a 104 {
bogdanm 2:7e489126fe7a 105 nsdl_create_dynamic_resource(resource_ptr, sizeof(LIGHT_RES_ID)-1, (uint8_t*)LIGHT_RES_ID, 0, 0, 0, &light_resource_cb, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED));
sam_grove 29:7512729587cf 106 nsdl_create_dynamic_resource(resource_ptr, sizeof(LIGHT_FLASH_ID)-1, (uint8_t*)LIGHT_FLASH_ID, 0, 0, 0, &light_flash_resource_cb, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED));
bogdanm 2:7e489126fe7a 107 return 0;
bogdanm 2:7e489126fe7a 108 }