bosko lekovic
/
EthTCPclient_05_02
p1
main.cpp@7:6a700c98b9b5, 2020-06-18 (annotated)
- Committer:
- bosko001
- Date:
- Thu Jun 18 15:23:05 2020 +0000
- Revision:
- 7:6a700c98b9b5
- Parent:
- 5:ecaf089d1c8a
p1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bosko001 | 0:b01306ccbbe1 | 1 | /* mbed Microcontroller Library |
bosko001 | 0:b01306ccbbe1 | 2 | * Copyright (c) 2019 ARM Limited |
bosko001 | 0:b01306ccbbe1 | 3 | * SPDX-License-Identifier: Apache-2.0 |
bosko001 | 0:b01306ccbbe1 | 4 | */ |
bosko001 | 0:b01306ccbbe1 | 5 | |
bosko001 | 0:b01306ccbbe1 | 6 | #include "mbed.h" |
bosko001 | 0:b01306ccbbe1 | 7 | #include "platform/mbed_thread.h" |
bosko001 | 0:b01306ccbbe1 | 8 | |
bosko001 | 0:b01306ccbbe1 | 9 | #include "EthernetInterface.h" |
bosko001 | 0:b01306ccbbe1 | 10 | |
bosko001 | 0:b01306ccbbe1 | 11 | #include <stdio.h> |
bosko001 | 0:b01306ccbbe1 | 12 | #include <string.h> |
bosko001 | 0:b01306ccbbe1 | 13 | #include "TCPSocket.h" |
bosko001 | 0:b01306ccbbe1 | 14 | |
bosko001 | 4:0aec01df33d2 | 15 | #include "PGSdisplay.h" |
bosko001 | 4:0aec01df33d2 | 16 | |
bosko001 | 0:b01306ccbbe1 | 17 | |
bosko001 | 4:0aec01df33d2 | 18 | // |
bosko001 | 4:0aec01df33d2 | 19 | //void putOnDispNo(int broj, char boja); |
bosko001 | 4:0aec01df33d2 | 20 | //void putOnDisp(char *s, char boja); |
markoc | 1:10e2a0bef1b4 | 21 | void requestMessageThread(struct s_rm *s); |
bosko001 | 4:0aec01df33d2 | 22 | //void slanjefun(); |
bosko001 | 0:b01306ccbbe1 | 23 | |
bosko001 | 0:b01306ccbbe1 | 24 | #define IP_ADDR "192.168.2.11" |
bosko001 | 0:b01306ccbbe1 | 25 | #define NET_MASK "255.255.248.0" |
bosko001 | 0:b01306ccbbe1 | 26 | #define GATW_ADDR "192.168.1.1" |
bosko001 | 0:b01306ccbbe1 | 27 | #define IP_ADDR_R "192.168.1.9" |
bosko001 | 0:b01306ccbbe1 | 28 | |
bosko001 | 0:b01306ccbbe1 | 29 | |
bosko001 | 0:b01306ccbbe1 | 30 | |
bosko001 | 0:b01306ccbbe1 | 31 | EthernetInterface eth; |
bosko001 | 0:b01306ccbbe1 | 32 | nsapi_error_t err; |
bosko001 | 0:b01306ccbbe1 | 33 | |
markoc | 1:10e2a0bef1b4 | 34 | char * extract_string( char delimiter, char *ulazni_str, int n_str ); |
markoc | 1:10e2a0bef1b4 | 35 | void putOnDisp( char *s, char boja); |
bosko001 | 0:b01306ccbbe1 | 36 | |
bosko001 | 4:0aec01df33d2 | 37 | |
bosko001 | 0:b01306ccbbe1 | 38 | |
bosko001 | 0:b01306ccbbe1 | 39 | void tcpThread_fun( EthernetInterface *e); |
markoc | 1:10e2a0bef1b4 | 40 | void requestMessage( EthernetInterface *e); |
bosko001 | 4:0aec01df33d2 | 41 | // |
bosko001 | 5:ecaf089d1c8a | 42 | //DigitalOut tast(PTB19,0); |
bosko001 | 5:ecaf089d1c8a | 43 | //////RawSerial rs485(PTC17,PTC16); |
bosko001 | 5:ecaf089d1c8a | 44 | //RawSerial rs485(PTD3,PTD2); |
markoc | 1:10e2a0bef1b4 | 45 | |
bosko001 | 2:4deba4264f65 | 46 | struct s_rm { |
markoc | 1:10e2a0bef1b4 | 47 | EthernetInterface *e; |
markoc | 1:10e2a0bef1b4 | 48 | SocketAddress *sa; |
markoc | 1:10e2a0bef1b4 | 49 | char *tx_msg; |
markoc | 1:10e2a0bef1b4 | 50 | void (*fun_rec)(char*); |
bosko001 | 7:6a700c98b9b5 | 51 | int response; |
markoc | 1:10e2a0bef1b4 | 52 | }; |
markoc | 1:10e2a0bef1b4 | 53 | |
bosko001 | 2:4deba4264f65 | 54 | char crm_req[]= {'{','1','|','1','}'}; //"{1|1}"; |
markoc | 1:10e2a0bef1b4 | 55 | char crm_resp[100]; |
markoc | 1:10e2a0bef1b4 | 56 | void crm_fun( char *rx) |
markoc | 1:10e2a0bef1b4 | 57 | { |
bosko001 | 2:4deba4264f65 | 58 | printf("crm_resp: %s\n\r",rx); |
bosko001 | 2:4deba4264f65 | 59 | strcmp(crm_resp, rx); |
markoc | 1:10e2a0bef1b4 | 60 | } |
markoc | 1:10e2a0bef1b4 | 61 | |
bosko001 | 2:4deba4264f65 | 62 | char srm_req[]="{5|1|0}"; |
markoc | 1:10e2a0bef1b4 | 63 | void srm_fun( char *rx ) |
markoc | 1:10e2a0bef1b4 | 64 | { |
bosko001 | 2:4deba4264f65 | 65 | printf("srm_resp: %s\n\r",rx); |
bosko001 | 2:4deba4264f65 | 66 | char *strno= extract_string(';',rx, 2); |
bosko001 | 2:4deba4264f65 | 67 | char boja = 0; |
bosko001 | 2:4deba4264f65 | 68 | if(atoi(strno) <= 0) { |
bosko001 | 2:4deba4264f65 | 69 | boja = 1; |
bosko001 | 2:4deba4264f65 | 70 | } else if(atoi(strno) >0 && atoi(strno)<5) { |
bosko001 | 2:4deba4264f65 | 71 | boja = 4; |
bosko001 | 2:4deba4264f65 | 72 | } else { |
bosko001 | 2:4deba4264f65 | 73 | boja = 2; |
bosko001 | 2:4deba4264f65 | 74 | } |
bosko001 | 2:4deba4264f65 | 75 | printf("broj: %d - boja: %d\n\r", atoi(strno),boja); |
bosko001 | 2:4deba4264f65 | 76 | if (atoi(strno) < 0) |
bosko001 | 2:4deba4264f65 | 77 | putOnDisp("0", boja); |
bosko001 | 2:4deba4264f65 | 78 | else |
bosko001 | 2:4deba4264f65 | 79 | putOnDisp((char*)strno, boja); |
markoc | 1:10e2a0bef1b4 | 80 | } |
markoc | 1:10e2a0bef1b4 | 81 | |
markoc | 1:10e2a0bef1b4 | 82 | |
markoc | 1:10e2a0bef1b4 | 83 | |
bosko001 | 0:b01306ccbbe1 | 84 | |
bosko001 | 0:b01306ccbbe1 | 85 | int main(void) |
bosko001 | 0:b01306ccbbe1 | 86 | { |
bosko001 | 7:6a700c98b9b5 | 87 | printf("\n\r----------------Pocetak------------ \n\r"); fflush(stdout); |
bosko001 | 2:4deba4264f65 | 88 | |
bosko001 | 5:ecaf089d1c8a | 89 | init_disp( ); |
bosko001 | 7:6a700c98b9b5 | 90 | thread_sleep_for(2000); |
bosko001 | 7:6a700c98b9b5 | 91 | |
bosko001 | 7:6a700c98b9b5 | 92 | putOnDisp("elcom", 3); |
bosko001 | 0:b01306ccbbe1 | 93 | |
bosko001 | 2:4deba4264f65 | 94 | if( (err = eth.set_network(IP_ADDR,NET_MASK,GATW_ADDR))!= NSAPI_ERROR_OK ) { |
bosko001 | 2:4deba4264f65 | 95 | printf(" greska setovanja mreze %d \n\r", err); |
bosko001 | 2:4deba4264f65 | 96 | return 1; |
bosko001 | 2:4deba4264f65 | 97 | } |
bosko001 | 2:4deba4264f65 | 98 | if( (err = eth.connect()) != NSAPI_ERROR_OK ) { |
bosko001 | 2:4deba4264f65 | 99 | printf(" greska konekcije %d \n\r", err); |
bosko001 | 2:4deba4264f65 | 100 | return 1; |
bosko001 | 2:4deba4264f65 | 101 | } |
bosko001 | 2:4deba4264f65 | 102 | |
bosko001 | 0:b01306ccbbe1 | 103 | const char *ip = eth.get_ip_address() ; |
bosko001 | 2:4deba4264f65 | 104 | |
bosko001 | 2:4deba4264f65 | 105 | |
bosko001 | 2:4deba4264f65 | 106 | printf( "MAC adresa: %s\n\r", eth.get_mac_address() ); |
bosko001 | 0:b01306ccbbe1 | 107 | printf( "IP adresa: %s\n\r", ip); |
bosko001 | 0:b01306ccbbe1 | 108 | printf( "net mask: %s\n\r", eth.get_netmask() ); |
bosko001 | 0:b01306ccbbe1 | 109 | printf( "GW adresa: %s\n\r", eth.get_gateway() ); |
bosko001 | 2:4deba4264f65 | 110 | |
markoc | 1:10e2a0bef1b4 | 111 | SocketAddress sa("192.168.2.254", 12197); |
bosko001 | 2:4deba4264f65 | 112 | |
bosko001 | 7:6a700c98b9b5 | 113 | struct s_rm s_crm = { ð, &sa, crm_req, crm_fun, 0 }; |
bosko001 | 7:6a700c98b9b5 | 114 | struct s_rm s_srm = { ð, &sa, srm_req, srm_fun, 0 }; |
bosko001 | 2:4deba4264f65 | 115 | |
bosko001 | 2:4deba4264f65 | 116 | |
bosko001 | 2:4deba4264f65 | 117 | putOnDisp("flash", 2); |
bosko001 | 2:4deba4264f65 | 118 | |
bosko001 | 2:4deba4264f65 | 119 | Thread CRMThread; |
bosko001 | 2:4deba4264f65 | 120 | CRMThread.start( callback( requestMessageThread, &s_crm )); |
bosko001 | 2:4deba4264f65 | 121 | CRMThread.join(); |
bosko001 | 0:b01306ccbbe1 | 122 | int i=0; |
bosko001 | 2:4deba4264f65 | 123 | // thread_sleep_for(15000); |
bosko001 | 2:4deba4264f65 | 124 | |
bosko001 | 2:4deba4264f65 | 125 | while(true) { |
bosko001 | 2:4deba4264f65 | 126 | thread_sleep_for(5000); |
bosko001 | 2:4deba4264f65 | 127 | |
bosko001 | 2:4deba4264f65 | 128 | Thread SRMThread; |
bosko001 | 7:6a700c98b9b5 | 129 | s_srm.response = 0; |
bosko001 | 2:4deba4264f65 | 130 | SRMThread.start( callback( requestMessageThread, &s_srm )); |
bosko001 | 2:4deba4264f65 | 131 | SRMThread.join(); |
bosko001 | 7:6a700c98b9b5 | 132 | printf("status response %d\n\r",s_srm.response); |
bosko001 | 0:b01306ccbbe1 | 133 | } |
bosko001 | 2:4deba4264f65 | 134 | // printf("\n\rmain: Kraj, error=%d\n\r",err); fflush(stdout); |
bosko001 | 0:b01306ccbbe1 | 135 | } |
bosko001 | 0:b01306ccbbe1 | 136 | |
bosko001 | 0:b01306ccbbe1 | 137 | |
bosko001 | 0:b01306ccbbe1 | 138 | |
bosko001 | 0:b01306ccbbe1 | 139 | |
bosko001 | 2:4deba4264f65 | 140 | unsigned char sendBuffer[]= "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: text/html\r\n\r\nMarko je ovde"; // HTTP Message Body, length = 11 |
bosko001 | 0:b01306ccbbe1 | 141 | unsigned char sb[1000]="HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n"; |
bosko001 | 0:b01306ccbbe1 | 142 | |
bosko001 | 0:b01306ccbbe1 | 143 | |
bosko001 | 0:b01306ccbbe1 | 144 | |
bosko001 | 0:b01306ccbbe1 | 145 | unsigned char txbuf[100]="test konekcije"; |
bosko001 | 0:b01306ccbbe1 | 146 | unsigned char rxbuf[100]; |
bosko001 | 0:b01306ccbbe1 | 147 | |
bosko001 | 0:b01306ccbbe1 | 148 | |
markoc | 1:10e2a0bef1b4 | 149 | char conf_server_ip[] = "192.168.1.26"; |
markoc | 1:10e2a0bef1b4 | 150 | int conf_server_port = 12197; |
markoc | 1:10e2a0bef1b4 | 151 | unsigned char msg_request_message[] = "{5|1|1;2}"; |
markoc | 1:10e2a0bef1b4 | 152 | unsigned char msg_catalogue_request_message[] = "{1|1}"; |
markoc | 1:10e2a0bef1b4 | 153 | |
markoc | 1:10e2a0bef1b4 | 154 | |
markoc | 1:10e2a0bef1b4 | 155 | |
bosko001 | 2:4deba4264f65 | 156 | void requestMessageThread(struct s_rm *s) |
markoc | 1:10e2a0bef1b4 | 157 | { |
bosko001 | 0:b01306ccbbe1 | 158 | |
bosko001 | 2:4deba4264f65 | 159 | TCPSocket tcpSocket; |
bosko001 | 2:4deba4264f65 | 160 | nsapi_error_t err=NULL; |
bosko001 | 2:4deba4264f65 | 161 | char rxbuf[1024]; |
bosko001 | 2:4deba4264f65 | 162 | memset(rxbuf, 0, sizeof(rxbuf)); |
bosko001 | 2:4deba4264f65 | 163 | if((err=tcpSocket.open( s->e )) ==0 ) { |
bosko001 | 2:4deba4264f65 | 164 | if((err = tcpSocket.connect(*(s->sa))) == 0) { |
bosko001 | 2:4deba4264f65 | 165 | char *ss = s->tx_msg; |
bosko001 | 4:0aec01df33d2 | 166 | if( (err = tcpSocket.send(s->tx_msg,strlen(ss))<0 ) ) { |
bosko001 | 2:4deba4264f65 | 167 | printf("requestMessage - send err: %d\n\r", err); |
bosko001 | 2:4deba4264f65 | 168 | } else { |
bosko001 | 2:4deba4264f65 | 169 | printf("requestMessage - poslano: %d bajtova od: %s\n\r", err, s->tx_msg); |
bosko001 | 2:4deba4264f65 | 170 | if( (err = tcpSocket.recv(rxbuf,sizeof(rxbuf)) <0 )) { |
bosko001 | 2:4deba4264f65 | 171 | printf("requestMessage - recv err %d\n\r", err); |
bosko001 | 2:4deba4264f65 | 172 | } else { |
bosko001 | 2:4deba4264f65 | 173 | |
bosko001 | 2:4deba4264f65 | 174 | printf("requestMessage - prim tel %d: %s\n\r", err, rxbuf) ; |
bosko001 | 2:4deba4264f65 | 175 | s->fun_rec( rxbuf ); |
bosko001 | 7:6a700c98b9b5 | 176 | s->response = dispGetResponce( ); |
bosko001 | 7:6a700c98b9b5 | 177 | |
markoc | 1:10e2a0bef1b4 | 178 | } |
markoc | 1:10e2a0bef1b4 | 179 | } |
markoc | 1:10e2a0bef1b4 | 180 | } |
markoc | 1:10e2a0bef1b4 | 181 | |
bosko001 | 2:4deba4264f65 | 182 | printf("requestMessage - zatvaranje konekcije\n\r"); |
bosko001 | 2:4deba4264f65 | 183 | tcpSocket.close(); |
bosko001 | 2:4deba4264f65 | 184 | } |
bosko001 | 2:4deba4264f65 | 185 | |
bosko001 | 2:4deba4264f65 | 186 | } |
bosko001 | 0:b01306ccbbe1 | 187 | |
bosko001 | 0:b01306ccbbe1 | 188 | // |
bosko001 | 0:b01306ccbbe1 | 189 | //enum nsapi_error { |
bosko001 | 0:b01306ccbbe1 | 190 | // NSAPI_ERROR_OK = 0, /*!< no error */ |
bosko001 | 0:b01306ccbbe1 | 191 | // NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */ |
bosko001 | 0:b01306ccbbe1 | 192 | // NSAPI_ERROR_UNSUPPORTED = -3002, /*!< unsupported functionality */ |
bosko001 | 0:b01306ccbbe1 | 193 | // NSAPI_ERROR_PARAMETER = -3003, /*!< invalid configuration */ |
bosko001 | 0:b01306ccbbe1 | 194 | // NSAPI_ERROR_NO_CONNECTION = -3004, /*!< not connected to a network */ |
bosko001 | 0:b01306ccbbe1 | 195 | // NSAPI_ERROR_NO_SOCKET = -3005, /*!< socket not available for use */ |
bosko001 | 0:b01306ccbbe1 | 196 | // NSAPI_ERROR_NO_ADDRESS = -3006, /*!< IP address is not known */ |
bosko001 | 0:b01306ccbbe1 | 197 | // NSAPI_ERROR_NO_MEMORY = -3007, /*!< memory resource not available */ |
bosko001 | 0:b01306ccbbe1 | 198 | // NSAPI_ERROR_NO_SSID = -3008, /*!< ssid not found */ |
bosko001 | 0:b01306ccbbe1 | 199 | // NSAPI_ERROR_DNS_FAILURE = -3009, /*!< DNS failed to complete successfully */ |
bosko001 | 0:b01306ccbbe1 | 200 | // NSAPI_ERROR_DHCP_FAILURE = -3010, /*!< DHCP failed to complete successfully */ |
bosko001 | 0:b01306ccbbe1 | 201 | // NSAPI_ERROR_AUTH_FAILURE = -3011, /*!< connection to access point failed */ |
bosko001 | 0:b01306ccbbe1 | 202 | // NSAPI_ERROR_DEVICE_ERROR = -3012, /*!< failure interfacing with the network processor */ |
bosko001 | 0:b01306ccbbe1 | 203 | // NSAPI_ERROR_IN_PROGRESS = -3013, /*!< operation (eg connect) in progress */ |
bosko001 | 0:b01306ccbbe1 | 204 | // NSAPI_ERROR_ALREADY = -3014, /*!< operation (eg connect) already in progress */ |
bosko001 | 0:b01306ccbbe1 | 205 | // NSAPI_ERROR_IS_CONNECTED = -3015, /*!< socket is already connected */ |
bosko001 | 0:b01306ccbbe1 | 206 | // NSAPI_ERROR_CONNECTION_LOST = -3016, /*!< connection lost */ |
bosko001 | 0:b01306ccbbe1 | 207 | // NSAPI_ERROR_CONNECTION_TIMEOUT = -3017, /*!< connection timed out */ |
bosko001 | 0:b01306ccbbe1 | 208 | //}; |
bosko001 | 0:b01306ccbbe1 | 209 | |
bosko001 | 0:b01306ccbbe1 | 210 |