NNP STM Generic Remote Module git-main
Loading...
Searching...
No Matches
objacces.c
Go to the documentation of this file.
1
13#include "data.h"
14
32#ifdef DEBUG_WAR_CONSOLE_ON
33UNS8 accessDictionaryError(UNS16 index, UNS8 subIndex,
34 UNS32 sizeDataDict, UNS32 sizeDataGiven, UNS32 code)
35{
36 MSG_WAR(0x2B09,"Dictionary index : ", index);
37 MSG_WAR(0X2B10," subindex : ", subIndex);
38 switch (code)
39 {
40 case OD_NO_SUCH_OBJECT:
41 MSG_WAR(0x2B11,"Index not found ", index);
42 break;
43 case OD_NO_SUCH_SUBINDEX :
44 MSG_WAR(0x2B12,"SubIndex not found ", subIndex);
45 break;
46 case OD_WRITE_NOT_ALLOWED :
47 MSG_WAR(0x2B13,"Write not allowed, data is read only ", index);
48 break;
49 case OD_LENGTH_DATA_INVALID :
50 MSG_WAR(0x2B14,"Conflict size data. Should be (bytes) : ", sizeDataDict);
51 MSG_WAR(0x2B15,"But you have given the size : ", sizeDataGiven);
52 break;
53 case OD_NOT_MAPPABLE :
54 MSG_WAR(0x2B16,"Not mappable data in a PDO at index : ", index);
55 break;
56 case OD_VALUE_TOO_LOW :
57 MSG_WAR(0x2B17,"Value range error : value too low. SDOabort : ", code);
58 break;
59 case OD_VALUE_TOO_HIGH :
60 MSG_WAR(0x2B18,"Value range error : value too high. SDOabort : ", code);
61 break;
62 default :
63 MSG_WAR(0x2B20, "Unknown error code : ", code);
64 }
65 return 0;
66}
67#else
68#define accessDictionaryError(index, subIndex, sizeDataDict, sizeDataGiven, code)
69#endif
70
112 UNS16 wIndex,
113 UNS8 bSubindex,
114 void * pDestData,
115 UNS32 * pExpectedSize,
116 UNS8 * pDataType,
117 UNS8 checkAccess,
118 UNS8 endianize)
119{ /* DO NOT USE MSG_ERR because the macro may send a PDO -> infinite
120 loop if it fails. */
121 UNS32 errorCode;
122 UNS32 szData;
123 const indextable * ptrTable;
124 ODCallback_t *Callback;
125
126 ptrTable = (*d->scanIndexOD)(wIndex, &errorCode, &Callback);
127
128 if (errorCode != OD_SUCCESSFUL)
129 return errorCode;
130 if( ptrTable->bSubCount <= bSubindex )
131 {
132 /* Subindex not found */
133 accessDictionaryError(wIndex, bSubindex, 0, 0, OD_NO_SUCH_SUBINDEX);
134 return OD_NO_SUCH_SUBINDEX;
135 }
136
137 if (checkAccess && (ptrTable->pSubindex[bSubindex].bAccessType & WO))
138 {
139 MSG_WAR(0x2B30, "Access Type : ", ptrTable->pSubindex[bSubindex].bAccessType);
140 accessDictionaryError(wIndex, bSubindex, 0, 0, OD_READ_NOT_ALLOWED);
141 return OD_READ_NOT_ALLOWED;
142 }
143
144
145 *pDataType = ptrTable->pSubindex[bSubindex].bDataType;
146 szData = ptrTable->pSubindex[bSubindex].size;
147
148 if(*pExpectedSize == 0 ||
149 *pExpectedSize == szData ||
150 /* allow to fetch a shorter string than expected */
151 (*pDataType >= visible_string && *pExpectedSize < szData))
152 {
153
154# ifdef CANOPEN_BIG_ENDIAN
155 if(endianize && *pDataType > boolean && !(
156 *pDataType >= visible_string &&
157 *pDataType <= domain))
158 {
159 /* data must be transmited with low byte first */
160 UNS8 i, j = 0;
161 MSG_WAR(boolean, "data type ", *pDataType);
162 MSG_WAR(visible_string, "data type ", *pDataType);
163 for ( i = szData ; i > 0 ; i--)
164 {
165 MSG_WAR(i," ", j);
166 ((UNS8*)pDestData)[j++] =
167 ((UNS8*)ptrTable->pSubindex[bSubindex].pObject)[i-1];
168 }
169
170 *pExpectedSize = szData;
171 }
172 else /* no endianisation change */
173# endif
174 if(*pDataType != visible_string)
175 {
176 memcpy(pDestData, ptrTable->pSubindex[bSubindex].pObject, szData);
177 *pExpectedSize = szData;
178 }
179
180 else
181 {
182
183 /* Copy null terminated string to user, and return discovered size */
184 UNS8 *ptr = (UNS8*)ptrTable->pSubindex[bSubindex].pObject;
185 UNS8 *ptr_start = ptr;
186
187 /* *pExpectedSize IS < szData . if null, use szData */
188 UNS8 *ptr_end = ptr + (*pExpectedSize ? *pExpectedSize : szData) ;
189 UNS8 *ptr_dest = (UNS8*)pDestData;
190
191 while( *ptr && ptr < ptr_end)
192 {
193 *(ptr_dest++) = *(ptr++);
194 }
195
196 *pExpectedSize = (UNS32)(ptr - ptr_start);
197 /* terminate string if not maximum length */
198 if (*pExpectedSize < szData)
199 *(ptr) = 0;
200
201 }
202
203 return OD_SUCCESSFUL;
204 }
205 else
206 { /* Error ! */
207 *pExpectedSize = szData;
208 accessDictionaryError(wIndex, bSubindex, szData,
209 *pExpectedSize, OD_LENGTH_DATA_INVALID);
210
211 return OD_LENGTH_DATA_INVALID;
212 }
213}
214
236 UNS16 wIndex,
237 UNS8 bSubindex,
238 void * pSourceData,
239 UNS32 * pExpectedSize,
240 UNS8 checkAccess,
241 UNS8 endianize)
242{
243 UNS32 szData;
244 UNS8 dataType;
245 UNS32 errorCode;
246 const indextable *ptrTable;
247 ODCallback_t *Callback;
248
249 ptrTable =(*d->scanIndexOD)(wIndex, &errorCode, &Callback);
250 if (errorCode != OD_SUCCESSFUL)
251 return errorCode;
252
253 if( ptrTable->bSubCount <= bSubindex )
254 {
255 /* Subindex not found */
256 accessDictionaryError(wIndex, bSubindex, 0, *pExpectedSize, OD_NO_SUCH_SUBINDEX);
257 return OD_NO_SUCH_SUBINDEX;
258 }
259 if (checkAccess && (ptrTable->pSubindex[bSubindex].bAccessType == RO))
260 {
261 MSG_WAR(0x2B25, "Access Type : ", ptrTable->pSubindex[bSubindex].bAccessType);
262 accessDictionaryError(wIndex, bSubindex, 0, *pExpectedSize, OD_WRITE_NOT_ALLOWED);
263 return OD_WRITE_NOT_ALLOWED;
264 }
265
266
267 dataType = ptrTable->pSubindex[bSubindex].bDataType;
268 szData = ptrTable->pSubindex[bSubindex].size;
269
270 if( *pExpectedSize == 0 ||
271 *pExpectedSize == szData ||
272 /* allow to store a shorter string than entry size */
273 (dataType == visible_string && *pExpectedSize < szData)) //look for else at bottom
274 {
275#ifdef CANOPEN_BIG_ENDIAN
276 /* re-endianize do not occur for bool, strings time and domains */
277 if(endianize && dataType > boolean && !(
278 dataType >= visible_string &&
279 dataType <= domain))
280 {
281 /* we invert the data source directly. This let us do range
282 testing without */
283 /* additional temp variable */
284 UNS8 i;
285 for ( i = 0 ; i < ( ptrTable->pSubindex[bSubindex].size >> 1) ; i++)
286 {
287 UNS8 tmp =((UNS8 *)pSourceData) [(ptrTable->pSubindex[bSubindex].size - 1) - i];
288 ((UNS8 *)pSourceData) [(ptrTable->pSubindex[bSubindex].size - 1) - i] = ((UNS8 *)pSourceData)[i];
289 ((UNS8 *)pSourceData)[i] = tmp;
290 }
291 }
292#endif
293 errorCode = (*d->valueRangeTest)(dataType, pSourceData);
294 if (errorCode)
295 {
296 accessDictionaryError(wIndex, bSubindex, szData, *pExpectedSize, errorCode);
297 return errorCode;
298 }
299 memcpy(ptrTable->pSubindex[bSubindex].pObject,pSourceData, szData);
300 /* TODO : CONFORM TO DS-301 :
301 * - stop using NULL terminated strings
302 * - store string size in td_subindex
303 * */
304 /* terminate visible_string with '\0' */
305 if(dataType == visible_string && *pExpectedSize < szData)
306 ((UNS8*)ptrTable->pSubindex[bSubindex].pObject)[*pExpectedSize] = 0;
307
308 *pExpectedSize = szData;
309
310 /* Callbacks */
311 if(Callback && Callback[bSubindex])
312 {
313 errorCode = (Callback[bSubindex])(d, ptrTable, bSubindex);
314 if(errorCode != OD_SUCCESSFUL)
315 {
316 return errorCode;
317 }
318 }
319
320 /* TODO : Store dans NVRAM */
321 if (ptrTable->pSubindex[bSubindex].bAccessType & TO_BE_SAVE)
322 {
323 (*d->storeODSubIndex)(d, wIndex, bSubindex);
324 }
325 return OD_SUCCESSFUL;
326 } // end if (outer IF -- 3rd from top)
327 else
328 {
329 *pExpectedSize = szData;
330 accessDictionaryError(wIndex, bSubindex, szData, *pExpectedSize, OD_LENGTH_DATA_INVALID);
331 return OD_LENGTH_DATA_INVALID;
332 }
333}
334
343const indextable * scanIndexOD (CO_Data* d, UNS16 wIndex, UNS32 *errorCode, ODCallback_t **Callback)
344{
345 return (*d->scanIndexOD)(wIndex, errorCode, Callback);
346}
347
357UNS32 RegisterSetODentryCallBack(CO_Data* d, UNS16 wIndex, UNS8 bSubindex, ODCallback_t Callback)
358{
359UNS32 errorCode;
360ODCallback_t *CallbackList;
361const indextable *odentry;
362
363 odentry = scanIndexOD (d, wIndex, &errorCode, &CallbackList);
364 if(errorCode == OD_SUCCESSFUL && CallbackList && bSubindex < odentry->bSubCount)
365 CallbackList[bSubindex] = Callback;
366 return errorCode;
367}
368
376void _storeODSubIndex (CO_Data* d, UNS16 wIndex, UNS8 bSubindex){}
#define UNS8
Unsigned int8 representation in CANFest.
Definition applicfg.h:25
#define UNS16
Unsigned int16 representation in CANFest.
Definition applicfg.h:26
#define MSG_WAR(num, str, val)
Definition of MSG_WAR.
Definition applicfg.h:64
#define UNS32
Unsigned int32 representation in CANFest.
Definition applicfg.h:27
#define OD_SUCCESSFUL
Definition def.h:16
void _storeODSubIndex(CO_Data *d, UNS16 wIndex, UNS8 bSubindex)
<BRIEF>
Definition objacces.c:376
UNS32 RegisterSetODentryCallBack(CO_Data *d, UNS16 wIndex, UNS8 bSubindex, ODCallback_t Callback)
<BRIEF>
Definition objacces.c:357
const indextable * scanIndexOD(CO_Data *d, UNS16 wIndex, UNS32 *errorCode, ODCallback_t **Callback)
Scan the index of object dictionary. Used only by setODentry and getODentry.
Definition objacces.c:343
UNS32 _setODentry(CO_Data *d, UNS16 wIndex, UNS8 bSubindex, void *pSourceData, UNS32 *pExpectedSize, UNS8 checkAccess, UNS8 endianize)
By this function you can write an entry into the object dictionary.
Definition objacces.c:235
#define accessDictionaryError(index, subIndex, sizeDataDict, sizeDataGiven, code)
Print MSG_WAR (s) if error to the access to the object dictionary occurs.
Definition objacces.c:68
UNS32 _getODentry(CO_Data *d, UNS16 wIndex, UNS8 bSubindex, void *pDestData, UNS32 *pExpectedSize, UNS8 *pDataType, UNS8 checkAccess, UNS8 endianize)
_getODentry() Reads an entry from the object dictionary.
Definition objacces.c:111
This structure contains all necessary informations to define a CANOpen node.
Definition data.h:44