Skip to content

Commit 19fed64

Browse files
author
Maksymilian Wojczuk
committed
Wakaaama
Cleaned up wakaama custom sources
1 parent d868339 commit 19fed64

File tree

15 files changed

+393
-375
lines changed

15 files changed

+393
-375
lines changed

Inc/communication/lwip_helpers.h

Lines changed: 0 additions & 7 deletions
This file was deleted.

Middlewares/Third_Party/wakaama/client/connection.h renamed to Inc/communication/wakaama_client/connection.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,6 @@ void connection_free(connection_t * connList);
5050

5151
int connection_send(connection_t *connP, uint8_t * buffer, size_t length);
5252

53+
int createUDPSocket(int port, int addressFamily);
54+
5355
#endif
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef __WAKAAMA_H
2+
#define __WAKAAMA_H
3+
4+
#include "connection.h"
5+
#include "objects.h"
6+
#include "communication_config.h"
7+
8+
typedef struct
9+
{
10+
lwm2m_object_t * securityObjP;
11+
int sock;
12+
connection_t * connList;
13+
int addressFamily;
14+
} client_data_t;
15+
16+
//WAKAAMA
17+
static lwm2m_context_t *lwm2mContext = NULL;
18+
static lwm2m_object_t *objArray[OBJ_COUNT];
19+
static client_data_t data;
20+
static int q_reset = 0;
21+
22+
static void print_state(lwm2m_context_t * lwm2mH);
23+
void taskWakaama(void *socket);
24+
void * lwm2m_connect_server(uint16_t secObjInstID,
25+
void * userData);
26+
void lwm2m_close_connection(void * sessionH,
27+
void * userData);
28+
#endif

Makefile

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
######################################
1414
# target
1515
######################################
16-
TARGET = JtagProgrammer
16+
TARGET = RemoteProgrammer
1717

1818

1919
######################################
@@ -161,7 +161,7 @@ Src/stm32f4xx_it.c \
161161
Src/stm32f4xx_hal_msp.c \
162162
Src/communication/dbgu.c \
163163
Src/communication/term_io.c \
164-
Src/communication/lwip_helpers.c
164+
Src/communication/wakaama_client/wakaama.c
165165

166166
# ASM sources
167167
ASM_SOURCES = \
@@ -191,19 +191,17 @@ Middlewares/Third_Party/wakaama/block1.c \
191191
Middlewares/Third_Party/wakaama/er-coap-13/er-coap-13.c
192192

193193
WAKAAMA_CUSTOM = \
194-
Middlewares/Third_Party/wakaama/platform/platform.c \
195-
Middlewares/Third_Party/wakaama/client/connection.c \
196-
Middlewares/Third_Party/wakaama/client/objects/object_device.c \
197-
Middlewares/Third_Party/wakaama/client/objects/object_security.c \
198-
Middlewares/Third_Party/wakaama/client/objects/object_server.c \
199-
Middlewares/Third_Party/wakaama/client/objects/test_object.c
194+
Src/communication/wakaama_client/platform/platform.c \
195+
Src/communication/wakaama_client/connection.c \
196+
Src/communication/wakaama_client/objects/object_device.c \
197+
Src/communication/wakaama_client/objects/object_security.c \
198+
Src/communication/wakaama_client/objects/object_server.c \
199+
Src/communication/wakaama_client/objects/test_object.c
200200

201201

202202
WAKAAMA_INC = \
203203
-IMiddlewares/Third_Party/wakaama \
204204
-IMiddlewares/Third_Party/wakaama/er-coap-13 \
205-
-IMiddlewares/Third_Party/wakaama/client \
206-
-IMiddlewares/Third_Party/wakaama/client/objects
207205

208206

209207
WAKAAMA_SYMBOL = -DLWM2M_LITTLE_ENDIAN
@@ -285,7 +283,10 @@ C_INCLUDES = \
285283
-IMiddlewares/Third_Party/LwIP/src/include/posix/sys \
286284
-IMiddlewares/Third_Party/LwIP/system/arch \
287285
-IDrivers/CMSIS/Include \
288-
-IInc/communication
286+
-IInc/communication \
287+
-IInc/communication/wakaama_client \
288+
-IInc/communication/wakaama_client/objects \
289+
-Iconfig
289290

290291
C_INCLUDES += $(WAKAAMA_INC)
291292

Src/communication/lwip_helpers.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
#include "sockets.h"
77
#include "mem.h"
88

