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

手机短信编程

开发平台:

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