update qcloud sdk

1. iot-hub sdk update to 3.2.0
2. iot-explorer update to 3.1.1
This commit is contained in:
daishengdong
2020-05-07 11:11:04 +08:00
parent c8e39739d3
commit 3e631cd96a
594 changed files with 47287 additions and 44165 deletions

View File

@@ -0,0 +1,88 @@
/*
* Copyright (c) 2019-2021 Tencent Group. All rights reserved.
* License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "qcloud_iot_import.h"
#include "qcloud_iot_export.h"
#ifdef AT_TCP_ENABLED
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "utils_ringbuff.h"
#include "at_client.h"
#include "stm32l4xx_hal.h"
#define HAL_AT_UART_IRQHandler USART1_IRQHandler
extern UART_HandleTypeDef huart1;
static UART_HandleTypeDef *pAtUart = &huart1;
extern void AT_Uart_Init(void);
extern void at_client_uart_rx_isr_cb(uint8_t *pdata, uint8_t len);
#include "board.h"
/**
* @brief This function handles AT UART global interrupt,push recv char to ringbuff.
*/
void HAL_AT_UART_IRQHandler(void)
{
uint8_t ch;
if(__HAL_UART_GET_FLAG(pAtUart, UART_FLAG_RXNE) == SET)
{
ch = (uint8_t) READ_REG(pAtUart->Instance->RDR)&0xFF;
/*this callback for at_client*/
at_client_uart_rx_isr_cb(&ch, 1);
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
}
__HAL_UART_CLEAR_PEFLAG(pAtUart);
}
/**
*pdata: pointer of data for send
*len: len of data to be sent
*return: the len of data send success
* @brief hal api for at data send
*/
int HAL_AT_Uart_Send(void *data, uint32_t size)
{
if(HAL_OK == HAL_UART_Transmit(pAtUart, data, size, 0xFFFF))
{
return size;
}
else
{
return 0;
}
}
int HAL_AT_Uart_Init(void)
{
AT_Uart_Init();
return QCLOUD_RET_SUCCESS;
}
int HAL_AT_Uart_Deinit(void)
{
return QCLOUD_RET_SUCCESS;
}
#endif

View File

