~sysman-one/starlet/main

« back to all changes in this revision

Viewing changes to avproto.h

  • Committer: Ruslan (The BadAss SysMan) Laishev
  • Date: 2023-08-31 09:10:48 UTC
  • Revision ID: git-v1:4a9fdc08f49897cf58d205cc4663d7e82e0bf2da
[+] Added API for TLV/AV

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef __AVPROTO$DEF__
 
2
#define __AVPROTO$DEF__ 1
 
3
 
 
4
#ifdef  __GNUC__
 
5
        #pragma GCC diagnostic ignored  "-Wparentheses"
 
6
        #pragma GCC diagnostic ignored  "-Wdate-time"
 
7
        #pragma GCC diagnostic ignored  "-Wunused-variable"
 
8
#endif
 
9
 
 
10
 
 
11
#ifdef _WIN32
 
12
        #pragma once
 
13
        #define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
 
14
#endif
 
15
 
 
16
 
 
17
/*
 
18
**++
 
19
**
 
20
**  FACILITY:  AVProto - Attribute/Value Pair (AKA TLV) based protocol
 
21
**
 
22
**  ABSTRACT: Carring data incapsulated in to the AVP microcontainers
 
23
**
 
24
**  DESCRIPTION: This module contains data structures and constant definition is supposed to be used
 
25
**      in the core AVProto routines as well as in general API routines.
 
26
**
 
27
**  DESIGN ISSUE:
 
28
**
 
29
**      A main idea is imaginated follows :
 
30
**
 
31
        PDU (protocol/packet data unit ) - is a block is supposed to be send to remote peer over network
 
32
 
 
33
                 |<- Payload (see Header.len field)-->|
 
34
        +--------+-------+-------+--    ...  ----+-------+
 
35
        | Header | TLV 0 | TLV 1 |          .... | TLV N |
 
36
        +--------+-------+-------+----  ...    --+-------+
 
37
 
 
38
        PDU's Header :
 
39
        +-----------+-----------+-----------+-----------+
 
40
        | magic     |  len      |  csr      |   seq     |
 
41
        | 8 octets  |  4 octets |  4 octets |   4 octets
 
42
        +-----------+-----------+-----------+-----------+
 
43
 
 
44
        T[ag] L[ength] V[alue] :
 
45
        0                    16                  32
 
46
        +---------+-----------+-------------------+
 
47
        | Type    |   Id      |      Length       |
 
48
        | 4 bits  |   12 bits |      16 bits      |
 
49
        +---------+-----------+-------------------+
 
50
        |    optional data (value)                |
 
51
        |      0 - 'Length' octets                |
 
52
        +---------+-----------+-------------------+
 
53
**
 
54
**  AUTHORS: Ruslan R. Laishev (RRL)
 
55
**
 
56
**  CREATION DATE:  20-JUL-2018
 
57
**
 
58
**  MODIFICATION HISTORY:
 
59
**
 
60
**      15-FEB-2019     RRL     Removed bitfields.
 
61
**
 
62
**      22-FEB-2019     RRL     Added closing for "#ifdef __cplusplus"
 
63
**                              some code reorganizing;
 
64
**
 
65
**      29-JUL-2019     RRL     Added declaration for avproto_dump();
 
66
**                              improved error handling in the avproto_hget();
 
67
**
 
68
**
 
69
**--
 
70
*/
 
71
 
 
72
#include        <endian.h>
 
73
 
 
74
 
 
75
#ifdef __cplusplus
 
