rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef USERDATA_H_ |
| 18 | #define USERDATA_H_ |
| 19 | #include <cstdint> |
| 20 | #include <string> |
| 21 | #include <map> |
| 22 | #include <memory> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "SmsHeader.h" |
| 26 | |
| 27 | class UserData { |
| 28 | public: |
| 29 | UserData(); |
| 30 | virtual ~UserData(); |
| 31 | std::string toString(); |
| 32 | /** |
| 33 | * User data encoding types. |
| 34 | * (See 3GPP2 C.R1001-F, v1.0, table 9.1-1) |
| 35 | */ |
| 36 | static constexpr int ENCODING_OCTET = 0x00; |
| 37 | static constexpr int ENCODING_IS91_EXTENDED_PROTOCOL = 0x01; |
| 38 | static constexpr int ENCODING_7BIT_ASCII = 0x02; |
| 39 | static constexpr int ENCODING_IA5 = 0x03; |
| 40 | static constexpr int ENCODING_UNICODE_16 = 0x04; |
| 41 | static constexpr int ENCODING_SHIFT_JIS = 0x05; |
| 42 | static constexpr int ENCODING_KOREAN = 0x06; |
| 43 | static constexpr int ENCODING_LATIN_HEBREW = 0x07; |
| 44 | static constexpr int ENCODING_LATIN = 0x08; |
| 45 | static constexpr int ENCODING_GSM_7BIT_ALPHABET = 0x09; |
| 46 | static constexpr int ENCODING_GSM_DCS = 0x0A; |
| 47 | |
| 48 | /** |
| 49 | * User data message type encoding types. |
| 50 | * (See 3GPP2 C.S0015-B, 4.5.2 and 3GPP 23.038, Section 4) |
| 51 | */ |
| 52 | static constexpr int ENCODING_GSM_DCS_7BIT = 0x00; |
| 53 | static constexpr int ENCODING_GSM_DCS_8BIT = 0x01; |
| 54 | static constexpr int ENCODING_GSM_DCS_16BIT = 0x02; |
| 55 | |
| 56 | /** |
| 57 | * IS-91 message types. |
| 58 | * (See TIA/EIS/IS-91-A-ENGL 1999, table 3.7.1.1-3) |
| 59 | */ |
| 60 | static constexpr int IS91_MSG_TYPE_VOICEMAIL_STATUS = 0x82; |
| 61 | static constexpr int IS91_MSG_TYPE_SHORT_MESSAGE_FULL = 0x83; |
| 62 | static constexpr int IS91_MSG_TYPE_CLI = 0x84; |
| 63 | static constexpr int IS91_MSG_TYPE_SHORT_MESSAGE = 0x85; |
| 64 | |
| 65 | /** |
| 66 | * US ASCII character mapping table. |
| 67 | * |
| 68 | * This table contains only the printable ASCII characters, with a |
| 69 | * 0x20 offset, meaning that the ASCII SPACE character is at index |
| 70 | * 0, with the resulting code of 0x20. |
| 71 | * |
| 72 | * Note this mapping is also equivalent to that used by both the |
| 73 | * IA5 and the IS-91 encodings. For the former this is defined |
| 74 | * using CCITT Rec. T.50 Tables 1 and 3. For the latter IS 637 B, |
| 75 | * Table 4.3.1.4.1-1 -- and note the encoding uses only 6 bits, |
| 76 | * and hence only maps entries up to the '_' character. |
| 77 | * |
| 78 | */ |
| 79 | static const char ASCII_MAP[]; |
| 80 | |
| 81 | /** |
| 82 | * Character to use when forced to encode otherwise unencodable |
| 83 | * characters, meaning those not in the respective ASCII or GSM |
| 84 | * 7-bit encoding tables. Current choice is SPACE, which is 0x20 |
| 85 | * in both the GSM-7bit and ASCII-7bit encodings. |
| 86 | */ |
| 87 | static constexpr uint8_t UNENCODABLE_7_BIT_CHAR = 0x20; |
| 88 | |
| 89 | /** |
| 90 | * Only elements between these indices in the ASCII table are printable. |
| 91 | */ |
| 92 | static constexpr int PRINTABLE_ASCII_MIN_INDEX = 0x20; |
| 93 | static constexpr int ASCII_NL_INDEX = 0x0A; |
| 94 | ; |
| 95 | static constexpr int ASCII_CR_INDEX = 0x0D; |
| 96 | static std::map<char, uint8_t> charToAscii; |
| 97 | |
| 98 | /* |
| 99 | * TODO(cleanup): Move this very generic functionality somewhere |
| 100 | * more general. |
| 101 | */ |
| 102 | /** |
| 103 | * Given a string generate a corresponding ASCII-encoded byte |
| 104 | * array, but limited to printable characters. If the input |
| 105 | * contains unprintable characters, return null. |
| 106 | */ |
| 107 | //static uint8_t* stringToAscii(std::string msg, int* length); |
| 108 | static std::vector<uint8_t> stringToAscii(std::string msg); |
| 109 | /** |
| 110 | * Mapping for ASCII values less than 32 are flow control signals |
| 111 | * and not used here. |
| 112 | */ |
| 113 | static constexpr int ASCII_MAP_BASE_INDEX = 0x20; |
| 114 | static const int ASCII_MAP_MAX_INDEX; |
| 115 | |
| 116 | /** |
| 117 | * Contains the data header of the user data |
| 118 | */ |
| 119 | std::shared_ptr<SmsHeader> userDataHeader = nullptr; |
| 120 | /** |
| 121 | * Contains the data encoding type for the SMS message |
| 122 | */ |
| 123 | int msgEncoding = -1; |
| 124 | bool msgEncodingSet = false; |
| 125 | |
| 126 | int msgType = 0; |
| 127 | |
| 128 | /** |
| 129 | * Number of invalid bits in the last byte of data. |
| 130 | */ |
| 131 | int paddingBits = 0; |
| 132 | |
| 133 | int numFields = 0; |
| 134 | |
| 135 | /** |
| 136 | * Contains the user data of a SMS message |
| 137 | * (See 3GPP2 C.S0015-B, v2, 4.5.2) |
| 138 | */ |
| 139 | //uint8_t* payload = nullptr; |
| 140 | //int payload_length = 0; |
| 141 | std::vector<uint8_t> payload; |
| 142 | std::string payloadStr; |
| 143 | //char* payloadStr = nullptr; |
| 144 | private: |
| 145 | static std::map<char, uint8_t> init(); |
| 146 | }; |
| 147 | |
| 148 | #endif /* USERDATA_H_ */ |