gpt4 book ai didi

c - 如何确定 AUTOSAR Runnable 中 PIM 的大小?

转载 作者:行者123 更新时间:2023-11-30 16:57:12 28 4
gpt4 key购买 nike

如何从 Runnable 内部确定 c 中 PIM(每实例内存)的大小(无需在生成的 RTE 中查找并添加固定值)?

情况:Runnable Foo 可以访问两个 PIM Pim1 和 Pim2。在示例中,Pim1 中的数据应复制到 Pim2。

不仅仅是因为安全性和安全性,我需要检查两个 PIM 的大小,以免覆盖非法数据区域。我知道PIM的大小是在SW-C描述(SWCD)中配置的。但由于 SWCD 在代码实现后可能会发生更改,并且为了使 Runnable 的代码更加通用,因此大小检查不应基于固定值。

我还考虑了数组的sizeof问题: How to find the 'sizeof'(a pointer pointing to an array)?

对于 PIM,RTE 生成器生成以下代码:

在Rte_Type.h中

typedef uint8 Rte_DT_DtImplRec1_0;
typedef uint16 Rte_DT_DtImplRec1_1;

typedef struct
{
Rte_DT_DtImplRec1_0 var1;
Rte_DT_DtImplRec1_1 var2;
Rte_DT_DtImplRec1_2 var3;
} DtImplRec1;

typedef uint8 Rte_DT_DtImplAry1_0;
typedef Rte_DT_DtImplAry1_0 DtImplAry1[5];

在 Rte.c

VAR(DtImplRec1, RTE_VAR_DEFAULT_RTE_PIM_GROUP) Rte_FOO_Pim1;
VAR(DtImplAry1, RTE_VAR_DEFAULT_RTE_PIM_GROUP) Rte_FOO_Pim2;

在 Rte_FOO.h 中

#define Rte_Pim_Pim1() (&Rte_FOO_Pim1)

#ifdef RTE_PTR2ARRAYBASETYPE_PASSING
# define Rte_Pim_Pim2() (&((*RtePim_Pim2())[0]))
#else
# define Rte_Pim_Pim2() RtePim_Pim2()
#endif

#define RtePim_Pim2() (&Rte_FOO_Pim2)

请注意,阵列 PIM 的定义也可能会发生变化,具体取决于 RTE_PTR2ARRAYBASETYPE_PASSING“开关”。

为 FOO 模板生成以下“访问权限”:

DtImplRec1 *Rte_Pim_Pim1(void);
Rte_DT_DtImplAry1_0 *Rte_Pim_Pim2(void)

Foo-Runnable 的代码可能如下所示:

FUNC(void, FOO_CODE) Foo(void)
{
DtImplRec1 *pim1 = Rte_Pim_Pim1();
Rte_DT_DtImplAry1_0 *pim2 = Rte_Pim_Pim2();

uint8 sizeOfPim1a = sizeof(Rte_Pim_Pim1()); /* always returns 4 as the size of the pointer */
uint8 sizeOfPim1b = sizeof(*Rte_Pim_Pim1()); /* evaluates to 6 */
uint8 sizeOfPim1c = sizeof(DtImplRec1); /* evaluates to 6 */
uint8 sizeOfPim1d = sizeof(Rte_FOO_Pim1); /* evaluates to 6 */

uint8 sizeOfPim2a = sizeof(Rte_Pim_Pim2()); /* always returns 4 as the size of the pointer */
uint8 sizeOfPim2b = sizeof(*Rte_Pim_Pim2()); /* evaluates to 1 */
uint8 sizeOfPim2c = sizeof(Rte_DT_DtImplAry1_0); /* evaluates to 1: sizeof(uint8) */

uint8 finalSize = MIN(sizeOfPim1b, sizeOfPim2b);

memcpy( pim2, pim1, finalSize ); /* (use of) memcpy is not the topic here */
}

为了让我的问题更加“明显”,这里是一个通过诊断编写 DID 的 Callback-Runnable 示例:

FUNC(Std_ReturnType, FOO_CODE)
DataServices_Data_FFFF_WriteData(P2CONST(uint8, AUTOMATIC, RTE_APPL_DATA) Data, Dcm_OpStatusType OpStatus, P2VAR(Dcm_NegativeResponseCodeType, AUTOMATIC, RTE_APPL_DATA) ErrorCode)
{
Std_ReturnType ret = E_NOT_OK;

#define sizeOfPim1 (5) /* how to determine the PIM size here if we do not know anything about it here? (PIM structure can change without modifying the code here) */
#define sizeOfDidFFFF (5) /* This is even another problem: How to determine the size of a DID. I will create another discussion thread for this question. */

/* Instead of this if-condition, an assert during compile-time would also be appropriate */
if( sizeOfPim1 == sizeOfDidFFFF )
{
/* We have to make sure that we do not copy more bytes as of the size of Pim1 */
memcpy( Rte_Pim_Pim1(), Data, sizeOfPim1 ); /* (use of) memcpy is not the topic here */
ret = E_OK;
}

return ret;
}

最佳答案

我这里没有任何 AUTOSAR 环境来测试这个,所以,如果您尝试其中任何一个,请告诉我它是否有效。此外,我不是专家,而且我已经很长时间没有编写 AUTOSAR 代码了,所以我可能会遗漏一些东西。我也不想公开任何供应商的任何 RTE 生成器,因此我只会引用标准。

使用sizeof(DtImplAry1)

您定义该类型并将其作为 RTE 生成器的输入,因此您知道该名称。如果您的 SWC 没有显式使用该类型,则 RTE 生成器无法将其包含在您的 .h 中,但您可以手动将其添加到 SWC arxml 中。我认为所有工具都允许执行此操作,而无需手动编辑 arxml,只需寻找在工具中包含其他 SWC 类型的选项即可。

使用Instance API访问SWC数据

如果您启用 API(在您的工具中查找它),该标准指定一个 Rte_CDS_FOO 类型的变量来保存指向 SWC 的 PIM(以及其他内容)的所有指针。此外,您应该可以使用变量Rte_Inst_FOO,并在 header 中将其声明为extern。您可以执行sizeof(*Rte_Inst_FOO->Pim_Pim2)

编辑:回复您的一些评论

我猜您找不到 CDS 的原因是这样的(来自 RTE 规范,4.2.2,5.4 RTE 数据结构):

The [CDS and Instance handler] definitions only apply to RTE generators operating in compatibility mode – in this mode the instance handle and the component data structure have to be defined even for those (object-code) software components for which multiple instantiation is forbidden to ensure compatibility.

另外,

[SWS_Rte_03793] If a software component does not support multiple instantiation,the name of the component data instance shall be Rte_Inst_cts, where cts is the component type symbol of the AtomicSwComponentType. (SRS_Rte_00011)

因此,当 RTE 生成器遵循此兼容模式时,这些变量必须存在。如果您使用的是特定于供应商的解决方案,那么,请尝试也使用该供应商名称来标记问题,希望有人可以回答。

编译时断言

我不会问你为什么要这样做,但恕我直言,我认为这听起来不对,接收缓冲区小于要复制的数据是否有意义?如果缓冲区小于您的struct,也许最好在编译时断言。或者,您可以将数组定义为结构体,并在需要时对其进行强制转换(如果您遵循 MISRA 规则,也许您会遇到问题,只需检查一下)。仅供引用,compile time assertions can use sizeof .

关于c - 如何确定 AUTOSAR Runnable 中 PIM 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39661464/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com