Enable WASI feature, enhance security and add SGX sample (#142)

Change emcc to clang
Refine interpreter to improve perforamnce
This commit is contained in:
Weining
2019-11-20 21:16:36 +08:00
committed by wenyongh
parent 29c7c743e9
commit 27f246b5f3
159 changed files with 9543 additions and 3789 deletions

View File

@ -1,38 +0,0 @@
Contiki is licensed under the 3-clause BSD license. This license gives
everyone the right to use and distribute the code, either in binary or
source code format, as long as the copyright license is retained in
the source code.
The copyright for different parts of the code is held by different
people and organizations, but the code is licensed under the same type
of license. The license text is:
* Copyright (c) (Year), (Name of copyright holder)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -1,8 +0,0 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
COAP_ROOT=${ZEPHYR_BASE}/subsys/aee/Beihai/runtime/utils/coap
subdir-ccflags-y += -I${COAP_ROOT}/er-coap -I${COAP_ROOT}/extension
obj-y += er-coap/
obj-y += extension/

View File

@ -0,0 +1,30 @@
Copyright (c) (Year), (Name of copyright holder)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -1,4 +0,0 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
obj-y += er-coap.o

View File

@ -0,0 +1,192 @@
/*
* Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*/
/**
* \file
* Collection of constants specified in the CoAP standard.
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
/**
* \addtogroup coap
* @{
*/
#ifndef COAP_CONSTANTS_H_
#define COAP_CONSTANTS_H_
#define COAP_DEFAULT_PORT 5683
#define COAP_DEFAULT_SECURE_PORT 5684
#define COAP_DEFAULT_MAX_AGE 60
#define COAP_RESPONSE_TIMEOUT 3
#define COAP_RESPONSE_RANDOM_FACTOR 1.5
#define COAP_MAX_RETRANSMIT 4
#define COAP_HEADER_LEN 4 /* | version:0x03 type:0x0C tkl:0xF0 | code | mid:0x00FF | mid:0xFF00 | */
#define COAP_TOKEN_LEN 8 /* The maximum number of bytes for the Token */
#define COAP_ETAG_LEN 8 /* The maximum number of bytes for the ETag */
#define COAP_HEADER_VERSION_MASK 0xC0
#define COAP_HEADER_VERSION_POSITION 6
#define COAP_HEADER_TYPE_MASK 0x30
#define COAP_HEADER_TYPE_POSITION 4
#define COAP_HEADER_TOKEN_LEN_MASK 0x0F
#define COAP_HEADER_TOKEN_LEN_POSITION 0
#define COAP_HEADER_OPTION_DELTA_MASK 0xF0
#define COAP_HEADER_OPTION_SHORT_LENGTH_MASK 0x0F
/* CoAP message types */
typedef enum {
COAP_TYPE_CON, /* confirmables */
COAP_TYPE_NON, /* non-confirmables */
COAP_TYPE_ACK, /* acknowledgements */
COAP_TYPE_RST /* reset */
} coap_message_type_t;
/* CoAP request method codes */
typedef enum {
COAP_GET = 1,
COAP_POST,
COAP_PUT,
COAP_DELETE
} coap_method_t;
/* CoAP response codes */
typedef enum {
NO_ERROR = 0,
CREATED_2_01 = 65, /* CREATED */
DELETED_2_02 = 66, /* DELETED */
VALID_2_03 = 67, /* NOT_MODIFIED */
CHANGED_2_04 = 68, /* CHANGED */
CONTENT_2_05 = 69, /* OK */
CONTINUE_2_31 = 95, /* CONTINUE */
BAD_REQUEST_4_00 = 128, /* BAD_REQUEST */
UNAUTHORIZED_4_01 = 129, /* UNAUTHORIZED */
BAD_OPTION_4_02 = 130, /* BAD_OPTION */
FORBIDDEN_4_03 = 131, /* FORBIDDEN */
NOT_FOUND_4_04 = 132, /* NOT_FOUND */
METHOD_NOT_ALLOWED_4_05 = 133, /* METHOD_NOT_ALLOWED */
NOT_ACCEPTABLE_4_06 = 134, /* NOT_ACCEPTABLE */
PRECONDITION_FAILED_4_12 = 140, /* BAD_REQUEST */
REQUEST_ENTITY_TOO_LARGE_4_13 = 141, /* REQUEST_ENTITY_TOO_LARGE */
UNSUPPORTED_MEDIA_TYPE_4_15 = 143, /* UNSUPPORTED_MEDIA_TYPE */
INTERNAL_SERVER_ERROR_5_00 = 160, /* INTERNAL_SERVER_ERROR */
NOT_IMPLEMENTED_5_01 = 161, /* NOT_IMPLEMENTED */
BAD_GATEWAY_5_02 = 162, /* BAD_GATEWAY */
SERVICE_UNAVAILABLE_5_03 = 163, /* SERVICE_UNAVAILABLE */
GATEWAY_TIMEOUT_5_04 = 164, /* GATEWAY_TIMEOUT */
PROXYING_NOT_SUPPORTED_5_05 = 165, /* PROXYING_NOT_SUPPORTED */
/* Erbium errors */
MEMORY_ALLOCATION_ERROR = 192,
PACKET_SERIALIZATION_ERROR,
/* Erbium hooks */
MANUAL_RESPONSE,
PING_RESPONSE
} coap_status_t;
/* CoAP header option numbers */
typedef enum {
COAP_OPTION_IF_MATCH = 1, /* 0-8 B */
COAP_OPTION_URI_HOST = 3, /* 1-255 B */
COAP_OPTION_ETAG = 4, /* 1-8 B */
COAP_OPTION_IF_NONE_MATCH = 5, /* 0 B */
COAP_OPTION_OBSERVE = 6, /* 0-3 B */
COAP_OPTION_URI_PORT = 7, /* 0-2 B */
COAP_OPTION_LOCATION_PATH = 8, /* 0-255 B */
COAP_OPTION_URI_PATH = 11, /* 0-255 B */
COAP_OPTION_CONTENT_FORMAT = 12, /* 0-2 B */
COAP_OPTION_MAX_AGE = 14, /* 0-4 B */
COAP_OPTION_URI_QUERY = 15, /* 0-255 B */
COAP_OPTION_ACCEPT = 17, /* 0-2 B */
COAP_OPTION_LOCATION_QUERY = 20, /* 0-255 B */
COAP_OPTION_BLOCK2 = 23, /* 1-3 B */
COAP_OPTION_BLOCK1 = 27, /* 1-3 B */
COAP_OPTION_SIZE2 = 28, /* 0-4 B */
COAP_OPTION_PROXY_URI = 35, /* 1-1034 B */
COAP_OPTION_PROXY_SCHEME = 39, /* 1-255 B */
COAP_OPTION_SIZE1 = 60, /* 0-4 B */
} coap_option_t;
/* CoAP Content-Formats */
typedef enum {
TEXT_PLAIN = 0,
TEXT_XML = 1,
TEXT_CSV = 2,
TEXT_HTML = 3,
IMAGE_GIF = 21,
IMAGE_JPEG = 22,
IMAGE_PNG = 23,
IMAGE_TIFF = 24,
AUDIO_RAW = 25,
VIDEO_RAW = 26,
APPLICATION_LINK_FORMAT = 40,
APPLICATION_XML = 41,
APPLICATION_OCTET_STREAM = 42,
APPLICATION_RDF_XML = 43,
APPLICATION_SOAP_XML = 44,
APPLICATION_ATOM_XML = 45,
APPLICATION_XMPP_XML = 46,
APPLICATION_EXI = 47,
APPLICATION_FASTINFOSET = 48,
APPLICATION_SOAP_FASTINFOSET = 49,
APPLICATION_JSON = 50,
APPLICATION_X_OBIX_BINARY = 51
} coap_content_format_t;
/**
* Resource flags for allowed methods and special functionalities.
*/
typedef enum {
NO_FLAGS = 0,
/* methods to handle */
METHOD_GET = (1 << 0),
METHOD_POST = (1 << 1),
METHOD_PUT = (1 << 2),
METHOD_DELETE = (1 << 3),
/* special flags */
HAS_SUB_RESOURCES = (1 << 4),
IS_SEPARATE = (1 << 5),
IS_OBSERVABLE = (1 << 6),
IS_PERIODIC = (1 << 7)
} coap_resource_flags_t;
#endif /* COAP_CONSTANTS_H_ */
/** @} */

View File

@ -1,75 +0,0 @@
/*
* Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*/
/**
* \file
* Collection of default configuration values.
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#ifndef ER_COAP_CONF_H_
#define ER_COAP_CONF_H_
/* Features that can be disabled to achieve smaller memory footprint */
#define COAP_LINK_FORMAT_FILTERING 0
#define COAP_PROXY_OPTION_PROCESSING 0
/* Listening port for the CoAP REST Engine */
#ifndef COAP_SERVER_PORT
#define COAP_SERVER_PORT COAP_DEFAULT_PORT
#endif
/* The number of concurrent messages that can be stored for retransmission in the transaction layer. */
#ifndef COAP_MAX_OPEN_TRANSACTIONS
#define COAP_MAX_OPEN_TRANSACTIONS 4
#endif /* COAP_MAX_OPEN_TRANSACTIONS */
/* Maximum number of failed request attempts before action */
#ifndef COAP_MAX_ATTEMPTS
#define COAP_MAX_ATTEMPTS 4
#endif /* COAP_MAX_ATTEMPTS */
/* Conservative size limit, as not all options have to be set at the same time. Check when Proxy-Uri option is used */
#ifndef COAP_MAX_HEADER_SIZE /* Hdr CoF If-Match Obs Blo strings */
#define COAP_MAX_HEADER_SIZE 512
//(4 + COAP_TOKEN_LEN + 3 + 1 + COAP_ETAG_LEN + 4 + 4 + 30) /* 65 */
#endif /* COAP_MAX_HEADER_SIZE */
/* Number of observer slots (each takes abot xxx bytes) */
#ifndef COAP_MAX_OBSERVERS
#define COAP_MAX_OBSERVERS COAP_MAX_OPEN_TRANSACTIONS - 1
#endif /* COAP_MAX_OBSERVERS */
/* Interval in notifies in which NON notifies are changed to CON notifies to check client. */
#define COAP_OBSERVE_REFRESH_INTERVAL 20
#endif /* ER_COAP_CONF_H_ */

View File

@ -1,161 +0,0 @@
/*
* Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*/
/**
* \file
* Collection of constants specified in the CoAP standard.
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#ifndef ER_COAP_CONSTANTS_H_
#define ER_COAP_CONSTANTS_H_
#define COAP_DEFAULT_PORT 5683
#define COAP_DEFAULT_MAX_AGE 60
#define COAP_RESPONSE_TIMEOUT 3
#define COAP_RESPONSE_RANDOM_FACTOR 1.5
#define COAP_MAX_RETRANSMIT 4
#define COAP_HEADER_LEN 4 /* | version:0x03 type:0x0C tkl:0xF0 | code | mid:0x00FF | mid:0xFF00 | */
#define COAP_TOKEN_LEN 8 /* The maximum number of bytes for the Token */
#define COAP_ETAG_LEN 8 /* The maximum number of bytes for the ETag */
#define COAP_HEADER_VERSION_MASK 0xC0
#define COAP_HEADER_VERSION_POSITION 6
#define COAP_HEADER_TYPE_MASK 0x30
#define COAP_HEADER_TYPE_POSITION 4
#define COAP_HEADER_TOKEN_LEN_MASK 0x0F
#define COAP_HEADER_TOKEN_LEN_POSITION 0
#define COAP_HEADER_OPTION_DELTA_MASK 0xF0
#define COAP_HEADER_OPTION_SHORT_LENGTH_MASK 0x0F
/* CoAP message types */
typedef enum {
COAP_TYPE_CON, /* confirmables */
COAP_TYPE_NON, /* non-confirmables */
COAP_TYPE_ACK, /* acknowledgements */
COAP_TYPE_RST /* reset */
} coap_message_type_t;
/* CoAP request method codes */
typedef enum {
COAP_GET = 1, COAP_POST, COAP_PUT, COAP_DELETE
} coap_method_t;
/* CoAP response codes */
typedef enum {
NO_ERROR = 0,
CREATED_2_01 = 65, /* CREATED */
DELETED_2_02 = 66, /* DELETED */
VALID_2_03 = 67, /* NOT_MODIFIED */
CHANGED_2_04 = 68, /* CHANGED */
CONTENT_2_05 = 69, /* OK */
CONTINUE_2_31 = 95, /* CONTINUE */
BAD_REQUEST_4_00 = 128, /* BAD_REQUEST */
UNAUTHORIZED_4_01 = 129, /* UNAUTHORIZED */
BAD_OPTION_4_02 = 130, /* BAD_OPTION */
FORBIDDEN_4_03 = 131, /* FORBIDDEN */
NOT_FOUND_4_04 = 132, /* NOT_FOUND */
METHOD_NOT_ALLOWED_4_05 = 133, /* METHOD_NOT_ALLOWED */
NOT_ACCEPTABLE_4_06 = 134, /* NOT_ACCEPTABLE */
PRECONDITION_FAILED_4_12 = 140, /* BAD_REQUEST */
REQUEST_ENTITY_TOO_LARGE_4_13 = 141, /* REQUEST_ENTITY_TOO_LARGE */
UNSUPPORTED_MEDIA_TYPE_4_15 = 143, /* UNSUPPORTED_MEDIA_TYPE */
INTERNAL_SERVER_ERROR_5_00 = 160, /* INTERNAL_SERVER_ERROR */
NOT_IMPLEMENTED_5_01 = 161, /* NOT_IMPLEMENTED */
BAD_GATEWAY_5_02 = 162, /* BAD_GATEWAY */
SERVICE_UNAVAILABLE_5_03 = 163, /* SERVICE_UNAVAILABLE */
GATEWAY_TIMEOUT_5_04 = 164, /* GATEWAY_TIMEOUT */
PROXYING_NOT_SUPPORTED_5_05 = 165, /* PROXYING_NOT_SUPPORTED */
/* Erbium errors */
MEMORY_ALLOCATION_ERROR = 192, PACKET_SERIALIZATION_ERROR,
/* Erbium hooks */
MANUAL_RESPONSE, PING_RESPONSE
} coap_status_t;
/* CoAP header option numbers */
typedef enum {
COAP_OPTION_IF_MATCH = 1, /* 0-8 B */
COAP_OPTION_URI_HOST = 3, /* 1-255 B */
COAP_OPTION_ETAG = 4, /* 1-8 B */
COAP_OPTION_IF_NONE_MATCH = 5, /* 0 B */
COAP_OPTION_OBSERVE = 6, /* 0-3 B */
COAP_OPTION_URI_PORT = 7, /* 0-2 B */
COAP_OPTION_LOCATION_PATH = 8, /* 0-255 B */
COAP_OPTION_URI_PATH = 11, /* 0-255 B */
COAP_OPTION_CONTENT_FORMAT = 12, /* 0-2 B */
COAP_OPTION_MAX_AGE = 14, /* 0-4 B */
COAP_OPTION_URI_QUERY = 15, /* 0-255 B */
COAP_OPTION_ACCEPT = 17, /* 0-2 B */
COAP_OPTION_LOCATION_QUERY = 20, /* 0-255 B */
COAP_OPTION_BLOCK2 = 23, /* 1-3 B */
COAP_OPTION_BLOCK1 = 27, /* 1-3 B */
COAP_OPTION_SIZE2 = 28, /* 0-4 B */
COAP_OPTION_PROXY_URI = 35, /* 1-1034 B */
COAP_OPTION_PROXY_SCHEME = 39, /* 1-255 B */
COAP_OPTION_SIZE1 = 60, /* 0-4 B */
} coap_option_t;
/* CoAP Content-Formats */
typedef enum {
TEXT_PLAIN = 0,
TEXT_XML = 1,
TEXT_CSV = 2,
TEXT_HTML = 3,
IMAGE_GIF = 21,
IMAGE_JPEG = 22,
IMAGE_PNG = 23,
IMAGE_TIFF = 24,
AUDIO_RAW = 25,
VIDEO_RAW = 26,
APPLICATION_LINK_FORMAT = 40,
APPLICATION_XML = 41,
APPLICATION_OCTET_STREAM = 42,
APPLICATION_RDF_XML = 43,
APPLICATION_SOAP_XML = 44,
APPLICATION_ATOM_XML = 45,
APPLICATION_XMPP_XML = 46,
APPLICATION_EXI = 47,
APPLICATION_FASTINFOSET = 48,
APPLICATION_SOAP_FASTINFOSET = 49,
APPLICATION_JSON = 50,
APPLICATION_X_OBIX_BINARY = 51
} coap_content_format_t;
#endif /* ER_COAP_CONSTANTS_H_ */

File diff suppressed because it is too large Load Diff

View File

@ -1,370 +0,0 @@
/*
* Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*/
/**
* \file
* An implementation of the Constrained Application Protocol (RFC).
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#ifndef ER_COAP_H_
#define ER_COAP_H_
#include "../extension/coap_platforms.h"
#include <stddef.h> /* for size_t */
#include <stdint.h>
//#include "contiki-net.h"
#include "er-coap-constants.h"
#include "er-coap-conf.h"
#ifdef __cplusplus
extern "C" {
#endif
/* sanity check for configured values */
#define COAP_MAX_PACKET_SIZE (COAP_MAX_HEADER_SIZE + REST_MAX_CHUNK_SIZE)
/*#if COAP_MAX_PACKET_SIZE > (UIP_BUFSIZE - UIP_IPH_LEN - UIP_UDPH_LEN)
#error "UIP_CONF_BUFFER_SIZE too small for REST_MAX_CHUNK_SIZE"
#endif
*/
/* ------------------------------------------------------ */
/* ------ section added by the coap request --------- */
/* ------------------------------------------------------ */
//typedef int (*restful_response_handler)(void *response,void *data);
#define RX_TIMEOUT (-1)
typedef int (*Tx_Data)(void * coap_ctx, const uip_ipaddr_t *dst_addr, void *buf, int len);
typedef int (*Rx_Data) (void * coap_ctx, void *buf, int len, int timeout);
typedef int (*Request_Handler) (void * coap_ctx, void *);
typedef int (*CoAP_Res_Handler) (void * request, void * response, char **out_payload, int * payload_len);
typedef struct _coap_resource_handler
{
struct _coap_resource_handler * next;
char * url;
CoAP_Res_Handler get_handler;
CoAP_Res_Handler put_handler;
CoAP_Res_Handler post_handler;
CoAP_Res_Handler other_handler; // create | delete
}coap_resource_handler_t;
typedef struct res_block_state
{
struct res_block_state * next;
char * url;
void * buffer;
int buffer_size;
uint32_t block_num;
uint16_t block_size;
uint16_t content_fmt;
uint32_t last_access;
uint8_t is_get;
} res_block_state_t;
typedef struct peer_block_state
{
struct peer_block_state * next;
struct net_addr_coap peer_addr;
res_block_state_t * list;
}peer_block_state_t;
typedef struct coap_context {
uint8_t * buf;
uint32_t buf_len; // the data length
uint32_t buf_size; // the malloced buffer size
struct net_addr_coap my_addr;
// the address associated with current buffer
struct net_addr_coap src_addr;
uint8_t status;
uint8_t is_used;
uint8_t response_on_not_found;
uint16_t default_retrans_cnt;
uint32_t default_retrans_ms;
Tx_Data tx_data;
Rx_Data rx_data;
int socket;
char * user_data;
peer_block_state_t * blockwise_list;
coap_resource_handler_t * resource_handlers;
void * transactions;
void * transaction_lock;
uint32_t last_checktime;
void * request_handler;
#ifdef WITH_DTLS
struct dtls_context_t *dtls_context;
dtls_handler_t dtls_handler;
struct process *process;
#endif /* WITH_DTLS */
} coap_context_t;
int add_resource_handler(coap_context_t * coap_ctx, coap_resource_handler_t * handler);
// coap_context_t * coap_context_new(uip_ipaddr_t *my_addr, uint16_t my_port); //CANNOTBUILD
void coap_context_close(coap_context_t *coap_ctx);
void coap_ctx_send(coap_context_t *coap_ctx, uint8_t *data,
uint16_t length);
/* ---------------- end of section ------------------ */
/* use Erbium CoAP for the REST Engine. Must come before include of rest-engine.h. */
//#define REST coap_rest_implementation
//#include "rest-engine.h"
/* REST_MAX_CHUNK_SIZE can be different from 2^x so we need to get next lower 2^x for COAP_MAX_BLOCK_SIZE */
#ifndef COAP_MAX_BLOCK_SIZE
#define COAP_MAX_BLOCK_SIZE (REST_MAX_CHUNK_SIZE < 32 ? 16 : \
(REST_MAX_CHUNK_SIZE < 64 ? 32 : \
(REST_MAX_CHUNK_SIZE < 128 ? 64 : \
(REST_MAX_CHUNK_SIZE < 256 ? 128 : \
(REST_MAX_CHUNK_SIZE < 512 ? 256 : \
(REST_MAX_CHUNK_SIZE < 1024 ? 512 : \
(REST_MAX_CHUNK_SIZE < 2048 ? 1024 : 2048)))))))
#endif /* COAP_MAX_BLOCK_SIZE */
/* direct access into the buffer */
#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
#ifdef NETSTACK_CONF_WITH_IPV6
#define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
#else
#define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN])
#endif
/* bitmap for set options */
enum { OPTION_MAP_SIZE = sizeof(uint8_t) * 8 };
#define SET_OPTION(packet, opt) ((packet)->options[opt / OPTION_MAP_SIZE] |= 1 << (opt % OPTION_MAP_SIZE))
#define IS_OPTION(packet, opt) ((packet)->options[opt / OPTION_MAP_SIZE] & (1 << (opt % OPTION_MAP_SIZE)))
/* parsed message struct */
typedef struct {
uint8_t *buffer; /* pointer to CoAP header / incoming packet buffer / memory to serialize packet */
uint8_t version;
coap_message_type_t type;
uint8_t code;
uint16_t mid;
uint8_t token_len;
uint8_t token[COAP_TOKEN_LEN];
uint8_t options[COAP_OPTION_SIZE1 / OPTION_MAP_SIZE + 1]; /* bitmap to check if option is set */
coap_content_format_t content_format; /* parse options once and store; allows setting options in random order */
uint32_t max_age;
uint8_t etag_len;
uint8_t etag[COAP_ETAG_LEN];
size_t proxy_uri_len;
const char *proxy_uri;
size_t proxy_scheme_len;
const char *proxy_scheme;
size_t uri_host_len;
const char *uri_host;
size_t location_path_len;
const char *location_path;
uint16_t uri_port;
size_t location_query_len;
const char *location_query;
size_t uri_path_len;
const char *uri_path;
int32_t observe;
coap_content_format_t accept;
uint8_t if_match_len;
uint8_t if_match[COAP_ETAG_LEN];
uint32_t block2_num;
uint8_t block2_more;
uint32_t block2_size;
uint32_t block2_offset;
uint32_t block1_num;
uint8_t block1_more;
uint32_t block1_size;
uint32_t block1_offset;
uint32_t size2;
uint32_t size1;
size_t uri_query_len;
const char *uri_query;
uint8_t if_none_match;
uint32_t payload_len;
uint8_t *payload;
} coap_packet_t;
/* option format serialization */
#define COAP_SERIALIZE_INT_OPTION(number, field, text) \
if(IS_OPTION(coap_pkt, number)) { \
PRINTF(text " [%u]\n", coap_pkt->field); \
option += coap_serialize_int_option(number, current_number, option, coap_pkt->field); \
current_number = number; \
}
#define COAP_SERIALIZE_BYTE_OPTION(number, field, text) \
if(IS_OPTION(coap_pkt, number)) { \
PRINTF(text " %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", coap_pkt->field##_len, \
coap_pkt->field[0], \
coap_pkt->field[1], \
coap_pkt->field[2], \
coap_pkt->field[3], \
coap_pkt->field[4], \
coap_pkt->field[5], \
coap_pkt->field[6], \
coap_pkt->field[7] \
); /* FIXME always prints 8 bytes */ \
option += coap_serialize_array_option(number, current_number, option, coap_pkt->field, coap_pkt->field##_len, '\0'); \
current_number = number; \
}
#define COAP_SERIALIZE_STRING_OPTION(number, field, splitter, text) \
if(IS_OPTION(coap_pkt, number)) { \
PRINTF(text " [%.*s]\n", coap_pkt->field##_len, coap_pkt->field); \
option += coap_serialize_array_option(number, current_number, option, (uint8_t *)coap_pkt->field, coap_pkt->field##_len, splitter); \
current_number = number; \
}
#define COAP_SERIALIZE_BLOCK_OPTION(number, field, text) \
if(IS_OPTION(coap_pkt, number)) \
{ \
PRINTF(text " [%lu%s (%u B/blk)]\n", coap_pkt->field##_num, coap_pkt->field##_more ? "+" : "", coap_pkt->field##_size); \
uint32_t block = coap_pkt->field##_num << 4; \
if(coap_pkt->field##_more) { block |= 0x8; } \
block |= 0xF & coap_log_2(coap_pkt->field##_size / 16); \
PRINTF(text " encoded: 0x%lX\n", block); \
option += coap_serialize_int_option(number, current_number, option, block); \
current_number = number; \
}
/* to store error code and human-readable payload */
extern coap_status_t erbium_status_code;
extern char *coap_error_message;
void coap_init_connection(uint16_t port);
uint16_t coap_get_mid(void);
void coap_init_message(void *packet, coap_message_type_t type, uint8_t code,
uint16_t mid);
size_t coap_serialize_message(void *packet, uint8_t *buffer);
void coap_send_message(coap_context_t*, uip_ipaddr_t *addr, uint16_t port, uint8_t *data,
uint16_t length);
coap_status_t coap_parse_message(void *request, uint8_t *data,
uint16_t data_len);
int coap_get_query_variable(void *packet, const char *name,
const char **output);
int coap_get_post_variable(void *packet, const char *name,
const char **output);
/*---------------------------------------------------------------------------*/
int coap_set_status_code(void *packet, unsigned int code);
int coap_set_token(void *packet, const uint8_t *token, size_t token_len);
int coap_get_header_content_format(void *packet, unsigned int *format);
int coap_set_header_content_format(void *packet, unsigned int format);
int coap_get_header_accept(void *packet, unsigned int *accept);
int coap_set_header_accept(void *packet, unsigned int accept);
int coap_get_header_max_age(void *packet, uint32_t *age);
int coap_set_header_max_age(void *packet, uint32_t age);
int coap_get_header_etag(void *packet, const uint8_t **etag);
int coap_set_header_etag(void *packet, const uint8_t *etag, size_t etag_len);
int coap_get_header_if_match(void *packet, const uint8_t **etag);
int coap_set_header_if_match(void *packet, const uint8_t *etag,
size_t etag_len);
int coap_get_header_if_none_match(void *packet);
int coap_set_header_if_none_match(void *packet);
int coap_get_header_proxy_uri(void *packet, const char **uri); /* in-place string might not be 0-terminated. */
int coap_set_header_proxy_uri(void *packet, const char *uri);
int coap_get_header_proxy_scheme(void *packet, const char **scheme); /* in-place string might not be 0-terminated. */
int coap_set_header_proxy_scheme(void *packet, const char *scheme);
int coap_get_header_uri_host(void *packet, const char **host); /* in-place string might not be 0-terminated. */
int coap_set_header_uri_host(void *packet, const char *host);
int coap_get_header_uri_path(void *packet, const char **path); /* in-place string might not be 0-terminated. */
int coap_set_header_uri_path(void *packet, const char *path);
int coap_get_header_uri_query(void *packet, const char **query); /* in-place string might not be 0-terminated. */
int coap_set_header_uri_query(void *packet, const char *query);
int coap_get_header_location_path(void *packet, const char **path); /* in-place string might not be 0-terminated. */
int coap_set_header_location_path(void *packet, const char *path); /* also splits optional query into Location-Query option. */
int coap_get_header_location_query(void *packet, const char **query); /* in-place string might not be 0-terminated. */
int coap_set_header_location_query(void *packet, const char *query);
int coap_get_header_observe(void *packet, uint32_t *observe);
int coap_set_header_observe(void *packet, uint32_t observe);
int coap_get_header_block2(void *packet, uint32_t *num, uint8_t *more,
uint32_t *size, uint32_t *offset);
int coap_set_header_block2(void *packet, uint32_t num, uint8_t more,
uint32_t size);
int coap_get_header_block1(void *packet, uint32_t *num, uint8_t *more,
uint16_t *size, uint32_t *offset);
int coap_set_header_block1(void *packet, uint32_t num, uint8_t more,
uint16_t size);
int coap_get_header_size2(void *packet, uint32_t *size);
int coap_set_header_size2(void *packet, uint32_t size);
int coap_get_header_size1(void *packet, uint32_t *size);
int coap_set_header_size1(void *packet, uint32_t size);
int coap_get_payload(void *packet, const uint8_t **payload);
int coap_set_payload(void *packet, const void *payload, size_t length);
#ifdef __cplusplus
}
#endif
#endif /* ER_COAP_H_ */

View File

@ -1,4 +0,0 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
obj-y += coap_over_tcp.o

View File

@ -1,94 +0,0 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "coap_ext.h"
char * coap_get_full_url_alloc(coap_packet_t * request)
{
const char *url = NULL;
const char * query = NULL;
int url_len = coap_get_header_uri_path(request, &url);
int query_len = coap_get_header_uri_query(request, &query);
if (url_len == 0)
return NULL;
char * url_alloc = (char*) bh_malloc(url_len + 1 + query_len + 1);
memcpy(url_alloc, url, url_len);
url_alloc[url_len] = 0;
// make the url looks like /abc?e=f
if (query_len != 0) {
strcat(url_alloc, "&");
memcpy(url_alloc + strlen(url_alloc), query, query_len);
url_alloc[url_len + 1 + query_len] = 0;
}
return url_alloc;
}
void convert_request_to_coap_packet(request_t * req, coap_packet_t * packet)
{
coap_init_message(packet, COAP_TYPE_NON, req->action, req->mid);
coap_set_token(packet, (uint8_t *) &req->mid, sizeof(req->mid));
coap_set_header_content_format(packet, req->fmt);
coap_set_header_uri_path(packet, req->url);
coap_set_payload(packet, req->payload, req->payload_len);
packet->mid = req->mid;
}
void convert_response_to_coap_packet(response_t * response,
coap_packet_t * packet)
{
coap_init_message(packet, COAP_TYPE_NON, response->status, response->mid);
coap_set_token(packet, (uint8_t *) &response->mid, sizeof(response->mid));
coap_set_header_content_format(packet, response->fmt);
coap_set_payload(packet, response->payload, response->payload_len);
packet->mid = response->mid;
}
// return: the length of url.
// note: the url is probably not end with 0 due to coap packing design.
int convert_coap_packet_to_request(coap_packet_t *packet, request_t *request)
{
const char *url = NULL;
int url_len = coap_get_header_uri_path(packet, &url);
memset(request, 0, sizeof(*request));
request->action = packet->code;
request->fmt = packet->content_format;
if (packet->token_len == 4) {
request->mid = *((unsigned long *) packet->token);
} else {
request->mid = packet->mid;
}
request->payload = packet->payload;
request->payload_len = packet->payload_len;
request->url = (char *)url;
return url_len;
}
void convert_coap_packet_to_response(coap_packet_t *packet,
response_t *response)
{
memset(response, 0, sizeof(*response));
response->status = packet->code;
response->fmt = packet->content_format;
if (packet->token_len == 4) {
response->mid = *((unsigned long *) packet->token);
} else {
response->mid = packet->mid;
}
response->payload = packet->payload;
response->payload_len = packet->payload_len;
return;
}

28
core/shared-lib/coap/extension/coap_ext.h Executable file → Normal file
View File

@ -6,8 +6,7 @@
#ifndef COAP_EXTENSION_COAP_EXT_H_
#define COAP_EXTENSION_COAP_EXT_H_
#include "er-coap.h"
#include "shared_utils.h"
#include "coap-constants.h"
#ifdef __cplusplus
extern "C" {
@ -15,31 +14,6 @@ extern "C" {
#define COAP_EVENT (COAP_DELETE + 2)
char * coap_get_full_url_alloc(coap_packet_t * request);
coap_status_t coap_parse_message_tcp(void *packet, uint8_t *data,
uint32_t data_len);
int coap_serialize_message_tcp(void *packet, uint8_t ** buffer_out);
int coap_set_payload_tcp(void *packet, const void *payload, size_t length);
uint8_t coap_is_request(coap_packet_t * coap_message);
uint16_t coap_find_mid(uint8_t *buffer);
uint8_t coap_find_code(uint8_t *buffer);
void coap_change_mid(uint8_t *buffer, uint16_t id);
int add_resource_handler(coap_context_t * coap_ctx,
coap_resource_handler_t * handler);
uint32_t check_blockwise_timeout_ms(coap_context_t * coap_ctx, int timeout_sec);
int convert_coap_packet_to_request(coap_packet_t *packet, request_t *request);
void convert_coap_packet_to_response(coap_packet_t *packet,
response_t *response);
void convert_response_to_coap_packet(response_t * response,
coap_packet_t * packet);
void convert_request_to_coap_packet(request_t * req, coap_packet_t * packet);
#ifdef __cplusplus
}
#endif

View File

@ -1,470 +0,0 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <string.h>
#include <stdio.h>
#include "bh_common.h"
#include "er-coap.h"
#include "coap_ext.h"
#include "er-coap-constants.h"
#define DEBUG 0
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#define PRINT6ADDR(addr) PRINTF("[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
#define PRINTLLADDR(lladdr) PRINTF("[%02x:%02x:%02x:%02x:%02x:%02x]", (lladdr)->addr[0], (lladdr)->addr[1], (lladdr)->addr[2], (lladdr)->addr[3], (lladdr)->addr[4], (lladdr)->addr[5])
#else
#define PRINTF(...)
#define PRINT6ADDR(addr)
#define PRINTLLADDR(addr)
#endif
extern size_t
coap_serialize_array_option(unsigned int number, unsigned int current_number,
uint8_t *buffer, uint8_t *array, size_t length, char split_char);
extern size_t
coap_serialize_int_option(unsigned int number, unsigned int current_number,
uint8_t *buffer, uint32_t value);
extern uint16_t coap_log_2(uint16_t value);
extern uint32_t coap_parse_int_option(uint8_t *bytes, size_t length);
extern void
coap_merge_multi_option(char **dst, size_t *dst_len, uint8_t *option,
size_t option_len, char separator);
/*
*
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Len=15 | TKL | Extended Length (32 bits)
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | Code | Token (if any, TKL bytes) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options (if any) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|1 1 1 1 1 1 1 1| Payload (if any) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
int coap_set_payload_tcp(void *packet, const void *payload, size_t length)
{
coap_packet_t * const coap_pkt = (coap_packet_t *) packet;
coap_pkt->payload = (uint8_t *) payload;
coap_pkt->payload_len = MIN(REST_MAX_CHUNK_SIZE, length);
return coap_pkt->payload_len;
}
#if 0
static size_t coap_calc_ext_len_field(int len)
{
if(len < 13)
return 0;
else if(len <= (0xFF+13))
return 1;
else if(len <= (0xFFFF+269))
return 2;
else if(len < (0xFFFFFFFF+65805))
return 4;
else
return 0;
}
#endif
static size_t coap_max_options_offset(void *packet)
{
coap_packet_t * const coap_pkt = (coap_packet_t *) packet;
return 6 + coap_pkt->token_len;
}
int coap_serialize_message_tcp(void *packet, uint8_t ** buffer_out)
{
coap_packet_t * const coap_pkt = (coap_packet_t *) packet;
uint8_t buffer[128];
uint8_t *option = buffer;
unsigned int current_number = 0;
if (coap_pkt->uri_path_len > 100) {
*buffer_out = 0;
return -1;
}
/* Serialize options */
current_number = 0;
if (0 == coap_pkt->token_len) {
bh_memcpy_s(coap_pkt->token, COAP_TOKEN_LEN, &coap_pkt->mid,
sizeof(coap_pkt->mid));
coap_pkt->token_len = sizeof(coap_pkt->mid);
}PRINTF("-Serializing options at %p-\n", option);
/* The options must be serialized in the order of their number */
COAP_SERIALIZE_BYTE_OPTION(COAP_OPTION_IF_MATCH, if_match, "If-Match");
COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_URI_HOST, uri_host, '\0',
"Uri-Host");
COAP_SERIALIZE_BYTE_OPTION(COAP_OPTION_ETAG, etag, "ETag");
COAP_SERIALIZE_INT_OPTION(COAP_OPTION_IF_NONE_MATCH,
content_format - coap_pkt-> content_format /* hack to get a zero field */,
"If-None-Match");
COAP_SERIALIZE_INT_OPTION(COAP_OPTION_OBSERVE, observe, "Observe");
COAP_SERIALIZE_INT_OPTION(COAP_OPTION_URI_PORT, uri_port, "Uri-Port");
COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_LOCATION_PATH, location_path, '/',
"Location-Path");
COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_URI_PATH, uri_path, 0, //'/',
"Uri-Path");
COAP_SERIALIZE_INT_OPTION(COAP_OPTION_CONTENT_FORMAT, content_format,
"Content-Format");
COAP_SERIALIZE_INT_OPTION(COAP_OPTION_MAX_AGE, max_age, "Max-Age");
COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_URI_QUERY, uri_query, '&',
"Uri-Query");
COAP_SERIALIZE_INT_OPTION(COAP_OPTION_ACCEPT, accept, "Accept");
COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_LOCATION_QUERY, location_query,
'&', "Location-Query");
COAP_SERIALIZE_BLOCK_OPTION(COAP_OPTION_BLOCK2, block2, "Block2");
COAP_SERIALIZE_BLOCK_OPTION(COAP_OPTION_BLOCK1, block1, "Block1");
COAP_SERIALIZE_INT_OPTION(COAP_OPTION_SIZE2, size2, "Size2");
COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_PROXY_URI, proxy_uri, '\0',
"Proxy-Uri");
COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_PROXY_SCHEME, proxy_scheme, '\0',
"Proxy-Scheme");
COAP_SERIALIZE_INT_OPTION(COAP_OPTION_SIZE1, size1, "Size1");
/* Pack payload */
if (coap_pkt->payload_len) {
*option = 0xFF;
++option;
}
uint32_t option_len = option - &buffer[0];
uint8_t * p = (uint8_t *) os_malloc(
coap_max_options_offset(packet) + option_len
+ coap_pkt->payload_len);
if (p == NULL)
return 0;
*buffer_out = p;
uint8_t first_4bits;
*p = (coap_pkt->token_len & 0xF);
uint32_t len = option_len + coap_pkt->payload_len;
if (len < 13) {
first_4bits = len;
*p++ |= first_4bits << 4;
} else if (len <= (0xFF + 13)) {
first_4bits = 13;
*p++ |= first_4bits << 4;
*p++ = len - 13;
} else if (len <= (0xFFFF + 269)) {
first_4bits = 14;
*p++ |= first_4bits << 4;
len -= 269;
*p = (uint8_t)(len >> 8);
p++;
*p = (uint8_t)(len & 0xFF);
p++;
} else {
first_4bits = 15;
*p++ |= first_4bits << 4;
len -= 65805;
*p++ = (uint8_t)(len >> 24);
*p++ = (uint8_t)(len >> 16);
*p++ = (uint8_t)(len >> 8);
*p++ = (uint8_t)(len & 0xFF);
}
*p = coap_pkt->code;
p++;
if (coap_pkt->token_len)
bh_memcpy_s(p, coap_pkt->token_len, coap_pkt->token,
coap_pkt->token_len);
p += coap_pkt->token_len;
bh_memcpy_s(p, option_len, buffer, option_len);
p += option_len;
bh_memcpy_s(p, coap_pkt->payload_len, coap_pkt->payload,
coap_pkt->payload_len);
p += coap_pkt->payload_len;
return (p - *buffer_out); /* packet length */
}
coap_status_t coap_parse_message_tcp(void *packet, uint8_t *data,
uint32_t data_len)
{
coap_packet_t * const coap_pkt = (coap_packet_t *) packet;
/* initialize packet */
memset(coap_pkt, 0, sizeof(coap_packet_t));
/* pointer to packet bytes */
coap_pkt->buffer = data;
/* parse header fields */
coap_pkt->version = 1;
coap_pkt->type = COAP_TYPE_NON;
coap_pkt->token_len = MIN(COAP_TOKEN_LEN, data[0] & 0xF);
coap_pkt->mid = 0;
uint8_t *p = data;
uint8_t first_4bits = data[0] >> 4;
uint32_t options_payload_size;
uint8_t ext_len_field = 0;
if (first_4bits < 13) {
options_payload_size = first_4bits;
p++;
} else if (first_4bits == 13) {
ext_len_field = 1;
options_payload_size = data[1] + 13;
p += 2;
} else if (first_4bits == 14) {
ext_len_field = 2;
options_payload_size = (uint16_t)(data[1] << 8) + data[2] + 269;
p += 3;
} else if (first_4bits == 15) {
ext_len_field = 4;
options_payload_size = (data[1] << 24) + (data[2] << 16)
+ (data[3] << 8) + data[4] + 65805;
p += 5;
}
// check the data size is smaller than the size indicated by the packet
if (ext_len_field + coap_pkt->token_len + 2 + options_payload_size
> data_len)
return BAD_REQUEST_4_00;
coap_pkt->code = *p++;
if (coap_pkt->token_len)
bh_memcpy_s(coap_pkt->token, COAP_TOKEN_LEN, p, coap_pkt->token_len);
if (coap_pkt->token_len >= 2) {
union {
uint16_t *mid;
uint8_t *token;
} mid_token_union;
mid_token_union.token = coap_pkt->token;
coap_pkt->mid = *(mid_token_union.mid);
}
p += coap_pkt->token_len;
uint8_t *current_option = p;
uint8_t * option_start = p;
/* parse options */
memset(coap_pkt->options, 0, sizeof(coap_pkt->options));
unsigned int option_number = 0;
unsigned int option_delta = 0;
size_t option_length = 0;
while (current_option < data + data_len) {
/* payload marker 0xFF, currently only checking for 0xF* because rest is reserved */
if ((current_option[0] & 0xF0) == 0xF0) {
coap_pkt->payload = ++current_option;
coap_pkt->payload_len = options_payload_size
- (coap_pkt->payload - option_start);
//coap_pkt->payload_len = data_len - (coap_pkt->payload - data);
break;
}
option_delta = current_option[0] >> 4;
option_length = current_option[0] & 0x0F;
++current_option;
/* avoids code duplication without function overhead */
unsigned int *x = &option_delta;
do {
if (*x == 13) {
*x += current_option[0];
++current_option;
} else if (*x == 14) {
*x += 255;
*x += current_option[0] << 8;
++current_option;
*x += current_option[0];
++current_option;
}
} while (x != (unsigned int*) &option_length && (x =
(unsigned int*) &option_length));
option_length = *x;
option_number += option_delta;
PRINTF("OPTION %u (delta %u, len %u): ", option_number, option_delta,
option_length);
SET_OPTION(coap_pkt, option_number);
switch (option_number) {
case COAP_OPTION_CONTENT_FORMAT:
coap_pkt->content_format = coap_parse_int_option(current_option,
option_length);
PRINTF("Content-Format [%u]\n", coap_pkt->content_format);
break;
case COAP_OPTION_MAX_AGE:
coap_pkt->max_age = coap_parse_int_option(current_option,
option_length);
PRINTF("Max-Age [%lu]\n", coap_pkt->max_age);
break;
case COAP_OPTION_ETAG:
coap_pkt->etag_len = MIN(COAP_ETAG_LEN, option_length);
bh_memcpy_s(coap_pkt->etag, COAP_ETAG_LEN, current_option,
coap_pkt->etag_len);
PRINTF("ETag %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n",
coap_pkt->etag_len, coap_pkt->etag[0], coap_pkt->etag[1],
coap_pkt->etag[2], coap_pkt->etag[3], coap_pkt->etag[4],
coap_pkt->etag[5], coap_pkt->etag[6], coap_pkt->etag[7]
); /*FIXME always prints 8 bytes */
break;
case COAP_OPTION_ACCEPT:
coap_pkt->accept = coap_parse_int_option(current_option,
option_length);
PRINTF("Accept [%u]\n", coap_pkt->accept);
break;
case COAP_OPTION_IF_MATCH:
/* TODO support multiple ETags */
coap_pkt->if_match_len = MIN(COAP_ETAG_LEN, option_length);
bh_memcpy_s(coap_pkt->if_match, COAP_ETAG_LEN, current_option,
coap_pkt->if_match_len);
PRINTF("If-Match %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n",
coap_pkt->if_match_len, coap_pkt->if_match[0],
coap_pkt->if_match[1], coap_pkt->if_match[2],
coap_pkt->if_match[3], coap_pkt->if_match[4],
coap_pkt->if_match[5], coap_pkt->if_match[6],
coap_pkt->if_match[7]
); /* FIXME always prints 8 bytes */
break;
case COAP_OPTION_IF_NONE_MATCH:
coap_pkt->if_none_match = 1;
PRINTF("If-None-Match\n");
break;
case COAP_OPTION_PROXY_URI:
#if COAP_PROXY_OPTION_PROCESSING
coap_pkt->proxy_uri = (char *)current_option;
coap_pkt->proxy_uri_len = option_length;
#endif
PRINTF("Proxy-Uri NOT IMPLEMENTED [%.*s]\n", coap_pkt->proxy_uri_len,
coap_pkt->proxy_uri);
coap_error_message = "This is a constrained server (Contiki)";
return PROXYING_NOT_SUPPORTED_5_05;
break;
case COAP_OPTION_PROXY_SCHEME:
#if COAP_PROXY_OPTION_PROCESSING
coap_pkt->proxy_scheme = (char *)current_option;
coap_pkt->proxy_scheme_len = option_length;
#endif
PRINTF("Proxy-Scheme NOT IMPLEMENTED [%.*s]\n",
coap_pkt->proxy_scheme_len, coap_pkt->proxy_scheme);
coap_error_message = "This is a constrained server (Contiki)";
return PROXYING_NOT_SUPPORTED_5_05;
break;
case COAP_OPTION_URI_HOST:
coap_pkt->uri_host = (char *) current_option;
coap_pkt->uri_host_len = option_length;
PRINTF("Uri-Host [%.*s]\n", coap_pkt->uri_host_len, coap_pkt->uri_host);
break;
case COAP_OPTION_URI_PORT:
coap_pkt->uri_port = coap_parse_int_option(current_option,
option_length);
PRINTF("Uri-Port [%u]\n", coap_pkt->uri_port);
break;
case COAP_OPTION_URI_PATH:
/* coap_merge_multi_option() operates in-place on the IPBUF, but final packet field should be const string -> cast to string */
coap_merge_multi_option((char **) &(coap_pkt->uri_path),
&(coap_pkt->uri_path_len), current_option, option_length,
'/');
PRINTF("Uri-Path [%.*s]\n", coap_pkt->uri_path_len, coap_pkt->uri_path);
break;
case COAP_OPTION_URI_QUERY:
/* coap_merge_multi_option() operates in-place on the IPBUF, but final packet field should be const string -> cast to string */
coap_merge_multi_option((char **) &(coap_pkt->uri_query),
&(coap_pkt->uri_query_len), current_option, option_length,
'&');
PRINTF("Uri-Query [%.*s]\n", coap_pkt->uri_query_len,
coap_pkt->uri_query);
break;
case COAP_OPTION_LOCATION_PATH:
/* coap_merge_multi_option() operates in-place on the IPBUF, but final packet field should be const string -> cast to string */
coap_merge_multi_option((char **) &(coap_pkt->location_path),
&(coap_pkt->location_path_len), current_option,
option_length, '/');
PRINTF("Location-Path [%.*s]\n", coap_pkt->location_path_len,
coap_pkt->location_path);
break;
case COAP_OPTION_LOCATION_QUERY:
/* coap_merge_multi_option() operates in-place on the IPBUF, but final packet field should be const string -> cast to string */
coap_merge_multi_option((char **) &(coap_pkt->location_query),
&(coap_pkt->location_query_len), current_option,
option_length, '&');
PRINTF("Location-Query [%.*s]\n", coap_pkt->location_query_len,
coap_pkt->location_query);
break;
case COAP_OPTION_OBSERVE:
coap_pkt->observe = coap_parse_int_option(current_option,
option_length);
PRINTF("Observe [%lu]\n", coap_pkt->observe);
break;
case COAP_OPTION_BLOCK2:
coap_pkt->block2_num = coap_parse_int_option(current_option,
option_length);
coap_pkt->block2_more = (coap_pkt->block2_num & 0x08) >> 3;
coap_pkt->block2_size = 16 << (coap_pkt->block2_num & 0x07);
coap_pkt->block2_offset = (coap_pkt->block2_num & ~0x0000000F)
<< (coap_pkt->block2_num & 0x07);
coap_pkt->block2_num >>= 4;
PRINTF("Block2 [%lu%s (%u B/blk)]\n", coap_pkt->block2_num,
coap_pkt->block2_more ? "+" : "", coap_pkt->block2_size);
break;
case COAP_OPTION_BLOCK1:
coap_pkt->block1_num = coap_parse_int_option(current_option,
option_length);
coap_pkt->block1_more = (coap_pkt->block1_num & 0x08) >> 3;
coap_pkt->block1_size = 16 << (coap_pkt->block1_num & 0x07);
coap_pkt->block1_offset = (coap_pkt->block1_num & ~0x0000000F)
<< (coap_pkt->block1_num & 0x07);
coap_pkt->block1_num >>= 4;
PRINTF("Block1 [%lu%s (%u B/blk)]\n", coap_pkt->block1_num,
coap_pkt->block1_more ? "+" : "", coap_pkt->block1_size);
break;
case COAP_OPTION_SIZE2:
coap_pkt->size2 = coap_parse_int_option(current_option,
option_length);
PRINTF("Size2 [%lu]\n", coap_pkt->size2);
break;
case COAP_OPTION_SIZE1:
coap_pkt->size1 = coap_parse_int_option(current_option,
option_length);
PRINTF("Size1 [%lu]\n", coap_pkt->size1);
break;
default:
PRINTF("unknown (%u)\n", option_number);
/* check if critical (odd) */
if (option_number & 1) {
coap_error_message = "Unsupported critical option";
return BAD_OPTION_4_02;
}
}
current_option += option_length;
} /* for */
PRINTF("-Done parsing-------\n");
return NO_ERROR;
}

