//外网模式下,服务器在线测试 USER_FUNC void pingTest(void *arg) { while (getPingServerThreadFlag()) { sleep(5); pingServer(); sleep(120); u_printf("check ping result"); if (getPingResultFlag()) { //if ping result is correct setPingResultFlag(0); } else { //若ping服务器无反应,则释放资源并让主板重新上线 resetPingData(); //关闭socket连接 closeSocket(getSocketFd()); sleep(10); //10s后主板开始重新上线 initPingData(); connectServer(); loginServer(); if (hfthread_create(recvTest, "RECV_THREAD", 256, NULL,HFTHREAD_PRIORITIES_LOW, NULL, NULL) != HF_SUCCESS) { u_printf("create recvMessage thread fail\n"); } if (hfthread_create(pingTest, "PING_THREAD", 256, NULL,HFTHREAD_PRIORITIES_LOW, NULL, NULL) != HF_SUCCESS) { u_printf("create pingServer thread fail\n"); } } } }
USER_FUNC void test_thread_and_timer_start(void) { if(hfthread_mutext_new(&test_lock)!=0) { HF_Debug(DEBUG_LEVEL,"create mutex fail\n"); return; } hfthread_create(test_thread_func,"app_main_test",256,(void*)1,1,NULL,NULL); //hfthread_create(test_thread_func,"app_main_test1",256,(void*)2,1,NULL,NULL); }
int USER_FUNC app_main (void) { time_t now=time(NULL); HF_Debug(DEBUG_LEVEL,"sdk version(%s),the app_main start time is %d %s\n",hfsys_get_sdk_version(),now,ctime(&now)); if(hfgpio_fmap_check()!=0) { while(1) { HF_Debug(DEBUG_ERROR,"gpio map file error\n"); msleep(1000); } return 0; } show_reset_reason(); if(hfsys_register_system_event((hfsys_event_callback_t)hfsys_event_callback)!=HF_SUCCESS) { u_printf("register system event fail\n"); } while(!hfnet_wifi_is_active()) { msleep(50); } if(hfnet_start_assis(ASSIS_PORT)!=HF_SUCCESS) { HF_Debug(DEBUG_WARN,"start httpd fail\n"); } if(hfnet_start_uart(HFTHREAD_PRIORITIES_LOW,(hfnet_callback_t)uart_recv_callback)!=HF_SUCCESS) { HF_Debug(DEBUG_WARN,"start uart fail!\n"); } if(hfnet_start_socketa(HFTHREAD_PRIORITIES_LOW,(hfnet_callback_t)socketa_recv_callback)!=HF_SUCCESS) { HF_Debug(DEBUG_WARN,"start socketa fail\n"); } if(hfnet_start_socketb(HFTHREAD_PRIORITIES_LOW,(hfnet_callback_t)socketb_recv_callback)!=HF_SUCCESS) { HF_Debug(DEBUG_WARN,"start socketb fail\n"); } if(hfnet_start_httpd(HFTHREAD_PRIORITIES_MID)!=HF_SUCCESS) { HF_Debug(DEBUG_WARN,"start httpd fail\n"); } hfthread_create(wifi_scan, "scan thread", 256, NULL, HFTHREAD_PRIORITIES_LOW, NULL,NULL); return 1; }
USER_FUNC void test_thread_and_timer_start(void) { if(hfthread_mutext_new(&test_lock)!=0) { HF_Debug(DEBUG_LEVEL,"create mutex fail\n"); return; } //创建一个自动定时器,每1s钟触发一次。 if((test_timer = hftimer_create("TEST-TIMER",1000,true,TEST_TIMER_ID,test_timer_callback1,0))==NULL) { u_printf("create timer fail\n"); } test_msgq = hfmsgq_create(10,4); if(test_msgq==NULL) { u_printf("create msgq fail\n"); } hfthread_create(test_thread_func,"app_main_test",256,(void*)1,1,NULL,NULL); //hfthread_create(test_thread_func,"app_main_test1",256,(void*)2,1,NULL,NULL); }
int USER_FUNC app_main (void) { time_t now=time(NULL); HF_Debug(DEBUG_LEVEL,"sdk version(%s),the app_main start time is %d %s[AT DEMO]\n",hfsys_get_sdk_version(),now,ctime(&now)); if(hfgpio_fmap_check()!=0) { while(1) { HF_Debug(DEBUG_ERROR,"gpio map file error\n"); msleep(1000); } } while(!hfnet_wifi_is_active()) { msleep(50); } if(hfnet_start_assis(ASSIS_PORT)!=HF_SUCCESS) { HF_Debug(DEBUG_WARN,"start httpd fail\n"); } if(hfnet_start_uart(HFTHREAD_PRIORITIES_LOW,(hfnet_callback_t)uart_recv_callback)!=HF_SUCCESS) { HF_Debug(DEBUG_WARN,"start uart fail!\n"); } parse_audio_image_file(); if(hfgpio_configure_fpin_interrupt(HFGPIO_F_PLAY_AUDIO_KEY,HFPIO_IT_EDGE,play_audio_key_press,1)!=HF_SUCCESS) { u_printf("configure HFGPIO_F_PLAY_AUDIO_KEY fail\n"); return 0; } hfthread_create(audio_thread_func,"audio_output",256,(void*)1,1,NULL,NULL); return 1; }
//外网模式接收测试 USER_FUNC void recvTest(void *arg) { if (hfthread_create(fdthread, "feed", 256, NULL, HFTHREAD_PRIORITIES_LOW, NULL, NULL) != HF_SUCCESS) //创建喂食线程 { u_printf("create feed thread fail\n"); } if (hfthread_create(wtthread, "water", 256, NULL, HFTHREAD_PRIORITIES_LOW, NULL, NULL) != HF_SUCCESS) //创建喂水线程 { u_printf("create water thread fail\n"); } if (hfthread_create(time_fdthread, "timed_fd", 256, NULL, HFTHREAD_PRIORITIES_LOW, NULL, NULL) != HF_SUCCESS) //创建定时线程 { u_printf("create timed_fd thread fail\n"); } while (getMessageThreadFlag()) { sleep(1); ///因为设备在连接服务器过程中此线程一直在循环,实际使用时需要删除这个时延 u_printf("Enter while!\n"); int msgType = recvMessage(); if (msgType == MESSAGE_FLAG) { //如果收到的是控制指令 // u_printf("recv-> %s",getCurrentMsg()); // sendMessage(getCurrentUser(),getCurrentMsg()); /** *第一步:调用getCurrentMsg()函数,获取收到的指令字符串,该函数返回字符数组(char*类型) *第二步:根据需要解析字符串得到有效指令码,并发到底层进行控制 *第三步:控制结束后,反馈的信息通过sendMessage(user,msg)函数发出,该函数需要传入两个参数 * user参数通过调用getCurrentUser()获得,msg参数即为底层要发给客户端的反馈信息(字符串类型) * *示例:char* codeStr = getCurrentMsg(); * char* code = 指令解析函数(); * 进行底层控制,并得到反馈字符串; * sendMessage(getCurrentUser(),"反馈字符串"); * */ u_printf("recv control cmd!\n"); memset(recvbuf, 0, sizeof(recvbuf)); char* str = getCurrentMsg(); u_printf("The client has received a message: %s\n", str); strcpy(recvbuf, str); // sendto(udp_fd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr *)&address, sizeof(address)); p = strtok(recvbuf, "#"); n = 0; while(p) { cmd[n] = p; n++; p = strtok(NULL, "#"); } u_printf("cmd[1] is :%s\n", cmd[1]); if (strncmp(cmd[0],"action",sizeof("action"))==0) //控制喂水、喂食,指令格式:action#1,10 { q = strtok(cmd[1], ","); j = 0; while(q) { actcmd[j] = q; j++; q = strtok(NULL, ","); } u_printf("actcmd[0] is :%s\n", actcmd[0]); if (strncmp(actcmd[0],"1",sizeof("1"))==0) { if (fdstate == 0) { fdstate = 1; //将喂食状态改为1,触发喂食线程 } else { sendMessage(getCurrentUser(), "fdbusy"); u_printf("Warning: fdbusy!\n"); } } else if (strncmp(actcmd[0],"0",sizeof("0"))==0) { if (wtstate == 0) { wtstate = 1; //将喂水状态改为1,触发喂水线程 } else { sendMessage(getCurrentUser(), "wtbusy"); u_printf("Warning: wtbusy!\n"); } } } else if(strncmp(cmd[0],"set",sizeof("set"))==0) //定时设置(添加和修改) { memset(read_flashbuf, 0, sizeof(read_flashbuf)); hfuflash_read(0, read_flashbuf, sizeof(read_flashbuf)); if (read_flashbuf[1] == ',') ///用户flash中有定时设置 { ///第一步:竖线分割flash中时间值,存于指针数组flash_set_str中 p_str = strtok(read_flashbuf, "|"); n_str = 0; while(p_str) { flash_set_str[n_str] = p_str; n_str++; p_str = strtok(NULL, "|"); } ///第二步:取flash_set_str值中标识与cmd[1]进行比较 k_str = n_str - 1; while(k_str >= 0) { memset(tmp_flash_strcmp, 0, sizeof(tmp_flash_strcmp)); strcpy(tmp_flash_strcmp, flash_set_str[k_str]); ///strcpy慎用 u_printf("The tmp_flash_strcmp is: %s\n", tmp_flash_strcmp); memset(tmp_cmd_strcmp, 0, sizeof(tmp_cmd_strcmp)); strcpy(tmp_cmd_strcmp, cmd[1]); u_printf("The tmp_cmd_strcmp is: %s\n", tmp_cmd_strcmp); if (strncmp(strtok(tmp_cmd_strcmp, ","), strtok(tmp_flash_strcmp, ","), 1) == 0) ///比较时间的标识 { // strcpy(flash_set_str[k_str], cmd[1]); flash_set_str[k_str] = cmd[1]; break; } k_str--; } ///第三步:连接字符串后放到store_flashbuf中 memset(store_flashbuf, 0, sizeof(store_flashbuf)); if (k_str < 0) //flash中没有该标识,即表示添加 { c_str = 0; while(c_str < n_str) { strcat(store_flashbuf, flash_set_str[c_str]); strcat(store_flashbuf, vertLine); c_str++; } strcat(store_flashbuf, cmd[1]); } else //修改某个标识的设置 { c_str = 0; while(c_str < n_str) { strcat(store_flashbuf, flash_set_str[c_str]); if (c_str < (n_str - 1)) strcat(store_flashbuf, vertLine); c_str++; } } } else ///flash中为空 { memset(store_flashbuf, 0, sizeof(store_flashbuf)); strcat(store_flashbuf, cmd[1]); } ///第四步:存储到用户flash中 hfuflash_erase_page(0, 1); hfuflash_write(0, store_flashbuf, sizeof(store_flashbuf)); u_printf("We have stored the store_flashbuf: %s\n", store_flashbuf); sendto(udp_fd, store_flashbuf, sizeof(store_flashbuf), 0, (struct sockaddr *)&address, sizeof(address)); sendMessage(getCurrentUser(),"control success"); } else if(strncmp(cmd[0], "cancel", sizeof("cancel")) == 0) //取消定时设置 { memset(read_flashbuf, 0, sizeof(read_flashbuf)); hfuflash_read(0, read_flashbuf, sizeof(read_flashbuf)); if (read_flashbuf[1] == ',') ///用户flash中有定时设置 { ///第一步:竖线分割flash中时间值,存于指针数组flash_set_str中 p_str = strtok(read_flashbuf, "|"); n_str = 0; while(p_str) { flash_set_str[n_str] = p_str; n_str++; p_str = strtok(NULL, "|"); } ///第二步:取flash_set_str值中标识与cmd[1]进行比较 k_str = 0; while(k_str < n_str) { memset(tmp_flash_strcmp, 0, sizeof(tmp_flash_strcmp)); strcpy(tmp_flash_strcmp, flash_set_str[k_str]); if (strncmp(strtok(tmp_flash_strcmp, ","), cmd[1], 1) == 0) { break; } k_str++; } ///第三步:flash_set_str[k_str]即为要删除的定时字符串,连接其余的放到store_flashbuf中 c_str = 0; memset(store_flashbuf, 0, sizeof(store_flashbuf)); while(c_str < n_str) { if(c_str == k_str) { c_str++; continue; } if (k_str < (n_str -1)) { strcat(store_flashbuf, flash_set_str[c_str]); if (c_str < (n_str - 1)) strcat(store_flashbuf, vertLine); c_str++; } else { strcat(store_flashbuf, flash_set_str[c_str]); if (c_str < (n_str - 2)) strcat(store_flashbuf, vertLine); c_str++; } } ///第四步:存储到用户flash中 hfuflash_erase_page(0,1); hfuflash_write(0, store_flashbuf, sizeof(store_flashbuf)); u_printf("We have stored the store_flashbuf: %s\n", store_flashbuf); sendto(udp_fd, store_flashbuf, sizeof(store_flashbuf), 0, (struct sockaddr *)&address, sizeof(address)); sendMessage(getCurrentUser(),"control success"); } } else if (strncmp(cmd[0],"flashMemory",sizeof("flashMemory"))==0) //读取内存中设定的时间值 { memset(read_flashbuf, 0, sizeof(read_flashbuf)); hfuflash_read(0, read_flashbuf, sizeof(read_flashbuf)); u_printf("The time setting is: %s\n", read_flashbuf); sendto(udp_fd, read_flashbuf, sizeof(read_flashbuf), 0, (struct sockaddr *)&address, sizeof(address)); sendMessage(getCurrentUser(), read_flashbuf); } // else if (strncmp(cmd[0],"clearAll",sizeof("clearAll"))==0) //清空内存中所有设定的时间值 // { // hfuflash_erase_page(0,1); // sendMessage(getCurrentUser(),"control success"); // } } } }
//主函数 int app_main(void) { time_t now = time(NULL); hfdbg_set_level(3); HF_Debug(DEBUG_LEVEL,"[FILE DEMO]sdk version(%s),the app_main start time is %d %s\n",hfsys_get_sdk_version(), now, ctime(&now)); if (hfgpio_fmap_check(HFM_TYPE_LPB100) != 0) { while (1) { HF_Debug(DEBUG_ERROR, "gpio map file error\n"); msleep(1000); } //return 0; } while (!hfnet_wifi_is_active()) { msleep(50); } if (hfnet_start_assis(ASSIS_PORT) != HF_SUCCESS) { HF_Debug(DEBUG_WARN, "start httpd fail\n"); } if (hfnet_start_httpd(HFTHREAD_PRIORITIES_MID) != HF_SUCCESS) { HF_Debug(DEBUG_WARN, "start httpd fail\n"); } #ifndef TEST_UART_SELECT // if (hfnet_start_uart(HFTHREAD_PRIORITIES_LOW, NULL) != HF_SUCCESS) // { // HF_Debug(DEBUG_WARN, "start uart fail!\n"); // } #endif sleep(2); //创建一个udp方便调试程序 if((udp_fd = socket(AF_INET,SOCK_DGRAM,0)) == -1) { u_printf("create socket error!\n"); exit(1); } bzero(&address,sizeof(address)); address.sin_family=AF_INET; address.sin_addr.s_addr=inet_addr("115.28.179.114"); address.sin_port=htons(8123); //在主函数中打开串口 if ((huart = hfuart_open(0))<0) { u_printf("open_port error!"); exit(1); } /*内网模式:初始化相关数据,开启udp服务,开启接收线程*/ /* //初始化udp相关数据 initUdpAddrData(); //开启udp服务 startUdpServer(); //创建一个线程接收udp数据 if (hfthread_create(udpRecvTest, "RECV_THREAD", 256, NULL,HFTHREAD_PRIORITIES_LOW, NULL, NULL) != HF_SUCCESS) { u_printf("create udpRecv thread fail\n"); return 0; } */ /*外网模式:初始化相关数据,并连接登陆服务器,开启相关线程*/ //初始化数据 initData(); //连接服务器 connectServer(); //登陆服务器 loginServer(); //创建一个线程接收数据 if (hfthread_create(recvTest, "RECV_THREAD", 256, NULL,HFTHREAD_PRIORITIES_LOW, NULL, NULL) != HF_SUCCESS) { u_printf("create recvMessage thread fail\n"); return 0; } //创建一个线程监测主板的在线状态 if (hfthread_create(pingTest, "RECON_THREAD", 256, NULL,HFTHREAD_PRIORITIES_LOW, NULL, NULL) != HF_SUCCESS) { u_printf("create pingServer thread fail\n"); return 0; } return 1; }