Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
plc/Connectivity.cpp@0:40a00c231fbd, 2018-09-15 (annotated)
- Committer:
- citolin
- Date:
- Sat Sep 15 19:33:16 2018 +0000
- Revision:
- 0:40a00c231fbd
First commit to this MBED I2C experiment
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| citolin | 0:40a00c231fbd | 1 | /* |
| citolin | 0:40a00c231fbd | 2 | * Connectivity.cpp |
| citolin | 0:40a00c231fbd | 3 | * |
| citolin | 0:40a00c231fbd | 4 | * Created on: Jul 16, 2018 |
| citolin | 0:40a00c231fbd | 5 | * Author: Claudio Pinheiro |
| citolin | 0:40a00c231fbd | 6 | */ |
| citolin | 0:40a00c231fbd | 7 | |
| citolin | 0:40a00c231fbd | 8 | #include <Connectivity.h> |
| citolin | 0:40a00c231fbd | 9 | |
| citolin | 0:40a00c231fbd | 10 | Connectivity::Connectivity() : thread(osPriorityNormal, 2048), pc(USBTX, USBRX), led_link(LED1) { |
| citolin | 0:40a00c231fbd | 11 | nsapi_error_t result; |
| citolin | 0:40a00c231fbd | 12 | result = eth.set_network(IP, MASK, GATEWAY); |
| citolin | 0:40a00c231fbd | 13 | |
| citolin | 0:40a00c231fbd | 14 | readCallback = NULL; |
| citolin | 0:40a00c231fbd | 15 | |
| citolin | 0:40a00c231fbd | 16 | if (!result) |
| citolin | 0:40a00c231fbd | 17 | thread.start(callback(this, &Connectivity::monitor)); |
| citolin | 0:40a00c231fbd | 18 | } |
| citolin | 0:40a00c231fbd | 19 | |
| citolin | 0:40a00c231fbd | 20 | Connectivity::~Connectivity() { |
| citolin | 0:40a00c231fbd | 21 | thread.terminate(); |
| citolin | 0:40a00c231fbd | 22 | } |
| citolin | 0:40a00c231fbd | 23 | |
| citolin | 0:40a00c231fbd | 24 | int Connectivity::send(char *data, int length) { |
| citolin | 0:40a00c231fbd | 25 | pc.printf("Sending: %s \n", data); |
| citolin | 0:40a00c231fbd | 26 | return client.send(data, length); |
| citolin | 0:40a00c231fbd | 27 | } |
| citolin | 0:40a00c231fbd | 28 | |
| citolin | 0:40a00c231fbd | 29 | void Connectivity::registerReadCallback(Callback<void(char*,int)> r) { |
| citolin | 0:40a00c231fbd | 30 | readCallback = r; |
| citolin | 0:40a00c231fbd | 31 | } |
| citolin | 0:40a00c231fbd | 32 | |
| citolin | 0:40a00c231fbd | 33 | void Connectivity::monitor() { |
| citolin | 0:40a00c231fbd | 34 | nsapi_error_t result; |
| citolin | 0:40a00c231fbd | 35 | |
| citolin | 0:40a00c231fbd | 36 | while (true) { |
| citolin | 0:40a00c231fbd | 37 | pc.printf("Connecting to network...\r\n"); |
| citolin | 0:40a00c231fbd | 38 | result = eth.connect(); |
| citolin | 0:40a00c231fbd | 39 | if (result == NSAPI_ERROR_OK) { |
| citolin | 0:40a00c231fbd | 40 | pc.printf("Conectado. IP: %s\n\r", eth.get_ip_address()); |
| citolin | 0:40a00c231fbd | 41 | |
| citolin | 0:40a00c231fbd | 42 | srv.open(ð); |
| citolin | 0:40a00c231fbd | 43 | srv.bind(eth.get_ip_address(), 1200); |
| citolin | 0:40a00c231fbd | 44 | srv.listen(1); |
| citolin | 0:40a00c231fbd | 45 | |
| citolin | 0:40a00c231fbd | 46 | srv.set_blocking(false); |
| citolin | 0:40a00c231fbd | 47 | client.set_timeout(0); |
| citolin | 0:40a00c231fbd | 48 | |
| citolin | 0:40a00c231fbd | 49 | while (eth.get_connection_status() == NSAPI_STATUS_GLOBAL_UP) { |
| citolin | 0:40a00c231fbd | 50 | result = srv.accept(&client, &client_addr); |
| citolin | 0:40a00c231fbd | 51 | if (result == NSAPI_ERROR_OK) { |
| citolin | 0:40a00c231fbd | 52 | pc.printf("Connection accepted from %s\r\n", client_addr.get_ip_address()); |
| citolin | 0:40a00c231fbd | 53 | } |
| citolin | 0:40a00c231fbd | 54 | memset(buffer,'\0',sizeof(buffer)); |
| citolin | 0:40a00c231fbd | 55 | result = client.recv(buffer, 256); |
| citolin | 0:40a00c231fbd | 56 | |
| citolin | 0:40a00c231fbd | 57 | // Callback |
| citolin | 0:40a00c231fbd | 58 | if (result > 0) |
| citolin | 0:40a00c231fbd | 59 | readCallback(buffer,sizeof(buffer)); |
| citolin | 0:40a00c231fbd | 60 | |
| citolin | 0:40a00c231fbd | 61 | } |
| citolin | 0:40a00c231fbd | 62 | } |
| citolin | 0:40a00c231fbd | 63 | else if (result != NSAPI_ERROR_IS_CONNECTED) { |
| citolin | 0:40a00c231fbd | 64 | eth.disconnect(); |
| citolin | 0:40a00c231fbd | 65 | } |
| citolin | 0:40a00c231fbd | 66 | } |
| citolin | 0:40a00c231fbd | 67 | } |