1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
use crate::CommandId;
/// The [`SessionState`] represents the state of an ESME session in the `SMPP` 5.0 protocol.
///
/// The session state determines what operations are allowed at any given point in the
/// communication between an ESME (External Short Message Entity) and an MC (Message Center).
///
/// The session state transitions are triggered by bind, unbind, and outbind operations.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub enum SessionState {
/// CLOSED state.
///
/// This is the initial state before any connection is established.
/// In this state, no communication is possible between the ESME and MC.
#[default]
Closed,
/// OPEN state.
///
/// This state is entered after a connection is established between
/// the ESME and MC, but before any `SMPP` bind operation is performed.
/// In this state, only bind operations are allowed.
Open,
/// BOUND_TX state (Transmitter mode).
///
/// This state is entered after a successful bind_transmitter operation.
/// In this state, the ESME can send messages to the MC but cannot receive messages.
BoundTx,
/// BOUND_RX state (Receiver mode).
///
/// This state is entered after a successful bind_receiver operation.
/// In this state, the ESME can receive messages from the MC but cannot send messages.
BoundRx,
/// BOUND_TRX state (Transceiver mode).
///
/// This state is entered after a successful bind_transceiver operation.
/// In this state, the ESME can both send messages to and receive messages from the MC.
BoundTrx,
/// OUTBOUND state.
///
/// This state is entered after an MC initiates an outbind operation to an ESME.
/// The ESME must respond with a bind_receiver or bind_transceiver operation.
/// In this state, no messaging operations are allowed until the ESME completes the binding process.
Outbound,
/// UNBOUND state.
///
/// This state is entered after an unbind operation is initiated by either the ESME or MC.
/// The session is in the process of being terminated, but the unbind_resp has not yet been sent.
/// No messaging operations are allowed in this state.
Unbound,
}
impl SessionState {
/// Returns true if the session is a bound state.
///
/// One of the following states:
/// [`SessionState::BoundTx`], [`SessionState::BoundRx`] or [`SessionState::BoundTrx`].
pub const fn is_bound(self) -> bool {
matches!(self, Self::BoundTx | Self::BoundRx | Self::BoundTrx)
}
/// Determines whether the current session state allows sending a given SMPP command as an ESME.
///
/// # Arguments
///
/// * `command` - The SMPP command to check.
///
/// # Returns true if an ESME in that state can send this command.
///
/// This follows the 2.4 Operation Matrix of the SMPP 5.0 specification
pub const fn can_send_as_esme(self, command: CommandId) -> bool {
match self {
SessionState::Closed => false,
SessionState::Open | SessionState::Outbound => {
matches!(
command,
CommandId::BindReceiver
| CommandId::BindTransmitter
| CommandId::BindTransceiver
| CommandId::EnquireLink
| CommandId::EnquireLinkResp
| CommandId::GenericNack
)
}
SessionState::BoundTx => {
matches!(
command,
CommandId::BroadcastSm
| CommandId::CancelBroadcastSm
| CommandId::CancelSm
| CommandId::DataSm
| CommandId::EnquireLink
| CommandId::EnquireLinkResp
| CommandId::GenericNack
| CommandId::QueryBroadcastSm
| CommandId::QuerySm
| CommandId::ReplaceSm
| CommandId::SubmitMulti
| CommandId::SubmitSm
| CommandId::Unbind
| CommandId::UnbindResp
)
}
SessionState::BoundRx => {
matches!(
command,
CommandId::DataSmResp
| CommandId::DeliverSmResp
| CommandId::EnquireLink
| CommandId::EnquireLinkResp
| CommandId::GenericNack
| CommandId::Unbind
| CommandId::UnbindResp
)
}
SessionState::BoundTrx => {
SessionState::BoundTx.can_send_as_esme(command)
|| SessionState::BoundRx.can_send_as_esme(command)
}
SessionState::Unbound => {
matches!(
command,
CommandId::EnquireLink | CommandId::EnquireLinkResp | CommandId::GenericNack
)
}
}
}
/// Determines whether the current session state allows an ESME to receive a given SMPP command.
///
/// # Arguments
///
/// * `command` - The SMPP command to check.
///
/// # Returns true if an ESME in that state can receive this command.
///
/// This follows the 2.4 Operation Matrix of the SMPP 5.0 specification
pub const fn can_receive_as_esme(self, command: CommandId) -> bool {
self.can_send_as_mc(command)
}
/// Determines whether the current session state allows a MC to send a given SMPP command.
///
/// # Arguments
///
/// * `command` - The SMPP command to check.
///
/// # Returns true if a MC in that state can send this command.
///
/// This follows the 2.4 Operation Matrix of the SMPP 5.0 specification
pub const fn can_send_as_mc(self, command: CommandId) -> bool {
match self {
SessionState::Closed => false,
SessionState::Open => {
matches!(
command,
CommandId::BindReceiverResp
| CommandId::BindTransmitterResp
| CommandId::BindTransceiverResp
| CommandId::EnquireLink
| CommandId::EnquireLinkResp
| CommandId::GenericNack
| CommandId::Outbind
)
}
SessionState::Outbound => {
matches!(
command,
CommandId::BindReceiverResp
| CommandId::BindTransmitterResp
| CommandId::BindTransceiverResp
| CommandId::EnquireLink
| CommandId::EnquireLinkResp
| CommandId::GenericNack
)
}
SessionState::BoundTx => {
matches!(
command,
CommandId::BroadcastSmResp
| CommandId::CancelBroadcastSmResp
| CommandId::CancelSmResp
| CommandId::DataSmResp
| CommandId::EnquireLink
| CommandId::EnquireLinkResp
| CommandId::GenericNack
| CommandId::QueryBroadcastSmResp
| CommandId::QuerySmResp
| CommandId::ReplaceSmResp
| CommandId::SubmitMultiResp
| CommandId::SubmitSmResp
| CommandId::Unbind
| CommandId::UnbindResp
)
}
SessionState::BoundRx => {
matches!(
command,
CommandId::AlertNotification
| CommandId::DataSm
| CommandId::DeliverSm
| CommandId::EnquireLink
| CommandId::EnquireLinkResp
| CommandId::GenericNack
| CommandId::Unbind
| CommandId::UnbindResp
)
}
SessionState::BoundTrx => {
SessionState::BoundTx.can_send_as_mc(command)
|| SessionState::BoundRx.can_send_as_mc(command)
}
SessionState::Unbound => {
matches!(
command,
CommandId::EnquireLink | CommandId::EnquireLinkResp | CommandId::GenericNack
)
}
}
}
/// Determines whether the current session state allows a MC to receive a given SMPP command.
///
/// # Arguments
///
/// * `command` - The SMPP command to check.
///
/// # Returns true if a MC in that state can receive this command.
///
/// This follows the 2.4 Operation Matrix of the SMPP 5.0 specification
pub const fn can_receive_as_mc(self, command: CommandId) -> bool {
self.can_send_as_esme(command)
}
}
#[cfg(test)]
mod tests {
use super::*;
use strum::IntoEnumIterator;
const ESME_BOUND_TX_COMMANDS: [CommandId; 9] = [
CommandId::BroadcastSm,
CommandId::CancelBroadcastSm,
CommandId::CancelSm,
CommandId::DataSm,
CommandId::QueryBroadcastSm,
CommandId::QuerySm,
CommandId::ReplaceSm,
CommandId::SubmitMulti,
CommandId::SubmitSm,
];
const ESME_BOUND_RX_COMMANDS: [CommandId; 2] =
[CommandId::DataSmResp, CommandId::DeliverSmResp];
#[test]
fn test_is_bound() {
assert!(!SessionState::Closed.is_bound());
assert!(SessionState::BoundTx.is_bound());
assert!(SessionState::BoundRx.is_bound());
assert!(SessionState::BoundTrx.is_bound());
assert!(!SessionState::Open.is_bound());
assert!(!SessionState::Outbound.is_bound());
assert!(!SessionState::Unbound.is_bound());
}
#[test]
fn test_status_close() {
for command in CommandId::iter() {
assert!(!SessionState::Closed.can_send_as_esme(command));
assert!(!SessionState::Closed.can_send_as_mc(command));
assert!(!SessionState::Closed.can_receive_as_esme(command));
assert!(!SessionState::Closed.can_receive_as_mc(command));
}
}
#[test]
fn test_link_nack() {
for command in [
CommandId::GenericNack,
CommandId::EnquireLink,
CommandId::EnquireLinkResp,
] {
for state in [
SessionState::Open,
SessionState::Outbound,
SessionState::BoundTx,
SessionState::BoundRx,
SessionState::BoundTrx,
SessionState::Unbound,
] {
assert!(state.can_send_as_esme(command));
assert!(state.can_send_as_mc(command));
assert!(state.can_receive_as_esme(command));
assert!(state.can_receive_as_mc(command));
}
}
}
#[test]
fn test_open_outbound() {
for state in [SessionState::Open, SessionState::Outbound] {
assert!(state.can_send_as_esme(CommandId::BindTransmitter));
assert!(state.can_send_as_esme(CommandId::BindTransceiver));
assert!(state.can_send_as_esme(CommandId::BindReceiver));
assert!(state.can_send_as_mc(CommandId::BindTransmitterResp));
assert!(state.can_send_as_mc(CommandId::BindTransceiverResp));
assert!(state.can_send_as_mc(CommandId::BindReceiverResp));
}
}
#[test]
fn test_tx() {
for command in ESME_BOUND_TX_COMMANDS {
assert!(SessionState::BoundTx.can_send_as_esme(command));
assert!(SessionState::BoundTx.can_receive_as_esme(command.matching_response()));
assert!(SessionState::BoundTx.can_receive_as_mc(command));
assert!(SessionState::BoundTx.can_send_as_mc(command.matching_response()));
}
}
#[test]
fn test_rx() {
for command in ESME_BOUND_RX_COMMANDS {
assert!(SessionState::BoundRx.can_send_as_esme(command));
assert!(SessionState::BoundRx.can_receive_as_esme(command.matching_request()));
assert!(SessionState::BoundRx.can_receive_as_mc(command));
assert!(SessionState::BoundRx.can_send_as_mc(command.matching_request()));
}
}
#[test]
fn test_trx() {
for command in ESME_BOUND_TX_COMMANDS {
assert!(SessionState::BoundTrx.can_send_as_esme(command));
assert!(SessionState::BoundTrx.can_receive_as_esme(command.matching_response()));
assert!(SessionState::BoundTrx.can_receive_as_mc(command));
assert!(SessionState::BoundTrx.can_send_as_mc(command.matching_response()));
}
for command in ESME_BOUND_RX_COMMANDS {
assert!(SessionState::BoundTrx.can_send_as_esme(command));
assert!(SessionState::BoundTrx.can_receive_as_esme(command.matching_request()));
assert!(SessionState::BoundTrx.can_receive_as_mc(command));
assert!(SessionState::BoundTrx.can_send_as_mc(command.matching_request()));
}
}
}