moved all third-party lib to separate folder

This commit is contained in:
2021-02-27 19:32:58 +01:00
parent b5650033d8
commit 6518a7f244
138 changed files with 22 additions and 2 deletions

View File

@ -0,0 +1,3 @@
add_subdirectory(posix)

View File

@ -0,0 +1,33 @@
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include <csp/arch/csp_malloc.h>
/* FreeRTOS includes */
#include <FreeRTOS.h>
void * csp_malloc(size_t size) {
return pvPortMalloc(size);
}
void csp_free(void *ptr) {
vPortFree(ptr);
}

View File

@ -0,0 +1,66 @@
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
/* FreeRTOS includes */
#include <FreeRTOS.h>
#include <queue.h>
/* CSP includes */
#include <csp/csp.h>
#include <csp/arch/csp_queue.h>
csp_queue_handle_t csp_queue_create(int length, size_t item_size) {
return xQueueCreate(length, item_size);
}
void csp_queue_remove(csp_queue_handle_t queue) {
vQueueDelete(queue);
}
int csp_queue_enqueue(csp_queue_handle_t handle, void * value, uint32_t timeout) {
if (timeout != CSP_MAX_DELAY)
timeout = timeout / portTICK_RATE_MS;
return xQueueSendToBack(handle, value, timeout);
}
int csp_queue_enqueue_isr(csp_queue_handle_t handle, void * value, CSP_BASE_TYPE * task_woken) {
return xQueueSendToBackFromISR(handle, value, task_woken);
}
int csp_queue_dequeue(csp_queue_handle_t handle, void * buf, uint32_t timeout) {
if (timeout != CSP_MAX_DELAY)
timeout = timeout / portTICK_RATE_MS;
return xQueueReceive(handle, buf, timeout);
}
int csp_queue_dequeue_isr(csp_queue_handle_t handle, void * buf, CSP_BASE_TYPE * task_woken) {
return xQueueReceiveFromISR(handle, buf, task_woken);
}
int csp_queue_size(csp_queue_handle_t handle) {
return uxQueueMessagesWaiting(handle);
}
int csp_queue_size_isr(csp_queue_handle_t handle) {
return uxQueueMessagesWaitingFromISR(handle);
}

View File

@ -0,0 +1,96 @@
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdint.h>
/* FreeRTOS includes */
#include <FreeRTOS.h>
#include <semphr.h>
/* CSP includes */
#include <csp/csp.h>
#include <csp/arch/csp_malloc.h>
#include <csp/arch/csp_semaphore.h>
#include <csp/arch/csp_queue.h>
int csp_mutex_create(csp_mutex_t * mutex) {
*mutex = xSemaphoreCreateMutex();
if (*mutex) {
return CSP_SEMAPHORE_OK;
} else {
return CSP_SEMAPHORE_ERROR;
}
}
int csp_mutex_remove(csp_mutex_t * mutex) {
return csp_bin_sem_remove(mutex);
}
int csp_mutex_lock(csp_mutex_t * mutex, uint32_t timeout) {
return csp_bin_sem_wait(mutex, timeout);
}
int csp_mutex_unlock(csp_mutex_t * mutex) {
return csp_bin_sem_post(mutex);
}
int csp_bin_sem_create(csp_bin_sem_handle_t * sem) {
vSemaphoreCreateBinary(*sem);
return CSP_SEMAPHORE_OK;
}
int csp_bin_sem_remove(csp_bin_sem_handle_t * sem) {
if ((sem != NULL) && (*sem != NULL)) {
csp_queue_remove(*sem);
}
return CSP_SEMAPHORE_OK;
}
int csp_bin_sem_wait(csp_bin_sem_handle_t * sem, uint32_t timeout) {
csp_log_lock("Wait: %p", sem);
if (timeout != CSP_MAX_DELAY)
timeout = timeout / portTICK_RATE_MS;
if (xSemaphoreTake(*sem, timeout) == pdPASS) {
return CSP_SEMAPHORE_OK;
} else {
return CSP_SEMAPHORE_ERROR;
}
}
int csp_bin_sem_post(csp_bin_sem_handle_t * sem) {
csp_log_lock("Post: %p", sem);
if (xSemaphoreGive(*sem) == pdPASS) {
return CSP_SEMAPHORE_OK;
} else {
return CSP_SEMAPHORE_ERROR;
}
}
int csp_bin_sem_post_isr(csp_bin_sem_handle_t * sem, CSP_BASE_TYPE * task_woken) {
csp_log_lock("Post: %p", sem);
if (xSemaphoreGiveFromISR(*sem, task_woken) == pdPASS) {
return CSP_SEMAPHORE_OK;
} else {
return CSP_SEMAPHORE_ERROR;
}
}

View File

@ -0,0 +1,139 @@
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdint.h>
#include <FreeRTOS.h>
#include <task.h>
#include <csp/csp.h>
#include <csp/csp_error.h>
#include <csp/arch/csp_system.h>
int csp_sys_tasklist(char * out) {
#if (tskKERNEL_VERSION_MAJOR < 8)
vTaskList((signed portCHAR *) out);
#else
vTaskList(out);
#endif
return CSP_ERR_NONE;
}
int csp_sys_tasklist_size(void) {
return 40 * uxTaskGetNumberOfTasks();
}
uint32_t csp_sys_memfree(void) {
uint32_t total = 0, max = UINT32_MAX, size;
void * pmem;
/* If size_t is less than 32 bits, start with 10 KiB */
size = sizeof(uint32_t) > sizeof(size_t) ? 10000 : 1000000;
while (1) {
pmem = pvPortMalloc(size + total);
if (pmem == NULL) {
max = size + total;
size = size / 2;
} else {
total += size;
if (total + size >= max)
size = size / 2;
vPortFree(pmem);
}
if (size < 32) break;
}
return total;
}
int csp_sys_reboot(void) {
extern void __attribute__((weak)) cpu_set_reset_cause(unsigned int);
if (cpu_set_reset_cause)
cpu_set_reset_cause(1);
extern void __attribute__((weak)) cpu_reset(void);
if (cpu_reset) {
cpu_reset();
while (1);
}
csp_log_error("Failed to reboot");
return CSP_ERR_INVAL;
}
int csp_sys_shutdown(void) {
extern void __attribute__((weak)) cpu_shutdown(void);
if (cpu_shutdown) {
cpu_shutdown();
while (1);
}
csp_log_error("Failed to shutdown");
return CSP_ERR_INVAL;
}
void csp_sys_set_color(csp_color_t color) {
unsigned int color_code, modifier_code;
switch (color & COLOR_MASK_COLOR) {
case COLOR_BLACK:
color_code = 30; break;
case COLOR_RED:
color_code = 31; break;
case COLOR_GREEN:
color_code = 32; break;
case COLOR_YELLOW:
color_code = 33; break;
case COLOR_BLUE:
color_code = 34; break;
case COLOR_MAGENTA:
color_code = 35; break;
case COLOR_CYAN:
color_code = 36; break;
case COLOR_WHITE:
color_code = 37; break;
case COLOR_RESET:
default:
color_code = 0; break;
}
switch (color & COLOR_MASK_MODIFIER) {
case COLOR_BOLD:
modifier_code = 1; break;
case COLOR_UNDERLINE:
modifier_code = 2; break;
case COLOR_BLINK:
modifier_code = 3; break;
case COLOR_HIDE:
modifier_code = 4; break;
case COLOR_NORMAL:
default:
modifier_code = 0; break;
}
printf("\033[%u;%um", modifier_code, color_code);
}

