-
Notifications
You must be signed in to change notification settings - Fork 23
/
sbn_pack.c
130 lines (111 loc) · 3 KB
/
sbn_pack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "sbn_pack.h"
#include <string.h> /* memcpy */
bool Pack_Init(Pack_t *PackPtr, void *Buf, size_t BufSz, bool ClearFlag)
{
PackPtr->Buf = Buf;
PackPtr->BufSz = BufSz;
PackPtr->BufUsed = 0;
if (ClearFlag)
{
memset(Buf, 0, BufSz);
} /* end if */
return true;
} /* end Pack_Init() */
bool Pack_Data(Pack_t *PackPtr, void *DataBuf, size_t DataBufSz)
{
if (PackPtr->BufUsed + DataBufSz > PackPtr->BufSz)
{
/* print an error? */
return false;
}
memcpy((uint8 *)PackPtr->Buf + PackPtr->BufUsed, DataBuf, DataBufSz);
PackPtr->BufUsed += DataBufSz;
return true;
} /* end Pack_Data() */
bool Pack_UInt8(Pack_t *PackPtr, uint8 Data)
{
return Pack_Data(PackPtr, &Data, sizeof(Data));
} /* end Pack_UInt8() */
bool Pack_UInt16(Pack_t *PackPtr, uint16 Data)
{
uint16 D = CFE_MAKE_BIG16(Data);
return Pack_Data(PackPtr, &D, sizeof(D));
} /* end Pack_UInt16() */
bool Pack_Int16(Pack_t *PackPtr, int16 Data)
{
int16 D = CFE_MAKE_BIG16(Data);
return Pack_Data(PackPtr, &D, sizeof(D));
} /* end Pack_Int16() */
bool Pack_UInt32(Pack_t *PackPtr, uint32 Data)
{
uint32 D = CFE_MAKE_BIG32(Data);
return Pack_Data(PackPtr, &D, sizeof(D));
} /* end Pack_UInt32() */
bool Pack_Time(Pack_t *PackPtr, OS_time_t Data)
{
OS_time_t D;
OS_TimeAssembleFromMicroseconds(
CFE_MAKE_BIG32(OS_TimeGetTotalSeconds(Data)),
CFE_MAKE_BIG32(OS_TimeGetMicrosecondsPart(Data)));
return Pack_Data(PackPtr, &D, sizeof(D));
} /* end Pack_Time() */
bool Pack_MsgID(Pack_t *PackPtr, CFE_SB_MsgId_t Data)
{
uint32 D;
D = CFE_MAKE_BIG32(CFE_SB_MsgIdToValue(Data));
return Pack_Data(PackPtr, &D, sizeof(D));
} /* end Pack_MsgID() */
bool Unpack_Data(Pack_t *Pack, void *DataBuf, size_t Sz)
{
if (Pack->BufUsed + Sz > Pack->BufSz)
{
return false;
}
memcpy(DataBuf, (uint8 *)Pack->Buf + Pack->BufUsed, Sz);
Pack->BufUsed += Sz;
return true;
} /* end Unpack_Data() */
bool Unpack_UInt8(Pack_t *Pack, uint8 *DataBuf)
{
return Unpack_Data(Pack, DataBuf, sizeof(*DataBuf));
} /* end Unpack_UInt8() */
bool Unpack_UInt16(Pack_t *Pack, uint16 *DataBuf)
{
uint16 D;
if (!Unpack_Data(Pack, &D, sizeof(D)))
{
return false;
}
*DataBuf = CFE_MAKE_BIG16(D);
return true;
} /* end Unpack_UInt16() */
bool Unpack_Int16(Pack_t *Pack, int16 *DataBuf)
{
int16 D;
if (!Unpack_Data(Pack, &D, sizeof(D)))
{
return false;
}
*DataBuf = CFE_MAKE_BIG16(D);
return true;
} /* end Unpack_Int16() */
bool Unpack_UInt32(Pack_t *Pack, uint32 *DataBuf)
{
uint32 D;
if (!Unpack_Data(Pack, &D, sizeof(D)))
{
return false;
}
*DataBuf = CFE_MAKE_BIG32(D);
return true;
} /* end Unpack_UInt32() */
bool Unpack_MsgID(Pack_t *Pack, CFE_SB_MsgId_t *DataBuf)
{
uint32 D;
if (!Unpack_Data(Pack, &D, sizeof(D)))
{
return false;
}
*DataBuf = CFE_SB_ValueToMsgId(CFE_MAKE_BIG32(D));
return true;
} /* end Unpack_MsgID() */