@@ -0,0 +1,164 @@
/*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "qcloud_iot_import.h"
#include "qcloud_iot_export.h"
#include "utils_param_check.h"
/* Enable this macro (also control by cmake) to use static string buffer to store device info */
/* To use specific storing methods like files/flash, disable this macro and implement dedicated methods */
#define DEBUG_DEV_INFO_USED
#ifdef DEBUG_DEV_INFO_USED
/* product Id */
static char sg_product_id[MAX_SIZE_OF_PRODUCT_ID + 1] = "Q0P18350R9";
/* device name */
static char sg_device_name[MAX_SIZE_OF_DEVICE_NAME + 1] = "new_sdk_dev001";
#ifdef DEV_DYN_REG_ENABLED
/* product secret for device dynamic Registration */
static char sg_product_secret[MAX_SIZE_OF_PRODUCT_SECRET + 1] = "YOUR_PRODUCT_SECRET";
#endif
#ifdef AUTH_MODE_CERT
/* public cert file name of certificate device */
static char sg_device_cert_file_name[MAX_SIZE_OF_DEVICE_CERT_FILE_NAME + 1] = "YOUR_DEVICE_NAME_cert.crt";
/* private key file name of certificate device */
static char sg_device_privatekey_file_name[MAX_SIZE_OF_DEVICE_SECRET_FILE_NAME + 1] = "YOUR_DEVICE_NAME_private.key";
#else
/* device secret of PSK device */
static char sg_device_secret[MAX_SIZE_OF_DEVICE_SECRET + 1] = "sgwkhdJ2d1l5zvmxMmpAuA==";
#endif
#ifdef GATEWAY_ENABLED
/* sub-device product id */
static char sg_sub_device_product_id[MAX_SIZE_OF_PRODUCT_ID + 1] = "PRODUCT_ID";
/* sub-device device name */
static char sg_sub_device_name[MAX_SIZE_OF_DEVICE_NAME + 1] = "YOUR_SUB_DEV_NAME";
#endif
static int device_info_copy(void *pdst, void *psrc, uint8_t max_len)
{
if(strlen(psrc) > max_len){
return QCLOUD_ERR_FAILURE;
}
memset(pdst, '\0', max_len);
strncpy(pdst, psrc, max_len);
return QCLOUD_RET_SUCCESS;
}
#endif
int HAL_SetDevInfo(void *pdevInfo)
{
POINTER_SANITY_CHECK(pdevInfo, QCLOUD_ERR_DEV_INFO);
int ret;
DeviceInfo *devInfo = (DeviceInfo *)pdevInfo;
#ifdef DEBUG_DEV_INFO_USED
ret = device_info_copy(sg_product_id, devInfo->product_id, MAX_SIZE_OF_PRODUCT_ID);//set product ID
ret |= device_info_copy(sg_device_name, devInfo->device_name, MAX_SIZE_OF_DEVICE_NAME);//set dev name
#ifdef AUTH_MODE_CERT
ret |= device_info_copy(sg_device_cert_file_name, devInfo->dev_cert_file_name, MAX_SIZE_OF_DEVICE_CERT_FILE_NAME);//set dev cert file name
ret |= device_info_copy(sg_device_privatekey_file_name, devInfo->dev_key_file_name, MAX_SIZE_OF_DEVICE_SECRET_FILE_NAME);//set dev key file name
#else
ret |= device_info_copy(sg_device_secret, devInfo->device_secret, MAX_SIZE_OF_DEVICE_SECRET);//set dev secret
#endif
#else
Log_e("HAL_SetDevInfo not implement yet");
ret = QCLOUD_ERR_DEV_INFO;
#endif
if(QCLOUD_RET_SUCCESS != ret) {
Log_e("Set device info err");
ret = QCLOUD_ERR_DEV_INFO;
}
return ret;
}
int HAL_GetDevInfo(void *pdevInfo)
{
POINTER_SANITY_CHECK(pdevInfo, QCLOUD_ERR_DEV_INFO);
int ret;
DeviceInfo *devInfo = (DeviceInfo *)pdevInfo;
memset((char *)devInfo, '\0', sizeof(DeviceInfo));
#ifdef DEBUG_DEV_INFO_USED
ret = device_info_copy(devInfo->product_id, sg_product_id, MAX_SIZE_OF_PRODUCT_ID);//get product ID
ret |= device_info_copy(devInfo->device_name, sg_device_name, MAX_SIZE_OF_DEVICE_NAME);//get dev name
#ifdef DEV_DYN_REG_ENABLED
ret |= device_info_copy(devInfo->product_secret, sg_product_secret, MAX_SIZE_OF_PRODUCT_SECRET );//get product ID
#endif
#ifdef AUTH_MODE_CERT
ret |= device_info_copy(devInfo->dev_cert_file_name, sg_device_cert_file_name, MAX_SIZE_OF_DEVICE_CERT_FILE_NAME);//get dev cert file name
ret |= device_info_copy(devInfo->dev_key_file_name, sg_device_privatekey_file_name, MAX_SIZE_OF_DEVICE_SECRET_FILE_NAME);//get dev key file name
#else
ret |= device_info_copy(devInfo->device_secret, sg_device_secret, MAX_SIZE_OF_DEVICE_SECRET);//get dev secret
#endif
#else
Log_e("HAL_GetDevInfo not implement yet");
ret = QCLOUD_ERR_DEV_INFO;
#endif
if(QCLOUD_RET_SUCCESS != ret){
Log_e("Get device info err");
ret = QCLOUD_ERR_DEV_INFO;
}
return ret;
}
#ifdef GATEWAY_ENABLED
int HAL_GetGwDevInfo(void *pgwDeviceInfo)
{
POINTER_SANITY_CHECK(pgwDeviceInfo, QCLOUD_ERR_DEV_INFO);
int ret;
GatewayDeviceInfo *gwDevInfo = (GatewayDeviceInfo *)pgwDeviceInfo;
memset((char *)gwDevInfo, 0, sizeof(GatewayDeviceInfo));
#ifdef DEBUG_DEV_INFO_USED
ret = HAL_GetDevInfo(&(gwDevInfo->gw_info));//get gw dev info
//only one sub-device is supported now
gwDevInfo->sub_dev_num = 1;
gwDevInfo->sub_dev_info = (DeviceInfo *)HAL_Malloc(sizeof(DeviceInfo)*(gwDevInfo->sub_dev_num));
if(gwDevInfo->sub_dev_info)
{
memset((char *)gwDevInfo->sub_dev_info, '\0', sizeof(DeviceInfo));
//copy sub dev info
ret = device_info_copy(gwDevInfo->sub_dev_info->product_id, sg_sub_device_product_id, MAX_SIZE_OF_PRODUCT_ID);
ret |= device_info_copy(gwDevInfo->sub_dev_info->device_name, sg_sub_device_name, MAX_SIZE_OF_DEVICE_NAME);
}
#else
Log_e("HAL_GetGwDevInfo not implement yet");
ret = QCLOUD_ERR_DEV_INFO;
#endif
if(QCLOUD_RET_SUCCESS != ret) {
Log_e("Get gateway device info err");
ret = QCLOUD_ERR_DEV_INFO;
}
return ret;
}
#endif

