xref: /nrf52832-nimble/packages/NimBLE-latest/porting/npl/riot/src/npl_os_riot.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 <stddef.h>
21 #include "nimble/nimble_npl.h"
22 
23 #define ENABLE_DEBUG    (0)
24 #include "debug.h"
25 
26 static void
_callout_fire(void * arg)27 _callout_fire(void *arg)
28 {
29     struct ble_npl_callout *co = (struct ble_npl_callout *)arg;
30     event_post(co->q, &co->e.e.super);
31 }
32 
33 ble_npl_error_t
ble_npl_sem_pend(struct ble_npl_sem * sem,ble_npl_time_t timeout)34 ble_npl_sem_pend(struct ble_npl_sem *sem, ble_npl_time_t timeout)
35 {
36     int rc;
37     struct timespec abs;
38     uint64_t time;
39 
40     time = xtimer_now_usec64() + ble_npl_time_ticks_to_ms32(timeout) * 1000;
41     abs.tv_sec = (time_t)(time / 1000000);
42     abs.tv_nsec = (long)((time % 1000000) * 1000);
43 
44     rc = sem_timedwait(&sem->sem, &abs);
45 
46     return rc == 0 ? BLE_NPL_OK : BLE_NPL_ENOENT;
47 }
48 
49 void
ble_npl_callout_init(struct ble_npl_callout * c,struct ble_npl_eventq * evq,ble_npl_event_fn * ev_cb,void * ev_arg)50 ble_npl_callout_init(struct ble_npl_callout *c, struct ble_npl_eventq *evq,
51                      ble_npl_event_fn *ev_cb, void *ev_arg)
52 {
53     c->timer.arg = (void *)c;
54     c->timer.callback = _callout_fire;
55     c->q = &evq->q;
56     ble_npl_event_init(&c->e, ev_cb, ev_arg);
57 }
58 
59 ble_npl_error_t
ble_npl_callout_reset(struct ble_npl_callout * c,ble_npl_time_t ticks)60 ble_npl_callout_reset(struct ble_npl_callout *c, ble_npl_time_t ticks)
61 {
62     uint32_t now = xtimer_now_usec();
63     c->target_ticks = (ble_npl_time_t)((now / 1000) + ticks);
64     xtimer_set(&c->timer, ((ticks * 1000) - ((xtimer_now_usec() - now) / 1000)));
65     return BLE_NPL_OK;
66 }
67