1 /* Copyright JS Foundation and other contributors, http://js.foundation
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
16 #define _BSD_SOURCE
17 #include <stdarg.h>
18 #include <stdlib.h>
19 #include <sys/time.h>
20
21 #include "jerry-core/include/jerryscript-port.h"
22
23 #include "us_ticker_api.h"
24
25 #ifndef JSMBED_OVERRIDE_JERRY_PORT_LOG
26 /**
27 * Provide log message implementation for the engine.
28 */
29 void
jerry_port_log(jerry_log_level_t level,const char * format,...)30 jerry_port_log (jerry_log_level_t level, /**< log level */
31 const char *format, /**< format string */
32 ...) /**< parameters */
33 {
34 (void) level; /* ignore log level */
35
36 va_list args;
37 va_start (args, format);
38 vfprintf (stderr, format, args);
39 va_end (args);
40
41 if (strlen (format) == 1 && format[0] == 0x0a) /* line feed (\n) */
42 {
43 printf ("\r"); /* add CR for proper display in serial monitors */
44 }
45 } /* jerry_port_log */
46 #endif /* JSMBED_OVERRIDE_JERRY_PORT_LOG */
47
48 /**
49 * Implementation of jerry_port_get_local_time_zone_adjustment.
50 *
51 * @return 0, as we live in UTC.
52 */
53 double
jerry_port_get_local_time_zone_adjustment(double unix_ms,bool is_utc)54 jerry_port_get_local_time_zone_adjustment (double unix_ms, bool is_utc)
55 {
56 return 0;
57 } /* jerry_port_get_local_time_zone_adjustment */
58
59 /**
60 * Implementation of jerry_port_get_current_time.
61 *
62 * @return current timer's counter value in milliseconds
63 */
64 double
jerry_port_get_current_time(void)65 jerry_port_get_current_time (void)
66 {
67 static uint64_t last_tick = 0;
68 static time_t last_time = 0;
69 static uint32_t skew = 0;
70
71 uint64_t curr_tick = us_ticker_read (); /* The value is in microseconds. */
72 time_t curr_time = time(NULL); /* The value is in seconds. */
73 double result = curr_time * 1000;
74
75 /* The us_ticker_read () has an overflow for each UINT_MAX microseconds
76 * (~71 mins). For each overflow event the ticker-based clock is about 33
77 * milliseconds fast. Without a timer thread the milliseconds part of the
78 * time can be corrected if the difference of two get_current_time calls
79 * are within the mentioned 71 mins. Above that interval we can assume
80 * that the milliseconds part of the time is negligibe.
81 */
82 if (curr_time - last_time > (time_t) (((uint32_t) - 1) / 1000000)) {
83 skew = 0;
84 } else if (last_tick > curr_tick) {
85 skew = (skew + 33) % 1000;
86 }
87 result += (curr_tick / 1000 - skew) % 1000;
88
89 last_tick = curr_tick;
90 last_time = curr_time;
91 return result;
92 } /* jerry_port_get_current_time */
93