Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.93 KB | None | 0 0
  1. static int gsm_atCmd_waitResponse(char * cmd, char *resp, char * resp1, int cmdSize, int timeout, char **response, int size)
  2. {
  3.     int len, res = 1, idx = 0, tot = 0, timeoutCnt = 0;
  4.     char *sresp = malloc( 256 );
  5.     char *data = malloc( 256 );
  6.  
  7.     if( (sresp == NULL) || (data == NULL) )
  8.     {
  9.         return 0;
  10.     }
  11.  
  12.     memset( sresp, 0, 256 );
  13.     memset( data, 0, 256 );
  14.  
  15.     // ** Send command to GSM
  16.     vTaskDelay(100 / portTICK_PERIOD_MS);
  17.     uart_flush( gsm_uartNum );
  18.  
  19.     // Send command
  20.     if (cmd != NULL)
  21.     {
  22.         if (cmdSize == -1)
  23.         {
  24.             cmdSize = strlen(cmd);
  25.         }
  26.         #if GSM_DEBUG
  27.         gsm_infoCommand(cmd, cmdSize, "AT COMMAND:");
  28.         #endif
  29.         uart_write_bytes(gsm_uartNum, (const char*)cmd, cmdSize);
  30.         uart_wait_tx_done(gsm_uartNum, 100 / portTICK_RATE_MS);
  31.     }
  32.  
  33.     if (response != NULL)
  34.     {
  35.         // Read GSM response into buffer
  36.         char *pbuf = *response;
  37.         len = uart_read_bytes(gsm_uartNum, (uint8_t*)data, 256, timeout / portTICK_RATE_MS);
  38.  
  39.         while (len > 0)
  40.         {
  41.             if ((tot + len) >= size)
  42.             {
  43.                 char *ptemp = realloc(pbuf, size+512);
  44.                 if (ptemp == NULL) return 0;
  45.                 size += 512;
  46.                 pbuf = ptemp;
  47.             }
  48.             memcpy(pbuf + tot, data, len);
  49.             tot += len;
  50.             response[tot] = '\0';
  51.             len = uart_read_bytes(gsm_uartNum, (uint8_t*)data, 256, 100 / portTICK_RATE_MS);
  52.         }
  53.         *response = pbuf;
  54.         return tot;
  55.     }
  56.  
  57.     // ** Wait for and check the response
  58.     idx = 0;
  59.     while(1)
  60.     {
  61.         memset(data, 0, 256);
  62.         len = 0;
  63.         len = uart_read_bytes(gsm_uartNum, (uint8_t*)data, 256, 10 / portTICK_RATE_MS);
  64.         if (len > 0)
  65.         {
  66.             for (int i=0; i<len;i++)
  67.             {
  68.                 if (idx < 256)
  69.                 {
  70.                     if ((data[i] >= 0x20) && (data[i] < 0x80)) sresp[idx++] = data[i];
  71.                     else sresp[idx++] = 0x2e;
  72.                 }
  73.             }
  74.             tot += len;
  75.         }
  76.         else
  77.         {
  78.             if (tot > 0)
  79.             {
  80.                 // Check the response
  81.                 if (strstr(sresp, resp) != NULL)
  82.                 {
  83.                     #if GSM_DEBUG
  84.                     snprintf( debugBuffer, sizeof(debugBuffer), "AT RESPONSE: [%s]\n", sresp);
  85.                     uart_write_bytes( UART_NUM_2, debugBuffer, strlen(debugBuffer) );
  86.                     #endif
  87.                     break;
  88.                 }
  89.                 else
  90.                 {
  91.                     if (resp1 != NULL)
  92.                     {
  93.                         if (strstr(sresp, resp1) != NULL)
  94.                         {
  95.                             #if GSM_DEBUG
  96.                             snprintf( debugBuffer, sizeof(debugBuffer), "AT RESPONSE (1): [%s]\n", sresp);
  97.                             uart_write_bytes( UART_NUM_2, debugBuffer, strlen(debugBuffer) );
  98.                             #endif
  99.                             res = 2;
  100.                             break;
  101.                         }
  102.                     }
  103.                     // no match
  104.                     #if GSM_DEBUG
  105.                     snprintf( debugBuffer, sizeof(debugBuffer), "AT BAD RESPONSE: [%s]\n", sresp);
  106.                     uart_write_bytes( UART_NUM_2, debugBuffer, strlen(debugBuffer) );
  107.                     #endif
  108.                     res = 0;
  109.                     break;
  110.                 }
  111.             }
  112.         }
  113.  
  114.         timeoutCnt += 10;
  115.         if (timeoutCnt > timeout)
  116.         {
  117.             // timeout
  118.             #if GSM_DEBUG
  119.             sprintf( debugBuffer, "AT: TIMEOUT\n");
  120.             uart_write_bytes( UART_NUM_2, debugBuffer, strlen(debugBuffer) );
  121.             #endif
  122.             res = 0;
  123.             break;
  124.         }
  125.     }
  126.    
  127.     free( sresp );
  128.     free( data );
  129.  
  130.     return res;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement