main.c
上传用户:yyhongfa
上传日期:2013-01-18
资源大小:267k
文件大小:14k
开发平台:

C/C++

  1. /****************************************************************************
  2. *main.c
  3. ****************************************************************************/
  4. #ifndef MAIN_C
  5. #define MAIN_C
  6. #include  "config.h"
  7. ////#include "IAP.h"
  8. ////#include "ffs.h"
  9. #include "uart.h"
  10. #include "timer.h"
  11. #include "main.h"
  12. #include "udp.h"
  13. #include "tcp.h"
  14. #include "ip_addr.h"
  15. #include "err.h"
  16. #define SERVER_IP   0x5250ad3d
  17. //#define SERVER_IP   0x1A01CECA  /*telnet:bbs.hbu.edu.cn*/
  18. //#define SERVER_IP     0xEE086FA6
  19. #define SERVER_PORT   23
  20. #define GPRS_NUM  "ATD*99***1#r"
  21. #define MAX_GPRS_DIAL_TIMES  3
  22. #define GPRS_DIAL_TIMEOUT  60
  23. #define MAX_MODEM_OPEN_TIME 3
  24. #define PARSE_STOP     ','
  25. #define STRING_START    '"'
  26. #define STRING_END         '"'
  27. #define CR           'r'
  28. #define LF           'n'
  29. #define AT_PARSE_FAIL        0
  30. #define AT_PARSE_SUCCESS   0
  31. #define AT_SEND_OK     0
  32. #define AT_SEND_FAIL  -1
  33. typedef enum
  34. {
  35.     PPP_DEAD = 0,
  36.     PPP_ESTABLISHED,
  37.     UDP_ESTABLISHED,
  38.     TCP_CONNECTING,
  39.     TCP_ESTABLISHED,
  40.     TCP_CLOSING,
  41.     
  42.     TCPIP_STATE_UNKNOWN, /*ppp expected unknown err, such as no response from Modem ,we must reset System*/
  43. }MY_TCPIP_STATE;
  44. typedef enum
  45. {
  46.    SYS_ERR_OK = 0,
  47.    SYS_ERR_AT_FAIL,
  48.    SYS_ERR_FAIL_OTHERS
  49. }sys_err;
  50. typedef struct
  51. {
  52.     char *command; /*the AT Command we send to Modem*/
  53.     char *result;       /*the result we expected*/
  54.     u8_t timeout;      /*MAX time of second that we could waiting*/
  55.     u8_t r_time;       /*MAX times that we could re_send to Modem*/
  56. }basic_AT;
  57. const basic_AT basic_at[] = 
  58. {
  59.      { "ATE0r",                                                  "OKrn",   3,   3 },
  60.      { "AT+CGDCONT=1,"IP","CMNET"r",     "OKrn",   3,   3 },
  61.      {  "AT+CNMI=2r",                                     "OKrn",   3,   3},
  62.      { "AT+CLIP=1r",                                      "OKrn",    3,   3},
  63.      {"AT+CSMP=53,167,,0r",                          "OKrn",   3,   3},
  64.   
  65.      { NULL, NULL, 0, 0},
  66. };
  67. extern void pppInProc(int pd, u_char *s, int l);
  68. extern void pppInit(void);
  69. extern int pppOpen(void *fd, void (*linkStatusCB)(void *ctx, int errCode, void *arg), void *linkStatusCtx);
  70. extern BOOL  ppp_dead(int pd);
  71. /*
  72. *local function
  73. */
  74. sys_err wait_atReturn(CHAR *str, u8_t second);
  75. void lwip_init(void);
  76. void pppStateCallback(void *ctx, int errCode, void *arg);
  77. void local_sys_init(void);
  78. void second_sleep(u32_t s);
  79. sys_err open_Modem(void);
  80. sys_err active_Modem(void);
  81. void powerOff_Modem(void);
  82. void shut_system(void);
  83. sys_err init_Modem(void);
  84. sys_err dial_GPRS(void);
  85. void tcpip_open(void);
  86. MY_TCPIP_STATE my_tcpip_state = PPP_DEAD;
  87. int my_sys_err = 0;
  88. /*
  89. *return the state of my tcpip
  90. */
  91. MY_TCPIP_STATE get_Mytcpip_state(void)
  92. {
  93.       return my_tcpip_state;
  94. }
  95. /*
  96. *set the state of my tcpip
  97. */
  98. void set_Mytcpip_state(MY_TCPIP_STATE state)
  99. {
  100.     my_tcpip_state = state;
  101. }
  102. void add_sys_err(void)
  103. {
  104.     my_sys_err++;
  105. }
  106. void clear_sys_err(void)
  107. {
  108.    my_sys_err = 0;
  109. }
  110. int get_sys_err(void)
  111. {
  112.     return my_sys_err;
  113. }
  114. /*********************************
  115. function: wait_atReturn
  116. input:          str:   the string for wait
  117.                   second:  max second to wait
  118. *********************************/
  119. sys_err wait_atReturn(CHAR *str, u8_t second)
  120. {
  121.  CHAR *tmp_str = str;
  122.  u32_t time = get_sys_tick();
  123.  u8_t ch;
  124.    
  125.         while((get_sys_tick() - time) < second)
  126.         {
  127.      if(GET_AT(&ch) EQ DATA_READ_OK)
  128.      {
  129.              /*waiting for the string that we expected*/
  130.   if(*tmp_str  EQ ch)
  131.          {
  132.                       tmp_str++;
  133.   
  134. if(*tmp_str  EQ '')
  135.               {
  136.     return SYS_ERR_OK;
  137.               }
  138.          }  
  139.          else
  140.   {   
  141.  tmp_str = str;
  142.    }   
  143.      }  
  144.      }
  145.    return SYS_ERR_AT_FAIL;
  146. }
  147. void pppStateCallback(void *ctx, int errCode, void *arg)
  148. {
  149.     DEBUG_FUNCTION("pppStateCallback()");
  150.     if(errCode EQ 0)
  151.     {
  152.           set_Mytcpip_state(PPP_ESTABLISHED);
  153.     }
  154.   
  155.     return;
  156. }
  157. extern void pbuf_init(void);
  158. extern void mem_init(void);
  159. extern void  memp_init(void);
  160. extern void netif_init(void);
  161. extern void ip_init(void);
  162. extern void tcp_init(void);
  163. extern void udp_init(void);
  164. /*
  165. * initialize lwip basic function
  166. */
  167. void lwip_init(void)
  168. {
  169.       /*
  170.       *init memory
  171.       */
  172.       pbuf_init();
  173.       mem_init();
  174.       memp_init();
  175.      /*
  176.      *init tcpip stack
  177.      */
  178.      pppInit();
  179.      netif_init();
  180.      ip_init(); 
  181.      tcp_init();
  182.      udp_init();  
  183. }
  184. #define   LEDCON 0x00003c00
  185. /*
  186. *local system init
  187. */
  188. void local_sys_init(void)
  189. {
  190.     //  PINSEL0 = 0x00000000; // 设置所有管脚连接GPIO
  191.     //  PINSEL1 = 0x00000000;
  192.    //   IODIR = LEDCON;
  193.      set_Mytcpip_state(PPP_DEAD);
  194.      my_sys_err = 0;
  195.    
  196.     UART0_Ini();
  197.     UART1_Ini();
  198. }
  199. /*
  200. *block system for x seconds
  201. */
  202. void second_sleep(u32_t s)
  203. {
  204.     u32_t time = get_sys_tick();
  205.     while((get_sys_tick() - time) < s);
  206. }
  207. /*block system for x ms*/
  208. void ms_sleep(u32_t ms)
  209. {
  210.     u32_t time = sys_jiffies();
  211.     while(( sys_jiffies() - time) < ms);
  212. }
  213. /*
  214. * send AT to GPRS Modem, test if Modem is ready
  215. */
  216. sys_err open_Modem(void)
  217. {
  218.        SEND_AT("ATr");
  219. return (wait_atReturn("OKrn",3));
  220. }
  221. /*
  222. * active GPRS Modem
  223. */
  224. sys_err active_Modem(void)
  225. {
  226.      int i;
  227.      DEBUG_FUNCTION("active_Modem()");
  228.   
  229.       //add your code here for triging GPRS Modem
  230.       DEBUG_EVENT("System block  5 seconds!");
  231.       second_sleep(5);
  232.      for(i = 0; i < MAX_MODEM_OPEN_TIME; i++)
  233.      {
  234.           if(open_Modem() EQ SYS_ERR_OK)
  235.    return SYS_ERR_OK;
  236.      }
  237.      
  238.  return SYS_ERR_FAIL_OTHERS;
  239. }
  240. /*
  241. *power off GPRS Modem
  242. */
  243. void powerOff_Modem(void)
  244. {
  245.    DEBUG_FUNCTION("powerOff_Modem()");
  246.     //add your code  here
  247.     return;
  248. }
  249. /*
  250. *some seriours error happend
  251. *we power off Modem,then reset System
  252. */
  253. void shut_system(void)
  254. {
  255.    DEBUG_FUNCTION("shut_system()");
  256.   
  257.    powerOff_Modem();
  258.     //waiting for Watch Dog 
  259.     while(1);
  260. }
  261. /*
  262. *send some AT Commands to Modem ,
  263. *initlization some parametes , such as the unexpected 
  264. *result code of SMS, Incoming Call, GPRS Paramets etc.
  265. */
  266. sys_err init_Modem(void)
  267. {
  268.     const basic_AT* con = &(basic_at[0]);
  269.     u8_t r_time;
  270.     DEBUG_FUNCTION("init_Modem()");
  271.     while(con->command  NEQ NULL)
  272.     {
  273.          for(r_time = 0; r_time < con->r_time; r_time++)
  274.          {
  275.               SEND_AT(con->command);
  276.   
  277.        DEBUG_EVENT(con->command);
  278.    
  279.        if(wait_atReturn(con->result, con->timeout) EQ SYS_ERR_OK)
  280.        {
  281.      break;
  282.        }
  283.          }
  284.  /*
  285.  *one AT Commands send fail
  286.  */
  287.  if(r_time  EQ con->r_time)
  288.  {
  289.            DEBUG_EVENT("one at commands init fail!");
  290.     return SYS_ERR_AT_FAIL;
  291.  }
  292.  con++;
  293.     }
  294.     return SYS_ERR_OK;
  295. }
  296. /*
  297. *dial GPRS, if successful, we start LCP Config
  298. */
  299. sys_err dial_GPRS(void)
  300. {
  301.     int i;
  302.     for(i = 0; i < MAX_GPRS_DIAL_TIMES; i++)
  303.     {
  304.         SEND_AT(GPRS_NUM);
  305.  if(wait_atReturn("CONNECTrn", GPRS_DIAL_TIMEOUT) EQ SYS_ERR_OK)
  306.   return SYS_ERR_OK;
  307.  open_Modem();
  308.     }
  309.    return SYS_ERR_FAIL_OTHERS;
  310. }
  311. /*
  312. *receive some UDP data
  313. */
  314. void udp_data_ind(u8_t *buf, u16_t len)
  315. {
  316.         u16_t i = 0;
  317.  DEBUG_FUNCTION("udp_data_ind()")
  318.  for(i = 0; i < len; i++)
  319.  {
  320.      sendByte_2_user((u8_t)buf[i]);
  321.  }    
  322. }
  323. /*
  324. *receive some TCP data
  325. */
  326. void tcp_data_ind(u8_t *buf, u16_t len)
  327. {
  328.         u16_t i = 0;
  329.  DEBUG_FUNCTION("tcp_data_ind()")
  330.  for(i = 0; i < len; i++)
  331.  {
  332.      sendByte_2_user((u8_t)buf[i]);
  333.  }    
  334. }
  335. /*
  336. *ipcp is up, now we start open a TCP/UDP Connect
  337. */
  338. void tcpip_open(void)
  339. {
  340.        CONNECT_TYPE  conn_type = UDP_CONNECT_DEFAULT;
  341. struct ip_addr  remote_ip;
  342. struct udp_pcb * udp;
  343. struct tcp_pcb * tcp;
  344. s8_t   err;
  345.    
  346. DEBUG_FUNCTION("tcpip_open()")
  347. remote_ip.addr = SERVER_IP;
  348. if(conn_type EQ UDP_CONNECT_DEFAULT)
  349. {
  350. /*
  351. *new a udp pcb
  352. */
  353.      if((udp = udp_new()) EQ NULL)
  354.      {
  355.           DEBUG_EVENT("udp_new, but return NULL, system unexpected err!")
  356.    set_Mytcpip_state(TCPIP_STATE_UNKNOWN);
  357.    return;
  358.      }
  359.           /*
  360.           *write remote IP and server to udp pcb
  361.           */
  362.    if(udp_connect(udp, &(remote_ip), SERVER_PORT) NEQ ERR_OK)
  363.     {
  364.           DEBUG_EVENT("udp_connect, but return ERR, system unexpected err!")
  365.    set_Mytcpip_state(TCPIP_STATE_UNKNOWN);
  366.    return;
  367.     }
  368.    set_Mytcpip_state(UDP_ESTABLISHED);
  369. }
  370. else
  371. {
  372.               /*
  373. *new a tcp pcb
  374. */
  375.      if((tcp = tcp_new()) EQ NULL)
  376.      {
  377.           DEBUG_EVENT("udp_new, but return NULL, system unexpected err!")
  378.    set_Mytcpip_state(TCPIP_STATE_UNKNOWN);
  379.    return;
  380.      }
  381.      if(tcp_connect(tcp, &(remote_ip), SERVER_PORT, NULL) NEQ ERR_OK)
  382.      {
  383.           DEBUG_EVENT("tcp_connect, but return ERR, system unexpected err!")
  384.    set_Mytcpip_state(TCPIP_STATE_UNKNOWN);
  385.    return;
  386.      }
  387.    set_Mytcpip_state(TCP_CONNECTING);  
  388. }
  389. }
  390. /*
  391. *if you want to check the state of socket periodly
  392. *this func should be called
  393. *but the counte should be changed
  394. */
  395. #if TCPIP_KEEPALIVE
  396. int tcpip_keepalive_err = 0;
  397. void tcpip_keepalive(void)
  398. {
  399.      /*
  400.      *add you code here.
  401.      *你可以定义一个函数,增加到
  402.      * 定时器函数sys_timer_start(),每隔MAX_KEEPALIVE_PERIOD
  403.      *周期调用一次,如果等待MAX_SERVER_WATI_TIME
  404.      *没有返回的话tcpip_keepalive_err++.
  405.      *if(tcpip_keepalive_err >= MAX_KEEPALIVE_ERR_ALLOW)
  406.      *close TCP connect.  重新连接(对于UDP要shut PPP, Dial ppp again).
  407.      *如果TCP仍然连接不成功,考虑PPP dial again.
  408.      *如果PPP Congig Fail, 考虑shut system.
  409.      *
  410.      */
  411.    //以上建议仅供参考。我不打算实现。
  412. }
  413. #endif
  414. extern err_t udp_write(u8_t *data, u16_t len);
  415. extern struct tcp_pcb *tcp_active_pcbs; 
  416. /****************************************************************************
  417. * 名称:main()
  418. ****************************************************************************/
  419. int main(void)
  420.     u8_t buf[MAX_READ_LEN + 1];
  421.     int len,pd;
  422.     sys_err err;
  423.     
  424.     int * fd = NULL;
  425.     int *linkstateCx = NULL;
  426.     
  427.    /*
  428.    *init basic parametes of ourselves
  429.    */
  430.     sys_timer_init();
  431.     local_sys_init();
  432.     lwip_init();
  433.     DEBUG_FUNCTION("main()");
  434.    /*
  435.    *start config GPRS Modem, such as HUAWEI GTM900, MC35i,etc.
  436.    */
  437.     err = active_Modem();
  438.     if(err NEQ SYS_ERR_OK)
  439.     {
  440.         DEBUG_EVENT("Modem open failed! we must shut down system");
  441.         shut_system();
  442.     }
  443.    DEBUG_EVENT("Modem open successful!");
  444.    DEBUG_EVENT("System block 30 seconds!");
  445.    
  446.    /*
  447.    *wait for GPRS Modem serching network, 
  448.    * initinalize: such as SMS, Telephone Book etc.
  449.    */
  450.    second_sleep(30);
  451.    err = init_Modem();
  452.    if(err NEQ SYS_ERR_OK)
  453.    {
  454.         DEBUG_EVENT("Modem basic AT Commands initialize failed! we must shut down system");
  455.         shut_system();
  456.    }
  457.    DEBUG_EVENT("Modem basic AT Commands initialize successful");
  458.  /*
  459.  *start Dial GPRS Number, such as: *99***1#
  460.  */
  461.  start_tcpip:
  462.    err = dial_GPRS();
  463.    if(err NEQ SYS_ERR_OK)
  464.    {
  465.         DEBUG_EVENT("Dial GPRS Failed,we must shut down system!");
  466.         shut_system();
  467.    }
  468.    DEBUG_EVENT("Dial GPRS successful, now we start PPP Config!");
  469.   /*
  470.   *start config PPP,and send LCP packet to GPRS Modem
  471.   */
  472.     pd = pppOpen((void*)fd,  &pppStateCallback, (void *)linkstateCx);/*send lcp req to peer*/
  473.    while(1)
  474.    {
  475.         /*
  476.         *check if one timer is overflow, if so, run it
  477.         */
  478.  check_sys_timer();
  479.         /*
  480.         *check the receive buffer of Modem
  481.         */
  482.  if(get_modem_datalen() > 0)
  483.  {
  484.            //DEBUG_EVENT(" Receive some data from Modem:rn");
  485.     len = get_modem_data(buf);
  486.      pppInProc(pd,  buf, len);
  487.  }
  488.       /*
  489.       *check the receive buffer of User Equipment
  490.       */
  491.        if(get_user_datalen() > 0)
  492.        {
  493.            //DEBUG_EVENT("Receive some data from User Eqipment!");
  494.    
  495.     len = get_user_data(buf);
  496.    
  497.     if(get_Mytcpip_state() EQ UDP_ESTABLISHED)
  498.     {
  499.         if( udp_write(buf, len)NEQ ERR_OK)
  500.  {
  501.             DEBUG_ERR("Send some data to UDP Peer, but failed!");
  502.      //add you code here.
  503.      //store the data
  504.         }
  505.     }
  506.     else if(get_Mytcpip_state() EQ TCP_ESTABLISHED)
  507.     {
  508.        if( tcp_write(tcp_active_pcbs, (const void *)buf,  len, 1) NEQ ERR_OK)
  509.         {
  510.             DEBUG_ERR("Send some data to TCP Peer, but failed!");
  511.      //add you code here.
  512.      //store the data
  513.         }
  514.     }
  515.     else
  516.     {
  517.         DEBUG_EVENT("No TCP or UDP Connect exist!drop the data")
  518.     }
  519.        }
  520.    
  521. /*
  522. *check the state of ppp
  523. *if the link is DEAD, I deal it very simple,
  524. *do not release the source of PPP/TCP/IP/UDP etc. 
  525. *just call the function: lwip_init(). but it work.
  526. *then goto the head of main func.
  527.        *The state of DCD should be checked here,but I will ignore it. 
  528.        */ 
  529. if(ppp_dead(pd) EQ TRUE)
  530. {
  531.     lwip_init();
  532.     set_Mytcpip_state(PPP_DEAD);
  533.     goto start_tcpip;
  534. }  
  535.         /*
  536.         *check the state of my tcpip
  537.         */
  538.         switch(get_Mytcpip_state())
  539.         {
  540.               case PPP_ESTABLISHED:
  541.                           if(get_sys_err() < MAX_SYS_ERR_ALLOW)
  542.                           {
  543.                               tcpip_open();
  544.          break;
  545.                           }
  546.     else
  547.   set_Mytcpip_state(TCPIP_STATE_UNKNOWN);
  548.          
  549.        case  TCPIP_STATE_UNKNOWN:
  550.        shut_system();
  551.    break;
  552. default:
  553.  break;
  554.         }
  555. //hit  the WATCH DOG
  556.    }
  557. }
  558. #endif