1/*********************************************************************
2   PicoTCP. Copyright (c) 2012-2017 Altran Intelligent Systems. Some rights reserved.
3   See COPYING, LICENSE.GPLv2 and LICENSE.GPLv3 for usage.
4
5 *********************************************************************/
6#include "pico_defines.h"
7#ifndef INCLUDE_PICO_CONFIG
8#define INCLUDE_PICO_CONFIG
9#ifndef __KERNEL__
10#include <stddef.h>
11#include <stdint.h>
12#include <stdlib.h>
13#include <string.h>
14#else
15#include <linux/types.h>
16#endif
17
18#if defined __IAR_SYSTEMS_ICC__ || defined ATOP
19#   define PACKED_STRUCT_DEF __packed struct
20#   define PEDANTIC_STRUCT_DEF __packed struct
21#   define PACKED_UNION_DEF  __packed union
22#   define PACKED __packed
23#   define WEAK
24#elif defined __WATCOMC__
25#   define PACKED_STRUCT_DEF   _Packed struct
26#   define PEDANTIC_STRUCT_DEF struct
27#   define PACKED_UNION_DEF    _Packed union
28#   define WEAK
29#else
30#   define PACKED_STRUCT_DEF struct __attribute__((packed))
31#   define PEDANTIC_STRUCT_DEF struct
32#   define PACKED_UNION_DEF  union   /* Sane compilers do not require packed unions */
33#   define PACKED __attribute__((packed))
34#   define WEAK __attribute__((weak))
35#   ifdef __GNUC__
36#       define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
37#       if ((GCC_VERSION >= 40800))
38#           define BYTESWAP_GCC
39#       endif
40#   endif
41#endif
42
43#ifdef PICO_BIGENDIAN
44
45# define PICO_IDETH_IPV4 0x0800
46# define PICO_IDETH_ARP 0x0806
47# define PICO_IDETH_IPV6 0x86DD
48
49# define PICO_ARP_REQUEST 0x0001
50# define PICO_ARP_REPLY   0x0002
51# define PICO_ARP_HTYPE_ETH 0x0001
52
53#define short_be(x) (x)
54#define long_be(x) (x)
55#define long_long_be(x) (x)
56
57static inline uint16_t short_from(void *_p)
58{
59    unsigned char *p = (unsigned char *)_p;
60    uint16_t r, p0, p1;
61    p0 = p[0];
62    p1 = p[1];
63    r = (p0 << 8) + p1;
64    return r;
65}
66
67static inline uint32_t long_from(void *_p)
68{
69    unsigned char *p = (unsigned char *)_p;
70    uint32_t r, p0, p1, p2, p3;
71    p0 = p[0];
72    p1 = p[1];
73    p2 = p[2];
74    p3 = p[3];
75    r = (p0 << 24) + (p1 << 16) + (p2 << 8) + p3;
76    return r;
77}
78
79#else
80
81static inline uint16_t short_from(void *_p)
82{
83    unsigned char *p = (unsigned char *)_p;
84    uint16_t r, _p0, _p1;
85    _p0 = p[0];
86    _p1 = p[1];
87    r = (uint16_t)((_p1 << 8u) + _p0);
88    return r;
89}
90
91static inline uint32_t long_from(void *_p)
92{
93    unsigned char *p = (unsigned char *)_p;
94    uint32_t r, _p0, _p1, _p2, _p3;
95    _p0 = p[0];
96    _p1 = p[1];
97    _p2 = p[2];
98    _p3 = p[3];
99    r = (_p3 << 24) + (_p2 << 16) + (_p1 << 8) + _p0;
100    return r;
101}
102
103
104# define PICO_IDETH_IPV4 0x0008
105# define PICO_IDETH_ARP 0x0608
106# define PICO_IDETH_IPV6 0xDD86
107
108# define PICO_ARP_REQUEST 0x0100
109# define PICO_ARP_REPLY   0x0200
110# define PICO_ARP_HTYPE_ETH 0x0100
111
112#   ifndef BYTESWAP_GCC
113static inline uint16_t short_be(uint16_t le)
114{
115    return (uint16_t)(((le & 0xFFu) << 8) | ((le >> 8u) & 0xFFu));
116}
117
118static inline uint32_t long_be(uint32_t le)
119{
120    uint8_t *b = (uint8_t *)&le;
121    uint32_t be = 0;
122    uint32_t b0, b1, b2;
123    b0 = b[0];
124    b1 = b[1];
125    b2 = b[2];
126    be = b[3] + (b2 << 8) + (b1 << 16) + (b0 << 24);
127    return be;
128}
129static inline uint64_t long_long_be(uint64_t le)
130{
131    uint8_t *b = (uint8_t *)&le;
132    uint64_t be = 0;
133    uint64_t b0, b1, b2, b3, b4, b5, b6;
134    b0 = b[0];
135    b1 = b[1];
136    b2 = b[2];
137    b3 = b[3];
138    b4 = b[4];
139    b5 = b[5];
140    b6 = b[6];
141    be = b[7] + (b6 << 8) + (b5 << 16) + (b4 << 24) + (b3 << 32) + (b2 << 40) + (b1 << 48) + (b0 << 56);
142    return be;
143}
144#   else
145/*
146   extern uint32_t __builtin_bswap32(uint32_t);
147   extern uint16_t __builtin_bswap16(uint16_t);
148   extern uint64_t __builtin_bswap64(uint64_t);
149 */
150
151static inline uint32_t long_be(uint32_t le)
152{
153    return (uint32_t)__builtin_bswap32(le);
154}
155
156static inline uint16_t short_be(uint16_t le)
157{
158    return (uint16_t)__builtin_bswap16(le);
159}
160
161static inline uint64_t long_long_be(uint64_t le)
162{
163    return (uint64_t)__builtin_bswap64(le);
164}
165
166#   endif /* BYTESWAP_GCC */
167#endif
168
169/* Mockables */
170#if defined UNIT_TEST
171#   define MOCKABLE __attribute__((weak))
172#else
173#   define MOCKABLE
174#endif
175
176#include "pico_constants.h"
177#include "pico_mm.h"
178
179#define IGNORE_PARAMETER(x)  ((void)x)
180
181#define PICO_MEM_DEFAULT_SLAB_SIZE 1600
182#define PICO_MEM_PAGE_SIZE 4096
183#define PICO_MEM_PAGE_LIFETIME 100
184#define PICO_MIN_HEAP_SIZE 600
185#define PICO_MIN_SLAB_SIZE 1200
186#define PICO_MAX_SLAB_SIZE 1600
187#define PICO_MEM_MINIMUM_OBJECT_SIZE 4
188
189/*** *** *** *** *** *** ***
190 *** PLATFORM SPECIFIC   ***
191 *** *** *** *** *** *** ***/
192#if defined PICO_PORT_CUSTOM
193# include "pico_port.h"
194#elif defined CORTEX_M4_HARDFLOAT
195# include "arch/pico_cortex_m.h"
196#elif defined CORTEX_M4_SOFTFLOAT
197# include "arch/pico_cortex_m.h"
198#elif defined CORTEX_M3
199# include "arch/pico_cortex_m.h"
200#elif defined CORTEX_M0
201# include "arch/pico_cortex_m.h"
202#elif defined DOS_WATCOM
203# include "arch/pico_dos.h"
204#elif defined PIC24
205# include "arch/pico_pic24.h"
206#elif defined PIC32
207# include "arch/pico_pic32.h"
208#elif defined MSP430
209# include "arch/pico_msp430.h"
210#elif defined MBED_TEST
211# include "arch/pico_mbed.h"
212#elif defined AVR
213# include "arch/pico_avr.h"
214#elif defined ARM9
215# include "arch/pico_arm9.h"
216#elif defined ESP8266
217# include "arch/pico_esp8266.h"
218#elif defined ATSAMD21J18
219# include "arch/pico_atsamd21j18.h"
220#elif defined MT7681
221# include "arch/pico_generic_gcc.h"
222#elif defined FAULTY
223# include "../test/pico_faulty.h"
224#elif defined ARCHNONE
225# include "arch/pico_none.h"
226#elif defined GENERIC
227# include "arch/pico_generic_gcc.h"
228#elif defined __KERNEL__
229# include "arch/pico_linux.h"
230/* #elif defined ... */
231#else
232# include "arch/pico_posix.h"
233#endif
234
235#ifdef PICO_SUPPORT_MM
236#define PICO_ZALLOC(x) pico_mem_zalloc(x)
237#define PICO_FREE(x) pico_mem_free(x)
238#else
239#define PICO_ZALLOC(x) pico_zalloc(x)
240#define PICO_FREE(x) pico_free(x)
241#endif  /* PICO_SUPPORT_MM */
242
243#endif
244