Example #1
0
void 
hnd_get_resource(coap_context_t  *ctx, struct coap_resource_t *resource, 
		 coap_address_t *peer, coap_pdu_t *request, str *token,
		 coap_pdu_t *response) {
  coap_key_t etag;
  unsigned char buf[2];
  coap_payload_t *test_payload;
  coap_block_t block;

  (void)ctx;
  (void)peer;
  (void)token;

  test_payload = coap_find_payload(resource->key);
  if (!test_payload) {
    response->hdr->code = COAP_RESPONSE_CODE(500);
    
    return;
  }

  response->hdr->code = COAP_RESPONSE_CODE(205);

  coap_add_option(response, COAP_OPTION_CONTENT_TYPE,
	  coap_encode_var_bytes(buf, test_payload->media_type), buf);

  /* add etag for the resource */
  if (test_payload->flags & REQUIRE_ETAG) {
    memset(etag, 0, sizeof(etag));
    coap_hash(test_payload->data, test_payload->length, etag);
    coap_add_option(response, COAP_OPTION_ETAG, sizeof(etag), etag);
  }
      
  if (request) {
    int res;

    if (coap_get_block(request, COAP_OPTION_BLOCK2, &block)) {
      res = coap_write_block_opt(&block, COAP_OPTION_BLOCK2, response,
				 test_payload->length);

      switch (res) {
      case -2:			/* illegal block */
	response->hdr->code = COAP_RESPONSE_CODE(400);
	goto error;
      case -1:			/* should really not happen */
	assert(0);
	/* fall through if assert is a no-op */
      case -3:			/* cannot handle request */
	response->hdr->code = COAP_RESPONSE_CODE(500);
	goto error;
      default:			/* everything is good */
	;
      }
      
      coap_add_block(response, test_payload->length, test_payload->data,
		     block.num, block.szx);
    } else {
      if (!coap_add_data(response, test_payload->length, test_payload->data)) {
	/* set initial block size, will be lowered by
	 * coap_write_block_opt) automatically */
	block.szx = 6;
	coap_write_block_opt(&block, COAP_OPTION_BLOCK2, response,
			     test_payload->length);
	
	coap_add_block(response, test_payload->length, test_payload->data,
		       block.num, block.szx);	
      }
    }    
  } else {		      /* this is a notification, block is 0 */
    /* FIXME: need to store block size with subscription */
  }
  
  return;

 error:
  coap_add_data(response, 
		strlen(coap_response_phrase(response->hdr->code)),
		(unsigned char *)coap_response_phrase(response->hdr->code));
}
void CoAPRDLookUpEPResource::handler_get(CoAPCallback &callback)
{
    std::string ep_result;
    
    find_ep_result(ep_result);

    if (ep_result.empty())
    {
    
        coap_pdu_t *response = (coap_pdu_t*)callback.response_;
        
        /* create response */
        response->hdr->code = COAP_RESPONSE_404;
    }
    else
    {

        unsigned char buf[3];
        std::string result;
        coap_block_t block_opt;
        
        coap_pdu_t *response = (coap_pdu_t*)callback.response_;
        
        /* create response */
        response->hdr->code = COAP_RESPONSE_CODE(205);
        
        coap_add_option(response, COAP_OPTION_CONTENT_TYPE,
                        coap_encode_var_bytes(buf, COAP_MEDIATYPE_APPLICATION_LINK_FORMAT), buf);
        
        coap_add_option(response, COAP_OPTION_MAXAGE,
                        coap_encode_var_bytes(buf, 0x2ffff), buf);
                        
        /* 1.if result more than 1024 byte, block wise transfer,
         * otherwise directly transfer
         * 2. if client set block2 option, response must be block-wise transferred
         */
        if (1 == coap_get_block((coap_pdu_t*)callback.request_, COAP_OPTION_BLOCK2, &block_opt))
        {
            ACE_DEBUG((LM_DEBUG, "send data by block from %d(size=%d)\n",block_opt.num,block_opt.szx));

            if (1 == send_data_by_block(response, &block_opt, (unsigned char*)ep_result.c_str(), ep_result.length()))
            {
                return;
            }
        }

        if (ep_result.length() > 1024 )
        {
            block_opt.szx = 6;
            block_opt.m = 1;
            block_opt.num = 0;

            ACE_DEBUG((LM_DEBUG, "send data by block from %d(size=%d)\n",block_opt.num,block_opt.szx));

            if (1 == send_data_by_block(response, &block_opt, (unsigned char*)ep_result.c_str(), ep_result.length()))
            {
                return;
            }            
        }

        /* at here, we send data directly*/
        {
        
            ACE_DEBUG((LM_DEBUG, "send data directly(len=%d)\n", ep_result.length()));
            
            coap_add_data(response, ep_result.length(), (unsigned char*)ep_result.c_str());
        }
    }
    
}