3D-ICE 3.0.0
network_message.c
1/******************************************************************************
2 * This file is part of 3D-ICE, version 3.1.0 . *
3 * *
4 * 3D-ICE is free software: you can redistribute it and/or modify it under *
5 * the terms of the GNU General Public License as published by the Free *
6 * Software Foundation, either version 3 of the License, or any later *
7 * version. *
8 * *
9 * 3D-ICE is distributed in the hope that it will be useful, but WITHOUT *
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for *
12 * more details. *
13 * *
14 * You should have received a copy of the GNU General Public License along *
15 * with 3D-ICE. If not, see <http://www.gnu.org/licenses/>. *
16 * *
17 * Copyright (C) 2021 *
18 * Embedded Systems Laboratory - Ecole Polytechnique Federale de Lausanne *
19 * All Rights Reserved. *
20 * *
21 * Authors: Arvind Sridhar Alessandro Vincenzi *
22 * Giseong Bak Martino Ruggiero *
23 * Thomas Brunschwiler Eder Zulian *
24 * Federico Terraneo Darong Huang *
25 * Luis Costero Marina Zapater *
26 * David Atienza *
27 * *
28 * For any comment, suggestion or request about 3D-ICE, please register and *
29 * write to the mailing list (see http://listes.epfl.ch/doc.cgi?liste=3d-ice) *
30 * Any usage of 3D-ICE for research, commercial or other purposes must be *
31 * properly acknowledged in the resulting products or publications. *
32 * *
33 * EPFL-STI-IEL-ESL Mail : 3d-ice@listes.epfl.ch *
34 * Batiment ELG, ELG 130 (SUBSCRIPTION IS NECESSARY) *
35 * Station 11 *
36 * 1015 Lausanne, Switzerland Url : http://esl.epfl.ch/3d-ice *
37 ******************************************************************************/
38
39#include <stdlib.h> // For the memory function calloc
40#include <string.h> // For the memory function memcpy
41
42#include "network_message.h"
43
44/******************************************************************************/
45
47{
48 message->Memory = (MessageWord_t *) calloc (MESSAGE_LENGTH, sizeof (MessageWord_t)) ;
49
50 message->MaxLength = MESSAGE_LENGTH ;
51
52 message->Length = message->Memory ;
53
54 message->MType = message->Length + 1u ;
55
56 message->Content = message->MType + 1u ;
57}
58
59/******************************************************************************/
60
62{
63 if (message->Memory != NULL)
64
65 free (message->Memory) ;
66
67 message->Memory = NULL ;
68 message->MaxLength = 0 ;
69 message->Length = NULL ;
70 message->MType = NULL ;
71 message->Content = NULL ;
72}
73
74/******************************************************************************/
75
77{
78 MessageWord_t *tmp = (MessageWord_t *) calloc (new_size, sizeof(MessageWord_t)) ;
79
80 memcpy (tmp, message->Memory, message->MaxLength * sizeof(MessageWord_t)) ;
81
82 free (message->Memory) ;
83
84 message->Memory = tmp ;
85 message->MaxLength = new_size ;
86 message->Length = message->Memory ;
87 message->MType = message->Length + 1u ;
88 message->Content = message->MType + 1u ;
89}
90
91/******************************************************************************/
92
94{
95 *message->Length = (MessageWord_t) 2u ;
96
97 *message->MType = (MessageWord_t) type ;
98}
99
100/******************************************************************************/
101
103(
104 NetworkMessage_t *message,
105 void *word
106)
107{
108 if (*message->Length == message->MaxLength)
109
110 increase_message_memory (message, 2 * message->MaxLength) ;
111
112 MessageWord_t *toinsert = message->Memory + *message->Length ;
113
114 memcpy (toinsert, word, sizeof (MessageWord_t)) ;
115
116 (*message->Length)++ ;
117}
118
119/******************************************************************************/
120
122(
123 NetworkMessage_t *message,
124 void *word,
125 Quantity_t index
126)
127{
128 if (index >= (message->MaxLength - 2))
129
130 return TDICE_FAILURE ;
131
132 memcpy (word, message->Content + index, sizeof (MessageWord_t)) ;
133
134 return TDICE_SUCCESS ;
135}
136
137/******************************************************************************/
138
void network_message_destroy(NetworkMessage_t *message)
void insert_message_word(NetworkMessage_t *message, void *word)
Error_t extract_message_word(NetworkMessage_t *message, void *word, Quantity_t index)
void build_message_head(NetworkMessage_t *message, MessageType_t type)
#define MESSAGE_LENGTH
void increase_message_memory(NetworkMessage_t *message, Quantity_t new_size)
void network_message_init(NetworkMessage_t *message)
Structure used to store messages to be sent over network.
MessageWord_t * Content
MessageWord_t * MType
MessageWord_t * Memory
Quantity_t MaxLength
MessageWord_t * Length
Error_t
Definition: types.h:401
@ TDICE_SUCCESS
The function returns with success.
Definition: types.h:402
@ TDICE_FAILURE
The function retuerns with a generic error.
Definition: types.h:403
uint32_t MessageWord_t
Definition: types.h:717
MessageType_t
Definition: types.h:559
uint32_t Quantity_t
Definition: types.h:59