CoAP Device Server Client

Dependencies:   EthernetInterface mbed-rtos mbed nsdl_lib

Fork of COAPmbed by th.iotkit2.ch

Constrained Application Protocol (Coap) ist ein Software-Protokoll welches für Internet der Dinge Geräte zugeschnitten ist.

COAP ist auf den meisten Geräten, die UDP Unterstützen, lauffähig.

Ein COAP fähiges Gerät publiziert seine Sensoren und Aktoren in einem Resource Directory oder stellt selber ein solches zur Verfügung.

Mittels Resource Discovery können die vorhandenen Sensoren und Aktoren mit ihren Attributen abgefragt werden.

Zeile 29: Node Name aendern und folgende Adresse aufrufen: http://nsp.cloudapp.net:8083/, User/PW = demo

Committer:
terohoo
Date:
Tue Oct 15 09:17:29 2013 +0000
Revision:
1:e35d7f10999a
Parent:
0:2edbfea18d23
Child:
2:7e489126fe7a
Bug fixed + minor modifications

Who changed what in which revision?

UserRevisionLine numberNew contents of line
terohoo 0:2edbfea18d23 1 /* Define DEBUG for USB serial communication */
terohoo 0:2edbfea18d23 2 #define DEBUG
terohoo 0:2edbfea18d23 3
terohoo 0:2edbfea18d23 4 #include "mbed.h"
terohoo 0:2edbfea18d23 5 #include "EthernetInterface.h"
terohoo 0:2edbfea18d23 6 #include "C12832_lcd.h"
terohoo 0:2edbfea18d23 7 #include "LM75B.h"
terohoo 0:2edbfea18d23 8 #include "MMA7660.h"
terohoo 0:2edbfea18d23 9 #include "Beep.h"
terohoo 0:2edbfea18d23 10 /* Sensinode includes */
terohoo 0:2edbfea18d23 11 #include "sn_nsdl.h"
terohoo 0:2edbfea18d23 12 #include "sn_coap_header.h"
terohoo 0:2edbfea18d23 13 #include "sn_coap_protocol.h"
terohoo 0:2edbfea18d23 14 #include "sn_nsdl_lib.h"
terohoo 0:2edbfea18d23 15
terohoo 0:2edbfea18d23 16 #include "resource_generation_help.h"
terohoo 0:2edbfea18d23 17
terohoo 0:2edbfea18d23 18 /* Define this to enable DHCP, otherwise manual address configuration is used */
terohoo 0:2edbfea18d23 19 #define DHCP
terohoo 0:2edbfea18d23 20
terohoo 0:2edbfea18d23 21 /* Manual IP configurations, if DHCP not defined */
terohoo 0:2edbfea18d23 22 #define IP "10.45.0.206"
terohoo 0:2edbfea18d23 23 #define MASK "255.255.255.0"
terohoo 0:2edbfea18d23 24 #define GW "10.45.0.1"
terohoo 0:2edbfea18d23 25
terohoo 0:2edbfea18d23 26 /* Change this IP address to that of your NanoService Platform installation */
terohoo 1:e35d7f10999a 27 const char* NSP_ADDRESS = "217.140.101.40"; /* internal NSP*/
terohoo 0:2edbfea18d23 28 const int NSP_PORT = 5683;
terohoo 0:2edbfea18d23 29
terohoo 0:2edbfea18d23 30 uint8_t nsp_addr[4];
terohoo 0:2edbfea18d23 31
terohoo 0:2edbfea18d23 32 /* The number of seconds between NSP registration messages */
terohoo 0:2edbfea18d23 33 #define RD_UPDATE_PERIOD 60
terohoo 1:e35d7f10999a 34 static uint8_t res_gps_val[] = {"52.182382,0.178849"};
terohoo 0:2edbfea18d23 35
terohoo 1:e35d7f10999a 36 static uint8_t ep_type[] = {"mbed_device"};
terohoo 0:2edbfea18d23 37 static uint8_t lifetime_ptr[] = {"1200"};
terohoo 0:2edbfea18d23 38
terohoo 0:2edbfea18d23 39 /* stored data for observable resource */
terohoo 0:2edbfea18d23 40 uint8_t obs_number = 0;
terohoo 0:2edbfea18d23 41 uint8_t *obs_token_ptr = 0;
terohoo 0:2edbfea18d23 42 uint8_t obs_token_len = 0;
terohoo 0:2edbfea18d23 43 char temp_val[5];
terohoo 0:2edbfea18d23 44
terohoo 0:2edbfea18d23 45 /* App board related stuff */
terohoo 0:2edbfea18d23 46 Beep buzzer(p26);
terohoo 0:2edbfea18d23 47 //PwmOut led1(LED1);
terohoo 0:2edbfea18d23 48 PwmOut led2(LED2);
terohoo 0:2edbfea18d23 49 PwmOut led3(LED3);
terohoo 0:2edbfea18d23 50 //PwmOut led4(LED4);
terohoo 0:2edbfea18d23 51 #ifdef DEBUG
terohoo 0:2edbfea18d23 52 Serial pc(USBTX, USBRX); // tx, rx
terohoo 0:2edbfea18d23 53 #endif
terohoo 0:2edbfea18d23 54 C12832_LCD lcd;
terohoo 0:2edbfea18d23 55 LM75B tmp(p28,p27);
terohoo 0:2edbfea18d23 56 MMA7660 MMA(p28, p27);
terohoo 0:2edbfea18d23 57
terohoo 0:2edbfea18d23 58 EthernetInterface eth;
terohoo 0:2edbfea18d23 59 UDPSocket server;
terohoo 0:2edbfea18d23 60 Endpoint nsp;
terohoo 0:2edbfea18d23 61 Endpoint from;
terohoo 0:2edbfea18d23 62
terohoo 0:2edbfea18d23 63 /* For creating uniq name for board.. */
terohoo 0:2edbfea18d23 64 char mbed_uid[33];
terohoo 0:2edbfea18d23 65 char endpoint_name[15] = "MyMbedexam";
terohoo 0:2edbfea18d23 66
terohoo 0:2edbfea18d23 67 extern "C"
terohoo 0:2edbfea18d23 68 {
terohoo 0:2edbfea18d23 69 uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr);
terohoo 0:2edbfea18d23 70 uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr);
terohoo 0:2edbfea18d23 71 void *own_alloc(uint16_t size);
terohoo 0:2edbfea18d23 72 void own_free(void* ptr_to_free);
terohoo 0:2edbfea18d23 73 static uint8_t temp_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto);
terohoo 0:2edbfea18d23 74 static uint8_t light_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto);
terohoo 0:2edbfea18d23 75 static uint8_t gps_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto);
terohoo 0:2edbfea18d23 76 static uint8_t relay_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto);
terohoo 0:2edbfea18d23 77 }
terohoo 0:2edbfea18d23 78
terohoo 0:2edbfea18d23 79 /* Thread for handling registration updates */
terohoo 0:2edbfea18d23 80 void registration_update_thread(void const *args)
terohoo 0:2edbfea18d23 81 {
terohoo 0:2edbfea18d23 82 sn_nsdl_ep_parameters_s *endpoint_ptr = 0;
terohoo 0:2edbfea18d23 83 while(true)
terohoo 0:2edbfea18d23 84 {
terohoo 0:2edbfea18d23 85 wait(RD_UPDATE_PERIOD);
terohoo 0:2edbfea18d23 86 INIT_REGISTER_NSDL_ENDPOINT(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
terohoo 0:2edbfea18d23 87 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
terohoo 0:2edbfea18d23 88 pc.printf("NSP re-registering failed\r\n");
terohoo 0:2edbfea18d23 89 else
terohoo 0:2edbfea18d23 90 pc.printf("NSP re-registering OK\r\n");
terohoo 0:2edbfea18d23 91 CLEAN_REGISTER_NSDL_ENDPOINT(endpoint_ptr);
terohoo 0:2edbfea18d23 92
terohoo 0:2edbfea18d23 93 }
terohoo 0:2edbfea18d23 94 }
terohoo 0:2edbfea18d23 95
terohoo 0:2edbfea18d23 96 /* Thread for calling libNsdl exec function (cleanup, resendings etc..) */
terohoo 0:2edbfea18d23 97 /* Node updates temperature every 10 seconds. Notification sending is done here. */
terohoo 0:2edbfea18d23 98 void exec_call_thread(void const *args)
terohoo 0:2edbfea18d23 99 {
terohoo 0:2edbfea18d23 100 int32_t time = 0;
terohoo 0:2edbfea18d23 101 while (true)
terohoo 0:2edbfea18d23 102 {
terohoo 0:2edbfea18d23 103 wait(1);
terohoo 0:2edbfea18d23 104 time++;
terohoo 0:2edbfea18d23 105 sn_nsdl_exec(time);
terohoo 0:2edbfea18d23 106 if((!(time % 10)) && obs_number != 0)
terohoo 0:2edbfea18d23 107 {
terohoo 0:2edbfea18d23 108 obs_number++;
terohoo 0:2edbfea18d23 109 sprintf(temp_val,"%2.2f" ,tmp.read());
terohoo 0:2edbfea18d23 110 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)
terohoo 0:2edbfea18d23 111 pc.printf("Observation sending failed\r\n");
terohoo 0:2edbfea18d23 112 else
terohoo 0:2edbfea18d23 113 pc.printf("Observation\r\n");
terohoo 0:2edbfea18d23 114 }
terohoo 0:2edbfea18d23 115 }
terohoo 0:2edbfea18d23 116 }
terohoo 0:2edbfea18d23 117
terohoo 0:2edbfea18d23 118 int main()
terohoo 0:2edbfea18d23 119 {
terohoo 0:2edbfea18d23 120
terohoo 0:2edbfea18d23 121 Thread registration_thread(registration_update_thread);
terohoo 0:2edbfea18d23 122 Thread exec_thread(exec_call_thread);
terohoo 0:2edbfea18d23 123
terohoo 0:2edbfea18d23 124 sn_nsdl_mem_s memory_cbs;
terohoo 0:2edbfea18d23 125 sn_nsdl_resource_info_s *resource_ptr = 0;
terohoo 0:2edbfea18d23 126 sn_nsdl_ep_parameters_s *endpoint_ptr = 0;
terohoo 0:2edbfea18d23 127 sn_nsdl_addr_s received_packet_address;
terohoo 0:2edbfea18d23 128 uint8_t received_address[4];
terohoo 0:2edbfea18d23 129
terohoo 0:2edbfea18d23 130 memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s));
terohoo 0:2edbfea18d23 131 received_packet_address.addr_ptr = received_address;
terohoo 0:2edbfea18d23 132
terohoo 0:2edbfea18d23 133 lcd.cls();
terohoo 0:2edbfea18d23 134 lcd.locate(0,0);
terohoo 0:2edbfea18d23 135 lcd.printf("mbed NanoService demo");
terohoo 0:2edbfea18d23 136 #ifdef DEBUG
terohoo 0:2edbfea18d23 137 pc.printf("mbed NanoService Example App 0.1\n\n\r");
terohoo 0:2edbfea18d23 138 #endif
terohoo 0:2edbfea18d23 139
terohoo 0:2edbfea18d23 140 /* Initialize network */
terohoo 0:2edbfea18d23 141 #ifdef DHCP
terohoo 0:2edbfea18d23 142 #ifdef DEBUG
terohoo 0:2edbfea18d23 143 pc.printf("DHCP in use\r\n");
terohoo 0:2edbfea18d23 144 #endif
terohoo 0:2edbfea18d23 145 eth.init();
terohoo 0:2edbfea18d23 146 #else
terohoo 0:2edbfea18d23 147 eth.init(IP, MASK, GW);
terohoo 0:2edbfea18d23 148 #endif
terohoo 0:2edbfea18d23 149 if(eth.connect(30000) == 0)
terohoo 0:2edbfea18d23 150 pc.printf("Connect OK\n\r");
terohoo 0:2edbfea18d23 151
terohoo 0:2edbfea18d23 152 mbed_interface_uid(mbed_uid);
terohoo 0:2edbfea18d23 153 mbed_uid[32] = '\0';
terohoo 0:2edbfea18d23 154 memcpy(&endpoint_name[10], &mbed_uid[27], 5);
terohoo 0:2edbfea18d23 155
terohoo 0:2edbfea18d23 156 lcd.locate(0,11);
terohoo 0:2edbfea18d23 157 lcd.printf("IP:%s", eth.getIPAddress());
terohoo 0:2edbfea18d23 158
terohoo 0:2edbfea18d23 159 #ifdef DEBUG
terohoo 0:2edbfea18d23 160 pc.printf("IP Address:%s \n\r", eth.getIPAddress());
terohoo 0:2edbfea18d23 161 #endif
terohoo 0:2edbfea18d23 162 server.init();
terohoo 0:2edbfea18d23 163 server.bind(NSP_PORT);
terohoo 0:2edbfea18d23 164
terohoo 0:2edbfea18d23 165 nsp.set_address(NSP_ADDRESS,NSP_PORT);
terohoo 0:2edbfea18d23 166
terohoo 0:2edbfea18d23 167 #ifdef DEBUG
terohoo 0:2edbfea18d23 168 pc.printf("name: %s\n\r", endpoint_name);
terohoo 0:2edbfea18d23 169 pc.printf("NSP=%s - port %d\r\n\n",NSP_ADDRESS, NSP_PORT);
terohoo 0:2edbfea18d23 170 #endif
terohoo 0:2edbfea18d23 171
terohoo 0:2edbfea18d23 172 lcd.locate(0,22);
terohoo 0:2edbfea18d23 173 lcd.printf("EP name:%s", endpoint_name);
terohoo 0:2edbfea18d23 174
terohoo 0:2edbfea18d23 175 memory_cbs.sn_nsdl_alloc = &own_alloc;
terohoo 0:2edbfea18d23 176 memory_cbs.sn_nsdl_free = &own_free;
terohoo 0:2edbfea18d23 177
terohoo 0:2edbfea18d23 178 /* Initialize libNsdl */
terohoo 0:2edbfea18d23 179 if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1)
terohoo 0:2edbfea18d23 180 pc.printf("libNsdl init failed\r\n");
terohoo 0:2edbfea18d23 181 else
terohoo 0:2edbfea18d23 182 pc.printf("libNsdl init done\r\n");
terohoo 0:2edbfea18d23 183
terohoo 0:2edbfea18d23 184 /* Set nsp address for library */
terohoo 0:2edbfea18d23 185 set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4);
terohoo 0:2edbfea18d23 186
terohoo 0:2edbfea18d23 187 #ifdef DEBUG
terohoo 0:2edbfea18d23 188 pc.printf("Creating resources\r\n");
terohoo 0:2edbfea18d23 189 #endif
terohoo 0:2edbfea18d23 190 /* Create resources */
terohoo 0:2edbfea18d23 191 resource_ptr = (sn_nsdl_resource_info_s*)own_alloc(sizeof(sn_nsdl_resource_info_s));
terohoo 0:2edbfea18d23 192 if(!resource_ptr)
terohoo 0:2edbfea18d23 193 return 0;
terohoo 0:2edbfea18d23 194 memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
terohoo 0:2edbfea18d23 195
terohoo 0:2edbfea18d23 196 resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)own_alloc(sizeof(sn_nsdl_resource_parameters_s));
terohoo 0:2edbfea18d23 197 if(!resource_ptr->resource_parameters_ptr)
terohoo 0:2edbfea18d23 198 {
terohoo 0:2edbfea18d23 199 own_free(resource_ptr);
terohoo 0:2edbfea18d23 200 return 0;
terohoo 0:2edbfea18d23 201 }
terohoo 0:2edbfea18d23 202 memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
terohoo 0:2edbfea18d23 203
terohoo 0:2edbfea18d23 204 /* Static resurces */
terohoo 1:e35d7f10999a 205 CREATE_STATIC_RESOURCE(resource_ptr, sizeof("dev/mfg")-1, (uint8_t*) "dev/mfg", 0, 0, (uint8_t*) "Sensinode", sizeof("Sensinode")-1);
terohoo 1:e35d7f10999a 206 CREATE_STATIC_RESOURCE(resource_ptr, sizeof("dev/mdl")-1, (uint8_t*) "dev/mdl", 0, 0, (uint8_t*) "NSDL-C mbed device", sizeof("NSDL-C mbed device")-1);
terohoo 0:2edbfea18d23 207
terohoo 1:e35d7f10999a 208 CREATE_DYNAMIC_RESOURCE(resource_ptr, sizeof("sen/temp")-1, (uint8_t*) "sen/temp", 0, 0, 1, &temp_resource_cb, SN_GRS_GET_ALLOWED);
terohoo 1:e35d7f10999a 209 CREATE_DYNAMIC_RESOURCE(resource_ptr, sizeof("lt/0/dim")-1, (uint8_t*) "lt/0/dim", 0, 0, 0, &light_resource_cb, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED));
terohoo 1:e35d7f10999a 210 CREATE_DYNAMIC_RESOURCE(resource_ptr, sizeof("gps/loc")-1, (uint8_t*) "gps/loc", 0, 0, 0, &gps_resource_cb, SN_GRS_GET_ALLOWED);
terohoo 1:e35d7f10999a 211 CREATE_DYNAMIC_RESOURCE(resource_ptr, sizeof("beep/0/on")-1, (uint8_t*) "beep/0/on", 0, 0, 0, &relay_resource_cb, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED));
terohoo 0:2edbfea18d23 212
terohoo 0:2edbfea18d23 213
terohoo 0:2edbfea18d23 214 /* Register with NSP */
terohoo 0:2edbfea18d23 215 INIT_REGISTER_NSDL_ENDPOINT(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
terohoo 0:2edbfea18d23 216
terohoo 0:2edbfea18d23 217 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
terohoo 0:2edbfea18d23 218 pc.printf("NSP registering failed\r\n");
terohoo 0:2edbfea18d23 219 else
terohoo 0:2edbfea18d23 220 pc.printf("NSP registering OK\r\n");
terohoo 0:2edbfea18d23 221
terohoo 0:2edbfea18d23 222 CLEAN_REGISTER_NSDL_ENDPOINT(endpoint_ptr);
terohoo 0:2edbfea18d23 223
terohoo 0:2edbfea18d23 224 own_free(resource_ptr->resource_parameters_ptr);
terohoo 0:2edbfea18d23 225 own_free(resource_ptr);
terohoo 0:2edbfea18d23 226
terohoo 0:2edbfea18d23 227 char buffer[1024];
terohoo 0:2edbfea18d23 228 while(1)
terohoo 0:2edbfea18d23 229 {
terohoo 0:2edbfea18d23 230 int n = server.receiveFrom(from, buffer, sizeof(buffer));
terohoo 0:2edbfea18d23 231 if (n < 0)
terohoo 0:2edbfea18d23 232 {
terohoo 0:2edbfea18d23 233 pc.printf("Socket error\n\r");
terohoo 0:2edbfea18d23 234 }
terohoo 0:2edbfea18d23 235 else
terohoo 0:2edbfea18d23 236 {
terohoo 0:2edbfea18d23 237 pc.printf("Received %d bytes\r\n", n);
terohoo 0:2edbfea18d23 238 sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address);
terohoo 0:2edbfea18d23 239 }
terohoo 0:2edbfea18d23 240 }
terohoo 0:2edbfea18d23 241 }
terohoo 0:2edbfea18d23 242 uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr)
terohoo 0:2edbfea18d23 243 {
terohoo 0:2edbfea18d23 244 pc.printf("TX callback!\n\rSending %d bytes:\r\n", data_len);
terohoo 0:2edbfea18d23 245
terohoo 0:2edbfea18d23 246 if(server.sendTo(nsp, (char*)data_ptr, data_len) != data_len)
terohoo 0:2edbfea18d23 247 pc.printf("sending failed\n\r");
terohoo 0:2edbfea18d23 248
terohoo 0:2edbfea18d23 249 return 1;
terohoo 0:2edbfea18d23 250 }
terohoo 0:2edbfea18d23 251
terohoo 0:2edbfea18d23 252
terohoo 0:2edbfea18d23 253 uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr)
terohoo 0:2edbfea18d23 254 {
terohoo 0:2edbfea18d23 255 pc.printf("RX callback!\r\n");
terohoo 0:2edbfea18d23 256
terohoo 0:2edbfea18d23 257 return 0;
terohoo 0:2edbfea18d23 258 }
terohoo 0:2edbfea18d23 259
terohoo 0:2edbfea18d23 260 void *own_alloc(uint16_t size)
terohoo 0:2edbfea18d23 261 {
terohoo 0:2edbfea18d23 262 return malloc(size);
terohoo 0:2edbfea18d23 263 }
terohoo 0:2edbfea18d23 264
terohoo 0:2edbfea18d23 265 void own_free(void* ptr_to_free)
terohoo 0:2edbfea18d23 266 {
terohoo 0:2edbfea18d23 267 free(ptr_to_free);
terohoo 0:2edbfea18d23 268 }
terohoo 0:2edbfea18d23 269
terohoo 0:2edbfea18d23 270 /* Only GET method allowed */
terohoo 0:2edbfea18d23 271 /* Observable resource */
terohoo 0:2edbfea18d23 272 static uint8_t temp_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
terohoo 0:2edbfea18d23 273 {
terohoo 0:2edbfea18d23 274 sprintf(temp_val,"%2.2f" ,tmp.read());
terohoo 0:2edbfea18d23 275 sn_coap_hdr_s *coap_res_ptr = 0;
terohoo 0:2edbfea18d23 276
terohoo 0:2edbfea18d23 277 pc.printf("temp callback\r\n");
terohoo 0:2edbfea18d23 278 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
terohoo 0:2edbfea18d23 279
terohoo 0:2edbfea18d23 280 coap_res_ptr->payload_len = 5;
terohoo 0:2edbfea18d23 281 coap_res_ptr->payload_ptr = (uint8_t*)temp_val;
terohoo 0:2edbfea18d23 282
terohoo 0:2edbfea18d23 283 if(received_coap_ptr->token_ptr)
terohoo 0:2edbfea18d23 284 {
terohoo 0:2edbfea18d23 285 pc.printf("Token included\r\n");
terohoo 0:2edbfea18d23 286 if(obs_token_ptr)
terohoo 0:2edbfea18d23 287 {
terohoo 0:2edbfea18d23 288 free(obs_token_ptr);
terohoo 0:2edbfea18d23 289 obs_token_ptr = 0;
terohoo 0:2edbfea18d23 290 }
terohoo 0:2edbfea18d23 291 obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len);
terohoo 0:2edbfea18d23 292 if(obs_token_ptr)
terohoo 0:2edbfea18d23 293 {
terohoo 0:2edbfea18d23 294 memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len);
terohoo 0:2edbfea18d23 295 obs_token_len = received_coap_ptr->token_len;
terohoo 0:2edbfea18d23 296 }
terohoo 0:2edbfea18d23 297 }
terohoo 0:2edbfea18d23 298
terohoo 0:2edbfea18d23 299 if(received_coap_ptr->options_list_ptr->observe)
terohoo 0:2edbfea18d23 300 {
terohoo 0:2edbfea18d23 301 coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s));
terohoo 0:2edbfea18d23 302 memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
terohoo 0:2edbfea18d23 303 coap_res_ptr->options_list_ptr->observe_ptr = &obs_number;
terohoo 0:2edbfea18d23 304 coap_res_ptr->options_list_ptr->observe_len = 1;
terohoo 0:2edbfea18d23 305 obs_number++;
terohoo 0:2edbfea18d23 306 }
terohoo 0:2edbfea18d23 307
terohoo 0:2edbfea18d23 308 sn_nsdl_send_coap_message(address, coap_res_ptr);
terohoo 0:2edbfea18d23 309
terohoo 1:e35d7f10999a 310 coap_res_ptr->options_list_ptr->observe_ptr = 0;
terohoo 1:e35d7f10999a 311 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
terohoo 0:2edbfea18d23 312 return 0;
terohoo 0:2edbfea18d23 313 }
terohoo 0:2edbfea18d23 314
terohoo 0:2edbfea18d23 315 /* Only GET and PUT method allowed */
terohoo 0:2edbfea18d23 316 static uint8_t light_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
terohoo 0:2edbfea18d23 317 {
terohoo 0:2edbfea18d23 318 sn_coap_hdr_s *coap_res_ptr = 0;
terohoo 0:2edbfea18d23 319 static float led_dimm = 0;
terohoo 0:2edbfea18d23 320 int led_state = 0;
terohoo 0:2edbfea18d23 321 char led_dimm_temp[4];
terohoo 0:2edbfea18d23 322
terohoo 0:2edbfea18d23 323 pc.printf("light callback\r\n");
terohoo 0:2edbfea18d23 324
terohoo 0:2edbfea18d23 325 if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
terohoo 0:2edbfea18d23 326 {
terohoo 0:2edbfea18d23 327 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
terohoo 0:2edbfea18d23 328
terohoo 0:2edbfea18d23 329 led_state = led_dimm * 100;
terohoo 0:2edbfea18d23 330 sprintf(led_dimm_temp, "%d", led_state);
terohoo 0:2edbfea18d23 331
terohoo 0:2edbfea18d23 332 coap_res_ptr->payload_len = strlen(led_dimm_temp);
terohoo 0:2edbfea18d23 333 coap_res_ptr->payload_ptr = (uint8_t*)led_dimm_temp;
terohoo 1:e35d7f10999a 334 sn_nsdl_send_coap_message(address, coap_res_ptr);
terohoo 0:2edbfea18d23 335 }
terohoo 1:e35d7f10999a 336 else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
terohoo 0:2edbfea18d23 337 {
terohoo 0:2edbfea18d23 338 memcpy(led_dimm_temp, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len);
terohoo 0:2edbfea18d23 339
terohoo 0:2edbfea18d23 340 led_dimm_temp[received_coap_ptr->payload_len] = '\0';
terohoo 0:2edbfea18d23 341
terohoo 0:2edbfea18d23 342 led_dimm = atof(led_dimm_temp);
terohoo 0:2edbfea18d23 343 led_dimm = led_dimm/100;
terohoo 0:2edbfea18d23 344
terohoo 0:2edbfea18d23 345 //led1.write(led_dimm);
terohoo 0:2edbfea18d23 346 led2.write(led_dimm);
terohoo 0:2edbfea18d23 347 led3.write(led_dimm);
terohoo 0:2edbfea18d23 348 //led4.write(led_dimm);
terohoo 0:2edbfea18d23 349
terohoo 0:2edbfea18d23 350 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED);
terohoo 1:e35d7f10999a 351 sn_nsdl_send_coap_message(address, coap_res_ptr);
terohoo 0:2edbfea18d23 352 }
terohoo 0:2edbfea18d23 353
terohoo 1:e35d7f10999a 354 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
terohoo 0:2edbfea18d23 355 return 0;
terohoo 0:2edbfea18d23 356 }
terohoo 0:2edbfea18d23 357
terohoo 0:2edbfea18d23 358 /* Only GET method allowed */
terohoo 0:2edbfea18d23 359 static uint8_t gps_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
terohoo 0:2edbfea18d23 360 {
terohoo 0:2edbfea18d23 361 sn_coap_hdr_s *coap_res_ptr = 0;
terohoo 0:2edbfea18d23 362 static float led_dimm = 0;
terohoo 0:2edbfea18d23 363 int led_state = 0;
terohoo 0:2edbfea18d23 364 char led_dimm_temp[4];
terohoo 0:2edbfea18d23 365
terohoo 0:2edbfea18d23 366 pc.printf("gps callback\r\n");
terohoo 0:2edbfea18d23 367
terohoo 0:2edbfea18d23 368 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
terohoo 0:2edbfea18d23 369
terohoo 0:2edbfea18d23 370 led_state = led_dimm * 100;
terohoo 0:2edbfea18d23 371 sprintf(led_dimm_temp, "%d", led_state);
terohoo 0:2edbfea18d23 372
terohoo 0:2edbfea18d23 373 coap_res_ptr->payload_len = sizeof(res_gps_val)-1;
terohoo 0:2edbfea18d23 374 coap_res_ptr->payload_ptr = (uint8_t*)res_gps_val;
terohoo 0:2edbfea18d23 375
terohoo 0:2edbfea18d23 376 sn_nsdl_send_coap_message(address, coap_res_ptr);
terohoo 0:2edbfea18d23 377
terohoo 1:e35d7f10999a 378 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
terohoo 1:e35d7f10999a 379
terohoo 0:2edbfea18d23 380 return 0;
terohoo 0:2edbfea18d23 381 }
terohoo 0:2edbfea18d23 382
terohoo 0:2edbfea18d23 383 /* Only GET and PUT method allowed */
terohoo 0:2edbfea18d23 384 static uint8_t relay_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
terohoo 0:2edbfea18d23 385 {
terohoo 0:2edbfea18d23 386 sn_coap_hdr_s *coap_res_ptr = 0;
terohoo 0:2edbfea18d23 387 static uint8_t relay_state = '0';
terohoo 0:2edbfea18d23 388
terohoo 0:2edbfea18d23 389 pc.printf("relay callback\r\n");
terohoo 0:2edbfea18d23 390
terohoo 0:2edbfea18d23 391 if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
terohoo 0:2edbfea18d23 392 {
terohoo 0:2edbfea18d23 393 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
terohoo 0:2edbfea18d23 394
terohoo 0:2edbfea18d23 395 coap_res_ptr->payload_len = 1;
terohoo 0:2edbfea18d23 396 coap_res_ptr->payload_ptr = &relay_state;
terohoo 1:e35d7f10999a 397 sn_nsdl_send_coap_message(address, coap_res_ptr);
terohoo 0:2edbfea18d23 398 }
terohoo 1:e35d7f10999a 399 else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
terohoo 0:2edbfea18d23 400 {
terohoo 0:2edbfea18d23 401 if(received_coap_ptr->payload_len)
terohoo 0:2edbfea18d23 402 {
terohoo 0:2edbfea18d23 403 if(*(received_coap_ptr->payload_ptr) == '1')
terohoo 0:2edbfea18d23 404 {
terohoo 0:2edbfea18d23 405 buzzer.beep(1000,0);
terohoo 0:2edbfea18d23 406 relay_state = '1';
terohoo 0:2edbfea18d23 407
terohoo 0:2edbfea18d23 408 }
terohoo 0:2edbfea18d23 409 else if(*(received_coap_ptr->payload_ptr) == '0')
terohoo 0:2edbfea18d23 410 {
terohoo 0:2edbfea18d23 411 buzzer.nobeep();
terohoo 0:2edbfea18d23 412 relay_state = '0';
terohoo 0:2edbfea18d23 413 }
terohoo 0:2edbfea18d23 414 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED);
terohoo 1:e35d7f10999a 415 sn_nsdl_send_coap_message(address, coap_res_ptr);
terohoo 0:2edbfea18d23 416 }
terohoo 0:2edbfea18d23 417 }
terohoo 0:2edbfea18d23 418
terohoo 1:e35d7f10999a 419 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
terohoo 0:2edbfea18d23 420
terohoo 0:2edbfea18d23 421 return 0;
terohoo 0:2edbfea18d23 422 }