xref: /nrf52832-nimble/packages/NimBLE-latest/porting/npl/linux/src/os_time.c (revision 042d53a763ad75cb1465103098bb88c245d95138)
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #include <assert.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include "os/os.h"
24 #include "nimble/nimble_npl.h"
25 
26 #include <unistd.h>
27 #include <time.h>
28 
29 /**
30  * Return ticks [ms] since system start as uint32_t.
31  */
32 ble_npl_time_t
ble_npl_time_get(void)33 ble_npl_time_get(void)
34 {
35     struct timespec now;
36     if (clock_gettime(CLOCK_MONOTONIC, &now)) {
37         return 0;
38     }
39     return now.tv_sec * 1000.0 + now.tv_nsec / 1000000.0;
40 }
41 
42 
ble_npl_time_ms_to_ticks(uint32_t ms,ble_npl_time_t * out_ticks)43 ble_npl_error_t ble_npl_time_ms_to_ticks(uint32_t ms, ble_npl_time_t *out_ticks)
44 {
45     *out_ticks = ms;
46 
47     return BLE_NPL_OK;
48 }
49 
50 
ble_npl_time_ticks_to_ms(ble_npl_time_t ticks,uint32_t * out_ms)51 ble_npl_error_t ble_npl_time_ticks_to_ms(ble_npl_time_t ticks, uint32_t *out_ms)
52 {
53     *out_ms = ticks;
54 
55     return BLE_NPL_OK;
56 }
57 
ble_npl_time_ms_to_ticks32(uint32_t ms)58 ble_npl_time_t ble_npl_time_ms_to_ticks32(uint32_t ms)
59 {
60     return ms;
61 }
62 
ble_npl_time_ticks_to_ms32(ble_npl_time_t ticks)63 uint32_t ble_npl_time_ticks_to_ms32(ble_npl_time_t ticks)
64 {
65     return ticks;
66 }
67 
68 void
ble_npl_time_delay(ble_npl_time_t ticks)69 ble_npl_time_delay(ble_npl_time_t ticks)
70 {
71     sleep(ble_npl_time_ticks_to_ms32(ticks)/1000);
72 }
73