summaryrefslogtreecommitdiffstats
path: root/stacks/lwip_stack/lwip_src/common/stackx_common.c
blob: 30567ee150595ec838ec5f8d151dae93cbe9f454 (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
*
* Copyright (c) 2018 Huawei Technologies Co.,Ltd.
* Licensed 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 <stdarg.h>
#include <stddef.h>
#include "stackx_common.h"
#include "nstack_securec.h"
#include "nstack_log.h"
#include "types.h"
#include "stackx_types.h"

int spl_snprintf(char *buffer, int buflen, const char *format, ...)
{
    int len;
    va_list ap;

    if ((NULL == buffer) || (0 >= buflen))
    {
        return -1;
    }

    if (format == NULL)
    {
        buffer[0] = '\0';
        return -1;
    }

    (void) va_start(ap, format);
    len = vsnprintf_s(buffer, buflen, buflen - 1, format, ap);
    if (-1 == len)
    {
        va_end(ap);
        return -1;
    }

    va_end(ap);
    if ((len >= buflen) && (buflen > 0) && (buffer != NULL))
    {
        buffer[buflen - 1] = '\0';
    }

    return len;
}

/*****************************************************************************
*   Prototype    : sbr_create_mzone
*   Description  : create mzone
*   Input        : const char* name
*                  size_t size
*   Output       : None
*   Return Value : mzone_handle
*   Calls        :
*   Called By    :
*
*****************************************************************************/
mzone_handle sbr_create_mzone(const char *name, size_t size)
{
    if (!name)
    {
        NSFW_LOGERR("name is NULL");
        return NULL;
    }

    mzone_handle zone;
    nsfw_mem_zone param;

    param.isocket_id = -1;
    param.lenth = size;
    param.stname.entype = NSFW_SHMEM;

    if (strcpy_s(param.stname.aname, NSFW_MEM_NAME_LENTH, name) != 0)
    {
        NSFW_LOGERR("STRCPY_S failed]name=%s", name);
        return NULL;
    }

    zone = nsfw_mem_zone_create(&param);
    if (!zone)
    {
        NSFW_LOGERR("nsfw_mem_zone_create failed]name=%s, size:%zu", name,
                    size);
        return NULL;
    }

    return zone;
}

/*****************************************************************************
*   Prototype    : sbr_lookup_mzone
*   Description  : lookup mzone
*   Input        : const char* name
*   Output       : None
*   Return Value : mzone_handle
*   Calls        :
*   Called By    :
*
*****************************************************************************/
mzone_handle sbr_lookup_mzone(const char *name)
{
    if (!name)
    {
        NSFW_LOGERR("name is NULL");
        return NULL;
    }

    mzone_handle zone;
    nsfw_mem_name param;

    param.entype = NSFW_SHMEM;
    param.enowner = NSFW_PROC_MAIN;
    if (strcpy_s(param.aname, NSFW_MEM_NAME_LENTH, name) != 0)
    {
        NSFW_LOGERR("STRCPY_S failed]name=%s", name);
        return NULL;
    }

    zone = nsfw_mem_zone_lookup(&param);
    if (!zone)
    {
        NSFW_LOGERR("nsfw_mem_zone_lookup failed]name=%s", name);
        return NULL;
    }

    return zone;
}

/*****************************************************************************
*   Prototype    : sbr_create_pool
*   Description  : create pool
*   Input        : const char* name
*                  i32 num
*                  u16 size
*   Output       : None
*   Return Value : mring_handle
*   Calls        :
*   Called By    :
*
*****************************************************************************/
mring_handle sbr_create_pool(const char *name, i32 num, u16 size)
{
    if (!name)
    {
        NSFW_LOGERR("name is NULL");
        return NULL;
    }

    nsfw_mem_sppool param;
    if (EOK != memset_s(&param, sizeof(param), 0, sizeof(param)))
    {
        NSFW_LOGERR("memset error]name=%s", name);
        return NULL;
    }

    param.enmptype = NSFW_MRING_MPMC;
    param.useltsize = size;
    param.usnum = num;
    param.stname.entype = NSFW_SHMEM;
    param.isocket_id = -1;
    if (strcpy_s(param.stname.aname, NSFW_MEM_NAME_LENTH, name) != 0)
    {
        NSFW_LOGERR("STRCPY_S failed]name=%s", name);
        return NULL;
    }

    mring_handle ring = nsfw_mem_sp_create(&param);
    if (!ring)
    {
        NSFW_LOGERR("Create pool failed]name=%s, num=%d, size=%u", name, num,
                    size);
    }

    return ring;
}

/*****************************************************************************
*   Prototype    : sbr_create_ring
*   Description  : create ring
*   Input        : const char* name
*                  i32 num
*   Output       : None
*   Return Value : mring_handle
*   Calls        :
*   Called By    :
*
*****************************************************************************/
mring_handle sbr_create_ring(const char *name, i32 num)
{
    if (!name)
    {
        NSFW_LOGERR("name is NULL");
        return NULL;
    }

    nsfw_mem_mring param;
    if (EOK != memset_s(&param, sizeof(param), 0, sizeof(param)))
    {
        NSFW_LOGERR("memset error]name=%s", name);
        return NULL;
    }

    param.enmptype = NSFW_MRING_MPMC;
    param.isocket_id = -1;
    param.usnum = num;
    param.stname.entype = NSFW_SHMEM;

    if (strcpy_s(param.stname.aname, NSFW_MEM_NAME_LENTH, name) != 0)
    {
        NSFW_LOGERR("STRCPY_S failed]name=%s", name);
        return NULL;
    }

    mring_handle ring = nsfw_mem_ring_create(&param);
    if (!ring)
    {
        NSFW_LOGERR("Create ring failed]name=%s, num=%d", name, num);
    }

    return ring;
}

/*****************************************************************************
*   Prototype    : sbr_create_multi_ring
*   Description  : create multi ring
*   Input        : const char* name
*                  u32 ring_size
*                  i32 ring_num
*                  mring_handle* array
*                  nsfw_mpool_type type
*   Output       : None
*   Return Value : int
*   Calls        :
*   Called By    :
*
*****************************************************************************/
int
sbr_create_multi_ring(const char *name, u32 ring_size, i32 ring_num,
                      mring_handle * array, nsfw_mpool_type type)
{
    if (!name)
    {
        NSFW_LOGERR("name is NULL");
        return -1;
    }

    if (!array)
    {
        NSFW_LOGERR("array is NULL");
        return -1;
    }

    nsfw_mem_mring param;

    if (EOK != memset_s(&param, sizeof(param), 0, sizeof(param)))
    {
        NSSBR_LOGERR("Error to memset]name=%s", name);
        return -1;
    }

    param.enmptype = type;
    param.stname.entype = NSFW_SHMEM;
    if (strcpy_s(param.stname.aname, NSFW_MEM_NAME_LENTH, name) != 0)
    {
        NSSBR_LOGERR("STRCPY_S failed]name=%s", name);
        return -1;
    }

    param.usnum = ring_size;
    param.isocket_id = -1;
    if (nsfw_mem_sp_ring_create(&param, array, ring_num) != 0)
    {
        NSSBR_LOGERR("Create ring pool failed]name=%s, ring_num=%d", name,
                     ring_num);
        return -1;
    }

    return 0;
}

/*****************************************************************************
*   Prototype    : sbr_lookup_ring
*   Description  : lookup ring
*   Input        : const char* name
*   Output       : None
*   Return Value : mring_handle
*   Calls        :
*   Called By    :
*
*****************************************************************************/
mring_handle sbr_lookup_ring(const char *name)
{
    nsfw_mem_name param;

    param.entype = NSFW_SHMEM;
    param.enowner = NSFW_PROC_MAIN;
    if (strcpy_s(param.aname, NSFW_MEM_NAME_LENTH, name) != 0)
    {
        NSFW_LOGERR("STRCPY_S failed]name=%s", name);
        return NULL;
    }

    mring_handle ring = nsfw_mem_ring_lookup(&param);
    if (!ring)
    {
        NSFW_LOGERR("lookup ring failed]name=%s", name);
    }

    return ring;
}

int sbr_timeval2msec(struct timeval *pTime, u64 * msec)
{
    if ((pTime->tv_sec < 0) || (pTime->tv_usec < 0))
    {
        NSFW_LOGERR("time->tv_sec is nagative");
        return -1;
    }

    if (STACKX_MAX_U64_NUM / 1000 < (u64_t) pTime->tv_sec)
    {
        NSFW_LOGERR("tout.tv_sec is too large]tout.tv_sec=%lu",
                    pTime->tv_sec);
        return -1;
    }

    u64 sec2msec = 1000 * pTime->tv_sec;
    u64 usec2msec = pTime->tv_usec / 1000;

    if (STACKX_MAX_U64_NUM - sec2msec < usec2msec)
    {
        NSFW_LOGERR
            ("nsec2msec plus sec2usec is too large]usec2msec=%lu,usec2msec=%lu",
             usec2msec, sec2msec);
        return -1;
    }

    *msec = sec2msec + usec2msec;
    return 0;
}