Example #1
0
//Cgi that allows the ESPFS image to be replaced via http POST
int ICACHE_FLASH_ATTR cgiUploadRaw(HttpdConnData *connData) {
	if (connData->conn==NULL) {
		//Connection aborted. Clean up.
		return HTTPD_CGI_DONE;
	}
	int part = get_updatable_partition();
	if (connData->post->processed == 0 && connData->getArgs!=0){
		// First run, init the partition and prep if necessary
		part = connData->getArgs[0] - '0'; // Turn for arg (should be partition ID) into partition index
		os_printf("\nFlashing partition: %d\n", part);
	}
	
	SpiFlashOpResult ret;
	// The source should be 4byte aligned, so go ahead an flash whatever we have
	ret=flash_binary(connData->post->buff, connData->post->buffLen, part);
	if (ret != connData->post->buffLen){
		os_printf("Error writing to the flash\n");
		return HTTPD_CGI_DONE;
	}	
	// Count bytes for data
	connData->post->processed += ret;

	os_printf("Wrote %d bytes (%dB of %d)\n", connData->post->buffSize, connData->post->processed, connData->post->len);//&connData->postBuff));

	if (connData->post->processed == connData->post->len){
		httpdSend(connData, "Finished uploading", -1);
		reset_flash();
		return HTTPD_CGI_DONE;
	} else {
		return HTTPD_CGI_MORE;
	}
}
Example #2
0
/*	cgiUploadRaw	*/
int ICACHE_FLASH_ATTR cgiUploadRaw(HttpdConnection *	ptConnection)
{
	int 	iRet	= 0;

	if (NULL == ptConnection->ptEspConnection)
	{
		/* connection aborted - clean up */
		iRet = HTTPD_CGI_DONE;
		goto lblCleanup;
	}

	int iPartition = get_updatable_partition();
	if ((0 == ptConnection->ptPost->cbProcessed) && (0 != ptConnection->pbGetArguments))
	{
		/* first call - initialize partition and prepare if necessary */
		/* convert to index */
		iPartition = ptConnection->pbGetArguments[0] - '0';

#ifdef CGIFLASH_DEBUG
		os_printf("cgiUploadRaw: flashing partition %d\n", iPartition);
#endif
	}
	
	SpiFlashOpResult cbFlashedBytes;

	/* source should be 4 byte aligned */
	cbFlashedBytes = flash_binary(ptConnection->ptPost->pbBuffer, ptConnection->ptPost->cbBufferLength, iPartition);
	if (cbFlashedBytes != ptConnection->ptPost->cbBufferLength)
	{

#ifdef CGIFLASH_DEBUG
		os_printf("cgiUploadRaw: error - wrote less bytes than required\n");
#endif

		iRet = HTTPD_CGI_DONE;
		goto lblCleanup;
	}

	/* update flashed bytes count */
	ptConnection->ptPost->cbProcessed += cbFlashedBytes;

#ifdef CGIFLASH_DEBUG
	os_printf("cgiUploadRaw: wrote %d bytes (%d of %d)\n", ptConnection->ptPost->cbMaxBufferSize, ptConnection->ptPost->cbProcessed, ptConnection->ptPost->cbPostLength);
#endif

	/* check if reached write target */
	if (ptConnection->ptPost->cbProcessed == ptConnection->ptPost->cbPostLength)
	{
		httpdSend(ptConnection, FINISHED_UPLOADING_STRING, -1);
		reset_flash();
		iRet = HTTPD_CGI_DONE;
		goto lblCleanup;
	}
	else
	{
		iRet = HTTPD_CGI_MORE;
		goto lblCleanup;
	}

lblCleanup:
	return iRet;
}