ESP 8266 httpワークキューの実装方法

15909 ワード

>              ,  8266            o(╯□╰)o。   ,          。     , HTTP        ,             ,         。          。

    app_net_work.c     app_net_work_init()、http_set_work(http_work_t job),     ,              。

      app_http.c     ,         ,              ,           ,               ,                  GlobalHttp.info.isworking 0,             。
/*
 * app_net_work.h
 *
 *  Created on: 2017 5 25 
 *      Author: Administrator
 */

#ifndef APP_INCLUDE_APP_NET_WORK_H_
#define APP_INCLUDE_APP_NET_WORK_H_

#include "jobs_of_net_work.h"

#define HTTP_TASK_SIZE  8

ETSTimer net_work_timer;

typedef struct{
    http_work_t     task_sequence[HTTP_TASK_SIZE];
    struct {
        uint8_t         active:4;
        uint8_t         current:3;
        uint8_t         isworking:1;
    }info;
}http_task_t;

http_task_t GlobalHttp;

int ICACHE_FLASH_ATTR app_net_work_init();
int ICACHE_FLASH_ATTR http_set_work(http_work_t job);

#endif /* APP_INCLUDE_APP_NET_WORK_H_ */
/*
 * app_net_work.c
 *
 *  Created on: 2017 5 25 
 *      Author: Administrator
 */
#include "includes.h"
#include "client.h"
#include "app_net_work.h"
#include "app_http.h"


//       
int ICACHE_FLASH_ATTR http_set_work(http_work_t job){

    if ((GlobalHttp.info.current + 1) % HTTP_TASK_SIZE == GlobalHttp.info.active)
    {
        os_printf("\r
In queue error,may be full.\r
"
); return FALSE; } // , GlobalHttp.task_sequence[GlobalHttp.info.current] = job; // GlobalHttp.info.current = (GlobalHttp.info.current + 1) % HTTP_TASK_SIZE; // +1 os_printf("\r
IN queue:%d\r
"
,job); return TRUE; } // int ICACHE_FLASH_ATTR ScanJobfn(){ http_work_t HttpJob; #ifdef HTTP_QUEUE os_printf("\r
Scan job.\r
"
); #endif if (GlobalHttp.info.active == GlobalHttp.info.current){ GlobalHttp.info.isworking = 1;// #ifdef HTTP_QUEUE os_printf("\r
empty\r
"
); #endif return FALSE; // , } if(GlobalHttp.info.isworking == 0){// GlobalHttp.info.active = (GlobalHttp.info.active + 1) % HTTP_TASK_SIZE; #ifdef HTTP_QUEUE os_printf(" --%d-",GlobalHttp.info.active); // +1, #endif if (GlobalHttp.info.active == GlobalHttp.info.current){ GlobalHttp.info.isworking = 1;// #ifdef HTTP_QUEUE os_printf("\r
. empty\r
"
); #endif return FALSE; // , } } HttpJob = GlobalHttp.task_sequence[GlobalHttp.info.active];// #ifdef HTTP_QUEUE os_printf("%d %d
"
,HttpJob,GlobalHttp.task_sequence[GlobalHttp.info.active+1]); #endif if(true == startHttpWork(HttpJob))// os_timer_disarm(&net_work_timer);// , GlobalHttp.info.isworking = 1;// , return TRUE; } int ICACHE_FLASH_ATTR app_net_work_init(){ initHttpWork(); GlobalHttp.info.isworking = 1; GlobalHttp.info.active = GlobalHttp.info.current = 0; // os_timer_setfn(&net_work_timer,ScanJobfn,NULL); os_timer_arm (&net_work_timer,3000,1); return TRUE; }
/*
 * app_http.h
 *
 *  Created on: 2017 5 25 
 *      Author: Administrator
 */

#ifndef APP_INCLUDE_APP_HTTP_H_
#define APP_INCLUDE_APP_HTTP_H_

#define POST "POST %s HTTP/1.1\r
HOST: %s\r
User-Agent: wifi/1.0\r
Content-Type:application/json;charset=UTF-8\r
Content-Length: %d\r
\r
\r
%s"
#include "hh_json.h" #include "includes.h" #include "app_net_work.h" #include "client.h" void ICACHE_FLASH_ATTR initHttpWork(); int ICACHE_FLASH_ATTR startHttpWork(http_work_t job_flag); #endif /* APP_INCLUDE_APP_HTTP_H_ */
/*
 * app_http.c
 *
 *  Created on: 2017 5 25 
 *      Author: Administrator
 */
