示例#1
0
boolean OTAUpdateClass::downloadFile(const char* name) {
	// make some http requests to check for firmware updates
	LGPRSClient c;
	uint8_t buffer[DIGEST_SIZE_BUFFER];
	int n , size, max_millis;
	
	// download the firmware
	if(!c.connect(this->host, 80)) {
		DEBUG_UPDATE("OTAUpdate::downloadFile - error connecting to update host\r\n");
		return false;
	}
	// connected... send the get request
	DEBUG_UPDATE("OTAUpdate::downloadFile %s:80 'GET /%s/%s'\r\n", this->host, this->path, &name[4]);
	c.printf("GET /%s/%s\n", this->path, &name[4]);

	// save the result
	max_millis = millis() + 10000;
	LFlash.begin();
	LFlash.remove((char*) name);
	
	LFile ota = LFlash.open(name, FILE_WRITE);
	ota.seek(0);
	size = 0;
	
	while(c.connected()) {
		int n = c.read(buffer, 1024);
		if(n > 0) {
			max_millis = millis() + 2000;
			ota.write(buffer, n);
			size += n;
		} else {
			if(millis() > max_millis) {
				DEBUG_UPDATE("OTAUpdate::downloadFile - timed out!\r\n");
				c.stop();
				ota.close();
				return false;
			} else {
				delay(100);
			}
		}		
	}
	c.stop();
	ota.close();
	
	DEBUG_UPDATE("OTAUpdate::downloadFile - done! got %d bytes\r\n", size);
	return size > 0;
}
boolean OTAUpdateClass::downloadFile(const char* name) {
    // make some http requests to check for firmware updates
    LGPRSClient c;
    uint8_t buffer[DIGEST_SIZE_BUFFER];
    int n , size, max_millis;
    char buff[256];
    static char endofheader[5] ;
    boolean HTTPHeaderreached = false;
    char byc;

    //convert string to int
    String sthostport = this->port;
    unsigned int uinthostport = sthostport.toInt();

    // download the firmware
    if(!c.connect(this->host, uinthostport)) {
        DEBUG_UPDATE("OTAUpdate::downloadFile - error connecting to update host\r\n");
        return false;
    }
    // connected... send the get request
    DEBUG_UPDATE("OTAUpdate::downloadFile %s:%d 'GET /%s/%s'\r\n", this->host, uinthostport, this->path, &name[4]);

    sprintf(buff, "GET /%s/%s", this->path, &name[4]);
    c.print(buff);
    //c.printf("GET /%s/%s", this->path, &name[4]);
    c.println(" HTTP/1.1");
    c.print("Host: ");
    c.println(this->host);
    c.println("Connection: close");
    c.println();

    // save the result
    max_millis = millis() + 10000;
    LFlash.begin();
    LFlash.remove((char*) name);

    LFile ota = LFlash.open(name, FILE_WRITE);
    ota.seek(0);
    size = 0;

    // get data content of the file
    while(c.connected()) {
        //skip byte until end of HTTP Header
        if(HTTPHeaderreached == false) {
            // read byte
            byc = c.read();
            if(byc > 0) {
                max_millis = millis() + 2000;
                // if HTTP header is not reached, read until find double CRLF
                Serial.print(byc);

                // proceed a right shift of the  array
                for(int i = 0; i < 3; i++) {
                    endofheader[i] =  endofheader[i+1];
                }
                // add last received char at the end of the array
                endofheader[3] = byc;
                //don't forget null caracter
                endofheader[4] = '\0';
                // compare array with end of HTTP header key (double CRLF)
                if (strcmp("\r\n\r\n", endofheader ) == 0) {
                    // return true
                    DEBUG_UPDATE("OTAUpdate::downloadFile - end of HTTP header reached\r\n");
                    HTTPHeaderreached = true;
                }
                else {
                    HTTPHeaderreached = false;
                }
            }
            else {
                if(millis() > max_millis) {
                    DEBUG_UPDATE("OTAUpdate::downloadFile - timed out!\r\n");
                    c.stop();
                    ota.close();
                    return false;
                } else {
                    delay(100);
                }
            }
        }
        else {
            int n = c.read(buffer, 1024);
            if(n > 0) {
                max_millis = millis() + 2000;
                ota.write(buffer, n);
                size += n;
                DEBUG_UPDATE("size = %d\r", size);
            } else {
                if(millis() > max_millis) {
                    DEBUG_UPDATE("OTAUpdate::downloadFile - timed out!\r\n");
                    c.stop();
                    ota.close();
                    return false;
                } else {
                    delay(100);
                }
            }
        }
    }
    c.stop();
    ota.close();

    DEBUG_UPDATE("\r\nOTAUpdate::downloadFile - done! got %d bytes\r\n", size);
    return size > 0;
}