Commit d69bc71a authored by Daniele Lacamera's avatar Daniele Lacamera

Added uncrustify. Enforced some coding rules. Added "make style" target.

parent a6972ebe
......@@ -239,3 +239,7 @@ mbed:
@zip -0 PicoTCP.zip -r include modules stack -x include/arch/ include/arch/* include/pico_config.h include/*.tmp modules/ptsocket/* modules/ptsocket/ modules/ptsocket/test/ modules/ptsocket/test/* modules/pico_dev_*
@rm include/pico_socket.h include/mbed.tmp
@mv include/socket.tmp include/pico_socket.h
style:
find . -iname "*.[c|h]" |xargs -x uncrustify --replace -l C -c uncrustify.cfg || true
/*********************************************************************
PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
See LICENSE and COPYING for usage.
PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
See LICENSE and COPYING for usage.
*********************************************************************/
*********************************************************************/
#ifndef _INCLUDE_PICO_LPC
#define _INCLUDE_PICO_LPC
......@@ -31,14 +31,14 @@ extern void pico_mutex_unlock(void*);
#define pico_free(x) vPortFree(x)
#define free(x) vPortFree(x)
static inline void * pico_zalloc(size_t size)
static inline void *pico_zalloc(size_t size)
{
void *ptr = pvPortMalloc(size);
void *ptr = pvPortMalloc(size);
if(ptr)
memset(ptr,0u,size);
if(ptr)
memset(ptr, 0u, size);
return ptr;
return ptr;
}
#define dbg(...)
......
/*********************************************************************
PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
See LICENSE and COPYING for usage.
Do not redistribute without a written permission by the Copyright
holders.
File: pico_mbed.h
Author: Toon Peters
*********************************************************************/
#ifndef PICO_SUPPORT_MBED
#define PICO_SUPPORT_MBED
#include <stdio.h>
//#include "mbed.h"
//#include "serial_api.h"
//#define TIME_PRESCALE
//#define PICO_MEASURE_STACK
//#define MEMORY_MEASURE
/*
Debug needs initialization:
* void serial_init (serial_t *obj, PinName tx, PinName rx);
* void serial_baud (serial_t *obj, int baudrate);
* void serial_format (serial_t *obj, int data_bits, SerialParity parity, int stop_bits);
*/
#define dbg(...)
#ifdef PICO_MEASURE_STACK
extern int freeStack;
#define STACK_TOTAL_WORDS 1000u
#define STACK_PATTERN (0xC0CAC01A)
void stack_fill_pattern(void *ptr);
void stack_count_free_words(void *ptr);
int stack_get_free_words(void);
#else
#define stack_fill_pattern(...) do{}while(0)
#define stack_count_free_words(...) do{}while(0)
#define stack_get_free_words() (0)
#endif
#ifdef MEMORY_MEASURE // in case, comment out the two defines above me.
extern uint32_t max_mem;
extern uint32_t cur_mem;
static inline void * pico_zalloc(int x)
{
uint32_t *ptr;
if ((cur_mem + x )> (10 * 1024))
return NULL;
ptr = (uint32_t *)calloc(x + 4, 1);
*ptr = (uint32_t)x;
cur_mem += x;
if (cur_mem > max_mem) {
max_mem = cur_mem;
// printf("max mem: %lu\n", max_mem);
}
return (void*)(ptr + 1);
}
static inline void pico_free(void *x)
{
uint32_t *ptr = (uint32_t*)(((uint8_t *)x) - 4);
cur_mem -= *ptr;
free(ptr);
}
#else
#define pico_zalloc(x) calloc(x, 1)
#define pico_free(x) free(x)
#endif
#define PICO_SUPPORT_MUTEX
extern void *pico_mutex_init(void);
extern void pico_mutex_lock(void*);
extern void pico_mutex_unlock(void*);
extern uint32_t os_time;
extern uint64_t local_time;
extern uint32_t last_os_time;
#ifdef TIME_PRESCALE
extern int32_t prescale_time;
#endif
extern uint32_t os_time;
#define UPDATE_LOCAL_TIME() do{local_time=local_time+(os_time-last_os_time);last_os_time=os_time;}while(0)
static inline uint64_t PICO_TIME(void)
{
UPDATE_LOCAL_TIME();
#ifdef TIME_PRESCALE
return (prescale_time < 0) ? (uint64_t)(local_time / 1000 << (-prescale_time)) : \
(uint64_t)(local_time / 1000 >> prescale_time);
#else
return (uint64_t)(local_time / 1000);
#endif
}
static inline uint64_t PICO_TIME_MS(void)
{
UPDATE_LOCAL_TIME();
#ifdef TIME_PRESCALE
return (prescale_time < 0) ? (uint64_t)(local_time << (-prescale_time)) : \
(uint64_t)(local_time >> prescale_time);
#else
return (uint64_t)local_time;
#endif
}
static inline void PICO_IDLE(void)
{
// TODO needs implementation
}
/*
static inline void PICO_DEBUG(const char * formatter, ... )
{
char buffer[256];
char *ptr;
va_list args;
va_start(args, formatter);
vsnprintf(buffer, 256, formatter, args);
ptr = buffer;
while(*ptr != '\0')
serial_putc(serial_t *obj, (int) (*(ptr++)));
va_end(args);
//TODO implement serial_t
}*/
#endif
/*********************************************************************
PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
See LICENSE and COPYING for usage.
Do not redistribute without a written permission by the Copyright
holders.
File: pico_mbed.h
Author: Toon Peters
*********************************************************************/
#ifndef PICO_SUPPORT_MBED
#define PICO_SUPPORT_MBED
#include <stdio.h>
/* #include "mbed.h" */
/* #include "serial_api.h" */
/* #define TIME_PRESCALE */
/* #define PICO_MEASURE_STACK */
/* #define MEMORY_MEASURE */
/*
Debug needs initialization:
* void serial_init (serial_t *obj, PinName tx, PinName rx);
* void serial_baud (serial_t *obj, int baudrate);
* void serial_format (serial_t *obj, int data_bits, SerialParity parity, int stop_bits);
*/
#define dbg(...)
#ifdef PICO_MEASURE_STACK
extern int freeStack;
#define STACK_TOTAL_WORDS 1000u
#define STACK_PATTERN (0xC0CAC01A)
void stack_fill_pattern(void *ptr);
void stack_count_free_words(void *ptr);
int stack_get_free_words(void);
#else
#define stack_fill_pattern(...) do {} while(0)
#define stack_count_free_words(...) do {} while(0)
#define stack_get_free_words() (0)
#endif
#ifdef MEMORY_MEASURE /* in case, comment out the two defines above me. */
extern uint32_t max_mem;
extern uint32_t cur_mem;
static inline void *pico_zalloc(int x)
{
uint32_t *ptr;
if ((cur_mem + x) > (10 * 1024))
return NULL;
ptr = (uint32_t *)calloc(x + 4, 1);
*ptr = (uint32_t)x;
cur_mem += x;
if (cur_mem > max_mem) {
max_mem = cur_mem;
/* printf("max mem: %lu\n", max_mem); */
}
return (void*)(ptr + 1);
}
static inline void pico_free(void *x)
{
uint32_t *ptr = (uint32_t*)(((uint8_t *)x) - 4);
cur_mem -= *ptr;
free(ptr);
}
#else
#define pico_zalloc(x) calloc(x, 1)
#define pico_free(x) free(x)
#endif
#define PICO_SUPPORT_MUTEX
extern void *pico_mutex_init(void);
extern void pico_mutex_lock(void*);
extern void pico_mutex_unlock(void*);
extern uint32_t os_time;
extern uint64_t local_time;
extern uint32_t last_os_time;
#ifdef TIME_PRESCALE
extern int32_t prescale_time;
#endif
extern uint32_t os_time;
#define UPDATE_LOCAL_TIME() do {local_time = local_time + (os_time - last_os_time);last_os_time = os_time;} while(0)
static inline uint64_t PICO_TIME(void)
{
UPDATE_LOCAL_TIME();
#ifdef TIME_PRESCALE
return (prescale_time < 0) ? (uint64_t)(local_time / 1000 << (-prescale_time)) : \
(uint64_t)(local_time / 1000 >> prescale_time);
#else
return (uint64_t)(local_time / 1000);
#endif
}
static inline uint64_t PICO_TIME_MS(void)
{
UPDATE_LOCAL_TIME();
#ifdef TIME_PRESCALE
return (prescale_time < 0) ? (uint64_t)(local_time << (-prescale_time)) : \
(uint64_t)(local_time >> prescale_time);
#else
return (uint64_t)local_time;
#endif
}
static inline void PICO_IDLE(void)
{
/* TODO needs implementation */
}
/*
static inline void PICO_DEBUG(const char * formatter, ... )
{
char buffer[256];
char *ptr;
va_list args;
va_start(args, formatter);
vsnprintf(buffer, 256, formatter, args);
ptr = buffer;
while(*ptr != '\0')
serial_putc(serial_t *obj, (int) (*(ptr++)));
va_end(args);
//TODO implement serial_t
}*/
#endif
/*********************************************************************
PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
See LICENSE and COPYING for usage.
PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
See LICENSE and COPYING for usage.
*********************************************************************/
*********************************************************************/
#ifndef _INCLUDE_PICO_LPC
#define _INCLUDE_PICO_LPC
......@@ -19,20 +19,20 @@ extern void free(void *);
#define PICO_TIME() msp430_time_s()
#define PICO_TIME_MS() msp430_time_ms()
#define PICO_IDLE() do{}while(0)
#define PICO_IDLE() do {} while(0)
#define pico_free(x) free(x)
static inline void * pico_zalloc(size_t size)
static inline void *pico_zalloc(size_t size)
{
void *ptr = malloc(size);
void *ptr = malloc(size);
if(ptr)
memset(ptr,0u,size);
if(ptr)
memset(ptr, 0u, size);
return ptr;
return ptr;
}
#define dbg(...)
#define dbg(...)
#endif
#ifndef PICO_SUPPORT_PIC24
#define PICO_SUPPORT_PIC24
#define dbg printf
//#define dbg(...)
/* #define dbg(...) */
/*************************/
......@@ -9,7 +9,7 @@
#include <stdio.h>
#include <stdint.h>
//#include "phalox_development_board.h"
/* #include "phalox_development_board.h" */
#ifndef __PIC24F__
#define __PIC24F__
......@@ -28,7 +28,7 @@
#endif
#ifndef PICO_SUPPORT_DHCP
//#define PICO_SUPPORT_DHCP
/* #define PICO_SUPPORT_DHCP */
#endif
#ifndef PICO_SUPPORT_TCP
......@@ -69,13 +69,13 @@
#ifdef PICO_SUPPORT_DEBUG_MEMORY
static inline void *pico_zalloc(int len)
{
//dbg("%s: Alloc object of len %d, caller: %p\n", __FUNCTION__, len, __builtin_return_address(0));
/* dbg("%s: Alloc object of len %d, caller: %p\n", __FUNCTION__, len, __builtin_return_address(0)); */
return calloc(len, 1);
}
static inline void pico_free(void *tgt)
{
//dbg("%s: Discarded object @%p, caller: %p\n", __FUNCTION__, tgt, __builtin_return_address(0));
/* dbg("%s: Discarded object @%p, caller: %p\n", __FUNCTION__, tgt, __builtin_return_address(0)); */
free(tgt);
}
#else
......@@ -83,42 +83,42 @@ static inline void pico_free(void *tgt)
# define pico_free(x) free(x)
#endif
extern void * pvPortMalloc( size_t xWantedSize );
extern void *pvPortMalloc( size_t xWantedSize );
extern volatile unsigned long __pic24_tick;
static inline unsigned long PICO_TIME(void)
{
unsigned long tick;
// Disable timer interrupts
TIMBASE_INT_E = 0;
tick = __pic24_tick;
// Enable timer interrupts
TIMBASE_INT_E = 1;
return tick / 1000;
unsigned long tick;
/* Disable timer interrupts */
TIMBASE_INT_E = 0;
tick = __pic24_tick;
/* Enable timer interrupts */
TIMBASE_INT_E = 1;
return tick / 1000;
}
static inline unsigned long PICO_TIME_MS(void)
{
unsigned long tick;
// Disable timer interrupts
TIMBASE_INT_E = 0;
tick = __pic24_tick;
// Enable timer interrupts
TIMBASE_INT_E = 1;
return tick;
unsigned long tick;
/* Disable timer interrupts */
TIMBASE_INT_E = 0;
tick = __pic24_tick;
/* Enable timer interrupts */
TIMBASE_INT_E = 1;
return tick;
}
static inline void PICO_IDLE(void)
{
unsigned long tick_now;
// Disable timer interrupts
TIMBASE_INT_E = 0;
tick_now = pico_tick;
// Enable timer interrupts
TIMBASE_INT_E = 1;
// Doesn't matter that this call isn't interrupt safe,
// we just check for the value to change
while(tick_now == __pic24_tick);
unsigned long tick_now;
/* Disable timer interrupts */
TIMBASE_INT_E = 0;
tick_now = pico_tick;
/* Enable timer interrupts */
TIMBASE_INT_E = 1;
/* Doesn't matter that this call isn't interrupt safe, */
/* we just check for the value to change */
while(tick_now == __pic24_tick) ;
}
#endif
......@@ -16,13 +16,13 @@
#ifdef PICO_SUPPORT_DEBUG_MEMORY
static inline void *pico_zalloc(int len)
{
//dbg("%s: Alloc object of len %d, caller: %p\n", __FUNCTION__, len, __builtin_return_address(0));
/* dbg("%s: Alloc object of len %d, caller: %p\n", __FUNCTION__, len, __builtin_return_address(0)); */
return calloc(len, 1);
}
static inline void pico_free(void *tgt)
{
//dbg("%s: Discarded object @%p, caller: %p\n", __FUNCTION__, tgt, __builtin_return_address(0));
/* dbg("%s: Discarded object @%p, caller: %p\n", __FUNCTION__, tgt, __builtin_return_address(0)); */
free(tgt);
}
#else
......@@ -34,21 +34,21 @@ static inline void pico_free(void *tgt)
static inline pico_time PICO_TIME(void)
{
struct timeval t;
gettimeofday(&t, NULL);
return (pico_time)t.tv_sec;
struct timeval t;
gettimeofday(&t, NULL);
return (pico_time)t.tv_sec;
}
static inline pico_time PICO_TIME_MS(void)
{
struct timeval t;
gettimeofday(&t, NULL);
return (pico_time)((t.tv_sec * 1000) + (t.tv_usec / 1000));
struct timeval t;
gettimeofday(&t, NULL);
return (pico_time)((t.tv_sec * 1000) + (t.tv_usec / 1000));
}
static inline void PICO_IDLE(void)
{
usleep(5000);
usleep(5000);
}
......
#ifndef PICO_SUPPORT_STELLARIS
#define PICO_SUPPORT_STELLARIS
//#define dbg printf
#define dbg(...) do{}while(0)
/* #define dbg printf */
#define dbg(...) do {} while(0)
/*************************/
......@@ -17,13 +17,13 @@ extern volatile uint32_t stellaris_tick;
#ifdef PICO_SUPPORT_DEBUG_MEMORY
static inline void *pico_zalloc(int len)
{
//dbg("%s: Alloc object of len %d, caller: %p\n", __FUNCTION__, len, __builtin_return_address(0));
/* dbg("%s: Alloc object of len %d, caller: %p\n", __FUNCTION__, len, __builtin_return_address(0)); */
return calloc(len, 1);
}
static inline void pico_free(void *tgt)
{
//dbg("%s: Discarded object @%p, caller: %p\n", __FUNCTION__, tgt, __builtin_return_address(0));
/* dbg("%s: Discarded object @%p, caller: %p\n", __FUNCTION__, tgt, __builtin_return_address(0)); */
free(tgt);
}
#else
......@@ -35,19 +35,19 @@ static inline void pico_free(void *tgt)
static inline unsigned long PICO_TIME(void)
{
register uint32_t tick = stellaris_tick;
return tick / 1000;
register uint32_t tick = stellaris_tick;
return tick / 1000;
}
static inline unsigned long PICO_TIME_MS(void)
{
return stellaris_tick;
return stellaris_tick;
}
static inline void PICO_IDLE(void)
{
unsigned long tick_now = stellaris_tick;
while(tick_now == stellaris_tick);
unsigned long tick_now = stellaris_tick;
while(tick_now == stellaris_tick) ;
}
#endif
......
#define dbg(...) do{}while(0)
//#define dbg printf
#define dbg(...) do {} while(0)
/* #define dbg printf */
/*************************/
......@@ -16,13 +16,13 @@ extern volatile uint32_t __stm32_tick;
#ifdef PICO_SUPPORT_DEBUG_MEMORY
static inline void *pico_zalloc(int len)
{
//dbg("%s: Alloc object of len %d, caller: %p\n", __FUNCTION__, len, __builtin_return_address(0));
/* dbg("%s: Alloc object of len %d, caller: %p\n", __FUNCTION__, len, __builtin_return_address(0)); */
return calloc(len, 1);
}
static inline void pico_free(void *tgt)
{
//dbg("%s: Discarded object @%p, caller: %p\n", __FUNCTION__, tgt, __builtin_return_address(0));
/* dbg("%s: Discarded object @%p, caller: %p\n", __FUNCTION__, tgt, __builtin_return_address(0)); */
free(tgt);
}
#else
......@@ -34,18 +34,18 @@ static inline void pico_free(void *tgt)
static inline unsigned long PICO_TIME(void)
{
register uint32_t tick = __stm32_tick;
return tick / 1000;
register uint32_t tick = __stm32_tick;
return tick / 1000;
}
static inline unsigned long PICO_TIME_MS(void)
{
return __stm32_tick;
return __stm32_tick;
}
static inline void PICO_IDLE(void)
{
unsigned long tick_now = __stm32_tick;
while(tick_now == __stm32_tick);
unsigned long tick_now = __stm32_tick;
while(tick_now == __stm32_tick) ;
}
/*********************************************************************
PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
See LICENSE and COPYING for usage.
PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
See LICENSE and COPYING for usage.
*********************************************************************/
*********************************************************************/
#define DECLARE_HEAP(type, orderby) \
struct heap_##type { \
uint32_t size; \
uint32_t n; \
type *top; \
}; \
typedef struct heap_##type heap_##type; \
static inline int heap_insert(struct heap_##type *heap, type *el) \
{ \
uint32_t i; \
type * newTop; \
if (++heap->n >= heap->size) { \
newTop = pico_zalloc((heap->n + 1) * sizeof(type)); \
if(!newTop) \
return -1; \
if (heap->top) {\
memcpy(newTop,heap->top,heap->n*sizeof(type)); \
pico_free(heap->top); \
struct heap_ ## type { \
uint32_t size; \
uint32_t n; \
type *top; \
}; \
typedef struct heap_ ## type heap_ ## type; \
static inline int heap_insert(struct heap_ ## type *heap, type * el) \
{ \
uint32_t i; \
type *newTop; \
if (++heap->n >= heap->size) { \
newTop = pico_zalloc((heap->n + 1) * sizeof(type)); \
if(!newTop) \
return -1; \
if (heap->top) { \
memcpy(newTop, heap->top, heap->n * sizeof(type)); \
pico_free(heap->top); \
} \
heap->top = newTop; \
heap->size++; \
} \
if (heap->n == 1) { \
memcpy(&heap->top[1], el, sizeof(type)); \
return 0; \
} \
for (i = heap->n; ((i > 1) && (heap->top[i / 2].orderby > el->orderby)); i /= 2) { \
memcpy(&heap->top[i], &heap->top[i / 2], sizeof(type)); \
} \
memcpy(&heap->top[i], el, sizeof(type)); \
return 0; \
} \
static inline int heap_peek(struct heap_ ## type *heap, type * first) \
{ \
type *last; \
uint32_t i, child; \
if(heap->n == 0) { \
return -1; \
} \
memcpy(first, &heap->top[1], sizeof(type)); \
last = &heap->top[heap->n--]; \
for(i = 1; (i * 2) <= heap->n; i = child) { \
child = 2 * i; \
if ((child != heap->n) && \
(heap->top[child + 1]).orderby \
< (heap->top[child]).orderby) \
child++; \
if (last->orderby > \
heap->top[child].orderby) \
memcpy(&heap->top[i], &heap->top[child], \
sizeof(type)); \
else \
break; \
} \
memcpy(&heap->top[i], last, sizeof(type)); \
return 0; \
} \
static inline type *heap_first(heap_ ## type * heap) \
{ \
if (heap->n == 0) \
return NULL; \
return &heap->top[1]; \
} \
static inline heap_ ## type *heap_init(void) \
{ \
heap_ ## type * p = (heap_ ## type *)pico_zalloc(sizeof(heap_ ## type)); \
return p; \
} \
static inline void heap_destroy(heap_ ## type * h) \
{ \
pico_free(h->top); \
pico_free(h); \
} \
heap->top = newTop; \
heap->size++; \
} \
if (heap->n == 1) { \
memcpy(&heap->top[1], el, sizeof(type)); \
return 0; \
} \
for (i = heap->n; ((i > 1) && (heap->top[i / 2].orderby > el->orderby)); i /= 2) { \
memcpy(&heap->top[i], &heap->top[i / 2], sizeof(type)); \
} \
memcpy(&heap->top[i], el, sizeof(type)); \
return 0; \
} \
static inline int heap_peek(struct heap_##type *heap, type *first) \
{ \
type *last; \
uint32_t i, child; \
if(heap->n == 0) { \
return -1; \