Ejemplo n.º 1
0
static cJSON * ICACHE_FLASH_ATTR get_config_by_key(cJSON *parent, char *key) {
    uint8_t i, pre = 0;
    cJSON * jparent = parent;
    cJSON * jchild = NULL;
    char *ptmp = NULL;
    char tmpstr[50];

    for (i=0; i<os_strlen(key); ++i) {
        if (key[i] != '/') continue;
        os_bzero(tmpstr, sizeof(tmpstr));
        if (i <= pre) continue;
        os_memcpy(tmpstr, key + pre, i - pre);
        DBG_MSG("found / at %d, key: %s.\r\n", i, tmpstr);
        pre = i+1;
        jchild = cJSON_GetObjectItem(jparent, tmpstr);
        if (jchild == NULL) break;
        DBG_MSG("found cJSON item %s.\r\n", jchild->string);
        jparent = jchild;
    }
    if (pre < (os_strlen(key) - 1)) {
        // Have last element
        os_bzero(tmpstr, sizeof(tmpstr));
        os_memcpy(tmpstr, key + pre, os_strlen(key) - pre);
        DBG_MSG("last element found key: %s.\r\n", tmpstr);
        jchild = cJSON_GetObjectItem(jparent, tmpstr);
    }
    return jchild;
}
Ejemplo n.º 2
0
static void mbedtls_msg_server_step(pmbedtls_msg msg)
{
	lwIP_ASSERT(msg);

	/*to prevent memory leaks, ensure that each allocated is deleted at every handshake*/
	if (msg->psession){
		mbedtls_session_free(&msg->psession);
	}
#if defined(ESP8266_PLATFORM)
    if (msg->quiet && msg->ssl.out_buf)
    {
        mbedtls_zeroize(msg->ssl.out_buf, MBEDTLS_SSL_OUTBUFFER_LEN);
        os_free(msg->ssl.out_buf);
        msg->ssl.out_buf = NULL;
    }
#endif
	mbedtls_entropy_free(&msg->entropy);
	mbedtls_ssl_free(&msg->ssl);
	mbedtls_ssl_config_free(&msg->conf);
	mbedtls_ctr_drbg_free(&msg->ctr_drbg);

	/*New connection ensure that each initial for next handshake */
	os_bzero(msg, sizeof(mbedtls_msg));
	msg->psession = mbedtls_session_new();
	if (msg->psession){
		mbedtls_net_init(&msg->fd);
		mbedtls_ssl_init(&msg->ssl);
		mbedtls_ssl_config_init(&msg->conf);
		mbedtls_ctr_drbg_init(&msg->ctr_drbg);
		mbedtls_entropy_init(&msg->entropy);
	}	
}
Ejemplo n.º 3
0
void ICACHE_FLASH_ATTR
sp_LightPairReportResult(bool res)
{
#if 1
	os_printf("sp_LightPairReportResult %d \r\n",res);
    char data_body[300];
    os_bzero(data_body,sizeof(data_body));
    uint8 mac_sta[6] = {0};
    wifi_get_macaddr(STATION_IF, mac_sta);
    os_sprintf(data_body,"{\"device_mac\":\"%02X%02X%02X%02X%02X%02X\",\"button_mac\":\"%02X%02X%02X%02X%02X%02X\",\"result\":%d,\"path\":\"%s\"}",MAC2STR(mac_sta),MAC2STR(buttonPairingInfo.button_mac),res,PAIR_RESULT);
	//{"device_mac":"...","result":1,"path":"/device/button/pair/result"}
	if (!mesh_json_add_elem(data_body, sizeof(data_body), pair_sip, ESP_MESH_JSON_IP_ELEM_LEN)) {
    	return;
    }
    if (!mesh_json_add_elem(data_body, sizeof(data_body), pair_sport, ESP_MESH_JSON_PORT_ELEM_LEN)) {
	    return;
    }
    char* dev_mac = (char*)mesh_GetMdevMac();
    if (!mesh_json_add_elem(data_body, sizeof(data_body), dev_mac, ESP_MESH_JSON_DEV_MAC_ELEM_LEN)) {
	    return;
    }
    #if ESP_MESH_SUPPORT
	response_send_str((struct espconn*)user_GetUserPConn(),true,data_body,os_strlen(data_body),NULL,0,0,0);
	#else
	response_send_str((struct espconn*)user_GetWebPConn(),true,data_body,os_strlen(data_body),NULL,0,0,0);
	#endif
#endif
}
LOCAL int ICACHE_FLASH_ATTR
user_info_json_set(struct jsontree_context *js_ctx,
		struct jsonparse_state *parser) {
	int type;
	while ((type = jsonparse_next(parser)) != 0) {
		if (type == JSON_TYPE_PAIR_NAME) {
			char buffer[64];
			os_bzero(buffer, 64);
			if (jsonparse_strcmp_value(parser, "studentID") == 0) {
				jsonparse_next(parser);
				jsonparse_next(parser);
				jsonparse_copy_value(parser, buffer, sizeof(buffer));
#ifdef ESP8266OLED
				if (!strcoll(buffer, "0000000000")) {

				} else {
					//FIXME 添加OLED显示学号
//					OLED_Print(0, 2, "                ", 2);
					OLED_Print(0, 2, "     Welcome    ", 2);
					OLED_Print(0, 4, "    ", 2);
					OLED_Print(3, 4, buffer, 2);
				}
#endif
#ifdef MENJINDEBUG
				os_printf("\nstudent ID :%s\n", buffer);
#endif
			}
		}
	}
	return 0;
}
LOCAL int ICACHE_FLASH_ATTR
open_door_cmd_json_set(struct jsontree_context *js_ctx,
		struct jsonparse_state *parser) {
	int type;
	while ((type = jsonparse_next(parser)) != 0) {
		if (type == JSON_TYPE_PAIR_NAME) {
			char buffer[64];
			os_bzero(buffer, 64);
			if (jsonparse_strcmp_value(parser, "cmd") == 0) {
				jsonparse_next(parser);
				jsonparse_next(parser);
				jsonparse_copy_value(parser, buffer, sizeof(buffer));
				if (!strcoll(buffer, "on")) {
#ifdef MENJINDEBUG
					os_printf("JSON: door open \n");
#endif
					easygpio_outputSet(JIDIANQI_IO, 0);
				} else if (!strcoll(buffer, "off")) {
#ifdef MENJINDEBUG
					os_printf("JSON: door clease \n");
#endif
//					OLED_Print(0, 2, "              ", 2);
					OLED_Print(0, 2, "    Permission  ", 2);
//					OLED_Print(0, 4, "              ", 2);
					OLED_Print(0, 4, "      Denied    ", 2);
					easygpio_outputSet(JIDIANQI_IO, 1);
				}
			}
		}
	}
	return 0;
}
Ejemplo n.º 6
0
/**
  * @brief  Setup commad of join to wifi ap.
  * @param  id: commad id number
  * @param  pPara: AT input param
  * @retval None
  */
