Harald Welte | 783d073 | 2014-12-29 17:09:11 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2014 by Harald Welte <laforge@gnumonks.org> |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
Harald Welte | 783d073 | 2014-12-29 17:09:11 +0100 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <string.h> |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
Holger Hans Peter Freyther | fdb4667 | 2015-11-09 16:32:43 +0000 | [diff] [blame] | 20 | #include <time.h> |
Harald Welte | 783d073 | 2014-12-29 17:09:11 +0100 | [diff] [blame] | 21 | |
| 22 | #include <osmocom/gsm/protocol/gsm_03_41.h> |
| 23 | #include <osmocom/gsm/gsm0341.h> |
| 24 | #include <osmocom/gsm/gsm_utils.h> |
| 25 | #include <osmocom/core/utils.h> |
| 26 | #include <osmocom/core/msgb.h> |
| 27 | |
Pau Espin Pedrol | 061f96d | 2022-08-05 13:46:44 +0200 | [diff] [blame] | 28 | struct gsm341_ms_message *gen_msg_from_text(uint16_t msg_id, uint8_t msg_code, const char *text) |
Harald Welte | 783d073 | 2014-12-29 17:09:11 +0100 | [diff] [blame] | 29 | { |
| 30 | struct gsm341_ms_message *cbmsg; |
| 31 | int text_len = strlen(text); |
| 32 | /* assuming default GSM alphabet, the encoded payload cannot be |
| 33 | * longer than the input text */ |
| 34 | uint8_t payload[text_len]; |
| 35 | int payload_octets; |
| 36 | |
Pau Espin Pedrol | 061f96d | 2022-08-05 13:46:44 +0200 | [diff] [blame] | 37 | //srand(time(NULL)); |
Harald Welte | 65f676d | 2014-12-30 00:43:39 +0100 | [diff] [blame] | 38 | |
Harald Welte | 783d073 | 2014-12-29 17:09:11 +0100 | [diff] [blame] | 39 | gsm_7bit_encode_n(payload, sizeof(payload), text, &payload_octets); |
Harald Welte | 65f676d | 2014-12-30 00:43:39 +0100 | [diff] [blame] | 40 | //cbmsg = gsm0341_build_msg(NULL, 0, rand(), 0, msg_id, 0x0f, 1, 1, payload, payload_octets); |
Pau Espin Pedrol | 061f96d | 2022-08-05 13:46:44 +0200 | [diff] [blame] | 41 | cbmsg = gsm0341_build_msg(NULL, 0, msg_code, 0, msg_id, 0x00, 1, 1, payload, payload_octets); |
Harald Welte | 783d073 | 2014-12-29 17:09:11 +0100 | [diff] [blame] | 42 | |
| 43 | printf("%s\n", osmo_hexdump_nospc((uint8_t *)cbmsg, sizeof(*cbmsg)+payload_octets)); |
| 44 | |
| 45 | return cbmsg; |
| 46 | } |
| 47 | |
| 48 | int main(int argc, char **argv) |
| 49 | { |
| 50 | uint16_t msg_id = GSM341_MSGID_ETWS_CMAS_MONTHLY_TEST; |
| 51 | char *text = "Mahlzeit!"; |
Harald Welte | c993a07 | 2014-12-30 13:22:56 +0100 | [diff] [blame] | 52 | char tbuf[GSM341_MAX_CHARS+1]; |
Pau Espin Pedrol | 061f96d | 2022-08-05 13:46:44 +0200 | [diff] [blame] | 53 | struct gsm341_ms_message *cbmsg; |
Harald Welte | 783d073 | 2014-12-29 17:09:11 +0100 | [diff] [blame] | 54 | |
| 55 | if (argc > 1) |
| 56 | msg_id = atoi(argv[1]); |
| 57 | |
| 58 | if (argc > 2) |
| 59 | text = argv[2]; |
| 60 | |
Harald Welte | c993a07 | 2014-12-30 13:22:56 +0100 | [diff] [blame] | 61 | strncpy(tbuf, text, GSM341_MAX_CHARS); |
| 62 | if (strlen(text) < GSM341_MAX_CHARS) |
Harald Welte | c13cf8b | 2014-12-30 13:25:44 +0100 | [diff] [blame] | 63 | memset(tbuf+strlen(text), GSM341_7BIT_PADDING, |
| 64 | sizeof(tbuf)-strlen(text)); |
| 65 | tbuf[GSM341_MAX_CHARS] = 0; |
Daniel Willmann | f626123 | 2014-12-30 12:07:17 +0100 | [diff] [blame] | 66 | |
Pau Espin Pedrol | 061f96d | 2022-08-05 13:46:44 +0200 | [diff] [blame] | 67 | cbmsg = gen_msg_from_text(msg_id, 1, tbuf); |
| 68 | talloc_free(cbmsg); |
Harald Welte | 783d073 | 2014-12-29 17:09:11 +0100 | [diff] [blame] | 69 | |
| 70 | return EXIT_SUCCESS; |
| 71 | } |