]> rtime.felk.cvut.cz Git - arc.git/blob - communication/PduR/PduR.c
Lint fixes on PduR and CanIf
[arc.git] / communication / PduR / PduR.c
1 /* -------------------------------- Arctic Core ------------------------------\r
2  * Arctic Core - the open source AUTOSAR platform http://arccore.com\r
3  *\r
4  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>\r
5  *\r
6  * This source code is free software; you can redistribute it and/or modify it\r
7  * under the terms of the GNU General Public License version 2 as published by the\r
8  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.\r
9  *\r
10  * This program is distributed in the hope that it will be useful, but\r
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\r
13  * for more details.\r
14  * -------------------------------- Arctic Core ------------------------------*/\r
15 \r
16 \r
17 \r
18 \r
19 \r
20 \r
21 \r
22 \r
23 \r
24 #include <stdlib.h>\r
25 #include <string.h>\r
26 \r
27 \r
28 #include "Det.h"\r
29 #if defined(USE_DEM)\r
30 #include "Dem.h"\r
31 #endif\r
32 #include "PduR.h"\r
33 #include "McuExtensions.h"\r
34 #include "debug.h"\r
35 \r
36 /*\r
37  * The state of the PDU router.\r
38  */\r
39 PduR_StateType PduRState = PDUR_UNINIT; // 960, 31 LINT: Borde åtgärdas\r
40 \r
41 const PduR_PBConfigType * PduRConfig;\r
42 \r
43 \r
44 /* ############### Zero Cost Operation Mode ############# */\r
45 /* Only define the following functions if zero cost operation\r
46  * mode is not used! They are defined as macros in PduR.h otherwise. */\r
47 #if PDUR_ZERO_COST_OPERATION == STD_OFF\r
48 \r
49 /*\r
50  * Initializes the PDU Router.\r
51  */\r
52 void PduR_Init (const PduR_PBConfigType* ConfigPtr) {\r
53 \r
54         //Enter(0);\r
55 \r
56         // Make sure the PDU Router is uninitialized.\r
57         // Otherwise raise an error.\r
58         if (PduRState != PDUR_UNINIT) {\r
59                 // Raise error and return.\r
60                 PDUR_DET_REPORTERROR(MODULE_ID_PDUR, PDUR_INSTANCE_ID, 0x00, PDUR_E_INVALID_REQUEST);\r
61                 return;\r
62         }\r
63 \r
64         if (ConfigPtr == NULL) {\r
65                 PDUR_DET_REPORTERROR(MODULE_ID_PDUR, PDUR_INSTANCE_ID, 0x00, PDUR_E_CONFIG_PTR_INVALID);\r
66                 return;\r
67         } else {\r
68                 PduRConfig = ConfigPtr;\r
69         }\r
70 \r
71         // Start initialization!\r
72         DEBUG(DEBUG_LOW,"--Initialization of PDU router--\n");\r
73 \r
74         uint8 failed = 0;\r
75 \r
76         // Initialize buffers.\r
77         int bufferNr = 0;\r
78         int i = 0;\r
79         PduRRoutingPath_type *path;\r
80         PduRConfig->PduRRoutingTable->NRoutingPaths = 0;\r
81         for (i = 0; (!PduRConfig->PduRRoutingTable->PduRRoutingPath[i].PduR_Arc_EOL) && (!failed); i++) {\r
82                 PduRConfig->PduRRoutingTable->NRoutingPaths++;\r
83                 path = &PduRConfig->PduRRoutingTable->PduRRoutingPath[i];\r
84 \r
85                 if (path->PduRDestPdu.DataProvision != PDUR_NO_PROVISION) {\r
86                         // Allocate memory for new buffer.\r
87                         PduRTxBuffer_type *buffer = path->PduRDestPdu.TxBufferRef;\r
88 \r
89                         if (bufferNr >= PDUR_MAX_TX_BUFFER_NUMBER) {\r
90                                 DEBUG(DEBUG_LOW,"PduR_Init: Initialization of buffer failed due to erroneous configuration.\nThe number of buffer exceeded the maximum number of allowed buffers.\n");\r
91                                 failed = 1;\r
92                                 break;\r
93                         }\r
94                         if      ((buffer->Buffer = (uint8 *)malloc(buffer->Depth * sizeof(uint8) * path->SduLength)) == 0) {\r
95                                 DEBUG(DEBUG_LOW,"PduR_Init: Initialization of buffer failed. Buffer space could not be allocated for buffer number %d\n", bufferNr);\r
96                                 failed = 1;\r
97                                 break;\r
98                         }\r
99 \r
100                         buffer->First = buffer->Buffer;\r
101                         buffer->Last = buffer->Buffer;\r
102 \r
103 \r
104                         // Initialize the new buffer.\r
105                         buffer->BufferId = i; // Set buffer id.\r
106                         buffer->BufferType = path->PduRDestPdu.DataProvision; // Set buffer data provision mode.\r
107                         buffer->Length = path->SduLength; // Set buffer sdu length.\r
108 \r
109                         if (path->PduRDestPdu.DataProvision == PDUR_TRIGGER_TRANSMIT) {\r
110                                 //memcpy(buffer->First, path->PduRDefaultValue.PduRDefaultValueElement->DefaultValueElement,path->SduLength);\r
111                                 PduR_BufferQueue(buffer, path->PduRDefaultValue.PduRDefaultValueElement->DefaultValueElement);\r
112                         }\r
113 \r
114                         // Save pointer to the new buffer.\r
115                         //PduR_RTable_LoIf.RoutingTable[i].TxBufferRef = &PduRBuffers[bufferNr];\r
116 \r
117                         DEBUG(DEBUG_LOW,"Initialized buffer %d. Id: %d, Type: %d, Depth: %d\n", bufferNr, buffer->BufferId, buffer->BufferType, buffer->Depth);\r
118                         bufferNr++;\r
119                 }\r
120         }\r
121 \r
122         if (failed) {\r
123                 // TODO Report PDUR_E_INIT_FAILED to Dem.\r
124                 PduRState = PDUR_REDUCED;\r
125                 DEBUG(DEBUG_LOW,"--Initialization of PDU router failed--\n");\r
126 \r
127 \r
128         } else {\r
129                 // The initialization succeeded!\r
130                 PduRState = PDUR_ONLINE;\r
131                 DEBUG(DEBUG_LOW,"--Initialization of PDU router completed --\n");\r
132 \r
133         }\r
134 }\r
135 \r
136 void PduR_BufferInc(PduRTxBuffer_type *Buffer, uint8 **ptr) {\r
137         (*ptr) = (*ptr) + Buffer->Length;\r
138 \r
139         // TODO make more efficient without multiplication.\r
140         if ( *ptr >= ( Buffer->Buffer + (Buffer->Depth * Buffer->Length) ) ) {\r
141                 *ptr = Buffer->Buffer;\r
142         }\r
143         //*val = (*val + 1) % Buffer->Depth;\r
144 }\r
145 \r
146 void PduR_BufferQueue(PduRTxBuffer_type *Buffer, const uint8 * SduPtr) {\r
147         imask_t state = McuE_EnterCriticalSection();\r
148 \r
149         if (PduR_BufferIsFull(Buffer)) { // Buffer is full\r
150                 PduR_BufferFlush(Buffer);\r
151 #if defined(USE_DEM)\r
152                 Dem_ReportErrorStatus(PDUR_E_PDU_INSTANCE_LOST, DEM_EVENT_STATUS_FAILED);\r
153 #endif\r
154 \r
155         } else {\r
156                 // Copy data to last place in buffer\r
157                 memcpy(Buffer->Last, SduPtr, sizeof(uint8) * Buffer->Length);\r
158 \r
159                 PduR_BufferInc(Buffer, &Buffer->Last);\r
160                 Buffer->NrItems++;\r
161                 DEBUG(DEBUG_LOW,"\tBuffer %d: Queued data %d. Status: NrItems %d, First %d, Last %d\n", Buffer->BufferId, *SduPtr, Buffer->NrItems, *Buffer->First, *Buffer->Last);\r
162 \r
163         }\r
164         McuE_ExitCriticalSection(state);\r
165 }\r
166 \r
167 void PduR_BufferDeQueue(PduRTxBuffer_type *Buffer, uint8 *SduPtr) {\r
168         imask_t state = McuE_EnterCriticalSection();\r
169 \r
170         if (Buffer->NrItems > 0) {\r
171                 memcpy(SduPtr, Buffer->First, sizeof(uint8) * Buffer->Length);\r
172                 PduR_BufferInc(Buffer, &Buffer->First);\r
173                 Buffer->NrItems--;\r
174                 DEBUG(DEBUG_LOW,"\tBuffer %d: DeQueueing data. Status: NrItems %d, First %d, Last %d\n", Buffer->BufferId, Buffer->NrItems, *Buffer->First, *Buffer->Last);\r
175         } else {\r
176                 DEBUG(DEBUG_LOW,"\tBuffer %d: Buffer is empty, nothing to dequeue!\n", Buffer->BufferId);\r
177         }\r
178         McuE_ExitCriticalSection(state);\r
179 }\r
180 \r
181 void PduR_BufferFlush(PduRTxBuffer_type *Buffer) {\r
182         DEBUG(DEBUG_LOW,"\tBuffer %d: Flushing!\n", Buffer->BufferId);\r
183         imask_t state = McuE_EnterCriticalSection();\r
184         Buffer->NrItems = 0;\r
185         Buffer->First = Buffer->Buffer;\r
186         Buffer->Last = Buffer->Buffer;\r
187         Buffer->TxConfP = 0;\r
188         McuE_ExitCriticalSection(state);\r
189 }\r
190 \r
191 uint8 PduR_BufferIsFull(PduRTxBuffer_type *Buffer) {\r
192         imask_t state = McuE_EnterCriticalSection();\r
193         uint8 rv = 0;\r
194         if (Buffer->NrItems < Buffer->Depth) {\r
195                 rv = 0;\r
196         } else {\r
197                 rv = 1;\r
198         }\r
199         McuE_ExitCriticalSection(state);\r
200         return rv;\r
201 }\r
202 \r
203 \r
204 #if PDUR_VERSION_INFO_API == STD_ON\r
205 void PduR_GetVersionInfo (Std_VersionInfoType* versionInfo){\r
206         versionInfo->moduleID = (uint16)MODULE_ID_PDUR;\r
207         versionInfo->vendorID = (uint16)1;\r
208 \r
209         // TODO Add vendor specific version numbers.\r
210 }\r
211 #endif\r
212 \r
213 uint32 PduR_GetConfigurationId () {\r
214         //PduR_DevCheck(0,1,0x18,E_NOT_OK);\r
215         return PduRConfig->PduRConfigurationId;\r
216 }\r
217 #endif // End of not Zero Cost Operation Mode\r
218 \r
219 Std_ReturnType PduR_CancelTransmitRequest(PduR_CancelReasonType PduCancelReason, PduIdType PduId) {\r
220         // TODO Implement!\r
221         return E_NOT_OK;\r
222 }\r
223 \r
224 void PduR_ChangeParameterRequest(PduR_ParameterValueType PduParameterValue, PduIdType PduId) {\r
225         // TODO Implement!\r
226 \r
227 }\r