EthTCPclient_05

Committer:
bosko001
Date:
Sat Feb 22 17:17:54 2020 +0000
Revision:
4:0aec01df33d2
Parent:
3:591190524a70
Child:
5:ecaf089d1c8a
ttt

Who changed what in which revision?

UserRevisionLine numberNew 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 2:4deba4264f65 42 DigitalOut tast(PTB19,0);
bosko001 4:0aec01df33d2 43 ////RawSerial rs485(PTC17,PTC16);
bosko001 0:b01306ccbbe1 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*);
markoc 1:10e2a0bef1b4 51 };
markoc 1:10e2a0bef1b4 52
bosko001 2:4deba4264f65 53 char crm_req[]= {'{','1','|','1','}'}; //"{1|1}";
markoc 1:10e2a0bef1b4 54 char crm_resp[100];
markoc 1:10e2a0bef1b4 55 void crm_fun( char *rx)
markoc 1:10e2a0bef1b4 56 {
bosko001 2:4deba4264f65 57 printf("crm_resp: %s\n\r",rx);
bosko001 2:4deba4264f65 58 strcmp(crm_resp, rx);
markoc 1:10e2a0bef1b4 59 }
markoc 1:10e2a0bef1b4 60
bosko001 2:4deba4264f65 61 char srm_req[]="{5|1|0}";
markoc 1:10e2a0bef1b4 62 void srm_fun( char *rx )
markoc 1:10e2a0bef1b4 63 {
bosko001 2:4deba4264f65 64 printf("srm_resp: %s\n\r",rx);
bosko001 2:4deba4264f65 65 char *strno= extract_string(';',rx, 2);
bosko001 2:4deba4264f65 66 char boja = 0;
bosko001 2:4deba4264f65 67 if(atoi(strno) <= 0) {
bosko001 2:4deba4264f65 68 boja = 1;
bosko001 2:4deba4264f65 69 } else if(atoi(strno) >0 && atoi(strno)<5) {
bosko001 2:4deba4264f65 70 boja = 4;
bosko001 2:4deba4264f65 71 } else {
bosko001 2:4deba4264f65 72 boja = 2;
bosko001 2:4deba4264f65 73 }
bosko001 2:4deba4264f65 74 printf("broj: %d - boja: %d\n\r", atoi(strno),boja);
bosko001 2:4deba4264f65 75 if (atoi(strno) < 0)
bosko001 2:4deba4264f65 76 putOnDisp("0", boja);
bosko001 2:4deba4264f65 77 else
bosko001 2:4deba4264f65 78 putOnDisp((char*)strno, boja);
markoc 1:10e2a0bef1b4 79 }
markoc 1:10e2a0bef1b4 80
markoc 1:10e2a0bef1b4 81
markoc 1:10e2a0bef1b4 82
bosko001 0:b01306ccbbe1 83
bosko001 0:b01306ccbbe1 84 int main(void)
bosko001 0:b01306ccbbe1 85 {
bosko001 2:4deba4264f65 86
bosko001 2:4deba4264f65 87
bosko001 2:4deba4264f65 88
bosko001 2:4deba4264f65 89 rs485.format(8, mbed::RawSerial::Even, 1);
markoc 1:10e2a0bef1b4 90
bosko001 0:b01306ccbbe1 91 Thread t;
bosko001 2:4deba4264f65 92 // t.start( slanjefun );
bosko001 2:4deba4264f65 93
bosko001 2:4deba4264f65 94 putOnDisp("elcom", 3);
bosko001 0:b01306ccbbe1 95
bosko001 2:4deba4264f65 96 if( (err = eth.set_network(IP_ADDR,NET_MASK,GATW_ADDR))!= NSAPI_ERROR_OK ) {
bosko001 2:4deba4264f65 97 printf(" greska setovanja mreze %d \n\r", err);
bosko001 2:4deba4264f65 98 return 1;
bosko001 2:4deba4264f65 99 }
bosko001 2:4deba4264f65 100 if( (err = eth.connect()) != NSAPI_ERROR_OK ) {
bosko001 2:4deba4264f65 101 printf(" greska konekcije %d \n\r", err);
bosko001 2:4deba4264f65 102 return 1;
bosko001 2:4deba4264f65 103 }
bosko001 2:4deba4264f65 104
bosko001 0:b01306ccbbe1 105 const char *ip = eth.get_ip_address() ;
bosko001 2:4deba4264f65 106
bosko001 2:4deba4264f65 107
bosko001 2:4deba4264f65 108 printf( "MAC adresa: %s\n\r", eth.get_mac_address() );
bosko001 0:b01306ccbbe1 109 printf( "IP adresa: %s\n\r", ip);
bosko001 0:b01306ccbbe1 110 printf( "net mask: %s\n\r", eth.get_netmask() );
bosko001 0:b01306ccbbe1 111 printf( "GW adresa: %s\n\r", eth.get_gateway() );
bosko001 2:4deba4264f65 112
markoc 1:10e2a0bef1b4 113 SocketAddress sa("192.168.2.254", 12197);
bosko001 2:4deba4264f65 114
bosko001 2:4deba4264f65 115 struct s_rm s_crm = { &eth, &sa, crm_req, crm_fun };
bosko001 2:4deba4264f65 116 struct s_rm s_srm = { &eth, &sa, srm_req, srm_fun };
bosko001 2:4deba4264f65 117
bosko001 2:4deba4264f65 118
bosko001 2:4deba4264f65 119 putOnDisp("flash", 2);
bosko001 2:4deba4264f65 120
bosko001 2:4deba4264f65 121 Thread CRMThread;
bosko001 2:4deba4264f65 122 CRMThread.start( callback( requestMessageThread, &s_crm ));
bosko001 2:4deba4264f65 123 CRMThread.join();
bosko001 0:b01306ccbbe1 124 int i=0;
bosko001 2:4deba4264f65 125 // thread_sleep_for(15000);
bosko001 2:4deba4264f65 126
bosko001 2:4deba4264f65 127 while(true) {
bosko001 2:4deba4264f65 128 thread_sleep_for(5000);
bosko001 2:4deba4264f65 129
bosko001 2:4deba4264f65 130 Thread SRMThread;
bosko001 2:4deba4264f65 131 SRMThread.start( callback( requestMessageThread, &s_srm ));
bosko001 2:4deba4264f65 132 SRMThread.join();
bosko001 2:4deba4264f65 133 printf("main %d\n\r",i++);
bosko001 0:b01306ccbbe1 134 }
bosko001 2:4deba4264f65 135 // printf("\n\rmain: Kraj, error=%d\n\r",err); fflush(stdout);
bosko001 0:b01306ccbbe1 136 }
bosko001 0:b01306ccbbe1 137
bosko001 0:b01306ccbbe1 138
bosko001 0:b01306ccbbe1 139
bosko001 0:b01306ccbbe1 140
bosko001 2:4deba4264f65 141 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 142 unsigned char sb[1000]="HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n";
bosko001 0:b01306ccbbe1 143
bosko001 0:b01306ccbbe1 144
bosko001 0:b01306ccbbe1 145
bosko001 0:b01306ccbbe1 146 unsigned char txbuf[100]="test konekcije";
bosko001 0:b01306ccbbe1 147 unsigned char rxbuf[100];
bosko001 0:b01306ccbbe1 148
bosko001 0:b01306ccbbe1 149
markoc 1:10e2a0bef1b4 150 char conf_server_ip[] = "192.168.1.26";
markoc 1:10e2a0bef1b4 151 int conf_server_port = 12197;
markoc 1:10e2a0bef1b4 152 unsigned char msg_request_message[] = "{5|1|1;2}";
markoc 1:10e2a0bef1b4 153 unsigned char msg_catalogue_request_message[] = "{1|1}";
markoc 1:10e2a0bef1b4 154
markoc 1:10e2a0bef1b4 155
markoc 1:10e2a0bef1b4 156
bosko001 2:4deba4264f65 157 void requestMessageThread(struct s_rm *s)
markoc 1:10e2a0bef1b4 158 {
bosko001 0:b01306ccbbe1 159
bosko001 2:4deba4264f65 160 TCPSocket tcpSocket;
bosko001 2:4deba4264f65 161 nsapi_error_t err=NULL;
bosko001 2:4deba4264f65 162 char rxbuf[1024];
bosko001 2:4deba4264f65 163 memset(rxbuf, 0, sizeof(rxbuf));
bosko001 2:4deba4264f65 164 if((err=tcpSocket.open( s->e )) ==0 ) {
bosko001 2:4deba4264f65 165 if((err = tcpSocket.connect(*(s->sa))) == 0) {
bosko001 2:4deba4264f65 166 char *ss = s->tx_msg;
bosko001 4:0aec01df33d2 167 if( (err = tcpSocket.send(s->tx_msg,strlen(ss))<0 ) ) {
bosko001 2:4deba4264f65 168 printf("requestMessage - send err: %d\n\r", err);
bosko001 2:4deba4264f65 169 } else {
bosko001 2:4deba4264f65 170 printf("requestMessage - poslano: %d bajtova od: %s\n\r", err, s->tx_msg);
bosko001 2:4deba4264f65 171 if( (err = tcpSocket.recv(rxbuf,sizeof(rxbuf)) <0 )) {
bosko001 2:4deba4264f65 172 printf("requestMessage - recv err %d\n\r", err);
bosko001 2:4deba4264f65 173 } else {
bosko001 2:4deba4264f65 174
bosko001 2:4deba4264f65 175 printf("requestMessage - prim tel %d: %s\n\r", err, rxbuf) ;
bosko001 2:4deba4264f65 176 s->fun_rec( rxbuf );
markoc 1:10e2a0bef1b4 177 }
markoc 1:10e2a0bef1b4 178 }
markoc 1:10e2a0bef1b4 179 }
markoc 1:10e2a0bef1b4 180
bosko001 2:4deba4264f65 181 printf("requestMessage - zatvaranje konekcije\n\r");
bosko001 2:4deba4264f65 182 tcpSocket.close();
bosko001 2:4deba4264f65 183 }
bosko001 2:4deba4264f65 184
bosko001 2:4deba4264f65 185 }
bosko001 0:b01306ccbbe1 186
bosko001 0:b01306ccbbe1 187 //
bosko001 0:b01306ccbbe1 188 //enum nsapi_error {
bosko001 0:b01306ccbbe1 189 // NSAPI_ERROR_OK = 0, /*!< no error */
bosko001 0:b01306ccbbe1 190 // NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */
bosko001 0:b01306ccbbe1 191 // NSAPI_ERROR_UNSUPPORTED = -3002, /*!< unsupported functionality */
bosko001 0:b01306ccbbe1 192 // NSAPI_ERROR_PARAMETER = -3003, /*!< invalid configuration */
bosko001 0:b01306ccbbe1 193 // NSAPI_ERROR_NO_CONNECTION = -3004, /*!< not connected to a network */
bosko001 0:b01306ccbbe1 194 // NSAPI_ERROR_NO_SOCKET = -3005, /*!< socket not available for use */
bosko001 0:b01306ccbbe1 195 // NSAPI_ERROR_NO_ADDRESS = -3006, /*!< IP address is not known */
bosko001 0:b01306ccbbe1 196 // NSAPI_ERROR_NO_MEMORY = -3007, /*!< memory resource not available */
bosko001 0:b01306ccbbe1 197 // NSAPI_ERROR_NO_SSID = -3008, /*!< ssid not found */
bosko001 0:b01306ccbbe1 198 // NSAPI_ERROR_DNS_FAILURE = -3009, /*!< DNS failed to complete successfully */
bosko001 0:b01306ccbbe1 199 // NSAPI_ERROR_DHCP_FAILURE = -3010, /*!< DHCP failed to complete successfully */
bosko001 0:b01306ccbbe1 200 // NSAPI_ERROR_AUTH_FAILURE = -3011, /*!< connection to access point failed */
bosko001 0:b01306ccbbe1 201 // NSAPI_ERROR_DEVICE_ERROR = -3012, /*!< failure interfacing with the network processor */
bosko001 0:b01306ccbbe1 202 // NSAPI_ERROR_IN_PROGRESS = -3013, /*!< operation (eg connect) in progress */
bosko001 0:b01306ccbbe1 203 // NSAPI_ERROR_ALREADY = -3014, /*!< operation (eg connect) already in progress */
bosko001 0:b01306ccbbe1 204 // NSAPI_ERROR_IS_CONNECTED = -3015, /*!< socket is already connected */
bosko001 0:b01306ccbbe1 205 // NSAPI_ERROR_CONNECTION_LOST = -3016, /*!< connection lost */
bosko001 0:b01306ccbbe1 206 // NSAPI_ERROR_CONNECTION_TIMEOUT = -3017, /*!< connection timed out */
bosko001 0:b01306ccbbe1 207 //};
bosko001 0:b01306ccbbe1 208
bosko001 0:b01306ccbbe1 209
bosko001 0:b01306ccbbe1 210
bosko001 4:0aec01df33d2 211 //
bosko001 4:0aec01df33d2 212 //
bosko001 4:0aec01df33d2 213 //void ev(int)
bosko001 4:0aec01df33d2 214 //{
bosko001 4:0aec01df33d2 215 // tast=0;
bosko001 4:0aec01df33d2 216 //}
bosko001 4:0aec01df33d2 217 //
bosko001 4:0aec01df33d2 218 //
bosko001 4:0aec01df33d2 219 //
bosko001 4:0aec01df33d2 220 //unsigned char buf_tel[100] = {0x71,0x00,0x0D,0x71,0x43,0x1,0x1B,0x4A,0x01,0x01, /*0x1b,0x45,0x1,*/ 0x1B,0x43,1,0x33,0xD5,0x16};
bosko001 4:0aec01df33d2 221 //#define NO_BEFORE_DATA 13
bosko001 4:0aec01df33d2 222 //#define NO_WITH_DATA (NO_BEFORE_DATA + strlen(s))
bosko001 4:0aec01df33d2 223 //
bosko001 4:0aec01df33d2 224 //void putOnDisp( char *s, char boja)
bosko001 4:0aec01df33d2 225 //{
bosko001 4:0aec01df33d2 226 // int ubb = NO_BEFORE_DATA+strlen(s)+1+1-4-2; // ubb = ukupan broj bajtova
bosko001 4:0aec01df33d2 227 // buf_tel[NO_BEFORE_DATA-1]=boja;
bosko001 4:0aec01df33d2 228 // memcpy(buf_tel+NO_BEFORE_DATA, s, strlen(s)+2);
bosko001 4:0aec01df33d2 229 //
bosko001 4:0aec01df33d2 230 // char suma=0;
bosko001 4:0aec01df33d2 231 // for(int i = 4; i < NO_WITH_DATA; i++) {
bosko001 4:0aec01df33d2 232 // suma +=buf_tel[i];
bosko001 4:0aec01df33d2 233 // }
bosko001 4:0aec01df33d2 234 //
bosko001 4:0aec01df33d2 235 // buf_tel[NO_WITH_DATA] = suma;
bosko001 4:0aec01df33d2 236 // buf_tel[NO_WITH_DATA+1] = 22;
bosko001 4:0aec01df33d2 237 // buf_tel[1]=ubb>>8;
bosko001 4:0aec01df33d2 238 // buf_tel[2]=ubb;
bosko001 4:0aec01df33d2 239 //
bosko001 4:0aec01df33d2 240 //
bosko001 4:0aec01df33d2 241 // tast=1;
bosko001 4:0aec01df33d2 242 // rs485.write(buf_tel, NO_WITH_DATA+2+2,ev);
bosko001 4:0aec01df33d2 243 //}
bosko001 4:0aec01df33d2 244 //
bosko001 4:0aec01df33d2 245 //void putOnDispNo(int broj, char boja)
bosko001 4:0aec01df33d2 246 //{
bosko001 4:0aec01df33d2 247 // char s[10];
bosko001 4:0aec01df33d2 248 // sprintf( s, "%d", broj);
bosko001 4:0aec01df33d2 249 // putOnDisp( s, boja);
bosko001 4:0aec01df33d2 250 //}
bosko001 4:0aec01df33d2 251 //
bosko001 4:0aec01df33d2 252 //
bosko001 4:0aec01df33d2 253 //void slanjefun()
bosko001 4:0aec01df33d2 254 //{
bosko001 4:0aec01df33d2 255 //
bosko001 4:0aec01df33d2 256 // int j=0;
bosko001 4:0aec01df33d2 257 //
bosko001 4:0aec01df33d2 258 //
bosko001 4:0aec01df33d2 259 // while (true) {
bosko001 4:0aec01df33d2 260 //// printf("disp thred %d\n\r",j);
bosko001 4:0aec01df33d2 261 // if(j >10 && j<15 ) putOnDisp("des",1);
bosko001 4:0aec01df33d2 262 // else putOnDispNo(j,1);
bosko001 4:0aec01df33d2 263 // j++;
bosko001 4:0aec01df33d2 264 // thread_sleep_for(1000);
bosko001 4:0aec01df33d2 265 // }
bosko001 4:0aec01df33d2 266 //}
bosko001 4:0aec01df33d2 267 //
bosko001 4:0aec01df33d2 268 //
bosko001 4:0aec01df33d2 269 //char * extract_string( char delimiter, char *ulazni_str, int n_str )
bosko001 4:0aec01df33d2 270 //{
bosko001 4:0aec01df33d2 271 // int br_str=0;
bosko001 4:0aec01df33d2 272 // char *ret_str=ulazni_str;
bosko001 4:0aec01df33d2 273 // char* ptr = ulazni_str;
bosko001 4:0aec01df33d2 274 //
bosko001 4:0aec01df33d2 275 // while(*ptr) {
bosko001 4:0aec01df33d2 276 // if(*ptr == delimiter) {
bosko001 4:0aec01df33d2 277 // *ptr = 0;
bosko001 4:0aec01df33d2 278 // if( br_str == n_str ) return ret_str;
bosko001 4:0aec01df33d2 279 // else {
bosko001 4:0aec01df33d2 280 // br_str++;
bosko001 4:0aec01df33d2 281 // ret_str = ptr + 1;
bosko001 4:0aec01df33d2 282 // }
bosko001 4:0aec01df33d2 283 // }
bosko001 4:0aec01df33d2 284 // ptr++;
bosko001 4:0aec01df33d2 285 // }
bosko001 4:0aec01df33d2 286 // if( br_str == n_str ) return ret_str;
bosko001 4:0aec01df33d2 287 // return NULL;
bosko001 4:0aec01df33d2 288 //}
bosko001 4:0aec01df33d2 289 //