Arch Pro mbed Device Server demo exposes LEDs as LWM2M style object

Dependencies:   EthernetInterface mbed-rtos mbed nsdl_lib

Committer:
michaeljkoster
Date:
Sat Oct 11 06:14:44 2014 +0000
Revision:
0:f7f236605c98
Initial Drop for SCU Class Demo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
michaeljkoster 0:f7f236605c98 1 // Light resource implementation
michaeljkoster 0:f7f236605c98 2
michaeljkoster 0:f7f236605c98 3 #include "mbed.h"
michaeljkoster 0:f7f236605c98 4 #include "nsdl_support.h"
michaeljkoster 0:f7f236605c98 5 #include "light.h"
michaeljkoster 0:f7f236605c98 6
michaeljkoster 0:f7f236605c98 7 #define LIGHT_RES_ID "11100/0/5900"
michaeljkoster 0:f7f236605c98 8
michaeljkoster 0:f7f236605c98 9 extern Serial pc;
michaeljkoster 0:f7f236605c98 10 char leds[] = {"1111"}; //YGBR
michaeljkoster 0:f7f236605c98 11
michaeljkoster 0:f7f236605c98 12 DigitalOut grn(LED1);
michaeljkoster 0:f7f236605c98 13 DigitalOut red(LED2);
michaeljkoster 0:f7f236605c98 14 DigitalOut blu(LED3);
michaeljkoster 0:f7f236605c98 15 DigitalOut yel(LED4);
michaeljkoster 0:f7f236605c98 16
michaeljkoster 0:f7f236605c98 17 void set_leds(char *leds)
michaeljkoster 0:f7f236605c98 18 {
michaeljkoster 0:f7f236605c98 19 int leds_int ;
michaeljkoster 0:f7f236605c98 20
michaeljkoster 0:f7f236605c98 21 sscanf(leds, "%x", &leds_int);
michaeljkoster 0:f7f236605c98 22
michaeljkoster 0:f7f236605c98 23 grn = ~leds_int & 1;
michaeljkoster 0:f7f236605c98 24 red = ~leds_int >> 4 & 1;
michaeljkoster 0:f7f236605c98 25 blu = ~leds_int >> 8 & 1;
michaeljkoster 0:f7f236605c98 26 yel = ~leds_int >> 12 & 1;
michaeljkoster 0:f7f236605c98 27 }
michaeljkoster 0:f7f236605c98 28
michaeljkoster 0:f7f236605c98 29
michaeljkoster 0:f7f236605c98 30 /* Only GET and PUT method allowed */
michaeljkoster 0:f7f236605c98 31 static uint8_t light_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
michaeljkoster 0:f7f236605c98 32 {
michaeljkoster 0:f7f236605c98 33 sn_coap_hdr_s *coap_res_ptr = 0;
michaeljkoster 0:f7f236605c98 34
michaeljkoster 0:f7f236605c98 35 //pc.printf("LED Strip callback\r\n");
michaeljkoster 0:f7f236605c98 36
michaeljkoster 0:f7f236605c98 37 if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
michaeljkoster 0:f7f236605c98 38 {
michaeljkoster 0:f7f236605c98 39 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
michaeljkoster 0:f7f236605c98 40
michaeljkoster 0:f7f236605c98 41 coap_res_ptr->payload_len = strlen(leds);
michaeljkoster 0:f7f236605c98 42 coap_res_ptr->payload_ptr = (uint8_t*)leds;
michaeljkoster 0:f7f236605c98 43 sn_nsdl_send_coap_message(address, coap_res_ptr);
michaeljkoster 0:f7f236605c98 44 }
michaeljkoster 0:f7f236605c98 45 else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
michaeljkoster 0:f7f236605c98 46 {
michaeljkoster 0:f7f236605c98 47 //pc.printf("PUT: %d bytes\r\n", received_coap_ptr->payload_len);
michaeljkoster 0:f7f236605c98 48 if(received_coap_ptr->payload_len == 4)
michaeljkoster 0:f7f236605c98 49 {
michaeljkoster 0:f7f236605c98 50 memcpy(leds, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len);
michaeljkoster 0:f7f236605c98 51
michaeljkoster 0:f7f236605c98 52 leds[received_coap_ptr->payload_len] = '\0';
michaeljkoster 0:f7f236605c98 53 pc.printf("PUT: %s\r\n",leds);
michaeljkoster 0:f7f236605c98 54
michaeljkoster 0:f7f236605c98 55 //call LED strup update function here
michaeljkoster 0:f7f236605c98 56 set_leds(leds);
michaeljkoster 0:f7f236605c98 57
michaeljkoster 0:f7f236605c98 58 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED);
michaeljkoster 0:f7f236605c98 59 sn_nsdl_send_coap_message(address, coap_res_ptr);
michaeljkoster 0:f7f236605c98 60 }
michaeljkoster 0:f7f236605c98 61 }
michaeljkoster 0:f7f236605c98 62
michaeljkoster 0:f7f236605c98 63 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
michaeljkoster 0:f7f236605c98 64 return 0;
michaeljkoster 0:f7f236605c98 65 }
michaeljkoster 0:f7f236605c98 66
michaeljkoster 0:f7f236605c98 67 int create_light_resource(sn_nsdl_resource_info_s *resource_ptr)
michaeljkoster 0:f7f236605c98 68 {
michaeljkoster 0:f7f236605c98 69 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));
michaeljkoster 0:f7f236605c98 70 return 0;
michaeljkoster 0:f7f236605c98 71 }