9-
int getUDPSocket(int port, int addressFamily){
9+
// TODO:
10+
int createUDPSocket(int port, int addressFamily){
1011
/* create a UDP socket */
1112
int socket;
1213
socket = lwip_socket(addressFamily, SOCK_DGRAM, 0);

Middlewares/Third_Party/wakaama/client/connection.c renamed to Src/communication/wakaama_client/connection.c

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,24 @@
2121
#include <ctype.h>
2222
#include "connection.h"
2323

24-
connection_t * connection_find(connection_t * connList, struct sockaddr_storage * addr, size_t addrLen)
25-
{
24+
connection_t * connection_find(connection_t * connList, struct sockaddr_storage * addr, size_t addrLen) {
2625
connection_t * connP;
2726

2827
connP = connList;
29-
while (connP != NULL)
30-
{
28+
while (connP != NULL) {
3129
if ((connP->addrLen == addrLen)
32-
&& (memcmp(&(connP->addr), addr, addrLen) == 0))
33-
{
30+
&& (memcmp(&(connP->addr), addr, addrLen) == 0)) {
3431
return connP;
3532
}
3633
connP = connP->next;
3734
}
38-
3935
return connP;
4036
}
4137

4238
connection_t * connection_new_incoming(connection_t * connList,
4339
int sock,
4440
struct sockaddr * addr,
45-
size_t addrLen)
46-
{
41+
size_t addrLen) {
4742
connection_t * connP;
4843

4944
connP = (connection_t *)lwm2m_malloc(sizeof(connection_t));
@@ -62,8 +57,7 @@ connection_t * connection_create(connection_t * connList,
6257
int sock,
6358
char * host,
6459
char * port,
65-
int addressFamily)
66-
{
60+
int addressFamily) {
6761
struct addrinfo hints;
6862
struct addrinfo *servinfo = NULL;
6963
struct addrinfo *p;
@@ -76,28 +70,24 @@ connection_t * connection_create(connection_t * connList,
7670
hints.ai_family = addressFamily;
7771
hints.ai_socktype = SOCK_DGRAM;
7872
int result = lwip_getaddrinfo(host, port, &hints, &servinfo);
79-
fprintf(stderr, "lwip_getaddrinfo %d\r\n", result);
8073
if (result != 0 || servinfo == NULL) {
74+
fprintf(stderr, "lwip_getaddrinfo returned: %d\r\n", result);
8175
return NULL;
8276
}
8377
// we test the various addresses
8478
s = -1;
85-
for(p = servinfo ; p != NULL && s == -1 ; p = p->ai_next)
86-
{
79+
for(p = servinfo ; p != NULL && s == -1 ; p = p->ai_next) {
8780
s = lwip_socket(p->ai_family, p->ai_socktype, p->ai_protocol);
88-
if (s >= 0)
89-
{
81+
if (s >= 0) {
9082
sa = p->ai_addr;
9183
sl = p->ai_addrlen;
92-
if (-1 == lwip_connect(s, p->ai_addr, p->ai_addrlen))
93-
{
84+
if (-1 == lwip_connect(s, p->ai_addr, p->ai_addrlen)) {
9485
lwip_close(s);
9586
s = -1;
9687
}
9788
}
9889
}
99-
if (s >= 0)
100-
{
90+
if (s >= 0) {
10191
connP = connection_new_incoming(connList, sock, sa, sl);
10292
lwip_close(s);
10393
}
@@ -108,10 +98,8 @@ connection_t * connection_create(connection_t * connList,
10898
return connP;
10999
}
110100

111-
void connection_free(connection_t * connList)
112-
{
113-
while (connList != NULL)
114-
{
101+
void connection_free(connection_t * connList) {
102+
while (connList != NULL) {
115103
connection_t * nextP;
116104

117105
nextP = connList->next;
@@ -123,8 +111,7 @@ void connection_free(connection_t * connList)
123111

124112
int connection_send(connection_t *connP,
125113
uint8_t * buffer,
126-
size_t length)
127-
{
114+
size_t length) {
128115
int nbSent;
129116
size_t offset;
130117

@@ -134,14 +121,11 @@ int connection_send(connection_t *connP,
134121

135122
s[0] = 0;
136123

137-
if (AF_INET == connP->addr.sin6_family)
138-
{
124+
if (AF_INET == connP->addr.sin6_family) {
139125
struct sockaddr_in *saddr = (struct sockaddr_in *)&connP->addr;
140126
inet_ntop(saddr->sin_family, &saddr->sin_addr, s, INET6_ADDRSTRLEN);
141-
port = saddr->sin_port;
142-
}
143-
else if (AF_INET6 == connP->addr.sin6_family)
144-
{
127+
port = saddr->sin_port; }
128+
else if (AF_INET6 == connP->addr.sin6_family) {
145129
struct sockaddr_in6 *saddr = (struct sockaddr_in6 *)&connP->addr;
146130
inet_ntop(saddr->sin6_family, &saddr->sin6_addr, s, INET6_ADDRSTRLEN);
147131
port = saddr->sin6_port;
@@ -153,8 +137,7 @@ int connection_send(connection_t *connP,
153137
#endif
154138

155139
offset = 0;
156-
while (offset != length)
157-
{
140+
while (offset != length) {
158141
nbSent = lwip_sendto(connP->sock, buffer + offset, length - offset, 0, (struct sockaddr *)&(connP->addr), connP->addrLen);
159142
if (nbSent == -1) return -1;
160143
offset += nbSent;
@@ -165,19 +148,16 @@ int connection_send(connection_t *connP,
165148
uint8_t lwm2m_buffer_send(void * sessionH,
166149
uint8_t * buffer,
167150
size_t length,
168-
void * userdata)
169-
{
151+
void * userdata) {
170152
connection_t * connP = (connection_t*) sessionH;
171153

172-
if (connP == NULL)
173-
{
174-
fprintf(stderr, "#> failed sending %lu bytes, missing connection\r\n", length);
154+
if (connP == NULL) {
155+
fprintf(stderr, "#> failed sending %u bytes, missing connection\r\n", length);
175156
return COAP_500_INTERNAL_SERVER_ERROR ;
176157
}
177158

178-
if (-1 == connection_send(connP, buffer, length))
179-
{
180-
fprintf(stderr, "#> failed sending %lu bytes\r\n", length);
159+
if (-1 == connection_send(connP, buffer, length)) {
160+
fprintf(stderr, "#> failed sending %u bytes\r\n", length);
181161
return COAP_500_INTERNAL_SERVER_ERROR ;
182162
}
183163

@@ -186,7 +166,32 @@ uint8_t lwm2m_buffer_send(void * sessionH,
186166

187167
bool lwm2m_session_is_equal(void * session1,
188168
void * session2,
189-
void * userData)
190-
{
169+
void * userData) {
191170
return (session1 == session2);
192171
}
172+
173+
int createUDPSocket(int port, int addressFamily){
174+
/* create a UDP socket */
175+
int socket;
176+
socket = lwip_socket(addressFamily, SOCK_DGRAM, 0);
177+
if (socket < 0 ) {
178+
printf("cannot create socket\r\n");
179+
return -1;
180+
}
181+
if(fcntl(socket, F_SETFL, O_NONBLOCK) == -1){
182+
printf("cannot set socket O_NONBLOCK\r\n");
183+
return -1;
184+
}
185+
struct sockaddr_in clientAddress;
186+
clientAddress.sin_family = addressFamily;
187+
clientAddress.sin_port = htons(port);
188+
clientAddress.sin_addr.s_addr = INADDR_ANY;
189+
190+
/* bind */
191+
if (lwip_bind(socket, (struct sockaddr *) &clientAddress, sizeof (clientAddress)) < 0) {
192+
printf("cannot bind socket\r\n");
193+
lwip_close(socket);
194+
return -1;
195+
}
196+
return socket;
197+
}

Middlewares/Third_Party/wakaama/client/objects/object_device.c renamed to Src/communication/wakaama_client/objects/object_device.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
#include <stdlib.h>
6565
#include <string.h>
6666
#include <ctype.h>
67-
#include "client_config.h"
67+
#include "communication_config.h"
6868

6969

7070
// Resource Id's:

Middlewares/Third_Party/wakaama/client/objects/object_security.c renamed to Src/communication/wakaama_client/objects/object_security.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
#include <string.h>
4747
#include <stdio.h>
4848

49-
#include "client_config.h"
49+
#include "communication_config.h"
5050

5151

5252
typedef struct _security_instance_

Middlewares/Third_Party/wakaama/client/objects/object_server.c renamed to Src/communication/wakaama_client/objects/object_server.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,4 +496,5 @@ void clean_server_object(lwm2m_object_t * object)
496496
object->instanceList = object->instanceList->next;
497497
lwm2m_free(serverInstance);
498498
}
499+
lwm2m_free(object);
499500
}

0 commit comments

Comments
 (0)