<commit_before>/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2011 GomSpace ApS (http://www.gomspace.com)
Copyright (C) 2011 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 <string.h>
#include <stdint.h>

/* CSP includes */
#include <csp/csp.h>
#include <csp/csp_cmp.h>
#include <csp/csp_endian.h>
#include <csp/csp_platform.h>

#include "arch/csp_time.h"
#include "arch/csp_malloc.h"

#ifdef CSP_POSIX
#include <sys/sysinfo.h>
#endif
#ifdef CSP_WINDOWS
#include <Windows.h>
#endif

/* CSP Management Protocol handler */
int csp_cmp_handler(csp_conn_t * conn, csp_packet_t * packet) {

	struct csp_cmp_message * cmp = (struct csp_cmp_message *) packet->data;

	/* Ignore everything but requests */
	if (cmp->type != CSP_CMP_REQUEST)
		return CSP_ERR_INVAL;

	switch (cmp->code) {

	case CSP_CMP_IDENT:
		cmp->type = CSP_CMP_REPLY;

		/* Copy revision */
		strncpy(cmp->ident.revision, GIT_REV, CSP_CMP_IDENT_REV_LEN);
		cmp->ident.revision[CSP_CMP_IDENT_REV_LEN - 1] = '\0';

		/* Copy compilation date */
		strncpy(cmp->ident.date, __DATE__, CSP_CMP_IDENT_DATE_LEN);
		cmp->ident.date[CSP_CMP_IDENT_DATE_LEN - 1] = '\0';

		/* Copy compilation time */
		strncpy(cmp->ident.time, __TIME__, CSP_CMP_IDENT_TIME_LEN);
		cmp->ident.time[CSP_CMP_IDENT_TIME_LEN - 1] = '\0';

		/* Copy hostname */
		strncpy(cmp->ident.hostname, csp_get_hostname(), CSP_HOSTNAME_LEN);
		cmp->ident.hostname[CSP_HOSTNAME_LEN - 1] = '\0';

		/* Copy model name */
		strncpy(cmp->ident.model, csp_get_model(), CSP_MODEL_LEN);
		cmp->ident.model[CSP_MODEL_LEN - 1] = '\0';

		packet->length = CMP_SIZE(ident);
		
		break;
	
	default:
		return CSP_ERR_INVAL;
	}

	return CSP_ERR_NONE;
}

void csp_service_handler(csp_conn_t * conn, csp_packet_t * packet) {

	switch (csp_conn_dport(conn)) {

	/* Pass to CMP handler */
	case CSP_CMP:
		if (csp_cmp_handler(conn, packet) != CSP_ERR_NONE) {
			csp_buffer_free(packet);
			return;
		}
		break;

	/* A ping means, just echo the packet, so no changes */
	case CSP_PING:
		csp_debug(CSP_INFO, "SERVICE: Ping received\r\n");
		break;

	/* Retrieve the ProcessList as a string */
	case CSP_PS: {
#if defined(CSP_FREERTOS)
		vTaskList((signed portCHAR *) packet->data);
#elif defined(CSP_POSIX)
		strcpy((char *)packet->data, "Tasklist in not available on posix");
#elif defined(CSP_WINDOWS)
		strcpy((char *)packet->data, "Tasklist in not available on windows");
#endif
		packet->length = strlen((char *)packet->data);
		packet->data[packet->length] = '\0';
		packet->length++;
		break;
	}

	/* Do a search for the largest block of free memory */
	case CSP_MEMFREE: {

#if defined(CSP_FREERTOS)
		/* Try to malloc a lot */
		uint32_t size = 1000000, total = 0, max = UINT32_MAX;
		void * pmem;
		while (1) {
			pmem = csp_malloc(size + total);
			if (pmem == NULL) {
				max = size + total;
				size = size / 2;
			} else {
				total += size;
				if (total + size >= max)
					size = size / 2;
				csp_free(pmem);
			}
			if (size < 32) break;
		}
#elif defined(CSP_POSIX)
		/* Read system statistics */
		size_t total = 0;
		struct sysinfo info;
		sysinfo(&info);
		total = info.freeram * info.mem_unit;
#elif defined(CSP_WINDOWS)
		MEMORYSTATUSEX statex;
		statex.dwLength = sizeof(statex);
		GlobalMemoryStatusEx(&statex);<commit_msg>Fixed wrong 64-bit size_t in csp_service_handler<commit_after>/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2011 GomSpace ApS (http://www.gomspace.com)
Copyright (C) 2011 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 <string.h>
#include <stdint.h>

/* CSP includes */
#include <csp/csp.h>
#include <csp/csp_cmp.h>
#include <csp/csp_endian.h>
#include <csp/csp_platform.h>

#include "arch/csp_time.h"
#include "arch/csp_malloc.h"

#ifdef CSP_POSIX
#include <sys/sysinfo.h>
#endif
#ifdef CSP_WINDOWS
#include <Windows.h>
#endif

/* CSP Management Protocol handler */
int csp_cmp_handler(csp_conn_t * conn, csp_packet_t * packet) {

	struct csp_cmp_message * cmp = (struct csp_cmp_message *) packet->data;

	/* Ignore everything but requests */
	if (cmp->type != CSP_CMP_REQUEST)
		return CSP_ERR_INVAL;

	switch (cmp->code) {

	case CSP_CMP_IDENT:
		cmp->type = CSP_CMP_REPLY;

		/* Copy revision */
		strncpy(cmp->ident.revision, GIT_REV, CSP_CMP_IDENT_REV_LEN);
		cmp->ident.revision[CSP_CMP_IDENT_REV_LEN - 1] = '\0';

		/* Copy compilation date */
		strncpy(cmp->ident.date, __DATE__, CSP_CMP_IDENT_DATE_LEN);
		cmp->ident.date[CSP_CMP_IDENT_DATE_LEN - 1] = '\0';

		/* Copy compilation time */
		strncpy(cmp->ident.time, __TIME__, CSP_CMP_IDENT_TIME_LEN);
		cmp->ident.time[CSP_CMP_IDENT_TIME_LEN - 1] = '\0';

		/* Copy hostname */
		strncpy(cmp->ident.hostname, csp_get_hostname(), CSP_HOSTNAME_LEN);
		cmp->ident.hostname[CSP_HOSTNAME_LEN - 1] = '\0';

		/* Copy model name */
		strncpy(cmp->ident.model, csp_get_model(), CSP_MODEL_LEN);
		cmp->ident.model[CSP_MODEL_LEN - 1] = '\0';

		packet->length = CMP_SIZE(ident);
		
		break;
	
	default:
		return CSP_ERR_INVAL;
	}

	return CSP_ERR_NONE;
}

void csp_service_handler(csp_conn_t * conn, csp_packet_t * packet) {

	switch (csp_conn_dport(conn)) {

	/* Pass to CMP handler */
	case CSP_CMP:
		if (csp_cmp_handler(conn, packet) != CSP_ERR_NONE) {
			csp_buffer_free(packet);
			return;
		}
		break;

	/* A ping means, just echo the packet, so no changes */
	case CSP_PING:
		csp_debug(CSP_INFO, "SERVICE: Ping received\r\n");
		break;

	/* Retrieve the ProcessList as a string */
	case CSP_PS: {
#if defined(CSP_FREERTOS)
		vTaskList((signed portCHAR *) packet->data);
#elif defined(CSP_POSIX)
		strcpy((char *)packet->data, "Tasklist in not available on posix");
#elif defined(CSP_WINDOWS)
		strcpy((char *)packet->data, "Tasklist in not available on windows");
#endif
		packet->length = strlen((char *)packet->data);
		packet->data[packet->length] = '\0';
		packet->length++;
		break;
	}

	/* Do a search for the largest block of free memory */
	case CSP_MEMFREE: {

#if defined(CSP_FREERTOS)
		/* Try to malloc a lot */
		uint32_t size = 1000000, total = 0, max = UINT32_MAX;
		void * pmem;
		while (1) {
			pmem = csp_malloc(size + total);
			if (pmem == NULL) {
				max = size + total;
				size = size / 2;
			} else {
				total += size;
				if (total + size >= max)
					size = size / 2;
				csp_free(pmem);
			}
			if (size < 32) break;
		}
#elif defined(CSP_POSIX)
		/* Read system statistics */
		uint32_t total = 0;
		struct sysinfo info;
		sysinfo(&info);
		total = info.freeram * info.mem_unit;
#elif defined(CSP_WINDOWS)
		MEMORYSTATUSEX statex;
		statex.dwLength = sizeof(statex);
		GlobalMemoryStatusEx(&statex);
{'fraction_non_alphanumeric': 0.12199333908967559, 'fraction_numerical': 0.013321820648822006, 'mean_word_length': 4.020433436532508, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4594068', 'n_tokens_mistral': 3233, 'n_tokens_neox': 2949, 'n_words': 818}