diff options
Diffstat (limited to 'demos/ATSAMA5D2/RT-SAMA5D2-XPLAINED-SEC/main.c')
-rwxr-xr-x | demos/ATSAMA5D2/RT-SAMA5D2-XPLAINED-SEC/main.c | 56 |
1 files changed, 55 insertions, 1 deletions
diff --git a/demos/ATSAMA5D2/RT-SAMA5D2-XPLAINED-SEC/main.c b/demos/ATSAMA5D2/RT-SAMA5D2-XPLAINED-SEC/main.c index 837678368..e432e48c1 100755 --- a/demos/ATSAMA5D2/RT-SAMA5D2-XPLAINED-SEC/main.c +++ b/demos/ATSAMA5D2/RT-SAMA5D2-XPLAINED-SEC/main.c @@ -17,9 +17,61 @@ #include "ch.h"
#include "hal.h"
#include "chtssi.h"
+#include "proxies/tssockstub.h"
#include "rt_test_root.h"
#include "oslib_test_root.h"
#include "chprintf.h"
+#include <string.h>
+
+#define SERVER_PORT_NUM 8080
+#define SERVER_IP_ADDRESS "192.136.23.21"
+
+void tcpexample(void) {
+ int socket_fd;
+ struct sockaddr_in ra;
+
+ int recv_data; char data_buffer[80];
+
+ /*
+ * Creates an TCP socket (SOCK_STREAM) with Internet Protocol Family
+ * (PF_INET). Protocol family and Address family related. For example
+ * PF_INET Protocol Family and AF_INET family are coupled.
+ */
+ socket_fd = socket(PF_INET, SOCK_STREAM, 0);
+
+ if ( socket_fd < 0 ) {
+ chprintf((BaseSequentialStream *)&SD1, "socket call failed");
+ return;
+ }
+
+ /* Connects to server ip-address. */
+ memset(&ra, 0, sizeof(struct sockaddr_in));
+ ra.sin_family = AF_INET;
+ ra.sin_addr.s_addr = inet_addr(SERVER_IP_ADDRESS);
+ ra.sin_port = htons(SERVER_PORT_NUM);
+
+
+ if (connect(socket_fd, (struct sockaddr *)&ra,
+ sizeof (struct sockaddr_in)) < 0) {
+ chprintf((BaseSequentialStream *)&SD1, "connect failed \n");
+ close(socket_fd);
+ return;
+ }
+ if (send(socket_fd, "Baba", sizeof "Baba", 0) < 0) {
+ chprintf((BaseSequentialStream *)&SD1, "send failed \n");
+ close(socket_fd);
+ return;
+ }
+ recv_data = recv(socket_fd, data_buffer, sizeof data_buffer, 0);
+ if (recv_data < 0) {
+ chprintf((BaseSequentialStream *)&SD1, "recv failed \n");
+ close(socket_fd);
+ return;
+ }
+ data_buffer[recv_data] = '\0';
+ chprintf((BaseSequentialStream *)&SD1, "received data: %s\n",data_buffer);
+ close(socket_fd);
+}
/*
* LED blinker thread, times are in milliseconds.
@@ -43,6 +95,8 @@ static THD_FUNCTION(Thread1, arg) { chThdSleepMilliseconds(160);
palToggleLine(LINE_LED_BLUE);
chThdSleepMilliseconds(600);
+ tcpexample();
+ chThdSleepMilliseconds(5000);
}
}
@@ -76,7 +130,7 @@ int main(void) { /*
* Creates the blinker thread (and any other ancillary thread).
*/
- chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO+10, Thread1, NULL);
+ chThdCreateStatic(waThread1, sizeof waThread1, NORMALPRIO+10, Thread1, NULL);
/*
* System initializations.
|