void wl_update_rssi_cache(wl_rssi_cache_ctrl_t *rssi_cache_ctrl, wl_scan_results_t *ss_list) { wl_rssi_cache_t *node, *prev, *leaf, **rssi_head; wl_bss_info_t *bi = NULL; int i, j, k; if (!ss_list->count) return; rssi_head = &rssi_cache_ctrl->m_cache_head; /* update RSSI */ for (i = 0; i < ss_list->count; i++) { node = *rssi_head; prev = NULL; k = 0; bi = bi ? (wl_bss_info_t *)((uintptr)bi + dtoh32(bi->length)) : ss_list->bss_info; for (;node;) { if (!memcmp(&node->BSSID, &bi->BSSID, ETHER_ADDR_LEN)) { ANDROID_INFO(("%s: Update %d with BSSID %pM, RSSI=%d, SSID \"%s\"\n", __FUNCTION__, k, &bi->BSSID, dtoh16(bi->RSSI), bi->SSID)); for(j=0; j<RSSIAVG_LEN-1; j++) node->RSSI[j] = node->RSSI[j+1]; node->RSSI[j] = dtoh16(bi->RSSI); node->dirty = 0; break; } prev = node; node = node->next; k++; } if (node) continue; leaf = kmalloc(sizeof(wl_rssi_cache_t), GFP_KERNEL); if (!leaf) { ANDROID_ERROR(("%s: Memory alloc failure %d\n", __FUNCTION__, sizeof(wl_rssi_cache_t))); return; } ANDROID_INFO(("%s: Add %d with cached BSSID %pM, RSSI=%d, SSID \"%s\" in the leaf\n", __FUNCTION__, k, &bi->BSSID, dtoh16(bi->RSSI), bi->SSID)); leaf->next = NULL; leaf->dirty = 0; memcpy(&leaf->BSSID, &bi->BSSID, ETHER_ADDR_LEN); for (j=0; j<RSSIAVG_LEN; j++) leaf->RSSI[j] = dtoh16(bi->RSSI); if (!prev) *rssi_head = leaf; else prev->next = leaf; } }
void wl_delete_dirty_bss_cache(wl_bss_cache_ctrl_t *bss_cache_ctrl) { wl_bss_cache_t *node, *prev, **bss_head; int i = -1, tmp = 0; bss_head = &bss_cache_ctrl->m_cache_head; node = *bss_head; prev = node; for (;node;) { i++; if (node->dirty >= BSSCACHE_LEN) { if (node == *bss_head) { tmp = 1; *bss_head = node->next; } else { tmp = 0; prev->next = node->next; } ANDROID_TRACE(("%s: Del %d with BSSID %pM, RSSI=%d, SSID \"%s\"\n", __FUNCTION__, i, &node->results.bss_info->BSSID, dtoh16(node->results.bss_info->RSSI), node->results.bss_info->SSID)); kfree(node); if (tmp == 1) { node = *bss_head; prev = node; } else { node = prev->next; } continue; } prev = node; node = node->next; } }
void wl_delete_disconnected_bss_cache(wl_bss_cache_ctrl_t *bss_cache_ctrl, u8 *bssid) { wl_bss_cache_t *node, *prev, **bss_head; int i = -1, tmp = 0; bss_head = &bss_cache_ctrl->m_cache_head; node = *bss_head; prev = node; for (;node;) { i++; if (!memcmp(&node->results.bss_info->BSSID, bssid, ETHER_ADDR_LEN)) { if (node == *bss_head) { tmp = 1; *bss_head = node->next; } else { tmp = 0; prev->next = node->next; } ANDROID_INFO(("%s: Del %d with BSSID %pM, RSSI=%d, SSID \"%s\"\n", __FUNCTION__, i, &node->results.bss_info->BSSID, dtoh16(node->results.bss_info->RSSI), node->results.bss_info->SSID)); kfree(node); if (tmp == 1) { node = *bss_head; prev = node; } else { node = prev->next; } continue; } prev = node; node = node->next; } }
uint16_t ptp_usb_getresp (PTPParams* params, PTPContainer* resp) { uint16_t ret; unsigned long rlen; PTPUSBBulkContainer usbresp; /*GPContext *context = ((PTPData *)params->data)->context;*/ gp_log (GP_LOG_DEBUG, "ptp2/ptp_usb_getresp", "reading response"); PTP_CNT_INIT(usbresp); /* read response, it should never be longer than sizeof(usbresp) */ ret = ptp_usb_getpacket(params, &usbresp, &rlen); if (ret!=PTP_RC_OK) { ret = PTP_ERROR_IO; } else if (dtoh16(usbresp.type)!=PTP_USB_CONTAINER_RESPONSE) { ret = PTP_ERROR_RESP_EXPECTED; } else if (dtoh16(usbresp.code)!=resp->Code) { ret = dtoh16(usbresp.code); } if (ret!=PTP_RC_OK) { gp_log (GP_LOG_DEBUG, "ptp2/usb_getresp","request code 0x%04x getting resp error 0x%04x", resp->Code, ret); return ret; } /* build an appropriate PTPContainer */ resp->Code=dtoh16(usbresp.code); resp->SessionID=params->session_id; resp->Transaction_ID=dtoh32(usbresp.trans_id); if (resp->Transaction_ID != params->transaction_id - 1) { if (MTP_ZEN_BROKEN_HEADER(params)) { gp_log (GP_LOG_DEBUG, "ptp2/ptp_usb_getresp", "Read broken PTP header (transid is %08x vs %08x), compensating.", resp->Transaction_ID, params->transaction_id - 1 ); resp->Transaction_ID=params->transaction_id-1; } /* else will be handled by ptp.c as error. */ } resp->Nparam=(rlen-12)/4; resp->Param1=dtoh32(usbresp.payload.params.param1); resp->Param2=dtoh32(usbresp.payload.params.param2); resp->Param3=dtoh32(usbresp.payload.params.param3); resp->Param4=dtoh32(usbresp.payload.params.param4); resp->Param5=dtoh32(usbresp.payload.params.param5); return ret; }
uint16_t ptp_usb_getresp (PTPParams* params, PTPContainer* resp) { uint16_t ret; uint32_t rlen; PTPUSBBulkContainer usbresp; /*GPContext *context = ((PTPData *)params->data)->context;*/ GP_LOG_D ("Reading PTP_OC 0x%0x (%s) response...", resp->Code, ptp_get_opcode_name(params, resp->Code)); PTP_CNT_INIT(usbresp); /* read response, it should never be longer than sizeof(usbresp) */ ret = ptp_usb_getpacket(params, &usbresp, &rlen); if (ret!=PTP_RC_OK) { ret = PTP_ERROR_IO; } else if (dtoh16(usbresp.type)!=PTP_USB_CONTAINER_RESPONSE) { ret = PTP_ERROR_RESP_EXPECTED; } else if (dtoh16(usbresp.code)!=resp->Code) { ret = dtoh16(usbresp.code); } if (ret!=PTP_RC_OK) { GP_LOG_E ("PTP_OC 0x%04x receiving resp failed: %s (0x%04x)", resp->Code, ptp_strerror(ret, params->deviceinfo.VendorExtensionID), ret); return ret; } /* build an appropriate PTPContainer */ resp->Code=dtoh16(usbresp.code); resp->SessionID=params->session_id; resp->Transaction_ID=dtoh32(usbresp.trans_id); if (resp->Transaction_ID != params->transaction_id - 1) { if (MTP_ZEN_BROKEN_HEADER(params)) { GP_LOG_D ("Read broken PTP header (transid is %08x vs %08x), compensating.", resp->Transaction_ID, params->transaction_id - 1 ); resp->Transaction_ID=params->transaction_id-1; } /* else will be handled by ptp.c as error. */ } resp->Nparam=(rlen-12)/4; resp->Param1=dtoh32(usbresp.payload.params.param1); resp->Param2=dtoh32(usbresp.payload.params.param2); resp->Param3=dtoh32(usbresp.payload.params.param3); resp->Param4=dtoh32(usbresp.payload.params.param4); resp->Param5=dtoh32(usbresp.payload.params.param5); return ret; }
static inline uint16_t ptp_usb_event (PTPParams* params, PTPContainer* event, int wait) { int result, timeout, fasttimeout; unsigned long rlen; PTPUSBEventContainer usbevent; Camera *camera = ((PTPData *)params->data)->camera; if (params->deviceinfo.VendorExtensionID == PTP_VENDOR_CANON) fasttimeout = PTP2_FAST_TIMEOUT*2; else fasttimeout = PTP2_FAST_TIMEOUT; PTP_CNT_INIT(usbevent); if (event==NULL) return PTP_ERROR_BADPARAM; switch(wait) { case PTP_EVENT_CHECK: result = gp_port_check_int (camera->port, (char*)&usbevent, sizeof(usbevent)); if (result <= 0) result = gp_port_check_int (camera->port, (char*)&usbevent, sizeof(usbevent)); break; case PTP_EVENT_CHECK_FAST: gp_port_get_timeout (camera->port, &timeout); gp_port_set_timeout (camera->port, fasttimeout); result = gp_port_check_int (camera->port, (char*)&usbevent, sizeof(usbevent)); if (result <= 0) result = gp_port_check_int (camera->port, (char*)&usbevent, sizeof(usbevent)); gp_port_set_timeout (camera->port, timeout); break; default: return PTP_ERROR_BADPARAM; } if (result < 0) { gp_log (GP_LOG_DEBUG, "ptp2/usb_event", "reading event an error %d occurred", result); if (result == GP_ERROR_TIMEOUT) return PTP_ERROR_TIMEOUT; return PTP_ERROR_IO; } if (result == 0) { gp_log (GP_LOG_DEBUG, "ptp2/usb_event", "reading event an 0 read occurred, assuming timeout."); return PTP_ERROR_TIMEOUT; } rlen = result; if (rlen < 8) { gp_log (GP_LOG_ERROR, "ptp2/usb_event", "reading event an short read of %ld bytes occurred", rlen); return PTP_ERROR_IO; } /* Only do the additional reads for "events". Canon IXUS 2 likes to * send unrelated data. */ if ( (dtoh16(usbevent.type) == PTP_USB_CONTAINER_EVENT) && (dtoh32(usbevent.length) > rlen) ) { gp_log (GP_LOG_DEBUG, "ptp2/usb_event","Canon incremental read (done: %ld, todo: %d)", rlen, dtoh32(usbevent.length)); gp_port_get_timeout (camera->port, &timeout); gp_port_set_timeout (camera->port, PTP2_FAST_TIMEOUT); while (dtoh32(usbevent.length) > rlen) { result = gp_port_check_int (camera->port, ((char*)&usbevent)+rlen, sizeof(usbevent)-rlen); if (result <= 0) break; rlen += result; } gp_port_set_timeout (camera->port, timeout); } /* if we read anything over interrupt endpoint it must be an event */ /* build an appropriate PTPContainer */ event->Nparam = (rlen-12)/4; event->Code = dtoh16(usbevent.code); event->SessionID=params->session_id; event->Transaction_ID=dtoh32(usbevent.trans_id); event->Param1 = dtoh32(usbevent.param1); event->Param2 = dtoh32(usbevent.param2); event->Param3 = dtoh32(usbevent.param3); return PTP_RC_OK; }
uint16_t ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler) { uint16_t ret; PTPUSBBulkContainer usbdata; unsigned char *data; unsigned long bytes_to_read, written, curread, oldsize; Camera *camera = ((PTPData *)params->data)->camera; int usecontext, progressid = 0, tries = 0, res; GPContext *context = ((PTPData *)params->data)->context; gp_log (GP_LOG_DEBUG, "ptp2/ptp_usb_getdata", "reading data"); PTP_CNT_INIT(usbdata); do { unsigned long len, rlen; ret = ptp_usb_getpacket(params, &usbdata, &rlen); if (ret!=PTP_RC_OK) { ret = PTP_ERROR_IO; break; } if (dtoh16(usbdata.type)!=PTP_USB_CONTAINER_DATA) { /* We might have got a response instead. On error for instance. */ if (dtoh16(usbdata.type) == PTP_USB_CONTAINER_RESPONSE) { params->response_packet = malloc(dtoh32(usbdata.length)); if (!params->response_packet) return PTP_RC_GeneralError; memcpy(params->response_packet, (uint8_t *) &usbdata, dtoh32(usbdata.length)); params->response_packet_size = dtoh32(usbdata.length); ret = PTP_RC_OK; } else { ret = PTP_ERROR_DATA_EXPECTED; } break; } if (dtoh16(usbdata.code)!=ptp->Code) { /* A creative Zen device breaks down here, by leaving out * Code and Transaction ID */ if (MTP_ZEN_BROKEN_HEADER(params)) { gp_log (GP_LOG_DEBUG, "ptp2/ptp_usb_getdata", "Read broken PTP header (Code is %04x vs %04x), compensating.", dtoh16(usbdata.code), ptp->Code ); usbdata.code = dtoh16(ptp->Code); usbdata.trans_id = htod32(ptp->Transaction_ID); } else { gp_log (GP_LOG_ERROR, "ptp2/ptp_usb_getdata", "Read broken PTP header (Code is %04x vs %04x).", dtoh16(usbdata.code), ptp->Code ); ret = PTP_ERROR_IO; break; } } if (usbdata.length == 0xffffffffU) { unsigned char *data = malloc (PTP_USB_BULK_HS_MAX_PACKET_LEN_READ); if (!data) return PTP_RC_GeneralError; /* Copy first part of data to 'data' */ handler->putfunc( params, handler->priv, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data, &written ); /* stuff data directly to passed data handler */ while (1) { unsigned long written; int result = gp_port_read (camera->port, (char*)data, PTP_USB_BULK_HS_MAX_PACKET_LEN_READ); if (result < 0) { free (data); return PTP_ERROR_IO; } handler->putfunc (params, handler->priv, result, data, &written); if (result < PTP_USB_BULK_HS_MAX_PACKET_LEN_READ) break; } free (data); return PTP_RC_OK; } if (rlen > dtoh32(usbdata.length)) { /* * Buffer the surplus response packet if it is >= * PTP_USB_BULK_HDR_LEN * (i.e. it is probably an entire package) * else discard it as erroneous surplus data. * This will even work if more than 2 packets appear * in the same transaction, they will just be handled * iteratively. * * Marcus observed stray bytes on iRiver devices; * these are still discarded. */ unsigned int packlen = dtoh32(usbdata.length); unsigned int surplen = rlen - packlen; if (surplen >= PTP_USB_BULK_HDR_LEN) { params->response_packet = malloc(surplen); if (!params->response_packet) return PTP_RC_GeneralError; memcpy(params->response_packet, (uint8_t *) &usbdata + packlen, surplen); params->response_packet_size = surplen; } else { gp_log (GP_LOG_DEBUG, "ptp2/ptp_usb_getdata", "read %ld bytes too much, expect problems!", rlen - dtoh32(usbdata.length)); } rlen = packlen; } /* For most PTP devices rlen is 512 == sizeof(usbdata) * here. For MTP devices splitting header and data it might * be 12. */ /* Evaluate full data length. */ len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN; /* autodetect split header/data MTP devices */ if (dtoh32(usbdata.length) > 12 && (rlen==12)) params->split_header_data = 1; /* Copy first part of data to 'data' */ handler->putfunc( params, handler->priv, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data, &written ); /* Is that all of data? */ if (len+PTP_USB_BULK_HDR_LEN<=rlen) break; /* If not read the rest of it. */ retry: oldsize = 0; data = malloc(READLEN); if (!data) return PTP_RC_GeneralError; bytes_to_read = len - (rlen - PTP_USB_BULK_HDR_LEN); usecontext = (bytes_to_read > CONTEXT_BLOCK_SIZE); ret = PTP_RC_OK; if (usecontext) progressid = gp_context_progress_start (context, (bytes_to_read/CONTEXT_BLOCK_SIZE), _("Downloading...")); curread = 0; res = 0; while (bytes_to_read > 0) { unsigned long toread = bytes_to_read; /* read in large blobs. * if smaller than large blob, read all but the last short packet * depending on EP packetsize. */ if (toread > READLEN) toread = READLEN; else if (toread > params->maxpacketsize) toread = toread - (toread % params->maxpacketsize); res = gp_port_read (camera->port, (char*)data, toread); if (res <= 0) { ret = PTP_ERROR_IO; break; } ret = handler->putfunc (params, handler->priv, res, data, &written ); if (ret != PTP_RC_OK) break; if (written != res) { ret = PTP_ERROR_IO; break; } bytes_to_read -= res; curread += res; if (usecontext && (oldsize/CONTEXT_BLOCK_SIZE < curread/CONTEXT_BLOCK_SIZE)) gp_context_progress_update (context, progressid, curread/CONTEXT_BLOCK_SIZE); if (gp_context_cancel(context) == GP_CONTEXT_FEEDBACK_CANCEL) { ret = PTP_ERROR_CANCEL; break; } oldsize = curread; } free (data); if (usecontext) gp_context_progress_stop (context, progressid); if (res == GP_ERROR_IO_READ) { gp_log (GP_LOG_DEBUG, "ptp2/usbread", "Clearing halt on IN EP and retrying once."); gp_port_usb_clear_halt (camera->port, GP_PORT_USB_ENDPOINT_IN); /* retrying only makes sense if we did not read anything yet */ if ((tries++ < 1) && (curread == 0)) goto retry; } if ((ret!=PTP_RC_OK) && (ret!=PTP_ERROR_CANCEL)) { ret = PTP_ERROR_IO; break; } } while (0); if ((ret!=PTP_RC_OK) && (ret!=PTP_ERROR_CANCEL)) { gp_log (GP_LOG_DEBUG, "ptp2/usb_getdata", "request code 0x%04x getting data error 0x%04x", ptp->Code, ret); } return ret; }
void wl_update_bss_cache(wl_bss_cache_ctrl_t *bss_cache_ctrl, wl_scan_results_t *ss_list) { wl_bss_cache_t *node, *prev, *leaf, *tmp, **bss_head; wl_bss_info_t *bi = NULL; int i, k=0; if (!ss_list->count) return; bss_head = &bss_cache_ctrl->m_cache_head; for (i=0; i < ss_list->count; i++) { node = *bss_head; prev = NULL; bi = bi ? (wl_bss_info_t *)((uintptr)bi + dtoh32(bi->length)) : ss_list->bss_info; for (;node;) { if (!memcmp(&node->results.bss_info->BSSID, &bi->BSSID, ETHER_ADDR_LEN)) { tmp = node; leaf = kmalloc(dtoh32(bi->length) + WLC_IW_SS_CACHE_CTRL_FIELD_MAXLEN, GFP_KERNEL); if (!leaf) { ANDROID_ERROR(("%s: Memory alloc failure %d and keep old BSS info\n", __FUNCTION__, dtoh32(bi->length) + WLC_IW_SS_CACHE_CTRL_FIELD_MAXLEN)); break; } memcpy(leaf->results.bss_info, bi, dtoh32(bi->length)); leaf->next = node->next; leaf->dirty = 0; leaf->results.count = 1; leaf->results.version = ss_list->version; ANDROID_TRACE(("%s: Update %d with BSSID %pM, RSSI=%d, SSID \"%s\"\n", __FUNCTION__, k, &bi->BSSID, dtoh16(bi->RSSI), bi->SSID)); if (!prev) *bss_head = leaf; else prev->next = leaf; node = leaf; prev = node; kfree(tmp); k++; break; } prev = node; node = node->next; } if (node) continue; leaf = kmalloc(dtoh32(bi->length) + WLC_IW_SS_CACHE_CTRL_FIELD_MAXLEN, GFP_KERNEL); if (!leaf) { ANDROID_ERROR(("%s: Memory alloc failure %d\n", __FUNCTION__, dtoh32(bi->length) + WLC_IW_SS_CACHE_CTRL_FIELD_MAXLEN)); return; } ANDROID_TRACE(("%s: Add %d with cached BSSID %pM, RSSI=%d, SSID \"%s\" in the leaf\n", __FUNCTION__, k, &bi->BSSID, dtoh16(bi->RSSI), bi->SSID)); memcpy(leaf->results.bss_info, bi, dtoh32(bi->length)); leaf->next = NULL; leaf->dirty = 0; leaf->results.count = 1; leaf->results.version = ss_list->version; k++; if (!prev) *bss_head = leaf; else prev->next = leaf; } }
int scan_and_get_security(uint8 ch, uint8* mac) { if (!mac) return -1; #if 0 LOGD("ch: %d%s\n", ch, ch?"":"(not specified)"); LOGD("mac: %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); #endif int ret = 0; int sec = 0; wl_scan_params_t p; memset(&p, 0, sizeof(p)); memcpy(&p.bssid, mac, ETHER_ADDR_LEN); p.bss_type = DOT11_BSSTYPE_ANY; p.scan_type = 0; p.nprobes = -1; p.active_time = 100; p.passive_time = -1; p.home_time = -1; p.channel_num = 0; if (ch > 0) { p.channel_num = 1; p.channel_list[0] = ch; } p.nprobes = htod32(p.nprobes); p.active_time = htod32(p.active_time); p.passive_time = htod32(p.passive_time); p.home_time = htod32(p.home_time); if ((ret = easy_setup_ioctl(WLC_SCAN, 1, &p, sizeof(p))) < 0) { return -1; } int size = 1024; uint8* ptr = malloc(size); wl_scan_results_t* r; r = (wl_scan_results_t*) ptr; int tries = 20; int delay_ms = 150; while (tries--) { memset(ptr, 0, size); r->buflen = size; ret = easy_setup_ioctl(WLC_SCAN_RESULTS, 0, r, size); if (!ret && r->count) break; //LOGD("sleep %dms\n", delay_ms); usleep(delay_ms*1000); } if (!ret && r->count) { int i=0; wl_bss_info_t* bi = r->bss_info; if (dtoh32(bi->version) == 107) { wl_bss_info_107_t *old = (wl_bss_info_107_t *)bi; bi->ie_length = old->ie_length; bi->ie_offset = sizeof(wl_bss_info_107_t); } uint16 cap = dtoh16(bi->capability); int totlen = dtoh32(bi->ie_length); int wpa = 0; if (totlen) { uint8* ie = (uint8 *)(((uint8 *)bi) + dtoh16(bi->ie_offset)); while (totlen>=2) { int type = ie[0]; int len = ie[1]; if (type == 221 && len >= 4) { int v = (ie[2]<<24) | (ie[3]<<16) | (ie[4]<<8) | (ie[5]); if (v == 0x0050f201) { sec = WLAN_SECURITY_WPA; break; } } if (type == 48) { sec = WLAN_SECURITY_WPA2; break; } totlen -= (len+2); ie += (len+2); } } if (sec != WLAN_SECURITY_WPA && sec != WLAN_SECURITY_WPA2) { if (cap & 0x10) { sec = WLAN_SECURITY_WEP; } else { sec = WLAN_SECURITY_NONE; } } } else { LOGD("scan gets no result(ret: %d, count: %d).\n", ret, (!ret)?r->count:0); sec = -1; } free(ptr); return sec; }
static int lua_get_scan(lua_State *L, void *wl, lua_cmd_t *cmd, char *argv) { int ret; wl_scan_results_t *list = (wl_scan_results_t*)buf; wl_bss_info_t *bi; char ssid[33]; uint i; ret = wl_get_scan(wl, WLC_SCAN_RESULTS, buf, WL_DUMP_BUF_LEN); if (ret == 0) { lua_newtable(L); bi = list->bss_info; for (i = 0; i < list->count; i++, bi = (wl_bss_info_t*)((int8*)bi + dtoh32(bi->length))) { lua_newtable(L); lua_pushstring(L, wl_ether_etoa(&bi->BSSID)); lua_setfield(L, -2, "bssid"); memset(ssid, 0, sizeof(ssid)); memcpy(ssid, bi->SSID, bi->SSID_len); lua_pushstring(L, ssid); lua_setfield(L, -2, "ssid"); lua_pushnumber(L, (int16)(dtoh16(bi->RSSI))); lua_setfield(L, -2, "rssi"); lua_pushnumber(L, bi->ctl_ch); lua_setfield(L, -2, "channel"); lua_pushstring(L, bi->n_cap ? "11n" : "11g"); lua_setfield(L, -2, "bgn_mode"); if (bi->capability & DOT11_CAP_PRIVACY) { if (dtoh32(bi->ie_length) && wl_if_wpa_rsn_ies((uint8 *)(((uint8 *)bi) + dtoh16(bi->ie_offset)), dtoh32(bi->ie_length))) { lua_pushstring(L, "psk"); lua_setfield(L, -2, "auth"); } else { lua_pushstring(L, "wep"); lua_setfield(L, -2, "auth"); } } else { lua_pushstring(L, "none"); lua_setfield(L, -2, "auth"); } lua_pushstring(L, (dtoh32(bi->nbss_cap) & HT_CAP_40MHZ) ? "40MHz" : "20MHz"); lua_setfield(L, -2, "cwm"); lua_rawseti(L, -2, i+1); } } return ret; }
uint16_t ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler) { uint16_t ret; PTPUSBBulkContainer usbdata; unsigned char *data = NULL; uint32_t bytes_to_read, bytes_read; Camera *camera = ((PTPData *)params->data)->camera; int report_progress, progress_id = 0, do_retry = TRUE, res = GP_OK; GPContext *context = ((PTPData *)params->data)->context; GP_LOG_D ("Reading PTP_OC 0x%0x (%s) data...", ptp->Code, ptp_get_opcode_name(params, ptp->Code)); PTP_CNT_INIT(usbdata); ret = ptp_usb_getpacket (params, &usbdata, &bytes_read); if (ret != PTP_RC_OK) goto exit; if (dtoh16(usbdata.type) != PTP_USB_CONTAINER_DATA) { /* We might have got a response instead. On error for instance. */ /* TODO: check if bytes_read == usbdata.length */ if (dtoh16(usbdata.type) == PTP_USB_CONTAINER_RESPONSE) { params->response_packet = malloc(dtoh32(usbdata.length)); if (!params->response_packet) return PTP_RC_GeneralError; memcpy(params->response_packet, (uint8_t *) &usbdata, dtoh32(usbdata.length)); params->response_packet_size = dtoh32(usbdata.length); ret = PTP_RC_OK; } else { ret = PTP_ERROR_DATA_EXPECTED; } goto exit; } if (dtoh16(usbdata.code)!=ptp->Code) { /* A creative Zen device breaks down here, by leaving out * Code and Transaction ID */ if (MTP_ZEN_BROKEN_HEADER(params)) { GP_LOG_D ("Read broken PTP header (Code is %04x vs %04x), compensating.", dtoh16(usbdata.code), ptp->Code); usbdata.code = dtoh16(ptp->Code); usbdata.trans_id = htod32(ptp->Transaction_ID); } else { GP_LOG_E ("Read broken PTP header (Code is %04x vs %04x).", dtoh16(usbdata.code), ptp->Code ); ret = PTP_ERROR_IO; goto exit; } } if (bytes_read > dtoh32(usbdata.length)) { /* * Buffer the surplus response packet if it is >= * PTP_USB_BULK_HDR_LEN * (i.e. it is probably an entire package) * else discard it as erroneous surplus data. * This will even work if more than 2 packets appear * in the same transaction, they will just be handled * iteratively. * * Marcus observed stray bytes on iRiver devices; * these are still discarded. */ uint32_t surplen = bytes_read - dtoh32(usbdata.length); if (surplen >= PTP_USB_BULK_HDR_LEN) { params->response_packet = malloc(surplen); if (!params->response_packet) return PTP_RC_GeneralError; memcpy(params->response_packet, (uint8_t *) &usbdata + dtoh32(usbdata.length), surplen); params->response_packet_size = surplen; } else { GP_LOG_D ("Read %ld bytes too much, expect problems!", (long)(bytes_read - dtoh32(usbdata.length))); } bytes_read = dtoh32(usbdata.length); } /* For most PTP devices rlen is 512 == sizeof(usbdata) * here. For MTP devices splitting header and data it might * be PTP_USB_BULK_HDR_LEN (12). */ /* autodetect split header/data MTP devices */ if (dtoh32(usbdata.length) > PTP_USB_BULK_HDR_LEN && (bytes_read==PTP_USB_BULK_HDR_LEN)) params->split_header_data = 1; /* Copy the payload bytes we already read (with the first usb packet) */ ret = handler->putfunc (params, handler->priv, bytes_read - PTP_USB_BULK_HDR_LEN, usbdata.payload.data); if (ret != PTP_RC_OK) goto exit; /* Check if we are done already... */ if (bytes_read >= dtoh32(usbdata.length)) goto exit; /* If not read the rest of it. */ /* Make bytes_to_read contain the number of remaining payload-bytes to read. */ bytes_to_read = dtoh32(usbdata.length) - bytes_read; /* Make bytes_read contain the number of payload-bytes already read. */ bytes_read -= PTP_USB_BULK_HDR_LEN; data = malloc(READLEN); if (!data) return PTP_RC_GeneralError; report_progress = (bytes_to_read > 2*CONTEXT_BLOCK_SIZE) && (dtoh32(usbdata.length) != 0xffffffffU); if (report_progress) progress_id = gp_context_progress_start (context, (bytes_to_read/CONTEXT_BLOCK_SIZE), _("Downloading...")); while (bytes_to_read > 0) { unsigned long chunk_to_read = bytes_to_read; /* if in read-until-short-packet mode, read one packet at a time */ /* else read in large blobs */ /* else read all but the last short packet depending on EP packetsize. */ if (dtoh32(usbdata.length) == 0xffffffffU) chunk_to_read = PTP_USB_BULK_HS_MAX_PACKET_LEN_READ; else if (chunk_to_read > READLEN) chunk_to_read = READLEN; else if (chunk_to_read > params->maxpacketsize) chunk_to_read = chunk_to_read - (chunk_to_read % params->maxpacketsize); res = gp_port_read (camera->port, (char*)data, chunk_to_read); if (res == GP_ERROR_IO_READ && do_retry) { GP_LOG_D ("Clearing halt on IN EP and retrying once."); gp_port_usb_clear_halt (camera->port, GP_PORT_USB_ENDPOINT_IN); /* retrying only once and only if we did not read anything yet */ do_retry = FALSE; continue; } else if (res <= 0) { ret = PTP_ERROR_IO; break; } else do_retry = FALSE; /* once we have succesfully read any data, don't try again */ ret = handler->putfunc (params, handler->priv, res, data); if (ret != PTP_RC_OK) break; if (dtoh32(usbdata.length) == 0xffffffffU) { /* once we have read a short packet, we are done. */ if (res < PTP_USB_BULK_HS_MAX_PACKET_LEN_READ) bytes_to_read = 0; } else bytes_to_read -= res; bytes_read += res; if (report_progress && ((bytes_read-res)/CONTEXT_BLOCK_SIZE < bytes_read/CONTEXT_BLOCK_SIZE)) gp_context_progress_update (context, progress_id, bytes_read/CONTEXT_BLOCK_SIZE); /* Only cancel transfers larger than 1MB. as * canceling did not work on eos_get_viewfinder of 200kb */ if ((bytes_read > 1024*1024) && gp_context_cancel(context) == GP_CONTEXT_FEEDBACK_CANCEL) { ret = PTP_ERROR_CANCEL; break; } } if (report_progress) gp_context_progress_stop (context, progress_id); exit: free (data); if ((ret!=PTP_RC_OK) && (ret!=PTP_ERROR_CANCEL)) { GP_LOG_E ("PTP_OC 0x%04x receiving data failed: %s (0x%04x)", ptp->Code, ptp_strerror(ret, params->deviceinfo.VendorExtensionID), ret); return PTP_ERROR_IO; } return ret; }