Wifly Driver  V1.0
Tcp.c
Go to the documentation of this file.
1 /*
2  * Tcp.c
3  *
4  * Created on: 25/12/2015
5  * Author: Victor
6  */
7 #include "LPC17xx.h"
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include "Type.h"
12 
13 #include "WiFly.h"
14 #include "Tcp.h"
15 #include "FreeRTOS.h"
16 #include "semphr.h"
17 
18 
19 #define TCP_SEND "SET VALUE=%d\r\n"
20 #define TCP_RECEIVE "GET RANDOM VALUE\r\n"
21 #define TCP_SET_RESPONSE "SET OK"
22 #define TCP_GET_RESPONSE "GET OK"
23 
24 #define IP_REMOTE 80
25 #define IP_PROTO 18
26 
27 xSemaphoreHandle xMutexTcp;
28 static char *Tcpssid;
29 static char *TcpPasw;
30 static uint32_t TcpAuthMode;
31 
32 
33 
41 uint8_t Tcp_start(char *ssid, uint32_t AuthMode, char *passw)
42 {
43  uint8_t rtn= FALSE;
44  if( xMutexTcp == NULL )
45  {
46  xMutexTcp = xSemaphoreCreateMutex();
47  }
48  /*S'utilitza el semàfor per inicialitzar el Port */
49  xSemaphoreTakeRecursive( xMutexTcp, portMAX_DELAY );
50  {
51  Tcpssid = ssid;
52  TcpAuthMode =AuthMode;
53  TcpPasw= passw;
54  }
55  xSemaphoreGiveRecursive( xMutexTcp );
56 
57  return rtn;
58 }
59 
60 uint8_t Tcp_sendValue( char *ipAddress, uint32_t remotePort, uint32_t value)
61 {
62  uint8_t rtn= FALSE;
63  char commRemote[128];
64 
65  xSemaphoreTakeRecursive( xMutexTcp, portMAX_DELAY );
66  {
67  if(Wifly_WLANConnectPing(TcpAuthMode, Tcpssid, TcpPasw, ipAddress)==TRUE)
68  {
69  sprintf(commRemote, TCP_SEND,value);
70  if(Wifly_TCPSend(ipAddress, remotePort, TCP_SET_RESPONSE, commRemote)==TRUE)
71  rtn=TRUE;
72 
73  }
74  }
75  xSemaphoreGiveRecursive( xMutexTcp );
76 
77  return rtn;
78 }
79 
80 int32_t Tcp_receiveValue( char *address, uint32_t remotePort)
81 {
82  int32_t value;
83  char* resp;
84 
85  xSemaphoreTakeRecursive( xMutexTcp, portMAX_DELAY );
86  {
87  if(Wifly_WLANConnectPing(TcpAuthMode, Tcpssid, TcpPasw, address)==TRUE)
88  {
89  resp= Wifly_TCPReceive(address, remotePort, TCP_RECEIVE);
90 
91  resp=strstr(resp, "VALUE=") + strlen("VALUE=");
92  value = strtol(resp , &resp, 10);
93 
94  }
95 
96  }
97  xSemaphoreGiveRecursive( xMutexTcp );
98 
99  return value;
100 }
xSemaphoreHandle xMutexTcp
Definition: Tcp.c:27
#define TCP_SET_RESPONSE
Definition: Tcp.c:21
uint8_t Wifly_WLANConnectPing(uint32_t AuthMode, char *ssid, char *passw, char *pingIP)
Definition: WiFly.c:408
#define TCP_RECEIVE
Definition: Tcp.c:20
uint8_t Tcp_start(char *ssid, uint32_t AuthMode, char *passw)
Definition: Tcp.c:41
#define FALSE
Definition: type.h:22
char resp[BUFSIZE]
Definition: Arpalab.c:32
char * Wifly_TCPReceive(char *address, uint32_t remotePort, const char *restrict command,...)
Definition: WiFly.c:1026
#define TRUE
Definition: type.h:26
#define NULL
Definition: type.h:18
uint8_t Wifly_TCPSend(char *address, uint32_t remotePort, char *exResp, const char *restrict command,...)
Definition: WiFly.c:1053
#define TCP_SEND
Definition: Tcp.c:19
int32_t Tcp_receiveValue(char *address, uint32_t remotePort)
Definition: Tcp.c:80
uint8_t Tcp_sendValue(char *ipAddress, uint32_t remotePort, uint32_t value)
Definition: Tcp.c:60