*** stock_iot-lab_M3/openwsn/04-TRAN/opencoap.c Thu Apr 24 11:01:36 2014 --- riot-openwsn-wip/openwsn/04-TRAN/opencoap.c Thu Apr 24 16:55:54 2014 *************** *** 9,36 **** #include "opentimers.h" #include "scheduler.h" ! //=========================== variables ======================================= ! // general variable to the CoAP core ! typedef struct { ! coap_resource_desc_t* resources; ! bool busySending; ! uint8_t delayCounter; ! uint16_t messageID; ! opentimer_id_t timerId; ! } opencoap_vars_t; opencoap_vars_t opencoap_vars; //=========================== prototype ======================================= ! void icmpv6coap_timer_cb(); //=========================== public ========================================== //===== from stack ! void opencoap_init() { // initialize the resource linked list opencoap_vars.resources = NULL; --- 9,30 ---- #include "opentimers.h" #include "scheduler.h" ! #include "thread.h" ! //=========================== variables ======================================= opencoap_vars_t opencoap_vars; + //static char openwsn_coap_stack[KERNEL_CONF_STACKSIZE_MAIN]; //=========================== prototype ======================================= ! void icmpv6coap_timer_cb(void); //=========================== public ========================================== //===== from stack ! void opencoap_init(void) { // initialize the resource linked list opencoap_vars.resources = NULL; *************** *** 52,58 **** coap_option_t last_option; coap_resource_desc_t* temp_desc; bool found; ! error_t outcome; // local variables passed to the handlers (with msg) coap_header_iht coap_header; coap_option_iht coap_options[MAX_COAP_OPTIONS]; --- 46,52 ---- coap_option_t last_option; coap_resource_desc_t* temp_desc; bool found; ! owerror_t outcome = 0; // local variables passed to the handlers (with msg) coap_header_iht coap_header; coap_option_iht coap_options[MAX_COAP_OPTIONS]; *************** *** 66,83 **** index = 0; coap_header.Ver = (msg->payload[index] & 0xc0) >> 6; coap_header.T = (coap_type_t)((msg->payload[index] & 0x30) >> 4); ! coap_header.OC = (msg->payload[index] & 0x0f); index++; coap_header.Code = (coap_code_t)(msg->payload[index]); index++; coap_header.messageID = msg->payload[index]*256+msg->payload[index+1]; index+=2; // reject unsupported header ! if ( ! coap_header.Ver!=COAP_VERSION || ! coap_header.OC>MAX_COAP_OPTIONS ! ) { ! openserial_printError(COMPONENT_OPENCOAP,ERR_6LOWPAN_UNSUPPORTED, (errorparameter_t)0, (errorparameter_t)coap_header.Ver); openqueue_freePacketBuffer(msg); --- 60,81 ---- index = 0; coap_header.Ver = (msg->payload[index] & 0xc0) >> 6; coap_header.T = (coap_type_t)((msg->payload[index] & 0x30) >> 4); ! coap_header.TKL = (msg->payload[index] & 0x0f); index++; coap_header.Code = (coap_code_t)(msg->payload[index]); index++; coap_header.messageID = msg->payload[index]*256+msg->payload[index+1]; index+=2; + + //poipoi xv. TKL tells us the length of the token. If we want to support tokens longer + //than one token needs to be converted to an array and memcopy here for the length of TKL + coap_header.token = (msg->payload[index]); + index+=coap_header.TKL; + + // reject unsupported header ! if (coap_header.Ver!=COAP_VERSION || coap_header.TKL>8) { ! openserial_printError(COMPONENT_OPENCOAP,ERR_WRONG_TRAN_PROTOCOL, (errorparameter_t)0, (errorparameter_t)coap_header.Ver); openqueue_freePacketBuffer(msg); *************** *** 89,102 **** } // fill in the coap_options last_option = COAP_OPTION_NONE; ! for (i=0;ipayload[index] & 0xf0) >> 4)); last_option = coap_options[i].type; coap_options[i].length = (msg->payload[index] & 0x0f); index++; coap_options[i].pValue = &(msg->payload[index]); ! index += coap_options[i].length; } // remove the CoAP header+options packetfunctions_tossHeader(msg,index); --- 87,106 ---- } // fill in the coap_options last_option = COAP_OPTION_NONE; ! for (i=0;ipayload[index]==0xFF){ ! //found the payload spacer, options are already parsed. ! index++; //skip it and break. ! break; ! } coap_options[i].type = (coap_option_t)((uint8_t)last_option+(uint8_t)((msg->payload[index] & 0xf0) >> 4)); last_option = coap_options[i].type; coap_options[i].length = (msg->payload[index] & 0x0f); index++; coap_options[i].pValue = &(msg->payload[index]); ! index += coap_options[i].length; //includes length as well } + // remove the CoAP header+options packetfunctions_tossHeader(msg,index); *************** *** 112,119 **** temp_desc = opencoap_vars.resources; while (found==FALSE) { if ( ! coap_options[0].type==COAP_OPTION_URIPATH && ! coap_options[1].type==COAP_OPTION_URIPATH && temp_desc->path0len>0 && temp_desc->path0val!=NULL && temp_desc->path1len>0 && --- 116,123 ---- temp_desc = opencoap_vars.resources; while (found==FALSE) { if ( ! coap_options[0].type==COAP_OPTION_NUM_URIPATH && ! coap_options[1].type==COAP_OPTION_NUM_URIPATH && temp_desc->path0len>0 && temp_desc->path0val!=NULL && temp_desc->path1len>0 && *************** *** 130,136 **** found = TRUE; }; } else if ( ! coap_options[0].type==COAP_OPTION_URIPATH && temp_desc->path0len>0 && temp_desc->path0val!=NULL ) { --- 134,140 ---- found = TRUE; }; } else if ( ! coap_options[0].type==COAP_OPTION_NUM_URIPATH && temp_desc->path0len>0 && temp_desc->path0val!=NULL ) { *************** *** 187,193 **** msg->payload = &(msg->packet[127]); msg->length = 0; // set the CoAP header ! coap_header.OC = 0; coap_header.Code = COAP_CODE_RESP_NOTFOUND; } --- 191,197 ---- msg->payload = &(msg->packet[127]); msg->length = 0; // set the CoAP header ! coap_header.TKL = 0; coap_header.Code = COAP_CODE_RESP_NOTFOUND; } *************** *** 196,202 **** msg->payload = &(msg->packet[127]); msg->length = 0; // set the CoAP header ! coap_header.OC = 0; coap_header.Code = COAP_CODE_RESP_METHODNOTALLOWED; } --- 200,206 ---- msg->payload = &(msg->packet[127]); msg->length = 0; // set the CoAP header ! coap_header.TKL = 0; coap_header.Code = COAP_CODE_RESP_METHODNOTALLOWED; } *************** *** 213,233 **** msg->l4_destination_port = msg->l4_sourcePortORicmpv6Type; msg->l4_sourcePortORicmpv6Type = temp_l4_destination_port; // fill in CoAP header ! packetfunctions_reserveHeaderSize(msg,4); msg->payload[0] = (COAP_VERSION << 6) | (COAP_TYPE_ACK << 4) | ! (coap_header.OC << 0); msg->payload[1] = coap_header.Code; msg->payload[2] = coap_header.messageID/256; msg->payload[3] = coap_header.messageID%256; if ((openudp_send(msg))==E_FAIL) { openqueue_freePacketBuffer(msg); } } ! void opencoap_sendDone(OpenQueueEntry_t* msg, error_t error) { coap_resource_desc_t* temp_resource; // take ownership over that packet --- 217,243 ---- msg->l4_destination_port = msg->l4_sourcePortORicmpv6Type; msg->l4_sourcePortORicmpv6Type = temp_l4_destination_port; + //set destination address as the current source. + msg->l3_destinationAdd.type = ADDR_128B; + memcpy(&msg->l3_destinationAdd.addr_128b[0],&msg->l3_sourceAdd.addr_128b[0],LENGTH_ADDR128b); + + // fill in CoAP header ! packetfunctions_reserveHeaderSize(msg,5); msg->payload[0] = (COAP_VERSION << 6) | (COAP_TYPE_ACK << 4) | ! (coap_header.TKL << 0); msg->payload[1] = coap_header.Code; msg->payload[2] = coap_header.messageID/256; msg->payload[3] = coap_header.messageID%256; + msg->payload[4] = coap_header.token; //this will be a memcopy for TKL size if ((openudp_send(msg))==E_FAIL) { openqueue_freePacketBuffer(msg); } } ! void opencoap_sendDone(OpenQueueEntry_t* msg, owerror_t error) { coap_resource_desc_t* temp_resource; // take ownership over that packet *************** *** 256,262 **** openqueue_freePacketBuffer(msg); } ! void timers_coap_fired() { //do something here if necessary } --- 266,272 ---- openqueue_freePacketBuffer(msg); } ! void timers_coap_fired(void) { //do something here if necessary } *************** *** 288,296 **** --- 298,316 ---- } } + /** + \brief Register a new CoAP resource. + + Registration consists in adding a new resource at the end of the linked list + of resources. + */ void opencoap_register(coap_resource_desc_t* desc) { coap_resource_desc_t* last_elem; + // since this CoAP resource will be at the end of the list, its next element + // should point to NULL, indicating the end of the linked list. + desc->next = NULL; + if (opencoap_vars.resources==NULL) { opencoap_vars.resources = desc; return; *************** *** 304,313 **** last_elem->next = desc; } ! error_t opencoap_send(OpenQueueEntry_t* msg, coap_type_t type, coap_code_t code, ! uint8_t numOptions, coap_resource_desc_t* descSender) { // change the global messageID opencoap_vars.messageID = openrandom_get16b(); --- 324,333 ---- last_elem->next = desc; } ! owerror_t opencoap_send(OpenQueueEntry_t* msg, coap_type_t type, coap_code_t code, ! uint8_t TKL, coap_resource_desc_t* descSender) { // change the global messageID opencoap_vars.messageID = openrandom_get16b(); *************** *** 316,335 **** // metadata msg->l4_sourcePortORicmpv6Type = WKP_UDP_COAP; // fill in CoAP header ! packetfunctions_reserveHeaderSize(msg,4); msg->payload[0] = (COAP_VERSION << 6) | (type << 4) | ! (numOptions << 0); msg->payload[1] = code; msg->payload[2] = (opencoap_vars.messageID>>8) & 0xff; msg->payload[3] = (opencoap_vars.messageID>>0) & 0xff; // record the messageID with this sender descSender->messageID = opencoap_vars.messageID; return openudp_send(msg); } //=========================== private ========================================= ! void icmpv6coap_timer_cb() { ! scheduler_push_task(timers_coap_fired,TASKPRIO_COAP); } \ No newline at end of file --- 336,364 ---- // metadata msg->l4_sourcePortORicmpv6Type = WKP_UDP_COAP; // fill in CoAP header ! packetfunctions_reserveHeaderSize(msg,5); msg->payload[0] = (COAP_VERSION << 6) | (type << 4) | ! (TKL << 0); msg->payload[1] = code; msg->payload[2] = (opencoap_vars.messageID>>8) & 0xff; msg->payload[3] = (opencoap_vars.messageID>>0) & 0xff; + //poipoi xv token needs to be defined by the app or here + #define TOKEN 123 + msg->payload[4] = TOKEN; //this will be a memcopy for TKL size + // record the messageID with this sender descSender->messageID = opencoap_vars.messageID; + descSender->token = TOKEN; + return openudp_send(msg); } //=========================== private ========================================= ! void icmpv6coap_timer_cb(void) { ! scheduler_push_task(timers_coap_fired,TASKPRIO_COAP); ! /*thread_create(openwsn_coap_stack, KERNEL_CONF_STACKSIZE_MAIN, ! PRIORITY_OPENWSN_COAP, CREATE_STACKTEST, ! timers_coap_fired, "timers coap fired");*/ } \ No newline at end of file