View File

@ -0,0 +1,38 @@
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <FreeRTOS.h>
#include <task.h>
/* CSP includes */
#include <csp/csp.h>
#include <csp/arch/csp_thread.h>
int csp_thread_create(csp_thread_return_t (* routine)(void *), const char * const thread_name, unsigned short stack_depth, void * parameters, unsigned int priority, csp_thread_handle_t * handle) {
#if (tskKERNEL_VERSION_MAJOR >= 8)
portBASE_TYPE ret = xTaskCreate(routine, thread_name, stack_depth, parameters, priority, handle);
#else
portBASE_TYPE ret = xTaskCreate(routine, (signed char *) thread_name, stack_depth, parameters, priority, handle);
#endif
if (ret != pdTRUE)
return CSP_ERR_NOMEM;
return CSP_ERR_NONE;
}

View File

@ -0,0 +1,46 @@
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
/* FreeRTOS includes */
#include <FreeRTOS.h>
#include <task.h>
/* CSP includes */
#include <csp/csp.h>
#include <csp/arch/csp_time.h>
uint32_t csp_get_ms(void) {
return (uint32_t)(xTaskGetTickCount() * (1000/configTICK_RATE_HZ));
}
uint32_t csp_get_ms_isr(void) {
return (uint32_t)(xTaskGetTickCountFromISR() * (1000/configTICK_RATE_HZ));
}
uint32_t csp_get_s(void) {
return (uint32_t)(xTaskGetTickCount()/configTICK_RATE_HZ);
}
uint32_t csp_get_s_isr(void) {
return (uint32_t)(xTaskGetTickCountFromISR()/configTICK_RATE_HZ);
}

View File

@ -0,0 +1,31 @@
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include <stdlib.h>
void * csp_malloc(size_t size) {
return malloc(size);
}
void csp_free(void *ptr) {
free(ptr);
}

View File

@ -0,0 +1,64 @@
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include <pthread.h>
/* CSP includes */
#include <csp/csp.h>
#include <csp/arch/posix/pthread_queue.h>
#include <csp/arch/csp_queue.h>
csp_queue_handle_t csp_queue_create(int length, size_t item_size) {
return pthread_queue_create(length, item_size);
}
void csp_queue_remove(csp_queue_handle_t queue) {
return pthread_queue_delete(queue);
}
int csp_queue_enqueue(csp_queue_handle_t handle, void *value, uint32_t timeout) {
return pthread_queue_enqueue(handle, value, timeout);
}
int csp_queue_enqueue_isr(csp_queue_handle_t handle, void * value, CSP_BASE_TYPE * task_woken) {
if (task_woken != NULL)
*task_woken = 0;
return csp_queue_enqueue(handle, value, 0);
}
int csp_queue_dequeue(csp_queue_handle_t handle, void *buf, uint32_t timeout) {
return pthread_queue_dequeue(handle, buf, timeout);
}
int csp_queue_dequeue_isr(csp_queue_handle_t handle, void *buf, CSP_BASE_TYPE * task_woken) {
*task_woken = 0;
return csp_queue_dequeue(handle, buf, 0);
}
int csp_queue_size(csp_queue_handle_t handle) {
return pthread_queue_items(handle);
}
int csp_queue_size_isr(csp_queue_handle_t handle) {
return pthread_queue_items(handle);
}

View File

@ -0,0 +1,105 @@
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <pthread.h>
#include <sys/time.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>
/* CSP includes */
#include <csp/csp.h>
#include <csp/arch/csp_semaphore.h>
int csp_mutex_create(csp_mutex_t * mutex) {
csp_log_lock("Mutex init: %p", mutex);
*mutex = pthread_queue_create(1, sizeof(int));
if (mutex) {
int dummy = 0;
pthread_queue_enqueue(*mutex, &dummy, 0);
return CSP_SEMAPHORE_OK;
} else {
return CSP_SEMAPHORE_ERROR;
}
}
int csp_mutex_remove(csp_mutex_t * mutex) {
pthread_queue_delete(*mutex);
return CSP_SEMAPHORE_OK;
}
int csp_mutex_lock(csp_mutex_t * mutex, uint32_t timeout) {
int ret;
csp_log_lock("Wait: %p timeout %"PRIu32, mutex, timeout);
if (timeout == CSP_INFINITY) {
/* TODO: fix this to be infinite */
int dummy = 0;
if (pthread_queue_dequeue(*mutex, &dummy, timeout) == PTHREAD_QUEUE_OK)
ret = CSP_MUTEX_OK;
else
ret = CSP_MUTEX_ERROR;
} else {
int dummy = 0;
if (pthread_queue_dequeue(*mutex, &dummy, timeout) == PTHREAD_QUEUE_OK)
ret = CSP_MUTEX_OK;
else
ret = CSP_MUTEX_ERROR;
}
return ret == CSP_MUTEX_ERROR ? CSP_SEMAPHORE_ERROR : CSP_SEMAPHORE_OK;
}
int csp_mutex_unlock(csp_mutex_t * mutex) {
int dummy = 0;
if (pthread_queue_enqueue(*mutex, &dummy, 0) == PTHREAD_QUEUE_OK) {
return CSP_SEMAPHORE_OK;
} else {
return CSP_SEMAPHORE_ERROR;
}
}
int csp_bin_sem_create(csp_bin_sem_handle_t * sem) {
return csp_mutex_create(sem);
}
int csp_bin_sem_remove(csp_bin_sem_handle_t * sem) {
return csp_mutex_remove(sem);
}
int csp_bin_sem_wait(csp_bin_sem_handle_t * sem, uint32_t timeout) {
return csp_mutex_lock(sem, timeout);
}
int csp_bin_sem_post(csp_bin_sem_handle_t * sem) {
return csp_mutex_unlock(sem);
}
int csp_bin_sem_post_isr(csp_bin_sem_handle_t * sem, CSP_BASE_TYPE * task_woken) {
return csp_mutex_unlock(sem);
}