View File

@@ -0,0 +1,187 @@
/*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "qcloud_iot_import.h"
#include "tos_k.h"
//#define PLATFORM_HAS_CMSIS
#ifdef PLATFORM_HAS_CMSIS
#include "cmsis_os.h"
#include "stm32l4xx_hal.h"
#endif
//TODO platform dependant
void HAL_SleepMs(_IN_ uint32_t ms)
{
(void)tos_sleep_hmsm(0,0,0, ms);
}
void HAL_Printf(_IN_ const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
fflush(stdout);
}
int HAL_Snprintf(_IN_ char *str, const int len, const char *fmt, ...)
{
va_list args;
int rc;
va_start(args, fmt);
rc = vsnprintf(str, len, fmt, args);
va_end(args);
return rc;
}
int HAL_Vsnprintf(_IN_ char *str, _IN_ const int len, _IN_ const char *format, va_list ap)
{
return vsnprintf(str, len, format, ap);
}
void *HAL_Malloc(_IN_ uint32_t size)
{
return tos_mmheap_alloc(size);
}
void HAL_Free(_IN_ void *ptr)
{
tos_mmheap_free(ptr);
}
void *HAL_MutexCreate(void)
{
k_mutex_t *mutex;
mutex = (k_mutex_t *)HAL_Malloc(sizeof(k_mutex_t));
if (!mutex) {
return K_NULL;
}
tos_mutex_create(mutex);
return (void *)mutex;
}
void HAL_MutexDestroy(_IN_ void *mutex)
{
k_err_t ret;
if (K_ERR_NONE != (ret = tos_mutex_destroy((k_mutex_t *)mutex))) {
HAL_Printf("osal_mutex_destroy err, err:%d\n\r", ret);
} else {
HAL_Free((void *)mutex);
}
}
void HAL_MutexLock(_IN_ void *mutex)
{
k_err_t ret;
if (K_ERR_NONE != (ret = tos_mutex_pend((k_mutex_t *)mutex))) {
HAL_Printf("osal_mutex_lock err, err:%d\n\r", ret);
}
}
int HAL_MutexTryLock(_IN_ void *mutex)
{
k_err_t ret;
if (K_ERR_NONE != (ret = tos_mutex_pend_timed((k_mutex_t *)mutex, 0))) {
HAL_Printf("osal_mutex_lock err, err:%d\n\r", ret);
return (int)ret;
}
return 0;
}
void HAL_MutexUnlock(_IN_ void *mutex)
{
k_err_t ret;
if (K_ERR_NONE != (ret = tos_mutex_post((k_mutex_t *)mutex))) {
HAL_Printf("osal_mutex_unlock err, err:%d\n\r", ret);
}
}
#if defined(PLATFORM_HAS_CMSIS) && defined(AT_TCP_ENABLED)
/*
* return void* threadId
*/
void * HAL_ThreadCreate(uint16_t stack_size, int priority, char * taskname,void *(*fn)(void*), void* arg)
{
osThreadId thread_t = (osThreadId)HAL_Malloc(sizeof(osThreadId));
osThreadDef(taskname, (os_pthread)fn, (osPriority)priority, 0, stack_size);
thread_t = osThreadCreate(osThread(taskname), arg);
if(NULL == thread_t){
HAL_Printf("create thread fail\n\r");
}
return (void *)thread_t;
}
int HAL_ThreadDestroy(void* threadId)
{
return osThreadTerminate(threadId);
}
void *HAL_SemaphoreCreate(void)
{
return (void *)osSemaphoreCreate(NULL, 1);
}
void HAL_SemaphoreDestroy(void *sem)
{
osStatus ret;
ret = osSemaphoreDelete ((osSemaphoreId)sem);
if(osOK != ret)
{
HAL_Printf("HAL_SemaphoreDestroy err, err:%d\n\r",ret);
}
}
void HAL_SemaphorePost(void *sem)
{
osStatus ret;
ret = osSemaphoreRelease ((osSemaphoreId) sem);
if(osOK != ret)
{
HAL_Printf("HAL_SemaphorePost err, err:%d\n\r",ret);
}
}
int HAL_SemaphoreWait(void *sem, uint32_t timeout_ms)
{
return osSemaphoreWait ((osSemaphoreId)sem, timeout_ms);
}
#endif

