Example #1
0
//-------------------------------------------------------------------------------
// TCP receive response from server
//-------------------------------------------------------------------------------
err_t ICACHE_FLASH_ATTR tc_recv(TCP_SERV_CONN *ts_conn) {
#if DEBUGSOO > 1
	tcpsrv_received_data_default(ts_conn);
#endif
	tcpsrv_unrecved_win(ts_conn);
    uint8 *pstr = ts_conn->pbufi;
    sint32 len = ts_conn->sizei;
#if DEBUGSOO > 4
    os_printf("IOT_Rec(%u): %s\n", len, pstr);
#endif
    os_memset(iot_last_status, 0, sizeof(iot_last_status));
    os_strncpy(iot_last_status, (char *)pstr, mMIN(sizeof(iot_last_status)-1, len)); // status/error
    iot_last_status_time = get_sntp_time();
	if(len >= sizeof(key_http_ok1) + 3 + sizeof(key_http_ok2)) {
		if(os_memcmp(pstr, key_http_ok1, sizeof(key_http_ok1)-1) == 0
				&& os_memcmp(pstr + sizeof(key_http_ok1)-1 + 3, key_http_ok2, sizeof(key_http_ok2)-1) == 0) { // Check - 200 OK?
			#if DEBUGSOO > 4
				os_printf(" - 200\n");
			#endif
	        uint8 *nstr = web_strnstr(pstr, "\r\n\r\n", len); // find body
	        if(nstr != NULL) {
	        	pstr = nstr + 4; // body start
	        	len -= nstr - pstr; // body size
		        uint8 *nstr = web_strnstr(pstr, "\r\n", len); // find next delimiter
		        if(nstr != NULL) *nstr = '\0';
	        	if(ahextoul(pstr)) { // not 0 = OK
	    			tc_init_flg &= ~TC_RUNNING; // clear run flag
	    			iot_data_processing->last_run = system_get_time();
					#if DEBUGSOO > 4
						os_printf("Ok!!!\n");
					#endif
	        	}
	        }
		}
	}
	//ts_conn->flag.rx_null = 1; // stop receiving data
   	tc_close();
	return ERR_OK;
}
Example #2
0
void web_fini(const uint8 * fname)
{
	struct buf_fini *p = (struct buf_fini *) os_zalloc(sizeof(struct buf_fini));
	if(p == NULL) {
#if DEBUGSOO > 1
		os_printf("Error mem!\n");
#endif
		return;
	}
	TCP_SERV_CONN * ts_conn = &p->ts_conn;
	WEB_SRV_CONN * web_conn = &p->web_conn;
	web_conn->bffiles[0] = WEBFS_INVALID_HANDLE;
	web_conn->bffiles[1] = WEBFS_INVALID_HANDLE;
	web_conn->bffiles[2] = WEBFS_INVALID_HANDLE;
	web_conn->bffiles[3] = WEBFS_INVALID_HANDLE;
	ts_conn->linkd = (uint8 *)web_conn;
	ts_conn->sizeo = FINI_BUF_SIZE;
	ts_conn->pbufo = p->buf;
	rom_strcpy(ts_conn->pbufo, (void *)fname, MAX_FILE_NAME_SIZE);
#if DEBUGSOO > 1
	os_printf("Run ini file: %s\n", ts_conn->pbufo);
#endif
	if(!web_inc_fopen(ts_conn, ts_conn->pbufo)) {
#if DEBUGSOO > 1
		os_printf("file not found!\n");
#endif
		return;
	}
	if(fatCache.flags & WEBFS_FLAG_ISZIPPED) {
#if DEBUGSOO > 1
		os_printf("\nError: file is ZIPped!\n");
#endif
		web_inc_fclose(web_conn);
		return;
	}
	user_uart_wait_tx_fifo_empty(1,1000);
	while(1) {
		web_conn->msgbufsize = ts_conn->sizeo;
		web_conn->msgbuflen = 0;
		uint8 *pstr = web_conn->msgbuf = ts_conn->pbufo;
		if(CheckSCB(SCB_RETRYCB)) { // повторный callback? да
#if DEBUGSOO > 2
			os_printf("rcb ");
#endif
			if(web_conn->func_web_cb != NULL) web_conn->func_web_cb(ts_conn);
			if(CheckSCB(SCB_RETRYCB)) break; // повторить ещё раз? да.
		}
		uint16 len = WEBFSGetArray(web_conn->webfile, pstr, FINI_BUF_SIZE);
#if DEBUGSOO > 3
		os_printf("ReadF[%u]=%u\n",web_conn->webfile, len);
#endif
		if(len) { // есть байты в файле
			uint8 *pend = web_strnstr(pstr, CRLF, FINI_BUF_SIZE);
			if(pend != NULL) {
				int cmp = pend - pstr;
				if(cmp >= 0) { // найден CRLF
					// откат файла
					WEBFSStubs[web_conn->webfile].addr -= len;
					WEBFSStubs[web_conn->webfile].bytesRem += len;
					// передвинуть указатель в файле на считанные байты с учетом маркера, без добавки длины для передачи
					WEBFSStubs[web_conn->webfile].addr += cmp+2;
					WEBFSStubs[web_conn->webfile].bytesRem -= cmp+2;
					if(cmp != 0) {
						pstr[cmp] = '\0'; // закрыть string calback-а
#if DEBUGSOO > 3
						os_printf("String:%s\n", pstr);
#endif

						if(!os_memcmp((void*)pstr, "inc:", 4)) { // "inc:file_name"
							if(!web_inc_fopen(ts_conn, &pstr[4])) {
#if DEBUGSOO > 1
								os_printf("file not found!");
#endif
							};
						}
						else web_int_callback(ts_conn);
					};
				};
			};
		}
		else if(web_inc_fclose(web_conn)) {
			if(web_conn->web_disc_cb != NULL) web_conn->web_disc_cb(web_conn->web_disc_par);
			return;
		}
	}
}