View File

@ -0,0 +1,99 @@
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <csp/csp.h>
#include <csp/csp_error.h>
#include <csp/arch/csp_system.h>
int csp_sys_tasklist(char * out) {
strcpy(out, "Tasklist not available on OSX");
return CSP_ERR_NONE;
}
int csp_sys_tasklist_size(void) {
return 100;
}
uint32_t csp_sys_memfree(void) {
/* TODO: Fix memory free on OSX */
uint32_t total = 0;
return total;
}
int csp_sys_reboot(void) {
/* TODO: Fix reboot on OSX */
csp_log_error("Failed to reboot");
return CSP_ERR_INVAL;
}
int csp_sys_shutdown(void) {
/* TODO: Fix shutdown on OSX */
csp_log_error("Failed to shutdown");
return CSP_ERR_INVAL;
}
void csp_sys_set_color(csp_color_t color) {
unsigned int color_code, modifier_code;
switch (color & COLOR_MASK_COLOR) {
case COLOR_BLACK:
color_code = 30; break;
case COLOR_RED:
color_code = 31; break;
case COLOR_GREEN:
color_code = 32; break;
case COLOR_YELLOW:
color_code = 33; break;
case COLOR_BLUE:
color_code = 34; break;
case COLOR_MAGENTA:
color_code = 35; break;
case COLOR_CYAN:
color_code = 36; break;
case COLOR_WHITE:
color_code = 37; break;
case COLOR_RESET:
default:
color_code = 0; break;
}
switch (color & COLOR_MASK_MODIFIER) {
case COLOR_BOLD:
modifier_code = 1; break;
case COLOR_UNDERLINE:
modifier_code = 2; break;
case COLOR_BLINK:
modifier_code = 3; break;
case COLOR_HIDE:
modifier_code = 4; break;
case COLOR_NORMAL:
default:
modifier_code = 0; break;
}
printf("\033[%u;%um", modifier_code, color_code);
}

View File

@ -0,0 +1,31 @@
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include <pthread.h>
/* CSP includes */
#include <csp/csp.h>
#include <csp/arch/csp_thread.h>
int csp_thread_create(csp_thread_return_t (* routine)(void *), const char * const thread_name, unsigned short stack_depth, void * parameters, unsigned int priority, csp_thread_handle_t * handle) {
return pthread_create(handle, NULL, routine, parameters);
}

View File

@ -0,0 +1,65 @@
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <time.h>
#include <sys/time.h>
#include <mach/clock.h>
#include <mach/mach.h>
/* CSP includes */
#include <csp/csp.h>
#include <csp/arch/csp_time.h>
uint32_t csp_get_ms(void) {
struct timespec ts;
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
ts.tv_sec = mts.tv_sec;
ts.tv_nsec = mts.tv_nsec;
return (uint32_t)(ts.tv_sec*1000+ts.tv_nsec/1000000);
}
uint32_t csp_get_ms_isr(void) {
return csp_get_ms();
}
uint32_t csp_get_s(void) {
struct timespec ts;
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
ts.tv_sec = mts.tv_sec;
ts.tv_nsec = mts.tv_nsec;
return (uint32_t)ts.tv_sec;
}
uint32_t csp_get_s_isr(void) {
return csp_get_s();
}

View File

