WXK
2025-02-05 961c1174bbf1aaae5fa2f672806ed4eaf2f917be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
 
#include <assert.h>
#include <cmac_driver/cmac_shared.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include "nimble_syscfg.h"
#include "mcu/mcu.h"
#include "os/os_arch.h"
#include "os/os.h"
 
#ifndef min
#define min(_a, _b)     ((_a) < (_b) ? (_a) : (_b))
#endif
 
static cmac_mbox_read_cb *g_cmac_mbox_read_cb;
static cmac_mbox_write_notif_cb *g_cmac_mbox_write_notif_cb;
 
void
cmac_mbox_set_read_cb(cmac_mbox_read_cb *cb)
{
    g_cmac_mbox_read_cb = cb;
}
 
void
cmac_mbox_set_write_notif_cb(cmac_mbox_write_notif_cb *cb)
{
    g_cmac_mbox_write_notif_cb = cb;
}
 
int
cmac_mbox_has_data(void)
{
#if MYNEWT_VAL(BLE_CONTROLLER)
    volatile struct cmac_mbox *mbox = &g_cmac_shared_data.mbox_s2c;
#else
    volatile struct cmac_mbox *mbox = &g_cmac_shared_data->mbox_c2s;
#endif
 
    return mbox->rd_off != mbox->wr_off;
}
 
int
cmac_mbox_read(void)
{
#if MYNEWT_VAL(BLE_CONTROLLER)
    volatile struct cmac_mbox *mbox = &g_cmac_shared_data.mbox_s2c;
    uint8_t *mbox_buf = (uint8_t *)&g_cmac_shared_data.mbox_s2c_buf;
    const uint16_t mbox_size = MYNEWT_VAL(CMAC_MBOX_SIZE_S2C);
#else
    volatile struct cmac_mbox *mbox = &g_cmac_shared_data->mbox_c2s;
    uint8_t *mbox_buf = (uint8_t *)&g_cmac_shared_data->mbox_c2s_buf;
    const uint16_t mbox_size = MYNEWT_VAL(CMAC_MBOX_SIZE_C2S);
#endif
    uint16_t rd_off;
    uint16_t wr_off;
    uint16_t chunk;
    int len = 0;
 
    if (!g_cmac_mbox_read_cb) {
        return 0;
    }
 
    do {
        rd_off = mbox->rd_off;
        wr_off = mbox->wr_off;
 
        if (rd_off <= wr_off) {
            chunk = wr_off - rd_off;
        } else {
            chunk = mbox_size - rd_off;
        }
 
        while (chunk) {
            len = g_cmac_mbox_read_cb(&mbox_buf[rd_off], chunk);
            if (len < 0) {
                break;
            }
 
            rd_off += len;
            chunk -= len;
        }
 
        mbox->rd_off = rd_off == mbox_size ? 0 : rd_off;
    } while ((mbox->rd_off != mbox->wr_off) && (len >= 0));
 
    return 0;
}
 
int
cmac_mbox_write(const void *data, uint16_t len)
{
#if MYNEWT_VAL(BLE_CONTROLLER)
    volatile struct cmac_mbox *mbox = &g_cmac_shared_data.mbox_c2s;
    uint8_t *mbox_buf = (uint8_t *)&g_cmac_shared_data.mbox_c2s_buf;
    const uint16_t mbox_size = MYNEWT_VAL(CMAC_MBOX_SIZE_C2S);
#else
    volatile struct cmac_mbox *mbox = &g_cmac_shared_data->mbox_s2c;
    uint8_t *mbox_buf = (uint8_t *)&g_cmac_shared_data->mbox_s2c_buf;
    const uint16_t mbox_size = MYNEWT_VAL(CMAC_MBOX_SIZE_S2C);
#endif
    uint16_t rd_off;
    uint16_t wr_off;
    uint16_t max_wr;
    uint16_t chunk;
 
    while (len) {
        rd_off = mbox->rd_off;
        wr_off = mbox->wr_off;
 
        /*
         * Calculate maximum length to write, i.e. up to end of buffer or stop
         * before rd_off to be able to detect full queue.
         */
        if (rd_off > wr_off) {
            /*
             * |0|1|2|3|4|5|6|7|
             * | | | |W| | |R| |
             *        `---^
             */
            max_wr = rd_off - wr_off - 1;
        } else if (rd_off == 0) {
            /*
             * |0|1|2|3|4|5|6|7|
             * |R| | |W| | | | |
             *        `-------^
             */
            max_wr = mbox_size - wr_off - 1;
        } else {
            /*
             * |0|1|2|3|4|5|6|7|
             * | |R| |W| | | | |
             *        `---------^
             */
            max_wr = mbox_size - wr_off;
        }
 
        chunk = min(len, max_wr);
 
        if (chunk == 0) {
            continue;
        }
 
        memcpy(&mbox_buf[wr_off], data, chunk);
 
        wr_off += chunk;
        mbox->wr_off = wr_off == mbox_size ? 0 : wr_off;
 
        g_cmac_mbox_write_notif_cb();
 
        len -= chunk;
        data = (uint8_t *)data + chunk;
    }
 
    return 0;
}