Skip to content

Commit

Permalink
Merge pull request #333 from cruise2018/iot_link
Browse files Browse the repository at this point in the history
clean:code:clean the code for CodeDex
  • Loading branch information
cruise2018 authored Apr 2, 2020
2 parents 7a764ec + dd0172c commit 0d71cf7
Show file tree
Hide file tree
Showing 32 changed files with 322 additions and 150 deletions.
8 changes: 6 additions & 2 deletions demos/oc_mqtt_demo/oc_mqtt_v5_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ static const char s_server_ca[] =

#define CN_EP_DEVICEID "54f107da-f251-436c-af4c-624f33b7d7b6"
#define CN_EP_PASSWD "f62fcf47d62c4ed18913"

///< the client use the cert mode SHA-256
//72DC0E75D88CEC38A025E9C48C79D222F608B039D080BCEFC0007DAD1AFFAD00
//#define CN_EP_DEVICEID "5d0c76788a48f95ac41bcb9c_ca002"

#define CN_BOOT_MODE 0
#define CN_LIFE_TIME 60 ///< the platform need more
//if your command is very fast,please use a queue here--TODO
Expand Down Expand Up @@ -331,15 +336,14 @@ static int task_reportmsg_entry(void *args)

connect_para.boostrap = CN_BOOT_MODE;
connect_para.device_id = CN_EP_DEVICEID;
connect_para.device_passwd = CN_EP_PASSWD;
connect_para.device_passwd = NULL;
connect_para.server_addr = CN_SERVER_IPV4;
connect_para.sevver_port = CN_SERVER_PORT;
connect_para.life_time = CN_LIFE_TIME;
connect_para.rcvfunc = app_msg_deal;

connect_para.security.type = CN_SECURITY_TYPE;


connect_para.security.u.cert.server_ca = (uint8_t *)s_server_ca;
connect_para.security.u.cert.server_ca_len = sizeof(s_server_ca);

Expand Down
4 changes: 3 additions & 1 deletion iot_link/at/at.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ static int __rcv_task_entry(void *args)
return 0;
}

while(1)
while(NULL != g_at_cb.devhandle)
{
if (1 == g_at_cb.streammode) //in stream mode, we need to save previous frames in buffer
{
Expand Down Expand Up @@ -315,6 +315,8 @@ static int __rcv_task_entry(void *args)
}
}
}

return 0;
}

/*******************************************************************************
Expand Down
15 changes: 7 additions & 8 deletions iot_link/inc/link_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <stdarg.h>
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>


/**
Expand Down Expand Up @@ -100,22 +101,20 @@ int link_log_level_set(en_link_log_level_t level);
* call LINK_LOG groups
*
* */
__attribute__((weak)) void link_printf(const char *format, ...);
//__attribute__((weak)) void link_printf(const char *format, ...);



#ifndef CONFIG_LINK_DEBUG_ENABLE
#define CONFIG_LINK_DEBUG_ENABLE 1 ///< default the debug is on
#ifndef link_printf
#define link_printf printf
#endif

#if CONFIG_LINK_DEBUG_ENABLE
#ifdef CONFIG_LINK_DEBUG_ENABLE

