int ATZ(int fd) //wifi模块重启 { char cmdbuf[100]="AT+Z\r"; char readbuf[100]; char tmp[50]; int pollret; wflag = write(fd, cmdbuf, strlen((char*)cmdbuf)); usleep(10000); pollret = poll_read(fd, readbuf, 50); if (pollret == -1) return -1;//something must be wrong //read(fd,readbuf,50); //printf("1 %d\n", strlen((char *)readbuf)); strncpy(tmp, (char *)readbuf, (strlen((char *)readbuf))); //printf("1 %d\n", strlen(tmp)); //printf("2 %d\n", strlen(para)); printf("ATZ%s\n", tmp); init_string(readbuf); return 0; if(wflag > 0) { //printf("%d\n", strlen((char*)cmdbuf)); printf("WIFI SET: %s\n",cmdbuf); } //usleep(7000000);//手册说明重启需要6s }
int timed_readn(int fd, void * cbuf, size_t len, int flags, int timeout) { int left; int nread; char * buf = (char *)cbuf; left = (int)len; while (left>0) { if (poll_read(fd, timeout)!=1) break; nread = recv(fd, buf, (size_t)left, flags); if (nread==-1) { if (errno==EINTR) nread = 0;/* call recv() again */ else return -1; } else if (nread==0) { /* EOF */ errno = 0; break; } left -= nread; buf += nread; } return (int)len - left; }
int timed_read(int fd, void * buf, size_t len, int flags, int timeout) { if (poll_read(fd, timeout)!=1) return -1; return recv(fd, buf, len, flags); }
int WMODE(int fd, char *para)//3 //wifi模块设置为STA模式 { char cmdbuf[100]="AT+WMODE"; //STA\r"; char ret = '\r'; //char ok[] = "+ok"; char readbuf[100]; char *pchar; int pollret; char *tmpstring; init_string(readbuf); wflag = write(fd, cmdbuf, strlen((char*)cmdbuf)); wflag = write(fd, &ret, 1); usleep(10000); pollret = poll_read(fd, readbuf, 50); if (pollret == -1) return -1;//something must be wrong //printf("readbuf lenth is %d \n",strlen(readbuf)); tmpstring = strchr(readbuf, '\r'); *tmpstring = '\0'; //printf("readbuf lenth is %d \n",strlen(readbuf)); printf("%s\n", readbuf); pchar = strstr((char *)readbuf, para); if (pchar != NULL) return 1; // network parameter unchanged init_string(readbuf); strcat(cmdbuf,"="); strcat(cmdbuf,para); strcat(cmdbuf,"\r"); wflag = write(fd, cmdbuf, strlen((char*)cmdbuf) + 1); usleep(10000); pollret = poll_read(fd, readbuf, 50); if (pollret == -1) return -1;//something must be wrong tmpstring = strchr(readbuf, '\r'); *tmpstring = '\0'; printf("%s\n", readbuf); //init_string(readbuf); return 0; /* pchar = strstr((char *)readbuf, ok); if (pchar != NULL){ printf("%s\n", ok); return 0; } if(wflag > 0) { printf("WIFI SET: %s\n",cmdbuf); } */ }
int WSKEY(int fd, char *para) //STA模式下查询或设置wifi模块的加密和连接密码 { //printf("enter wskey"); char cmdbuf[100]="AT+WSKEY"; //WPA2PSK,AES,abc123456\r"; char ret = '\r'; char *tmpstring; char readbuf[100]; char *pchar; int pollret; init_string(readbuf); wflag = write(fd, cmdbuf, strlen((char*)cmdbuf)); wflag = write(fd, &ret, 1); usleep(100000); pollret = poll_read(fd, readbuf, 50); if (pollret == -1) return -1;//something must be wrong //usleep(400000); //read(fd,readbuf,50); tmpstring = strchr(readbuf, '\r'); *tmpstring = '\0'; //strncpy(tmp, (char *)readbuf, (strlen((char *)readbuf))); //printf("1 %d\n", strlen(tmp)); //printf("2 %d\n", strlen(para)); printf("%s\n", readbuf); pchar = strstr(readbuf, para); if (pchar != NULL) return 1;/// network parameter unchanged init_string(readbuf); strcat(cmdbuf,"="); strcat(cmdbuf,para); strcat(cmdbuf,"\r"); wflag = write(fd, cmdbuf, strlen((char*)cmdbuf) + 1); usleep(100000); pollret = poll_read(fd, readbuf, 50); if (pollret == -1) return -1;//something must be wrong //usleep(400000); //read(fd,readbuf,50); tmpstring = strchr(readbuf, '\r'); *tmpstring = '\0'; printf("%s\n", readbuf); return 0;//should not get there }
int EnterAT(int fd){//1 char cmdbuf[] = "+++"; char a = 'a'; char readbuf[20]; int pollret; char *tmpstring; init_string(readbuf); wflag = write(fd, cmdbuf, strlen((char*)cmdbuf)); usleep(10000); pollret = poll_read(fd, readbuf, 10); if (pollret == -1) return -1;//need reset wifi module printf("%s ", readbuf);//should print a init_string(readbuf); wflag = write(fd, &a, 1); usleep(100000); pollret = poll_read(fd, readbuf, 10); if (pollret == -1) return -1;//need reset wifi module tmpstring = strchr(readbuf, '\r'); *tmpstring = '\0'; printf("%s\n", readbuf);//should print +ok //init_string(readbuf); return 0; }
int NETP(int fd, char *para) //设置要连接到的内网服务器IP和端口号 { //printf("enter netp \n"); //printf("para is %s\n", para); char cmdbuf[100]="AT+NETP"; //TCP,CLIENT,8899,192.168.1.101\r"; char ret = '\r'; char readbuf[100]; char *pchar; char *tmpstring; int pollret; init_string(readbuf); wflag = write(fd, cmdbuf, strlen((char*)cmdbuf)); wflag = write(fd, &ret, 1); usleep(100000); pollret = poll_read(fd, readbuf, 50); if (pollret == -1) return -1;//something must be wrong //printf("readbuf length is %d\n", strlen(readbuf)); //printf("netp is %s\n", readbuf); tmpstring = strchr(readbuf, '\r'); *tmpstring = '\0'; printf("%s\n", readbuf); pchar = strstr(readbuf, para); if (pchar != NULL) return 1;// network parameter unchanged init_string(readbuf); strcat(cmdbuf,"="); strcat(cmdbuf,para); strcat(cmdbuf,"\r"); wflag = write(fd, cmdbuf, strlen((char*)cmdbuf) + 1); usleep(100000); pollret = poll_read(fd, readbuf, 50); if (pollret == -1) return -1;//something must be wrong tmpstring = strchr(readbuf, '\r'); *tmpstring = '\0'; printf("%s\n", readbuf); return 0; }
int WSSSID(int fd, char *para)//4 //查询或设置wifi模块的SSSID { char cmdbuf[100]="AT+WSSSID"; //test\r"; char ret = '\r'; char readbuf[100]; init_string(readbuf); char *pchar; int pollret; char *tmpstring; wflag = write(fd, cmdbuf, strlen((char*)cmdbuf)); wflag = write(fd, &ret, 1); usleep(1000000); pollret = poll_read(fd, readbuf, 50); if (pollret == -1) return -1;//something must be wrong tmpstring = strchr(readbuf, '\r'); *tmpstring = '\0'; printf("%s\n", readbuf); pchar = strstr((char *)readbuf, para); if (pchar != NULL){ pchar = strchr(readbuf, '='); pchar++; if (strcmp(pchar, para) == 0) return 1; //*para isn't substring of *pchar and unchanged } init_string(readbuf); strcat(cmdbuf,"="); strcat(cmdbuf,para); strcat(cmdbuf,"\r"); wflag = write(fd, cmdbuf, strlen((char*)cmdbuf) + 1); usleep(100000); pollret = poll_read(fd, readbuf, 50); if (pollret == -1) return -1;//something must be wrong tmpstring = strchr(readbuf, '\r'); *tmpstring = '\0'; printf("%s", readbuf); return 0; }
int WANN(int fd) //查询或设置WAN设置 { char cmdbuf[100]="AT+WANN\r"; char readbuf[100]; //char tmp[50]; int pollret; char *tmpstring; wflag = write(fd, cmdbuf, strlen((char*)cmdbuf) + 1); usleep(10000); pollret = poll_read(fd, readbuf, 80); if (pollret == -1) return -1;//something must be wrong tmpstring = strchr(readbuf, '\r'); *tmpstring = '\0'; printf("%s\n", readbuf); //init_string(readbuf); //strncpy(tmp, (char *)readbuf, (strlen((char *)readbuf))); //printf("%s\n", tmp); return 0; // should not get there }
int stop_return(int fd){//2 char cmdbuf[20]="AT+E\r"; char readbuf[20]; int pollret; char *tmpstring; init_string(readbuf); wflag = write(fd, cmdbuf, strlen((char*)cmdbuf) + 1); usleep(100000); pollret = poll_read(fd, readbuf, 10); //read(fd,readbuf,20); if (pollret == -1) return -1;//something must be wrong //tmpstring = strchr(readbuf, '\n'); //*tmpstring = '\0'; tmpstring = strchr(readbuf, '\r'); *tmpstring = '\0'; printf("%s\n", readbuf); //init_string(readbuf); return 0;//should print +ok }
int try_receive_something(int fd, int *data, int len) { int err = 0; struct xdma6_buffer_info buf; poll_read(fd,10); buf.flags = xdma_DEV_TO_MEM; err = ioctl(fd, XDMA_REQ_BUFFER, &buf); if(err) { printf("ERROR: deq_buffer\n"); return 1; } memcpy((char*)data,buf.data,len*sizeof(int)); printf("-> GOT BUF no.%d: %d %d %d %d %d ...\n", buf.kp_data, data[0], data[1], data[2], data[3], data[4]); buf.flags = xdma_DEV_TO_MEM; err = ioctl(fd, XDMA_ENQ_BUFFER, &buf); if(err) { printf("ERROR: enq_buffer\n"); return 1; } return err; }
int ENTM(int fd) //进入透传模式 { char cmdbuf[100]="AT+ENTM\r"; char readbuf[100]; char tmp[50]; int pollret; wflag = write(fd, cmdbuf, strlen((char*)cmdbuf) + 1); usleep(10000); pollret = poll_read(fd, readbuf, 50); if (pollret == -1) return -1;//something must be wrong //read(fd,readbuf,50); strncpy(tmp, (char *)readbuf, (strlen((char *)readbuf))); printf("ENTM%s\n", tmp); init_string(readbuf); return 0; if(wflag > 0 ) { printf("WIFI SET: %s\n",cmdbuf); } }
static int std_read(connection_t *conn, char *buf, int len, int timeout) { int fd; int result; int retry = 3; int poll_result; int poll_timeout; if (! conn) { return -1; } fd = conn->fd; if (fd <= 0 || conn->is_read_shutdown) { return -1; } if (timeout >= 0) { poll_timeout = timeout; } else { poll_timeout = conn->socket_timeout; } if (conn->is_recv_timeout) { if (conn->recv_timeout != poll_timeout) { conn->recv_timeout = poll_timeout; resin_tcp_set_recv_timeout(conn, poll_timeout); } } else { poll_result = poll_read(fd, poll_timeout); if (poll_result <= 0) { return calculate_poll_result(conn, poll_result); } } do { /* recv returns 0 on end of file */ result = recv(fd, buf, len, 0); // fprintf(stderr, "rcv %d\n", result); if (result > 0) { return result; } else if (result == 0) { /* recv returns 0 on end of file */ return -1; } if (errno == EINTR) { /* EAGAIN is returned by a timeout */ poll_result = poll_read(fd, conn->socket_timeout); if (poll_result <= 0) { return calculate_poll_result(conn, poll_result); } } else { return read_exception_status(conn, errno); } } while (retry-- >= 0); if (result > 0) { return result; } else if (result == 0) { return -1; } else { return read_exception_status(conn, errno); } }
int _BSP_poll_char(void) { return poll_read(); }