static int USER_FUNC socketa_recv_callback(uint32_t event,char *data,uint32_t len,uint32_t buf_len) { if(event==HFNET_SOCKETA_CONNECTED) { hfnet_socketa_send("CONNECT OK",sizeof("CONNECT OK")-1,1000); } else if(event==HFNET_SOCKETA_DISCONNECTED) { hfuart_send(HFUART0,"TCP DISCONNECTED\r\n",sizeof("TCP DISCONNECTED\r\n")-1,1000); } else if(event==HFNET_SOCKETA_DATA_READY) { if(len>128) { hfnet_socketa_send("INVALID PACKET\n",sizeof("INVALID PACKET\n")-1,1000); } data[len]=0; if(strcasecmp("GPIO NLINK LOW",data)==0) { hftimer_stop(hnlink_timer); hfgpio_fset_out_high(HFGPIO_F_NLINK); } else if(strcasecmp("GPIO NLINK HIGH",data)==0) { hftimer_stop(hnlink_timer); hfgpio_fset_out_low(HFGPIO_F_NLINK); } else if(strcasecmp("GPIO NLINK FALSH",data)==0) { hftimer_start(hnlink_timer); } else { hfuart_send(HFUART0,data,len,1000); } HF_Debug(DEBUG_LEVEL_LOW,"[%d]socketa recv %d bytes data %d %c\n",event,len,buf_len,data[0]); } //返回0,HSF透传机制不再向串口发数据 return 0; }
///喂水线程 USER_FUNC void wtthread(void *arg) { while(1) { if (wtstate == 1) { u_printf("Start to water %d secs!\n", atoi(actcmd[1])); control_water[3]=(char)(atoi(actcmd[1])); if (hfuart_send(huart, control_water, sizeof(control_water), 0) > 0) { sendMessage(getCurrentUser(),"control success"); } sleep((int)(atoi(actcmd[1]) / 0.7343)); wtstate = 0; u_printf("Watering has finished!\n"); } msleep(100); } }
///喂食线程 USER_FUNC void fdthread(void *arg) { while(1) { if (fdstate == 1) { u_printf("Start to feed %d secs!\n", atoi(actcmd[1])); control_feed[3]=(char)(atoi(actcmd[1])); if (hfuart_send(huart, control_feed, sizeof(control_feed), 0) > 0) { sendMessage(getCurrentUser(),"control success"); } sleep((int)(atoi(actcmd[1]) / 0.7343)); fdstate = 0; u_printf("Feeding has finished!\n"); } msleep(100); } }
///定时线程,需要连接跳线 USER_FUNC void time_fdthread(void *arg) { while(1) { ///第一步:读时钟模块上的时间值 hfuart_send(huart, query_time_cmd, sizeof(query_time_cmd), 0); msleep(500); if (hfuart_recv(huart, recv_timebuf, sizeof(recv_timebuf), 0) > 0) { memset(time_str, 0, sizeof(time_str)); hour = BCDToInt(recv_timebuf[0]); ///将BCD码转成十进制的时 min = BCDToInt(recv_timebuf[1]); time_str[0] = (hour / 10) + 48; ///数字到字符 time_str[1] = (hour % 10) + 48; time_str[2] = ':'; time_str[3] = (min / 10) + 48; time_str[4] = (min % 10) + 48; // sendto(udp_fd, time_str, sizeof(time_str), 0, (struct sockaddr *)&address, sizeof(address)); } ///第二步:取内存中设定的时间值与当前获取的时间值进行匹配 /// 备注:每次都轮询完flash中所有的时间设置,标识1~4为喂食,标识5~8为喂水,这样set和cancel不用作修改 memset(read_timed_flashbuf, 0, sizeof(read_timed_flashbuf)); hfuflash_read(0, read_timed_flashbuf, sizeof(read_timed_flashbuf)); if (read_timed_flashbuf[1] == ',') ///如果不空 { ///2.1 分割整个竖线连接的字符串read_timed_flashbuf数组 p_timed_str = strtok(read_timed_flashbuf, "|"); n_timed = 0; while(p_timed_str) { timed_flash_set_str[n_timed] = p_timed_str; n_timed++; p_timed_str = strtok(NULL, "|"); } ///2.2 匹配时间并喂食,将需要连接的字符串标识存入存储数组中 k_timed = -1; memset(store_timed_flashbuf, 0, sizeof(store_timed_flashbuf)); while(k_timed < (n_timed - 1)) { k_timed++; memset(timed_cmpbuf, 0, sizeof(timed_cmpbuf)); strcpy(timed_cmpbuf, timed_flash_set_str[k_timed]); ///timed_flash_set_str[0 - (n_timed-1)]的格式为:1,08:30,1,3 // u_printf("timed_cmpbuf is: %s\n", timed_cmpbuf); ///以","分割timed_cmpbuf k_cmd = 0; p_cmd = strtok(timed_cmpbuf, ","); while(p_cmd) { everyTimeCmd[k_cmd] = p_cmd; k_cmd++; p_cmd = strtok(NULL, ","); } ///取everyTimeCmd[1]中时间值与时钟模块time_str比较 if (strcmp(time_str, everyTimeCmd[1]) == 0) { sendto(udp_fd, "Got it", sizeof("Got it"), 0, (struct sockaddr *)&address, sizeof(address)); //注:此处添加定时喂食、喂水的判断 control_feed[3]=(char)(atoi(everyTimeCmd[3]) * 5); hfuart_send(huart, control_feed, sizeof(control_feed), 0); if (strcmp(everyTimeCmd[2], "1") == 0) { continue; } } ///连接字符串 strcat(store_timed_flashbuf, timed_flash_set_str[k_timed]); strcat(store_timed_flashbuf, vertLine); } ///写进flash中 hfuflash_erase_page(0,1); hfuflash_write(0, store_timed_flashbuf, sizeof(store_timed_flashbuf)); sendto(udp_fd, "Only pet(A496): ", sizeof("Only pet(A496): "), 0, (struct sockaddr *)&address, sizeof(address)); sendto(udp_fd, store_timed_flashbuf, sizeof(store_timed_flashbuf), 0, (struct sockaddr *)&address, sizeof(address)); } sleep(75); ///保证每分钟轮询一次 } }
USER_FUNC void upnp_main_thread(void* arg) { int upnp_fd; uint32_t if_addr; fd_set sockfd_set; int max_fd ; struct timeval tv; char *pkt_buffer; int ret,i; char *p=0; #define UPNP_RECV_PACKET_SIZE (1024) extern int hfnet_enable_multicast(int enable); hfnet_enable_multicast(1); pkt_buffer = hfmem_malloc(UPNP_RECV_PACKET_SIZE); if(pkt_buffer==NULL) { //u_printf("no mem\n"); return; } while(1) { if((if_addr=get_if_ip_addr())==0) //µÃµ½Ä£¿éµ±Ç°ipµØÖ· kim { msleep(500); continue; } get_mac_addr(); //u_printf("kim+++ if_addr is %x\n",if_addr); upnp_fd = create_multicast_socket(if_addr); if(upnp_fd<0) return; else break; } //tcp server listenfd = socket(AF_INET, SOCK_STREAM, 0); memset((char*)&servaddr,0,sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(30001); servaddr.sin_addr.s_addr=htonl(INADDR_ANY); bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr)); listen(listenfd,10); //u_printf("tcp server bind ok!\n"); if(listenfd > upnp_fd) max_fd =listenfd; else max_fd =upnp_fd; maxi = -1; for(i=0;i<MAXCLIENT;i++) { client[i] = -1; } // while(1) { int recvsize=0; tv.tv_sec=0; tv.tv_usec=200; ////u_printf("time=%d\n",hfsys_get_time()); FD_ZERO(&sockfd_set); FD_SET(upnp_fd,&sockfd_set); FD_SET(listenfd,&sockfd_set); for(i=0;i<MAXCLIENT;i++) { if(client[i]>=0) { FD_SET(client[i],&sockfd_set); if(client[i] > max_fd) max_fd =client[i]; } } /* count++; if(count == 1000) { //u_printf("send alive package\r\n"); send_alive_packet(upnp_fd,pkt_buffer,UPNP_RECV_PACKET_SIZE,if_addr); count = 0; } */ ret=select(max_fd+1,&sockfd_set,NULL,NULL,&tv); if(ret<=0) { //send_alive_packet(upnp_fd,pkt_buffer,UPNP_RECV_PACKET_SIZE,if_addr); continue; } if(FD_ISSET(listenfd, &sockfd_set)) //s2 { clilen = sizeof(cliaddr); connfd = accept(listenfd,(struct sockaddr *)&cliaddr,&clilen); for(i=0;i<MAXCLIENT;i++) { if(client[i]<0) { client[i] = connfd; maxi = maxi+1; //u_printf("accept client socket : %d client count : %d\n",connfd,maxi+1); break; } } if(i == MAXCLIENT) { //u_printf("too many clients\r\n"); } /*FD_SET(connfd, &sockfd_set); //½«ÒÑÁ¬½Ósocket ¼ÓÈëselect if(connfd > max_fd) max_fd = connfd; */ //if(i > maxi) if(--ret<=0) continue; } for(i=0;i<=maxi;i++) //¼ì²éËùÓеÄÒÑÁ¬½ÓsocketÓÐÎÞÊý¾Ý //s3 { if(((sockfd = client[i])<0)) continue; if(FD_ISSET(sockfd, &sockfd_set)) { if((recv_num =recv(sockfd,tcpserver_buf,sizeof(tcpserver_buf),0))>0) { while(tcp_send_finish == 0) { u_printf("busy,try late again\r\n"); msleep(10); } tcp_send_finish = 0; ////u_printf("recv data bytes:%d\n %s\r\n",recv_num,tcpserver_buf); if(recv_num <400) //·ÀÖ¹Á½ÌõÏûÏ¢Õ³Á¬ { TCP_Encode(); //add tcp identification code u_printf("FW to MCU,LOCAL TCP,TCP fd is %d:\r\n%s\r\n\r\n",sockfd,tcpserver_encodedbuf); hfuart_send(HFUART0,tcpserver_encodedbuf,strlen(tcpserver_encodedbuf), 50); // if(tcp_uartfd == 0) //{ tcp_uartfd = sockfd; } else { u_printf("recv num is %d, a catastrophe.Get rid of it\r\n",recv_num); } tcp_send_finish = 1; //²âÊÔ²éѯÉ豸Óà /* if((p = strstr(tcpserver_buf,"info"))!= NULL) //ÊÖ»ú²éѯÉ豸ÐÅÏ¢ { //u_printf("send info\r\n"); write(sockfd,info,strlen(info)); } else if((p = strstr(tcpserver_buf,"channel"))!= NULL) //ÊÖ»ú²éѯ°²È«Ä£Ê½´ò¿ª¹Ø±ÕµÄ״̬ { //u_printf("send channel\r\n"); write(sockfd,channel,strlen(channel)); } */ memset(tcpserver_encodedbuf,0,400); memset(tcpserver_buf,0,400); } else { close(sockfd); //FD_CLR(sockfd, &sockfd_set); client[i] = -1; maxi=maxi -1; //u_printf("delet client socket ,client count : %d\n",maxi+1); } if(--ret <= 0) break; } } if(FD_ISSET(upnp_fd, &sockfd_set)) //s1 { struct sockaddr_in fromaddr; socklen_t sockaddr_size = sizeof(struct sockaddr_in); memset(pkt_buffer,0,UPNP_RECV_PACKET_SIZE); recvsize = recvfrom(upnp_fd, (char*)pkt_buffer, UPNP_RECV_PACKET_SIZE-4, 0,(struct sockaddr *) &fromaddr, &sockaddr_size); // //u_printf("kim+++ Broadcast from ip:%x, port:%x\n",fromaddr.sin_addr.s_addr,fromaddr.sin_port); if (recvsize < 0) { //u_printf("recv() fail\n"); } else { if(strstr(pkt_buffer,"Lucis-tech")!=NULL) { //u_printf("recv length=%d,buffer is %s\n",recvsize,pkt_buffer); if(Sync_Flag == 1) //only after mcu sync Fa info to wifi { //u_printf("test AAA\r\n"); send_msearch_rsp_packet(&fromaddr); } } } if(--ret<=0) continue; } } if(pkt_buffer!=NULL) hfmem_free(pkt_buffer); return ; }