Added new services.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@11770 110e8d01-0319-4d1e-a829-52ad28d1bb01
This commit is contained in:
isiora 2018-03-15 12:24:14 +00:00
parent 19c6dfea6d
commit f9adf287c9
3 changed files with 80 additions and 16 deletions

View File

@ -141,6 +141,7 @@ CSRC = $(STARTUPSRC) \
$(TESTSRC) \
$(CHIBIOS)/os/hal/lib/streams/chprintf.c \
tservices.c \
proxies/tssockstub.c \
main.c
# C++ sources that can be compiled in ARM or THUMB mode depending on the global

View File

@ -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.

View File

@ -24,7 +24,8 @@
#include "ch.h"
#include "hal.h"
#include "chtssi.h"
#include "tservices.h"
#include "proxies/tssockstub.h"
#include "chprintf.h"
/*===========================================================================*/
@ -50,28 +51,35 @@
static THD_WORKING_AREA(waTsSimpleService, 1024);
static THD_FUNCTION(TsSimpleService, tsstate) {
/* WARNING: do not put blocking call out of the cycle,
i.e. no calls that suspend
the current thread!.*/
BaseSequentialStream *ssp = (BaseSequentialStream*)&SD1;
ts_state_t *svcp = tsstate;
/* Start the request/process/response cycle.*/
while (tssiWaitRequest(tsstate) == SMC_SVC_OK) {
/* Start the 'wait request / process / response' cycle.*/
for (;/* ever */;) {
int i;
chprintf((BaseSequentialStream*)&SD1,
"TsSimpleService received a new request.\r\n");
/* Wait a service request.*/
msg_t r = tssiWaitRequest(tsstate);
/* Check if status is ko. It could not happen.*/
if (r != SMC_SVC_OK) {
chprintf(ssp, "Unexpected wait request error.\r\n");
continue;
}
/* Process the request.*/
chprintf(ssp, "r = %d, TsSimpleService received a new request.\r\n", r);
if (svcp->ts_datalen > 0) {
*(TS_GET_DATA(svcp) + TS_GET_DATALEN(svcp) - 1) = '\0';
chprintf((BaseSequentialStream*)&SD1,
"My non secure 'alter ego' has a request.\r\n");
chprintf((BaseSequentialStream*)&SD1,
"She tells: '");
chprintf((BaseSequentialStream*)&SD1, TS_GET_DATA(svcp));
chprintf((BaseSequentialStream*)&SD1, "'\r\n");
chprintf(ssp, "My non secure 'alter ego' has a request.\r\n");
chprintf(ssp, "She tells: '");
chprintf(ssp, TS_GET_DATA(svcp));
chprintf(ssp, "'\r\n");
}
for (i = 0; i < 100000; ++i)
;
/* Set the response.*/
TS_SET_STATUS(svcp, i);
}
@ -89,6 +97,7 @@ static THD_FUNCTION(TsSimpleService, tsstate) {
TS_STATE_TABLE
TS_CONF_TABLE_BEGIN
TS_CONF_TABLE_ENTRY("TsSimpleService", waTsSimpleService, TS_BASE_PRIO, TsSimpleService, TS_STATE(0))
TS_CONF_TABLE_ENTRY("TsStubsService", waTsStubsService, TS_BASE_PRIO+10, TsStubsService, TS_STATE(1))
TS_CONF_TABLE_END
/** @} */