extern unsigned long long osal_sys_time();
#define LINK_LOG(level,fmt, ...) \
do \
{ \
link_printf("[%s][%u][%s:%d] " fmt "\r\n", \
link_log_level_name((level)), (uint32_t)osal_sys_time(), __FUNCTION__, __LINE__, ##__VA_ARGS__); \
(void)link_printf("[%s][%u][%s:%d] " fmt "\r\n", \
link_log_level_name((level)), (unsigned int)osal_sys_time(), __FUNCTION__, __LINE__, ##__VA_ARGS__); \
} while (0)

#define LINK_LOG_TRACE(fmt, ...) \
Expand Down
6 changes: 5 additions & 1 deletion iot_link/link_log/link_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ const char *link_log_level_name(en_link_log_level_t log_level)

}

/*
#ifndef CONFIG_LINK_LOGBUF_LEN
#define CONFIG_LINK_LOGBUF_LEN 256 ///< you could modify it
#endif
Expand All @@ -93,10 +95,12 @@ __attribute__((weak)) void link_printf(const char *format, ...)
(void) (void) memset(str_buf, 0, CONFIG_LINK_LOGBUF_LEN);
va_start(list, format);
(void) vsnprintf(str_buf, CONFIG_LINK_LOGBUF_LEN, format, list);
(void) vsnprintf(str_buf, sizeof(str_buf), format, list);
va_end(list);
(void) printf("%s", str_buf);
return;
}
*/
19 changes: 13 additions & 6 deletions iot_link/link_log/link_log.mk
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
################################################################################
# this is used for compile the iotlink
################################################################################
log_src = ${wildcard $(iot_link_root)/link_log/*.c}
C_SOURCES += $(log_src)

log_inc = -I $(iot_link_root)/link_log
C_INCLUDES += $(log_inc)

C_DEFS += -D CONFIG_LINK_DEBUG_ENABLE=1
ifeq ($(CONFIG_LOG_ENABLE), y)

log_src = ${wildcard $(iot_link_root)/link_log/*.c}
C_SOURCES += $(log_src)

log_inc = -I $(iot_link_root)/link_log
C_INCLUDES += $(log_inc)

C_DEFS += -D CONFIG_LINK_DEBUG_ENABLE=1

endif





29 changes: 18 additions & 11 deletions iot_link/link_misc/link_ring_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ int ring_buffer_write(tag_ring_buffer_t *ring,unsigned char *buf, int len)
{
return ret;//which means parameters error
}
if(ring->datalen == ring->buflen)
if((ring->datalen == ring->buflen)|| (ring->buflen <= 0))
{
ret = 0;
return ret;//which means you could copy nothing here
Expand All @@ -85,9 +85,12 @@ int ring_buffer_write(tag_ring_buffer_t *ring,unsigned char *buf, int len)
offset = (ring->dataoff+ring->datalen)%ring->buflen; //we could move it one time
cpylen = lenleft;
dst = ring->buf + offset;
(void) memcpy(dst,src,cpylen);
ring->datalen += cpylen;
lenleft -= cpylen;
if(cpylen > 0)
{
(void) memcpy(dst,src,cpylen);
ring->datalen += cpylen;
lenleft -= cpylen;
}
}
else if((ring->dataoff+ring->datalen + lenleft)>ring->buflen) //which means the data will be roll back
{
Expand Down Expand Up @@ -125,7 +128,7 @@ int ring_buffer_read(tag_ring_buffer_t *ring,unsigned char *buf, int len)
{
return ret;//which means parameters error
}
if(ring->datalen == 0)
if((ring->datalen == 0) || (ring->buflen <= 0))
{
ret = 0;
return ret;//which means you could copy nothing here
Expand All @@ -134,16 +137,20 @@ int ring_buffer_read(tag_ring_buffer_t *ring,unsigned char *buf, int len)
//now let us think the method to fill the data,take care of the roll back
lenleft = ret;
dst = buf;
if((ring->dataoff+ lenleft)>=ring->buflen) //which means the data has roll back
if(ring->dataoff >= (ring->buflen - lenleft)) //which means the data has roll back
{
offset =ring->dataoff; //we cpy part
cpylen = ring->buflen - ring->dataoff;
src = ring->buf + offset;
(void) memcpy(dst,src,cpylen);
ring->dataoff = (ring->dataoff + cpylen)%ring->buflen;
ring->datalen -= cpylen;
lenleft -= cpylen;
dst += cpylen;

if(cpylen > 0)
{
(void) memcpy(dst,src,cpylen);
ring->dataoff = (ring->dataoff + cpylen)%ring->buflen;
ring->datalen -= cpylen;
lenleft -= cpylen;
dst += cpylen;
}
}
//here means we could move it by one time
if(lenleft > 0)
Expand Down
61 changes: 60 additions & 1 deletion iot_link/link_misc/link_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ char *osal_strcat(char *str[])
return ret;
}


/*
int hexstr2byte(const char *bufin, int len, char *bufout)
{
int i = 0;
Expand All @@ -169,6 +169,65 @@ int hexstr2byte(const char *bufin, int len, char *bufout)
return 0;
}
*/

int hexstr2byte(const char *buf, int len, char *bufout)
{
int ret = -1;
int i = 0;
uint8_t low;
uint8_t high;

if (NULL == buf || len <= 0 || NULL == bufout)
{
return ret;
}

ret = 0;
for(i = 0; i < len; i = i+2)
{
if(((buf[i]) >= '0') && (buf[i] <= '9'))
{
high = (uint8_t)( buf[i] - '0');
}
else if((buf[i] >= 'A') && (buf[i] <= 'F'))
{
high = (uint8_t)( buf[i] - 'A') + 10;
}
else if((buf[i] >= 'a') && (buf[i] <= 'f'))
{
high = (uint8_t)( buf[i] - 'a') + 10;
}
else
{
ret = -1;
break;
}

if(((buf[i+1]) >= '0') && (buf[i+1] <= '9'))
{
low = (uint8_t)( buf[i+1] - '0');
}
else if((buf[i+1] >= 'A') && (buf[i+1] <= 'F'))
{
low = (uint8_t)( buf[i+1] - 'A') + 10;
}
else if((buf[i+1] >= 'a') && (buf[i+1] <= 'f'))
{
low = (uint8_t)( buf[i+1] - 'a') + 10;
}
else
{
ret = -1;
break;
}

bufout[i/2] = (char)((high<<4)|(low&0x0F));
}
return ret;
}



//make a byte to 2 ascii hex
int byte2hexstr(uint8_t *bufin, int len, char *bufout)
Expand Down
9 changes: 2 additions & 7 deletions iot_link/network/dtls/mbedtls/mbedtls_port/dtls_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@

mbedtls_ssl_context *dtls_ssl_new(dtls_establish_info_s *info, char plat_type)
{
int ret;
int ret = -1;
mbedtls_ssl_context *ssl = NULL;
mbedtls_ssl_config *conf = NULL;
mbedtls_entropy_context *entropy = NULL;
Expand Down Expand Up @@ -153,12 +153,7 @@ mbedtls_ssl_context *dtls_ssl_new(dtls_establish_info_s *info, char plat_type)

MBEDTLS_LOG("setting up the SSL structure");

ret = mbedtls_ssl_config_defaults(conf,plat_type,transport,MBEDTLS_SSL_PRESET_DEFAULT);
if (ret != 0)
{
MBEDTLS_LOG("mbedtls_ssl_config_defaults failed: -0x%x", -ret);
goto exit_fail;
}
(void) mbedtls_ssl_config_defaults(conf,plat_type,transport,MBEDTLS_SSL_PRESET_DEFAULT);

mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_REQUIRED);
mbedtls_ssl_conf_rng(conf, mbedtls_ctr_drbg_random, ctr_drbg);
Expand Down
2 changes: 1 addition & 1 deletion iot_link/network/dtls/mbedtls/mbedtls_port/mbed_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ en_dtls_al_err_t mbed_new(dtls_al_para_t *para, void **handle)

en_dtls_al_err_t mbed_destroy(void *handle)
{
int ret = EN_DTLS_AL_ERR_PARA;
en_dtls_al_err_t ret = EN_DTLS_AL_ERR_PARA;
if(NULL != handle)
{
dtls_ssl_destroy(handle);
Expand Down
4 changes: 2 additions & 2 deletions iot_link/network/lwm2m/wakaama_lwm2m/port/lwm2m_rpt.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ static void lwm2m_notify_stack_rpt_data_change(lwm2m_dl_list *node, void *contex

if (!lwm2m_list_empty(&rpt_list->rpt_list))
{
ATINY_LOG(LOG_INFO, "data change cnt %d "URI_FORMAT, rpt_list->rpt_node_cnt, URI_LOG_PARAM(&rpt_list->uri));
ATINY_LOG(LOG_INFO, "data change cnt %d "URI_FORMAT, (int)rpt_list->rpt_node_cnt, URI_LOG_PARAM(&rpt_list->uri));
lwm2m_resource_value_changed(context, &rpt_list->uri);
}
}
Expand Down Expand Up @@ -311,7 +311,7 @@ int lwm2m_queue_rpt_data(const lwm2m_uri_t *uri, const data_report_t *data)
if (rpt_list->rpt_node_cnt >= rpt_list->max_cnt)
{
ATINY_LOG(LOG_INFO, "uri rpt exceed rpt cnt %d max cnt %d," URI_FORMAT,
rpt_list->rpt_node_cnt, rpt_list->max_cnt, URI_LOG_PARAM(uri));
(int)rpt_list->rpt_node_cnt, (int)rpt_list->max_cnt, URI_LOG_PARAM(uri));
ret = LWM2M_RESOURCE_NOT_ENOUGH;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion iot_link/network/lwm2m/wakaama_lwm2m/port/lwm2m_rpt.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ extern "C" {
#endif

#define URI_FORMAT "uri(flag:0x%x, objId:%d, instId:%d, resId:%d)"
#define URI_LOG_PARAM(uri) (uri)->flag, (uri)->objectId, (uri)->instanceId, (uri)->resourceId
#define URI_LOG_PARAM(uri) (uri)->flag, (int)(uri)->objectId, (int)(uri)->instanceId, (int)(uri)->resourceId

LWM2M_INLINE void get_instance_uri(uint16_t object_id, uint16_t instance_id, lwm2m_uri_t *uri)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ static uint8_t prv_read(uint16_t instanceId,

if (NULL == targetP)
{
ATINY_LOG(LOG_ERR, "plat inst not found %d", instanceId);
ATINY_LOG(LOG_ERR, "plat inst not found %d", (int)instanceId);
return COAP_404_NOT_FOUND;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ static uint8_t prv_firmware_write(uint16_t instanceId,

if ((LWM2M_TYPE_STRING != dataArray[i].type) || (NULL == dataArray[i].value.asBuffer.buffer))
{
ATINY_LOG(LOG_ERR, "type ERR %d", dataArray[i].type);
ATINY_LOG(LOG_ERR, "type ERR %d", (int)dataArray[i].type);
result = COAP_400_BAD_REQUEST;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,16 @@

#ifdef LWM2M_WITH_LOGS
#include <inttypes.h>
#define LOG(STR) lwm2m_printf("[%d][%s:%d] " STR "\r\n", (uint32_t)lwm2m_gettime(), __func__ , __LINE__)
#define LOG_ARG(FMT, ...) lwm2m_printf("[%d][%s:%d] " FMT "\r\n", (uint32_t)lwm2m_gettime(), __func__ , __LINE__ , __VA_ARGS__)
#define LOG(STR) lwm2m_printf("[%d][%s:%d] " STR "\r\n", (int)lwm2m_gettime(), __func__ , __LINE__)
#define LOG_ARG(FMT, ...) lwm2m_printf("[%d][%s:%d] " FMT "\r\n", (int)lwm2m_gettime(), __func__ , __LINE__ , __VA_ARGS__)
#define LOG_URI(URI) \
{ \
if ((URI) == NULL) lwm2m_printf("[%s:%d] NULL\r\n", __func__ , __LINE__); \
else \
{ \
lwm2m_printf("[%s:%d] /%d", __func__ , __LINE__ , (URI)->objectId); \
if (LWM2M_URI_IS_SET_INSTANCE(URI)) lwm2m_printf("/%d", (URI)->instanceId); \
if (LWM2M_URI_IS_SET_RESOURCE(URI)) lwm2m_printf("/%d", (URI)->resourceId); \
lwm2m_printf("[%s:%d] /%d", __func__ , __LINE__ , (int)(URI)->objectId); \
if (LWM2M_URI_IS_SET_INSTANCE(URI)) lwm2m_printf("/%d",(int)(URI)->instanceId); \
if (LWM2M_URI_IS_SET_RESOURCE(URI)) lwm2m_printf("/%d", (int)(URI)->resourceId); \
lwm2m_printf("\r\n"); \
} \
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,8 @@ void lwm2m_setBsCtrlStat(lwm2m_context_t *contextP, lwm2m_client_state_t state)
if ((oldState != contextP->bsCtrl.state)
|| (oldCnt != contextP->bsCtrl.cnt))
{
LOG_ARG("bsctrlstat (%d,%d) to (%d,%d)", oldState, oldCnt,
contextP->bsCtrl.state, contextP->bsCtrl.cnt);
LOG_ARG("bsctrlstat (%d,%d) to (%d,%d)",(int)oldState, (int)oldCnt,
(int)contextP->bsCtrl.state,(int)contextP->bsCtrl.cnt);
}

}
Expand Down
Loading

0 comments on commit 0d71cf7

Please sign in to comment.