#include "app_http.h"
#include "hh_json.h"
#include "user_main.h"
#include "string.h"
#include "driver/mqtt.h"
#include "user_mqtt.h"

extern http_task_t GlobalHttp;
ETSTimer wait_wifi_conn;

void ICACHE_FLASH_ATTR NULL_fn(){}
/*****************************************************/
/********************       **********************/

void ICACHE_FLASH_ATTR regist_recv_cb(void *arg,char *pdata,unsigned short len){
os_printf("
regist_recv_cb:%s
"
,pdata); /* */ if(success) GlobalHttp.info.isworking = 0;// isworking=0 , success 。 } void ICACHE_FLASH_ATTR login_recv_cb(void *arg,char *pdata,unsigned short len){ os_printf("
login_recv_cb
"
); /* */ if(success) GlobalHttp.info.isworking = 0; } void ICACHE_FLASH_ATTR module_ota_recv_cb(void *arg,char *pdata,unsigned short len){ /* */ if(success) GlobalHttp.info.isworking = 0; } void ICACHE_FLASH_ATTR module_ota_download_cb(void *arg,char *pdata,unsigned short len){ /* */ if(success) GlobalHttp.info.isworking = 0; } // void* user_tcp_recv_cb[HTTP_TASK_SIZE]={NULL_fn,regist_recv_cb,login_recv_cb,NULL_fn,module_ota_recv_cb,module_ota_download_cb}; /******************** ***********************/ /*****************************************************/ /********************** **********************/ /*****************************************************/ void ICACHE_FLASH_ATTR regist_recv_pack(char* buf){ /* */ } void ICACHE_FLASH_ATTR login_recv_pack(char* buf){ /* */ } void ICACHE_FLASH_ATTR module_ota_pack(char* buf){ /* */ } void ICACHE_FLASH_ATTR module_ota_download_pack(char* buf){ /* */ } void (*user_make_package[HTTP_TASK_SIZE])(char*)={NULL_fn,regist_recv_pack,login_recv_pack,NULL_fn,module_ota_pack,module_ota_download_pack}; /********************** *************************/ /*********************************************************/ int ICACHE_FLASH_ATTR startHttpWork(http_work_t job_flag) { os_printf("\r
HTTP JOB: %d\r
"
,job_flag); // (user_make_package[job_flag])(sent_buffer); os_printf("\r
%s\r
"
,sent_buffer); if(0 != espconn_connect(&user_tcp_conn[job_flag])) { os_printf("
FAIL TO start CONNECT
"
); return false; } espconn_regist_recvcb(&user_tcp_conn[job_flag],user_tcp_recv_cb[job_flag]); return true; } void ICACHE_FLASH_ATTR DSNfound(const char *name, ip_addr_t *ipaddr, void *arg){ struct ip_info ipinfo; os_printf("{\r
-------------------\r
}"
); wifi_get_ip_info(STATION_IF,&ipinfo); os_printf("{\r
-------------------\r
}"
); char textip[4]={192,168,1,209}; // ip my_tcp_station_init((struct ip_addr*)textip,&ipinfo.ip,10025);// //my_tcp_station_init(ipaddr,&ipinfo.ip,80); os_printf("{\r
-------------------\r
}"
); os_timer_disarm(&wait_wifi_conn); //wifiConnectCb(wifi_station_get_connect_status()); } void ICACHE_FLASH_ATTR startinit(){ ip_addr_t dns_addr;// , struct espconn user_con; if(wifi_station_get_connect_status() != STATION_GOT_IP) return; if(ESPCONN_OK == espconn_gethostbyname(&user_con,MY_COMPANY_DNS, &dns_addr,DSNfound)) os_timer_disarm(&wait_wifi_conn); } void ICACHE_FLASH_ATTR initHttpWork(){ os_timer_setfn(&wait_wifi_conn,startinit,NULL); os_timer_arm(&wait_wifi_conn,2000,1); os_printf("-"); }