Files
David Lin 2533fdd50e Update osal_os.c
strncpy(version, ver, IOTX_FIRMWARE_VER_LEN)
->
strncpy(version, ver, len);
2020-07-17 19:00:41 +08:00

235 lines
5.7 KiB
C

/*
* 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 <stdarg.h>
#include <string.h>
#include "tos_k.h"
#include "infra_defs.h"
#ifdef DYNAMIC_REGISTER
char _product_key[IOTX_PRODUCT_KEY_LEN + 1] = "a1ZETBPbycq";
char _product_secret[IOTX_PRODUCT_SECRET_LEN + 1] = "L68wCVXYUaNg1Ey9";
char _device_name[IOTX_DEVICE_NAME_LEN + 1] = "example1";
char _device_secret[IOTX_DEVICE_SECRET_LEN + 1] = "";
#else
#ifdef DEVICE_MODEL_ENABLED
char _product_key[IOTX_PRODUCT_KEY_LEN + 1] = "a1RIsMLz2BJ";
char _product_secret[IOTX_PRODUCT_SECRET_LEN + 1] = "fSAF0hle6xL0oRWd";
char _device_name[IOTX_DEVICE_NAME_LEN + 1] = "example1";
char _device_secret[IOTX_DEVICE_SECRET_LEN + 1] = "RDXf67itLqZCwdMCRrw0N5FHbv5D7jrE";
#else
char _product_key[IOTX_PRODUCT_KEY_LEN + 1] = "a1MZxOdcBnO";
char _product_secret[IOTX_PRODUCT_SECRET_LEN + 1] = "h4I4dneEFp7EImTv";
char _device_name[IOTX_DEVICE_NAME_LEN + 1] = "test_01";
char _device_secret[IOTX_DEVICE_SECRET_LEN + 1] = "t9GmMf2jb3LgWfXBaZD2r3aJrfVWBv56";
#endif
#endif
void *HAL_Malloc(uint32_t size);
void HAL_Free(void *ptr);
void HAL_Printf(const char *fmt, ...);
void HAL_Printf(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}
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;
}
int HAL_Vsnprintf(char *str, const int len, const char *format, va_list ap)
{
return vsnprintf(str, len, format, ap);
}
int HAL_GetModuleID(char *mid_str)
{
memset(mid_str, 0x0, IOTX_MODULE_ID_LEN);
strcpy(mid_str, "c-sdk-2.3.0-mid");
return strlen(mid_str);
}
int HAL_SetProductKey(char *product_key)
{
int len = strlen(product_key);
if (len > IOTX_PRODUCT_KEY_LEN) {
return -1;
}
memset(_product_key, 0x0, IOTX_PRODUCT_KEY_LEN + 1);
strncpy(_product_key, product_key, len);
return len;
}
int HAL_SetDeviceName(char *device_name)
{
int len = strlen(device_name);
if (len > IOTX_DEVICE_NAME_LEN) {
return -1;
}
memset(_device_name, 0x0, IOTX_DEVICE_NAME_LEN + 1);
strncpy(_device_name, device_name, len);
return len;
}
int HAL_SetProductSecret(char *product_secret)
{
int len = strlen(product_secret);
if (len > IOTX_PRODUCT_SECRET_LEN) {
return -1;
}
memset(_product_secret, 0x0, IOTX_PRODUCT_SECRET_LEN + 1);
strncpy(_product_secret, product_secret, len);
return len;
}
int HAL_SetDeviceSecret(char *device_secret)
{
int len = strlen(device_secret);
if (len > IOTX_DEVICE_SECRET_LEN) {
return -1;
}
memset(_device_secret, 0x0, IOTX_DEVICE_SECRET_LEN + 1);
strncpy(_device_secret, device_secret, len);
return len;
}
int HAL_GetProductKey(char product_key[IOTX_PRODUCT_KEY_LEN + 1])
{
int len = strlen(_product_key);
memset(product_key, 0x0, IOTX_PRODUCT_KEY_LEN + 1);
strncpy(product_key, _product_key, len);
return len;
}
int HAL_GetProductSecret(char product_secret[IOTX_PRODUCT_SECRET_LEN + 1])
{
int len = strlen(_product_secret);
memset(product_secret, 0x0, IOTX_PRODUCT_SECRET_LEN + 1);
strncpy(product_secret, _product_secret, len);
return len;
}
int HAL_GetDeviceName(char device_name[IOTX_DEVICE_NAME_LEN + 1])
{
int len = strlen(_device_name);
memset(device_name, 0x0, IOTX_DEVICE_NAME_LEN + 1);
strncpy(device_name, _device_name, len);
return strlen(device_name);
}
int HAL_GetDeviceSecret(char device_secret[IOTX_DEVICE_SECRET_LEN + 1])
{
int len = strlen(_device_secret);
memset(device_secret, 0x0, IOTX_DEVICE_SECRET_LEN + 1);
strncpy(device_secret, _device_secret, len);
return len;
}
int HAL_GetFirmwareVersion(char *version)
{
char *ver = "app-1.0.0-20180101.1000";
int len = strlen(ver);
memset(version, 0x0, IOTX_FIRMWARE_VER_LEN);
strncpy(version, ver, len);
version[len] = '\0';
return strlen(version);
}
void *HAL_MutexCreate(void)
{
k_mutex_t *mutex;
mutex = (k_mutex_t *)HAL_Malloc(sizeof(k_mutex_t));
tos_mutex_create(mutex);
return (void *)mutex;
}
void HAL_MutexDestroy(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(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);
}
}
void HAL_MutexUnlock(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);
}
}
void *HAL_Malloc(uint32_t size)
{
return tos_mmheap_alloc(size);
}
void HAL_Free(void *ptr)
{
tos_mmheap_free(ptr);
}
void HAL_SleepMs(uint32_t ms)
{
(void)tos_sleep_hmsm(0,0,0, ms);
}