[asterisk-commits] may: branch may/smpp r401373 - /team/may/smpp/branches/10/addons/res_smpp.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Oct 21 16:25:23 CDT 2013


Author: may
Date: Mon Oct 21 16:25:21 2013
New Revision: 401373

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=401373
Log:
convert tlv macros to functions
stop listener before other threads

Modified:
    team/may/smpp/branches/10/addons/res_smpp.c

Modified: team/may/smpp/branches/10/addons/res_smpp.c
URL: http://svnview.digium.com/svn/asterisk/team/may/smpp/branches/10/addons/res_smpp.c?view=diff&rev=401373&r1=401372&r2=401373
==============================================================================
--- team/may/smpp/branches/10/addons/res_smpp.c (original)
+++ team/may/smpp/branches/10/addons/res_smpp.c Mon Oct 21 16:25:21 2013
@@ -153,6 +153,74 @@
 	return msgid;
 }
 
+static void get_tlvs(struct ast_msg *msg, tlv_t *tlv, char *tmp, int tmplen, char *tmp2, int tmp2len) 
+{
+	int k, tlvnum = 0;
+	while (tlv) {
+		tlvnum++;
+		snprintf(tmp2, tmp2len, "tlv.%d.tag",  tlvnum);
+		tmp[0] = 0;
+		for (k = 0; tlvs[k].tlvid; k++) {
+			if (tlvs[k].tlvid == tlv->tag) {
+				strcpy(tmp, tlvs[k].tlv);
+				char *space = strchr(tmp, ' ');
+				if (space) {
+					*space = 0;
+				}
+				break;
+			}
+		}
+		if (!tmp[0]) {
+			snprintf(tmp, tmplen, "%d" , tlv->tag);
+		}
+		ast_msg_set_var(msg, tmp2, tmp);
+		snprintf(tmp2, tmp2len, "tlv.%d.length",  tlvnum);
+		snprintf(tmp, tmplen, "%d" , tlv->length);
+		ast_msg_set_var(msg, tmp2, tmp);
+		snprintf(tmp2, tmp2len, "tlv.%d.value",  tlvnum);
+		ast_base64encode(tmp, (void *)&tlv->value, tlv->length, tmplen);
+		ast_msg_set_var(msg, tmp2, tmp);
+		tlv = tlv->next;
+	}
+	snprintf(tmp, tmplen, "%d",  tlvnum);
+	ast_msg_set_var(msg, "tlvs", tmp);
+}
+
+static tlv_t* put_tlvs(tlv_t **stlv, struct ast_msg *message, char *tmp, int tmplen)
+{
+	tlv_t* tlv;
+	int k, tlvnum = atoi(ast_msg_get_var(message, "tlvs"));
+	while (tlvnum) {
+		tlv = ast_calloc(1, sizeof(tlv_t));
+		snprintf(tmp, tmplen, "tlv.%d.tag", tlvnum);
+		if (ast_msg_get_var(message, tmp)) {
+			tlv->tag = atoi(ast_msg_get_var(message, tmp));
+			if (!tlv->tag) {
+				const char *tag = ast_msg_get_var(message, tmp);
+				for(k = 0; tlvs[k].tlvid; k++) {
+					if (!strncmp(tag, tlvs[k].tlv, strlen(tag))) {
+						tlv->tag = tlvs[k].tlvid;
+						break;
+					}
+				}
+			}
+		}
+		snprintf(tmp, tmplen, "tlv.%d.length", tlvnum);
+		if (ast_msg_get_var(message, tmp)) {
+			tlv->length = atoi(ast_msg_get_var(message, tmp));
+		}
+		snprintf(tmp, tmplen, "tlv.%d.value", tlvnum);
+		if (tlv->tag && tlv->length &&  ast_msg_get_var(message, tmp) &&
+			 ast_base64decode((unsigned char *)&tlv->value,
+				ast_msg_get_var(message, tmp), sizeof(tlv->value))) {
+			build_tlv(stlv, tlv);
+		} else {
+			free(tlv);
+		}
+		tlvnum--;
+	}
+	return *stlv;
+}
 // SMPP processing
 
 static int smsc_connect(struct smpp_smsc *smsc)
@@ -282,7 +350,6 @@
 		char body[1024];
 		char tmp[1024];
 		char tmp2[256];
-		tlv_t *tlv;
 		int res;
 		deliver_sm_resp_t sm_resp;
 
@@ -318,45 +385,11 @@
 
 		ast_msg_set_var(msg, "smpp_source", smsc->name);
 // parse tlv
-
-#define GET_TLVS(msg, deliver, tlv, tmp, tmp2) \
-		int tlvnum = 0; \
-		tlv = deliver->tlv; \
-		while (tlv) { \
-			tlvnum++; \
-			snprintf(tmp2, sizeof(tmp2), "tlv.%d.tag",  tlvnum); \
-			tmp[0] = 0; \
-			int k; \
-			for (k = 0; tlvs[k].tlvid; k++) { \
-				if (tlvs[k].tlvid == tlv->tag) { \
-					strcpy(tmp, tlvs[k].tlv); \
-					char *space = strchr(tmp, ' '); \
-					if (space) { \
-						*space = 0; \
-					} \
-					break; \
-				} \
-			} \
-			if (!tmp[0]) { \
-				snprintf(tmp, sizeof(tmp), "%d" , tlv->tag); \
-			} \
-			ast_msg_set_var(msg, tmp2, tmp); \
-			snprintf(tmp2, sizeof(tmp2), "tlv.%d.length",  tlvnum); \
-			snprintf(tmp, sizeof(tmp), "%d" , tlv->length); \
-			ast_msg_set_var(msg, tmp2, tmp); \
-			snprintf(tmp2, sizeof(tmp2), "tlv.%d.value",  tlvnum); \
-			ast_base64encode(tmp, (void *)&tlv->value, tlv->length, sizeof(tmp)); \
-			ast_msg_set_var(msg, tmp2, tmp); \
-			tlv = tlv->next; \
-		} \
-		snprintf(tmp, sizeof(tmp), "%d",  tlvnum); \
-		ast_msg_set_var(msg, "tlvs", tmp);
-
-		GET_TLVS(msg, deliver, tlv, tmp, tmp2);
+		get_tlvs(msg, deliver->tlv, tmp, sizeof(tmp), tmp2, sizeof(tmp2));
 
 		res = ast_msg_queue(msg);
