feat: 移植腾讯云物联网开发平台 C SDK
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
/**
|
||||
* @copyright
|
||||
*
|
||||
* Tencent is pleased to support the open source community by making IoT Hub available.
|
||||
* Copyright(C) 2018 - 2022 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.
|
||||
*
|
||||
* @file HAL_Device_tencentos_tiny.c
|
||||
* @brief
|
||||
* @author fancyxu (fancyxu@tencent.com)
|
||||
* @version 1.0
|
||||
* @date 2022-01-24
|
||||
*
|
||||
* @par Change Log:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2022-01-24 <td>1.0 <td>fancyxu <td>first commit
|
||||
* </table>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "qcloud_iot_platform.h"
|
||||
|
||||
#include "qcloud_iot_params_check.h"
|
||||
|
||||
/**
|
||||
* @brief product Id
|
||||
*
|
||||
*/
|
||||
static char sg_product_id[MAX_SIZE_OF_PRODUCT_ID + 1] = "Q5NNWVC2Z8";
|
||||
|
||||
/**
|
||||
* @brief device name
|
||||
*
|
||||
*/
|
||||
static char sg_device_name[MAX_SIZE_OF_DEVICE_NAME + 1] = "test1";
|
||||
|
||||
/**
|
||||
* @brief device secret of PSK device
|
||||
*
|
||||
*/
|
||||
static char sg_device_secret[MAX_SIZE_OF_DEVICE_SECRET + 1] = "jACrR7OalxdQfdJgEduZ3w==";
|
||||
|
||||
/**
|
||||
* @brief Copy device info from src to dst
|
||||
*
|
||||
* @param[out] dst dst to copy
|
||||
* @param[in] src srt to be copied
|
||||
* @param[in] max_len max_len to be copy
|
||||
* @return @see IotReturnCode
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Save device info
|
||||
*
|
||||
* @param[in] device_info @see DeviceInfo
|
||||
* @return @see IotReturnCode
|
||||
*/
|
||||
int HAL_SetDevInfo(DeviceInfo *device_info)
|
||||
{
|
||||
POINTER_SANITY_CHECK(device_info, QCLOUD_ERR_DEV_INFO);
|
||||
int rc;
|
||||
rc = device_info_copy(sg_product_id, device_info->product_id, MAX_SIZE_OF_PRODUCT_ID); // set product ID
|
||||
rc |= device_info_copy(sg_device_name, device_info->device_name, MAX_SIZE_OF_DEVICE_NAME); // set dev name
|
||||
rc |= device_info_copy(sg_device_secret, device_info->device_secret, MAX_SIZE_OF_DEVICE_SECRET); // set dev secret
|
||||
if (rc) {
|
||||
Log_e("Set device info err");
|
||||
rc = QCLOUD_ERR_DEV_INFO;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get device info
|
||||
*
|
||||
* @param[in] device_info @see DeviceInfo
|
||||
* @return @see IotReturnCode
|
||||
*/
|
||||
int HAL_GetDevInfo(DeviceInfo *device_info)
|
||||
{
|
||||
POINTER_SANITY_CHECK(device_info, QCLOUD_ERR_DEV_INFO);
|
||||
int ret;
|
||||
memset(device_info, '\0', sizeof(DeviceInfo));
|
||||
ret = device_info_copy(device_info->product_id, sg_product_id, MAX_SIZE_OF_PRODUCT_ID); // get product ID
|
||||
ret |= device_info_copy(device_info->device_name, sg_device_name, MAX_SIZE_OF_DEVICE_NAME); // get dev name
|
||||
ret |= device_info_copy(device_info->device_secret, sg_device_secret, MAX_SIZE_OF_DEVICE_SECRET); // get dev secret
|
||||
if (QCLOUD_RET_SUCCESS != ret) {
|
||||
Log_e("Get device info err");
|
||||
ret = QCLOUD_ERR_DEV_INFO;
|
||||
}
|
||||
return ret;
|
||||
}
|
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* @copyright
|
||||
*
|
||||
* Tencent is pleased to support the open source community by making IoT Hub available.
|
||||
* Copyright(C) 2018 - 2022 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.
|
||||
*
|
||||
* @file HAL_File_tencentos_tiny.c
|
||||
* @brief
|
||||
* @author fancyxu (fancyxu@tencent.com)
|
||||
* @version 1.0
|
||||
* @date 2022-04-07
|
||||
*
|
||||
* @par Change Log:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2022-04-07 <td>1.0 <td>fancyxu <td>first commit
|
||||
* </table>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "qcloud_iot_platform.h"
|
||||
|
||||
/**
|
||||
* @brief Functions for saving file into NVS(files/FLASH)
|
||||
* @param[in] filename file path name
|
||||
* @param[in] buf source need write buffer
|
||||
* @param[in] write_len length of file to write
|
||||
* @return length of data save when success, or 0 for failure
|
||||
*/
|
||||
size_t HAL_File_Write(const char *filename, const void *buf, size_t write_len, size_t offset)
|
||||
{
|
||||
return write_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Functions for reading file from NVS(files/FLASH)
|
||||
* @param[in] filename file path name
|
||||
* @param[in] buf destination log buffer
|
||||
* @param[in] read_len length to read
|
||||
* @return length of data read when success, or 0 for failure
|
||||
*/
|
||||
size_t HAL_File_Read(const char *filename, void *buf, size_t read_len, size_t offset)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Functions for deleting file in NVS(files/FLASH).
|
||||
* @param[in] filename file path name
|
||||
* @return 0 when success
|
||||
*/
|
||||
int HAL_File_Del(const char *filename)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Functions for reading the size of file in NVS(files/FLASH).
|
||||
* @param[in] filename file path name
|
||||
* @return 0 when nothing exist
|
||||
*/
|
||||
size_t HAL_File_GetSize(const char *filename)
|
||||
{
|
||||
return 0;
|
||||
}
|
@@ -0,0 +1,172 @@
|
||||
/**
|
||||
* @copyright
|
||||
*
|
||||
* Tencent is pleased to support the open source community by making IoT Hub available.
|
||||
* Copyright(C) 2018 - 2022 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.
|
||||
*
|
||||
* @file HAL_AT_Linux.c
|
||||
* @brief
|
||||
* @author fancyxu (fancyxu@tencent.com)
|
||||
* @version 1.0
|
||||
* @date 2022-04-21
|
||||
*
|
||||
* @par Change Log:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2022-04-21 <td>1.0 <td>fancyxu <td>first commit
|
||||
* </table>
|
||||
*/
|
||||
|
||||
#include "qcloud_iot_platform.h"
|
||||
|
||||
#include "qcloud_iot_at_client.h"
|
||||
#include "tos_hal.h"
|
||||
|
||||
#define YOUR_WIFI_SSID "youga_wifi"
|
||||
#define YOUR_WIFI_PWD "Iot@2018"
|
||||
|
||||
static hal_uart_t sg_uart;
|
||||
|
||||
/**
|
||||
* @brief At send function.
|
||||
*
|
||||
* @return 0 for success
|
||||
*/
|
||||
static int qcloud_iot_usr_at_send(const void *data, size_t data_len)
|
||||
{
|
||||
return tos_hal_uart_write(&sg_uart, data, data_len, 0xffffffff);
|
||||
}
|
||||
|
||||
static int _esp8266_init(void)
|
||||
{
|
||||
const char *at_cmd_resp[][2] = {
|
||||
{"AT+RESTORE\r\n", "ready"},
|
||||
{"ATE0\r\n", NULL},
|
||||
{"AT+CWMODE=1\r\n", NULL},
|
||||
{"AT+CIPMODE=0\r\n", NULL},
|
||||
};
|
||||
int rc;
|
||||
for (int i = 0; i < sizeof(at_cmd_resp) / sizeof(const char *) / 2; i++) {
|
||||
int retry = 3;
|
||||
do {
|
||||
rc = HAL_Module_SendAtCmdWaitResp(at_cmd_resp[i][0], at_cmd_resp[i][1], 5000);
|
||||
} while (rc && retry-- > 0);
|
||||
if (rc) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Init at module.
|
||||
*
|
||||
* @return 0 for success
|
||||
*/
|
||||
int HAL_Module_Init(void)
|
||||
{
|
||||
// init at client
|
||||
int rc = qcloud_iot_at_client_init(2048, qcloud_iot_usr_at_send);
|
||||
if (rc < 0) {
|
||||
Log_e("at client init fail");
|
||||
return rc;
|
||||
}
|
||||
// init uart
|
||||
rc = tos_hal_uart_init(&sg_uart, HAL_UART_PORT_2);
|
||||
if (rc) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
// init esp8266
|
||||
return _esp8266_init();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Deinit at module.
|
||||
*
|
||||
*/
|
||||
void HAL_Module_Deinit(void)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Send at cmd to at module and wait for resp.
|
||||
*
|
||||
* @param[in] at_cmd at cmd
|
||||
* @param[in] at_expect expect resp
|
||||
* @param[in] timeout_ms wait timeout
|
||||
* @return 0 for success
|
||||
*/
|
||||
int HAL_Module_SendAtCmdWaitResp(const char *at_cmd, const char *at_expect, uint32_t timeout_ms)
|
||||
{
|
||||
Log_d("at_cmd:%s", at_cmd);
|
||||
return qcloud_iot_at_client_send_at_util_expect(at_cmd, at_expect, NULL, 0, timeout_ms);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Send at cmd and waif for data.
|
||||
*
|
||||
* @param[in] at_cmd at cmd
|
||||
* @param[in] at_expect expect resp
|
||||
* @param[out] recv_buf recv data buffer
|
||||
* @param[out] recv_len recv data length
|
||||
* @param[in] timeout_ms wait timeout
|
||||
* @return 0 for success
|
||||
*/
|
||||
int HAL_Module_SendAtCmdWaitRespWithData(const char *at_cmd, const char *at_expect, void *recv_buf, uint32_t *recv_len,
|
||||
uint32_t timeout_ms)
|
||||
{
|
||||
return qcloud_iot_at_client_send_at_util_expect(at_cmd, at_expect, recv_buf, recv_len, timeout_ms);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Send date to at module.
|
||||
*
|
||||
* @param[in] data data to send
|
||||
* @param[in] data_len data length
|
||||
* @return 0 for success
|
||||
*/
|
||||
int HAL_Module_SendAtData(const void *data, int data_len)
|
||||
{
|
||||
return qcloud_iot_at_client_send_data(data, data_len);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set urc.
|
||||
*
|
||||
* @param[in] urc irc string
|
||||
* @param[in] urc_handler urc handler
|
||||
* @return 0 for success
|
||||
*/
|
||||
int HAL_Module_SetUrc(const char *urc, OnUrcHandler urc_handler)
|
||||
{
|
||||
static QcloudATUrc sg_urc_table[10];
|
||||
static int sg_urc_table_count;
|
||||
|
||||
sg_urc_table[sg_urc_table_count].urc_prefix = urc;
|
||||
sg_urc_table[sg_urc_table_count].urc_handle = urc_handler;
|
||||
sg_urc_table_count++;
|
||||
qcloud_iot_at_client_set_urc(sg_urc_table, sg_urc_table_count);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief connect network
|
||||
*
|
||||
* @return int 0 for success
|
||||
*/
|
||||
int HAL_Module_ConnectNetwork(void)
|
||||
{
|
||||
return HAL_Module_SendAtCmdWaitResp("AT+CWJAP=\"" YOUR_WIFI_SSID "\",\"" YOUR_WIFI_PWD "\"\r\n", "WIFI GOT IP",
|
||||
10000);
|
||||
}
|
@@ -0,0 +1,373 @@
|
||||
/**
|
||||
* @copyright
|
||||
*
|
||||
* Tencent is pleased to support the open source community by making IoT Hub available.
|
||||
* Copyright(C) 2018 - 2022 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.
|
||||
*
|
||||
* @file HAL_OS_tencentos_tiny.c
|
||||
* @brief
|
||||
* @author fancyxu (fancyxu@tencent.com)
|
||||
* @version 1.0
|
||||
* @date 2022-01-24
|
||||
*
|
||||
* @par Change Log:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2022-01-24 <td>1.0 <td>fancyxu <td>first commit
|
||||
* </table>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "qcloud_iot_platform.h"
|
||||
#include "tos_k.h"
|
||||
|
||||
/**
|
||||
* @brief Sleep for ms
|
||||
*
|
||||
* @param[in] ms ms to sleep
|
||||
*/
|
||||
void HAL_SleepMs(uint32_t ms)
|
||||
{
|
||||
(void)tos_sleep_hmsm(0, 0, 0, ms);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Printf with format.
|
||||
*
|
||||
* @param[in] fmt format
|
||||
*/
|
||||
void HAL_Printf(const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
va_start(args, fmt);
|
||||
vprintf(fmt, args);
|
||||
va_end(args);
|
||||
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Snprintf with format.
|
||||
*
|
||||
* @param[out] str buffer to save
|
||||
* @param[in] len buffer len
|
||||
* @param[in] fmt format
|
||||
* @return length of formatted string, >0 for success.
|
||||
*/
|
||||
int HAL_Snprintf(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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Malloc from heap.
|
||||
*
|
||||
* @param[in] size size to malloc
|
||||
* @return pointer to buffer, NULL for failed.
|
||||
*/
|
||||
void *HAL_Malloc(size_t size)
|
||||
{
|
||||
return tos_mmheap_alloc(size);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Free buffer malloced by HAL_Malloc.
|
||||
*
|
||||
* @param[in] ptr
|
||||
*/
|
||||
void HAL_Free(void *ptr)
|
||||
{
|
||||
tos_mmheap_free(ptr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Mutex create.
|
||||
*
|
||||
* @return pointer to mutex
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Mutex destroy.
|
||||
*
|
||||
* @param[in,out] mutex pointer to mutex
|
||||
*/
|
||||
void HAL_MutexDestroy(void *mutex)
|
||||
{
|
||||
k_err_t rc;
|
||||
|
||||
if (!mutex) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (K_ERR_NONE != (rc = tos_mutex_destroy((k_mutex_t *)mutex))) {
|
||||
tos_kprintf("osal_mutex_destroy err, err:%d\n\r", rc);
|
||||
} else {
|
||||
HAL_Free((void *)mutex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Mutex lock.
|
||||
*
|
||||
* @param[in,out] mutex pointer to mutex
|
||||
*/
|
||||
void HAL_MutexLock(void *mutex)
|
||||
{
|
||||
k_err_t rc;
|
||||
|
||||
if (!mutex) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (K_ERR_NONE != (rc = tos_mutex_pend((k_mutex_t *)mutex))) {
|
||||
if (K_ERR_MUTEX_NESTING != rc && K_ERR_PEND_NOWAIT != rc) {
|
||||
k_task_t *current_task = tos_task_curr_task_get();
|
||||
tos_kprintf("%s osal_mutex_lock err, err:%d\n\r", current_task->name, rc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Mutex try lock.
|
||||
*
|
||||
* @param[in,out] mutex pointer to mutex
|
||||
* @return 0 for success
|
||||
*/
|
||||
int HAL_MutexTryLock(void *mutex)
|
||||
{
|
||||
k_err_t rc;
|
||||
if (!mutex) {
|
||||
return -1;
|
||||
}
|
||||
if (K_ERR_NONE != (rc = tos_mutex_pend_timed((k_mutex_t *)mutex, 0))) {
|
||||
if (K_ERR_MUTEX_NESTING != rc && K_ERR_PEND_NOWAIT != rc) {
|
||||
tos_kprintf("osal_mutex_lock err, err:%d\n\r", rc);
|
||||
return (int)rc;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Mutex unlock.
|
||||
*
|
||||
* @param[in,out] mutex pointer to mutex
|
||||
*/
|
||||
void HAL_MutexUnlock(void *mutex)
|
||||
{
|
||||
k_err_t rc;
|
||||
if (!mutex) {
|
||||
return;
|
||||
}
|
||||
if (K_ERR_NONE != (rc = tos_mutex_post((k_mutex_t *)mutex))) {
|
||||
if (K_ERR_MUTEX_NESTING != rc && K_ERR_PEND_TIMEOUT != rc) {
|
||||
tos_kprintf("osal_mutex_unlock err, err:%d\n\r", rc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief platform-dependent thread create function
|
||||
*
|
||||
* @param[in,out] params params to create thread @see ThreadParams
|
||||
* @return @see IotReturnCode
|
||||
*/
|
||||
int HAL_ThreadCreate(ThreadParams *params)
|
||||
{
|
||||
params->thread_id = HAL_Malloc(sizeof(k_task_t));
|
||||
if (!params->thread_id) {
|
||||
return QCLOUD_ERR_MALLOC;
|
||||
}
|
||||
k_prio_t priority = TOS_CFG_TASK_PRIO_MAX - 2;
|
||||
switch (params->priority) {
|
||||
case THREAD_PRIORITY_HIGH:
|
||||
priority = 2;
|
||||
break;
|
||||
case THREAD_PRIORITY_MIDDLE:
|
||||
priority = TOS_CFG_TASK_PRIO_MAX / 2;
|
||||
break;
|
||||
case THREAD_PRIORITY_LOW:
|
||||
priority = TOS_CFG_TASK_PRIO_MAX - 2;
|
||||
break;
|
||||
}
|
||||
return tos_task_create((k_task_t *)params->thread_id, params->thread_name, (k_task_entry_t)params->thread_func,
|
||||
params->user_arg, priority, params->stack_base, params->stack_size, 20);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief platform-dependent thread destroy function.
|
||||
*
|
||||
*/
|
||||
void HAL_ThreadDestroy(void *threadId)
|
||||
{
|
||||
// no use in sdk
|
||||
tos_task_destroy((k_task_t *)threadId);
|
||||
HAL_Free(threadId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief platform-dependent semaphore create function.
|
||||
*
|
||||
* @return pointer to semaphore
|
||||
*/
|
||||
void *HAL_SemaphoreCreate(void)
|
||||
{
|
||||
k_sem_t *sem = (k_sem_t *)malloc(sizeof(k_sem_t));
|
||||
if (!sem) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (0 != tos_sem_create(sem, 0)) {
|
||||
free(sem);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return sem;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief platform-dependent semaphore destory function.
|
||||
*
|
||||
* @param[in] sem pointer to semaphore
|
||||
*/
|
||||
void HAL_SemaphoreDestroy(void *sem)
|
||||
{
|
||||
if (!sem) {
|
||||
return;
|
||||
}
|
||||
tos_sem_destroy((k_sem_t *)sem);
|
||||
free(sem);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief platform-dependent semaphore post function.
|
||||
*
|
||||
* @param[in] sem pointer to semaphore
|
||||
*/
|
||||
void HAL_SemaphorePost(void *sem)
|
||||
{
|
||||
if (!sem) {
|
||||
return;
|
||||
}
|
||||
tos_sem_post((k_sem_t *)sem);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief platform-dependent semaphore wait function.
|
||||
*
|
||||
* @param[in] sem pointer to semaphore
|
||||
* @param[in] timeout_ms wait timeout
|
||||
* @return @see IotReturnCode
|
||||
*/
|
||||
int HAL_SemaphoreWait(void *sem, uint32_t timeout_ms)
|
||||
{
|
||||
if (!sem) {
|
||||
return -1;
|
||||
}
|
||||
return tos_sem_pend((k_sem_t *)sem, timeout_ms);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief platform-dependent mail queue init function.
|
||||
*
|
||||
* @param[in] pool pool using in mail queue
|
||||
* @param[in] mail_size mail size
|
||||
* @param[in] mail_count mail count
|
||||
* @return pointer to mail queue
|
||||
*/
|
||||
void *HAL_MailQueueInit(void *pool, size_t mail_size, int mail_count)
|
||||
{
|
||||
k_mail_q_t *mail_q = HAL_Malloc(sizeof(k_mail_q_t));
|
||||
if (!mail_q) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int rc = tos_mail_q_create(mail_q, pool, mail_count, mail_size);
|
||||
if (rc) {
|
||||
HAL_Free(mail_q);
|
||||
return NULL;
|
||||
}
|
||||
return mail_q;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief platform-dependent mail queue deinit function.
|
||||
*
|
||||
* @param[in] mail_q pointer to mail queue
|
||||
*/
|
||||
void HAL_MailQueueDeinit(void *mail_q)
|
||||
{
|
||||
if (!mail_q) {
|
||||
return;
|
||||
}
|
||||
tos_mail_q_destroy((k_mail_q_t *)mail_q);
|
||||
HAL_Free(mail_q);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief platform-dependent mail queue send function.
|
||||
*
|
||||
* @param[in] mail_q pointer to mail queue
|
||||
* @param[in] buf data buf
|
||||
* @param[in] size data size
|
||||
* @return 0 for success
|
||||
*/
|
||||
int HAL_MailQueueSend(void *mail_q, const void *buf, size_t size)
|
||||
{
|
||||
if (!mail_q) {
|
||||
return -1;
|
||||
}
|
||||
return tos_mail_q_post((k_mail_q_t *)mail_q, (void *)buf, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief platform-dependent mail queue send function.
|
||||
*
|
||||
* @param[in] mail_q pointer to mail queue
|
||||
* @param[out] buf data buf
|
||||
* @param[in] size data size
|
||||
* @param[in] timeout_ms
|
||||
* @return 0 for success
|
||||
*/
|
||||
int HAL_MailQueueRecv(void *mail_q, void *buf, size_t *size, uint32_t timeout_ms)
|
||||
{
|
||||
if (!mail_q) {
|
||||
return -1;
|
||||
}
|
||||
return tos_mail_q_pend((k_mail_q_t *)mail_q, buf, size, timeout_ms);
|
||||
}
|
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* @copyright
|
||||
*
|
||||
* Tencent is pleased to support the open source community by making IoT Hub available.
|
||||
* Copyright(C) 2018 - 2022 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.
|
||||
*
|
||||
* @file HAL_TCP_module.c
|
||||
* @brief
|
||||
* @author fancyxu (fancyxu@tencent.com)
|
||||
* @version 1.0
|
||||
* @date 2022-01-24
|
||||
*
|
||||
* @par Change Log:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2022-01-24 <td>1.0 <td>fancyxu <td>first commit
|
||||
* </table>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "qcloud_iot_common.h"
|
||||
|
||||
#include "tos_k.h"
|
||||
#include "sal_module_wrapper.h"
|
||||
|
||||
/**
|
||||
* @brief TCP connect in linux
|
||||
*
|
||||
* @param[in] host host to connect
|
||||
* @param[out] port port to connect
|
||||
* @return socket fd
|
||||
*/
|
||||
int HAL_TCP_Connect(const char *host, const char *port)
|
||||
{
|
||||
int fd;
|
||||
Log_i("osal_tcp_connect entry, host=%s port=%d(%s)", host, port, port);
|
||||
fd = tos_sal_module_connect(host, port, TOS_SAL_PROTO_TCP);
|
||||
if (fd < 0) {
|
||||
Log_i("net connect fail\n\r");
|
||||
return -1;
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TCP disconnect
|
||||
*
|
||||
* @param[in] fd socket fd
|
||||
* @return 0 for success
|
||||
*/
|
||||
int HAL_TCP_Disconnect(int fd)
|
||||
{
|
||||
(void)tos_sal_module_close(fd);
|
||||
return QCLOUD_RET_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TCP write
|
||||
*
|
||||
* @param[in] fd socket fd
|
||||
* @param[in] buf buf to write
|
||||
* @param[in] len buf len
|
||||
* @param[in] timeout_ms timeout
|
||||
* @param[out] written_len data written length
|
||||
* @return @see IotReturnCode
|
||||
*/
|
||||
int HAL_TCP_Write(int 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TCP read.
|
||||
*
|
||||
* @param[in] fd socket fd
|
||||
* @param[out] buf buffer to save read data
|
||||
* @param[in] len buffer len
|
||||
* @param[in] timeout_ms timeout
|
||||
* @param[out] read_len length of data read
|
||||
* @return @see IotReturnCode
|
||||
*/
|
||||
int HAL_TCP_Read(int 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;
|
||||
}
|
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* @copyright
|
||||
*
|
||||
* Tencent is pleased to support the open source community by making IoT Hub available.
|
||||
* Copyright(C) 2018 - 2022 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.
|
||||
*
|
||||
* @file HAL_Timer_tencentos_tiny.c
|
||||
* @brief
|
||||
* @author fancyxu (fancyxu@tencent.com)
|
||||
* @version 1.0
|
||||
* @date 2022-01-24
|
||||
*
|
||||
* @par Change Log:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2022-01-24 <td>1.0 <td>fancyxu <td>first commit
|
||||
* </table>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include "qcloud_iot_platform.h"
|
||||
|
||||
#include "tos_k.h"
|
||||
|
||||
static uint64_t sg_time_stamp_ms;
|
||||
|
||||
static uint64_t _tos_get_systick_ms(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
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get utc time ms timestamp.
|
||||
*
|
||||
* @return timestamp
|
||||
*/
|
||||
uint64_t HAL_Timer_CurrentMs(void)
|
||||
{
|
||||
return sg_time_stamp_ms + _tos_get_systick_ms();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Time format string.
|
||||
*
|
||||
* @return time format string
|
||||
*/
|
||||
char *HAL_Timer_Current(void)
|
||||
{
|
||||
static char time_stamp[32];
|
||||
HAL_Snprintf(time_stamp, sizeof(time_stamp), "%llu", HAL_Timer_CurrentMs() / 1000);
|
||||
return time_stamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set system time using ms timestamp
|
||||
*
|
||||
* @param[in] timestamp_ms
|
||||
* @return 0 for success
|
||||
*/
|
||||
int HAL_Timer_SetSystimeMs(uint64_t timestamp_ms)
|
||||
{
|
||||
sg_time_stamp_ms = timestamp_ms - _tos_get_systick_ms();
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user