Commit 4c49d8fb authored by Dave Thaler's avatar Dave Thaler

IOT-1072 Unfork libcoap: update forked headers

Make the .h files compatible between the forked libcoap and the official
libcoap, so that no breaking changes (only additions) are done to the
official libcoap, which other projects depend on the current
types/prototypes in.

Make any additions (that are beyond the official libcoap, but that are
already used by IoTivity code) to libcoap .h files follow the conventions of
libcoap, to make it more likely they'll be accepted back into the upstream
libcoap.  E.g., coap_transport_type -> coap_transport_t, etc.

Leaving WITH_UPSTREAM_LIBCOAP off by default on all OS's in this change.
Turning it on by default in certain OS's will be done as part of a
separate change.

Change-Id: I373ef818b8aeb7608c5e3511dc9880047946fbe0
Signed-off-by: default avatarDave Thaler <dthaler@microsoft.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/9373Tested-by: default avatarjenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: default avatarDavid Antler <david.a.antler@intel.com>
parent 7aa1b42a
......@@ -36,13 +36,17 @@ ca_transport = libcoap_env.get('TARGET_TRANSPORT')
with_tcp = libcoap_env.get('WITH_TCP')
with_upstream_libcoap = libcoap_env.get('WITH_UPSTREAM_LIBCOAP')
# Temporary LibCoAP URL is a fork of the original.
# Once a pull request is merged, change this back to the obgm original below.
libcoap_repo_url = 'https://github.com/dthaler/libcoap'
#libcoap_repo_url = 'https://github.com/obgm/libcoap'
######################################################################
# Download libCoAP
######################################################################
libcoap_branch = 'develop'
libcoap_dir = os.path.join(src_dir, 'extlibs', 'libcoap', 'libcoap')
libcoap_zip_file = os.path.join(src_dir, 'extlibs', 'libcoap', libcoap_branch + '.zip')
libcoap_repo_url = 'https://github.com/obgm/libcoap'
libcoap_url = libcoap_repo_url + '/archive/' + libcoap_branch + '.zip'
libcoap_checkout_command = 'git clone ' + libcoap_repo_url + '.git extlibs/libcoap/libcoap --branch ' + libcoap_branch
......@@ -134,6 +138,7 @@ if env.get('LOGGING') == '1':
######################################################################
with_upstream_libcoap = libcoap_env.get('WITH_UPSTREAM_LIBCOAP')
if with_upstream_libcoap == '1':
libcoap_env.PrependUnique(CPPPATH = ['libcoap/include/coap'])
libcoap_env.AppendUnique(CPPDEFINES = ['WITH_UPSTREAM_LIBCOAP'])
libcoap_src_root = os.path.join(libcoap_dir, 'src')
......@@ -151,11 +156,14 @@ if with_upstream_libcoap == '1':
'\@LIBCOAP_PACKAGE_VERSION\@' : libcoap_version
}
libcoap_env.Substfile(coap_h_pc_file, SUBST_DICT = pc_vars)
libcoap_src = glob.glob(os.path.join(libcoap_src_root, '*.c'))
libcoap_src.remove(os.path.join(libcoap_src_root, 'coap_io_lwip.c'))
else:
# For bring up purposes only, the forked version will live here.
libcoap_src_root = src_dir + '/resource/csdk/connectivity/lib/libcoap-4.1.1'
libcoap_src = glob.glob(os.path.join(libcoap_src_root, '*.c'))
libcoap_src = glob.glob(os.path.join(libcoap_src_root, '*.c'))
libcoap = libcoap_env.StaticLibrary('coap', libcoap_src, OBJPREFIX='libcoap_')
libcoap_env.InstallTarget([libcoap], 'coap')
......
......@@ -52,7 +52,7 @@
# if defined(__cplusplus)
# define INLINE_API inline
# else
# ifdef _WIN32
# ifdef _MSC_VER
# define INLINE_API static __inline
# else
# define INLINE_API static inline
......
......@@ -60,7 +60,7 @@ static const uint8_t PAYLOAD_MARKER = 1;
* @return generated pdu.
*/
coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info, const CAEndpoint_t *endpoint,
coap_list_t **optlist, coap_transport_type *transport);
coap_list_t **optlist, coap_transport_t *transport);
/**
* extracts request information from received pdu.
......@@ -102,7 +102,7 @@ CAResult_t CAGetErrorInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endp
*/
coap_pdu_t *CAGeneratePDUImpl(code_t code, const CAInfo_t *info,
const CAEndpoint_t *endpoint, coap_list_t *options,
coap_transport_type *transport);
coap_transport_t *transport);
/**
* parse the URI and creates the options.
......@@ -201,7 +201,8 @@ coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode,
* @param[in] endpoint endpoint information.
* @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
*/
CAResult_t CAGetTokenFromPDU(const coap_hdr_t *pdu_hdr, CAInfo_t *outInfo,
CAResult_t CAGetTokenFromPDU(const coap_hdr_transport_t *pdu_hdr,
CAInfo_t *outInfo,
const CAEndpoint_t *endpoint);
/**
......
......@@ -94,7 +94,7 @@ void CATCPPullData();
* @param[out] transport TCP Server address.
* @param[out] headerlen TCP Server port.
*/
void CAGetTCPHeaderDetails(unsigned char *recvBuffer, coap_transport_type *transport,
void CAGetTCPHeaderDetails(unsigned char *recvBuffer, coap_transport_t *transport,
size_t *headerlen);
/**
......
......@@ -20,17 +20,15 @@
#include "include/coap/async.h"
coap_async_state_t *
coap_register_async(coap_context_t *context, coap_address_t *peer, coap_pdu_t *request,
unsigned char flags, void *data)
{
coap_register_async(coap_context_t *context, coap_address_t *peer,
coap_pdu_t *request, unsigned char flags, void *data) {
coap_async_state_t *s;
coap_tid_t id;
coap_transaction_id(peer, request, &id);
LL_SEARCH_SCALAR(context->async_state, s, id, id);
if (s != NULL)
{
if (s != NULL) {
/* We must return NULL here as the caller must know that he is
* responsible for releasing @p data. */
debug("asynchronous state for transaction %d already registered\n", id);
......@@ -39,28 +37,26 @@ coap_register_async(coap_context_t *context, coap_address_t *peer, coap_pdu_t *r
/* store information for handling the asynchronous task */
s = (coap_async_state_t *) coap_malloc(sizeof(coap_async_state_t) +
request->hdr->coap_hdr_udp_t.token_length);
if (!s)
{
request->transport_hdr->udp.token_length);
if (!s) {
coap_log(LOG_CRIT, "coap_register_async: insufficient memory\n");
return NULL;
}
memset(s, 0, sizeof(coap_async_state_t) + request->hdr->coap_hdr_udp_t.token_length);
memset(s, 0, sizeof(coap_async_state_t) + request->transport_hdr->udp.token_length);
/* set COAP_ASYNC_CONFIRM according to request's type */
s->flags = flags & ~COAP_ASYNC_CONFIRM;
if (request->hdr->coap_hdr_udp_t.type == COAP_MESSAGE_CON)
if (request->transport_hdr->udp.type == COAP_MESSAGE_CON)
s->flags |= COAP_ASYNC_CONFIRM;
s->appdata = data;
memcpy(&s->peer, peer, sizeof(coap_address_t));
if (request->hdr->coap_hdr_udp_t.token_length)
{
s->tokenlen = request->hdr->coap_hdr_udp_t.token_length;
memcpy(s->token, request->hdr->coap_hdr_udp_t.token, request->hdr->coap_hdr_udp_t.token_length);
if (request->transport_hdr->udp.token_length) {
s->tokenlen = request->transport_hdr->udp.token_length;
memcpy(s->token, request->transport_hdr->udp.token, request->transport_hdr->udp.token_length);
}
memcpy(&s->id, &id, sizeof(coap_tid_t));
......@@ -73,15 +69,14 @@ coap_register_async(coap_context_t *context, coap_address_t *peer, coap_pdu_t *r
}
coap_async_state_t *
coap_find_async(coap_context_t *context, coap_tid_t id)
{
coap_find_async(coap_context_t *context, coap_tid_t id) {
coap_async_state_t *tmp;
LL_SEARCH_SCALAR(context->async_state, tmp, id, id);
return tmp;
}
int coap_remove_async(coap_context_t *context, coap_tid_t id, coap_async_state_t **s)
{
int
coap_remove_async(coap_context_t *context, coap_tid_t id, coap_async_state_t **s) {
coap_async_state_t *tmp = coap_find_async(context, id);
if (tmp)
......@@ -91,8 +86,8 @@ int coap_remove_async(coap_context_t *context, coap_tid_t id, coap_async_state_t
return tmp != NULL;
}
void coap_free_async(coap_async_state_t *s)
{
void
coap_free_async(coap_async_state_t *s) {
if (s && (s->flags & COAP_ASYNC_RELEASE_DATA) != 0)
coap_free(s->appdata);
coap_free(s);
......
......@@ -23,36 +23,34 @@
#define min(a,b) ((a) < (b) ? (a) : (b))
#ifndef WITHOUT_BLOCK
unsigned int coap_opt_block_num(const coap_opt_t *block_opt)
{
unsigned int
coap_opt_block_num(const coap_opt_t *block_opt) {
unsigned int num = 0;
unsigned short len;
len = coap_opt_length(block_opt);
if (len == 0)
{
if (len == 0) {
return 0;
}
if (len > 1)
{
num = coap_decode_var_bytes(COAP_OPT_VALUE(block_opt), COAP_OPT_LENGTH(block_opt) - 1);
if (len > 1) {
num = coap_decode_var_bytes(COAP_OPT_VALUE(block_opt),
COAP_OPT_LENGTH(block_opt) - 1);
}
return (num << 4) | ((*COAP_OPT_BLOCK_LAST(block_opt) & 0xF0) >> 4);
}
int coap_get_block(coap_pdu_t *pdu, unsigned short type, coap_block_t *block)
{
int
coap_get_block(coap_pdu_t *pdu, unsigned short type, coap_block_t *block) {
coap_opt_iterator_t opt_iter;
coap_opt_t *option;
assert(block);
memset(block, 0, sizeof(coap_block_t));
if (pdu && (option = coap_check_option(pdu, type, &opt_iter)))
{
if (pdu && (option = coap_check_option(pdu, type, &opt_iter)) != NULL) {
block->szx = COAP_OPT_BLOCK_SZX(option);
if (COAP_OPT_BLOCK_MORE(option))
block->m = 1;
......@@ -63,24 +61,22 @@ int coap_get_block(coap_pdu_t *pdu, unsigned short type, coap_block_t *block)
return 0;
}
int coap_write_block_opt(coap_block_t *block, unsigned short type, coap_pdu_t *pdu,
size_t data_length)
{
int
coap_write_block_opt(coap_block_t *block, unsigned short type,
coap_pdu_t *pdu, size_t data_length) {
size_t start, want, avail;
unsigned char buf[3];
assert(pdu);
/* Block2 */
if (type != COAP_OPTION_BLOCK2)
{
if (type != COAP_OPTION_BLOCK2) {
warn("coap_write_block_opt: skipped unknown option\n");
return -1;
}
start = block->num << (block->szx + 4);
if (data_length <= start)
{
if (data_length <= start) {
debug("illegal block requested\n");
return -2;
}
......@@ -89,30 +85,24 @@ int coap_write_block_opt(coap_block_t *block, unsigned short type, coap_pdu_t *p
want = 1 << (block->szx + 4);
/* check if entire block fits in message */
if (want <= avail)
{
if (want <= avail) {
block->m = want < data_length - start;
}
else
{
} else {
/* Sender has requested a block that is larger than the remaining
* space in pdu. This is ok if the remaining data fits into the pdu
* anyway. The block size needs to be adjusted only if there is more
* data left that cannot be delivered in this message. */
if (data_length - start <= avail)
{
if (data_length - start <= avail) {
/* it's the final block and everything fits in the message */
block->m = 0;
}
else
{
else {
unsigned char szx;
/* we need to decrease the block size */
if (avail < 16)
{ /* bad luck, this is the smallest block size */
if (avail < 16) { /* bad luck, this is the smallest block size */
debug("not enough space, even the smallest block does not fit");
return -3;
}
......@@ -126,21 +116,22 @@ int coap_write_block_opt(coap_block_t *block, unsigned short type, coap_pdu_t *p
/* to re-encode the block option */
coap_add_option(pdu, type,
coap_encode_var_bytes(buf, ((block->num << 4) | (block->m << 3) | block->szx)), buf,
coap_udp);
coap_encode_var_bytes(buf, ((block->num << 4) | (block->m << 3) | block->szx)), buf);
return 1;
}
int coap_add_block(coap_pdu_t *pdu, unsigned int len, const unsigned char *data,
unsigned int block_num, unsigned char block_szx)
{
int
coap_add_block(coap_pdu_t *pdu, unsigned int len, const unsigned char *data,
unsigned int block_num, unsigned char block_szx) {
size_t start;
start = block_num << (block_szx + 4);
if (len <= start)
return 0;
return coap_add_data(pdu, min(len - start, (unsigned int)(1 << (block_szx + 4))), data + start);
return coap_add_data(pdu,
min(len - start, (unsigned int)(1 << (block_szx + 4))),
data + start);
}
#endif /* WITHOUT_BLOCK */
......@@ -37,24 +37,25 @@
static coap_log_t maxlog = LOG_WARNING; /* default maximum log level */
coap_log_t coap_get_log_level()
{
coap_log_t
coap_get_log_level() {
return maxlog;
}
void coap_set_log_level(coap_log_t level)
{
void
coap_set_log_level(coap_log_t level) {
maxlog = level;
}
/* this array has the same order as the type log_t */
static char *loglevels[] =
{ "EMRG", "ALRT", "CRIT", "ERR", "WARN", "NOTE", "INFO", "DEBG" };
static char *loglevels[] = {
"EMRG", "ALRT", "CRIT", "ERR", "WARN", "NOTE", "INFO", "DEBG"
};
#ifdef HAVE_TIME_H
INLINE_API size_t print_timestamp(char *s, size_t len, coap_tick_t t)
{
INLINE_API size_t
print_timestamp(char *s, size_t len, coap_tick_t t) {
struct tm *tmp;
time_t now = clock_offset + (t / COAP_TICKS_PER_SECOND);
tmp = localtime(&now);
......@@ -64,8 +65,7 @@ INLINE_API size_t print_timestamp(char *s, size_t len, coap_tick_t t)
#else /* alternative implementation: just print the timestamp */
INLINE_API size_t
print_timestamp(char *s, size_t len, coap_tick_t t)
{
print_timestamp(char *s, size_t len, coap_tick_t t) {
#ifdef HAVE_SNPRINTF
return snprintf(s, len, "%u.%03u",
(unsigned int)(clock_offset + (t / COAP_TICKS_PER_SECOND)),
......@@ -90,8 +90,7 @@ print_timestamp(char *s, size_t len, coap_tick_t t)
* @return The length of @p s.
*/
INLINE_API size_t
strnlen(const char *s, size_t maxlen)
{
strnlen(const char *s, size_t maxlen) {
size_t n = 0;
while(*s++ && n < maxlen)
++n;
......@@ -99,9 +98,9 @@ strnlen(const char *s, size_t maxlen)
}
#endif /* HAVE_STRNLEN */
unsigned int print_readable(const unsigned char *data, unsigned int len, unsigned char *result,
unsigned int buflen, int encode_always)
{
unsigned int
print_readable(const unsigned char *data, unsigned int len, unsigned char *result,
unsigned int buflen, int encode_always) {
const unsigned char hex[] = "0123456789ABCDEF";
unsigned int cnt = 0;
assert(data || len == 0);
......@@ -109,19 +108,14 @@ unsigned int print_readable(const unsigned char *data, unsigned int len, unsigne
if (buflen == 0 || len == 0)
return 0;
while (len)
{
if (!encode_always && isprint(*data))
{
while (len) {
if (!encode_always && isprint(*data)) {
if (cnt == buflen)
break;
*result++ = *data;
++cnt;
}
else
{
if (cnt + 4 < buflen)
{
} else {
if (cnt + 4 < buflen) {
*result++ = '\\';
*result++ = 'x';
*result++ = hex[(*data & 0xf0) >> 4];
......@@ -144,8 +138,8 @@ unsigned int print_readable(const unsigned char *data, unsigned int len, unsigne
#define min(a,b) ((a) < (b) ? (a) : (b))
#endif
size_t coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t len)
{
size_t
coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t len) {
#if defined(HAVE_ARPA_INET_H) || defined(_WIN32)
const void *addrptr = NULL;
#if defined(__ANDROID__)
......@@ -157,8 +151,7 @@ size_t coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, si
#endif
unsigned char *p = buf;
switch (addr->addr.sa.sa_family)
{
switch (addr->addr.sa.sa_family) {
case AF_INET:
addrptr = &addr->addr.sin.sin_addr;
port = ntohs(addr->addr.sin.sin_port);
......@@ -178,18 +171,15 @@ size_t coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, si
return min(22, len);
}
if (inet_ntop(addr->addr.sa.sa_family, addrptr, (char *) p, len) == 0)
{
if (inet_ntop(addr->addr.sa.sa_family, addrptr, (char *) p, len) == 0) {
perror("coap_print_addr");
return 0;
}
p += strnlen((char *) p, len);
if (addr->addr.sa.sa_family == AF_INET6)
{
if (p < buf + len)
{
if (addr->addr.sa.sa_family == AF_INET6) {
if (p < buf + len) {
*p++ = ']';
}
else
......@@ -215,10 +205,8 @@ size_t coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, si
*p++ = '[';
for (i=0; i < 16; i += 2)
{
if (i)
{
for (i=0; i < 16; i += 2) {
if (i) {
*p++ = ':';
}
*p++ = hex[(addr->addr.u8[i] & 0xf0) >> 4];
......@@ -252,8 +240,8 @@ size_t coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, si
}
#ifndef WITH_CONTIKI
void coap_show_pdu(const coap_pdu_t *pdu)
{
void
coap_show_pdu(const coap_pdu_t *pdu) {
#ifndef WITH_TCP
unsigned char buf[COAP_MAX_PDU_SIZE]; /* need some space for output creation */
#else
......@@ -263,22 +251,18 @@ void coap_show_pdu(const coap_pdu_t *pdu)
coap_opt_iterator_t opt_iter;
coap_opt_t *option;
fprintf(COAP_DEBUG_FD, "v:%d t:%d tkl:%d c:%d id:%u", pdu->hdr->coap_hdr_udp_t.version,
pdu->hdr->coap_hdr_udp_t.type, pdu->hdr->coap_hdr_udp_t.token_length,
pdu->hdr->coap_hdr_udp_t.code, ntohs(pdu->hdr->coap_hdr_udp_t.id));
fprintf(COAP_DEBUG_FD, "v:%d t:%d tkl:%d c:%d id:%u", pdu->transport_hdr->udp.version,
pdu->transport_hdr->udp.type, pdu->transport_hdr->udp.token_length,
pdu->transport_hdr->udp.code, ntohs(pdu->transport_hdr->udp.id));
/* show options, if any */
coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL, coap_udp);
coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL);
while ((option = coap_option_next(&opt_iter)))
{
if (!have_options)
{
while ((option = coap_option_next(&opt_iter))) {
if (!have_options) {
have_options = 1;
fprintf(COAP_DEBUG_FD, " o: [");
}
else
{
} else {
fprintf(COAP_DEBUG_FD, ",");
}
......@@ -286,12 +270,9 @@ void coap_show_pdu(const coap_pdu_t *pdu)
|| opt_iter.type == COAP_OPTION_URI_HOST
|| opt_iter.type == COAP_OPTION_LOCATION_PATH
|| opt_iter.type == COAP_OPTION_LOCATION_QUERY
|| opt_iter.type == COAP_OPTION_URI_PATH || opt_iter.type == COAP_OPTION_URI_QUERY)
{
|| opt_iter.type == COAP_OPTION_URI_PATH || opt_iter.type == COAP_OPTION_URI_QUERY) {
encode = 0;
}
else
{
} else {
encode = 1;
}
......@@ -303,8 +284,7 @@ void coap_show_pdu(const coap_pdu_t *pdu)
if (have_options)
fprintf(COAP_DEBUG_FD, " ]");
if (pdu->data)
{
if (pdu->data) {
assert(pdu->data < (unsigned char *) pdu->hdr + pdu->length);
print_readable(pdu->data, (unsigned char *) pdu->hdr + pdu->length - pdu->data, buf,
sizeof(buf), 0);
......@@ -317,25 +297,22 @@ void coap_show_pdu(const coap_pdu_t *pdu)
#else /* WITH_CONTIKI */
void
coap_show_pdu(const coap_pdu_t *pdu)
{
coap_show_pdu(const coap_pdu_t *pdu) {
unsigned char buf[80]; /* need some space for output creation */
PRINTF("v:%d t:%d oc:%d c:%d id:%u",
pdu->hdr->coap_hdr_udp_t.version, pdu->hdr->coap_hdr_udp_t.type,
pdu->hdr->coap_hdr_udp_t.optcnt, pdu->hdr->coap_hdr_udp_t.code,
uip_ntohs(pdu->hdr->coap_hdr_udp_t.id));
pdu->transport_hdr->udp.version, pdu->transport_hdr->udp.type,
pdu->transport_hdr->udp.optcnt, pdu->transport_hdr->udp.code,
uip_ntohs(pdu->transport_hdr->udp.id));
/* show options, if any */
if (pdu->hdr->coap_hdr_udp_t.optcnt)
{
if (pdu->transport_hdr->udp.optcnt) {
coap_opt_iterator_t opt_iter;
coap_opt_t *option;
coap_option_iterator_init((coap_pdu_t *)pdu, &opt_iter, COAP_OPT_ALL, coap_udp);
coap_option_iterator_init((coap_pdu_t *)pdu, &opt_iter, COAP_OPT_ALL);
PRINTF(" o:");
while ((option = coap_option_next(&opt_iter)))
{
while ((option = coap_option_next(&opt_iter))) {
if (print_readable(COAP_OPT_VALUE(option),
COAP_OPT_LENGTH(option),
......@@ -344,8 +321,7 @@ coap_show_pdu(const coap_pdu_t *pdu)
}
}
if (pdu->data < (unsigned char *)pdu->hdr + pdu->length)
{
if (pdu->data < (unsigned char *)pdu->hdr + pdu->length) {
print_readable(pdu->data,
(unsigned char *)pdu->hdr + pdu->length - pdu->data,
buf, sizeof(buf), 0 );
......@@ -358,16 +334,16 @@ coap_show_pdu(const coap_pdu_t *pdu)
#endif /* NDEBUG */
#ifdef WITH_ARDUINO
void coap_log_impl(coap_log_t level, const char *format, ...)
{
void
coap_log_impl(coap_log_t level, const char *format, ...) {
//TODO: Implement logging functionalities for Arduino
}
#endif
#ifndef WITH_ARDUINO
#ifndef WITH_CONTIKI
void coap_log_impl(coap_log_t level, const char *format, ...)
{
void
coap_log_impl(coap_log_t level, const char *format, ...) {
char timebuf[32];
coap_tick_t now;
va_list ap;
......@@ -392,8 +368,7 @@ void coap_log_impl(coap_log_t level, const char *format, ...)
}
#else /* WITH_CONTIKI */
void
coap_log_impl(coap_log_t level, const char *format, ...)
{
coap_log_impl(coap_log_t level, const char *format, ...) {
char timebuf[32];
coap_tick_t now;
va_list ap;
......
......@@ -82,7 +82,7 @@ size_t coap_opt_size(const coap_opt_t *opt);
* @param pdu The PDU containing the options.
* @return A pointer to the first option if available, or @c NULL otherwise.
*/
coap_opt_t *options_start(coap_pdu_t *pdu, coap_transport_type transport);
coap_opt_t *options_start(coap_pdu_t *