ESP8266 ILI9341 display support code with printf sources, wire-frame viewer and custom fonts  1.0
ESP8266ILI9341DisplayProject
send.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <fcntl.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <netinet/in.h>
9 #include <arpa/inet.h>
10 #include <netdb.h>
11 #include <unistd.h>
12 #include <errno.h>
13 #include <netinet/if_ether.h>
14 #include <net/if.h>
15 #include <linux/sockios.h>
16 
17 typedef unsigned char uint8_t;
18 typedef unsigned short uint16_t;
19 typedef unsigned int uint32_t;
20 
21 //#define TCP_PORT 31416
22 #ifndef TCP_PORT
23 #error Please define TCP_PORT
24 #endif
25 
26 int escape(char *message, int size)
27 {
28  char *buffer;
29  char *bptr;
30  char *mptr;
31  int len;
32  unsigned char c;
33 
34  len = strlen(message);
35  if(len > size)
36  {
37  fprintf(stderr,"message too long\n");
38  len = size;
39  }
40 
41  mptr = message;
42  buffer = calloc(len+2,1);
43  bptr = buffer;
44  if(!buffer)
45  {
46  perror("Can not allocate buffer\n");
47  exit(1);
48  }
49 
50  while(*mptr)
51  {
52  c = *mptr++;
53  if(c == '\\')
54  {
55  if(*mptr)
56  {
57  c = *mptr;
58  if( c == 'n')
59  {
60  c = '\n';
61  mptr++;
62  }
63  else if( c == 'r')
64  {
65  c = '\r';
66  mptr++;
67  }
68  else if( c == 't')
69  {
70  c = '\t';
71  mptr++;
72  }
73  else if( c == 'f')
74  {
75  c = '\f';
76  mptr++;
77  }
78  }
79  }
80  *bptr++ = c;
81  }
82  *bptr = 0;
83  strcpy(message,buffer);
84  return(strlen(message));
85 }
86 
87 void send_message(char *message, char *ip, int port, int echoback)
88 {
89  int sockfd, portno, n;
90  char buffer[4096];
91 
92  struct sockaddr_in dest_addr;
93  struct hostent *server;
94 
95  portno = port;
96  // IF UDP use: AF_INET, SOCK_DGRAM, IPPROTO_UDP
97 
98  // TCP socket
99  sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
100  if (sockfd < 0)
101  {
102  perror("ERROR opening socket");
103  exit(1);
104  }
105 
106  server = gethostbyname(ip);
107  if (server == NULL) {
108  fprintf(stderr,"ERROR, no such host\n");
109  exit(0);
110  }
111  printf("Host name: %s\n", server->h_name);
112 
113  bzero((char *) &dest_addr, sizeof(dest_addr));
114  dest_addr.sin_family = AF_INET;
115  bcopy((char *)server->h_addr,
116  (char *)&dest_addr.sin_addr.s_addr,
117  server->h_length);
118  dest_addr.sin_port = htons(portno);
119  if (connect(sockfd,(struct sockaddr *)&dest_addr,sizeof(dest_addr)) < 0)
120  {
121  perror("ERROR connecting");
122  exit(1);
123  }
124  n = write(sockfd,message,strlen(message)+1);
125  if (n < 0)
126  {
127  perror("ERROR writing to socket");
128  exit(1);
129  }
130 
131  // reads characters coming back from the connection - debugging
132  // interrup to exit
133  // this code blocks waiting for input
134  if(echoback)
135  {
136  do
137  {
138  n = read(sockfd,buffer,sizeof(buffer)-1);
139  write(1, buffer, n);
140  }
141  while(n);
142  }
143 
144  close(sockfd);
145 }
146 
147 
148 int main(int argc,char *argv[])
149 {
150  int i,ret;
151  int echoback = 0;
152  struct net *p;
153  char message[8192];
154  char ip[INET_ADDRSTRLEN];
155  int port_no = TCP_PORT;
156 
157 // htonl() and htons()..
158 
159  memset(ip,0,sizeof(ip)-1);
160  memset(message,0,sizeof(message)-1);
161  for(i=1;i<argc;++i) {
162  if(strcmp(argv[i],"-i") == 0) {
163  strcpy(ip, argv[++i]);
164  }
165  if(strcmp(argv[i],"-m") == 0) {
166  strncpy(message,argv[++i], sizeof(message)-1);
167  }
168  if(strcmp(argv[i],"-p") == 0) {
169  sscanf(argv[++i], "%d", &port_no);
170  }
171  if(strcmp(argv[i],"-e") == 0) {
172  echoback = 1;
173  }
174  }
175 
176  if(!strlen(ip) || !strlen(message))
177  {
178  fprintf(stderr,"usage: -i ip_address -m \"message\" [ -p port ]\n ");
179  exit(1);
180  }
181  if(!strlen(message))
182  {
183  fprintf(stderr,"Message missing\n");
184  exit(1);
185  }
186  escape(message, sizeof(message)-1);
187  printf("ip:%s, port:%d, message:\n%s\n",ip,port_no,message);
188  fflush(stdout);
189  send_message(message, ip, port_no, echoback);
190  return(0);
191 }
MEMSPACE int WEAK_ATR strcmp(const char *str, const char *pat)
Compare two strings.
Definition: stringsup.c:362
unsigned short uint16_t
Definition: send.c:18
MEMSPACE size_t WEAK_ATR strlen(const char *str)
String Length.
Definition: stringsup.c:146
MEMSPACE int close(int fileno)
POSIX Close a file with fileno handel.
Definition: posix.c:685
unsigned int uint32_t
Definition: send.c:19
MEMSPACE void perror(const char *s)
POSIX perror() - convert POSIX errno to text with user message.
Definition: posix.c:1814
MEMSPACE WEAK_ATR char * strncpy(char *dest, const char *src, size_t size)
copy a string of at most N characters
Definition: stringsup.c:179
MEMSPACE int fprintf(FILE *fp, const char *format,...)
fprintf function Example user defined printf function using fputc for I/O This method allows I/O to d...
Definition: posix.c:2484
MEMSPACE ssize_t write(int fd, const void *buf, size_t count)
POSIX Write count bytes from *buf to fileno fd.
Definition: posix.c:1180
MEMSPACE void * calloc(size_t nmemb, size_t size)
calloc buffer POSIX function
Definition: system.c:69
MEMSPACE WEAK_ATR char * strcpy(char *dest, const char *src)
copy a string
Definition: stringsup.c:161
int escape(char *message, int size)
Definition: send.c:26
#define NULL
Definition: cpu.h:55
MEMSPACE ssize_t read(int fd, const void *buf, size_t count)
POSIX read count bytes from *buf to fileno fd.
Definition: posix.c:1006
void send_message(char *message, char *ip, int port, int echoback)
Definition: send.c:87
int main(int argc, char *argv[])
Definition: send.c:148
int sscanf(const char *strp, const char *fmt,...)
#define stdout
Definition: posix.h:270
MEMSPACE int printf(const char *format,...)
#define stderr
Definition: posix.h:271
unsigned char uint8_t
Definition: send.c:17