View File

@@ -0,0 +1,272 @@
/*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "lwip/sockets.h"
#include "lwip/netdb.h"
#include "lwip/inet.h"
#include "qcloud_iot_import.h"
#include "qcloud_iot_export_log.h"
#include "qcloud_iot_export_error.h"
#include "qcloud_iot_common.h"
/* lwIP socket handle start from 0 */
#define LWIP_SOCKET_FD_SHIFT 3
static uint32_t _time_left(uint32_t t_end, uint32_t t_now)
{
uint32_t t_left;
if (t_end > t_now) {
t_left = t_end - t_now;
} else {
t_left = 0;
}
return t_left;
}
uintptr_t HAL_TCP_Connect(const char *host, uint16_t port)
{
int ret;
struct addrinfo hints, *addr_list, *cur;
int fd = 0;
char port_str[6];
HAL_Snprintf(port_str, 6, "%d", port);
memset(&hints, 0x00, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
ret = getaddrinfo(host, port_str, &hints, &addr_list);
if (ret) {
Log_e("getaddrinfo(%s:%s) error", host, port_str);
return 0;
}
for (cur = addr_list; cur != NULL; cur = cur->ai_next) {
fd = (int) socket( cur->ai_family, cur->ai_socktype, cur->ai_protocol );
if( fd < 0 )
{
ret = 0;
continue;
}
if (connect(fd, cur->ai_addr, cur->ai_addrlen) == 0)
{
ret = fd + LWIP_SOCKET_FD_SHIFT;
break;
}
close( fd );
ret = 0;
}
if (ret == 0) {
Log_e("failed to connect with TCP server: %s:%s", host, port_str);
} else {
/* reduce log print due to frequent log server connect/disconnect */
if (0 == strncmp(host, LOG_UPLOAD_SERVER_DOMAIN, HOST_STR_LENGTH))
UPLOAD_DBG("connected with TCP server: %s:%s", host, port_str);
else
Log_i("connected with TCP server: %s:%s", host, port_str);
}
freeaddrinfo(addr_list);
return (uintptr_t)ret;
}
int HAL_TCP_Disconnect(uintptr_t fd)
{
int rc;
fd -= LWIP_SOCKET_FD_SHIFT;
/* Shutdown both send and receive operations. */
rc = shutdown((int) fd, 2);
if (0 != rc) {
Log_e("shutdown error: %s", strerror(errno));
return -1;
}
rc = close((int) fd);
if (0 != rc) {
Log_e("closesocket error: %s", strerror(errno));
return -1;
}
return 0;
}
int HAL_TCP_Write(uintptr_t fd, const unsigned char *buf, uint32_t len, uint32_t timeout_ms, size_t *written_len)
{
int ret;
uint32_t len_sent;
uint32_t t_end, t_left;
fd_set sets;
fd -= LWIP_SOCKET_FD_SHIFT;
t_end = HAL_GetTimeMs() + timeout_ms;
len_sent = 0;
ret = 1; /* send one time if timeout_ms is value 0 */
do {
t_left = _time_left(t_end, HAL_GetTimeMs());
if (0 != t_left) {
struct timeval timeout;
FD_ZERO(&sets);
FD_SET(fd, &sets);
timeout.tv_sec = t_left / 1000;
timeout.tv_usec = (t_left % 1000) * 1000;
ret = select(fd + 1, NULL, &sets, NULL, &timeout);
if (ret > 0) {
if (0 == FD_ISSET(fd, &sets)) {
Log_e("Should NOT arrive");
/* If timeout in next loop, it will not sent any data */
ret = 0;
continue;
}
} else if (0 == ret) {
ret = QCLOUD_ERR_TCP_WRITE_TIMEOUT;
Log_e("select-write timeout %d", (int)fd);
break;
} else {
if (EINTR == errno) {
Log_e("EINTR be caught");
continue;
}
ret = QCLOUD_ERR_TCP_WRITE_FAIL;
Log_e("select-write fail: %s", strerror(errno));
break;
}
}
else {
ret = QCLOUD_ERR_TCP_WRITE_TIMEOUT;
}
if (ret > 0) {
ret = send(fd, buf + len_sent, len - len_sent, 0);
if (ret > 0) {
len_sent += ret;
} else if (0 == ret) {
Log_e("No data be sent. Should NOT arrive");
} else {
if (EINTR == errno) {
Log_e("EINTR be caught");
continue;
}
ret = QCLOUD_ERR_TCP_WRITE_FAIL;
Log_e("send fail: %s", strerror(errno));
break;
}
}
} while ((len_sent < len) && (_time_left(t_end, HAL_GetTimeMs()) > 0));
*written_len = (size_t)len_sent;
return len_sent > 0 ? QCLOUD_RET_SUCCESS : ret;
}
int HAL_TCP_Read(uintptr_t fd, unsigned char *buf, uint32_t len, uint32_t timeout_ms, size_t *read_len)
{
int ret, err_code;
uint32_t len_recv;
uint32_t t_end, t_left;
fd_set sets;
struct timeval timeout;
fd -= LWIP_SOCKET_FD_SHIFT;
t_end = HAL_GetTimeMs() + timeout_ms;
len_recv = 0;
err_code = 0;
do {
t_left = _time_left(t_end, HAL_GetTimeMs());
if (0 == t_left) {
err_code = QCLOUD_ERR_TCP_READ_TIMEOUT;
break;
}
FD_ZERO(&sets);
FD_SET(fd, &sets);
timeout.tv_sec = t_left / 1000;
timeout.tv_usec = (t_left % 1000) * 1000;
ret = select(fd + 1, &sets, NULL, NULL, &timeout);
if (ret > 0) {
ret = recv(fd, buf + len_recv, len - len_recv, 0);
if (ret > 0) {
len_recv += ret;
} else if (0 == ret) {
struct sockaddr_in peer;
socklen_t sLen = sizeof(peer);
int peer_port = 0;
getpeername(fd, (struct sockaddr*)&peer, &sLen);
peer_port = ntohs(peer.sin_port);
/* reduce log print due to frequent log server connect/disconnect */
if (peer_port == LOG_UPLOAD_SERVER_PORT)
UPLOAD_DBG("connection is closed by server: %s:%d", inet_ntoa(peer.sin_addr), peer_port);
else
Log_e("connection is closed by server: %s:%d", inet_ntoa(peer.sin_addr), peer_port);
err_code = QCLOUD_ERR_TCP_PEER_SHUTDOWN;
break;
} else {
if (EINTR == errno) {
Log_e("EINTR be caught");
continue;
}
Log_e("recv error: %s", strerror(errno));
err_code = QCLOUD_ERR_TCP_READ_FAIL;
break;
}
} else if (0 == ret) {
err_code = QCLOUD_ERR_TCP_READ_TIMEOUT;
break;
} else {
Log_e("select-recv error: %s", strerror(errno));
err_code = QCLOUD_ERR_TCP_READ_FAIL;
break;
}
} while ((len_recv < len));
*read_len = (size_t)len_recv;
if (err_code == QCLOUD_ERR_TCP_READ_TIMEOUT && len_recv == 0)
err_code = QCLOUD_ERR_TCP_NOTHING_TO_READ;
return (len == len_recv) ? QCLOUD_RET_SUCCESS : err_code;
}

View File

@@ -0,0 +1,108 @@
/*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "qcloud_iot_import.h"
#include "qcloud_iot_export_log.h"
#include "qcloud_iot_export_error.h"
#include "qcloud_iot_common.h"
#include "tos_k.h"
#include "sal_module_wrapper.h"
#define PORT_BUFF_LEN 16
static uint32_t _time_left(uint32_t t_end, uint32_t t_now)
{
uint32_t t_left;
if (t_end > t_now) {
t_left = t_end - t_now;
} else {
t_left = 0;
}
return t_left;
}
uintptr_t HAL_TCP_Connect(const char *host, uint16_t port)
{
int fd;
char port_str[PORT_BUFF_LEN] = {0};
snprintf(port_str, PORT_BUFF_LEN, "%u", port);
Log_i("osal_tcp_connect entry, host=%s port=%d(%s)", host , port, port_str);
fd = tos_sal_module_connect(host, port_str, TOS_SAL_PROTO_TCP);
if (fd < 0) {
Log_i("net connect fail\n\r");
if (QCLOUD_RET_SUCCESS == tos_sal_module_init()) {
Log_i("net reinit success\n\r");
fd = tos_sal_module_connect(host, port_str, TOS_SAL_PROTO_TCP);
if (fd < 0) {
Log_i("net connect fail\n\r");
return NULL;
} else {
Log_i("net connect success, fd=%d\n\r", fd);
}
} else {
Log_i("net reinit fail\n\r");
return NULL;
}
}
return fd;
}
int HAL_TCP_Disconnect(uintptr_t fd)
{
(void)tos_sal_module_close(fd);
return QCLOUD_RET_SUCCESS;
}
int HAL_TCP_Write(uintptr_t fd, const unsigned char *buf, uint32_t len, uint32_t timeout_ms, size_t *written_len)
{
int ret;
ret = tos_sal_module_send(fd, buf, len);
if (ret < 0) {
return QCLOUD_ERR_TCP_WRITE_FAIL;
}
(*(int *)written_len) = ret;
return QCLOUD_RET_SUCCESS;
}
int HAL_TCP_Read(uintptr_t fd, unsigned char *buf, uint32_t len, uint32_t timeout_ms, size_t *read_len)
{
int ret;
ret = tos_sal_module_recv_timeout(fd, buf, len, timeout_ms);
if (ret < 0) {
return QCLOUD_ERR_TCP_READ_FAIL;
}
if (ret == 0) {
return QCLOUD_ERR_TCP_NOTHING_TO_READ;
}
(*(int *)read_len) = ret;
return QCLOUD_RET_SUCCESS;
}

View File

@@ -0,0 +1,95 @@
/*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "qcloud_iot_import.h"
#include "tos_k.h"
static char now_time_str[20] = {0};
uint32_t HAL_GetTimeMs(void)
{
#if (TOS_CFG_CPU_TICK_PER_SECOND == 1000)
return tos_systick_get();
#else
k_tick_t tick = 0u;
tick = tos_systick_get() * 1000;
return ((tick + TOS_CFG_CPU_TICK_PER_SECOND - 1) / TOS_CFG_CPU_TICK_PER_SECOND);
#endif
}
/*Get timestamp*/
long HAL_Timer_current_sec(void)
{
return HAL_GetTimeMs() / 1000;
}
char* HAL_Timer_current(void)
{
long time_sec;
time_sec = HAL_Timer_current_sec();
memset(now_time_str, 0, 20);
snprintf(now_time_str, 20, "%ld", time_sec);
return now_time_str;
}
bool HAL_Timer_expired(Timer *timer)
{
return HAL_GetTimeMs() > timer->end_time ? 1 : 0;
}
void HAL_Timer_countdown_ms(Timer *timer, unsigned int timeout_ms)
{
timer->end_time = HAL_GetTimeMs();
timer->end_time += timeout_ms;
}
void HAL_Timer_countdown(Timer *timer, unsigned int timeout)
{
timer->end_time = HAL_GetTimeMs();
timer->end_time += timeout * 1000;
}
int HAL_Timer_remain(Timer *timer)
{
uint32_t now;
now = HAL_GetTimeMs();
if (timer->end_time <= now) {
return 0;
}
return timer->end_time - now;
}
void HAL_Timer_init(Timer *timer)
{
timer->end_time = 0;
}
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,161 @@
/*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "lwip/sockets.h"
#include "lwip/netdb.h"
#include "lwip/inet.h"
#include "qcloud_iot_import.h"
#include "qcloud_iot_export_log.h"
#include "qcloud_iot_export_error.h"
#ifdef COAP_COMM_ENABLED
/* lwIP socket handle start from 0 */
#define LWIP_SOCKET_FD_SHIFT 3
uintptr_t HAL_UDP_Connect(const char *host, unsigned short port)
{
#define NETWORK_ADDR_LEN (16)
int ret;
struct addrinfo hints, *addr_list, *cur;
int fd = 0;
char port_str[6] = {0};
HAL_Snprintf(port_str, 6, "%d", port);
memset((char *)&hints, 0x00, sizeof(hints));
hints.ai_socktype = SOCK_DGRAM;
hints.ai_family = AF_INET;
hints.ai_protocol = IPPROTO_UDP;
Log_d("establish tcp connection with server(host=%s port=%s)", host, port_str);
if (getaddrinfo(host, port_str, &hints, &addr_list) != 0) {
Log_e("getaddrinfo error,errno:%s",strerror(errno));
return 0;
}
for (cur = addr_list; cur != NULL; cur = cur->ai_next) {
fd = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
if (fd < 0) {
ret = 0;
continue;
}
if (0 == connect(fd, cur->ai_addr, cur->ai_addrlen)) {
ret = fd + LWIP_SOCKET_FD_SHIFT;
break;
}
close(fd);
ret = 0;
}
if (0 == ret) {
Log_e("fail to establish udp");
} else {
Log_d("success to establish udp, fd=%d", ret);
}
freeaddrinfo(addr_list);
return (uintptr_t)ret;
#undef NETWORK_ADDR_LEN
}
void HAL_UDP_Disconnect(uintptr_t fd)
{
long socket_id = -1;
fd -= LWIP_SOCKET_FD_SHIFT;
socket_id = (int)fd;
close(socket_id);
}
int HAL_UDP_Write(uintptr_t fd, const unsigned char *p_data, unsigned int datalen)
{
int rc = -1;
long socket_id = -1;
fd -= LWIP_SOCKET_FD_SHIFT;
socket_id = (int)fd;
rc = send(socket_id, (char *)p_data, (int)datalen, 0);
if (-1 == rc) {
return -1;
}
return rc;
}
int HAL_UDP_Read(uintptr_t fd, unsigned char *p_data, unsigned int datalen)
{
long socket_id = -1;
int count = -1;
fd -= LWIP_SOCKET_FD_SHIFT;
socket_id = (int)fd;
count = (int)read(socket_id, p_data, datalen);
return count;
}
int HAL_UDP_ReadTimeout(uintptr_t fd, unsigned char *p_data, unsigned int datalen, unsigned int timeout_ms)
{
int ret;
struct timeval tv;
fd_set read_fds;
int socket_id = -1;
fd -= LWIP_SOCKET_FD_SHIFT;
socket_id = (int)fd;
if (socket_id < 0) {
return -1;
}
FD_ZERO(&read_fds);
FD_SET(socket_id, &read_fds);
tv.tv_sec = timeout_ms / 1000;
tv.tv_usec = (timeout_ms % 1000) * 1000;
ret = select(socket_id + 1, &read_fds, NULL, NULL, timeout_ms == 0 ? NULL : &tv);
/* Zero fds ready means we timed out */
if (ret == 0) {
return QCLOUD_ERR_SSL_READ_TIMEOUT; /* receive timeout */
}
if (ret < 0) {
if (errno == EINTR) {
return -3; /* want read */
}
return QCLOUD_ERR_SSL_READ; /* receive failed */
}
/* This call will not block */
return HAL_UDP_Read(fd, p_data, datalen);
}
#endif

View File

@@ -0,0 +1,117 @@
/*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "qcloud_iot_import.h"
#include "qcloud_iot_export_log.h"
#include "qcloud_iot_export_error.h"
#include "tos_k.h"
#include "sal_module_wrapper.h"
#define PORT_BUFF_LEN 16
#ifdef COAP_COMM_ENABLED
uintptr_t HAL_UDP_Connect(const char *host, unsigned short port)
{
int fd;
char port_str[PORT_BUFF_LEN] = {0};
snprintf(port_str, PORT_BUFF_LEN, "%u", port);
Log_i("osal_udp_connect entry, host=%s port=%d(%s)", host , port, port_str);
fd = tos_sal_module_connect("111.230.127.136", "5684", TOS_SAL_PROTO_UDP);
if (fd < 0) {
Log_i("net connect fail\n\r");
if (QCLOUD_RET_SUCCESS == tos_sal_module_init()) { /* ÖØÐ³õʼ»¯Ä£×é */
Log_i("net reinit success\n\r");
fd = tos_sal_module_connect(host, port_str, TOS_SAL_PROTO_UDP);
if (fd < 0) {
Log_i("net connect fail\n\r");
return NULL;
} else {
Log_i("net connect success, fd=%d\n\r", fd);
}
} else {
Log_i("net reinit fail\n\r");
return NULL;
}
}
return fd;
}
void HAL_UDP_Disconnect(uintptr_t fd)
{
(void)tos_sal_module_close(fd);
}
int HAL_UDP_Write(uintptr_t fd, const unsigned char *p_data, unsigned int datalen)
{
int ret;
ret = tos_sal_module_sendto(fd, NULL, NULL, p_data, datalen);
if (ret < 0) {
return QCLOUD_ERR_UDP_WRITE_FAIL;
}
return QCLOUD_RET_SUCCESS;
}
int HAL_UDP_Read(uintptr_t fd, unsigned char *p_data, unsigned int datalen)
{
int ret;
Log_i("osal_udp_read len %d\r\n", datalen);
ret = tos_sal_module_recvfrom(fd, p_data, datalen);
if (ret < 0) {
return QCLOUD_ERR_UDP_READ_FAIL;
}
if (ret == 0) {
return QCLOUD_ERR_UDP_NOTHING_TO_READ;
}
return QCLOUD_RET_SUCCESS;
}
int HAL_UDP_ReadTimeout(uintptr_t fd, unsigned char *p_data, unsigned int datalen, unsigned int timeout_ms)
{
int ret;
Log_i("osal_udp_read len %d timeout %d\r\n", datalen, timeout_ms);
ret = tos_sal_module_recvfrom_timeout(fd, p_data, datalen, timeout_ms);
if (ret < 0) {
return QCLOUD_ERR_UDP_READ_FAIL;
}
if (ret == 0) {
return QCLOUD_ERR_UDP_NOTHING_TO_READ;
}
return QCLOUD_RET_SUCCESS;
}
#endif