@ -0,0 +1,179 @@
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
Inspired by c-pthread-queue by Matthew Dickinson
http://code.google.com/p/c-pthread-queue/
*/
#include <pthread.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <mach/clock.h>
#include <mach/mach.h>
/* CSP includes */
#include <csp/arch/posix/pthread_queue.h>
pthread_queue_t * pthread_queue_create(int length, size_t item_size) {
pthread_queue_t * q = malloc(sizeof(pthread_queue_t));
if (q != NULL) {
q->buffer = malloc(length*item_size);
if (q->buffer != NULL) {
q->size = length;
q->item_size = item_size;
q->items = 0;
q->in = 0;
q->out = 0;
if (pthread_mutex_init(&(q->mutex), NULL) || pthread_cond_init(&(q->cond_full), NULL) || pthread_cond_init(&(q->cond_empty), NULL)) {
free(q->buffer);
free(q);
q = NULL;
}
} else {
free(q);
q = NULL;
}
}
return q;
}
void pthread_queue_delete(pthread_queue_t * q) {
if (q == NULL)
return;
free(q->buffer);
free(q);
return;
}
int pthread_queue_enqueue(pthread_queue_t * queue, void * value, uint32_t timeout) {
int ret;
/* Calculate timeout */
struct timespec ts;
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
ts.tv_sec = mts.tv_sec;
ts.tv_nsec = mts.tv_nsec;
uint32_t sec = timeout / 1000;
uint32_t nsec = (timeout - 1000 * sec) * 1000000;
ts.tv_sec += sec;
if (ts.tv_nsec + nsec > 1000000000)
ts.tv_sec++;
ts.tv_nsec = (ts.tv_nsec + nsec) % 1000000000;
/* Get queue lock */
pthread_mutex_lock(&(queue->mutex));
while (queue->items == queue->size) {
ret = pthread_cond_timedwait(&(queue->cond_full), &(queue->mutex), &ts);
if (ret != 0) {
pthread_mutex_unlock(&(queue->mutex));
return PTHREAD_QUEUE_FULL;
}
}
/* Coby object from input buffer */
memcpy(queue->buffer+(queue->in * queue->item_size), value, queue->item_size);
queue->items++;
queue->in = (queue->in + 1) % queue->size;
pthread_mutex_unlock(&(queue->mutex));
/* Nofify blocked threads */
pthread_cond_broadcast(&(queue->cond_empty));
return PTHREAD_QUEUE_OK;
}
int pthread_queue_dequeue(pthread_queue_t * queue, void * buf, uint32_t timeout) {
int ret;
/* Calculate timeout */
struct timespec ts;
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
ts.tv_sec = mts.tv_sec;
ts.tv_nsec = mts.tv_nsec;
uint32_t sec = timeout / 1000;
uint32_t nsec = (timeout - 1000 * sec) * 1000000;
ts.tv_sec += sec;
if (ts.tv_nsec + nsec > 1000000000)
ts.tv_sec++;
ts.tv_nsec = (ts.tv_nsec + nsec) % 1000000000;
/* Get queue lock */
pthread_mutex_lock(&(queue->mutex));
while (queue->items == 0) {
ret = pthread_cond_timedwait(&(queue->cond_empty), &(queue->mutex), &ts);
if (ret != 0) {
pthread_mutex_unlock(&(queue->mutex));
return PTHREAD_QUEUE_EMPTY;
}
}
/* Coby object to output buffer */
memcpy(buf, queue->buffer+(queue->out * queue->item_size), queue->item_size);
queue->items--;
queue->out = (queue->out + 1) % queue->size;
pthread_mutex_unlock(&(queue->mutex));
/* Nofify blocked threads */
pthread_cond_broadcast(&(queue->cond_full));
return PTHREAD_QUEUE_OK;
}
int pthread_queue_items(pthread_queue_t * queue) {
pthread_mutex_lock(&(queue->mutex));
int items = queue->items;
pthread_mutex_unlock(&(queue->mutex));
return items;
}

View File

@ -0,0 +1,9 @@
target_sources(${LIB_CSP_NAME} PRIVATE
csp_malloc.c
csp_queue.c
csp_semaphore.c
csp_system.c
csp_thread.c
csp_time.c
pthread_queue.c
)

View File

@ -0,0 +1,31 @@
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include <stdlib.h>
void * csp_malloc(size_t size) {
return malloc(size);
}
void csp_free(void *ptr) {
free(ptr);
}

View File

@ -0,0 +1,64 @@
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include <pthread.h>
/* CSP includes */
#include <csp/csp.h>
#include <csp/arch/posix/pthread_queue.h>
#include <csp/arch/csp_queue.h>
csp_queue_handle_t csp_queue_create(int length, size_t item_size) {
return pthread_queue_create(length, item_size);
}
void csp_queue_remove(csp_queue_handle_t queue) {
return pthread_queue_delete(queue);
}
int csp_queue_enqueue(csp_queue_handle_t handle, void *value, uint32_t timeout) {
return pthread_queue_enqueue(handle, value, timeout);
}
int csp_queue_enqueue_isr(csp_queue_handle_t handle, void * value, CSP_BASE_TYPE * task_woken) {
if (task_woken != NULL)
*task_woken = 0;
return csp_queue_enqueue(handle, value, 0);
}
int csp_queue_dequeue(csp_queue_handle_t handle, void *buf, uint32_t timeout) {
return pthread_queue_dequeue(handle, buf, timeout);
}
int csp_queue_dequeue_isr(csp_queue_handle_t handle, void *buf, CSP_BASE_TYPE * task_woken) {
*task_woken = 0;
return csp_queue_dequeue(handle, buf, 0);
}
int csp_queue_size(csp_queue_handle_t handle) {
return pthread_queue_items(handle);
}
int csp_queue_size_isr(csp_queue_handle_t handle) {
return pthread_queue_items(handle);
}

View File

