Lucas Citolin
/
Autoline
MBED experiment.
Embed:
(wiki syntax)
Show/hide line numbers
Connectivity.cpp
00001 /* 00002 * Connectivity.cpp 00003 * 00004 * Created on: Jul 16, 2018 00005 * Author: Claudio Pinheiro 00006 */ 00007 00008 #include <Connectivity.h> 00009 00010 Connectivity::Connectivity() : thread(osPriorityNormal, 2048), pc(USBTX, USBRX), led_link(LED1) { 00011 nsapi_error_t result; 00012 result = eth.set_network(IP, MASK, GATEWAY); 00013 00014 readCallback = NULL; 00015 00016 if (!result) 00017 thread.start(callback(this, &Connectivity::monitor)); 00018 } 00019 00020 Connectivity::~Connectivity() { 00021 thread.terminate(); 00022 } 00023 00024 int Connectivity::send(char *data, int length) { 00025 pc.printf("Sending: %s \n", data); 00026 return client.send(data, length); 00027 } 00028 00029 void Connectivity::registerReadCallback(Callback<void(char*,int)> r) { 00030 readCallback = r; 00031 } 00032 00033 void Connectivity::monitor() { 00034 nsapi_error_t result; 00035 00036 while (true) { 00037 pc.printf("Connecting to network...\r\n"); 00038 result = eth.connect(); 00039 if (result == NSAPI_ERROR_OK) { 00040 pc.printf("Conectado. IP: %s\n\r", eth.get_ip_address()); 00041 00042 srv.open(ð); 00043 srv.bind(eth.get_ip_address(), 1200); 00044 srv.listen(1); 00045 00046 srv.set_blocking(false); 00047 client.set_timeout(0); 00048 00049 while (eth.get_connection_status() == NSAPI_STATUS_GLOBAL_UP) { 00050 result = srv.accept(&client, &client_addr); 00051 if (result == NSAPI_ERROR_OK) { 00052 pc.printf("Connection accepted from %s\r\n", client_addr.get_ip_address()); 00053 } 00054 memset(buffer,'\0',sizeof(buffer)); 00055 result = client.recv(buffer, 256); 00056 00057 // Callback 00058 if (result > 0) 00059 readCallback(buffer,sizeof(buffer)); 00060 00061 } 00062 } 00063 else if (result != NSAPI_ERROR_IS_CONNECTED) { 00064 eth.disconnect(); 00065 } 00066 } 00067 }
Generated on Mon Jul 25 2022 15:13:16 by 1.7.2