HAL_OS_rtthread.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (C) 2012-2019 UCloud. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <stdarg.h>
  19. #include "uiot_defs.h"
  20. #include "stdint.h"
  21. #include <rtthread.h>
  22. #define DEBUG_DEV_INFO_USED
  23. // 测试时可在此处填入设备四元组
  24. #ifdef DEBUG_DEV_INFO_USED
  25. /* 产品序列号 */
  26. static char sg_product_sn[IOT_PRODUCT_SN_LEN + 1] = "YOUR_PRODUCT_SN";
  27. /* 产品密钥 */
  28. static char sg_product_secret[IOT_PRODUCT_SECRET_LEN + 1] = "PRODUCT_SECRET";
  29. /* 设备序列号 */
  30. static char sg_device_sn[IOT_DEVICE_SN_LEN + 1] = "YOUR_DEVICE_SN";
  31. /* 设备密钥 */
  32. static char sg_device_secret[IOT_DEVICE_SECRET_LEN + 1] = "DEVICE_SECRET";
  33. #endif
  34. static int g_mutexnum = 0;
  35. void *HAL_MutexCreate(void)
  36. {
  37. char name[RT_NAME_MAX];
  38. rt_snprintf(name, RT_NAME_MAX, "mutex%d", g_mutexnum);
  39. g_mutexnum++;
  40. return rt_mutex_create(name, RT_IPC_FLAG_FIFO);
  41. }
  42. void HAL_MutexDestroy(_IN_ void *mutex)
  43. {
  44. rt_mutex_delete((rt_mutex_t)mutex);
  45. return;
  46. }
  47. void HAL_MutexLock(_IN_ void *mutex)
  48. {
  49. rt_mutex_take((rt_mutex_t)mutex, 0xFFFF);
  50. return;
  51. }
  52. void HAL_MutexUnlock(_IN_ void *mutex)
  53. {
  54. rt_mutex_release((rt_mutex_t)mutex);
  55. return;
  56. }
  57. void *HAL_Malloc(_IN_ uint32_t size)
  58. {
  59. return rt_malloc(size);
  60. }
  61. void HAL_Free(_IN_ void *ptr)
  62. {
  63. rt_free(ptr);
  64. }
  65. #define HAL_OS_LOG_MAXLEN 1024
  66. void HAL_Printf(_IN_ const char *fmt, ...)
  67. {
  68. va_list args;
  69. char log_buf[HAL_OS_LOG_MAXLEN];
  70. va_start(args, fmt);
  71. rt_vsnprintf(log_buf, HAL_OS_LOG_MAXLEN, fmt, args);
  72. va_end(args);
  73. printf("%s", log_buf);
  74. }
  75. int HAL_Snprintf(_IN_ char *str, _IN_ const int len, _IN_ const char *fmt, ...) {
  76. va_list args;
  77. int rc;
  78. va_start(args, fmt);
  79. rc = vsnprintf(str, len, fmt, args);
  80. va_end(args);
  81. return rc;
  82. }
  83. int HAL_Vsnprintf(_OU_ char *str, _IN_ const int len, _IN_ const char *format, _IN_ va_list ap) {
  84. return vsnprintf(str, len, format, ap);
  85. }
  86. void HAL_SleepMs(uint32_t ms)
  87. {
  88. rt_thread_mdelay(ms);
  89. }
  90. IoT_Error_t HAL_GetProductSN(_OU_ char productSN[IOT_PRODUCT_SN_LEN + 1]) {
  91. #ifdef DEBUG_DEV_INFO_USED
  92. int len = strlen(sg_product_sn);
  93. memset(productSN, 0x0, IOT_PRODUCT_SN_LEN + 1);
  94. strncpy(productSN, sg_product_sn, len);
  95. return SUCCESS_RET;
  96. #else
  97. HAL_Printf("HAL_GetProductSN is not implement");
  98. return FAILURE_RET;
  99. #endif
  100. }
  101. IoT_Error_t HAL_GetProductSecret(_OU_ char productSecret[IOT_PRODUCT_SECRET_LEN + 1]) {
  102. #ifdef DEBUG_DEV_INFO_USED
  103. int len = strlen(sg_product_secret);
  104. memset(productSecret, 0x0, IOT_PRODUCT_SECRET_LEN + 1);
  105. strncpy(productSecret, sg_product_secret, len);
  106. return SUCCESS_RET;
  107. #else
  108. Log_e("HAL_GetProductSecret is not implement");
  109. return FAILURE_RET;
  110. #endif
  111. }
  112. IoT_Error_t HAL_GetDeviceSN(_OU_ char deviceSN[IOT_DEVICE_SN_LEN + 1]) {
  113. #ifdef DEBUG_DEV_INFO_USED
  114. int len = strlen(sg_device_sn);
  115. memset(deviceSN, 0x0, IOT_DEVICE_SN_LEN + 1);
  116. strncpy(deviceSN, sg_device_sn, len);
  117. return SUCCESS_RET;
  118. #else
  119. HAL_Printf("HAL_GetDeviceSN is not implement");
  120. return FAILURE_RET;
  121. #endif
  122. }
  123. IoT_Error_t HAL_GetDeviceSecret(_OU_ char deviceSecret[IOT_DEVICE_SECRET_LEN + 1]) {
  124. #ifdef DEBUG_DEV_INFO_USED
  125. int len = strlen(sg_device_secret);
  126. memset(deviceSecret, 0x0, IOT_DEVICE_SECRET_LEN + 1);
  127. strncpy(deviceSecret, sg_device_secret, len);
  128. return SUCCESS_RET;
  129. #else
  130. HAL_Printf("HAL_GetDeviceSecret is not implement");
  131. return FAILURE_RET;
  132. #endif
  133. }
  134. IoT_Error_t HAL_SetProductSN(_IN_ const char *pProductSN) {
  135. #ifdef DEBUG_DEV_INFO_USED
  136. int len = strlen(pProductSN);
  137. if (len > IOT_PRODUCT_SN_LEN) {
  138. return FAILURE_RET;
  139. }
  140. memset(sg_product_sn, 0x0, IOT_PRODUCT_SN_LEN + 1);
  141. strncpy(sg_product_sn, pProductSN, len);
  142. return SUCCESS_RET;
  143. #else
  144. HAL_Printf("HAL_SetProductSN is not implement");
  145. return FAILURE_RET;
  146. #endif
  147. }
  148. IoT_Error_t HAL_SetProductSecret(_IN_ const char *pProductSecret) {
  149. #ifdef DEBUG_DEV_INFO_USED
  150. int len = strlen(pProductSecret);
  151. if (len > IOT_PRODUCT_SECRET_LEN) {
  152. return FAILURE_RET;
  153. }
  154. memset(sg_product_secret, 0x0, IOT_PRODUCT_SECRET_LEN + 1);
  155. strncpy(sg_product_secret, pProductSecret, len);
  156. return SUCCESS_RET;
  157. #else
  158. HAL_Printf("HAL_SetProductSecret is not implement");
  159. return FAILURE_RET;
  160. #endif
  161. }
  162. IoT_Error_t HAL_SetDeviceSN(_IN_ const char *pDeviceSN) {
  163. #ifdef DEBUG_DEV_INFO_USED
  164. int len = strlen(pDeviceSN);
  165. if (len > IOT_DEVICE_SN_LEN) {
  166. return FAILURE_RET;
  167. }
  168. memset(sg_device_sn, 0x0, IOT_DEVICE_SN_LEN + 1);
  169. strncpy(sg_device_sn, pDeviceSN, len);
  170. return SUCCESS_RET;
  171. #else
  172. HAL_Printf("HAL_SetDeviceSN is not implement");
  173. return FAILURE_RET;
  174. #endif
  175. }
  176. IoT_Error_t HAL_SetDeviceSecret(_IN_ const char *pDeviceSecret) {
  177. #ifdef DEBUG_DEV_INFO_USED
  178. int len = strlen(pDeviceSecret);
  179. if (len > IOT_DEVICE_SECRET_LEN) {
  180. return FAILURE_RET;
  181. }
  182. memset(sg_device_secret, 0x0, IOT_DEVICE_SECRET_LEN + 1);
  183. strncpy(sg_device_secret, pDeviceSecret, len);
  184. return SUCCESS_RET;
  185. #else
  186. HAL_Printf("HAL_SetDeviceSecret is not implement");
  187. return FAILURE_RET;
  188. #endif
  189. }