@ -0,0 +1,164 @@
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <semaphore.h>
#include <pthread.h>
#include <sys/time.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
/* CSP includes */
#include <csp/csp.h>
#include <csp/arch/csp_semaphore.h>
int csp_mutex_create(csp_mutex_t * mutex) {
csp_log_lock("Mutex init: %p", mutex);
if (pthread_mutex_init(mutex, NULL) == 0) {
return CSP_SEMAPHORE_OK;
} else {
return CSP_SEMAPHORE_ERROR;
}
}
int csp_mutex_remove(csp_mutex_t * mutex) {
if (pthread_mutex_destroy(mutex) == 0) {
return CSP_SEMAPHORE_OK;
} else {
return CSP_SEMAPHORE_ERROR;
}
}
int csp_mutex_lock(csp_mutex_t * mutex, uint32_t timeout) {
int ret;
struct timespec ts;
uint32_t sec, nsec;
csp_log_lock("Wait: %p timeout %"PRIu32, mutex, timeout);
if (timeout == CSP_INFINITY) {
ret = pthread_mutex_lock(mutex);
} else {
if (clock_gettime(CLOCK_REALTIME, &ts))
return CSP_SEMAPHORE_ERROR;
sec = timeout / 1000;
nsec = (timeout - 1000 * sec) * 1000000;
ts.tv_sec += sec;
if (ts.tv_nsec + nsec >= 1000000000)
ts.tv_sec++;
ts.tv_nsec = (ts.tv_nsec + nsec) % 1000000000;
ret = pthread_mutex_timedlock(mutex, &ts);
}
if (ret != 0)
return CSP_SEMAPHORE_ERROR;
return CSP_SEMAPHORE_OK;
}
int csp_mutex_unlock(csp_mutex_t * mutex) {
if (pthread_mutex_unlock(mutex) == 0) {
return CSP_SEMAPHORE_OK;
} else {
return CSP_SEMAPHORE_ERROR;
}
}
int csp_bin_sem_create(csp_bin_sem_handle_t * sem) {
csp_log_lock("Semaphore init: %p", sem);
if (sem_init(sem, 0, 1) == 0) {
return CSP_SEMAPHORE_OK;
} else {
return CSP_SEMAPHORE_ERROR;
}
}
int csp_bin_sem_remove(csp_bin_sem_handle_t * sem) {
if (sem_destroy(sem) == 0)
return CSP_SEMAPHORE_OK;
else
return CSP_SEMAPHORE_ERROR;
}
int csp_bin_sem_wait(csp_bin_sem_handle_t * sem, uint32_t timeout) {
int ret;
struct timespec ts;
uint32_t sec, nsec;
csp_log_lock("Wait: %p timeout %"PRIu32, sem, timeout);
if (timeout == CSP_INFINITY) {
ret = sem_wait(sem);
} else {
if (clock_gettime(CLOCK_REALTIME, &ts))
return CSP_SEMAPHORE_ERROR;
sec = timeout / 1000;
nsec = (timeout - 1000 * sec) * 1000000;
ts.tv_sec += sec;
if (ts.tv_nsec + nsec >= 1000000000)
ts.tv_sec++;
ts.tv_nsec = (ts.tv_nsec + nsec) % 1000000000;
ret = sem_timedwait(sem, &ts);
}
if (ret != 0)
return CSP_SEMAPHORE_ERROR;
return CSP_SEMAPHORE_OK;
}
int csp_bin_sem_post(csp_bin_sem_handle_t * sem) {
CSP_BASE_TYPE dummy = 0;
return csp_bin_sem_post_isr(sem, &dummy);
}
int csp_bin_sem_post_isr(csp_bin_sem_handle_t * sem, CSP_BASE_TYPE * task_woken) {
csp_log_lock("Post: %p", sem);
*task_woken = 0;
int value;
sem_getvalue(sem, &value);
if (value > 0)
return CSP_SEMAPHORE_OK;
if (sem_post(sem) == 0) {
return CSP_SEMAPHORE_OK;
} else {
return CSP_SEMAPHORE_ERROR;
}
}

View File

@ -0,0 +1,131 @@
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/sysinfo.h>
#include <sys/reboot.h>
#include <linux/reboot.h>
#include <csp/csp.h>
#include <csp/csp_error.h>
#include <csp/arch/csp_system.h>
int csp_sys_tasklist(char * out) {
strcpy(out, "Tasklist not available on POSIX");
return CSP_ERR_NONE;
}
int csp_sys_tasklist_size(void) {
return 100;
}
uint32_t csp_sys_memfree(void) {
uint32_t total = 0;
struct sysinfo info;
sysinfo(&info);
total = info.freeram * info.mem_unit;
return total;
}
int csp_sys_reboot(void) {
#ifdef CSP_USE_INIT_SHUTDOWN
/* Let init(1) handle the reboot */
int ret = system("reboot");
(void) ret; /* Silence warning */
#else
int magic = LINUX_REBOOT_CMD_RESTART;
/* Sync filesystem before reboot */
sync();
reboot(magic);
#endif
/* If reboot(2) returns, it is an error */
csp_log_error("Failed to reboot: %s", strerror(errno));
return CSP_ERR_INVAL;
}
int csp_sys_shutdown(void) {
#ifdef CSP_USE_INIT_SHUTDOWN
/* Let init(1) handle the shutdown */
int ret = system("halt");
(void) ret; /* Silence warning */
#else
int magic = LINUX_REBOOT_CMD_HALT;
/* Sync filesystem before reboot */
sync();
reboot(magic);
#endif
/* If reboot(2) returns, it is an error */
csp_log_error("Failed to shutdown: %s", strerror(errno));
return CSP_ERR_INVAL;
}
void csp_sys_set_color(csp_color_t color) {
unsigned int color_code, modifier_code;
switch (color & COLOR_MASK_COLOR) {
case COLOR_BLACK:
color_code = 30; break;
case COLOR_RED:
color_code = 31; break;
case COLOR_GREEN:
color_code = 32; break;
case COLOR_YELLOW:
color_code = 33; break;
case COLOR_BLUE:
color_code = 34; break;
case COLOR_MAGENTA:
color_code = 35; break;
case COLOR_CYAN:
color_code = 36; break;
case COLOR_WHITE:
color_code = 37; break;
case COLOR_RESET:
default:
color_code = 0; break;
}
switch (color & COLOR_MASK_MODIFIER) {
case COLOR_BOLD:
modifier_code = 1; break;
case COLOR_UNDERLINE:
modifier_code = 2; break;
case COLOR_BLINK:
modifier_code = 3; break;
case COLOR_HIDE:
modifier_code = 4; break;
case COLOR_NORMAL:
default:
modifier_code = 0; break;
}
printf("\033[%u;%um", modifier_code, color_code);
}

View File

@ -0,0 +1,55 @@
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include <pthread.h>
#include <limits.h>
/* CSP includes */
#include <csp/csp.h>
#include <csp/arch/csp_thread.h>
int csp_thread_create(csp_thread_return_t (* routine)(void *), const char * const thread_name, unsigned short stack_depth, void * parameters, unsigned int priority, csp_thread_handle_t * handle) {
pthread_attr_t attributes, *attr_ref;
int return_code;
if( pthread_attr_init(&attributes) == 0 )
{
unsigned int stack_size = PTHREAD_STACK_MIN;// use at least one memory page
while(stack_size < stack_depth)// must reach at least the provided size
{
stack_size += PTHREAD_STACK_MIN;// keep memory page boundary (some systems may fail otherwise))
}
attr_ref = &attributes;
pthread_attr_setdetachstate(attr_ref, PTHREAD_CREATE_DETACHED);// do not waste memory on each call
pthread_attr_setstacksize(attr_ref, stack_size);
}
else
{
attr_ref = NULL;
}
return_code = pthread_create(handle, attr_ref, routine, parameters);
if( attr_ref != NULL ) pthread_attr_destroy(attr_ref);
return return_code;
}

View File

