nova verzija

Committer:
bosko001
Date:
Fri Feb 21 11:44:48 2020 +0000
Revision:
2:4deba4264f65
Parent:
1:10e2a0bef1b4
nova verzija

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