/** * Resume write operation * @param buf * @param l * @return */ RES_CODE CHandle::tsk_resume_write(const void * buf, unsigned int l) { if(complete()) { //handle is idle and open len = l; set_res_cmd(CMD_WRITE); src.as_voidptr = (void*)buf; tsk_start_handle(); if(tsk_resume_wait_signal(signal)) res &= ~FLG_SIGNALED; else tsk_cancel(); } return (res); }
unsigned int esp8266_module::get_socket_state(unsigned int sock_id) { RES_CODE res; const char* cmd = "+CIPSTATUS"; unsigned int sig=0; uint32_t index; bool found = false; cmd_state |= WIFI_CMD_STATE_ROW_STOP; res = wifi_send_cmd(cmd, 15); cmd_state &= ~WIFI_CMD_STATE_ROW_STOP; do { if(sig) { process_input(sig, cmd); res = cmd_state; } if (res >= WIFI_CMD_STATE_OK) break; if(res & WIFI_CMD_STATE_RETURNED) { cmd_state = res & ~WIFI_CMD_STATE_RETURNED; if(tmos_sscanf(buf, "+CIPSTATUS:%u", &index)) { if(index == sock_id) found = true; } row_start = 0; row_end = 0; } sig = tsk_resume_wait_signal(rcv_hnd.signal); } while(sig); if((cmd_state & WIFI_CMD_STATE_OK) && found ) return 1; return 0; }
RES_CODE esp8266_module::process_write(CSocket* sock) { unsigned size;//, newsize, id; // unsigned int start_size, write_size, sock_state; CSTRING cmd; unsigned char snd_pending; while(1) { size = sock->len; if(!size) { TRACELN1("WIFI: write OK"); return RES_SIG_OK; } if(size >1022) size = 1022; if(sock->sock_state != SOCKET_CONECTED) break; // Send command cmd.format("+CIPSEND=%u,%u", sock->sock_id, size); TRACELN("WIFI: WRITE %d?", size); cmd_state |= WIFI_CMD_STATE_HND; if(wifi_send_cmd(cmd.c_str(), 20) != WIFI_CMD_STATE_RETURNED) { wifi_net_error(NET_ERR_SOCK_WRITE); return RES_SIG_ERROR; } // Send data snd_pending = '>'; do { process_input(rcv_hnd.signal, cmd.c_str(), snd_pending); if ( cmd_state >= WIFI_CMD_STATE_HND ) { if ( cmd_state & WIFI_CMD_STATE_HND ) { unsigned int mytime; cmd_state &= ~WIFI_CMD_STATE_HND; snd_pending = 0; rcv_hnd.tsk_start_read(&received_ch, 1); mytime = CURRENT_TASK->time; tsk_sleep(55); // data sheet recomendation if( snd_hnd.tsk_write(sock->src.as_voidptr, size, WIFI_WRITE_TOT) != RES_OK) break; CURRENT_TASK->time = mytime; } else { if ( cmd_state >= WIFI_CMD_STATE_OK ) break; // command completed with OK, ERROR .. } } } while(tsk_resume_wait_signal(rcv_hnd.signal)); // wifi_on_blink_transfer(this, GPRS_TRANSFER_INDICATOR); //Check the result if(cmd_state & WIFI_CMD_STATE_OK) { TRACE1(" done!"); sock->src.as_byteptr += size; sock->len -= size; continue; } if (cmd_state & WIFI_CMD_STATE_CMES) { TRACE_ERROR("\r\nWIFI:%s write ERROR", sock->client.task->name); } break; } wifi_sleep(120); wifi_net_error(NET_ERR_SOCK_WRITE); return RES_SIG_ERROR; }
NET_CODE esp8266_module::wifi_esp8266_socket_open(CSocket* sock) { unsigned int sid; sock_mode_t* mode; RES_CODE res; const char* cmd = "+CIPSTATUS"; unsigned int sig=0; CSTRING msg_list("\r\n"); uint32_t index=1; cmd_state |= WIFI_CMD_STATE_ROW_STOP; res = wifi_send_cmd(cmd, 15); cmd_state &= ~WIFI_CMD_STATE_ROW_STOP; do { if(sig) { process_input(sig, cmd); res = cmd_state; } if (res >= WIFI_CMD_STATE_OK) break; if(res & WIFI_CMD_STATE_RETURNED) { cmd_state = res & ~WIFI_CMD_STATE_RETURNED; msg_list.appendf("%u:%s\r\n", index++, buf); row_start = 0; row_end = 0; } sig = tsk_resume_wait_signal(rcv_hnd.signal); } while(sig); // if(cmd_state & WIFI_CMD_STATE_OK) // TRACE1(msg_list.c_str()); mode = (sock_mode_t*)sock->mode.as_voidptr; if(mode ) { if ( (mode->sock_type == IP_SOCKET_UDP && mode->port) ||(mode->sock_type == IP_SOCKET_TCP) ) { for(sid=0; sid < WIFI_ESP8266_MAX_SOCKETS; sid++) { if(NULL == alloc_sockets[sid]) { sock->sock_id = sid; sock->sock_state = SOCKET_OPEN; alloc_sockets[sid] = sock; #if USE_GPRS_LISTEN if (mode->sock_type == IP_SOCKET_UDP) listen_ports[sid] = mode->port; accept_id[sid] = GPRS_LEON_MAX_SOCKETS; #endif used_sockets++; // msg_list.format("\r\nWIFI:%s sock open %d.", sock->client.task->name, sid); // TRACE1(msg_list.c_str()); return NET_OK; } } } } TRACE_WIFI_ERROR("\r\nWIFI:%s create socket ERROR", sock->client.task->name); return wifi_net_error(NET_ERR_SOCK_CREATE); }
NET_CODE esp8266_module::wifi_esp8266_init_net(CSocket * sock) { NET_CODE res; CSTRING cmd; wifi_AP_t AP; uint32_t sig = 0; bool found; res = wifi_send_cmd("+CIFSR", 50); for(int i =0; i < 5; i++) { found = false; cmd_state |= WIFI_CMD_STATE_ROW_STOP; res = wifi_send_cmd("+CWLAP", 15); cmd_state &= ~WIFI_CMD_STATE_ROW_STOP; do { if(sig) { process_input(sig, "+CWLAP"); res = cmd_state; } if (res >= WIFI_CMD_STATE_OK) break; if(res & WIFI_CMD_STATE_RETURNED) { cmd_state = res & ~WIFI_CMD_STATE_RETURNED; if(!found) { res = wifi_on_get_AP(this, sock, &AP); if(res == RES_OK) found = true; } row_start = 0; row_end = 0; } sig = tsk_resume_wait_signal(rcv_hnd.signal); } while(sig); if(cmd_state & WIFI_CMD_STATE_OK) { if(!found) res = NET_ERR_WIFI_NET_NAME; else res = NET_OK; break; } } if (res != NET_OK) return wifi_error(res); // If the wifi is already connected to a network different than the one // requested - return NET_IDLE - unavailable if (NET_OK == wifi_get_network_name(cmd)) { if (0 != strcmp(cmd.c_str(), AP.name.c_str())) { return NET_IDLE; } } cmd.format("+CWJAP=\"%s\",\"%s\"", AP.name.c_str(), AP.pass.c_str()); for(int i=0; i < 3; i++) { res = wifi_send_cmd(cmd.c_str(), 50); if (WIFI_CMD_STATE_OK == res) { res = wifi_send_cmd("+CIFSR", 50); if (WIFI_CMD_STATE_ROK == res) { connected_network_name = AP.name; return NET_OK; } break; } } return NET_ERR_WIFI_REGISTER; if (WIFI_CMD_STATE_OK == res) { res = wifi_send_cmd("+CIFSR", 50); if (WIFI_CMD_STATE_ROK == res) { connected_network_name = AP.name; wifi_send_cmd("+CIPSTART=\"TCP\",\"www.tmos-arm.com\",80", 50); //wifi_send_cmd("+CIPSTART=\"TCP\",\"192.168.147.100\",6112", 50); cmd.clear(); cmd.append("+CIPSEND="); cmd.appendf("%d", strlen(message) + 1); wifi_send_cmd(cmd.c_str(), 200); // make sure the handle is working if it is open while(rcv_hnd.res < FLG_BUSY) { process_input(0, NULL); } wifi_sleep(20); //(the recommended value is at least 20 ms) // make sure no URC is coming and the buf is empty if( cmd_state & WIFI_CMD_STATE_STARTED) { if (tsk_wait_signal(rcv_hnd.signal, 8192)) { do { process_input(rcv_hnd.signal, NULL); } while ( (cmd_state & WIFI_CMD_STATE_STARTED) && tsk_resume_wait_signal(rcv_hnd.signal) ); } } //start clean cmd_state &= (WIFI_CMD_STATE_ROW_STOP | WIFI_CMD_STATE_HND); row_start = row_end = 0; snd_hnd.tsk_write(message, strlen(message) + 1, WIFI_WRITE_TOT); return NET_OK; } } return wifi_net_error(res); }