@ -0,0 +1,54 @@
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
/* CSP includes */
#include <csp/csp.h>
#include <csp/arch/csp_time.h>
uint32_t csp_get_ms(void) {
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
return (uint32_t)(ts.tv_sec*1000+ts.tv_nsec/1000000);
else
return 0;
}
uint32_t csp_get_ms_isr(void) {
return csp_get_ms();
}
uint32_t csp_get_s(void) {
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
return (uint32_t)ts.tv_sec;
else
return 0;
}
uint32_t csp_get_s_isr(void) {
return csp_get_s();
}

View File

@ -0,0 +1,243 @@
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
Inspired by c-pthread-queue by Matthew Dickinson
http://code.google.com/p/c-pthread-queue/
*/
#include <pthread.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdint.h>
#include <sys/time.h>
/* CSP includes */
#include <csp/arch/posix/pthread_queue.h>
static inline int get_deadline(struct timespec *ts, uint32_t timeout_ms)
{
int ret = clock_gettime(CLOCK_MONOTONIC, ts);
if (ret < 0) {
return ret;
}
uint32_t sec = timeout_ms / 1000;
uint32_t nsec = (timeout_ms - 1000 * sec) * 1000000;
ts->tv_sec += sec;
if (ts->tv_nsec + nsec >= 1000000000) {
ts->tv_sec++;
}
ts->tv_nsec = (ts->tv_nsec + nsec) % 1000000000;
return ret;
}
static inline int init_cond_clock_monotonic(pthread_cond_t * cond)
{
int ret;
pthread_condattr_t attr;
pthread_condattr_init(&attr);
ret = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
if (ret == 0) {
ret = pthread_cond_init(cond, &attr);
}
pthread_condattr_destroy(&attr);
return ret;
}
pthread_queue_t * pthread_queue_create(int length, size_t item_size) {
pthread_queue_t * q = malloc(sizeof(pthread_queue_t));
if (q != NULL) {
q->buffer = malloc(length*item_size);
if (q->buffer != NULL) {
q->size = length;
q->item_size = item_size;
q->items = 0;
q->in = 0;
q->out = 0;
if (pthread_mutex_init(&(q->mutex), NULL) || init_cond_clock_monotonic(&(q->cond_full)) || init_cond_clock_monotonic(&(q->cond_empty))) {
free(q->buffer);
free(q);
q = NULL;
}
} else {
free(q);
q = NULL;
}
}
return q;
}
void pthread_queue_delete(pthread_queue_t * q) {
if (q == NULL)
return;
free(q->buffer);
free(q);
return;
}
static inline int wait_slot_available(pthread_queue_t * queue, struct timespec *ts) {
int ret;
while (queue->items == queue->size) {
if (ts != NULL) {
ret = pthread_cond_timedwait(&(queue->cond_full), &(queue->mutex), ts);
} else {
ret = pthread_cond_wait(&(queue->cond_full), &(queue->mutex));
}
if (ret != 0 && errno != EINTR) {
return PTHREAD_QUEUE_FULL; //Timeout
}
}
return PTHREAD_QUEUE_OK;
}
int pthread_queue_enqueue(pthread_queue_t * queue, void * value, uint32_t timeout) {
int ret;
struct timespec ts;
struct timespec *pts = NULL;
/* Calculate timeout */
if (timeout != CSP_MAX_DELAY) {
if (get_deadline(&ts, timeout) != 0) {
return PTHREAD_QUEUE_ERROR;
}
pts = &ts;
} else {
pts = NULL;
}
/* Get queue lock */
pthread_mutex_lock(&(queue->mutex));
ret = wait_slot_available(queue, pts);
if (ret == PTHREAD_QUEUE_OK) {
/* Copy object from input buffer */
memcpy(queue->buffer+(queue->in * queue->item_size), value, queue->item_size);
queue->items++;
queue->in = (queue->in + 1) % queue->size;
}
pthread_mutex_unlock(&(queue->mutex));
if (ret == PTHREAD_QUEUE_OK) {
/* Nofify blocked threads */
pthread_cond_broadcast(&(queue->cond_empty));
}
return ret;
}
static inline int wait_item_available(pthread_queue_t * queue, struct timespec *ts) {
int ret;
while (queue->items == 0) {
if (ts != NULL) {
ret = pthread_cond_timedwait(&(queue->cond_empty), &(queue->mutex), ts);
} else {
ret = pthread_cond_wait(&(queue->cond_empty), &(queue->mutex));
}
if (ret != 0 && errno != EINTR) {
return PTHREAD_QUEUE_EMPTY; //Timeout
}
}
return PTHREAD_QUEUE_OK;
}
int pthread_queue_dequeue(pthread_queue_t * queue, void * buf, uint32_t timeout) {
int ret;
struct timespec ts;
struct timespec *pts;
/* Calculate timeout */
if (timeout != CSP_MAX_DELAY) {
if (get_deadline(&ts, timeout) != 0) {
return PTHREAD_QUEUE_ERROR;
}
pts = &ts;
} else {
pts = NULL;
}
/* Get queue lock */
pthread_mutex_lock(&(queue->mutex));
ret = wait_item_available(queue, pts);
if (ret == PTHREAD_QUEUE_OK) {
/* Coby object to output buffer */
memcpy(buf, queue->buffer+(queue->out * queue->item_size), queue->item_size);
queue->items--;
queue->out = (queue->out + 1) % queue->size;
}
pthread_mutex_unlock(&(queue->mutex));
if (ret == PTHREAD_QUEUE_OK) {
/* Nofify blocked threads */
pthread_cond_broadcast(&(queue->cond_full));
}
return ret;
}
int pthread_queue_items(pthread_queue_t * queue) {
pthread_mutex_lock(&(queue->mutex));
int items = queue->items;
pthread_mutex_unlock(&(queue->mutex));
return items;
}

View File

@ -0,0 +1,18 @@
This directory contains files specific to the windows port of libcsp.
To compile and create a static library, execute:
python waf configure --with-os=windows build
from the root of this project. Note python must be in PATH.
The build requirements are:
* Windows Vista SP1
* A recent version of MinGW _or_ MinGW-w64
* Windows API headers
* cPython 2.5 or newer
What provides the Windows API headers depends on the development environment:
Using MinGW: Headers provided by w32api package. windows_glue.h header is needed because these headers do not declare condition variables.
Using MinGW-w64: Headers should be available in the default configuration. You may have to compile the distribution from source. windows_glue.h should not be needed.

