UDP client aangemaakt
This commit is contained in:
parent
dfad42f170
commit
840160250c
@ -52,6 +52,7 @@
|
||||
extern void * GSensorTask(void *arg0);
|
||||
extern void * MQTTTask(void *arg0);
|
||||
extern void * handshakeTask(void *arg0);
|
||||
extern void * UDPClientTask(void *arg0);
|
||||
|
||||
/* Stack size in bytes */
|
||||
#define BIGOLTHREADSTACK 4096
|
||||
@ -84,9 +85,9 @@ int main(void)
|
||||
}
|
||||
|
||||
|
||||
/* ==============================================
|
||||
=== tread for G sensor =======================
|
||||
==============================================
|
||||
/* ===============================================
|
||||
=== thread for G sensor =======================
|
||||
===============================================
|
||||
*/
|
||||
pthread_t GSensorThread;
|
||||
retc = pthread_create(&GSensorThread, &attrs, GSensorTask, NULL);
|
||||
@ -97,9 +98,9 @@ int main(void)
|
||||
}
|
||||
|
||||
|
||||
/* ==============================================
|
||||
=== tread for MQTT ===========================
|
||||
==============================================
|
||||
/* ===============================================
|
||||
=== thread for MQTT ===========================
|
||||
===============================================
|
||||
*/
|
||||
pthread_t MQTTThread;
|
||||
retc = pthread_create(&MQTTThread, &attrs, MQTTTask, NULL);
|
||||
@ -110,18 +111,28 @@ int main(void)
|
||||
}
|
||||
|
||||
|
||||
/* ==============================================
|
||||
=== tread for Handshake ======================
|
||||
==============================================
|
||||
/* ===============================================
|
||||
=== thread for Handshake ======================
|
||||
===============================================
|
||||
*/
|
||||
pthread_t Handshakehread;
|
||||
retc = pthread_create(&Handshakehread, &attrs, handshakeTask, NULL);
|
||||
pthread_t HandshakeThread;
|
||||
retc = pthread_create(&HandshakeThread, &attrs, handshakeTask, NULL);
|
||||
if (retc != 0) {
|
||||
/* pthread_create() failed */
|
||||
// LOG_CRITICAL("main(): failed to create tread for MQTT");
|
||||
while(1);
|
||||
}
|
||||
|
||||
/* ===============================================
|
||||
* == thread for UDP =============================
|
||||
* ===============================================
|
||||
*/
|
||||
pthread_t UDPClientThread;
|
||||
retc = pthread_create(&UDPClientThread, &attrs, UDPClientTask, NULL);
|
||||
if (retc != 0) {
|
||||
while(1);
|
||||
}
|
||||
|
||||
BIOS_start();
|
||||
|
||||
return (0);
|
||||
|
||||
73
src/udp_client.c
Normal file
73
src/udp_client.c
Normal file
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* udp_client.c
|
||||
*
|
||||
* Created on: 6 Nov 2020
|
||||
* Author: BramB
|
||||
*/
|
||||
|
||||
#include <ti/display/Display.h>
|
||||
#include <ti/drivers/net/wifi/slnetifwifi.h>
|
||||
#include <mqueue.h>
|
||||
|
||||
Display_Handle lookielookie;
|
||||
mqd_t UDPQueue;
|
||||
|
||||
#define UDP_PORT sl_Htons(12345) // Port gedefineerd in de UDP server
|
||||
#define UDP_IP SL_IPV4_VAL(192, 168, 0, 0) // IP gedefineerd in de UDP server
|
||||
|
||||
typedef struct {
|
||||
_u8 buffboi[128];
|
||||
_u8 len;
|
||||
} UDPMsgQueue;
|
||||
|
||||
void UDPSend (char msg[128], int len) {
|
||||
UDPMsgQueue Msg;
|
||||
uint8_t i;
|
||||
for(i=0; i<128; i++){
|
||||
Msg.buffboi[i] = msg[i];
|
||||
}
|
||||
Msg.len = len;
|
||||
mq_send(UDPQueue, (const char*)&Msg.buffboi, sizeof(UDPMsgQueue), 0);
|
||||
}
|
||||
|
||||
void* UDPClientTask(void *args) {
|
||||
_i16 sokken = sl_Socket(SL_AF_INET, SL_SOCK_DGRAM, SL_IPPROTO_UDP);
|
||||
if (sokken < 0) {
|
||||
while(1);
|
||||
}
|
||||
|
||||
SlSockAddrIn_t clientAddress = {SL_AF_INET, UDP_PORT, sl_Htonl(SL_INADDR_ANY)};
|
||||
SlSockAddrIn_t serverAddress = {SL_AF_INET, UDP_PORT, UDP_IP};
|
||||
|
||||
_i16 error = sl_Connect(sokken, (SlSockAddr_t *)&serverAddress, sizeof(clientAddress));
|
||||
if (error < 0) {
|
||||
while(1);
|
||||
}
|
||||
|
||||
_u8 buffering[100];
|
||||
|
||||
_i16 sokkenAan = sl_Recv(sokken, &buffering, 100, 0);
|
||||
if (error < 0) {
|
||||
while(1);
|
||||
}
|
||||
Display_printf(lookielookie, 0, 0, "Ontvangen van server: %s", buffering);
|
||||
|
||||
mq_attr attr;
|
||||
attr.mq_maxmsg = 10;
|
||||
attr.mq_msgsize = sizeof(UDPMsgQueue);
|
||||
UDPQueue = mq_open("UDPQueue", O_CREAT, 0, &attr);
|
||||
if(((int)UDPQueue) <= 0){
|
||||
while(1);
|
||||
}
|
||||
|
||||
UDPMsgQueue queueElement;
|
||||
|
||||
while(1) {
|
||||
mq_receive(UDPQueue, (char*)&queueElement, sizeof(UDPMsgQueue), NULL);
|
||||
|
||||
error = sl_Send(sokken, &queueElement.buffboi, queueElement.len, NULL);
|
||||
if (error < 0) {
|
||||
while(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user