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 | |
Harald Welte | c959afd | 2015-12-25 17:14:07 +0100 | [diff] [blame] | 33 | extern const struct value_string osmo_prim_op_names[5]; |
Harald Welte | a2db75f | 2015-12-22 22:11:27 +0100 | [diff] [blame] | 34 | |
Harald Welte | 7166094 | 2017-10-16 14:52:37 +0200 | [diff] [blame] | 35 | /*!< 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] | 36 | #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 Welte | a2db75f | 2015-12-22 22:11:27 +0100 | [diff] [blame] | 40 | #define _SAP_SS7_BASE (0x03 << _SAP_GSM_SHIFT) |
Harald Welte | 5e924a3 | 2011-06-23 15:04:47 +0200 | [diff] [blame] | 41 | |
Harald Welte | 7166094 | 2017-10-16 14:52:37 +0200 | [diff] [blame] | 42 | /*! Osmocom primitive header */ |
Harald Welte | 9b21e88 | 2011-06-23 14:14:20 +0200 | [diff] [blame] | 43 | struct osmo_prim_hdr { |
Harald Welte | 7166094 | 2017-10-16 14:52:37 +0200 | [diff] [blame] | 44 | unsigned int sap; /*!< Service Access Point Identifier */ |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 45 | unsigned int primitive; /*!< Primitive number */ |
| 46 | enum osmo_prim_operation operation; /*! Primitive Operation */ |
Harald Welte | 7166094 | 2017-10-16 14:52:37 +0200 | [diff] [blame] | 47 | 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 Welte | 9b21e88 | 2011-06-23 14:14:20 +0200 | [diff] [blame] | 54 | }; |
| 55 | |
Harald Welte | 7166094 | 2017-10-16 14:52:37 +0200 | [diff] [blame] | 56 | /*! Convenience function to initialize a primitive header |
Harald Welte | eee3790 | 2011-08-17 16:14:11 +0200 | [diff] [blame] | 57 | * \param[in,out] oph primitive header |
| 58 | * \param[in] sap Service Access Point |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 59 | * \param[in] primitive Primitive Number |
Harald Welte | eee3790 | 2011-08-17 16:14:11 +0200 | [diff] [blame] | 60 | * \param[in] operation Primitive Operation (REQ/RESP/IND/CONF) |
| 61 | * \param[in] msg Message |
| 62 | */ |
Harald Welte | 5e924a3 | 2011-06-23 15:04:47 +0200 | [diff] [blame] | 63 | static inline void |
| 64 | osmo_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 Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 74 | /*! primitive handler callback type */ |
Harald Welte | 5e924a3 | 2011-06-23 15:04:47 +0200 | [diff] [blame] | 75 | typedef int (*osmo_prim_cb)(struct osmo_prim_hdr *oph, void *ctx); |
Harald Welte | eee3790 | 2011-08-17 16:14:11 +0200 | [diff] [blame] | 76 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 77 | /*! magic value to be used as final record of \ref |
Harald Welte | acd08fe | 2017-04-08 23:35:24 +0200 | [diff] [blame] | 78 | * osmo_prim_event_map */ |
| 79 | #define OSMO_NO_EVENT 0xFFFFFFFF |
| 80 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 81 | /*! single entry in a SAP/PRIM/OP -> EVENT map */ |
Harald Welte | acd08fe | 2017-04-08 23:35:24 +0200 | [diff] [blame] | 82 | struct 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 | |
| 89 | uint32_t osmo_event_for_prim(const struct osmo_prim_hdr *oph, |
| 90 | const struct osmo_prim_event_map *maps); |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 91 | /*! @} */ |