/* ** CDMA 2000 project, SMPP module ** ** File name: smpp_test.c ** Written by Li Long at 2004-11-25 ** CVS $Id: smpp_test.c,v0.1 2004/11/25 17:03:14 lilong Exp $ ** */ #include "./include/smpp.h" #define SD_PKT_NUM 10 #define STORE_MSG_ITEM 5 typedef struct SMPP_MSG_STORE_STRUCT { BYTE short_message[SMPP_MSG_LEN]; BYTE message_id[65]; } SMPP_MSG_STORE_STRUCT; typedef struct SMPP_LINK_ATTRIBUTE { BYTE enable; BYTE session_type; //BIND_TX/BIND_RX/BIND_TRX BYTE message_mode; //STORE AND FORWARD/DATAGRAM/TRANSACTION BYTE terminal_type; //SMPP_CLIENT/SMPP_SERVER } SMPP_LINK_ATTRIBUTE; static struct itimerval itimer,old_itimer; static int timer_counter[MAX_SMPP_LINK]; static int stateFlag[MAX_SMPP_LINK]; static int msg_count[MAX_SMPP_LINK]; static int msg_pointer[MAX_SMPP_LINK]; static SMPP_MSG_STORE_STRUCT store_msg_buf[MAX_SMPP_LINK][STORE_MSG_ITEM]; static SMPP_LINK_ATTRIBUTE smpp_link_attr[MAX_SMPP_LINK]; static void On_Timer(); static void SetTimer(); static void smpp_test_init(); static void sendTestSM(int, int); static int smpp_onDataRecv(BYTE linkNo, SMPP_MSG *pData, WORD dataLen); static int smpp_onLinkStateChange(BYTE linkNo, BYTE linkstate); void smpp_test_init() { int i; for (i=0; imessage_type) { case 0x0B: /* SUBMIT SM */ printf("[TEST PROGRAM]Link %d received SUBMIT SM:\n", linkNo); /* store smpp message */ if (smpp_link_attr[linkNo].message_mode == STORE_AND_FORWARD) { //if (pMsg->pdu.submit_sm.sm_length < SMPP_MSG_LEN) { pointer = (msg_pointer[linkNo]+1)%STORE_MSG_ITEM; memcpy(&store_msg_buf[linkNo][pointer].short_message, pMsg->pdu.submit_sm.short_message, pMsg->pdu.submit_sm.sm_length); sprintf(store_msg_buf[linkNo][pointer].message_id, "%ld", pMsg->pdu.submit_sm.head.sequence_number); } } /* fill with SUBMIT SM RESP structure. */ smpp_msg.message_type = 0x0C; smpp_msg.pdu.submit_sm_resp.head.command_status = htonl(ESME_ROK); smpp_msg.pdu.submit_sm_resp.head.sequence_number = pMsg->pdu.submit_sm.head.sequence_number; sprintf(smpp_msg.pdu.submit_sm_resp.message_id, "%ld", smpp_msg.pdu.submit_sm_resp.head.sequence_number); smpp_send(linkNo, &smpp_msg, dataLen); break; case 0x0C: /* SUBMIT SM RESP */ printf("[TEST PROGRAM]Link %d received SUBMIT SM RESP.\n", linkNo); break; case 0x0D: /* SUBMIT MULTI */ printf("[TEST PROGRAM]Link %d received SUBMIT MULTI.\n", linkNo); /* store smpp message */ if (smpp_link_attr[linkNo].message_mode == STORE_AND_FORWARD) { //if (pMsg->pdu.submit_multi.sm_length < SMPP_MSG_LEN) { pointer = (msg_pointer[linkNo]+1)%STORE_MSG_ITEM; memcpy(&store_msg_buf[linkNo][pointer].short_message, pMsg->pdu.submit_sm.short_message, pMsg->pdu.submit_multi.sm_length); sprintf(store_msg_buf[linkNo][pointer].message_id, "%ld", pMsg->pdu.submit_multi.head.sequence_number); } } /* fill with SUBMIT MULTI RESP structure. */ smpp_msg.message_type = 0x0E; smpp_msg.pdu.submit_multi_resp.head.command_status = htonl(ESME_ROK); smpp_msg.pdu.submit_multi_resp.head.sequence_number = pMsg->pdu.submit_multi.head.sequence_number; sprintf(smpp_msg.pdu.submit_multi_resp.message_id, "%ld", smpp_msg.pdu.submit_multi_resp.head.sequence_number); smpp_msg.pdu.submit_multi_resp.no_unsuccess = 0; smpp_send(linkNo, &smpp_msg, dataLen); break; case 0x0E: /* SUBMIT MULTI RESP */ printf("[TEST PROGRAM]Link %d received SUBMIT MULTI RESP.\n", linkNo); break; case 0x0F: /* DELIVER SM */ printf("[TEST PROGRAM]Link %d received DELIVER SM.\n", linkNo); /* store smpp message */ if (smpp_link_attr[linkNo].message_mode == STORE_AND_FORWARD) { //if (pMsg->pdu.deliver_sm.sm_length < SMPP_MSG_LEN) { pointer = (msg_pointer[linkNo]+1)%STORE_MSG_ITEM; memcpy(&store_msg_buf[linkNo][pointer].short_message, pMsg->pdu.deliver_sm.short_message, pMsg->pdu.deliver_sm.sm_length); sprintf(store_msg_buf[linkNo][pointer].message_id, "%ld", pMsg->pdu.deliver_sm.head.sequence_number); } } /* fill with DELIVER SM RESP structure. */ smpp_msg.message_type = 0x10; smpp_msg.pdu.deliver_sm_resp.head.command_status = htonl(ESME_ROK); smpp_msg.pdu.deliver_sm_resp.head.sequence_number = pMsg->pdu.deliver_sm.head.sequence_number; smpp_msg.pdu.deliver_sm_resp.message_id = 0x0; smpp_send(linkNo, &smpp_msg, dataLen); break; case 0x10: /* DELIVER SM RESP */ printf("[TEST PROGRAM]Link %d received DELIVER SM RESP.\n", linkNo); break; case 0x11: /* DATA SM */ printf("[TEST PROGRAM]Link %d received DATA SM.\n", linkNo); /* store smpp message */ if (smpp_link_attr[linkNo].message_mode == STORE_AND_FORWARD) { if (strlen(pMsg->pdu.data_sm.message_payload) < SMPP_MSG_LEN) { pointer = (msg_pointer[linkNo]+1)%STORE_MSG_ITEM; memcpy(&store_msg_buf[linkNo][pointer].short_message, pMsg->pdu.data_sm.message_payload, strlen(pMsg->pdu.data_sm.message_payload)); sprintf(store_msg_buf[linkNo][pointer].message_id, "%ld", pMsg->pdu.data_sm.head.sequence_number); } } /* fill with DATA SM RESP structure. */ smpp_msg.message_type = 0x12; smpp_msg.pdu.data_sm_resp.head.command_status = htonl(ESME_ROK); smpp_msg.pdu.data_sm_resp.head.sequence_number = pMsg->pdu.data_sm.head.sequence_number; sprintf(smpp_msg.pdu.data_sm_resp.message_id, "%ld", smpp_msg.pdu.data_sm_resp.head.sequence_number); smpp_send(linkNo, &smpp_msg, dataLen); break; case 0x12: /* DATA SM RESP */ printf("[TEST PROGRAM]Link %d received DATA SM RESP.\n", linkNo); break; case 0x13: /* QUERY SM */ printf("[TEST PROGRAM]Link %d received QUERY SM.\n", linkNo); pointer = -1; for (i=0; ipdu.query_sm.message_id) == 0) { pointer = i; break; } } /* fill with QUERY SM RESP structure */ smpp_msg.message_type = 0x14; smpp_msg.pdu.query_sm_resp.head.command_status = htonl(ESME_ROK); smpp_msg.pdu.query_sm_resp.head.sequence_number = pMsg->pdu.query_sm.head.sequence_number; sprintf(smpp_msg.pdu.query_sm_resp.message_id, "%ld", smpp_msg.pdu.query_sm_resp.head.sequence_number); smpp_msg.pdu.query_sm_resp.final_date[0] = 0x0; if (pointer == -1) { smpp_msg.pdu.query_sm_resp.message_state = 0x04; } else { smpp_msg.pdu.query_sm_resp.message_state = 0x02; } smpp_msg.pdu.query_sm_resp.error_code = 0x0; smpp_send(linkNo, &smpp_msg, dataLen); break; case 0x14: /* QUERY SM RESP */ printf("[TEST PROGRAM]Link %d received QUERY SM RESP.\n", linkNo); break; case 0x15: /* CANCEL SM */ printf("[TEST PROGRAM]Link %d received CANCEL SM RESP.\n", linkNo); pointer = -1; if (pMsg->pdu.cancel_sm.message_id[0] == 0){ memset(store_msg_buf[linkNo], 0, sizeof(SMPP_MSG_STORE_STRUCT)*STORE_MSG_ITEM); msg_pointer[linkNo] = 0; smpp_msg.pdu.cancel_sm_resp.head.command_status = htonl(ESME_ROK); }else{ for (i=0; ipdu.cancel_sm.message_id, store_msg_buf[linkNo][i].message_id); if (strcmp(store_msg_buf[linkNo][i].message_id, pMsg->pdu.cancel_sm.message_id) == 0) { pointer = i; break; } } if (pointer == -1) { smpp_msg.pdu.cancel_sm_resp.head.command_status = htonl(ESME_RCANCELFAIL); }else{ smpp_msg.pdu.cancel_sm_resp.head.command_status = htonl(ESME_ROK); } } /* fill with CANCEL SM RESP structure */ smpp_msg.message_type = 0x16; smpp_msg.pdu.cancel_sm_resp.head.sequence_number = pMsg->pdu.cancel_sm.head.sequence_number; smpp_send(linkNo, &smpp_msg, dataLen); break; case 0x16: /* CANCEL SM RESP */ printf("[TEST PROGRAM]Link %d received CANCEL RESP.\n", linkNo); break; case 0x17: /* REPLACE SM */ printf("[TEST PROGRAM]Link %d received REPLACE SM.\n", linkNo); pointer = -1; for (i=0; ipdu.replace_sm.message_id) == 0) { pointer = i; break; } } /* store new smpp message */ if (pointer != -1) { //if (pMsg->pdu.replace_sm.sm_length < SMPP_MSG_LEN) { memcpy(&store_msg_buf[linkNo][pointer].short_message, pMsg->pdu.replace_sm.short_message, pMsg->pdu.replace_sm.sm_length); } } /* fill with REPLACE SM RESP structure */ smpp_msg.message_type = 0x18; if (pointer == -1) { smpp_msg.pdu.replace_sm_resp.head.command_status = htonl(ESME_RREPLACEFAIL); } else { smpp_msg.pdu.replace_sm_resp.head.command_status = htonl(ESME_ROK); } smpp_msg.pdu.replace_sm_resp.head.sequence_number = pMsg->pdu.replace_sm.head.sequence_number; smpp_send(linkNo, &smpp_msg, dataLen); break; case 0x18: /* REPLACE SM RESP */ printf("[TEST PROGRAM]Link %d received REPLACE SM RESP.\n", linkNo); break; default: return 0; } return sequence_number; } int smpp_onLinkStateChange(BYTE linkNo, BYTE linkstate) { printf("[TEST PROGRAM]link %d state change to %d\n",linkNo, linkstate); if (linkstate == 1) stateFlag[linkNo] = TRUE; else stateFlag[linkNo] = FALSE; return linkNo; } //0-submit_sm, 1-deliver_sm, 2-data_sm void sendTestSM(int linkNo, int msg_type) { SMPP_MSG smpp_msg; WORD dataLen; memset(&smpp_msg, 0, sizeof(SMPP_MSG)); switch (msg_type) { case 0: //send submit sm smpp_msg.message_type = 0x0B; smpp_msg.optional_param_flag1 = 0x0FF67FFF; smpp_msg.optional_param_flag2 = 0x0; //Mandatory parameters. strcpy(smpp_msg.pdu.submit_sm.service_type, "USSD"); smpp_msg.pdu.submit_sm.source_addr_ton = 0x2; //National smpp_msg.pdu.submit_sm.source_addr_npi = 0x1; //ISDN strcpy(smpp_msg.pdu.submit_sm.source_addr, "172.18.166.1"); smpp_msg.pdu.submit_sm.dest_addr_ton = 0x2; //National smpp_msg.pdu.submit_sm.dest_addr_npi = 0x1; //ISDN strcpy(smpp_msg.pdu.submit_sm.destination_addr, "13923482085"); smpp_msg.pdu.submit_sm.esm_class = 0x00; smpp_msg.pdu.submit_sm.protocol_id = 0x0; smpp_msg.pdu.submit_sm.priority_flag = 0x3; strcpy(smpp_msg.pdu.submit_sm.schedule_delivery_time, "041231151200000R"); strcpy(smpp_msg.pdu.submit_sm.validity_period, "041231151200000+"); smpp_msg.pdu.submit_sm.registered_delivery = 0x11; smpp_msg.pdu.submit_sm.replace_if_present_flag = 0x01; smpp_msg.pdu.submit_sm.data_coding = 0x07; smpp_msg.pdu.submit_sm.sm_default_msg_id = 0x01; smpp_msg.pdu.submit_sm.sm_length = 0x0; strcpy(smpp_msg.pdu.submit_sm.short_message, ""); //Optional parameters. smpp_msg.pdu.submit_sm.user_message_reference = 0x00; smpp_msg.pdu.submit_sm.source_port = 0x6666; smpp_msg.pdu.submit_sm.source_addr_subunit = 0x01; smpp_msg.pdu.submit_sm.destination_port = 0x8888; smpp_msg.pdu.submit_sm.dest_addr_subunit = 0x02; smpp_msg.pdu.submit_sm.sar_msg_ref_num = 0x7700; smpp_msg.pdu.submit_sm.sar_total_segments = 0x01; smpp_msg.pdu.submit_sm.sar_segment_seqnum = 0x01; smpp_msg.pdu.submit_sm.more_messages_to_send = 0x01; smpp_msg.pdu.submit_sm.payload_type = 0x00; strcpy(smpp_msg.pdu.submit_sm.message_payload, "messae in submit sm optional"); smpp_msg.pdu.submit_sm.privacy_indicator = 0x03; //strcpy(smpp_msg.pdu.submit_sm.callback_num, "F0453245"); smpp_msg.pdu.submit_sm.callback_num[0] = 0x01; //ASCII smpp_msg.pdu.submit_sm.callback_num[1] = 0x02; //National smpp_msg.pdu.submit_sm.callback_num[2] = 0x03; //Data strcpy(smpp_msg.pdu.submit_sm.callback_num+3, "F0453245"); smpp_msg.pdu.submit_sm.callback_num_pres_ind = 0x06; strcpy(smpp_msg.pdu.submit_sm.callback_num_atag, "callback_num_atag"); //strcpy(smpp_msg.pdu.submit_sm.source_subaddress, "123"); //smpp_msg.pdu.submit_sm.source_subaddress[0] = 0x88; //strcpy(smpp_msg.pdu.submit_sm.source_subaddress+1,"123"); //smpp_msg.pdu.submit_sm.dest_subaddress[0] = 0x88; //strcpy(smpp_msg.pdu.submit_sm.dest_subaddress+1, "789"); smpp_msg.pdu.submit_sm.user_response_code = 0x03; smpp_msg.pdu.submit_sm.display_time = 0x02; //smpp_msg.pdu.submit_sm.sms_signal = 0x03; smpp_msg.pdu.submit_sm.ms_validity = 0x03; smpp_msg.pdu.submit_sm.ms_msg_wait_facilities = 0x03; smpp_msg.pdu.submit_sm.number_of_messages = 0x03; smpp_msg.pdu.submit_sm.alert_on_message_delivery = 0x03; smpp_msg.pdu.submit_sm.language_indicator = 0x01; smpp_msg.pdu.submit_sm.its_reply_type = 0x03; smpp_msg.pdu.submit_sm.its_session_info[0] = 0x01; smpp_msg.pdu.submit_sm.its_session_info[1] = 0x53; smpp_msg.pdu.submit_sm.ussd_service_op = 0x03; break; case 1: //send deliver sm smpp_msg.message_type = 0x0F; smpp_msg.optional_param_flag1 = 0x000167FF; smpp_msg.optional_param_flag2 = 0x0; //Mandatory parameters. strcpy(smpp_msg.pdu.deliver_sm.service_type, "USSD"); smpp_msg.pdu.deliver_sm.source_addr_ton = 0x2; //National smpp_msg.pdu.deliver_sm.source_addr_npi = 0x1; //ISDN strcpy(smpp_msg.pdu.deliver_sm.source_addr, "172.18.166.1"); smpp_msg.pdu.deliver_sm.dest_addr_ton = 0x2; //National smpp_msg.pdu.deliver_sm.dest_addr_npi = 0x1; //ISDN strcpy(smpp_msg.pdu.deliver_sm.destination_addr, "13923482085"); smpp_msg.pdu.deliver_sm.esm_class = 0x00; smpp_msg.pdu.deliver_sm.protocol_id = 0x0; smpp_msg.pdu.deliver_sm.priority_flag = 0x3; smpp_msg.pdu.deliver_sm.schedule_delivery_time = 0x0; smpp_msg.pdu.deliver_sm.validity_period = 0x0; smpp_msg.pdu.deliver_sm.registered_delivery = 0x12; smpp_msg.pdu.deliver_sm.replace_if_present_flag = 0x01; smpp_msg.pdu.deliver_sm.data_coding = 0x07; smpp_msg.pdu.deliver_sm.sm_default_msg_id = 0x01; smpp_msg.pdu.deliver_sm.sm_length = 0x0; strcpy(smpp_msg.pdu.deliver_sm.short_message, ""); //Optional parameters. smpp_msg.pdu.deliver_sm.user_message_reference = 0x00; smpp_msg.pdu.deliver_sm.source_port = 0x6666; smpp_msg.pdu.deliver_sm.destination_port = 0x8888; smpp_msg.pdu.deliver_sm.sar_msg_ref_num = 0x7700; smpp_msg.pdu.deliver_sm.sar_total_segments = 0x01; smpp_msg.pdu.deliver_sm.sar_segment_seqnum = 0x01; smpp_msg.pdu.deliver_sm.user_response_code = 0x03; smpp_msg.pdu.deliver_sm.privacy_indicator = 0x03; smpp_msg.pdu.deliver_sm.payload_type = 0x00; strcpy(smpp_msg.pdu.deliver_sm.message_payload, "messae in deliver sm optional"); smpp_msg.pdu.deliver_sm.callback_num[0] = 0x01; //ASCII smpp_msg.pdu.deliver_sm.callback_num[1] = 0x02; //National smpp_msg.pdu.deliver_sm.callback_num[2] = 0x03; //Data strcpy(smpp_msg.pdu.deliver_sm.callback_num+3, "F0453245"); //smpp_msg.pdu.deliver_sm.source_subaddress[0] = 0xA0; //smpp_msg.pdu.deliver_sm.source_subaddress[1] = 0x01; //strcpy(smpp_msg.pdu.deliver_sm.source_subaddress+1, "123"); //smpp_msg.pdu.deliver_sm.dest_subaddress[0] = 0xA0; //smpp_msg.pdu.deliver_sm.dest_subaddress[1] = 0x01; //strcpy(smpp_msg.pdu.deliver_sm.dest_subaddress+1, "321"); smpp_msg.pdu.deliver_sm.language_indicator = 0x01; smpp_msg.pdu.deliver_sm.its_session_info[0] = 0x01; smpp_msg.pdu.deliver_sm.its_session_info[1] = 0x53; //smpp_msg.pdu.deliver_sm.network_error_code[0] = 0x03; //smpp_msg.pdu.deliver_sm.network_error_code[1] = 0x03; //smpp_msg.pdu.deliver_sm.network_error_code[2] = 0x00; smpp_msg.pdu.deliver_sm.message_state = 0x01; //strcpy(smpp_msg.pdu.deliver_sm.receipted_message_id, "receipted message id in deliver sm"); break; case 2: //data_sm smpp_msg.message_type = 0x11; smpp_msg.optional_param_flag1 = 0xB3EBFFFF; smpp_msg.optional_param_flag2 = 0x0000003F; //smpp_msg.optional_param_flag1 = 0x0C000000; //smpp_msg.optional_param_flag2 = 0x0; //Mandatory parameters. strcpy(smpp_msg.pdu.data_sm.service_type, "USSD"); smpp_msg.pdu.data_sm.source_addr_ton = 0x2; //National smpp_msg.pdu.data_sm.source_addr_npi = 0x1; //ISDN strcpy(smpp_msg.pdu.data_sm.source_addr, "172.18.166.1"); smpp_msg.pdu.data_sm.dest_addr_ton = 0x2; //National smpp_msg.pdu.data_sm.dest_addr_npi = 0x1; //ISDN strcpy(smpp_msg.pdu.data_sm.destination_addr, "13923482085"); smpp_msg.pdu.data_sm.esm_class = 0x00; smpp_msg.pdu.data_sm.registered_delivery = 0x11; smpp_msg.pdu.data_sm.data_coding = 0x07; //Optional parameters. smpp_msg.pdu.data_sm.source_port = 0x6666; smpp_msg.pdu.data_sm.source_addr_subunit = 0x01; smpp_msg.pdu.data_sm.source_network_type = 0x01; smpp_msg.pdu.data_sm.source_bearer_type = 0x03; smpp_msg.pdu.data_sm.source_telematics_id = 0x0003; smpp_msg.pdu.data_sm.destination_port = 0x8888; smpp_msg.pdu.data_sm.dest_addr_subunit = 0x03; smpp_msg.pdu.data_sm.dest_network_type = 0x03; smpp_msg.pdu.data_sm.dest_bearer_type = 0x03; smpp_msg.pdu.data_sm.dest_telematics_id = 0x0003; smpp_msg.pdu.data_sm.sar_msg_ref_num = 0x7700; smpp_msg.pdu.data_sm.sar_total_segments = 0x01; smpp_msg.pdu.data_sm.sar_segment_seqnum = 0x01; smpp_msg.pdu.data_sm.more_messages_to_send = 0x01; smpp_msg.pdu.data_sm.qos_time_to_live = 0x000000FF; smpp_msg.pdu.data_sm.payload_type = 0x00; strcpy(smpp_msg.pdu.data_sm.message_payload, "smpp message in data sm optional"); smpp_msg.pdu.data_sm.set_dpf = 0x01; //strcpy(smpp_msg.pdu.data_sm.receipted_message_id, "receipted message id"); smpp_msg.pdu.data_sm.message_state = 0x01; //smpp_msg.pdu.data_sm.network_error_code[0] = 0x03; //smpp_msg.pdu.data_sm.network_error_code[1] = 0x03; //smpp_msg.pdu.data_sm.network_error_code[2] = 0x00; smpp_msg.pdu.data_sm.user_message_reference = 0x00; smpp_msg.pdu.data_sm.privacy_indicator = 0x03; smpp_msg.pdu.data_sm.callback_num[0] = 0x01; smpp_msg.pdu.data_sm.callback_num[1] = 0x02; smpp_msg.pdu.data_sm.callback_num[2] = 0x03; strcpy(smpp_msg.pdu.data_sm.callback_num+3, "call back num"); smpp_msg.pdu.data_sm.callback_num_pres_ind = 0x06; strcpy(smpp_msg.pdu.data_sm.callback_num_atag, "callback num atag in data sm"); smpp_msg.pdu.data_sm.source_subaddress[0] = 0xA0; smpp_msg.pdu.data_sm.source_subaddress[1] = 0x01; //strcpy(smpp_msg.pdu.data_sm.source_subaddress+1, "123"); smpp_msg.pdu.data_sm.dest_subaddress[0] = 0xA0; smpp_msg.pdu.data_sm.dest_subaddress[1] = 0x01; //strcpy(smpp_msg.pdu.data_sm.dest_subaddress+1, "321"); smpp_msg.pdu.data_sm.user_response_code = 0x03; smpp_msg.pdu.data_sm.display_time = 0x02; //smpp_msg.pdu.data_sm.sms_signal = 0x03; smpp_msg.pdu.data_sm.ms_validity = 0x03; smpp_msg.pdu.data_sm.ms_msg_wait_facilities = 0x03; smpp_msg.pdu.data_sm.number_of_messages = 0x03; smpp_msg.pdu.data_sm.alert_on_message_delivery = 0x03; smpp_msg.pdu.data_sm.language_indicator = 0x01; smpp_msg.pdu.data_sm.its_reply_type = 0x03; smpp_msg.pdu.data_sm.its_session_info[0] = 0x01; smpp_msg.pdu.data_sm.its_session_info[1] = 0x53; break; } smpp_send(linkNo, &smpp_msg, dataLen); }