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:
Wed Oct 09 09:31:17 2013 +0000
Revision:
0:2edbfea18d23
Child:
1:e35d7f10999a
Adding nsdl example app

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