qcloud_iot_import.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. * Tencent is pleased to support the open source community by making IoT Hub
  3. available.
  4. * Copyright (C) 2018-2020 THL A29 Limited, a Tencent company. All rights
  5. reserved.
  6. * Licensed under the MIT License (the "License"); you may not use this file
  7. except in
  8. * compliance with the License. You may obtain a copy of the License at
  9. * http://opensource.org/licenses/MIT
  10. * Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is
  12. * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND,
  14. * either express or implied. See the License for the specific language
  15. governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. #ifndef QCLOUD_IOT_IMPORT_H_
  20. #define QCLOUD_IOT_IMPORT_H_
  21. #if defined(__cplusplus)
  22. extern "C" {
  23. #endif
  24. #include <inttypes.h>
  25. #include <stdarg.h>
  26. #include <stdbool.h>
  27. #include <stdint.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. //#include "utlTime.h"
  32. #include "config.h"
  33. #include "platform.h"
  34. #define _IN_ /* indicate an input parameter */
  35. #define _OU_ /* indicate a output parameter */
  36. #define IOT_TRUE (1) /* indicate boolean value true */
  37. #define IOT_FALSE (0) /* indicate boolean value false */
  38. #define Max(a, b) ((a) > (b) ? (a) : (b))
  39. #define Min(a, b) ((a) < (b) ? (a) : (b))
  40. /**********************************************************************
  41. * QCloud IoT C-SDK Hardware Abstraction Layer
  42. * Platform/OS/IP stack/SSL dependant functions
  43. * Check platform folder for reference implementaions
  44. * Require porting when adapt SDK to new platform/OS
  45. *********************************************************************/
  46. typedef void (*ThreadRunFunc)(void *arg);
  47. typedef struct ThreadParams {
  48. char * thread_name;
  49. size_t thread_id;
  50. ThreadRunFunc thread_func;
  51. void * user_arg;
  52. uint16_t priority;
  53. uint32_t stack_size;
  54. } ThreadParams;
  55. /**
  56. * @brief Create a thread/task
  57. *
  58. * @param params thread parameters
  59. * @return 0 when success, or error code otherwise
  60. */
  61. int HAL_ThreadCreate(ThreadParams *params);
  62. /**
  63. * @brief Destroy a thread/task
  64. *
  65. * @return QCLOUD_RET_SUCCESS for success, or err code for failure
  66. */
  67. int HAL_ThreadDestroy(void *thread_t);
  68. /**
  69. * @brief create semaphore
  70. *
  71. * @return a valid semaphore handle when success, or NULL otherwise
  72. */
  73. void *HAL_SemaphoreCreate(void);
  74. /**
  75. * @brief Destroy semaphore
  76. * @param sem semaphore handle
  77. */
  78. void HAL_SemaphoreDestroy(void *sem);
  79. /**
  80. * @brief Post semaphore
  81. * @param sem semaphore handle
  82. */
  83. void HAL_SemaphorePost(void *sem);
  84. /**
  85. * @brief Wait for semaphore
  86. * @param sem semaphore handle
  87. * @param timeout_ms waiting timeout value (unit: ms)
  88. * @return QCLOUD_RET_SUCCESS for success, or err code for failure
  89. */
  90. int HAL_SemaphoreWait(void *sem, uint32_t timeout_ms);
  91. /**
  92. * @brief Create mutex
  93. *
  94. * @return a valid mutex handle when success, or NULL otherwise
  95. */
  96. void *HAL_MutexCreate(void);
  97. /**
  98. * @brief Destroy mutex
  99. *
  100. * @param mutex mutex handle
  101. */
  102. void HAL_MutexDestroy(_IN_ void *mutex);
  103. /**
  104. * @brief Lock a mutex in blocking way
  105. *
  106. * @param mutex mutex handle
  107. */
  108. void HAL_MutexLock(_IN_ void *mutex);
  109. /**
  110. * @brief Lock a mutex in non-blocking way
  111. *
  112. * @param mutex mutex handle
  113. * @return 0 for success, or err code for failure
  114. */
  115. int HAL_MutexTryLock(_IN_ void *mutex);
  116. /**
  117. * @brief Unlock/release mutex
  118. *
  119. * @param mutex mutex handle
  120. */
  121. void HAL_MutexUnlock(_IN_ void *mutex);
  122. /**
  123. * @brief Malloc memory
  124. *
  125. * @param size Expected memory size (unit: byte)
  126. * @return pointer to the memory
  127. */
  128. void *HAL_Malloc(_IN_ uint32_t size);
  129. /**
  130. * @brief Free memory
  131. *
  132. * @param ptr pointer to the pre-malloc memory
  133. */
  134. void HAL_Free(_IN_ void *ptr);
  135. /**
  136. * @brief Print data to console in format
  137. *
  138. * @param fmt print format
  139. * @param ... variable number of arguments
  140. */
  141. void HAL_Printf(_IN_ const char *fmt, ...);
  142. /**
  143. * @brief Print data to string in format
  144. *
  145. * @param str destination string
  146. * @param len Max size of the output
  147. * @param fmt print format
  148. * @param ... variable number of arguments
  149. * @return number of bytes that print successfull
  150. */
  151. int HAL_Snprintf(_IN_ char *str, const int len, const char *fmt, ...);
  152. /**
  153. Print data to string in format
  154. *
  155. * @param str destination string
  156. * @param len Max size of the output
  157. * @param fmt print format
  158. * @param ap arguments list
  159. * @return number of bytes that print successfull
  160. */
  161. int HAL_Vsnprintf(_OU_ char *str, _IN_ const int len, _IN_ const char *fmt, _IN_ va_list ap);
  162. /**
  163. * @brief Get timestamp in millisecond
  164. *
  165. * @return timestamp in millisecond
  166. */
  167. uint32_t HAL_GetTimeMs(void);
  168. /**
  169. * @brief Delay operation in blocking way
  170. *
  171. * @param ms sleep interval in millisecond
  172. */
  173. void HAL_DelayMs(_IN_ uint32_t ms);
  174. /**
  175. * @brief Sleep for a while
  176. *
  177. * @param ms sleep interval in millisecond
  178. */
  179. void HAL_SleepMs(_IN_ uint32_t ms);
  180. /**
  181. * @brief Set device info to NVS(flash/files)
  182. *
  183. * @param pdevInfo reference to device info
  184. * @return QCLOUD_RET_SUCCESS for success, or err code for failure
  185. */
  186. int HAL_SetDevInfo(void *pdevInfo);
  187. /**
  188. * @brief Get device info from NVS(flash/files)
  189. *
  190. * @param pdevInfo reference to device info
  191. * @return QCLOUD_RET_SUCCESS for success, or err code for failure
  192. */
  193. int HAL_GetDevInfo(void *pdevInfo);
  194. /**
  195. * @brief Get device info from a JSON file
  196. *
  197. * @param file_name JSON file path
  198. * @param pdevInfo reference to device info
  199. * @return QCLOUD_RET_SUCCESS for success, or err code for failure
  200. */
  201. int HAL_GetDevInfoFromFile(const char *file_name, void *dev_info);
  202. #ifdef GATEWAY_ENABLED
  203. /**
  204. * @brief Get gateway device info from NVS(flash/files)
  205. *
  206. * @param pgwDeviceInfo reference to gateway device info
  207. * @return QCLOUD_RET_SUCCESS for success, or err code for failure
  208. */
  209. int HAL_GetGwDevInfo(void *pgwDeviceInfo);
  210. /**
  211. * @brief Get gateway device info from NVS(flash/files)
  212. *
  213. * @param pgwDeviceInfo reference to gateway device info
  214. * @return QCLOUD_RET_SUCCESS for success, or err code for failure
  215. */
  216. int HAL_GetGwDevInfoFromFile(const char *file_name, void *pgwDeviceInfo);
  217. #endif
  218. /**
  219. * @brief Set the name of file which contain device info
  220. *
  221. * @param file_name the name of file which contain device info
  222. * @return QCLOUD_RET_SUCCESS for success, or err code for failure
  223. */
  224. int HAL_SetDevInfoFile(const char *file_name);
  225. /**
  226. * Define timer structure, platform dependant
  227. */
  228. struct Timer {
  229. struct timeval end_time;
  230. };
  231. typedef struct Timer Timer;
  232. /**
  233. * @brief Check if timer expires or not
  234. *
  235. * @param timer reference to timer
  236. * @return true = expired, false = not expired yet
  237. */
  238. bool HAL_Timer_expired(Timer *timer);
  239. /**
  240. * @brief Set the countdown/expired value for the timer
  241. *
  242. * @param timer reference to timer
  243. * @param timeout_ms countdown/expired value (unit: millisecond)
  244. */
  245. void HAL_Timer_countdown_ms(Timer *timer, unsigned int timeout_ms);
  246. /**
  247. * @brief Set the countdown/expired value for the timer
  248. *
  249. * @param timer reference to timer
  250. * @param timeout countdown/expired value (unit: second)
  251. */
  252. void HAL_Timer_countdown(Timer *timer, unsigned int timeout);
  253. /**
  254. * @brief Check the remain time of the timer
  255. *
  256. * @param timer reference to timer
  257. * @return 0 if expired, or the left time in millisecond
  258. */
  259. int HAL_Timer_remain(Timer *timer);
  260. /**
  261. * @brief Init the timer
  262. *
  263. * @param timer reference to timer
  264. */
  265. void HAL_Timer_init(Timer *timer);
  266. #define TIME_FORMAT_STR_LEN (20)
  267. /**
  268. * @brief Get local time in format: %y-%m-%d %H:%M:%S
  269. *
  270. * @return string of formatted time
  271. */
  272. char *HAL_Timer_current(char *time_str);
  273. /**
  274. * @brief Get timestamp in second
  275. *
  276. * @return timestamp in second
  277. */
  278. long HAL_Timer_current_sec(void);
  279. #ifdef AT_TCP_ENABLED
  280. int HAL_AT_TCP_Init(void);
  281. uintptr_t HAL_AT_TCP_Connect(const char *host, uint16_t port);
  282. int HAL_AT_TCP_Disconnect(uintptr_t fd);
  283. int HAL_AT_TCP_Write(uintptr_t fd, const unsigned char *buf, uint32_t len, uint32_t timeout_ms, size_t *written_len);
  284. int HAL_AT_TCP_Read(uintptr_t fd, uint8_t *buf, uint32_t len, uint32_t timeout_ms, uint32_t *read_len);
  285. int at_device_init(void);
  286. int HAL_AT_Uart_Init(void);
  287. int HAL_AT_Uart_Deinit(void);
  288. int HAL_AT_Uart_Send(void *data, uint32_t size);
  289. int HAL_AT_Uart_Recv(void *data, uint32_t expect_size, uint32_t *recv_size, uint32_t timeout);
  290. #endif
  291. /********** TLS/DTLS network sturcture and operations **********/
  292. #ifndef AUTH_WITH_NOTLS
  293. #ifndef MAX_SIZE_OF_CLIENT_ID
  294. #define MAX_SIZE_OF_CLIENT_ID (80)
  295. #endif
  296. /**
  297. * @brief Define structure for TLS connection parameters
  298. *
  299. */
  300. typedef struct {
  301. unsigned char profile_idx;
  302. const char *ca_crt;
  303. uint16_t ca_crt_len;
  304. #ifdef AUTH_MODE_CERT
  305. /**
  306. * Device with certificate
  307. */
  308. const char *cert_file; // public certificate file
  309. const char *key_file; // pravite certificate file
  310. #else
  311. /**
  312. * Device with PSK
  313. */
  314. const char *psk; // PSK string
  315. char psk_id[MAX_SIZE_OF_CLIENT_ID + 1]; // PSK ID
  316. #endif
  317. size_t psk_length; // PSK length
  318. unsigned int timeout_ms; // SSL handshake timeout in millisecond
  319. } SSLConnectParams;
  320. typedef SSLConnectParams TLSConnectParams;
  321. /**
  322. * @brief Setup TLS connection with server
  323. *
  324. * @param pConnectParams reference to TLS connection parameters
  325. * @host server address
  326. * @port server port
  327. * @return TLS connect handle when success, or 0 otherwise
  328. */
  329. uintptr_t HAL_TLS_Connect(TLSConnectParams *pConnectParams, const char *host, int port);
  330. /**
  331. * @brief Disconnect with TLS server and release resources
  332. *
  333. * @param handle TLS connect handle
  334. */
  335. void HAL_TLS_Disconnect(uintptr_t handle);
  336. /**
  337. * @brief Write data via TLS connection
  338. *
  339. * @param handle TLS connect handle
  340. * @param data source data to write
  341. * @param totalLen length of data
  342. * @param timeout_ms timeout value in millisecond
  343. * @param written_len length of data written successfully
  344. * @return QCLOUD_RET_SUCCESS for success, or err code for failure
  345. */
  346. int HAL_TLS_Write(uintptr_t handle, unsigned char *data, size_t totalLen, uint32_t timeout_ms, size_t *written_len);
  347. /**
  348. * @brief Read data via TLS connection
  349. *
  350. * @param handle TLS connect handle
  351. * @param data destination data buffer where to put data
  352. * @param totalLen length of data
  353. * @param timeout_ms timeout value in millisecond
  354. * @param read_len length of data read successfully
  355. * @return QCLOUD_RET_SUCCESS for success, or err code for failure
  356. */
  357. int HAL_TLS_Read(uintptr_t handle, unsigned char *data, size_t totalLen, uint32_t timeout_ms, size_t *read_len);
  358. /********** DTLS network **********/
  359. #ifdef COAP_COMM_ENABLED
  360. typedef SSLConnectParams DTLSConnectParams;
  361. /**
  362. * @brief Setup DTLS connection with server
  363. *
  364. * @param pConnectParams reference to DTLS connection parameters
  365. * @host server address
  366. * @port server port
  367. * @return DTLS connect handle when success, or 0 otherwise
  368. */
  369. uintptr_t HAL_DTLS_Connect(DTLSConnectParams *pConnectParams, const char *host, int port);
  370. /**
  371. * @brief Disconnect with DTLS server and release resources
  372. *
  373. * @param handle DTLS connect handle
  374. */
  375. void HAL_DTLS_Disconnect(uintptr_t handle);
  376. /**
  377. * @brief Write data via DTLS connection
  378. *
  379. * @param handle DTLS connect handle
  380. * @param data source data to write
  381. * @param totalLen length of data
  382. * @param timeout_ms timeout value in millisecond
  383. * @param written_len length of data written successfully
  384. * @return QCLOUD_RET_SUCCESS for success, or err code for failure
  385. */
  386. int HAL_DTLS_Write(uintptr_t handle, const unsigned char *data, size_t datalen, size_t *written_len);
  387. /**
  388. * @brief Read data via DTLS connection
  389. *
  390. * @param handle DTLS connect handle
  391. * @param data destination data buffer where to put data
  392. * @param totalLen length of data
  393. * @param timeout_ms timeout value in millisecond
  394. * @param read_len length of data read successfully
  395. * @return QCLOUD_RET_SUCCESS for success, or err code for failure
  396. */
  397. int HAL_DTLS_Read(uintptr_t handle, unsigned char *data, size_t datalen, uint32_t timeout_ms, size_t *read_len);
  398. #endif // COAP_COMM_ENABLED
  399. #endif // AUTH_WITH_NOTLS
  400. /********** TCP network **********/
  401. /**
  402. * @brief Setup TCP connection with server
  403. *
  404. * @host server address
  405. * @port server port
  406. * @return TCP socket handle (value>0) when success, or 0 otherwise
  407. */
  408. uintptr_t HAL_TCP_Connect(unsigned char profile_idx, const char *host, uint16_t port);
  409. /**
  410. * @brief Disconnect with server and release resource
  411. *
  412. * @param fd TCP Socket handle
  413. * @return 0 when success
  414. */
  415. int HAL_TCP_Disconnect(uintptr_t fd);
  416. /**
  417. * @brief Write data via TCP connection
  418. *
  419. * @param fd TCP socket handle
  420. * @param data source data to write
  421. * @param len length of data
  422. * @param timeout_ms timeout value in millisecond
  423. * @param written_len length of data written successfully
  424. * @return QCLOUD_RET_SUCCESS for success, or err code for failure
  425. */
  426. int HAL_TCP_Write(uintptr_t fd, const unsigned char *data, uint32_t len, uint32_t timeout_ms, size_t *written_len);
  427. /**
  428. * @brief Read data via TCP connection
  429. *
  430. * @param fd TCP socket handle
  431. * @param data destination data buffer where to put data
  432. * @param len length of data
  433. * @param timeout_ms timeout value in millisecond
  434. * @param read_len length of data read successfully
  435. * @return QCLOUD_RET_SUCCESS for success, or err code for failure
  436. */
  437. int HAL_TCP_Read(uintptr_t fd, unsigned char *data, uint32_t len, uint32_t timeout_ms, size_t *read_len);
  438. /********** UDP network **********/
  439. #ifdef COAP_COMM_ENABLED
  440. /**
  441. * @brief Setup UDP connection with server
  442. *
  443. * @host server address
  444. * @port server port
  445. * @return UPD socket handle (value>0) when success, or 0 otherwise
  446. */
  447. uintptr_t HAL_UDP_Connect(unsigned char profile_idx, const char *host, unsigned short port);
  448. /**
  449. * @brief Disconnect with server and release resource
  450. *
  451. * @param fd UDP Socket handle
  452. * @return 0 when success
  453. */
  454. void HAL_UDP_Disconnect(uintptr_t fd);
  455. /**
  456. * @brief Write data via UDP connection
  457. *
  458. * @param fd UDP socket handle
  459. * @param data source data to write
  460. * @param len length of data
  461. * @return length of data written when success, or err code for
  462. * failure
  463. */
  464. int HAL_UDP_Write(uintptr_t fd, const unsigned char *data, unsigned int len);
  465. /**
  466. * @brief Read data via UDP connection
  467. *
  468. * @param fd UDP socket handle
  469. * @param data destination data buffer where to put data
  470. * @param len length of data
  471. * @return length of data read when success, or err code for
  472. * failure
  473. */
  474. int HAL_UDP_Read(uintptr_t fd, unsigned char *data, unsigned int len);
  475. /**
  476. * @brief Read data via UDP connection
  477. *
  478. * @param fd UDP socket handle
  479. * @param data destination data buffer where to put data
  480. * @param len length of data
  481. * @param timeout_ms timeout value in millisecond
  482. * @return length of data read when success, or err code for
  483. * failure
  484. */
  485. int HAL_UDP_ReadTimeout(uintptr_t fd, unsigned char *p_data, unsigned int datalen, unsigned int timeout_ms);
  486. #endif // COAP_COMM_ENABLED
  487. #ifdef LOG_UPLOAD
  488. /* Functions for saving/reading logs into/from NVS(files/FLASH) after log upload
  489. * fail/recover */
  490. /**
  491. * @brief Functions for saving logs into NVS(files/FLASH) after log upload fail
  492. * @param log source log buffer
  493. * @param len length of log to save
  494. * @return length of data save when success, or 0 for failure
  495. */
  496. size_t HAL_Log_Save(const char *log, size_t len);
  497. /**
  498. * @brief Functions for reading logs from NVS(files/FLASH) when log upload ready
  499. * @param buf destination log buffer
  500. * @param len length of log to read
  501. * @return length of data read when success, or 0 for failure
  502. */
  503. size_t HAL_Log_Read(char *buf, size_t len);
  504. /**
  505. * @brief Functions for deleting logs in NVS(files/FLASH).
  506. * @return 0 when success
  507. */
  508. int HAL_Log_Del(void);
  509. /**
  510. * @brief Functions for reading the size of logs in NVS(files/FLASH).
  511. * @return 0 when nothing exist
  512. */
  513. size_t HAL_Log_Get_Size(void);
  514. #endif
  515. #if defined(__cplusplus)
  516. }
  517. #endif
  518. #endif /* QCLOUD_IOT_IMPORT_H_ */