blob: 99eabff049f10ae56069031bb64c49e409768e4f [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
Harald Weltec959afd2015-12-25 17:14:07 +010033extern const struct value_string osmo_prim_op_names[5];
Harald Weltea2db75f2015-12-22 22:11:27 +010034
Harald Welte71660942017-10-16 14:52:37 +020035/*!< The upper 8 byte of the technology, the lower 24 bits for the SAP */
Harald Welte5e924a32011-06-23 15:04:47 +020036#define _SAP_GSM_SHIFT 24
37
38#define _SAP_GSM_BASE (0x01 << _SAP_GSM_SHIFT)
39#define _SAP_TETRA_BASE (0x02 << _SAP_GSM_SHIFT)
Harald Weltea2db75f2015-12-22 22:11:27 +010040#define _SAP_SS7_BASE (0x03 << _SAP_GSM_SHIFT)
Harald Welte5e924a32011-06-23 15:04:47 +020041
Harald Welte71660942017-10-16 14:52:37 +020042/*! Osmocom primitive header */
Harald Welte9b21e882011-06-23 14:14:20 +020043struct osmo_prim_hdr {
Harald Welte71660942017-10-16 14:52:37 +020044 unsigned int sap; /*!< Service Access Point Identifier */
Neels Hofmeyr87e45502017-06-20 00:17:59 +020045 unsigned int primitive; /*!< Primitive number */
46 enum osmo_prim_operation operation; /*! Primitive Operation */
Harald Welte71660942017-10-16 14:52:37 +020047 struct msgb *msg; /*!< \ref msgb containing associated data.
48 * Note this can be slightly confusing, as the \ref osmo_prim_hdr
49 * is stored inside a \ref msgb, but then it contains a pointer
50 * back to the msgb. This is to simplify development: You can
51 * pass around a \ref osmo_prim_hdr by itself, and any function
52 * can autonomously resolve the underlying msgb, if needed (e.g.
53 * for \ref msgb_free. */
Harald Welte9b21e882011-06-23 14:14:20 +020054};
55
Harald Welte71660942017-10-16 14:52:37 +020056/*! Convenience function to initialize a primitive header
Harald Welteeee37902011-08-17 16:14:11 +020057 * \param[in,out] oph primitive header
58 * \param[in] sap Service Access Point
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +010059 * \param[in] primitive Primitive Number
Harald Welteeee37902011-08-17 16:14:11 +020060 * \param[in] operation Primitive Operation (REQ/RESP/IND/CONF)
61 * \param[in] msg Message
62 */
Harald Welte5e924a32011-06-23 15:04:47 +020063static inline void
64osmo_prim_init(struct osmo_prim_hdr *oph, unsigned int sap,
65 unsigned int primitive, enum osmo_prim_operation operation,
66 struct msgb *msg)
67{
68 oph->sap = sap;
69 oph->primitive = primitive;
70 oph->operation = operation;
71 oph->msg = msg;
72}
73
Neels Hofmeyr87e45502017-06-20 00:17:59 +020074/*! primitive handler callback type */
Harald Welte5e924a32011-06-23 15:04:47 +020075typedef int (*osmo_prim_cb)(struct osmo_prim_hdr *oph, void *ctx);
Harald Welteeee37902011-08-17 16:14:11 +020076
Neels Hofmeyr87e45502017-06-20 00:17:59 +020077/*! magic value to be used as final record of \ref
Harald Welteacd08fe2017-04-08 23:35:24 +020078 * osmo_prim_event_map */
79#define OSMO_NO_EVENT 0xFFFFFFFF
80
Neels Hofmeyr87e45502017-06-20 00:17:59 +020081/*! single entry in a SAP/PRIM/OP -> EVENT map */
Harald Welteacd08fe2017-04-08 23:35:24 +020082struct osmo_prim_event_map {
83 unsigned int sap; /*!< SAP to match */
84 unsigned int primitive; /*!< primtiive to match */
85 enum osmo_prim_operation operation; /*!< operation to match */
86 uint32_t event; /*!< event as result if above match */
87};
88
89uint32_t osmo_event_for_prim(const struct osmo_prim_hdr *oph,
90 const struct osmo_prim_event_map *maps);
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +010091/*! @} */