Skip to content

Commit

Permalink
fix time and random API to portable
Browse files Browse the repository at this point in the history
Fix following time API to support WIN32
- gettimeofday() -> g_get_real_time()
- clock_gettime() -> g_get_real_time()

Add ifdef macro to support WIN32
- gmtime_r / gmtime_s
- localtime_r / localtime_s
- srandom / srand
- random / rand

Signed-off-by: Inho Oh <[email protected]>
  • Loading branch information
webispy authored and JeanTracker committed May 28, 2024
1 parent 269c987 commit c822147
Show file tree
Hide file tree
Showing 23 changed files with 175 additions and 87 deletions.
2 changes: 1 addition & 1 deletion examples/profiling/main_prof.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ static void test_finished(void)

/* CSV data, NOLINTNEXTLINE(cert-err33-c) */
fprintf(fp_out, "%s,%d,%ld,%d,%d,%d,%d,%d,%s\n",
test_name.c_str(), test_iteration, tmp->timestamp.tv_sec,
test_name.c_str(), test_iteration, (long int)(tmp->timestamp / 1000000),
nugu_prof_get_diff_msec_type(NUGU_PROF_TYPE_ASR_LISTENING_STARTED, NUGU_PROF_TYPE_ASR_END_POINT_DETECTED),
nugu_prof_get_diff_msec_type(NUGU_PROF_TYPE_ASR_END_POINT_DETECTED, NUGU_PROF_TYPE_ASR_RESULT),
nugu_prof_get_diff_msec_type(NUGU_PROF_TYPE_ASR_RESULT, NUGU_PROF_TYPE_TTS_SPEAK_DIRECTIVE),
Expand Down
3 changes: 2 additions & 1 deletion include/base/nugu_prof.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <nugu.h>
#include <base/nugu_uuid.h>
#include <glib.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -202,7 +203,7 @@ struct nugu_prof_data {
enum nugu_prof_type type; /**< profiling type */
char dialog_id[NUGU_MAX_UUID_STRING_SIZE + 1]; /* dialog request id */
char msg_id[NUGU_MAX_UUID_STRING_SIZE + 1]; /* message id */
struct timespec timestamp; /* timestamp */
gint64 timestamp; /* timestamp(microseconds) */
char *contents; /* additional contents */
};

Expand Down
16 changes: 8 additions & 8 deletions include/base/nugu_uuid.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <time.h>
#include <nugu.h>
#include <glib.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -92,14 +93,13 @@ NUGU_API int nugu_uuid_convert_base16(const unsigned char *bytes,
* @brief Convert byte array to base16-encoded string
* @param[in] bytes byte array
* @param[in] bytes_len length
* @param[out] out_time memory allocated structure
* @param[out] msec milliseconds
* @return Result of conversion success or failure
* @retval 0 Success
* @retval -1 Failure
*/
NUGU_API int nugu_uuid_convert_timespec(const unsigned char *bytes,
size_t bytes_len,
struct timespec *out_time);
NUGU_API int nugu_uuid_convert_msec(const unsigned char *bytes,
size_t bytes_len, gint64 *msec);

/**
* @brief Generate random bytes and fill to destination buffer
Expand All @@ -113,7 +113,7 @@ NUGU_API int nugu_uuid_fill_random(unsigned char *dest, size_t dest_len);

/**
* @brief Fill to output buffer with NUGU UUID format using parameters
* @param[in] time timestamp information
* @param[in] msec milliseconds
* @param[in] hash hash value(e.g. SHA1(token))
* @param[in] hash_len length of hash value
* @param[out] out memory allocated output buffer
Expand All @@ -122,9 +122,9 @@ NUGU_API int nugu_uuid_fill_random(unsigned char *dest, size_t dest_len);
* @retval 0 Success
* @retval -1 Failure
*/
NUGU_API int nugu_uuid_fill(const struct timespec *time,
const unsigned char *hash, size_t hash_len,
unsigned char *out, size_t out_len);
NUGU_API int nugu_uuid_fill(gint64 msec, const unsigned char *hash,
size_t hash_len, unsigned char *out,
size_t out_len);
/**
* @}
*/
Expand Down
4 changes: 4 additions & 0 deletions plugins/filedump.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ static int _dumpfile_open(const char *path, const char *prefix)
int fd;

now = time(NULL);
#ifdef _WIN32
localtime_s(&now_tm, &now);
#else
localtime_r(&now, &now_tm);
#endif

snprintf(ymd, sizeof(ymd), "%04d%02d%02d", now_tm.tm_year + 1900,
now_tm.tm_mon + 1, now_tm.tm_mday);
Expand Down
4 changes: 4 additions & 0 deletions plugins/filereader.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ static int _dumpfile_open(const char *path, const char *prefix)
return -1;

