add new qloud-c-sdk component
This commit is contained in:
@@ -0,0 +1,182 @@
|
||||
/**
|
||||
* @copyright
|
||||
*
|
||||
* Tencent is pleased to support the open source community by making IoT Hub available.
|
||||
* Copyright(C) 2018 - 2021 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_linux.c
|
||||
* @brief Get and set device info
|
||||
* @author fancyxu (fancyxu@tencent.com)
|
||||
* @version 1.0
|
||||
* @date 2021-05-31
|
||||
*
|
||||
* @par Change Log:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2021-05-31 <td>1.0 <td>fancyxu <td>first commit
|
||||
* <tr><td>2021-07-09 <td>1.1 <td>fancyxu <td>fix code standard of IotReturnCode
|
||||
* </table>
|
||||
*/
|
||||
|
||||
#include "qcloud_iot_platform.h"
|
||||
|
||||
/**
|
||||
* @brief 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.
|
||||
*
|
||||
*/
|
||||
#ifdef DEBUG_DEV_INFO_USED
|
||||
|
||||
/**
|
||||
* @brief product Id
|
||||
*
|
||||
*/
|
||||
static char sg_product_id[MAX_SIZE_OF_PRODUCT_ID + 1] = "FWR8PGACUS";
|
||||
|
||||
/**
|
||||
* @brief device name
|
||||
*
|
||||
*/
|
||||
static char sg_device_name[MAX_SIZE_OF_DEVICE_NAME + 1] = "dev001";
|
||||
|
||||
#ifdef DEV_DYN_REG_ENABLED
|
||||
/**
|
||||
* @brief 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
|
||||
|
||||
/**
|
||||
* @brief 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";
|
||||
|
||||
/**
|
||||
* @brief 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
|
||||
/**
|
||||
* @brief device secret of PSK device
|
||||
*
|
||||
*/
|
||||
static char sg_device_secret[MAX_SIZE_OF_DEVICE_SECRET + 1] = "XIBjgofTv/QEQTlRTDQnGg==";
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @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 *dst, void *src, uint8_t max_len)
|
||||
{
|
||||
if (strlen(src) > max_len) {
|
||||
return QCLOUD_ERR_FAILURE;
|
||||
}
|
||||
memset(dst, '\0', max_len);
|
||||
strncpy(dst, src, max_len);
|
||||
return QCLOUD_RET_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Save device info
|
||||
*
|
||||
* @param[in] dev_info device info to be saved
|
||||
* @return @see IotReturnCode
|
||||
*/
|
||||
int HAL_SetDevInfo(void *dev_info)
|
||||
{
|
||||
POINTER_SANITY_CHECK(dev_info, QCLOUD_ERR_DEV_INFO);
|
||||
int rc;
|
||||
DeviceInfo *device_info = (DeviceInfo *)dev_info;
|
||||
|
||||
#ifdef DEBUG_DEV_INFO_USED
|
||||
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
|
||||
|
||||
#ifdef DEV_DYN_REG_ENABLE
|
||||
rc |= device_info_copy(sg_product_secret, device_info->product_secret, MAX_SIZE_OF_PRODUCT_SECRET);
|
||||
#endif
|
||||
|
||||
#ifdef AUTH_MODE_CERT
|
||||
rc |= device_info_copy(sg_device_cert_file_name, device_info->dev_cert_file_name,
|
||||
MAX_SIZE_OF_DEVICE_CERT_FILE_NAME); // set dev cert file name
|
||||
rc |= device_info_copy(sg_device_privatekey_file_name, device_info->dev_key_file_name,
|
||||
MAX_SIZE_OF_DEVICE_SECRET_FILE_NAME); // set dev key file name
|
||||
#else
|
||||
rc |= device_info_copy(sg_device_secret, device_info->device_secret, MAX_SIZE_OF_DEVICE_SECRET); // set dev secret
|
||||
#endif
|
||||
|
||||
#else
|
||||
rc = iot_save_devinfo_to_json_file(device_info);
|
||||
#endif
|
||||
|
||||
if (rc) {
|
||||
Log_e("Set device info err");
|
||||
rc = QCLOUD_ERR_DEV_INFO;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get device info
|
||||
*
|
||||
* @param[in] dev_info buffer to save device info
|
||||
* @return @see IotReturnCode
|
||||
*/
|
||||
int HAL_GetDevInfo(void *dev_info)
|
||||
{
|
||||
POINTER_SANITY_CHECK(dev_info, QCLOUD_ERR_DEV_INFO);
|
||||
|
||||
int rc;
|
||||
DeviceInfo *device_info = (DeviceInfo *)dev_info;
|
||||
memset((char *)device_info, '\0', sizeof(DeviceInfo));
|
||||
|
||||
#ifdef DEBUG_DEV_INFO_USED
|
||||
rc = device_info_copy(device_info->product_id, sg_product_id, MAX_SIZE_OF_PRODUCT_ID); // get product ID
|
||||
rc |= device_info_copy(device_info->device_name, sg_device_name, MAX_SIZE_OF_DEVICE_NAME); // get dev name
|
||||
|
||||
#ifdef DEV_DYN_REG_ENABLED
|
||||
rc |=
|
||||
device_info_copy(device_info->product_secret, sg_product_secret, MAX_SIZE_OF_PRODUCT_SECRET); // get product ID
|
||||
#endif
|
||||
|
||||
#ifdef AUTH_MODE_CERT
|
||||
rc |= device_info_copy(device_info->dev_cert_file_name, sg_device_cert_file_name,
|
||||
MAX_SIZE_OF_DEVICE_CERT_FILE_NAME); // get dev cert file name
|
||||
rc |= device_info_copy(device_info->dev_key_file_name, sg_device_privatekey_file_name,
|
||||
MAX_SIZE_OF_DEVICE_SECRET_FILE_NAME); // get dev key file name
|
||||
#else
|
||||
rc |= device_info_copy(device_info->device_secret, sg_device_secret, MAX_SIZE_OF_DEVICE_SECRET); // get dev secret
|
||||
#endif
|
||||
|
||||
#else
|
||||
// get devinfo from file
|
||||
rc = HAL_GetDevInfoFromFile(sg_device_info_file, device_info);
|
||||
#endif
|
||||
|
||||
if (rc) {
|
||||
Log_e("Get device info err");
|
||||
rc = QCLOUD_ERR_DEV_INFO;
|
||||
}
|
||||
return rc;
|
||||
}
|
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* @file HAL_File_Linux.c
|
||||
* @author {hubert} ({hubertxxu@tencent.com})
|
||||
* @brief
|
||||
* @version 1.0
|
||||
* @date 2022-01-11
|
||||
*
|
||||
* @copyright
|
||||
*
|
||||
* Tencent is pleased to support the open source community by making IoT Hub available.
|
||||
* Copyright(C) 2018 - 2021 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.
|
||||
*
|
||||
* @par Change File:
|
||||
* <table>
|
||||
* Date Version Author Description
|
||||
* 2022-01-11 1.0 hubertxxu first commit
|
||||
* </table>
|
||||
*/
|
||||
|
||||
#include "qcloud_iot_platform.h"
|
||||
|
||||
/**
|
||||
* @brief Functions for saving file into NVS(files/FLASH)
|
||||
* @param[in] filename file path name
|
||||
* @param[in] write_buffer source need write buffer
|
||||
* @param[in] len length of file to save
|
||||
* @return length of data save when success, or 0 for failure
|
||||
*/
|
||||
size_t HAL_File_Save(const char *filename, const char *write_buffer, size_t write_len)
|
||||
{
|
||||
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] len length of log to read
|
||||
* @return length of data read when success, or 0 for failure
|
||||
*/
|
||||
size_t HAL_File_Read(const char *filename, char *buff, size_t read_len)
|
||||
{
|
||||
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_Get_Size(const char *filename)
|
||||
{
|
||||
return 0;
|
||||
}
|
@@ -0,0 +1,392 @@
|
||||
/**
|
||||
* @copyright
|
||||
*
|
||||
* Tencent is pleased to support the open source community by making IoT Hub available.
|
||||
* Copyright(C) 2018 - 2021 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_linux.c
|
||||
* @brief Linux os api
|
||||
* @author fancyxu (fancyxu@tencent.com)
|
||||
* @version 1.0
|
||||
* @date 2021-05-31
|
||||
*
|
||||
* @par Change Log:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2021-05-31 <td>1.0 <td>fancyxu <td>first commit
|
||||
* <tr><td>2021-07-08 <td>1.1 <td>fancyxu <td>fix code standard of IotReturnCode
|
||||
* </table>
|
||||
*/
|
||||
|
||||
#include "tos_k.h"
|
||||
|
||||
|
||||
#include "qcloud_iot_platform.h"
|
||||
|
||||
/**
|
||||
* @brief Mutex create.
|
||||
*
|
||||
* @return pointer to mutex
|
||||
*/
|
||||
void *HAL_MutexCreate(void)
|
||||
{
|
||||
#ifdef MULTITHREAD_ENABLED
|
||||
k_err_t ret;
|
||||
|
||||
k_mutex_t *mutex = (k_mutex_t *)HAL_Malloc(sizeof(k_mutex_t));
|
||||
if (!mutex) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = tos_mutex_create(mutex);
|
||||
if (ret != K_ERR_NONE) {
|
||||
HAL_Free(mutex);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return mutex;
|
||||
#else
|
||||
return (void *)0xFFFFFFFF;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Mutex destroy.
|
||||
*
|
||||
* @param[in,out] mutex pointer to mutex
|
||||
*/
|
||||
void HAL_MutexDestroy(void *mutex)
|
||||
{
|
||||
if (!mutex) {
|
||||
return;
|
||||
}
|
||||
#ifdef MULTITHREAD_ENABLED
|
||||
k_err_t err = tos_mutex_destroy((k_mutex_t *)mutex);
|
||||
if (err != K_ERR_NONE) {
|
||||
}
|
||||
|
||||
HAL_Free(mutex);
|
||||
#else
|
||||
return;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Mutex lock.
|
||||
*
|
||||
* @param[in,out] mutex pointer to mutex
|
||||
*/
|
||||
void HAL_MutexLock(void *mutex)
|
||||
{
|
||||
if (!mutex) {
|
||||
return;
|
||||
}
|
||||
#ifdef MULTITHREAD_ENABLED
|
||||
k_err_t err = tos_mutex_pend((k_mutex_t *)mutex);
|
||||
if (err != K_ERR_NONE && err != K_ERR_MUTEX_NESTING) {
|
||||
return;
|
||||
}
|
||||
#else
|
||||
return;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Mutex try lock.
|
||||
*
|
||||
* @param[in,out] mutex pointer to mutex
|
||||
* @return 0 for success
|
||||
*/
|
||||
int HAL_MutexTryLock(void *mutex)
|
||||
{
|
||||
if (!mutex) {
|
||||
return -1;
|
||||
}
|
||||
#ifdef MULTITHREAD_ENABLED
|
||||
k_err_t err = tos_mutex_pend_timed((k_mutex_t *)mutex, 0);
|
||||
if (err != K_ERR_NONE && err != K_ERR_MUTEX_NESTING) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Mutex unlock.
|
||||
*
|
||||
* @param[in,out] mutex pointer to mutex
|
||||
*/
|
||||
void HAL_MutexUnlock(void *mutex)
|
||||
{
|
||||
if (!mutex) {
|
||||
return;
|
||||
}
|
||||
#ifdef MULTITHREAD_ENABLED
|
||||
k_err_t err = tos_mutex_post((k_mutex_t *)mutex);
|
||||
if (err != K_ERR_NONE && err != K_ERR_MUTEX_NESTING) {
|
||||
return;
|
||||
}
|
||||
#else
|
||||
return;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @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)
|
||||
{
|
||||
if (ptr)
|
||||
tos_mmheap_free(ptr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 Sleep for ms
|
||||
*
|
||||
* @param[in] ms ms to sleep
|
||||
*/
|
||||
void HAL_SleepMs(uint32_t ms)
|
||||
{
|
||||
tos_sleep_ms(ms);
|
||||
}
|
||||
|
||||
#ifdef MULTITHREAD_ENABLED
|
||||
|
||||
/**
|
||||
* @brief platform-dependent thread create function
|
||||
*
|
||||
* @param[in,out] params params to create thread @see ThreadParams
|
||||
* @return @see IotReturnCode
|
||||
*/
|
||||
int HAL_ThreadCreate(ThreadParams *params)
|
||||
{
|
||||
if (params == NULL)
|
||||
return QCLOUD_ERR_INVAL;
|
||||
|
||||
k_err_t err;
|
||||
|
||||
params->thread_id = (k_task_t *)HAL_Malloc(sizeof(k_task_t *));
|
||||
if (params->thread_id == NULL) {
|
||||
return QCLOUD_ERR_FAILURE;
|
||||
}
|
||||
|
||||
err = tos_task_create((k_task_t *)¶ms->thread_id, params->thread_name, params->thread_func, (void *)params->user_arg,
|
||||
params->priority, (k_stack_t *)params->stack_base, (size_t)params->stack_size, 0);
|
||||
if (err != K_ERR_NONE) {
|
||||
return QCLOUD_ERR_FAILURE;
|
||||
}
|
||||
|
||||
return QCLOUD_RET_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief platform-dependent thread destroy function.
|
||||
*
|
||||
*/
|
||||
void HAL_ThreadDestroy(void *thread_id) {
|
||||
|
||||
tos_task_destroy((k_task_t *)thread_id);
|
||||
|
||||
HAL_Free(thread_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief platform-dependent semaphore create function.
|
||||
*
|
||||
* @return pointer to semaphore
|
||||
*/
|
||||
void *HAL_SemaphoreCreate(void)
|
||||
{
|
||||
k_sem_t *sem = (k_sem_t *)HAL_Malloc(sizeof(k_sem_t));
|
||||
if (!sem) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (tos_sem_create(sem, 0)) {
|
||||
HAL_Free(sem);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return sem;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief platform-dependent semaphore destory function.
|
||||
*
|
||||
* @param[in] sem pointer to semaphore
|
||||
*/
|
||||
void HAL_SemaphoreDestroy(void *sem)
|
||||
{
|
||||
tos_sem_destroy((k_sem_t *)sem);
|
||||
HAL_Free(sem);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief platform-dependent semaphore post function.
|
||||
*
|
||||
* @param[in] sem pointer to semaphore
|
||||
*/
|
||||
void HAL_SemaphorePost(void *sem)
|
||||
{
|
||||
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)
|
||||
{
|
||||
k_err_t err;
|
||||
|
||||
err = tos_sem_pend(sem, timeout_ms);
|
||||
|
||||
return err == K_ERR_NONE ? QCLOUD_RET_SUCCESS : QCLOUD_ERR_FAILURE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Mail buffer in tos.
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
long int type;
|
||||
uint8_t data[2048];
|
||||
} MailBuffer;
|
||||
|
||||
/**
|
||||
* @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_err_t err;
|
||||
k_mail_q_t *mail_q;
|
||||
|
||||
mail_q = HAL_Malloc(sizeof(k_mail_q_t));
|
||||
if (!mail_q) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
err = tos_mail_q_create(mail_q, pool, mail_count, mail_size);
|
||||
if (err != K_ERR_NONE) {
|
||||
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)
|
||||
{
|
||||
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, void *buf, size_t size)
|
||||
{
|
||||
k_err_t err;
|
||||
|
||||
err = tos_mail_q_post(mail_q, buf, size);
|
||||
|
||||
return err == K_ERR_NONE ? 0 : -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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, int timeout_ms)
|
||||
{
|
||||
k_err_t err;
|
||||
|
||||
err = tos_mail_q_pend((k_mail_q_t *)mail_q, buf, size, timeout_ms);
|
||||
|
||||
return err == K_ERR_NONE ? 0 : -1;
|
||||
}
|
||||
|
||||
#endif
|
@@ -0,0 +1,122 @@
|
||||
/**
|
||||
* @copyright
|
||||
*
|
||||
* Tencent is pleased to support the open source community by making IoT Hub available.
|
||||
* Copyright(C) 2018 - 2021 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_linux.c
|
||||
* @brief Linux tcp api
|
||||
* @author fancyxu (fancyxu@tencent.com)
|
||||
* @version 1.0
|
||||
* @date 2021-05-31
|
||||
*
|
||||
* @par Change Log:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2021-05-31 <td>1.0 <td>fancyxu <td>first commit
|
||||
* <tr><td>2021-07-09 <td>1.1 <td>fancyxu <td>refactor for support tls, change port to str format
|
||||
* </table>
|
||||
*/
|
||||
|
||||
#include "sal_module_wrapper.h"
|
||||
#include "qcloud_iot_platform.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 rc;
|
||||
int fd = 0;
|
||||
char host_ip[32];
|
||||
|
||||
rc = tos_sal_module_parse_domain(host, host_ip, 32);
|
||||
if (rc < 0) {
|
||||
return QCLOUD_ERR_TCP_UNKNOWN_HOST;
|
||||
}
|
||||
|
||||
fd = tos_sal_module_connect(host_ip, port, TOS_SAL_PROTO_TCP);
|
||||
if (fd < 0) {
|
||||
return QCLOUD_ERR_TCP_CONNECT;
|
||||
}
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TCP disconnect
|
||||
*
|
||||
* @param[in] fd socket fd
|
||||
* @return 0 for success
|
||||
*/
|
||||
int HAL_TCP_Disconnect(int fd)
|
||||
{
|
||||
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 uint8_t *buf, uint32_t len, uint32_t timeout_ms, size_t *written_len)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
ret = tos_sal_module_send(fd, buf, len);
|
||||
if (ret < 0) {
|
||||
ret = QCLOUD_ERR_TCP_WRITE_FAIL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
*(int *)written_len = ret;
|
||||
// We always know hom much should write.
|
||||
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, uint8_t *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;
|
||||
} else if (ret == 0) {
|
||||
return QCLOUD_ERR_TCP_NOTHING_TO_READ;
|
||||
}
|
||||
|
||||
*(int *)read_len = ret;
|
||||
|
||||
// We always don't know hom much should read.
|
||||
return QCLOUD_RET_SUCCESS;
|
||||
}
|
@@ -0,0 +1,171 @@
|
||||
/**
|
||||
* @copyright
|
||||
*
|
||||
* Tencent is pleased to support the open source community by making IoT Hub available.
|
||||
* Copyright(C) 2018 - 2021 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_linux.c
|
||||
* @brief Linux timer function
|
||||
* @author fancyxu (fancyxu@tencent.com)
|
||||
* @version 1.0
|
||||
* @date 2021-05-31
|
||||
*
|
||||
* @par Change Log:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2021-05-31 <td>1.0 <td>fancyxu <td>first commit
|
||||
* </table>
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "tos_k.h"
|
||||
|
||||
#include "qcloud_iot_platform.h"
|
||||
|
||||
#define HAL_TIMESPEC_TO_TIMEVAL(ts, tv) \
|
||||
{ \
|
||||
tv.tv_sec = ts.tv_sec; \
|
||||
tv.tv_usec = ts.tv_nsec / 1000; \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return if timer expired.
|
||||
*
|
||||
* @param[in] timer @see Timer
|
||||
* @return true expired
|
||||
* @return false no expired
|
||||
*/
|
||||
bool HAL_Timer_Expired(Timer *timer)
|
||||
{
|
||||
k_tick_t now;
|
||||
|
||||
now = tos_systick_get();
|
||||
|
||||
return now >= timer->end_time ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Countdown ms.
|
||||
*
|
||||
* @param[in,out] timer @see Timer
|
||||
* @param[in] timeout_ms ms to count down
|
||||
*/
|
||||
void HAL_Timer_CountdownMs(Timer *timer, unsigned int timeout_ms)
|
||||
{
|
||||
k_tick_t tick;
|
||||
k_tick_t now;
|
||||
|
||||
tick = tos_millisec2tick(timeout_ms);
|
||||
now = tos_systick_get();
|
||||
|
||||
timer->end_time = now + tick;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Countdown second
|
||||
*
|
||||
* @param[in,out] timer @see Timer
|
||||
* @param[in] timeout second to count down
|
||||
*/
|
||||
void HAL_Timer_Countdown(Timer *timer, unsigned int timeout)
|
||||
{
|
||||
k_tick_t now;
|
||||
k_tick_t tick;
|
||||
|
||||
tick = timeout * TOS_CFG_CPU_TICK_PER_SECOND;
|
||||
now = tos_systick_get();
|
||||
|
||||
timer->end_time = now + tick;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Timer remain ms.
|
||||
*
|
||||
* @param[in] timer @see Timer
|
||||
* @return ms
|
||||
*/
|
||||
uint32_t HAL_Timer_Remain(Timer *timer)
|
||||
{
|
||||
k_tick_t now;
|
||||
|
||||
now = tos_systick_get();
|
||||
if (now >= timer->end_time) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
now = tos_systick_get();
|
||||
|
||||
return (k_time_t)(((timer->end_time) - now + TOS_CFG_CPU_TICK_PER_SECOND - 1) / TOS_CFG_CPU_TICK_PER_SECOND);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief time format string
|
||||
*
|
||||
* @return time format string, such as "2021-05-31 15:58:46"
|
||||
*/
|
||||
char *HAL_Timer_Current(void)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get current utf timestamp of second
|
||||
*
|
||||
* @return timestamp
|
||||
*/
|
||||
uint32_t HAL_Timer_CurrentSec(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get utc time ms timestamp.
|
||||
*
|
||||
* @return timestamp
|
||||
*/
|
||||
uint64_t HAL_Timer_CurrentMs(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set system time using second timestamp
|
||||
*
|
||||
* @param[in] timestamp_sec timestamp to set
|
||||
* @return 0 for success
|
||||
*/
|
||||
int HAL_Timer_SetSystimeSec(uint32_t timestamp_sec)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set system time using ms timestamp
|
||||
*
|
||||
* @param[in] timestamp_ms
|
||||
* @return 0 for success
|
||||
*/
|
||||
int HAL_Timer_SetSystimeMs(uint64_t timestamp_ms)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user