#if 1 #include "pns.h" #ifdef GY_OS_NOVA #include "bbox_receive.h" #endif #ifdef GY_OS_AMBA // #include "radar_receive.h" //#include "radar_parser.h" #endif struct MemoryStruct { char memory[MEMORY_SIZE];//256KB size_t size; }; struct data { char trace_ascii; /* 1 or 0 */ }; extern pthread_mutex_t mutex_pns; extern pthread_mutex_t mutex_snap; //extern pthread_mutex_t mutex_get_canvas; const char* key_for_ddEnc_ddDec = "qazwsxmeritlilin22988988"; //char global_url_for_pns[64] = "http://cloud.ddnsipcam.com/pns/snapshot.php"; char global_url_for_pns[64] = "https://cloud.ddnsipcam.com/pns/"; char Global_URL_for_DDNS[128] = "https://www.ddnsipcam.com/tool/ddns-acc-chk.php"; #ifdef GY_OS_AMBA static char g_canvas_addr[MAX_NUM_SNAP_IMAGES][MAX_IMG_SIZE] = { 0 }; static size_t g_canvas_size[MAX_NUM_SNAP_IMAGES] = { 0 }; #endif static int g_start_to_store_canvas = 0; #ifdef GY_OS_AMBA static size_t g_current_index_canvas_images = 0; static size_t g_cache_canvas_size = 0; #endif static int canvas_live_mode_flag = 1; CURL* curl_handle_for_snapshot = NULL; #if 1 void ddDec(char* input_str,char *restored_str) { size_t decoded_scram_str_len = 0; unsigned char temp_str[8 * 8192] = { 0 }; base64_decode(input_str, strlen(input_str), &decoded_scram_str_len, temp_str); char decoded_scrambled_str[8 * 8192] = {0}; strncpy(decoded_scrambled_str, (char *)temp_str, decoded_scram_str_len); decoded_scrambled_str[decoded_scram_str_len] = '\0'; //printf("[dd Dec] Scrambled string decoded from base64: %s\n", decoded_scrambled_str); size_t scrambled_str_len = strlen(decoded_scrambled_str); //printf("---------------[dd Dec] Length: %ld\n", scrambled_str_len); // Generate processed key string by MD5 function. unsigned char key_by_md5[16] = { 0 }; md5String((char*)key_for_ddEnc_ddDec, (uint8_t*)key_by_md5); char key_by_md5_in_char[DEF_MD5_LENGTH + 1] = { 0 }; for (int i = 0; i < 16; i++) sprintf(&key_by_md5_in_char[i * 2], "%02x", key_by_md5[i]); //key_by_md5_in_char[DEF_MD5_LENGTH] = '\0'; //printf("[dd Dec] MD5 string from hex code: %s\n", key_by_md5_in_char); /*if (key_by_md5) { free(key_by_md5); key_by_md5 = NULL; }*/ //char* restored_str = malloc(scrambled_str_len + 1); for (int i = 0; i < scrambled_str_len; i++) { //printf("input_str[%d]=%c --> %d\n", i, input_str[i], (int)input_str[i]); //printf("extract_md5_char[%d]=%c --> %d\n", i, extract_md5_char[i], (int)extract_md5_char[i]); //int value = ((int)input_str[i] + (int)extract_md5_char[i]) % 256; int value = (256 + (int)decoded_scrambled_str[i] - (int)key_by_md5_in_char[i % DEF_MD5_LENGTH]) % 256; restored_str[i] = (char)value; //printf("value=%d\n", value); } restored_str[scrambled_str_len] = '\0'; //printf("[dd Dec] Restored string: %s\n", restored_str); /*if (decoded_scrambled_str) { free(decoded_scrambled_str); decoded_scrambled_str = NULL; }*/ } #endif // External callers need to release the returned string. void ddEnc(char* input_str, char * output_str) { int input_str_len = strlen(input_str); //printf("[dd Enc] Input string: %s\n", input_str); //printf("[dd Enc] Length: %d\n", input_str_len); // Generate processed key string by MD5 function. unsigned char key_by_md5[16] = { 0 }; md5String((char*)key_for_ddEnc_ddDec, (uint8_t*)key_by_md5); char key_by_md5_in_char[DEF_MD5_LENGTH + 1] = { 0 }; for (int i = 0; i < (DEF_MD5_LENGTH/2); i++) sprintf(&key_by_md5_in_char[i * 2], "%02x", key_by_md5[i]); //key_by_md5_in_char[DEF_MD5_LENGTH] = '\0'; //printf("[dd Enc] MD5 string (hex code): %s\n", key_by_md5_in_char); /*if (key_by_md5) { free(key_by_md5); key_by_md5 = NULL; }*/ // Scramble original string by MD5 key string. unsigned char scrambled_values[8 * 8192] = { 0 }; for (int i = 0; i < input_str_len; i++) { int value = ((int)input_str[i] + (int)key_by_md5_in_char[i % DEF_MD5_LENGTH]) % 256; scrambled_values[i] = (unsigned char)value; //printf("value=%d\n", value); } //scrambled_values[input_str_len] = '\0'; //printf("[dd Enc] Scrambled string (ASCII): %s\n", scrambled_values); size_t base64_encode_len = 0; char original_base64_output[8*8192] = { 0 }; base64_encode(scrambled_values, input_str_len, &base64_encode_len, original_base64_output); //printf("sizeof(original_base64_output)=%ld\n", sizeof(original_base64_output)); //printf("[dd Enc] Base64 encoded length: %ld\n", base64_encode_len); strncpy(output_str, original_base64_output, base64_encode_len); output_str[base64_encode_len] = '\0'; //printf("[dd Enc] Base64 encoded scrambled string: %s\n", output_str); #if 0 // Simply test ddDec from the end of the ddEnc. // Should be removed before the project is released. char* test_str = ddDec(output_str); if (test_str) { free(test_str); test_str = NULL; } #endif } #ifdef CURL_SAVE_JPG_TO_BUFF static size_t WriteMemoryCallback(void* contents, size_t size, size_t nmemb, void* user_ptr) { size_t realsize = size * nmemb; struct MemoryStruct* mem = (struct MemoryStruct*)user_ptr; /* char* ptr = realloc(mem->memory, mem->size + realsize + 1); if (!ptr) { // out of memory! printf("not enough memory (realloc returned NULL)\n"); return 0; } */ if (mem->size + realsize + 1 >= MEMORY_SIZE) { // out of memory! printf("memory is over MEMORY_SIZE size (returned NULL)\n"); return 0; } //mem->memory = ptr; memcpy(&(mem->memory[mem->size]), contents, realsize); mem->size += realsize; mem->memory[mem->size] = 0; return realsize; } #else #if 0 struct FtpFile { const char* filename; FILE* stream; }; static size_t WriteToFileCallback(void* buffer, size_t size, size_t nmemb, void* user_ptr) { struct FtpFile* out = (struct FtpFile*)user_ptr; if (!out->stream) { /* open file for writing */ out->stream = fopen(out->filename, "wb"); if (!out->stream) return -1; /* failure, can't open file to write */ } return fwrite(buffer, size, nmemb, out->stream); } #endif #endif static size_t WriteResponseString(void* contents, size_t size, size_t nmemb, void* user_ptr) { size_t realsize = size * nmemb; struct MemoryStruct* mem = (struct MemoryStruct*)user_ptr; /* char* ptr = realloc(mem->memory, mem->size + realsize + 1); if (!ptr) { // out of memory! printf("not enough memory (realloc returned NULL)\n"); return 0; }*/ if (mem->size + realsize + 1 >= MEMORY_SIZE)//25KB { printf("over MEMORY_SIZE KB memory (returned NULL)\n"); return 0; } //mem->memory = ptr; memcpy(&(mem->memory[mem->size]), contents, realsize); mem->size += realsize; mem->memory[mem->size] = 0; return realsize; } #if 0 static void dump(const char* text, FILE* stream, unsigned char* ptr, size_t size, char nohex) { size_t i; size_t c; unsigned int width = 0x10; if (nohex) /* without the hex output, we can fit more on screen */ width = 0x40; fprintf(stream, "%s, %10.10lu bytes (0x%8.8lx)\n", text, (unsigned long)size, (unsigned long)size); for (i = 0; i < size; i += width) { fprintf(stream, "%4.4lx: ", (unsigned long)i); if (!nohex) { /* hex not disabled, show it */ for (c = 0; c < width; c++) if (i + c < size) fprintf(stream, "%02x ", ptr[i + c]); else fputs(" ", stream); } for (c = 0; (c < width) && (i + c < size); c++) { /* check for 0D0A; if found, skip past and start a new line of output */ if (nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D && ptr[i + c + 1] == 0x0A) { i += (c + 2 - width); break; } fprintf(stream, "%c", (ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80) ? ptr[i + c] : '.'); /* check again for 0D0A, to avoid an extra \n if it's at width */ if (nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D && ptr[i + c + 2] == 0x0A) { i += (c + 3 - width); break; } } fputc('\n', stream); /* newline */ } fflush(stream); } #endif #if 0 static int my_trace(CURL* handle, curl_infotype type, char* data, size_t size, void* userp) { struct data* debug_data = (struct data*)userp; const char* text; (void)handle; /* prevent compiler warning */ switch (type) { case CURLINFO_TEXT: fprintf(stderr, "== Info: %s", data); /* FALLTHROUGH */ default: /* in case a new one is introduced to shock us */ return 0; case CURLINFO_HEADER_OUT: text = "=> Send header"; break; case CURLINFO_DATA_OUT: text = "=> Send data"; break; case CURLINFO_SSL_DATA_OUT: text = "=> Send SSL data"; break; case CURLINFO_HEADER_IN: text = "<= Recv header"; break; case CURLINFO_DATA_IN: text = "<= Recv data"; break; case CURLINFO_SSL_DATA_IN: text = "<= Recv SSL data"; break; } dump(text, stderr, (unsigned char*)data, size, debug_data->trace_ascii); return 0; } #endif int PNS_Get_Device_Name(int arg_url_encoded_or_not,char *returned_msg) { int get_OK = 0; pthread_mutex_lock(&mutex_curl); /*if (g_http_handle != NULL) { curl_easy_cleanup(g_http_handle); curl_global_cleanup(); curl_global_init(CURL_GLOBAL_SSL); g_http_handle = NULL; g_http_handle = curl_easy_init(); }*/ //pthread_mutex_lock(&mutex_pns); //char error_msg[CURL_ERROR_SIZE]; CURLcode result = 0; CURL* curl_handle_for_dev_name = g_http_handle; // Initialization for current handler. if (curl_handle_for_dev_name) { struct curl_slist *pList = NULL; pList = curl_slist_append(pList, "Connection: close"); pList = curl_slist_append(pList, "charset: utf-8"); pList = curl_slist_append(pList, "Expect:"); curl_easy_setopt(curl_handle_for_dev_name, CURLOPT_HTTPHEADER, pList); // Specify URL for getting the snapshot. char url_for_dev_name[50] = "http://"; //strcat(url_for_dev_name, accountData[0].account_username); //strcat(url_for_dev_name, ":"); //strcat(url_for_dev_name, accountData[0].account_password); strcat(url_for_dev_name, "127.0.0.1:");//@127.0.0.1: strcat(url_for_dev_name, accountData[0].account_port); strcat(url_for_dev_name, "/server"); curl_easy_setopt(curl_handle_for_dev_name, CURLOPT_URL, url_for_dev_name); // Invoke "server" command from localhost address. char buf_account[512] = { 0 }; urldecode((unsigned char *)accountData[0].account_username, (unsigned char *)buf_account); char buf_account2[512] = { 0 }; urldecode((unsigned char *)accountData[0].account_password, (unsigned char *)buf_account2); curl_easy_setopt(curl_handle_for_dev_name, CURLOPT_USERNAME, buf_account); curl_easy_setopt(curl_handle_for_dev_name, CURLOPT_PASSWORD, buf_account2); long port_num = atoi((const char*)accountData[0].account_port); curl_easy_setopt(curl_handle_for_dev_name, CURLOPT_PORT, port_num); curl_easy_setopt(curl_handle_for_dev_name, CURLOPT_CONNECTTIMEOUT, 5); // 設定連線超時,單位秒 curl_easy_setopt(curl_handle_for_dev_name, CURLOPT_TIMEOUT, 10); curl_easy_setopt(curl_handle_for_dev_name, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(curl_handle_for_dev_name, CURLOPT_SSL_VERIFYPEER, 0); //證書取消 curl_easy_setopt(curl_handle_for_dev_name, CURLOPT_SSL_VERIFYHOST, 0); //證書取消 curl_easy_setopt(curl_handle_for_dev_name, CURLOPT_USERAGENT, "IPVideo"); curl_easy_setopt(curl_handle_for_dev_name, CURLOPT_VERBOSE, 0L); // Switch on full protocol/debug output curl_easy_setopt(curl_handle_for_dev_name, CURLOPT_LOW_SPEED_LIMIT, 1L); curl_easy_setopt(curl_handle_for_dev_name, CURLOPT_LOW_SPEED_TIME, 5L); //printf("[PNS Get Device Name] CURLOPT_URL: %s\n", curl_easy_strerror(result)); //curl_easy_setopt(curl_handle_for_dev_name, CURLOPT_ERRORBUFFER, error_msg); //printf("[PNS Get Device Name] CURLOPT_ERRORBUFFER: %s\n", curl_easy_strerror(result)); curl_easy_setopt(curl_handle_for_dev_name, CURLOPT_WRITEFUNCTION, WriteResponseString); //printf("[PNS Get Device Name] CURLOPT_WRITEFUNCTION: %s\n", curl_easy_strerror(result)); curl_easy_setopt(curl_handle_for_dev_name, CURLOPT_TCP_FASTOPEN, 1L); struct MemoryStruct memory_temp_msg; struct MemoryStruct* ptr_responded_msg = &memory_temp_msg; //ptr_responded_msg->memory = NULL; ptr_responded_msg->size = 0; curl_easy_setopt(curl_handle_for_dev_name, CURLOPT_WRITEDATA, ptr_responded_msg); //printf("[PNS Get Device Name] CURLOPT_WRITEDATA: %s\n", curl_easy_strerror(result)); curl_easy_setopt(curl_handle_for_dev_name, CURLOPT_FORBID_REUSE, 0L); //Set to 0 to have libcurl keep the connection open for possible later re-use (default behavior). result = curl_easy_perform(curl_handle_for_dev_name); //printf("[PNS Get Device Name] curl_easy_perform: %s\n", curl_easy_strerror(result)); char dev_name[512] = { 0 }; //char* returned_msg = NULL; if (result != CURLE_OK) // Check for errors { char* err_msg = (char*)curl_easy_strerror(result); strcpy(returned_msg, err_msg); write_to_logs_html(returned_msg, "PNS Get Device Name", "ERROR", SystemSetting.enable_system_logs); fprintf(stderr, "[PNS Get Device Name] Failed to initialize the CURL handler for curl_handle_for_dev_name. Reason: %s\n", err_msg); //printf("[PNS Get Device Name] Failed to initialize the CURL for curl_handle_for_dev_name. Reason: %s\n", error_msg); } else { //printf("[PNS Get Device Name] ptr_responded_msg->size = %ld\n", ptr_responded_msg->size); //printf("[PNS Get Device Name] ptr_responded_msg->memory = %s\n", ptr_responded_msg->memory); char *sFindStr_Start = NULL; char *sFindStr_End = NULL; sFindStr_Start = strstr(ptr_responded_msg->memory, "device name="); if (sFindStr_Start) { sFindStr_Start = sFindStr_Start + strlen("device name="); sFindStr_End = strstr(sFindStr_Start, "\n"); } if (sFindStr_End) { if ((sFindStr_End - sFindStr_Start) > 0) { memcpy(dev_name, sFindStr_Start, (sFindStr_End - sFindStr_Start)); #ifdef GY_OS_AMBA check_ptz(ptr_responded_msg->memory); #endif char *sFindStr_Start_svn = NULL; char *sFindStr_End_svn = NULL; sFindStr_Start_svn = strstr(ptr_responded_msg->memory, "SVN_VERSION="); if (sFindStr_Start_svn) { sFindStr_Start_svn = sFindStr_Start_svn + strlen("SVN_VERSION="); sFindStr_End_svn = strstr(sFindStr_Start_svn, "\n"); } if (sFindStr_End_svn) { if ((sFindStr_End_svn - sFindStr_Start_svn) > 0) { char svn_version_name[512] = { 0 }; char temp_svn_version_name[512] = { 0 }; memcpy(svn_version_name, sFindStr_Start_svn, (sFindStr_End_svn - sFindStr_Start_svn)); int check_sdk_ok = 0; int index_temp = 0; //printf("\n------svn_version_name:%sEND\n", svn_version_name); for (int index_c = 0; index_c < (int)strlen(svn_version_name); index_c++) { if (isdigit(svn_version_name[index_c])) { check_sdk_ok = 1; temp_svn_version_name[index_temp] = svn_version_name[index_c]; index_temp++; } } if (check_sdk_ok == 1) { temp_svn_version_name[index_temp] = '\0'; g_sdk_version = atoi(temp_svn_version_name); //printf("\n----------g_sdk_version:%d\n", g_sdk_version); } } } } else { //pthread_mutex_unlock(&mutex_pns); static const char err_msg[] = "(Device name fetching error)"; strcpy(returned_msg, err_msg); write_to_logs_html(returned_msg, "PNS Get Device Name", "ERROR", SystemSetting.enable_system_logs); printf("[PNS Get Device Name] Device name fetching error.\n"); } } else { //pthread_mutex_unlock(&mutex_pns); static const char err_msg[] = "(Device name is empty or too long)\n"; strcpy(returned_msg, err_msg); write_to_logs_html(returned_msg, "PNS Get Device Name", "ERROR", SystemSetting.enable_system_logs); printf("[PNS Get Device Name] Device name is empty or too long.\n"); } } curl_easy_reset(curl_handle_for_dev_name); if (pList != NULL) curl_slist_free_all(pList); ptr_responded_msg->size = 0; if (arg_url_encoded_or_not == 1 && strlen(dev_name) >= 1) { CURL* curl_for_url_encode = g_http_handle; char* url_encoded_str = curl_easy_escape(curl_for_url_encode, dev_name, 0); // 0: Uses strlen() on the input string to find out the size. strcpy(returned_msg, url_encoded_str); //printf("[PNS Get Device Name] URL encoded device name: %s", returned_msg); curl_free(url_encoded_str); curl_easy_reset(curl_for_url_encode); get_OK = 1; } /*if (ptr_responded_msg != NULL) { free(ptr_responded_msg); ptr_responded_msg = NULL; }*/ } pthread_mutex_unlock(&mutex_curl); return get_OK; } // Based on the definitions from Vega's document "PNS2_API-1.pdf", part 2-1. void PNS_Query_Service_Status(char* returned_msg) { CURL* curl_handle_for_pns; CURLcode res_pns; // For starting PNS and connections to the cloud. //struct data debug_data_for_query; pthread_mutex_lock(&mutex_curl); curl_handle_for_pns = g_http_handle; // Initialization for each handler. // cat: Category, pns/others // act: Actions, such as check, send or list. // vend: Vendor, 0 for LILIN, 1, 2, 3, 4, 5, 6, 7, and etc. // ver: 2.1 // prd: Production ready (keyword: prd) or development (keyword: dev) // cno: Random check num. Server will add 1 in response. // dt: Device type, 0:IPCam, 1:NVR, 2:NAV // et: event type (no need in 2-1) // eid: event ID (no need in 2-1) // mac: MAC addr. (Done) // msg: (no need in 2-1) // tp: describe what kind of alarm motion event. For AI team, it's always 8 (or shown "ag==" after ddEnc). // dt: date // tm: time // cu: camera URL // cp: camera port // vu: video URL of the event // im: information // ch: in which channel of the event // de: device type // mc: which MAC addr. detects the event // dn: device name (URL encoded) // es: event type string (AI event title in locale) // en: event type ID // ds: event description (AI event description in locale) const char* data0 = "{ \"cat\": \"oqPU\", \"act\" : \"lZ3GmJs=\", \"vend\" : \"Yg==\", \"ver\" : \"ZGOS\", \"prd\" : \"oqfF\", \"cno\" : \""; const char* data1 = "\", \"dt\" : \"Yg==\", \"et\" : \"\", \"eid\" : \"\", \"mac\" : \""; const char* data2 = "\" }"; /*const char* data2 = "\", \"msg\" : [ {\ \"tp\" : \"ag==\",\ \"dt\" : \"ZGWSbl1hZWJjlA==\",\ \"tm\" : \"Y2ebZ2NqZmk=\",\ \"cu\" : \"mqnVpWpfYmZelWZnlGQ=\",\ \"cp\" : \"amWZZQ==\",\ \"vu\" : \"mqnVpWpfYmZelWZnlGRm3M+ZnNNfmKHP0V+TlWVq\",\ \"im\" : \"l66rZpNiia55zaedyp2N1L+Kec+KfGrgwoikzYuNZ9qLZ5qpeZmqnpOqfq7JqYCcr5+NvJGNpc7EnbTde6Rnog==\",\ \"ch\" : \"Yg==\",\ \"de\" : \""; const char* data3 = "\",\ \"mc\" : \"YmWRe3ZzbG5mmWhn\",\ \"dn\" : \"V3qWWmlgWG10iH1ri3Fvi6hm\",\ \"es\" : \"\",\ \"en\" : \"\",\ \"ds\" : \"V3qVWnJxWG1yiH1oi3J5i6hr\" } ] }";*/ //-------------------------------------------------- // Random number 0-999 for 'cno' field. int rand_for_cno = rand() % 1000; char str_rand_for_cno[8] = { 0 }; snprintf(str_rand_for_cno, 5, "%d", rand_for_cno); char ddenc_str_rand_for_cno[8] = { 0 }; ddEnc(str_rand_for_cno, ddenc_str_rand_for_cno); //-------------------------------------------------- // MAC address char mac_addr[32] = { 0 }; char mac_addr_temp[32] = { 0 }; memset(mac_addr_temp, 0x00, sizeof(mac_addr_temp)); if (strlen(g_mac_address) >= 1) { strcpy(mac_addr_temp,g_mac_address); } else { GetMACAddress(mac_addr_temp); // Can use another function "GetMACAddress_WithoutDash" to reduce codes but need to verify after changing. } int index_mac_addr = 0; for (int i = 0; i < (int)strlen(mac_addr_temp); i++) { if (mac_addr_temp[i] == '-' || mac_addr_temp[i] == '\n') { } else { mac_addr[index_mac_addr] = mac_addr_temp[i]; index_mac_addr++; } } mac_addr[index_mac_addr] = '\0'; char ddenc_mac[32] = { 0 }; ddEnc(mac_addr, ddenc_mac); //-------------------------------------------------- // Calculate the total length of the data //size_t total_len = strlen(data0) + strlen(ddenc_str_rand_for_cno) + strlen(data1) + strlen(ddenc_mac) + strlen(data2) + strlen(ddenc_device_name) + strlen(data3); //size_t total_len = strlen(data0) + strlen(ddenc_str_rand_for_cno) + strlen(data1) + strlen(ddenc_mac) + strlen(data2); //-------------------------------------------------- // Assemble the string for sending char send_data[8 * 8192] = { 0 }; strcpy(send_data, data0); strcat(send_data, ddenc_str_rand_for_cno); strcat(send_data, data1); strcat(send_data, ddenc_mac); strcat(send_data, data2); //strcat(send_data, ddenc_device_name); //strcat(send_data, data3); //send_data[total_len] = '\0'; //printf("[PNS Query Service Status] JSON string for querying: %s\n", send_data); //-------------------------------------------------- // CURL section struct curl_slist* headers = NULL; headers = curl_slist_append(headers, "Accept: application/json"); headers = curl_slist_append(headers, "Content-Type: application/json"); headers = curl_slist_append(headers, "charset: utf-8"); headers = curl_slist_append(headers, "Connection: close"); curl_easy_setopt(curl_handle_for_pns, CURLOPT_HTTPHEADER, headers); // Specify URL to send POST data //CURLcode result = 0; curl_easy_setopt(curl_handle_for_pns, CURLOPT_URL, global_url_for_pns); //printf("[PNS Query Service Status] CURLOPT_URL: %s\n", curl_easy_strerror(result)); //result = curl_easy_setopt(curl_handle_for_pns, CURLOPT_USE_SSL, CURLUSESSL_ALL); //printf("[PNS] CURLOPT_USE_SSL: %s\n", curl_easy_strerror(result)); //result = curl_easy_setopt(curl_handle_for_pns, CURLOPT_SSL_VERIFYPEER, 1L); //printf("[PNS Query Service Status] CURLOPT_SSL_VERIFYPEER: %s\n", curl_easy_strerror(result)); //result = curl_easy_setopt(curl_handle_for_pns, CURLOPT_SSL_VERIFYHOST, 2L); //printf("[PNS Query Service Status] CURLOPT_SSL_VERIFYHOST: %s\n", curl_easy_strerror(result)); //result = curl_easy_setopt(curl_handle_for_pns, CURLOPT_USERAGENT, "curl/7.64.1"); //printf("[PNS] CURLOPT_USERAGENT: %s\n", curl_easy_strerror(result)); curl_easy_setopt(curl_handle_for_pns, CURLOPT_LOW_SPEED_LIMIT, 1L); curl_easy_setopt(curl_handle_for_pns, CURLOPT_LOW_SPEED_TIME, 10L); //result = curl_easy_setopt(http_handle, CURLOPT_USERNAME, ""); //printf("[PNS] CURLOPT_USERNAME: %s\n", curl_easy_strerror(result)); //result = curl_easy_setopt(http_handle, CURLOPT_PASSWORD, ""); //printf("[PNS] CURLOPT_PASSWORD: %s\n", curl_easy_strerror(result)); //result = curl_easy_setopt(curl_handle_for_pns, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); //printf("[PNS] CURLOPT_SSLVERSION: %s\n", curl_easy_strerror(result)); //result = curl_easy_setopt(curl_handle_for_pns, CURLOPT_CAINFO, "/emmc/plugin/Aida_data/cacert.pem"); curl_easy_setopt(curl_handle_for_pns, CURLOPT_SSL_VERIFYPEER, 0); //證書取消 curl_easy_setopt(curl_handle_for_pns, CURLOPT_SSL_VERIFYHOST, 0); //證書取消 //printf("[PNS Query Service Status] CURLOPT_CAINFO: %s\n", curl_easy_strerror(result)); //result = curl_easy_setopt(curl_handle_for_pns, CURLOPT_SSL_CIPHER_LIST, "TLSv1.2"); // No need. //result = curl_easy_setopt(curl_handle_for_pns, CURLOPT_TLS13_CIPHERS, NULL); // No need. //result = curl_easy_setopt(curl_handle_for_pns, CURLOPT_USERNAME, "xxx"); // No need. //result = curl_easy_setopt(curl_handle_for_pns, CURLOPT_PASSWORD, "yyy"); // No need. //char error_for_pns[CURL_ERROR_SIZE]; //curl_easy_setopt(curl_handle_for_pns, CURLOPT_ERRORBUFFER, error_for_pns); //printf("[PNS Query Service Status] CURLOPT_ERRORBUFFER: %s\n", curl_easy_strerror(result)); curl_easy_setopt(curl_handle_for_pns, CURLOPT_POST, 1L); // Switch on full protocol/debug output //printf("[PNS Query Service Status] CURLOPT_POST: %s\n", curl_easy_strerror(result)); //curl_easy_setopt(curl_handle_for_pns, CURLOPT_READFUNCTION, read_callback); //curl_easy_setopt(curl_handle_for_pns, CURLOPT_READDATA, &payload); struct MemoryStruct memory_temp_msg; struct MemoryStruct* ptr_responded_msg = &memory_temp_msg; //ptr_responded_msg->memory = NULL; ptr_responded_msg->size = 0; curl_easy_setopt(curl_handle_for_pns, CURLOPT_WRITEFUNCTION, WriteResponseString); //printf("[PNS Query Service Status] CURLOPT_WRITEFUNCTION: %s\n", curl_easy_strerror(result)); curl_easy_setopt(curl_handle_for_pns, CURLOPT_WRITEDATA, ptr_responded_msg); //printf("[PNS Query Service Status] CURLOPT_WRITEDATA: %s\n", curl_easy_strerror(result)); curl_easy_setopt(curl_handle_for_pns, CURLOPT_POSTFIELDSIZE, strlen(send_data)); //printf("[PNS Query Service Status] CURLOPT_POSTFIELDSIZE: %s\n", curl_easy_strerror(result)); curl_easy_setopt(curl_handle_for_pns, CURLOPT_POSTFIELDS, send_data); //printf("[PNS Query Service Status] CURLOPT_POSTFIELDS: %s\n", curl_easy_strerror(result)); //result = curl_easy_setopt(curl_handle_for_pns, CURLOPT_DEBUGFUNCTION, my_trace); //printf("[PNS] CURLOPT_DEBUGFUNCTION: %s\n", curl_easy_strerror(result)); //result = curl_easy_setopt(curl_handle_for_pns, CURLOPT_DEBUGDATA, &debug_data_for_query); //printf("[PNS] CURLOPT_DEBUGDATA: %s\n", curl_easy_strerror(result)); curl_easy_setopt(curl_handle_for_pns, CURLOPT_VERBOSE, 1L); // Switch on full protocol/debug output //printf("[PNS Query Service Status] CURLOPT_VERBOSE: %s\n", curl_easy_strerror(result)); curl_easy_setopt(curl_handle_for_pns, CURLOPT_TCP_FASTOPEN, 1L); curl_easy_setopt(curl_handle_for_pns, CURLOPT_FORBID_REUSE, 0L); //Set to 0 to have libcurl keep the connection open for possible later re-use (default behavior). res_pns = curl_easy_perform(curl_handle_for_pns); if (res_pns != CURLE_OK) { char* err_msg = (char*)curl_easy_strerror(res_pns); strcpy(returned_msg, err_msg); fprintf(stderr, "[PNS Query Service Status] Failed to initialize the CURL handler for PNS. Reason: %s\n", returned_msg); //printf("[PNS Query Service Status] Failed to initialize the CURL for PNS Query. Reason: %s\n", error_for_pns); } else { strcpy(returned_msg, ptr_responded_msg->memory); //printf("[PNS Query Service Status] curl_easy_perform(curl_handle_for_pns) succeeded.\n"); } // Should return: // {"err":"Yg==","msg":"oaA=","ut":"Y2uTZmhjaWVolw==","cat":"oqPU","act":"paPCpaOYoqk=","mac":"YmWRe3ZzZWlmpG54","cno":"Y2eV","vend":"Yg==","prd":"oqfF","dt":"Yg==","et":"Yg==","eid":"a24=","task":"Yg=="} curl_slist_free_all(headers); ptr_responded_msg->size = 0; /*if (ptr_responded_msg) { free(ptr_responded_msg); ptr_responded_msg = NULL; }*/ curl_easy_reset(curl_handle_for_pns); pthread_mutex_unlock(&mutex_curl); } int count_snap_curl = -1; size_t PNS_Get_Snapshot_From_IPCam(char* file_name, char* img_addr, int enable_to_save_file)//remote看起來程式碼是測試用的,這邊就先拿掉 { pthread_mutex_lock(&mutex_curl); /*if (count_snap_curl == -1) { count_snap_curl = 0; initial_curl_handle_for_snapshot(); } else if(curl_handle_for_snapshot == NULL)*/{ //count_snap_curl = 0; initial_curl_handle_for_snapshot(); } //count_snap_curl++; int returned_size = 0; // Specify URL for getting the snapshot. char url_for_snapshot[50] = "http://"; //strcat(url_for_snapshot, accountData[0].account_username); //strcat(url_for_snapshot, ":"); //strcat(url_for_snapshot, accountData[0].account_password); strcat(url_for_snapshot, "127.0.0.1:");//@127.0.0.1: strcat(url_for_snapshot, accountData[0].account_port); strcat(url_for_snapshot, "/snap"); //char error_for_snapshot[CURL_ERROR_SIZE]; int check_reboot = 0; if (curl_handle_for_snapshot) { struct curl_slist *pList = NULL; pList = curl_slist_append(pList, "Connection: close"); pList = curl_slist_append(pList, "charset: utf-8"); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_HTTPHEADER, pList); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_URL, url_for_snapshot); char buf_account[512] = { 0 }; urldecode((unsigned char *)accountData[0].account_username, (unsigned char *)buf_account); char buf_account2[512] = { 0 }; urldecode((unsigned char *)accountData[0].account_password, (unsigned char *)buf_account2); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_USERNAME, buf_account); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_PASSWORD, buf_account2); long port_num = atoi((const char*)accountData[0].account_port); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_PORT, port_num); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_CONNECTTIMEOUT, 1); // 設定連線超時,單位秒 curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_TIMEOUT, 3); //curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_ACCEPT_ENCODING, "gzip"); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_SSL_VERIFYHOST, 0L); //curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_USERAGENT, "IPVideo"); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_LOW_SPEED_LIMIT, 1L); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_LOW_SPEED_TIME, 1L); //curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_ERRORBUFFER, error_for_snapshot); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_VERBOSE, 0L); // Switch on full protocol/debug output (1L) curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_TCP_FASTOPEN, 1L); //printf("[PNS] Start to fetch the snapshot from IP Cam.\n"); struct MemoryStruct memory_temp_msg; struct MemoryStruct* ptr_received_img = &memory_temp_msg; //ptr_received_img->memory = NULL; /* will be grown as needed by the realloc above */ ptr_received_img->size = 0; /* no data at this point */ curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); // Send all data to this function curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_WRITEDATA, ptr_received_img); // Pass 'chunk' struct to the callback function curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_FORBID_REUSE, 0L); //Set to 0 to have libcurl keep the connection open for possible later re-use (default behavior). CURLcode res_snapshot = curl_easy_perform(curl_handle_for_snapshot); if (res_snapshot != CURLE_OK) // Check for errors { fprintf(stderr, "[PNS Get Snapshot From IPCam] Failed to initialize the CURL handler for snapshot. Reason: %s\n", curl_easy_strerror(res_snapshot)); //printf("[PNS : Query For Service] Failed to initialize the CURL for snapshot. Reason: %s\n", error_for_snapshot); if (res_snapshot == CURLE_OUT_OF_MEMORY) { printf("[PNS Get Snapshot From IPCam]CURLE_OUT_OF_MEMORY may sometimes indicate a conversion error \n instead of a memory allocation error if CURL_DOES_CONVERSIONS \n is defined\n"); check_reboot = 1; } } else { //printf("[PNS Get Snapshot From IPCam] ptr_received_img->memory addr. = %p\n", ptr_received_img->memory); //printf("[PNS Get Snapshot From IPCam] ptr_received_img->size = %lu (bytes retrieved)\n", (unsigned long)ptr_received_img->size); #if 1 memcpy(img_addr, &ptr_received_img->memory, ptr_received_img->size); returned_size = ptr_received_img->size; if (enable_to_save_file == 1) { pthread_mutex_lock(&mutex_snap); // Protect the file I/O. char write_filename[256] = "/emmc/plugin/Aida_data/storage/"; strcat(write_filename, file_name); FILE* fp = fopen(write_filename, "w+"); if (fp != NULL) { fwrite(ptr_received_img->memory, sizeof(char), ptr_received_img->size, fp); //printf("[PNS Get Snapshot From IPCam] Wrote to the file: %s.\n", write_filename); fclose(fp); } else printf("[PNS Get Snapshot From IPCam] (For debugging) Error occurred when opening file I/O.\n"); pthread_mutex_unlock(&mutex_snap); // Protect the file I/O. } #endif } if (pList != NULL) curl_slist_free_all(pList); ptr_received_img->size = 0; /*if (ptr_received_img != NULL) { free(ptr_received_img); ptr_received_img = NULL; }*/ curl_easy_reset(curl_handle_for_snapshot); } else printf("[PNS Get Snapshot From IPCam] CURL failed to initialize!\n"); pthread_mutex_unlock(&mutex_curl); if (check_reboot == 1) { char temp_msg[8192] = { 0 }; snprintf(temp_msg,sizeof(temp_msg), "free:%d,%s", get_free_mem_data(), "CURLE OUT OF MEMORY"); write_to_logs_html(temp_msg, "PNS Get Snapshot", "CGI_REBOOT", "Yes"); write_to_log_if_error(temp_msg, "PNS Get Snapshot", "CGI_REBOOT"); saveCounters(); pthread_t auto_reboot_thread_id; if (pthread_create(&auto_reboot_thread_id, 0, auto_reboot, NULL)) { printf("create auto reboot thread faile \n"); } } return returned_size; } #ifdef GY_OS_AMBA int count_remote_snap_curl = -1; size_t PNS_Get_Snapshot_From_Remote_IPCam(char* file_name, char* img_addr, int enable_to_save_file) // 將 remote 連線方式寫在此 function,避免影響原本的 PNS_Get_Snapshot_From_IPCam { pthread_mutex_lock(&mutex_curl); /*if (count_remote_snap_curl == -1) { count_remote_snap_curl = 0; initial_curl_handle_for_snapshot(); } else if(curl_handle_for_snapshot == NULL)*/{ //count_remote_snap_curl = 0; initial_curl_handle_for_snapshot(); } count_remote_snap_curl++; int returned_size = 0; // Specify URL for getting the snapshot. char url_for_snapshot[50] = "http://"; // 改從 setting 欄位的 IP 和 Port 建立連線 strcat(url_for_snapshot, tofData.tof_camera_ip); strcat(url_for_snapshot, ":"); strcat(url_for_snapshot, tofData.tof_camera_port); strcat(url_for_snapshot, "/snap"); //char error_for_snapshot[CURL_ERROR_SIZE]; int check_reboot = 0; if (curl_handle_for_snapshot) { struct curl_slist *pList = NULL; pList = curl_slist_append(pList, "Connection: close"); pList = curl_slist_append(pList, "charset: utf-8"); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_HTTPHEADER, pList); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_URL, url_for_snapshot); // curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_USERNAME, accountData[0].account_username); // curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_PASSWORD, accountData[0].account_password); // long port_num = atoi((const char*)accountData[0].account_port); // curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_PORT, port_num); // 改從 setting 欄位的 user & password & port number 建立連線 char buf_account[512] = { 0 }; urldecode((unsigned char *)tofData.tof_camera_username, (unsigned char *)buf_account); char buf_account2[512] = { 0 }; urldecode((unsigned char *)tofData.tof_camera_password, (unsigned char *)buf_account2); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_USERNAME, buf_account); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_PASSWORD, buf_account2); long port_num = atoi((const char*)tofData.tof_camera_port); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_PORT, port_num); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_CONNECTTIMEOUT, 1); // 設定連線超時,單位秒 curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_TIMEOUT, 3); //curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_ACCEPT_ENCODING, "gzip"); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_SSL_VERIFYHOST, 0L); //curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_USERAGENT, "IPVideo"); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_LOW_SPEED_LIMIT, 1L); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_LOW_SPEED_TIME, 1L); //curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_ERRORBUFFER, error_for_snapshot); curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_VERBOSE, 0L); // Switch on full protocol/debug output (1L) curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_TCP_FASTOPEN, 1L); //printf("[PNS] Start to fetch the snapshot from IP Cam.\n"); struct MemoryStruct memory_temp_msg; struct MemoryStruct* ptr_received_img = &memory_temp_msg; //ptr_received_img->memory = NULL; /* will be grown as needed by the realloc above */ ptr_received_img->size = 0; /* no data at this point */ curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); // Send all data to this function curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_WRITEDATA, ptr_received_img); // Pass 'chunk' struct to the callback function curl_easy_setopt(curl_handle_for_snapshot, CURLOPT_FORBID_REUSE, 0L); //Set to 0 to have libcurl keep the connection open for possible later re-use (default behavior). CURLcode res_snapshot = curl_easy_perform(curl_handle_for_snapshot); if (res_snapshot != CURLE_OK) // Check for errors { fprintf(stderr, "[PNS Get Snapshot From Remote IPCam] Failed to initialize the CURL handler for snapshot. Reason: %s\n", curl_easy_strerror(res_snapshot)); //printf("[PNS : Query For Service] Failed to initialize the CURL for snapshot. Reason: %s\n", error_for_snapshot); if (res_snapshot == CURLE_OUT_OF_MEMORY) { printf("[PNS Get Snapshot From Remote IPCam]CURLE_OUT_OF_MEMORY may sometimes indicate a conversion error \n instead of a memory allocation error if CURL_DOES_CONVERSIONS \n is defined\n"); check_reboot = 1; } } else { //printf("[PNS Get Snapshot From Remote IPCam] ptr_received_img->memory addr. = %p\n", ptr_received_img->memory); //printf("[PNS Get Snapshot From Remote IPCam] ptr_received_img->size = %lu (bytes retrieved)\n", (unsigned long)ptr_received_img->size); #if 1 memcpy(img_addr, &ptr_received_img->memory, ptr_received_img->size); returned_size = ptr_received_img->size; if (enable_to_save_file == 1) { pthread_mutex_lock(&mutex_snap); // Protect the file I/O. char write_filename[256] = "/emmc/plugin/Aida_data/storage/"; strcat(write_filename, file_name); FILE* fp = fopen(write_filename, "w+"); if (fp != NULL) { fwrite(ptr_received_img->memory, sizeof(char), ptr_received_img->size, fp); //printf("[PNS Get Snapshot From Remote IPCam] Wrote to the file: %s.\n", write_filename); fclose(fp); } else printf("[PNS Get Snapshot From Remote IPCam] (For debugging) Error occurred when opening file I/O.\n"); pthread_mutex_unlock(&mutex_snap); // Protect the file I/O. } #endif } if (pList != NULL) curl_slist_free_all(pList); ptr_received_img->size = 0; /*if (ptr_received_img != NULL) { free(ptr_received_img); ptr_received_img = NULL; }*/ curl_easy_reset(curl_handle_for_snapshot); } else printf("[PNS Get Snapshot From Remote IPCam] CURL failed to initialize!\n"); pthread_mutex_unlock(&mutex_curl); if (check_reboot == 1) { char temp_msg[8192] = { 0 }; snprintf(temp_msg,sizeof(temp_msg), "free:%d,%s", get_free_mem_data(), "CURLE OUT OF MEMORY"); write_to_logs_html(temp_msg, "PNS Get Snapshot remote", "CGI_REBOOT", "Yes"); write_to_log_if_error(temp_msg, "PNS Get Snapshot remote", "CGI_REBOOT"); saveCounters(); pthread_t auto_reboot_thread_id; if (pthread_create(&auto_reboot_thread_id, 0, auto_reboot, NULL)) { printf("create auto reboot thread faile \n"); } } return returned_size; } #endif void initial_curl_handle_for_snapshot() { //curl_handle_for_snapshot = curl_easy_init(); // Initialization for each handler. curl_handle_for_snapshot = g_http_handle; } void clean_curl_handle_for_snapshot() { //curl_easy_cleanup(curl_handle_for_snapshot); //curl_handle_for_snapshot = NULL; curl_easy_reset(curl_handle_for_snapshot); } void *Get_canvas_From_IPCam_thread(void *ptr) { pthread_detach(pthread_self()); setPthreadName("get_canvas"); //int count_snap = 0; #ifdef GY_OS_AMBA char g_cache_canvas_addr[MAX_IMG_SIZE] = { 0 }; while (canvas_live_mode_flag) { #if 1 if ((viewChannelData[0].enable_traffic /*|| viewChannelData[0].enable_anpr*/) /*&& (strcmp(SystemSetting.enable_display_properties, "Yes") == 0 || strcmp(viewChannelData[0].enable_unknown_object, "Yes") == 0 || (strcmp(SystemSetting.cloud_enable_notification, "Yes") == 0))*/ && AI_fps >= 1) //當只有車牌時,則不支援get_g_canvas_addr,提升fps。 { /*if (g_osdSysTimeStamp < 1000000000) { usSleep(3000000); continue; }*/ set_record_process_note("get_canvas_start"); //pthread_mutex_lock(&mutex_get_canvas); pthread_mutex_lock(&mutex_get_network_input); //memset(g_canvas_addr, 0x00, MAX_IMG_SIZE); //g_cache_canvas_size = PNS_Get_Snapshot_From_IPCam("snap_buf.jpg", g_cache_canvas_addr, 0); memset(g_cache_canvas_addr,0x00,sizeof(g_cache_canvas_addr)); g_cache_canvas_size = (size_t)get_ori_c_image(g_cache_canvas_addr); //printf("\n------MAX_IMG_SIZE:%d,g_cache_canvas_size:%d\n", MAX_IMG_SIZE, g_cache_canvas_size); if (g_cache_canvas_size >= 1) { memcpy(g_canvas_addr[g_current_index_canvas_images], g_cache_canvas_addr, g_cache_canvas_size); g_canvas_size[g_current_index_canvas_images] = g_cache_canvas_size; g_current_index_canvas_images++; if (g_current_index_canvas_images >= MAX_NUM_SNAP_IMAGES) { g_current_index_canvas_images = 0; } if (g_start_to_store_canvas < MAX_NUM_SNAP_IMAGES) { g_start_to_store_canvas++; } } /* if (count_snap >= 5) { count_snap = 0; clean_curl_handle_for_snapshot(); initial_curl_handle_for_snapshot(); }*/ pthread_mutex_unlock(&mutex_get_network_input); //pthread_mutex_unlock(&mutex_get_canvas); set_record_process_note("get_canvas_end"); } #endif if (viewChannelData[0].enable_traffic || viewChannelData[0].enable_anpr) { usSleep(USLEEP_PNS_SNAP_THREAD); } else { usSleep(5000000); } //count_snap++; } #endif pthread_exit(NULL); } size_t get_g_canvas_addr(char * output_canvas_addr) { if (g_start_to_store_canvas >= MAX_NUM_SNAP_IMAGES) { #ifdef GY_OS_AMBA //pthread_mutex_lock(&mutex_get_canvas); int index_snap = g_current_index_canvas_images + 1 >= MAX_NUM_SNAP_IMAGES ? 0 : g_current_index_canvas_images + 1; if (g_canvas_size[index_snap] >= 1) { memcpy(output_canvas_addr, g_canvas_addr[index_snap], g_canvas_size[index_snap]); } //pthread_mutex_unlock(&mutex_get_canvas); return g_canvas_size[index_snap]; #else return 0; #endif } else { return 0; } } void stop_canvas_live_mode_flag() { canvas_live_mode_flag = 0; } // Based on the definitions from Vega's document "PNS2_API-1.pdf", part 2-5. int g_pns_account_OK = 0; int g_pns_access_level = -1; int g_check_noapplink = 0; int g_check_invalidpass = 0; int g_check_emailnotvalid = 0; //char g_last_cloud_account[256] = { 0 }; //char g_last_cloud_password[256] = { 0 }; struct timeval g_currtime_pns_post; long g_last_s_pns_post = 0; void set_g_pns_account_OK(int temp_g_pns_account_OK) { g_pns_account_OK = temp_g_pns_account_OK; } void set_g_pns_access_level(int temp_g_pns_access_level) { g_pns_access_level = temp_g_pns_access_level; } void set_g_check_invalidpass(int temp_g_check_invalidpass) { g_check_invalidpass = temp_g_check_invalidpass; } void set_g_check_emailnotvalid(int temp_g_check_emailnotvalid) { g_check_emailnotvalid = temp_g_check_emailnotvalid; } int get_g_check_wrong_pass_or_email() { if (g_check_emailnotvalid == 1 || g_check_invalidpass == 1) { return 1; } else { return 0; } } void PNS_Send_Multipart_POST(char* arg_json_string_for_im, char* arg_event_str, char* arg_event_id, char* arg_event_id_16, char* arg_event_desc, char* user_account, char* user_password, char* region_cloud_addr, char *returned_msg, char* image_buff, int image_buff_size, time_t rawtime) { //printf("\n---------------[PNS Send Multipart POST]:%d\n",1); gettimeofday(&g_currtime_pns_post, NULL); long current_s_pns_post = g_currtime_pns_post.tv_sec; if (g_check_noapplink == 0) { g_last_s_pns_post = current_s_pns_post; } else{ if (g_check_ping_OK == 1) { if (g_pns_access_level <= 0 && (current_s_pns_post - g_last_s_pns_post) >= 3600) { g_check_noapplink = 0; g_last_s_pns_post = current_s_pns_post; } else if (g_pns_access_level >= 1 && (current_s_pns_post - g_last_s_pns_post) >= 600) { g_check_noapplink = 0; g_last_s_pns_post = current_s_pns_post; } } } int check_reboot = 0; if (g_check_ping_OK == 1 && g_check_noapplink == 0 && g_pns_access_level >= 0) { //CURL* curl = curl_easy_init(); curl_mime* form = NULL; curl_mimepart* mimepart = NULL; struct curl_slist* headerlist = NULL; static const char buf[] = "Expect:"; char snapshot_addr[MAX_IMG_SIZE] = { 0 }; size_t snapshot_size = 0; //struct data debug_data_for_multipart; //printf("\n---------------[PNS Send Multipart POST]:%d\n", 2); //memset(g_canvas_addr, 0x00, MAX_IMG_SIZE); //printf("\n---------------[PNS Send Multipart POST]:%d\n", 21); //size_t snapshot_size = (size_t)get_ori_c_image(g_canvas_addr); //size_t snapshot_size = PNS_Get_Snapshot_From_IPCam("snap_buf.jpg", g_canvas_addr, 0); //snapshot_size = get_g_canvas_addr(snapshot_addr); if (image_buff_size >= 1) { snapshot_size = (size_t)image_buff_size; memcpy(snapshot_addr, image_buff, snapshot_size); } //printf("\n---------------[PNS Send Multipart POST]:%d-----snapshot_size%d\n", 22,(int)snapshot_size); //printf("\n---------------[PNS Send Multipart POST]:%d\n", 23); char device_name[512] = { 0 }; if (strlen(g_device_name) >= 1) { strcpy(device_name, g_device_name); } //printf("\n---------------[PNS Send Multipart POST]:%d,strlen(device_name):%d,account_OK:%d,snapshot_size:%d\n", 3, strlen(device_name), account_OK, snapshot_size); pthread_mutex_lock(&mutex_curl); /*if (g_http_handle != NULL) { curl_easy_cleanup(g_http_handle); curl_global_cleanup(); curl_global_init(CURL_GLOBAL_SSL); g_http_handle = NULL; g_http_handle = curl_easy_init(); }*/ if (strlen(device_name) >= 1 && g_pns_account_OK == 1 && g_pns_access_level >= 0 && g_http_handle && snapshot_size >= 1) { //printf("\n---------------[PNS Send Multipart POST]:%d\n", 4); CURL* curl_for_url_encode = g_http_handle; char* url_encoded_str = curl_easy_escape(curl_for_url_encode, arg_event_desc, 0); // 0: Uses strlen() on the input string to find out the size. char ddenc_event_desc[256] = { 0 }; ddEnc(url_encoded_str, ddenc_event_desc); curl_free(url_encoded_str); curl_easy_reset(curl_for_url_encode); //char *snapshot_addr = malloc(MAX_IMG_SIZE); //memset(snapshot_addr, 0x00, MAX_IMG_SIZE); //char snapshot_addr[MEMORY_SIZE] = { 0 }; //size_t snapshot_size = PNS_Get_Snapshot_From_IPCam("curl_saved_from_pns_buf.jpg", snapshot_addr, 0); //printf("[PNS Send Multipart POST] CURL handler initialization for multipart/form-data.\n"); form = curl_mime_init(g_http_handle); // Create the form // cat: Category, pns/others (Using pns) // act: Action, check/send/list/snapshot (Using snapshot for 2-5) // vend: Vendor, 0 for LILIN, 1, 2, 3, 4, 5, 6, 7, and etc. (Using 0 for 2-5) // ver: 2.1 (Default) // prd: Production ready "prd" or under development "dev". (Using prd for 2-5) // cno: Random check num. Server will add 1 in response. // dt: Device type, 0:IPCam, 1:NVR, 2:NAV (Using 0) // et: Event type (from arguments) // eid: Event ID (from arguments) // mac: MAC addr. (Done) // msg: // tp: Describe what kind of alarm motion event. For AI team, it's always 8 (always shown as "ag==" after ddEnc). // dt: Date. (Done) // tm: Time. (Done) // cu: Camera URL // cp: Camera port // vu: Video URL of the event // im: Information to interpret // ch: Which channel of the event // de: Device type // mc: Which MAC addr. detects the event // dn: Device name (URL encoded) // es: Event type string (AI event title in locale) // en: Event type ID // ds: Event description (AI event description in locale) /*const char* complete_string_for_debug = "\",\ \"msg\" : [ {\ \"tp\" : \"Yg==\",\ \"dt\" : \"ZGWSbl1hZWJjlA==\",\ \"tm\" : \"Y2ebZ2NqZmk=\",\ \"cu\" : \"mqnVpWpfYmZelWZnlGQ=\",\ \"cp\" : \"amWZZQ==\",\ \"vu\" : \"mqnVpWpfYmZelWZnlGRm3M+ZnNNfmKHP0V+TlWVq\",\ \"im\" : \"l66rZpNiia55zaedyp2N1L+Kec+KfGrgwoikzYuNZ9qLZ5qpeZmqnpOqfq7JqYCcr5+NvJGNpc7EnbTde6Rnog==\",\ \"ch\" : \"Yg==\",\ \"de\" : \"Yg==\",\ \"mc\" : \"YmWRe3ZzbG5mmWhn\",\ \"dn\" : \"V3qWWmlgWG10iH1ri3Fvi6hm\",\ \"es\" : \"\",\ \"en\" : \"\",\ \"ds\" : \"V3qVWnJxWG1yiH1oi3J5i6hr\"\ } ] }";*/ //-------------------------------------------------- // Random number 0-999 for 'cno' field. int rand_for_cno = rand() % 1000; char str_rand_for_cno[8] = { 0 }; snprintf(str_rand_for_cno, 5, "%d", rand_for_cno); char ddenc_str_rand_for_cno[8] = { 0 }; ddEnc(str_rand_for_cno, ddenc_str_rand_for_cno); const char* data1 = "{ \"cat\":\"oqPU\", \"act\":\""; const char *msg_act_snap = "paPCpaOYoqk="; const char *msg_act_text = "pZrPmQ=="; const char* data1_2 = "\", \"vend\":\"Yg==\", \"ver\":\"ZGOS\", \"prd\":\"oqfF\", \"cno\":\""; const char* data2 = "\", \"dt\":\"Yg==\", \"et\":\"a24=\", \"eid\":\""; const char* data3 = "\", \"mac\":\""; const char* msg_section1 = "\", \"msg\" : [ { \"tp\":\"ag==\", \"dt\":\""; const char* msg_section2 = "\", \"tm\":\""; const char* msg_section3 = "\", \"cu\":\"mqnVpWpfYmZelWZnlGQ=\", \"cp\":\"amWZZQ==\", \"vu\":\"mqnVpWpfYmZelWZnlGRm3M+ZnNNfmKHP0V+TlWVq\", \"im\":\""; const char* msg_section4_mac = "\", \"ch\":\"Yg==\", \"de\":\"Yg==\", \"mc\":\""; const char* msg_dev_name = "\", \"dn\":\""; const char* msg_evn_str = "\", \"es\":\""; const char* msg_evn_id = "\", \"en\":\""; const char* msg_evn_id_16 = "\", \"ti\":\""; const char* msg_evn_desc = "\", \"ds\":\""; const char* msg_end = "\" } ] }"; //-------------------------------------------------- // System local time of the date and the time strings. char str_date[16] = { 0 }; char str_time[16] = { 0 }; //time_t rawtime; //time(&rawtime); struct tm* timeinfo = localtime(&rawtime); strftime(str_date, sizeof(str_date) - 1, "%Y-%m-%d", timeinfo); strftime(str_time, sizeof(str_time) - 1, "%H:%M:%S", timeinfo); char ddenc_str_date[32] = { 0 }; char ddenc_str_time[32] = { 0 }; ddEnc(str_date, ddenc_str_date); ddEnc(str_time, ddenc_str_time); //-------------------------------------------------- // MAC address char mac_addr[32] = { 0 }; char mac_addr_temp[32] = { 0 }; //printf("\n---------------[PNS Send Multipart POST]:%d\n", 3); memset(mac_addr_temp, 0x00, sizeof(mac_addr_temp)); if (strlen(g_mac_address) >= 1) { strcpy(mac_addr_temp, g_mac_address); } else { GetMACAddress(mac_addr_temp); // Can use another function "GetMACAddress_WithoutDash" to reduce codes but need to verify after changing. } //printf("\n---------------[PNS Send Multipart POST]:%d\n", 31); int index_mac_addr = 0; for (int i = 0; i < (int)strlen(mac_addr_temp); i++) { if (mac_addr_temp[i] == '-' || mac_addr_temp[i] == '\n') { } else { mac_addr[index_mac_addr] = mac_addr_temp[i]; index_mac_addr++; } } mac_addr[index_mac_addr] = '\0'; //printf("\n---------------[PNS Send Multipart POST]:%d\n", 32); char ddenc_mac[32] = { 0 }; ddEnc(mac_addr, ddenc_mac); //printf("\n---------------[PNS Send Multipart POST]:%d\n", 4); //-------------------------------------------------- // Device name (Needed in procedure 2-5. Enable URL-encoded) char ddenc_device_name[512] = { 0 }; ddEnc(device_name, ddenc_device_name); //-------------------------------------------------- // Prepare basic strings for "im" field const char* im_msg1 = "{ \"uid\":\""; const char* im_msg2 = "\", \"pid\":\""; const char* im_msg3 = "\", \"desc\":\""; const char* im_msg4 = "\" }"; ////////////////////// char Authorization_user_account[512] = { 0 }; char Authorization_user_password[512] = { 0 }; //size_t base64_encode_length_user_account = 0; //size_t base64_encode_length_user_password = 0; //char httpAuth_user_account[1280] = { 0 }; //char httpAuth_user_password[1280] = { 0 }; char ddenc_user_account[1280] = { 0 }; char ddenc_user_password[1280] = { 0 }; if (strlen(user_account) > 0 && g_pns_access_level >= 1) { char buf_account[512] = { 0 }; urldecode((unsigned char *)user_account, (unsigned char *)buf_account); strcpy(Authorization_user_account, buf_account); if (strlen(Authorization_user_account) >= 1) { //base64_encode((const unsigned char*)Authorization_user_account, strlen(Authorization_user_account), &base64_encode_length_user_account, httpAuth_user_account); //httpAuth_user_account[base64_encode_length_user_account] = '\0'; //if(base64_encode_length_user_account >= 1) //ddEnc(httpAuth_user_account, ddenc_user_account); ddEnc(Authorization_user_account, ddenc_user_account); } } if (strlen(user_password) > 0 && g_pns_access_level >= 1) { char buf_account2[512] = { 0 }; urldecode((unsigned char *)user_password, (unsigned char *)buf_account2); strcat(Authorization_user_password, buf_account2); if (strlen(Authorization_user_password) >= 1) { //base64_encode((const unsigned char*)Authorization_user_password, strlen(Authorization_user_password), &base64_encode_length_user_password, httpAuth_user_password); //httpAuth_user_password[base64_encode_length_user_password] = '\0'; //if(base64_encode_length_user_password >= 1) //ddEnc(httpAuth_user_password, ddenc_user_password); ddEnc(Authorization_user_password, ddenc_user_password); } } int len_arg_json_string_for_im = strlen(arg_json_string_for_im); //-------------------------------------------------- // Prepare base64 encoded desc in "im" field size_t len_desc_by_base64 = 0; char str_temp[8 * 8192] = { 0 }; base64_encode((const unsigned char*)arg_json_string_for_im, strlen(arg_json_string_for_im), &len_desc_by_base64, str_temp); char str_desc_by_base64[8 * 8192] = { 0 }; strncpy(str_desc_by_base64, str_temp, len_desc_by_base64); str_desc_by_base64[len_desc_by_base64] = '\0'; char ddenc_str_desc_by_base64[8 * 8192] = { 0 }; ddEnc(str_desc_by_base64, ddenc_str_desc_by_base64); //printf("\n---------------[PNS Send Multipart POST]:%d\n", 5); //-------------------------------------------------- // Concatenate the whole im string. size_t im_len_total = strlen(im_msg1) + strlen(ddenc_user_account) + strlen(im_msg2) + strlen(ddenc_user_password) + strlen(im_msg3) + strlen(ddenc_str_desc_by_base64) + strlen(im_msg4); char im_string[im_len_total + 1]; strcpy(im_string, im_msg1); if(strlen(ddenc_user_account) >= 1) strcat(im_string, ddenc_user_account); strcat(im_string, im_msg2); if (strlen(ddenc_user_password) >= 1) strcat(im_string, ddenc_user_password); strcat(im_string, im_msg3); strcat(im_string, ddenc_str_desc_by_base64); strcat(im_string, im_msg4); im_string[im_len_total] = '\0'; //printf("[PNS Send Multipart POST] im_string = %s\n", im_string); //-------------------------------------------------- // Encode the whole im string by base64 again. size_t len_im_by_base64 = 0; char str_temp_2[8 * 8192] = { 0 }; base64_encode((const unsigned char*)im_string, strlen(im_string), &len_im_by_base64, str_temp_2); char str_im_by_base64[8 * 8192] = { 0 }; strncpy(str_im_by_base64, str_temp_2, len_im_by_base64); str_im_by_base64[len_im_by_base64] = '\0'; //printf("\n---------------[PNS Send Multipart POST]:%d\n", 6); //-------------------------------------------------- // ddEncode on im string char ddenc_json_str_for_im[8 * 8192] = { 0 }; ddEnc(str_im_by_base64, ddenc_json_str_for_im); //-------------------------------------------------- // ddEncode on Event string, event ID from the arguments char ddenc_event_str[50] = { 0 }; ddEnc(arg_event_str, ddenc_event_str); char ddenc_event_id[30] = { 0 }; ddEnc(arg_event_id, ddenc_event_id); char ddenc_event_id_16[50] = { 0 }; ddEnc(arg_event_id_16, ddenc_event_id_16); //-------------------------------------------------- // URL-encoded and then ddEncode for event description from the arguments //printf("[PNS Send Multipart POST] Event desc: %s", url_encoded_str); int len_ddenc_json_str_for_im = strlen(ddenc_json_str_for_im); //printf("[PNS Send Multipart POST] URL encoded event desc: %s", ddenc_event_desc); //printf("\n---------------[PNS Send Multipart POST]:%d\n", 7); //-------------------------------------------------- // Calculate the total length of the data //size_t total_len = strlen(data1) + strlen(data1_2) + strlen(ddenc_str_rand_for_cno) + strlen(data2) + strlen(ddenc_event_id_16) + strlen(data3) + strlen(ddenc_mac) //+ strlen(msg_section1) + strlen(ddenc_str_date) + strlen(msg_section2) + strlen(ddenc_str_time) + strlen(msg_section3) + strlen(ddenc_json_str_for_im) //+ strlen(msg_section4_mac) + strlen(ddenc_mac) + strlen(msg_dev_name) + strlen(ddenc_device_name) //+ strlen(msg_evn_str) + strlen(ddenc_event_str) + strlen(msg_evn_id) + strlen(ddenc_event_id) + strlen(msg_evn_id_16) + strlen(ddenc_event_id_16) + strlen(msg_evn_desc) + strlen(ddenc_event_desc) + strlen(msg_end); //-------------------------------------------------- // Concatenate total length of the data //if (g_pns_access_level >= 1 && strcmp(SystemSetting.cloud_enable_snap, "Yes") == 0) { //total_len += strlen(msg_act_snap); //} //else { //total_len += strlen(msg_act_text); //} char send_data[8 * 8192] = { 0 }; strcpy(send_data, data1); if (g_pns_access_level >= 1 && strcmp(SystemSetting.cloud_enable_snap, "Yes") == 0) { strcat(send_data, msg_act_snap); } else { strcat(send_data, msg_act_text); } strcat(send_data, data1_2); strcat(send_data, ddenc_str_rand_for_cno); strcat(send_data, data2); strcat(send_data, ddenc_event_id_16); strcat(send_data, data3); strcat(send_data, ddenc_mac); strcat(send_data, msg_section1); strcat(send_data, ddenc_str_date); strcat(send_data, msg_section2); strcat(send_data, ddenc_str_time); strcat(send_data, msg_section3); strcat(send_data, ddenc_json_str_for_im); strcat(send_data, msg_section4_mac); strcat(send_data, ddenc_mac); strcat(send_data, msg_dev_name); strcat(send_data, ddenc_device_name); strcat(send_data, msg_evn_str); strcat(send_data, ddenc_event_str); strcat(send_data, msg_evn_id); strcat(send_data, ddenc_event_id); strcat(send_data, msg_evn_id_16); strcat(send_data, ddenc_event_id_16); strcat(send_data, msg_evn_desc); strcat(send_data, ddenc_event_desc); strcat(send_data, msg_end); //send_data[total_len] = '\0'; //printf("[PNS Send Multipart POST] JSON string for multipart section 1: %s\n", send_data); //printf("\n---------------------------send_data:total_len:%d\n", (int)total_len); //printf("\n---------------------------send_data:snapshot_size:%d\n", (int)snapshot_size); // Fill in the submit field mimepart = curl_mime_addpart(form); curl_mime_name(mimepart, "data"); curl_mime_type(mimepart, "application/json"); curl_mime_data(mimepart, send_data, CURL_ZERO_TERMINATED); //printf("[PNS Send Multipart POST] g_canvas_addr = %p\n", g_canvas_addr); //printf("[PNS Send Multipart POST] snapshot_size = %lu (bytes retrieved)\n", snapshot_size); //printf("\n---------------[PNS Send Multipart POST]:%d\n", 8); if (g_pns_access_level >= 1 && snapshot_size >= 1 && strcmp(SystemSetting.cloud_enable_snap, "Yes") == 0) { // Fill in the filename field mimepart = curl_mime_addpart(form); curl_mime_type(mimepart, "image/jpg"); curl_mime_data(mimepart, snapshot_addr, snapshot_size); curl_mime_filename(mimepart, "cayman_test.jpg"); curl_mime_name(mimepart, "file1"); } // initialize custom header list (stating that Expect: 100-continue is not wanted //headerlist = curl_slist_append(headers, "Accept: application/json"); headerlist = curl_slist_append(headerlist, "Accept: application/json"); headerlist = curl_slist_append(headerlist, "Content-Type: multipart/form-data"); headerlist = curl_slist_append(headerlist, "Connection: close"); //char* content_length_str = calloc(content_length + 17, 1); //sprintf(content_length_str, "Content-Length: %ld", content_length); //headerlist = curl_slist_append(headerlist, content_length_str); //headerlist = curl_slist_append(headerlist, "charset: utf-8"); headerlist = curl_slist_append(headerlist, buf); //CURLcode result = 0; // what URL that receives this POST curl_easy_setopt(g_http_handle, CURLOPT_URL, region_cloud_addr); ///printf("[PNS Send Multipart POST] CURLOPT_URL: %s\n", curl_easy_strerror(result)); //result = curl_easy_setopt(g_http_handle, CURLOPT_SSL_VERIFYPEER, 1L); //printf("[PNS Send Multipart POST] CURLOPT_SSL_VERIFYPEER: %s\n", curl_easy_strerror(result)); //result = curl_easy_setopt(g_http_handle, CURLOPT_SSL_VERIFYHOST, 2L); //printf("[PNS Send Multipart POST] CURLOPT_SSL_VERIFYHOST: %s\n", curl_easy_strerror(result)); curl_easy_setopt(g_http_handle, CURLOPT_CONNECTTIMEOUT, 5); // 設定連線超時,單位秒 curl_easy_setopt(g_http_handle, CURLOPT_TIMEOUT, 10); //curl_easy_setopt(g_http_handle, CURLOPT_ACCEPT_ENCODING, "gzip"); curl_easy_setopt(g_http_handle, CURLOPT_NOSIGNAL, 1); //result = curl_easy_setopt(g_http_handle, CURLOPT_CAINFO, "/emmc/plugin/Aida_data/cacert.pem"); curl_easy_setopt(g_http_handle, CURLOPT_SSL_VERIFYPEER, 0); //證書取消 curl_easy_setopt(g_http_handle, CURLOPT_SSL_VERIFYHOST, 0); //證書取消 curl_easy_setopt(g_http_handle, CURLOPT_USERAGENT, "IPVideo"); //printf("[PNS Send Multipart POST] CURLOPT_CAINFO: %s\n", curl_easy_strerror(result)); //result = curl_easy_setopt(g_http_handle, CURLOPT_DEBUGFUNCTION, my_trace); //printf("[PNS Send Multipart POST] CURLOPT_DEBUGFUNCTION: %s\n", curl_easy_strerror(result)); //result = curl_easy_setopt(g_http_handle, CURLOPT_DEBUGDATA, &debug_data_for_multipart); //printf("[PNS Send Multipart POST] CURLOPT_DEBUGDATA: %s\n", curl_easy_strerror(result)); curl_easy_setopt(g_http_handle, CURLOPT_VERBOSE, 0L); //printf("[PNS Send Multipart POST] CURLOPT_VERBOSE: %s\n", curl_easy_strerror(result)); //result = curl_easy_setopt(g_http_handle, CURLOPT_POST, 1L); // Switch on full protocol/debug output //printf("[PNS Send Multipart POST] CURLOPT_POST: %s\n", curl_easy_strerror(result)); curl_easy_setopt(g_http_handle, CURLOPT_LOW_SPEED_LIMIT, 1L); curl_easy_setopt(g_http_handle, CURLOPT_LOW_SPEED_TIME, 5L); //printf("\n---------------[PNS Send Multipart POST]:%d\n", 9); struct MemoryStruct memory_temp_msg; struct MemoryStruct* ptr_responded_msg = &memory_temp_msg; //ptr_responded_msg->memory = NULL; ptr_responded_msg->size = 0; curl_easy_setopt(g_http_handle, CURLOPT_WRITEFUNCTION, WriteResponseString); //printf("[PNS Send Multipart POST] CURLOPT_WRITEFUNCTION: %s\n", curl_easy_strerror(result)); curl_easy_setopt(g_http_handle, CURLOPT_WRITEDATA, ptr_responded_msg); //printf("[PNS Send Multipart POST] CURLOPT_WRITEDATA: %s\n", curl_easy_strerror(result)); curl_easy_setopt(g_http_handle, CURLOPT_TCP_FASTOPEN, 1L); curl_easy_setopt(g_http_handle, CURLOPT_HTTPHEADER, headerlist); //printf("[PNS Send Multipart POST] CURLOPT_HTTPHEADER: %s\n", curl_easy_strerror(result)); curl_easy_setopt(g_http_handle, CURLOPT_MIMEPOST, form); //printf("[PNS Send Multipart POST] CURLOPT_MIMEPOST: %s\n", curl_easy_strerror(result)); // Perform the request, res will get the return code curl_easy_setopt(g_http_handle, CURLOPT_FORBID_REUSE, 0L); //Set to 0 to have libcurl keep the connection open for possible later re-use (default behavior). //printf("\n---------------[PNS Send Multipart POST]:%d\n", 10); CURLcode res = curl_easy_perform(g_http_handle); check_if_correct_post = 1; // Check for errors if (res != CURLE_OK) { check_if_run_post = 0; //curl_easy_cleanup(g_http_handle); // always cleanup //curl_mime_free(mimepart); // then cleanup the form curl_mime_free(form); // then cleanup the form char* err_msg = (char*)curl_easy_strerror(res); #if 1 if (res == CURLE_OUT_OF_MEMORY) { char c_access_level[50] = { 0 }; if (g_pns_access_level == 0) { sprintf(c_access_level, "free:%d,access: %d (only text, not support JPEG),len_desc:%d,len_ddenc:%d,", get_free_mem_data(), g_pns_access_level, len_arg_json_string_for_im, len_ddenc_json_str_for_im); } else { sprintf(c_access_level, "free:%d,access: %d,len_desc:%d,len_ddenc:%d,", get_free_mem_data(), g_pns_access_level, len_arg_json_string_for_im, len_ddenc_json_str_for_im); } strcpy(returned_msg, c_access_level); strcat(returned_msg, "CURLE_OUT_OF_MEMORY may sometimes indicate a conversion error \n instead of a memory allocation error if CURL_DOES_CONVERSIONS \n is defined"); printf("[PNS Send Multipart POST]CURLE_OUT_OF_MEMORY may sometimes indicate a conversion error \n instead of a memory allocation error if CURL_DOES_CONVERSIONS \n is defined\n"); write_to_logs_html(returned_msg, "curl to post pns", "CGI_REBOOT", "Yes"); write_to_log_if_error(returned_msg, "curl to post pns", "CGI_REBOOT"); check_reboot = 1; } else { char c_access_level[50] = { 0 }; if (g_pns_access_level == 0) { sprintf(c_access_level, "free:%d,access: %d (only text, not support JPEG),len_desc:%d,len_ddenc:%d,", get_free_mem_data(), g_pns_access_level, len_arg_json_string_for_im, len_ddenc_json_str_for_im); } else { sprintf(c_access_level, "free:%d,access: %d,len_desc:%d,len_ddenc:%d,", get_free_mem_data(), g_pns_access_level, len_arg_json_string_for_im, len_ddenc_json_str_for_im); } strcpy(returned_msg, c_access_level); strcat(returned_msg, err_msg); write_to_log_if_error_post_pns(returned_msg); write_to_logs_html(returned_msg, "curl to post pns", "ERROR", SystemSetting.enable_system_logs); } #endif fprintf(stderr, "[PNS Send Multipart POST] curl_easy_perform() failed: %s\n", returned_msg); } else { check_if_run_post = 1; //printf("\n---------------[PNS Send Multipart POST]:%d\n", 11); //curl_easy_cleanup(g_http_handle); // always cleanup //curl_mime_free(mimepart); // then cleanup the form curl_mime_free(form); // then cleanup the form //printf("[PNS Send Multipart POST] Multipart/form-data command finished.\n"); char *loc = strstr(ptr_responded_msg->memory, "msg\":\""); char *loc_end = NULL; if (loc != NULL) { loc_end = strstr(loc, "\",\""); if (loc_end == NULL) { loc_end = strstr(loc, "\"}"); } } //printf("\n--------ptr_responded_msg->memory:%s\n", ptr_responded_msg->memory); char temp_memory[8 * 8192] = { 0 }; char temp_msg[512] = { 0 }; int index_temp_memory = 0; if (loc != NULL && loc_end != NULL) { for (int index_msg = 6; index_msg < strlen(loc) - strlen(loc_end); index_msg++) { temp_memory[index_temp_memory] = loc[index_msg]; index_temp_memory++; } temp_memory[index_temp_memory] = '\0'; ddDec(temp_memory, temp_msg); char c_access_level[50] = { 0 }; if (g_pns_access_level == 0) { sprintf(c_access_level, "free:%d,access: %d (only text, not support JPEG),len_desc:%d,len_ddenc:%d,", get_free_mem_data(), g_pns_access_level, len_arg_json_string_for_im, len_ddenc_json_str_for_im); } else { sprintf(c_access_level, "free:%d,access: %d,len_desc:%d,len_ddenc:%d,", get_free_mem_data(), g_pns_access_level, len_arg_json_string_for_im, len_ddenc_json_str_for_im); } strcpy(returned_msg, "Regular service: "); strcat(returned_msg, c_access_level); strcat(returned_msg, temp_msg); write_to_logs_html(returned_msg, "curl to post pns", "INFO", SystemSetting.enable_system_logs); if (strcmp(temp_msg,"noAppLink")==0 || strstr(temp_msg, "\"msg\":\"oKSipaB8nKOb\"") != NULL) { g_check_noapplink = 1; } if (strcmp(temp_msg, "invalidPass") == 0 || strstr(temp_msg, "\"msg\":\"m6PXlpyZl4WR1qs=\"") != NULL) { g_check_invalidpass = 1; g_pns_access_level = 0; } if (strcmp(temp_msg, "emailNotValid") == 0 || strstr(temp_msg, "\"msg\":\"l6LCnpx+oqmGxKSdyg==\"") != NULL) { g_check_emailnotvalid = 1; g_pns_access_level = 0; } } else { char c_access_level[50] = { 0 }; if (g_pns_access_level == 0) { sprintf(c_access_level, "free:%d,access: %d (only text, not support JPEG),len_desc:%d,len_ddenc:%d,", get_free_mem_data(), g_pns_access_level, len_arg_json_string_for_im, len_ddenc_json_str_for_im); } else { sprintf(c_access_level, "free:%d,access: %d,len_desc:%d,len_ddenc:%d,", get_free_mem_data(), g_pns_access_level, len_arg_json_string_for_im, len_ddenc_json_str_for_im); } strcpy(returned_msg, "Regular service: "); strcat(returned_msg, c_access_level); strcat(returned_msg, ptr_responded_msg->memory); write_to_logs_html(returned_msg, "curl to post pns", "ERROR", SystemSetting.enable_system_logs); if (strstr(ptr_responded_msg->memory, "\"msg\":\"oKSipaB8nKOb\"") != NULL) { g_check_noapplink = 1; } if (strstr(ptr_responded_msg->memory, "\"msg\":\"m6PXlpyZl4WR1qs=\"") != NULL) { g_check_invalidpass = 1; g_pns_access_level = 0; } if (strstr(ptr_responded_msg->memory, "\"msg\":\"l6LCnpx+oqmGxKSdyg==\"") != NULL) { g_check_emailnotvalid = 1; g_pns_access_level = 0; } } //printf("\n---------------[PNS Send Multipart POST]:%d\n", 12); } snapshot_size = 0; //printf("\n---------------[PNS Send Multipart POST]:%d\n", 13); curl_slist_free_all(headerlist); // free slist ptr_responded_msg->size = 0; /*if (ptr_responded_msg != NULL) { free(ptr_responded_msg); ptr_responded_msg = NULL; }*/ //printf("\n---------------[PNS Send Multipart POST]:%d\n", 14); curl_easy_reset(g_http_handle); } else { //printf("\n---------------[PNS Send Multipart POST]:%d\n", 5); if (strlen(device_name) == 0) { char c_access_level[50] = { 0 }; if (g_pns_access_level == 0) { sprintf(c_access_level, "free:%d,", get_free_mem_data()); } else { sprintf(c_access_level, "free:%d,", get_free_mem_data()); } strcpy(returned_msg, c_access_level); strcat(returned_msg, "Device name empty."); printf("[PNS Send Multipart POST] Device name empty.\n"); write_to_logs_html(returned_msg, "curl to post pns", "ERROR", SystemSetting.enable_system_logs); } else if (snapshot_size == 0) { char c_access_level[50] = { 0 }; if (g_pns_access_level == 0) { sprintf(c_access_level, "free:%d,", get_free_mem_data()); } else { sprintf(c_access_level, "free:%d,", get_free_mem_data()); } strcpy(returned_msg, c_access_level); strcat(returned_msg, "Image empty."); printf("[PNS Send Multipart POST] Image empty.\n"); write_to_logs_html(returned_msg, "curl to post pns", "ERROR", SystemSetting.enable_system_logs); } else if (g_http_handle == NULL) { char c_access_level[50] = { 0 }; if (g_pns_access_level == 0) { sprintf(c_access_level, "free:%d,", get_free_mem_data()); } else { sprintf(c_access_level, "free:%d,", get_free_mem_data()); } strcpy(returned_msg, c_access_level); strcat(returned_msg, "CURL initialization failed."); printf("[PNS Send Multipart POST] CURL initialization failed.\n"); write_to_logs_html(returned_msg, "curl to post pns", "ERROR", SystemSetting.enable_system_logs); } else if (g_pns_access_level == -2) { char c_access_level[50] = { 0 }; if (g_pns_access_level == 0) { sprintf(c_access_level, "free:%d,", get_free_mem_data()); } else { sprintf(c_access_level, "free:%d,", get_free_mem_data()); } strcpy(returned_msg, c_access_level); strcat(returned_msg, "Wrong account."); printf("[PNS Send Multipart POST] Wrong account.\n"); write_to_logs_html(returned_msg, "PNS check", "ERROR", SystemSetting.enable_system_logs); } else { char c_access_level[50] = { 0 }; if (g_pns_access_level == 0) { sprintf(c_access_level, "free:%d,", get_free_mem_data()); } else { sprintf(c_access_level, "free:%d,", get_free_mem_data()); } //printf("\n---------------[PNS Send Multipart POST]:%d\n", 15); strcpy(returned_msg, c_access_level); //strcat(returned_msg, returned_msg_temp); printf("[PNS Send Multipart POST] pns check error.\n"); write_to_logs_html(returned_msg, "PNS check", "ERROR", SystemSetting.enable_system_logs); } } if (strstr(returned_msg, "Timeout was reached") != NULL || strstr(returned_msg, "timeout was reached") != NULL || strstr(returned_msg, "Couldn't resolve host name") != NULL || strstr(returned_msg, "couldn't resolve host name") != NULL) { g_check_ping_OK = 0; } pthread_mutex_unlock(&mutex_curl); } /* if (snapshot_addr != NULL) { free(snapshot_addr); snapshot_addr = NULL; }*/ if (check_reboot == 1) { saveCounters(); pthread_t auto_reboot_thread_id; if (pthread_create(&auto_reboot_thread_id, 0, auto_reboot, NULL)) { printf("create auto reboot thread faile \n"); } } //printf("\n---------------[PNS Send Multipart POST]:%d\n", 6); //curl_easy_cleanup(curl); // always cleanup } int DDNS_Communication(int operation, char* arg_email_addr, char* arg_password, char* returned_msg) { pthread_mutex_lock(&mutex_curl); CURL* curl_handler = g_http_handle; // Initialization for each handler. CURLcode result = 0; // For starting PNS and connections to the cloud. //-------------------------------------------------- // CURL section struct curl_slist* headers = NULL; headers = curl_slist_append(headers, "Accept: application/json"); headers = curl_slist_append(headers, "Content-Type: application/json"); headers = curl_slist_append(headers, "charset: utf-8"); headers = curl_slist_append(headers, "Connection: close"); curl_easy_setopt(curl_handler, CURLOPT_HTTPHEADER, headers); // CURL error handling //char error_string[CURL_ERROR_SIZE]; //result = curl_easy_setopt(curl_handler, CURLOPT_ERRORBUFFER, error_string); //printf("[DDNS Communication] CURLOPT_ERRORBUFFER: %s\n", curl_easy_strerror(result)); result = curl_easy_setopt(curl_handler, CURLOPT_POST, 1L); // Switch on full protocol/debug output //printf("[DDNS Communication] CURLOPT_POST: %s\n", curl_easy_strerror(result)); // Specify URL to send POST data result = curl_easy_setopt(curl_handler, CURLOPT_URL, Global_URL_for_DDNS); //printf("[DDNS Communication] CURLOPT_URL: %s\n", curl_easy_strerror(result)); //result = curl_easy_setopt(curl_handler, CURLOPT_USE_SSL, CURLUSESSL_ALL); //printf("[PNS] CURLOPT_USE_SSL: %s\n", curl_easy_strerror(result)); //result = curl_easy_setopt(curl_handler, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); //printf("[PNS] CURLOPT_SSLVERSION: %s\n", curl_easy_strerror(result)); //result = curl_easy_setopt(curl_handler, CURLOPT_SSL_VERIFYPEER, 1L); //printf("[DDNS Communication] CURLOPT_SSL_VERIFYPEER: %s\n", curl_easy_strerror(result)); //result = curl_easy_setopt(curl_handler, CURLOPT_SSL_VERIFYHOST, 2L); //printf("[DDNS Communication] CURLOPT_SSL_VERIFYHOST: %s\n", curl_easy_strerror(result)); //result = curl_easy_setopt(curl, CURLOPT_CAINFO, "/emmc/plugin/Aida_data/cacert.pem"); curl_easy_setopt(curl_handler, CURLOPT_SSL_VERIFYPEER, 0); //證書取消 curl_easy_setopt(curl_handler, CURLOPT_SSL_VERIFYHOST, 0); //證書取消 //printf("[DDNS Communication] CURLOPT_CAINFO: %s\n", curl_easy_strerror(result)); struct MemoryStruct memory_temp_msg; struct MemoryStruct* ptr_responded_msg = &memory_temp_msg; //ptr_responded_msg->memory = NULL; ptr_responded_msg->size = 0; result = curl_easy_setopt(curl_handler, CURLOPT_WRITEFUNCTION, WriteResponseString); //printf("[DDNS Communication] CURLOPT_WRITEFUNCTION: %s\n", curl_easy_strerror(result)); result = curl_easy_setopt(curl_handler, CURLOPT_WRITEDATA, ptr_responded_msg); //printf("[DDNS Communication] CURLOPT_WRITEDATA: %s\n", curl_easy_strerror(result)); //-------------------------------------------------- // Assemble the string for sending const char* json_check = "{ \"act\":\"lZ3GmJs=\", \"email\":\""; // ddEnc("check") = "lZ3GmJs=" const char* json_regis = "{ \"act\":\"pJrInqOk\", \"email\":\""; // ddEnc("regist") = "pJrInqOk" const char* json_login = "{ \"act\":\"nqTInp4=\", \"email\":\""; // ddEnc("login") = "nqTInp4=" const char* json_pass = "\", \"pass\":\""; const char* json_end = "\" }"; // Prepare the command field char json_cmd[MEMORY_SIZE] = {0}; if (operation == 0) strcpy(json_cmd, json_check); else if (operation == 1) strcpy(json_cmd, json_regis); else strcpy(json_cmd, json_login); // Prepare the email field char ddenc_email[50] = { 0 }; ddEnc(arg_email_addr, ddenc_email); // Prepare the password field char ddenc_password[50] = { 0 }; ddEnc(arg_password, ddenc_password); // Calculate the total length of the JSON string //size_t total_len = strlen(json_cmd) + strlen(ddenc_email) + strlen(json_pass) + strlen(ddenc_password) + strlen(json_end); // Construct the JSON string char send_data[8 * 8192] = { 0 }; strcpy(send_data, json_cmd); strcat(send_data, ddenc_email); strcat(send_data, json_pass); strcat(send_data, ddenc_password); strcat(send_data, json_end); //send_data[total_len] = '\0'; //printf("[DDNS Communication] JSON string for this DDNS operation: %s\n", send_data); //-------------------------------------------------- result = curl_easy_setopt(curl_handler, CURLOPT_POSTFIELDSIZE, strlen(send_data)); //printf("[DDNS Communication] CURLOPT_POSTFIELDSIZE: %s\n", curl_easy_strerror(result)); result = curl_easy_setopt(curl_handler, CURLOPT_POSTFIELDS, send_data); //printf("[DDNS Communication] CURLOPT_POSTFIELDS: %s\n", curl_easy_strerror(result)); //result = curl_easy_setopt(curl_handler, CURLOPT_DEBUGFUNCTION, my_trace); //printf("[PNS] CURLOPT_DEBUGFUNCTION: %s\n", curl_easy_strerror(result)); //result = curl_easy_setopt(curl_handler, CURLOPT_DEBUGDATA, &debug_data_for_query); //printf("[PNS] CURLOPT_DEBUGDATA: %s\n", curl_easy_strerror(result)); result = curl_easy_setopt(curl_handler, CURLOPT_VERBOSE, 1L); // Switch on full protocol/debug output //printf("[DDNS Communication] CURLOPT_VERBOSE: %s\n", curl_easy_strerror(result)); curl_easy_setopt(curl_handler, CURLOPT_TCP_FASTOPEN, 1L); curl_easy_setopt(curl_handler, CURLOPT_FORBID_REUSE, 0L); //Set to 0 to have libcurl keep the connection open for possible later re-use (default behavior). result = curl_easy_perform(curl_handler); int ret = 0; if (result != CURLE_OK) { char* err_msg = (char*)curl_easy_strerror(result); strcpy(returned_msg, err_msg); printf("[DDNS Communication] CURL perform failed: %s\n", err_msg); //printf("[DDNS Communication] Reason: %s\n", error_string); curl_slist_free_all(headers); curl_easy_reset(curl_handler); ptr_responded_msg->size = 0; /*if (ptr_responded_msg != NULL) { free(ptr_responded_msg); ptr_responded_msg = NULL; }*/ ret = 0; } else { char *loc = strstr(ptr_responded_msg->memory, "msg\":\""); char *loc_end = strstr(ptr_responded_msg->memory, "\"}"); char temp_memory[8 * 8192] = { 0 }; char temp_msg[512] = { 0 }; int index_temp_memory = 0; if (loc != NULL && loc_end != NULL) { for (int index_msg = 6; index_msg < strlen(loc) - strlen(loc_end); index_msg++) { temp_memory[index_temp_memory] = loc[index_msg]; index_temp_memory++; } temp_memory[index_temp_memory] = '\0'; ddDec(temp_memory, temp_msg); strcat(returned_msg, temp_msg); } else { strcpy(returned_msg, ""); } //strcpy(returned_msg, ptr_responded_msg->memory); //printf("[DDNS Communication] curl_easy_perform succeeded.\n"); curl_slist_free_all(headers); curl_easy_reset(curl_handler); ptr_responded_msg->size = 0; /*if (ptr_responded_msg != NULL) { free(ptr_responded_msg); ptr_responded_msg = NULL; }*/ ret = 1; } pthread_mutex_unlock(&mutex_curl); return ret; } void PNS_check_if_to_post_image(char* arg_email_addr, char* arg_password,char* returned_msg,char *region_cloud_addr) { pthread_mutex_lock(&mutex_curl); /*if (g_http_handle != NULL) { curl_easy_cleanup(g_http_handle); curl_global_cleanup(); curl_global_init(CURL_GLOBAL_SSL); g_http_handle = NULL; g_http_handle = curl_easy_init(); }*/ //CURL* curl_handle_for_pns; curl_mime* form = NULL; curl_mimepart* mimepart = NULL; struct curl_slist* headerlist = NULL; static const char buf[] = "Expect:"; if (g_http_handle != NULL) { form = curl_mime_init(g_http_handle); // Create the form //curl_handle_for_pns = curl_easy_init(); // Initialization for each handler. const char* data0 = "{ \"cat\": \"oqPU\", \"act\" : \"lZ3GmJs=\", \"prd\" : \"\", \"cno\" : \""; const char* data1 = "\", \"dt\" : \"Yg==\", \"et\" : \"\", \"eid\" : \""; const char* data1_2 = "\", \"mac\" : \""; const char* data2 = "\"}"; //-------------------------------------------------- // Random number 0-9999 for 'cno' field. int rand_for_cno = rand() % 10000; char str_rand_for_cno[256] = { 0 }; sprintf(str_rand_for_cno, "%d", rand_for_cno); char ddenc_str_rand_for_cno[256] = { 0 }; ddEnc(str_rand_for_cno, ddenc_str_rand_for_cno); //printf("\n----str_rand_for_cno:%s\n", str_rand_for_cno); //printf("\n----ddenc_str_rand_for_cno #1:%s\n", ddenc_str_rand_for_cno); //-------------------------------------------------- // MAC address char mac_addr[256] = { 0 }; char mac_addr_temp[256] = { 0 }; if (strlen(g_mac_address) >= 1) { strcpy(mac_addr_temp, g_mac_address); } else { GetMACAddress(mac_addr_temp); // Can use another function "GetMACAddress_WithoutDash" to reduce codes but need to verify after changing. } //printf("\n----ddenc_str_rand_for_cno #2:%s\n", ddenc_str_rand_for_cno); //printf("\n------(int)strlen(mac_addr_temp):%d\n", (int)strlen(mac_addr_temp)); int index_mac_addr = 0; for (int i = 0; i < (int)strlen(mac_addr_temp); i++) { if (mac_addr_temp[i] == '-' || mac_addr_temp[i] == '\n') { } else { mac_addr[index_mac_addr] = mac_addr_temp[i]; index_mac_addr++; } } //mac_addr[index_mac_addr] = '\0'; //printf("\n----ddenc_str_rand_for_cno #3:%s\n", ddenc_str_rand_for_cno); char ddenc_mac[256] = { 0 }; ddEnc(mac_addr, ddenc_mac); //printf("\n----ddenc_str_rand_for_cno #4:%s\n", ddenc_str_rand_for_cno); char ddenc_email[256] = { 0 }; ddEnc(arg_email_addr, ddenc_email); //printf("\n----ddenc_str_rand_for_cno #5:%s\n", ddenc_str_rand_for_cno); //-------------------------------------------------- // Calculate the total length of the data //size_t total_len = strlen(data0) + strlen(ddenc_str_rand_for_cno) + strlen(data1) + strlen(ddenc_mac) + strlen(data2) + strlen(ddenc_device_name) + strlen(data3); //size_t total_len = strlen(data0) + strlen(ddenc_str_rand_for_cno) + strlen(data1) + strlen(ddenc_email) + strlen(data1_2) + strlen(ddenc_mac) + strlen(data2); //-------------------------------------------------- // Assemble the string for sending char send_data[8192*8] = { 0 }; strcpy(send_data, data0); strcat(send_data, ddenc_str_rand_for_cno); strcat(send_data, data1); strcat(send_data, ddenc_email); strcat(send_data, data1_2); strcat(send_data, ddenc_mac); strcat(send_data, data2); //strcat(send_data, ddenc_device_name); //strcat(send_data, data3); //send_data[total_len] = '\0'; //printf("[PNS Check] JSON string before sending: %s\n", send_data); //printf("\n----ddenc_str_rand_for_cno #6:%s\n", ddenc_str_rand_for_cno); //write_to_logs_html(send_data, "PNS check send data", "Info", SystemSetting.enable_system_logs); mimepart = curl_mime_addpart(form); curl_mime_name(mimepart, "data"); curl_mime_type(mimepart, "application/json"); curl_mime_data(mimepart, send_data, CURL_ZERO_TERMINATED); //-------------------------------------------------- // CURL section headerlist = curl_slist_append(headerlist, "Accept: application/json"); headerlist = curl_slist_append(headerlist, "Content-Type: multipart/form-data"); headerlist = curl_slist_append(headerlist, "Connection: close"); headerlist = curl_slist_append(headerlist, buf); // Specify URL to send POST data //CURLcode result = 0; curl_easy_setopt(g_http_handle, CURLOPT_URL, region_cloud_addr); curl_easy_setopt(g_http_handle, CURLOPT_CONNECTTIMEOUT, 5); // 設定連線超時,單位秒 curl_easy_setopt(g_http_handle, CURLOPT_TIMEOUT, 10); curl_easy_setopt(g_http_handle, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(g_http_handle, CURLOPT_SSL_VERIFYPEER, 0); //證書取消 curl_easy_setopt(g_http_handle, CURLOPT_SSL_VERIFYHOST, 0); //證書取消 curl_easy_setopt(g_http_handle, CURLOPT_USERAGENT, "IPVideo"); curl_easy_setopt(g_http_handle, CURLOPT_VERBOSE, 0L); // Switch on full protocol/debug output curl_easy_setopt(g_http_handle, CURLOPT_LOW_SPEED_LIMIT, 1L); curl_easy_setopt(g_http_handle, CURLOPT_LOW_SPEED_TIME, 5L); struct MemoryStruct memory_temp_msg; struct MemoryStruct* ptr_responded_msg = &memory_temp_msg; //ptr_responded_msg->memory = NULL; ptr_responded_msg->size = 0; curl_easy_setopt(g_http_handle, CURLOPT_WRITEFUNCTION, WriteResponseString); curl_easy_setopt(g_http_handle, CURLOPT_WRITEDATA, ptr_responded_msg); curl_easy_setopt(g_http_handle, CURLOPT_TCP_FASTOPEN, 1L); curl_easy_setopt(g_http_handle, CURLOPT_HTTPHEADER, headerlist); curl_easy_setopt(g_http_handle, CURLOPT_MIMEPOST, form); curl_easy_setopt(g_http_handle, CURLOPT_FORBID_REUSE, 0L); //Set to 0 to have libcurl keep the connection open for possible later re-use (default behavior). CURLcode res = curl_easy_perform(g_http_handle); if (res != CURLE_OK) { curl_mime_free(form); char* err_msg = (char*)curl_easy_strerror(res); strcpy(returned_msg, err_msg); fprintf(stderr, "[PNS check if to post image] Failed to initialize the CURL handler for PNS. Reason: %s\n", returned_msg); write_to_logs_html(returned_msg, "PNS check", "ERROR", SystemSetting.enable_system_logs); } else { curl_mime_free(form); strcpy(returned_msg, ptr_responded_msg->memory); //write_to_logs_html(returned_msg, "PNS check", "Info", SystemSetting.enable_system_logs); //printf("\n--------response msg:%s\n", returned_msg); } curl_slist_free_all(headerlist); ptr_responded_msg->size = 0; curl_easy_reset(g_http_handle); } else { static const char err_msg[] = "(CURL initialization failed.)\n"; strcpy(returned_msg, err_msg); printf("[PNS check if to post image] CURL initialization failed.\n"); write_to_logs_html(returned_msg, "PNS check", "ERROR", SystemSetting.enable_system_logs); } if (strstr(returned_msg,"Timeout was reached") != NULL || strstr(returned_msg, "timeout was reached") != NULL || strstr(returned_msg, "Couldn't resolve host name") != NULL || strstr(returned_msg, "couldn't resolve host name") != NULL) { g_check_ping_OK = 0; } pthread_mutex_unlock(&mutex_curl); } #endif