now = time(NULL);
#ifdef _WIN32
localtime_s(&now_tm, &now);
#else
localtime_r(&now, &now_tm);
#endif

snprintf(ymd, sizeof(ymd), "%04d%02d%02d", now_tm.tm_year + 1900,
now_tm.tm_mon + 1, now_tm.tm_mday);
Expand Down
4 changes: 4 additions & 0 deletions plugins/gstreamer_pcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ static int _dumpfile_open(const char *path, const char *prefix)
return -1;

now = time(NULL);
#ifdef _WIN32
localtime_s(&now_tm, &now);
#else
localtime_r(&now, &now_tm);
#endif

snprintf(ymd, sizeof(ymd), "%04d%02d%02d", now_tm.tm_year + 1900,
now_tm.tm_mon + 1, now_tm.tm_mday);
Expand Down
4 changes: 4 additions & 0 deletions plugins/gstreamer_recorder.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ static int _dumpfile_open(const char *path, const char *prefix)
return -1;

now = time(NULL);
#ifdef _WIN32
localtime_s(&now_tm, &now);
#else
localtime_r(&now, &now_tm);
#endif

snprintf(ymd, sizeof(ymd), "%04d%02d%02d", now_tm.tm_year + 1900,
now_tm.tm_mon + 1, now_tm.tm_mday);
Expand Down
4 changes: 4 additions & 0 deletions plugins/opus.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ static int _dumpfile_open(const char *path, const char *prefix)
return -1;

now = time(NULL);
#ifdef _WIN32
localtime_s(&now_tm, &now);
#else
localtime_r(&now, &now_tm);
#endif

snprintf(ymd, sizeof(ymd), "%04d%02d%02d", now_tm.tm_year + 1900,
now_tm.tm_mon + 1, now_tm.tm_mday);
Expand Down
4 changes: 4 additions & 0 deletions plugins/opus_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ static int _dumpfile_open(const char *path, const char *prefix)
return -1;

now = time(NULL);
#ifdef _WIN32
localtime_s(&now_tm, &now);
#else
localtime_r(&now, &now_tm);
#endif

snprintf(ymd, sizeof(ymd), "%04d%02d%02d", now_tm.tm_year + 1900,
now_tm.tm_mon + 1, now_tm.tm_mday);
Expand Down
4 changes: 4 additions & 0 deletions plugins/portaudio_pcm_async.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ static int _dumpfile_open(const char *path, const char *prefix)
return -1;

now = time(NULL);
#ifdef _WIN32
localtime_s(&now_tm, &now);
#else
localtime_r(&now, &now_tm);
#endif

snprintf(ymd, sizeof(ymd), "%04d%02d%02d", now_tm.tm_year + 1900,
now_tm.tm_mon + 1, now_tm.tm_mday);
Expand Down
4 changes: 4 additions & 0 deletions plugins/portaudio_pcm_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ static int _dumpfile_open(const char *path, const char *prefix)
return -1;

now = time(NULL);
#ifdef _WIN32
localtime_s(&now_tm, &now);
#else
localtime_r(&now, &now_tm);
#endif

snprintf(ymd, sizeof(ymd), "%04d%02d%02d", now_tm.tm_year + 1900,
now_tm.tm_mon + 1, now_tm.tm_mday);
Expand Down
4 changes: 4 additions & 0 deletions plugins/portaudio_recorder.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ static int _dumpfile_open(const char *path, const char *prefix)
return -1;

now = time(NULL);
#ifdef _WIN32
localtime_s(&now_tm, &now);
#else
localtime_r(&now, &now_tm);
#endif

snprintf(ymd, sizeof(ymd), "%04d%02d%02d", now_tm.tm_year + 1900,
now_tm.tm_mon + 1, now_tm.tm_mday);
Expand Down
4 changes: 4 additions & 0 deletions plugins/speex.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ static int _dumpfile_open(const char *path, const char *prefix)
return -1;

now = time(NULL);
#ifdef _WIN32
localtime_s(&now_tm, &now);
#else
localtime_r(&now, &now_tm);
#endif

