Skip to content

Commit 7ff70b1

Browse files
gordonwang0huguesBouvier
authored andcommitted
Add delay function to platform layer. (aws#235)
1 parent fc99012 commit 7ff70b1

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

PortingGuide.md

+11-8
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ Current SDK Directory Layout (mbedTLS)
1515
|--`include` (Header files of the AWS IoT device SDK) <br>
1616
|--`src` (Source files of the AWS IoT device SDK) <br>
1717
|--`platform` (Platform specific files) <br>
18-
|--`samples` (Samples including makefiles for building on mbedTLS) <br>
19-
|--`tests` (Tests for verifying SDK is functioning as expected) <br>
18+
|--`samples` (Samples including makefiles for building on mbedTLS) <br>
19+
|--`tests` (Tests for verifying SDK is functioning as expected) <br>
2020

2121
All makefiles in this SDK were configured using the documented folder structure above, so moving or renaming folders will require modifications to makefiles.
2222

@@ -44,7 +44,7 @@ This section explains the API calls that need to be implemented in order for the
4444

4545
### Timer Functions
4646

47-
A timer implementation is necessary to handle request timeouts (sending MQTT connect, subscribe, etc. commands) as well as connection maintenance (MQTT keep-alive pings). Timers need millisecond resolution and are polled for expiration so these can be implemented using a "milliseconds since startup" free-running counter if desired. In the synchronous sample provided with this SDK only one command will be "in flight" at one point in time plus the client's ping timer.
47+
A timer implementation is necessary to handle request timeouts (sending MQTT connect, subscribe, etc. commands) as well as connection maintenance (MQTT keep-alive pings). Timers need millisecond resolution and are polled for expiration so these can be implemented using a "milliseconds since startup" free-running counter if desired. In the synchronous sample provided with this SDK only one command will be "in flight" at one point in time plus the client's ping timer.
4848

4949
Define the `Timer` Struct as in `timer_platform.h`
5050

@@ -63,10 +63,13 @@ countdown_sec - set the timer to expire in x seconds and start the timer.
6363
`uint32_t left_ms(Timer *);`
6464
left_ms - query time in milliseconds left on the timer.
6565

66+
`void delay(unsigned milliseconds)`
67+
delay - sleep for the specified number of milliseconds.
68+
6669

6770
### Network Functions
6871

69-
In order for the MQTT client stack to be able to communicate via the TCP/IP network protocol stack using a mutually authenticated TLS connection, the following API calls need to be implemented for your platform.
72+
In order for the MQTT client stack to be able to communicate via the TCP/IP network protocol stack using a mutually authenticated TLS connection, the following API calls need to be implemented for your platform.
7073

7174
For additional details about API parameters refer to the [API documentation](http://aws-iot-device-sdk-embedded-c-docs.s3-website-us-east-1.amazonaws.com/index.html).
7275

@@ -76,7 +79,7 @@ This is used for data specific to the TLS library being used.
7679
`IoT_Error_t iot_tls_init(Network *pNetwork, char *pRootCALocation, char *pDeviceCertLocation,
7780
char *pDevicePrivateKeyLocation, char *pDestinationURL,
7881
uint16_t DestinationPort, uint32_t timeout_ms, bool ServerVerificationFlag);`
79-
Initialize the network client / structure.
82+
Initialize the network client / structure.
8083

8184
`IoT_Error_t iot_tls_connect(Network *pNetwork, TLSConnectParams *TLSParams);`
8285
Create a TLS TCP socket to the configure address using the credentials provided via the NewNetwork API call. This will include setting up certificate locations / arrays.
@@ -102,15 +105,15 @@ The TLS library generally provides the API for the underlying TCP socket.
102105

103106
### Threading Functions
104107

105-
The MQTT client uses a state machine to control operations in multi-threaded situations. However it requires a mutex implementation to guarantee thread safety. This is not required in situations where thread safety is not important and it is disabled by default. The _ENABLE_THREAD_SUPPORT_ macro needs to be defined in aws_iot_config.h to enable this layer. You will also need to add the -lpthread linker flag for the compiler if you are using the provided reference implementation.
108+
The MQTT client uses a state machine to control operations in multi-threaded situations. However it requires a mutex implementation to guarantee thread safety. This is not required in situations where thread safety is not important and it is disabled by default. The _ENABLE_THREAD_SUPPORT_ macro needs to be defined in aws_iot_config.h to enable this layer. You will also need to add the -lpthread linker flag for the compiler if you are using the provided reference implementation.
106109

107110
For additional details about API parameters refer to the [API documentation](http://aws-iot-device-sdk-embedded-c-docs.s3-website-us-east-1.amazonaws.com/index.html).
108111

109112
Define the `IoT_Mutex_t` Struct as in `threads_platform.h`
110113
This is used for data specific to the TLS library being used.
111114

112115
`IoT_Error_t aws_iot_thread_mutex_init(IoT_Mutex_t *);`
113-
Initialize the mutex provided as argument.
116+
Initialize the mutex provided as argument.
114117

115118
`IoT_Error_t aws_iot_thread_mutex_lock(IoT_Mutex_t *);`
116119
Lock the mutex provided as argument
@@ -125,7 +128,7 @@ The threading layer provides the implementation of mutexes used for thread-safe
125128

126129
### Sample Porting:
127130

128-
Marvell has ported the SDK for their development boards. [These](https://github.com/marvell-iot/aws_starter_sdk/tree/master/sdk/external/aws_iot/platform/wmsdk) files are example implementations of the above mentioned functions.
131+
Marvell has ported the SDK for their development boards. [These](https://github.com/marvell-iot/aws_starter_sdk/tree/master/sdk/external/aws_iot/platform/wmsdk) files are example implementations of the above mentioned functions.
129132
This provides a port of the timer and network layer. The threading layer is not a part of this port.
130133

131134
## Time source for certificate validation

platform/linux/common/timer.c

+8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extern "C" {
2626
#include <sys/types.h>
2727
#include <stdint.h>
2828
#include <stdbool.h>
29+
#include <unistd.h>
2930

3031
#include "timer_platform.h"
3132

@@ -69,6 +70,13 @@ void init_timer(Timer *timer) {
6970
timer->end_time = (struct timeval) {0, 0};
7071
}
7172

73+
void delay(unsigned milliseconds)
74+
{
75+
useconds_t sleepTime = (useconds_t)(milliseconds * 1000);
76+
77+
usleep(sleepTime);
78+
}
79+
7280
#ifdef __cplusplus
7381
}
7482
#endif

platform/linux/common/timer_platform.h

+7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ struct Timer {
3434
struct timeval end_time;
3535
};
3636

37+
/**
38+
* @brief Delay (sleep) for the specified number of milliseconds.
39+
*
40+
* @param milliseconds The number of milliseconds to sleep.
41+
*/
42+
void delay(unsigned milliseconds);
43+
3744
#ifdef __cplusplus
3845
}
3946
#endif

0 commit comments

Comments
 (0)