p1

Committer:
bosko001
Date:
Mon Feb 24 13:55:09 2020 +0000
Revision:
5:ecaf089d1c8a
Parent:
4:0aec01df33d2
Child:
7:be56cf51685d
EthTCPclient_5

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 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*);
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 5:ecaf089d1c8a 88 init_disp( );
markoc 1:10e2a0bef1b4 89
bosko001 0:b01306ccbbe1 90
bosko001 2:4deba4264f65 91 if( (err = eth.set_network(IP_ADDR,NET_MASK,GATW_ADDR))!= NSAPI_ERROR_OK ) {
bosko001 2:4deba4264f65 92 printf(" greska setovanja mreze %d \n\r", err);
bosko001 2:4deba4264f65 93 return 1;
bosko001 2:4deba4264f65 94 }
bosko001 2:4deba4264f65 95 if( (err = eth.connect()) != NSAPI_ERROR_OK ) {
bosko001 2:4deba4264f65 96 printf(" greska konekcije %d \n\r", err);
bosko001 2:4deba4264f65 97 return 1;
bosko001 2:4deba4264f65 98 }
bosko001 2:4deba4264f65 99
bosko001 0:b01306ccbbe1 100 const char *ip = eth.get_ip_address() ;
bosko001 2:4deba4264f65 101
bosko001 2:4deba4264f65 102
bosko001 2:4deba4264f65 103 printf( "MAC adresa: %s\n\r", eth.get_mac_address() );
bosko001 0:b01306ccbbe1 104 printf( "IP adresa: %s\n\r", ip);
bosko001 0:b01306ccbbe1 105 printf( "net mask: %s\n\r", eth.get_netmask() );
bosko001 0:b01306ccbbe1 106 printf( "GW adresa: %s\n\r", eth.get_gateway() );
bosko001 2:4deba4264f65 107
markoc 1:10e2a0bef1b4 108 SocketAddress sa("192.168.2.254", 12197);
bosko001 2:4deba4264f65 109
bosko001 2:4deba4264f65 110 struct s_rm s_crm = { &eth, &sa, crm_req, crm_fun };
bosko001 2:4deba4264f65 111 struct s_rm s_srm = { &eth, &sa, srm_req, srm_fun };
bosko001 2:4deba4264f65 112
bosko001 2:4deba4264f65 113
bosko001 2:4deba4264f65 114 putOnDisp("flash", 2);
bosko001 2:4deba4264f65 115
bosko001 2:4deba4264f65 116 Thread CRMThread;
bosko001 2:4deba4264f65 117 CRMThread.start( callback( requestMessageThread, &s_crm ));
bosko001 2:4deba4264f65 118 CRMThread.join();
bosko001 0:b01306ccbbe1 119 int i=0;
bosko001 2:4deba4264f65 120 // thread_sleep_for(15000);
bosko001 2:4deba4264f65 121
bosko001 2:4deba4264f65 122 while(true) {
bosko001 2:4deba4264f65 123 thread_sleep_for(5000);
bosko001 2:4deba4264f65 124
bosko001 2:4deba4264f65 125 Thread SRMThread;
bosko001 2:4deba4264f65 126 SRMThread.start( callback( requestMessageThread, &s_srm ));
bosko001 2:4deba4264f65 127 SRMThread.join();
bosko001 2:4deba4264f65 128 printf("main %d\n\r",i++);
bosko001 0:b01306ccbbe1 129 }
bosko001 2:4deba4264f65 130 // printf("\n\rmain: Kraj, error=%d\n\r",err); fflush(stdout);
bosko001 0:b01306ccbbe1 131 }
bosko001 0:b01306ccbbe1 132
bosko001 0:b01306ccbbe1 133
bosko001 0:b01306ccbbe1 134
bosko001 0:b01306ccbbe1 135
bosko001 2:4deba4264f65 136 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 137 unsigned char sb[1000]="HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n";
bosko001 0:b01306ccbbe1 138
bosko001 0:b01306ccbbe1 139
bosko001 0:b01306ccbbe1 140
bosko001 0:b01306ccbbe1 141 unsigned char txbuf[100]="test konekcije";
bosko001 0:b01306ccbbe1 142 unsigned char rxbuf[100];
bosko001 0:b01306ccbbe1 143
bosko001 0:b01306ccbbe1 144
markoc 1:10e2a0bef1b4 145 char conf_server_ip[] = "192.168.1.26";
markoc 1:10e2a0bef1b4 146 int conf_server_port = 12197;
markoc 1:10e2a0bef1b4 147 unsigned char msg_request_message[] = "{5|1|1;2}";
markoc 1:10e2a0bef1b4 148 unsigned char msg_catalogue_request_message[] = "{1|1}";
markoc 1:10e2a0bef1b4 149
markoc 1:10e2a0bef1b4 150
markoc 1:10e2a0bef1b4 151
bosko001 2:4deba4264f65 152 void requestMessageThread(struct s_rm *s)
markoc 1:10e2a0bef1b4 153 {
bosko001 0:b01306ccbbe1 154
bosko001 2:4deba4264f65 155 TCPSocket tcpSocket;
bosko001 2:4deba4264f65 156 nsapi_error_t err=NULL;
bosko001 2:4deba4264f65 157 char rxbuf[1024];
bosko001 2:4deba4264f65 158 memset(rxbuf, 0, sizeof(rxbuf));
bosko001 2:4deba4264f65 159 if((err=tcpSocket.open( s->e )) ==0 ) {
bosko001 2:4deba4264f65 160 if((err = tcpSocket.connect(*(s->sa))) == 0) {
bosko001 2:4deba4264f65 161 char *ss = s->tx_msg;
bosko001 4:0aec01df33d2 162 if( (err = tcpSocket.send(s->tx_msg,strlen(ss))<0 ) ) {
bosko001 2:4deba4264f65 163 printf("requestMessage - send err: %d\n\r", err);
bosko001 2:4deba4264f65 164 } else {
bosko001 2:4deba4264f65 165 printf("requestMessage - poslano: %d bajtova od: %s\n\r", err, s->tx_msg);
bosko001 2:4deba4264f65 166 if( (err = tcpSocket.recv(rxbuf,sizeof(rxbuf)) <0 )) {
bosko001 2:4deba4264f65 167 printf("requestMessage - recv err %d\n\r", err);
bosko001 2:4deba4264f65 168 } else {
bosko001 2:4deba4264f65 169
bosko001 2:4deba4264f65 170 printf("requestMessage - prim tel %d: %s\n\r", err, rxbuf) ;
bosko001 2:4deba4264f65 171 s->fun_rec( rxbuf );
markoc 1:10e2a0bef1b4 172 }
markoc 1:10e2a0bef1b4 173 }
markoc 1:10e2a0bef1b4 174 }
markoc 1:10e2a0bef1b4 175
bosko001 2:4deba4264f65 176 printf("requestMessage - zatvaranje konekcije\n\r");
bosko001 2:4deba4264f65 177 tcpSocket.close();
bosko001 2:4deba4264f65 178 }
bosko001 2:4deba4264f65 179
bosko001 2:4deba4264f65 180 }
bosko001 0:b01306ccbbe1 181
bosko001 0:b01306ccbbe1 182 //
bosko001 0:b01306ccbbe1 183 //enum nsapi_error {
bosko001 0:b01306ccbbe1 184 // NSAPI_ERROR_OK = 0, /*!< no error */
bosko001 0:b01306ccbbe1 185 // NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */
bosko001 0:b01306ccbbe1 186 // NSAPI_ERROR_UNSUPPORTED = -3002, /*!< unsupported functionality */
bosko001 0:b01306ccbbe1 187 // NSAPI_ERROR_PARAMETER = -3003, /*!< invalid configuration */
bosko001 0:b01306ccbbe1 188 // NSAPI_ERROR_NO_CONNECTION = -3004, /*!< not connected to a network */
bosko001 0:b01306ccbbe1 189 // NSAPI_ERROR_NO_SOCKET = -3005, /*!< socket not available for use */
bosko001 0:b01306ccbbe1 190 // NSAPI_ERROR_NO_ADDRESS = -3006, /*!< IP address is not known */
bosko001 0:b01306ccbbe1 191 // NSAPI_ERROR_NO_MEMORY = -3007, /*!< memory resource not available */
bosko001 0:b01306ccbbe1 192 // NSAPI_ERROR_NO_SSID = -3008, /*!< ssid not found */
bosko001 0:b01306ccbbe1 193 // NSAPI_ERROR_DNS_FAILURE = -3009, /*!< DNS failed to complete successfully */
bosko001 0:b01306ccbbe1 194 // NSAPI_ERROR_DHCP_FAILURE = -3010, /*!< DHCP failed to complete successfully */
bosko001 0:b01306ccbbe1 195 // NSAPI_ERROR_AUTH_FAILURE = -3011, /*!< connection to access point failed */
bosko001 0:b01306ccbbe1 196 // NSAPI_ERROR_DEVICE_ERROR = -3012, /*!< failure interfacing with the network processor */
bosko001 0:b01306ccbbe1 197 // NSAPI_ERROR_IN_PROGRESS = -3013, /*!< operation (eg connect) in progress */
bosko001 0:b01306ccbbe1 198 // NSAPI_ERROR_ALREADY = -3014, /*!< operation (eg connect) already in progress */
bosko001 0:b01306ccbbe1 199 // NSAPI_ERROR_IS_CONNECTED = -3015, /*!< socket is already connected */
bosko001 0:b01306ccbbe1 200 // NSAPI_ERROR_CONNECTION_LOST = -3016, /*!< connection lost */
bosko001 0:b01306ccbbe1 201 // NSAPI_ERROR_CONNECTION_TIMEOUT = -3017, /*!< connection timed out */
bosko001 0:b01306ccbbe1 202 //};
bosko001 0:b01306ccbbe1 203
bosko001 0:b01306ccbbe1 204