Exemple #1
0
void destory_request(ghttp_request *request)
{
	if(request != NULL)
	{
		ghttp_request_destroy(request);
	}
	LOG_PRINTF("Info: Func: %s Destroy request is ok. \n", __func__);
	#ifdef DEBUG_MSG
    	closelog();
    	#endif
	return ;
}
Exemple #2
0
void *MainService(void * arg)
{
	
	status=INITIALIZE;
	if(init("config","orderid")){
		puts("Initialization error.");
		exit(1);
	}
	else
		status=READY;
	update_display("default.png",0);

	
	libusb_init(NULL);
	handle=libusb_open_device_with_vid_pid(NULL,USB_VENDORID,USB_PRODUCTID);
	if(!handle)
	{	
		printf("OPEN DEVICE ERROR.\n");
		getchar();
		exit(1);
	}
	libusb_claim_interface(handle,0);
	pthread_t readusb_thread;
	main_thread=pthread_self();
	struct ev_loop *loop = EV_DEFAULT;
	ev_init (&net_watcher, net_cb);
	ev_signal_init(&usb_watcher,usb_cb,SIGUSR1);
	ev_signal_start(loop,&usb_watcher);
	if(pthread_create(&readusb_thread,NULL,read_from_usb,NULL))
	{
		printf("CREATE THREAD ERROR.\n");
		getchar();
		exit(1);
	}
	
	while(1)
	{
		if(!ev_is_active(EV_A_ &net_watcher))
		{
			ev_io_set (&net_watcher, fd_net, EV_READ);
			ev_io_start(loop,&net_watcher);
		}
		ev_run (loop, 0);
		if(net_io)
			ev_io_stop (EV_A_ &net_watcher);
	}
	munmap((void *)p_order_id,sizeof(unsigned int));
	ghttp_close(request);
	ghttp_request_destroy(request);
	return 0;
}
Exemple #3
0
ghttp_request *ghttp_request_new_url(char *url)
{
	ghttp_request *request = NULL;

	if(!url)
		return NULL;
	request = ghttp_request_new();
	if(!request)
		return NULL;
	if( ghttp_set_uri(request, url) < 0)
		goto ERR;
	
	return request;
ERR:
	ghttp_request_destroy(request);
	return NULL;
}
Exemple #4
0
ghttp_request *ghttp_request_new2(ghttp_type action, char *url, ghttp_sync_mode mode)
{
	ghttp_request *request = NULL;

	if(!url)
		return NULL;
	request = ghttp_request_new();
	if(!request)
		return NULL;
	if( ghttp_set_uri(request, url) < 0)
		goto ERR;
	ghttp_set_type(request, action);
	ghttp_set_sync(request, mode);
	
	return request;
ERR:
	ghttp_request_destroy(request);
	return NULL;
}
Exemple #5
0
static ghttp_request *ghttp_request_redirect(ghttp_request *cur_request, char *new_url)
{
	ghttp_request *request = NULL;
	
	if (!cur_request || !cur_request->req || !cur_request->conn)
		return NULL;

	request = ghttp_request_new();
	if(!request)
		return NULL;

	if( ghttp_set_uri(request, new_url) < 0)
		goto ERR;
	ghttp_set_type(request, cur_request->req->type);
	ghttp_set_sync(request, cur_request->conn->sync);

	return request;
ERR:
	ghttp_request_destroy(request);
	return NULL;
}
int ParseJSON(cJSON *node, const char *box_id)
{
	FILE *stream = NULL;
	char buf[512] = {0};
	char *result_url;
	cJSON *cmdArray = cJSON_GetObjectItem(node, "remote_control");
        ghttp_request *req_remotectl_feedback;
        char *id = NULL, *content = NULL;
        char *command = NULL, *ptr = NULL, *p = NULL;

        if(!cmdArray)
	{
                bail("parse result : json has no node named remote_control");
		return 0;
	}

        char *errorMsg = NULL;
        cJSON *cmdList = cmdArray->child;
        while(cmdList != NULL)
        {
                if(cJSON_GetObjectItem(cmdList, "id")->valuestring != NULL)
                {
                        id = cJSON_GetObjectItem(cmdList, "id")->valuestring;
                        printf("id : %s\n", id);
                }
		
		if(cJSON_GetObjectItem(cmdList, "content")->valuestring != NULL)
                {
                        content = cJSON_GetObjectItem(cmdList, "content")->valuestring; 
                        printf("content : %s\n", content);
                }
                 
		stream = NULL;
		
                if(strcmp(content, "reboot") == 0)
		{
			printf("command : reboot\n");
                        printf("send http feedback...\n");
                        result_url = (char *)malloc(150);
        	        req_remotectl_feedback = ghttp_request_new();
                	sprintf(result_url, "http://www.ailvgobox.com/box_manage/remote_feedback.php?box_id=%s&id=%s&result=reboot_ok", box_id, id);
                        printf("result_url : %s\n", result_url);
                        result_url[149] = '\0';
                        send_http_request(req_remotectl_feedback, result_url);
                        ghttp_request_destroy(req_remotectl_feedback);
                        free(result_url);
                        printf("command : reboot, run!\n");
			stream = popen(content, "r");
                        pclose(stream);
		}
		
                ptr = strstr(content, "@");

                if(!ptr)
                {
                        printf("content : one command!\n");
                    
                        printf("command : %s, run!\n", content);

                        stream = popen(content, "r");
		        memset(buf, 0, 512);
                        fread(buf, sizeof(char), sizeof(buf), stream);
                        buf[511] = '\0';
                        printf("buf : %s\n", buf);
                        pclose(stream);
			str_rep(buf, '\n', ',');
                        str_rep(buf, ' ', '-');
			if(strlen(buf) == 0)
                              strcpy(buf, "no_result"); 
 
                        printf("send http feedback...\n");
			result_url = (char *)malloc(strlen(buf)+150);
			memset(result_url, 0, strlen(buf)+150);
			req_remotectl_feedback = ghttp_request_new();
			sprintf(result_url, "http://www.ailvgobox.com/box_manage/remote_feedback.php?box_id=%s&id=%s&result=%s", box_id, id, buf);
			printf("result_url : %s\n", result_url);
			result_url[strlen(buf)+150-1] = '\0';
			send_http_request(req_remotectl_feedback, result_url);
			ghttp_request_destroy(req_remotectl_feedback);
			free(result_url);
                }
                else
                {
                        printf("content : multiple command!\n");
                        
                        command = strtok_r(content, "@", &p);

                        while(command)
                        {  
                              printf("command : %s\n", command);

                              if(strcmp(command, "reboot") == 0)
                              {
                                      printf("command : reboot\n");
                                      printf("send http feedback...\n");
                                      result_url = (char *)malloc(150);
                                      req_remotectl_feedback = ghttp_request_new();
                                      sprintf(result_url, "http://www.ailvgobox.com/box_manage/remote_feedback.php?box_id=%s&id=%s&result=reboot_ok", box_id, id);
                                      printf("result_url : %s\n", result_url);
                                      result_url[149] = '\0';
                                      send_http_request(req_remotectl_feedback, result_url);
                                      ghttp_request_destroy(req_remotectl_feedback);
                                      free(result_url);
                                      printf("command : reboot, run!\n");
                                      stream = popen(command, "r");
                                      pclose(stream);
                               }
                              else
                              {
                                      printf("command : %s, run!\n", command);
                                      stream = popen(command, "r");
                                      memset(buf, 0, 512);
                                      fread(buf, sizeof(char), sizeof(buf), stream);
                                      buf[511] = '\0';
                                      printf("buf : %s\n", buf);
                                      pclose(stream);
                                      str_rep(buf, '\n', ',');
                                      str_rep(buf, ' ', '-');
                                      if(strlen(buf) == 0)
                                              strcpy(buf, "no_result");
                                      printf("send http feedback...\n");
                                      result_url = (char *)malloc(strlen(buf)+150);
                                      memset(result_url, 0, strlen(buf)+150);
                                      req_remotectl_feedback = ghttp_request_new();
                                      sprintf(result_url, "http://www.ailvgobox.com/box_manage/remote_feedback.php?box_id=%s&id=%s&result=%s", box_id, id, buf);
                                      printf("result_url : %s\n", result_url);
                                      result_url[strlen(buf)+150-1] = '\0';
                                      send_http_request(req_remotectl_feedback, result_url);
                                      ghttp_request_destroy(req_remotectl_feedback);
                                      free(result_url);
                               }  
                              
                              command = strtok_r(NULL, "@", &p);  
                        }
                }
                
		cmdList = cmdList->next;
      }
}
int remote_control()
{
	ghttp_request *req;
	char *http_body = NULL;
	req = ghttp_request_new();
	sqlite3 *db = NULL;
        int rc = 0;
        sqlite3_stmt *ppstmt = NULL;
        char sql_cmd[100] ={0};
        const char *box_id;
        char box_id_tmp[10] = {0};
        const char *wan_state;
        char *errorMsg = NULL;
	char request_url[200] = {0};

	printf("****************************************\n");
        printf("remote_control : start\n");
        printf("****************************************\n");

        rc = sqlite3_open(box_db, &db);
        if(rc == SQLITE_ERROR)
        {
                printf("cannot open box.db!\n");
                return 0;
        }

        strcpy(sql_cmd, "select box_id from box_info");
        sqlite3_prepare(db, sql_cmd, -1, &ppstmt, 0);
        rc = sqlite3_step(ppstmt);
	if(rc == SQLITE_ROW)
        {
                box_id = sqlite3_column_text(ppstmt, 0);
                strcpy(box_id_tmp, box_id);
                printf("box_id : %s\n", box_id_tmp);
        }
	
        sqlite3_finalize(ppstmt);
        sqlite3_close(db);

	printf("send http request...\n");
        sprintf(request_url, "http://www.ailvgobox.com/box_manage/remote_control.php?box_id=%s", box_id_tmp);
        printf("request_url  : %s\n", request_url);
	http_body = send_http_request(req, request_url);
	fprintf(stderr, "http_body : %s\n", http_body);

	cJSON *node;
	
        if(http_body)
        {
        	printf("HTTP success!\n");

                if(strcmp(http_body, "null") == 0)
                     printf("http_body : null, no remote_control!\n");
                else
                {
                     node = cJSON_Parse(http_body);
                     ParseJSON(node, box_id_tmp);
                }
        }
	else
        {
                printf("HTTP failure!\n");
        }

        if(http_body)
               free(http_body);

        ghttp_request_destroy(req);
        free(request_url);
        printf("remote_control : complete!\n");
        return 1;
}
Exemple #8
0
int ghttp_download_file(char *path, char *url)
{
	ghttp_request *request = NULL;
	FILE * pFile=NULL;
	char *buf=NULL;
	int ret = 0;

	ghttp_status req_status;
	ghttp_proc req_proc;
	int bytes_read=0,recvbytes=0;
	int status_code=0;
	char *redirect = NULL;
#if GHTTP_DEBUG
	char *tmp_pchar = NULL;
#endif

	request = ghttp_request_new();
	if( ghttp_set_uri(request, url) < 0 ){
			ghttpDebug("invalid url: %s \n", url);
       		ret = -1;
			goto END;
	}
	
	if(!path)
		path = ghttp_get_resource_name(request);
	if(!path)
		path = "httpget.html";
	
	pFile = fopen ( path , "wb" );
	if(pFile == NULL){
		ghttpDebug("error: %s [%s]\n", strerror(errno), path);
		ret = -2;
		goto END;
	}
	ghttpDebug("host: %s \n", ghttp_get_host(request));
	if( ghttp_set_type(request, ghttp_type_get) < 0 ){
    		ret = -3;
		goto END;
	}
	if (ghttp_set_sync(request, ghttp_async) < 0){
		ret = -3;
		goto END;
	}
	if( ghttp_prepare(request) < 0 ){
		ret = -3;
		goto END;
	}

	do
	{
		req_status = ghttp_process(request);
		if( req_status == ghttp_error ){
			ghttpDebug("%s \n", ghttp_get_error(request));
			ret = -4;
			goto END;
		}
		else
		{
			if( req_status == ghttp_done )
			{
				status_code = ghttp_status_code(request);
				if(status_code != 200){
					fclose(pFile);
					pFile = NULL;
					break;
				}
			}

			req_proc = ghttp_get_proc(request);
			if( req_proc == ghttp_proc_response || req_proc == ghttp_proc_done )
			{
			#if GHTTP_DEBUG
				if( !tmp_pchar )
				{
					tmp_pchar = (char *)ghttp_get_header(request, "Content-Length");
					ghttpDebug("Content-Length: %s \n", tmp_pchar ? tmp_pchar : "null");
					tmp_pchar = (char *)ghttp_get_header(request, "Transfer-Encoding");
					ghttpDebug("Transfer-Encoding: %s \n", tmp_pchar ? tmp_pchar : "null");
					tmp_pchar = (char *)ghttp_get_header(request, "Content-Encoding");
					ghttpDebug("Content-Encoding: %s \n", tmp_pchar ? tmp_pchar : "null");
					tmp_pchar = (char *)1;
					
					ghttpDebug("recvbytes: ");
				}
			#endif
			
				ghttp_flush_response_buffer(request);
				if(ghttp_get_body_len(request) > 0)
				{
					buf = ghttp_get_body(request);
					bytes_read = ghttp_get_body_len(request);
					recvbytes += bytes_read;
					if(buf)
						fwrite(buf,bytes_read,1,pFile);
				}

				ghttpDebug("%d", recvbytes);
			}
		}
	}while (req_status == ghttp_not_done);

	//ret = status_code;
	switch(status_code)
	{
	case 200:
	default:
		break;
	case 302:
		buf = (char *)ghttp_get_header(request, "Location");
		if(buf){
			redirect = (char *)malloc(strlen(buf)+1);
			if(redirect == NULL){
				ret = -1;
				goto END;
			}
			strcpy(redirect, buf);
		}
		break;
	}
	
END:
	ghttp_request_destroy(request);
    	if(pFile)
		fclose(pFile);
	if(redirect){
		ghttpDebug("redirect: %s \n", redirect);
		ret = ghttp_download_file(path, redirect);
		free(redirect);
	}
	
	return ret;
}
Exemple #9
0
int ghttp_post_work(ghttp_request *request, struct ghttp_result *result, struct ghttp_post_data *data)
{
	int ret = 0;
	ghttp_status req_status;
	ghttp_proc req_proc;
	int status_code = 0;
//	ghttp_request *redirect_request;
//	char *redirect = NULL, *buf = NULL;

	#if GHTTP_DEBUG
	char *tmp_pchar = NULL;
	#endif

	if(!request || !data)
		return -1;
	ghttp_set_type(request, ghttp_type_post);
	ghttp_set_sync(request, ghttp_async);
	if( ghttp_prepare(request) < 0 ){
		ret = -3;
		goto END;
	}

	do
	{
		req_status = ghttp_process(request);
		if( req_status == ghttp_error ){
			ghttpDebug("%s \n", ghttp_get_error(request));
			ret = -3;
			goto END;
		}
		else
		{
			if(req_status == ghttp_next ){
				data->post_data_func(request, data);
			}
			if( req_status == ghttp_done )
			{
				status_code = ghttp_status_code(request);
				if(status_code != 200){
					ghttp_result_recv_finish(request, result);
					break;
				}
			}

			req_proc = ghttp_get_proc(request);
			if( req_proc == ghttp_proc_response || req_proc == ghttp_proc_done )
			{
			#if GHTTP_DEBUG
				if( !tmp_pchar )
				{
					tmp_pchar = (char *)ghttp_get_header(request, "Content-Length");
					ghttpDebug("Content-Length: %s \n", tmp_pchar ? tmp_pchar : "null");
					tmp_pchar = (char *)ghttp_get_header(request, "Transfer-Encoding");
					ghttpDebug("Transfer-Encoding: %s \n", tmp_pchar ? tmp_pchar : "null");
					tmp_pchar = (char *)ghttp_get_header(request, "Content-Encoding");
					ghttpDebug("Content-Encoding: %s \n", tmp_pchar ? tmp_pchar : "null");
					tmp_pchar = (char *)ghttp_get_header(request, "Content-Type");
					ghttpDebug("Content-Type: %s \n", tmp_pchar ? tmp_pchar : "null");
					tmp_pchar = (char *)1;
				}
			#endif
				ghttp_result_recv(request, result);
			}
		}
	}while (req_status == ghttp_not_done);
	
END:
	ghttpDebug("http_code: %d \n", status_code);
	if(ret == 0 && (status_code < 200 || status_code >= 300))
		ret = -3;
	if(result)
		result->http_code = status_code;
	ghttp_result_recv_finish(request, result);

	ghttp_request_destroy(request);
	
	return ret;
}
Exemple #10
0
int ghttp_get_work(ghttp_request *request, struct ghttp_result *result)
{
	int ret = 0;
	ghttp_status req_status;
	ghttp_proc req_proc;
	int status_code = 0;
	ghttp_request *redirect_request;
	char *redirect = NULL, *buf = NULL;

	#if GHTTP_DEBUG
	char *tmp_pchar = NULL;
	#endif

	if(!request)
		return -1;
	ghttp_set_type(request, ghttp_type_get);
	ghttp_set_sync(request, ghttp_async);
	if( ghttp_prepare(request) < 0 ){
		ret = -3;
		goto END;
	}

	do
	{
		req_status = ghttp_process(request);
		if( req_status == ghttp_error ){
			ghttpDebug("%s \n", ghttp_get_error(request));
			ret = -3;
			goto END;
		}
		else
		{
			if( req_status == ghttp_done )
			{
				status_code = ghttp_status_code(request);
				if(status_code != 200){
					ghttp_result_recv_finish(request, result);
					break;
				}
			}

			req_proc = ghttp_get_proc(request);
			if( req_proc == ghttp_proc_response || req_proc == ghttp_proc_done )
			{
			#if GHTTP_DEBUG
				if( !tmp_pchar )
				{
					tmp_pchar = (char *)ghttp_get_header(request, "Content-Length");
					ghttpDebug("Content-Length: %s \n", tmp_pchar ? tmp_pchar : "null");
					tmp_pchar = (char *)ghttp_get_header(request, "Transfer-Encoding");
					ghttpDebug("Transfer-Encoding: %s \n", tmp_pchar ? tmp_pchar : "null");
					tmp_pchar = (char *)ghttp_get_header(request, "Content-Encoding");
					ghttpDebug("Content-Encoding: %s \n", tmp_pchar ? tmp_pchar : "null");
					tmp_pchar = (char *)ghttp_get_header(request, "Content-Type");
					ghttpDebug("Content-Type: %s \n", tmp_pchar ? tmp_pchar : "null");
					tmp_pchar = (char *)1;
				}
			#endif
				ghttp_result_recv(request, result);
			}
		}
	}while (req_status == ghttp_not_done);
	
	switch(status_code)
	{
	case 200:
	default:
		break;
	case 302:
		buf = (char *)ghttp_get_header(request, "Location");
		if(buf){
			redirect = (char *)malloc(strlen(buf)+1);
			if(redirect == NULL){
				ret = -2;
				goto END;
			}
			strcpy(redirect, buf);
		}
		break;
	}
	
END:
	if(result)
		result->http_code = status_code;
	ghttp_result_recv_finish(request, result);
	
	if(redirect){
		ghttpDebug("redirect: %s \n", redirect);
		redirect_request = ghttp_request_redirect(request, redirect);
		ghttp_request_destroy(request);
		free(redirect);
		ret = ghttp_get_work(redirect_request, ghttp_result_clean(result));
	}
	else
		ghttp_request_destroy(request);
	
	return ret;
}
Exemple #11
0
main()
{
	ghttp_request *req;
	char http_body[1024] = {0}, *errmsg = NULL;
	sqlite3 *db = NULL;
	int rc = 0, result = 0;
    	sqlite3_stmt *ppstmt = NULL;
    	char sql_cmd[100] ={0};
	const char *box_id, *wifi_detect;
	char box_id_tmp[10] = {0}, wifi_detect_tmp[5] = {0};
	const char *trafficnum = NULL;
	char trafficnum_tmp[10] = {0};
	char request_url[5000] = {0}, traffic_list[1500] = {0}, time_list[3000] = {0};
	int t = 0, select_cnt, ailvgobox_break;
       
    	printf("****************************************\n"); 
	printf("heart_beat : start\n");
    	printf("****************************************\n");
	
	printf("box.db : check box_id, wifi_detect\n");
	
	select_cnt = 0;
	while(select_cnt < 20)
	{
		if(sqlite3_open(box_db, &db))
			printf("cannot open box.db!\n");

    		strcpy(sql_cmd, "select box_id, wifi_detect from box_info");
		sqlite3_prepare(db, sql_cmd, -1, &ppstmt, 0);
    		rc = sqlite3_step(ppstmt);
		
		if(rc == SQLITE_ROW)
    		{
			box_id = sqlite3_column_text(ppstmt, 0);
			strcpy(box_id_tmp, box_id);
			printf("box_id : %s\n", box_id_tmp);
        		wifi_detect = sqlite3_column_text(ppstmt, 1);
			strcpy(wifi_detect_tmp, wifi_detect);
			printf("wifi_detect : %s\n", wifi_detect_tmp);
    			sqlite3_finalize(ppstmt);
			sqlite3_close(db);
			break;
		}
		else
		{
			printf("select box_id,wifi_detect failure!\n");
    			sqlite3_finalize(ppstmt);
			sqlite3_close(db);
			sleep(1);
			select_cnt++;
		}
	}

    	if(strcmp(wifi_detect_tmp, "off") == 0)
	{
		printf("wifi detect : off, traffic = null!\n");
		strcpy(trafficnum_tmp, "null");
	}
    	else
    	{
		printf("wifi detect : on, collect traffic!\n");

		t = time(NULL)+8*3600;
		
		printf("traffic.db : check new traffic\n");

		select_cnt = 0;
		while(select_cnt < 20)
		{
			rc = sqlite3_open(traffic_db, &db);
			if(rc == SQLITE_ERROR)
				printf("cannot open traffic.db!\n");
        
			ppstmt = NULL;
			sprintf(sql_cmd, "select count(*) from traffic_info where time>=%d-650 and time<=%d", t, t);
			sqlite3_prepare(db, sql_cmd, -1, &ppstmt, 0);
			rc = sqlite3_step(ppstmt);
			
			if(rc == SQLITE_ROW)
			{
				trafficnum = sqlite3_column_text(ppstmt, 0);
				strcpy(trafficnum_tmp, trafficnum);
				printf("new traffic in last 10 minutes : %s\n", trafficnum);
				sqlite3_finalize(ppstmt);
				sqlite3_close(db);
				break;
			}
			else
			{
				printf("select count(*) from traffic.db failure!\n");
				sqlite3_finalize(ppstmt);
				sqlite3_close(db);
				sleep(10);
				select_cnt++;
			}
		}

		printf("traffic_total.db : check total traffic\n");

		select_cnt = 0;
		while(select_cnt < 20)
		{
			rc = sqlite3_open(traffic_total_db, &db);
			if(rc == SQLITE_ERROR)
				printf("cannot open traffic_total.db!\n");

			ppstmt = NULL;
			sprintf(sql_cmd, "select count(*) from traffic_total where time>=%d-650 and time<=%d", t, t);
			sqlite3_prepare(db, sql_cmd, -1, &ppstmt, 0);
			rc = sqlite3_step(ppstmt);

			if(rc == SQLITE_ROW)
			{
				trafficnum = sqlite3_column_text(ppstmt, 0);
				if(atoi(trafficnum) < atoi(trafficnum_tmp))
				{
					printf("total traffic in last 10 minutes : %s\n", trafficnum);
					printf("collect in traffic_total.db in progress, retry\n");
					sqlite3_finalize(ppstmt);
					sqlite3_close(db);
					sleep(10);
					select_cnt++;
				}
				else
				{
					strcat(trafficnum_tmp, "*");
					strcat(trafficnum_tmp, trafficnum);
					printf("total traffic in last 10 minutes : %s\n", trafficnum);
					printf("trafficnum_tmp : %s\n", trafficnum_tmp);
					sqlite3_finalize(ppstmt);
					sqlite3_close(db);
					break;
				}
			}
			else
			{
				printf("select count(*) from traffic_total.db failure!\n");
				sqlite3_finalize(ppstmt);
				sqlite3_close(db);
				sleep(10);
				select_cnt++;
			}
		}
	}
        
	printf("------------ailvgobox monitor---------------\n");
	ailvgobox_break = ailvgobox_monitor();
	
	if(ailvgobox_break == 0)
	{	
		printf("ailvgobox server : online!\n");

		printf("------------send heart_beat request---------------\n");
		sprintf(request_url, "http://www.ailvgobox.com/box_manage_2/heart_beat_1.php?box_id=%s&traffic=%s", box_id_tmp, trafficnum_tmp);
		printf("request_url : %s\n", request_url);
		req = ghttp_request_new();
		strcpy(http_body, send_http_request(req, request_url));
		ghttp_request_destroy(req);

		printf("http_body : %s\n", http_body);
		printf("length of http_body : %d\n", strlen(http_body));

		if(strlen(http_body) == 0)
		{    
			printf("HTTP failure!\n");

			if(strcmp(trafficnum_tmp, "null") == 0)
				printf("Wifi detect : off, skip save traffic\n");
			else
			{
				printf("Wifi detect : on, save traffic...\n");
				
				select_cnt = 0;
				while(select_cnt < 20)
				{
					rc = sqlite3_open(traffic_saved_db, &db);
					if(rc == SQLITE_ERROR)
						printf("cannot open traffic_saved.db!\n");

					memset(sql_cmd, 0, sizeof(sql_cmd));
					sprintf(sql_cmd, "insert into traffic_saved values('%s', %d)", trafficnum_tmp, t);
					printf("sql_cmd : %s\n", sql_cmd);
					result = sqlite3_exec(db, sql_cmd, NULL, NULL, &errmsg);
	
					if(result == SQLITE_OK)
					{
						printf("insert traffic_saved.db successfully!\n");
						sqlite3_free(errmsg);
						sqlite3_close(db);
						break;
					}
					else
					{
						printf("insert traffic_saved.db error : %s", errmsg);
						sqlite3_free(errmsg);
						sqlite3_close(db);
						sleep(1);
						select_cnt++;
					}
				}
			}
		}
		else 
		{
			printf("HTTP success!\n");

			if(strcmp(trafficnum_tmp, "null") == 0)
				printf("Wifi detect : off, skip send saved_traffic\n");
			else
			{
				printf("Wifi detect : on, send saved_traffic...\n");

				printf("traffic_saved.db : check record\n");

				rc = sqlite3_open(traffic_saved_db, &db);
				if(rc == SQLITE_ERROR)
					printf("cannot open traffic_saved.db!\n");

				ppstmt = NULL;
				int saved_cnt_tmp = 0;
				sprintf(sql_cmd, "select count(*) from traffic_saved");
				sqlite3_prepare(db, sql_cmd, -1, &ppstmt, 0);
				rc = sqlite3_step(ppstmt);
				if(rc == SQLITE_ROW)
				{
					saved_cnt_tmp = sqlite3_column_int(ppstmt, 0);
					printf("saved_cnt_tmp : %d\n", saved_cnt_tmp);
				}

				if(saved_cnt_tmp == 0)
					printf("No saved traffic, skip traffic resend!\n");

				if(saved_cnt_tmp > 0)
				{
					ppstmt = NULL;
					printf("records in traffic_saved.db : %d\n", saved_cnt_tmp);
					if(saved_cnt_tmp <= 200)
						sprintf(sql_cmd, "select traffic, time from traffic_saved");
					else if(saved_cnt_tmp > 200)
						sprintf(sql_cmd, "select traffic, time from traffic_saved limit %d, %d", saved_cnt_tmp-200, 200);
					sqlite3_prepare(db, sql_cmd, -1, &ppstmt, 0);
					rc = sqlite3_step(ppstmt);
					while(rc == SQLITE_ROW)
					{
						trafficnum = sqlite3_column_text(ppstmt, 0);
						strcpy(trafficnum_tmp, trafficnum);
						printf("traffic : %s\n", trafficnum);
						t = sqlite3_column_int(ppstmt, 1);
						printf("time : %d\n", t);
								
						strcat(traffic_list, trafficnum_tmp);
						strcat(traffic_list, "-");
						sprintf(time_list, "%s%d-", time_list, t);
						rc = sqlite3_step(ppstmt);	
					}
					traffic_list[strlen(traffic_list)-1] = '\0';
					time_list[strlen(time_list)-1] = '\0';
			        
					printf("-----------send saved_traffic request-----------\n");
					ghttp_request *req_traffic_saved;
					req_traffic_saved = ghttp_request_new();
					sprintf(request_url, "http://www.ailvgobox.com/box_manage_2/traffic_resend_1.php?box_id=%s&traffic=%s&time=%s", box_id_tmp, traffic_list, time_list);
					printf("request_url : %s\n", request_url);
					strcpy(http_body, send_http_request(req_traffic_saved, request_url));
					ghttp_request_destroy(req_traffic_saved);

					printf("http_body : %s\n", http_body);              
			      
					if(strlen(http_body) > 0)
					{
						printf("traffic resend success, delete all records in traffic_saved.db\n");
						result = sqlite3_exec(db, "delete from traffic_saved", NULL, NULL, &errmsg);
						if(result != SQLITE_OK)
							printf("delete database error : %s", errmsg);
					}
				}
			
				sqlite3_free(errmsg);
				sqlite3_finalize(ppstmt);
				sqlite3_close(db);
			}
		}
	}
	else
	{
		printf("ailvgobox server : offline!\n");

		if(strcmp(trafficnum_tmp, "null") == 0)
			printf("Wifi detect : off, skip save traffic\n");
		else
		{
			printf("Wifi detect : on, save traffic...\n");

			select_cnt = 0;
			while(select_cnt < 20)
			{
				rc = sqlite3_open(traffic_saved_db, &db);
				if(rc == SQLITE_ERROR)
					printf("cannot open traffic_saved.db!\n");

				memset(sql_cmd, 0, sizeof(sql_cmd));
				sprintf(sql_cmd, "insert into traffic_saved values('%s', %d)", trafficnum_tmp, t);
				printf("sql_cmd : %s\n", sql_cmd);
				result = sqlite3_exec(db, sql_cmd, NULL, NULL, &errmsg);
			
				if(result == SQLITE_OK)
				{
					printf("insert traffic_saved.db successfully!\n");
					sqlite3_free(errmsg);
					sqlite3_close(db);
					break;
				}
				else
				{
					printf("insert traffic_saved.db error : %s\n", errmsg);
					sqlite3_free(errmsg);
					sqlite3_close(db);
					sleep(1);
					select_cnt++;
				}
			}
					
		}
	}

	printf("heart_beat : complete!\n");
}
Exemple #12
0
upload_file(const char *filename, const char *type, sqlite3 *db)
{
	FILE *stream = NULL;
	char sh_cmd[512] = {0};
	char filename_all[100] = {0};
        char filename_query[100] = {0};
	char sql_cmd[100] = {0}, del_cmd[100] = {0}, request_url[200] = {0};
	char buf[100] = {0}, uploadfile_folder[50] = {0};
	char *errorMsg = NULL, *md5_value = NULL, *md5 = NULL;
        int rc = 0;
	ghttp_request *req_upload;
        char http_body[200] = {0};

        memset(sh_cmd, 0, sizeof(sh_cmd));
	memset(filename_all, 0, sizeof(filename_all));
	memset(sql_cmd, 0, sizeof(sql_cmd));

	if(strcmp(type, "1") == 0)
	{
		sprintf(sh_cmd, "curl -u user:user -T /ailvgo/www/userfile/pic/%s.jpg ftp://www.ailvgobox.com/userfile_manage/pic/%s.jpg", filename, filename);
		sprintf(filename_all, "/ailvgo/www/userfile/pic/%s.jpg", filename);
		strcpy(uploadfile_folder, "/userfile_manage/pic/");
                sprintf(filename_query, "%s.jpg", filename);
	}
	if(strcmp(type, "2") == 0)
	{
		sprintf(sh_cmd, "curl -u user:user -T /ailvgo/www/userfile/audio/%s.mp3 ftp://www.ailvgobox.com/userfile_manage/audio/%s.mp3 ", filename, filename);
		sprintf(filename_all, "/ailvgo/www/userfile/audio/%s.mp3", filename);
		strcpy(uploadfile_folder, "/userfile_manage/audio/");
                sprintf(filename_query, "%s.mp3", filename);
	}
	if(strcmp(type, "3") == 0)
	{
		sprintf(sh_cmd, "curl -u user:user -T /ailvgo/www/userfile/video/%s.mp4 ftp://www.ailvgobox.com/userfile_manage/video/%s.mp4 ", filename, filename);
		sprintf(filename_all, "/ailvgo/www/userfile/video/%s.mp4", filename);
		strcpy(uploadfile_folder, "/userfile_manage/video/");
                sprintf(filename_query, "%s.mp4", filename);
	}
	printf("sh_md : %s\n", sh_cmd);
	sprintf(sql_cmd, "update userfile_info set upload=\"1\" where filename=\"%s\"", filename);
	printf("sql_cmd : %s\n", sql_cmd);
	if(access(filename_all, W_OK) == -1)
	{
		memset(del_cmd, 0, sizeof(del_cmd));
		sprintf(del_cmd, "delete from userfile_info where filename=\"%s\"", filename);
		printf("del_cmd : %s\n", del_cmd);
		printf("result : upload file not exist\n");
		sqlite3_exec(db, del_cmd, NULL, NULL, &errorMsg);
                if (errorMsg)
                      printf("errorMsg : %s\n", errorMsg);
		return 0;
	}
	else
	{
		md5_value = md5_file(filename_all);
		sprintf(request_url, "http://www.ailvgobox.com/box_manage_2/file_query_1.php?file_name=%s&folder=%s", filename_query, uploadfile_folder);
        	printf("request_url : %s\n", request_url);
        	req_upload = ghttp_request_new();
        	strcpy(http_body, send_http_request(req_upload, request_url));
                ghttp_request_destroy(req_upload);
        	printf("http_body : %s\n", http_body);

        	cJSON *node;

        	if(strlen(http_body) == 0)
		{
                	printf("HTTP failure!\n");
		}
		else
		{
			if(strcmp(http_body, "null") == 0)
				printf("server has no file!\n");
			else
			{
				node = cJSON_Parse(http_body);
				cJSON *file_query = cJSON_GetObjectItem(node, "file_query");
				if(file_query)
				{
					char *md5 = cJSON_GetObjectItem(file_query, "md5")->valuestring;
					printf("md5_value - md5 : %s - %s", md5_value, md5);
					if(strcmp(md5_value, md5) == 0)
					{
						printf("update databse...\n");
        					sqlite3_exec(db, sql_cmd, NULL, NULL, &errorMsg);
        					if(errorMsg)
                					printf("errorMsg : %s\n", errorMsg);
						return 1;
					}
				}
			}
		}
	}
		
	printf("upload cmd : %s\n", sh_cmd);
	
        sleep(2);
        printf("upload start...\n");
        system(sh_cmd);
        sleep(2);

	return 1;
}
Exemple #13
0
int get_http_request(char * url, char * result, int result_len){
	
	int64 post_begin = get_tick_count();
	
	ghttp_request * request = ghttp_request_new();
	if(!request){
		log_out(log_error, "http_get::ghttp_request_new fail\n");
		return -1;
	}

	if(ghttp_set_uri(request, (char*)url) == -1){
		log_out(log_error, "http_get::ghttp_set_uri fail(%s)\n", ghttp_get_error(request));
		ghttp_request_destroy(request);
		return -1;
	}

	if(ghttp_set_type(request, ghttp_type_get) == -1) {
		log_out(log_error, "http_get::ghttp_set_type fail(%s)\n", ghttp_get_error(request));
		ghttp_request_destroy(request);
		return -1;
	}
	
	//����ͷ
	ghttp_set_header(request, http_hdr_Accept_Charset, "x-www-form-urlencoded");
	ghttp_set_header(request, http_hdr_Connection, "close");
	ghttp_set_header(request, http_hdr_Content_Type, "json");
	ghttp_set_header(request, http_hdr_Timeout, "5000");  


	if(ghttp_prepare(request) < 0){
		log_out(log_error, "http_get::ghttp_prepare fail(%s)\n", ghttp_get_error(request));
		ghttp_request_destroy(request);
		return -1;
	}
	
	ghttp_status req_status = ghttp_process(request);
	if (req_status == ghttp_error) {
		log_out(log_error, "http_get::ghttp_process url(%s) fail(%s)\n",
			url, ghttp_get_error(request));
		ghttp_request_destroy(request);
		return -1;
	}
	
	int stat_code = ghttp_status_code(request);
	if(stat_code != 200){
		log_out(log_error, "http_get::status code(%d)\n", stat_code);
		ghttp_request_destroy(request);
		return -1;
	}
	
	int	rsplen = ghttp_get_body_len(request);
	char * rspbody = ghttp_get_body(request);
	if((!rspbody) || (rsplen <= 0)){
		log_out(log_error, "http_get::ghttp_get_body fail(%s)\n", ghttp_get_error(request));
		ghttp_request_destroy(request);
		return -1;
	}
	
	if(rspbody[rsplen -1] == '\n')rsplen--;
	int ncopy = result_len < rsplen ? result_len : rsplen;
	strncpy(result, rspbody, ncopy);
	ghttp_request_destroy(request);
	
	uint take = (uint)(get_tick_count() - post_begin);
	log_out(log_debug, "http_get::take(%ums) response(%s)\n", take, result);
	
	return 0;
}
Exemple #14
0
int post_http_request(char * url, char * json, int len, 
				char * result, int result_len){
	int64 post_begin = get_tick_count();
	ghttp_request * request = ghttp_request_new();
	if(!request){
		log_out(log_error, "http_post::ghttp_request_new fail\n");
		return -1;
	}

	if(ghttp_set_uri(request, (char*)url) == -1){
		log_out(log_error, "http_post::ghttp_set_uri fail(%s)\n", ghttp_get_error(request));
		ghttp_request_destroy(request);
		return -1;
	}

	if(ghttp_set_type(request, ghttp_type_post) == -1) {
		log_out(log_error, "http_post::ghttp_set_type fail(%s)\n", ghttp_get_error(request));
		ghttp_request_destroy(request);
		return -1;
	}

	//����ͷ
	ghttp_set_header(request, http_hdr_Accept_Charset, "x-www-form-urlencoded");
	ghttp_set_header(request, http_hdr_Connection, "keep-alive");
	ghttp_set_header(request, http_hdr_Content_Type, "json");
	//ghttp_set_header(request, http_hdr_Authorization, "Basic c2hlbmdhbmc6MTIzNDU2");
	ghttp_set_header(request, http_hdr_Timeout, "5000");  
	char content_len[16] = {0};
	sprintf(content_len, "%d", len);
	ghttp_set_header(request, http_hdr_Content_Length, content_len);

	if(ghttp_set_body(request, json, len) == -1){
		log_out(log_error, "http_post::ghttp_set_body fail(%s)\n", ghttp_get_error(request));
		ghttp_request_destroy(request);
		return -1;
	}

	if(ghttp_prepare(request) < 0){
		log_out(log_error, "http_post::ghttp_prepare fail(%s)\n", ghttp_get_error(request));
		ghttp_request_destroy(request);
		return -1;
	}

	ghttp_status req_status = ghttp_process(request);
	if (req_status == ghttp_error){
		log_out(log_error, "http_post::ghttp_process url(%s) fail(%s)\n",
				url, ghttp_get_error(request));
		ghttp_request_destroy(request);
		return -1;
	}

	int stat_code = ghttp_status_code(request);
	if(stat_code != 200){
		log_out(log_error, "http_post::data len(%d) status code(%d)\n", len, stat_code);
		ghttp_request_destroy(request);
		return -1;
	}

	int	rsplen = ghttp_get_body_len(request);
	char * rspbody = ghttp_get_body(request);
	if((!rspbody) || (rsplen <= 1)){
		log_out(log_error, "http_post::ghttp_get_body fail(%s)\n", ghttp_get_error(request));
		ghttp_request_destroy(request);
		return -1;
	}

	if(rspbody[rsplen - 1] == '\n'){
		rsplen--;}
	int ncopy = (result_len <= rsplen) ? result_len -1 : rsplen;
	if(result){
		strncpy(result, rspbody, ncopy);
	}

	ghttp_request_destroy(request);
	
	uint take = (uint)(get_tick_count() - post_begin);
	log_out(log_debug, "http_post::data len(%d) take(%ums) response(%s)\n", len, take, result);
	
	return 0;				
}
Exemple #15
0
int main()
{
	printf("\n********************************\n");
	printf("box_conf_change : start\n");
	printf("********************************\n");
		
	char url[1024] = {0};
	ghttp_request *req;
	char http_body[1024] = {0};

	sqlite3 *db = NULL;
    sqlite3_stmt *ppstmt = NULL;
    int rc = 0;
    char sql_cmd[100] = {0};
	char *errorMsg = NULL;
	const char *box_id = NULL;
    char box_id_tmp[10] = {0};
	int db_cnt = 0;

	while(db_cnt < 20)
	{
		rc = sqlite3_open(box_db, &db);
		if(rc == SQLITE_ERROR)
            printf("open box.db failed");

        ppstmt = NULL;
        memset(sql_cmd, 0, sizeof(sql_cmd));
        strcpy(sql_cmd, "select box_id from box_info");
        sqlite3_prepare(db, sql_cmd, -1, &ppstmt, 0);
        rc = sqlite3_step(ppstmt);
        if(rc == SQLITE_ROW)
        {
            box_id = sqlite3_column_text(ppstmt, 0);
            strcpy(box_id_tmp, box_id);
            printf("box_id : %s\n", box_id_tmp);
			sqlite3_finalize(ppstmt);
			sqlite3_close(db);
			break;
		}
		else
		{
			printf("select box_id failure!\n");
			sqlite3_finalize(ppstmt);
			sqlite3_close(db);
			sleep(1);
			db_cnt++;
		}
	}

	printf("----------------send http request-------------\n");
	sprintf(url, "http://www.ailvgobox.com/box_manage_2/box_conf_change_1.php?box_id=%s", box_id_tmp);
	printf("request_url : %s\n",url);

	req = ghttp_request_new();
	strcpy(http_body, send_http_request(req, url));
    ghttp_request_destroy(req);
	
	printf("http_body : %s\n", http_body);
    printf("length of http_body : %d\n", strlen(http_body));
        
	cJSON *node;

    if(strlen(http_body) <= 2)
		printf("HTTP failure!\n");
    else
    {
        printf("HTTP success!\n");

        if(strcmp(http_body, "null") == 0)
            printf("http_body : null, no box_conf_change!\n");
        else
        {
	        node = cJSON_Parse(http_body);
            ParseJSON(node);
        }
    }
	
	printf("box_conf_chanage : complete!\n");

	if(change_flag == 1)
	{
		printf("reboot due to conf change!\n");
		sleep(5);
		system("reboot");
	}
}