1 // Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef _ESP_TRANSPORT_UTILS_H_
16 #define _ESP_TRANSPORT_UTILS_H_
17 #include <sys/time.h>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 /**
24  * @brief      Utility macro to be used for NULL ptr check after malloc
25  *
26  */
27 #define ESP_TRANSPORT_MEM_CHECK(TAG, a, action) if (!(a)) {                                         \
28         ESP_LOGE(TAG,"%s(%d): %s", __FUNCTION__, __LINE__, "Memory exhausted");                     \
29         action;                                                                                     \
30         }
31 
32 /**
33  * @brief      Utility macro for checking the error code of esp_err_t
34  */
35 #define ESP_TRANSPORT_ERR_OK_CHECK(TAG, err, action) \
36         {                                                     \
37             esp_err_t _esp_transport_err_to_check = err;      \
38             if (_esp_transport_err_to_check != ESP_OK) {      \
39                 ESP_LOGE(TAG,"%s(%d): Expected ESP_OK; reported: %d", __FUNCTION__, __LINE__, _esp_transport_err_to_check); \
40                 action;                                       \
41             }                                                 \
42         }
43 
44 /**
45  * @brief      Convert milliseconds to timeval struct for valid timeouts, otherwise
46  *             (if "wait forever" requested by timeout_ms=-1) timeval structure is not updated and NULL returned
47  *
48  * @param[in]  timeout_ms  The timeout value in milliseconds or -1 to waiting forever
49  * @param[out] tv          Pointer to timeval struct
50  *
51  * @return
52  * - NULL if timeout_ms=-1 (wait forever)
53  * - pointer to the updated timeval structure (provided as "tv" argument) with recalculated timeout value
54  */
55 struct timeval* esp_transport_utils_ms_to_timeval(int timeout_ms, struct timeval *tv);
56 
57 
58 #ifdef __cplusplus
59 }
60 #endif
61 #endif /* _ESP_TRANSPORT_UTILS_H_ */
62