FB::variant ibrowserAPI::uploadFile(const std::string& fileName, const boost::optional<FB::JSObjectPtr>& pcb, F_ADD) { if(fileName.empty()) return false; THREAD(&ibrowserAPI::uploadFile,fileName,pcb); const char *file_name=fileName.c_str(); char target_file[1024]; sprintf(target_file, "%s/%s",uploadFileDir.c_str(), basename((char *)file_name)); uint64_t target_file_handle = 0; if (AFC_E_SUCCESS != afc_file_open(afc_client, target_file, AFC_FOPEN_WRONLY, &target_file_handle)){ ERRO("afc_file_open error!"); } FILE *file_handle= fopen(file_name, "r"); if (!file_handle) { afc_remove_path(afc_client, target_file); ERRO("open file error!"); } off_t fileSize = 0; struct stat st; if (stat(file_name, &st) == 0) fileSize = st.st_size; static int read_buf_size = 1024; char read_buf[read_buf_size]; int bytes_read; uint32_t bytes_written = 0; uint32_t doneSize = 0; while((bytes_read = fread(read_buf, 1, read_buf_size, file_handle)) >0 ) { if (AFC_E_SUCCESS != afc_file_write(afc_client, target_file_handle, read_buf, bytes_read, &bytes_written) || bytes_read !=bytes_written) { ERRO("afc_file_write error!"); } memset(read_buf, 0, read_buf_size); doneSize = doneSize + bytes_read; if(pcb && fileSize > 0) (*pcb)->InvokeAsync("", FB::variant_list_of((double)doneSize/fileSize)); } SUCC(target_file); return target_file; }
FB::variant ibrowserAPI::uninstallPackage(const std::string& fileName, const boost::optional<FB::JSObjectPtr>& pcb, F_ADD) { if(fileName.empty()) return false; THREAD(&ibrowserAPI::uninstallPackage,fileName,pcb); int ret=0; Callback *cb = new Callback(); cb->set("pcb",*pcb); cb->set("scb",*scb); cb->set("ecb",*ecb); while (INSTPROXY_E_OP_IN_PROGRESS == (ret = instproxy_uninstall(instproxy_client, fileName.c_str(), NULL, &ibrowserAPI::installCallback, (void*)cb))) { log("uninstallPackage %s sleep...",fileName.c_str()); sleep(1); } if(INSTPROXY_E_SUCCESS != ret) { ERRO("instproxy_uninstall"); } return true; }
FB::variant ibrowserAPI::getAppList(F_ADD) { THREAD(&ibrowserAPI::getAppList); char *xml_doc=NULL; uint32_t xml_length=0; plist_t node = NULL; plist_t client_opts = instproxy_client_options_new(); instproxy_client_options_add(client_opts, "ApplicationType", "User", NULL); if(INSTPROXY_E_SUCCESS != instproxy_browse(instproxy_client,client_opts,&node)) { ERRO("instproxy_browse"); } instproxy_client_options_free(client_opts); plist_to_xml(node, &xml_doc, &xml_length); plist_free(node); SUCC(xml_doc); return xml_doc; }
FB::variant ibrowserAPI::getDeviceInfo(const std::vector<std::string>& domains,F_ADD) { THREAD(&ibrowserAPI::getDeviceInfo, domains); std::vector<std::string> result; for(int i=0;i<domains.size();i++) { std::string domain = domains[i]; plist_t node = NULL; int ret = 0; if(LOCKDOWN_E_SUCCESS != (ret = lockdownd_get_value(lockdownd_client, domain.empty()?NULL:domain.c_str(), NULL, &node)) ) { ERRO("lockdownd_get_value"); } char *xml_doc=NULL; uint32_t xml_length; plist_to_xml(node, &xml_doc, &xml_length); plist_free(node); result.insert(result.end(),std::string(xml_doc)); free(xml_doc); } SUCC(result); return result; }
int main(int argc, char *argv[]) { int sock; struct sockaddr_in echoserver; struct sockaddr_in echoclient; char buffer[BUFFSIZE]; unsigned int echolen, clientlen; int received = 0; char LAPTOP[]="192.168.1.212"; /* Create the UDP socket */ if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { ERRO("Failed to create socket"); } /* Construct the server sockaddr_in structure */ memset(&echoserver, 0, sizeof(echoserver)); /* Clear struct */ echoserver.sin_family = AF_INET; /* Internet/IP */ echoserver.sin_addr.s_addr = inet_addr(LAPTOP); /* IP address */ echoserver.sin_port = htons(atoi("1977")); /* server port */ /* Send the word to the server */ echolen = strlen("KEY1_VALUE=0"); if (sendto(sock,"KEY1_VALUE=0", echolen, 0, (struct sockaddr *) &echoserver, sizeof(echoserver)) != echolen) { ERRO("Mismatch in number of sent bytes"); } /* Receive the word back from the server */ fprintf(stdout, "Received: "); clientlen = sizeof(echoclient); if ((received = recvfrom(sock, buffer, BUFFSIZE, 0, (struct sockaddr *) &echoclient, &clientlen)) != echolen) { ERRO("Mismatch in number of received bytes"); } /* Check that client and server are using same socket */ if (echoserver.sin_addr.s_addr != echoclient.sin_addr.s_addr) { ERRO("Received a packet from an unexpected server"); } buffer[received] = '\0'; /* Assure null terminated string */ fprintf(stdout, buffer); fprintf(stdout, "\n"); close(sock); exit(0); }
FB::variant ibrowserAPI::setIdeviceEventCallback(const FB::JSObjectPtr& callback,F_ADD) { Callback *cb = new Callback(); cb->set("callback",callback); if(IDEVICE_E_SUCCESS != idevice_event_subscribe(&ibrowserAPI::ideviceEventCallback, (void *)cb)) ERRO("idevice_event_subscribe"); return true; }
FB::variant ibrowserAPI::getSbservicesIconPngdata(const std::string& bundleId,F_ADD) { THREAD(&ibrowserAPI::getSbservicesIconPngdata, bundleId); if(bundleId.empty()) return NULL; char *data = NULL; uint64_t size = 0; if (SBSERVICES_E_SUCCESS != sbservices_get_icon_pngdata(sbservices_client,bundleId.c_str(),&data,&size)) { ERRO("get_sbservices_icon_pngdata error"); } char *base64 = base64encode(data,size); free(data); SUCC(base64); return base64; }
FB::variant ibrowserAPI::downloadFile(const std::string& url,const std::string& filename,const boost::optional<FB::JSObjectPtr>& pcb, F_ADD) { if(url.empty() || filename.empty()) ERRO("url or filename is empty!"); THREAD(&ibrowserAPI::downloadFile,url,filename,pcb); curl_global_init(CURL_GLOBAL_ALL); double fileSize = -1; CURL *fileSizeCurl = curl_easy_init(); curl_easy_setopt(fileSizeCurl, CURLOPT_URL,url.c_str()); curl_easy_setopt(fileSizeCurl, CURLOPT_HEADER, 1); curl_easy_setopt(fileSizeCurl, CURLOPT_NOBODY, 1); curl_easy_perform(fileSizeCurl); curl_easy_getinfo(fileSizeCurl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &fileSize); curl_easy_cleanup(fileSizeCurl); log("download file size:%.0f",fileSize); if(0 >= fileSize) { ERRO("get file size error"); } char tmpName[1024]; sprintf(tmpName,"%s/%s",dirname(tmpnam(NULL)),filename.c_str()); log("start download, save file:%s, use %d threads",tmpName,this->downloadThreads); double partSize=fileSize/this->downloadThreads; pthread_t threads[this->downloadThreads]; std::vector<double> counter; for(int i=0;i<this->downloadThreads;i++) { double start=partSize*i; double end=0; if(i+1<this->downloadThreads) end=partSize*(i+1)-1; else end=fileSize; FILE *tmpFile=fopen(tmpName,"wb+"); if(!tmpFile) ERRO("create tmp file error"); fseek(tmpFile,start,SEEK_SET); counter.push_back(0); log("threadid:%d,range:%.0f-%.0f",i,start,end); DownloadConfig *cfg=new DownloadConfig(i,url,tmpFile,*pcb,fileSize,start,end,&counter); pthread_create(&threads[i],NULL,&ibrowserAPI::downloadThread,cfg); } for(int i=0; i< this->downloadThreads; i++) { pthread_join(threads[i], NULL); } curl_global_cleanup(); double downloadSize=0; int len=counter.size(); for(int i=0;i<len;i++) { downloadSize+=counter.at(i); } //downloadSize+=1; log("downloadSize:%.0f",downloadSize); if(downloadSize == fileSize) { SUCC(tmpName); }else{ ERRO("download error"); } return true; }
bool ibrowserAPI::init(F_SUCC,F_ERRO) { lockdownd_service_descriptor_t service = NULL; if (NULL == device) { if (IDEVICE_E_SUCCESS != idevice_new(&device, NULL)) { ERRO("idevice_new"); } idevice_set_debug_level(1); } if (NULL == lockdownd_client) { if (LOCKDOWN_E_SUCCESS != (lockdownd_client_new_with_handshake(device, &lockdownd_client, CLIENT_LABEL))) { ERRO("lockdownd_client_new_with_handshake"); } } if (NULL == instproxy_client) { if(LOCKDOWN_E_SUCCESS != (lockdownd_start_service(lockdownd_client,"com.apple.mobile.installation_proxy",&service) || !service->port)) { ERRO("lockdownd_start_service com.apple.mobile.installation_proxy"); } if(INSTPROXY_E_SUCCESS != instproxy_client_new(device,service,&instproxy_client) ) { ERRO("instproxy_client_new"); } } if (NULL == afc_client) { if(LOCKDOWN_E_SUCCESS != (lockdownd_start_service(lockdownd_client,"com.apple.afc",&service)) || !service->port) { ERRO("lockdownd_start_service com.apple.afc"); } if (afc_client_new(device, service, &afc_client) != AFC_E_SUCCESS) { ERRO("afc_client_new"); } } if (NULL == sbservices_client) { if(LOCKDOWN_E_SUCCESS != (lockdownd_start_service(lockdownd_client,"com.apple.springboardservices",&service)) || !service->port) { ERRO("lockdownd_start_service com.apple.springboardservices"); } if (sbservices_client_new(device, service, &sbservices_client) != AFC_E_SUCCESS) { ERRO("sbservices_client_new"); } } return true; }