Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 1 | #pragma once |
Harald Welte | 9b21e88 | 2011-06-23 14:14:20 +0200 | [diff] [blame] | 2 | |
Harald Welte | eee3790 | 2011-08-17 16:14:11 +0200 | [diff] [blame] | 3 | /*! \defgroup prim Osmocom primitives |
| 4 | * @{ |
Harald Welte | 7166094 | 2017-10-16 14:52:37 +0200 | [diff] [blame] | 5 | * |
| 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 Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 17 | * \file prim.h */ |
Harald Welte | eee3790 | 2011-08-17 16:14:11 +0200 | [diff] [blame] | 18 | |
Harald Welte | 9b21e88 | 2011-06-23 14:14:20 +0200 | [diff] [blame] | 19 | #include <stdint.h> |
Harald Welte | 5e924a3 | 2011-06-23 15:04:47 +0200 | [diff] [blame] | 20 | #include <osmocom/core/msgb.h> |
Harald Welte | 9b21e88 | 2011-06-23 14:14:20 +0200 | [diff] [blame] | 21 | |
Andreas Eversberg | 78122ab | 2011-09-27 12:06:55 +0200 | [diff] [blame] | 22 | #define OSMO_PRIM(prim, op) ((prim << 8) | (op & 0xFF)) |
| 23 | #define OSMO_PRIM_HDR(oph) OSMO_PRIM((oph)->primitive, (oph)->operation) |
| 24 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 25 | /*! primitive operation */ |
Harald Welte | 9b21e88 | 2011-06-23 14:14:20 +0200 | [diff] [blame] | 26 | enum osmo_prim_operation { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 27 | PRIM_OP_REQUEST, /*!< request */ |
| 28 | PRIM_OP_RESPONSE, /*!< response */ |
| 29 | PRIM_OP_INDICATION, /*!< indication */ |
| 30 | PRIM_OP_CONFIRM, /*!< confirm */ |
Harald Welte | 9b21e88 | 2011-06-23 14:14:20 +0200 | [diff] [blame] | 31 | }; |
| 32 | |
Neels Hofmeyr | ba1e200 | 2023-02-21 04:59:37 +0100 | [diff] [blame] | 33 | extern const struct value_string osmo_prim_op_names[]; |
| 34 | static 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 Welte | a2db75f | 2015-12-22 22:11:27 +0100 | [diff] [blame] | 38 | |
Harald Welte | 7166094 | 2017-10-16 14:52:37 +0200 | [diff] [blame] | 39 | /*!< The upper 8 byte of the technology, the lower 24 bits for the SAP */ |
Harald Welte | 5e924a3 | 2011-06-23 15:04:47 +0200 | [diff] [blame] | 40 | #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 Welte | a2db75f | 2015-12-22 22:11:27 +0100 | [diff] [blame] | 44 | #define _SAP_SS7_BASE (0x03 << _SAP_GSM_SHIFT) |
Harald Welte | 5e924a3 | 2011-06-23 15:04:47 +0200 | [diff] [blame] | 45 | |
Harald Welte | 7166094 | 2017-10-16 14:52:37 +0200 | [diff] [blame] | 46 | /*! Osmocom primitive header */ |
Harald Welte | 9b21e88 | 2011-06-23 14:14:20 +0200 | [diff] [blame] | 47 | struct osmo_prim_hdr { |
Harald Welte | 7166094 | 2017-10-16 14:52:37 +0200 | [diff] [blame] | 48 | unsigned int sap; /*!< Service Access Point Identifier */ |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 49 | unsigned int primitive; /*!< Primitive number */ |
| 50 | enum osmo_prim_operation operation; /*! Primitive Operation */ |
Harald Welte | 7166094 | 2017-10-16 14:52:37 +0200 | [diff] [blame] | 51 | 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 Welte | 9b21e88 | 2011-06-23 14:14:20 +0200 | [diff] [blame] | 58 | }; |
| 59 | |
Harald Welte | 7166094 | 2017-10-16 14:52:37 +0200 | [diff] [blame] | 60 | /*! Convenience function to initialize a primitive header |
Harald Welte | eee3790 | 2011-08-17 16:14:11 +0200 | [diff] [blame] | 61 | * \param[in,out] oph primitive header |
| 62 | * \param[in] sap Service Access Point |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 63 | * \param[in] primitive Primitive Number |
Harald Welte | eee3790 | 2011-08-17 16:14:11 +0200 | [diff] [blame] | 64 | * \param[in] operation Primitive Operation (REQ/RESP/IND/CONF) |
| 65 | * \param[in] msg Message |
| 66 | */ |
Harald Welte | 5e924a3 | 2011-06-23 15:04:47 +0200 | [diff] [blame] | 67 | static inline void |
| 68 | osmo_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 Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 78 | /*! primitive handler callback type */ |
Harald Welte | 5e924a3 | 2011-06-23 15:04:47 +0200 | [diff] [blame] | 79 | typedef int (*osmo_prim_cb)(struct osmo_prim_hdr *oph, void *ctx); |
Harald Welte | eee3790 | 2011-08-17 16:14:11 +0200 | [diff] [blame] | 80 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 81 | /*! magic value to be used as final record of \ref |
Harald Welte | acd08fe | 2017-04-08 23:35:24 +0200 | [diff] [blame] | 82 | * osmo_prim_event_map */ |
| 83 | #define OSMO_NO_EVENT 0xFFFFFFFF |
| 84 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 85 | /*! single entry in a SAP/PRIM/OP -> EVENT map */ |
Harald Welte | acd08fe | 2017-04-08 23:35:24 +0200 | [diff] [blame] | 86 | struct 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 | |
| 93 | uint32_t osmo_event_for_prim(const struct osmo_prim_hdr *oph, |
| 94 | const struct osmo_prim_event_map *maps); |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 95 | /*! @} */ |