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 <stdbool.h>
21 
22 #include <DA1469xAB.h>
23 #include <rand.h>
24 #include <shm.h>
25 
26 int
cmac_rand_is_active(void)27 cmac_rand_is_active(void)
28 {
29     return g_cmac_shm.rand->cmr_active;
30 }
31 
32 int
cmac_rand_is_full(void)33 cmac_rand_is_full(void)
34 {
35     int next;
36     bool rc;
37 
38     next = cmac_rand_get_next();
39     if (next == g_cmac_shm.rand->cmr_out) {
40         rc = 1;
41     } else {
42         rc = 0;
43     }
44     return rc;
45 }
46 
47 int
cmac_rand_get_next(void)48 cmac_rand_get_next(void)
49 {
50     int next;
51 
52     /* If active and not full, put event on queue to get random numbers */
53     next = g_cmac_shm.rand->cmr_in + 1;
54     if (next == g_cmac_shm.config->rand_size) {
55         next = 0;
56     }
57     return next;
58 }
59 
60 void
cmac_rand_fill(uint32_t * buf,int num_words)61 cmac_rand_fill(uint32_t *buf, int num_words)
62 {
63     int next;
64 
65     /* XXX: if words is 0, is it possible we could get into a state
66        where we are waiting for random numbers but M33 does not know it
67        has to fill any? */
68 
69     /* NOTE: we already know the buffer is not full first time through */
70     next = g_cmac_shm.rand->cmr_in;
71     while (num_words) {
72         g_cmac_shm.rand->cmr_buf[next] = buf[0];
73         next = cmac_rand_get_next();
74         g_cmac_shm.rand->cmr_in = next;
75         next = cmac_rand_get_next();
76         if (next == g_cmac_shm.rand->cmr_out) {
77             break;
78         }
79         --num_words;
80         ++buf;
81     }
82 }
83