apidemo_cngp.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. /* 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 = 15;
  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, "1062");
  132.         /* 目的地址个数,强制为1,为以后扩展使用 */
  133.         byDestUsrTl = 1;
  134.         /* 目的用户手机号码 */        
  135.         strcpy(psDestTerminalID, "07554000001");
  136.         strcpy(psDestTerminalID + 21, "07554000002");
  137.         strcpy(psDestTerminalID + 42, "07554000003");
  138.         strcpy(psDestTerminalID + 63, "07554000004");
  139.         strcpy(psDestTerminalID + 84, "07554000005");
  140.         /* 短消息内容 */
  141.         strcpy(pMsgContent, "提交短消息678901234567890");
  142.         /* 短消息内容的长度 */
  143.         byMsgLength = strlen(pMsgContent);
  144.         /* 保留字段,请填为空 */
  145.         strcpy(pReserve, "");
  146.         
  147. ret = CNGPSnd(
  148.     psMsgSrc,
  149.     3,
  150.             byRegistered,
  151.             byMsgLevel,
  152.             pServiceID,            
  153.             psFeeType,
  154.             0,
  155.             psFeeCode,            
  156.             byMsgFmt,            
  157. psValidTime,
  158.             psAtTime,
  159.             psSrcTerminalID,
  160.             psFeeTerminalID,
  161.             byDestUsrTl,
  162.             psDestTerminalID,
  163.             byMsgLength,
  164.             pMsgContent,
  165.             LID);
  166.         
  167. if( ret == SUCCESS)
  168.             printf("nnSubmit SM Success!");
  169.         else if(ret == FAILURE)
  170.         {
  171.             printf("nSubmit SM Fail!");
  172.             getchar();
  173.         }
  174.         else
  175.         {
  176.             printf("nFatal Error!");
  177.             getchar();
  178.         }
  179.         /* 
  180.         ** 接收 CMPP 提交消息的应答
  181.         */    
  182.         ret = CNGPGetSndAck( &sLid,   /*此值为发送短消息时,应用程序填写的序列号*/
  183.                     sGWMsgID,         /* 网关返回的消息ID */
  184.                     &iStatus);        /* 此条生消息发送到短消息中心时的状态,0表示成功,其它表示失败 */
  185. while( ret == SUCCESS )  /* 接收成功 */
  186. {
  187. printf("nrecv submit ack:LID = %d", LID);
  188.             printf("nGW Msg ID:");
  189.             for (i = 0; i < 10; i++)
  190.             {
  191.                 printf("%02x_", sGWMsgID[i]);
  192.             }
  193. printf("nStatus:%dn", iStatus);
  194.             /* 看是否还有其这的应答消息 */
  195. ret = CNGPGetSndAck( &sLid,
  196.                     sGWMsgID,
  197.                     &iStatus);
  198. }        
  199.         /* 
  200.         ** 接收从短消息中心下发来的短消息 
  201.         */
  202.         memset(pMsgContent, 0, sizeof(pMsgContent));
  203.         memset(pReserve, 0, sizeof(pReserve));
  204.         
  205. ret = CNGPRcv(
  206.          pMsgID,         /* 网关返回的消息ID */
  207.                 &byRegistered,  /* 1:需要状态报告,0:普通短消息 */       
  208.                 &byMsgFmt,      /*信息格式  0:ASCII串  3:短信写卡操作  4:二进制信息  8:UCS2编码15:含GB汉字  。。。。。。*/
  209.                 pRecvTime,
  210. pSrcminalID,    /* 主叫号码,一般为用户手机号码 */
  211.                 pDestnationID,  /* 目的地址,一般为业务的接入地址码 */
  212.                 &byMsgLength,   /* 短消息的长度 */
  213.                 pMsgContent);    /* 短消息的内容 */ 
  214.         while( ret == SUCCESS)
  215.         {
  216.             printf("nnRecv sm Success!"); 
  217.             printf("npMsgID:");
  218.             for (i = 0; i < 10; i++)
  219.             {
  220.                 printf("%02x_", pMsgID[i]);
  221.             };
  222.             printf("npRecvTime:%s", pRecvTime);
  223.             printf("npDestnationID:%s", pDestnationID);
  224.             printf("nbyMsgFmt:%d", byMsgFmt);
  225.             printf("npSrcminalID:%s", pSrcminalID);
  226.             printf("nbyRegistered:%d", byRegistered);
  227.             printf("nbyMsgLength:%d", byMsgLength);
  228.             /*printf("npReserve:%s", pReserve);  */
  229. /*
  230.             if (byRegistered == 1) //状态报告消息 
  231.             {
  232.                 Cmpp_Status_Report *pStatus = (Cmpp_Status_Report *)pMsgContent;
  233.                 
  234.                 memset(sState, 0, sizeof(sState));
  235.                 memset(sSubmitTime, 0, sizeof(sSubmitTime));
  236.                 memset(sDoneTime, 0, sizeof(sDoneTime));
  237.                 memset(sDestAddr, 0, sizeof(sDestAddr));
  238.                 memcpy(sState, pStatus->sState, 7);
  239.                 memcpy(sSubmitTime, pStatus->sSubmitTime, 10);
  240.                 memcpy(sDoneTime, pStatus->sDoneTime, 10);
  241.                 memcpy(sDestAddr, pStatus->sDestAddr, 20);
  242.                 ulSmscSequenceID = ntohl(pStatus->ulSmscSequenceID);
  243.                 printf("nnpStatus report:");
  244.                 printf("npMsgID:");
  245.                 for (i = 0; i < 8; i++)
  246.                 {
  247.                     printf("%02x_", pStatus->byMsgID[i]);
  248.                 };
  249.                 printf("nsState:%s", sState);
  250.                 printf("nsSubmitTime:%s", sSubmitTime);
  251.                 printf("nsDoneTime:%s", sDoneTime);
  252.                 printf("nsDestAddr:%s", sDestAddr);
  253.                 printf("nulSmscSequenceID:%d", ulSmscSequenceID);
  254.             }
  255.             else // 一般的下发短消息 
  256. {
  257.                 printf("npMsgContent(txt):%s", pMsgContent);            
  258.             }
  259. */
  260. printf("npMsgContent(txt):%s", pMsgContent);     
  261.             
  262.             /* 
  263.             ** 短消息内容的16进制显示 
  264.             */
  265.             printf("nSM Data Content(hex):");            
  266.             tmpPsz = (unsigned char *)pMsgContent;
  267.             while(byMsgLength > 0)
  268.             {
  269.                 if(byMsgLength > 16)
  270.                     j = 16;
  271.                 else
  272.                     j = byMsgLength;
  273.                 for(i = 0; i< j; i++)
  274.                 {
  275.                     printf("%02x_", *(tmpPsz + i));
  276.                 }
  277.                 printf("rn");
  278.                 byMsgLength -= j;
  279.                 tmpPsz += 16;
  280.             }
  281.             
  282.             /* 
  283.             ** 看是否还有其它的消息 
  284.             */
  285.             memset(pMsgContent, 0, sizeof(pMsgContent));
  286.             memset(pReserve, 0, sizeof(pReserve));
  287.         
  288.             ret = CNGPRcv(
  289.          pMsgID,         /* 网关返回的消息ID */
  290.                 &byRegistered,  /* 1:需要状态报告,0:普通短消息 */       
  291.                 &byMsgFmt,      /*信息格式  0:ASCII串  3:短信写卡操作  4:二进制信息  8:UCS2编码15:含GB汉字  。。。。。。*/
  292.                 pRecvTime,
  293. pSrcminalID,    /* 主叫号码,一般为用户手机号码 */
  294.                 pDestnationID,  /* 目的地址,一般为业务的接入地址码 */
  295.                 &byMsgLength,   /* 短消息的长度 */
  296.                 pMsgContent);    /* 短消息的内容 */ 
  297.         }
  298.         
  299.         
  300.         /* 等待3秒 */
  301.         sleep(1);
  302.         /*
  303.         i = getchar();
  304.         */
  305.     }
  306. }