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