blob: 8e6b436da1f89752f41a22b6a23ddb3f1413e183 [file] [log] [blame]
Sylvain Munaut12ba7782014-06-16 10:13:40 +02001#pragma once
Harald Welte9b21e882011-06-23 14:14:20 +02002
Harald Welteeee37902011-08-17 16:14:11 +02003/*! \defgroup prim Osmocom primitives
4 * @{
Harald Welte71660942017-10-16 14:52:37 +02005 *
6 * Osmocom Primitives are a method to express inter-layer primitives as
7 * used often in ITU/ETSI/3GPP specifications in a generic way. They
8 * are based on \ref msgb and encapsulate any (optional) user payload
9 * data with a primitive header. The header contains information on
10 * - which SAP this primitive is used on
11 * - what is the name of the primitive
12 * - is it REQUEST, RESPONSE, INDICATION or CONFIRMATION
13 *
14 * For more information on the inter-layer primitives concept, see
15 * ITU-T X.21@ as found at https://www.itu.int/rec/T-REC-X.212-199511-I/en
16 *
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020017 * \file prim.h */
Harald Welteeee37902011-08-17 16:14:11 +020018
Harald Welte9b21e882011-06-23 14:14:20 +020019#include <stdint.h>
Harald Welte5e924a32011-06-23 15:04:47 +020020#include <osmocom/core/msgb.h>
Harald Welte9b21e882011-06-23 14:14:20 +020021
Andreas Eversberg78122ab2011-09-27 12:06:55 +020022#define OSMO_PRIM(prim, op) ((prim << 8) | (op & 0xFF))
23#define OSMO_PRIM_HDR(oph) OSMO_PRIM((oph)->primitive, (oph)->operation)
24
Neels Hofmeyr87e45502017-06-20 00:17:59 +020025/*! primitive operation */
Harald Welte9b21e882011-06-23 14:14:20 +020026enum osmo_prim_operation {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020027 PRIM_OP_REQUEST, /*!< request */
28 PRIM_OP_RESPONSE, /*!< response */
29 PRIM_OP_INDICATION, /*!< indication */
30 PRIM_OP_CONFIRM, /*!< confirm */
Harald Welte9b21e882011-06-23 14:14:20 +020031};
32
Neels Hofmeyrba1e2002023-02-21 04:59:37 +010033extern const struct value_string osmo_prim_op_names[];
34static inline const char *osmo_prim_operation_name(enum osmo_prim_operation val)
35{
36 return get_value_string(osmo_prim_op_names, val);
37}
Harald Weltea2db75f2015-12-22 22:11:27 +010038
Harald Welte71660942017-10-16 14:52:37 +020039/*!< The upper 8 byte of the technology, the lower 24 bits for the SAP */
Harald Welte5e924a32011-06-23 15:04:47 +020040#define _SAP_GSM_SHIFT 24
41
42#define _SAP_GSM_BASE (0x01 << _SAP_GSM_SHIFT)
43#define _SAP_TETRA_BASE (0x02 << _SAP_GSM_SHIFT)
Harald Weltea2db75f2015-12-22 22:11:27 +010044#define _SAP_SS7_BASE (0x03 << _SAP_GSM_SHIFT)
Harald Welte5e924a32011-06-23 15:04:47 +020045
Harald Welte71660942017-10-16 14:52:37 +020046/*! Osmocom primitive header */
Harald Welte9b21e882011-06-23 14:14:20 +020047struct osmo_prim_hdr {
Harald Welte71660942017-10-16 14:52:37 +020048 unsigned int sap; /*!< Service Access Point Identifier */
Neels Hofmeyr87e45502017-06-20 00:17:59 +020049 unsigned int primitive; /*!< Primitive number */
50 enum osmo_prim_operation operation; /*! Primitive Operation */
Harald Welte71660942017-10-16 14:52:37 +020051 struct msgb *msg; /*!< \ref msgb containing associated data.
52 * Note this can be slightly confusing, as the \ref osmo_prim_hdr
53 * is stored inside a \ref msgb, but then it contains a pointer
54 * back to the msgb. This is to simplify development: You can
55 * pass around a \ref osmo_prim_hdr by itself, and any function
56 * can autonomously resolve the underlying msgb, if needed (e.g.
57 * for \ref msgb_free. */
Harald Welte9b21e882011-06-23 14:14:20 +020058};
59
Harald Welte71660942017-10-16 14:52:37 +020060/*! Convenience function to initialize a primitive header
Harald Welteeee37902011-08-17 16:14:11 +020061 * \param[in,out] oph primitive header
62 * \param[in] sap Service Access Point
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +010063 * \param[in] primitive Primitive Number
Harald Welteeee37902011-08-17 16:14:11 +020064 * \param[in] operation Primitive Operation (REQ/RESP/IND/CONF)
65 * \param[in] msg Message
66 */
Harald Welte5e924a32011-06-23 15:04:47 +020067static inline void
68osmo_prim_init(struct osmo_prim_hdr *oph, unsigned int sap,
69 unsigned int primitive, enum osmo_prim_operation operation,
70 struct msgb *msg)
71{
72 oph->sap = sap;
73 oph->primitive = primitive;
74 oph->operation = operation;
75 oph->msg = msg;
76}
77
Neels Hofmeyr87e45502017-06-20 00:17:59 +020078/*! primitive handler callback type */
Harald Welte5e924a32011-06-23 15:04:47 +020079typedef int (*osmo_prim_cb)(struct osmo_prim_hdr *oph, void *ctx);
Harald Welteeee37902011-08-17 16:14:11 +020080
Neels Hofmeyr87e45502017-06-20 00:17:59 +020081/*! magic value to be used as final record of \ref
Harald Welteacd08fe2017-04-08 23:35:24 +020082 * osmo_prim_event_map */
83#define OSMO_NO_EVENT 0xFFFFFFFF
84
Neels Hofmeyr87e45502017-06-20 00:17:59 +020085/*! single entry in a SAP/PRIM/OP -> EVENT map */
Harald Welteacd08fe2017-04-08 23:35:24 +020086struct osmo_prim_event_map {
87 unsigned int sap; /*!< SAP to match */
88 unsigned int primitive; /*!< primtiive to match */
89 enum osmo_prim_operation operation; /*!< operation to match */
90 uint32_t event; /*!< event as result if above match */
91};
92
93uint32_t osmo_event_for_prim(const struct osmo_prim_hdr *oph,
94 const struct osmo_prim_event_map *maps);
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +010095/*! @} */