final coding convention fixes

dev/timer
Oliver Hahm 10 years ago
parent 5c52e1ce2e
commit d01fd9a508

@ -1,28 +1,13 @@
/******************************************************************************
Copyright 2008, Freie Universitaet Berlin (FUB). All rights reserved.
These sources were developed at the Freie Universitaet Berlin, Computer Systems
and Telematics group (http://cst.mi.fu-berlin.de).
-------------------------------------------------------------------------------
This file is part of RIOT.
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
RIOT is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see http://www.gnu.org/licenses/ .
--------------------------------------------------------------------------------
For further information and questions please use the web site
http://scatterweb.mi.fu-berlin.de
and the mailinglist (subscription via web site)
scatterweb@lists.spline.inf.fu-berlin.de
*******************************************************************************/
/**
* TI Chipcon CC110x radio driver
*
* Copyright (C) 2009-2013 Freie Universität Berlin
*
* This file subject to the terms and conditions of the GNU Lesser General
* Public License. See the file LICENSE in the top level directory for more
* details.
*
*/
/**
* @ingroup dev_cc110x

@ -6,12 +6,14 @@ unsigned long hash_string(unsigned char *str)
unsigned long hash = 5381;
int c;
while ((c = *str++))
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
while((c = *str++)) {
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
}
return hash;
}
int cmp_string(char* a, char* b) {
return (strcmp(a,b) == 0);
int cmp_string(char *a, char *b)
{
return (strcmp(a, b) == 0);
}

@ -1,7 +1,7 @@
#ifndef __HASH_STRING_H
#define __HASH_STRING_H
#define __HASH_STRING_H
unsigned long hash_string(unsigned char *str);
int cmp_string(char* a, char* b);
int cmp_string(char *a, char *b);
#endif /* __HASH_STRING_H */