View File

@ -0,0 +1,9 @@
#include <stdlib.h>
void * csp_malloc(size_t size) {
return malloc(size);
}
void csp_free(void * ptr) {
free(ptr);
}

View File

@ -0,0 +1,40 @@
#include <stdint.h>
#include <csp/csp.h>
#include <csp/arch/csp_queue.h>
#include "windows_queue.h"
csp_queue_handle_t csp_queue_create(int length, size_t item_size) {
return windows_queue_create(length, item_size);
}
void csp_queue_remove(csp_queue_handle_t queue) {
windows_queue_delete(queue);
}
int csp_queue_enqueue(csp_queue_handle_t handle, void *value, uint32_t timeout) {
return windows_queue_enqueue(handle, value, timeout);
}
int csp_queue_enqueue_isr(csp_queue_handle_t handle, void * value, CSP_BASE_TYPE * task_woken) {
if( task_woken != NULL )
*task_woken = 0;
return windows_queue_enqueue(handle, value, 0);
}
int csp_queue_dequeue(csp_queue_handle_t handle, void *buf, uint32_t timeout) {
return windows_queue_dequeue(handle, buf, timeout);
}
int csp_queue_dequeue_isr(csp_queue_handle_t handle, void * buf, CSP_BASE_TYPE * task_woken) {
if( task_woken != NULL )
*task_woken = 0;
return windows_queue_dequeue(handle, buf, 0);
}
int csp_queue_size(csp_queue_handle_t handle) {
return windows_queue_items(handle);
}
int csp_queue_size_isr(csp_queue_handle_t handle) {
return windows_queue_items(handle);
}

View File

@ -0,0 +1,74 @@
#include <Windows.h>
#include <csp/csp.h>
#include <csp/arch/csp_semaphore.h>
int csp_mutex_create(csp_mutex_t * mutex) {
HANDLE mutexHandle = CreateMutex(NULL, FALSE, FALSE);
if( mutexHandle == NULL ) {
return CSP_MUTEX_ERROR;
}
*mutex = mutexHandle;
return CSP_MUTEX_OK;
}
int csp_mutex_remove(csp_mutex_t * mutex) {
if( !CloseHandle(*mutex) ) {
return CSP_MUTEX_ERROR;
}
return CSP_MUTEX_OK;
}
int csp_mutex_lock(csp_mutex_t * mutex, uint32_t timeout) {
if(WaitForSingleObject(*mutex, timeout) == WAIT_OBJECT_0) {
return CSP_MUTEX_OK;
}
return CSP_MUTEX_ERROR;
}
int csp_mutex_unlock(csp_mutex_t * mutex) {
if( !ReleaseMutex(*mutex) ) {
return CSP_MUTEX_ERROR;
}
return CSP_MUTEX_OK;
}
int csp_bin_sem_create(csp_bin_sem_handle_t * sem) {
HANDLE semHandle = CreateSemaphore(NULL, 1, 1, NULL);
if( semHandle == NULL ) {
return CSP_SEMAPHORE_ERROR;
}
*sem = semHandle;
return CSP_SEMAPHORE_OK;
}
int csp_bin_sem_remove(csp_bin_sem_handle_t * sem) {
if( !CloseHandle(*sem) ) {
return CSP_SEMAPHORE_ERROR;
}
return CSP_SEMAPHORE_OK;
}
int csp_bin_sem_wait(csp_bin_sem_handle_t * sem, uint32_t timeout) {
if( WaitForSingleObject(*sem, timeout) == WAIT_OBJECT_0 ) {
return CSP_SEMAPHORE_OK;
}
return CSP_SEMAPHORE_ERROR;
}
int csp_bin_sem_post(csp_bin_sem_handle_t * sem) {
if( !ReleaseSemaphore(*sem, 1, NULL) ) {
return CSP_SEMAPHORE_ERROR;
}
return CSP_SEMAPHORE_OK;
}
int csp_bin_sem_post_isr(csp_bin_sem_handle_t * sem, CSP_BASE_TYPE * task_woken) {
if( task_woken != NULL ) {
*task_woken = 0;
}
return csp_bin_sem_post(sem);
}

View File

@ -0,0 +1,60 @@
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include <string.h>
#include <Windows.h>
#include <csp/csp.h>
#include <csp/csp_error.h>
#include <csp/arch/csp_system.h>
int csp_sys_tasklist(char * out) {
strcpy(out, "Tasklist not available on Windows");
return CSP_ERR_NONE;
}
uint32_t csp_sys_memfree(void) {
MEMORYSTATUSEX statex;
statex.dwLength = sizeof(statex);
GlobalMemoryStatusEx(&statex);
DWORDLONG freePhysicalMem = statex.ullAvailPhys;
size_t total = (size_t) freePhysicalMem;
return (uint32_t)total;
}
int csp_sys_reboot(void) {
/* TODO: Fix reboot on Windows */
csp_log_error("Failed to reboot");
return CSP_ERR_INVAL;
}
int csp_sys_shutdown(void) {
/* TODO: Fix shutdown on Windows */
csp_log_error("Failed to shutdown");
return CSP_ERR_INVAL;
}
void csp_sys_set_color(csp_color_t color) {
/* TODO: Add Windows color output here */
}

View File

@ -0,0 +1,11 @@
#include <Windows.h>
#include <process.h>
#include <csp/arch/csp_thread.h>
int csp_thread_create(csp_thread_return_t (* routine)(void *)__attribute__((stdcall)), const char * const thread_name, unsigned short stack_depth, void * parameters, unsigned int priority, csp_thread_handle_t * handle) {
HANDLE taskHandle = (HANDLE) _beginthreadex(NULL, stack_depth, routine, parameters, 0, 0);
if( taskHandle == 0 )
return CSP_ERR_NOMEM; // Failure
*handle = taskHandle;
return CSP_ERR_NONE;
}

View File