snprintf(ymd, sizeof(ymd), "%04d%02d%02d", now_tm.tm_year + 1900,
now_tm.tm_mon + 1, now_tm.tm_mday);
Expand Down
6 changes: 4 additions & 2 deletions src/base/network/http2/threadsync.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ enum thread_sync_result thread_sync_wait_secs(ThreadSync *s, unsigned int secs)
{
struct timespec spec;
int status = 0;
gint64 microseconds;

g_return_val_if_fail(s != NULL, THREAD_SYNC_RESULT_FAILURE);

clock_gettime(CLOCK_REALTIME, &spec);
spec.tv_sec = spec.tv_sec + secs;
microseconds = g_get_real_time();
spec.tv_nsec = (microseconds % 1000000) * 1000;
spec.tv_sec = microseconds / 1000000 + secs;

pthread_mutex_lock(&s->lock);
if (s->flag == 0)
Expand Down
4 changes: 4 additions & 0 deletions src/base/network/http2/v1_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ static int _get_next_timeout(V1Ping *ping)
/**
* max(ttl_max_ms * (0 < random() <= 1), retry_delay_ms)
*/
#ifdef _WIN32
timeout = ping->policy.ttl_max_ms * (rand() / (float)RAND_MAX * 1.0f);
#else
timeout = ping->policy.ttl_max_ms * (random() / (float)RAND_MAX * 1.0f);
#endif
if (timeout < ping->policy.retry_delay_ms)
timeout = ping->policy.retry_delay_ms;

Expand Down
19 changes: 14 additions & 5 deletions src/base/nugu_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,24 @@ static int _log_make_prefix(char *prefix, enum nugu_log_level level,
int len = 0;

if (_log_prefix_fields & NUGU_LOG_PREFIX_TIMESTAMP) {
struct timespec tp;
struct tm ti;
int msec;
gint64 microseconds;
time_t now;

clock_gettime(CLOCK_REALTIME, &tp);
localtime_r(&(tp.tv_sec), &ti);
microseconds = g_get_real_time();

msec = (int)((microseconds % 1000000) / 1000);
now = (time_t)(microseconds / 1000000);

#ifdef _WIN32
localtime_s(&ti, &now);
#else
localtime_r(&now, &ti);
#endif

len += (int)strftime(prefix, 15, "%m-%d %H:%M:%S", &ti);
len += snprintf(prefix + len, 6, ".%03ld ",
tp.tv_nsec / 1000000);
len += snprintf(prefix + len, 6, ".%03d ", msec);
}

if (_log_prefix_fields & NUGU_LOG_PREFIX_PID ||
Expand Down
23 changes: 14 additions & 9 deletions src/base/nugu_network_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct _nugu_network {

/* Server */
DGServer *server;
struct timespec ts_connected;
time_t tsec_connected;

/* Handoff */
DGServer *handoff;
Expand Down Expand Up @@ -648,7 +648,7 @@ static void on_directives_closed(enum nugu_equeue_type type, void *data,
void *userdata)
{
NetworkManager *nm = userdata;
struct timespec spec;
time_t sec = 0;

nugu_prof_mark(NUGU_PROF_TYPE_NETWORK_DIRECTIVES_CLOSED);

Expand All @@ -657,9 +657,9 @@ static void on_directives_closed(enum nugu_equeue_type type, void *data,
return;
}

clock_gettime(CLOCK_REALTIME, &spec);
sec = time(NULL);

if (spec.tv_sec - nm->ts_connected.tv_sec == 0) {
if (sec - nm->tsec_connected == 0) {
nugu_error(
"Connection was finished immediately on the server.");

Expand Down Expand Up @@ -706,7 +706,7 @@ static void on_event(enum nugu_equeue_type type, void *data, void *userdata)
nugu_prof_mark(
NUGU_PROF_TYPE_NETWORK_SERVER_ESTABLISH_RESPONSE);
nugu_prof_mark(NUGU_PROF_TYPE_NETWORK_CONNECTED);
clock_gettime(CLOCK_REALTIME, &nm->ts_connected);
nm->tsec_connected = time(NULL);
_process_connecting(userdata, STEP_SERVER_CONNECTED);
break;
default:
Expand Down Expand Up @@ -798,7 +798,6 @@ static NetworkManager *_network;

int nugu_network_manager_initialize(void)
{
struct timespec spec;
curl_version_info_data *cinfo;

if (_network) {
Expand All @@ -823,9 +822,11 @@ int nugu_network_manager_initialize(void)
}
}

clock_gettime(CLOCK_REALTIME, &spec);
srandom(spec.tv_nsec ^ spec.tv_sec);

#ifdef _WIN32
srand(g_get_real_time());
#else
srandom(g_get_real_time());
#endif
_network = nugu_network_manager_new();
if (!_network)
return -1;
Expand Down Expand Up @@ -1074,7 +1075,11 @@ int nugu_network_manager_send_event(NuguEvent *nev)
struct tm tm;

now = time(NULL);
#ifdef _WIN32
gmtime_s(&tm, &now);
#else
gmtime_r(&now, &tm);
#endif
if (strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S %Z",
&tm) == 0) {
nugu_error("strftime() failed");
Expand Down
Loading

0 comments on commit c822147

Please sign in to comment.