Esempio n. 1
0
static coap_pdu_t *
make_pdu( unsigned int value ) {
  coap_pdu_t *pdu;
  unsigned char enc;
  static unsigned char buf[20];
  int len, ls;

  if (!(pdu = coap_pdu_init(0, 0, 0, COAP_DEFAULT_MTU)))
    return NULL;

  pdu->type = COAP_MESSAGE_NON;
  pdu->code = COAP_REQUEST_POST;
  pdu->tid = id++;

  enc = COAP_PSEUDOFP_ENCODE_8_4_DOWN(value,ls);

  len = sprintf((char *)buf, "%c%u", enc, COAP_PSEUDOFP_DECODE_8_4(enc));
  coap_add_data( pdu, len, buf );

  return pdu;
}
Esempio n. 2
0
void
notify(coap_context_t *context, coap_resource_t *res,
       coap_subscription_t *sub, unsigned int duration, int code) {
  coap_pdu_t *pdu;
  int ls, finished=0;
  unsigned char ct, d;
  unsigned int length;
#ifndef NDEBUG
  char addr[INET6_ADDRSTRLEN];
#endif

  if ( !context || !res || !sub || !(pdu = coap_new_pdu()) )
    return;

  pdu->hdr->type = COAP_MESSAGE_CON;
  pdu->hdr->id = rand();	/* use a random transaction id */
  pdu->hdr->code = code;

  /* FIXME: content-type and data (how about block?) */
  if (res->uri->na.length)
    coap_add_option (pdu, COAP_OPTION_URI_AUTHORITY,
		     res->uri->na.length,
		     res->uri->na.s );

  if (res->uri->path.length)
    coap_add_option (pdu, COAP_OPTION_URI_PATH,
		     res->uri->path.length,
		     res->uri->path.s);

  d = COAP_PSEUDOFP_ENCODE_8_4_DOWN(duration, ls);

  coap_add_option ( pdu, COAP_OPTION_SUBSCRIPTION, 1, &d );

  if (sub->token.length) {
    coap_add_option (pdu, COAP_OPTION_TOKEN,
		     sub->token.length,
		     sub->token.s);
  }

  if (res->uri->query.length)
    coap_add_option (pdu, COAP_OPTION_URI_QUERY,
		     res->uri->query.length,
		     res->uri->query.s );

  if (res->data) {
    length = (unsigned char *)pdu->hdr + COAP_MAX_PDU_SIZE - pdu->data;
    ct = res->mediatype;
    res->data(res->uri, &ct, 0, pdu->data, &length, &finished);
    pdu->length += length;

    /* TODO: add block option if not finished */
    /* TODO: add mediatype */
  }

#ifndef NDEBUG
  if ( inet_ntop(AF_INET6, &(sub->subscriber.sin6_addr), addr, INET6_ADDRSTRLEN) ) {
    debug("*** notify for %s to [%s]:%d\n", res->uri->path.s, addr, ntohs(sub->subscriber.sin6_port));
  }
#endif
  if ( pdu && coap_send_confirmed(context,
		  &sub->subscriber, pdu ) == COAP_INVALID_TID ) {
    debug("coap_check_resource_list: error sending notification\n");
    coap_delete_pdu(pdu);
  }
}