static void ICACHE_FLASH_ATTR wifi_connect(char * ssid, char * password, char * bssid)
{
	char temp[64];
	struct station_config stationConf;
	int8_t len;
	connect_attempts++;
	//wifi_station_get_config(&stationConf);
	os_bzero(&stationConf, sizeof(struct station_config));
	os_memcpy(&stationConf.ssid, ssid, os_strlen(ssid));
	//os_memcpy(&stationConf.password, password, os_strlen(password));
	os_memcpy(&stationConf.bssid, bssid, 6);
	stationConf.bssid_set = 1;
    wifi_station_disconnect();
    os_printf("stationConf.ssid: -%s-\r\n", stationConf.ssid);
    os_printf("stationConf.password: -%s-\r\n", stationConf.password);

    ETS_UART_INTR_DISABLE();
    wifi_station_set_config(&stationConf);
    ETS_UART_INTR_ENABLE();
    wifi_station_connect();
    os_timer_disarm(&at_japDelayChack);
    os_timer_setfn(&at_japDelayChack, (os_timer_func_t *)at_japChack, NULL);
    os_timer_arm(&at_japDelayChack, 3000, 0);

}
Ejemplo n.º 7
0
void ICACHE_FLASH_ATTR set_wifi2_mesh_configuration(const int8_t cid,
	const SetWifi2MeshConfiguration *data) {

	configuration_current.mesh_enable = data->mesh_enable;

	os_memcpy(configuration_current.mesh_router_ip, data->mesh_router_ip,
		sizeof(data->mesh_router_ip));

	os_memcpy(configuration_current.mesh_router_subnet_mask, data->mesh_router_subnet_mask,
		sizeof(data->mesh_router_subnet_mask));

	os_memcpy(configuration_current.mesh_router_gateway, data->mesh_router_gateway,
		sizeof(data->mesh_router_gateway));

	os_memcpy(configuration_current.mesh_router_bssid, data->mesh_router_bssid,
		sizeof(data->mesh_router_bssid));

	os_memcpy(configuration_current.mesh_group_id, data->mesh_group_id,
		sizeof(data->mesh_group_id));

	os_bzero(configuration_current.mesh_ssid_prefix,
		sizeof(configuration_current.mesh_ssid_prefix));

	os_memcpy(configuration_current.mesh_ssid_prefix, data->mesh_ssid_prefix,
		sizeof(data->mesh_ssid_prefix));

	os_memcpy(configuration_current.mesh_gateway_ip, data->mesh_gateway_ip,
		sizeof(data->mesh_gateway_ip));

	configuration_current.mesh_gateway_port = data->mesh_gateway_port;

	com_return_setter(cid, data);
}
Ejemplo n.º 8
0
void ICACHE_FLASH_ATTR
sp_LightPairRequestPermission()
{
#if 1
    char data_body[200];
    os_bzero(data_body,sizeof(data_body));
    uint8 mac_sta[6] = {0};
    wifi_get_macaddr(STATION_IF, mac_sta);
    os_sprintf(data_body,"{\"device_mac\":\"%02X%02X%02X%02X%02X%02X\",\"button_mac\":\"%02X%02X%02X%02X%02X%02X\",\"path\":\"%s\"}",MAC2STR(mac_sta),MAC2STR(buttonPairingInfo.button_mac),PAIR_FOUND_REQUEST);
	
	if (!mesh_json_add_elem(data_body, sizeof(data_body), pair_sip, ESP_MESH_JSON_IP_ELEM_LEN)) {
    	return;
    }
    if (!mesh_json_add_elem(data_body, sizeof(data_body), pair_sport, ESP_MESH_JSON_PORT_ELEM_LEN)) {
	    return;
    }
    char* dev_mac = (char*)mesh_GetMdevMac();
    if (!mesh_json_add_elem(data_body, sizeof(data_body), dev_mac, ESP_MESH_JSON_DEV_MAC_ELEM_LEN)) {
	    return;
    }
    #if ESP_MESH_SUPPORT
	response_send_str((struct espconn*)user_GetUserPConn(),true,data_body,os_strlen(data_body),NULL,0,0,0);
	#else
	response_send_str((struct espconn*)user_GetWebPConn(),true,data_body,os_strlen(data_body),NULL,0,0,0);
	#endif
#endif
}
LOCAL int ICACHE_FLASH_ATTR
json_set(struct jsontree_context *js_ctx, struct jsonparse_state *parser)
{
	int type;
	INFO("Json set/n");
	INFO(parser);
	INFO("/n");
    while ((type = jsonparse_next(parser)) != 0) {
    	INFO (parser);
    	INFO("/n");
        if (type == JSON_TYPE_PAIR_NAME) {
            char buffer[64];
            os_bzero(buffer, 64);
            if (jsonparse_strcmp_value(parser, "switch") == 0) {
                jsonparse_next(parser);
                jsonparse_next(parser);
                jsonparse_copy_value(parser, buffer, sizeof(buffer));
                if (!strcoll(buffer, "on")) {
                	INFO("JSON: Switch on\n", buffer);
        			GPIO_OUTPUT_SET(SWITCH03_GPIO, 1);
                } else if (!strcoll(buffer, "off")) {
                	INFO("JSON: Switch off\n", buffer);
        			GPIO_OUTPUT_SET(SWITCH03_GPIO, 0);
                }
            }
        }
    }
    return 0;
}
Ejemplo n.º 10
0
int ICACHE_FLASH_ATTR
jsonrpc_wifiscan_init(void)
{
    os_bzero(&param, sizeof(param));

    jsonrpc_add_method_handler("wifiscan", scan_dispatch_handler);
    jsonrpc_add_method_handler("wifiscanresult", scanres_dispatch_handler);
}
Ejemplo n.º 11
0
static void ICACHE_FLASH_ATTR
scanres_dispatch_handler(char *id,
                         struct jsonrpc_transfer *trans,
                         void *buf,
                         int len)
{
    int i;
    struct jsontree_context js_ctx;
    char res_buf[jsonSize];
    DEBUG_PRINTF("begin | id: %s\n", id);

    param.id = mem_deref(param.id);
    param.id = mem_ref(id);
    param.scanresult_list = mem_ref(user_wifi_scan_result());

    if(param.scanresult_list)
    {
        param.le = list_head(param.scanresult_list);
        param.totalpage = (scannum / 8) + ((scannum % 8) ? 1 : 0);
        param.pagenum = 0;

        jsontree_setup(&js_ctx, (struct jsontree_value *)&scanresult_req, json_putchar);
        json_parse_buf(&js_ctx, buf, len);

        if(param.totalpage > 0)
        {
            if(param.pagenum >= param.totalpage)
                param.pagenum = param.totalpage - 1;
        }
        DEBUG_PRINTF("page=%d\n", param.pagenum);
        
        if(param.pagenum > 0)
        {
            int i;
            struct le *le;
            struct le *cur;
            
            i = (param.pagenum - 1) * 8;
            le = list_head(param.scanresult_list);

            while(le && (i > 0))
            {
                cur = le;
                le = cur->next;
                --i;
            }
            param.le = le;
        }
    }

    os_bzero(res_buf, jsonSize);
    json_ws_send((struct jsontree_value *)&scanresult_res, "scanresult_res", res_buf);
    trans->send(res_buf, str_len(res_buf));

    param.scanresult_list = mem_deref(param.scanresult_list);
    DEBUG_PRINTF("end\n");
}
Ejemplo n.º 12
0
void ICACHE_FLASH_ATTR set_wifi2_mesh_router_ssid(const int8_t cid,
	const SetWifi2MeshRouterSSID *data) {
	os_bzero(configuration_current.mesh_router_ssid, sizeof(configuration_current.mesh_router_ssid));

	os_memcpy(configuration_current.mesh_router_ssid, data->mesh_router_ssid,
		sizeof(data->mesh_router_ssid));

	com_return_setter(cid, data);
}
Ejemplo n.º 13
0
ICACHE_FLASH_ATTR void md5_done(Md5State *state, uint8_t *hash) {
    uint64_t total;
    uint8_t i;

    /* assert state->buflen < 64 */

    if (state->buflen < 56) {
        state->totallen += state->buflen;

        state->buf[state->buflen++] = 0x80;
        os_bzero(state->buf+state->buflen, 56-state->buflen);
    }

    else {
        state->totallen += state->buflen;

        state->buf[state->buflen++] = 0x80;
        os_bzero(state->buf+state->buflen, 64-state->buflen);
        md5_consume(state, state->buf);

        os_bzero(state->buf, 56);
    }

    total = state->totallen * 8;

    state->buf[63] = total >> (24+32);
    state->buf[62] = total >> (16+32);
    state->buf[61] = total >> (8+32);
    state->buf[60] = total >> 32;

    state->buf[59] = total >> 24;
    state->buf[58] = total >> 16;
    state->buf[57] = total >> 8;
    state->buf[56] = total;

    md5_consume(state, state->buf);

    for (i=0; i<4; i++) {
        *(hash+i*4  ) = state->h[i];
        *(hash+i*4+1) = state->h[i] >> 8;
        *(hash+i*4+2) = state->h[i] >> 16;
        *(hash+i*4+3) = state->h[i] >> 24;
    }
}
Ejemplo n.º 14
0
void ICACHE_FLASH_ATTR get_wifi2_mesh_ap_status(const int8_t cid,
	const GetWifi2MeshAPStatus *data) {
		uint8_t mac[6];
		struct ip_info info_ipv4;
		struct softap_config config_ap;

		os_bzero(&gw2masr.header, sizeof(gw2masr.header));

		gw2masr.header = data->header;
		gw2masr.header.length = sizeof(GetWifi2MeshAPStatusReturn);

		if((wifi_softap_get_config(&config_ap)) && (wifi_get_ip_info(SOFTAP_IF, &info_ipv4)) \
		&& (wifi_get_macaddr(SOFTAP_IF, mac))) {
			os_bzero(gw2masr.ssid, sizeof(gw2masr.ssid));
			os_memcpy(gw2masr.ssid, config_ap.ssid, sizeof(config_ap.ssid));

			os_bzero(gw2masr.ip, sizeof(gw2masr.ip));
			os_memcpy(gw2masr.ip, (uint8_t *)&info_ipv4.ip.addr, sizeof(info_ipv4.ip.addr));

			os_bzero(gw2masr.sub, sizeof(gw2masr.sub));
			os_memcpy(gw2masr.sub, (uint8_t *)&info_ipv4.netmask.addr, sizeof(info_ipv4.ip.addr));

			os_bzero(gw2masr.gw, sizeof(gw2masr.gw));
			os_memcpy(gw2masr.gw, (uint8_t *)&info_ipv4.gw.addr, sizeof(info_ipv4.ip.addr));

			os_bzero(gw2masr.mac, sizeof(gw2masr.mac));
			os_memcpy(gw2masr.mac, mac, sizeof(mac));
		}

		com_send(&gw2masr, sizeof(GetWifi2MeshAPStatusReturn), cid);
}
Ejemplo n.º 15
0
void ICACHE_FLASH_ATTR get_wifi2_mesh_station_status(const int8_t cid,
	const GetWifi2MeshStationStatus *data) {
		uint8_t mac[6];
		char *hostname_ptr;
		struct ip_info info_ipv4;
		struct station_config *config_st;

		os_bzero(&gw2mssr.header, sizeof(gw2mssr.header));

		gw2mssr.header = data->header;
		gw2mssr.header.length = sizeof(GetWifi2MeshStationStatusReturn);

		if((wifi_get_ip_info(STATION_IF, &info_ipv4)) && (wifi_get_macaddr(STATION_IF, mac))) {
			hostname_ptr = wifi_station_get_hostname();

			os_bzero(gw2mssr.hostname, sizeof(gw2mssr.hostname));
			os_memcpy(gw2mssr.hostname, hostname_ptr, sizeof(gw2mssr.hostname));

			os_bzero(gw2mssr.ip, sizeof(gw2mssr.ip));
			os_memcpy(gw2mssr.ip, (uint8_t *)&info_ipv4.ip.addr, sizeof(info_ipv4.ip.addr));

			os_bzero(gw2mssr.sub, sizeof(gw2mssr.sub));
			os_memcpy(gw2mssr.sub, (uint8_t *)&info_ipv4.netmask.addr, sizeof(info_ipv4.netmask.addr));

			os_bzero(gw2mssr.gw, sizeof(gw2mssr.gw));
			os_memcpy(gw2mssr.gw, (uint8_t *)&info_ipv4.gw.addr, sizeof(info_ipv4.gw.addr));

			os_bzero(gw2mssr.mac, sizeof(gw2mssr.mac));
			os_memcpy(gw2mssr.mac, mac, sizeof(mac));
		}

		com_send(&gw2mssr, sizeof(GetWifi2MeshStationStatusReturn), cid);
}
Ejemplo n.º 16
0
app_status_t ICACHE_FLASH_ATTR lamp_create(char * id, cJSON * config, app_lamp_t **lamp) {
    cJSON * jtmp = NULL;
    app_lamp_t *app = NULL;
    app_status_t result = APP_STATUS_ERR;
    char *ctmp = NULL;

    DBG_MSG("id: %s, config: %p\r\n", id, config);
    if ((id == NULL) || (config == NULL)) return APP_STATUS_INVALID_PARAM;

    app = os_malloc(sizeof(app_lamp_t));
    DBG_MSG("app: %p\r\n", app);
    if (app == NULL) goto done;
    os_bzero(app, 0, sizeof(app_lamp_t));

    app->id = id;

    jtmp = cJSON_GetObjectItem(config, ALAMP_CFG_URL_STR);
    DBG_MSG("jtmp: %p\r\n", jtmp);
    if (jtmp == NULL) goto err1;
    ctmp = (char *)os_malloc(os_strlen(jtmp->valuestring) + 1);
    os_memset(ctmp, 0, os_strlen(jtmp->valuestring) + 1);
    os_memcpy(ctmp, jtmp->valuestring, os_strlen(jtmp->valuestring));
    app->url = ctmp;
    DBG_MSG("app->url: %s\r\n", app->url);

    jtmp = cJSON_GetObjectItem(config, ALAMP_CFG_PIN_STR);
    DBG_MSG("jtmp: %p\r\n", jtmp);
    if (jtmp == NULL) goto err1;
    app->pin = jtmp->valueint;
    DBG_MSG("app->pin: %d\r\n", app->pin);

    app->type = APP_LAMP;
    app->recv = lamp_recv;
    app->online = lamp_online;
    app->offline = lamp_offline;

    // Init GPIO pin
    gpio_function(app->pin);
    GPIO_OUTPUT_SET(GPIO_ID_PIN(app->pin), 0);

    // If everything ok, goto done
    result = APP_STATUS_OK;
    *lamp = app;
    goto done;
err1:
    DBG_MSG("Error occured with status %d\r\n", result);
    if ((app != NULL) && (app->id != NULL)) os_free(app->id);
    if ((app != NULL) && (app->url != NULL)) os_free(app->url);
    if (app != NULL) os_free(app);
done:
    return result;
}
Ejemplo n.º 17
0
void ICACHE_FLASH_ATTR get_wifi2_mesh_router_ssid(const int8_t cid, const GetWifi2MeshRouterSSID *data) {
	GetWifi2MeshRouterSSIDReturn gw2mrsr;

	os_bzero(&gw2mrsr, sizeof(GetWifi2MeshRouterSSID));

	gw2mrsr.header         = data->header;
	gw2mrsr.header.length  = sizeof(GetWifi2MeshRouterSSIDReturn);

	os_memcpy(gw2mrsr.mesh_router_ssid, configuration_current.mesh_router_ssid,
		sizeof(configuration_current.mesh_router_ssid));

	com_send(&gw2mrsr, sizeof(GetWifi2MeshRouterSSIDReturn), cid);
}
Ejemplo n.º 18
0
static void ICACHE_FLASH_ATTR ping_timeout(void* arg)
{
//	struct ping_msg *pingmsg = (struct ping_msg *)arg;
	pingmsg->timeout_count ++;
	if (pingmsg->ping_opt->recv_function == NULL){
		os_printf("ping timeout\n");
	} else {
		struct ping_resp pingresp;
		os_bzero(&pingresp, sizeof(struct ping_resp));
		pingresp.ping_err = -1;
		pingmsg->ping_opt->recv_function(pingmsg->ping_opt, (void*)&pingresp);
	}
}
Ejemplo n.º 19
0
void ICACHE_FLASH_ATTR
sp_LightPairReplyKeepAlive()
{
	char data_resp[100];
	os_bzero(data_resp,sizeof(data_resp));
	os_sprintf(data_resp, "{\"status\":200,\"path\":\"%s\"}",PAIR_KEEP_ALIVE);
	os_printf("response send str...\r\n");
	#if ESP_MESH_SUPPORT
	response_send_str((struct espconn*)user_GetUserPConn(), true, data_resp,os_strlen(data_resp),NULL,0,1,0);
	#else
	response_send_str((struct espconn*)user_GetWebPConn(), true, data_resp,os_strlen(data_resp),NULL,0,1,0);
	#endif
}
Ejemplo n.º 20
0
/******************************************************************************
 * FunctionName : espconn_tcp_reconnect
 * Description  : reconnect with host
 * Parameters   : arg -- Additional argument to pass to the callback function
 * Returns      : none
*******************************************************************************/
static void ICACHE_FLASH_ATTR
espconn_tcp_reconnect(void *arg)
{
	espconn_msg *precon_cb = arg;
	sint8 re_err = 0;
	espconn_buf *perr_buf = NULL;
	espconn_buf *perr_back = NULL;
	espconn_kill_oldest_pcb();
	if (precon_cb != NULL) {
		struct espconn *espconn = precon_cb->preverse;
		re_err = precon_cb->pcommon.err;
		if (precon_cb->pespconn != NULL){
			if (espconn != NULL){/*Process the server's message block*/
				if (precon_cb->pespconn->proto.tcp != NULL){
					espconn_copy_partial(espconn, precon_cb->pespconn);
					espconn_printf("server: %d.%d.%d.%d : %d reconnection\n", espconn->proto.tcp->remote_ip[0],
							espconn->proto.tcp->remote_ip[1],espconn->proto.tcp->remote_ip[2],
							espconn->proto.tcp->remote_ip[3],espconn->proto.tcp->remote_port);
					os_free(precon_cb->pespconn->proto.tcp);
					precon_cb->pespconn->proto.tcp = NULL;
				}
				os_free(precon_cb->pespconn);
				precon_cb->pespconn = NULL;
			} else {/*Process the client's message block*/
				espconn = precon_cb->pespconn;
				espconn_printf("client: %d.%d.%d.%d : %d reconnection\n", espconn->proto.tcp->local_ip[0],
										espconn->proto.tcp->local_ip[1],espconn->proto.tcp->local_ip[2],
										espconn->proto.tcp->local_ip[3],espconn->proto.tcp->local_port);
			}
		}

		/*to prevent memory leaks, ensure that each allocated is deleted*/
		perr_buf = precon_cb->pcommon.pbuf;
		while (perr_buf != NULL){
			perr_back = perr_buf;
			perr_buf = perr_back->pnext;
			espconn_pbuf_delete(&precon_cb->pcommon.pbuf,perr_back);
			os_free(perr_back);
			perr_back = NULL;
		}
		os_bzero(&pktinfo[1], sizeof(struct espconn_packet));
		os_memcpy(&pktinfo[1], (void*)&precon_cb->pcommon.packet_info, sizeof(struct espconn_packet));
		os_free(precon_cb);
		precon_cb = NULL;
		if (espconn && espconn->proto.tcp && espconn->proto.tcp->reconnect_callback != NULL) {
			espconn->proto.tcp->reconnect_callback(espconn, re_err);
		}
	} else {
		espconn_printf("espconn_tcp_reconnect err\n");
	}
}
Ejemplo n.º 21
0
void ICACHE_FLASH_ATTR get_wifi2_mesh_common_status(const int8_t cid,
	const GetWifi2MeshCommonStatus *data) {
		os_bzero(&gw2mcsr.header, sizeof(gw2mcsr.header));

		gw2mcsr.header = data->header;
		gw2mcsr.header.length = sizeof(GetWifi2MeshCommonStatusReturn);

		gw2mcsr.status = espconn_mesh_get_status();
		gw2mcsr.is_root_node = espconn_mesh_is_root();
		gw2mcsr.is_root_candidate = espconn_mesh_is_root_candidate();
		gw2mcsr.connected_nodes = espconn_mesh_get_sub_dev_count();

		com_send(&gw2mcsr, sizeof(GetWifi2MeshCommonStatusReturn), cid);
}
Ejemplo n.º 22
0
void ICACHE_FLASH_ATTR simple_cb(u8 * macaddr, u8 * data, u8 len)
{
	int i;
	u8 ack_buf[16];
	u8 recv_buf[17];
	os_printf("now from[");
	for (i = 0; i < 6; i++)
		os_printf("%02X, ", macaddr[i]);
	os_bzero(recv_buf, 17);
	os_memcpy(recv_buf, data, len < 17 ? len : 16);
	os_printf(" len: %d, data: %s]\n", len, recv_buf);

	if (os_strncmp(data, "ACK", 3) == 0)
		return;

	os_sprintf(ack_buf, "ACK[%08x]", ack_count++);
	esp_now_send(macaddr, ack_buf, os_strlen(ack_buf));
}
Ejemplo n.º 23
0
static pmbedtls_msg mbedtls_msg_new(void)
{
	pmbedtls_msg msg = (pmbedtls_msg)os_zalloc( sizeof(mbedtls_msg));
	if (msg) {
		os_bzero(msg, sizeof(mbedtls_msg));
		msg->psession = mbedtls_session_new();
		if (msg->psession){
			mbedtls_net_init(&msg->listen_fd);
			mbedtls_net_init(&msg->fd);
			mbedtls_ssl_init(&msg->ssl);
			mbedtls_ssl_config_init(&msg->conf);		
			mbedtls_ctr_drbg_init(&msg->ctr_drbg);
			mbedtls_entropy_init(&msg->entropy);
		} else{
			os_free(msg);
			msg = NULL;
		}
	}
	return msg;
}
Ejemplo n.º 24
0
ICACHE_FLASH_ATTR void md5_consume(Md5State *state, const uint8_t *chunk) {
    int i;
    uint32_t m[16];
    uint32_t hh[4];
    uint32_t f, g;

    os_bzero(m, sizeof(m));

    for (i=0; i<16; i++)
        m[i] = (chunk[i*4+3]<<24) | (chunk[i*4+2]<<16) |
               (chunk[i*4+1]<<8)  |  chunk[i*4  ];

    for (i=0; i<4; i++)
        hh[i] = state->h[i];

    for (i=0; i<64; i++) {
        if (0 <= i && i <= 15) {
            f = (hh[1] & hh[2]) | (~hh[1] & hh[3]);
            g = i;
        } else if (16 <= i && i <= 31) {
            f = (hh[3] & hh[1]) | (~hh[3] & hh[2]);
            g = (5*i + 1) % 16;
        } else if (32 <= i && i <= 47) {
            f = hh[1] ^ hh[2] ^ hh[3];
            g = (3*i + 5) % 16;
        } else if (48 <= i && i <= 63) {
            f = hh[2] ^ (hh[1] | ~hh[3]);
            g = (7*i) % 16;
        }

        f = f + hh[0] + md5_k[i] + m[g];
        hh[0] = hh[3];
        hh[3] = hh[2];
        hh[2] = hh[1];
        hh[1] = hh[1] + LROTATE(f, md5_s[i]);
    }

    for (i=0; i<4; i++)
        state->h[i] += hh[i];
}
Ejemplo n.º 25
0
static void ICACHE_FLASH_ATTR
ping_coarse_tmr(void *arg)
{
//	struct ping_msg *pingmsg = (struct ping_msg*)arg;
	struct ping_option *ping_opt= NULL;
	struct ping_resp pingresp;
	ip_addr_t ping_target;

	LWIP_ASSERT("ping_timeout: no pcb given!", pingmsg != NULL);
	ping_target.addr = pingmsg->ping_opt->ip;
	ping_opt = pingmsg->ping_opt;
	if (--pingmsg->sent_count != 0){
		pingmsg ->ping_sent = system_get_time();
		ping_send(pingmsg->ping_pcb, &ping_target);

		sys_timeout(PING_TIMEOUT_MS, ping_timeout, pingmsg);
		sys_timeout(pingmsg->coarse_time, ping_coarse_tmr, pingmsg);
	} else {
		uint32 delay = system_relative_time(pingmsg->ping_start);
		delay /= PING_COARSE;
		//ping_seq_num = 0;
		if (ping_opt->sent_function == NULL){
			os_printf("ping %d, timeout %d, total payload %d bytes, %d ms\n",
					pingmsg->max_count, pingmsg->timeout_count, PING_DATA_SIZE*(pingmsg->max_count - pingmsg->timeout_count),delay);
		} else {
			os_bzero(&pingresp, sizeof(struct ping_resp));
			pingresp.total_count = pingmsg->max_count;
			pingresp.timeout_count = pingmsg->timeout_count;
			pingresp.total_bytes = PING_DATA_SIZE*(pingmsg->max_count - pingmsg->timeout_count);
			pingresp.total_time = delay;
			pingresp.ping_err = 0;
		}
		sys_untimeout(ping_coarse_tmr, pingmsg);
		raw_remove(pingmsg->ping_pcb);
//		os_free(pingmsg);
		if (ping_opt->sent_function != NULL)
			ping_opt->sent_function(ping_opt,(uint8*)&pingresp);
	}
}
LOCAL int ICACHE_FLASH_ATTR
time_info_json_set(struct jsontree_context *js_ctx,
		struct jsonparse_state *parser) {
	int type;
	while ((type = jsonparse_next(parser)) != 0) {
		if (type == JSON_TYPE_PAIR_NAME) {
			char buffer[64];
			os_bzero(buffer, 64);
			if (jsonparse_strcmp_value(parser, "time") == 0) {
				jsonparse_next(parser);
				jsonparse_next(parser);
				jsonparse_copy_value(parser, buffer, sizeof(buffer));
				//TODO 添加显示时间!
				OLED_Print(0, 2, "      ", 2);
				OLED_Print(5, 2, buffer, 2);
				OLED_Print(10, 2, "     ", 2);

			}
		}
	}
	return 0;
}
Ejemplo n.º 27
0
void ICACHE_FLASH_ATTR set_wifi2_configuration(const int8_t cid, const SetWifi2Configuration *data) {
	char str_fw_version[4];

	os_bzero(str_fw_version, sizeof(str_fw_version));
	os_sprintf(str_fw_version, "%d%d%d", FIRMWARE_VERSION_MAJOR, FIRMWARE_VERSION_MINOR,
		FIRMWARE_VERSION_REVISION);

	configuration_current.general_port           = data->port;
	configuration_current.general_websocket_port = data->websocket_port;
	configuration_current.general_website_port   = data->website_port;
	configuration_current.general_phy_mode       = data->phy_mode;
	configuration_current.general_sleep_mode     = data->sleep_mode;
	configuration_current.general_website        = data->website;

	if(strtoul(str_fw_version, NULL, 10) >= 204) {
		if(data->website != 1) {
			configuration_current.general_website_port = 1;
		}
	}

	com_return_setter(cid, data);
}
Ejemplo n.º 28
0
void ICACHE_FLASH_ATTR user_init()
{
	/* set baud rate */
	uart_div_modify(0, UART_CLK_FREQ / 115200);

	wifi_set_opmode(1); /* station mode */
	struct station_config config;
	os_bzero(&config, sizeof(config));
	strncpy((char *)config.ssid, WIFI_SSID, sizeof(config.ssid));
	strncpy((char *)config.password, WIFI_PASS, sizeof(config.password));
	wifi_station_set_config(&config);

	MQTT_InitConnection(&mqtt_client, MQTT_HOST, MQTT_PORT, 0);
	MQTT_InitClient(&mqtt_client, MQTT_CLIENT_ID, MQTT_USER, MQTT_PASS,
			MQTT_KEEPALIVE, MQTT_CLEAN_SESSION);
	MQTT_OnConnected(&mqtt_client, on_mqtt_connected);
	MQTT_OnData(&mqtt_client, on_mqtt_data);

	dmesg("Starting up");

	/* setup timer (1000ms, repeating) */
	os_timer_setfn(&some_timer, (os_timer_func_t *)on_timer, NULL);
	os_timer_arm(&some_timer, 1000, 1);
}
Ejemplo n.º 29
0
void ICACHE_FLASH_ATTR get_wifi2_mesh_configuration(const int8_t cid,
	const GetWifi2MeshConfiguration *data) {
	GetWifi2MeshConfigurationReturn gw2mcr;

	os_bzero(&gw2mcr, sizeof(GetWifi2MeshConfigurationReturn));

	gw2mcr.header         = data->header;
	gw2mcr.header.length  = sizeof(GetWifi2MeshConfigurationReturn);

	gw2mcr.mesh_enable    = configuration_current.mesh_enable;

	os_memcpy(gw2mcr.mesh_router_ip, configuration_current.mesh_router_ip,
		sizeof(configuration_current.mesh_router_ip));

	os_memcpy(gw2mcr.mesh_router_subnet_mask, configuration_current.mesh_router_subnet_mask,
		sizeof(configuration_current.mesh_router_subnet_mask));

	os_memcpy(gw2mcr.mesh_router_gateway, configuration_current.mesh_router_gateway,
		sizeof(configuration_current.mesh_router_gateway));

	os_memcpy(gw2mcr.mesh_router_bssid, configuration_current.mesh_router_bssid,
		sizeof(configuration_current.mesh_router_bssid));

	os_memcpy(gw2mcr.mesh_group_id, configuration_current.mesh_group_id,
		sizeof(configuration_current.mesh_group_id));

	os_memcpy(gw2mcr.mesh_ssid_prefix, configuration_current.mesh_ssid_prefix,
		sizeof(configuration_current.mesh_ssid_prefix));

	os_memcpy(gw2mcr.mesh_gateway_ip, configuration_current.mesh_gateway_ip,
		sizeof(configuration_current.mesh_gateway_ip));

	gw2mcr.mesh_gateway_port = configuration_current.mesh_gateway_port;

	com_send(&gw2mcr, sizeof(GetWifi2MeshConfigurationReturn), cid);
}
Ejemplo n.º 30
0
void ICACHE_FLASH_ATTR tfp_sent_callback(void *arg) {
	espconn *con = (espconn *)arg;
	uint8_t tfp_mesh_send_packet_len = 0;
	uint8_t tfp_mesh_send_packet[TFP_MAX_LENGTH];
	TFPConnection *tfp_con = (TFPConnection *)con->reverse;

	os_bzero(tfp_mesh_send_packet, sizeof(tfp_mesh_send_packet));

	if(!configuration_current.mesh_enable) {
		packet_counter(con, PACKET_COUNT_TX);

		if(tfp_con->state == TFP_CON_STATE_CLOSED_AFTER_SEND) {
			espconn_disconnect(tfp_con->con);
		}
		else {
			tfp_con->state = TFP_CON_STATE_OPEN;
		}
	}
	else {
		// If TFP mesh send buffer is empty then nothing to do.
		if(ringbuffer_is_empty(&tfp_mesh_send_rb)) {
			// Change the state of the socket to be ready to send.

			/*
			 * FIXME: Technically tfp_con->state should be same as tfp_cons[0].state
			 * but this seem to be not the case. Maybe because the Mesh SDK does
			 * something ?
			 */
			tfp_cons[0].state = TFP_CON_STATE_OPEN;

			return;
		}

		// Send packet from the TFP send buffer and remove the packet from the buffer.

		// Read 5 bytes of the packet present in the buffer.
		if(ringbuffer_peak(&tfp_mesh_send_rb, tfp_mesh_send_packet, 5) != 5) {
			loge("MSH:Error peaking for packet length on send buffer\n");

			return;
		}

		// Get the length field of the packet.
		tfp_mesh_send_packet_len = tfp_mesh_send_packet[4];

		/*
		 * Now that the length of the packet is known try to get the whole packet
		 * from the TFP mesh send buffer for sending.
		 */
		if(ringbuffer_peak(&tfp_mesh_send_rb,
											 tfp_mesh_send_packet,
											 tfp_mesh_send_packet_len) != tfp_mesh_send_packet_len) {
		  loge("MSH:Error peaking for packet from send buffer\n");

			return;
		}

		ringbuffer_remove(&tfp_mesh_send_rb, tfp_mesh_send_packet_len);
		tfp_mesh_send(tfp_cons[0].con, tfp_mesh_send_packet, tfp_mesh_send_packet_len);
	}
}