View File

@ -1,78 +0,0 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef COAP_PLATFORMS_H_
#define COAP_PLATFORMS_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "bh_platform.h"
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
/*#include "list_coap.h"*/
#include <stdbool.h>
#define COAP_TRANS_LOCK(ctx) coap_lock(ctx->transaction_lock)
#define COAP_TRANS_UNLOCK(ctx ) coap_unlock(ctx->transaction_lock)
/* REST_MAX_CHUNK_SIZE is the max size of payload.
* The maximum buffer size that is provided for resource responses and must be respected due to the limited IP buffer.
* Larger data must be handled by the resource and will be sent chunk-wise through a TCP stream or CoAP blocks.
*/
#ifndef REST_MAX_CHUNK_SIZE
#define REST_MAX_CHUNK_SIZE (1024*1024)
#endif
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif /* MIN */
#define CLOCK_SECOND 1000
typedef enum {
A_Raw, A_Sock_Addr, A_IP_Addr, A_Custom
} Net_Addr_Type;
#define NET_ADDR_RAW_SIZE 32
typedef struct net_addr_coap {
Net_Addr_Type addr_type;
union {
char raw[NET_ADDR_RAW_SIZE];
struct sockaddr_in sock_addr;
} u;
uint16_t port;
uint16_t addr_len;
} net_addr_t;
#define uip_ipaddr_t struct net_addr_coap
#define memb_free(x, y) free(x)
void set_addr_ip(uip_ipaddr_t *, char * ip, int port);
uip_ipaddr_t * new_net_addr(Net_Addr_Type type);
void copy_net_addr(uip_ipaddr_t * dest, uip_ipaddr_t * src);
bool compare_net_addr(uip_ipaddr_t * dest, uip_ipaddr_t * src);
uint32_t get_elpased_ms(uint32_t * last_system_clock);
uint32_t get_platform_time();
uint32_t get_platform_time_sec();
void coap_sleep_ms(uint32_t ms);
void coap_lock(void *);
void coap_unlock(void *);
void * coap_create_lock();
void coap_free_lock(void *);
void *xalloc(uint32_t size);
#define os_malloc bh_malloc
#define os_free bh_free
#ifdef __cplusplus
}
#endif
#endif /* COAP_PLATFORMS_H_ */