@ -0,0 +1,20 @@
#include <Windows.h>
#include <stdint.h>
#include <csp/arch/csp_time.h>
uint32_t csp_get_ms(void) {
return (uint32_t)GetTickCount();
}
uint32_t csp_get_ms_isr(void) {
return csp_get_ms();
}
uint32_t csp_get_s(void) {
uint32_t time_ms = csp_get_ms();
return time_ms/1000;
}
uint32_t csp_get_s_isr(void) {
return csp_get_s();
}

View File

@ -0,0 +1,23 @@
#ifndef WINDOWS_GLUE_H
#define WINDOWS_GLUE_H
#include <Windows.h>
#undef interface
#if (_WIN32_WINNT >= 0x0600)
#define RTL_CONDITION_VARIABLE_INIT 0
#define RTL_CONDITION_VARIABLE_LOCKMODE_SHARED 1
#define CONDITION_VARIABLE_INIT RTL_CONDITION_VARIABLE_INIT
#define CONDITION_VARIABLE_LOCKMODE_SHARED RTL_CONDITION_VARIABLE_LOCKMODE_SHARED
typedef PVOID RTL_CONDITION_VARIABLE;
typedef RTL_CONDITION_VARIABLE CONDITION_VARIABLE, *PCONDITION_VARIABLE;
WINBASEAPI VOID WINAPI InitializeConditionVariable(PCONDITION_VARIABLE ConditionVariable);
WINBASEAPI WINBOOL WINAPI SleepConditionVariableCS(PCONDITION_VARIABLE ConditionVariable, PCRITICAL_SECTION CriticalSection, DWORD dwMilliseconds);
WINBASEAPI VOID WINAPI WakeAllConditionVariable(PCONDITION_VARIABLE ConditionVariable);
WINBASEAPI VOID WINAPI WakeConditionVariable(PCONDITION_VARIABLE ConditionVariable);
#endif // _WIN#"_WINNT
#endif

View File

@ -0,0 +1,91 @@
#include "windows_queue.h"
#include "windows_glue.h"
#include <Windows.h>
static int queueFull(windows_queue_t * queue) {
return queue->items == queue->size;
}
static int queueEmpty(windows_queue_t * queue) {
return queue->items == 0;
}
windows_queue_t * windows_queue_create(int length, size_t item_size) {
windows_queue_t *queue = (windows_queue_t*)malloc(sizeof(windows_queue_t));
if(queue == NULL)
goto queue_malloc_failed;
queue->buffer = malloc(length*item_size);
if(queue->buffer == NULL)
goto buffer_malloc_failed;
queue->size = length;
queue->item_size = item_size;
queue->items = 0;
queue->head_idx = 0;
InitializeCriticalSection(&(queue->mutex));
InitializeConditionVariable(&(queue->cond_full));
InitializeConditionVariable(&(queue->cond_empty));
goto queue_init_success;
buffer_malloc_failed:
free(queue);
queue = NULL;
queue_malloc_failed:
queue_init_success:
return queue;
}
void windows_queue_delete(windows_queue_t * q) {
if(q==NULL) return;
DeleteCriticalSection(&(q->mutex));
free(q->buffer);
free(q);
}
int windows_queue_enqueue(windows_queue_t * queue, void * value, int timeout) {
int offset;
EnterCriticalSection(&(queue->mutex));
while(queueFull(queue)) {
int ret = SleepConditionVariableCS(&(queue->cond_full), &(queue->mutex), timeout);
if( !ret ) {
LeaveCriticalSection(&(queue->mutex));
return ret == WAIT_TIMEOUT ? WINDOWS_QUEUE_FULL : WINDOWS_QUEUE_ERROR;
}
}
offset = ((queue->head_idx+queue->items) % queue->size) * queue->item_size;
memcpy((unsigned char*)queue->buffer + offset, value, queue->item_size);
queue->items++;
LeaveCriticalSection(&(queue->mutex));
WakeAllConditionVariable(&(queue->cond_empty));
return WINDOWS_QUEUE_OK;
}
int windows_queue_dequeue(windows_queue_t * queue, void * buf, int timeout) {
EnterCriticalSection(&(queue->mutex));
while(queueEmpty(queue)) {
int ret = SleepConditionVariableCS(&(queue->cond_empty), &(queue->mutex), timeout);
if( !ret ) {
LeaveCriticalSection(&(queue->mutex));
return ret == WAIT_TIMEOUT ? WINDOWS_QUEUE_EMPTY : WINDOWS_QUEUE_ERROR;
}
}
memcpy(buf, (unsigned char*)queue->buffer+(queue->head_idx%queue->size*queue->item_size), queue->item_size);
queue->items--;
queue->head_idx = (queue->head_idx + 1) % queue->size;
LeaveCriticalSection(&(queue->mutex));
WakeAllConditionVariable(&(queue->cond_full));
return WINDOWS_QUEUE_OK;
}
int windows_queue_items(windows_queue_t * queue) {
int items;
EnterCriticalSection(&(queue->mutex));
items = queue->items;
LeaveCriticalSection(&(queue->mutex));
return items;
}

View File

@ -0,0 +1,41 @@
#ifndef _WINDOWS_QUEUE_H_
#define _WINDOWS_QUEUE_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <Windows.h>
#include "windows_glue.h"
#undef interface
#include <csp/arch/csp_queue.h>
#define WINDOWS_QUEUE_ERROR CSP_QUEUE_ERROR
#define WINDOWS_QUEUE_EMPTY CSP_QUEUE_ERROR
#define WINDOWS_QUEUE_FULL CSP_QUEUE_ERROR
#define WINDOWS_QUEUE_OK CSP_QUEUE_OK
typedef struct windows_queue_s {
void * buffer;
int size;
int item_size;
int items;
int head_idx;
CRITICAL_SECTION mutex;
CONDITION_VARIABLE cond_full;
CONDITION_VARIABLE cond_empty;
} windows_queue_t;
windows_queue_t * windows_queue_create(int length, size_t item_size);
void windows_queue_delete(windows_queue_t * q);
int windows_queue_enqueue(windows_queue_t * queue, void * value, int timeout);
int windows_queue_dequeue(windows_queue_t * queue, void * buf, int timeout);
int windows_queue_items(windows_queue_t * queue);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif // _WINDOWS_QUEUE_H_