76
extern "C" {
 
77
#endif
 
78
 
 
79
 
 
80
 
 
81
/*
 
82
 * AVP/TLV - type of block types to help transparent conversion of the well-know datas
 
83
 * is carried in NBO or to help LE/Be conversion
 
84
 */
 
85
enum    {
 
86
        TAG$K_BBLOCK = 0,       /* A default type - octets block        */
 
87
        TAG$K_WORD = 3,         /* 16-bits word, NBO                    */
 
88
        TAG$K_LONGWORD,         /* 32-bits word, NBO                    */
 
89
        TAG$K_QWORD,            /* 64-bits word, NBO                    */
 
90
        TAG$K_UUID = 8,         /* UUID                                 */
 
91
 
 
92
 
 
93
        TAG$K_MAX               /* EOL marker, must be last!            */
 
94
};
 
95
 
 
96
 
 
97
#pragma pack (push, 1)
 
98
 
 
99
#define TAG$M_TYPE      0xf000
 
100
#define TAG$M_ID        (~(TAG$M_TYPE))
 
101
 
 
102
typedef struct __avproto_tlv__ {
 
103
 
 
104
        unsigned short  w_tag;  /* Tag's type and id                    */
 
105
        unsigned short  w_len;  /* Actual data length in the b_val[]    */
 
106
 
 
107
        union   {               /* Placeholder for data block           */
 
108
                unsigned char   b_val[0];
 
109
                unsigned short  w_val[0];
 
110
                unsigned        u_val[0];
 
111
        unsigned long long      q_val[0];
 
112
        };
 
113
} AVPROTO_TLV;
 
114
 
 
115
typedef struct __avproto_hdr__ {
 
116
        unsigned char   magic[8];
 
117
        unsigned        u_len;          /* A length of payload follows header   */
 
118
        unsigned        u_csr;          /* Command/Status Register              */
 
119
 
 
120
        unsigned        u_seq;          /* A PDU sequence number is supposed to help
 
121
                                          parallel and asyncronus processing    */
 
122
 
 
123
} AVPROTO_HDR;
 
124
 
 
125
typedef struct __avproto_pdu__ {
 
126
        AVPROTO_HDR     r_hdr;          /* Fixed length header                  */
 
127
        AVPROTO_TLV     r_tlv[0];       /* Placeholder for variable part of PDU */
 
128
} AVPROTO_PDU;
 
129
 
 
130
#pragma pack (pop)
 
131
 
 
132
/**
 
133
 * @brief avproto_encode_tag - encapsulate given 'v_type' and  'v_tag' into the 16-bits field.
 
134
 *      return NBO representation of the w_tag;
 
135
 *
 
136
 * @param v_type        - Tag type, see TAG$K_* constatnts
 
137
 * @param v_tag         - Tasg Id, application specific Tag Id
 
138
 *
 
139
 * @return      - 16 bits TLV.w_tag field, NBO
 
140
 */
 
141
inline  static unsigned short avproto_encode_tag
 
142
                (
 
143
        unsigned short  v_type,
 
144
        unsigned short  v_tag
 
145
                )
 
146
{
 
147
        return  htobe16 ( ( (v_type << 12) | v_tag ));
 
148
}
 
149
 
 
150
/**
 
151
 * @brief avproto_decode_tag - extract 'tag id' and 'tag type' field from the TLV.w_tag.
 
152
 *      Return w_gat in the Host Order Byte
 
153
 *
 
154
 * @param w_tag         - TLV.w_tag field, NBO
 
155
 * @param v_type        - an address of variable to accept unpacked 'tag type' field
 
156
 * @param v_tag         - an address of variable to accept unpacked 'tag id field
 
157
 *
 
158
 * @return      - 16 bits, TLV_w_tag in Host Order Byte
 
159
 */
 
160
inline static unsigned short    avproto_decode_tag
 
161
                (
 
162
        unsigned short  w_tag,
 
163
        unsigned short  *v_type,
 
164
        unsigned short  *v_tag
 
165
                )
 
166
{
 
167
unsigned short tag = 0;
 
168
 
 
169
        tag = be16toh(w_tag);
 
170
 
 
171
        *v_type = tag >> 12;
 
172
        *v_tag = tag & TAG$M_ID;
 
173
 
 
174
        return  tag;
 
175
}
 
176
 
 
177
 
 
178
int     avproto_put  (void *pdu, unsigned short pdusz, unsigned v_tag, unsigned v_type, void *val, unsigned valsz);
 
179
int     avproto_get  (void *pdu, int *context, unsigned v_tag, unsigned *v_type, void *val, unsigned *valsz);
 
180
void    avproto_dump (void *pdu);
 
181
 
 
182
 
 
183
/*
 
184
 *  DESCRIPTION: Inialize given PDU's header with given values, reset length of the PDU
 
185
 *      to initial.
 
186
 *
 
187
 *  INPUTS:
 
188
 *      sig:    A signature string, ASCIZ
 
189
 *      pdu:    A Prototocol Data Unit
 
190
 *      u_csr:  A command/status register field
 
191
 *      u_seq:  PDU's sequence field
 
192
 *
 
193
 *  OUTPUTS:
 
194
 *      NONE
 
195
 *
 
196
 *   RETURNS:
 
197
 *      condition code
 
198
 */
 
199
inline static int       avproto_hset    (
 
200
        void            *pdup,
 
201
        unsigned char   *sig,
 
202
        unsigned        u_csr,
 
203
        unsigned        u_seq
 
204
                        )
 
205
{
 
206
AVPROTO_PDU     *pdu = (AVPROTO_PDU *) pdup;
 
207
 
 
208
        memset(&pdu->r_hdr, 0, sizeof(AVPROTO_HDR));
 
209
        strncpy(pdu->r_hdr.magic, sig, sizeof(pdu->r_hdr.magic));
 
210
 
 
211
        pdu->r_hdr.u_csr = htobe32(u_csr);
 
212
        pdu->r_hdr.u_seq = htobe32(u_seq);
 
213
 
 
214
        return  STS$K_SUCCESS;
 
215
}
 
216
 
 
217
 
 
218
/*
 
219
 *  DESCRIPTION: Get information from the PDU's header.
 
220
 *
 
221
 *  INPUTS:
 
222
 *      pdu:    A Prototocol Data Unit
 
223
 *      sig:    A signature string, ASCIZ
 
224
 *
 
225
 *  OUTPUTS:
 
226
 *      u_len:  A length of the PDU's payload
 
227
 *      u_csr:  A command/status register field
 
228
 *      u_seq:  PDU's sequence field
 
229
 *
 
230
 *   RETURNS:
 
231
 *      condition code
 
232
 */
 
233
inline static int       avproto_hget    (
 
234
        void            *pdup,
 
235
        unsigned char   *sig,
 
236
        unsigned        *u_len,
 
237
        unsigned        *u_csr,
 
238
        unsigned        *u_seq
 
239
                        )
 
240
{
 
241
int     len;
 
242
AVPROTO_PDU     *pdu = (AVPROTO_PDU *) pdup;
 
243
 
 
244
        *u_csr = STS$K_ERROR;
 
245
 
 
246
        if ( (len = strlen(sig)) > (sizeof (pdu->r_hdr.magic)) )
 
247
                return  STS$K_ERROR;
 
248
 
 
249
        if ( strncmp(pdu->r_hdr.magic, sig, sizeof (pdu->r_hdr.magic)) )
 
250
                return  STS$K_ERROR;
 
251
 
 
252
        *u_len = be32toh(pdu->r_hdr.u_len);
 
253
        *u_csr = be32toh(pdu->r_hdr.u_csr);
 
254
        *u_seq = be32toh(pdu->r_hdr.u_seq);
 
255
 
 
256
        return  STS$K_SUCCESS;
 
257
}
 
258
 
 
259
 
 
260
#ifdef __cplusplus
 
261
        }
 
262
#endif
 
263
 
 
264
#endif  /* #ifndef      __AVPROTO$DEF__ */