bsc_api: Change submit dtap to allow to automatically use the SACH

For SAPI=3 on a TCH it might be nice to use the SACH to submit
the message. The api allows to automatically use the SACH if
a message like this is submitted.
diff --git a/openbsc/include/openbsc/bsc_api.h b/openbsc/include/openbsc/bsc_api.h
index 314ce9c..36ec370 100644
--- a/openbsc/include/openbsc/bsc_api.h
+++ b/openbsc/include/openbsc/bsc_api.h
@@ -26,7 +26,7 @@
 };
 
 int bsc_api_init(struct gsm_network *network, struct bsc_api *api);
-int gsm0808_submit_dtap(struct gsm_subscriber_connection *conn, struct msgb *msg, int link_id);
+int gsm0808_submit_dtap(struct gsm_subscriber_connection *conn, struct msgb *msg, int link_id, int allow_sach);
 int gsm0808_assign_req(struct gsm_subscriber_connection *conn, int chan_mode, int full_rate);
 int gsm0808_cipher_mode(struct gsm_subscriber_connection *conn, int cipher,
 			const uint8_t *key, int len, int include_imeisv);
diff --git a/openbsc/src/bsc_api.c b/openbsc/src/bsc_api.c
index c86d62c..085a8c9 100644
--- a/openbsc/src/bsc_api.c
+++ b/openbsc/src/bsc_api.c
@@ -174,12 +174,18 @@
 }
 
 int gsm0808_submit_dtap(struct gsm_subscriber_connection *conn,
-			struct msgb *msg, int link_id)
+			struct msgb *msg, int link_id, int allow_sach)
 {
 	uint8_t sapi = link_id & 0x7;
 	msg->lchan = conn->lchan;
 	msg->trx = msg->lchan->ts->trx;
 
+	/* If we are on a TCH and need to submit a SMS (on SAPI=3) we need to use the SACH */
+	if (allow_sach && sapi != 0) {
+		if (conn->lchan->type == GSM_LCHAN_TCH_F || conn->lchan->type == GSM_LCHAN_TCH_H)
+			link_id |= 0x40;
+	}
+
 	msg->l3h = msg->data;
 	if (conn->lchan->sapis[sapi] == LCHAN_SAPI_UNUSED) {
 		OBSC_LINKID_CB(msg) = link_id;
diff --git a/openbsc/src/gsm_04_08.c b/openbsc/src/gsm_04_08.c
index fd3c002..5a76baa 100644
--- a/openbsc/src/gsm_04_08.c
+++ b/openbsc/src/gsm_04_08.c
@@ -104,7 +104,7 @@
 				gh->proto_discr, gh->msg_type);
 	}
 
-	return gsm0808_submit_dtap(conn, msg, 0);
+	return gsm0808_submit_dtap(conn, msg, 0, 0);
 }
 
 int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
diff --git a/openbsc/src/gsm_04_11.c b/openbsc/src/gsm_04_11.c
index 2931026..c46f772 100644
--- a/openbsc/src/gsm_04_11.c
+++ b/openbsc/src/gsm_04_11.c
@@ -142,7 +142,7 @@
 {
 	DEBUGP(DSMS, "GSM4.11 TX %s\n", hexdump(msg->data, msg->len));
 	msg->l3h = msg->data;
-	return gsm0808_submit_dtap(conn, msg, link_id);
+	return gsm0808_submit_dtap(conn, msg, link_id, 1);
 }
 
 /* SMC TC1* is expired */
diff --git a/openbsc/src/gsm_04_80.c b/openbsc/src/gsm_04_80.c
index e0f47ff..8414dc9 100644
--- a/openbsc/src/gsm_04_80.c
+++ b/openbsc/src/gsm_04_80.c
@@ -107,7 +107,7 @@
 					| (1<<7);  /* TI direction = 1 */
 	gh->msg_type = GSM0480_MTYPE_RELEASE_COMPLETE;
 
-	return gsm0808_submit_dtap(conn, msg, 0);
+	return gsm0808_submit_dtap(conn, msg, 0, 0);
 }
 
 int gsm0480_send_ussd_reject(struct gsm_subscriber_connection *conn,
@@ -136,7 +136,7 @@
 	gh->proto_discr |= req->transaction_id | (1<<7);  /* TI direction = 1 */
 	gh->msg_type = GSM0480_MTYPE_RELEASE_COMPLETE;
 
-	return gsm0808_submit_dtap(conn, msg, 0);
+	return gsm0808_submit_dtap(conn, msg, 0, 0);
 }
 
 int gsm0480_send_ussdNotify(struct gsm_subscriber_connection *conn, int level, const char *text)
@@ -156,7 +156,7 @@
 	gh->proto_discr = GSM48_PDISC_NC_SS;
 	gh->msg_type = GSM0480_MTYPE_REGISTER;
 
-	return gsm0808_submit_dtap(conn, msg, 0);
+	return gsm0808_submit_dtap(conn, msg, 0, 0);
 }
 
 int gsm0480_send_releaseComplete(struct gsm_subscriber_connection *conn)
@@ -172,5 +172,5 @@
 	gh->proto_discr = GSM48_PDISC_NC_SS;
 	gh->msg_type = GSM0480_MTYPE_RELEASE_COMPLETE;
 
-	return gsm0808_submit_dtap(conn, msg, 0);
+	return gsm0808_submit_dtap(conn, msg, 0, 0);
 }