-		if (tlv) {
-			destroy_tlv(tlv);
+		if (deliver->tlv) {
+			destroy_tlv(deliver->tlv);
 		}
 
 		memset(&sm_resp, 0, sizeof(sm_resp));
@@ -375,7 +408,6 @@
 		char tmp2[256];
 		char messageid[66];
 		int res;
-		tlv_t *tlv;
 		submit_sm_resp_t sm_resp;
 
 		if (!msg) {
@@ -399,12 +431,12 @@
 		ast_msg_set_var(msg, "smpp_message_id", generate_msgid(messageid));
 
 		ast_msg_set_var(msg, "smpp_source", smsc->name);
-
-		GET_TLVS(msg, submit, tlv, tmp, tmp2);
+// parse tlv
+		get_tlvs(msg, submit->tlv, tmp, sizeof(tmp), tmp2, sizeof(tmp2));
 
 		res = ast_msg_queue(msg);
-		if (tlv) {
-			destroy_tlv(tlv);
+		if (submit->tlv) {
+			destroy_tlv(submit->tlv);
 		}
 
 
@@ -1060,6 +1092,22 @@
 {
 	struct smpp_smsc *smsc;
 	ast_mutex_lock(&smpp_lock);
+
+	if (listener != AST_PTHREADT_NULL) {
+		if (listener != AST_PTHREADT_STOP) {
+			smpplistener_stop = 1;
+			if (smppDebug) {
+				ast_verbose("stopping smpp listener thread\n");
+			}
+			pthread_kill(listener, SIGURG);
+			while (smpplistener_stop) {
+				sleep(1);
+			}
+			listener = AST_PTHREADT_NULL;
+			ast_verbose("listener terminated\n");
+		}
+	}
+
 	smsc = smsc_list;
 	while (smsc) {
 		if (smsc->monitor != AST_PTHREADT_NULL) {
@@ -1077,21 +1125,6 @@
 			}
 		}
 		smsc = smsc->next;
-	}
-
-	if (listener != AST_PTHREADT_NULL) {
-		if (listener != AST_PTHREADT_STOP) {
-			smpplistener_stop = 1;
-			if (smppDebug) {
-				ast_verbose("stopping smpp listener thread\n");
-			}
-			pthread_kill(listener, SIGURG);
-			while (smpplistener_stop) {
-				sleep(1);
-			}
-			listener = AST_PTHREADT_NULL;
-			ast_verbose("listener terminated\n");
-		}
 	}
 
 	ast_mutex_unlock(&smpp_lock);
@@ -1310,39 +1343,6 @@
 		}
 
 
-#define PUT_TLVS(submit, message, tlv, tmp) \
-		int tlvnum = atoi(ast_msg_get_var(message, "tlvs")); \
-		while (tlvnum) { \
-			tlv = ast_calloc(1, sizeof(tlv_t)); \
-			snprintf(tmp, sizeof(tmp), "tlv.%d.tag", tlvnum); \
-			if (ast_msg_get_var(message, tmp)) { \
-				tlv->tag = atoi(ast_msg_get_var(message, tmp)); \
-				if (!tlv->tag) { \
-					const char *tag = ast_msg_get_var(message, tmp); \
-					int k; \
-					for(k = 0; tlvs[k].tlvid; k++) { \
-						if (!strncmp(tag, tlvs[k].tlv, strlen(tag))) { \
-							tlv->tag = tlvs[k].tlvid; \
-							break; \
-						} \
-					} \
-				} \
-			} \
-			snprintf(tmp, sizeof(tmp), "tlv.%d.length", tlvnum); \
-			if (ast_msg_get_var(message, tmp)) { \
-				tlv->length = atoi(ast_msg_get_var(message, tmp)); \
-			} \
-			snprintf(tmp, sizeof(tmp), "tlv.%d.value", tlvnum); \
-			if (tlv->tag && tlv->length &&  ast_msg_get_var(message, tmp) && \
-				 ast_base64decode((unsigned char *)&tlv->value, \
-					ast_msg_get_var(message, tmp), sizeof(tlv->value))) { \
-				build_tlv(&submit.tlv, tlv); \
-			} else { \
-				free(tlv); \
-			} \
-			tlvnum--; \
-		} \
-		tlv = submit.tlv; \
 
 		submit.source_addr_ton = 0;
 		submit.source_addr_npi = 0;
@@ -1373,7 +1373,7 @@
 			submit.sm_length = strlen((char *)submit.short_message);
 		}
 
-		PUT_TLVS(submit, message, tlv, tmp);
+		tlv = put_tlvs(&submit.tlv, message, tmp, sizeof(tmp));
 
 		unpack = &submit;
 	} else {
@@ -1413,7 +1413,7 @@
 			deliver.sm_length = strlen((char *)deliver.short_message);
 		}
 
-		PUT_TLVS(deliver, message, tlv, tmp);
+		tlv = put_tlvs(&deliver.tlv, message, tmp, sizeof(tmp));
 
 		unpack = &deliver;
 	}




More information about the asterisk-commits mailing list