View File

@ -8,7 +8,6 @@
#include "bh_assert.h"
#include "bh_platform.h"
#include "bh_log.h"
#include "bh_list.h"
typedef void (*bh_print_function_t)(const char* message);

View File

@ -45,7 +45,7 @@ void bh_memory_destroy();
* Get the pool size of memory, if memory is initialized with allocator,
* return 1GB by default.
*/
int bh_memory_pool_size();
unsigned bh_memory_pool_size();
#if BEIHAI_ENABLE_MEMORY_PROFILING == 0

View File

@ -54,7 +54,7 @@ void
bh_queue_destroy(bh_queue *queue);
char * bh_message_payload(bh_message_t message);
int bh_message_payload_len(bh_message_t message);
uint32 bh_message_payload_len(bh_message_t message);
int bh_message_type(bh_message_t message);
bh_message_t bh_new_msg(unsigned short tag, void *body, unsigned int len,

View File

@ -95,7 +95,7 @@ void bh_memory_destroy()
memory_mode = MEMORY_MODE_UNKNOWN;
}
int bh_memory_pool_size()
unsigned bh_memory_pool_size()
{
if (memory_mode == MEMORY_MODE_POOL)
return global_pool_size;
@ -155,7 +155,7 @@ void* bh_malloc_profile(const char *file,
profile = bh_malloc_internal(sizeof(memory_profile_t));
if (!profile) {
vm_mutex_unlock(&profile_lock);
memcpy(p, &size, sizeof(size));
bh_memcpy_s(p, size + 8, &size, sizeof(size));
return (char *)p + 8;
}
@ -171,7 +171,7 @@ void* bh_malloc_profile(const char *file,
vm_mutex_unlock(&profile_lock);
memcpy(p, &size, sizeof(size));
bh_memcpy_s(p, size + 8, &size, sizeof(size));
memory_in_use += size;
memory_profile_print(file, line, func, size);

View File

@ -94,7 +94,7 @@ static void unlink_hmu(gc_heap_t *heap, hmu_t *hmu)
size = hmu_get_size(hmu);
if (HMU_IS_FC_NORMAL(size)) {
int node_idx = size >> 3;
uint32 node_idx = size >> 3;
hmu_normal_node_t* node = heap->kfc_normal_list[node_idx].next;
hmu_normal_node_t** p = &(heap->kfc_normal_list[node_idx].next);
while (node) {
@ -120,7 +120,7 @@ static void hmu_set_free_size(hmu_t *hmu)
bh_assert(hmu && hmu_get_ut(hmu) == HMU_FC);
size = hmu_get_size(hmu);
*((int*) ((char*) hmu + size) - 1) = size;
*((uint32*) ((char*) hmu + size) - 1) = size;
}
/* Add free chunk back to KFC*/
@ -135,7 +135,7 @@ void gci_add_fc(gc_heap_t *heap, hmu_t *hmu, gc_size_t size)
{
hmu_normal_node_t *np = NULL;
hmu_tree_node_t *root = NULL, *tp = NULL, *node = NULL;
int node_idx;
uint32 node_idx;
bh_assert(gci_is_heap_valid(heap));
bh_assert(
@ -203,7 +203,7 @@ void gci_add_fc(gc_heap_t *heap, hmu_t *hmu, gc_size_t size)
BH_STATIC hmu_t *alloc_hmu(gc_heap_t *heap, gc_size_t size)
{
hmu_normal_node_t *node = NULL, *p = NULL;
int node_idx = 0, init_node_idx = 0;
uint32 node_idx = 0, init_node_idx = 0;
hmu_tree_node_t *root = NULL, *tp = NULL, *last_tp = NULL;
hmu_t *next, *rest;
@ -216,7 +216,7 @@ BH_STATIC hmu_t *alloc_hmu(gc_heap_t *heap, gc_size_t size)
/* check normal list at first*/
if (HMU_IS_FC_NORMAL(size)) {
/* find a non-empty slot in normal_node_list with good size*/
init_node_idx = (int) (size >> 3);
init_node_idx = (size >> 3);
for (node_idx = init_node_idx; node_idx < HMU_NORMAL_NODE_CNT;
node_idx++) {
node = heap->kfc_normal_list + node_idx;
@ -233,8 +233,8 @@ BH_STATIC hmu_t *alloc_hmu(gc_heap_t *heap, gc_size_t size)
node->next = p->next;
bh_assert(((gc_int32)(uintptr_t)hmu_to_obj(p) & 7) == 0);
if ((gc_size_t) node_idx
!= init_node_idx&& ((gc_size_t)node_idx << 3) >= size + GC_SMALLEST_SIZE) { /* with bigger size*/
if ((gc_size_t)node_idx != (uint32)init_node_idx
&& ((gc_size_t)node_idx << 3) >= size + GC_SMALLEST_SIZE) { /* with bigger size*/
rest = (hmu_t*) (((char *) p) + size);
gci_add_fc(heap, rest, (node_idx << 3) - size);
hmu_mark_pinuse(rest);

View File

@ -169,7 +169,7 @@ extern int gc_destroy_with_pool(gc_handle_t handle);
* @param size [in] the size of stats
* @param mmt [in] type of heap, MMT_SHARED or MMT_INSTANCE
*/
extern void* gc_heap_stats(void *heap, int* stats, int size, gc_mm_t mmt);
extern void* gc_heap_stats(void *heap, uint32* stats, int size, gc_mm_t mmt);
/**
* Set GC threshold factor

View File

@ -77,7 +77,7 @@ extern void hmu_verify(hmu_t *hmu);
#define hmu_obj_size(s) ((s)-OBJ_EXTRA_SIZE)
#define GC_ALIGN_8(s) (((int)(s) + 7) & ~7)
#define GC_ALIGN_8(s) (((uint32)(s) + 7) & (uint32)~7)
#define GC_SMALLEST_SIZE GC_ALIGN_8(HMU_SIZE + OBJ_PREFIX_SIZE + OBJ_SUFFIX_SIZE + 8)
#define GC_GET_REAL_SIZE(x) GC_ALIGN_8(HMU_SIZE + OBJ_PREFIX_SIZE + OBJ_SUFFIX_SIZE + (((x) > 8) ? (x): 8))
@ -86,14 +86,14 @@ extern void hmu_verify(hmu_t *hmu);
#define SETBIT(v, offset) (v) |= (1 << (offset))
#define GETBIT(v, offset) ((v) & (1 << (offset)) ? 1 : 0)
#define CLRBIT(v, offset) (v) &= ~(1 << (offset))
#define CLRBIT(v, offset) (v) &= (uint32)(~(1 << (offset)))
#define SETBITS(v, offset, size, value) do { \
(v) &= ~(((1 << size) - 1) << offset); \
(v) |= value << offset; \
} while(0)
(v) &= (uint32)(~(((1 << size) - 1) << offset));\
(v) |= (uint32)(value << offset); \
} while(0)
#define CLRBITS(v, offset, size) (v) &= ~(((1 << size) - 1) << offset)
#define GETBITS(v, offset, size) (((v) & (((1 << size) - 1) << offset)) >> offset)
#define GETBITS(v, offset, size) (((v) & ((uint32)(((1 << size) - 1) << offset))) >> offset)
/*////// gc object layout definition*/

View File

@ -41,7 +41,7 @@ int gci_check_platform()
gc_handle_t gc_init_with_pool(char *buf, gc_size_t buf_size)
{
char *buf_end = buf + buf_size;
char *buf_aligned = (char*) (((uintptr_t) buf + 7) & ~7);
char *buf_aligned = (char*) (((uintptr_t) buf + 7) & (uintptr_t)~7);
char *base_addr = buf_aligned + sizeof(gc_heap_t);
gc_heap_t *heap = (gc_heap_t*) buf_aligned;
gc_size_t heap_max_size;
@ -60,8 +60,8 @@ gc_handle_t gc_init_with_pool(char *buf, gc_size_t buf_size)
return NULL;
}
base_addr = (char*) (((uintptr_t) base_addr + 7) & ~7) + GC_HEAD_PADDING;
heap_max_size = (buf_end - base_addr) & ~7;
base_addr = (char*) (((uintptr_t) base_addr + 7) & (uintptr_t)~7) + GC_HEAD_PADDING;
heap_max_size = (uint32)(buf_end - base_addr) & (uint32)~7;
memset(heap, 0, sizeof *heap);
memset(base_addr, 0, heap_max_size);
@ -154,7 +154,7 @@ void gci_verify_heap(gc_heap_t *heap)
}
#endif
void* gc_heap_stats(void *heap_arg, int* stats, int size, gc_mm_t mmt)
void* gc_heap_stats(void *heap_arg, uint32* stats, int size, gc_mm_t mmt)
{
(void) mmt;
int i;
@ -175,7 +175,7 @@ void* gc_heap_stats(void *heap_arg, int* stats, int size, gc_mm_t mmt)
stats[i] = heap->total_gc_count;
break;
case GC_STAT_TIME:
stats[i] = (int) heap->total_gc_time;
stats[i] = (uint32)heap->total_gc_time;
break;
default:
break;

View File

@ -3,7 +3,7 @@
include_directories (./include ../include ./${PLATFORM})
add_definitions (-D__POSIX__ -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=199309L -D_BSD_SOURCE)
add_definitions (-D__POSIX__ -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=200809L -D_BSD_SOURCE)
file (GLOB_RECURSE source_all ${PLATFORM}/*.c)
add_library (supportlib ${source_all})

View File

@ -4,14 +4,20 @@
*/
#include "bh_platform.h"
#include "bh_common.h"
#include <stdlib.h>
#include <string.h>
char *bh_strdup(const char *s)
{
uint32 size;
char *s1 = NULL;
if (s && (s1 = bh_malloc(strlen(s) + 1)))
memcpy(s1, s, strlen(s) + 1);
if (s) {
size = (uint32)(strlen(s) + 1);
if ((s1 = bh_malloc(size)))
bh_memcpy_s(s1, size, s, size);
}
return s1;
}

18
core/shared-lib/platform/darwin/bh_platform.c Normal file → Executable file
View File

@ -4,6 +4,7 @@
*/
#include "bh_platform.h"
#include "bh_common.h"
#include <sys/stat.h>
#include <fcntl.h>
@ -11,9 +12,14 @@
char *bh_strdup(const char *s)
{
uint32 size;
char *s1 = NULL;
if (s && (s1 = bh_malloc(strlen(s) + 1)))
memcpy(s1, s, strlen(s) + 1);
if (s) {
size = (uint32)(strlen(s) + 1);
if ((s1 = bh_malloc(size)))
bh_memcpy_s(s1, size, s, size);
}
return s1;
}
@ -23,11 +29,11 @@ int bh_platform_init()
}
char*
bh_read_file_to_buffer(const char *filename, int *ret_size)
bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
{
char *buffer;
int file;
int file_size, read_size;
uint32 file_size, read_size;
struct stat stat_buf;
if (!filename || !ret_size) {
@ -48,7 +54,7 @@ bh_read_file_to_buffer(const char *filename, int *ret_size)
return NULL;
}
file_size = stat_buf.st_size;
file_size = (uint32)stat_buf.st_size;
if (!(buffer = bh_malloc(file_size))) {
printf("Read file to buffer failed: alloc memory failed.\n");
@ -56,7 +62,7 @@ bh_read_file_to_buffer(const char *filename, int *ret_size)
return NULL;
}
read_size = read(file, buffer, file_size);
read_size = (uint32)read(file, buffer, file_size);
close(file);
if (read_size < file_size) {

View File

@ -21,6 +21,7 @@
#include <ctype.h>
#include <pthread.h>
#include <limits.h>
#include <fcntl.h>
#include <semaphore.h>
#include <errno.h>
#include <sys/socket.h>
@ -98,7 +99,7 @@ int b_strcpy_s(char * s1, size_t s1max, const char * s2);
int fopen_s(FILE ** pFile, const char *filename, const char *mode);
char *bh_read_file_to_buffer(const char *filename, int *ret_size);
char *bh_read_file_to_buffer(const char *filename, uint32 *ret_size);
char *bh_strdup(const char *s);

View File

@ -56,7 +56,7 @@ void vm_thread_sys_destroy(void)
typedef struct {
thread_start_routine_t start;
void* stack;
int stack_size;
uint32 stack_size;
void* arg;
} thread_wrapper_arg;
@ -64,7 +64,7 @@ static void *vm_thread_wrapper(void *arg)
{
thread_wrapper_arg * targ = arg;
LOG_VERBOSE("THREAD CREATE 0x%08x\n", &targ);
targ->stack = (void *)((uintptr_t)(&arg) & ~0xfff);
targ->stack = (void *)((uintptr_t)(&arg) & (uintptr_t)~0xfff);
_vm_tls_put(1, targ);
targ->start(targ->arg);
bh_free(targ);
@ -73,7 +73,7 @@ static void *vm_thread_wrapper(void *arg)
}
int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
void *arg, unsigned int stack_size, int prio)
void *arg, unsigned int stack_size, int prio)
{
pthread_attr_t tattr;
thread_wrapper_arg *targ;
@ -114,7 +114,7 @@ int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
}
int _vm_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
unsigned int stack_size)
unsigned int stack_size)
{
return _vm_thread_create_with_prio(tid, start, arg, stack_size,
BH_THREAD_DEFAULT_PRIORITY);
@ -261,7 +261,7 @@ int _vm_sem_wait(korp_sem *sem)
bh_assert(sem);
if (mills == BHT_WAIT_FOREVER) {
if (mills == (int)BHT_WAIT_FOREVER) {
ret = sem_wait(sem);
} else {
@ -330,8 +330,8 @@ static void msec_nsec_to_abstime(struct timespec *ts, int64 msec, int32 nsec)
gettimeofday(&tv, NULL);
ts->tv_sec = tv.tv_sec + msec / 1000;
ts->tv_nsec = tv.tv_usec * 1000 + (msec % 1000) * 1000000 + nsec;
ts->tv_sec = (long int)(tv.tv_sec + msec / 1000);
ts->tv_nsec = (long int)(tv.tv_usec * 1000 + (msec % 1000) * 1000000 + nsec);
if (ts->tv_nsec >= 1000000000L) {
ts->tv_sec++;
@ -344,7 +344,7 @@ int _vm_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, int mills)
int ret;
struct timespec abstime;
if (mills == BHT_WAIT_FOREVER)
if (mills == (int)BHT_WAIT_FOREVER)
ret = pthread_cond_wait(cond, mutex);
else {
msec_nsec_to_abstime(&abstime, mills, 0);

View File

@ -16,7 +16,7 @@
*/
uint64 _bh_time_get_tick_millisecond()
{
return sysconf(_SC_CLK_TCK);
return (uint64)sysconf(_SC_CLK_TCK);
}
/*
@ -30,12 +30,12 @@ uint64 _bh_time_get_boot_millisecond()
return 0;
}
return ((uint64) ts.tv_sec) * 1000 + ts.tv_nsec / (1000 * 1000);
return ((uint64) ts.tv_sec) * 1000 + ((uint64)ts.tv_nsec) / (1000 * 1000);
}
uint32 bh_get_tick_sec()
{
return _bh_time_get_boot_millisecond() / 1000;
return (uint32)(_bh_time_get_boot_millisecond() / 1000);
}
/*
@ -48,12 +48,13 @@ uint64 _bh_time_get_millisecond_from_1970()
ftime(&tp);
return ((uint64) tp.time) * 1000 + tp.millitm
- (tp.dstflag == 0 ? 0 : 60 * 60 * 1000) + tp.timezone * 60 * 1000;
- (tp.dstflag == 0 ? 0 : 60 * 60 * 1000)
+ ((uint64)tp.timezone) * 60 * 1000;
}
size_t _bh_time_strftime(char *s, size_t max, const char *format, int64 time)
{
time_t time_sec = time / 1000;
time_t time_sec = (time_t)(time / 1000);
struct timeb tp;
struct tm *ltp;

View File

@ -52,16 +52,3 @@ int b_strcpy_s(char * s1, size_t s1max, const char * s2)
return 0;
}
int fopen_s(FILE ** pFile, const char *filename, const char *mode)
{
if (NULL == pFile || NULL == filename || NULL == mode) {
return -1;
}
*pFile = fopen(filename, mode);
if (NULL == *pFile)
return -1;
return 0;
}

View File

@ -15,12 +15,35 @@ static bh_print_function_t print_function = NULL;
char *bh_strdup(const char *s)
{
uint32 size;
char *s1 = NULL;
if (s && (s1 = bh_malloc(strlen(s) + 1)))
memcpy(s1, s, strlen(s) + 1);
if (s) {
size = (uint32)(strlen(s) + 1);
if ((s1 = bh_malloc(size)))
bh_memcpy_s(s1, size, s, size);
}
return s1;
}
const unsigned short ** __ctype_b_loc(void)
{
/* TODO */
return NULL;
}
const int32_t ** __ctype_toupper_loc(void)
{
/* TODO */
return NULL;
}
const int32_t ** __ctype_tolower_loc(void)
{
/* TODO */
return NULL;
}
int bh_platform_init()
{
return 0;

View File

@ -14,6 +14,7 @@
#include <assert.h>
#include <time.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
@ -21,6 +22,7 @@
#include <ctype.h>
#include <pthread.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
#ifdef __cplusplus

View File

@ -20,14 +20,14 @@ void vm_thread_sys_destroy(void)
}
int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
void *arg, unsigned int stack_size, int prio)
void *arg, unsigned int stack_size, int prio)
{
return BHT_ERROR;
// return BHT_OK;
}
int _vm_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
unsigned int stack_size)
unsigned int stack_size)
{
return _vm_thread_create_with_prio(tid, start, arg, stack_size,
BH_THREAD_DEFAULT_PRIORITY);

View File

@ -4,6 +4,7 @@
*/
#include "bh_platform.h"
#include "bh_common.h"
#include <sys/stat.h>
#include <fcntl.h>
@ -11,9 +12,14 @@
char *bh_strdup(const char *s)
{
uint32 size;
char *s1 = NULL;
if (s && (s1 = bh_malloc(strlen(s) + 1)))
memcpy(s1, s, strlen(s) + 1);
if (s) {
size = (uint32)(strlen(s) + 1);
if ((s1 = bh_malloc(size)))
bh_memcpy_s(s1, size, s, size);
}
return s1;
}
@ -23,11 +29,11 @@ int bh_platform_init()
}
char*
bh_read_file_to_buffer(const char *filename, int *ret_size)
bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
{
char *buffer;
int file;
int file_size, read_size;
uint32 file_size, read_size;
struct stat stat_buf;
if (!filename || !ret_size) {
@ -48,7 +54,7 @@ bh_read_file_to_buffer(const char *filename, int *ret_size)
return NULL;
}
file_size = stat_buf.st_size;
file_size = (uint32)stat_buf.st_size;
if (!(buffer = bh_malloc(file_size))) {
printf("Read file to buffer failed: alloc memory failed.\n");
@ -56,7 +62,7 @@ bh_read_file_to_buffer(const char *filename, int *ret_size)
return NULL;
}
read_size = read(file, buffer, file_size);
read_size = (uint32)read(file, buffer, file_size);
close(file);
if (read_size < file_size) {

View File

@ -20,8 +20,9 @@
#include <stdarg.h>
#include <ctype.h>
#include <pthread.h>
#include <limits.h>
#include <semaphore.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
@ -98,7 +99,7 @@ int b_strcpy_s(char * s1, size_t s1max, const char * s2);
int fopen_s(FILE ** pFile, const char *filename, const char *mode);
char *bh_read_file_to_buffer(const char *filename, int *ret_size);
char *bh_read_file_to_buffer(const char *filename, uint32 *ret_size);
char *bh_strdup(const char *s);

View File

@ -56,7 +56,7 @@ void vm_thread_sys_destroy(void)
typedef struct {
thread_start_routine_t start;
void* stack;
int stack_size;
uint32 stack_size;
void* arg;
} thread_wrapper_arg;
@ -64,7 +64,7 @@ static void *vm_thread_wrapper(void *arg)
{
thread_wrapper_arg * targ = arg;
LOG_VERBOSE("THREAD CREATE 0x%08x\n", &targ);
targ->stack = (void *)((uintptr_t)(&arg) & ~0xfff);
targ->stack = (void *)((uintptr_t)(&arg) & (uintptr_t)~0xfff);
_vm_tls_put(1, targ);
targ->start(targ->arg);
bh_free(targ);
@ -73,7 +73,7 @@ static void *vm_thread_wrapper(void *arg)
}
int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
void *arg, unsigned int stack_size, int prio)
void *arg, unsigned int stack_size, int prio)
{
pthread_attr_t tattr;
thread_wrapper_arg *targ;
@ -114,7 +114,7 @@ int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
}
int _vm_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
unsigned int stack_size)
unsigned int stack_size)
{
return _vm_thread_create_with_prio(tid, start, arg, stack_size,
BH_THREAD_DEFAULT_PRIORITY);
@ -261,7 +261,7 @@ int _vm_sem_reltimedwait(korp_sem *sem, int mills)
bh_assert(sem);
if (mills == BHT_WAIT_FOREVER) {
if (mills == (int)BHT_WAIT_FOREVER) {
ret = sem_wait(sem);
} else {
@ -329,8 +329,8 @@ static void msec_nsec_to_abstime(struct timespec *ts, int64 msec, int32 nsec)
gettimeofday(&tv, NULL);
ts->tv_sec = tv.tv_sec + msec / 1000;
ts->tv_nsec = tv.tv_usec * 1000 + (msec % 1000) * 1000000 + nsec;
ts->tv_sec = (long int)(tv.tv_sec + msec / 1000);
ts->tv_nsec = (long int)(tv.tv_usec * 1000 + (msec % 1000) * 1000000 + nsec);
if (ts->tv_nsec >= 1000000000L) {
ts->tv_sec++;
@ -343,7 +343,7 @@ int _vm_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, int mills)
int ret;
struct timespec abstime;
if (mills == BHT_WAIT_FOREVER)
if (mills == (int)BHT_WAIT_FOREVER)
ret = pthread_cond_wait(cond, mutex);
else {
msec_nsec_to_abstime(&abstime, mills, 0);

View File

@ -16,7 +16,7 @@
*/
uint64 _bh_time_get_tick_millisecond()
{
return sysconf(_SC_CLK_TCK);
return (uint64)sysconf(_SC_CLK_TCK);
}
/*
@ -30,12 +30,12 @@ uint64 _bh_time_get_boot_millisecond()
return 0;
}
return ((uint64) ts.tv_sec) * 1000 + ts.tv_nsec / (1000 * 1000);
return ((uint64) ts.tv_sec) * 1000 + ((uint64)ts.tv_nsec) / (1000 * 1000);
}
uint32 bh_get_tick_sec()
{
return _bh_time_get_boot_millisecond() / 1000;
return (uint32)(_bh_time_get_boot_millisecond() / 1000);
}
/*
@ -48,12 +48,13 @@ uint64 _bh_time_get_millisecond_from_1970()
ftime(&tp);
return ((uint64) tp.time) * 1000 + tp.millitm
- (tp.dstflag == 0 ? 0 : 60 * 60 * 1000) + tp.timezone * 60 * 1000;
- (tp.dstflag == 0 ? 0 : 60 * 60 * 1000)
+ ((uint64)tp.timezone) * 60 * 1000;
}
size_t _bh_time_strftime(char *s, size_t max, const char *format, int64 time)
{
time_t time_sec = time / 1000;
time_t time_sec = (time_t)(time / 1000);
struct timeb tp;
struct tm *ltp;

View File

@ -41,8 +41,9 @@ int b_strcat_s(char * s1, size_t s1max, const char * s2)
int b_strcpy_s(char * s1, size_t s1max, const char * s2)
{
if (NULL == s1|| NULL == s2
|| s1max < (strlen(s2) + 1) || s1max > RSIZE_MAX) {
if (NULL == s1 || NULL == s2
|| s1max < (strlen(s2) + 1)
|| s1max > RSIZE_MAX) {
return -1;
}

View File

@ -4,6 +4,7 @@
*/
#include "bh_platform.h"
#include "bh_common.h"
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
@ -15,9 +16,14 @@
char *bh_strdup(const char *s)
{
uint32 size;
char *s1 = NULL;
if (s && (s1 = bh_malloc(strlen(s) + 1)))
memcpy(s1, s, strlen(s) + 1);
if (s) {
size = (uint32)(strlen(s) + 1);
if ((s1 = bh_malloc(size)))
bh_memcpy_s(s1, size, s, size);
}
return s1;
}
@ -27,11 +33,11 @@ int bh_platform_init()
}
char*
bh_read_file_to_buffer(const char *filename, int *ret_size)
bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
{
char *buffer;
int file;
int file_size, read_size;
uint32 file_size, read_size;
struct stat stat_buf;
if (!filename || !ret_size) {
@ -52,7 +58,7 @@ bh_read_file_to_buffer(const char *filename, int *ret_size)
return NULL;
}
file_size = stat_buf.st_size;
file_size = (uint32)stat_buf.st_size;
if (!(buffer = bh_malloc(file_size))) {
printf("Read file to buffer failed: alloc memory failed.\n");
@ -60,7 +66,7 @@ bh_read_file_to_buffer(const char *filename, int *ret_size)
return NULL;
}
read_size = read(file, buffer, file_size);
read_size = (uint32)read(file, buffer, file_size);
close(file);
if (read_size < file_size) {

View File

@ -22,6 +22,7 @@
#include <ctype.h>
#include <pthread.h>
#include <limits.h>
#include <fcntl.h>
#include <semaphore.h>
#include <errno.h>
@ -96,7 +97,7 @@ int b_memcpy_s(void * s1, unsigned int s1max, const void * s2,
int b_strcat_s(char * s1, size_t s1max, const char * s2);
int b_strcpy_s(char * s1, size_t s1max, const char * s2);
char *bh_read_file_to_buffer(const char *filename, int *ret_size);
char *bh_read_file_to_buffer(const char *filename, uint32 *ret_size);
char *bh_strdup(const char *s);

View File

@ -56,7 +56,7 @@ void vm_thread_sys_destroy(void)
typedef struct {
thread_start_routine_t start;
void* stack;
int stack_size;
uint32 stack_size;
void* arg;
} thread_wrapper_arg;
@ -64,7 +64,7 @@ static void *vm_thread_wrapper(void *arg)
{
thread_wrapper_arg * targ = arg;
LOG_VERBOSE("THREAD CREATE 0x%08x\n", &targ);
targ->stack = (void *)((uintptr_t)(&arg) & ~0xfff);
targ->stack = (void *)((uintptr_t)(&arg) & (uintptr_t)~0xfff);
_vm_tls_put(1, targ);
targ->start(targ->arg);
bh_free(targ);
@ -73,7 +73,7 @@ static void *vm_thread_wrapper(void *arg)
}
int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
void *arg, unsigned int stack_size, int prio)
void *arg, unsigned int stack_size, int prio)
{
pthread_attr_t tattr;
thread_wrapper_arg *targ;
@ -114,7 +114,7 @@ int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
}
int _vm_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
unsigned int stack_size)
unsigned int stack_size)
{
return _vm_thread_create_with_prio(tid, start, arg, stack_size,
BH_THREAD_DEFAULT_PRIORITY);
@ -261,7 +261,7 @@ int _vm_sem_reltimedwait(korp_sem *sem, int mills)
bh_assert(sem);
if (mills == BHT_WAIT_FOREVER) {
if (mills == (int)BHT_WAIT_FOREVER) {
ret = sem_wait(sem);
} else {
@ -329,8 +329,8 @@ static void msec_nsec_to_abstime(struct timespec *ts, int64 msec, int32 nsec)
gettimeofday(&tv, NULL);
ts->tv_sec = tv.tv_sec + msec / 1000;
ts->tv_nsec = tv.tv_usec * 1000 + (msec % 1000) * 1000000 + nsec;
ts->tv_sec = (long int)(tv.tv_sec + msec / 1000);
ts->tv_nsec = (long int)(tv.tv_usec * 1000 + (msec % 1000) * 1000000 + nsec);
if (ts->tv_nsec >= 1000000000L) {
ts->tv_sec++;
@ -343,7 +343,7 @@ int _vm_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, int mills)
int ret;
struct timespec abstime;
if (mills == BHT_WAIT_FOREVER)
if (mills == (int)BHT_WAIT_FOREVER)
ret = pthread_cond_wait(cond, mutex);
else {
msec_nsec_to_abstime(&abstime, mills, 0);

View File

@ -15,7 +15,7 @@
*/
uint64 _bh_time_get_tick_millisecond()
{
return sysconf(_SC_CLK_TCK);
return (uint64)sysconf(_SC_CLK_TCK);
}
/*
@ -29,12 +29,12 @@ uint64 _bh_time_get_boot_millisecond()
return 0;
}
return ((uint64) ts.tv_sec) * 1000 + ts.tv_nsec / (1000 * 1000);
return ((uint64) ts.tv_sec) * 1000 + ((uint64)ts.tv_nsec) / (1000 * 1000);
}
uint32 bh_get_tick_sec()
{
return _bh_time_get_boot_millisecond() / 1000;
return (uint32)(_bh_time_get_boot_millisecond() / 1000);
}
/*
@ -49,12 +49,12 @@ uint64 _bh_time_get_millisecond_from_1970()
return 0;
}
return ((uint64) ts.tv_sec) * 1000 + ts.tv_nsec / (1000 * 1000);
return ((uint64) ts.tv_sec) * 1000 + ((uint64)ts.tv_nsec) / (1000 * 1000);
}
size_t _bh_time_strftime(char *s, size_t max, const char *format, int64 time)
{
time_t time_sec = time / 1000;
time_t time_sec = (time_t)(time / 1000);
struct tm *ltp;
ltp = localtime(&time_sec);

View File

@ -4,14 +4,20 @@
*/
#include "bh_platform.h"
#include "bh_common.h"
#include <stdlib.h>
#include <string.h>
char *bh_strdup(const char *s)
{
uint32 size;
char *s1 = NULL;
if (s && (s1 = bh_malloc(strlen(s) + 1)))
memcpy(s1, s, strlen(s) + 1);
if (s) {
size = (uint32)(strlen(s) + 1);
if ((s1 = bh_malloc(size)))
bh_memcpy_s(s1, size, s, size);
}
return s1;
}

View File

@ -19,7 +19,9 @@
#include <limits.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#ifndef CONFIG_NET_BUF_USER_DATA_SIZE
#define CONFIG_NET_BUF_USER_DATA_SIZE 0
#endif

View File

@ -1,6 +0,0 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
include (${CMAKE_CURRENT_LIST_DIR}/utils/shared_utils.cmake)
include (${CMAKE_CURRENT_LIST_DIR}/mem-alloc/mem_alloc.cmake)
include (${CMAKE_CURRENT_LIST_DIR}/platform/${PLATFORM}/shared_platform.cmake)

View File

@ -94,7 +94,7 @@ void _bh_log_vprintf(const char *fmt, va_list ap)
if (fmt && (cur_log_enabled = is_log_enabled(fmt))) {
char buf[32];
bh_time_strftime(buf, 32, "%Y-%m-%d %H:%M:%S",
bh_time_get_millisecond_from_1970());
(int64)bh_time_get_millisecond_from_1970());
bh_log_emit_helper("\n[%s - %X]: ", buf, (int) self);
/* Strip the "Vn." prefix. */

View File

@ -13,7 +13,7 @@ typedef struct _bh_queue_node {
struct _bh_queue_node * next;
struct _bh_queue_node * prev;
unsigned short tag;
unsigned long len;
unsigned int len;
void * body;
bh_msg_cleaner msg_cleaner;
} bh_queue_node;
@ -35,7 +35,7 @@ char * bh_message_payload(bh_message_t message)
return message->body;
}
int bh_message_payload_len(bh_message_t message)
uint32 bh_message_payload_len(bh_message_t message)
{
return message->len;
}
@ -230,7 +230,7 @@ void bh_queue_enter_loop_run(bh_queue *queue,
return;
while (!queue->exit_loop_run) {
bh_queue_node * message = bh_get_msg(queue, BH_WAIT_FOREVER);
bh_queue_node * message = bh_get_msg(queue, (int)BH_WAIT_FOREVER);
if (message) {
handle_cb(message, arg);

View File

@ -55,7 +55,7 @@ uint32 bh_get_elpased_ms(uint32 * last_system_clock)
}
static app_timer_t * remove_timer_from(timer_ctx_t ctx, uint32 timer_id,
bool active_list)
bool active_list)
{
vm_mutex_lock(&ctx->mutex);
app_timer_t ** head;
@ -94,7 +94,7 @@ static app_timer_t * remove_timer_from(timer_ctx_t ctx, uint32 timer_id,
}
static app_timer_t * remove_timer(timer_ctx_t ctx, uint32 timer_id,
bool * active)
bool * active)
{
app_timer_t* t = remove_timer_from(ctx, timer_id, true);
if (t) {
@ -196,8 +196,8 @@ void release_timer_list(app_timer_t ** p_list)
*/
timer_ctx_t create_timer_ctx(timer_callback_f timer_handler,
check_timer_expiry_f expiery_checker, int prealloc_num,
unsigned int owner)
check_timer_expiry_f expiery_checker, int prealloc_num,
unsigned int owner)
{
timer_ctx_t ctx = (timer_ctx_t) bh_malloc(sizeof(struct _timer_ctx));
if (ctx == NULL)
@ -252,19 +252,6 @@ void destroy_timer_ctx(timer_ctx_t ctx)
bh_free(ctx);
}
void timer_ctx_set_lock(timer_ctx_t ctx, bool lock)
{
if (lock)
vm_mutex_lock(&ctx->mutex);
else
vm_mutex_unlock(&ctx->mutex);
}
void * timer_ctx_get_lock(timer_ctx_t ctx)
{
return &ctx->mutex;
}
unsigned int timer_ctx_get_owner(timer_ctx_t ctx)
{
return ctx->owner;
@ -279,14 +266,14 @@ void add_idle_timer(timer_ctx_t ctx, app_timer_t * timer)
}
uint32 sys_create_timer(timer_ctx_t ctx, int interval, bool is_period,
bool auto_start)
bool auto_start)
{
app_timer_t *timer;
if (ctx->pre_allocated) {
if (ctx->free_timers == NULL)
return -1;
return (uint32)-1;
else {
timer = ctx->free_timers;
ctx->free_timers = timer->next;
@ -294,16 +281,16 @@ uint32 sys_create_timer(timer_ctx_t ctx, int interval, bool is_period,
} else {
timer = (app_timer_t*) bh_malloc(sizeof(app_timer_t));
if (timer == NULL)
return -1;
return (uint32)-1;
}
memset(timer, 0, sizeof(*timer));
ctx->g_max_id++;
if (ctx->g_max_id == -1)
if (ctx->g_max_id == (uint32)-1)
ctx->g_max_id++;
timer->id = ctx->g_max_id;
timer->interval = interval;
timer->interval = (uint32)interval;
timer->is_periodic = is_period;
if (auto_start)
@ -347,7 +334,7 @@ bool sys_timer_restart(timer_ctx_t ctx, uint32 timer_id, int interval)
return false;
if (interval > 0)
t->interval = interval;
t->interval = (uint32)interval;
reschedule_timer(ctx, t);
@ -393,7 +380,7 @@ int get_expiry_ms(timer_ctx_t ctx)
if (ctx->g_app_timers == NULL)
ms_to_next_expiry = 7 * 24 * 60 * 60 * 1000; // 1 week
else if (ctx->g_app_timers->expiry >= now)
ms_to_next_expiry = ctx->g_app_timers->expiry - now;
ms_to_next_expiry = (int)(ctx->g_app_timers->expiry - now);
else
ms_to_next_expiry = 0;
vm_mutex_unlock(&ctx->mutex);

View File

@ -22,8 +22,6 @@ typedef void (*check_timer_expiry_f)(timer_ctx_t ctx);
timer_ctx_t create_timer_ctx(timer_callback_f timer_handler,
check_timer_expiry_f, int prealloc_num, unsigned int owner);
void destroy_timer_ctx(timer_ctx_t);
void timer_ctx_set_lock(timer_ctx_t ctx, bool lock);
void * timer_ctx_get_lock(timer_ctx_t ctx);
unsigned int timer_ctx_get_owner(timer_ctx_t ctx);
uint32 sys_create_timer(timer_ctx_t ctx, int interval, bool is_period,