@ -14,35 +14,52 @@ Credit for primes table: Aaron Krowne
http://planetmath.org/encyclopedia/GoodHashTablePrimes.html
*/
static const uint32_t primes[] = {
53, 97, 193, 389,
769, 1543, 3079, 6151,
12289, 24593, 49157, 98317,
196613, 393241, 786433, 1572869,
3145739, 6291469, 12582917, 25165843,
50331653, 100663319, 201326611, 402653189,
805306457, 1610612741
53, 97, 193, 389,
769, 1543, 3079, 6151,
12289, 24593, 49157, 98317,
196613, 393241, 786433, 1572869,
3145739, 6291469, 12582917, 25165843,
50331653, 100663319, 201326611, 402653189,
805306457, 1610612741
};
const unsigned int prime_table_length = sizeof(primes)/sizeof(primes[0]);
const unsigned int prime_table_length = sizeof(primes) / sizeof(primes[0]);
const float max_load_factor = 0.65;
/*****************************************************************************/
struct hashtable *
create_hashtable(uint32_t minsize,
unsigned int (*hashf) (void*),
int (*eqf) (void*,void*))
unsigned int (*hashf)(void *),
int (*eqf)(void *, void *))
{
struct hashtable *h;
unsigned int pindex, size = primes[0];
/* Check requested hashtable isn't too large */
if (minsize > (1u << 30)) return NULL;
if(minsize > (1u << 30)) {
return NULL;
}
/* Enforce size as prime */
for (pindex=0; pindex < prime_table_length; pindex++) {
if (primes[pindex] > minsize) { size = primes[pindex]; break; }
for(pindex = 0; pindex < prime_table_length; pindex++) {
if(primes[pindex] > minsize) {
size = primes[pindex];
break;
}
}
h = (struct hashtable *)malloc(sizeof(struct hashtable));
if (NULL == h) return NULL; /*oom*/
h->table = (struct entry **)malloc(sizeof(struct entry*) * size);
if (NULL == h->table) { free(h); return NULL; } /*oom*/
if(NULL == h) {
return NULL; /*oom*/
}
h->table = (struct entry **)malloc(sizeof(struct entry *) * size);
if(NULL == h->table) {
free(h); /*oom*/
return NULL;
}
memset(h->table, 0, size * sizeof(struct entry *));
h->tablelength = size;
h->primeindex = pindex;
@ -61,9 +78,9 @@ hash(struct hashtable *h, void *k)
* - logic taken from java 1.4 hashtable source */
unsigned int i = h->hashfn(k);
i += ~(i << 9);
i ^= ((i >> 14) | (i << 18)); /* >>> */
i += (i << 4);
i ^= ((i >> 10) | (i << 22)); /* >>> */
i ^= ((i >> 14) | (i << 18)); /* >>> */
i += (i << 4);
i ^= ((i >> 10) | (i << 22)); /* >>> */
return i;
}
@ -76,44 +93,54 @@ hashtable_expand(struct hashtable *h)
struct entry *e;
struct entry **pE;
unsigned int newsize, i, index;
/* Check we're not hitting max capacity */
if (h->primeindex == (prime_table_length - 1)) return 0;
if(h->primeindex == (prime_table_length - 1)) {
return 0;
}
newsize = primes[++(h->primeindex)];
newtable = (struct entry **)malloc(sizeof(struct entry*) * newsize);
if (NULL != newtable)
{
newtable = (struct entry **)malloc(sizeof(struct entry *) * newsize);
if(NULL != newtable) {
memset(newtable, 0, newsize * sizeof(struct entry *));
/* This algorithm is not 'stable'. ie. it reverses the list
* when it transfers entries between the tables */
for (i = 0; i < h->tablelength; i++) {
while (NULL != (e = h->table[i])) {
for(i = 0; i < h->tablelength; i++) {
while(NULL != (e = h->table[i])) {
h->table[i] = e->next;
index = indexFor(newsize,e->h);
index = indexFor(newsize, e->h);
e->next = newtable[index];
newtable[index] = e;
}
}
free(h->table);
h->table = newtable;
}
/* Plan B: realloc instead */
else
{
else {
newtable = (struct entry **)
realloc(h->table, newsize * sizeof(struct entry *));
if (NULL == newtable) { (h->primeindex)--; return 0; }
if(NULL == newtable) {
(h->primeindex)--;
return 0;
}
h->table = newtable;
memset(newtable[h->tablelength], 0, newsize - h->tablelength);
for (i = 0; i < h->tablelength; i++) {
for (pE = &(newtable[i]), e = *pE; e != NULL; e = *pE) {
index = indexFor(newsize,e->h);
if (index == i)
{
for(i = 0; i < h->tablelength; i++) {
for(pE = &(newtable[i]), e = *pE; e != NULL; e = *pE) {
index = indexFor(newsize, e->h);
if(index == i) {
pE = &(e->next);
}
else
{
else {
*pE = e->next;
e->next = newtable[index];
newtable[index] = e;
@ -121,6 +148,7 @@ hashtable_expand(struct hashtable *h)
}
}
}
h->tablelength = newsize;
h->loadlimit = (unsigned int) ceil(newsize * max_load_factor);
return -1;
@ -140,18 +168,24 @@ hashtable_insert(struct hashtable *h, void *k, void *v)
/* This method allows duplicate keys - but they shouldn't be used */
unsigned int index;
struct entry *e;
if (++(h->entrycount) > h->loadlimit)
{
if(++(h->entrycount) > h->loadlimit) {
/* Ignore the return value. If expand fails, we should
* still try cramming just this value into the existing table
* -- we may not have memory for a larger table, but one more
* element may be ok. Next time we insert, we'll try expanding again.*/
hashtable_expand(h);
}
e = (struct entry *)malloc(sizeof(struct entry));
if (NULL == e) { --(h->entrycount); return 0; } /*oom*/
e->h = hash(h,k);
index = indexFor(h->tablelength,e->h);
if(NULL == e) {
--(h->entrycount); /*oom*/
return 0;
}
e->h = hash(h, k);
index = indexFor(h->tablelength, e->h);
e->k = k;
e->v = v;
e->next = h->table[index];
@ -165,15 +199,19 @@ hashtable_search(struct hashtable *h, void *k)
{
struct entry *e;
unsigned int hashvalue, index;
hashvalue = hash(h,k);
index = indexFor(h->tablelength,hashvalue);
hashvalue = hash(h, k);
index = indexFor(h->tablelength, hashvalue);
e = h->table[index];
while (NULL != e)
{
while(NULL != e) {
/* Check hash value to short circuit heavier comparison */
if ((hashvalue == e->h) && (h->eqfn(k, e->k))) return e->v;
if((hashvalue == e->h) && (h->eqfn(k, e->k))) {
return e->v;
}
e = e->next;
}
return NULL;
}
@ -189,15 +227,14 @@ hashtable_remove(struct hashtable *h, void *k)
void *v;
unsigned int hashvalue, index;
hashvalue = hash(h,k);
index = indexFor(h->tablelength,hash(h,k));
hashvalue = hash(h, k);
index = indexFor(h->tablelength, hash(h, k));
pE = &(h->table[index]);
e = *pE;
while (NULL != e)
{
while(NULL != e) {
/* Check hash value to short circuit heavier comparison */
if ((hashvalue == e->h) && (h->eqfn(k, e->k)))
{
if((hashvalue == e->h) && (h->eqfn(k, e->k))) {
*pE = e->next;
h->entrycount--;
v = e->v;
@ -205,9 +242,11 @@ hashtable_remove(struct hashtable *h, void *k)
free(e);
return v;
}
pE = &(e->next);
e = e->next;
}
return NULL;
}
@ -219,24 +258,33 @@ hashtable_destroy(struct hashtable *h, int free_values)
unsigned int i;
struct entry *e, *f;
struct entry **table = h->table;
if (free_values)
{
for (i = 0; i < h->tablelength; i++)
{
if(free_values) {
for(i = 0; i < h->tablelength; i++) {
e = table[i];
while (NULL != e)
{ f = e; e = e->next; freekey(f->k); free(f->v); free(f); }
while(NULL != e) {
f = e;
e = e->next;
freekey(f->k);
free(f->v);
free(f);
}
}
}
else
{
for (i = 0; i < h->tablelength; i++)
{
else {
for(i = 0; i < h->tablelength; i++) {
e = table[i];
while (NULL != e)
{ f = e; e = e->next; freekey(f->k); free(f); }
while(NULL != e) {
f = e;
e = e->next;
freekey(f->k);
free(f);
}
}
}
free(h->table);
free(h);
}
@ -244,23 +292,23 @@ hashtable_destroy(struct hashtable *h, int free_values)
/*
* Copyright (c) 2002, Christopher Clark
* All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
*
* * Neither the name of the original author; nor the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
*
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR

@ -19,7 +19,7 @@ struct hashtable;
* v = (struct some_value *) malloc(sizeof(struct some_value));
*
* (initialise k and v to suitable values)
*
*
* if (! hashtable_insert(h,k,v) )
* { exit(-1); }
*
@ -33,7 +33,7 @@ struct hashtable;
/* Macros may be used to define type-safe(r) hashtable access functions, with
* methods specialized to take known key and value types as parameters.
*
*
* Example:
*
* Insert this at the start of your file:
@ -63,7 +63,7 @@ struct hashtable;
/*****************************************************************************
* create_hashtable
* @name create_hashtable
* @param minsize minimum initial size of hashtable
* @param hashfunction function for hashing keys
@ -73,12 +73,12 @@ struct hashtable;
struct hashtable *
create_hashtable(uint32_t minsize,
unsigned int (*hashfunction) (void*),
int (*key_eq_fn) (void*,void*));
unsigned int (*hashfunction)(void *),
int (*key_eq_fn)(void *, void *));
/*****************************************************************************
* hashtable_insert
* @name hashtable_insert
* @param h the hashtable to insert into
* @param k the key - hashtable claims ownership and will free on removal
@ -95,7 +95,7 @@ create_hashtable(uint32_t minsize,
* If in doubt, remove before insert.
*/
int
int
hashtable_insert(struct hashtable *h, void *k, void *v);
#define DEFINE_HASHTABLE_INSERT(fnname, keytype, valuetype) \
@ -106,7 +106,7 @@ int fnname (struct hashtable *h, keytype *k, valuetype *v) \
/*****************************************************************************
* hashtable_search
* @name hashtable_search
* @param h the hashtable to search
* @param k the key to search for - does not claim ownership
@ -124,7 +124,7 @@ valuetype * fnname (struct hashtable *h, keytype *k) \
/*****************************************************************************
* hashtable_remove
* @name hashtable_remove
* @param h the hashtable to remove the item from
* @param k the key to search for - does not claim ownership
@ -143,7 +143,7 @@ valuetype * fnname (struct hashtable *h, keytype *k) \
/*****************************************************************************
* hashtable_count
* @name hashtable_count
* @param h the hashtable
* @return the number of items stored in the hashtable
@ -154,7 +154,7 @@ hashtable_count(struct hashtable *h);
/*****************************************************************************
* hashtable_destroy
* @name hashtable_destroy
* @param h the hashtable
* @param free_values whether to call 'free' on the remaining values
@ -168,23 +168,23 @@ hashtable_destroy(struct hashtable *h, int free_values);
/*
* Copyright (c) 2002, Christopher Clark
* All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
*
* * Neither the name of the original author; nor the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
*
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR

@ -6,8 +6,7 @@
#include "hashtable.h"
/*****************************************************************************/
struct entry
{
struct entry {
void *k, *v;
unsigned int h;
struct entry *next;
@ -19,8 +18,8 @@ struct hashtable {
unsigned int entrycount;
unsigned int loadlimit;
unsigned int primeindex;
unsigned int (*hashfn) (void *k);
int (*eqfn) (void *k1, void *k2);
unsigned int (*hashfn)(void *k);
int (*eqfn)(void *k1, void *k2);
};
/*****************************************************************************/
@ -30,7 +29,8 @@ hash(struct hashtable *h, void *k);
/*****************************************************************************/
/* indexFor */
static inline unsigned int
indexFor(unsigned int tablelength, unsigned int hashvalue) {
indexFor(unsigned int tablelength, unsigned int hashvalue)
{
return (hashvalue % tablelength);
};
@ -54,23 +54,23 @@ indexFor(unsigned int tablelength, unsigned int hashvalue)
/*
* Copyright (c) 2002, Christopher Clark
* All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
*
* * Neither the name of the original author; nor the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
*
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR

@ -1,3 +1,20 @@
/**
* Ringbuffer implementation
*
* Copyright (C) 2013 Freie Universität Berlin
* Copyright (C) 2013 INRIA
*
* This file subject to the terms and conditions of the GNU Lesser General
* Public License. See the file LICENSE in the top level directory for more
* details.
*
* @ingroup sys_lib
* @{
* @file ringbuffer.c
* @author Kaspar Schleiser <kaspar.schleiser@fu-berlin.de>
* @}
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
@ -13,7 +30,8 @@
//#define DEBUG(...) printf (__VA_ARGS__)
#define DEBUG(...)
void ringbuffer_init(ringbuffer_t *rb, char* buffer, unsigned int bufsize) {
void ringbuffer_init(ringbuffer_t *rb, char *buffer, unsigned int bufsize)
{
rb->buf = buffer;
rb->start = 0;
rb->end = 0;
@ -21,37 +39,53 @@ void ringbuffer_init(ringbuffer_t *rb, char* buffer, unsigned int bufsize) {
rb->avail = 0;
}
void rb_add_elements(ringbuffer_t* rb, char *buf, int n) {
for (int i = 0; i < n; i++) {
void rb_add_elements(ringbuffer_t *rb, char *buf, int n)
{
for(int i = 0; i < n; i++) {
rb_add_element(rb, buf[i]);
}
}
void rb_add_element(ringbuffer_t* rb, char c) {
if (rb->avail == rb->size) rb_get_element(rb);
void rb_add_element(ringbuffer_t *rb, char c)
{
if(rb->avail == rb->size) {
rb_get_element(rb);
}
rb->buf[rb->end++] = c;
if (rb->end >= rb->size) rb->end = 0;
if(rb->end >= rb->size) {
rb->end = 0;
}
rb->avail++;
}
int rb_get_element(ringbuffer_t *rb) {
if (rb->avail == 0) return -1;
int rb_get_element(ringbuffer_t *rb)
{
if(rb->avail == 0) {
return -1;
}
rb->avail--;
int c = (char)rb->buf[rb->start++];
if (rb->start >= rb->size) rb->start = 0;
if(rb->start >= rb->size) {
rb->start = 0;
}
return c;
}
int rb_get_elements(ringbuffer_t *rb, char* buf, int n) {
int rb_get_elements(ringbuffer_t *rb, char *buf, int n)
{
int count = 0;
while (rb->avail && (count < n)) {
while(rb->avail && (count < n)) {
buf[count++] = rb_get_element(rb);
}
return count;
}
@ -71,7 +105,7 @@ int main(int argc, char *argv[] ){
rb_add_element(&r, 8);
rb_add_element(&r, 9);
rb_add_element(&r, 10);
int c;
while ( r.avail ) {
c = rb_get_element(&r);
@ -103,7 +137,7 @@ int main(int argc, char *argv[] ){
rb_add_element(&r, 8);
rb_add_element(&r, 9);
rb_add_element(&r, 10);
while ( r.avail ) {
c = rb_get_element(&r);
if (c == -1) break;

@ -1,5 +1,22 @@
/**
* Ringbuffer header
*
* Copyright (C) 2013 Freie Universität Berlin
* Copyright (C) 2013 INRIA
*
* This file subject to the terms and conditions of the GNU Lesser General
* Public License. See the file LICENSE in the top level directory for more
* details.
*
* @ingroup sys_lib
* @{
* @file ringbuffer.h
* @author Kaspar Schleiser <kaspar.schleiser@fu-berlin.de>
* @}
*/
#ifndef __RINGBUFFER_H
#define __RINGBUFFER_H
#define __RINGBUFFER_H
typedef struct ringbuffer {
char *buf;
@ -9,7 +26,7 @@ typedef struct ringbuffer {
unsigned int avail;
} ringbuffer_t;
void ringbuffer_init(ringbuffer_t *rb, char* buffer, unsigned int bufsize);
void ringbuffer_init(ringbuffer_t *rb, char *buffer, unsigned int bufsize);
void rb_add_element(ringbuffer_t *rb, char c);
void rb_add_elements(ringbuffer_t *rb, char *buf, int n);
int rb_get_element(ringbuffer_t *rb);

@ -1,31 +1,15 @@
/******************************************************************************
Copyright 2009-2010, Freie Universitaet Berlin (FUB). All rights reserved.
These sources were developed at the Freie Universitaet Berlin, Computer Systems
and Telematics group (http://cst.mi.fu-berlin.de).
-------------------------------------------------------------------------------
This file is part of RIOT.
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
RIOT is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see http://www.gnu.org/licenses/ .
--------------------------------------------------------------------------------
For further information and questions please use the web site
http://scatterweb.mi.fu-berlin.de
and the mailinglist (subscription via web site)
scatterweb@lists.spline.inf.fu-berlin.de
*******************************************************************************/
/**
* Logging daemon
*
* Copyright (C) 2009-2013 Freie Universitaet Berlin
*
* This file subject to the terms and conditions of the GNU Lesser General
* Public License. See the file LICENSE in the top level directory for more
* details.
*/
/**
* @file
* @file logd.c
* @brief Simple logging demon implementation
*
* @author Freie Universität Berlin, Computer Systems & Telematics
@ -39,26 +23,25 @@ and the mailinglist (subscription via web site)
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
// core
/* core */
#include "msg.h"
#include "flags.h"
#include "mutex.h"
#include "thread.h"
#include "kernel.h"
// system
/* system */
#include "logd.h"
#include "list.h"
typedef struct log_queue_t
{
list_node_t listnode;
char* str;
int str_len;
typedef struct {
list_node_t listnode;
char *str;
int str_len;
} log_queue_t;
static volatile int log_pid = -1;
static int logd_stack_size = LOGD_STACK_SIZE_NORMAL;
static FILE* fh = NULL;
static FILE *fh = NULL;
static int log_count = 0;
static mutex_t log_mutex;
static list_t log_msg_queue;
@ -69,125 +52,156 @@ static volatile bool echo_on = false;
static void close_file_handle(void)
{
if (fh != NULL) {
fclose(fh);
fh = NULL;
}
if(fh != NULL) {
fclose(fh);
fh = NULL;
}
}
static void write_to_file(char* str, int str_len)
static void write_to_file(char *str, int str_len)
{
if (fh != NULL && str_len > 0) {
if (fwrite(str, sizeof(char), str_len, fh) != str_len) {
if (echo_on && logd_stack_size >= LOGD_STACK_SIZE_CONSOLE) {
printf("LOGD [WARN]: file write failed, closing file\n");
}
close_file_handle();
return;
}
if (fflush(fh) == EOF) {
if (echo_on && logd_stack_size >= LOGD_STACK_SIZE_CONSOLE) {
printf("LOGD [WARN]: file write failed, closing file\n");
}
close_file_handle();
return;
}
} else {
fh = fopen("/LOGD.LOG", "w");
if (!fh) {
if (echo_on && logd_stack_size >= LOGD_STACK_SIZE_CONSOLE) {
printf("LOGD [WARN]: file reopen failed, damn!\n");
}
} else {
write_to_file(str, str_len);
}
}
if(fh != NULL && str_len > 0) {
if(fwrite(str, sizeof(char), str_len, fh) != str_len) {
if(echo_on && logd_stack_size >= LOGD_STACK_SIZE_CONSOLE) {
printf("LOGD [WARN]: file write failed, closing file\n");
}
close_file_handle();
return;
}
if(fflush(fh) == EOF) {
if(echo_on && logd_stack_size >= LOGD_STACK_SIZE_CONSOLE) {
printf("LOGD [WARN]: file write failed, closing file\n");
}
close_file_handle();
return;
}
}
else {
fh = fopen("/LOGD.LOG", "w");
if(!fh) {
if(echo_on && logd_stack_size >= LOGD_STACK_SIZE_CONSOLE) {
printf("LOGD [WARN]: file reopen failed, damn!\n");
}
}
else {
write_to_file(str, str_len);
}
}
}
static void logd_process(void)
{
msg m;
log_queue_t* node;
do
{
if (!exit_flag) msg_receive(&m);
mutex_lock(&log_mutex);
while ((node = (log_queue_t*) list_remove_head(&log_msg_queue)) != NULL) {
write_to_file(node->str, node->str_len);
if (echo_on && logd_stack_size >= LOGD_STACK_SIZE_CONSOLE) {
printf("%s", node->str);
}
log_count++;
free(node->str);
free(node);
}
mutex_unlock(&log_mutex, 0);
} while (m.type != MSG_EXIT && !exit_flag);
/* Logging thread is terminating, close log file */
close_file_handle();
msg m;
log_queue_t *node;
do {
if(!exit_flag) {
msg_receive(&m);
}
mutex_lock(&log_mutex);
while((node = (log_queue_t *) list_remove_head(&log_msg_queue)) != NULL) {
write_to_file(node->str, node->str_len);
if(echo_on && logd_stack_size >= LOGD_STACK_SIZE_CONSOLE) {
printf("%s", node->str);
}
log_count++;
free(node->str);
free(node);
}
mutex_unlock(&log_mutex, 0);
}
while(m.type != MSG_EXIT && !exit_flag);
/* Logging thread is terminating, close log file */
close_file_handle();
}
/*---------------------------------------------------------------------------*/
static void logd_init0(void) {
fh = fopen("/LOGD.LOG", "w");
if (!fh) return;
log_pid = thread_create(logd_stack_size, PRIORITY_LOGD, CREATE_STACKTEST, logd_process, "logd");
static void logd_init0(void)
{
fh = fopen("/LOGD.LOG", "w");
if(!fh) {
return;
}
log_pid = thread_create(logd_stack_size, PRIORITY_LOGD, CREATE_STACKTEST, logd_process, "logd");
}
void logd_init(int stack_size)
{
logd_stack_size = stack_size;
mutex_init(&log_mutex);
list_init(&log_msg_queue);
logd_init0();
logd_stack_size = stack_size;
mutex_init(&log_mutex);
list_init(&log_msg_queue);
logd_init0();
}
void logd_set_console_enabled(bool enabled)
{
echo_on = enabled;
echo_on = enabled;
}
bool logd_log(char* str, int str_len)
bool logd_log(char *str, int str_len)
{
msg m;
// Test if logd process was created
if (log_pid == -1) {
// no logd created, because fopen() on log file failed. So try again
logd_init0();
if (log_pid == -1) {
// Still errors opening log file, exit now
return false;
}
}
log_queue_t* lq = malloc(sizeof(*lq));
if (lq == NULL) return false;
lq->str = malloc(sizeof(char) * str_len + 1); // 1 byte for string termination char
if (lq->str == NULL) {
free(lq);
return false;
}
strncpy(lq->str, str, str_len);
lq->str_len = str_len;
lq->str[str_len] = '\0'; // add string termination char at end of buffer
mutex_lock(&log_mutex);
list_append(&log_msg_queue, (list_node_t*) lq);
mutex_unlock(&log_mutex, 0);
m.type = MSG_POLL;
m.content.ptr = NULL;
msg_send(&m, log_pid, false);
return true;
msg m;
/* Test if logd process was created */
if(log_pid == -1) {
/* no logd created, because fopen() on log file failed. So try again */
logd_init0();
if(log_pid == -1) {
/* Still errors opening log file, exit now */
return false;
}
}
log_queue_t *lq = malloc(sizeof(*lq));
if(lq == NULL) {
return false;
}
lq->str = malloc(sizeof(char) * str_len + 1); /* 1 byte for string termination char */
if(lq->str == NULL) {
free(lq);
return false;
}
strncpy(lq->str, str, str_len);
lq->str_len = str_len;
lq->str[str_len] = '\0'; /* add string termination char at end of buffer */
mutex_lock(&log_mutex);
list_append(&log_msg_queue, (list_node_t *) lq);
mutex_unlock(&log_mutex, 0);
m.type = MSG_POLL;
m.content.ptr = NULL;
msg_send(&m, log_pid, false);
return true;
}
void logd_exit(void)
{
msg m;
// Test if logd process was created
if (log_pid == -1) {
return;
}
exit_flag = true;
m.type = MSG_EXIT;
m.content.ptr = NULL;
msg_send(&m, log_pid, false);
msg m;
/* Test if logd process was created */
if(log_pid == -1) {
return;
}
exit_flag = true;
m.type = MSG_EXIT;
m.content.ptr = NULL;
msg_send(&m, log_pid, false);
}

@ -1,8 +1,18 @@
/*
* destiny.c
/**
* Destiny transpor layer implementation
*
* Created on: 03.09.2011
* Author: Oliver
* Copyright (C) 2013 INRIA.
*
* This file subject to the terms and conditions of the GNU Lesser General
* Public License. See the file LICENSE in the top level directory for more
* details.
*
* @ingroup destiny
* @{
* @file destiny.c
* @brief transpor layer functions
* @author Oliver Gesch <oliver.gesch@googlemail.com>
* @}
*/
#include <thread.h>
@ -17,28 +27,34 @@
#include "destiny.h"
void init_transport_layer(void)
{
printf("Initializing transport layer packages. Size of socket_type: %u\n", sizeof(socket_internal_t));
// SOCKETS
memset(sockets, 0, MAX_SOCKETS*sizeof(socket_internal_t));
{
printf("Initializing transport layer packages. Size of socket_type: %u\n",
sizeof(socket_internal_t));
/* SOCKETS */
memset(sockets, 0, MAX_SOCKETS * sizeof(socket_internal_t));
// UDP
int udp_thread_pid = thread_create(udp_stack_buffer, UDP_STACK_SIZE, PRIORITY_MAIN, CREATE_STACKTEST, udp_packet_handler, "udp_packet_handler");
set_udp_packet_handler_pid(udp_thread_pid);
/* UDP */
int udp_thread_pid = thread_create(udp_stack_buffer, UDP_STACK_SIZE,
PRIORITY_MAIN, CREATE_STACKTEST,
udp_packet_handler,"udp_packet_handler");
set_udp_packet_handler_pid(udp_thread_pid);
// TCP
timex_t now;
vtimer_now(&now);
srand(now.microseconds);
/* TCP */
timex_t now;
vtimer_now(&now);
srand(now.microseconds);
#ifdef TCP_HC
printf("TCP_HC enabled!\n");
global_context_counter = rand();
printf("TCP_HC enabled!\n");
global_context_counter = rand();
#endif
global_sequence_counter = rand();
global_sequence_counter = rand();
int tcp_thread_pid = thread_create(tcp_stack_buffer, TCP_STACK_SIZE, PRIORITY_MAIN, CREATE_STACKTEST, tcp_packet_handler, "tcp_packet_handler");
set_tcp_packet_handler_pid(tcp_thread_pid);
int tcp_thread_pid = thread_create(tcp_stack_buffer, TCP_STACK_SIZE,
PRIORITY_MAIN, CREATE_STACKTEST,
tcp_packet_handler, "tcp_packet_handler");
set_tcp_packet_handler_pid(tcp_thread_pid);
thread_create(tcp_timer_stack, TCP_TIMER_STACKSIZE, PRIORITY_MAIN+1, CREATE_STACKTEST, tcp_general_timer, "tcp_general_timer");
thread_create(tcp_timer_stack, TCP_TIMER_STACKSIZE, PRIORITY_MAIN + 1,
CREATE_STACKTEST, tcp_general_timer, "tcp_general_timer");
}
}

File diff suppressed because it is too large Load Diff

@ -1,10 +1,21 @@
/*
* socket.h
/**
* Destiny socket API
*
* Copyright (C) 2013 INRIA.
*
* Created on: 16.09.2011
* Author: Oliver
* This file subject to the terms and conditions of the GNU Lesser General
* Public License. See the file LICENSE in the top level directory for more
* details.
*
* @ingroup destiny
* @{
* @file socket.h
* @brief header for BSD socket API
* @author Oliver Gesch <oliver.gesch@googlemail.com>
* @}
*/
#ifndef SOCKET_H_
#define SOCKET_H_
@ -119,82 +130,81 @@
#define SEND_MSG_BUF_SIZE 64
typedef struct socka6
{
typedef struct socka6 {
uint8_t sin6_family; /* AF_INET6 */
uint16_t sin6_port; /* transport layer port # */
uint32_t sin6_flowinfo; /* IPv6 flow information */
ipv6_addr_t sin6_addr; /* IPv6 address */
} sockaddr6_t;
typedef struct tcp_hc_con
{
uint16_t context_id;
uint32_t seq_rcv; // Last received packet values
uint32_t ack_rcv;
uint16_t wnd_rcv;
uint32_t seq_snd; // Last sent packet values
uint32_t ack_snd;
uint16_t wnd_snd;
uint8_t hc_type;
} tcp_hc_context_t;
typedef struct tcp_control_block
{
uint32_t send_una;
uint32_t send_nxt;
uint16_t send_wnd;
uint32_t send_iss;
uint32_t rcv_nxt;
uint16_t rcv_wnd;
uint32_t rcv_irs;
timex_t last_packet_time;
uint8_t no_of_retries;
uint16_t mss;
uint8_t state;
double srtt;
double rttvar;
double rto;
} sockaddr6_t;
typedef struct tcp_hc_con {
uint16_t context_id;
uint32_t seq_rcv; // Last received packet values
uint32_t ack_rcv;
uint16_t wnd_rcv;
uint32_t seq_snd; // Last sent packet values
uint32_t ack_snd;
uint16_t wnd_snd;
uint8_t hc_type;
} tcp_hc_context_t;
typedef struct tcp_control_block {
uint32_t send_una;
uint32_t send_nxt;
uint16_t send_wnd;
uint32_t send_iss;
uint32_t rcv_nxt;
uint16_t rcv_wnd;
uint32_t rcv_irs;
timex_t last_packet_time;
uint8_t no_of_retries;
uint16_t mss;
uint8_t state;
double srtt;
double rttvar;
double rto;
#ifdef TCP_HC
tcp_hc_context_t tcp_context;
tcp_hc_context_t tcp_context;
#endif
} tcp_cb_t;
typedef struct sock_t
{
uint8_t domain;
uint8_t type;
uint8_t protocol;
tcp_cb_t tcp_control;
sockaddr6_t local_address;
sockaddr6_t foreign_address;
} socket_t;
typedef struct socket_in_t
{
uint8_t socket_id;
uint8_t recv_pid;
uint8_t send_pid;
uint8_t tcp_input_buffer_end;
mutex_t tcp_buffer_mutex;
socket_t socket_values;
uint8_t tcp_input_buffer[MAX_TCP_BUFFER];
} socket_internal_t;
} tcp_cb_t;
typedef struct sock_t {
uint8_t domain;
uint8_t type;
uint8_t protocol;
tcp_cb_t tcp_control;
sockaddr6_t local_address;
sockaddr6_t foreign_address;
} socket_t;
typedef struct socket_in_t {
uint8_t socket_id;
uint8_t recv_pid;
uint8_t send_pid;
uint8_t tcp_input_buffer_end;
mutex_t tcp_buffer_mutex;
socket_t socket_values;
uint8_t tcp_input_buffer[MAX_TCP_BUFFER];
} socket_internal_t;
extern socket_internal_t sockets[MAX_SOCKETS];
int socket(int domain, int type, int protocol);
int connect(int socket, sockaddr6_t *addr, uint32_t addrlen);
socket_internal_t *getWaitingConnectionSocket(int socket, ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header);
socket_internal_t *getWaitingConnectionSocket(int socket,
ipv6_hdr_t *ipv6_header,
tcp_hdr_t *tcp_header);
void close_socket(socket_internal_t *current_socket);
int32_t recvfrom( int s, void *buf, uint32_t len, int flags, sockaddr6_t *from, uint32_t *fromlen );
int32_t sendto( int s, const void *msg, uint32_t len, int flags, sockaddr6_t *to, uint32_t tolen);
int32_t recvfrom(int s, void *buf, uint32_t len, int flags, sockaddr6_t *from,
uint32_t *fromlen);
int32_t sendto(int s, const void *msg, uint32_t len, int flags,
sockaddr6_t *to, uint32_t tolen);
int32_t send(int s, void *msg, uint32_t len, int flags);
int recv(int s, void *buf, uint32_t len, int flags);
int close(int s);
@ -210,14 +220,23 @@ void print_internal_socket(socket_internal_t *current_socket_internal);
void print_socket(socket_t *current_socket);
void printf_tcp_context(tcp_hc_context_t *current_tcp_context);
bool exists_socket(uint8_t socket);
socket_internal_t *new_tcp_queued_socket(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header);
void print_tcp_status(int in_or_out, ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, socket_t *tcp_socket);
void set_socket_address(sockaddr6_t *sockaddr, uint8_t sin6_family, uint16_t sin6_port, uint32_t sin6_flowinfo, ipv6_addr_t *sin6_addr);
void set_tcp_cb(tcp_cb_t *tcp_control, uint32_t rcv_nxt, uint16_t rcv_wnd, uint32_t send_nxt, uint32_t send_una, uint16_t send_wnd);
void set_tcp_packet(tcp_hdr_t *tcp_hdr, uint16_t src_port, uint16_t dst_port, uint32_t seq_nr, uint32_t ack_nr,
uint8_t dataOffset_reserved, uint8_t reserved_flags, uint16_t window, uint16_t checksum, uint16_t urg_pointer);
socket_internal_t *new_tcp_queued_socket(ipv6_hdr_t *ipv6_header,
tcp_hdr_t *tcp_header);
void print_tcp_status(int in_or_out, ipv6_hdr_t *ipv6_header,
tcp_hdr_t *tcp_header, socket_t *tcp_socket);
void set_socket_address(sockaddr6_t *sockaddr, uint8_t sin6_family,
uint16_t sin6_port, uint32_t sin6_flowinfo,
ipv6_addr_t *sin6_addr);
void set_tcp_cb(tcp_cb_t *tcp_control, uint32_t rcv_nxt, uint16_t rcv_wnd,
uint32_t send_nxt, uint32_t send_una, uint16_t send_wnd);
void set_tcp_packet(tcp_hdr_t *tcp_hdr, uint16_t src_port, uint16_t dst_port,
uint32_t seq_nr, uint32_t ack_nr,
uint8_t dataOffset_reserved, uint8_t reserved_flags,
uint16_t window, uint16_t checksum, uint16_t urg_pointer);
int check_tcp_consistency(socket_t *current_tcp_socket, tcp_hdr_t *tcp_header);
void switch_tcp_packet_byte_order(tcp_hdr_t *current_tcp_packet);
int send_tcp(socket_internal_t *current_socket, tcp_hdr_t *current_tcp_packet, ipv6_hdr_t *temp_ipv6_header, uint8_t flags, uint8_t payload_length);
int send_tcp(socket_internal_t *current_socket, tcp_hdr_t *current_tcp_packet,
ipv6_hdr_t *temp_ipv6_header, uint8_t flags,
uint8_t payload_length);
bool isTCPSocket(uint8_t s);
#endif /* SOCKET_H_ */

@ -1,10 +1,21 @@
/*
* tcp.c
/**
* Destiny TCP implementation
*
* Created on: 29.09.2011
* Author: Oliver
* Copyright (C) 2013 INRIA.
*
* This file subject to the terms and conditions of the GNU Lesser General
* Public License. See the file LICENSE in the top level directory for more
* details.
*
* @