apidemo_cmpp.c
上传用户:xd_1999
上传日期:2007-05-02
资源大小:2286k
文件大小:10k
源码类别:

手机短信编程

开发平台:

Java

  1. #include    <stdio.h>
  2. #include  <stdlib.h>
  3. #include    <netinet/in.h>
  4. #include    "proxy_api.h"
  5. main(argc,argv)
  6. int argc;
  7. char *argv[]; 
  8. {
  9.     int  ret;
  10.     int  i, j;
  11.     /* 
  12.     ** 发送短消息所需要的参数 
  13.     */
  14.     unsigned char   byPkTotal;
  15.     unsigned char   byPkNumber;
  16.     unsigned char   byRegistered;
  17.     unsigned char   byMsgLevel;
  18.     char            pServiceID[11];
  19.     unsigned char   byFeeUserType;
  20.     char            psFeeTerminalID[21];
  21.     unsigned char   byTPPID;
  22.     unsigned char   byTPUDHI;
  23.     unsigned char   byMsgFmt;
  24.     char            psMsgSrc[7];
  25.     char            psFeeType[3];
  26.     char            psFeeCode[7];
  27. char            psFixedFee[7];
  28.     char            psValidTime[18];
  29.     char            psAtTime[18];
  30.     char            psSrcTerminalID[21];
  31.     unsigned char   byDestUsrTl;
  32.     char            psDestTerminalID[2100];
  33.     unsigned char   byMsgLength;
  34.     char            pMsgContent[252];
  35.     char            pReserve[9];
  36.     unsigned char  *tmpPsz;
  37.     /* 
  38.     ** 消息发送的序列号,用于配对发送消息以及其应答消息, 由应用程序保证其唯一 
  39.     */
  40.     unsigned short  LID, sLid;
  41.     /* 
  42.     ** 接收提交消息的应答消息所需要的缓冲参数 
  43.     */
  44.     unsigned char   sGWMsgID[8];  
  45.     int             iStatus;    
  46.     /* 
  47.     ** 接收短消息所需要的缓冲参数 
  48.     */
  49.     unsigned char   pMsgID[8];  
  50.     char            pDestnationID[21], pSrcminalID[21];
  51.     unsigned char   byTPID;
  52.     /*
  53.     ** 用于状态报告的缓冲区
  54.     */
  55.     char            sState[8];
  56. char            sSubmitTime[11];
  57. char            sDoneTime[11];
  58. char            sDestAddr[21];
  59.     unsigned long   ulSmscSequenceID;
  60.     LID = 0;
  61.     /* 
  62.     ** 连接到短消息代理服务器上 
  63.     */
  64.     ret = proxy_login(1);
  65.     if(ret == SUCCESS)
  66.         printf("nLogin Success!");
  67.     else if(ret == FAILURE)
  68.         printf("nLogin Fail!");
  69.     else
  70.         printf("nFatal Error!");
  71.     for(;;)
  72.     {
  73.         /******************************************************
  74.         ** 提交短消息 
  75.         *******************************************************/
  76.         LID ++;           /* 应用程序自身的序列号,请自己保证唯一,建议采用++操作 */
  77.         /* 请设置为1 */
  78.         byPkTotal = 1;
  79.         /* 请设置为1 */
  80.         byPkNumber = 1;
  81.         /* 1:需要状态报告,0:不需要状态报告 */
  82.         byRegistered = 1;
  83.         /* 信息级别 */
  84.         byMsgLevel = 0;
  85.         /* 业务类别 */
  86.         strcpy(pServiceID, "goodnews");
  87.         /* 计费用户类型字段,
  88.         *  0:对目的终端MSISDN计费;
  89.            1:对源终端MSISDN计费;
  90.            2:对SP计费;
  91.            3:表示本字段无效,对谁计费参见psFeeTerminalID参数。 
  92.         */
  93.         byFeeUserType = 0; 
  94.         /* 被计费用户的号码(如本字节填空,则表示本字段无效,
  95.         对谁计费参见byFeeUserType字段。本字段与Fee_UserType字段互斥)
  96.         */
  97.         strcpy(psFeeTerminalID, "");
  98.         /*
  99.         GSM协议类型。详细是解释请参考GSM03.40中的9.2.3.9
  100.         此值请填0
  101.         */
  102.         byTPPID = 0;
  103.         /*
  104.         GSM协议类型。表示是否有头结构,详细是解释请参考GSM03.40中的9.2.3.23,仅使用1位,右对齐
  105.         请填写为0。
  106.         */
  107.         byTPUDHI = 0;
  108.         /*
  109.         信息格式  0:ASCII串  3:短信写卡操作  4:二进制信息  8:UCS2编码15:含GB汉字  。。。。。。
  110.         */
  111.         byMsgFmt = 8;
  112.         /* 信息内容来源(SP_ID) */
  113.         strcpy(psMsgSrc, "huawei");
  114.         /* 资费类别 */
  115.         strcpy(psFeeType, "01");
  116.         /* 资费代码(以分为单位)*/
  117.         strcpy(psFeeCode, "999");
  118. /* 包月费率(以分为单位) */
  119.         strcpy(psFixedFee, "");
  120.         /* 有效期,一般填空 */
  121.         strcpy(psValidTime, "");
  122.         /* 定时发送时间,一般填空 */
  123.         strcpy(psAtTime, "");
  124.         /* 源终端MSISDN号码, 即此短消息的主叫地址*/
  125.         strcpy(psSrcTerminalID, "3333");
  126.         /* 目的地址个数,强制为1,为以后扩展使用 */
  127.         byDestUsrTl = 1;
  128.         /* 目的用户手机号码 */        
  129.         strcpy(psDestTerminalID, "13988800001");
  130.         /*strcpy(psDestTerminalID + 21, "13988800002");
  131.         strcpy(psDestTerminalID + 42, "13988800003");
  132.         strcpy(psDestTerminalID + 63, "13988800004");
  133.         strcpy(psDestTerminalID + 84, "13988800005");
  134.         */
  135.         /* 短消息内容 */
  136.         strcpy(pMsgContent, "提交短消息678901234567890");
  137.         /* 短消息内容的长度 */
  138.         byMsgLength = strlen(pMsgContent);
  139.         /* 保留字段,请填为空 */
  140.         strcpy(pReserve, "");
  141.         
  142.         ret = CMPPSnd(
  143.             byPkTotal,            
  144.             byPkNumber,
  145.             byRegistered,
  146.             byMsgLevel,
  147.             pServiceID,
  148.             byFeeUserType,
  149.             psFeeTerminalID,
  150.             byTPPID,
  151.             byTPUDHI,
  152.             byMsgFmt,
  153.             psMsgSrc,
  154.             psFeeType,
  155.             psFeeCode,
  156.             psValidTime,
  157.             psAtTime,
  158.             psSrcTerminalID,
  159.             byDestUsrTl,
  160.             psDestTerminalID,
  161.             byMsgLength,
  162.             pMsgContent,
  163.             pReserve,
  164.             LID);
  165.         
  166. if( ret == SUCCESS)
  167.             printf("nnSubmit SM Success!");
  168.         else if(ret == FAILURE)
  169.         {
  170.             printf("nSubmit SM Fail!");
  171.             getchar();
  172.         }
  173.         else
  174.         {
  175.             printf("nFatal Error!");
  176.             getchar();
  177.         }
  178.         /* 
  179.         ** 接收 CMPP 提交消息的应答
  180.         */
  181.         
  182.    
  183.         ret = SMGPGetSndAck( &sLid,   /*此值为发送短消息时,应用程序填写的序列号*/
  184.                     sGWMsgID,         /* 网关返回的消息ID */
  185.                     &iStatus);        /* 此条生消息发送到短消息中心时的状态,0表示成功,其它表示失败 */
  186. while( ret == SUCCESS )  /* 接收成功 */
  187. {
  188. printf("nrecv submit ack:LID = %d", LID);
  189.             printf("nGW Msg ID:");
  190.             for (i = 0; i < 8; i++)
  191.             {
  192.                 printf("%02x_", sGWMsgID[i]);
  193.             }
  194. printf("nStatus:%dn", iStatus);
  195.             /* 看是否还有其这的应答消息 */
  196.             
  197. ret = CMPPGetSndAck( &sLid,
  198.                     sGWMsgID,
  199.                     &iStatus);
  200. }        
  201.         /* 
  202.         ** 接收从短消息中心下发来的短消息 
  203.         */
  204.         memset(pMsgContent, 0, sizeof(pMsgContent));
  205.         memset(pReserve, 0, sizeof(pReserve));
  206.         
  207.         ret = CMPPRcv(
  208.                   pMsgID,         //   网关返回的消息ID   
  209.                   pDestnationID,  //   目的地址,一般为业务的接入地址码   
  210.                   pServiceID,     //   业务类别   
  211.                   &byTPID,        
  212.                   &byTPUDHI,
  213.                   &byMsgFmt,      //  信息格式  0:ASCII串  3:短信写卡操作  4:二进制信息  8:UCS2编码15:含GB汉字  。。。。。。  
  214.                   pSrcminalID,    //   主叫号码,一般为用户手机号码   
  215.                   &byRegistered,  //   1:需要状态报告,0:普通短消息   
  216.                   &byMsgLength,   //   短消息的长度   
  217.                   pMsgContent,    //   短消息的内容    
  218.                   pReserve);        
  219.         while( ret == SUCCESS)
  220.         {
  221.             printf("nnRecv sm Success!"); 
  222.             printf("npMsgID:");
  223.             for (i = 0; i < 8; i++)
  224.             {
  225.                 printf("%02x_", pMsgID[i]);
  226.             };
  227.             printf("npDestnationID:%s", pDestnationID);
  228.             printf("npServiceID:%s", pServiceID);
  229.             printf("nbyTPID:%d", byTPID);
  230.             printf("nbyTPUDHI:%d", byTPUDHI);
  231.             printf("nbyMsgFmt:%d", byMsgFmt);
  232.             printf("npSrcminalID:%s", pSrcminalID);
  233.             printf("nbyRegistered:%d", byRegistered);
  234.             printf("nbyMsgLength:%d", byMsgLength);
  235.             printf("npReserve:%s", pReserve);
  236.             if (byRegistered == 1) //状态报告消息 
  237.             {
  238.                 Cmpp_Status_Report *pStatus = (Cmpp_Status_Report *)pMsgContent;
  239.                 
  240.                 memset(sState, 0, sizeof(sState));
  241.                 memset(sSubmitTime, 0, sizeof(sSubmitTime));
  242.                 memset(sDoneTime, 0, sizeof(sDoneTime));
  243.                 memset(sDestAddr, 0, sizeof(sDestAddr));
  244.                 memcpy(sState, pStatus->sState, 7);
  245.                 memcpy(sSubmitTime, pStatus->sSubmitTime, 10);
  246.                 memcpy(sDoneTime, pStatus->sDoneTime, 10);
  247.                 memcpy(sDestAddr, pStatus->sDestAddr, 20);
  248.                 ulSmscSequenceID = ntohl(pStatus->ulSmscSequenceID);
  249.                 printf("nnpStatus report:");
  250.                 printf("npMsgID:");
  251.                 for (i = 0; i < 8; i++)
  252.                 {
  253.                     printf("%02x_", pStatus->byMsgID[i]);
  254.                 };
  255.                 printf("nsState:%s", sState);
  256.                 printf("nsSubmitTime:%s", sSubmitTime);
  257.                 printf("nsDoneTime:%s", sDoneTime);
  258.                 printf("nsDestAddr:%s", sDestAddr);
  259.                 printf("nulSmscSequenceID:%d", ulSmscSequenceID);
  260.             }
  261.             else // 一般的下发短消息 
  262. {
  263.                 printf("npMsgContent(txt):%s", pMsgContent);            
  264.             }
  265. printf("npMsgContent(txt):%s", pMsgContent);     
  266.             
  267.             /* 
  268.             ** 短消息内容的16进制显示 
  269.             */
  270.             printf("nSM Data Content(hex):");            
  271.             tmpPsz = (unsigned char *)pMsgContent;
  272.             while(byMsgLength > 0)
  273.             {
  274.                 if(byMsgLength > 16)
  275.                     j = 16;
  276.                 else
  277.                     j = byMsgLength;
  278.                 for(i = 0; i< j; i++)
  279.                 {
  280.                     printf("%02x_", *(tmpPsz + i));
  281.                 }
  282.                 printf("rn");
  283.                 byMsgLength -= j;
  284.                 tmpPsz += 16;
  285.             }
  286.             
  287.             /* 
  288.             ** 看是否还有其它的消息 
  289.             */
  290.             memset(pMsgContent, 0, sizeof(pMsgContent));
  291.             memset(pReserve, 0, sizeof(pReserve));
  292.         
  293.             ret = CMPPRcv(
  294.                     pMsgID,
  295.                     pDestnationID,
  296.                     pServiceID,
  297.                     &byTPID,
  298.                     &byTPUDHI,
  299.                     &byMsgFmt,
  300.                     pSrcminalID,
  301.                     &byRegistered,
  302.                     &byMsgLength,
  303.                     pMsgContent,
  304.                     pReserve);
  305.         }
  306.         
  307.         
  308.         /* 等待3秒 */
  309.         sleep(1);
  310.         /*
  311.         i = getchar();
  312.         */
  313.     }
  314. }