LED Demo for Tech Con 2014

Dependencies:   EthernetInterface PololuLedStripx mbed-rtos mbed nanoservice_client_1_12_X

Fork of LPC1768_LWM2M_Client by MBED_DEMOS

Committer:
michaeljkoster
Date:
Sat Sep 27 21:53:28 2014 +0000
Revision:
1:09a525977925
Parent:
0:9101343a70cd
Child:
2:4166c3e5b321
Client for Tech Con demo, resource stub

Who changed what in which revision?

UserRevisionLine numberNew contents of line
michaeljkoster 0:9101343a70cd 1 // Light resource implementation
michaeljkoster 0:9101343a70cd 2
michaeljkoster 0:9101343a70cd 3 #include "mbed.h"
michaeljkoster 0:9101343a70cd 4 #include "nsdl_support.h"
michaeljkoster 0:9101343a70cd 5 #include "light.h"
michaeljkoster 0:9101343a70cd 6
michaeljkoster 1:09a525977925 7 #define LIGHT_RES_ID "11100/0/5900"
michaeljkoster 0:9101343a70cd 8
michaeljkoster 0:9101343a70cd 9 extern Serial pc;
michaeljkoster 1:09a525977925 10 char led_color[] = {"00000000"};
michaeljkoster 0:9101343a70cd 11
michaeljkoster 0:9101343a70cd 12 /* Only GET and PUT method allowed */
michaeljkoster 0:9101343a70cd 13 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:9101343a70cd 14 {
michaeljkoster 0:9101343a70cd 15 sn_coap_hdr_s *coap_res_ptr = 0;
michaeljkoster 0:9101343a70cd 16
michaeljkoster 1:09a525977925 17 pc.printf("LED Strip callback\r\n");
michaeljkoster 0:9101343a70cd 18
michaeljkoster 0:9101343a70cd 19 if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
michaeljkoster 0:9101343a70cd 20 {
michaeljkoster 0:9101343a70cd 21 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
michaeljkoster 0:9101343a70cd 22
michaeljkoster 1:09a525977925 23 coap_res_ptr->payload_len = strlen(led_color);
michaeljkoster 1:09a525977925 24 coap_res_ptr->payload_ptr = (uint8_t*)led_color;
michaeljkoster 0:9101343a70cd 25 sn_nsdl_send_coap_message(address, coap_res_ptr);
michaeljkoster 0:9101343a70cd 26 }
michaeljkoster 0:9101343a70cd 27 else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
michaeljkoster 0:9101343a70cd 28 {
michaeljkoster 1:09a525977925 29 pc.printf("PUT: %d bytes\r\n", received_coap_ptr->payload_len);
michaeljkoster 1:09a525977925 30 if(received_coap_ptr->payload_len == 8)
michaeljkoster 1:09a525977925 31 {
michaeljkoster 1:09a525977925 32 memcpy(led_color, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len);
michaeljkoster 0:9101343a70cd 33
michaeljkoster 1:09a525977925 34 led_color[received_coap_ptr->payload_len] = '\0';
michaeljkoster 1:09a525977925 35 pc.printf("PUT: %s\r\n",led_color);
michaeljkoster 0:9101343a70cd 36
michaeljkoster 1:09a525977925 37 //call LED strup update function here
michaeljkoster 1:09a525977925 38
michaeljkoster 1:09a525977925 39 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED);
michaeljkoster 1:09a525977925 40 sn_nsdl_send_coap_message(address, coap_res_ptr);
michaeljkoster 1:09a525977925 41 }
michaeljkoster 0:9101343a70cd 42 }
michaeljkoster 0:9101343a70cd 43
michaeljkoster 0:9101343a70cd 44 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
michaeljkoster 0:9101343a70cd 45 return 0;
michaeljkoster 0:9101343a70cd 46 }
michaeljkoster 0:9101343a70cd 47
michaeljkoster 0:9101343a70cd 48 int create_light_resource(sn_nsdl_resource_info_s *resource_ptr)
michaeljkoster 0:9101343a70cd 49 {
michaeljkoster 0:9101343a70cd 50 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:9101343a70cd 51 return 0;
michaeljkoster 0:9101343a70cd 52 }