Max has uploaded this change for review. ( https://gerrit.osmocom.org/12573
Change subject: Add function to combine nibbles into byte ...................................................................... Add function to combine nibbles into byte It's a very common patter throughout GSM 04.08 and other protocols where single byte store 2 different values occupying 1 nibble each. Change-Id: I380687c31a787c0a109819f7c3b2946eae52675e --- M include/osmocom/core/bits.h M include/osmocom/gsm/gsm0808_utils.h M include/osmocom/gsm/gsm48.h M include/osmocom/gsm/protocol/gsm_03_41.h M src/gsm/gsm0808.c M src/gsm/gsm23003.c M src/gsm/gsm48.c M tests/utils/utils_test.c 8 files changed, 21 insertions(+), 9 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/73/12573/1 diff --git a/include/osmocom/core/bits.h b/include/osmocom/core/bits.h index b1b8040..8027bdd 100644 --- a/include/osmocom/core/bits.h +++ b/include/osmocom/core/bits.h @@ -47,6 +47,16 @@ int osmo_pbit2ubit(ubit_t *out, const pbit_t *in, unsigned int num_bits); +/*! Nibble combination. + * \param[in] lo Value to be placed in LSB nibble + * \param[in] hi Value to be placed in LSB nibble + * \returns combined nibble value + */ +static inline uint8_t osmo_nibble(uint8_t lo, uint8_t hi) +{ + return lo | (hi << 4); +} + void osmo_nibble_shift_right(uint8_t *out, const uint8_t *in, unsigned int num_nibbles); void osmo_nibble_shift_left_unal(uint8_t *out, const uint8_t *in, diff --git a/include/osmocom/gsm/gsm0808_utils.h b/include/osmocom/gsm/gsm0808_utils.h index 4a2233e..4cc917b 100644 --- a/include/osmocom/gsm/gsm0808_utils.h +++ b/include/osmocom/gsm/gsm0808_utils.h @@ -237,7 +237,7 @@ return 0; } - return channel_mode << 4 | channel; + return osmo_nibble(channel, channel_mode); } const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct); diff --git a/include/osmocom/gsm/gsm48.h b/include/osmocom/gsm/gsm48.h index 0f5727a..69b2eeb 100644 --- a/include/osmocom/gsm/gsm48.h +++ b/include/osmocom/gsm/gsm48.h @@ -71,4 +71,4 @@ uint8_t pdisc, uint8_t msg_type); #define gsm48_push_l3hdr_tid(msg, pdisc, tid, msg_type) \ - gsm48_push_l3hdr(msg, (pdisc & 0x0f) | (tid << 4), msg_type) + gsm48_push_l3hdr(msg, osmo_nibble(pdisc & 0x0f, tid), msg_type) diff --git a/include/osmocom/gsm/protocol/gsm_03_41.h b/include/osmocom/gsm/protocol/gsm_03_41.h index 1b399ae..cd84fd4 100644 --- a/include/osmocom/gsm/protocol/gsm_03_41.h +++ b/include/osmocom/gsm/protocol/gsm_03_41.h @@ -5,6 +5,7 @@ #include <stdint.h> +#include <osmocom/core/bits.h> #include <osmocom/core/endian.h> #include <osmocom/gsm/protocol/gsm_04_12.h> @@ -73,7 +74,7 @@ uint8_t data[0]; } __attribute__((packed)); -#define GSM341_MSG_CODE(ms) ((ms)->serial.code_lo | ((ms)->serial.code_hi << 4)) +#define GSM341_MSG_CODE(ms) osmo_nibble((ms)->serial.code_lo, (ms)->serial.code_hi) /* Section 9.3.2.1 - Geographical Scope */ #define GSM341_GS_CELL_WIDE_IMMED 0 diff --git a/src/gsm/gsm0808.c b/src/gsm/gsm0808.c index 59b1657..2254630 100644 --- a/src/gsm/gsm0808.c +++ b/src/gsm/gsm0808.c @@ -293,7 +293,7 @@ return NULL; /* Set cause code class in the upper byte */ - cause = 0x80 | (class << 4); + cause = osmo_nibble(0x80, class); cause = cause << 8; /* Set cause code extension in the lower byte */ diff --git a/src/gsm/gsm23003.c b/src/gsm/gsm23003.c index 1d9cefe..eaf0fb4 100644 --- a/src/gsm/gsm23003.c +++ b/src/gsm/gsm23003.c @@ -32,6 +32,7 @@ #include <osmocom/gsm/gsm23003.h> #include <osmocom/gsm/protocol/gsm_23_003.h> #include <osmocom/core/utils.h> +#include <osmocom/core/bits.h> static bool is_n_digits(const char *str, int min_digits, int max_digits) { @@ -218,10 +219,10 @@ to_bcd(bcd, plmn->mnc); if (plmn->mnc > 99 || plmn->mnc_3_digits) { bcd_dst[1] |= bcd[2] << 4; - bcd_dst[2] = bcd[0] | (bcd[1] << 4); + bcd_dst[2] = osmo_nibble(bcd[0], bcd[1]); } else { bcd_dst[1] |= 0xf << 4; - bcd_dst[2] = bcd[1] | (bcd[2] << 4); + bcd_dst[2] = osmo_nibble(bcd[1], bcd[2]); } } diff --git a/src/gsm/gsm48.c b/src/gsm/gsm48.c index 795e98b..e5d1592 100644 --- a/src/gsm/gsm48.c +++ b/src/gsm/gsm48.c @@ -659,7 +659,7 @@ else upper = osmo_char2bcd(id[++off]) & 0x0f; - buf[2 + i] = (upper << 4) | lower; + buf[2 + i] = osmo_nibble(lower, upper); } return 2 + buf[1]; diff --git a/tests/utils/utils_test.c b/tests/utils/utils_test.c index a773b3f..b677a5f 100644 --- a/tests/utils/utils_test.c +++ b/tests/utils/utils_test.c @@ -510,7 +510,7 @@ in_buf[16] = '\0'; for (j = 0; j < 16; j++) { for (i = 0; i < 16; i++) - in_buf[i] = (j << 4) | i; + in_buf[i] = osmo_nibble(i, j); printf("\"%s\"\n", osmo_escape_str((const char*)in_buf, 16)); } @@ -558,7 +558,7 @@ in_buf[16] = '\0'; for (j = 0; j < 16; j++) { for (i = 0; i < 16; i++) - in_buf[i] = (j << 4) | i; + in_buf[i] = osmo_nibble(i, j); printf("'%s'\n", osmo_quote_str((const char*)in_buf, 16)); } -- To view, visit https://gerrit.osmocom.org/12573 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-MessageType: newchange Gerrit-Change-Id: I380687c31a787c0a109819f7c3b2946eae52675e Gerrit-Change-Number: 12573 Gerrit-PatchSet: 1 Gerrit-Owner: Max <msur...@sysmocom.de>