Questions about CC3000_BUFFER_MAGIC_NUMBER and debug mode

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
jamesfw
 
Posts: 3
Joined: Tue Dec 10, 2013 6:20 pm

Questions about CC3000_BUFFER_MAGIC_NUMBER and debug mode

Post by jamesfw »

I am using the Adafruit CC3000 Wireless shield with an Arduino Uno R3. As part of a project, I am trying to send POST requests with the Adafruit_CC3000_Client.

I've been struggling with a particular issue - sometimes, sending a POST request will cause the program to get stuck.
I delved into the Adafruit_CC3000 library to check it out. I found the place we're getting stuck!

It's a function called SpiWrite located in Adafruit_CC3000/ccspi.cpp. The offending while(1) loop is found here:

Code: Select all

long SpiWrite(unsigned char *pUserBuffer, unsigned short usLength)
{

.
.
.

/* The magic number that resides at the end of the TX/RX buffer (1 byte after the allocated size)
   * for the purpose of overrun detection. If the magic number is overwritten - buffer overrun
   * occurred - and we will be stuck here forever! */
  if (wlan_tx_buffer[CC3000_TX_BUFFER_SIZE - 1] != CC3000_BUFFER_MAGIC_NUMBER)
  {
    DEBUGPRINT_F("\tCC3000: Error - No magic number found in SpiWrite\n\r");
    while (1);
  }
If I try to send a POST request which is too big, then the buffer overruns the spot in memory where the magic number resides, and we are sent into this while(1) loop. Before we enter the loop, DEBUGPRINT_F is used to display an error message. This would be great if debug mode was meant to be turned on for normal users. Unfortunately, according to several forum posts, debug mode was only for the Adafruit developers to use to debug hardware problems during development of the shield / breakout board.

Here are my questions:

1. Why is the user purposefully being sent into a while(1) loop with no indication of the cause? I don't see this being documented anywhere, and we've already established that none of the DEBUG statements are supposed to be visible to users.
2. Can the location of CC3000_BUFFER_MAGIC_NUMBER be changed? I want to send a postdata string longer than 105 characters, and every time I go over this limit I overrun the buffer. I'm using a standard size POST header generated by this code:

Code: Select all

      www.fastrprint(F("POST "));
      www.fastrprint(WEBPAGE);
      www.fastrprint(F(" HTTP/1.1\r\nHost: "));
      www.fastrprint(WEBSITE); www.fastrprint(F("\r\n")); 
      www.fastrprint(F("User-Agent: Arduino/1.0\r\n"));
      www.fastrprint(F("Connection: close\r\n"));
      www.fastrprint(F("Content-Type: application/x-www-form-urlencoded\r\n"));
      www.fastrprint(F("Content-Length: ")); www.fastrprint(len_c); www.fastrprint(F("\r\n"));
      www.fastrprint(F("\r\n")); // Extra empty line for post argument
      www.fastrprint(data_c); www.fastrprint(F("\r\n"));
      www.fastrprint(F("\r\n"));
      www.println(); Serial.println();

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Questions about CC3000_BUFFER_MAGIC_NUMBER and debug mod

Post by adafruit_support_mike »

jamesfw wrote:1. Why is the user purposefully being sent into a while(1) loop with no indication of the cause?
Because we can't guarantee any form of output will be visible to the user. A circuit not connected to the Arduino IDE wouldn't have a serial console to display error messages, for instance.
jamesfw wrote:2. Can the location of CC3000_BUFFER_MAGIC_NUMBER be changed?
Probably, but it's easier to break the string you want to send into smaller pieces and send them one at a time.

Neither the server nor the TCP connection are aware of the CC3000's output buffer. All they see is a string of bytes. It doesn't matter to them whether the CC3000 sends them in a single pass through the buffer or in smaller chunks.

PierlugiRovere
 
Posts: 15
Joined: Tue Jul 08, 2014 8:40 am

Re: Questions about CC3000_BUFFER_MAGIC_NUMBER and debug mod

Post by PierlugiRovere »

How can i send it in a smaller chunks?
Probably i've the same issue.

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Questions about CC3000_BUFFER_MAGIC_NUMBER and debug mod

Post by adafruit_support_mike »

The easiest way is to build the string in small chunks to begin with.. instead of concatenating one great big string, send each piece one at a time.

If you already have a great big string, you'd use something like this:

Code: Select all

#define MY_BUFSIZE  32

//  fixed-size buffer
unsigned char myBuffer[ MY_BUFSIZE ];

//  a pointer into the buffer
unsigned char * bp;   

//  a pointer into the string we want to shorten:
unsigned char * sp = longString;


//  C strings end with a zero, so if *sp is nonzero it means
//  we haven't gotten to the end of the string yet.

while ( *sp ) {
//      go back to the beginning of the fixed buffer
    bp = myBuffer;      

//      loop for MY_BUFSIZE chars  
//      * OR *
//      until *sp equals zero
    for ( int i=MY_BUFSIZE-1 ; i && *sp ; i-- ) {

//      copy the next character in the string into the buffer,
//      then increment both pointers.
        *bp++ = *sp++;
    }
//      put a zero-terminator on the string
    *bp = 0;
    
    cc3000.write( myBuffer, bp - myBuffer );
}

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”