/* rt_uint8_t exist_dev(dev_t *dev) { rt_int8_t fd; rt_uint8_t i; struct stat buf; dev_t *tmpbuff = RT_NULL; if((fd = open(DEV, DFS_O_CREAT|DFS_O_RDWR,0))<0) { rt_kprintf("%s:%d Open %s failed\n",__func__,__LINE__,DEV); list_mem(); return FAILE; } //rt_kprintf("%s:%d Open %s fd = %d\n",__func__,__LINE__,DEV,fd); stat(DEV, &buf); //rt_kprintf("%s:%d buf.st_size = %d\n",__func__,__LINE__,buf.st_size); if(buf.st_size>0) { tmpbuff = (dev_t*)rt_malloc(buf.st_size); rt_memset(tmpbuff,0,buf.st_size); if(0==tmpbuff) { rt_kprintf("%s:%d Memory allocation failure\n",__func__,__LINE__); goto failed; } if(read(fd,tmpbuff,buf.st_size)<buf.st_size) { rt_kprintf("%s:%d Read %s error\n",__func__,__LINE__,DEV); goto failed; } for(i=0;i<(buf.st_size/sizeof(dev_t));i++) { if(rt_memcmp((tmpbuff+i)->mac,dev->mac,MAC_LEN)==0) { // rt_kprintf("%s:%d device amc: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x shortadd = %04x\n",__func__,__LINE__, // dev->mac[0],dev->mac[1],dev->mac[2],dev->mac[3],dev->mac[4],dev->mac[5],dev->mac[6],dev->mac[7],dev->shortadd); dev->stat = (tmpbuff+i)->stat; dev->shortadd = (tmpbuff+i)->shortadd; // rt_kprintf("%s:%d device amc: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x shortadd = %04x\n",__func__,__LINE__, // dev->mac[0],dev->mac[1],dev->mac[2],dev->mac[3],dev->mac[4],dev->mac[5],dev->mac[6],dev->mac[7],dev->shortadd); goto success; } } rt_kprintf("%s:%d device not find\n",__func__,__LINE__); } goto failed; success: if(tmpbuff) rt_free(tmpbuff); close(fd); return SUCCESS; failed: if(tmpbuff) rt_free(tmpbuff); close(fd); return FAILE; } */ rt_uint8_t exist_dev(dev_t *dev,int n) { rt_int8_t fd; rt_uint8_t i,j; struct stat buf; dev_t *tmpbuff = RT_NULL; if((fd = open(DEV, DFS_O_CREAT|DFS_O_RDWR,0))<0) { rt_kprintf("%s:%d Open %s failed\n",__func__,__LINE__,DEV); list_mem(); return FAILE; } //rt_kprintf("%s:%d Open %s fd = %d\n",__func__,__LINE__,DEV,fd); stat(DEV, &buf); //rt_kprintf("%s:%d buf.st_size = %d\n",__func__,__LINE__,buf.st_size); if(buf.st_size>0) { tmpbuff = (dev_t*)rt_malloc(buf.st_size); rt_memset(tmpbuff,0,buf.st_size); if(0==tmpbuff) { rt_kprintf("%s:%d Memory allocation failure\n",__func__,__LINE__); goto failed; } if(read(fd,tmpbuff,buf.st_size)<buf.st_size) { rt_kprintf("%s:%d Read %s error\n",__func__,__LINE__,DEV); goto failed; } for(j=0;j<n;j++) { for(i=0;i<(buf.st_size/sizeof(dev_t));i++) { if(rt_memcmp((tmpbuff+i)->mac,(dev+j)->mac,MAC_LEN)==0) { (dev+j)->stat = (tmpbuff+i)->stat; (dev+j)->shortadd = (tmpbuff+i)->shortadd; } } } } goto failed; success: if(tmpbuff) rt_free(tmpbuff); close(fd); return SUCCESS; failed: if(tmpbuff) rt_free(tmpbuff); close(fd); return FAILE; }
rt_uint8_t change_dev(dev_t *dev) { rt_int8_t fd; rt_uint8_t i; struct stat buf; dev_t *tmpbuff = RT_NULL; if((fd = open(DEV, O_RDWR|O_CREAT,0)<0)) { rt_kprintf("%s:%d Open %s failed\n",__func__,__LINE__,DEV); return FAILE; } rt_kprintf("%s:%d Open %s fd = %d\n",__func__,__LINE__,DEV,fd); stat(DEV, &buf); if(buf.st_size>0) { tmpbuff = (dev_t*)rt_malloc(buf.st_size); if(0==tmpbuff) { rt_kprintf("%s:%d Memory allocation failure\n",__func__,__LINE__); goto failed; } if(read(fd,tmpbuff,buf.st_size)<buf.st_size) { rt_kprintf("%s:%d Read %s error\n",__func__,__LINE__,DEV); goto failed; } for(i=0;i<(buf.st_size/sizeof(dev_t));i++) { if(rt_memcmp((tmpbuff+i)->mac,dev->mac,MAC_LEN)==0 || (tmpbuff+i)->shortadd==dev->shortadd) { (tmpbuff+i)->stat = dev->stat; lseek(fd,0,DFS_SEEK_SET); write(fd,tmpbuff,buf.st_size); goto success; } } } goto failed; success: if(tmpbuff) rt_free(tmpbuff); close(fd); return SUCCESS; failed: if(tmpbuff) rt_free(tmpbuff); close(fd); return FAILE; }
/** * This function will return the first occurrence of a string. * * @param s1 the source string * @param s2 the find string * * @return the first occurrence of a s2 in s1, or RT_NULL if no found. */ char *rt_strstr(const char *s1, const char *s2) { int l1, l2; l2 = rt_strlen(s2); if (!l2) return (char *)s1; l1 = rt_strlen(s1); while (l1 >= l2) { l1 --; if (!rt_memcmp(s1, s2, l2)) return (char *)s1; s1 ++; } return RT_NULL; }
/* * 如果找到了返回对应起始地址 * 如果没有找到,当有空闲位置时返回一个空位置地址,否则,返回NULL * */ static char *find_ptct_sn(char *sn, ptct_sn_set_t ptct_sn_set, int max) { int i; char *first_empty = NULL; char *tmp; for (i=0; i<max; ++i) { tmp = ptct_sn_set[i]; if (0 == rt_memcmp(sn, tmp, DEV_SN_MODE_LEN)) { return tmp; } if (0 == ptct_sn_set[i][0]) first_empty = ptct_sn_set[i]; } return first_empty; }
void syscfgdata_syn_proc(void) { struct syscfgdata_tbl *p; rt_err_t ret_sem; if (is_syscfgdata_tbl_dirty(syscfgdata_tbl_cache->systbl_flag_set) || is_syscfgdata_tbl_wthrough(syscfgdata_tbl_cache->systbl_flag_set)) { p = rt_calloc(RT_ALIGN(SYSCFGDATA_TBL_SIZE_OF_FLASH, 4), 1); if (NULL == p) { printf_syn("func:%s(), line:%d:mem alloc fail\n", __FUNCTION__, __LINE__); return; } ret_sem = rt_sem_take(&write_syscfgdata_sem, RT_WAITING_FOREVER); if (RT_EOK != ret_sem) { SYSCFG_DATA_LOG(("func:%s, line:%d, error(%d)", __FUNCTION__, __LINE__, ret_sem)); goto free_entry; } if (0 != read_whole_syscfgdata_tbl(p, SYSCFGDATA_TBL_SIZE_OF_FLASH)) goto release_sem; if (0==rt_memcmp(&syscfgdata_tbl_cache->syscfg_data, p, sizeof(syscfgdata_tbl_cache->syscfg_data))) goto release_sem; write_whole_syscfgdata_tbl(&syscfgdata_tbl_cache->syscfg_data); clr_syscfgdata_tbl_dirty(syscfgdata_tbl_cache->systbl_flag_set); clr_syscfgdata_tbl_wthrough(syscfgdata_tbl_cache->systbl_flag_set); release_sem: rt_sem_release(&write_syscfgdata_sem); free_entry: rt_free(p); } return; }
rt_err_t rt_wlan_dev_control(rt_device_t dev, rt_uint8_t cmd, void *args) { rt_err_t error=RT_EOK; struct rt_wlan_dev* netdev=(struct rt_wlan_dev*)(dev->user_data); WlanInfo *wlan=netdev->priv; WlanCard *card=wlan->card; char addressbuf[6]={0xff,0xff,0xff,0xff,0xff,0xff}; char *myadd=(char *)args; if(dev==RT_NULL||dev->user_data==RT_NULL) { error=-RT_EEMPTY; goto done; } switch (cmd) { case NIOCTL_GADDR: if(rt_memcmp(card->MyMacAddress,addressbuf,6)==0) { error=-RT_ERROR; break; } else{ rt_memcpy((u8*)args,card->MyMacAddress,6); rt_kprintf("my address %x,%x,%x,%x,%x,%x,%x\r\n",myadd[0],myadd[1],myadd[2], myadd[3],myadd[4],myadd[5]); } break; default : break; } done: return error; }
/** * @brief This function processes received packet and forwards it * to kernel/upper layer * * @param priv A pointer to wlan_private * @param skb A pointer to skb which includes the received packet * @return WLAN_STATUS_SUCCESS or WLAN_STATUS_FAILURE */ void ProcessRxedPacket(WlanCard *cardinfo,u8 *data,u32 len) { struct pbuf* p = RT_NULL; RxPacketHdr_t *pRxPkt; RxPD *pRxPD; u32 pbuflen; int hdrChop; int minlen; EthII_Hdr_t *pEthHdr; Rx_Pbuf_List *RxNode = NULL; rt_base_t level; WlanCard *card = cardinfo; Rx_Pbuf_List *HeadNode = &card->RxList; const u8 rfc1042_eth_hdr[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 }; RxNode = rt_malloc(sizeof(RxNode)); if (RxNode == NULL) { WlanDebug(WlanErr,"RX packet Error: memory alloc failed\r\n"); goto done; } pRxPD = (RxPD *) data; pRxPkt = (RxPacketHdr_t *) ((u8 *) pRxPD + pRxPD->PktOffset); /*mac source address :6byte .mac destination address :6byte. length 2 bytes =14*/ minlen = (8 + 4 + (pRxPD->PktOffset)); if (len < minlen) { WlanDebug( WlanErr,"RX Error: packet length is too long\n"); rt_free(RxNode); goto done; } WlanDebug(WlanData, "RX Data:%d\n",len - pRxPD->PktOffset); WlanDebug(WlanData, "SNR: %d, NF: %d\n", pRxPD->SNR, pRxPD->NF); WlanDebug(WlanData,"RX Data Dest \r\n %x,%x,%x,%x,%x,%x\r\n", pRxPkt->eth803_hdr.dest_addr[0], pRxPkt->eth803_hdr.dest_addr[1], pRxPkt->eth803_hdr.dest_addr[2], pRxPkt->eth803_hdr.dest_addr[3], pRxPkt->eth803_hdr.dest_addr[4], pRxPkt->eth803_hdr.dest_addr[5]); WlanDebug(WlanData,"RX Data Src\r\n %x,%x,%x,%x,%x,%x\r\n", pRxPkt->eth803_hdr.src_addr[0], pRxPkt->eth803_hdr.src_addr[1], pRxPkt->eth803_hdr.src_addr[2], pRxPkt->eth803_hdr.src_addr[3], pRxPkt->eth803_hdr.src_addr[4], pRxPkt->eth803_hdr.src_addr[5]); if (rt_memcmp(&pRxPkt->rfc1042_hdr, rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) { /* * Replace the 803 header and rfc1042 header (llc/snap) with an * EthernetII header, keep the src/dst and snap_type (ethertype) * * The firmware only passes up SNAP frames converting * all RX Data from 802.11 to 802.2/LLC/SNAP frames. * * To create the Ethernet II, just move the src, dst address right * before the snap_type. */ pEthHdr = (EthII_Hdr_t *) ((u8 *) &pRxPkt->eth803_hdr + sizeof(pRxPkt->eth803_hdr) + sizeof(pRxPkt->rfc1042_hdr) - sizeof(pRxPkt->eth803_hdr.dest_addr) - sizeof(pRxPkt->eth803_hdr.src_addr) - sizeof(pRxPkt->rfc1042_hdr.snap_type)); rt_memcpy(pEthHdr->src_addr, pRxPkt->eth803_hdr.src_addr, sizeof(pEthHdr->src_addr)); rt_memcpy(pEthHdr->dest_addr, pRxPkt->eth803_hdr.dest_addr, sizeof(pEthHdr->dest_addr)); /* Chop off the RxPD + the excess memory from the 802.2/llc/snap header * that was removed */ hdrChop = (u8 *) pEthHdr - (u8 *) pRxPD; } else { hexdump("RX Data: LLC/SNAP", (u8 *) &pRxPkt->rfc1042_hdr, sizeof(pRxPkt->rfc1042_hdr)); /* Chop off the RxPD */ hdrChop = (u8 *) &pRxPkt->eth803_hdr - (u8 *) pRxPD; rt_free(RxNode); } /* Chop off the leading header bytes so the skb points to the start of * either the reconstructed EthII frame or the 802.2/llc/snap frame */ pbuflen = (len - hdrChop); if (pbuflen < 100) pbuflen = 100; p = pbuf_alloc(PBUF_LINK, pbuflen, PBUF_RAM); if (p == RT_NULL) { WlanDebug(WlanErr,"alloc pbuf failed length %d",pbuflen); rt_free(RxNode); return; } rt_memcpy(p->payload, (u8*) ((u32) pRxPD + hdrChop), pbuflen); RxNode->p = p; level = rt_hw_interrupt_disable(); if (HeadNode->next == HeadNode) { HeadNode->next = RxNode; HeadNode->pre = RxNode; RxNode->next = HeadNode; RxNode->pre = HeadNode; } else { HeadNode->pre->next = RxNode; RxNode->pre = HeadNode->pre; HeadNode->pre = RxNode; RxNode->next = HeadNode; } card->RxQueueCount++; rt_hw_interrupt_enable(level); done: return; }
rt_uint8_t del_dev(dev_t *dev) { rt_int8_t fd; rt_uint8_t i; struct stat buf; dev_t *tmpbuff = RT_NULL; dev_t tmp; rt_memset(&tmp,0,sizeof(dev_t)); rt_memset(&buf,0,sizeof(buf)); if((fd = open(DEV, DFS_O_CREAT|DFS_O_RDWR,0)<0)) { rt_kprintf("%s:%d Open %s failed\n",__func__,__LINE__,DEV); return FAILE; } rt_kprintf("%s:%d Open %s fd = %d\n",__func__,__LINE__,DEV,fd); stat(DEV, &buf); rt_kprintf("%s,%d: buf.st_size = %d\n", __func__,__LINE__,buf.st_size); rt_kprintf("%s:%d device amc: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x \n",__func__,__LINE__, dev->mac[0],dev->mac[1],dev->mac[2],dev->mac[3],dev->mac[4],dev->mac[5],dev->mac[6],dev->mac[7]); if(buf.st_size>0) { tmpbuff = (dev_t*)rt_malloc(buf.st_size); rt_memset(tmpbuff,0,buf.st_size); if(0==tmpbuff) { rt_kprintf("%s:%d Memory allocation failure\n",__func__,__LINE__); goto failed; } if(read(fd,tmpbuff,buf.st_size)<buf.st_size) { rt_kprintf("%s:%d Read %s error\n",__func__,__LINE__,DEV); goto failed; } close(fd); if((fd = open(DEV, DFS_O_CREAT|DFS_O_RDWR|DFS_O_TRUNC,0)<0)) { rt_kprintf("%s:%d Open %s failed\n",__func__,__LINE__,DEV); return FAILE; } rt_kprintf("%s:%d Open %s fd = %d\n",__func__,__LINE__,DEV,fd); for(i=0;i<(buf.st_size/sizeof(dev_t));i++) { rt_kprintf("%s:%d device amc: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x \n",__func__,__LINE__, (tmpbuff+i)->mac[0],(tmpbuff+i)->mac[1],(tmpbuff+i)->mac[2],(tmpbuff+i)->mac[3],(tmpbuff+i)->mac[4],(tmpbuff+i)->mac[5],(tmpbuff+i)->mac[6],(tmpbuff+i)->mac[7]); if(rt_memcmp((tmpbuff+i)->mac,dev->mac,MAC_LEN)==0) { continue; } write(fd,tmpbuff+i,sizeof(dev_t)); } goto success; } goto failed; success: if(tmpbuff) rt_free(tmpbuff); close(fd); return SUCCESS; failed: if(tmpbuff) rt_free(tmpbuff); close(fd); return FAILE; }
rt_uint8_t add_dev(dev_t *dev) { rt_int8_t fd; rt_uint8_t i; struct stat buf; memset(&buf,0,sizeof(buf)); dev_t *tmpbuff = RT_NULL; if((fd=open(DEV,DFS_O_CREAT|DFS_O_RDWR,0))<0) { rt_kprintf("%s:%d Open %s failed\n",__func__,__LINE__,DEV); return FAILE; } rt_kprintf("%s:%d Open %s fd = %d\n",__func__,__LINE__,DEV,fd); stat(DEV, &buf); if(buf.st_size>0) { tmpbuff = (dev_t*)rt_malloc(buf.st_size); if(0==tmpbuff) { rt_kprintf("%s:%s Memory allocation failure\n",__func__,__LINE__); goto failed; } if(read(fd,tmpbuff,buf.st_size)<buf.st_size) { rt_kprintf("%s:%d Read %s error\n",__func__,__LINE__,DEV); goto failed; } for(i=0;i<(buf.st_size/sizeof(dev_t));i++) { if(rt_memcmp((tmpbuff+i)->mac,dev->mac,MAC_LEN)==0) { rt_kprintf("%s:%d Device existed\n",__func__,__LINE__); goto exist; } } } if(buf.st_size>0) { lseek(fd,0,SEEK_END); } if(write(fd,dev,sizeof(dev_t))<sizeof(dev_t)) { rt_kprintf("%s:%d Write %s error\n",__func__,__LINE__,DEV); goto failed; } goto success; success: if(!tmpbuff) rt_free(tmpbuff); close(fd); return SUCCESS; failed: if(tmpbuff) rt_free(tmpbuff); close(fd); return FAILE; exist: if(tmpbuff) rt_free(tmpbuff); close(fd); return EXIST; }
rt_uint8_t change_shortadd_dev(dev_t *dev) { rt_int8_t fd; rt_uint8_t i; rt_uint8_t tmpmac[8] = {0}; struct stat buf; dev_t *tmpbuff = RT_NULL; if((fd = open(DEV, O_RDWR|O_CREAT,0)<0)) { rt_kprintf("%s:%d Open %s failed\n",__func__,__LINE__,DEV); return FAILE; } rt_kprintf("%s:%d Open %s fd = %d\n",__func__,__LINE__,DEV,fd); stat(DEV, &buf); if(buf.st_size>0) { tmpbuff = (dev_t*)rt_malloc(buf.st_size); if(0==tmpbuff) { rt_kprintf("%s:%d Memory allocation failure\n",__func__,__LINE__); goto failed; } if(read(fd,tmpbuff,buf.st_size)<buf.st_size) { rt_kprintf("%s:%d Read %s error\n",__func__,__LINE__,DEV); goto failed; } for(i=0;i<(buf.st_size/sizeof(dev_t));i++) { if(rt_memcmp((tmpbuff+i)->mac,dev->mac,MAC_LEN)==0) { (tmpbuff+i)->shortadd=dev->shortadd; // (tmpbuff+i)->stat = dev->stat; lseek(fd,0,DFS_SEEK_SET); if(write(fd,tmpbuff,buf.st_size)<0) { goto failed; } rt_kprintf("%s:%d device amc: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x shortadd = %04x\n",__func__,__LINE__, dev->mac[0],dev->mac[1],dev->mac[2],dev->mac[3],dev->mac[4],dev->mac[5],dev->mac[6],dev->mac[7],dev->shortadd); goto success; } if(rt_memcmp(dev->mac,tmpmac,MAC_LEN)==0 && (tmpbuff+i)->shortadd==dev->shortadd) { (tmpbuff+i)->shortadd=0xffff;; (tmpbuff+i)->stat = dev->stat; lseek(fd,0,DFS_SEEK_SET); if(write(fd,tmpbuff,buf.st_size)<0) { goto failed; } goto success; } } } // lseek(fd,0,DFS_SEEK_END); rt_kprintf("%s:%d device amc: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x shortadd = %04x\n",__func__,__LINE__, dev->mac[0],dev->mac[1],dev->mac[2],dev->mac[3],dev->mac[4],dev->mac[5],dev->mac[6],dev->mac[7],dev->shortadd); if(write(fd,dev,sizeof(dev_t))<0) { goto failed; } success: if(tmpbuff) rt_free(tmpbuff); close(fd); return SUCCESS; failed: if(tmpbuff) rt_free(tmpbuff); close(fd); return FAILE; }