HAL_OS_rtthread.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Copyright (c) 2006-2018 RT-Thread Development Team. All rights reserved.
  3. * License-Identifier: Apache-2.0
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. * not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <stdarg.h>
  21. #include <string.h>
  22. #include <sys/time.h>
  23. #include "iot_import.h"
  24. #include <rtthread.h>
  25. #ifdef MQTT_ID2_AUTH
  26. #include "tfs.h"
  27. #endif /**< MQTT_ID2_AUTH*/
  28. #define __DEMO__
  29. #ifdef __DEMO__
  30. char _product_key[PRODUCT_KEY_LEN + 1];
  31. char _product_secret[PRODUCT_SECRET_LEN + 1];
  32. char _device_name[DEVICE_NAME_LEN + 1];
  33. char _device_secret[DEVICE_SECRET_LEN + 1];
  34. #define UNUSED(expr) do { (void)(expr); } while (0)
  35. #endif
  36. #undef perror
  37. #define perror rt_kprintf
  38. #define HAL_OS_LOG_MAXLEN 512
  39. static char log_buf[HAL_OS_LOG_MAXLEN];
  40. void *HAL_MutexCreate(void)
  41. {
  42. rt_mutex_t mutex= rt_mutex_create("ali_ld_mutex", RT_IPC_FLAG_FIFO);
  43. if (NULL == mutex) {
  44. return NULL;
  45. }
  46. return mutex;
  47. }
  48. void HAL_MutexDestroy(_IN_ void *mutex)
  49. {
  50. int err_num;
  51. err_num = err_num;
  52. if (0 != (err_num = rt_mutex_delete((rt_mutex_t)mutex))) {
  53. perror("destroy mutex failed");
  54. }
  55. }
  56. void HAL_MutexLock(_IN_ void *mutex)
  57. {
  58. int err_num;
  59. err_num = err_num;
  60. if (0 != (err_num = rt_mutex_take((rt_mutex_t)mutex, RT_WAITING_FOREVER))) {
  61. perror("lock mutex failed");
  62. }
  63. }
  64. void HAL_MutexUnlock(_IN_ void *mutex)
  65. {
  66. int err_num;
  67. err_num = err_num;
  68. if (0 != (err_num = rt_mutex_release((rt_mutex_t)mutex))) {
  69. perror("unlock mutex failed");
  70. }
  71. }
  72. void *HAL_Malloc(_IN_ uint32_t size)
  73. {
  74. return rt_malloc(size);
  75. }
  76. void HAL_Free(_IN_ void *ptr)
  77. {
  78. rt_free(ptr);
  79. }
  80. uint64_t HAL_UptimeMs(void)
  81. {
  82. #if (RT_TICK_PER_SECOND == 1000)
  83. return (uint64_t)rt_tick_get();
  84. #else
  85. uint64_t tick;
  86. tick = rt_tick_get();
  87. tick = tick * 1000;
  88. return (tick + RT_TICK_PER_SECOND - 1)/RT_TICK_PER_SECOND;
  89. #endif
  90. }
  91. void HAL_SleepMs(_IN_ uint32_t ms)
  92. {
  93. rt_thread_delay(rt_tick_from_millisecond(ms));
  94. }
  95. void HAL_Srandom(uint32_t seed)
  96. {
  97. srand(seed);
  98. }
  99. uint32_t HAL_Random(uint32_t region)
  100. {
  101. return (region > 0) ? (rand() % region) : 0;
  102. }
  103. int HAL_Snprintf(_IN_ char *str, const int len, const char *fmt, ...)
  104. {
  105. va_list args;
  106. int rc;
  107. va_start(args, fmt);
  108. rc = rt_vsnprintf(str, len, fmt, args);
  109. va_end(args);
  110. return rc;
  111. }
  112. int HAL_Vsnprintf(_IN_ char *str, _IN_ const int len, _IN_ const char *format, va_list ap)
  113. {
  114. return rt_vsnprintf(str, len, format, ap);
  115. }
  116. void HAL_Printf(_IN_ const char *fmt, ...)
  117. {
  118. va_list args;
  119. va_start(args, fmt);
  120. rt_vsnprintf(log_buf, HAL_OS_LOG_MAXLEN, fmt, args);
  121. va_end(args);
  122. rt_kprintf("%s", log_buf);
  123. }
  124. int HAL_GetPartnerID(char* pid_str)
  125. {
  126. memset(pid_str, 0x0, PID_STRLEN_MAX);
  127. #ifdef __DEMO__
  128. strcpy(pid_str, "example.demo.partner-id");
  129. #endif
  130. return strlen(pid_str);
  131. }
  132. int HAL_GetModuleID(char* mid_str)
  133. {
  134. memset(mid_str, 0x0, MID_STRLEN_MAX);
  135. #ifdef __DEMO__
  136. strcpy(mid_str, "example.demo.module-id");
  137. #endif
  138. return strlen(mid_str);
  139. }
  140. char *HAL_GetChipID(_OU_ char* cid_str)
  141. {
  142. memset(cid_str, 0x0, HAL_CID_LEN);
  143. #ifdef __DEMO__
  144. strncpy(cid_str, "rtl8188eu 12345678", HAL_CID_LEN);
  145. cid_str[HAL_CID_LEN - 1] = '\0';
  146. #endif
  147. return cid_str;
  148. }
  149. int HAL_GetDeviceID(_OU_ char* device_id)
  150. {
  151. memset(device_id, 0x0, DEVICE_ID_LEN);
  152. #ifdef __DEMO__
  153. HAL_Snprintf(device_id, DEVICE_ID_LEN, "%s.%s", _product_key, _device_name);
  154. device_id[DEVICE_ID_LEN - 1] = '\0';
  155. #endif
  156. return strlen(device_id);
  157. }
  158. #ifdef MQTT_ID2_AUTH
  159. int HAL_GetID2(_OU_ char* id2_str)
  160. {
  161. int rc;
  162. uint8_t id2[TFS_ID2_LEN + 1] = {0};
  163. uint32_t id2_len = TFS_ID2_LEN + 1;
  164. memset(id2_str, 0x0, TFS_ID2_LEN + 1);
  165. #ifdef __DEMO__
  166. rc = tfs_get_ID2(id2, &id2_len);
  167. if (rc < 0) return rc;
  168. strncpy(id2_str, (const char*)id2, TFS_ID2_LEN);
  169. #endif
  170. return strlen(id2_str);
  171. }
  172. #endif /**< MQTT_ID2_AUTH*/
  173. int HAL_SetProductKey(_IN_ char* product_key)
  174. {
  175. unsigned int written_len = 0;
  176. written_len = written_len;
  177. int len = strlen(product_key);
  178. #ifdef __DEMO__
  179. if (len > PRODUCT_KEY_LEN) return -1;
  180. memset(_product_key, 0x0, PRODUCT_KEY_LEN + 1);
  181. strncpy(_product_key, product_key, len);
  182. #endif
  183. return len;
  184. }
  185. int HAL_SetDeviceName(_IN_ char* device_name)
  186. {
  187. unsigned int written_len = 0;
  188. written_len = written_len;
  189. int len = strlen(device_name);
  190. #ifdef __DEMO__
  191. if (len > DEVICE_NAME_LEN) return -1;
  192. memset(_device_name, 0x0, DEVICE_NAME_LEN + 1);
  193. strncpy(_device_name, device_name, len);
  194. #endif
  195. return len;
  196. }
  197. int HAL_SetDeviceSecret(_IN_ char* device_secret)
  198. {
  199. unsigned int written_len = 0;
  200. written_len = written_len;
  201. int len = strlen(device_secret);
  202. #ifdef __DEMO__
  203. if (len > DEVICE_SECRET_LEN) return -1;
  204. memset(_device_secret, 0x0, DEVICE_SECRET_LEN + 1);
  205. strncpy(_device_secret, device_secret, len);
  206. #endif
  207. return len;
  208. }
  209. int HAL_SetProductSecret(_IN_ char* product_secret)
  210. {
  211. unsigned int written_len = 0;
  212. written_len = written_len;
  213. int len = strlen(product_secret);
  214. #ifdef __DEMO__
  215. if (len > PRODUCT_SECRET_LEN) return -1;
  216. memset(_product_secret, 0x0, PRODUCT_SECRET_LEN + 1);
  217. strncpy(_product_secret, product_secret, len);
  218. #endif
  219. return len;
  220. }
  221. int HAL_GetProductKey(_OU_ char* product_key)
  222. {
  223. int len;
  224. UNUSED(len);
  225. memset(product_key, 0x0, PRODUCT_KEY_LEN);
  226. #ifdef __DEMO__
  227. strncpy(product_key, _product_key, PRODUCT_KEY_LEN);
  228. product_key[PRODUCT_KEY_LEN] = '\0';
  229. #endif
  230. return strlen(product_key);
  231. }
  232. int HAL_GetProductSecret(_OU_ char* product_secret)
  233. {
  234. int len;
  235. UNUSED(len);
  236. memset(product_secret, 0x0, PRODUCT_SECRET_LEN);
  237. #ifdef __DEMO__
  238. strncpy(product_secret, _product_secret, PRODUCT_SECRET_LEN);
  239. product_secret[PRODUCT_SECRET_LEN] = '\0';
  240. #endif
  241. return strlen(product_secret);
  242. }
  243. int HAL_GetDeviceName(_OU_ char* device_name)
  244. {
  245. int len;
  246. UNUSED(len);
  247. memset(device_name, 0x0, DEVICE_NAME_LEN);
  248. #ifdef __DEMO__
  249. strncpy(device_name, _device_name, DEVICE_NAME_LEN);
  250. device_name[DEVICE_NAME_LEN] = '\0';
  251. #endif
  252. return strlen(device_name);
  253. }
  254. int HAL_GetDeviceSecret(_OU_ char* device_secret)
  255. {
  256. int len;
  257. UNUSED(len);
  258. memset(device_secret, 0x0, DEVICE_SECRET_LEN);
  259. #ifdef __DEMO__
  260. strncpy(device_secret, _device_secret, DEVICE_SECRET_LEN);
  261. device_secret[DEVICE_SECRET_LEN] = '\0';
  262. #endif
  263. return strlen(device_secret);
  264. }
  265. int HAL_GetFirmwareVesion(_OU_ char* version)
  266. {
  267. char *ver = "1.0";
  268. int len = strlen(ver);
  269. memset(version, 0x0, FIRMWARE_VERSION_MAXLEN);
  270. #ifdef __DEMO__
  271. strncpy(version, ver, len);
  272. version[len] = '\0';
  273. #endif
  274. return strlen(version);
  275. }
  276. #define otafilename "/tmp/alinkota.bin"
  277. void HAL_Firmware_Persistence_Start(void)
  278. {
  279. return;
  280. }
  281. int HAL_Firmware_Persistence_Write(_IN_ char *buffer, _IN_ uint32_t length)
  282. {
  283. return 0;
  284. }
  285. int HAL_Firmware_Persistence_Stop(void)
  286. {
  287. /* check file md5, and burning it to flash ... finally reboot system */
  288. return 0;
  289. }